Skip to content

Commit

Permalink
Merge tag 'libnvdimm-for-6.5' of git://git.kernel.org/pub/scm/linux/k…
Browse files Browse the repository at this point in the history
…ernel/git/nvdimm/nvdimm

Pull nvdimm and DAX updates from Vishal Verma:
 "This is mostly small cleanups and fixes, with the biggest change being
  the change to the DAX fault handler allowing it to return
  VM_FAULT_HWPOISON.

  Summary:

   - DAX fixes and cleanups including a use after free, extra
     references, and device unregistration, and a redundant variable.

   - Allow the DAX fault handler to return VM_FAULT_HWPOISON

   - A few libnvdimm cleanups such as making some functions and
     variables static where sufficient.

   - Add a few missing prototypes for wrapped functions in
     tools/testing/nvdimm"

* tag 'libnvdimm-for-6.5' of git://git.kernel.org/pub/scm/linux/kernel/git/nvdimm/nvdimm:
  dax: enable dax fault handler to report VM_FAULT_HWPOISON
  nvdimm: make security_show static
  nvdimm: make nd_class variable static
  dax/kmem: Pass valid argument to memory_group_register_static
  fsdax: remove redundant variable 'error'
  dax: Cleanup extra dax_region references
  dax: Introduce alloc_dev_dax_id()
  dax: Use device_unregister() in unregister_dax_mapping()
  dax: Fix dax_mapping_release() use after free
  tools/testing/nvdimm: Drop empty platform remove function
  libnvdimm: mark 'security_show' static again
  testing: nvdimm: add missing prototypes for wrapped functions
  dax: fix missing-prototype warnings
  • Loading branch information
torvalds committed Jul 1, 2023
2 parents be21a73 + 1ea7ca1 commit 0a1c979
Show file tree
Hide file tree
Showing 19 changed files with 116 additions and 78 deletions.
64 changes: 38 additions & 26 deletions drivers/dax/bus.c
Original file line number Diff line number Diff line change
Expand Up @@ -446,18 +446,33 @@ static void unregister_dev_dax(void *dev)
put_device(dev);
}

static void dax_region_free(struct kref *kref)
{
struct dax_region *dax_region;

dax_region = container_of(kref, struct dax_region, kref);
kfree(dax_region);
}

static void dax_region_put(struct dax_region *dax_region)
{
kref_put(&dax_region->kref, dax_region_free);
}

/* a return value >= 0 indicates this invocation invalidated the id */
static int __free_dev_dax_id(struct dev_dax *dev_dax)
{
struct dax_region *dax_region = dev_dax->region;
struct device *dev = &dev_dax->dev;
struct dax_region *dax_region;
int rc = dev_dax->id;

device_lock_assert(dev);

if (is_static(dax_region) || dev_dax->id < 0)
if (!dev_dax->dyn_id || dev_dax->id < 0)
return -1;
dax_region = dev_dax->region;
ida_free(&dax_region->ida, dev_dax->id);
dax_region_put(dax_region);
dev_dax->id = -1;
return rc;
}
Expand All @@ -473,6 +488,20 @@ static int free_dev_dax_id(struct dev_dax *dev_dax)
return rc;
}

static int alloc_dev_dax_id(struct dev_dax *dev_dax)
{
struct dax_region *dax_region = dev_dax->region;
int id;

id = ida_alloc(&dax_region->ida, GFP_KERNEL);
if (id < 0)
return id;
kref_get(&dax_region->kref);
dev_dax->dyn_id = true;
dev_dax->id = id;
return id;
}

static ssize_t delete_store(struct device *dev, struct device_attribute *attr,
const char *buf, size_t len)
{
Expand Down Expand Up @@ -560,20 +589,6 @@ static const struct attribute_group *dax_region_attribute_groups[] = {
NULL,
};

static void dax_region_free(struct kref *kref)
{
struct dax_region *dax_region;

dax_region = container_of(kref, struct dax_region, kref);
kfree(dax_region);
}

void dax_region_put(struct dax_region *dax_region)
{
kref_put(&dax_region->kref, dax_region_free);
}
EXPORT_SYMBOL_GPL(dax_region_put);

static void dax_region_unregister(void *region)
{
struct dax_region *dax_region = region;
Expand Down Expand Up @@ -625,7 +640,6 @@ struct dax_region *alloc_dax_region(struct device *parent, int region_id,
return NULL;
}

kref_get(&dax_region->kref);
if (devm_add_action_or_reset(parent, dax_region_unregister, dax_region))
return NULL;
return dax_region;
Expand All @@ -635,10 +649,12 @@ EXPORT_SYMBOL_GPL(alloc_dax_region);
static void dax_mapping_release(struct device *dev)
{
struct dax_mapping *mapping = to_dax_mapping(dev);
struct dev_dax *dev_dax = to_dev_dax(dev->parent);
struct device *parent = dev->parent;
struct dev_dax *dev_dax = to_dev_dax(parent);

ida_free(&dev_dax->ida, mapping->id);
kfree(mapping);
put_device(parent);
}

static void unregister_dax_mapping(void *data)
Expand All @@ -655,8 +671,7 @@ static void unregister_dax_mapping(void *data)
dev_dax->ranges[mapping->range_id].mapping = NULL;
mapping->range_id = -1;

device_del(dev);
put_device(dev);
device_unregister(dev);
}

static struct dev_dax_range *get_dax_range(struct device *dev)
Expand Down Expand Up @@ -778,6 +793,7 @@ static int devm_register_dax_mapping(struct dev_dax *dev_dax, int range_id)
dev = &mapping->dev;
device_initialize(dev);
dev->parent = &dev_dax->dev;
get_device(dev->parent);
dev->type = &dax_mapping_type;
dev_set_name(dev, "mapping%d", mapping->id);
rc = device_add(dev);
Expand Down Expand Up @@ -1295,12 +1311,10 @@ static const struct attribute_group *dax_attribute_groups[] = {
static void dev_dax_release(struct device *dev)
{
struct dev_dax *dev_dax = to_dev_dax(dev);
struct dax_region *dax_region = dev_dax->region;
struct dax_device *dax_dev = dev_dax->dax_dev;

put_dax(dax_dev);
free_dev_dax_id(dev_dax);
dax_region_put(dax_region);
kfree(dev_dax->pgmap);
kfree(dev_dax);
}
Expand All @@ -1324,6 +1338,7 @@ struct dev_dax *devm_create_dev_dax(struct dev_dax_data *data)
if (!dev_dax)
return ERR_PTR(-ENOMEM);

dev_dax->region = dax_region;
if (is_static(dax_region)) {
if (dev_WARN_ONCE(parent, data->id < 0,
"dynamic id specified to static region\n")) {
Expand All @@ -1339,13 +1354,11 @@ struct dev_dax *devm_create_dev_dax(struct dev_dax_data *data)
goto err_id;
}

rc = ida_alloc(&dax_region->ida, GFP_KERNEL);
rc = alloc_dev_dax_id(dev_dax);
if (rc < 0)
goto err_id;
dev_dax->id = rc;
}

dev_dax->region = dax_region;
dev = &dev_dax->dev;
device_initialize(dev);
dev_set_name(dev, "dax%d.%d", dax_region->id, dev_dax->id);
Expand Down Expand Up @@ -1386,7 +1399,6 @@ struct dev_dax *devm_create_dev_dax(struct dev_dax_data *data)
dev_dax->target_node = dax_region->target_node;
dev_dax->align = dax_region->align;
ida_init(&dev_dax->ida);
kref_get(&dax_region->kref);

inode = dax_inode(dax_dev);
dev->devt = inode->i_rdev;
Expand Down
8 changes: 0 additions & 8 deletions drivers/dax/bus.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ struct dev_dax;
struct resource;
struct dax_device;
struct dax_region;
void dax_region_put(struct dax_region *dax_region);

/* dax bus specific ioresource flags */
#define IORESOURCE_DAX_STATIC BIT(0)
Expand Down Expand Up @@ -49,13 +48,6 @@ void dax_driver_unregister(struct dax_device_driver *dax_drv);
void kill_dev_dax(struct dev_dax *dev_dax);
bool static_dev_dax(struct dev_dax *dev_dax);

/*
* While run_dax() is potentially a generic operation that could be
* defined in include/linux/dax.h we don't want to grow any users
* outside of drivers/dax/
*/
void run_dax(struct dax_device *dax_dev);

#define MODULE_ALIAS_DAX_DEVICE(type) \
MODULE_ALIAS("dax:t" __stringify(type) "*")
#define DAX_DEVICE_MODALIAS_FMT "dax:t%d"
Expand Down
8 changes: 1 addition & 7 deletions drivers/dax/cxl.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ static int cxl_dax_region_probe(struct device *dev)
struct cxl_region *cxlr = cxlr_dax->cxlr;
struct dax_region *dax_region;
struct dev_dax_data data;
struct dev_dax *dev_dax;

if (nid == NUMA_NO_NODE)
nid = memory_add_physaddr_to_nid(cxlr_dax->hpa_range.start);
Expand All @@ -28,13 +27,8 @@ static int cxl_dax_region_probe(struct device *dev)
.id = -1,
.size = range_len(&cxlr_dax->hpa_range),
};
dev_dax = devm_create_dev_dax(&data);
if (IS_ERR(dev_dax))
return PTR_ERR(dev_dax);

/* child dev_dax instances now own the lifetime of the dax_region */
dax_region_put(dax_region);
return 0;
return PTR_ERR_OR_ZERO(devm_create_dev_dax(&data));
}

static struct cxl_driver cxl_dax_region_driver = {
Expand Down
11 changes: 10 additions & 1 deletion drivers/dax/dax-private.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ struct dax_mapping {
* @region - parent region
* @dax_dev - core dax functionality
* @target_node: effective numa node if dev_dax memory range is onlined
* @id: ida allocated id
* @dyn_id: is this a dynamic or statically created instance
* @id: ida allocated id when the dax_region is not static
* @ida: mapping id allocator
* @dev - device core
* @pgmap - pgmap for memmap setup / lifetime (driver owned)
Expand All @@ -64,6 +65,7 @@ struct dev_dax {
struct dax_device *dax_dev;
unsigned int align;
int target_node;
bool dyn_id;
int id;
struct ida ida;
struct device dev;
Expand All @@ -76,6 +78,13 @@ struct dev_dax {
} *ranges;
};

/*
* While run_dax() is potentially a generic operation that could be
* defined in include/linux/dax.h we don't want to grow any users
* outside of drivers/dax/
*/
void run_dax(struct dax_device *dax_dev);

static inline struct dev_dax *to_dev_dax(struct device *dev)
{
return container_of(dev, struct dev_dax, dev);
Expand Down
3 changes: 1 addition & 2 deletions drivers/dax/device.c
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,7 @@ static void dev_dax_kill(void *dev_dax)
kill_dev_dax(dev_dax);
}

int dev_dax_probe(struct dev_dax *dev_dax)
static int dev_dax_probe(struct dev_dax *dev_dax)
{
struct dax_device *dax_dev = dev_dax->dax_dev;
struct device *dev = &dev_dax->dev;
Expand Down Expand Up @@ -471,7 +471,6 @@ int dev_dax_probe(struct dev_dax *dev_dax)
run_dax(dax_dev);
return devm_add_action_or_reset(dev, dev_dax_kill, dev_dax);
}
EXPORT_SYMBOL_GPL(dev_dax_probe);

static struct dax_device_driver device_dax_driver = {
.probe = dev_dax_probe,
Expand Down
8 changes: 1 addition & 7 deletions drivers/dax/hmem/hmem.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ static int dax_hmem_probe(struct platform_device *pdev)
struct dax_region *dax_region;
struct memregion_info *mri;
struct dev_dax_data data;
struct dev_dax *dev_dax;

/*
* @region_idle == true indicates that an administrative agent
Expand All @@ -38,13 +37,8 @@ static int dax_hmem_probe(struct platform_device *pdev)
.id = -1,
.size = region_idle ? 0 : range_len(&mri->range),
};
dev_dax = devm_create_dev_dax(&data);
if (IS_ERR(dev_dax))
return PTR_ERR(dev_dax);

/* child dev_dax instances now own the lifetime of the dax_region */
dax_region_put(dax_region);
return 0;
return PTR_ERR_OR_ZERO(devm_create_dev_dax(&data));
}

static struct platform_driver dax_hmem_driver = {
Expand Down
2 changes: 1 addition & 1 deletion drivers/dax/kmem.c
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ static int dev_dax_kmem_probe(struct dev_dax *dev_dax)
if (!data->res_name)
goto err_res_name;

rc = memory_group_register_static(numa_node, total_len);
rc = memory_group_register_static(numa_node, PFN_UP(total_len));
if (rc < 0)
goto err_reg_mgid;
data->mgid = rc;
Expand Down
7 changes: 1 addition & 6 deletions drivers/dax/pmem.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ static struct dev_dax *__dax_pmem_probe(struct device *dev)
int rc, id, region_id;
resource_size_t offset;
struct nd_pfn_sb *pfn_sb;
struct dev_dax *dev_dax;
struct dev_dax_data data;
struct nd_namespace_io *nsio;
struct dax_region *dax_region;
Expand Down Expand Up @@ -65,12 +64,8 @@ static struct dev_dax *__dax_pmem_probe(struct device *dev)
.pgmap = &pgmap,
.size = range_len(&range),
};
dev_dax = devm_create_dev_dax(&data);

/* child dev_dax instances now own the lifetime of the dax_region */
dax_region_put(dax_region);

return dev_dax;
return devm_create_dev_dax(&data);
}

static int dax_pmem_probe(struct device *dev)
Expand Down
5 changes: 4 additions & 1 deletion drivers/dax/super.c
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,8 @@ size_t dax_copy_to_iter(struct dax_device *dax_dev, pgoff_t pgoff, void *addr,
int dax_zero_page_range(struct dax_device *dax_dev, pgoff_t pgoff,
size_t nr_pages)
{
int ret;

if (!dax_alive(dax_dev))
return -ENXIO;
/*
Expand All @@ -213,7 +215,8 @@ int dax_zero_page_range(struct dax_device *dax_dev, pgoff_t pgoff,
if (nr_pages != 1)
return -EIO;

return dax_dev->ops->zero_page_range(dax_dev, pgoff, nr_pages);
ret = dax_dev->ops->zero_page_range(dax_dev, pgoff, nr_pages);
return dax_mem2blk_err(ret);
}
EXPORT_SYMBOL_GPL(dax_zero_page_range);

Expand Down
2 changes: 1 addition & 1 deletion drivers/nvdimm/bus.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

int nvdimm_major;
static int nvdimm_bus_major;
struct class *nd_class;
static struct class *nd_class;
static DEFINE_IDA(nd_ida);

static int to_nd_device_type(const struct device *dev)
Expand Down
4 changes: 2 additions & 2 deletions drivers/nvdimm/dimm_devs.c
Original file line number Diff line number Diff line change
Expand Up @@ -349,8 +349,8 @@ static ssize_t available_slots_show(struct device *dev,
}
static DEVICE_ATTR_RO(available_slots);

ssize_t security_show(struct device *dev,
struct device_attribute *attr, char *buf)
static ssize_t security_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
struct nvdimm *nvdimm = to_nvdimm(dev);

Expand Down
2 changes: 1 addition & 1 deletion drivers/nvdimm/pmem.c
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ __weak long __pmem_direct_access(struct pmem_device *pmem, pgoff_t pgoff,
long actual_nr;

if (mode != DAX_RECOVERY_WRITE)
return -EIO;
return -EHWPOISON;

/*
* Set the recovery stride is set to kernel page size because
Expand Down
3 changes: 2 additions & 1 deletion drivers/s390/block/dcssblk.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ static int dcssblk_dax_zero_page_range(struct dax_device *dax_dev,
rc = dax_direct_access(dax_dev, pgoff, nr_pages, DAX_ACCESS,
&kaddr, NULL);
if (rc < 0)
return rc;
return dax_mem2blk_err(rc);

memset(kaddr, 0, nr_pages << PAGE_SHIFT);
dax_flush(dax_dev, kaddr, nr_pages << PAGE_SHIFT);
return 0;
Expand Down
Loading

0 comments on commit 0a1c979

Please sign in to comment.