diff mbox series

[v2,1/2] libnvdimm: nd_region flush callback support

Message ID 20181013050021.11962-2-pagupta@redhat.com (mailing list archive)
State New, archived
Headers show
Series kvm "fake DAX" device | expand

Commit Message

Pankaj Gupta Oct. 13, 2018, 5 a.m. UTC
This patch adds functionality to perform flush from guest
to host over VIRTIO. We are registering a callback based
on 'nd_region' type. virtio_pmem driver requires this special
flush function. For rest of the region types we are registering
existing flush function. Report error returned by host fsync
failure to userspace. 

This also handles asynchronous flush requests from the block layer 
by creating a child bio and chaining it with parent bio.

Signed-off-by: Pankaj Gupta <pagupta@redhat.com>
---
 drivers/acpi/nfit/core.c     |  4 ++--
 drivers/nvdimm/claim.c       |  6 ++++--
 drivers/nvdimm/nd.h          |  1 +
 drivers/nvdimm/pmem.c        | 12 ++++++++----
 drivers/nvdimm/region_devs.c | 38 ++++++++++++++++++++++++++++++++++++--
 include/linux/libnvdimm.h    |  5 ++++-
 6 files changed, 55 insertions(+), 11 deletions(-)

Comments

kernel test robot Oct. 13, 2018, 8:31 a.m. UTC | #1
Hi Pankaj,

Thank you for the patch! Perhaps something to improve:

[auto build test WARNING on linux-nvdimm/libnvdimm-for-next]
[also build test WARNING on v4.19-rc7 next-20181012]
[if your patch is applied to the wrong git tree, please drop us a note to help improve the system]

url:    https://github.com/0day-ci/linux/commits/Pankaj-Gupta/libnvdimm-nd_region-flush-callback-support/20181013-152624
base:   https://git.kernel.org/pub/scm/linux/kernel/git/nvdimm/nvdimm.git libnvdimm-for-next
config: x86_64-randconfig-x017-201840 (attached as .config)
compiler: gcc-7 (Debian 7.3.0-1) 7.3.0
reproduce:
        # save the attached .config to linux build tree
        make ARCH=x86_64 

All warnings (new ones prefixed by >>):

   In file included from drivers/nvdimm/bus.c:14:0:
>> include/linux/libnvdimm.h:206:54: warning: 'struct bio' declared inside parameter list will not be visible outside of this definition or declaration
    int nvdimm_flush(struct nd_region *nd_region, struct bio *bio, bool async);
                                                         ^~~

vim +206 include/linux/libnvdimm.h

   159	
   160	void badrange_init(struct badrange *badrange);
   161	int badrange_add(struct badrange *badrange, u64 addr, u64 length);
   162	void badrange_forget(struct badrange *badrange, phys_addr_t start,
   163			unsigned int len);
   164	int nvdimm_bus_add_badrange(struct nvdimm_bus *nvdimm_bus, u64 addr,
   165			u64 length);
   166	struct nvdimm_bus *nvdimm_bus_register(struct device *parent,
   167			struct nvdimm_bus_descriptor *nfit_desc);
   168	void nvdimm_bus_unregister(struct nvdimm_bus *nvdimm_bus);
   169	struct nvdimm_bus *to_nvdimm_bus(struct device *dev);
   170	struct nvdimm *to_nvdimm(struct device *dev);
   171	struct nd_region *to_nd_region(struct device *dev);
   172	struct device *nd_region_dev(struct nd_region *nd_region);
   173	struct nd_blk_region *to_nd_blk_region(struct device *dev);
   174	struct nvdimm_bus_descriptor *to_nd_desc(struct nvdimm_bus *nvdimm_bus);
   175	struct device *to_nvdimm_bus_dev(struct nvdimm_bus *nvdimm_bus);
   176	const char *nvdimm_name(struct nvdimm *nvdimm);
   177	struct kobject *nvdimm_kobj(struct nvdimm *nvdimm);
   178	unsigned long nvdimm_cmd_mask(struct nvdimm *nvdimm);
   179	void *nvdimm_provider_data(struct nvdimm *nvdimm);
   180	struct nvdimm *nvdimm_create(struct nvdimm_bus *nvdimm_bus, void *provider_data,
   181			const struct attribute_group **groups, unsigned long flags,
   182			unsigned long cmd_mask, int num_flush,
   183			struct resource *flush_wpq);
   184	const struct nd_cmd_desc *nd_cmd_dimm_desc(int cmd);
   185	const struct nd_cmd_desc *nd_cmd_bus_desc(int cmd);
   186	u32 nd_cmd_in_size(struct nvdimm *nvdimm, int cmd,
   187			const struct nd_cmd_desc *desc, int idx, void *buf);
   188	u32 nd_cmd_out_size(struct nvdimm *nvdimm, int cmd,
   189			const struct nd_cmd_desc *desc, int idx, const u32 *in_field,
   190			const u32 *out_field, unsigned long remainder);
   191	int nvdimm_bus_check_dimm_count(struct nvdimm_bus *nvdimm_bus, int dimm_count);
   192	struct nd_region *nvdimm_pmem_region_create(struct nvdimm_bus *nvdimm_bus,
   193			struct nd_region_desc *ndr_desc);
   194	struct nd_region *nvdimm_blk_region_create(struct nvdimm_bus *nvdimm_bus,
   195			struct nd_region_desc *ndr_desc);
   196	struct nd_region *nvdimm_volatile_region_create(struct nvdimm_bus *nvdimm_bus,
   197			struct nd_region_desc *ndr_desc);
   198	void *nd_region_provider_data(struct nd_region *nd_region);
   199	void *nd_blk_region_provider_data(struct nd_blk_region *ndbr);
   200	void nd_blk_region_set_provider_data(struct nd_blk_region *ndbr, void *data);
   201	struct nvdimm *nd_blk_region_to_dimm(struct nd_blk_region *ndbr);
   202	unsigned long nd_blk_memremap_flags(struct nd_blk_region *ndbr);
   203	unsigned int nd_region_acquire_lane(struct nd_region *nd_region);
   204	void nd_region_release_lane(struct nd_region *nd_region, unsigned int lane);
   205	u64 nd_fletcher64(void *addr, size_t len, bool le);
 > 206	int nvdimm_flush(struct nd_region *nd_region, struct bio *bio, bool async);
   207	int generic_nvdimm_flush(struct nd_region *nd_region);
   208	int nvdimm_has_flush(struct nd_region *nd_region);
   209	int nvdimm_has_cache(struct nd_region *nd_region);
   210	

---
0-DAY kernel test infrastructure                Open Source Technology Center
https://lists.01.org/pipermail/kbuild-all                   Intel Corporation
kernel test robot Oct. 13, 2018, 9:38 a.m. UTC | #2
Hi Pankaj,

Thank you for the patch! Perhaps something to improve:

[auto build test WARNING on linux-nvdimm/libnvdimm-for-next]
[also build test WARNING on v4.19-rc7 next-20181012]
[if your patch is applied to the wrong git tree, please drop us a note to help improve the system]

url:    https://github.com/0day-ci/linux/commits/Pankaj-Gupta/libnvdimm-nd_region-flush-callback-support/20181013-152624
base:   https://git.kernel.org/pub/scm/linux/kernel/git/nvdimm/nvdimm.git libnvdimm-for-next
config: x86_64-randconfig-g0-10131621 (attached as .config)
compiler: gcc-4.9 (Debian 4.9.4-2) 4.9.4
reproduce:
        # save the attached .config to linux build tree
        make ARCH=x86_64 

All warnings (new ones prefixed by >>):

   In file included from drivers//acpi/nfit/core.c:14:0:
>> include/linux/libnvdimm.h:206:54: warning: 'struct bio' declared inside parameter list
    int nvdimm_flush(struct nd_region *nd_region, struct bio *bio, bool async);
                                                         ^
>> include/linux/libnvdimm.h:206:54: warning: its scope is only this definition or declaration, which is probably not what you want

vim +206 include/linux/libnvdimm.h

   159	
   160	void badrange_init(struct badrange *badrange);
   161	int badrange_add(struct badrange *badrange, u64 addr, u64 length);
   162	void badrange_forget(struct badrange *badrange, phys_addr_t start,
   163			unsigned int len);
   164	int nvdimm_bus_add_badrange(struct nvdimm_bus *nvdimm_bus, u64 addr,
   165			u64 length);
   166	struct nvdimm_bus *nvdimm_bus_register(struct device *parent,
   167			struct nvdimm_bus_descriptor *nfit_desc);
   168	void nvdimm_bus_unregister(struct nvdimm_bus *nvdimm_bus);
   169	struct nvdimm_bus *to_nvdimm_bus(struct device *dev);
   170	struct nvdimm *to_nvdimm(struct device *dev);
   171	struct nd_region *to_nd_region(struct device *dev);
   172	struct device *nd_region_dev(struct nd_region *nd_region);
   173	struct nd_blk_region *to_nd_blk_region(struct device *dev);
   174	struct nvdimm_bus_descriptor *to_nd_desc(struct nvdimm_bus *nvdimm_bus);
   175	struct device *to_nvdimm_bus_dev(struct nvdimm_bus *nvdimm_bus);
   176	const char *nvdimm_name(struct nvdimm *nvdimm);
   177	struct kobject *nvdimm_kobj(struct nvdimm *nvdimm);
   178	unsigned long nvdimm_cmd_mask(struct nvdimm *nvdimm);
   179	void *nvdimm_provider_data(struct nvdimm *nvdimm);
   180	struct nvdimm *nvdimm_create(struct nvdimm_bus *nvdimm_bus, void *provider_data,
   181			const struct attribute_group **groups, unsigned long flags,
   182			unsigned long cmd_mask, int num_flush,
   183			struct resource *flush_wpq);
   184	const struct nd_cmd_desc *nd_cmd_dimm_desc(int cmd);
   185	const struct nd_cmd_desc *nd_cmd_bus_desc(int cmd);
   186	u32 nd_cmd_in_size(struct nvdimm *nvdimm, int cmd,
   187			const struct nd_cmd_desc *desc, int idx, void *buf);
   188	u32 nd_cmd_out_size(struct nvdimm *nvdimm, int cmd,
   189			const struct nd_cmd_desc *desc, int idx, const u32 *in_field,
   190			const u32 *out_field, unsigned long remainder);
   191	int nvdimm_bus_check_dimm_count(struct nvdimm_bus *nvdimm_bus, int dimm_count);
   192	struct nd_region *nvdimm_pmem_region_create(struct nvdimm_bus *nvdimm_bus,
   193			struct nd_region_desc *ndr_desc);
   194	struct nd_region *nvdimm_blk_region_create(struct nvdimm_bus *nvdimm_bus,
   195			struct nd_region_desc *ndr_desc);
   196	struct nd_region *nvdimm_volatile_region_create(struct nvdimm_bus *nvdimm_bus,
   197			struct nd_region_desc *ndr_desc);
   198	void *nd_region_provider_data(struct nd_region *nd_region);
   199	void *nd_blk_region_provider_data(struct nd_blk_region *ndbr);
   200	void nd_blk_region_set_provider_data(struct nd_blk_region *ndbr, void *data);
   201	struct nvdimm *nd_blk_region_to_dimm(struct nd_blk_region *ndbr);
   202	unsigned long nd_blk_memremap_flags(struct nd_blk_region *ndbr);
   203	unsigned int nd_region_acquire_lane(struct nd_region *nd_region);
   204	void nd_region_release_lane(struct nd_region *nd_region, unsigned int lane);
   205	u64 nd_fletcher64(void *addr, size_t len, bool le);
 > 206	int nvdimm_flush(struct nd_region *nd_region, struct bio *bio, bool async);
   207	int generic_nvdimm_flush(struct nd_region *nd_region);
   208	int nvdimm_has_flush(struct nd_region *nd_region);
   209	int nvdimm_has_cache(struct nd_region *nd_region);
   210	

---
0-DAY kernel test infrastructure                Open Source Technology Center
https://lists.01.org/pipermail/kbuild-all                   Intel Corporation
diff mbox series

Patch

diff --git a/drivers/acpi/nfit/core.c b/drivers/acpi/nfit/core.c
index b072cfc..f154852 100644
--- a/drivers/acpi/nfit/core.c
+++ b/drivers/acpi/nfit/core.c
@@ -2234,7 +2234,7 @@  static void write_blk_ctl(struct nfit_blk *nfit_blk, unsigned int bw,
 		offset = to_interleave_offset(offset, mmio);
 
 	writeq(cmd, mmio->addr.base + offset);
-	nvdimm_flush(nfit_blk->nd_region);
+	nvdimm_flush(nfit_blk->nd_region, NULL, false);
 
 	if (nfit_blk->dimm_flags & NFIT_BLK_DCR_LATCH)
 		readq(mmio->addr.base + offset);
@@ -2283,7 +2283,7 @@  static int acpi_nfit_blk_single_io(struct nfit_blk *nfit_blk,
 	}
 
 	if (rw)
-		nvdimm_flush(nfit_blk->nd_region);
+		nvdimm_flush(nfit_blk->nd_region, NULL, false);
 
 	rc = read_blk_stat(nfit_blk, lane) ? -EIO : 0;
 	return rc;
diff --git a/drivers/nvdimm/claim.c b/drivers/nvdimm/claim.c
index fb667bf..a1dfa06 100644
--- a/drivers/nvdimm/claim.c
+++ b/drivers/nvdimm/claim.c
@@ -263,7 +263,7 @@  static int nsio_rw_bytes(struct nd_namespace_common *ndns,
 	struct nd_namespace_io *nsio = to_nd_namespace_io(&ndns->dev);
 	unsigned int sz_align = ALIGN(size + (offset & (512 - 1)), 512);
 	sector_t sector = offset >> 9;
-	int rc = 0;
+	int rc = 0, ret = 0;
 
 	if (unlikely(!size))
 		return 0;
@@ -301,7 +301,9 @@  static int nsio_rw_bytes(struct nd_namespace_common *ndns,
 	}
 
 	memcpy_flushcache(nsio->addr + offset, buf, size);
-	nvdimm_flush(to_nd_region(ndns->dev.parent));
+	ret = nvdimm_flush(to_nd_region(ndns->dev.parent), NULL, false);
+	if (ret)
+		rc = ret;
 
 	return rc;
 }
diff --git a/drivers/nvdimm/nd.h b/drivers/nvdimm/nd.h
index 98317e7..d53a2d1 100644
--- a/drivers/nvdimm/nd.h
+++ b/drivers/nvdimm/nd.h
@@ -160,6 +160,7 @@  struct nd_region {
 	struct nd_interleave_set *nd_set;
 	struct nd_percpu_lane __percpu *lane;
 	struct nd_mapping mapping[0];
+	int (*flush)(struct nd_region *nd_region);
 };
 
 struct nd_blk_region {
diff --git a/drivers/nvdimm/pmem.c b/drivers/nvdimm/pmem.c
index 6071e29..5d6a4a1 100644
--- a/drivers/nvdimm/pmem.c
+++ b/drivers/nvdimm/pmem.c
@@ -192,6 +192,7 @@  static blk_status_t pmem_do_bvec(struct pmem_device *pmem, struct page *page,
 
 static blk_qc_t pmem_make_request(struct request_queue *q, struct bio *bio)
 {
+	int ret = 0;
 	blk_status_t rc = 0;
 	bool do_acct;
 	unsigned long start;
@@ -201,7 +202,7 @@  static blk_qc_t pmem_make_request(struct request_queue *q, struct bio *bio)
 	struct nd_region *nd_region = to_region(pmem);
 
 	if (bio->bi_opf & REQ_PREFLUSH)
-		nvdimm_flush(nd_region);
+		ret = nvdimm_flush(nd_region, bio, true);
 
 	do_acct = nd_iostat_start(bio, &start);
 	bio_for_each_segment(bvec, bio, iter) {
@@ -216,7 +217,10 @@  static blk_qc_t pmem_make_request(struct request_queue *q, struct bio *bio)
 		nd_iostat_end(bio, start);
 
 	if (bio->bi_opf & REQ_FUA)
-		nvdimm_flush(nd_region);
+		ret = nvdimm_flush(nd_region, bio, true);
+
+	if (ret)
+		bio->bi_status = errno_to_blk_status(ret);
 
 	bio_endio(bio);
 	return BLK_QC_T_NONE;
@@ -528,14 +532,14 @@  static int nd_pmem_remove(struct device *dev)
 		sysfs_put(pmem->bb_state);
 		pmem->bb_state = NULL;
 	}
-	nvdimm_flush(to_nd_region(dev->parent));
+	nvdimm_flush(to_nd_region(dev->parent), NULL, false);
 
 	return 0;
 }
 
 static void nd_pmem_shutdown(struct device *dev)
 {
-	nvdimm_flush(to_nd_region(dev->parent));
+	nvdimm_flush(to_nd_region(dev->parent), NULL, false);
 }
 
 static void nd_pmem_notify(struct device *dev, enum nvdimm_event event)
diff --git a/drivers/nvdimm/region_devs.c b/drivers/nvdimm/region_devs.c
index fa37afc..5508727 100644
--- a/drivers/nvdimm/region_devs.c
+++ b/drivers/nvdimm/region_devs.c
@@ -290,7 +290,9 @@  static ssize_t deep_flush_store(struct device *dev, struct device_attribute *att
 		return rc;
 	if (!flush)
 		return -EINVAL;
-	nvdimm_flush(nd_region);
+	rc = nvdimm_flush(nd_region, NULL, false);
+	if (rc)
+		return rc;
 
 	return len;
 }
@@ -1065,6 +1067,11 @@  static struct nd_region *nd_region_create(struct nvdimm_bus *nvdimm_bus,
 	dev->of_node = ndr_desc->of_node;
 	nd_region->ndr_size = resource_size(ndr_desc->res);
 	nd_region->ndr_start = ndr_desc->res->start;
+	if (ndr_desc->flush)
+		nd_region->flush = ndr_desc->flush;
+	else
+		nd_region->flush = generic_nvdimm_flush;
+
 	nd_device_register(dev);
 
 	return nd_region;
@@ -1105,11 +1112,36 @@  struct nd_region *nvdimm_volatile_region_create(struct nvdimm_bus *nvdimm_bus,
 }
 EXPORT_SYMBOL_GPL(nvdimm_volatile_region_create);
 
+int nvdimm_flush(struct nd_region *nd_region, struct bio *bio, bool async)
+{
+	int rc = 0;
+
+	/* Create child bio for asynchronous flush and chain with
+	 * parent bio. Otherwise directly call nd_region flush.
+	 */
+	if (async && bio->bi_iter.bi_sector != -1) {
+
+		struct bio *child = bio_alloc(GFP_ATOMIC, 0);
+
+		if (!child)
+			return -ENOMEM;
+		bio_copy_dev(child, bio);
+		child->bi_opf = REQ_PREFLUSH;
+		child->bi_iter.bi_sector = -1;
+		bio_chain(child, bio);
+		submit_bio(child);
+	} else {
+		if (nd_region->flush(nd_region))
+			rc = -EIO;
+	}
+
+	return rc;
+}
 /**
  * nvdimm_flush - flush any posted write queues between the cpu and pmem media
  * @nd_region: blk or interleaved pmem region
  */
-void nvdimm_flush(struct nd_region *nd_region)
+int generic_nvdimm_flush(struct nd_region *nd_region)
 {
 	struct nd_region_data *ndrd = dev_get_drvdata(&nd_region->dev);
 	int i, idx;
@@ -1133,6 +1165,8 @@  void nvdimm_flush(struct nd_region *nd_region)
 		if (ndrd_get_flush_wpq(ndrd, i, 0))
 			writeq(1, ndrd_get_flush_wpq(ndrd, i, idx));
 	wmb();
+
+	return 0;
 }
 EXPORT_SYMBOL_GPL(nvdimm_flush);
 
diff --git a/include/linux/libnvdimm.h b/include/linux/libnvdimm.h
index 097072c..b49632c 100644
--- a/include/linux/libnvdimm.h
+++ b/include/linux/libnvdimm.h
@@ -115,6 +115,7 @@  struct nd_mapping_desc {
 	int position;
 };
 
+struct nd_region;
 struct nd_region_desc {
 	struct resource *res;
 	struct nd_mapping_desc *mapping;
@@ -126,6 +127,7 @@  struct nd_region_desc {
 	int numa_node;
 	unsigned long flags;
 	struct device_node *of_node;
+	int (*flush)(struct nd_region *nd_region);
 };
 
 struct device;
@@ -201,7 +203,8 @@  unsigned long nd_blk_memremap_flags(struct nd_blk_region *ndbr);
 unsigned int nd_region_acquire_lane(struct nd_region *nd_region);
 void nd_region_release_lane(struct nd_region *nd_region, unsigned int lane);
 u64 nd_fletcher64(void *addr, size_t len, bool le);
-void nvdimm_flush(struct nd_region *nd_region);
+int nvdimm_flush(struct nd_region *nd_region, struct bio *bio, bool async);
+int generic_nvdimm_flush(struct nd_region *nd_region);
 int nvdimm_has_flush(struct nd_region *nd_region);
 int nvdimm_has_cache(struct nd_region *nd_region);