dma-mapping updates for Linux 6.9

- leak pages on dma_set_decrypted() failure (Rick Edgecombe)
  - add a new swiotlb debugfs file (ZhangPeng)
 -----BEGIN PGP SIGNATURE-----
 
 iQI/BAABCgApFiEEgdbnc3r/njty3Iq9D55TZVIEUYMFAmXvBrgLHGhjaEBsc3Qu
 ZGUACgkQD55TZVIEUYNbfg/+N9KB5HnthckS+GDEkpg3Fh61w8C0h7Fe4EC/CKfH
 eRnblFTXswDDaUp0+2g5pxLAa+BLyVgoBLkjqEIU3ijOOcZ0j5XlNdg1PEeoS7wb
 4pajWYQQYHHoGOsa8gHpPDfLSsw4dt7IahJ8+vN79AwqB8Hg0bTCE0KPlr3XsrxR
 zA5Ufyj/7PJYQcyXM38Za364GPBbecYNmLc2ovBJj0jfF0lW9+iY6v3mODWuKYXc
 rmejgOzv1kCgHCBXAiYr8WEEeYtlaZNOFwmqC0NPG6gyPTzlzELGq5Wq+/OK8jT7
 wlAZWZL6ggKuQDFNW71E4EKlEZIthMF4KJE35xkYORaQaOZ+/EfaZ6oiO/Y4zhzd
 Rxp8a5PEAKDsuY2sI+JArG86AEfxwFmoivihNagvxF9PVflpCgUZukZ/dBv5n21R
 bUpbyvf5ZdcHX1BRdfFEk5imj0gbzWyo6Aqmze9K61g/C65yYnXOqlIg+rjQT3Mm
 NHFxBYS+b1ZxFH1NhNIqvsDGGITO/6MKV206Im+c0buxwacl4/kw2kOIoWqgemQy
 zgL71/hXYuRQuQLw2B55tmR0k93kyRrq1x5TL9+xPpOA4xhACgD3s29GSvYZ9WEn
 +3dzD7rrBkGwoK8v6N4qvMwai9NEvgGMLMySfz3MATLsatFCHJVT4VQ8fnX1TpDV
 6pQ=
 =aCDZ
 -----END PGP SIGNATURE-----

Merge tag 'dma-mapping-6.9-2024-03-11' of git://git.infradead.org/users/hch/dma-mapping

Pull dma-mapping updates from Christoph Hellwig:

 - fix leaked pages on dma_set_decrypted() failure (Rick Edgecombe)

 - add a new swiotlb debugfs file (ZhangPeng)

* tag 'dma-mapping-6.9-2024-03-11' of git://git.infradead.org/users/hch/dma-mapping:
  dma-direct: Leak pages on dma_set_decrypted() failure
  swiotlb: add debugfs to track swiotlb transient pool usage
This commit is contained in:
Linus Torvalds 2024-03-13 09:23:21 -07:00
commit 7d62cb2a59
3 changed files with 54 additions and 4 deletions

View File

@ -120,6 +120,8 @@ struct io_tlb_pool {
* debugfs.
* @used_hiwater: The high water mark for total_used. Used only for reporting
* in debugfs.
* @transient_nslabs: The total number of slots in all transient pools that
* are currently used across all areas.
*/
struct io_tlb_mem {
struct io_tlb_pool defpool;
@ -137,6 +139,7 @@ struct io_tlb_mem {
#ifdef CONFIG_DEBUG_FS
atomic_long_t total_used;
atomic_long_t used_hiwater;
atomic_long_t transient_nslabs;
#endif
};

View File

@ -286,7 +286,7 @@ void *dma_direct_alloc(struct device *dev, size_t size,
} else {
ret = page_address(page);
if (dma_set_decrypted(dev, ret, size))
goto out_free_pages;
goto out_leak_pages;
}
memset(ret, 0, size);
@ -307,6 +307,8 @@ out_encrypt_pages:
out_free_pages:
__dma_direct_free_pages(dev, page, size);
return NULL;
out_leak_pages:
return NULL;
}
void dma_direct_free(struct device *dev, size_t size,
@ -367,12 +369,11 @@ struct page *dma_direct_alloc_pages(struct device *dev, size_t size,
ret = page_address(page);
if (dma_set_decrypted(dev, ret, size))
goto out_free_pages;
goto out_leak_pages;
memset(ret, 0, size);
*dma_handle = phys_to_dma_direct(dev, page_to_phys(page));
return page;
out_free_pages:
__dma_direct_free_pages(dev, page, size);
out_leak_pages:
return NULL;
}

View File

@ -956,6 +956,28 @@ static void dec_used(struct io_tlb_mem *mem, unsigned int nslots)
}
#endif /* CONFIG_DEBUG_FS */
#ifdef CONFIG_SWIOTLB_DYNAMIC
#ifdef CONFIG_DEBUG_FS
static void inc_transient_used(struct io_tlb_mem *mem, unsigned int nslots)
{
atomic_long_add(nslots, &mem->transient_nslabs);
}
static void dec_transient_used(struct io_tlb_mem *mem, unsigned int nslots)
{
atomic_long_sub(nslots, &mem->transient_nslabs);
}
#else /* !CONFIG_DEBUG_FS */
static void inc_transient_used(struct io_tlb_mem *mem, unsigned int nslots)
{
}
static void dec_transient_used(struct io_tlb_mem *mem, unsigned int nslots)
{
}
#endif /* CONFIG_DEBUG_FS */
#endif /* CONFIG_SWIOTLB_DYNAMIC */
/**
* swiotlb_search_pool_area() - search one memory area in one pool
* @dev: Device which maps the buffer.
@ -1170,6 +1192,7 @@ static int swiotlb_find_slots(struct device *dev, phys_addr_t orig_addr,
spin_lock_irqsave(&dev->dma_io_tlb_lock, flags);
list_add_rcu(&pool->node, &dev->dma_io_tlb_pools);
spin_unlock_irqrestore(&dev->dma_io_tlb_lock, flags);
inc_transient_used(mem, pool->nslabs);
found:
WRITE_ONCE(dev->dma_uses_io_tlb, true);
@ -1415,6 +1438,7 @@ static bool swiotlb_del_transient(struct device *dev, phys_addr_t tlb_addr)
dec_used(dev->dma_io_tlb_mem, pool->nslabs);
swiotlb_del_pool(dev, pool);
dec_transient_used(dev->dma_io_tlb_mem, pool->nslabs);
return true;
}
@ -1557,6 +1581,23 @@ phys_addr_t default_swiotlb_limit(void)
}
#ifdef CONFIG_DEBUG_FS
#ifdef CONFIG_SWIOTLB_DYNAMIC
static unsigned long mem_transient_used(struct io_tlb_mem *mem)
{
return atomic_long_read(&mem->transient_nslabs);
}
static int io_tlb_transient_used_get(void *data, u64 *val)
{
struct io_tlb_mem *mem = data;
*val = mem_transient_used(mem);
return 0;
}
DEFINE_DEBUGFS_ATTRIBUTE(fops_io_tlb_transient_used, io_tlb_transient_used_get,
NULL, "%llu\n");
#endif /* CONFIG_SWIOTLB_DYNAMIC */
static int io_tlb_used_get(void *data, u64 *val)
{
@ -1605,6 +1646,11 @@ static void swiotlb_create_debugfs_files(struct io_tlb_mem *mem,
&fops_io_tlb_used);
debugfs_create_file("io_tlb_used_hiwater", 0600, mem->debugfs, mem,
&fops_io_tlb_hiwater);
#ifdef CONFIG_SWIOTLB_DYNAMIC
atomic_long_set(&mem->transient_nslabs, 0);
debugfs_create_file("io_tlb_transient_nslabs", 0400, mem->debugfs,
mem, &fops_io_tlb_transient_used);
#endif
}
static int __init swiotlb_create_default_debugfs(void)