Skip to content

Commit fa17437

Browse files
Ram Paitorvalds
authored andcommitted
selftests/vm/pkeys: test correct behaviour of pkey-0
Ensure that pkey-0 is allocated on start and that it can be attached dynamically in various modes, without failures. 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/9b7c54a9b4261894fe0c7e884c70b87214ff8fbb.1585646528.git.sandipan@linux.ibm.com Signed-off-by: Linus Torvalds <[email protected]>
1 parent 6e2c2d0 commit fa17437

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed

tools/testing/selftests/vm/protection_keys.c

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -964,6 +964,58 @@ __attribute__((noinline)) int read_ptr(int *ptr)
964964
return *ptr;
965965
}
966966

967+
void test_pkey_alloc_free_attach_pkey0(int *ptr, u16 pkey)
968+
{
969+
int i, err;
970+
int max_nr_pkey_allocs;
971+
int alloced_pkeys[NR_PKEYS];
972+
int nr_alloced = 0;
973+
long size;
974+
975+
pkey_assert(pkey_last_malloc_record);
976+
size = pkey_last_malloc_record->size;
977+
/*
978+
* This is a bit of a hack. But mprotect() requires
979+
* huge-page-aligned sizes when operating on hugetlbfs.
980+
* So, make sure that we use something that's a multiple
981+
* of a huge page when we can.
982+
*/
983+
if (size >= HPAGE_SIZE)
984+
size = HPAGE_SIZE;
985+
986+
/* allocate every possible key and make sure key-0 never got allocated */
987+
max_nr_pkey_allocs = NR_PKEYS;
988+
for (i = 0; i < max_nr_pkey_allocs; i++) {
989+
int new_pkey = alloc_pkey();
990+
pkey_assert(new_pkey != 0);
991+
992+
if (new_pkey < 0)
993+
break;
994+
alloced_pkeys[nr_alloced++] = new_pkey;
995+
}
996+
/* free all the allocated keys */
997+
for (i = 0; i < nr_alloced; i++) {
998+
int free_ret;
999+
1000+
if (!alloced_pkeys[i])
1001+
continue;
1002+
free_ret = sys_pkey_free(alloced_pkeys[i]);
1003+
pkey_assert(!free_ret);
1004+
}
1005+
1006+
/* attach key-0 in various modes */
1007+
err = sys_mprotect_pkey(ptr, size, PROT_READ, 0);
1008+
pkey_assert(!err);
1009+
err = sys_mprotect_pkey(ptr, size, PROT_WRITE, 0);
1010+
pkey_assert(!err);
1011+
err = sys_mprotect_pkey(ptr, size, PROT_EXEC, 0);
1012+
pkey_assert(!err);
1013+
err = sys_mprotect_pkey(ptr, size, PROT_READ|PROT_WRITE, 0);
1014+
pkey_assert(!err);
1015+
err = sys_mprotect_pkey(ptr, size, PROT_READ|PROT_WRITE|PROT_EXEC, 0);
1016+
pkey_assert(!err);
1017+
}
1018+
9671019
void test_read_of_write_disabled_region(int *ptr, u16 pkey)
9681020
{
9691021
int ptr_contents;
@@ -1448,6 +1500,7 @@ void (*pkey_tests[])(int *ptr, u16 pkey) = {
14481500
test_pkey_syscalls_on_non_allocated_pkey,
14491501
test_pkey_syscalls_bad_args,
14501502
test_pkey_alloc_exhaust,
1503+
test_pkey_alloc_free_attach_pkey0,
14511504
};
14521505

14531506
void run_tests_once(void)

0 commit comments

Comments
 (0)