From: Ahmad Fatoum <a.fatoum@pengutronix.de>
To: barebox@lists.infradead.org
Cc: Ahmad Fatoum <a.fatoum@barebox.org>
Subject: [PATCH 15/19] driver: add devm_request_mem_resource()
Date: Wed, 28 Oct 2026 10:14:49 +0100 [thread overview]
Message-ID: <20261028092442.3300753-16-a.fatoum@pengutronix.de> (raw)
In-Reply-To: <20261028092442.3300753-1-a.fatoum@pengutronix.de>
From: Ahmad Fatoum <a.fatoum@barebox.org>
Drivers using dev_request_mem_resource() have to release the region in
every later error path and on remove. Add a managed variant that does
this for them.
Assisted-by: Claude:opus-5-1m
Signed-off-by: Ahmad Fatoum <a.fatoum@barebox.org>
---
drivers/base/devres.c | 36 ++++++++++++++++++++++++++++++++++++
include/driver.h | 3 +++
test/self/devres.c | 38 ++++++++++++++++++++++++++++++++++++++
3 files changed, 77 insertions(+)
diff --git a/drivers/base/devres.c b/drivers/base/devres.c
index bf118ae0820d..efc810a8898f 100644
--- a/drivers/base/devres.c
+++ b/drivers/base/devres.c
@@ -5,6 +5,7 @@
* Managed resources are children of a per-device talloc context.
*/
+#include <barebox.h>
#include <driver.h>
#include <dma.h>
#include <talloc.h>
@@ -168,3 +169,38 @@ int devm_add_action(struct device *dev, void (*action)(void *), void *data)
return talloc_add_destructor(ctx, action, data);
}
EXPORT_SYMBOL(devm_add_action);
+
+static void devm_region_release(void *res)
+{
+ /* No need to release regions when shutting down */
+ if (barebox_system_state == BAREBOX_EXITING)
+ return;
+
+ release_region(res);
+}
+
+/**
+ * devm_request_mem_resource() - exclusively request resource 'num' for a device
+ * @dev: device to request the memory resource for
+ * @num: index of the memory resource
+ *
+ * Like dev_request_mem_resource(), but device-managed.
+ *
+ * Return: the requested resource or an ERR_PTR() on failure.
+ */
+struct resource *devm_request_mem_resource(struct device *dev, int num)
+{
+ struct resource *res;
+ int ret;
+
+ res = dev_request_mem_resource(dev, num);
+ if (IS_ERR(res))
+ return res;
+
+ ret = devm_add_action_or_reset(dev, devm_region_release, res);
+ if (ret)
+ return ERR_PTR(ret);
+
+ return res;
+}
+EXPORT_SYMBOL(devm_request_mem_resource);
diff --git a/include/driver.h b/include/driver.h
index c394c455b971..4005804bb1a9 100644
--- a/include/driver.h
+++ b/include/driver.h
@@ -193,6 +193,9 @@ struct resource *dev_request_mem_resource(struct device *dev, int num);
struct resource *dev_request_mem_resource_by_name(struct device *dev,
const char *name);
+/* Like dev_request_mem_resource(), but released when the device goes away */
+struct resource *devm_request_mem_resource(struct device *dev, int num);
+
void __iomem *dev_platform_get_and_ioremap_resource(struct device *dev,
int num,
struct resource **out_res);
diff --git a/test/self/devres.c b/test/self/devres.c
index 4fdc71064b4a..6992eb20ace3 100644
--- a/test/self/devres.c
+++ b/test/self/devres.c
@@ -8,6 +8,7 @@
#include <linux/bitmap.h>
#include <driver.h>
#include <linux/device.h>
+#include <linux/sizes.h>
BSELFTEST_GLOBALS();
@@ -136,6 +137,42 @@ static void test_devres_shutdown(void)
free_device(dev);
}
+static void test_devres_region(void)
+{
+ /* The last page of the address space, skipped if it's in use */
+ resource_size_t start = iomem_resource.end - SZ_4K + 1;
+ enum system_states state = barebox_system_state;
+ struct device *dev = device_alloc("devres-test", DEVICE_ID_DYNAMIC);
+ struct resource *res;
+
+ device_add_resource(dev, NULL, start, SZ_4K, IORESOURCE_MEM);
+ if (selftest_check(register_device(dev) == 0) == false)
+ return;
+
+ res = devm_request_mem_resource(dev, 0);
+ if (IS_ERR(res)) {
+ skipped_tests++;
+ goto out;
+ }
+
+ /* Shutdown keeps the region, but drops the action all the same */
+ barebox_system_state = BAREBOX_EXITING;
+ devres_run_actions(dev);
+ barebox_system_state = state;
+ if (selftest_check(IS_ERR(devm_request_mem_resource(dev, 0))))
+ release_region(res);
+
+ /* Releasing frees it */
+ selftest_check(!IS_ERR(devm_request_mem_resource(dev, 0)));
+ devres_release_all(dev);
+ res = request_iomem_region("devres-test", start, iomem_resource.end);
+ if (selftest_check(!IS_ERR(res)))
+ release_region(res);
+out:
+ unregister_device(dev);
+ free_device(dev);
+}
+
static int probe_result;
static char *probe_buf;
@@ -282,6 +319,7 @@ static void test_devres(void)
test_devres_alloc();
test_devres_action();
test_devres_shutdown();
+ test_devres_region();
test_devres_probe();
test_devres_unbind();
test_devres_child();
--
2.47.3
next prev parent reply other threads:[~2026-10-28 9:29 UTC|newest]
Thread overview: 20+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-10-28 9:14 [PATCH 00/19] driver: add devm_ support Ahmad Fatoum
2026-10-28 9:14 ` [PATCH 01/19] commands: devunbind: refuse to unbind devices that were never bound Ahmad Fatoum
2026-10-28 9:14 ` [PATCH 02/19] commands: devunbind: don't poison the active list entry Ahmad Fatoum
2026-10-28 9:14 ` [PATCH 03/19] talloc: remove unused talloc_steal() and talloc_steal_children() Ahmad Fatoum
2026-10-28 9:14 ` [PATCH 04/19] talloc: free children in a loop Ahmad Fatoum
2026-10-28 9:14 ` [PATCH 05/19] talloc: add support for destructors Ahmad Fatoum
2026-10-28 9:14 ` [PATCH 06/19] test: self: talloc: add destructor tests Ahmad Fatoum
2026-10-28 9:14 ` [PATCH 07/19] talloc: add talloc_run_destructors() Ahmad Fatoum
2026-10-28 9:14 ` [PATCH 08/19] mfd: pca9450: don't free device-managed memory in probe error path Ahmad Fatoum
2026-10-28 9:14 ` [PATCH 09/19] video: backlight-pwm: allocate brightness levels with devm_kcalloc Ahmad Fatoum
2026-10-28 9:14 ` [PATCH 10/19] driver: implement devm_ allocation helpers on top of talloc Ahmad Fatoum
2026-10-28 9:14 ` [PATCH 11/19] devres: Provide kmalloc_obj*-style API for devm_kmalloc Ahmad Fatoum
2026-10-28 9:14 ` [PATCH 12/19] memory: atmel-ebi: use generic devm_kzalloc() Ahmad Fatoum
2026-10-28 9:14 ` [PATCH 13/19] driver: add devm_add_action() Ahmad Fatoum
2026-10-28 9:14 ` [PATCH 14/19] test: self: add devm_ selftest Ahmad Fatoum
2026-10-28 9:14 ` Ahmad Fatoum [this message]
2026-10-28 9:14 ` [PATCH 16/19] driver: make dev_platform_ioremap_resource() device-managed Ahmad Fatoum
2026-10-28 9:14 ` [PATCH 17/19] treewide: rename dev_platform_ioremap_resource() to devm_platform_ioremap_resource() Ahmad Fatoum
2026-10-28 9:14 ` [PATCH 18/19] driver: make dev_platform_get_and_ioremap_resource() device-managed Ahmad Fatoum
2026-10-28 9:14 ` [PATCH 19/19] serial: stm32: request the registers with devm_request_mem_resource() Ahmad Fatoum
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=20261028092442.3300753-16-a.fatoum@pengutronix.de \
--to=a.fatoum@pengutronix.de \
--cc=a.fatoum@barebox.org \
--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