From: Yuho Choi <dbgh9129@gmail.com>
To: Mark Brown <broonie@kernel.org>,
Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
"Rafael J . Wysocki" <rafael@kernel.org>,
Danilo Krummrich <dakr@kernel.org>
Cc: linux-kernel@vger.kernel.org, driver-core@lists.linux.dev,
Yuho Choi <dbgh9129@gmail.com>
Subject: [PATCH v1] regmap: ram: fix memory leaks in __regmap_init_ram() on error
Date: Thu, 16 Apr 2026 19:56:30 -0400 [thread overview]
Message-ID: <20260416235630.78408-1-dbgh9129@gmail.com> (raw)
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, ®map_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)
next reply other threads:[~2026-04-16 23:56 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-04-16 23:56 Yuho Choi [this message]
2026-04-17 11:07 ` [PATCH v1] regmap: ram: fix memory leaks in __regmap_init_ram() on error Mark Brown
2026-04-17 14:50 ` 최유호
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=20260416235630.78408-1-dbgh9129@gmail.com \
--to=dbgh9129@gmail.com \
--cc=broonie@kernel.org \
--cc=dakr@kernel.org \
--cc=driver-core@lists.linux.dev \
--cc=gregkh@linuxfoundation.org \
--cc=linux-kernel@vger.kernel.org \
--cc=rafael@kernel.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