Skip to content

Commit 604c496

Browse files
Ram Paitorvalds
authored andcommitted
selftests/vm/pkeys: introduce generic pkey abstractions
This introduces some generic abstractions and provides the corresponding architecture-specfic implementations for these abstractions. Signed-off-by: Ram Pai <[email protected]> Signed-off-by: Thiago Jung Bauermann <[email protected]> Signed-off-by: Sandipan Das <[email protected]> Signed-off-by: Andrew Morton <[email protected]> Acked-by: Dave Hansen <[email protected]> Cc: Dave Hansen <[email protected]> Cc: Florian Weimer <[email protected]> Cc: "Desnes A. Nunes do Rosario" <[email protected]> Cc: Ingo Molnar <[email protected]> Cc: "Aneesh Kumar K.V" <[email protected]> Cc: Michael Ellerman <[email protected]> Cc: Michal Hocko <[email protected]> Cc: Michal Suchanek <[email protected]> Cc: Shuah Khan <[email protected]> Link: http://lkml.kernel.org/r/1c977915e69fb7767fb0dbd55ac7656554b15b93.1585646528.git.sandipan@linux.ibm.com Signed-off-by: Linus Torvalds <[email protected]>
1 parent 57bcb57 commit 604c496

File tree

3 files changed

+29
-6
lines changed

3 files changed

+29
-6
lines changed

tools/testing/selftests/vm/pkey-helpers.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,9 @@ extern void abort_hooks(void);
7474
} \
7575
} while (0)
7676

77+
__attribute__((noinline)) int read_ptr(int *ptr);
78+
void expected_pkey_fault(int pkey);
79+
7780
#if defined(__i386__) || defined(__x86_64__) /* arch */
7881
#include "pkey-x86.h"
7982
#else /* arch */
@@ -172,4 +175,13 @@ static inline void __pkey_write_allow(int pkey, int do_allow_write)
172175
#define __stringify_1(x...) #x
173176
#define __stringify(x...) __stringify_1(x)
174177

178+
static inline u32 *siginfo_get_pkey_ptr(siginfo_t *si)
179+
{
180+
#ifdef si_pkey
181+
return &si->si_pkey;
182+
#else
183+
return (u32 *)(((u8 *)si) + si_pkey_offset);
184+
#endif
185+
}
186+
175187
#endif /* _PKEYS_HELPER_H */

tools/testing/selftests/vm/pkey-x86.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
#endif
4343

4444
#define NR_PKEYS 16
45+
#define NR_RESERVED_PKEYS 2 /* pkey-0 and exec-only-pkey */
4546
#define PKEY_BITS_PER_PKEY 2
4647
#define HPAGE_SIZE (1UL<<21)
4748
#define PAGE_SIZE 4096
@@ -158,4 +159,18 @@ int pkey_reg_xstate_offset(void)
158159
return xstate_offset;
159160
}
160161

162+
static inline int get_arch_reserved_keys(void)
163+
{
164+
return NR_RESERVED_PKEYS;
165+
}
166+
167+
void expect_fault_on_read_execonly_key(void *p1, int pkey)
168+
{
169+
int ptr_contents;
170+
171+
ptr_contents = read_ptr(p1);
172+
dprintf2("ptr (%p) contents@%d: %x\n", p1, __LINE__, ptr_contents);
173+
expected_pkey_fault(pkey);
174+
}
175+
161176
#endif /* _PKEYS_X86_H */

tools/testing/selftests/vm/protection_keys.c

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1307,9 +1307,7 @@ void test_executing_on_unreadable_memory(int *ptr, u16 pkey)
13071307
madvise(p1, PAGE_SIZE, MADV_DONTNEED);
13081308
lots_o_noops_around_write(&scratch);
13091309
do_not_expect_pkey_fault("executing on PROT_EXEC memory");
1310-
ptr_contents = read_ptr(p1);
1311-
dprintf2("ptr (%p) contents@%d: %x\n", p1, __LINE__, ptr_contents);
1312-
expected_pkey_fault(pkey);
1310+
expect_fault_on_read_execonly_key(p1, pkey);
13131311
}
13141312

13151313
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)
13361334
madvise(p1, PAGE_SIZE, MADV_DONTNEED);
13371335
lots_o_noops_around_write(&scratch);
13381336
do_not_expect_pkey_fault("executing on PROT_EXEC memory");
1339-
ptr_contents = read_ptr(p1);
1340-
dprintf2("ptr (%p) contents@%d: %x\n", p1, __LINE__, ptr_contents);
1341-
expected_pkey_fault(UNKNOWN_PKEY);
1337+
expect_fault_on_read_execonly_key(p1, UNKNOWN_PKEY);
13421338

13431339
/*
13441340
* Put the memory back to non-PROT_EXEC. Should clear the

0 commit comments

Comments
 (0)