From: Peng Fan <peng.fan@linux.dev>
To: Mark Brown <broonie@kernel.org>,
Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
"Rafael J. Wysocki" <rafael@kernel.org>,
Danilo Krummrich <dakr@kernel.org>,
Johan Hovold <johan@kernel.org>,
Lucas Tanure <tanureal@opensource.cirrus.com>
Cc: linux-kernel@vger.kernel.org, driver-core@lists.linux.dev,
Peng Fan <peng.fan@nxp.com>
Subject: [PATCH v2 09/10] regmap: use cleanup API in __regmap_init()
Date: Fri, 13 Nov 2026 23:31:57 +0800 [thread overview]
Message-ID: <20261113-regmap-improve-v2-9-5dfd6a89078f@nxp.com> (raw)
In-Reply-To: <20261113-regmap-improve-v2-0-5dfd6a89078f@nxp.com>
From: Peng Fan <peng.fan@nxp.com>
Convert the error unwinding in __regmap_init() to the scope-based
cleanup API and drop all of the err_* labels. A dedicated
regmap_init_release() helper unwinds a partially initialised map in the
same order as the old labels.
The err label only returned ERR_PTR(ret) after the per-device bus
allocation was removed, so the two early failure paths now return
directly as well.
regcache_exit() requires an initialised cache, so map->cache_type is now
set right before regcache_init() instead of during the initial map setup
and reset to REGCACHE_NONE if regcache_init() fails. This makes
regcache_exit() safe to call from the cleanup path at any point. Nothing
between the old and new assignment reads cache_type.
No functional change.
Signed-off-by: Peng Fan <peng.fan@nxp.com>
---
drivers/base/regmap/regmap.c | 69 +++++++++++++++++++++-----------------------
1 file changed, 33 insertions(+), 36 deletions(-)
diff --git a/drivers/base/regmap/regmap.c b/drivers/base/regmap/regmap.c
index 04cbf15adcb0..8a68c9bcebc8 100644
--- a/drivers/base/regmap/regmap.c
+++ b/drivers/base/regmap/regmap.c
@@ -1037,6 +1037,22 @@ static int regmap_init_format(struct regmap *map,
return 0;
}
+static void regmap_init_release(struct regmap *map)
+{
+ if (!map)
+ return;
+
+ regcache_exit(map);
+ regmap_range_exit(map);
+ kfree(map->work_buf);
+ if (map->hwlock)
+ hwspin_lock_free(map->hwlock);
+ kfree_const(map->name);
+ kfree(map);
+}
+
+DEFINE_FREE(regmap_init_release, struct regmap *, regmap_init_release(_T));
+
struct regmap *__regmap_init(struct device *dev,
const struct regmap_bus *bus,
void *bus_context,
@@ -1044,30 +1060,25 @@ struct regmap *__regmap_init(struct device *dev,
struct lock_class_key *lock_key,
const char *lock_name)
{
- struct regmap *map;
bool fast_io;
int ret;
- if (!config) {
- ret = -EINVAL;
- goto err;
- }
+ if (!config)
+ return ERR_PTR(-EINVAL);
- map = kzalloc_obj(*map);
- if (map == NULL) {
- ret = -ENOMEM;
- goto err;
- }
+ struct regmap *map __free(regmap_init_release) = kzalloc_obj(*map);
+ if (map == NULL)
+ return ERR_PTR(-ENOMEM);
ret = regmap_set_name(map, config);
if (ret)
- goto err_map;
+ return ERR_PTR(ret);
fast_io = (bus && bus->fast_io) || config->fast_io;
ret = regmap_set_lock(map, config, fast_io, lock_key, lock_name);
if (ret)
- goto err_name;
+ return ERR_PTR(ret);
/*
* When we write in fast-paths with regmap_bulk_write() don't allocate
@@ -1119,7 +1130,6 @@ struct regmap *__regmap_init(struct device *dev,
map->writeable_noinc_reg = config->writeable_noinc_reg;
map->readable_noinc_reg = config->readable_noinc_reg;
map->reg_default_cb = config->reg_default_cb;
- map->cache_type = config->cache_type;
spin_lock_init(&map->async_lock);
INIT_LIST_HEAD(&map->async_list);
@@ -1148,7 +1158,7 @@ struct regmap *__regmap_init(struct device *dev,
REGMAP_ENDIAN_NATIVE,
REGMAP_ENDIAN_NATIVE);
if (ret)
- goto err_hwlock;
+ return ERR_PTR(ret);
} else if (!bus) {
map->reg_read = config->reg_read;
map->reg_write = config->reg_write;
@@ -1172,45 +1182,32 @@ struct regmap *__regmap_init(struct device *dev,
regmap_get_reg_endian(bus, config),
regmap_get_val_endian(dev, bus, config));
if (ret)
- goto err_hwlock;
+ return ERR_PTR(ret);
}
-
ret = regmap_init_ranges(map, config);
if (ret)
- goto err_range;
+ return ERR_PTR(ret);
if (bus && bus->init_limits)
bus->init_limits(map, config);
+ map->cache_type = config->cache_type;
ret = regcache_init(map, config);
- if (ret != 0)
- goto err_range;
+ if (ret) {
+ map->cache_type = REGCACHE_NONE;
+ return ERR_PTR(ret);
+ }
if (dev) {
ret = regmap_attach_dev(dev, map, config);
if (ret != 0)
- goto err_regcache;
+ return ERR_PTR(ret);
} else {
regmap_debugfs_init(map);
}
- return map;
-
-err_regcache:
- regcache_exit(map);
-err_range:
- regmap_range_exit(map);
- kfree(map->work_buf);
-err_hwlock:
- if (map->hwlock)
- hwspin_lock_free(map->hwlock);
-err_name:
- kfree_const(map->name);
-err_map:
- kfree(map);
-err:
- return ERR_PTR(ret);
+ return_ptr(map);
}
EXPORT_SYMBOL_GPL(__regmap_init);
--
2.56.0
next prev parent reply other threads:[~2026-11-13 15:32 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-13 15:31 [PATCH v2 00/10] regmap: clean up and improve __regmap_init() Peng Fan
2026-11-13 15:31 ` [PATCH v2 01/10] regmap: fix reinit cache failure handling Peng Fan
2026-11-13 15:31 ` [PATCH v2 02/10] regmap-i2c: keep bus raw write limit when applying read quirk Peng Fan
2026-11-13 15:31 ` [PATCH v2 03/10] regmap-i2c: fix write limit clamping for adapter quirks Peng Fan
2026-11-13 15:31 ` [PATCH v2 04/10] regmap: deduplicate fast_io checks Peng Fan
2026-11-13 15:31 ` [PATCH v2 05/10] regmap: factor out regmap_set_lock() Peng Fan
2026-11-13 15:31 ` [PATCH v2 06/10] regmap: factor out regmap_init_ranges() Peng Fan
2026-11-13 15:31 ` [PATCH v2 07/10] regmap: factor out format initialisation into helpers Peng Fan
2026-11-13 15:31 ` [PATCH v2 08/10] regmap: drop regmap_bus::free_on_exit Peng Fan
2026-11-13 15:31 ` Peng Fan [this message]
2026-11-13 15:31 ` [PATCH v2 10/10] regmap: kunit: add test for regmap_bus::init_limits Peng Fan
2026-11-14 13:01 ` [PATCH v2 00/10] regmap: clean up and improve __regmap_init() Peng Fan
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=20261113-regmap-improve-v2-9-5dfd6a89078f@nxp.com \
--to=peng.fan@linux.dev \
--cc=broonie@kernel.org \
--cc=dakr@kernel.org \
--cc=driver-core@lists.linux.dev \
--cc=gregkh@linuxfoundation.org \
--cc=johan@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=peng.fan@nxp.com \
--cc=rafael@kernel.org \
--cc=tanureal@opensource.cirrus.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.