Re: [PATCH v2 2/2] PCI: Fix runtime PM race with PME polling

From: Lukas Wunner
Date: Tue Jan 23 2024 - 05:47:11 EST


On Mon, Jan 22, 2024 at 03:50:03PM -0700, Alex Williamson wrote:
> On Mon, 22 Jan 2024 23:17:30 +0100 Lukas Wunner <lukas@xxxxxxxxx> wrote:
> > On Thu, Jan 18, 2024 at 11:50:49AM -0700, Alex Williamson wrote:
> > > To do that I used pm_runtime_get_if_active(), but in retrospect this
> > > requires the device to be in RPM_ACTIVE so we end up skipping anything
> > > suspended or transitioning.
> >
> > How about dropping the calls to pm_runtime_get_if_active() and
> > pm_runtime_put() and instead simply do:
> >
> > if (pm_runtime_suspended(&pdev->dev) &&
> > pdev->current_state != PCI_D3cold)
> > pci_pme_wakeup(pdev, NULL);
>
> Do we require that the polled device is in the RPM_SUSPENDED state?

If the device is RPM_SUSPENDING, why immediately resume it for polling?
It's sufficient to poll it the next time around, i.e. 1 second later.

Likewise, if it's already RPM_RESUMING or RPM_ACTIVE anyway, no need
to poll PME.

This leaves RPM_SUSPENDED as the only state in which it makes sense to
poll.


> Also pm_runtime_suspended() can also only be trusted while holding the
> device power.lock, we need a usage count reference to maintain that
> state.

Why? Let's say there's a race and the device resumes immediately after
we call pm_runtime_suspended() here. So we might call pci_pme_wakeup()
gratuitouly. So what? No biggie.


> + if (bdev) {
> + spin_lock_irq(&bdev->power.lock);

Hm, I'd expect that lock to be internal to the PM core,
although there *are* a few stray users outside of it.

Thanks,

Lukas