Linux PCI subsystem development
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Aneesh Kumar K.V (Arm)" <aneesh.kumar@kernel.org>
Cc: kvm@vger.kernel.org, linux-pci@vger.kernel.org
Subject: Re: [PATCH v10 18/18] PCI/TSM: Wait for vIOMMUs to release the TSM before unregistering
Date: Wed, 25 Nov 2026 08:43:23 +0000	[thread overview]
Message-ID: <sashiko-outbox-232929@kernel.org> (raw)
In-Reply-To: <20261125081121.379481-19-aneesh.kumar@kernel.org>

Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [High] PCI/TSM: use-after-free on tsm_dev in tsm_unregister() and
  sev_tsm_uninit()
- [Medium] PCI/TSM: holding registration_lock across wait in
  pci_tsm_destroy()
--

commit 12207b3dbfdc30d4f6117b169c90eebf960192b8
Author: Aneesh Kumar K.V (Arm) <aneesh.kumar@kernel.org>

PCI/TSM: Wait for vIOMMUs to release the TSM before unregistering

This commit adds a user reference count to struct tsm_dev so that
tsm_unregister() waits for active TSM users (such as IOMMUFD vIOMMUs) to
drain before tearing down backend resources, and coordinates PF0 removal in
pci_tsm_destroy() with TSM unregistration via registration_lock.

> diff --git a/drivers/pci/tsm.c b/drivers/pci/tsm.c
> index b10bf0b2d5b7b..8b3f6d1513c6c 100644
> --- a/drivers/pci/tsm.c
> +++ b/drivers/pci/tsm.c
[ ... ]
> @@ -728,52 +750,93 @@ static void __pci_tsm_destroy(struct pci_dev *pdev, struct tsm_dev *tsm_dev)
>  
>  void pci_tsm_destroy(struct pci_dev *pdev)
>  {
> -	struct pci_tsm_pf0 *pf0 = NULL;
> -	struct completion *drained;
> -	bool interrupted = false;
> -	long rc;
> +	struct tsm_dev *tsm_dev = NULL;
> +	bool registration_locked = false;
>  
>  	down_write(&pci_tsm_rwsem);
> -	if (pdev->tsm &&
> -	    is_link_tsm(pdev->tsm->tsm_dev) && is_pci_tsm_pf0(pdev)) {
> -		pf0 = to_pci_tsm_pf0(pdev->tsm);
> -		drained = &pf0->dsm_context_drain_complete;
> +	if (pdev->tsm && is_link_tsm(pdev->tsm->tsm_dev) &&
> +	    is_pci_tsm_pf0(pdev)) {
> +		struct pci_tsm_pf0 *pf0 = to_pci_tsm_pf0(pdev->tsm);
> +
>  		mutex_lock(&pf0->lock);
>  		pf0->removing = true;
> -
>  		mutex_unlock(&pf0->lock);
> -		/* An unused DSM may never have completed dsm_context_drain_complete. */
> -		if (pf0->dsm_context_users)
> -			rc = try_wait_for_completion(drained);
> -		else
> -			rc = 1;
> -
> -		/* Context release needs the read side of pci_tsm_rwsem. */
> -		up_write(&pci_tsm_rwsem);
> -		while (rc <= 0) {
> -			if (interrupted) {
> -				rc = wait_for_completion_timeout(drained, HZ * 10);
> +
> +		if (pf0->dsm_context_users) {
> +			struct completion *drain_complete;
> +			const char *users;
> +			bool interrupted = false;
> +			long rc;
> +
> +			tsm_dev = pdev->tsm->tsm_dev;
> +			/*
> +			 * Retain tsm_dev and its users_drain_complete while waiting
> +			 * without the TSM registration lock.
> +			 */
> +			get_device(&tsm_dev->dev);
> +			registration_locked = tsm_trylock_registration(tsm_dev);
> +			if (registration_locked) {
> +				/* The TSM registration lock prevents freeing PF0 here. */
> +				drain_complete = &pf0->dsm_context_drain_complete;
> +				users = "IOMMUFD device assignment contexts";
>  			} else {
> -				rc = wait_for_completion_interruptible_timeout(drained,
> -									       HZ * 10);
> -				if (rc < 0) {
> -					interrupted = true;
> -					pci_warn(pdev, "Task \"%s\" (%d) blocked until vdevices are released\n",
> -						 current->comm, task_pid_nr(current));
> +				/*
> +				 * The DSM completion is embedded in PF0.
> +				 * If unregister runs while pci_tsm_rwsem
> +				 * is dropped, it may free PF0 after the
> +				 * last context signals the completion but
> +				 * before this waiter finishes accessing
> +				 * it.
> +				 *
> +				 * Without the registration lock, wait on
> +				 * the TSM's users_drain_complete completion
> +				 * instead. The device reference keeps that
> +				 * completion allocated even if unregister
> +				 * finishes before this waiter resumes.
> +				 */
> +
> +				drain_complete = &tsm_dev->users_drain_complete;
> +				users = "virtual IOMMUs and other TSM users";
> +			}
> +
> +			/* Context release needs the read side of pci_tsm_rwsem. */
> +			up_write(&pci_tsm_rwsem);
> +			rc = try_wait_for_completion(drain_complete);
> +			while (rc <= 0) {
> +				if (interrupted) {
> +					rc = wait_for_completion_timeout(drain_complete, HZ * 10);
> +				} else {
> +					rc = wait_for_completion_interruptible_timeout(
> +						drain_complete, HZ * 10);
> +					if (rc < 0) {
> +						interrupted = true;
> +						pci_warn(pdev, "Task \"%s\" (%d) removing Device Security Manager; waiting for %s\n",
> +							 current->comm, task_pid_nr(current),
> +							 users);
> +					}
>  				}
> +
> +				if (!rc)
> +					pci_warn(pdev, "Device Security Manager removal blocked; waiting for %s\n",
> +						 users);
>  			}
> -			if (!rc)
> -				pci_warn(pdev, "TSM connection is in use, waiting for vdevices\n");
> +
> +			down_write(&pci_tsm_rwsem);
>  		}
> -		down_write(&pci_tsm_rwsem);
>  	}
> +
>  	__pci_tsm_destroy(pdev, NULL);
>  	up_write(&pci_tsm_rwsem);
> +	if (registration_locked)
> +		tsm_unlock_registration(tsm_dev);
> +
> +	if (tsm_dev)
> +		put_device(&tsm_dev->dev);
>  }

[Severity: Medium]
Does holding tsm_dev->registration_lock for read across the userspace drain
wait in pci_tsm_destroy() block a concurrent tsm_unregister() in
down_write(&tsm_dev->registration_lock) before it can drop its initial
tsm_dev->users reference?

If tsm_unregister() is called while pci_tsm_destroy() is waiting on
pf0->dsm_context_drain_complete, tsm_unregister() blocks in an unbounded
TASK_UNINTERRUPTIBLE sleep on down_write(&tsm_dev->registration_lock)
instead of reaching tsm_put_user() and the timed wait in
tsm_wait_for_users().

While tsm_unregister() is queued as a writer on registration_lock,
down_read_trylock() in tsm_trylock_registration() also fails for any second
PF0 entering pci_tsm_destroy(), causing that second PF0 to wait on
tsm_dev->users_drain_complete even though tsm_unregister() has not yet
called tsm_put_user() to release the initial users reference.  Even when
tsm_unregister() already holds the write lock, falling back to
tsm_dev->users_drain_complete makes PF0 removal wait for all vIOMMUs on the
TSM rather than just PF0's own dsm_context_users, which deadlocks if
userspace closes PF0's vdevice on hot-unplug and waits for PF0 removal to
finish before destroying the vIOMMU.

> diff --git a/drivers/virt/coco/tsm-core.c b/drivers/virt/coco/tsm-core.c
> index 8cb6fdaf8a846..fa717e0702088 100644
> --- a/drivers/virt/coco/tsm-core.c
> +++ b/drivers/virt/coco/tsm-core.c
[ ... ]
> @@ -137,12 +216,63 @@ struct tsm_dev *tsm_register(struct device *parent, struct pci_tsm_ops *pci_ops)
[ ... ]
> +int tsm_unregister(struct tsm_dev *tsm_dev)
>  {
> -	/* Remove the class lookup first. */
> +	down_write(&tsm_dev->registration_lock);
> +	if (tsm_dev->unregistering) {
> +		up_write(&tsm_dev->registration_lock);
> +		return -ENODEV;
> +	}
> +
> +	tsm_dev->unregistering = true;
> +
>  	device_del(&tsm_dev->dev);
> -	pci_tsm_unregister(tsm_dev);
> +	/* No new user references can be acquired while the write lock is held. */
> +	tsm_put_user(tsm_dev);
> +	tsm_wait_for_users(tsm_dev);
> +
> +	if (IS_ENABLED(CONFIG_PCI_TSM))
> +		pci_tsm_unregister(tsm_dev);
> +
> +	up_write(&tsm_dev->registration_lock);
>  	put_device(&tsm_dev->dev);
> +	return 0;
>  }

[Severity: High]
Can a second concurrent call to tsm_unregister() trigger a use-after-free on
tsm_dev?

tsm_unregister() checks tsm_dev->unregistering under
tsm_dev->registration_lock to reject a second caller with -ENODEV, but the
first caller drops the final registration reference with
put_device(&tsm_dev->dev) immediately after up_write().  In SEV-TIO,
sev_firmware_shutdown() calls sev_tsm_uninit() outside sev_cmd_mutex, and
sev_tsm_uninit() leaves sev->tsmdev non-NULL while tsm_wait_for_users()
sleeps waiting for userspace to close vIOMMUs:

CPU0                                  CPU1
sev_platform_shutdown()
  sev_firmware_shutdown()
    sev_tsm_uninit()
      tsm_unregister(tsm_dev)
        down_write(&registration_lock)
        tsm_dev->unregistering = true
        device_del(&tsm_dev->dev)
        tsm_wait_for_users(tsm_dev)
                                      sev_dev_destroy()
                                        sev_firmware_shutdown()
                                          sev_tsm_uninit()
                                            tsm_unregister(tsm_dev)
                                              down_write(&registration_lock)
        ...
        up_write(&registration_lock)
        put_device(&tsm_dev->dev)
          tsm_release()
            kfree(tsm_dev)
                                              if (tsm_dev->unregistering)
                                                up_write(&registration_lock)
                                                <-- use-after-free

In addition, if tsm_unregister() returns -ENODEV to sev_tsm_uninit(),
sev_tsm_uninit() warns and returns early without waiting for the ongoing
unregistration, and sev_firmware_shutdown() proceeds to run
__sev_firmware_shutdown() and kfree(sev->tio_status) while the first caller
is still draining TSM users or running pci_tsm_unregister().

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261125081121.379481-1-aneesh.kumar@kernel.org?part=18

  reply	other threads:[~2026-11-25  8:43 UTC|newest]

Thread overview: 24+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-25  8:11 [PATCH v10 00/18] iommufd: TSM-backed virtual IOMMUs for confidential guests Aneesh Kumar K.V (Arm)
2026-11-25  8:11 ` [PATCH v10 01/18] MAINTAINERS: Update TSM file coverage and repository Aneesh Kumar K.V (Arm)
2026-11-25  8:11 ` [PATCH v10 02/18] virt: coco: tsm: Require PCI operations when registering a TSM Aneesh Kumar K.V (Arm)
2026-11-25  8:11 ` [PATCH v10 03/18] virt: coco: tsm: Remove the device from lookup before PCI teardown Aneesh Kumar K.V (Arm)
2026-11-25  8:11 ` [PATCH v10 04/18] virt: coco: tsm: Complete PCI registration before publishing the device Aneesh Kumar K.V (Arm)
2026-11-25  8:11 ` [PATCH v10 05/18] virt: coco: tsm: Register the TSM class before device drivers Aneesh Kumar K.V (Arm)
2026-11-25  8:25   ` sashiko-bot
2026-11-25  8:11 ` [PATCH v10 06/18] iommufd/device: Associate KVM file pointer with iommufd_device Aneesh Kumar K.V (Arm)
2026-11-25  8:11 ` [PATCH v10 07/18] iommufd/viommu: Keep a reference to the KVM file Aneesh Kumar K.V (Arm)
2026-11-25  8:11 ` [PATCH v10 08/18] iommufd: Add a vDEVICE operation ioctl framework Aneesh Kumar K.V (Arm)
2026-11-25  8:11 ` [PATCH v10 09/18] PCI/TSM: Remove the legacy guest request interface Aneesh Kumar K.V (Arm)
2026-11-25  8:11 ` [PATCH v10 10/18] PCI/TSM: Add helpers to prevent disconnect while in use Aneesh Kumar K.V (Arm)
2026-11-25  8:11 ` [PATCH v10 11/18] PCI/TSM: Remove the legacy bind and unbind interface Aneesh Kumar K.V (Arm)
2026-11-25  8:11 ` [PATCH v10 12/18] iommufd/viommu: Select vIOMMU operations before allocation Aneesh Kumar K.V (Arm)
2026-11-25  8:28   ` sashiko-bot
2026-11-25  8:11 ` [PATCH v10 13/18] PCI/TSM: Add helpers to look up and reference a device's TSM Aneesh Kumar K.V (Arm)
2026-11-25  8:11 ` [PATCH v10 14/18] iommufd/viommu: Identify physical IOMMU backing with a tag Aneesh Kumar K.V (Arm)
2026-11-25  8:11 ` [PATCH v10 15/18] iommufd/viommu: Allow PCI TSM backends to provide vIOMMU operations Aneesh Kumar K.V (Arm)
2026-11-25  8:33   ` sashiko-bot
2026-11-25  8:11 ` [PATCH v10 16/18] iommufd/viommu: Allow vIOMMUs without a parent HWPT Aneesh Kumar K.V (Arm)
2026-11-25  8:11 ` [PATCH v10 17/18] PCI/TSM: Wait for DSM users before removing PF0 Aneesh Kumar K.V (Arm)
2026-11-25  8:11 ` [PATCH v10 18/18] PCI/TSM: Wait for vIOMMUs to release the TSM before unregistering Aneesh Kumar K.V (Arm)
2026-11-25  8:43   ` sashiko-bot [this message]
2026-11-25  8:43 ` [PATCH v10 00/18] iommufd: TSM-backed virtual IOMMUs for confidential guests 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-232929@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=aneesh.kumar@kernel.org \
    --cc=kvm@vger.kernel.org \
    --cc=linux-pci@vger.kernel.org \
    --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