@@ -44,11 +44,16 @@ static inline void write_pkru(u32 pkru)
if (!cpu_feature_enabled(X86_FEATURE_OSPKE))
return;
/*
+ * Update the actual register.
+ *
* WRPKRU is relatively expensive compared to RDPKRU.
* Avoid WRPKRU when it would not change the value.
*/
if (pkru != rdpkru())
wrpkru(pkru);
+
+ /* Update the thread-local, context-switched value: */
+ current->thread.pkru = pkru;
}
static inline void pkru_write_default(void)
@@ -482,6 +482,9 @@ static __always_inline void setup_pku(st
cr4_set_bits(X86_CR4_PKE);
/* Load the default PKRU value */
pkru_write_default();
+
+ /* Establish the default value for future tasks: */
+ init_task.thread.pkru = init_pkru_value;
}
#ifdef CONFIG_X86_INTEL_MEMORY_PROTECTION_KEYS
@@ -349,15 +349,14 @@ static __always_inline void load_seg_leg
static __always_inline void x86_pkru_load(struct thread_struct *prev,
struct thread_struct *next)
{
- if (!cpu_feature_enabled(X86_FEATURE_OSPKE))
- return;
+ u32 pkru = read_pkru();
/* Stash the prev task's value: */
- prev->pkru = rdpkru();
+ prev->pkru = pkru;
/*
- * PKRU writes are slightly expensive. Avoid them when not
- * strictly necessary:
+ * PKRU writes are slightly expensive. Avoid
+ * them when not strictly necessary:
*/
if (prev->pkru != next->pkru)
wrpkru(next->pkru);
@@ -159,6 +159,8 @@ static ssize_t init_pkru_write_file(stru
return -EINVAL;
WRITE_ONCE(init_pkru_value, new_init_pkru);
+ WRITE_ONCE(init_task.thread.pkru, new_init_pkru);
+
return count;
}