Skip to content

Commit 94c8a22

Browse files
Ram Paitorvalds
authored andcommitted
selftests/vm/pkeys: improve checks to determine pkey support
For the pkeys subsystem to work, both the CPU and the kernel need to have support. So, additionally check if the kernel supports pkeys apart from the CPU feature checks. Signed-off-by: Ram Pai <[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: Thiago Jung Bauermann <[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/8fb76c63ebdadcf068ecd2d23731032e195cd364.1585646528.git.sandipan@linux.ibm.com Signed-off-by: Linus Torvalds <[email protected]>
1 parent b0acc5d commit 94c8a22

File tree

4 files changed

+37
-5
lines changed

4 files changed

+37
-5
lines changed

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ extern void abort_hooks(void);
7676

7777
__attribute__((noinline)) int read_ptr(int *ptr);
7878
void expected_pkey_fault(int pkey);
79+
int sys_pkey_alloc(unsigned long flags, unsigned long init_val);
80+
int sys_pkey_free(unsigned long pkey);
7981

8082
#if defined(__i386__) || defined(__x86_64__) /* arch */
8183
#include "pkey-x86.h"
@@ -186,4 +188,32 @@ static inline u32 *siginfo_get_pkey_ptr(siginfo_t *si)
186188
#endif
187189
}
188190

191+
static inline int kernel_has_pkeys(void)
192+
{
193+
/* try allocating a key and see if it succeeds */
194+
int ret = sys_pkey_alloc(0, 0);
195+
if (ret <= 0) {
196+
return 0;
197+
}
198+
sys_pkey_free(ret);
199+
return 1;
200+
}
201+
202+
static inline int is_pkeys_supported(void)
203+
{
204+
/* check if the cpu supports pkeys */
205+
if (!cpu_has_pkeys()) {
206+
dprintf1("SKIP: %s: no CPU support\n", __func__);
207+
return 0;
208+
}
209+
210+
/* check if the kernel supports pkeys */
211+
if (!kernel_has_pkeys()) {
212+
dprintf1("SKIP: %s: no kernel support\n", __func__);
213+
return 0;
214+
}
215+
216+
return 1;
217+
}
218+
189219
#endif /* _PKEYS_HELPER_H */

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,9 @@ static inline void __write_pkey_reg(u64 pkey_reg)
6464
__func__, __read_pkey_reg(), pkey_reg);
6565
}
6666

67-
static inline int cpu_has_pku(void)
67+
static inline int cpu_has_pkeys(void)
6868
{
69+
/* No simple way to determine this */
6970
return 1;
7071
}
7172

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ static inline void __cpuid(unsigned int *eax, unsigned int *ebx,
9797
#define X86_FEATURE_PKU (1<<3) /* Protection Keys for Userspace */
9898
#define X86_FEATURE_OSPKE (1<<4) /* OS Protection Keys Enable */
9999

100-
static inline int cpu_has_pku(void)
100+
static inline int cpu_has_pkeys(void)
101101
{
102102
unsigned int eax;
103103
unsigned int ebx;

tools/testing/selftests/vm/protection_keys.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1378,7 +1378,7 @@ void test_mprotect_pkey_on_unsupported_cpu(int *ptr, u16 pkey)
13781378
int size = PAGE_SIZE;
13791379
int sret;
13801380

1381-
if (cpu_has_pku()) {
1381+
if (cpu_has_pkeys()) {
13821382
dprintf1("SKIP: %s: no CPU support\n", __func__);
13831383
return;
13841384
}
@@ -1447,12 +1447,13 @@ void pkey_setup_shadow(void)
14471447
int main(void)
14481448
{
14491449
int nr_iterations = 22;
1450+
int pkeys_supported = is_pkeys_supported();
14501451

14511452
setup_handlers();
14521453

1453-
printf("has pku: %d\n", cpu_has_pku());
1454+
printf("has pkeys: %d\n", pkeys_supported);
14541455

1455-
if (!cpu_has_pku()) {
1456+
if (!pkeys_supported) {
14561457
int size = PAGE_SIZE;
14571458
int *ptr;
14581459

0 commit comments

Comments
 (0)