Linux PCI subsystem development
 help / color / mirror / Atom feed
From: Samiullah Khawaja <skhawaja@google.com>
To: David Matlack <dmatlack@google.com>
Cc: Vipin Sharma <vipinsh@google.com>,
	Alex Williamson <alex@shazbot.org>,
	 Joerg Roedel <joro@8bytes.org>,
	Bjorn Helgaas <bhelgaas@google.com>,
	 Nicolin Chen <nicolinc@nvidia.com>,
	Jason Gunthorpe <jgg@ziepe.ca>,
	 Kevin Tian <kevin.tian@intel.com>,
	Robin Murphy <robin.murphy@arm.com>,
	jrhilke@google.com,  tatashin@google.com,
	Will Deacon <will@kernel.org>,
	kvm@vger.kernel.org,  iommu@lists.linux.dev,
	linux-pci@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [RFC PATCH 0/1] vfio: circular locking dependency in pci_dev_reset_iommu_prepare()
Date: Thu, 27 Aug 2026 18:03:15 +0000	[thread overview]
Message-ID: <apB6tsN6cHz9Iuv3@google.com> (raw)
In-Reply-To: <CALzav=f5wUUt4mKZn0tvQez6_1iYPcb8eX5q-XjabTNy03JiNg@mail.gmail.com>

On Wed, Aug 26, 2026 at 01:27:20PM -0700, David Matlack wrote:
>On Tue, Aug 25, 2026 at 12:06 PM David Matlack <dmatlack@google.com> wrote:
>>
>> On 2026-08-21 12:35 PM, Vipin Sharma wrote:
>>
>> > ================================================================================
>> > Potential Solutions Suggested by AI
>> > ================================================================================
>> >
>> > 1. Decouple iommu_setup_dma_ops() from group->mutex in drivers/iommu/iommu.c:
>> >    iommu_setup_dma_ops() only requires struct device * and the domain pointer
>> >    (group->default_domain); it does not mutate any fields in struct iommu_group.
>> >    Moving the iommu_setup_dma_ops() calls after mutex_unlock(&group->mutex) in
>> >    iommu_probe_device(), bus_iommu_probe(), and iommu_group_store_type() breaks
>> >    the initial &group->mutex -> cpu_hotplug_lock dependency.

The group->mutex is needed here also since it sets up the dma_ops on the
default_domain that is currently attached to the device. And those
attachments are protected with group->mutex.
>>
>> Are there any other code paths that rely on group->mutex -->
>> mm->mmap_lock ordering? If so fixing this one case wouldn't help.
>>
>> > 2. Avoid holding down_write(&vdev->memory_lock) across pci_try_reset_function()
>> >    in VFIO:
>> >    vfio-pci could zap active BAR mappings under memory_lock and set a state
>> >    flag / disable memory decoding, drop memory_lock before calling
>> >    pci_try_reset_function(), and then re-acquire memory_lock to re-enable
>> >    memory. While resetting, any concurrent user fault will see the memory
>> >    disabled condition and return VM_FAULT_SIGBUS safely.
>>
>> This would change the userspace-visible behavior of faulting on a VFIO
>> device BAR from "block until reset is done and the succeed" to "fail
>> with SIGBUS". And it would allow VFIO to access VFIO device BARs during
>> the reset through vfio_pci_core_iowrite*().
>>
>> But I think we can extend this idea to solve those problems by
>> introducing a wait queue for tasks to sit on while a device is being
>> reset.
>>
>> e.g. Something like this (completely untested and partially written by AI):
>>
>> From: David Matlack <dmatlack@google.com>
>> Date: Tue, 25 Aug 2026 18:37:52 +0000
>> Subject: [PATCH] vfio/pci: Avoid circular locking dependency during device reset
>>
>> Avoid a circular locking dependency during VFIO device reset by dropping
>> vdev->memory_lock prior to calling PCI reset functions
>> (pci_try_reset_function() and pci_reset_bus()). Introduce an explicit reset
>> state flag (vdev->resetting) and wait queue (vdev->reset_done_wq) to stall
>> concurrent BAR page faults and MMIO accesses during reset without holding
>> vdev->memory_lock across PCI reset operations.
>...
>
>This approach does not look ideal. The implementation has a bug where
>concurrent resets can lead to vdev->resetting being cleared too early.
>And from a maintainability perspective, there are more call sites that
>currently take memory_lock that would probably also have to be updated
>to wait for vdev->resetting to become false.
>
>> >
>> > 3. Refine synchronization in pci_dev_reset_iommu_prepare():
>> >    Evaluate if attaching to the blocking domain and pausing ATS during device
>> >    reset can be protected using more fine-grained locking or atomic state
>> >    flags without holding the coarse &group->mutex.

This might not work as reset_iommu_prepare() changes the domain of the
device being reset and those things protected by the group->mutex.
>>
>> I don't know enough about this part of the kernel to say, but this would
>> directly address the new lock ordering dependency vdev->memory_lock -->
>> group->mutex introduced by commit f5b16b802174 ("PCI: Suspend iommu function
>> prior to resetting a device"), which is what led to this lockdep error.

Thanks,
Sami

      parent reply	other threads:[~2026-08-27 18:03 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-08-21 19:35 [RFC PATCH 0/1] vfio: circular locking dependency in pci_dev_reset_iommu_prepare() Vipin Sharma
2026-08-21 19:35 ` [RFC PATCH 1/1] selftests: vfio: Add mmap fault and device reset test Vipin Sharma
2026-08-21 19:46   ` sashiko-bot
2026-08-25 19:06 ` [RFC PATCH 0/1] vfio: circular locking dependency in pci_dev_reset_iommu_prepare() David Matlack
2026-08-26 20:27   ` David Matlack
2026-08-27 17:36     ` David Matlack
2026-08-27 18:03     ` Samiullah Khawaja [this message]

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=apB6tsN6cHz9Iuv3@google.com \
    --to=skhawaja@google.com \
    --cc=alex@shazbot.org \
    --cc=bhelgaas@google.com \
    --cc=dmatlack@google.com \
    --cc=iommu@lists.linux.dev \
    --cc=jgg@ziepe.ca \
    --cc=joro@8bytes.org \
    --cc=jrhilke@google.com \
    --cc=kevin.tian@intel.com \
    --cc=kvm@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-pci@vger.kernel.org \
    --cc=nicolinc@nvidia.com \
    --cc=robin.murphy@arm.com \
    --cc=tatashin@google.com \
    --cc=vipinsh@google.com \
    --cc=will@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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox