From: ZhangPeng <[email protected]>
Introduce a new debugfs interface io_tlb_transient_nslabs. The device
driver can create a new swiotlb transient memory pool once default
memory pool is full. To export the swiotlb transient memory pool usage
via debugfs would help the user estimate the size of transient swiotlb
memory pool or analyze device driver memory leak issue.
Signed-off-by: ZhangPeng <[email protected]>
---
include/linux/swiotlb.h | 3 +++
kernel/dma/swiotlb.c | 46 +++++++++++++++++++++++++++++++++++++++++
2 files changed, 49 insertions(+)
diff --git a/include/linux/swiotlb.h b/include/linux/swiotlb.h
index ecde0312dd52..ea23097e351f 100644
--- a/include/linux/swiotlb.h
+++ b/include/linux/swiotlb.h
@@ -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
};
diff --git a/kernel/dma/swiotlb.c b/kernel/dma/swiotlb.c
index b079a9a8e087..77974cea3e69 100644
--- a/kernel/dma/swiotlb.c
+++ b/kernel/dma/swiotlb.c
@@ -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)
--
2.25.1
Kindly ping.
Could someone please review this patch? Comments are welcome.
Thanks.
On 2024/1/9 15:04, Peng Zhang wrote:
> From: ZhangPeng <[email protected]>
>
> Introduce a new debugfs interface io_tlb_transient_nslabs. The device
> driver can create a new swiotlb transient memory pool once default
> memory pool is full. To export the swiotlb transient memory pool usage
> via debugfs would help the user estimate the size of transient swiotlb
> memory pool or analyze device driver memory leak issue.
>
> Signed-off-by: ZhangPeng <[email protected]>
> ---
> include/linux/swiotlb.h | 3 +++
> kernel/dma/swiotlb.c | 46 +++++++++++++++++++++++++++++++++++++++++
> 2 files changed, 49 insertions(+)
>
> diff --git a/include/linux/swiotlb.h b/include/linux/swiotlb.h
> index ecde0312dd52..ea23097e351f 100644
> --- a/include/linux/swiotlb.h
> +++ b/include/linux/swiotlb.h
> @@ -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
> };
>
> diff --git a/kernel/dma/swiotlb.c b/kernel/dma/swiotlb.c
> index b079a9a8e087..77974cea3e69 100644
> --- a/kernel/dma/swiotlb.c
> +++ b/kernel/dma/swiotlb.c
> @@ -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)
--
Best Regards,
Peng
Thanks,
added to the dma-mapping for-next tree.