Code that needs to look at the resource tree to decide whether it can
release a resource together with the resources underneath it has to
traverse the tree, and both the check and the release must be done
under resource_lock so that no resource gets added in between.

Make resource_lock and resource_tree_next() available through ioport.h
and move resource_tree_for_each() there. As resource_tree_next() is now
public, assert that resource_lock is held.

Signed-off-by: Ilpo Järvinen <[email protected]>
---
 include/linux/ioport.h | 21 +++++++++++++++++++++
 kernel/resource.c      | 21 +++++++--------------
 2 files changed, 28 insertions(+), 14 deletions(-)

diff --git a/include/linux/ioport.h b/include/linux/ioport.h
index f38800fb957d..830d621907b7 100644
--- a/include/linux/ioport.h
+++ b/include/linux/ioport.h
@@ -14,6 +14,7 @@
 #include <linux/bits.h>
 #include <linux/compiler.h>
 #include <linux/minmax.h>
+#include <linux/spinlock_types.h>
 #include <linux/types.h>
 /*
  * Resources are tree-like, allowing
@@ -241,6 +242,26 @@ extern struct resource ioport_resource;
 extern struct resource iomem_resource;
 extern struct resource soft_reserve_resource;
 
+/* Protects all resource trees */
+extern rwlock_t resource_lock;
+
+struct resource *resource_tree_next(struct resource *p, bool skip_children,
+                                   struct resource *subtree_root);
+
+/*
+ * Traverse the resource subtree under @_root in pre-order, excluding
+ * @_root itself. With @_skip_children, only the children of @_root are
+ * traversed.
+ *
+ * Context: Caller must hold resource_lock.
+ *
+ * NOTE: '__p' is introduced to avoid shadowing '_p' outside of loop.
+ * And it is referenced to avoid unused variable warning.
+ */
+#define resource_tree_for_each(_root, _p, _skip_children)                      
\
+       for (typeof(_root) __root = (_root), __p = _p = __root->child;  \
+            __p && _p; _p = resource_tree_next(_p, _skip_children, __root))
+
 extern struct resource *request_resource_conflict(struct resource *root, 
struct resource *new);
 extern int request_resource(struct resource *root, struct resource *new);
 extern int release_resource(struct resource *new);
diff --git a/kernel/resource.c b/kernel/resource.c
index 8d6928c638ff..fbcbdae99887 100644
--- a/kernel/resource.c
+++ b/kernel/resource.c
@@ -58,17 +58,21 @@ struct resource soft_reserve_resource = {
        .flags  = IORESOURCE_MEM,
 };
 
-static DEFINE_RWLOCK(resource_lock);
+DEFINE_RWLOCK(resource_lock);
 
 /*
  * 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.
+ *
+ * Context: Caller must hold resource_lock.
  */
-static struct resource *resource_tree_next(struct resource *p, bool 
skip_children,
-                                          struct resource *subtree_root)
+struct resource *resource_tree_next(struct resource *p, bool skip_children,
+                                   struct resource *subtree_root)
 {
+       lockdep_assert_held(&resource_lock);
+
        if (!skip_children && p->child)
                return p->child;
        while (!p->sibling && p->parent) {
@@ -79,17 +83,6 @@ static struct resource *resource_tree_next(struct resource 
*p, bool skip_childre
        return p->sibling;
 }
 
-/*
- * Traverse the resource subtree under @_root in pre-order, excluding
- * @_root itself.
- *
- * NOTE: '__p' is introduced to avoid shadowing '_p' outside of loop.
- * And it is referenced to avoid unused variable warning.
- */
-#define resource_tree_for_each(_root, _p, _skip_children)                      
\
-       for (typeof(_root) __root = (_root), __p = _p = __root->child;  \
-            __p && _p; _p = resource_tree_next(_p, _skip_children, __root))
-
 #ifdef CONFIG_PROC_FS
 
 enum { MAX_IORES_LEVEL = 8 };
-- 
2.47.3

Reply via email to