Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.215.199 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796001841; b=qgxorei60zZ8LKnUdYTTTmhW0xBda28MQcsAJnIELjUMgfoeFW2th0qiDss6Dcohfgfp /bX0hWdGc2CyBfj6apgNKmDxe6FQt2zHFDfJskRLijWFRQVli7L7+93zoPmVBAJ4wKRNJ Kx2JF1+u3928teNpazLoV8DtoHmes5roxksgwj8S4u90/BzuyqU/91MmmkWX3olBkEk3z sEzLZilMSO5mpvbmKSG3v8Lu4II+lBVqbQPQC8Nq6e1M93zsdNv0k/KfgByaycPFsVGMi 7FzCSIrxigxUqN3+b5ZtZwwHMbgh5d+pS96Ix7Ag1s3PHlBDwLRuvBJcUESSKK50wtw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796001841; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Received:Date:In-Reply-To: Mime-Version:References:X-Mailer:Message-ID:Subject:From:To:Cc: Content-Type; bh=UyY1AM9892IhAhWQfVqvSe/9uzAtwaFRj7YWgb7LJwc=; b=mbxdUtubhgibhH0409pcTUlYk5puePYKCCtTr1sk4OpTzCHro6VsWiCmiEfCZ6E7JO+v 5IMJhvb11OrJTL+XWK8VSxtsvGn0OXdFe7CsBW9KCxQVIaAoeh5XL3Gik0oBcK0rcSETo oK+ygzCp1cssDR9cG6vn7F2V5fMOZP1AffDxj+AjYtwLKfhhjE7FB7Dhp+sv+ZplS7CND nm7rFSPJJ1DBSs5JP/JF8+MRT+k68FmlSuWrylNp1XSD6m/Blu74zjroB0DBRMNes4soi GKDbxEJGQNVJko47Bzq9NIJztyXnUMWkBwSlAelwXU+TfYetG6JwpdfBfj3/T3VlzUQ== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=google.com; spf=pass smtp.mailfrom=flex--souravpanda.bounces.google.com; dkim=pass header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20260707 header.b=mGLULtZe; arc=none smtp.remote-ip=209.85.215.199 Received: from mail-pg1-f199.google.com (mail-pg1-f199.google.com [209.85.215.199]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 8EEE060A2603 for ; Mon, 30 Nov 2026 01:24:00 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 8EEE060A2603 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--souravpanda.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20260707 header.b=mGLULtZe Received: by mail-pg1-f199.google.com with SMTP id 41be03b00d2f7-cd60e4e8bc3so6621926a12.1 for ; Sun, 29 Nov 2026 17:24:00 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20260707; t=1796001840; x=1796606640; darn=vger.kernel.org; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:from:to:cc:subject:date:message-id:reply-to :content-type; bh=UyY1AM9892IhAhWQfVqvSe/9uzAtwaFRj7YWgb7LJwc=; b=mGLULtZeQwkTrSGoEy0X/3gI3Blduuz0dv3eYIETu4SCxzAIOX7nKHlQFUMe96wchw iybQF4/0l2qYGzkb/L4Vy7LfCY9C5gG2hHsak0wbrsCHgSR01+dM1N/g9EJbns8+gerp RJe/ZszL5cDAamgFu/Lp1eze5N1IgdbYVc5cu1w0aECiJB2YfpDS+RZyCNy799OTKWTz KYvQHOuoo8YjVSzrZTphTgm3LWLLJKqa6o9Btd3/+uklfYhDD/MbDs23IrRHphyaFjR5 L7ILG70W9rT03zM4qK6hAM+Ag9S/aVbVp5z01QDmYMfuYwuzP3Sc7/3FK8leYq7MGJLi dxyg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796001840; x=1796606640; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=UyY1AM9892IhAhWQfVqvSe/9uzAtwaFRj7YWgb7LJwc=; b=qUasLQAvE1XXhwuib1xqbzrVRaLcm0RlRhP21BlN7X+B3eX00//0HqQJMsAvPD69qL EkS5XLQ+GJBtDrtUxVoym+L4cpq6PIu+2vtT2+N+HsoVnIE+GrHNM5zaE7bxe7JGCIab jsZh7qmRdZ6NJKqn5TZJ6AX/OnN3rz7ShwCVV+L5YeT1XNXLR7dI0+g2PEpGq8ahvsaY UNRXTVPA4TQOK1QDwA2c+LHptQysToNBtYnc7K1gVdjmkNVEy72+IO9z/Rbr1nxv/+Hu Rz9+YF9zaItVQ/pHIhmAXxrOC3tKoGDzd0zz6TQIvt7b/kPAed717fwarqKxszo7s/pr wk2w== X-Forwarded-Encrypted: i=1; AL+hErF+wq8urMp15uWslGmOwif4+Xuuh+lXFX+bH89ir/w8k3rUJHtQIIeZKDhZjMJ5T9KBC15BfAP8KkU=@vger.kernel.org X-Gm-Message-State: APuU4Z+CNohIqq1Lekn4DtPDBo2qU+yV6fRjwl3QsonwrIWdhPmtepMm qSj20nHc5qqG7K7CrGTbPb10K9EFZVREhzfa1d8B3LtwjC493UM9yemSHdXTuIFRyiziZRHSe+3 0ioIIb3Ggm77tBSn7kjFG39hoUg== X-Received: from plbju13.prod.google.com ([2002:a17:903:428d:b0:2f0:7fa6:7804]) (user=souravpanda job=prod-delivery.src-stubby-dispatcher) by 2002:a17:902:db06:b0:2eb:a1b:e9e5 with SMTP id d9443c01a7336-2f03f1e870fmr325815625ad.52.1796001839639; Sun, 29 Nov 2026 17:23:59 -0800 (PST) Date: Mon, 30 Nov 2026 01:23:53 +0000 In-Reply-To: <20261130012354.1894611-1-souravpanda@google.com> X-Mailing-List: linux-cxl@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list Mime-Version: 1.0 References: <20261130012354.1894611-1-souravpanda@google.com> X-Mailer: git-send-email 2.56.1.839.g22a8290db9-goog Message-ID: <20261130012354.1894611-5-souravpanda@google.com> Subject: [RFC PATCH 4/5] dax/device: add fallocate() discard support From: Sourav Panda 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 Content-Type: text/plain; charset="UTF-8" Memory 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. Signed-off-by: Sourav Panda --- 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(-) diff --git a/Documentation/ABI/testing/sysfs-bus-dax b/Documentation/ABI/testing/sysfs-bus-dax index 60703fffa99e0..4d6399e29ced9 100644 --- a/Documentation/ABI/testing/sysfs-bus-dax +++ b/Documentation/ABI/testing/sysfs-bus-dax @@ -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: nvdimm@lists.linux.dev +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. diff --git a/drivers/dax/bus.c b/drivers/dax/bus.c index e40c25401cf06..652541627dd78 100644 --- a/drivers/dax/bus.c +++ b/drivers/dax/bus.c @@ -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); diff --git a/drivers/dax/bus.h b/drivers/dax/bus.h index bfc31923f3ff4..aa6a607ebdf22 100644 --- a/drivers/dax/bus.h +++ b/drivers/dax/bus.h @@ -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, diff --git a/drivers/dax/dax-private.h b/drivers/dax/dax-private.h index 607a53a91f58b..32bdf644f9e22 100644 --- a/drivers/dax/dax-private.h +++ b/drivers/dax/dax-private.h @@ -8,6 +8,9 @@ #include #include #include +#include +#include +#include /* 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) { diff --git a/drivers/dax/device.c b/drivers/dax/device.c index 9c956085d9122..53192614bab3f 100644 --- a/drivers/dax/device.c +++ b/drivers/dax/device.c @@ -10,6 +10,7 @@ #include #include #include +#include #include "dax-private.h" #include "bus.h" @@ -226,11 +227,18 @@ static vm_fault_t dev_dax_huge_fault(struct vm_fault *vmf, unsigned int order) vm_fault_t rc = VM_FAULT_SIGBUS; int id; struct dev_dax *dev_dax = filp->private_data; + /* + * Only a discard-capable device has to exclude dax_fallocate(). The + * percpu rwsem keeps concurrent faults from sharing a cache line. + */ + bool discard = dev_dax_discard_granularity(dev_dax); dev_dbg(&dev_dax->dev, "%s: op=%s addr=%#lx order=%d\n", current->comm, (vmf->flags & FAULT_FLAG_WRITE) ? "write" : "read", vmf->address & ~((1UL << (order + PAGE_SHIFT)) - 1), order); + if (discard) + percpu_down_read(&dev_dax->discard_rwsem); id = dax_read_lock(); if (order == 0) rc = __dev_dax_pte_fault(dev_dax, vmf); @@ -242,6 +250,8 @@ static vm_fault_t dev_dax_huge_fault(struct vm_fault *vmf, unsigned int order) rc = VM_FAULT_SIGBUS; dax_read_unlock(id); + if (discard) + percpu_up_read(&dev_dax->discard_rwsem); return rc; } @@ -276,6 +286,180 @@ static const struct vm_operations_struct dax_vm_ops = { .pagesize = dev_dax_pagesize, }; +/* + * Discard support + * + * When a dev_dax's provider supports discard (see dax_region->discard): + * - fallocate(fd, FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE, offset, len) + * and madvise(MADV_REMOVE) release the backing store of the range so + * subsequent reads return zeroes. + * - Offsets and lengths must be multiples of dax_region->discard_granularity, + * independent of dev_dax->align. Devices without a discard provider reject + * fallocate() with -EOPNOTSUPP. + * - Serialization against concurrent faults and GUP/DMA pins uses + * dev_dax->discard_rwsem (write) + unmap_mapping_range() (aligned to + * dev_dax->align so huge mappings are zapped without splitting) followed by + * a folio_ref_count() > 1 check (-EBUSY if pinned or referenced). + */ + +#define DEV_DAX_DISCARD_BATCH 16 + +static bool dax_range_valid(struct dev_dax *dev_dax, loff_t offset, loff_t len, + u64 gran) +{ + u64 end; + + if (offset < 0 || len <= 0) + return false; + if (!IS_ALIGNED(offset, gran) || !IS_ALIGNED(len, gran)) + return false; + if (check_add_overflow((u64)offset, (u64)len, &end) || + end > dev_dax_bytes(dev_dax)) + return false; + return true; +} + +static bool dax_range_busy(const struct range *r) +{ + unsigned long pfn = PHYS_PFN(r->start); + unsigned long end_pfn = PHYS_PFN(r->end) + 1; + unsigned long iter = 0; + + /* + * After unmap_mapping_range() with discard_rwsem held for write, an + * unreferenced MEMORY_DEVICE_GENERIC folio has refcount == 1. Any + * higher refcount indicates an active FOLL_PIN (DMA/GUP) or FOLL_GET + * (e.g. vmsplice pipe) reference. A folio only partly covered by the + * range is conservatively checked as a whole. + */ + while (pfn < end_pfn) { + struct folio *folio = pfn_folio(pfn); + + if (folio_ref_count(folio) > 1) + return true; + pfn = folio_pfn(folio) + folio_nr_pages(folio); + if ((++iter & 1023) == 0) + cond_resched(); + } + return false; +} + +static bool dax_intersect_range(const struct dev_dax_range *dax_range, + u64 cur, u64 end, struct range *r, u64 *next) +{ + u64 rstart = (u64)dax_range->pgoff << PAGE_SHIFT; + u64 rend = rstart + range_len(&dax_range->range); + u64 cstart, cend; + + if (rend <= cur || rstart >= end) + return false; + cstart = max(cur, rstart); + cend = min(end, rend); + r->start = dax_range->range.start + (cstart - rstart); + r->end = dax_range->range.start + (cend - rstart) - 1; + *next = cend; + return true; +} + +/* + * Validate and discard device byte range [start, end); caller holds + * discard_rwsem for write and dax_read_lock(). Pre-validates alignment and + * DMA pin absence across every intersecting sub-range before issuing any + * batched discard to the provider. + */ +static int dax_discard_range(struct dev_dax *dev_dax, u64 start, u64 end, + u64 gran) +{ + struct dax_region *dax_region = dev_dax->region; + struct range batch[DEV_DAX_DISCARD_BATCH]; + unsigned int nr = 0; + u64 cur = start; + int i, rc; + + for (i = 0; i < dev_dax->nr_range && cur < end; i++) { + struct range r; + + if (!dax_intersect_range(&dev_dax->ranges[i], cur, end, &r, &cur)) + continue; + if (!IS_ALIGNED(r.start - dax_region->res.start, gran) || + !IS_ALIGNED(range_len(&r), gran)) + return -EINVAL; + if (dax_range_busy(&r)) + return -EBUSY; + } + if (cur < end) + return -EINVAL; + + cur = start; + for (i = 0; i < dev_dax->nr_range && cur < end; i++) { + if (!dax_intersect_range(&dev_dax->ranges[i], cur, end, + &batch[nr], &cur)) + continue; + if (++nr == ARRAY_SIZE(batch)) { + rc = dax_region->discard(dax_region, batch, nr); + if (rc) + return rc; + nr = 0; + } + } + if (nr) + return dax_region->discard(dax_region, batch, nr); + return 0; +} + +/* + * A file opened under device_dax can outlive the driver binding, and another + * dax driver (e.g. fsdev_dax) may mark the dax_device alive again after a + * rebind. Only discard while device_dax still owns the device. Caller holds + * dax_read_lock(): dev_dax_kill() clears device_dax_bound only after + * kill_dax() has waited for all readers. + */ +static bool dev_dax_discard_allowed(struct dev_dax *dev_dax) +{ + return dax_alive(dev_dax->dax_dev) && + READ_ONCE(dev_dax->device_dax_bound); +} + +static long dax_fallocate(struct file *filp, int mode, loff_t offset, + loff_t len) +{ + struct dev_dax *dev_dax = filp->private_data; + struct address_space *mapping = filp->f_mapping; + struct device *dev = &dev_dax->dev; + u64 start = offset, end, gran, unmap_start, unmap_end; + int rc, id; + + if (mode != (FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE)) + return -EOPNOTSUPP; + /* Fixed when the device is created; discard_rwsem exists only if set */ + gran = dev_dax_discard_granularity(dev_dax); + if (!gran) + return -EOPNOTSUPP; + + percpu_down_write(&dev_dax->discard_rwsem); + id = dax_read_lock(); + if (!dev_dax_discard_allowed(dev_dax)) { + rc = -ENODEV; + goto out; + } + if (!dax_range_valid(dev_dax, offset, len, gran)) { + dev_dbg(dev, "%s: %s: invalid range %#llx+%#llx\n", + current->comm, __func__, offset, len); + rc = -EINVAL; + goto out; + } + end = start + len; + unmap_start = round_down(start, dev_dax->align); + unmap_end = round_up(end, dev_dax->align); + unmap_mapping_range(mapping, unmap_start, unmap_end - unmap_start, 1); + + rc = dax_discard_range(dev_dax, start, end, gran); +out: + dax_read_unlock(id); + percpu_up_write(&dev_dax->discard_rwsem); + return rc; +} + static int dax_mmap_prepare(struct vm_area_desc *desc) { struct file *filp = desc->file; @@ -370,6 +554,7 @@ static const struct file_operations dax_fops = { .release = dax_release, .get_unmapped_area = dax_get_unmapped_area, .mmap_prepare = dax_mmap_prepare, + .fallocate = dax_fallocate, .fop_flags = FOP_MMAP_SYNC, }; @@ -378,9 +563,12 @@ static void dev_dax_cdev_del(void *cdev) cdev_del(cdev); } -static void dev_dax_kill(void *dev_dax) +static void dev_dax_kill(void *data) { + struct dev_dax *dev_dax = data; + kill_dev_dax(dev_dax); + WRITE_ONCE(dev_dax->device_dax_bound, false); } static int dev_dax_probe(struct dev_dax *dev_dax) @@ -454,6 +642,7 @@ static int dev_dax_probe(struct dev_dax *dev_dax) if (rc) return rc; + WRITE_ONCE(dev_dax->device_dax_bound, true); run_dax(dax_dev); return devm_add_action_or_reset(dev, dev_dax_kill, dev_dax); } diff --git a/drivers/dax/pmem.c b/drivers/dax/pmem.c index bee93066a8497..d8f3176512d2f 100644 --- a/drivers/dax/pmem.c +++ b/drivers/dax/pmem.c @@ -4,8 +4,18 @@ #include #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, -- 2.56.1.839.g22a8290db9-goog