[PATCH 0/7] regmap: cleanups for regcache and regmap init paths
From: Peng Fan (OSS)
Date: Wed Sep 30 2026 - 05:51:27 EST
Following up the previous regmap guard lock patchset [1].
[1] https://lore.kernel.org/all/179062150526.139547.3870545428656485907.b4-ty@b4/#r
This is the 2nd batch to do more cleanup.
This series tidies up locking, error handling, and resource management
across the regmap core and regcache.
- Extract regcache_locked_op() and regcache_locked_exit() helpers to
replace open-coded lock/unlock pairs in regcache_init() and
regcache_exit(), avoiding the cleanup.h-vs-goto concern that
prevented the earlier scoped lock guard conversion.
- Extract __regcache_sync() to deduplicate the sync dispatch logic
shared between regcache_sync() and regcache_sync_region().
- Flatten control flow in regcache_init(), regcache_lookup_reg(), and
regcache_sync_block() by using early returns, moving guards into
helpers, and merging redundant sanity checks.
- Switch regmap_register_patch() from krealloc() to krealloc_array()
for overflow-safe element-count arithmetic.
- Convert bus struct copies in regmap-i2c and regmap-spi from
kmemdup() to devm_kmemdup(), letting devres handle lifetime and
removing the free_on_exit flag from struct regmap_bus entirely.
- Simplify the early error paths in __regmap_init() by returning
directly instead of jumping to a label that does nothing but return.
- NULL map->reg_defaults after freeing in regcache_exit() to prevent
a potential double-free if regmap_reinit_cache() later fails during
cache re-initialisation.
Tested with the regmap KUnit suite (drivers/base/regmap/regmap-kunit.c)
with lockdep (PROVE_LOCKING, DEBUG_LOCK_ALLOC, DEBUG_ATOMIC_SLEEP) enabled.
Built clean with sparse (C=1).
Signed-off-by: Peng Fan <peng.fan@xxxxxxx>
---
Peng Fan (7):
regcache: extract locked helpers to replace open-coded lock/unlock pairs
regcache: extract __regcache_sync() to deduplicate sync dispatch
regcache: simplify control flow and reduce nesting
regmap: use krealloc_array() in regmap_register_patch()
regmap: use devm_kmemdup() for bus copies and remove free_on_exit
regmap: return errors directly before map allocation in __regmap_init()
regmap: fix potential double-free of map->reg_defaults on cache reinit
drivers/base/regmap/regcache.c | 132 +++++++++++++++++++--------------------
drivers/base/regmap/regmap-i2c.c | 3 +-
drivers/base/regmap/regmap-spi.c | 3 +-
drivers/base/regmap/regmap.c | 31 +++------
include/linux/regmap.h | 2 -
5 files changed, 77 insertions(+), 94 deletions(-)
---
base-commit: 6474fa070f2b8013b4b87350b775b8c3be6e8aac
change-id: 20260930-regmap-lock-2nd-f481885c24d6
Best regards,
--
Peng Fan <peng.fan@xxxxxxx>