From: andrew.cooper3@citrix.com
To: Dave Hansen <dave.hansen@linux.intel.com>, dave.hansen@intel.com
Cc: linux-kernel@vger.kernel.org, jannh@google.com, x86@kernel.org,
luto@kernel.org, peterz@infradead.org
Subject: Re: [PATCH] x86/mm: Remove "INVPCID single" feature tracking
Date: Fri, 14 Jul 2023 21:27:24 +0100 [thread overview]
Message-ID: <df0750e0-dd6f-7418-53bd-64a9ad1e0086@citrix.com> (raw)
In-Reply-To: <20230714183555.7DD052F4@davehans-spike.ostc.intel.com>
On 14/07/2023 7:35 pm, Dave Hansen wrote:
> From: Dave Hansen <dave.hansen@linux.intel.com>
>
> tl;dr: Replace a synthetic X86_FEATURE with a hardware X86_FEATURE
> and check of existing per-cpu state.
>
> == Background ==
>
> There are three features in play here:
> 1. Good old Page Table Isolation (PTI)
> 2. Process Context IDentifiers (PCIDs) which allow entries from
> multiple address spaces to be in the TLB at once.
> 3. Support for the "Invalidate PCID" (INVPCID) instruction,
> specifically the "individual address" mode (aka. mode 0).
>
> When all *three* of these are in place, INVPCID can and should be used
> to flush out individual addresses in the PTI user address space.
>
> But there's a wrinkle or two: First, this INVPCID mode is dependent on
> CR4.PCIDE. Even if X86_FEATURE_INVPCID==1, the instruction may #GP
> without setting up CR4.
Can the SDM authors go and reconsider their position of (not) including
this condition in the exception list.
Or give up and just point intel.com/sdm at AMD, because AMD do describe
this coherently.
> diff -puN arch/x86/mm/tlb.c~remove-invpcid-single arch/x86/mm/tlb.c
> --- a/arch/x86/mm/tlb.c~remove-invpcid-single 2023-07-14 08:29:08.665225945 -0700
> +++ b/arch/x86/mm/tlb.c 2023-07-14 08:29:08.673225955 -0700
> @@ -1141,20 +1141,24 @@ void flush_tlb_one_kernel(unsigned long
> STATIC_NOPV void native_flush_tlb_one_user(unsigned long addr)
> {
> u32 loaded_mm_asid = this_cpu_read(cpu_tlbstate.loaded_mm_asid);
> + bool cpu_pcide = this_cpu_read(cpu_tlbstate.cr4) & X86_CR4_PCIDE;
>
> + /* Flush 'addr' from the kernel PCID: */
> asm volatile("invlpg (%0)" ::"r" (addr) : "memory");
>
> + /* If PTI is off there is no user PCID and nothing to flush. */
> if (!static_cpu_has(X86_FEATURE_PTI))
> return;
As a minor observation, the common case is for the function to exit
here, but you've got both this_cpu_read()'s ahead of a full compiler
memory barrier.
If you move them here, you'll drop the reads from the common case. But...
>
> /*
> - * Some platforms #GP if we call invpcid(type=1/2) before CR4.PCIDE=1.
> - * Just use invalidate_user_asid() in case we are called early.
> + * invpcid_flush_one(pcid>0) will #GP if CR4.PCIDE==0. Check
> + * 'cpu_pcide' to ensure that *this* CPU will not trigger those
> + * #GP's even if called before CR4.PCIDE has been initialized.
> */
> - if (!this_cpu_has(X86_FEATURE_INVPCID_SINGLE))
> - invalidate_user_asid(loaded_mm_asid);
> - else
> + if (boot_cpu_has(X86_FEATURE_INVPCID) && cpu_pcide)
... why can't this just be && loaded_mm_asid ?
There's no plausible way the asid can be nonzero here without CR4.PCIDE
being set, and that avoids looking at cr4 directly.
~Andrew
next prev parent reply other threads:[~2023-07-14 20:27 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-07-14 18:35 Dave Hansen
2023-07-14 20:27 ` andrew.cooper3 [this message]
2023-07-14 23:01 ` Dave Hansen
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=df0750e0-dd6f-7418-53bd-64a9ad1e0086@citrix.com \
--to=andrew.cooper3@citrix.com \
--cc=dave.hansen@intel.com \
--cc=dave.hansen@linux.intel.com \
--cc=jannh@google.com \
--cc=linux-kernel@vger.kernel.org \
--cc=luto@kernel.org \
--cc=peterz@infradead.org \
--cc=x86@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®