regmap: Fix bus context frees on init failure
Subsystems (3)
cover regmap: Fix bus context frees on init failure
Since b1ef855c6260, a failing __devm_regmap_init() can free the bus context, so spi-avmm and sch56xx free it twice. Patch 1 restores the old behaviour. Patch 2 fixes the mmio context leak on init failure.
Tested with failslab on an Orange Pi 5 (devm mmio regmap): kmemleak finds the mmio leak, and with patch 2 alone KASAN reports the double free. With both patches, no reports. spi-avmm and sch56xx were not run.
Developed with Claude (Anthropic), which wrote the code and the commit messages. I reviewed every change.
---
Arun Iyengar (2):
regmap: Don't free the bus context when devm init fails
regmap: mmio: Free the context if regmap init fails
drivers/base/regmap/regmap-mmio.c | 18 ++++++++++++++---- drivers/base/regmap/regmap.c | 20 ++++++++++++-------- 2 files changed, 26 insertions(+), 12 deletions(-)
---
base-commit: 222b61e9c570dac6c7a786d039e4c226624b426d
change-id: 20261129-regmap-devm-ctx-2955bb2c1077
Best regards,
Signature · 1 line
Arun Iyengar <[email protected]>
PATCH 1/2 regmap: Don't free the bus context when devm init fails
Since the devres is added after __regmap_init(), a failure to add it runs regmap_exit(), which frees the bus context. Callers that free their context when __devm_regmap_init() fails (spi-avmm, sch56xx) then free it twice.
Allocate the devres before __regmap_init() again.
--- drivers/base/regmap/regmap.c | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-)
drivers/base/regmap/regmap.c +12 -8
@@ -1212,9 +1212,9 @@ struct regmap *__regmap_init(struct device *dev, }EXPORT_SYMBOL_GPL (__regmap_init); -static void devm_regmap_release (void *regmap) +static void devm_regmap_release (struct device *dev,void *res) { -regmap_exit (regmap); +regmap_exit (*(struct regmap **)res); } struct regmap *__devm_regmap_init(struct device *dev,
@@ -1224,17 +1224,21 @@ struct regmap *__devm_regmap_init(struct device *dev,struct lock_class_key *lock_key, const char *lock_name) { - struct regmap *regmap; -int ret; + struct regmap **ptr, *regmap; + + ptr =devres_alloc (devm_regmap_release,sizeof (*ptr),GFP_KERNEL ); +if (!ptr) +return ERR_PTR (-ENOMEM ); regmap =__regmap_init (dev, bus, bus_context, config, lock_key, lock_name); -if (IS_ERR (regmap)) +if (IS_ERR (regmap)) { +devres_free (ptr);return regmap; + } - ret =devm_add_action_or_reset (dev, devm_regmap_release, regmap); -if (ret) -return ERR_PTR (ret); + *ptr = regmap; +devres_add (dev, ptr);return regmap; }
Signature · 1 line
2.55.0
PATCH 2/2 regmap: mmio: Free the context if regmap init fails
In reply to Arun IyengarThe MMIO context is freed only by regmap_exit(), so it leaks, together with its prepared clock, when __regmap_init() or __devm_regmap_init() fails. Free it on that error.
The devm part needs the previous patch: without it a failed devres add has already freed the context.
--- drivers/base/regmap/regmap-mmio.c | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-)
drivers/base/regmap/regmap-mmio.c +14 -4
@@ -559,13 +559,18 @@ struct regmap *__regmap_init_mmio_clk(struct device *dev, const char *clk_id, const char *lock_name) {struct regmap_mmio_context *ctx; +struct regmap *map; ctx =regmap_mmio_gen_context (dev, clk_id, regs, config);if (IS_ERR (ctx))return ERR_CAST (ctx); -return __regmap_init (dev, ®map_mmio, ctx, config, - lock_key, lock_name); + map =__regmap_init (dev, ®map_mmio, ctx, config, + lock_key, lock_name); +if (IS_ERR (map)) +regmap_mmio_free_context (ctx); + +return map; }EXPORT_SYMBOL_GPL (__regmap_init_mmio_clk);
@@ -577,13 +582,18 @@ struct regmap *__devm_regmap_init_mmio_clk(struct device *dev, const char *lock_name) {struct regmap_mmio_context *ctx; +struct regmap *map; ctx =regmap_mmio_gen_context (dev, clk_id, regs, config);if (IS_ERR (ctx))return ERR_CAST (ctx); -return __devm_regmap_init (dev, ®map_mmio, ctx, config, - lock_key, lock_name); + map =__devm_regmap_init (dev, ®map_mmio, ctx, config, + lock_key, lock_name); +if (IS_ERR (map)) +regmap_mmio_free_context (ctx); + +return map; }EXPORT_SYMBOL_GPL (__devm_regmap_init_mmio_clk);
Signature · 1 line
2.55.0
[email protected]
AI reviewSashiko has reviewed this 2-patch series and found no issues.
Sashiko has reviewed this 2-patch series and found no issues.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]
Quoted 8 lines · Arun Iyengar
Since
b1ef855c6260, a failing__devm_regmap_init()can free the bus context, so spi-avmm and sch56xx free it twice. Patch 1 restores the old behaviour. Patch 2 fixes the mmio context leak on init failure.Tested with failslab on an Orange Pi 5 (devm mmio regmap): kmemleak finds the mmio leak, and with patch 2 alone KASAN reports the double free. With both patches, no reports. spi-avmm and sch56xx were not run.
Quoted 8 lines
Developed with Claude (Anthropic), which wrote the code and the commit messages. I reviewed every change.
---
Arun Iyengar (2):
regmap: Don't free the bus context when devm init fails
regmap: mmio: Free the context if regmap init fails
Mark Brown
maintainerQuoted 11 lines · Arun Iyengar
regmap: Fix bus context frees on init failure
Since
b1ef855c6260, a failing__devm_regmap_init()can free the bus context, so spi-avmm and sch56xx free it twice. Patch 1 restores the old behaviour. Patch 2 fixes the mmio context leak on init failure.Tested with failslab on an Orange Pi 5 (devm mmio regmap): kmemleak finds the mmio leak, and with patch 2 alone KASAN reports the double free. With both patches, no reports. spi-avmm and sch56xx were not run.
[...]
Applied to
https://git.kernel.org/pub/scm/linux/kernel/git/broonie/regmap.git for-7.5
Thanks!
[1/2] regmap: Don't free the bus context when devm init fails
https://git.kernel.org/broonie/regmap/c/e648012fd321 [2/2] regmap: mmio: Free the context if regmap init fails https://git.kernel.org/broonie/regmap/c/a0ca2536454a
All being well this means that it will be integrated into the linux-next tree (usually sometime in the next 24 hours) and sent to Linus during the next merge window (or sooner if it is a bug fix), however if problems are discovered then the patch may be dropped or reverted.
You may get further e-mails resulting from automated or manual testing and review of the tree, please engage with people reporting problems and send followup patches addressing any issues that are reported if needed.
If any updates are required or you are submitting further changes they should be sent as incremental updates against current git, existing patches will not be replaced.
Please add any relevant lists and maintainers to the CCs when replying to this mail.
Thanks,
Mark