From: Lee Jones <lee@kernel.org>
To: "Nguyễn Công" <congnt264@gmail.com>
Cc: Pavel Machek <pavel@kernel.org>, Luca Weiss <luca@lucaweiss.eu>,
linux-leds@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH] leds: sgm3140: Put the child node reference on success
Date: Wed, 2 Dec 2026 16:18:47 +0000 [thread overview]
Message-ID: <20261202161847.GI43081@kernel.org> (raw)
In-Reply-To: <CAJpJ=Gvs08=k-VAnAB1-DsB6a_kc9T5Mn4ooxfgyELQZH=Cs3Q@mail.gmail.com>
On Thu, 26 Nov 2026, Nguyễn Công wrote:
> On Thu, Nov 26, 2026 at 7:41 PM Lee Jones <lee@kernel.org> wrote:
> >
> > On Wed, 25 Nov 2026, Cong Nguyen wrote:
> >
> > > sgm3140_probe() gets the LED child node with device_get_next_child_node(),
> > > which returns it with its reference count raised. The error path puts the
> > > reference, but a successful probe returns before reaching that code, so the
> > > reference is never dropped. The v4l2 flash sub-device takes its own
> > > reference on the node, and the LED class device only stores the pointer,
> >
> > As the commit message notes, devm_led_classdev_flash_register_ext()
> > stores 'init_data->fwnode' in 'led_cdev->dev->fwnode' without taking a
> > reference on it and led_classdev_unregister() subsequently calls
> > fwnode_handle_put(led_cdev->dev->fwnode) when the device is removed.
>
> Hi Lee,
> I could not find that call. led_classdev_unregister() only stops
> triggers and blinking, turns the LED off and calls device_unregister(),
> and device_create_release() just does kfree(). The only
> fwnode_handle_put() in led-class.c is in fwnode_led_get(), for the node
> it looked up itself.
>
> >
> > So 'led_cdev->dev->fwnode' takes ownership of the caller's 'child_node'
> > reference on successful registration. Dropping 'child_node' here on the
> > success path causes a reference underflow / use-after-free while the LED
> > device is registered and a double-put on unbind.
> >
> > In fact, there is already a bug on the error path if v4l2_flash_init()
> > fails after devm_led_classdev_flash_register_ext() has succeeded: 'goto
> > err' drops 'child_node' once, and then devres teardown calls
> > devm_led_classdev_release() -> led_classdev_unregister() which calls
> > fwnode_handle_put() a second time!
>
> v4l2_flash_init() takes its own reference on the node and puts it on
> its error path, so the one 'goto err' drops is the probe's. Other
> drivers drop their reference right after registering, for example
> leds-lt3593:
>
> init_data.fwnode = child;
> ret = devm_led_classdev_register_ext(dev, &led_data->cdev, &init_data);
> fwnode_handle_put(child);
>
> Is there a path I am missing?
You're right, led_classdev_unregister() doesn't call fwnode_handle_put()
directly. Sorry about that.
However, the underlying lifetime issue remains.
led_classdev_register_ext() assigns:
led_cdev->dev->fwnode = init_data->fwnode;
led_cdev->dev->of_node = to_of_node(init_data->fwnode);
Without calling fwnode_handle_get(). Those pointers remain live on
'led_cdev->dev' for the entire lifetime of the LED device (and are
dereferenced by triggers, sysfs, fwnode_led_get() consumers, etc.). If
the caller drops its reference as soon as probe() returns,
'led_cdev->dev->fwnode' is left holding an unreferenced pointer (in this
driver v4l2_flash_init() happens to take its own reference, but that is
dropped in v4l2_flash_release() before devm unregisters the LED device,
and when CONFIG_V4L2_FLASH_LED_CLASS is disabled v4l2_flash_init() is a
no-op stub that takes no reference at all).
That said, since LED core currently neither takes nor releases a
reference on 'init_data->fwnode', virtually every other LED driver drops
its 'child' reference after registration just as you pointed out with
leds-lt3593. Fixing 'led-class' to properly take and release a
reference in led_classdev_register_ext() / led_classdev_unregister()
would be needed if we wanted 'led_cdev->dev' to own one, and until then
the patch does match the existing practice across the subsystem.
--
Lee Jones
next prev parent reply other threads:[~2026-12-02 16:18 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-25 13:40 [PATCH] leds: sgm3140: Put the child node reference on success Cong Nguyen
2026-11-25 13:45 ` sashiko-bot
2026-11-25 15:15 ` Markus Elfring
2026-11-26 12:28 ` Nguyễn Công
2026-11-26 12:41 ` Lee Jones
2026-11-26 13:33 ` Nguyễn Công
2026-12-02 16:18 ` Lee Jones [this message]
2026-12-02 16:21 ` Lee Jones
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=20261202161847.GI43081@kernel.org \
--to=lee@kernel.org \
--cc=congnt264@gmail.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-leds@vger.kernel.org \
--cc=luca@lucaweiss.eu \
--cc=pavel@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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.