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

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