mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Baolu Lu <baolu.lu@linux.intel.com>
To: Qihang <q.h.hack.winter@gmail.com>
Cc: baolu.lu@linux.intel.com, Joerg Roedel <joro@8bytes.org>,
	Will Deacon <will@kernel.org>, Jason Gunthorpe <jgg@nvidia.com>,
	Robin Murphy <robin.murphy@arm.com>,
	iommu@lists.linux.dev, linux-kernel@vger.kernel.org
Subject: Re: [PATCH 0/2] iommu/io-pgfault: reject partial faults after device removal
Date: Sat, 21 Nov 2026 09:25:28 +0800	[thread overview]
Message-ID: <aceb9e9f-7ba3-48e2-8945-95179f22581d@linux.intel.com> (raw)
In-Reply-To: <CAH78GvuUtU=zhe-Qbn9k73D3U5n1Ye1=8wtQqR7vCEpDzgNUJg@mail.gmail.com>

On 11/20/2026 5:26 PM, Qihang wrote:
> Hi Baolu,
> 
> Thanks. I agree that the driver must drain pending faults before
> calling iopf_queue_remove_device(). My selftest removed the mock
> driver's serialization, so it doesn't establish that fault reporting
> can overlap removal in an unmodified driver. I'm withdrawing this
> series and won't resend it.
> 
> I still have two questions about where the drivers establish that guarantee.
> 
> On VT-d, blocking_domain_attach_dev() calls
> iopf_for_domain_remove(old, dev) before device_block_translation(dev).
> When that drops the last IOPF reference, it calls
> iopf_queue_remove_device(). On an SM-capable IOMMU, for a device that
> isn't a real DMA subdevice, the only PRQ drain on this path is reached
> through device_block_translation() → intel_pasid_tear_down_entry() →
> intel_iommu_drain_pasid_prq(). By then iopf_refcount is zero, so the
> drain returns early.
> 
> In the same file, identity_domain_attach_dev() calls
> device_block_translation(dev) first and releases the old domain's IOPF
> reference afterwards. The comment there explains that putting the
> device in the blocking state leaves the IOPF reference count
> unchanged. Should blocking_domain_attach_dev() use the same order? If
> the current order is intentional, what ensures that pending PRQ
> reports for this device have drained before the last reference is
> dropped?

Good catch. This is a bug in the vt-d driver. I already have a patch
that changes the order in blocking_domain_attach_dev(). I will submit it
for discussion after the necessary assessment.

> On AMD, detach_device() calls iopf_queue_flush_dev() immediately
> before amd_iommu_iopf_remove_device(). The flush helper waits for
> queued work and documents that the caller must have flushed the
> low-level queue first. The PPR interrupt thread calls
> iommu_report_device_fault() directly, so waiting for queued work alone
> doesn't establish that the thread has finished reporting faults for
> this device. What in the detach path ensures that PPR reporting for
> this device has quiesced before the device is removed from the IOPF
> queue?
> 
> I may be missing a step in both cases. A pointer would be enough.
> 
> Thanks,
> Qihang
> 
> On Fri, Nov 20, 2026 at 3:56 PM Baolu Lu <baolu.lu@linux.intel.com> wrote:
>>
>> On 11/17/2026 9:54 AM, Qihang wrote:
>>> iopf_queue_remove_device() kfree()s the entries on
>>> fault_param->partial without unlinking them, leaving the list head
>>> pointing into freed memory.  Since a74c077b9021 the fault parameter is
>>> refcounted and can outlive the drain, a report that took its reference
>>> earlier can still list_add() through those pointers.  Unlinking
>>> instead turns that into a leak: the late node lands on a parameter
>>> that is no longer on queue->devices, whose final put does not free
>>> partial faults.
>>>
>>> This needs a device removed from its iopf queue while a non-last
>>> (partial) request is in flight, with the caller not blocking new reports
>>> for the duration.
>>>
>>> Patch 1 unlinks each partial before freeing it, matching
>>> iopf_queue_discard_partial().
>>>
>>> Patch 2 adds fault_param->removing, set and tested under
>>> fault_param->lock: a late partial is either drained, or rejected and
>>> freed by report_partial_fault() (-ENODEV).  It covers partial-list
>>> admission only; callers must still quiesce reports for the remaining
>>> lifetimes.
>>>
>>> Both patches are scoped to fault_param->partial, not the group path,
>>> the attach handle, or paths that never call iopf_queue_remove_device().
>>>
>>> Verified on v7.4-rc3 with KASAN + DEBUG_LIST and the iommufd mock
>>> driver, its caller-side IOPF serialization deliberately removed and
>>> test-only delays added; this shows the mechanism, not production
>>> reachability.  The runs cover queue-before-removal,
>>> removal-before-insertion, concurrent late reporters and re-enable.
>>> Each round queues one partial before the removal and then races the
>>> late reporters against the drain, so a clean run ends with every node
>>> accounted for: 1800 = 900 + 900 and 3600 = 900 + 2700 (allocated =
>>> drained + rejected), counted at a final removal with no reporter in
>>> flight.  Nothing was left on fault_param->partial and KASAN stayed
>>> quiet.
>>>
>>> With patch 1 reverted, the report is:
>>>
>>>     BUG: KASAN: slab-use-after-free in __list_add_valid_or_report
>>>     Freed by task 1: kfree <- iopf_queue_remove_device
>>>     56 bytes inside a freed 64-byte region (struct iopf_fault, list.prev)
>>>
>>> With patch 1 only, the same reproducer runs 1460 rounds without a KASAN
>>> report and leaks one late node per round, which patch 2 addresses.
>>>
>>> Reachability on an unmodified production kernel, and any security
>>> impact, remain unconfirmed: the test shows memory corruption and a
>>> KASAN panic on the modified mock driver, not a user-triggerable bug on
>>> a stock kernel.
>>>
>>> An AI coding assistant helped find this defect and build the
>>> reproducer and instrumentation, which are available on request along
>>> with the KASAN logs.
>>
>> +Jason
>>
>> The issues addressed by both patches are false positives. They rely on a
>> fault being reported for a device after iopf_queue_remove_device() has
>> been called for that device. The iommu driver must not allow that.
>> Pending i/o page faults must be drained before the device is removed
>> from its iommu's io page fault queue.
>>
>> Thanks,
>> baolu

Thanks,
baolu

      reply	other threads:[~2026-11-21  1:25 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-17  1:54 Qihang
2026-11-17  1:54 ` [PATCH 1/2] iommu/io-pgfault: unlink partial faults before freeing them Qihang
2026-11-17  1:54 ` [PATCH 2/2] iommu/io-pgfault: reject partial faults after the device is removed Qihang
2026-11-20  7:56 ` [PATCH 0/2] iommu/io-pgfault: reject partial faults after device removal Baolu Lu
2026-11-20  9:26   ` Qihang
2026-11-21  1:25     ` Baolu Lu [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=aceb9e9f-7ba3-48e2-8945-95179f22581d@linux.intel.com \
    --to=baolu.lu@linux.intel.com \
    --cc=iommu@lists.linux.dev \
    --cc=jgg@nvidia.com \
    --cc=joro@8bytes.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=q.h.hack.winter@gmail.com \
    --cc=robin.murphy@arm.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

all inboxes | Powered by JetHome®