Skip to content

Commit 6e2c2d0

Browse files
Ram Paitorvalds
authored andcommitted
selftests/vm/pkeys: introduce a sub-page allocator
This introduces a new allocator that allocates 4K hardware pages to back 64K linux pages. This allocator is available only on powerpc. 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/c4a82fa962ec71015b994fab1aaf83bdfd091553.1585646528.git.sandipan@linux.ibm.com Signed-off-by: Linus Torvalds <[email protected]>
1 parent 4e06e71 commit 6e2c2d0

File tree

4 files changed

+37
-0
lines changed

4 files changed

+37
-0
lines changed

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@
2828
extern int dprint_in_signal;
2929
extern char dprint_in_signal_buffer[DPRINT_IN_SIGNAL_BUF_SIZE];
3030

31+
extern int test_nr;
32+
extern int iteration_nr;
33+
3134
#ifdef __GNUC__
3235
__attribute__((format(printf, 1, 2)))
3336
#endif
@@ -78,6 +81,9 @@ __attribute__((noinline)) int read_ptr(int *ptr);
7881
void expected_pkey_fault(int pkey);
7982
int sys_pkey_alloc(unsigned long flags, unsigned long init_val);
8083
int sys_pkey_free(unsigned long pkey);
84+
int mprotect_pkey(void *ptr, size_t size, unsigned long orig_prot,
85+
unsigned long pkey);
86+
void record_pkey_malloc(void *ptr, long size, int prot);
8187

8288
#if defined(__i386__) || defined(__x86_64__) /* arch */
8389
#include "pkey-x86.h"

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,4 +107,29 @@ void expect_fault_on_read_execonly_key(void *p1, int pkey)
107107
/* 4-byte instructions * 16384 = 64K page */
108108
#define __page_o_noops() asm(".rept 16384 ; nop; .endr")
109109

110+
void *malloc_pkey_with_mprotect_subpage(long size, int prot, u16 pkey)
111+
{
112+
void *ptr;
113+
int ret;
114+
115+
dprintf1("doing %s(size=%ld, prot=0x%x, pkey=%d)\n", __func__,
116+
size, prot, pkey);
117+
pkey_assert(pkey < NR_PKEYS);
118+
ptr = mmap(NULL, size, prot, MAP_ANONYMOUS|MAP_PRIVATE, -1, 0);
119+
pkey_assert(ptr != (void *)-1);
120+
121+
ret = syscall(__NR_subpage_prot, ptr, size, NULL);
122+
if (ret) {
123+
perror("subpage_perm");
124+
return PTR_ERR_ENOTSUP;
125+
}
126+
127+
ret = mprotect_pkey((void *)ptr, PAGE_SIZE, prot, pkey);
128+
pkey_assert(!ret);
129+
record_pkey_malloc(ptr, size, prot);
130+
131+
dprintf1("%s() for pkey %d @ %p\n", __func__, pkey, ptr);
132+
return ptr;
133+
}
134+
110135
#endif /* _PKEYS_POWERPC_H */

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,4 +173,9 @@ void expect_fault_on_read_execonly_key(void *p1, int pkey)
173173
expected_pkey_fault(pkey);
174174
}
175175

176+
void *malloc_pkey_with_mprotect_subpage(long size, int prot, u16 pkey)
177+
{
178+
return PTR_ERR_ENOTSUP;
179+
}
180+
176181
#endif /* _PKEYS_X86_H */

tools/testing/selftests/vm/protection_keys.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -845,6 +845,7 @@ void *malloc_pkey_mmap_dax(long size, int prot, u16 pkey)
845845
void *(*pkey_malloc[])(long size, int prot, u16 pkey) = {
846846

847847
malloc_pkey_with_mprotect,
848+
malloc_pkey_with_mprotect_subpage,
848849
malloc_pkey_anon_huge,
849850
malloc_pkey_hugetlb
850851
/* can not do direct with the pkey_mprotect() API:

0 commit comments

Comments
 (0)