virtio-pmem & device-dax: host-backed range discard via fallocate() / MADV_REMOVE
Subsystems (7)
cover virtio-pmem & device-dax: host-backed range discard via fallocate() / MADV_REMOVE
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
Signature · 1 line
2.56.1.839.g22a8290db9-goog
RFC PATCH 1/5 dax/device: validate mmap offset alignment against dev_dax->align
__check_vma() requires the start and end of a device-dax mapping to be aligned to dev_dax->align, but it never looks at the file offset. A caller that supplies an aligned address, either as a hint that happens to be honoured or with MAP_FIXED, together with an offset that is only page aligned gets a mapping. Its PMD and PUD faults then resolve to a physical address that is not aligned to the fault size.
Reject offsets that are not a multiple of dev_dax->align at mmap() time. Also make the PMD and PUD fault handlers refuse to install an entry for an unaligned physical address, as a backstop in case an aligned offset still translates to one.
--- drivers/dax/device.c | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-)
drivers/dax/device.c +13 -7
@@ -14,8 +14,8 @@#include "bus.h" static int __check_vma (struct dev_dax *dev_dax,vma_flags_t flags, -unsigned long start,unsigned long end,struct file *file, -const char *func) +unsigned long start,unsigned long end,pgoff_t pgoff, +struct file *file,const char *func) {struct device *dev = &dev_dax->dev;unsigned long mask;
@@ -39,6 +39,12 @@ static int __check_vma(struct dev_dax *dev_dax, vma_flags_t flags, mask);return -EINVAL ; } +if (pgoff & (mask >>PAGE_SHIFT )) { +dev_info_ratelimited (dev, +"%s: %s: fail, unaligned offset (%#lx, %#lx)\n" , + current->comm, func, pgoff, mask >>PAGE_SHIFT ); +return -EINVAL ; + }if (!file_is_dax (file)) { dev_info_ratelimited(dev,
@@ -54,7 +60,7 @@ static int check_vma(struct dev_dax *dev_dax, struct vm_area_struct *vma, const char *func) {return __check_vma (dev_dax, vma->flags, vma->vm_start, vma->vm_end, - vma->vm_file, func); + vma->vm_pgoff, vma->vm_file, func); } static void dax_set_mapping(struct vm_fault *vmf, unsigned long pfn,
@@ -148,7 +154,7 @@ static vm_fault_t __dev_dax_pmd_fault(struct dev_dax *dev_dax, pgoff =linear_page_index (vmf->vma, pmd_addr); phys =dax_pgoff_to_phys (dev_dax, pgoff,PMD_SIZE ); -if (phys ==-1 ) { +if (phys ==-1 || !IS_ALIGNED (phys, fault_size)) {dev_dbg (dev,"pgoff_to_phys(%#lx) failed\n" , pgoff);return VM_FAULT_SIGBUS ; }
@@ -194,7 +200,7 @@ static vm_fault_t __dev_dax_pud_fault(struct dev_dax *dev_dax, pgoff =linear_page_index (vmf->vma, pud_addr); phys =dax_pgoff_to_phys (dev_dax, pgoff,PUD_SIZE ); -if (phys ==-1 ) { +if (phys ==-1 || !IS_ALIGNED (phys, fault_size)) {dev_dbg (dev,"pgoff_to_phys(%#lx) failed\n" , pgoff);return VM_FAULT_SIGBUS ; }
@@ -283,8 +289,8 @@ static int dax_mmap_prepare(struct vm_area_desc *desc)* fault time. */ id =dax_read_lock (); - rc =__check_vma (dev_dax, desc->vma_flags, desc->start, desc->end, filp, - __func__); + rc =__check_vma (dev_dax, desc->vma_flags, desc->start, desc->end, + desc->pgoff, filp, __func__);dax_read_unlock (id);if (rc)return rc;
Signature · 1 line
2.56.1.839.g22a8290db9-goog
RFC PATCH 2/5 virtio_pmem: add VIRTIO_PMEM_F_DISCARD and region discard hook
In reply to Sourav PandaA virtio-pmem guest can ask the host to flush the region, but it cannot tell the host that part of the region is no longer needed. The host has to preserve every page the guest ever touched for as long as the device exists.
Add a discard request, negotiated with a new feature bit VIRTIO_PMEM_F_DISCARD. The device advertises two config fields:discard_granularity, the alignment in bytes that offsets and lengths must respect, and max_discard_extents, the number of extents it accepts per request. A request carries a count followed by {offset, len} extents relative to the start of the region. The device validates every extent before it releases anything and completes the request once all of them are released, after which the guest reads zeroes from those ranges. The status word, so far only zero or non-zero, gets named values so the device can tell an invalid request from an I/O error.
The driver does not trust the advertised values. A granularity that is not a power of two or is smaller than PAGE_SIZE, or an extent count of zero, turns the feature off, both when features are validated and when the config is read again in probe. A status the driver does not know is treated as -EIO.
Flush and discard now share one submission path, virtio_pmem_submit(), and remain serialized by flush_lock. A call with more ranges than the device accepts is split into several requests, so a failure part way through leaves the earlier requests applied.
So that device-dax does not need to know about virtio, the request is exposed as an optional ->discard() callback and a granularity on the nd_region, reached through nvdimm_discard() and nvdimm_discard_granularity(). Device-dax starts using them in a later patch, and pmem.c could use the same hook for REQ_OP_DISCARD.
--- 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 +- include/linux/libnvdimm.h | 20 ++++ include/uapi/linux/virtio_pmem.h | 49 ++++++++- 7 files changed, 330 insertions(+), 27 deletions(-)
drivers/nvdimm/nd.h +3 -0
@@ -421,6 +421,9 @@ struct nd_region {struct nd_interleave_set *nd_set;struct nd_lane *lane;int (*flush)(struct nd_region *nd_region,struct bio *bio); +int (*discard)(struct nd_region *nd_region,const struct range *ranges, +unsigned int nr); +u64 discard_granularity; struct nd_mapping mapping[] __counted_by(ndr_mappings); };
drivers/nvdimm/nd_virtio.c +145 -25
232 lines not on this page · raw message
drivers/nvdimm/region_devs.c +61 -0
73 lines not on this page · raw message
drivers/nvdimm/virtio_pmem.c +44 -0
62 lines not on this page · raw message
drivers/nvdimm/virtio_pmem.h +9 -1
@@ -43,7 +43,7 @@ struct virtio_pmem {/* Virtio pmem request queue */ struct virtqueue *req_vq; -/* Serialize flush requests to the device. */ +/* Serialize requests to the device. */ struct mutex flush_lock;/* Complete asynchronous FUA flushes outside the submit path. */
@@ -68,10 +68,18 @@ struct virtio_pmem {/* Memory region information */ __u64 start;__u64 size; + +/* Discard parameters negotiated via VIRTIO_PMEM_F_DISCARD */ +u64 discard_granularity; + u32 max_discard_extents; }; +#define VIRTIO_PMEM_DISCARD_MAX_EXTENTS ND_DISCARD_MAX_RANGES +void virtio_pmem_host_ack (struct virtqueue *vq);void virtio_pmem_mark_broken (struct virtio_pmem *vpmem);void virtio_pmem_drain (struct virtio_pmem *vpmem);int async_pmem_flush (struct nd_region *nd_region,struct bio *bio); +int virtio_pmem_discard (struct nd_region *nd_region,const struct range *ranges, +unsigned int nr);#endif
include/linux/libnvdimm.h +20 -0
@@ -124,6 +124,7 @@ struct nd_mapping_desc { };struct bio ; +struct range ;struct resource ;struct nd_region ;
@@ -149,6 +150,15 @@ struct nd_region_desc {int memregion;struct device_node *of_node;int (*flush)(struct nd_region *nd_region,struct bio *bio); +/* +* Optional: return the backing store of @nr physical ranges to the +* provider (e.g. a virtio-pmem host punches holes in the backing +* file). Subsequent reads of the ranges return zeroes. +* @discard_granularity must be a power-of-2 byte alignment >= PAGE_SIZE. +*/ +int (*discard)(struct nd_region *nd_region,const struct range *ranges, +unsigned int nr); +u64 discard_granularity; };struct device ;
@@ -310,6 +320,16 @@ u64 nd_fletcher64(void *addr, size_t len, bool le);int nvdimm_flush (struct nd_region *nd_region,struct bio *bio);int generic_nvdimm_flush (struct nd_region *nd_region);int nvdimm_has_flush (struct nd_region *nd_region); +/* Maximum number of ranges accepted by one nvdimm_discard() call */ +#define ND_DISCARD_MAX_RANGES 64 +static inline bool nvdimm_discard_granularity_valid (u64 gran) +{ +return gran >=PAGE_SIZE && gran <=S64_MAX && !(gran & (gran -1 )); +} + +u64 nvdimm_discard_granularity (struct nd_region *nd_region); +int nvdimm_discard (struct nd_region *nd_region,const struct range *ranges, +unsigned int nr);int nvdimm_has_cache (struct nd_region *nd_region);int nvdimm_in_overwrite (struct nvdimm *nvdimm);bool is_nvdimm_sync (struct nd_region *nd_region);
include/uapi/linux/virtio_pmem.h +48 -1
70 lines not on this page · raw message
Signature · 1 line
2.56.1.839.g22a8290db9-goog
RFC PATCH 3/5 vfs: allow fallocate() on character devices implementing ->fallocate()
In reply to Sourav Pandavfs_fallocate() returns -ENODEV for anything that is not a regular file or a block device, before it looks at f_op->fallocate. Let a character device through when its driver implements ->fallocate(). All other character devices keep returning -ENODEV, and since no character device in the tree implements ->fallocate() today, nothing changes for existing drivers.
The first user is device-dax, in a later patch, where punching a hole releases device memory. An ioctl could do that too, but madvise(MADV_REMOVE) is implemented by calling vfs_fallocate() with FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE on the mapped file. Going through ->fallocate() lets an allocator release part of a device-dax mapping with the madvise() call it already uses for shmem.
The permission and sanity checks in vfs_fallocate() are unchanged and still run before the driver is called.
--- fs/open.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-)
fs/open.c +7 -1
@@ -321,7 +321,13 @@ int vfs_fallocate(struct file *file, int mode, loff_t offset, loff_t len)if (S_ISDIR (inode->i_mode))return -EISDIR ; -if (!S_ISREG (inode->i_mode) && !S_ISBLK (inode->i_mode)) +/* +* Character devices that implement ->fallocate() (device-dax uses it +* for discard, see drivers/dax/device.c) are allowed through; all other +* character devices keep -ENODEV. +*/ +if (!S_ISREG (inode->i_mode) && !S_ISBLK (inode->i_mode) && + !(S_ISCHR (inode->i_mode) && file->f_op->fallocate))return -ENODEV ;/* Check for wraparound */
Signature · 1 line
2.56.1.839.g22a8290db9-goog
Jan Kara
reviewerRe: [RFC PATCH 3/5] vfs: allow fallocate() on character devices implementing ->fallocate()
Quoted 28 lines · Sourav Panda
vfs_fallocate()returns-ENODEVfor anything that is not a regular file or a block device, before it looks atf_op->fallocate. Let a character device through when its driver implements ->fallocate(). All other character devices keep returning-ENODEV, and since no character device in the tree implements ->fallocate() today, nothing changes for existing drivers.The first user is device-dax, in a later patch, where punching a hole releases device memory. An ioctl could do that too, but
madvise(MADV_REMOVE)is implemented by callingvfs_fallocate()withFALLOC_FL_PUNCH_HOLE|FALLOC_FL_KEEP_SIZEon the mapped file. Going through ->fallocate() lets an allocator release part of a device-dax mapping with themadvise()call it already uses for shmem.The permission and sanity checks in
vfs_fallocate()are unchanged and still run before the driver is called.Signed-off-by Sourav Panda <[email protected]>--- fs/open.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-)fs/open.c +0
-0@@ -321,7 +321,13 @@ int vfs_fallocate(struct file *file, int mode, loff_t offset, loff_t len)if (S_ISDIR (inode->i_mode))
fs/open.c int vfs_fallocate(struct file *file, int mode, loff_t offset, loff_t len) · line 330
Quoted 10 lines
fs/open.c +7
-1return -EISDIR ; -if (!S_ISREG (inode->i_mode) && !S_ISBLK (inode->i_mode)) +/* +* Character devices that implement ->fallocate() (device-dax uses it +* for discard, see drivers/dax/device.c) are allowed through; all other +* character devices keep -ENODEV. +*/ +if (!S_ISREG (inode->i_mode) && !S_ISBLK (inode->i_mode) && + !(S_ISCHR (inode->i_mode) && file->f_op->fallocate))
I think you can just add S_ISCHR() check here. If ->fallocate isn't set, we'll return EOPNOTSUPP a few conditions below. Yes, the error would change from ENODEV to EOPNOTSUPP but I don't think that really matters and if we're going to support fallocate() for char devices, then better keep it consistent with e.g. block devices.
Honza
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR
RFC PATCH 4/5 dax/device: add fallocate() discard support
In reply to Sourav PandaMemory handed out through a device-dax instance cannot be given back. A process can stop using part of the device, but nothing tells the provider, so a host backing the device for a guest keeps every page the guest has touched.
Implement ->fallocate() for FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE. When the dax_region has a discard callback, punching a hole releases the backing of the range and it reads back as zeroes. dax_pmem installs the callback if the nd_region provides one, which virtio-pmem does once VIRTIO_PMEM_F_DISCARD is negotiated. madvise(MADV_REMOVE) ends up in the same place.
Offset and length must be multiples of the provider's granularity, which has nothing to do with dev_dax->align. It is exported in a new read-only discard_granularity attribute; 0 means the device cannot discard, and fallocate() then fails with -EOPNOTSUPP.
Before the provider is called the range has to be unreachable:
- Mappings are removed with unmap_mapping_range() over the range rounded out to dev_dax->align. Device-dax only installs entries of that size, so a 2M discard inside a 1G mapping zaps the 1G entry and the next access faults it back in at 1G. Nothing is split.
- Faults must not repopulate the range while it is being released. A per-device percpu rw-semaphore is held for write across the unmap and the discard, and dev_dax_huge_fault() takes it for read. The mapping's invalidate_lock would work as well, but with a shared rwsem in the fault path 16 processes faulting on one device lost up to 64% of their throughput to cache line contention in a fault-heavy microbenchmark. With the percpu lock they match an unpatched kernel. The cost moves to the write side: an isolated discard waits for an RCU grace period (about 4ms instead of 0.3ms in the same setup), while back-to-back discards do not. Devices that cannot discard do not take the lock at all.
- Pages can still be referenced without being mapped, for example when pinned for DMA or sitting in a pipe after vmsplice(). After the unmap an idle folio has a refcount of one, so a higher count fails the call with -EBUSY before any range is released.
A file opened under device_dax can outlive the driver binding, and by then another dax driver may have marked the dax_device alive again. dax_fallocate() therefore also checks that device_dax is still the bound driver and returns -ENODEV if it is not.
--- 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 | 191 +++++++++++++++++++++++- drivers/dax/pmem.c | 16 ++ 6 files changed, 301 insertions(+), 1 deletion(-)
Documentation/ABI/testing/sysfs-bus-dax +11 -0
@@ -175,3 +175,14 @@ Description: A device can only be onlined from the "unplugged" state, so a device must be returned to "unplugged" before it can be onlined into a different state. + +What: /sys/bus/dax/devices/daxX.Y/discard_granularity +Date: November, 2026 +KernelVersion: v7.4 +Contact: [email protected] +Description: + (RO) Reports the discard alignment granularity in bytes for + fallocate(FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE) and + madvise(MADV_REMOVE) on this dax device. Returns a power-of-2 + byte size (>= PAGE_SIZE) if the backing provider supports + discard, or "0" if discard is not supported.
drivers/dax/bus.c +40 -0
@@ -690,6 +690,27 @@ struct dax_region *alloc_dax_region(struct device *parent, int region_id, }EXPORT_SYMBOL_GPL (alloc_dax_region); +/** +* dax_region_set_discard - install a provider hook to return physical ranges +* @dax_region: region whose child devices gain fallocate() discard support +* @discard: callback releasing the backing store of physical ranges +* @granularity: power-of-2 byte alignment (>= PAGE_SIZE) for @discard +* +* Must be called before any dev_dax is created in @dax_region. +*/ +void dax_region_set_discard (struct dax_region *dax_region, +int (*discard)(struct dax_region *dax_region, +const struct range *ranges, +unsigned int nr), +u64 granularity) +{ +if (!discard || !dax_discard_granularity_valid (granularity)) +return ; + dax_region->discard = discard; + dax_region->discard_granularity = granularity; +} +EXPORT_SYMBOL_GPL (dax_region_set_discard); +static void dax_mapping_release (struct device *dev) {struct dax_mapping *mapping =to_dax_mapping (dev);
@@ -1377,6 +1398,16 @@ static ssize_t memmap_on_memory_store(struct device *dev, }static DEVICE_ATTR_RW (memmap_on_memory); +static ssize_t discard_granularity_show (struct device *dev, +struct device_attribute *attr, +char *buf) +{ +struct dev_dax *dev_dax =to_dev_dax (dev); + +return sysfs_emit (buf,"%llu\n" ,dev_dax_discard_granularity (dev_dax)); +} +static DEVICE_ATTR_RO (discard_granularity); +static umode_t dev_dax_visible (struct kobject *kobj,struct attribute *a,int n) { struct device *dev = container_of(kobj, struct device, kobj);
@@ -1404,6 +1435,7 @@ static struct attribute *dev_dax_attributes[] = { &dev_attr_resource.attr, &dev_attr_numa_node.attr, &dev_attr_memmap_on_memory.attr, + &dev_attr_discard_granularity.attr, NULL, };
@@ -1425,6 +1457,7 @@ static void dev_dax_release(struct device *dev)put_dax (dax_dev);free_dev_dax_id (dev_dax);kfree (dev_dax->pgmap); +percpu_free_rwsem (&dev_dax->discard_rwsem);kfree (dev_dax); }
@@ -1468,6 +1501,12 @@ static struct dev_dax *__devm_create_dev_dax(struct dev_dax_data *data)return ERR_PTR (-ENOMEM ); dev_dax->region = dax_region; +if (dax_region->discard) { + rc =percpu_init_rwsem (&dev_dax->discard_rwsem); +if (rc) + goto err_id; + dev_dax->discard_granularity = dax_region->discard_granularity; + }if (is_static (dax_region)) {if (dev_WARN_ONCE (parent, data->id <0 ,"dynamic id specified to static region\n" )) {
@@ -1564,6 +1603,7 @@ static struct dev_dax *__devm_create_dev_dax(struct dev_dax_data *data) err_range:free_dev_dax_id (dev_dax); err_id: +percpu_free_rwsem (&dev_dax->discard_rwsem);kfree (dev_dax);return ERR_PTR (rc);
drivers/dax/bus.h +5 -0
@@ -31,6 +31,11 @@ struct dev_dax_data { };struct dev_dax *devm_create_dev_dax (struct dev_dax_data *data); +void dax_region_set_discard (struct dax_region *dax_region, +int (*discard)(struct dax_region *dax_region, +const struct range *ranges, +unsigned int nr), +u64 granularity);enum dax_driver_type {DAXDRV_KMEM_TYPE ,
drivers/dax/dax-private.h +39 -0
@@ -8,6 +8,9 @@#include <linux/device.h> #include <linux/cdev.h> #include <linux/idr.h> +#include <linux/percpu-rwsem.h> +#include <linux/range.h> +#include <linux/sizes.h> /* private routines between core files */ struct dax_device ;
@@ -27,6 +30,9 @@ void dax_bus_exit(void);* @res: resource tree to track instance allocations * @seed: allow userspace to find the first unbound seed device * @youngest: allow userspace to find the most recently created device +* @discard: optional provider hook returning the backing store of a +* physical range (see dev_dax fallocate() support in device.c) +* @discard_granularity: power-of-2 byte alignment for @discard (0 if none) */ struct dax_region {int id;
@@ -38,6 +44,9 @@ struct dax_region {struct resource res;struct device *seed;struct device *youngest; +int (*discard)(struct dax_region *dax_region,const struct range *ranges, +unsigned int nr); +u64 discard_granularity; };/**
@@ -80,6 +89,13 @@ struct dev_dax_range {* @memmap_on_memory: allow kmem to put the memmap in the memory * @nr_range: size of @ranges * @ranges: range tuples of memory used +* @device_dax_bound: device_dax is the bound driver (see dev_dax_probe()) +* @discard_granularity: copy of the region's discard granularity, 0 if +* the region cannot discard; unlike @region it +* stays valid for as long as the dev_dax does +* @discard_rwsem: held for read by page faults and for write by +* dax_fallocate(); only initialized when +* @discard_granularity is non-zero */ struct dev_dax {struct dax_region *region;
@@ -95,6 +111,9 @@ struct dev_dax {bool memmap_on_memory;int nr_range;struct dev_dax_range *ranges; +bool device_dax_bound; +u64 discard_granularity; +struct percpu_rw_semaphore discard_rwsem; };/*
@@ -116,6 +135,26 @@ static inline struct dax_mapping *to_dax_mapping(struct device *dev)phys_addr_t dax_pgoff_to_phys (struct dev_dax *dev_dax,pgoff_t pgoff,unsigned long size); +static inline bool dax_discard_granularity_valid (u64 gran) +{ +return gran >=PAGE_SIZE && gran <=S64_MAX && !(gran & (gran -1 )); +} + +static inline u64 dev_dax_discard_granularity (struct dev_dax *dev_dax) +{ +return dev_dax->discard_granularity; +} + +static inline u64 dev_dax_bytes (struct dev_dax *dev_dax) +{ +u64 size =0 ; +int i; + +for (i =0 ; i < dev_dax->nr_range; i++) + size +=range_len (&dev_dax->ranges[i].range); +return size; +} +#ifdef CONFIG_TRANSPARENT_HUGEPAGE static inline bool dax_align_valid (unsigned long align) {
drivers/dax/device.c +190 -1
240 lines not on this page · raw message
drivers/dax/pmem.c +16 -0
@@ -4,8 +4,18 @@#include <linux/module.h> #include "../nvdimm/pfn.h" #include "../nvdimm/nd.h" +#include "dax-private.h" #include "bus.h" +/* Forward a dev_dax discard to the nd_region provider (e.g. virtio-pmem) */ +static int dax_pmem_discard (struct dax_region *dax_region, +const struct range *ranges,unsigned int nr) +{ +struct nd_region *nd_region =to_nd_region (dax_region->dev->parent); + +return nvdimm_discard (nd_region, ranges, nr); +} +static struct dev_dax *__dax_pmem_probe (struct device *dev) {struct range range;
@@ -20,6 +30,7 @@ static struct dev_dax *__dax_pmem_probe(struct device *dev)struct nd_dax *nd_dax =to_nd_dax (dev);struct nd_pfn *nd_pfn = &nd_dax->nd_pfn;struct nd_region *nd_region =to_nd_region (dev->parent); +u64 discard_gran; ndns =nvdimm_namespace_common_probe (dev);if (IS_ERR (ndns))
@@ -56,6 +67,11 @@ static struct dev_dax *__dax_pmem_probe(struct device *dev) IORESOURCE_DAX_STATIC);if (!dax_region)return ERR_PTR (-ENOMEM ); + discard_gran =nvdimm_discard_granularity (nd_region); +if (discard_gran && +IS_ALIGNED (range.start - nd_region->ndr_start, discard_gran)) +dax_region_set_discard (dax_region, dax_pmem_discard, + discard_gran); data = (struct dev_dax_data ) { .dax_region = dax_region,
Signature · 1 line
2.56.1.839.g22a8290db9-goog
RFC PATCH 5/5 selftests/dax: add devdax_discard tests
In reply to Sourav PandaAdd 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().
--- 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
tools/testing/selftests/dax/.gitignore new file +2 -0
@@ -0,0 +1,2 @@ +# SPDX-License-Identifier: GPL-2.0-only +devdax_discard
tools/testing/selftests/dax/Makefile +2 -1
@@ -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
tools/testing/selftests/dax/devdax_discard.c new file +250 -0
250 lines not on this page · raw message
Signature · 1 line
2.56.1.839.g22a8290db9-goog