The Linux Kernel Mailing List
 help / color / mirror / Atom feed
From: Carlo Szelinsky <github@szelinsky.de>
To: "Mark Brown" <broonie@kernel.org>,
	"Liam Girdwood" <lgirdwood@gmail.com>,
	"André Draszik" <andre.draszik@linaro.org>
Cc: linux-kernel@vger.kernel.org
Subject: regulator: core: use-after-free of rdev->bdev when a probe fails
Date: Sat, 28 Nov 2026 14:29:14 +0100	[thread overview]
Message-ID: <20261128132914.3954393-1-github@szelinsky.de> (raw)

Hi,

since 304f5784e972 ("regulator: core: reresolve unresolved supplies
when available"), unregistering a regulator that had an unresolved
supply when it registered gives a "does not have a release() function"
WARNING. If the unregister comes soon after the register, it is also a
use-after-free. I see it on net-next f89726ffabef with PSE patches on
top, none of which touch the regulator core. drivers/regulator/core.c
is the same in mainline (7cd8495c0f16).

How it happens:

 1. regulator_register() cannot resolve the supply, so it adds
    rdev->bdev on regulator_bus. The only driver on that bus prefers
    asynchronous probing, so device_add() queues
    __device_attach_async_helper(), which holds a reference on the bdev.
 2. The driver's probe then fails, for example with -EPROBE_DEFER, and
    devres calls regulator_unregister().
 3. device_unregister(&rdev->bdev) drops the bdev's references on its
    parent rdev->dev, but the bdev is still alive through the async
    helper. device_unregister(&rdev->dev) then frees rdev in
    regulator_dev_release(), and the bdev is part of rdev.
 4. The async helper then reads the freed bdev, and its last
    put_device() reaches device_release(), which WARNs because the bdev
    has no release function. Without the async race, the WARNING comes
    from regulator_unregister() itself. From the code, the bdev is
    only removed in regulator_unregister(), so a regulator whose supply
    was resolved later should WARN there too. I have not tested that.

So any driver that registers a regulator with an unresolved supply and
then fails its probe can hit this. I hit it while testing a PSE
controller patch: the PI regulators registered while their supply was
not ready yet (not registered, or its device still probing), then the
PSE driver deferred its probe. In QEMU (arm64, KASAN) one boot gave
37 of these WARNINGs and this report:

  BUG: KASAN: slab-use-after-free in __device_attach_async_helper+0x260/0x298
  Read of size 8 at addr ffff00000535c4f8 by task kworker/u4:2/30
  Call trace:
   __device_attach_async_helper+0x260/0x298
   async_run_entry_fn+0x80/0x264
   process_one_work+0x850/0x1844
   worker_thread+0x73c/0xca4
  Allocated by task 1:
   regulator_register+0x220/0x1d10
   devm_regulator_register+0x58/0xbc
   pse_controller_register+0xa84/0x1aa4
  Freed by task 1:
   kfree+0x1f0/0x4bc
   regulator_dev_release+0x74/0x98
   device_release+0xfc/0x204
   kobject_put+0x138/0x234
   device_unregister+0x20/0x2c
   regulator_unregister+0x228/0x2e8
   devm_rdev_release+0x24/0x40
   ...
   really_probe+0x3b4/0x59c

  Device 'regulator.6.bdev' does not have a release() function, it is
  broken and must be fixed. See Documentation/core-api/kobject.rst.
  WARNING: drivers/base/core.c:2642 at device_release+0x184/0x204
  Call trace:
   device_release+0x184/0x204
   kobject_put+0x138/0x234
   device_unregister+0x20/0x2c
   regulator_unregister+0x25c/0x2e8
   devm_rdev_release+0x24/0x40
   ...
   really_probe+0x3b4/0x59c

The reproducer is an out-of-tree test driver, so I have not seen it
with an in-tree driver yet.

One way to fix it would be to let the bdev hold a reference on
rdev->dev: take it before device_add(&rdev->bdev) and drop it in a new
release function for the bdev. Then rdev stays alive until the last
bdev reference is gone. I have not written or tested that, so I leave
the fix to you.

I found and analysed this with help from an LLM assistant.

Thanks,
Carlo

                 reply	other threads:[~2026-11-28 13:29 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=20261128132914.3954393-1-github@szelinsky.de \
    --to=github@szelinsky.de \
    --cc=andre.draszik@linaro.org \
    --cc=broonie@kernel.org \
    --cc=lgirdwood@gmail.com \
    --cc=linux-kernel@vger.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