linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 0/2] regulator: core: fix some resource leaks
@ 2022-12-01 12:27 Yang Yingliang
  2022-12-01 12:27 ` [PATCH 1/2] regulator: core: fix module refcount leak in set_supply() Yang Yingliang
                   ` (2 more replies)
  0 siblings, 3 replies; 5+ messages in thread
From: Yang Yingliang @ 2022-12-01 12:27 UTC (permalink / raw)
  To: lgirdwood, broonie, javier, mirq-linux, christian; +Cc: linux-kernel

This patchset fixes resource leaks in some error cases.

Yang Yingliang (2):
  regulator: core: fix module refcount leak in set_supply()
  regulator: core: fix resource leak in regulator_register()

 drivers/regulator/core.c | 2 ++
 1 file changed, 2 insertions(+)

-- 
2.25.1


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

* [PATCH 1/2] regulator: core: fix module refcount leak in set_supply()
  2022-12-01 12:27 [PATCH 0/2] regulator: core: fix some resource leaks Yang Yingliang
@ 2022-12-01 12:27 ` Yang Yingliang
  2022-12-01 12:27 ` [PATCH 2/2] regulator: core: fix resource leak in regulator_register() Yang Yingliang
  2022-12-01 18:16 ` [PATCH 0/2] regulator: core: fix some resource leaks Mark Brown
  2 siblings, 0 replies; 5+ messages in thread
From: Yang Yingliang @ 2022-12-01 12:27 UTC (permalink / raw)
  To: lgirdwood, broonie, javier, mirq-linux, christian; +Cc: linux-kernel

If create_regulator() fails in set_supply(), the module refcount
needs be put to keep refcount balanced.

Fixes: e2c09ae7a74d ("regulator: core: Increase refcount for regulator supply's module")
Signed-off-by: Yang Yingliang <yangyingliang@huawei.com>
---
 drivers/regulator/core.c | 1 +
 1 file changed, 1 insertion(+)

diff --git a/drivers/regulator/core.c b/drivers/regulator/core.c
index bc4bba899aea..4a7d8ec415e8 100644
--- a/drivers/regulator/core.c
+++ b/drivers/regulator/core.c
@@ -1638,10 +1638,11 @@ static int set_supply(struct regulator_dev *rdev,
 	if (!try_module_get(supply_rdev->owner))
 		return -ENODEV;
 
 	rdev->supply = create_regulator(supply_rdev, &rdev->dev, "SUPPLY");
 	if (rdev->supply == NULL) {
+		module_put(supply_rdev->owner);
 		err = -ENOMEM;
 		return err;
 	}
 	supply_rdev->open_count++;
 
-- 
2.25.1


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

* [PATCH 2/2] regulator: core: fix resource leak in regulator_register()
  2022-12-01 12:27 [PATCH 0/2] regulator: core: fix some resource leaks Yang Yingliang
  2022-12-01 12:27 ` [PATCH 1/2] regulator: core: fix module refcount leak in set_supply() Yang Yingliang
@ 2022-12-01 12:27 ` Yang Yingliang
  2022-12-01 17:19   ` Mark Brown
  2022-12-01 18:16 ` [PATCH 0/2] regulator: core: fix some resource leaks Mark Brown
  2 siblings, 1 reply; 5+ messages in thread
From: Yang Yingliang @ 2022-12-01 12:27 UTC (permalink / raw)
  To: lgirdwood, broonie, javier, mirq-linux, christian; +Cc: linux-kernel

I got some resource leak reports while doing fault injection test:

  OF: ERROR: memory leak, expected refcount 1 instead of 100,
  of_node_get()/of_node_put() unbalanced - destroy cset entry:
  attach overlay node /i2c/pmic@64/regulators/buck1

unreferenced object 0xffff88810deea000 (size 512):
  comm "490-i2c-rt5190a", pid 253, jiffies 4294859840 (age 5061.046s)
  hex dump (first 32 bytes):
    00 00 00 00 ad 4e ad de ff ff ff ff 00 00 00 00  .....N..........
    ff ff ff ff ff ff ff ff a0 1e 00 a1 ff ff ff ff  ................
  backtrace:
    [<00000000d78541e2>] kmalloc_trace+0x21/0x110
    [<00000000b343d153>] device_private_init+0x32/0xd0
    [<00000000be1f0c70>] device_add+0xb2d/0x1030
    [<00000000e3e6344d>] regulator_register+0xaf2/0x12a0
    [<00000000e2f5e754>] devm_regulator_register+0x57/0xb0
    [<000000008b898197>] rt5190a_probe+0x52a/0x861 [rt5190a_regulator]

unreferenced object 0xffff88810b617b80 (size 32):
  comm "490-i2c-rt5190a", pid 253, jiffies 4294859904 (age 5060.983s)
  hex dump (first 32 bytes):
    72 65 67 75 6c 61 74 6f 72 2e 32 38 36 38 2d 53  regulator.2868-S
    55 50 50 4c 59 00 ff ff 29 00 00 00 2b 00 00 00  UPPLY...)...+...
  backtrace:
    [<000000009da9280d>] __kmalloc_node_track_caller+0x44/0x1b0
    [<0000000025c6a4e5>] kstrdup+0x3a/0x70
    [<00000000790efb69>] create_regulator+0xc0/0x4e0
    [<0000000005ed203a>] regulator_resolve_supply+0x2d4/0x440
    [<0000000045796214>] regulator_register+0x10b3/0x12a0
    [<00000000e2f5e754>] devm_regulator_register+0x57/0xb0
    [<000000008b898197>] rt5190a_probe+0x52a/0x861 [rt5190a_regulator]

After calling regulator_resolve_supply(), the 'rdev->supply' is set
by set_supply(), after this set, in the error path, the resources
need be released, so call regulator_put() to avoid the leaks.

Fixes: aea6cb99703e ("regulator: resolve supply after creating regulator")
Fixes: 8a866d527ac0 ("regulator: core: Resolve supply name earlier to prevent double-init")
Signed-off-by: Yang Yingliang <yangyingliang@huawei.com>
---
 drivers/regulator/core.c | 1 +
 1 file changed, 1 insertion(+)

diff --git a/drivers/regulator/core.c b/drivers/regulator/core.c
index 4a7d8ec415e8..42b683963f9e 100644
--- a/drivers/regulator/core.c
+++ b/drivers/regulator/core.c
@@ -5641,10 +5641,11 @@ regulator_register(const struct regulator_desc *regulator_desc,
 	mutex_lock(&regulator_list_mutex);
 	unset_regulator_supplies(rdev);
 	regulator_remove_coupling(rdev);
 	mutex_unlock(&regulator_list_mutex);
 wash:
+	regulator_put(rdev->supply);
 	kfree(rdev->coupling_desc.coupled_rdevs);
 	mutex_lock(&regulator_list_mutex);
 	regulator_ena_gpio_free(rdev);
 	mutex_unlock(&regulator_list_mutex);
 	put_device(&rdev->dev);
-- 
2.25.1


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

* Re: [PATCH 2/2] regulator: core: fix resource leak in regulator_register()
  2022-12-01 12:27 ` [PATCH 2/2] regulator: core: fix resource leak in regulator_register() Yang Yingliang
@ 2022-12-01 17:19   ` Mark Brown
  0 siblings, 0 replies; 5+ messages in thread
From: Mark Brown @ 2022-12-01 17:19 UTC (permalink / raw)
  To: Yang Yingliang; +Cc: lgirdwood, javier, mirq-linux, christian, linux-kernel

[-- Attachment #1: Type: text/plain, Size: 394 bytes --]

On Thu, Dec 01, 2022 at 08:27:06PM +0800, Yang Yingliang wrote:
> I got some resource leak reports while doing fault injection test:
> 
>   OF: ERROR: memory leak, expected refcount 1 instead of 100,
>   of_node_get()/of_node_put() unbalanced - destroy cset entry:
>   attach overlay node /i2c/pmic@64/regulators/buck1

This doesn't apply against current code, please check and resend.

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 488 bytes --]

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

* Re: [PATCH 0/2] regulator: core: fix some resource leaks
  2022-12-01 12:27 [PATCH 0/2] regulator: core: fix some resource leaks Yang Yingliang
  2022-12-01 12:27 ` [PATCH 1/2] regulator: core: fix module refcount leak in set_supply() Yang Yingliang
  2022-12-01 12:27 ` [PATCH 2/2] regulator: core: fix resource leak in regulator_register() Yang Yingliang
@ 2022-12-01 18:16 ` Mark Brown
  2 siblings, 0 replies; 5+ messages in thread
From: Mark Brown @ 2022-12-01 18:16 UTC (permalink / raw)
  To: Yang Yingliang, javier, christian, lgirdwood, mirq-linux; +Cc: linux-kernel

On Thu, 1 Dec 2022 20:27:04 +0800, Yang Yingliang wrote:
> This patchset fixes resource leaks in some error cases.
> 
> Yang Yingliang (2):
>   regulator: core: fix module refcount leak in set_supply()
>   regulator: core: fix resource leak in regulator_register()
> 
> drivers/regulator/core.c | 2 ++
>  1 file changed, 2 insertions(+)
> 
> [...]

Applied to

   https://git.kernel.org/pub/scm/linux/kernel/git/broonie/regulator.git for-next

Thanks!

[1/2] regulator: core: fix module refcount leak in set_supply()
      commit: da46ee19cbd8344d6860816b4827a7ce95764867
[2/2] regulator: core: fix resource leak in regulator_register()
      (no commit info)

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

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

end of thread, other threads:[~2022-12-01 18:16 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-12-01 12:27 [PATCH 0/2] regulator: core: fix some resource leaks Yang Yingliang
2022-12-01 12:27 ` [PATCH 1/2] regulator: core: fix module refcount leak in set_supply() Yang Yingliang
2022-12-01 12:27 ` [PATCH 2/2] regulator: core: fix resource leak in regulator_register() Yang Yingliang
2022-12-01 17:19   ` Mark Brown
2022-12-01 18:16 ` [PATCH 0/2] regulator: core: fix some resource leaks Mark Brown

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).