Skip to contentSkip to conversation

pkey: add syscall numbers for pkey

PATCH Superseded Li Wang 2 participants 7 messages Jun 26, 2019 – Jul 1, 2019

v4 of 5 Superseded Latest is v5 →

lore
Share & tools
lore

Li Wang

author
#0 lore raw

PATCH v4 1/2 pkey: add syscall numbers for pkey

---
 include/lapi/syscalls/aarch64.in   | 3 +++
 include/lapi/syscalls/arm.in       | 3 +++
 include/lapi/syscalls/i386.in      | 3 +++
 include/lapi/syscalls/ia64.in      | 3 +++
 include/lapi/syscalls/powerpc.in   | 3 +++
 include/lapi/syscalls/powerpc64.in | 3 +++
 include/lapi/syscalls/s390.in      | 3 +++
 include/lapi/syscalls/s390x.in     | 3 +++
 include/lapi/syscalls/sparc.in     | 3 +++
 include/lapi/syscalls/sparc64.in   | 3 +++
 include/lapi/syscalls/x86_64.in    | 3 +++
 11 files changed, 33 insertions(+)
include/lapi/syscalls/aarch64.in +3 -0
index ce6080894..7db6e281c 100644
@@ -265,5 +265,8 @@ mlock2 284
 copy_file_range 285
 preadv2 286
 pwritev2 287
+pkey_mprotect 288
+pkey_alloc 289
+pkey_free 290
 pidfd_send_signal 424
 _sysctl 1078
include/lapi/syscalls/arm.in +3 -0
index 33168889f..00e99f2b9 100644
@@ -349,5 +349,8 @@ mlock2 (__NR_SYSCALL_BASE+390)
 copy_file_range (__NR_SYSCALL_BASE+391)
 preadv2 (__NR_SYSCALL_BASE+392)
 pwritev2 (__NR_SYSCALL_BASE+393)
+pkey_mprotect (__NR_SYSCALL_BASE+394)
+pkey_alloc (__NR_SYSCALL_BASE+395)
+pkey_free (__NR_SYSCALL_BASE+396)
 statx (__NR_SYSCALL_BASE+397)
 pidfd_send_signal (__NR_SYSCALL_BASE+424)
include/lapi/syscalls/i386.in +3 -0
index 3355b9127..02f3955ba 100644
@@ -347,5 +347,8 @@ mlock2 376
 copy_file_range 377
 preadv2 378
 pwritev2 379
+pkey_mprotect 380
+pkey_alloc 381
+pkey_free 382
 statx 383
 pidfd_send_signal 424
include/lapi/syscalls/ia64.in +3 -0
index 9ac81e91e..cf9f73e85 100644
@@ -305,4 +305,7 @@ mlock2 1346
 copy_file_range 1347
 preadv2 1348
 pwritev2 1349
+pkey_mprotect 1354
+pkey_alloc 1355
+pkey_free 1356
 pidfd_send_signal 1448
include/lapi/syscalls/powerpc.in +3 -0
index 488d6dfa9..660165d7a 100644
@@ -356,3 +356,6 @@ preadv2 380
 pwritev2 381
 statx 383
 pidfd_send_signal 424
+pkey_mprotect 386
+pkey_alloc 384
+pkey_free 385
include/lapi/syscalls/powerpc64.in +3 -0
index 488d6dfa9..660165d7a 100644
@@ -356,3 +356,6 @@ preadv2 380
 pwritev2 381
 statx 383
 pidfd_send_signal 424
+pkey_mprotect 386
+pkey_alloc 384
+pkey_free 385
include/lapi/syscalls/s390.in +3 -0
index 69b49eac0..c304ef4b7 100644
@@ -338,4 +338,7 @@ mlock2 374
 copy_file_range 375
 preadv2 376
 pwritev2 377
+pkey_mprotect 384
+pkey_alloc 385
+pkey_free 386
 pidfd_send_signal 424
include/lapi/syscalls/s390x.in +3 -0
index 685c16f68..7d632d1dc 100644
@@ -337,4 +337,7 @@ mlock2 374
 copy_file_range 375
 preadv2 376
 pwritev2 377
+pkey_mprotect 384
+pkey_alloc 385
+pkey_free 386
 pidfd_send_signal 424
include/lapi/syscalls/sparc.in +3 -0
index e74b48170..ab7204663 100644
@@ -343,4 +343,7 @@ mlock2 356
 copy_file_range 357
 preadv2 358
 pwritev2 359
+pkey_mprotect 362
+pkey_alloc 363
+pkey_free 364
 pidfd_send_signal 424
include/lapi/syscalls/sparc64.in +3 -0
index 577aa9b35..d17dce5cd 100644
@@ -319,4 +319,7 @@ mlock2 356
 copy_file_range 357
 preadv2 358
 pwritev2 359
+pkey_mprotect 362
+pkey_alloc 363
+pkey_free 364
 pidfd_send_signal 424
include/lapi/syscalls/x86_64.in +3 -0
index 99d387277..fdb414c10 100644
@@ -314,5 +314,8 @@ mlock2 325
 copy_file_range 326
 preadv2 327
 pwritev2 328
+pkey_mprotect 329
+pkey_alloc 330
+pkey_free 331
 statx 332
 pidfd_send_signal 424
Signature · 1 line
2.20.1
author
#1 lore raw

PATCH v4 2/2 pkey: add test for memory protection keys

Memory Protection Keys for Userspace (PKU aka PKEYs) is a Skylake-SP server feature that provides a mechanism for enforcing page-based protections, but without requiring modification of the page tables when an application changes protection domains. It works by dedicating 4 previously ignored bits in each page table entry to a "protection key", giving 16 possible keys.

Basic method for PKEYs test:
1. test allocates a pkey(e.g. PKEY_DISABLE_ACCESS) via pkey_alloc()
2. pkey_mprotect() apply this pkey to a piece of memory buffer
3. check if the access right of buffer has been changed and take effect
4. remove the access right(pkey) from this buffer via pkey_mprotect()
5. check if buffer area can be read or write after removing pkey
6. pkey_free() releases the pkey after using it

Looping around this basic test on diffenrent types of memory.

---

Notes:
v3 --> v4
* correct syscall with __NR_ prefix
* verify the value has been wrote in nr_hugepages
* tst_res() message could print also mmap flags
* add flag_to_str() function
* .needs_root = 1

 configure.ac                               |   1 +
 runtest/syscalls                           |   2 +
 testcases/kernel/syscalls/pkeys/.gitignore |   1 +
 testcases/kernel/syscalls/pkeys/Makefile   |   8 +
 testcases/kernel/syscalls/pkeys/pkey.h     |  50 ++++
 testcases/kernel/syscalls/pkeys/pkey01.c   | 266 +++++++++++++++++++++
 6 files changed, 328 insertions(+)
 create mode 100644 testcases/kernel/syscalls/pkeys/.gitignore
 create mode 100644 testcases/kernel/syscalls/pkeys/Makefile
 create mode 100644 testcases/kernel/syscalls/pkeys/pkey.h
 create mode 100644 testcases/kernel/syscalls/pkeys/pkey01.c
configure.ac +1 -0
index 65fc2a232..5a9b74b0a 100644
@@ -74,6 +74,7 @@ AC_CHECK_FUNCS([ \
     pidfd_send_signal \
     preadv \
     preadv2 \
+    pkey_mprotect \
     profil \
     pwritev \
     pwritev2 \
runtest/syscalls +2 -0
index c6a064481..6ea991f12 100644
@@ -721,6 +721,8 @@ mprotect02 mprotect02
 mprotect03 mprotect03
 mprotect04 mprotect04
 
+pkey01 pkey01
+
 mq_notify01 mq_notify01
 mq_notify02 mq_notify02
 mq_open01 mq_open01
testcases/kernel/syscalls/pkeys/.gitignore new file +1 -0
new file mode 100644
index 000000000..6fd5addb8
@@ -0,0 +1 @@
+/pkey01
testcases/kernel/syscalls/pkeys/Makefile new file +8 -0
new file mode 100644
index 000000000..9ee2c2ea5
@@ -0,0 +1,8 @@
+# SPDX-License-Identifier: GPL-2.0-or-later
+# Copyright (c) 2019 Red Hat, Inc.
+
+top_srcdir		?= ../../../..
+
+include $(top_srcdir)/include/mk/testcases.mk
+
+include $(top_srcdir)/include/mk/generic_leaf_target.mk
testcases/kernel/syscalls/pkeys/pkey.h new file +50 -0

50 lines not on this page · raw message

testcases/kernel/syscalls/pkeys/pkey01.c new file +266 -0

266 lines not on this page · raw message

Signature · 1 line
2.20.1
#2 lore raw

PATCH v4 2/2 pkey: add test for memory protection keys

----- Original Message -----

testcases/kernel/syscalls/pkeys/pkey01.c line 67

Quoted 13 lines
testcases/kernel/syscalls/pkeys/pkey01.c new file +13 -0
+
+static void setup(void)
+{
+	int i, fd;
+
+	if (access(PATH_VM_NRHPS, F_OK)) {
+		tst_res(TINFO, "Huge page is not supported");
+		size = getpagesize();
+		no_hugepage = 1;
+	} else {
+		int val;
+
+		SAFE_FILE_PRINTF(PATH_VM_NRHPS, "%d", 1);

This is still SAFE write, which may trigger TBROK:

# ./pkey01 
tst_test.c:1100: INFO: Timeout per run is 0h 05m 00s
safe_file_ops.c:301: BROK: Failed to close FILE '/proc/sys/vm/nr_hugepages' at pkey01.c:67: EOPNOTSUPP
safe_macros.c:773: WARN: pkey01.c:91: umount(tmp_pkey) failed: ENOENT
safe_macros.c:184: WARN: pkey01.c:92: rmdir(tmp_pkey) failed: ENOENT

Review

Quoted 2 lines
(quoted diff) +2 -0
+
+		SAFE_FILE_SCANF(PATH_VM_NRHPS, "%d", &val);

safe_file_ops.c:157: BROK: The FILE '/proc/sys/vm/nr_hugepages' ended prematurely at pkey01.c:69

Quoted 7 lines
testcases/kernel/syscalls/pkeys/pkey01.c new file +7 -0
+		if (val != 1)
+			tst_brk(TBROK, "nr_hugepages = %d, but expect %d",
+					val, 1);
+
+		size = SAFE_READ_MEMINFO("Hugepagesize:") * 1024;
+	}
+
author
#3 lore raw

PATCH v4 2/2 pkey: add test for memory protection keys

Quoted 18 lines · Jan Stancek

----- Original Message -----

testcases/kernel/syscalls/pkeys/pkey01.c line 67

Quoted 13 lines
testcases/kernel/syscalls/pkeys/pkey01.c new file +13 -0
+
+static void setup(void)
+{
+     int i, fd;
+
+     if (access(PATH_VM_NRHPS, F_OK)) {
+             tst_res(TINFO, "Huge page is not supported");
+             size = getpagesize();
+             no_hugepage = 1;
+     } else {
+             int val;
+
+             SAFE_FILE_PRINTF(PATH_VM_NRHPS, "%d", 1);

This is still SAFE write, which may trigger TBROK:

Quoted 7 lines
# ./pkey01
tst_test.c:1100: INFO: Timeout per run is 0h 05m 00s
safe_file_ops.c:301: BROK: Failed to close FILE
'/proc/sys/vm/nr_hugepages' at pkey01.c:67: EOPNOTSUPP
safe_macros.c:773: WARN: pkey01.c:91: umount(tmp_pkey) failed: ENOENT
safe_macros.c:184: WARN: pkey01.c:92: rmdir(tmp_pkey) failed: ENOENT

Er, sorry about still not working here.

If a system(e.g PowerKVM guest) configured with NO huge page support, then the file '/proc/sys/vm/nr_hugepages' exist but EOPNOTSUPP to read/write. In this key01, perhaps I shouldn't use the "/proc/.../nr_hugepages" to detect that at the beginning.

Seems the correct way is to use "/sys/kernel/mm/hugepages/" for huge page detecting and leave the SAFE_FILE_* still in setup(). Because we need to catch the "/proc/.../nr_hugepages" open/close or read/write issue in testing.

What do you think? Should we mask the EOPNOTSUPP as a TCONF skipping or others?

Signature · 5 lines
Regards,
Li Wang
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.linux.it/pipermail/ltp/attachments/20190626/8ce26f4e/attachment-0001.htm>
#4 lore raw

PATCH v4 2/2 pkey: add test for memory protection keys

----- Original Message -----

Quoted 35 lines · Jan Stancek
Quoted 19 lines

----- Original Message -----

testcases/kernel/syscalls/pkeys/pkey01.c line 67

Quoted 13 lines
testcases/kernel/syscalls/pkeys/pkey01.c new file +13 -0
+
+static void setup(void)
+{
+     int i, fd;
+
+     if (access(PATH_VM_NRHPS, F_OK)) {
+             tst_res(TINFO, "Huge page is not supported");
+             size = getpagesize();
+             no_hugepage = 1;
+     } else {
+             int val;
+
+             SAFE_FILE_PRINTF(PATH_VM_NRHPS, "%d", 1);

This is still SAFE write, which may trigger TBROK:

Quoted 7 lines
# ./pkey01
tst_test.c:1100: INFO: Timeout per run is 0h 05m 00s
safe_file_ops.c:301: BROK: Failed to close FILE
'/proc/sys/vm/nr_hugepages' at pkey01.c:67: EOPNOTSUPP
safe_macros.c:773: WARN: pkey01.c:91: umount(tmp_pkey) failed: ENOENT
safe_macros.c:184: WARN: pkey01.c:92: rmdir(tmp_pkey) failed: ENOENT

Er, sorry about still not working here.

If a system(e.g PowerKVM guest) configured with NO huge page support, then the file '/proc/sys/vm/nr_hugepages' exist but EOPNOTSUPP to read/write. In this key01, perhaps I shouldn't use the "/proc/.../nr_hugepages" to detect that at the beginning.

Quoted 4 lines

Seems the correct way is to use "/sys/kernel/mm/hugepages/" for huge page detecting and leave the SAFE_FILE_* still in setup(). Because we need to catch the "/proc/.../nr_hugepages" open/close or read/write issue in testing.

That should work, I see we used that in some tests already.

Quoted 8 lines

What do you think? Should we mask the EOPNOTSUPP as a TCONF skipping or others?

--
Regards,
Li Wang

author
#5 lore raw

PATCH v4 2/2 pkey: add test for memory protection keys

Quoted 9 lines · Jan Stancek

...

Quoted 2 lines

If a system(e.g PowerKVM guest) configured with NO huge page support,

then

Quoted 1 line

the file '/proc/sys/vm/nr_hugepages' exist but EOPNOTSUPP to read/write.

In

Quoted 1 line

this key01, perhaps I shouldn't use the "/proc/.../nr_hugepages" to

detect

Quoted 9 lines
Quoted 6 lines

that at the beginning.

Seems the correct way is to use "/sys/kernel/mm/hugepages/" for huge page detecting and leave the SAFE_FILE_* still in setup(). Because we need to catch the "/proc/.../nr_hugepages" open/close or read/write issue in testing.

That should work, I see we used that in some tests already.

Thanks, should I format new patch v5? or any more comments?

Signature · 5 lines
Regards,
Li Wang
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.linux.it/pipermail/ltp/attachments/20190701/c6f7a909/attachment.htm>
#6 lore raw

PATCH v4 2/2 pkey: add test for memory protection keys

----- Original Message -----

Quoted 18 lines · Jan Stancek
Quoted 17 lines

...

Quoted 2 lines

If a system(e.g PowerKVM guest) configured with NO huge page support,

then

Quoted 1 line

the file '/proc/sys/vm/nr_hugepages' exist but EOPNOTSUPP to read/write.

In

Quoted 1 line

this key01, perhaps I shouldn't use the "/proc/.../nr_hugepages" to

detect

Quoted 6 lines

that at the beginning.

Seems the correct way is to use "/sys/kernel/mm/hugepages/" for huge page detecting and leave the SAFE_FILE_* still in setup(). Because we need to catch the "/proc/.../nr_hugepages" open/close or read/write issue in testing.

Quoted 4 lines
Quoted 2 lines

That should work, I see we used that in some tests already.

Thanks, should I format new patch v5? or any more comments?

Yes, please send v5.