Skip to contentSkip to conversation

dma-debug: Avoid uninitialized mapping dumps

PATCH New Avi Weiss 1 participant 3 messages Nov 19

Subsystems (2)
lore
Share & tools
lore

Avi Weiss

author
#0 lore raw

v2 cover dma-debug: Avoid uninitialized mapping dumps

Booting with dma_debug=off leaves the DMA-debug hash buckets uninitialized, but the debugfs mapping dump is still registered. Reading dma-api/dump then traverses an uninitialized list and causes a NULL pointer dereference in dump_show().

Patch 1 skips debugfs registration when DMA debugging was not initialized. Patch 2 removes the obsolete debug_dma_dump_mappings() helper and its only caller in the legacy AMD GART IOMMU code.

Validation on x86-64 QEMU:
- baseline with dma_debug=off - NULL dereference in dump_show()
- patched with dma_debug=off - dma-api debugfs directory absent
- patched with DMA debugging enabled - mapping dump readable
- both objects compiled successfully with W=1

Changes since v1:
- guard debugfs registration instead of the dump paths, as suggested by Anthony Iliopoulos
- remove the obsolete mapping dump helper and its AMD GART caller (due to feedback from Robin Murphy and Marek Szyprowski)

v1 discussion:
https://lore-kernel.gnuweeb.org/linux-iommu/05c05ec4-b321-49d4-acf7-b9fa4223dba9%40samsung.com/T/

Avi Weiss (2):
dma-debug: Skip debugfs registration when uninitialized
dma-debug: Remove obsolete mapping dump helper

 arch/x86/kernel/amd_gart_64.c |  1 -
 include/linux/dma-map-ops.h   |  4 ----
 kernel/dma/debug.c            | 40 ++++++-----------------------------
 3 files changed, 6 insertions(+), 39 deletions(-)

base-commit: 97d73c8251226cf27cc6656b4c542869bb8c69fa

Signature · 1 line
2.43.0
author
#1 lore raw

PATCH v2 1/2 dma-debug: Skip debugfs registration when uninitialized

dma_debug_init() returns without initializing the hash buckets when DMA API debugging is disabled at boot. dma_debug_fs_init() still registers the debugfs files, allowing a read of dma-api/dump to traverse an uninitialized list and dereference NULL.

Skip debugfs registration unless dma_debug_initialized is set. Keep the files available if debugging disables itself after initialization, so previously recorded mappings remain accessible.

Fixes 173735c346c4 ("dma-debug: fix debugfs initialization order")

---
v2:
- check dma_debug_initialized before registering the debugfs files instead of guarding both dump functions

 kernel/dma/debug.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
kernel/dma/debug.c +6 -1
index 36c1f7cbb2ae..6e554dcfc9b9 100644
@@ -839,7 +839,12 @@ static const struct file_operations filter_fops = {
 
 static int __init dma_debug_fs_init(void)
 {
-	struct dentry *dentry = debugfs_create_dir("dma-api", NULL);
+	struct dentry *dentry;
+
+	if (!dma_debug_initialized)
+		return 0;
+
+	dentry = debugfs_create_dir("dma-api", NULL);
 
 	debugfs_create_bool("disabled", 0444, dentry, &global_disable);
 	debugfs_create_u32("error_count", 0444, dentry, &error_count);
Signature · 1 line
2.43.0
author
#2 lore raw

PATCH v2 2/2 dma-debug: Remove obsolete mapping dump helper

In reply to Avi Weiss

debug_dma_dump_mappings() is only called by the legacy AMD GART IOMMU leak diagnostic. The helper traverses DMA-debug hash buckets even when DMA debugging was never initialized.

Remove the helper, its declarations and the GART call. Keep the GART stack trace for diagnosing IOMMU exhaustion.

---
 arch/x86/kernel/amd_gart_64.c |  1 -
 include/linux/dma-map-ops.h   |  4 ----
 kernel/dma/debug.c            | 33 ---------------------------------
 3 files changed, 38 deletions(-)
arch/x86/kernel/amd_gart_64.c +0 -1
index b5f1f031d45b..965844299a1b 100644
@@ -157,7 +157,6 @@ static void dump_leak(void)
 	dump = 1;
 
 	show_stack(NULL, NULL, KERN_ERR);
-	debug_dma_dump_mappings(NULL);
 }
 #endif
 
include/linux/dma-map-ops.h +0 -4
index afaf2c12189b..eb32ed923455 100644
@@ -418,14 +418,10 @@ static inline void arch_teardown_dma_ops(struct device *dev)
 
 #ifdef CONFIG_DMA_API_DEBUG
 void dma_debug_add_bus(const struct bus_type *bus);
-void debug_dma_dump_mappings(struct device *dev);
 #else
 static inline void dma_debug_add_bus(const struct bus_type *bus)
 {
 }
-static inline void debug_dma_dump_mappings(struct device *dev)
-{
-}
 #endif /* CONFIG_DMA_API_DEBUG */
 
 extern const struct dma_map_ops dma_dummy_ops;
kernel/dma/debug.c +0 -33
index 6e554dcfc9b9..f1b6f7b9aa14 100644
@@ -535,39 +535,6 @@ static void active_cacheline_remove(struct dma_debug_entry *entry)
 	spin_unlock_irqrestore(&radix_lock, flags);
 }
 
-/*
- * Dump mappings entries on kernel space for debugging purposes
- */
-void debug_dma_dump_mappings(struct device *dev)
-{
-	int idx;
-	phys_addr_t cln;
-
-	for (idx = 0; idx < HASH_SIZE; idx++) {
-		struct hash_bucket *bucket = &dma_entry_hash[idx];
-		struct dma_debug_entry *entry;
-		unsigned long flags;
-
-		spin_lock_irqsave(&bucket->lock, flags);
-		list_for_each_entry(entry, &bucket->list, list) {
-			if (!dev || dev == entry->dev) {
-				cln = to_cacheline_number(entry);
-				dev_info(entry->dev,
-					 "%s idx %d P=%pa D=%llx L=%llx cln=%pa %s %s attrs=0x%lx\n",
-					 type2name[entry->type], idx,
-					 &entry->paddr, entry->dev_addr,
-					 entry->size, &cln,
-					 dir2name[entry->direction],
-					 maperr2str[entry->map_err_type],
-					 entry->attrs);
-			}
-		}
-		spin_unlock_irqrestore(&bucket->lock, flags);
-
-		cond_resched();
-	}
-}
-
 /*
  * Dump mappings entries on user space via debugfs
  */
Signature · 1 line
2.43.0