Kernel KVM virtualization development
 help / color / mirror / Atom feed
From: Baolu Lu <baolu.lu@linux.intel.com>
To: Samiullah Khawaja <skhawaja@google.com>
Cc: David Woodhouse <dwmw2@infradead.org>,
	Joerg Roedel <joro@8bytes.org>, Will Deacon <will@kernel.org>,
	Jason Gunthorpe <jgg@ziepe.ca>,
	Robin Murphy <robin.murphy@arm.com>,
	Kevin Tian <kevin.tian@intel.com>,
	Alex Williamson <alex@shazbot.org>, Shuah Khan <shuah@kernel.org>,
	iommu@lists.linux.dev, linux-kernel@vger.kernel.org,
	kvm@vger.kernel.org, Pratyush Yadav <pratyush@kernel.org>,
	Pasha Tatashin <pasha.tatashin@soleen.com>,
	David Matlack <dmatlack@google.com>,
	Andrew Morton <akpm@linux-foundation.org>,
	Pranjal Shrivastava <praan@google.com>,
	Vipin Sharma <vipinsh@google.com>
Subject: Re: [PATCH v4 08/18] iommu/vt-d: Clear unpreserved context entries during shutdown
Date: Thu, 27 Aug 2026 14:09:26 +0800	[thread overview]
Message-ID: <5862e3cc-e7e4-43be-a829-8569d12d6bb4@linux.intel.com> (raw)
In-Reply-To: <ao9ADjoGzgNX4t1M@google.com>

On 8/27/26 04:30, Samiullah Khawaja wrote:
> 
>>> +static void clear_unpreserved_context(struct device_domain_info 
>>> *info, u8 bus, u8 devfn)
>>> +{
>>> +    struct context_entry *context;
>>> +
>>> +    /*
>>> +     * This cleanup is done during shutdown, so it should be fine to 
>>> only
>>> +     * clear the entries here and issue one global invalidation 
>>> later to
>>> +     * invalidate all cleared entries.
>>> +     *
>>> +     * Note that the device IOTLB invalidation for unpreserved 
>>> devices is
>>> +     * skipped this way, but that should not be needed as the 
>>> devices are
>>> +     * quiesced at this point. This should improve the performance 
>>> of the
>>> +     * cleanup process and avoids any invalidation timeouts because 
>>> drivers
>>> +     * might have moved devices to D3 state.
>>> +     */
>>> +    context = iommu_context_addr(info->iommu, bus, devfn, 0);
>>> +    if (context) {
>>> +        context_clear_entry(context);
>>> +        __iommu_flush_cache(info->iommu, context, sizeof(*context));
>>
>> For tearing down a present context entry, please follow the VT-d
>> recommended sequence:
>>
>> - clear only the Present bit,
>> - flush the updated entry to memory if necessary,
>> - issue the required cache invalidations,
> 
> Do we still need the individual cache invalidations if we issue global
> invalidations (context, pasid-cache and iotlb), after clearing all
> entries, as they would be done if a new root table was being setup?
> 
> Looking at the VT-d specs (Invalidation of Translation Caches), each
> invalidation type (cache, pasid and iotlb) defines granularity in both
> register and queue based interface. And the granularity indicates that
> Global invalidations clear the cached entries for that specific type.
> For example following text is used for each cache type (in queue
> interface):
> 
>    Context-cache:
>    Global Invalidation (01b): All context-cache entries cached at the
>    remapping hardware are invalidated.
> 
>    Pasid-cache:
>    Global Invalidation (11b): All PASID-cache entries are invalidated.
> 
>    Iotlb:
>    Global Invalidation (01b):
>    - All IOTLB entries are invalidated.
>    - All paging-structure-cache entries are invalidated.
> 
> A similar note about using Global invalidation is suggested in the specs
> when setting up root table (Set Root Table Pointer Operation).
> 
>    ... software must perform a global invalidate of the contextcache,
>    PASID-cache (if applicable), and IOTLB, in that order. This is
>    required to ensure hardware references only the remapping structures
>    referenced by the new root table pointer and not stale cached entries.
> 
> Also please note that this is happening during dmar unit teardown and
> system shutdown, and while the context table entries in root table are
> being cleared, the memory is not freed until the global invalidation is
> issued.
> 
> Since this is during shutdown, issuing global invalidations instead of
> multiple individual invalidations for devices and aliases is simpler and
> would likely also have shutdown time improvements and reduce the
> blackout time during liveupdate.
> 
> Please let me know if my global invalidations and granularity
> understanding is not correct.

The global-invalidation approach (similar to the “set new root table”
flow) looks correct to me.

On the device side: if ATS is enabled, some translations may still be
cached in the device. My understanding is that unpreserved devices are
already DMA-quiesced and then go through reset + reprobe (as in a normal
reboot), which should flush those device-side caches. Are we aligned on
that assumption?

> 
> I added a comment at the top of this function to explain this, let me
> know if you want me to expand it with more details.
> 
>> - then clear the remaining fields of the entry. 

One important point remains: even if cache invalidation is deferred and
done globally, we should still clear the Present bit before clearing the
rest of a context entry. While Present is set, hardware may fetch the
256-bit entry in multiple chunks. Rewriting the full entry is not atomic
(it becomes multiple CPU writes), so hardware could observe a torn value
(a mix of old and new fields), which may lead to undefined behavior or
spurious faults.

So the recommended sequence is:
- clear only the Present bit,
- flush the updated entry to memory if necessary,
- [add a comment about the delayed global invalidation approach,]
- then clear the remaining fields of the entry.

Thanks,
baolu


  reply	other threads:[~2026-08-27  6:09 UTC|newest]

Thread overview: 40+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-08-08  2:27 [PATCH v4 00/18] iommu: Add live update state preservation Samiullah Khawaja
2026-08-08  2:27 ` [PATCH v4 01/18] memfd: export memfd_get_seals() Samiullah Khawaja
2026-08-08  2:27 ` [PATCH v4 02/18] iommu: Implement IOMMU Live update FLB callbacks Samiullah Khawaja
2026-08-08  2:27 ` [PATCH v4 03/18] iommu/pages: Add APIs to preserve/unpreserve/restore iommu pages Samiullah Khawaja
2026-08-08  2:27 ` [PATCH v4 04/18] iommupt: Implement preserve/unpreserve/restore callbacks Samiullah Khawaja
2026-08-08  2:27 ` [PATCH v4 05/18] iommu: Implement IOMMU domain preservation Samiullah Khawaja
2026-08-08  2:27 ` [PATCH v4 06/18] iommu: Implement device and IOMMU HW preservation Samiullah Khawaja
2026-08-08  2:27 ` [PATCH v4 07/18] iommu/vt-d: Implement device and iommu preserve/unpreserve ops Samiullah Khawaja
2026-08-26  7:10   ` Baolu Lu
2026-08-26 20:46     ` Samiullah Khawaja
2026-08-08  2:27 ` [PATCH v4 08/18] iommu/vt-d: Clear unpreserved context entries during shutdown Samiullah Khawaja
2026-08-26  8:05   ` Baolu Lu
2026-08-26 12:30     ` Pranjal Shrivastava
2026-08-26 20:37       ` Samiullah Khawaja
2026-08-26 20:30     ` Samiullah Khawaja
2026-08-27  6:09       ` Baolu Lu [this message]
2026-08-27 18:57         ` Samiullah Khawaja
2026-08-08  2:27 ` [PATCH v4 09/18] iommu: Add APIs to get iommu and device preserved state Samiullah Khawaja
2026-08-12  6:29   ` Ankit Soni
2026-08-12 23:23     ` Samiullah Khawaja
2026-08-08  2:27 ` [PATCH v4 10/18] iommu/vt-d: Restore IOMMU state and reclaimed domain ids Samiullah Khawaja
2026-08-27  7:22   ` Baolu Lu
2026-08-27 18:47     ` Samiullah Khawaja
2026-08-28  1:36       ` Baolu Lu
2026-08-08  2:27 ` [PATCH v4 11/18] iommu: Restore and reattach preserved domains to devices Samiullah Khawaja
2026-08-14 16:59   ` Ankit Soni
2026-08-14 19:46     ` Samiullah Khawaja
2026-08-17 15:32       ` Ankit Soni
2026-08-08  2:27 ` [PATCH v4 12/18] iommu/vt-d: Handle reattach of the restored domain Samiullah Khawaja
2026-08-27  8:12   ` Baolu Lu
2026-08-27 17:52     ` Samiullah Khawaja
2026-08-28  1:42       ` Baolu Lu
2026-08-28  1:55       ` Baolu Lu
2026-08-08  2:27 ` [PATCH v4 13/18] iommu/vt-d: Preserve PASID table of preserved device Samiullah Khawaja
2026-08-08  2:27 ` [PATCH v4 14/18] iommufd: Implement ioctl to mark HWPT for preservation Samiullah Khawaja
2026-08-08  2:27 ` [PATCH v4 15/18] iommufd: Persist iommu hardware pagetables for live update Samiullah Khawaja
2026-08-08  2:27 ` [PATCH v4 16/18] iommufd: Add APIs to preserve/unpreserve a vfio cdev Samiullah Khawaja
2026-08-08  2:27 ` [PATCH v4 17/18] vfio/pci: Preserve the iommufd state of the " Samiullah Khawaja
2026-08-08  2:27 ` [PATCH v4 18/18] iommufd/selftest: Add test to verify iommufd preservation Samiullah Khawaja
2026-08-26  8:12 ` [PATCH v4 00/18] iommu: Add live update state preservation David Woodhouse

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=5862e3cc-e7e4-43be-a829-8569d12d6bb4@linux.intel.com \
    --to=baolu.lu@linux.intel.com \
    --cc=akpm@linux-foundation.org \
    --cc=alex@shazbot.org \
    --cc=dmatlack@google.com \
    --cc=dwmw2@infradead.org \
    --cc=iommu@lists.linux.dev \
    --cc=jgg@ziepe.ca \
    --cc=joro@8bytes.org \
    --cc=kevin.tian@intel.com \
    --cc=kvm@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=pasha.tatashin@soleen.com \
    --cc=praan@google.com \
    --cc=pratyush@kernel.org \
    --cc=robin.murphy@arm.com \
    --cc=shuah@kernel.org \
    --cc=skhawaja@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