From: Juanlu Herrero <juanlu@fastmail.com>
To: Robin Murphy <robin.murphy@arm.com>
Cc: Andrew Lunn <andrew+netdev@lunn.ch>,
"David S. Miller" <davem@davemloft.net>,
Jakub Kicinski <kuba@kernel.org>,
Paolo Abeni <pabeni@redhat.com>, Shuah Khan <shuah@kernel.org>,
Marek Szyprowski <m.szyprowski@samsung.com>,
Eric Dumazet <edumazet@kernel.org>,
netdev@vger.kernel.org, linux-kselftest@vger.kernel.org,
linux-kernel@vger.kernel.org, iommu@lists.linux.dev
Subject: Re: [PATCH 2/3] dma-debug: skip irrelevant buckets for partial syncs
Date: Thu, 3 Dec 2026 15:44:02 -0800 [thread overview]
Message-ID: <axH8ntfsFgl-oNoI@devvm8761.dkl0.facebook.com> (raw)
In-Reply-To: <41001cf2-6304-45ff-977f-ac1b66ecf559@arm.com>
On Thu, Dec 03, 2026 at 05:38:58PM +0000, Robin Murphy wrote:
> On 2026-11-10 9:36 pm, Juanlu Herrero wrote:
> > Partial syncs may start after a mapping's first hash bucket. Finding the
> > mapping then requires locking earlier buckets.
> >
> > Keep a conservative size bound per bucket and skip those that cannot
> > span the sync range. Raise the bound on insertion and reset it when the
> > bucket empties. Stale larger bounds only add searches, so removal needs
> > no list rescan to update the bound.
> >
> > This relies on ordered map publication before sync and unmapping after
> > sync. The bucket lock carries earlier bound updates through later
> > insertions even when the bound is unchanged.
>
> Doesn't this then run the risk of then mis-reporting a sync/unmap with a
> larger-than-mapped size? The whole point of dma-debug is to catch bad API
> usage, so obviously it cannot rely on callers being correct. It is an
> explicit choice for developers who want to check correctness and accept the
> massive performance impact of doing so, so any reduction of
> robustness/coverage for the sake of making the inherently slow thing a
> little less slow seems like a wrong move, and rather missing the point.
>
> Thanks,
> Robin.
> > Signed-off-by: Juanlu Herrero <juanlu@fastmail.com>
> > ---
> > kernel/dma/debug.c | 49 +++++++++++++++++++++++++++++++++++++++----------
> > 1 file changed, 39 insertions(+), 10 deletions(-)
> >
> > diff --git a/kernel/dma/debug.c b/kernel/dma/debug.c
> > index 4cb5f86a7228a..8dad45d0a4360 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,27 +356,49 @@ 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);
> > - for (int i = 0; i < limit; i++) {
> > + entry = __hash_bucket_find(*bucket, ref, containing_match);
> > + if (entry)
> > + return entry;
> > + put_hash_bucket(*bucket, *flags);
> > +
> > + 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)];
> > + /*
> > + * Writers serialize updates with the bucket lock. For any entry
> > + * that can validly match this sync, publishing the mapping is
> > + * ordered before the sync, including a cross-CPU handoff, and
> > + * unmapping is ordered after it. The bound only grows while the
> > + * bucket is nonempty, so it cannot become smaller than a live
> > + * entry. Removing an unrelated mapping may leave a larger bound,
> > + * which only causes an unnecessary search.
> > + */
> > + max_entry_size = READ_ONCE((*bucket)->max_entry_size);
> > + /* The mapping must span the distance back to this bucket. */
> > + if (max_entry_size < ref->size ||
> > + max_entry_size - ref->size < min_offset)
> > + 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);
> > return NULL;
> > }
> > -
> > /*
> > * Add an entry to a hash bucket
> > */
> > @@ -382,14 +406,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);
> > }
> > /*
> > @@ -1100,7 +1129,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);
> > /*
> >
>
Hi Robin,
thanks for raising those concerns. I checked the oversized-request
behavior and unmap still uses the same exact-address lookup and map/unmap
size comparison. For oversized syncs, containing_match() already rejects
the mapping and error reported is still "memory it has not allocated."
With LLM assistance, I A/B tested the patch and all instances reported
the same error count while only in 1 case (when the requested sync's
address + size overflows) reports a different error ("outside allocated
range" vs "memory it has not allocated"). I can add in v2, kunit tests
covering oversized sync/unmap!
Do you consider the diagnostic change a regression that needs
addressing?
--
Juanlu Herrero
next prev parent reply other threads:[~2026-12-03 23:44 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
[not found] <CGME20261110213736eucas1p1eab83ca5602049b731a5e4937b7d8c4f@eucas1p1.samsung.com>
2026-11-10 21:36 ` [PATCH 0/3] dma-debug: skip irrelevant buckets for partial syncs Juanlu Herrero
2026-11-10 21:36 ` [PATCH 1/3] selftests: net: make iou-zcrx area size configurable Juanlu Herrero
2026-11-10 21:36 ` [PATCH 2/3] dma-debug: skip irrelevant buckets for partial syncs Juanlu Herrero
2026-12-03 17:38 ` Robin Murphy
2026-12-03 23:44 ` Juanlu Herrero [this message]
2026-11-10 21:36 ` [PATCH 3/3] dma-debug: add bucket bound lifetime coverage Juanlu Herrero
2026-12-03 12:17 ` [PATCH 0/3] dma-debug: skip irrelevant buckets for partial syncs Marek Szyprowski
2026-12-03 17:29 ` Juanlu Herrero
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=axH8ntfsFgl-oNoI@devvm8761.dkl0.facebook.com \
--to=juanlu@fastmail.com \
--cc=andrew+netdev@lunn.ch \
--cc=davem@davemloft.net \
--cc=edumazet@kernel.org \
--cc=iommu@lists.linux.dev \
--cc=kuba@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-kselftest@vger.kernel.org \
--cc=m.szyprowski@samsung.com \
--cc=netdev@vger.kernel.org \
--cc=pabeni@redhat.com \
--cc=robin.murphy@arm.com \
--cc=shuah@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox