Commit 89ec9f2c authored by Dan Williams's avatar Dan Williams

device-dax: Move resource pinning+mapping into the common driver

Move the responsibility of calling devm_request_resource() and
devm_memremap_pages() into the common device-dax driver. This is another
preparatory step to allowing an alternate personality driver for a
device-dax range.
Signed-off-by: default avatarDan Williams <dan.j.williams@intel.com>
parent 9567da0b
// SPDX-License-Identifier: GPL-2.0 // SPDX-License-Identifier: GPL-2.0
/* Copyright(c) 2017-2018 Intel Corporation. All rights reserved. */ /* Copyright(c) 2017-2018 Intel Corporation. All rights reserved. */
#include <linux/memremap.h>
#include <linux/device.h> #include <linux/device.h>
#include <linux/slab.h> #include <linux/slab.h>
#include <linux/dax.h> #include <linux/dax.h>
...@@ -206,7 +207,8 @@ static void unregister_dev_dax(void *dev) ...@@ -206,7 +207,8 @@ static void unregister_dev_dax(void *dev)
put_device(dev); put_device(dev);
} }
struct dev_dax *devm_create_dev_dax(struct dax_region *dax_region, int id) struct dev_dax *devm_create_dev_dax(struct dax_region *dax_region, int id,
struct dev_pagemap *pgmap)
{ {
struct device *parent = dax_region->dev; struct device *parent = dax_region->dev;
struct dax_device *dax_dev; struct dax_device *dax_dev;
...@@ -222,6 +224,8 @@ struct dev_dax *devm_create_dev_dax(struct dax_region *dax_region, int id) ...@@ -222,6 +224,8 @@ struct dev_dax *devm_create_dev_dax(struct dax_region *dax_region, int id)
if (!dev_dax) if (!dev_dax)
return ERR_PTR(-ENOMEM); return ERR_PTR(-ENOMEM);
memcpy(&dev_dax->pgmap, pgmap, sizeof(*pgmap));
/* /*
* No 'host' or dax_operations since there is no access to this * No 'host' or dax_operations since there is no access to this
* device outside of mmap of the resulting character device. * device outside of mmap of the resulting character device.
......
...@@ -10,7 +10,8 @@ struct dax_region; ...@@ -10,7 +10,8 @@ struct dax_region;
void dax_region_put(struct dax_region *dax_region); void dax_region_put(struct dax_region *dax_region);
struct dax_region *alloc_dax_region(struct device *parent, int region_id, struct dax_region *alloc_dax_region(struct device *parent, int region_id,
struct resource *res, unsigned int align, unsigned long flags); struct resource *res, unsigned int align, unsigned long flags);
struct dev_dax *devm_create_dev_dax(struct dax_region *dax_region, int id); struct dev_dax *devm_create_dev_dax(struct dax_region *dax_region, int id,
struct dev_pagemap *pgmap);
int __dax_driver_register(struct device_driver *drv, int __dax_driver_register(struct device_driver *drv,
struct module *module, const char *mod_name); struct module *module, const char *mod_name);
#define dax_driver_register(driver) \ #define dax_driver_register(driver) \
......
...@@ -42,15 +42,22 @@ struct dax_region { ...@@ -42,15 +42,22 @@ struct dax_region {
}; };
/** /**
* struct dev_dax - instance data for a subdivision of a dax region * struct dev_dax - instance data for a subdivision of a dax region, and
* data while the device is activated in the driver.
* @region - parent region * @region - parent region
* @dax_dev - core dax functionality * @dax_dev - core dax functionality
* @dev - device core * @dev - device core
* @pgmap - pgmap for memmap setup / lifetime (driver owned)
* @ref: pgmap reference count (driver owned)
* @cmp: @ref final put completion (driver owned)
*/ */
struct dev_dax { struct dev_dax {
struct dax_region *region; struct dax_region *region;
struct dax_device *dax_dev; struct dax_device *dax_dev;
struct device dev; struct device dev;
struct dev_pagemap pgmap;
struct percpu_ref ref;
struct completion cmp;
}; };
static inline struct dev_dax *to_dev_dax(struct device *dev) static inline struct dev_dax *to_dev_dax(struct device *dev)
......
// SPDX-License-Identifier: GPL-2.0 // SPDX-License-Identifier: GPL-2.0
/* Copyright(c) 2016-2018 Intel Corporation. All rights reserved. */ /* Copyright(c) 2016-2018 Intel Corporation. All rights reserved. */
#include <linux/memremap.h>
#include <linux/pagemap.h> #include <linux/pagemap.h>
#include <linux/module.h> #include <linux/module.h>
#include <linux/device.h> #include <linux/device.h>
...@@ -13,6 +14,38 @@ ...@@ -13,6 +14,38 @@
#include "dax-private.h" #include "dax-private.h"
#include "bus.h" #include "bus.h"
static struct dev_dax *ref_to_dev_dax(struct percpu_ref *ref)
{
return container_of(ref, struct dev_dax, ref);
}
static void dev_dax_percpu_release(struct percpu_ref *ref)
{
struct dev_dax *dev_dax = ref_to_dev_dax(ref);
dev_dbg(&dev_dax->dev, "%s\n", __func__);
complete(&dev_dax->cmp);
}
static void dev_dax_percpu_exit(void *data)
{
struct percpu_ref *ref = data;
struct dev_dax *dev_dax = ref_to_dev_dax(ref);
dev_dbg(&dev_dax->dev, "%s\n", __func__);
wait_for_completion(&dev_dax->cmp);
percpu_ref_exit(ref);
}
static void dev_dax_percpu_kill(struct percpu_ref *data)
{
struct percpu_ref *ref = data;
struct dev_dax *dev_dax = ref_to_dev_dax(ref);
dev_dbg(&dev_dax->dev, "%s\n", __func__);
percpu_ref_kill(ref);
}
static int check_vma(struct dev_dax *dev_dax, struct vm_area_struct *vma, static int check_vma(struct dev_dax *dev_dax, struct vm_area_struct *vma,
const char *func) const char *func)
{ {
...@@ -416,10 +449,38 @@ static int dev_dax_probe(struct device *dev) ...@@ -416,10 +449,38 @@ static int dev_dax_probe(struct device *dev)
{ {
struct dev_dax *dev_dax = to_dev_dax(dev); struct dev_dax *dev_dax = to_dev_dax(dev);
struct dax_device *dax_dev = dev_dax->dax_dev; struct dax_device *dax_dev = dev_dax->dax_dev;
struct resource *res = &dev_dax->region->res;
struct inode *inode; struct inode *inode;
struct cdev *cdev; struct cdev *cdev;
void *addr;
int rc; int rc;
/* 1:1 map region resource range to device-dax instance range */
if (!devm_request_mem_region(dev, res->start, resource_size(res),
dev_name(dev))) {
dev_warn(dev, "could not reserve region %pR\n", res);
return -EBUSY;
}
init_completion(&dev_dax->cmp);
rc = percpu_ref_init(&dev_dax->ref, dev_dax_percpu_release, 0,
GFP_KERNEL);
if (rc)
return rc;
rc = devm_add_action_or_reset(dev, dev_dax_percpu_exit, &dev_dax->ref);
if (rc)
return rc;
dev_dax->pgmap.ref = &dev_dax->ref;
dev_dax->pgmap.kill = dev_dax_percpu_kill;
addr = devm_memremap_pages(dev, &dev_dax->pgmap);
if (IS_ERR(addr)) {
devm_remove_action(dev, dev_dax_percpu_exit, &dev_dax->ref);
percpu_ref_exit(&dev_dax->ref);
return PTR_ERR(addr);
}
inode = dax_inode(dax_dev); inode = dax_inode(dax_dev);
cdev = inode->i_cdev; cdev = inode->i_cdev;
cdev_init(cdev, &dax_fops); cdev_init(cdev, &dax_fops);
......
...@@ -18,54 +18,16 @@ ...@@ -18,54 +18,16 @@
#include "../nvdimm/nd.h" #include "../nvdimm/nd.h"
#include "bus.h" #include "bus.h"
struct dax_pmem {
struct device *dev;
struct percpu_ref ref;
struct dev_pagemap pgmap;
struct completion cmp;
};
static struct dax_pmem *to_dax_pmem(struct percpu_ref *ref)
{
return container_of(ref, struct dax_pmem, ref);
}
static void dax_pmem_percpu_release(struct percpu_ref *ref)
{
struct dax_pmem *dax_pmem = to_dax_pmem(ref);
dev_dbg(dax_pmem->dev, "trace\n");
complete(&dax_pmem->cmp);
}
static void dax_pmem_percpu_exit(void *data)
{
struct percpu_ref *ref = data;
struct dax_pmem *dax_pmem = to_dax_pmem(ref);
dev_dbg(dax_pmem->dev, "trace\n");
wait_for_completion(&dax_pmem->cmp);
percpu_ref_exit(ref);
}
static void dax_pmem_percpu_kill(struct percpu_ref *ref)
{
struct dax_pmem *dax_pmem = to_dax_pmem(ref);
dev_dbg(dax_pmem->dev, "trace\n");
percpu_ref_kill(ref);
}
static int dax_pmem_probe(struct device *dev) static int dax_pmem_probe(struct device *dev)
{ {
void *addr;
struct resource res; struct resource res;
int rc, id, region_id; int rc, id, region_id;
resource_size_t offset;
struct nd_pfn_sb *pfn_sb; struct nd_pfn_sb *pfn_sb;
struct dev_dax *dev_dax; struct dev_dax *dev_dax;
struct dax_pmem *dax_pmem;
struct nd_namespace_io *nsio; struct nd_namespace_io *nsio;
struct dax_region *dax_region; struct dax_region *dax_region;
struct dev_pagemap pgmap = { 0 };
struct nd_namespace_common *ndns; struct nd_namespace_common *ndns;
struct nd_dax *nd_dax = to_nd_dax(dev); struct nd_dax *nd_dax = to_nd_dax(dev);
struct nd_pfn *nd_pfn = &nd_dax->nd_pfn; struct nd_pfn *nd_pfn = &nd_dax->nd_pfn;
...@@ -75,61 +37,37 @@ static int dax_pmem_probe(struct device *dev) ...@@ -75,61 +37,37 @@ static int dax_pmem_probe(struct device *dev)
return PTR_ERR(ndns); return PTR_ERR(ndns);
nsio = to_nd_namespace_io(&ndns->dev); nsio = to_nd_namespace_io(&ndns->dev);
dax_pmem = devm_kzalloc(dev, sizeof(*dax_pmem), GFP_KERNEL);
if (!dax_pmem)
return -ENOMEM;
/* parse the 'pfn' info block via ->rw_bytes */ /* parse the 'pfn' info block via ->rw_bytes */
rc = devm_nsio_enable(dev, nsio); rc = devm_nsio_enable(dev, nsio);
if (rc) if (rc)
return rc; return rc;
rc = nvdimm_setup_pfn(nd_pfn, &dax_pmem->pgmap); rc = nvdimm_setup_pfn(nd_pfn, &pgmap);
if (rc) if (rc)
return rc; return rc;
devm_nsio_disable(dev, nsio); devm_nsio_disable(dev, nsio);
pfn_sb = nd_pfn->pfn_sb; /* reserve the metadata area, device-dax will reserve the data */
pfn_sb = nd_pfn->pfn_sb;
if (!devm_request_mem_region(dev, nsio->res.start, offset = le64_to_cpu(pfn_sb->dataoff);
resource_size(&nsio->res), if (!devm_request_mem_region(dev, nsio->res.start, offset,
dev_name(&ndns->dev))) { dev_name(&ndns->dev))) {
dev_warn(dev, "could not reserve region %pR\n", &nsio->res); dev_warn(dev, "could not reserve metadata\n");
return -EBUSY; return -EBUSY;
} }
dax_pmem->dev = dev;
init_completion(&dax_pmem->cmp);
rc = percpu_ref_init(&dax_pmem->ref, dax_pmem_percpu_release, 0,
GFP_KERNEL);
if (rc)
return rc;
rc = devm_add_action(dev, dax_pmem_percpu_exit, &dax_pmem->ref);
if (rc) {
percpu_ref_exit(&dax_pmem->ref);
return rc;
}
dax_pmem->pgmap.ref = &dax_pmem->ref;
dax_pmem->pgmap.kill = dax_pmem_percpu_kill;
addr = devm_memremap_pages(dev, &dax_pmem->pgmap);
if (IS_ERR(addr))
return PTR_ERR(addr);
/* adjust the dax_region resource to the start of data */
memcpy(&res, &dax_pmem->pgmap.res, sizeof(res));
res.start += le64_to_cpu(pfn_sb->dataoff);
rc = sscanf(dev_name(&ndns->dev), "namespace%d.%d", &region_id, &id); rc = sscanf(dev_name(&ndns->dev), "namespace%d.%d", &region_id, &id);
if (rc != 2) if (rc != 2)
return -EINVAL; return -EINVAL;
/* adjust the dax_region resource to the start of data */
memcpy(&res, &pgmap.res, sizeof(res));
res.start += offset;
dax_region = alloc_dax_region(dev, region_id, &res, dax_region = alloc_dax_region(dev, region_id, &res,
le32_to_cpu(pfn_sb->align), PFN_DEV|PFN_MAP); le32_to_cpu(pfn_sb->align), PFN_DEV|PFN_MAP);
if (!dax_region) if (!dax_region)
return -ENOMEM; return -ENOMEM;
dev_dax = devm_create_dev_dax(dax_region, id); dev_dax = devm_create_dev_dax(dax_region, id, &pgmap);
/* child dev_dax instances now own the lifetime of the dax_region */ /* child dev_dax instances now own the lifetime of the dax_region */
dax_region_put(dax_region); dax_region_put(dax_region);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment