mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Sourav Panda <souravpanda@google.com>
To: djbw@kernel.org, vishal.l.verma@intel.com, dave.jiang@intel.com,
	 alison.schofield@intel.com, pankaj.gupta.linux@gmail.com,
	mst@redhat.com,  jasowangio@gmail.com, eperezma@redhat.com,
	viro@zeniv.linux.org.uk,  brauner@kernel.org
Cc: iweiny@kernel.org, jack@suse.cz, xuanzhuo@linux.alibaba.com,
	 shuah@kernel.org, david@kernel.org, souravpanda@google.com,
	 nvdimm@lists.linux.dev, linux-cxl@vger.kernel.org,
	 virtualization@lists.linux.dev, linux-fsdevel@vger.kernel.org,
	 linux-kselftest@vger.kernel.org, linux-mm@kvack.org,
	 linux-kernel@vger.kernel.org
Subject: [RFC PATCH 5/5] selftests/dax: add devdax_discard tests
Date: Mon, 30 Nov 2026 01:23:54 +0000	[thread overview]
Message-ID: <20261130012354.1894611-6-souravpanda@google.com> (raw)
In-Reply-To: <20261130012354.1894611-1-souravpanda@google.com>

Add a kselftest for hole punching on device-dax.  It needs a device
whose provider supports discard and skips otherwise.  DAXDEV selects the
device and defaults to /dev/dax0.0.

The test checks that bad arguments are rejected (unaligned or
out-of-range offsets and lengths, and modes other than PUNCH_HOLE |
KEEP_SIZE), that mmap() refuses an offset that is not aligned to the
device, that fallocate() and madvise(MADV_REMOVE) zero exactly the
requested range and leave its neighbours alone, and that data survives
close() and munmap().

Signed-off-by: Sourav Panda <souravpanda@google.com>
---
 tools/testing/selftests/dax/.gitignore       |   2 +
 tools/testing/selftests/dax/Makefile         |   3 +-
 tools/testing/selftests/dax/devdax_discard.c | 250 +++++++++++++++++++
 3 files changed, 254 insertions(+), 1 deletion(-)
 create mode 100644 tools/testing/selftests/dax/.gitignore
 create mode 100644 tools/testing/selftests/dax/devdax_discard.c

diff --git a/tools/testing/selftests/dax/.gitignore b/tools/testing/selftests/dax/.gitignore
new file mode 100644
index 0000000000000..33e329fbff967
--- /dev/null
+++ b/tools/testing/selftests/dax/.gitignore
@@ -0,0 +1,2 @@
+# SPDX-License-Identifier: GPL-2.0-only
+devdax_discard
diff --git a/tools/testing/selftests/dax/Makefile b/tools/testing/selftests/dax/Makefile
index 25a4f3d73a5ba..e218839319319 100644
--- a/tools/testing/selftests/dax/Makefile
+++ b/tools/testing/selftests/dax/Makefile
@@ -1,6 +1,7 @@
 # SPDX-License-Identifier: GPL-2.0
-all:
+CFLAGS += -Wall -O2 $(KHDR_INCLUDES)
 
 TEST_PROGS := dax-kmem-hotplug.sh
+TEST_GEN_PROGS := devdax_discard
 
 include ../lib.mk
diff --git a/tools/testing/selftests/dax/devdax_discard.c b/tools/testing/selftests/dax/devdax_discard.c
new file mode 100644
index 0000000000000..4fae31b186d8d
--- /dev/null
+++ b/tools/testing/selftests/dax/devdax_discard.c
@@ -0,0 +1,250 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Selftests for device-dax fallocate() discard (drivers/dax/device.c).
+ *
+ * Requires a discard-capable device-dax instance (for example, a virtio-pmem
+ * backed device-dax instance with VIRTIO_PMEM_F_DISCARD negotiated).  Select
+ * the device via DAXDEV=/dev/daxX.Y (default /dev/dax0.0); the tests skip
+ * when the device is absent or reports discard_granularity == 0.
+ *
+ * Semantics under test:
+ *   fallocate(fd, PUNCH_HOLE|KEEP_SIZE, off, len)  discard
+ *   madvise(MADV_REMOVE)                           discard through a mapping
+ *   close() / munmap()                             preserves data (PMEM)
+ */
+#define _GNU_SOURCE
+#include <errno.h>
+#include <fcntl.h>
+#include <linux/falloc.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/mman.h>
+#include <sys/stat.h>
+#include <sys/sysmacros.h>
+#include <unistd.h>
+
+#include "../kselftest_harness.h"
+
+#define DEFAULT_DAXDEV	"/dev/dax0.0"
+#define DISCARD		(FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE)
+
+static const char *daxdev(void)
+{
+	const char *p = getenv("DAXDEV");
+
+	return p && *p ? p : DEFAULT_DAXDEV;
+}
+
+static int sysfs_read(int fd, const char *attr, char *buf, size_t len)
+{
+	struct stat st;
+	char path[256];
+	ssize_t n;
+	int sfd;
+
+	if (fstat(fd, &st) || !S_ISCHR(st.st_mode))
+		return -1;
+	snprintf(path, sizeof(path), "/sys/dev/char/%u:%u/%s",
+		 major(st.st_rdev), minor(st.st_rdev), attr);
+	sfd = open(path, O_RDONLY);
+	if (sfd < 0)
+		return -1;
+	n = read(sfd, buf, len - 1);
+	close(sfd);
+	if (n < 0)
+		return -1;
+	buf[n] = '\0';
+	return 0;
+}
+
+static long long sysfs_num(int fd, const char *attr)
+{
+	char buf[64];
+
+	if (sysfs_read(fd, attr, buf, sizeof(buf)))
+		return -1;
+	return strtoll(buf, NULL, 0);
+}
+
+static int open_dev(void)
+{
+	return open(daxdev(), O_RDWR | O_CLOEXEC);
+}
+
+static int all_bytes(const unsigned char *p, size_t len, unsigned char v)
+{
+	size_t page = (size_t)sysconf(_SC_PAGESIZE);
+	size_t i;
+
+	for (i = 0; i < len; i += page)
+		if (p[i] != v || p[i + page - 1] != v)
+			return 0;
+	return 1;
+}
+
+FIXTURE(dax)
+{
+	int fd;
+	size_t size;		/* device size */
+	size_t align;		/* device mapping alignment */
+	size_t granularity;	/* provider discard granularity */
+	size_t region;		/* test window: >= align and >= 4 * granularity */
+};
+
+FIXTURE_SETUP(dax)
+{
+	long long gran, sz, al;
+
+	self->fd = open_dev();
+	if (self->fd < 0)
+		SKIP(return, "cannot open %s: %s", daxdev(), strerror(errno));
+	gran = sysfs_num(self->fd, "discard_granularity");
+	if (gran <= 0)
+		SKIP(return, "%s: discard_granularity <= 0", daxdev());
+	sz = sysfs_num(self->fd, "size");
+	al = sysfs_num(self->fd, "align");
+	ASSERT_GT(sz, 0LL);
+	ASSERT_GT(al, 0LL);
+	self->granularity = (size_t)gran;
+	self->size = (size_t)sz;
+	self->align = (size_t)al;
+	self->region = self->align > 4 * self->granularity ?
+		       self->align : 4 * self->granularity;
+	if (self->region % self->align)
+		self->region = ((self->region + self->align - 1) /
+				self->align) * self->align;
+	ASSERT_LE(self->region, self->size);
+}
+
+FIXTURE_TEARDOWN(dax)
+{
+	if (self->fd >= 0) {
+		close(self->fd);
+		self->fd = -1;
+	}
+}
+
+TEST_F(dax, invalid_arguments)
+{
+	size_t g = self->granularity;
+	size_t page = (size_t)sysconf(_SC_PAGESIZE);
+
+	EXPECT_EQ(fallocate(self->fd, 0, 0, g), -1);
+	EXPECT_EQ(errno, EOPNOTSUPP);
+	if (g > page) {
+		EXPECT_EQ(fallocate(self->fd, DISCARD, page, g), -1);
+		EXPECT_EQ(errno, EINVAL);
+		EXPECT_EQ(fallocate(self->fd, DISCARD, 0, g + page), -1);
+		EXPECT_EQ(errno, EINVAL);
+	}
+	EXPECT_EQ(fallocate(self->fd, DISCARD, self->size, g), -1);
+	EXPECT_EQ(errno, EINVAL);
+	EXPECT_EQ(fallocate(self->fd, DISCARD, self->size - g, 2 * g), -1);
+	EXPECT_EQ(errno, EINVAL);
+	EXPECT_EQ(fallocate(self->fd, FALLOC_FL_ZERO_RANGE, 0, g), -1);
+	EXPECT_EQ(errno, EOPNOTSUPP);
+	EXPECT_EQ(fallocate(self->fd, FALLOC_FL_PUNCH_HOLE, 0, g), -1);
+	EXPECT_EQ(errno, EOPNOTSUPP);
+}
+
+TEST_F(dax, mmap_rejects_unaligned_offset)
+{
+	size_t page = (size_t)sysconf(_SC_PAGESIZE);
+	void *res, *hint, *p;
+
+	if (self->align <= page || self->size < self->align + self->granularity)
+		SKIP(return, "device align/size too small for unaligned offset test");
+
+	/* Obtain an align-aligned VA hint without relying on kernel placement. */
+	res = mmap(NULL, 2 * self->align, PROT_NONE,
+		   MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
+	ASSERT_NE(res, MAP_FAILED);
+	hint = (void *)(((uintptr_t)res + self->align - 1) &
+			~(self->align - 1));
+	ASSERT_EQ(munmap(res, 2 * self->align), 0);
+
+	/* Page-aligned but sub-align offset must fail even with aligned VA. */
+	p = mmap(hint, self->align, PROT_READ | PROT_WRITE, MAP_SHARED,
+		 self->fd, page);
+	EXPECT_EQ(p, MAP_FAILED);
+	EXPECT_EQ(errno, EINVAL);
+	if (p != MAP_FAILED)
+		munmap(p, self->align);
+
+	/*
+	 * On align > granularity (e.g. 1 GiB align with 2 MiB discard),
+	 * a granularity-aligned offset is still unaligned for the device's
+	 * compound folio size and must fail.
+	 */
+	if (self->align > self->granularity) {
+		p = mmap(hint, self->align, PROT_READ | PROT_WRITE, MAP_SHARED,
+			 self->fd, self->granularity);
+		EXPECT_EQ(p, MAP_FAILED);
+		EXPECT_EQ(errno, EINVAL);
+		if (p != MAP_FAILED)
+			munmap(p, self->align);
+	}
+}
+
+TEST_F(dax, discard_reads_zero)
+{
+	size_t g = self->granularity;
+	unsigned char *p;
+
+	p = mmap(NULL, self->region, PROT_READ | PROT_WRITE, MAP_SHARED,
+		 self->fd, 0);
+	ASSERT_NE(p, MAP_FAILED);
+
+	memset(p, 0xa5, 3 * g);
+	ASSERT_TRUE(all_bytes(p, 3 * g, 0xa5));
+
+	/* explicit discard of the first granularity unit */
+	ASSERT_EQ(fallocate(self->fd, DISCARD, 0, g), 0);
+	EXPECT_TRUE(all_bytes(p, g, 0));
+	EXPECT_TRUE(all_bytes(p + g, 2 * g, 0xa5));
+
+	/* madvise(MADV_REMOVE) of the second unit goes through the same path */
+	ASSERT_EQ(madvise(p + g, g, MADV_REMOVE), 0);
+	EXPECT_TRUE(all_bytes(p + g, g, 0));
+	EXPECT_TRUE(all_bytes(p + 2 * g, g, 0xa5));
+
+	/* the discarded range is still usable afterwards */
+	memset(p, 0x3c, g);
+	EXPECT_TRUE(all_bytes(p, g, 0x3c));
+
+	EXPECT_EQ(munmap(p, self->region), 0);
+}
+
+TEST_F(dax, close_preserves_data)
+{
+	size_t g = self->granularity;
+	unsigned char *p;
+	int fd2;
+
+	p = mmap(NULL, self->region, PROT_READ | PROT_WRITE, MAP_SHARED,
+		 self->fd, 0);
+	ASSERT_NE(p, MAP_FAILED);
+	memset(p + 2 * g, 0xee, g);
+
+	ASSERT_EQ(munmap(p, self->region), 0);
+	ASSERT_EQ(close(self->fd), 0);
+	self->fd = -1;
+
+	/* Reopening must observe previously written data until discarded. */
+	fd2 = open_dev();
+	ASSERT_GE(fd2, 0);
+	p = mmap(NULL, self->region, PROT_READ | PROT_WRITE, MAP_SHARED, fd2,
+		 0);
+	ASSERT_NE(p, MAP_FAILED);
+	EXPECT_TRUE(all_bytes(p + 2 * g, g, 0xee));
+
+	/* Clean up with an explicit discard. */
+	EXPECT_EQ(fallocate(fd2, DISCARD, 2 * g, g), 0);
+	EXPECT_TRUE(all_bytes(p + 2 * g, g, 0));
+	EXPECT_EQ(munmap(p, self->region), 0);
+	EXPECT_EQ(close(fd2), 0);
+}
+
+TEST_HARNESS_MAIN
-- 
2.56.1.839.g22a8290db9-goog


      parent reply	other threads:[~2026-11-30  1:24 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-30  1:23 [RFC PATCH 0/5] virtio-pmem & device-dax: host-backed range discard via fallocate() / MADV_REMOVE Sourav Panda
2026-11-30  1:23 ` [RFC PATCH 1/5] dax/device: validate mmap offset alignment against dev_dax->align Sourav Panda
2026-11-30  1:23 ` [RFC PATCH 2/5] virtio_pmem: add VIRTIO_PMEM_F_DISCARD and region discard hook Sourav Panda
2026-11-30  1:23 ` [RFC PATCH 3/5] vfs: allow fallocate() on character devices implementing ->fallocate() Sourav Panda
2026-11-30  1:23 ` [RFC PATCH 4/5] dax/device: add fallocate() discard support Sourav Panda
2026-11-30  1:23 ` Sourav Panda [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261130012354.1894611-6-souravpanda@google.com \
    --to=souravpanda@google.com \
    --cc=alison.schofield@intel.com \
    --cc=brauner@kernel.org \
    --cc=dave.jiang@intel.com \
    --cc=david@kernel.org \
    --cc=djbw@kernel.org \
    --cc=eperezma@redhat.com \
    --cc=iweiny@kernel.org \
    --cc=jack@suse.cz \
    --cc=jasowangio@gmail.com \
    --cc=linux-cxl@vger.kernel.org \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=mst@redhat.com \
    --cc=nvdimm@lists.linux.dev \
    --cc=pankaj.gupta.linux@gmail.com \
    --cc=shuah@kernel.org \
    --cc=viro@zeniv.linux.org.uk \
    --cc=virtualization@lists.linux.dev \
    --cc=vishal.l.verma@intel.com \
    --cc=xuanzhuo@linux.alibaba.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®