Linux PCI subsystem development
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Priyank Rathod" <rathodpriyank@google.com>
Cc: linux-pci@vger.kernel.org
Subject: Re: [PATCH v8 2/3] PCI/LMR: Add Lane Margining at the Receiver debugfs interface
Date: Wed, 25 Nov 2026 06:45:59 +0000	[thread overview]
Message-ID: <sashiko-outbox-232732@kernel.org> (raw)
In-Reply-To: <20261125-pcie-link-endpoints-v8-2-19d49c17c05f@google.com>

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] PCI/LMR: use-after-free on port->dev in pci_lmr_release() on
  teardown race
--

commit c126cec11a02e704f7895b57855ddcc4e39ac680
Author: Priyank Rathod <rathodpriyank@google.com>

PCI/LMR: Add Lane Margining at the Receiver debugfs interface

This commit adds a debugfs interface for the PCIe Lane Margining at the
Receiver Extended Capability on Downstream Ports and Function 0 devices
below them, allowing user space to run margining sessions on links at
16.0 GT/s or faster.

> diff --git a/drivers/pci/pcie/margin.c b/drivers/pci/pcie/margin.c
> new file mode 100644
> index 0000000000000..df73fed885fa5
> --- /dev/null
> +++ b/drivers/pci/pcie/margin.c
[ ... ]
> +static struct pci_dev *pci_lmr_teardown(struct pci_lmr_port *port)
> +{
> +	struct pci_dev *partner = port->partner;
> +	bool removing;
> +
> +	lockdep_assert_held(&port->lock);
> +
> +	if (port->state != LMR_ACTIVE)
> +		return NULL;
> +
> +	removing = !pci_dev_is_added(port->dev) || !pci_dev_is_added(partner);
> +
> +	if (!pci_dev_is_disconnected(port->dev))
> +		pci_lmr_reset_lanes(port);
> +	pci_lmr_autonomous_restore(port);
> +	if (!removing)
> +		pci_lmr_aspm_restore(port);
> +	else
> +		port->aspm_inhibited = false;
> +
> +	pci_lmr_forget_rx(port);
> +	scoped_guard(mutex, &pci_lmr_mutex) {
> +		port->partner = NULL;
> +		pci_lmr_set_state(port, LMR_IDLE);
> +	}
> +
> +	return partner;
> +}
> +
> +static void pci_lmr_release(struct pci_lmr_port *port, struct pci_dev *partner)
> +{
> +	pm_runtime_put_sync(&partner->dev);
> +	pm_runtime_put_sync(&port->dev->dev);
> +	pci_dev_put(partner);
> +}
[ ... ]
> +static void pci_lmr_end_sessions(struct pci_dev *dev)
> +{
> +	struct pci_lmr_port *p;
> +	struct pci_dev *ended;
> +
> +	for (;;) {
> +		scoped_guard(mutex, &pci_lmr_mutex)
> +			p = pci_lmr_find_session(dev);
> +		if (!p)
> +			break;
> +
> +		scoped_guard(mutex, &p->lock)
> +			ended = pci_lmr_teardown(p);
> +		if (ended)
> +			pci_lmr_release(p, ended);
> +		pci_lmr_port_put(p);
> +	}
> +}
> +
> +/**
> + * pci_lmr_exit - End margining on a device that is being removed
> + * @dev: PCI device
> + *
> + * Called from pci_destroy_dev() for every device.  Removes the debugfs
> + * directory of @dev, waiting for file operations in progress, and ends
> + * every session that involves @dev, whichever end of the link hosts it.
> + */
> +void pci_lmr_exit(struct pci_dev *dev)
> +{
> +	struct pci_lmr_port *port;
> +
> +	scoped_guard(mutex, &pci_lmr_mutex)
> +		port = pci_lmr_find_port(dev);
> +
> +	if (port)
> +		debugfs_remove(port->dir);
> +
> +	pci_lmr_end_sessions(dev);
> +
> +	if (!port)
> +		return;
> +
> +	scoped_guard(mutex, &pci_lmr_mutex)
> +		list_del(&port->node);
> +	pci_lmr_port_put(port);
> +}

[Severity: Medium]
Can pci_lmr_release() use port->dev after it has been freed if another
context tears the session down concurrently with pci_destroy_dev(port->dev)?

Neither pci_lmr_init() nor pci_lmr_start() takes a pci_dev_get() reference
on port->dev, and port->kref only pins struct pci_lmr_port.  Because
pci_lmr_teardown() sets port->state to LMR_IDLE before its caller drops
port->lock and runs pci_lmr_release(), pci_lmr_exit(port->dev) does not
wait for pci_lmr_release() to finish:

CPU0                                  CPU1
pci_destroy_dev(partner)
  pci_lmr_exit(partner)
    pci_lmr_end_sessions(partner)
      p = pci_lmr_find_session(...)
      pci_lmr_teardown(p)
        p->partner = NULL
        p->state = LMR_IDLE
                                      pci_destroy_dev(p->dev)
                                        pci_lmr_exit(p->dev)
                                          debugfs_remove(p->dir)
                                          pci_lmr_end_sessions(p->dev)
                                            /* state is LMR_IDLE, returns */
                                          list_del(&p->node)
                                          pci_lmr_port_put(p)
                                        device_del(&p->dev->dev)
                                        put_device(&p->dev->dev)
      pci_lmr_release(p, ended)
        pm_runtime_put_sync(&p->dev->dev)  <-- use-after-free

Taking a pci_dev_get(port->dev) reference for the lifetime of the session
(or of struct pci_lmr_port) and keeping the session non-idle until
pci_lmr_release() completes would prevent pci_destroy_dev() from tearing
down and freeing port->dev early.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261125-pcie-link-endpoints-v8-0-19d49c17c05f@google.com?part=2

  reply	other threads:[~2026-11-25  6:45 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-25  6:31 [PATCH v8 0/3] PCI: Add Lane Margining at the Receiver debugfs interface Priyank Rathod
2026-11-25  6:31 ` [PATCH v8 1/3] PCI/ASPM: Add internal pcie_aspm_inhibit() helper Priyank Rathod
2026-11-25  6:31 ` [PATCH v8 2/3] PCI/LMR: Add Lane Margining at the Receiver debugfs interface Priyank Rathod
2026-11-25  6:45   ` sashiko-bot [this message]
2026-11-25  6:31 ` [PATCH v8 3/3] selftests/pcie_lmr: Add tests for the Lane Margining " Priyank Rathod
2026-11-25  6:46 ` [PATCH v8 0/3] PCI: Add Lane Margining at the Receiver " sashiko-bot

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=sashiko-outbox-232732@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=linux-pci@vger.kernel.org \
    --cc=rathodpriyank@google.com \
    --cc=sashiko-reviews@lists.linux.dev \
    /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