lkml.org 
[lkml]   [2021]   [Oct]   [24]   [last100]   RSS Feed
Views: [wrap][no wrap]   [headers]  [forward] 
 
Messages in this thread
/
SubjectRe: [PATCH V3 32/49] x86/entry: Add the C version ist_restore_cr3()
From
Date


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
>
> /*
>

\
 
 \ /
  Last update: 2021-10-25 02:36    [W:0.203 / U:0.528 seconds]
©2003-2020 Jasper Spaans|hosted at Digital Ocean and TransIP|Read the blog|Advertise on this site