From: Andrew Cooper <andrew.cooper3@citrix.com>
To: "Kirill A. Shutemov" <kirill@shutemov.name>,
Linus Torvalds <torvalds@linux-foundation.org>
Cc: Josh Poimboeuf <jpoimboe@kernel.org>,
x86@kernel.org, Thomas Gleixner <tglx@linutronix.de>,
Borislav Petkov <bp@alien8.de>,
Peter Zijlstra <peterz@infradead.org>,
Pawan Gupta <pawan.kumar.gupta@linux.intel.com>,
Waiman Long <longman@redhat.com>,
Dave Hansen <dave.hansen@linux.intel.com>,
Ingo Molnar <mingo@redhat.com>,
Michael Ellerman <mpe@ellerman.id.au>,
linuxppc-dev@lists.ozlabs.org,
Mark Rutland <mark.rutland@arm.com>
Subject: Re: [PATCH] x86/uaccess: Avoid barrier_nospec() in copy_from_user()
Date: Mon, 14 Oct 2024 16:39:26 +0100 [thread overview]
Message-ID: <064d7068-c666-49f0-b7df-774c2e281abc@citrix.com> (raw)
In-Reply-To: <r5ruxmop7vewd7aq6gu2zve4hfmcu4byhlebygswbqkgz43qfq@rsajyfiypcsd>
On 14/10/2024 1:30 pm, Kirill A. Shutemov wrote:
> On Sun, Oct 13, 2024 at 11:50:55PM -0700, Linus Torvalds wrote:
>> Anyway, the attached patch
>>
>> diff --git a/arch/x86/lib/getuser.S b/arch/x86/lib/getuser.S
>> index d066aecf8aeb..7d5730aa18b8 100644
>> --- a/arch/x86/lib/getuser.S
>> +++ b/arch/x86/lib/getuser.S
>> @@ -37,11 +37,17 @@
>>
>> #define ASM_BARRIER_NOSPEC ALTERNATIVE "", "lfence", X86_FEATURE_LFENCE_RDTSC
>>
>> +#define X86_CANONICAL_MASK ALTERNATIVE \
>> + "movq $0x80007fffffffffff,%rdx", \
>> + "movq $0x80ffffffffffffff,%rdx", X86_FEATURE_LA57
>> +
>> .macro check_range size:req
>> .if IS_ENABLED(CONFIG_X86_64)
>> mov %rax, %rdx
>> sar $63, %rdx
>> or %rdx, %rax
>> + X86_CANONICAL_MASK
>> + and %rdx,%rax
>> .else
>> cmp $TASK_SIZE_MAX-\size+1, %eax
>> jae .Lbad_get_user
> Given that LAM enforces bit 47/56 to be equal to bit 63 I think we can do
> this unconditionally instead of masking:
>
> diff --git a/arch/x86/lib/getuser.S b/arch/x86/lib/getuser.S
> index d066aecf8aeb..86d4511520b1 100644
> --- a/arch/x86/lib/getuser.S
> +++ b/arch/x86/lib/getuser.S
> @@ -37,9 +37,14 @@
>
> #define ASM_BARRIER_NOSPEC ALTERNATIVE "", "lfence", X86_FEATURE_LFENCE_RDTSC
>
> +#define SHIFT_LEFT_TO_MSB ALTERNATIVE \
> + "shl $(64 - 48), %rdx", \
> + "shl $(64 - 57), %rdx", X86_FEATURE_LA57
> +
> .macro check_range size:req
> .if IS_ENABLED(CONFIG_X86_64)
> mov %rax, %rdx
> + SHIFT_LEFT_TO_MSB
> sar $63, %rdx
> or %rdx, %rax
> .else
That looks like it ought to DTRT in some cases, but I'll definitely ask
AMD for confirmation.
But, I expect it will malfunction on newer hardware when
CONFIG_X86_5LEVEL=n, because it causes Linux to explicitly ignore the
LA57 bit. That can be fixed by changing how CONFIG_X86_5LEVEL works.
I also expect it will malfunction under virt on an LA57-capable system
running a VM in LA48 mode (this time, Linux doesn't get to see the
relevant uarch detail), and I have no good suggestion here.
~Andrew
next prev parent reply other threads:[~2024-10-14 15:39 UTC|newest]
Thread overview: 51+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-10-12 4:09 [PATCH] x86/uaccess: Avoid barrier_nospec() in copy_from_user() Josh Poimboeuf
2024-10-12 8:48 ` Andrew Cooper
2024-10-12 14:09 ` Josh Poimboeuf
2024-10-12 14:21 ` Borislav Petkov
2024-10-12 15:58 ` Linus Torvalds
2024-10-12 14:26 ` Andrew Cooper
2024-10-12 15:44 ` Linus Torvalds
2024-10-12 17:23 ` Andrew Cooper
2024-10-12 17:44 ` Linus Torvalds
2024-10-13 0:53 ` Linus Torvalds
2024-10-13 1:21 ` Linus Torvalds
2024-10-14 3:54 ` Josh Poimboeuf
2024-10-14 6:50 ` Linus Torvalds
2024-10-14 9:59 ` David Laight
2024-10-14 11:20 ` Linus Torvalds
2024-10-14 14:40 ` David Laight
2024-10-14 11:12 ` Andrew Cooper
2024-10-14 12:30 ` Kirill A. Shutemov
2024-10-14 15:39 ` Andrew Cooper [this message]
2024-10-15 10:00 ` Borislav Petkov
2024-10-20 22:44 ` Josh Poimboeuf
2024-10-20 22:59 ` Linus Torvalds
2024-10-20 23:11 ` Josh Poimboeuf
2024-10-20 23:14 ` Josh Poimboeuf
2024-10-20 23:35 ` Linus Torvalds
2024-10-23 9:44 ` Borislav Petkov
2024-10-23 19:17 ` Linus Torvalds
2024-10-23 20:07 ` Linus Torvalds
2024-10-23 23:32 ` Linus Torvalds
2024-10-24 2:00 ` Linus Torvalds
2024-10-24 9:21 ` David Laight
2024-10-24 16:53 ` Linus Torvalds
2024-10-25 8:56 ` David Laight
2024-10-25 16:35 ` Linus Torvalds
2024-10-21 10:48 ` Kirill A. Shutemov
2024-10-22 2:36 ` Linus Torvalds
2024-10-22 10:33 ` Kirill A. Shutemov
2024-10-22 8:16 ` Pawan Gupta
2024-10-22 10:44 ` Kirill A. Shutemov
2024-10-14 16:55 ` Linus Torvalds
2024-10-16 16:10 ` Linus Torvalds
2024-10-16 22:02 ` Andrew Cooper
2024-10-16 22:13 ` Kirill A. Shutemov
2024-10-16 22:34 ` Linus Torvalds
2024-10-28 11:29 ` Kirill A. Shutemov
2024-10-28 18:44 ` Linus Torvalds
2024-10-28 20:31 ` Kirill A. Shutemov
2024-10-28 20:49 ` Linus Torvalds
2024-10-16 22:32 ` Linus Torvalds
2024-10-17 11:00 ` Borislav Petkov
2024-10-14 11:56 ` Kirill A. Shutemov
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=064d7068-c666-49f0-b7df-774c2e281abc@citrix.com \
--to=andrew.cooper3@citrix.com \
--cc=bp@alien8.de \
--cc=dave.hansen@linux.intel.com \
--cc=jpoimboe@kernel.org \
--cc=kirill@shutemov.name \
--cc=linuxppc-dev@lists.ozlabs.org \
--cc=longman@redhat.com \
--cc=mark.rutland@arm.com \
--cc=mingo@redhat.com \
--cc=mpe@ellerman.id.au \
--cc=pawan.kumar.gupta@linux.intel.com \
--cc=peterz@infradead.org \
--cc=tglx@linutronix.de \
--cc=torvalds@linux-foundation.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;
as well as URLs for NNTP newsgroup(s).