anitazha / rpms / ndctl

Forked from rpms/ndctl a year ago
Clone

Blame SOURCES/0006-ndctl-test-Exercise-soft_offline_page-corner-cases.patch

e0018b
From 940acf65a61595e8c0db3aebe1c74307acbbef68 Mon Sep 17 00:00:00 2001
e0018b
From: Dan Williams <dan.j.williams@intel.com>
e0018b
Date: Tue, 12 Jan 2021 23:15:14 -0800
e0018b
Subject: [PATCH 006/217] ndctl/test: Exercise soft_offline_page() corner cases
e0018b
e0018b
Test soft-offline injection into PMEM namespace metadata and user mapped
e0018b
space. Both attempts should fail on kernels with a pfn_to_online_page()
e0018b
implementation that considers subsection ZONE_DEVICE ranges.
e0018b
e0018b
Signed-off-by: Dan Williams <dan.j.williams@intel.com>
e0018b
Signed-off-by: Vishal Verma <vishal.l.verma@intel.com>
e0018b
Link: https://lore.kernel.org/r/161052211455.1804207.13884321454837200896.stgit@dwillia2-desk3.amr.corp.intel.com
e0018b
---
e0018b
 test/dax-poison.c | 19 +++++++++++++++++++
e0018b
 test/device-dax.c | 45 +++++++++++++++++++++++++++++++++++++++++++++
e0018b
 2 files changed, 64 insertions(+)
e0018b
e0018b
diff --git a/test/dax-poison.c b/test/dax-poison.c
e0018b
index a4ef12e..4e09761 100644
e0018b
--- a/test/dax-poison.c
e0018b
+++ b/test/dax-poison.c
e0018b
@@ -5,6 +5,7 @@
e0018b
 #include <signal.h>
e0018b
 #include <setjmp.h>
e0018b
 #include <sys/mman.h>
e0018b
+#include <linux/mman.h>
e0018b
 #include <fcntl.h>
e0018b
 #include <string.h>
e0018b
 #include <errno.h>
e0018b
@@ -49,6 +50,7 @@ int test_dax_poison(struct ndctl_test *test, int dax_fd, unsigned long align,
e0018b
 	unsigned char *addr = MAP_FAILED;
e0018b
 	struct sigaction act;
e0018b
 	unsigned x = x;
e0018b
+	FILE *smaps;
e0018b
 	void *buf;
e0018b
 	int rc;
e0018b
 
e0018b
@@ -94,6 +96,9 @@ int test_dax_poison(struct ndctl_test *test, int dax_fd, unsigned long align,
e0018b
 		goto out;
e0018b
 	}
e0018b
 
e0018b
+	fprintf(stderr, "%s: mmap got %p align: %ld offset: %zd\n",
e0018b
+			__func__, addr, align, offset);
e0018b
+
e0018b
 	if (sigsetjmp(sj_env, 1)) {
e0018b
 		if (sig_mcerr_ar) {
e0018b
 			fprintf(stderr, "madvise triggered 'action required' sigbus\n");
e0018b
@@ -104,6 +109,20 @@ int test_dax_poison(struct ndctl_test *test, int dax_fd, unsigned long align,
e0018b
 		}
e0018b
 	}
e0018b
 
e0018b
+	rc = madvise(addr + align / 2, 4096, MADV_SOFT_OFFLINE);
e0018b
+	if (rc == 0) {
e0018b
+		fprintf(stderr, "softoffline should always fail for dax\n");
e0018b
+		smaps = fopen("/proc/self/smaps", "r");
e0018b
+		do {
e0018b
+			rc = fread(buf, 1, 4096, smaps);
e0018b
+			fwrite(buf, 1, rc, stderr);
e0018b
+		} while (rc);
e0018b
+		fclose(smaps);
e0018b
+		fail();
e0018b
+		rc = -ENXIO;
e0018b
+		goto out;
e0018b
+	}
e0018b
+
e0018b
 	rc = madvise(addr + align / 2, 4096, MADV_HWPOISON);
e0018b
 	if (rc) {
e0018b
 		fail();
e0018b
diff --git a/test/device-dax.c b/test/device-dax.c
e0018b
index 5f0da29..aad8fa5 100644
e0018b
--- a/test/device-dax.c
e0018b
+++ b/test/device-dax.c
e0018b
@@ -128,6 +128,44 @@ static int verify_data(struct daxctl_dev *dev, char *dax_buf,
e0018b
 	return 0;
e0018b
 }
e0018b
 
e0018b
+static int test_dax_soft_offline(struct ndctl_test *test, struct ndctl_namespace *ndns)
e0018b
+{
e0018b
+	unsigned long long resource = ndctl_namespace_get_resource(ndns);
e0018b
+	int fd, rc;
e0018b
+	char *buf;
e0018b
+
e0018b
+	if (resource == ULLONG_MAX) {
e0018b
+		fprintf(stderr, "failed to get resource: %s\n",
e0018b
+				ndctl_namespace_get_devname(ndns));
e0018b
+		return -ENXIO;
e0018b
+	}
e0018b
+
e0018b
+	fd = open("/sys/devices/system/memory/soft_offline_page", O_WRONLY);
e0018b
+	if (fd < 0) {
e0018b
+		fprintf(stderr, "failed to open soft_offline_page\n");
e0018b
+		return -ENOENT;
e0018b
+	}
e0018b
+
e0018b
+	rc = asprintf(&buf, "%#llx\n", resource);
e0018b
+	if (rc < 0) {
e0018b
+		fprintf(stderr, "failed to alloc resource\n");
e0018b
+		close(fd);
e0018b
+		return -ENOMEM;
e0018b
+	}
e0018b
+
e0018b
+	fprintf(stderr, "%s: try to offline page @%#llx\n", __func__, resource);
e0018b
+	rc = write(fd, buf, rc);
e0018b
+	free(buf);
e0018b
+	close(fd);
e0018b
+
e0018b
+	if (rc >= 0) {
e0018b
+		fprintf(stderr, "%s: should have failed\n", __func__);
e0018b
+		return -ENXIO;
e0018b
+	}
e0018b
+
e0018b
+	return 0;
e0018b
+}
e0018b
+
e0018b
 static int __test_device_dax(unsigned long align, int loglevel,
e0018b
 		struct ndctl_test *test, struct ndctl_ctx *ctx)
e0018b
 {
e0018b
@@ -278,6 +316,13 @@ static int __test_device_dax(unsigned long align, int loglevel,
e0018b
 			goto out;
e0018b
 		}
e0018b
 
e0018b
+		rc = test_dax_soft_offline(test, ndns);
e0018b
+		if (rc) {
e0018b
+			fprintf(stderr, "%s: failed dax soft offline\n",
e0018b
+					ndctl_namespace_get_devname(ndns));
e0018b
+			goto out;
e0018b
+		}
e0018b
+
e0018b
 		rc = test_dax_poison(test, fd, align, NULL, 0, devdax);
e0018b
 		if (rc) {
e0018b
 			fprintf(stderr, "%s: failed dax poison\n",
e0018b
-- 
e0018b
2.27.0
e0018b