dma-debug: improve search for partial syncs
authorRobin Murphy <robin.murphy@arm.com>
Mon, 15 Aug 2022 19:28:40 +0000 (20:28 +0100)
committerChristoph Hellwig <hch@lst.de>
Wed, 7 Sep 2022 08:38:16 +0000 (10:38 +0200)
When bucket_find_contains() tries to find the original entry for a
partial sync, it manages to constrain its search in a way that is both
too restrictive and not restrictive enough. A driver which only uses
single mappings rather than scatterlists might not set max_seg_size, but
could still technically perform a partial sync at an offset of more than
64KB into a sufficiently large mapping, so we could stop searching too
early before reaching a legitimate entry. Conversely, if no valid entry
is present and max_range is large enough, we can pointlessly search
buckets that we've already searched, or that represent an impossible
wrapping around the bottom of the address space. At worst, the
(legitimate) case of max_seg_size == UINT_MAX can make the loop
infinite.

Replace the fragile and frankly hard-to-follow "range" logic with a
simple counted loop for the number of possible hash buckets below the
given address.

Reported-by: Yunfei Wang <yf.wang@mediatek.com>
Signed-off-by: Robin Murphy <robin.murphy@arm.com>
Signed-off-by: Christoph Hellwig <hch@lst.de>
kernel/dma/debug.c

index 2caafd13f8aac27f92e0675d94050175c45d585d..18c93c2276cae436c89341e11830cb694fc87e8f 100644 (file)
@@ -350,11 +350,10 @@ static struct dma_debug_entry *bucket_find_contain(struct hash_bucket **bucket,
                                                   unsigned long *flags)
 {
 
-       unsigned int max_range = dma_get_max_seg_size(ref->dev);
        struct dma_debug_entry *entry, index = *ref;
-       unsigned int range = 0;
+       int limit = min(HASH_SIZE, (index.dev_addr >> HASH_FN_SHIFT) + 1);
 
-       while (range <= max_range) {
+       for (int i = 0; i < limit; i++) {
                entry = __hash_bucket_find(*bucket, ref, containing_match);
 
                if (entry)
@@ -364,7 +363,6 @@ static struct dma_debug_entry *bucket_find_contain(struct hash_bucket **bucket,
                 * Nothing found, go back a hash bucket
                 */
                put_hash_bucket(*bucket, *flags);
-               range          += (1 << HASH_FN_SHIFT);
                index.dev_addr -= (1 << HASH_FN_SHIFT);
                *bucket = get_hash_bucket(&index, flags);
        }