[Devel] [PATCH RHEL9 COMMIT] selftests/prctl: Test managing memory allocation scopes

Konstantin Khorenko khorenko at virtuozzo.com
Thu Apr 20 16:55:46 MSK 2023


The commit is pushed to "branch-rh9-5.14.0-162.18.1.vz9.19.x-ovz" and will appear at https://src.openvz.org/scm/ovz/vzkernel.git
after rh9-5.14.0-162.18.1.vz9.19.6
------>
commit f16aef5aef9a703681d1bbda44c3ec1761865187
Author: Alexander Atanasov <alexander.atanasov at virtuozzo.com>
Date:   Wed Apr 12 20:09:18 2023 +0300

    selftests/prctl: Test managing memory allocation scopes
    
    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>
    
    Feature: vStorage
---
 tools/testing/selftests/prctl/Makefile        |  5 +-
 tools/testing/selftests/prctl/memflags-test.c | 96 +++++++++++++++++++++++++++
 2 files changed, 100 insertions(+), 1 deletion(-)

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);
+}


More information about the Devel mailing list