lists.openwall.net   lists  /  announce  owl-users  owl-dev  john-users  john-dev  passwdqc-users  yescrypt  popa3d-users  /  oss-security  kernel-hardening  musl  sabotage  tlsify  passwords  /  crypt-dev  xvendor  /  Bugtraq  Full-Disclosure  linux-kernel  linux-netdev  linux-ext4  linux-hardening  PHC 
Open Source and information security mailing list archives
 
Hash Suite: Windows password security audit tool. GUI, reports in PDF.
[<prev] [next>] [<thread-prev] [day] [month] [year] [list]
Date:   Mon, 25 Oct 2021 08:34:54 +0800
From:   Lai Jiangshan <laijs@...ux.alibaba.com>
To:     Lai Jiangshan <jiangshanlai@...il.com>,
        linux-kernel@...r.kernel.org, Peter Zijlstra <peterz@...radead.org>
Cc:     x86@...nel.org, Andy Lutomirski <luto@...nel.org>,
        Thomas Gleixner <tglx@...utronix.de>,
        Ingo Molnar <mingo@...hat.com>, Borislav Petkov <bp@...en8.de>,
        "H. Peter Anvin" <hpa@...or.com>
Subject: Re: [PATCH V3 32/49] x86/entry: Add the C version ist_restore_cr3()



On 2021/10/14 11:41, Lai Jiangshan wrote:
>
>   static __always_inline void switch_to_kernel_cr3(void)
>   {
>   	if (static_cpu_has(X86_FEATURE_PTI))
> @@ -49,9 +70,34 @@ static __always_inline unsigned long ist_switch_to_kernel_cr3(void)
>   
>   	return cr3;
>   }
> +
> +static __always_inline void ist_restore_cr3(unsigned long cr3)
> +{
> +	if (!static_cpu_has(X86_FEATURE_PTI))
> +		return;
> +
> +	if (unlikely(cr3 & PTI_USER_PGTABLE_MASK)) {
> +		pti_switch_to_user_cr3(cr3);
> +		return;
> +	}

The C code is semantically copied from ASM.

The ASM code is from the commit 21e9445911025("x86/mm: Optimize RESTORE_CR3")
which still keeps the older behavior of writing to CR3 unconditionally
even the saved CR3 is kernel CR3.

Is there any special reason that the CR3 needs to be written for kernel CR3?

I would add a commit to change it in ASM code by skipping cr3 write when
it is kernel CR3, and then make the C code as the same as new ASM code.

> +
> +	/*
> +	 * KERNEL pages can always resume with NOFLUSH as we do
> +	 * explicit flushes.
> +	 */
> +	if (static_cpu_has(X86_FEATURE_PCID))
> +		cr3 |= X86_CR3_PCID_NOFLUSH;
> +
> +	/*
> +	 * The CR3 write could be avoided when not changing its value,
> +	 * but would require a CR3 read.
> +	 */
> +	native_write_cr3(cr3);
> +}
>   #else
>   static __always_inline void switch_to_kernel_cr3(void) {}
>   static __always_inline unsigned long ist_switch_to_kernel_cr3(void) { return 0; }
> +static __always_inline void ist_restore_cr3(unsigned long cr3) {}
>   #endif
>   
>   /*
> 

Powered by blists - more mailing lists