public inbox for driver-core@lists.linux.dev
 help / color / mirror / Atom feed
From: "Danilo Krummrich" <dakr@kernel.org>
To: "Ulf Hansson" <ulf.hansson@linaro.org>
Cc: "Saravana Kannan" <saravanak@kernel.org>,
	"Rafael J . Wysocki" <rafael@kernel.org>,
	"Greg Kroah-Hartman" <gregkh@linuxfoundation.org>,
	<linux-pm@vger.kernel.org>,
	"Sudeep Holla" <sudeep.holla@kernel.org>,
	"Cristian Marussi" <cristian.marussi@arm.com>,
	"Kevin Hilman" <khilman@baylibre.com>,
	"Stephen Boyd" <sboyd@kernel.org>,
	"Marek Szyprowski" <m.szyprowski@samsung.com>,
	"Bjorn Andersson" <andersson@kernel.org>,
	"Abel Vesa" <abel.vesa@oss.qualcomm.com>,
	"Peng Fan" <peng.fan@oss.nxp.com>,
	"Tomi Valkeinen" <tomi.valkeinen@ideasonboard.com>,
	"Maulik Shah" <maulik.shah@oss.qualcomm.com>,
	"Konrad Dybcio" <konradybcio@kernel.org>,
	"Thierry Reding" <thierry.reding@gmail.com>,
	"Jonathan Hunter" <jonathanh@nvidia.com>,
	"Geert Uytterhoeven" <geert@linux-m68k.org>,
	"Dmitry Baryshkov" <dmitry.baryshkov@oss.qualcomm.com>,
	<linux-arm-kernel@lists.infradead.org>,
	<linux-kernel@vger.kernel.org>,
	"Geert Uytterhoeven" <geert+renesas@glider.be>,
	<driver-core@lists.linux.dev>
Subject: Re: [PATCH v2 1/9] driver core: Enable suppliers to implement fine grained sync_state support
Date: Tue, 05 May 2026 14:46:49 +0200	[thread overview]
Message-ID: <DIAR5LATLBNK.1CSW7AD5VRN77@kernel.org> (raw)
In-Reply-To: <CAPDyKFoKNpUexGrM0zcysKMKcdYT-cU0_Q_AHbgiGPk_gU-3nA@mail.gmail.com>

On Tue May 5, 2026 at 1:12 PM CEST, Ulf Hansson wrote:
> On Wed, 22 Apr 2026 at 12:59, Danilo Krummrich <dakr@kernel.org> wrote:
>>
>> On Wed Apr 22, 2026 at 12:07 PM CEST, Ulf Hansson wrote:
>> > On Sat, 18 Apr 2026 at 13:23, Danilo Krummrich <dakr@kernel.org> wrote:
>> >> On Fri Apr 10, 2026 at 12:40 PM CEST, Ulf Hansson wrote:
>> >> > @@ -1126,6 +1128,9 @@ static void __device_links_queue_sync_state(struct device *dev,
>> >> >       if (dev->state_synced)
>> >> >               return;
>> >> >
>> >> > +     if (dev->driver && dev->driver->queue_sync_state)
>> >> > +             dev->driver->queue_sync_state(dev);
>> >>
>> >> This seems to be called without the device lock being held, which seems to allow
>> >> the queue_sync_state() callback to execute concurrently with remove(). This
>> >> opens the door for all kinds of UAF conditions in drivers.
>> >
>> > If that were the case, this whole function would be unsafe even before
>> > this change. I assume this isn't because of how the function is being
>> > called, but I may be wrong.
>>
>> This function does not issue any driver callbacks intentionally; the existing
>> sync_state() callback is deferred to device_links_flush_sync_list(), which is
>> called without the device_links_write_lock() held, but takes the device_lock()
>> to protect against other concurrent driver callbacks, such as remove().
>>
>> I.e. we can't take the device_lock() when the device_links_write_lock() is held,
>> as it would be prone to lock inversion.
>
> You are right, the device_lock() is needed in
> device_links_flush_sync_list() to protect against concurrent driver
> callbacks from being invoked, such as the ->remove().
>
> However, as part of the removal procedure in
> __device_release_driver(), we also need to account for device links.
> Hence we call device_links_busy() which takes the
> device_links_write_lock().

I don't think this is sufficient.

I wrapped my head around this about three weeks ago and I think there were
multiple cases where this falls apart. I think one case was:

  1. Consumer binds, supplier is added to deferred_sync. Consumer unbinds,
     link goes to AVAILABLE.

  2. device_links_busy(supplier) takes and releases device_links_write_lock(),
     finds no ACTIVE consumers, returns false -- drv->remove() starts without
     device_links_write_lock() held.

  3. device_links_supplier_sync_state_resume() takes device_links_write_lock(),
     finds supplier still on deferred_sync -- device_links_driver_cleanup()
     hasn't run yet, blocked on the lock. queue_sync_state() is called
     concurrently with drv->remove().

But again, I think that's what I concluded three weeks ago and this state
machine is rather tricky.

That said, *even if* we could prove that this works in all cases, it would be
very subtle, pretty fragile and not by design.

The whole state machine around sync state is already rather complicated. So, if
we really want to make it an invariant that it is valid to call bus device
callbacks without holding the device lock from
__device_links_queue_sync_state(), I think this has to be carefully reflected by
the overall design making it *very explicit* how this invariant holds up in all
cases.

Otherwise, I'd be rather concerned that this becomes a source of subtle bugs.

  reply	other threads:[~2026-05-05 12:46 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <20260410104058.83748-1-ulf.hansson@linaro.org>
     [not found] ` <CAPDyKFrPz9gaBBp6xV1=KkoemEfapc0p3POZxuBTvDw7Vamxtg@mail.gmail.com>
2026-04-18 11:23   ` [PATCH v2 0/9] driver core / pmdomain: Add support for fined grained sync_state Danilo Krummrich
     [not found] ` <20260410104058.83748-2-ulf.hansson@linaro.org>
2026-04-18 11:23   ` [PATCH v2 1/9] driver core: Enable suppliers to implement fine grained sync_state support Danilo Krummrich
2026-04-22 10:07     ` Ulf Hansson
2026-04-22 10:59       ` Danilo Krummrich
2026-05-05 11:12         ` Ulf Hansson
2026-05-05 12:46           ` Danilo Krummrich [this message]
2026-05-05 14:15             ` Ulf Hansson
     [not found] ` <20260410104058.83748-3-ulf.hansson@linaro.org>
2026-04-18 11:23   ` [PATCH v2 2/9] driver core: Add dev_set_drv_queue_sync_state() Danilo Krummrich
2026-04-22 10:25     ` Ulf Hansson
2026-04-22 11:30       ` Danilo Krummrich

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=DIAR5LATLBNK.1CSW7AD5VRN77@kernel.org \
    --to=dakr@kernel.org \
    --cc=abel.vesa@oss.qualcomm.com \
    --cc=andersson@kernel.org \
    --cc=cristian.marussi@arm.com \
    --cc=dmitry.baryshkov@oss.qualcomm.com \
    --cc=driver-core@lists.linux.dev \
    --cc=geert+renesas@glider.be \
    --cc=geert@linux-m68k.org \
    --cc=gregkh@linuxfoundation.org \
    --cc=jonathanh@nvidia.com \
    --cc=khilman@baylibre.com \
    --cc=konradybcio@kernel.org \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-pm@vger.kernel.org \
    --cc=m.szyprowski@samsung.com \
    --cc=maulik.shah@oss.qualcomm.com \
    --cc=peng.fan@oss.nxp.com \
    --cc=rafael@kernel.org \
    --cc=saravanak@kernel.org \
    --cc=sboyd@kernel.org \
    --cc=sudeep.holla@kernel.org \
    --cc=thierry.reding@gmail.com \
    --cc=tomi.valkeinen@ideasonboard.com \
    --cc=ulf.hansson@linaro.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