forked from ~ljy/RK356X_SDK_RELEASE

hc
2023-12-08 01573e231f18eb2d99162747186f59511f56b64d
kernel/arch/x86/mm/pkeys.c
....@@ -1,15 +1,7 @@
1
+// SPDX-License-Identifier: GPL-2.0-only
12 /*
23 * Intel Memory Protection Keys management
34 * Copyright (c) 2015, Intel Corporation.
4
- *
5
- * This program is free software; you can redistribute it and/or modify it
6
- * under the terms and conditions of the GNU General Public License,
7
- * version 2, as published by the Free Software Foundation.
8
- *
9
- * This program is distributed in the hope it will be useful, but WITHOUT
10
- * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
- * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12
- * more details.
135 */
146 #include <linux/debugfs.h> /* debugfs_create_u32() */
157 #include <linux/mm_types.h> /* mm_struct, vma, etc... */
....@@ -18,6 +10,7 @@
1810
1911 #include <asm/cpufeature.h> /* boot_cpu_has, ... */
2012 #include <asm/mmu_context.h> /* vma_pkey() */
13
+#include <asm/fpu/internal.h> /* init_fpstate */
2114
2215 int __execute_only_pkey(struct mm_struct *mm)
2316 {
....@@ -39,17 +32,12 @@
3932 * dance to set PKRU if we do not need to. Check it
4033 * first and assume that if the execute-only pkey is
4134 * write-disabled that we do not have to set it
42
- * ourselves. We need preempt off so that nobody
43
- * can make fpregs inactive.
35
+ * ourselves.
4436 */
45
- preempt_disable();
4637 if (!need_to_set_mm_pkey &&
47
- current->thread.fpu.initialized &&
4838 !__pkru_allows_read(read_pkru(), execute_only_pkey)) {
49
- preempt_enable();
5039 return execute_only_pkey;
5140 }
52
- preempt_enable();
5341
5442 /*
5543 * Set up PKRU so that it denies access for everything
....@@ -75,7 +63,7 @@
7563 static inline bool vma_is_pkey_exec_only(struct vm_area_struct *vma)
7664 {
7765 /* Do this check first since the vm_flags should be hot */
78
- if ((vma->vm_flags & (VM_READ | VM_WRITE | VM_EXEC)) != VM_EXEC)
66
+ if ((vma->vm_flags & VM_ACCESS_FLAGS) != VM_EXEC)
7967 return false;
8068 if (vma_pkey(vma) != vma->vm_mm->context.execute_only_pkey)
8169 return false;
....@@ -147,13 +135,6 @@
147135 {
148136 u32 init_pkru_value_snapshot = READ_ONCE(init_pkru_value);
149137 /*
150
- * Any write to PKRU takes it out of the XSAVE 'init
151
- * state' which increases context switch cost. Avoid
152
- * writing 0 when PKRU was already 0.
153
- */
154
- if (!init_pkru_value_snapshot && !read_pkru())
155
- return;
156
- /*
157138 * Override the PKRU state that came from 'init_fpstate'
158139 * with the baseline from the process.
159140 */
....@@ -173,6 +154,7 @@
173154 static ssize_t init_pkru_write_file(struct file *file,
174155 const char __user *user_buf, size_t count, loff_t *ppos)
175156 {
157
+ struct pkru_state *pk;
176158 char buf[32];
177159 ssize_t len;
178160 u32 new_init_pkru;
....@@ -195,6 +177,10 @@
195177 return -EINVAL;
196178
197179 WRITE_ONCE(init_pkru_value, new_init_pkru);
180
+ pk = get_xsave_addr(&init_fpstate.xsave, XFEATURE_PKRU);
181
+ if (!pk)
182
+ return -EINVAL;
183
+ pk->pkru = new_init_pkru;
198184 return count;
199185 }
200186