[PATCH 11/13] x86/pkeys/selftests: Save off 'prot' for allocations

From: Dave Hansen
Date: Wed May 09 2018 - 13:19:49 EST



From: Dave Hansen <dave.hansen@xxxxxxxxxxxxxxx>

This makes it possible to to tell what 'prot' a given allocation
is supposed to have. That way, if we want to change just the
pkey, we know what 'prot' to pass to mprotect_pkey().

Also, keep a record of the most recent allocation so the tests
can easily find it.

Signed-off-by: Dave Hansen <dave.hansen@xxxxxxxxxxxxxxx>
Cc: Ram Pai <linuxram@xxxxxxxxxx>
Cc: Thomas Gleixner <tglx@xxxxxxxxxxxxx>
Cc: Dave Hansen <dave.hansen@xxxxxxxxx>
Cc: Michael Ellermen <mpe@xxxxxxxxxxxxxx>
Cc: Ingo Molnar <mingo@xxxxxxxxxx>
Cc: Andrew Morton <akpm@xxxxxxxxxxxxxxxxxxxx>
Cc: Shuah Khan <shuah@xxxxxxxxxx>
---

b/tools/testing/selftests/x86/protection_keys.c | 14 +++++++++-----
1 file changed, 9 insertions(+), 5 deletions(-)

diff -puN tools/testing/selftests/x86/protection_keys.c~pkeys-update-selftests-store-malloc-record tools/testing/selftests/x86/protection_keys.c
--- a/tools/testing/selftests/x86/protection_keys.c~pkeys-update-selftests-store-malloc-record 2018-05-09 09:20:23.340698395 -0700
+++ b/tools/testing/selftests/x86/protection_keys.c 2018-05-09 09:20:23.344698395 -0700
@@ -662,10 +662,12 @@ int mprotect_pkey(void *ptr, size_t size
struct pkey_malloc_record {
void *ptr;
long size;
+ int prot;
};
struct pkey_malloc_record *pkey_malloc_records;
+struct pkey_malloc_record *pkey_last_malloc_record;
long nr_pkey_malloc_records;
-void record_pkey_malloc(void *ptr, long size)
+void record_pkey_malloc(void *ptr, long size, int prot)
{
long i;
struct pkey_malloc_record *rec = NULL;
@@ -697,6 +699,8 @@ void record_pkey_malloc(void *ptr, long
(int)(rec - pkey_malloc_records), rec, ptr, size);
rec->ptr = ptr;
rec->size = size;
+ rec->prot = prot;
+ pkey_last_malloc_record = rec;
nr_pkey_malloc_records++;
}

@@ -741,7 +745,7 @@ void *malloc_pkey_with_mprotect(long siz
pkey_assert(ptr != (void *)-1);
ret = mprotect_pkey((void *)ptr, PAGE_SIZE, prot, pkey);
pkey_assert(!ret);
- record_pkey_malloc(ptr, size);
+ record_pkey_malloc(ptr, size, prot);
rdpkru();

dprintf1("%s() for pkey %d @ %p\n", __func__, pkey, ptr);
@@ -762,7 +766,7 @@ void *malloc_pkey_anon_huge(long size, i
size = ALIGN_UP(size, HPAGE_SIZE * 2);
ptr = mmap(NULL, size, PROT_NONE, MAP_ANONYMOUS|MAP_PRIVATE, -1, 0);
pkey_assert(ptr != (void *)-1);
- record_pkey_malloc(ptr, size);
+ record_pkey_malloc(ptr, size, prot);
mprotect_pkey(ptr, size, prot, pkey);

dprintf1("unaligned ptr: %p\n", ptr);
@@ -835,7 +839,7 @@ void *malloc_pkey_hugetlb(long size, int
pkey_assert(ptr != (void *)-1);
mprotect_pkey(ptr, size, prot, pkey);

- record_pkey_malloc(ptr, size);
+ record_pkey_malloc(ptr, size, prot);

dprintf1("mmap()'d hugetlbfs for pkey %d @ %p\n", pkey, ptr);
return ptr;
@@ -857,7 +861,7 @@ void *malloc_pkey_mmap_dax(long size, in

mprotect_pkey(ptr, size, prot, pkey);

- record_pkey_malloc(ptr, size);
+ record_pkey_malloc(ptr, size, prot);

dprintf1("mmap()'d for pkey %d @ %p\n", pkey, ptr);
close(fd);
_