public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH v1] regmap: ram: fix memory leaks in __regmap_init_ram() on error
@ 2026-04-16 23:56 Yuho Choi
  2026-04-17 11:07 ` Mark Brown
  0 siblings, 1 reply; 3+ messages in thread
From: Yuho Choi @ 2026-04-16 23:56 UTC (permalink / raw)
  To: Mark Brown, Greg Kroah-Hartman, Rafael J . Wysocki,
	Danilo Krummrich
  Cc: linux-kernel, driver-core, Yuho Choi

Two allocations in __regmap_init_ram() are not cleaned up on failure.

If the kzalloc_objs() for data->written fails, data->read is returned
with no way for the caller to free it.

If __regmap_init() fails, neither data->read nor data->written is freed
because its error paths do not call bus->free_context() (which is
regmap_ram_free_context() here). Only regmap_exit() does, and that is
never reached on an init failure.

Free the allocated arrays before returning any error.

Fixes: f6352424e37e ("regmap: Add RAM backed register map")
Signed-off-by: Yuho Choi <dbgh9129@gmail.com>
---
 drivers/base/regmap/regmap-ram.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)

diff --git a/drivers/base/regmap/regmap-ram.c b/drivers/base/regmap/regmap-ram.c
index 0272d53fead11..c7356b0d8c832 100644
--- a/drivers/base/regmap/regmap-ram.c
+++ b/drivers/base/regmap/regmap-ram.c
@@ -71,11 +71,17 @@ struct regmap *__regmap_init_ram(struct device *dev,
 		return ERR_PTR(-ENOMEM);
 
 	data->written = kzalloc_objs(bool, config->max_register + 1);
-	if (!data->written)
+	if (!data->written) {
+		kfree(data->read);
 		return ERR_PTR(-ENOMEM);
+	}
 
 	map = __regmap_init(dev, &regmap_ram, data, config,
 			    lock_key, lock_name);
+	if (IS_ERR(map)) {
+		kfree(data->read);
+		kfree(data->written);
+	}
 
 	return map;
 }
-- 
2.50.1 (Apple Git-155)


^ permalink raw reply related	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2026-04-17 14:50 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-04-16 23:56 [PATCH v1] regmap: ram: fix memory leaks in __regmap_init_ram() on error Yuho Choi
2026-04-17 11:07 ` Mark Brown
2026-04-17 14:50   ` 최유호

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox