diff mbox series

[ndctl,4/4] ndctl/test: Exercise soft_offline_page() corner cases

Message ID 161052211455.1804207.13884321454837200896.stgit@dwillia2-desk3.amr.corp.intel.com (mailing list archive)
State Accepted
Commit 940acf65a61595e8c0db3aebe1c74307acbbef68
Headers show
Series ndctl/test: softoffline, mremap, and misc fixups | expand

Commit Message

Dan Williams Jan. 13, 2021, 7:15 a.m. UTC
Test soft-offline injection into PMEM namespace metadata and user mapped
space. Both attempts should fail on kernels with a pfn_to_online_page()
implementation that considers subsection ZONE_DEVICE ranges.

Signed-off-by: Dan Williams <dan.j.williams@intel.com>
---
 test/dax-poison.c |   19 +++++++++++++++++++
 test/device-dax.c |   45 +++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 64 insertions(+)
diff mbox series

Patch

diff --git a/test/dax-poison.c b/test/dax-poison.c
index a4ef12eca1be..4e09761f0a5e 100644
--- a/test/dax-poison.c
+++ b/test/dax-poison.c
@@ -5,6 +5,7 @@ 
 #include <signal.h>
 #include <setjmp.h>
 #include <sys/mman.h>
+#include <linux/mman.h>
 #include <fcntl.h>
 #include <string.h>
 #include <errno.h>
@@ -49,6 +50,7 @@  int test_dax_poison(struct ndctl_test *test, int dax_fd, unsigned long align,
 	unsigned char *addr = MAP_FAILED;
 	struct sigaction act;
 	unsigned x = x;
+	FILE *smaps;
 	void *buf;
 	int rc;
 
@@ -94,6 +96,9 @@  int test_dax_poison(struct ndctl_test *test, int dax_fd, unsigned long align,
 		goto out;
 	}
 
+	fprintf(stderr, "%s: mmap got %p align: %ld offset: %zd\n",
+			__func__, addr, align, offset);
+
 	if (sigsetjmp(sj_env, 1)) {
 		if (sig_mcerr_ar) {
 			fprintf(stderr, "madvise triggered 'action required' sigbus\n");
@@ -104,6 +109,20 @@  int test_dax_poison(struct ndctl_test *test, int dax_fd, unsigned long align,
 		}
 	}
 
+	rc = madvise(addr + align / 2, 4096, MADV_SOFT_OFFLINE);
+	if (rc == 0) {
+		fprintf(stderr, "softoffline should always fail for dax\n");
+		smaps = fopen("/proc/self/smaps", "r");
+		do {
+			rc = fread(buf, 1, 4096, smaps);
+			fwrite(buf, 1, rc, stderr);
+		} while (rc);
+		fclose(smaps);
+		fail();
+		rc = -ENXIO;
+		goto out;
+	}
+
 	rc = madvise(addr + align / 2, 4096, MADV_HWPOISON);
 	if (rc) {
 		fail();
diff --git a/test/device-dax.c b/test/device-dax.c
index 5f0da297f28e..aad8fa5f1cb1 100644
--- a/test/device-dax.c
+++ b/test/device-dax.c
@@ -128,6 +128,44 @@  static int verify_data(struct daxctl_dev *dev, char *dax_buf,
 	return 0;
 }
 
+static int test_dax_soft_offline(struct ndctl_test *test, struct ndctl_namespace *ndns)
+{
+	unsigned long long resource = ndctl_namespace_get_resource(ndns);
+	int fd, rc;
+	char *buf;
+
+	if (resource == ULLONG_MAX) {
+		fprintf(stderr, "failed to get resource: %s\n",
+				ndctl_namespace_get_devname(ndns));
+		return -ENXIO;
+	}
+
+	fd = open("/sys/devices/system/memory/soft_offline_page", O_WRONLY);
+	if (fd < 0) {
+		fprintf(stderr, "failed to open soft_offline_page\n");
+		return -ENOENT;
+	}
+
+	rc = asprintf(&buf, "%#llx\n", resource);
+	if (rc < 0) {
+		fprintf(stderr, "failed to alloc resource\n");
+		close(fd);
+		return -ENOMEM;
+	}
+
+	fprintf(stderr, "%s: try to offline page @%#llx\n", __func__, resource);
+	rc = write(fd, buf, rc);
+	free(buf);
+	close(fd);
+
+	if (rc >= 0) {
+		fprintf(stderr, "%s: should have failed\n", __func__);
+		return -ENXIO;
+	}
+
+	return 0;
+}
+
 static int __test_device_dax(unsigned long align, int loglevel,
 		struct ndctl_test *test, struct ndctl_ctx *ctx)
 {
@@ -278,6 +316,13 @@  static int __test_device_dax(unsigned long align, int loglevel,
 			goto out;
 		}
 
+		rc = test_dax_soft_offline(test, ndns);
+		if (rc) {
+			fprintf(stderr, "%s: failed dax soft offline\n",
+					ndctl_namespace_get_devname(ndns));
+			goto out;
+		}
+
 		rc = test_dax_poison(test, fd, align, NULL, 0, devdax);
 		if (rc) {
 			fprintf(stderr, "%s: failed dax poison\n",