resource: Avoid unnecessary resource tree walking in __region_intersects()
PATCH 1/1 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. This is achieved using for_each_resource(), which 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 renames next_resource() to __next_resource(), and makes it possible to only traverse the subtree under the specified resource. 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'.
It appears even better to revise for_each_resource() to traverse the resource subtree under "_root" only. But that will cause "_root" to be evaluated twice, which I don't find a good way to eliminate.
Thanks David Hildenbrand for providing a good resource tree example.
---
Changes:
RFC->v1:
- Revised patch description and comments, Thanks David and Andy!
- Link to RFC: https://lore.kernel.org/linux-mm/[email protected]/
--- kernel/resource.c | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-)
kernel/resource.c +23 -3
@@ -50,15 +50,34 @@ EXPORT_SYMBOL(iomem_resource);static DEFINE_RWLOCK (resource_lock); -static struct resource *next_resource (struct resource *p,bool skip_children) +/* +* Return the next node of @p in pre-order tree traversal. If +* @skip_children is true, skip the descendant nodes of @p in +* traversal. If @p is a descendant of @subtree_root, only traverse +* the subtree under @subtree_root. +*/ +static struct resource *__next_resource (struct resource *p,bool skip_children, +struct resource *subtree_root) {if (!skip_children && p->child)return p->child; -while (!p->sibling && p->parent) +while (!p->sibling && p->parent) { p = p->parent; +if (p == subtree_root) +return NULL ; + }return p->sibling; } +static struct resource *next_resource (struct resource *p,bool skip_children) +{ +return __next_resource (p, skip_children,NULL ); +} + +/* +* Traverse the whole resource tree with @_root as root in pre-order. +* NOTE: @_root should be the topmost node, that is, @_root->parent == NULL. +*/ #define for_each_resource (_root, _p, _skip_children) \ for ((_p) = (_root)->child; (_p); (_p) = next_resource(_p, _skip_children))
@@ -572,7 +591,8 @@ static int __region_intersects(struct resource *parent, resource_size_t start, covered = false; ostart =max (res.start, p->start); oend =min (res.end, p->end); -for_each_resource (p, dp, false) { +/* Traverse the subtree under 'p'. */ +for (dp = p->child; dp; dp =__next_resource (dp, false, p)) {if (!resource_overlaps (dp, &res))continue ; is_type = (dp->flags & flags) == flags &&
Signature · 1 line
2.39.2
Huang Ying wrote:
[..]
Quoted 12 lines
For the example resource tree as follows,
X
|
A----D----E
|
B--Cif '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'.
Quoted 5 lines
It appears even better to revise
for_each_resource()to traverse the resource subtree under "_root" only. But that will cause "_root" to be evaluated twice, which I don't find a good way to eliminate.Thanks David Hildenbrand for providing a good resource tree example.
Should this have a Reported-by: and a Closes: tags for that report? Seems useful to capture that in the history.
Quoted 67 lines
Signed-off-by "Huang, Ying" <[email protected]>Cc Dan Williams <[email protected]>Cc David Hildenbrand <[email protected]>Cc Davidlohr Bueso <[email protected]>Cc Jonathan Cameron <[email protected]>Cc Alistair Popple <[email protected]>Cc Andy Shevchenko <[email protected]>Cc Bjorn Helgaas <[email protected]>Cc Baoquan He <[email protected]>Cc Dave Jiang <[email protected]>Cc Alison Schofield <[email protected]>---
Changes:
RFC->v1:- Revised patch description and comments, Thanks David and Andy!
- Link to RFC: https://lore.kernel.org/linux-mm/[email protected]/
--- kernel/resource.c | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-)kernel/resource.c +21
-2@@ -50,15 +50,34 @@ EXPORT_SYMBOL(iomem_resource);static DEFINE_RWLOCK (resource_lock); -static struct resource *next_resource (struct resource *p,bool skip_children) +/* +* Return the next node of @p in pre-order tree traversal. If +* @skip_children is true, skip the descendant nodes of @p in +* traversal. If @p is a descendant of @subtree_root, only traverse +* the subtree under @subtree_root. +*/ +static struct resource *__next_resource (struct resource *p,bool skip_children, +struct resource *subtree_root) {if (!skip_children && p->child)return p->child; -while (!p->sibling && p->parent) +while (!p->sibling && p->parent) { p = p->parent; +if (p == subtree_root) +return NULL ; + }return p->sibling; } +static struct resource *next_resource (struct resource *p,bool skip_children) +{ +return __next_resource (p, skip_children,NULL ); +} + +/* +* Traverse the whole resource tree with @_root as root in pre-order. +* NOTE: @_root should be the topmost node, that is, @_root->parent == NULL. +*/ #define for_each_resource (_root, _p, _skip_children) \ for ((_p) = (_root)->child; (_p); (_p) = next_resource(_p, _skip_children))
kernel/resource.c static int __region_intersects(struct resource *parent, resource_size_t start, · line 595
Quoted 6 lines
kernel/resource.c +2
-1@@ -572,7 +591,8 @@ static int __region_intersects(struct resource *parent, resource_size_t start, covered = false; ostart =max (res.start, p->start); oend =min (res.end, p->end); -for_each_resource (p, dp, false) { +/* Traverse the subtree under 'p'. */ +for (dp = p->child; dp; dp =__next_resource (dp, false, p)) {
Perhaps a new for_each_resource_descendant() to clarify this new iterator from for_each_resource()?
Otherwise looks good to me:
Quoted 14 lines · Dan Williams
Huang Ying wrote:
[..]Quoted 12 lines
For the example resource tree as follows,
X
|
A----D----E
|
B--Cif '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'.
Quoted 8 lines
Quoted 5 lines
It appears even better to revise
for_each_resource()to traverse the resource subtree under "_root" only. But that will cause "_root" to be evaluated twice, which I don't find a good way to eliminate.Thanks David Hildenbrand for providing a good resource tree example.
Should this have a Reported-by: and a Closes: tags for that report? Seems useful to capture that in the history.
IIUC, David didn't reported an issue. He just provided an example to explain the different traversal behavior.
Quoted 66 lines
Quoted 66 lines
Signed-off-by "Huang, Ying" <[email protected]>Cc Dan Williams <[email protected]>Cc David Hildenbrand <[email protected]>Cc Davidlohr Bueso <[email protected]>Cc Jonathan Cameron <[email protected]>Cc Alistair Popple <[email protected]>Cc Andy Shevchenko <[email protected]>Cc Bjorn Helgaas <[email protected]>Cc Baoquan He <[email protected]>Cc Dave Jiang <[email protected]>Cc Alison Schofield <[email protected]>---
Changes:
RFC->v1:- Revised patch description and comments, Thanks David and Andy!
- Link to RFC: https://lore.kernel.org/linux-mm/[email protected]/
--- kernel/resource.c | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-)kernel/resource.c +21
-2@@ -50,15 +50,34 @@ EXPORT_SYMBOL(iomem_resource);static DEFINE_RWLOCK (resource_lock); -static struct resource *next_resource (struct resource *p,bool skip_children) +/* +* Return the next node of @p in pre-order tree traversal. If +* @skip_children is true, skip the descendant nodes of @p in +* traversal. If @p is a descendant of @subtree_root, only traverse +* the subtree under @subtree_root. +*/ +static struct resource *__next_resource (struct resource *p,bool skip_children, +struct resource *subtree_root) {if (!skip_children && p->child)return p->child; -while (!p->sibling && p->parent) +while (!p->sibling && p->parent) { p = p->parent; +if (p == subtree_root) +return NULL ; + }return p->sibling; } +static struct resource *next_resource (struct resource *p,bool skip_children) +{ +return __next_resource (p, skip_children,NULL ); +} + +/* +* Traverse the whole resource tree with @_root as root in pre-order. +* NOTE: @_root should be the topmost node, that is, @_root->parent == NULL. +*/ #define for_each_resource (_root, _p, _skip_children) \ for ((_p) = (_root)->child; (_p); (_p) = next_resource(_p, _skip_children))
Quoted 10 lines
kernel/resource.c static int __region_intersects(struct resource *parent, resource_size_t start, · line 595
Quoted 7 lines
kernel/resource.c +2
-1@@ -572,7 +591,8 @@ static int __region_intersects(struct resource *parent, resource_size_t start, covered = false; ostart =max (res.start, p->start); oend =min (res.end, p->end); -for_each_resource (p, dp, false) { +/* Traverse the subtree under 'p'. */ +for (dp = p->child; dp; dp =__next_resource (dp, false, p)) {Perhaps a new
for_each_resource_descendant()to clarify this new iterator fromfor_each_resource()?
Yes. That's a good idea. The problem is that it's hard to avoid double evaluation in an elegant way. We have discussed this in
https://lore.kernel.org/linux-mm/[email protected]/
I have proposed something like,
#define for_each_resource_descendant(_root, _p) \ for (typeof(_root) __root = (_root), __p = (_p) = (__root)->child; \ __p && (_p); (_p) = __next_resource(_p, false, __root))
But this doesn't look elegant.
Quoted 3 lines
Otherwise looks good to me:
Acked-by Dan Williams <[email protected]>
Thanks!
--
Best Regards,
Huang, Ying