pmem: switch to devm_ allocations

Signed-off-by: Christoph Hellwig <hch@lst.de>
[djbw: tools/testing/nvdimm/ and memunmap_pmem support]
Reviewed-by: Ross Zwisler <ross.zwisler@linux.intel.com>
Signed-off-by: Dan Williams <dan.j.williams@intel.com>
This commit is contained in:
Christoph Hellwig 2015-08-10 23:07:08 -04:00 committed by Dan Williams
parent 7d3dcf26a6
commit 708ab62bef
4 changed files with 47 additions and 53 deletions

View File

@ -119,7 +119,7 @@ static struct pmem_device *pmem_alloc(struct device *dev,
{ {
struct pmem_device *pmem; struct pmem_device *pmem;
pmem = kzalloc(sizeof(*pmem), GFP_KERNEL); pmem = devm_kzalloc(dev, sizeof(*pmem), GFP_KERNEL);
if (!pmem) if (!pmem)
return ERR_PTR(-ENOMEM); return ERR_PTR(-ENOMEM);
@ -128,19 +128,16 @@ static struct pmem_device *pmem_alloc(struct device *dev,
if (!arch_has_pmem_api()) if (!arch_has_pmem_api())
dev_warn(dev, "unable to guarantee persistence of writes\n"); dev_warn(dev, "unable to guarantee persistence of writes\n");
if (!request_mem_region(pmem->phys_addr, pmem->size, dev_name(dev))) { if (!devm_request_mem_region(dev, pmem->phys_addr, pmem->size,
dev_name(dev))) {
dev_warn(dev, "could not reserve region [0x%pa:0x%zx]\n", dev_warn(dev, "could not reserve region [0x%pa:0x%zx]\n",
&pmem->phys_addr, pmem->size); &pmem->phys_addr, pmem->size);
kfree(pmem);
return ERR_PTR(-EBUSY); return ERR_PTR(-EBUSY);
} }
pmem->virt_addr = memremap_pmem(pmem->phys_addr, pmem->size); pmem->virt_addr = memremap_pmem(dev, pmem->phys_addr, pmem->size);
if (!pmem->virt_addr) { if (!pmem->virt_addr)
release_mem_region(pmem->phys_addr, pmem->size);
kfree(pmem);
return ERR_PTR(-ENXIO); return ERR_PTR(-ENXIO);
}
return pmem; return pmem;
} }
@ -210,20 +207,12 @@ static int pmem_rw_bytes(struct nd_namespace_common *ndns,
return 0; return 0;
} }
static void pmem_free(struct pmem_device *pmem)
{
memunmap_pmem(pmem->virt_addr);
release_mem_region(pmem->phys_addr, pmem->size);
kfree(pmem);
}
static int nd_pmem_probe(struct device *dev) static int nd_pmem_probe(struct device *dev)
{ {
struct nd_region *nd_region = to_nd_region(dev->parent); struct nd_region *nd_region = to_nd_region(dev->parent);
struct nd_namespace_common *ndns; struct nd_namespace_common *ndns;
struct nd_namespace_io *nsio; struct nd_namespace_io *nsio;
struct pmem_device *pmem; struct pmem_device *pmem;
int rc;
ndns = nvdimm_namespace_common_probe(dev); ndns = nvdimm_namespace_common_probe(dev);
if (IS_ERR(ndns)) if (IS_ERR(ndns))
@ -236,16 +225,14 @@ static int nd_pmem_probe(struct device *dev)
dev_set_drvdata(dev, pmem); dev_set_drvdata(dev, pmem);
ndns->rw_bytes = pmem_rw_bytes; ndns->rw_bytes = pmem_rw_bytes;
if (is_nd_btt(dev)) if (is_nd_btt(dev))
rc = nvdimm_namespace_attach_btt(ndns); return nvdimm_namespace_attach_btt(ndns);
else if (nd_btt_probe(ndns, pmem) == 0) {
if (nd_btt_probe(ndns, pmem) == 0)
/* we'll come back as btt-pmem */ /* we'll come back as btt-pmem */
rc = -ENXIO; return -ENXIO;
} else return pmem_attach_disk(ndns, pmem);
rc = pmem_attach_disk(ndns, pmem);
if (rc)
pmem_free(pmem);
return rc;
} }
static int nd_pmem_remove(struct device *dev) static int nd_pmem_remove(struct device *dev)
@ -256,7 +243,6 @@ static int nd_pmem_remove(struct device *dev)
nvdimm_namespace_detach_btt(to_nd_btt(dev)->ndns); nvdimm_namespace_detach_btt(to_nd_btt(dev)->ndns);
else else
pmem_detach_disk(pmem); pmem_detach_disk(pmem);
pmem_free(pmem);
return 0; return 0;
} }

View File

@ -46,9 +46,9 @@ static inline void memcpy_from_pmem(void *dst, void __pmem const *src, size_t si
memcpy(dst, (void __force const *) src, size); memcpy(dst, (void __force const *) src, size);
} }
static inline void memunmap_pmem(void __pmem *addr) static inline void memunmap_pmem(struct device *dev, void __pmem *addr)
{ {
memunmap((void __force *) addr); devm_memunmap(dev, (void __force *) addr);
} }
/** /**
@ -97,13 +97,15 @@ static inline void default_memcpy_to_pmem(void __pmem *dst, const void *src,
* wmb_pmem() arrange for the data to be written through the * wmb_pmem() arrange for the data to be written through the
* cache to persistent media. * cache to persistent media.
*/ */
static inline void __pmem *memremap_pmem(resource_size_t offset, static inline void __pmem *memremap_pmem(struct device *dev,
unsigned long size) resource_size_t offset, unsigned long size)
{ {
#ifdef ARCH_MEMREMAP_PMEM #ifdef ARCH_MEMREMAP_PMEM
return (void __pmem *) memremap(offset, size, ARCH_MEMREMAP_PMEM); return (void __pmem *) devm_memremap(dev, offset, size,
ARCH_MEMREMAP_PMEM);
#else #else
return (void __pmem *) memremap(offset, size, MEMREMAP_WT); return (void __pmem *) devm_memremap(dev, offset, size,
MEMREMAP_WT);
#endif #endif
} }

View File

@ -1,9 +1,9 @@
ldflags-y += --wrap=ioremap_wc ldflags-y += --wrap=ioremap_wc
ldflags-y += --wrap=devm_ioremap_nocache ldflags-y += --wrap=devm_ioremap_nocache
ldflags-y += --wrap=memremap ldflags-y += --wrap=devm_memremap
ldflags-y += --wrap=memunmap
ldflags-y += --wrap=ioremap_nocache ldflags-y += --wrap=ioremap_nocache
ldflags-y += --wrap=iounmap ldflags-y += --wrap=iounmap
ldflags-y += --wrap=__devm_request_region
ldflags-y += --wrap=__request_region ldflags-y += --wrap=__request_region
ldflags-y += --wrap=__release_region ldflags-y += --wrap=__release_region

View File

@ -80,8 +80,8 @@ void __iomem *__wrap_devm_ioremap_nocache(struct device *dev,
} }
EXPORT_SYMBOL(__wrap_devm_ioremap_nocache); EXPORT_SYMBOL(__wrap_devm_ioremap_nocache);
void *__wrap_memremap(resource_size_t offset, size_t size, void *__wrap_devm_memremap(struct device *dev, resource_size_t offset,
unsigned long flags) size_t size, unsigned long flags)
{ {
struct nfit_test_resource *nfit_res; struct nfit_test_resource *nfit_res;
@ -91,9 +91,9 @@ void *__wrap_memremap(resource_size_t offset, size_t size,
if (nfit_res) if (nfit_res)
return (void __iomem *) nfit_res->buf + offset return (void __iomem *) nfit_res->buf + offset
- nfit_res->res->start; - nfit_res->res->start;
return memremap(offset, size, flags); return devm_memremap(dev, offset, size, flags);
} }
EXPORT_SYMBOL(__wrap_memremap); EXPORT_SYMBOL(__wrap_devm_memremap);
void __iomem *__wrap_ioremap_nocache(resource_size_t offset, unsigned long size) void __iomem *__wrap_ioremap_nocache(resource_size_t offset, unsigned long size)
{ {
@ -120,22 +120,9 @@ void __wrap_iounmap(volatile void __iomem *addr)
} }
EXPORT_SYMBOL(__wrap_iounmap); EXPORT_SYMBOL(__wrap_iounmap);
void __wrap_memunmap(void *addr) static struct resource *nfit_test_request_region(struct device *dev,
{ struct resource *parent, resource_size_t start,
struct nfit_test_resource *nfit_res; resource_size_t n, const char *name, int flags)
rcu_read_lock();
nfit_res = get_nfit_res((unsigned long) addr);
rcu_read_unlock();
if (nfit_res)
return;
return memunmap(addr);
}
EXPORT_SYMBOL(__wrap_memunmap);
struct resource *__wrap___request_region(struct resource *parent,
resource_size_t start, resource_size_t n, const char *name,
int flags)
{ {
struct nfit_test_resource *nfit_res; struct nfit_test_resource *nfit_res;
@ -163,10 +150,29 @@ struct resource *__wrap___request_region(struct resource *parent,
return res; return res;
} }
} }
if (dev)
return __devm_request_region(dev, parent, start, n, name);
return __request_region(parent, start, n, name, flags); return __request_region(parent, start, n, name, flags);
} }
struct resource *__wrap___request_region(struct resource *parent,
resource_size_t start, resource_size_t n, const char *name,
int flags)
{
return nfit_test_request_region(NULL, parent, start, n, name, flags);
}
EXPORT_SYMBOL(__wrap___request_region); EXPORT_SYMBOL(__wrap___request_region);
struct resource *__wrap___devm_request_region(struct device *dev,
struct resource *parent, resource_size_t start,
resource_size_t n, const char *name)
{
if (!dev)
return NULL;
return nfit_test_request_region(dev, parent, start, n, name, 0);
}
EXPORT_SYMBOL(__wrap___devm_request_region);
void __wrap___release_region(struct resource *parent, resource_size_t start, void __wrap___release_region(struct resource *parent, resource_size_t start,
resource_size_t n) resource_size_t n)
{ {