A 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. Signed-off-by: Sourav Panda --- 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(-) diff --git a/drivers/nvdimm/nd.h b/drivers/nvdimm/nd.h index ed69dea5b75fd..8547b27f3f8d4 100644 --- a/drivers/nvdimm/nd.h +++ b/drivers/nvdimm/nd.h @@ -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); }; diff --git a/drivers/nvdimm/nd_virtio.c b/drivers/nvdimm/nd_virtio.c index 3b8be79a20a0f..3b4b58f913032 100644 --- a/drivers/nvdimm/nd_virtio.c +++ b/drivers/nvdimm/nd_virtio.c @@ -38,7 +38,7 @@ static bool virtio_pmem_req_done(struct virtio_pmem_request *req) static void virtio_pmem_complete_err(struct virtio_pmem_request *req) { - req->resp.ret = cpu_to_le32(1); + req->resp.ret = cpu_to_le32(VIRTIO_PMEM_RESP_TYPE_EIO); virtio_pmem_signal_done(req); } @@ -138,16 +138,38 @@ void virtio_pmem_host_ack(struct virtqueue *vq) } EXPORT_SYMBOL_GPL(virtio_pmem_host_ack); - /* The request submission function */ -static int virtio_pmem_flush(struct nd_region *nd_region) +/* Translate a VIRTIO_PMEM_RESP_TYPE_* wire status into an errno. */ +static int virtio_pmem_resp_to_errno(u32 ret) { - struct virtio_device *vdev = nd_region->provider_data; - struct virtio_pmem *vpmem = vdev->priv; - struct virtio_pmem_request *req_data; + switch (ret) { + case VIRTIO_PMEM_RESP_TYPE_OK: + return 0; + case VIRTIO_PMEM_RESP_TYPE_EINVAL: + return -EINVAL; + case VIRTIO_PMEM_RESP_TYPE_ENOTSUP: + return -EOPNOTSUPP; + case VIRTIO_PMEM_RESP_TYPE_EIO: + default: + return -EIO; + } +} + +/* + * Submit one request buffer to the device and wait for the host to complete + * it. Takes ownership of @req_data and drops the caller's kref before + * returning. + */ +static int virtio_pmem_submit(struct virtio_pmem *vpmem, + struct virtio_pmem_request *req_data, + void *req, size_t req_len) +{ + struct virtio_device *vdev = vpmem->vdev; struct scatterlist *sgs[2], sg, ret; unsigned long flags; int err, err1; + kref_init(&req_data->kref); + guard(mutex)(&vpmem->flush_lock); /* @@ -156,23 +178,21 @@ static int virtio_pmem_flush(struct nd_region *nd_region) */ if (vdev->config->get_status(vdev) & VIRTIO_CONFIG_S_NEEDS_RESET) { dev_info(&vdev->dev, "virtio pmem device needs a reset\n"); - return -EIO; + err = -EIO; + goto out_put; } - if (READ_ONCE(vpmem->broken)) - return -EIO; - - req_data = kmalloc_obj(*req_data, GFP_NOIO); - if (!req_data) - return -ENOMEM; + if (READ_ONCE(vpmem->broken)) { + err = -EIO; + goto out_put; + } - kref_init(&req_data->kref); + req_data->resp.ret = cpu_to_le32(VIRTIO_PMEM_RESP_TYPE_EIO); WRITE_ONCE(req_data->done, false); init_waitqueue_head(&req_data->host_acked); init_waitqueue_head(&req_data->wq_buf); INIT_LIST_HEAD(&req_data->list); - req_data->req.type = cpu_to_le32(VIRTIO_PMEM_REQ_TYPE_FLUSH); - sg_init_one(&sg, &req_data->req, sizeof(req_data->req)); + sg_init_one(&sg, req, req_len); sgs[0] = &sg; sg_init_one(&ret, &req_data->resp.ret, sizeof(req_data->resp)); sgs[1] = &ret; @@ -246,15 +266,122 @@ static int virtio_pmem_flush(struct nd_region *nd_region) virtio_pmem_req_done(req_data) || READ_ONCE(vpmem->broken)); if (virtio_pmem_req_done(req_data)) - err = le32_to_cpu(req_data->resp.ret); + err = virtio_pmem_resp_to_errno(le32_to_cpu(req_data->resp.ret)); else err = -EIO; } +out_put: kref_put(&req_data->kref, virtio_pmem_req_release); return err; +} + +/* The flush request submission function */ +static int virtio_pmem_flush(struct nd_region *nd_region) +{ + struct virtio_device *vdev = nd_region->provider_data; + struct virtio_pmem *vpmem = vdev->priv; + struct virtio_pmem_request *req_data; + + req_data = kmalloc_obj(*req_data, GFP_NOIO); + if (!req_data) + return -ENOMEM; + + req_data->req.type = cpu_to_le32(VIRTIO_PMEM_REQ_TYPE_FLUSH); + return virtio_pmem_submit(vpmem, req_data, &req_data->req, + sizeof(req_data->req)); +} + +struct virtio_pmem_discard_request { + struct virtio_pmem_request base; + struct virtio_pmem_discard_req dreq; }; +/* virtio_pmem_req_release() frees the allocation through &base */ +static_assert(offsetof(struct virtio_pmem_discard_request, base) == 0); + +/** + * virtio_pmem_discard - ask the host to drop the backing store of ranges + * @nd_region: region created by virtio_pmem_probe() + * @ranges: physical address ranges (inclusive end) inside the region + * @nr: number of ranges, 1..ND_DISCARD_MAX_RANGES + * + * Only installed as nd_region->discard when VIRTIO_PMEM_F_DISCARD was + * negotiated. Every range must be aligned to vpmem->discard_granularity. + * Ranges are pre-validated and submitted in chunks of up to + * vpmem->max_discard_extents. The host releases the backing store of all + * extents (or none, on a validation failure) before completing each request; + * subsequent reads return zeroes. + */ +int virtio_pmem_discard(struct nd_region *nd_region, const struct range *ranges, + unsigned int nr) +{ + struct virtio_device *vdev = nd_region->provider_data; + struct virtio_pmem *vpmem = vdev->priv; + struct virtio_pmem_discard_request *vreq; + unsigned int i, off; + u64 region_end; + int err = 0; + + if (!virtio_has_feature(vdev, VIRTIO_PMEM_F_DISCARD)) + return -EOPNOTSUPP; + if (!nr || nr > ND_DISCARD_MAX_RANGES || !vpmem->size || + !vpmem->discard_granularity || !vpmem->max_discard_extents) + return -EINVAL; + region_end = vpmem->start + vpmem->size - 1; + if (region_end < vpmem->start) + return -EINVAL; + for (i = 0; i < nr; i++) { + u64 len; + + if (ranges[i].start > ranges[i].end) + return -EINVAL; + len = range_len(&ranges[i]); + if (ranges[i].start < vpmem->start || !len || + ranges[i].end > region_end || + !IS_ALIGNED(ranges[i].start - vpmem->start, + vpmem->discard_granularity) || + !IS_ALIGNED(len, vpmem->discard_granularity)) + return -EINVAL; + } + + for (off = 0; off < nr; ) { + unsigned int chunk = min_t(unsigned int, nr - off, + vpmem->max_discard_extents); + size_t req_len = struct_size(&vreq->dreq, extents, chunk); + + vreq = kzalloc(struct_size(vreq, dreq.extents, chunk), + GFP_NOIO); + if (!vreq) + return -ENOMEM; + + vreq->dreq.type = cpu_to_le32(VIRTIO_PMEM_REQ_TYPE_DISCARD); + vreq->dreq.flags = 0; + vreq->dreq.count = cpu_to_le16(chunk); + for (i = 0; i < chunk; i++) { + vreq->dreq.extents[i].offset = + cpu_to_le64(ranges[off + i].start - + vpmem->start); + vreq->dreq.extents[i].len = + cpu_to_le64(range_len(&ranges[off + i])); + } + + err = virtio_pmem_submit(vpmem, &vreq->base, &vreq->dreq, + req_len); + if (err) { + dev_warn_ratelimited(&vdev->dev, + "discard of %u extent(s) from %#llx failed: %d\n", + chunk, + ranges[off].start - vpmem->start, + err); + return err; + } + off += chunk; + } + return 0; +} +EXPORT_SYMBOL_GPL(virtio_pmem_discard); + static void virtio_pmem_flush_work(struct work_struct *work) { struct virtio_pmem_flush_work *flush; @@ -262,8 +389,6 @@ static void virtio_pmem_flush_work(struct work_struct *work) flush = container_of(work, struct virtio_pmem_flush_work, work); err = virtio_pmem_flush(flush->nd_region); - if (err > 0) - err = -EIO; if (err) flush->bio->bi_status = errno_to_blk_status(err); bio_endio(flush->bio); @@ -277,7 +402,6 @@ int async_pmem_flush(struct nd_region *nd_region, struct bio *bio) struct virtio_pmem *vpmem = vdev->priv; struct virtio_pmem_flush_work *flush; unsigned long flags; - int err; if (bio && bio->bi_iter.bi_sector != -1) { flush = kmalloc_obj(*flush, GFP_NOIO); @@ -299,11 +423,7 @@ int async_pmem_flush(struct nd_region *nd_region, struct bio *bio) return NVDIMM_FLUSH_ASYNC; } - err = virtio_pmem_flush(nd_region); - if (err > 0) - return -EIO; - - return err; + return virtio_pmem_flush(nd_region); }; EXPORT_SYMBOL_GPL(async_pmem_flush); MODULE_DESCRIPTION("Virtio Persistent Memory Driver"); diff --git a/drivers/nvdimm/region_devs.c b/drivers/nvdimm/region_devs.c index 09842ca681107..25ff8489313f9 100644 --- a/drivers/nvdimm/region_devs.c +++ b/drivers/nvdimm/region_devs.c @@ -1045,6 +1045,11 @@ static struct nd_region *nd_region_create(struct nvdimm_bus *nvdimm_bus, nd_region->flush = ndr_desc->flush; else nd_region->flush = NULL; + if (ndr_desc->discard && + nvdimm_discard_granularity_valid(ndr_desc->discard_granularity)) { + nd_region->discard = ndr_desc->discard; + nd_region->discard_granularity = ndr_desc->discard_granularity; + } device_initialize(dev); lockdep_set_class(&dev->mutex, &nvdimm_region_key); @@ -1173,6 +1178,62 @@ int nvdimm_has_flush(struct nd_region *nd_region) } EXPORT_SYMBOL_GPL(nvdimm_has_flush); +/** + * nvdimm_discard_granularity - return the discard alignment granularity in bytes + * @nd_region: pmem region + * + * Returns a power-of-2 byte granularity (>= PAGE_SIZE) if the region provider + * supports discarding physical ranges, or 0 if unsupported. + */ +u64 nvdimm_discard_granularity(struct nd_region *nd_region) +{ + if (!nd_region->discard) + return 0; + return nd_region->discard_granularity; +} +EXPORT_SYMBOL_GPL(nvdimm_discard_granularity); + +/** + * nvdimm_discard - return the backing store of physical ranges to the provider + * @nd_region: pmem region + * @ranges: physical address ranges (inclusive end), each inside the region + * @nr: number of ranges, 1..ND_DISCARD_MAX_RANGES + * + * Returns 0 on success, -EOPNOTSUPP when the provider has no discard + * capability, -EINVAL for ranges outside the region or unaligned to + * nd_region->discard_granularity. + */ +int nvdimm_discard(struct nd_region *nd_region, const struct range *ranges, + unsigned int nr) +{ + u64 gran = nd_region->discard_granularity; + unsigned int i; + u64 region_end; + + if (!nd_region->discard || !gran) + return -EOPNOTSUPP; + if (!nr || nr > ND_DISCARD_MAX_RANGES || !nd_region->ndr_size) + return -EINVAL; + region_end = nd_region->ndr_start + nd_region->ndr_size - 1; + if (region_end < nd_region->ndr_start) + return -EINVAL; + for (i = 0; i < nr; i++) { + u64 len; + + if (ranges[i].start > ranges[i].end) + return -EINVAL; + len = range_len(&ranges[i]); + if (ranges[i].start < nd_region->ndr_start || !len || + ranges[i].end > region_end || + !IS_ALIGNED(ranges[i].start - nd_region->ndr_start, gran) || + !IS_ALIGNED(len, gran)) + return -EINVAL; + } + + return nd_region->discard(nd_region, ranges, nr); +} +EXPORT_SYMBOL_GPL(nvdimm_discard); + int nvdimm_has_cache(struct nd_region *nd_region) { return is_nd_pmem(&nd_region->dev) && diff --git a/drivers/nvdimm/virtio_pmem.c b/drivers/nvdimm/virtio_pmem.c index 3e06ff34c4afc..7a97c963b7df2 100644 --- a/drivers/nvdimm/virtio_pmem.c +++ b/drivers/nvdimm/virtio_pmem.c @@ -56,6 +56,24 @@ static int virtio_pmem_validate(struct virtio_device *vdev) VIRTIO_PMEM_SHMEM_REGION_ID); __virtio_clear_bit(vdev, VIRTIO_PMEM_F_SHMEM_REGION); } + if (virtio_has_feature(vdev, VIRTIO_PMEM_F_DISCARD)) { + u64 granularity = 0; + u32 max_extents = 0; + + if (vdev->config->get) { + virtio_cread_le(vdev, struct virtio_pmem_config, + discard_granularity, &granularity); + virtio_cread_le(vdev, struct virtio_pmem_config, + max_discard_extents, &max_extents); + } + if (!nvdimm_discard_granularity_valid(granularity) || + !max_extents) { + dev_notice(&vdev->dev, + "invalid discard config (granularity=%llu, max_extents=%u), disabling discard\n", + granularity, max_extents); + __virtio_clear_bit(vdev, VIRTIO_PMEM_F_DISCARD); + } + } return 0; } @@ -133,6 +151,31 @@ static int virtio_pmem_probe(struct virtio_device *vdev) } ndr_desc.flush = async_pmem_flush; + if (virtio_has_feature(vdev, VIRTIO_PMEM_F_DISCARD)) { + u32 max_extents; + + virtio_cread_le(vdev, struct virtio_pmem_config, + discard_granularity, + &vpmem->discard_granularity); + virtio_cread_le(vdev, struct virtio_pmem_config, + max_discard_extents, &max_extents); + vpmem->max_discard_extents = + min_t(u32, max_extents, VIRTIO_PMEM_DISCARD_MAX_EXTENTS); + /* The config was validated earlier but is re-read here */ + if (nvdimm_discard_granularity_valid(vpmem->discard_granularity) && + vpmem->max_discard_extents) { + ndr_desc.discard = virtio_pmem_discard; + ndr_desc.discard_granularity = + vpmem->discard_granularity; + dev_info(&vdev->dev, + "discard supported (granularity=%llu, max_extents=%u)\n", + vpmem->discard_granularity, + vpmem->max_discard_extents); + } else { + dev_notice(&vdev->dev, + "discard config changed after validation, disabling discard\n"); + } + } ndr_desc.provider_data = vdev; set_bit(ND_REGION_PAGEMAP, &ndr_desc.flags); set_bit(ND_REGION_ASYNC, &ndr_desc.flags); @@ -219,6 +262,7 @@ static int virtio_pmem_restore(struct virtio_device *vdev) static unsigned int features[] = { VIRTIO_PMEM_F_SHMEM_REGION, + VIRTIO_PMEM_F_DISCARD, }; static struct virtio_driver virtio_pmem_driver = { diff --git a/drivers/nvdimm/virtio_pmem.h b/drivers/nvdimm/virtio_pmem.h index 0b90777d7658b..51fc0fa4b0651 100644 --- a/drivers/nvdimm/virtio_pmem.h +++ b/drivers/nvdimm/virtio_pmem.h @@ -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 diff --git a/include/linux/libnvdimm.h b/include/linux/libnvdimm.h index d929d83abf3be..9214e395a07de 100644 --- a/include/linux/libnvdimm.h +++ b/include/linux/libnvdimm.h @@ -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); diff --git a/include/uapi/linux/virtio_pmem.h b/include/uapi/linux/virtio_pmem.h index ede4f3564977d..2698f7a7410a2 100644 --- a/include/uapi/linux/virtio_pmem.h +++ b/include/uapi/linux/virtio_pmem.h @@ -17,6 +17,11 @@ /* Feature bits */ /* guest physical address range will be indicated as shared memory region 0 */ #define VIRTIO_PMEM_F_SHMEM_REGION 0 +/* + * Device supports VIRTIO_PMEM_REQ_TYPE_DISCARD: the guest can ask the host + * to release the backing store of ranges of the pmem region. + */ +#define VIRTIO_PMEM_F_DISCARD 1 /* shmid of the shared memory region corresponding to the pmem */ #define VIRTIO_PMEM_SHMEM_REGION_ID 0 @@ -24,12 +29,29 @@ struct virtio_pmem_config { __le64 start; __le64 size; + /* + * Valid when VIRTIO_PMEM_F_DISCARD is negotiated: + * @discard_granularity: power-of-2 byte alignment (>= PAGE_SIZE) for + * discard extent offsets and lengths. + * @max_discard_extents: maximum number of extents per discard request, + * 1..65535 (virtio_pmem_discard_req.count is 16 bits wide). + */ + __le64 discard_granularity; + __le32 max_discard_extents; + __le32 reserved; }; #define VIRTIO_PMEM_REQ_TYPE_FLUSH 0 +#define VIRTIO_PMEM_REQ_TYPE_DISCARD 1 + +/* Host return status (struct virtio_pmem_resp.ret) */ +#define VIRTIO_PMEM_RESP_TYPE_OK 0 +#define VIRTIO_PMEM_RESP_TYPE_EIO 1 +#define VIRTIO_PMEM_RESP_TYPE_EINVAL 2 +#define VIRTIO_PMEM_RESP_TYPE_ENOTSUP 3 struct virtio_pmem_resp { - /* Host return status corresponding to flush request */ + /* Host return status corresponding to the request */ __le32 ret; }; @@ -38,4 +60,29 @@ struct virtio_pmem_req { __le32 type; }; +struct virtio_pmem_discard_extent { + /* byte offset relative to the start of the pmem region */ + __le64 offset; + /* number of bytes, non-zero */ + __le64 len; +}; + +/* + * VIRTIO_PMEM_REQ_TYPE_DISCARD request. Offsets and lengths must be + * multiples of virtio_pmem_config.discard_granularity, and @count must be + * in 1..virtio_pmem_config.max_discard_extents. The device validates every + * extent before releasing any backing store (e.g. fallocate(FALLOC_FL_PUNCH_HOLE) + * on the host file) and only completes the request once all extents have + * been released; subsequent guest reads of the ranges return zeroes. + */ +struct virtio_pmem_discard_req { + /* VIRTIO_PMEM_REQ_TYPE_DISCARD */ + __le32 type; + /* reserved, must be zero */ + __le16 flags; + /* number of valid entries in @extents */ + __le16 count; + struct virtio_pmem_discard_extent extents[]; +}; + #endif -- 2.56.1.839.g22a8290db9-goog