mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: "Ilpo Järvinen" <ilpo.jarvinen@linux.intel.com>
To: "Bjorn Helgaas" <bhelgaas@google.com>,
	"Lorenzo Pieralisi" <lorenzo.pieralisi@arm.com>,
	"Rob Herring" <robh@kernel.org>,
	"Krzysztof Wilczyński" <kw@linux.com>,
	"Alex Deucher" <alexander.deucher@amd.com>,
	"Christian König" <christian.koenig@amd.com>,
	"Jani Nikula" <jani.nikula@linux.intel.com>,
	"Joonas Lahtinen" <joonas.lahtinen@linux.intel.com>,
	"Rodrigo Vivi" <rodrigo.vivi@intel.com>,
	"Tvrtko Ursulin" <tursulin@ursulin.net>,
	"Matthew Brost" <matthew.brost@intel.com>,
	"Thomas Hellström" <thomas.hellstrom@linux.intel.com>,
	"Michał Winiarski" <michal.winiarski@intel.com>,
	"Ville Syrjälä" <ville.syrjala@linux.intel.com>,
	"David Airlie" <airlied@gmail.com>,
	"Simona Vetter" <simona@ffwll.ch>,
	dri-devel@lists.freedesktop.org, amd-gfx@lists.freedesktop.org,
	intel-gfx@lists.freedesktop.org, intel-xe@lists.freedesktop.org,
	linux-kernel@vger.kernel.org
Cc: "Ilpo Järvinen" <ilpo.jarvinen@linux.intel.com>
Subject: [PATCH 11/15] resource: Make resource_lock and resource tree traversal available
Date: Tue,  1 Dec 2026 17:58:35 +0200	[thread overview]
Message-ID: <20261201155844.2278-12-ilpo.jarvinen@linux.intel.com> (raw)
In-Reply-To: <20261201155844.2278-1-ilpo.jarvinen@linux.intel.com>

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 <ilpo.jarvinen@linux.intel.com>
---
 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


  parent reply	other threads:[~2026-12-01 16:01 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-01 15:58 [PATCH 00/15] PCI: Resource release fixes and improvements Ilpo Järvinen
2026-12-01 15:58 ` [PATCH 01/15] PCI: Release all resources before restoring them in BAR resize rollback Ilpo Järvinen
2026-12-01 15:58 ` [PATCH 02/15] PCI: Set new BAR Size only after releasing the BARs for BAR resize Ilpo Järvinen
2026-12-01 15:58 ` [PATCH 03/15] PCI: Abort BAR resize only if saving the resized BAR fails Ilpo Järvinen
2026-12-01 15:58 ` [PATCH 04/15] PCI: Consistently use Header Type to detect bridge headers Ilpo Järvinen
2026-12-01 15:58 ` [PATCH 05/15] resource: Check that the resource is in the tree when releasing it Ilpo Järvinen
2026-12-01 15:58 ` [PATCH 06/15] resource: Add KUnit test for releasing a resource not in the tree Ilpo Järvinen
2026-12-01 15:58 ` [PATCH 07/15] resource: Add release_leaf_resource() Ilpo Järvinen
2026-12-01 15:58 ` [PATCH 08/15] resource: Add KUnit test for release_leaf_resource() Ilpo Järvinen
2026-12-01 15:58 ` [PATCH 09/15] PCI: Do not release resources with children in pci_release_resource() Ilpo Järvinen
2026-12-01 15:58 ` [PATCH 10/15] resource: Rename next_resource() and for_each_resource() Ilpo Järvinen
2026-12-01 15:58 ` Ilpo Järvinen [this message]
2026-12-01 15:58 ` [PATCH 12/15] resource: Add release_resource_locked() Ilpo Järvinen
2026-12-01 15:58 ` [PATCH 13/15] PCI: Split pci_resource_post_release() out of pci_release_resource() Ilpo Järvinen
2026-12-01 15:58 ` [PATCH 14/15] PCI: Add iterators for devices and resources in bus windows Ilpo Järvinen
2026-12-01 15:58 ` [PATCH 15/15] PCI: Release only PCI resources when releasing a resource tree Ilpo Järvinen
2026-12-02  0:22   ` Ilpo Järvinen

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=20261201155844.2278-12-ilpo.jarvinen@linux.intel.com \
    --to=ilpo.jarvinen@linux.intel.com \
    --cc=airlied@gmail.com \
    --cc=alexander.deucher@amd.com \
    --cc=amd-gfx@lists.freedesktop.org \
    --cc=bhelgaas@google.com \
    --cc=christian.koenig@amd.com \
    --cc=dri-devel@lists.freedesktop.org \
    --cc=intel-gfx@lists.freedesktop.org \
    --cc=intel-xe@lists.freedesktop.org \
    --cc=jani.nikula@linux.intel.com \
    --cc=joonas.lahtinen@linux.intel.com \
    --cc=kw@linux.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=lorenzo.pieralisi@arm.com \
    --cc=matthew.brost@intel.com \
    --cc=michal.winiarski@intel.com \
    --cc=robh@kernel.org \
    --cc=rodrigo.vivi@intel.com \
    --cc=simona@ffwll.ch \
    --cc=thomas.hellstrom@linux.intel.com \
    --cc=tursulin@ursulin.net \
    --cc=ville.syrjala@linux.intel.com \
    /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

all inboxes | Powered by JetHome®