Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

resource: avoid unnecessary resource tree walking in __region_intersects()

Currently, if __region_intersects() finds any overlapped but unmatched
resource, it walks the descendant resource tree to check for overlapped
and matched descendant resources using for_each_resource(). However, in
current kernel, for_each_resource() iterates not only the descendant tree,
but also subsequent sibling trees in certain scenarios. While this
doesn't introduce bugs, it makes code hard to be understood and
potentially inefficient.

So, the patch revises next_resource() and for_each_resource() and makes
for_each_resource() traverse the subtree under the specified subtree root
only. Test shows that this avoids unnecessary resource tree walking in
__region_intersects().

For the example resource tree as follows,

X
|
A----D----E
|
B--C

if 'A' is the overlapped but unmatched resource, original kernel
iterates 'B', 'C', 'D', 'E' when it walks the descendant tree. While
the patched kernel iterates only 'B', 'C'.

Thanks David Hildenbrand for providing a good resource tree example.

Link: https://lkml.kernel.org/r/20241029122735.79164-1-ying.huang@intel.com
Signed-off-by: "Huang, Ying" <ying.huang@intel.com>
Acked-by: Dan Williams <dan.j.williams@intel.com>
Cc: David Hildenbrand <david@redhat.com>
Cc: Davidlohr Bueso <dave@stgolabs.net>
Cc: Jonathan Cameron <jonathan.cameron@huawei.com>
Cc: Alistair Popple <apopple@nvidia.com>
Cc: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
Cc: Bjorn Helgaas <bhelgaas@google.com>
Cc: Baoquan He <bhe@redhat.com>
Cc: Dave Jiang <dave.jiang@intel.com>
Cc: Alison Schofield <alison.schofield@intel.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>

authored by

Huang Ying and committed by
Andrew Morton
d7ce9c73 77e94b04

+22 -4
+22 -4
kernel/resource.c
··· 50 50 51 51 static DEFINE_RWLOCK(resource_lock); 52 52 53 - static struct resource *next_resource(struct resource *p, bool skip_children) 53 + /* 54 + * Return the next node of @p in pre-order tree traversal. If 55 + * @skip_children is true, skip the descendant nodes of @p in 56 + * traversal. If @p is a descendant of @subtree_root, only traverse 57 + * the subtree under @subtree_root. 58 + */ 59 + static struct resource *next_resource(struct resource *p, bool skip_children, 60 + struct resource *subtree_root) 54 61 { 55 62 if (!skip_children && p->child) 56 63 return p->child; 57 - while (!p->sibling && p->parent) 64 + while (!p->sibling && p->parent) { 58 65 p = p->parent; 66 + if (p == subtree_root) 67 + return NULL; 68 + } 59 69 return p->sibling; 60 70 } 61 71 72 + /* 73 + * Traverse the resource subtree under @_root in pre-order, excluding 74 + * @_root itself. 75 + * 76 + * NOTE: '__p' is introduced to avoid shadowing '_p' outside of loop. 77 + * And it is referenced to avoid unused variable warning. 78 + */ 62 79 #define for_each_resource(_root, _p, _skip_children) \ 63 - for ((_p) = (_root)->child; (_p); (_p) = next_resource(_p, _skip_children)) 80 + for (typeof(_root) __root = (_root), __p = _p = __root->child; \ 81 + __p && _p; _p = next_resource(_p, _skip_children, __root)) 64 82 65 83 #ifdef CONFIG_PROC_FS 66 84 ··· 106 88 107 89 (*pos)++; 108 90 109 - return (void *)next_resource(p, false); 91 + return (void *)next_resource(p, false, NULL); 110 92 } 111 93 112 94 static void r_stop(struct seq_file *m, void *v)