[RFC PATCH 0/5] virtio-pmem & device-dax: host-backed range discard via fallocate() / MADV_REMOVE

From: Sourav Panda

Date: Sun Nov 29 2026 - 20:24:06 EST


Device-dax on top of virtio-pmem is a cheap way to give an application
in a guest a large heap. With 1G alignment the guest spends about 4K
of struct page per GiB instead of 16M, the mappings are PUD entries, and
the guest physical layout never changes. What it cannot do is give
memory back: fallocate() on /dev/daxX.Y fails with -ENODEV, so once the
application has touched a page the host backs it until the device goes
away.

This series lets userspace punch holes in a device-dax instance, with
fallocate(FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE) or
madvise(MADV_REMOVE), and passes them to the host through a new
virtio-pmem discard request. The host frees the backing, for example by
punching a hole in the backing file, and the range reads back as zeroes.

Guest
userspace fallocate(PUNCH_HOLE) / madvise(MADV_REMOVE)
|
v
vfs vfs_fallocate() [patch 3]
|
v
device-dax dax_fallocate() [patch 4]
| 1. block page faults on the device
| 2. zap the align-sized mappings covering the range
| 3. -EBUSY if any page is still referenced
v
libnvdimm nvdimm_discard() -> nd_region->discard() [patch 2]
|
v
virtio-pmem VIRTIO_PMEM_REQ_TYPE_DISCARD {offset, len} [patch 2]
=====|==============================================================
v
Host VMM punch a hole in the backing file

The granularity of a discard is whatever the host can free, 2M for a
hugetlbfs-backed region say, and has nothing to do with the size of the
guest mappings. Device-dax only has mappings of dev_dax->align, so a
discard zaps the entries that cover it and lets them fault back in at
full size. Guest mappings are never split:

guest mapping |<------------------ 1 GiB ----------------->|
host backing | 2M | 2M | 2M | .. | 2M | 2M | .. | 2M | 2M |
^^
released to the host; zero-filled on next touch

Patch 1 fixes an existing bug in the mmap() offset check. Patch 2 adds
the virtio-pmem request and a ->discard() hook on the nd_region, patch 3
lets a character device implement ->fallocate(), patch 4 is the
device-dax side and patch 5 a selftest.

Things I would like opinions on:

- The virtio side needs a spec change, which I will send separately.
The request guarantees zeroes on the next read, which virtio-blk
DISCARD does not, so "discard" may be the wrong name for it.

- virtio-mem solves a related problem, but it resizes kernel-managed
RAM at the host's request. Here the application owns the region and
decides which ranges to release, without hot(un)plug and without
giving up the 1G mappings.

- On devices that can discard, page faults take a per-device percpu
rwsem for read. Patch 4 has the numbers behind that choice.

- Nothing wipes a device when its last user exits. A supervisor that
hands a device to a new user is expected to punch out the whole range
first.

Sourav Panda (5):
dax/device: validate mmap offset alignment against dev_dax->align
virtio_pmem: add VIRTIO_PMEM_F_DISCARD and region discard hook
vfs: allow fallocate() on character devices implementing ->fallocate()
dax/device: add fallocate() discard support
selftests/dax: add devdax_discard tests

Documentation/ABI/testing/sysfs-bus-dax | 11 +
drivers/dax/bus.c | 40 +++
drivers/dax/bus.h | 5 +
drivers/dax/dax-private.h | 39 +++
drivers/dax/device.c | 211 +++++++++++++++-
drivers/dax/pmem.c | 16 ++
drivers/nvdimm/nd.h | 3 +
drivers/nvdimm/nd_virtio.c | 170 +++++++++++--
drivers/nvdimm/region_devs.c | 61 +++++
drivers/nvdimm/virtio_pmem.c | 44 ++++
drivers/nvdimm/virtio_pmem.h | 10 +-
fs/open.c | 8 +-
include/linux/libnvdimm.h | 20 ++
include/uapi/linux/virtio_pmem.h | 49 +++-
tools/testing/selftests/dax/.gitignore | 2 +
tools/testing/selftests/dax/Makefile | 3 +-
tools/testing/selftests/dax/devdax_discard.c | 250 +++++++++++++++++++
17 files changed, 905 insertions(+), 37 deletions(-)
create mode 100644 tools/testing/selftests/dax/.gitignore
create mode 100644 tools/testing/selftests/dax/devdax_discard.c


base-commit: ebb7a8dc1e74c705daffe11c2faf38ec2628f838
--
2.56.1.839.g22a8290db9-goog