From: Ahmad Fatoum <a.fatoum@pengutronix.de>
To: barebox@lists.infradead.org
Cc: Ahmad Fatoum <a.fatoum@pengutronix.de>
Subject: [PATCH 6/6] resource: implement release_region_range
Date: Thu, 11 Dec 2025 21:50:08 +0100 [thread overview]
Message-ID: <20251211205240.2836186-7-a.fatoum@pengutronix.de> (raw)
In-Reply-To: <20251211205240.2836186-1-a.fatoum@pengutronix.de>
For EFI loader use, we will need the ability to free only part of a
previously requested region.
Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
common/resource.c | 134 +++++++++++++++++++++++++++++++++++++++++
include/linux/ioport.h | 5 ++
2 files changed, 139 insertions(+)
diff --git a/common/resource.c b/common/resource.c
index c8623b9c40a6..078dc9f2ff2d 100644
--- a/common/resource.c
+++ b/common/resource.c
@@ -107,6 +107,140 @@ int release_region(struct resource *res)
return 0;
}
+static int yes_free(struct resource *res, void *data)
+{
+ return 1;
+}
+
+/*
+ * release a region previously requested with request_*_region
+ */
+int release_region_range(struct resource *parent,
+ resource_size_t start, resource_size_t size,
+ int (*should_free)(struct resource *res, void *data),
+ void *data)
+{
+ resource_size_t end = start + size - 1;
+ struct resource *r, *tmp;
+ int ret, err = 0;
+
+ if (end < parent->start || start > parent->end)
+ return 0;
+
+ if (!should_free)
+ should_free = yes_free;
+
+ list_for_each_entry_safe(r, tmp, &parent->children, sibling) {
+ if (end < r->start || start > r->end)
+ continue;
+
+ /*
+ * CASE 1: fully covered
+ *
+ * r: |----------------|
+ * cut: |xxxxxxxxxxxxxxxxxxx|
+ *
+ * remove fully
+ */
+ if (start <= r->start && r->end <= end) {
+ ret = should_free(r, data);
+ if (ret < 0)
+ return ret;
+ if (ret == 0)
+ continue;
+
+ ret = release_region(r);
+ if (ret)
+ err = ret;
+ continue;
+ }
+
+ /*
+ * CASE 2: trim head
+ *
+ * r: |----------------|
+ * cut: |xxxxx|
+ * new pieces:
+ * left = removed
+ * right = end+1 .. r.end
+ */
+ if (start <= r->start && r->end > end) {
+ ret = should_free(r, data);
+ if (ret < 0)
+ return ret;
+ if (ret == 0)
+ continue;
+
+ if (list_empty(&r->children))
+ r->start = end + 1;
+ else
+ err = -EBUSY;
+ continue;
+ }
+
+ /*
+ * CASE 3: trim tail
+ *
+ * r: |----------------|
+ * cut: |xxxxx|
+ * new pieces:
+ * left = r.start .. start-1
+ * right = removed
+ */
+ if (start > r->start && r->end <= end) {
+ ret = should_free(r, data);
+ if (ret < 0)
+ return ret;
+ if (ret == 0)
+ continue;
+
+ if (list_empty(&r->children))
+ r->end = start - 1;
+ else
+ err = -EBUSY;
+ continue;
+ }
+
+ /*
+ * CASE 4: split
+ *
+ * r: |----------------|
+ * cut: |xxxxx|
+ * new pieces:
+ * left = r.start .. start-1
+ * right = end+1 .. r.end
+ */
+ if (start > r->start && r->end > end) {
+ struct resource *right;
+
+ ret = should_free(r, data);
+ if (ret < 0)
+ return ret;
+ if (ret == 0)
+ continue;
+
+ if (!list_empty(&r->children)) {
+ err = -EBUSY;
+ continue;
+ }
+
+ right = xzalloc(sizeof(*right));
+ init_resource(right, r->name);
+
+ right->start = end + 1;
+ right->end = r->end;
+ right->parent = parent;
+ right->flags = r->flags;
+
+ r->end = start - 1;
+
+ list_add(&right->sibling, &r->sibling);
+ continue;
+ }
+ }
+
+ return WARN_ON(err);
+}
/*
* merge two adjacent sibling regions.
diff --git a/include/linux/ioport.h b/include/linux/ioport.h
index 1d8686d51e81..2a190e96b1a7 100644
--- a/include/linux/ioport.h
+++ b/include/linux/ioport.h
@@ -209,6 +209,11 @@ int __merge_regions(const char *name,
int release_region(struct resource *res);
+int release_region_range(struct resource *parent,
+ resource_size_t start, resource_size_t size,
+ int (*should_free)(struct resource *res, void *data),
+ void *data);
+
extern struct resource iomem_resource;
extern struct resource ioport_resource;
--
2.47.3
next prev parent reply other threads:[~2025-12-11 20:53 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-12-11 20:50 [PATCH 0/6] resource: add support for walking resource gaps Ahmad Fatoum
2025-12-11 20:50 ` [PATCH 1/6] resource: implement resource walker Ahmad Fatoum
2025-12-11 20:50 ` [PATCH 2/6] test: self: implement resource walker selftest Ahmad Fatoum
2025-12-11 20:50 ` [PATCH 3/6] commands: iomem: add support for printing gaps Ahmad Fatoum
2025-12-11 20:50 ` [PATCH 4/6] test: py: add test for valid JSON output from iomem/clk_dump Ahmad Fatoum
2025-12-11 20:50 ` [PATCH 5/6] memory: add helpers for iterating over memory regions Ahmad Fatoum
2025-12-11 20:50 ` Ahmad Fatoum [this message]
2025-12-15 9:29 ` [PATCH 0/6] resource: add support for walking resource gaps Sascha Hauer
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=20251211205240.2836186-7-a.fatoum@pengutronix.de \
--to=a.fatoum@pengutronix.de \
--cc=barebox@lists.infradead.org \
/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