libnvdimm, pfn, convert nd_pfn_probe() to devm
authorDan Williams <dan.j.williams@intel.com>
Fri, 18 Mar 2016 01:16:15 +0000 (18:16 -0700)
committerDan Williams <dan.j.williams@intel.com>
Fri, 22 Apr 2016 17:59:54 +0000 (10:59 -0700)
Pass the device performing the probe so we can use a devm allocation for
the pfn superblock.

Cc: Ross Zwisler <ross.zwisler@linux.intel.com>
Reviewed-by: Johannes Thumshirn <jthumshirn@suse.de>
Signed-off-by: Dan Williams <dan.j.williams@intel.com>
drivers/nvdimm/nd.h
drivers/nvdimm/pfn_devs.c
drivers/nvdimm/pmem.c

index b0a4ab91307b8bce451d5505d132abaeaf537281..c831caa3d60ad3e49d0c3415229cfb78738755f8 100644 (file)
@@ -219,12 +219,14 @@ static inline struct device *nd_btt_create(struct nd_region *nd_region)
 
 struct nd_pfn *to_nd_pfn(struct device *dev);
 #if IS_ENABLED(CONFIG_NVDIMM_PFN)
-int nd_pfn_probe(struct nd_namespace_common *ndns, void *drvdata);
+int nd_pfn_probe(struct device *dev, struct nd_namespace_common *ndns,
+               void *drvdata);
 bool is_nd_pfn(struct device *dev);
 struct device *nd_pfn_create(struct nd_region *nd_region);
 int nd_pfn_validate(struct nd_pfn *nd_pfn);
 #else
-static inline int nd_pfn_probe(struct nd_namespace_common *ndns, void *drvdata)
+static inline int nd_pfn_probe(struct device *dev, struct nd_namespace_common *ndns,
+               void *drvdata)
 {
        return -ENODEV;
 }
index e071e214febadae71d2d369319b76cd5b6b1a851..96aa5490c279a1530cd2b46bafd27cd769f18cf5 100644 (file)
@@ -410,11 +410,12 @@ int nd_pfn_validate(struct nd_pfn *nd_pfn)
 }
 EXPORT_SYMBOL(nd_pfn_validate);
 
-int nd_pfn_probe(struct nd_namespace_common *ndns, void *drvdata)
+int nd_pfn_probe(struct device *dev, struct nd_namespace_common *ndns,
+               void *drvdata)
 {
        int rc;
-       struct device *dev;
        struct nd_pfn *nd_pfn;
+       struct device *pfn_dev;
        struct nd_pfn_sb *pfn_sb;
        struct nd_region *nd_region = to_nd_region(ndns->dev.parent);
 
@@ -422,24 +423,22 @@ int nd_pfn_probe(struct nd_namespace_common *ndns, void *drvdata)
                return -ENODEV;
 
        nvdimm_bus_lock(&ndns->dev);
-       dev = __nd_pfn_create(nd_region, ndns);
+       pfn_dev = __nd_pfn_create(nd_region, ndns);
        nvdimm_bus_unlock(&ndns->dev);
-       if (!dev)
+       if (!pfn_dev)
                return -ENOMEM;
-       dev_set_drvdata(dev, drvdata);
-       pfn_sb = kzalloc(sizeof(*pfn_sb), GFP_KERNEL);
-       nd_pfn = to_nd_pfn(dev);
+       dev_set_drvdata(pfn_dev, drvdata);
+       pfn_sb = devm_kzalloc(dev, sizeof(*pfn_sb), GFP_KERNEL);
+       nd_pfn = to_nd_pfn(pfn_dev);
        nd_pfn->pfn_sb = pfn_sb;
        rc = nd_pfn_validate(nd_pfn);
-       nd_pfn->pfn_sb = NULL;
-       kfree(pfn_sb);
-       dev_dbg(&ndns->dev, "%s: pfn: %s\n", __func__,
-                       rc == 0 ? dev_name(dev) : "<none>");
+       dev_dbg(dev, "%s: pfn: %s\n", __func__,
+                       rc == 0 ? dev_name(pfn_dev) : "<none>");
        if (rc < 0) {
-               __nd_detach_ndns(dev, &nd_pfn->ndns);
-               put_device(dev);
+               __nd_detach_ndns(pfn_dev, &nd_pfn->ndns);
+               put_device(pfn_dev);
        } else
-               __nd_device_register(&nd_pfn->dev);
+               __nd_device_register(pfn_dev);
 
        return rc;
 }
index 2b51d4d34207dae229416429d6cfc8a4babdb8ac..4d8f5c55aade9f864bc4457825c7fc2d010def69 100644 (file)
@@ -330,18 +330,19 @@ static int pmem_rw_bytes(struct nd_namespace_common *ndns,
 
 static int nd_pfn_init(struct nd_pfn *nd_pfn)
 {
-       struct nd_pfn_sb *pfn_sb = kzalloc(sizeof(*pfn_sb), GFP_KERNEL);
        struct pmem_device *pmem = dev_get_drvdata(&nd_pfn->dev);
        struct nd_namespace_common *ndns = nd_pfn->ndns;
        u32 start_pad = 0, end_trunc = 0;
        resource_size_t start, size;
        struct nd_namespace_io *nsio;
        struct nd_region *nd_region;
+       struct nd_pfn_sb *pfn_sb;
        unsigned long npfns;
        phys_addr_t offset;
        u64 checksum;
        int rc;
 
+       pfn_sb = devm_kzalloc(&nd_pfn->dev, sizeof(*pfn_sb), GFP_KERNEL);
        if (!pfn_sb)
                return -ENOMEM;
 
@@ -357,7 +358,7 @@ static int nd_pfn_init(struct nd_pfn *nd_pfn)
                dev_info(&nd_pfn->dev,
                                "%s is read-only, unable to init metadata\n",
                                dev_name(&nd_region->dev));
-               goto err;
+               return -ENXIO;
        }
 
        memset(pfn_sb, 0, sizeof(*pfn_sb));
@@ -402,12 +403,12 @@ static int nd_pfn_init(struct nd_pfn *nd_pfn)
        else if (nd_pfn->mode == PFN_MODE_RAM)
                offset = ALIGN(start + SZ_8K, nd_pfn->align) - start;
        else
-               goto err;
+               return -ENXIO;
 
        if (offset + start_pad + end_trunc >= pmem->size) {
                dev_err(&nd_pfn->dev, "%s unable to satisfy requested alignment\n",
                                dev_name(&ndns->dev));
-               goto err;
+               return -ENXIO;
        }
 
        npfns = (pmem->size - offset - start_pad - end_trunc) / SZ_4K;
@@ -424,30 +425,16 @@ static int nd_pfn_init(struct nd_pfn *nd_pfn)
        checksum = nd_sb_checksum((struct nd_gen_sb *) pfn_sb);
        pfn_sb->checksum = cpu_to_le64(checksum);
 
-       rc = nvdimm_write_bytes(ndns, SZ_4K, pfn_sb, sizeof(*pfn_sb));
-       if (rc)
-               goto err;
-
-       return 0;
- err:
-       nd_pfn->pfn_sb = NULL;
-       kfree(pfn_sb);
-       return -ENXIO;
+       return nvdimm_write_bytes(ndns, SZ_4K, pfn_sb, sizeof(*pfn_sb));
 }
 
-static int nvdimm_namespace_detach_pfn(struct nd_pfn *nd_pfn)
+static void nvdimm_namespace_detach_pfn(struct nd_pfn *nd_pfn)
 {
        struct pmem_device *pmem;
 
        /* free pmem disk */
        pmem = dev_get_drvdata(&nd_pfn->dev);
        pmem_detach_disk(pmem);
-
-       /* release nd_pfn resources */
-       kfree(nd_pfn->pfn_sb);
-       nd_pfn->pfn_sb = NULL;
-
-       return 0;
 }
 
 /*
@@ -587,7 +574,8 @@ static int nd_pmem_probe(struct device *dev)
        if (is_nd_pfn(dev))
                return nvdimm_namespace_attach_pfn(ndns);
 
-       if (nd_btt_probe(ndns, pmem) == 0 || nd_pfn_probe(ndns, pmem) == 0) {
+       if (nd_btt_probe(ndns, pmem) == 0
+                       || nd_pfn_probe(dev, ndns, pmem) == 0) {
                /*
                 * We'll come back as either btt-pmem, or pfn-pmem, so
                 * drop the queue allocation for now.