mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Ahmad Fatoum <a.fatoum@pengutronix.de>
To: barebox@lists.infradead.org
Cc: Ahmad Fatoum <a.fatoum@pengutronix.de>
Subject: [PATCH v2 08/13] resource: implement resize_region
Date: Mon,  9 Feb 2026 19:10:47 +0100	[thread overview]
Message-ID: <20260209181138.2023065-9-a.fatoum@pengutronix.de> (raw)
In-Reply-To: <20260209181138.2023065-1-a.fatoum@pengutronix.de>

resize_region allows enlarging or reducing a region end while checking
for conflicts. This is less effort than removing a region and
reallocating it.

Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
 common/resource.c      | 62 ++++++++++++++++++++++++++++++++++++++++++
 include/linux/ioport.h |  2 ++
 2 files changed, 64 insertions(+)

diff --git a/common/resource.c b/common/resource.c
index e391d268e0bb..131f5e190c58 100644
--- a/common/resource.c
+++ b/common/resource.c
@@ -92,6 +92,68 @@ struct resource *__request_region(struct resource *parent,
 	return new;
 }
 
+/**
+ * resize_region - change the size of an allocated resource region
+ * @res: resource to resize (may be NULL for no-op)
+ * @size: new desired size in bytes (must be non-zero)
+ *
+ * Shrinking checks that no child extends past the new end.
+ * Growing checks that the new end does not overflow, stays within
+ * the parent region and does not collide with the next sibling.
+ *
+ * Return: 0 on success, negative error code on failure
+ */
+int resize_region(struct resource *res, resource_size_t size)
+{
+	struct resource *parent;
+	struct resource *next;
+	resource_size_t newend;
+
+	if (!res)
+		return 0;
+	if (!size)
+		return -EINVAL;
+
+	if (size == resource_size(res))
+		return 0;
+
+	if (size < resource_size(res)) {
+		struct resource *last;
+
+		last = list_last_entry_or_null(&res->children,
+					       struct resource, sibling);
+		if (last && last->end > res->start + size - 1)
+			return -EBUSY;
+		res->end = res->start + size - 1;
+		return 0;
+	}
+
+	parent = res->parent;
+	if (!parent)
+		return -EINVAL;
+
+	if (check_add_overflow(res->start, size - 1, &newend))
+		return -EINVAL;
+
+	if (newend > parent->end)
+		return -EINVAL;
+
+	/*
+	 * parent->children is the list_head that anchors the ordered list of
+	 * children. If res is not the last entry, the immediate next entry is
+	 * the only sibling we must check.
+	 */
+	if (res->sibling.next != &parent->children) {
+		next = list_next_entry(res, sibling);
+
+		if (newend >= next->start)
+			return -EBUSY;
+	}
+
+	res->end = newend;
+	return 0;
+}
+
 /*
  * release a region previously requested with request_*_region
  */
diff --git a/include/linux/ioport.h b/include/linux/ioport.h
index 5baae0dae10a..8f9191d1c6be 100644
--- a/include/linux/ioport.h
+++ b/include/linux/ioport.h
@@ -272,6 +272,8 @@ static inline void resource_set_range(struct resource *res,
 	resource_set_size(res, size);
 }
 
+int resize_region(struct resource *res, resource_size_t size);
+
 #define region_is_gap(region) ((region)->flags & IORESOURCE_UNSET)
 
 struct resource *resource_iter_first(struct resource *current, struct resource *gap);
-- 
2.47.3




  parent reply	other threads:[~2026-02-09 18:12 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-02-09 18:10 [PATCH v2 00/13] bootm: prepare loadable abstraction rework Ahmad Fatoum
2026-02-09 18:10 ` [PATCH v2 01/13] FIT: implement fit_count_images Ahmad Fatoum
2026-02-09 18:10 ` [PATCH v2 02/13] FIT: add image index argument to fit_open_image Ahmad Fatoum
2026-02-09 18:10 ` [PATCH v2 03/13] resource: implement gap-aware lookup_region Ahmad Fatoum
2026-02-09 18:10 ` [PATCH v2 04/13] bootm: give bootm_load_ functions an end address Ahmad Fatoum
2026-02-09 18:10 ` [PATCH v2 05/13] bootm: store separate image_type and kernel_type Ahmad Fatoum
2026-02-09 18:10 ` [PATCH v2 06/13] bootm: fit: move length calculation into fit_open Ahmad Fatoum
2026-02-09 18:10 ` [PATCH v2 07/13] libfile: factor out zero-page resistant read_file as __read_full_anywhere Ahmad Fatoum
2026-02-09 18:10 ` Ahmad Fatoum [this message]
2026-02-09 18:10 ` [PATCH v2 09/13] bootm: rename image_data::os/initrd with _uimage suffix Ahmad Fatoum
2026-02-09 18:10 ` [PATCH v2 10/13] uimage: record original file name in uimage_handle Ahmad Fatoum
2026-02-09 18:10 ` [PATCH v2 11/13] bootm: factor out file detection into helper Ahmad Fatoum
2026-02-09 18:10 ` [PATCH v2 12/13] efi: payload: bootm: add dry run support Ahmad Fatoum
2026-02-09 18:10 ` [PATCH v2 13/13] efi: initrd: make efi_initrd_register initrd pointer param const Ahmad Fatoum
2026-02-13 14:07 ` [PATCH v2 00/13] bootm: prepare loadable abstraction rework 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=20260209181138.2023065-9-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