diff mbox series

[ndctl,16/16] test/ndctl: mremap pmd confusion

Message ID 159408970344.2386154.3822093364134233478.stgit@dwillia2-desk3.amr.corp.intel.com (mailing list archive)
State Superseded
Headers show
Series Firmware Activation and Test Updates | expand

Commit Message

Dan Williams July 7, 2020, 2:41 a.m. UTC
mremap() fails to check for pmd_devmap() entries in some kernels leading to
a case where move_page_tables() interprets a pmd_devmap() entry as a page
table and not a leaf entry. Reproduce this failing condition across
device-dax and fileystem-dax.

Signed-off-by: Dan Williams <dan.j.williams@intel.com>
---
 test.h            |    2 +
 test/dax-dev.c    |    4 ++
 test/dax-pmd.c    |   99 +++++++++++++++++++++++++++++++++++++++++++++++++++--
 test/device-dax.c |    7 ++++
 4 files changed, 108 insertions(+), 4 deletions(-)
diff mbox series

Patch

diff --git a/test.h b/test.h
index fa0c0cff9daf..3f6212e067fc 100644
--- a/test.h
+++ b/test.h
@@ -38,6 +38,8 @@  struct ndctl_ctx;
 int test_parent_uuid(int loglevel, struct ndctl_test *test, struct ndctl_ctx *ctx);
 int test_multi_pmem(int loglevel, struct ndctl_test *test, struct ndctl_ctx *ctx);
 int test_dax_directio(int dax_fd, unsigned long align, void *dax_addr, off_t offset);
+int test_dax_remap(struct ndctl_test *test, int dax_fd, unsigned long align, void *dax_addr,
+		off_t offset, bool fsdax);
 #ifdef ENABLE_POISON
 int test_dax_poison(struct ndctl_test *test, int dax_fd, unsigned long align,
 		void *dax_addr, off_t offset, bool fsdax);
diff --git a/test/dax-dev.c b/test/dax-dev.c
index 49ccaa334e31..ab6b35a67183 100644
--- a/test/dax-dev.c
+++ b/test/dax-dev.c
@@ -60,6 +60,10 @@  struct ndctl_namespace *ndctl_get_test_dev(struct ndctl_ctx *ctx)
 	if (!ndns)
 		goto out;
 
+	rc = ndctl_namespace_enable(ndns);
+	if (rc)
+		goto out;
+
 	mode = ndctl_namespace_get_mode(ndns);
 	if (mode >= 0 && mode != NDCTL_NS_MODE_MEMORY)
 		goto out;
diff --git a/test/dax-pmd.c b/test/dax-pmd.c
index 0c95b20707c2..df3219639a6d 100644
--- a/test/dax-pmd.c
+++ b/test/dax-pmd.c
@@ -27,6 +27,7 @@ 
 #include <test.h>
 #include <util/size.h>
 #include <linux/fiemap.h>
+#include <linux/version.h>
 
 #define NUM_EXTENTS 5
 #define fail() fprintf(stderr, "%s: failed at: %d (%s)\n", \
@@ -35,6 +36,92 @@ 
 	__func__, __LINE__, i, strerror(errno))
 #define TEST_FILE "test_dax_data"
 
+#define REGION_MEM_SIZE 4096*4
+#define REGION_PM_SIZE        4096*512
+#define REMAP_SIZE      4096
+
+static sigjmp_buf sj_env;
+
+static void sigbus(int sig, siginfo_t *siginfo, void *d)
+{
+	siglongjmp(sj_env, 1);
+}
+
+int test_dax_remap(struct ndctl_test *test, int dax_fd, unsigned long align, void *dax_addr,
+		off_t offset, bool fsdax)
+{
+	void *anon, *remap, *addr;
+	struct sigaction act;
+	int rc, val;
+
+	if ((fsdax || align == SZ_2M) && !ndctl_test_attempt(test, KERNEL_VERSION(5, 8, 0))) {
+		/* kernel's prior to 5.8 may crash on this test */
+		fprintf(stderr, "%s: SKIP mremap() test\n", __func__);
+		return 0;
+	}
+
+	anon = mmap(NULL, REGION_MEM_SIZE, PROT_READ|PROT_WRITE,
+			MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
+
+	addr = mmap(dax_addr, 2*align,
+			PROT_READ|PROT_WRITE, MAP_SHARED, dax_fd, offset);
+
+	fprintf(stderr, "%s: addr: %p size: %#lx\n", __func__, addr, 2*align);
+
+	if (addr == MAP_FAILED) {
+		rc = -errno;
+		faili(0);
+		return rc;
+	}
+
+	memset(anon, 'a', REGION_MEM_SIZE);
+	memset(addr, 'i', align*2);
+
+	remap = mremap(addr, REMAP_SIZE, REMAP_SIZE, MREMAP_MAYMOVE|MREMAP_FIXED, anon);
+
+	if (remap != anon) {
+		rc = -ENXIO;
+		perror("mremap");
+		faili(1);
+		return rc;
+	}
+
+	fprintf(stderr, "%s: addr: %p size: %#x\n", __func__, remap, REMAP_SIZE);
+
+	memset(&act, 0, sizeof(act));
+	act.sa_sigaction = sigbus;
+	act.sa_flags = SA_SIGINFO;
+	if (sigaction(SIGBUS, &act, 0)) {
+		perror("sigaction");
+		rc = EXIT_FAILURE;
+		goto out;
+	}
+
+	/* test fault after device-dax instance disabled */
+	if (sigsetjmp(sj_env, 1)) {
+		if (!fsdax && align > SZ_4K) {
+			fprintf(stderr, "got expected SIGBUS after mremap() of device-dax\n");
+			rc = 0;
+		} else {
+			fprintf(stderr, "unpexpected SIGBUS after mremap()\n");
+			rc = -EIO;
+		}
+		goto out;
+	}
+
+	*(int *) anon = 0xAA;
+	val = *(int *) anon;
+
+	if (val != 0xAA) {
+		faili(2);
+		return -ENXIO;
+	}
+
+	rc = 0;
+out:
+	return rc;
+}
+
 int test_dax_directio(int dax_fd, unsigned long align, void *dax_addr, off_t offset)
 {
 	int i, rc = -ENXIO;
@@ -254,15 +341,19 @@  static int test_pmd(struct ndctl_test *test, int fd)
 
 	pmd_addr = (char *) base + m_align;
 	pmd_off =  ext->fe_logical + p_align;
+	rc = test_dax_remap(test, fd, HPAGE_SIZE, pmd_addr, pmd_off, fsdax);
+	if (rc)
+		goto err_test;
+
 	rc = test_dax_directio(fd, HPAGE_SIZE, pmd_addr, pmd_off);
 	if (rc)
-		goto err_directio;
+		goto err_test;
 
 	rc = test_dax_poison(test, fd, HPAGE_SIZE, pmd_addr, pmd_off, fsdax);
 
- err_directio:
- err_extent:
- err_mmap:
+err_test:
+err_extent:
+err_mmap:
 	free(map);
 	return rc;
 }
diff --git a/test/device-dax.c b/test/device-dax.c
index b19c1ed0b535..9de10682e34d 100644
--- a/test/device-dax.c
+++ b/test/device-dax.c
@@ -268,6 +268,13 @@  static int __test_device_dax(unsigned long align, int loglevel,
 					ndctl_namespace_get_devname(ndns));
 			goto out;
 		}
+
+		rc = test_dax_remap(test, fd, align, NULL, 0, devdax);
+		if (rc) {
+			fprintf(stderr, "%s: failed dax remap\n",
+					ndctl_namespace_get_devname(ndns));
+			goto out;
+		}
 		close(fd);
 
 		fprintf(stderr, "%s: test dax poison\n",