[Devel] [PATCH vz9 v2 2/2] selftests/prctl: test managing memory allocation scopes
Alexander Atanasov
alexander.atanasov at virtuozzo.com
Wed Apr 12 20:09:18 MSK 2023
Add basic tests to verify memory allocation flags operations
via prctl(...).
https://jira.sw.ru/browse/PSBM-141577
Signed-off-by: Alexander Atanasov <alexander.atanasov at virtuozzo.com>
---
tools/testing/selftests/prctl/Makefile | 5 +-
tools/testing/selftests/prctl/memflags-test.c | 96 +++++++++++++++++++
2 files changed, 100 insertions(+), 1 deletion(-)
create mode 100644 tools/testing/selftests/prctl/memflags-test.c
v1->v2: no change in test
diff --git a/tools/testing/selftests/prctl/Makefile b/tools/testing/selftests/prctl/Makefile
index c7923b205222..802a7ab4944c 100644
--- a/tools/testing/selftests/prctl/Makefile
+++ b/tools/testing/selftests/prctl/Makefile
@@ -1,10 +1,13 @@
# SPDX-License-Identifier: GPL-2.0
+
+TEST_PROGS := memflags-test
+
ifndef CROSS_COMPILE
uname_M := $(shell uname -m 2>/dev/null || echo not)
ARCH ?= $(shell echo $(uname_M) | sed -e s/i.86/x86/ -e s/x86_64/x86/)
ifeq ($(ARCH),x86)
-TEST_PROGS := disable-tsc-ctxt-sw-stress-test disable-tsc-on-off-stress-test \
+TEST_PROGS += disable-tsc-ctxt-sw-stress-test disable-tsc-on-off-stress-test \
disable-tsc-test
all: $(TEST_PROGS)
diff --git a/tools/testing/selftests/prctl/memflags-test.c b/tools/testing/selftests/prctl/memflags-test.c
new file mode 100644
index 000000000000..6f240d11cebc
--- /dev/null
+++ b/tools/testing/selftests/prctl/memflags-test.c
@@ -0,0 +1,96 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Tests for prctl(PR_MEMALLOC_FLAGS, ...)
+ *
+ * Basic test to test behaviour of PR_MEMALLOC_FLAGS
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <signal.h>
+#include <errno.h>
+#include <inttypes.h>
+
+
+#include <sys/prctl.h>
+#include <linux/prctl.h>
+
+#ifndef PR_MEMALLOC_FLAGS
+/* Set task memalloc flags */
+#define PR_MEMALLOC_FLAGS 1001
+#define PR_MEMALLOC_GET_FLAGS 1
+#define PR_MEMALLOC_SET_FLAGS 2
+#define PR_MEMALLOC_CLEAR_FLAGS 3
+#endif
+
+#ifndef PF_MEMALLOC
+#define PF_MEMALLOC 0x00000800 /* Allocating memory */
+#endif
+
+#ifndef PF_MEMALLOC_NOFS
+#define PF_MEMALLOC_NOFS 0x00040000 /* All allocation requests will inherit GFP_NOFS */
+#endif
+
+#ifndef PF_MEMALLOC_NOIO
+#define PF_MEMALLOC_NOIO 0x00080000 /* All allocation requests will inherit GFP_NOIO */
+#endif
+
+#ifndef PF_MEMALLOC_PIN
+#define PF_MEMALLOC_PIN 0x10000000 /* Allocation context constrained to zones which allow long term pinning. */
+#endif
+
+void test_flag(int testflag, const char *flagname)
+{
+ int flags = prctl(PR_MEMALLOC_FLAGS, PR_MEMALLOC_SET_FLAGS, testflag);
+ if (flags == -1) {
+ fprintf(stdout, "SET_FLAGS (%s) == %d errno=%d\n", flagname,
+ flags, errno);
+ fflush(stdout);
+ exit(EXIT_FAILURE);
+ }
+ flags = prctl(PR_MEMALLOC_FLAGS, PR_MEMALLOC_GET_FLAGS);
+ if (flags != testflag) {
+ fprintf(stdout, "SET_FLAGS (%s) success but not set : %d\n",
+ flagname, flags);
+ fprintf(stdout, "GET_FLAGS == %d\n", flags);
+ exit(EXIT_FAILURE);
+ }
+
+ flags = prctl(PR_MEMALLOC_FLAGS, PR_MEMALLOC_CLEAR_FLAGS, testflag);
+ if (flags == -1) {
+ fprintf(stdout, "CLEAR_FLAGS (%s) == %d errno=%d\n",
+ flagname, flags, errno);
+ fflush(stdout);
+ exit(EXIT_FAILURE);
+ }
+}
+
+#define TESTFLAG(x) test_flag(x, #x)
+
+int main(void)
+{
+ int flags;
+
+ flags = prctl(PR_MEMALLOC_FLAGS, PR_MEMALLOC_GET_FLAGS);
+ if (flags == -1) {
+ fprintf(stdout, "GET_FLAGS errno=%d\n", errno);
+ fflush(stdout);
+ exit(EXIT_FAILURE);
+ }
+
+ flags = prctl(PR_MEMALLOC_FLAGS, PR_MEMALLOC_SET_FLAGS, 1);
+ if (flags != -1) {
+ fprintf(stdout, "SET_FLAGS (invalid) == %d errno=%d\n", flags,
+ errno);
+ fflush(stdout);
+ exit(EXIT_FAILURE);
+ }
+
+ TESTFLAG(PF_MEMALLOC);
+ TESTFLAG(PF_MEMALLOC_NOFS);
+ TESTFLAG(PF_MEMALLOC_NOIO);
+ TESTFLAG(PF_MEMALLOC_PIN);
+
+ exit(EXIT_SUCCESS);
+}
--
2.39.1
More information about the Devel
mailing list