Linux IOMMU Development
 help / color / mirror / Atom feed
From: Baolu Lu <baolu.lu@linux.intel.com>
To: Dmytro Maluka <dmaluka@chromium.org>
Cc: David Woodhouse <dwmw2@infradead.org>,
	iommu@lists.linux.dev, Joerg Roedel <joro@8bytes.org>,
	Will Deacon <will@kernel.org>,
	Robin Murphy <robin.murphy@arm.com>,
	linux-kernel@vger.kernel.org,
	"Vineeth Pillai (Google)" <vineeth@bitbyteword.org>,
	Aashish Sharma <aashish@aashishsharma.net>,
	Grzegorz Jaszczyk <jaszczyk@chromium.org>,
	Chuanxiao Dong <chuanxiao.dong@intel.com>,
	Kevin Tian <kevin.tian@intel.com>
Subject: Re: [PATCH 1/2] iommu/vt-d: Ensure memory ordering in context entry updates
Date: Tue, 23 Dec 2025 14:10:45 +0800	[thread overview]
Message-ID: <e63edcfe-0ee9-4582-b29e-5beb549904ce@linux.intel.com> (raw)
In-Reply-To: <aUfyBmPQfosuCDab@mitya-t14-2025>

On 12/21/25 21:11, Dmytro Maluka wrote:
> On Sun, Dec 21, 2025 at 05:04:27PM +0800, Baolu Lu wrote:
>> On 12/21/25 09:43, Dmytro Maluka wrote:
>>> +static inline void context_set_bits(u64 *ptr, u64 mask, u64 bits)
>>> +{
>>> +	u64 old;
>>> +
>>> +	old = READ_ONCE(*ptr);
>>> +	WRITE_ONCE(*ptr, (old & ~mask) | bits);
>>> +}
>>
>> Add a line to ensures that the input "bits" cannot overflow the assigned
>> "mask".
>>
>> static inline void context_set_bits(u64 *ptr, u64 mask, u64 bits)
>> {
>> 	u64 val;
>>
>> 	val = READ_ONCE(*ptr);
>> 	val &= ~mask;
>> 	val |= (bits & mask);
>> 	WRITE_ONCE(*ptr, val);
>> }
> 
> Makes sense. And then worth doing the same in pasid_set_bits() as well?
> 
> Actually we can use the same helper for both context and pasid entries
> (rename it e.g. to entry_set_bits()).

Yeah, that sounds better.

> 
>>>    static inline void context_set_present(struct context_entry *context)
>>>    {
>>> -	context->lo |= 1;
>>> +	context_set_bits(&context->lo, 1 << 0, 1);
>>>    }
>>
>> How about adding a smp_wmb() before setting the present bit? Maybe it's
>> unnecessary for x86 architecture, but at least it's harmless and more
>> readable. Or not?
>>
>> static inline void context_set_present(struct context_entry *context)
>> {
>> 	smp_wmb();
>> 	context_set_bits(&context->lo, 1ULL << 0, 1ULL);
>> }
> 
> Maybe... And if so, then in pasid_set_present() as well?
> 
> Actually this would make a slight behavioral difference even on x86:
> right now this patch only provides ordering of context entry updates
> between each other, while smp_wmb() would add a full compiler barrier
> here.
> 
> So this barrier may be redundant as long as we always use these
> context_*() helpers (and thus always use WRITE_ONCE) for any updates of
> context entries. On the other hand, it might make it more robust if we
> still occasionally do that without WRITE_ONCE, for example in
> context_entry_set_pasid_table() which I also changed to use WRITE_ONCE
> in this patch.

Fair point. It is somewhat redundant. However, I would suggest adding a
comment there. For example:

static inline void context_set_present(struct context_entry *context)
{
  	/*
	 * smp_wmb() is unnecessary here because x86 hardware naturally
	 * keeps writes in order and all context entry modifications
	 * use WRITE_ONCE().
	 */
  	context_set_bits(&context->lo, 1ULL << 0, 1ULL);
}

So that people are still aware of this when porting this driver to
platforms with a weak memory model.

Thanks,
baolu

  reply	other threads:[~2025-12-23  6:10 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-12-21  1:43 [PATCH 0/2] iommu/vt-d: Ensure memory ordering in context & root entry updates Dmytro Maluka
2025-12-21  1:43 ` [PATCH 1/2] iommu/vt-d: Ensure memory ordering in context " Dmytro Maluka
2025-12-21  9:04   ` Baolu Lu
2025-12-21 13:11     ` Dmytro Maluka
2025-12-23  6:10       ` Baolu Lu [this message]
2025-12-27 18:06         ` Dmytro Maluka
2025-12-21  1:43 ` [PATCH 2/2] iommu/vt-d: Use WRITE_ONCE for setting root table entries Dmytro Maluka

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=e63edcfe-0ee9-4582-b29e-5beb549904ce@linux.intel.com \
    --to=baolu.lu@linux.intel.com \
    --cc=aashish@aashishsharma.net \
    --cc=chuanxiao.dong@intel.com \
    --cc=dmaluka@chromium.org \
    --cc=dwmw2@infradead.org \
    --cc=iommu@lists.linux.dev \
    --cc=jaszczyk@chromium.org \
    --cc=joro@8bytes.org \
    --cc=kevin.tian@intel.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=robin.murphy@arm.com \
    --cc=vineeth@bitbyteword.org \
    --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