[PATCH v2 1/2] dma-debug: skip irrelevant buckets for partial syncs

From: Juanlu Herrero

Date: Fri Dec 04 2026 - 18:07:44 EST


Partial syncs may search many hash buckets. Track a conservative size
bound per bucket and skip those unable to contain the requested range.

The bound grows on insertion and resets when the bucket empties; ordered
map/sync/unmap lifetimes keep it conservative. Bucket locking orders
updates even when later insertions do not change the bound.

Use the full lookup for overflowing ranges and retry failed searches
that skipped buckets to preserve diagnostic selection.

Signed-off-by: Juanlu Herrero <juanlu@xxxxxxxxxxxx>
---
kernel/dma/debug.c | 56 +++++++++++++++++++++++++++++++++++++++++++++---------
1 file changed, 47 insertions(+), 9 deletions(-)

diff --git a/kernel/dma/debug.c b/kernel/dma/debug.c
index f5605ac800fbe..e9d72e7dc5e82 100644
--- a/kernel/dma/debug.c
+++ b/kernel/dma/debug.c
@@ -90,6 +90,8 @@ typedef bool (*match_fn)(struct dma_debug_entry *, struct dma_debug_entry *);
struct hash_bucket {
struct list_head list;
spinlock_t lock;
+ /* Upper bound on live mapping sizes; reset when the bucket empties. */
+ unsigned long max_entry_size;
};

/* Hash list to save the allocated dma addresses */
@@ -354,24 +356,55 @@ static struct dma_debug_entry *bucket_find_contain(struct hash_bucket **bucket,
struct dma_debug_entry *ref,
unsigned long *flags)
{
-
struct dma_debug_entry *entry, index = *ref;
+ u64 min_offset = (index.dev_addr & ((1ULL << HASH_FN_SHIFT) - 1)) + 1;
int limit = min(HASH_SIZE, (index.dev_addr >> HASH_FN_SHIFT) + 1);
+ /*
+ * Wrapped ranges and misses after skipping buckets need the full
+ * lookup to preserve diagnostic selection.
+ */
+ bool use_bound = ref->size <= U64_MAX - ref->dev_addr;
+ bool skipped = false;
+
+retry:
+ entry = __hash_bucket_find(*bucket, ref, containing_match);
+ if (entry)
+ return entry;
+ put_hash_bucket(*bucket, *flags);

- for (int i = 0; i < limit; i++) {
+ for (int i = 1; i < limit; i++) {
+ unsigned long max_entry_size;
+
+ index.dev_addr -= (1 << HASH_FN_SHIFT);
+ *bucket = &dma_entry_hash[hash_fn(&index)];
+ if (use_bound) {
+ max_entry_size = READ_ONCE((*bucket)->max_entry_size);
+ if (max_entry_size < ref->size ||
+ max_entry_size - ref->size < min_offset) {
+ skipped = true;
+ goto next;
+ }
+ }
+
+ *bucket = get_hash_bucket(&index, flags);
entry = __hash_bucket_find(*bucket, ref, containing_match);

if (entry)
return entry;

- /*
- * Nothing found, go back a hash bucket
- */
put_hash_bucket(*bucket, *flags);
- index.dev_addr -= (1 << HASH_FN_SHIFT);
- *bucket = get_hash_bucket(&index, flags);
+next:
+ min_offset += 1ULL << HASH_FN_SHIFT;
}

+ *bucket = get_hash_bucket(ref, flags);
+ if (skipped) {
+ index = *ref;
+ min_offset = (index.dev_addr & ((1ULL << HASH_FN_SHIFT) - 1)) + 1;
+ use_bound = false;
+ skipped = false;
+ goto retry;
+ }
return NULL;
}

@@ -382,14 +415,19 @@ static void hash_bucket_add(struct hash_bucket *bucket,
struct dma_debug_entry *entry)
{
list_add_tail(&entry->list, &bucket->list);
+ if (entry->size > bucket->max_entry_size)
+ WRITE_ONCE(bucket->max_entry_size, entry->size);
}

/*
* Remove entry from a hash bucket list
*/
-static void hash_bucket_del(struct dma_debug_entry *entry)
+static void hash_bucket_del(struct hash_bucket *bucket,
+ struct dma_debug_entry *entry)
{
list_del(&entry->list);
+ if (list_empty(&bucket->list))
+ WRITE_ONCE(bucket->max_entry_size, 0);
}

/*
@@ -1072,7 +1110,7 @@ static void check_unmap(struct dma_debug_entry *ref)
}
#undef DMA_ATTR_UNMAP_VALID

- hash_bucket_del(entry);
+ hash_bucket_del(bucket, entry);
put_hash_bucket(bucket, flags);

/*

--
2.53.1-Meta