]> git.baikalelectronics.ru Git - kernel.git/commitdiff
selftests/vm/pkeys: introduce generic pkey abstractions
authorRam Pai <linuxram@us.ibm.com>
Thu, 4 Jun 2020 23:52:12 +0000 (16:52 -0700)
committerLinus Torvalds <torvalds@linux-foundation.org>
Fri, 5 Jun 2020 02:06:27 +0000 (19:06 -0700)
This introduces some generic abstractions and provides the corresponding
architecture-specfic implementations for these abstractions.

Signed-off-by: Ram Pai <linuxram@us.ibm.com>
Signed-off-by: Thiago Jung Bauermann <bauerman@linux.ibm.com>
Signed-off-by: Sandipan Das <sandipan@linux.ibm.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Acked-by: Dave Hansen <dave.hansen@intel.com>
Cc: Dave Hansen <dave.hansen@intel.com>
Cc: Florian Weimer <fweimer@redhat.com>
Cc: "Desnes A. Nunes do Rosario" <desnesn@linux.vnet.ibm.com>
Cc: Ingo Molnar <mingo@kernel.org>
Cc: "Aneesh Kumar K.V" <aneesh.kumar@linux.ibm.com>
Cc: Michael Ellerman <mpe@ellerman.id.au>
Cc: Michal Hocko <mhocko@kernel.org>
Cc: Michal Suchanek <msuchanek@suse.de>
Cc: Shuah Khan <shuah@kernel.org>
Link: http://lkml.kernel.org/r/1c977915e69fb7767fb0dbd55ac7656554b15b93.1585646528.git.sandipan@linux.ibm.com
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
tools/testing/selftests/vm/pkey-helpers.h
tools/testing/selftests/vm/pkey-x86.h
tools/testing/selftests/vm/protection_keys.c

index 0e3da7c8d6282f25382011316500ad5267db2043..621fb2a0a5efe277ec41fe4b10eab861b4ddc386 100644 (file)
@@ -74,6 +74,9 @@ extern void abort_hooks(void);
        }                                       \
 } while (0)
 
+__attribute__((noinline)) int read_ptr(int *ptr);
+void expected_pkey_fault(int pkey);
+
 #if defined(__i386__) || defined(__x86_64__) /* arch */
 #include "pkey-x86.h"
 #else /* arch */
@@ -172,4 +175,13 @@ static inline void __pkey_write_allow(int pkey, int do_allow_write)
 #define __stringify_1(x...)     #x
 #define __stringify(x...)       __stringify_1(x)
 
+static inline u32 *siginfo_get_pkey_ptr(siginfo_t *si)
+{
+#ifdef si_pkey
+       return &si->si_pkey;
+#else
+       return (u32 *)(((u8 *)si) + si_pkey_offset);
+#endif
+}
+
 #endif /* _PKEYS_HELPER_H */
index def2a1bcf6a5d21ae1e86a4396fd9124813cd7e5..a0c59d4f7af2ec48c7bdf10670f70a20dc002883 100644 (file)
@@ -42,6 +42,7 @@
 #endif
 
 #define NR_PKEYS               16
+#define NR_RESERVED_PKEYS      2 /* pkey-0 and exec-only-pkey */
 #define PKEY_BITS_PER_PKEY     2
 #define HPAGE_SIZE             (1UL<<21)
 #define PAGE_SIZE              4096
@@ -158,4 +159,18 @@ int pkey_reg_xstate_offset(void)
        return xstate_offset;
 }
 
+static inline int get_arch_reserved_keys(void)
+{
+       return NR_RESERVED_PKEYS;
+}
+
+void expect_fault_on_read_execonly_key(void *p1, int pkey)
+{
+       int ptr_contents;
+
+       ptr_contents = read_ptr(p1);
+       dprintf2("ptr (%p) contents@%d: %x\n", p1, __LINE__, ptr_contents);
+       expected_pkey_fault(pkey);
+}
+
 #endif /* _PKEYS_X86_H */
index 535e464e27e9db916af3302a3b15b9f59eddb0b7..57c71056c93d89b6dc30bb191934b379a71a8ec3 100644 (file)
@@ -1307,9 +1307,7 @@ void test_executing_on_unreadable_memory(int *ptr, u16 pkey)
        madvise(p1, PAGE_SIZE, MADV_DONTNEED);
        lots_o_noops_around_write(&scratch);
        do_not_expect_pkey_fault("executing on PROT_EXEC memory");
-       ptr_contents = read_ptr(p1);
-       dprintf2("ptr (%p) contents@%d: %x\n", p1, __LINE__, ptr_contents);
-       expected_pkey_fault(pkey);
+       expect_fault_on_read_execonly_key(p1, pkey);
 }
 
 void test_implicit_mprotect_exec_only_memory(int *ptr, u16 pkey)
@@ -1336,9 +1334,7 @@ void test_implicit_mprotect_exec_only_memory(int *ptr, u16 pkey)
        madvise(p1, PAGE_SIZE, MADV_DONTNEED);
        lots_o_noops_around_write(&scratch);
        do_not_expect_pkey_fault("executing on PROT_EXEC memory");
-       ptr_contents = read_ptr(p1);
-       dprintf2("ptr (%p) contents@%d: %x\n", p1, __LINE__, ptr_contents);
-       expected_pkey_fault(UNKNOWN_PKEY);
+       expect_fault_on_read_execonly_key(p1, UNKNOWN_PKEY);
 
        /*
         * Put the memory back to non-PROT_EXEC.  Should clear the