// SPDX-License-Identifier: GPL-2.0/* Copyright(c) 2017-2018 Intel Corporation. All rights reserved. */#include <linux/memremap.h>#include <linux/device.h>#include <linux/mutex.h>#include <linux/list.h>#include <linux/slab.h>#include <linux/dax.h>#include <linux/io.h>#include "dax-private.h"#include "bus.h" static struct resource dax_regions = DEFINE_RES_MEM_NAMED(0, -1, "DAX Regions");static DEFINE_MUTEX(dax_bus_lock); /* * All changes to the dax region configuration occur with this lock held * for write. */DECLARE_RWSEM(dax_region_rwsem); /* * All changes to the dax device configuration occur with this lock held * for write. */DECLARE_RWSEM(dax_dev_rwsem); #define DAX_NAME_LEN 30struct dax_id { struct list_head list; char dev_name[DAX_NAME_LEN];}; static int dax_bus_uevent(const struct device *dev, struct kobj_uevent_env *env){ /* * We only ever expect to handle device-dax instances, i.e. the * @type argument to MODULE_ALIAS_DAX_DEVICE() is always zero */ return add_uevent_var(env, "MODALIAS=" DAX_DEVICE_MODALIAS_FMT, 0);} static struct dax_id *__dax_match_id(const struct dax_device_driver *dax_drv, const char *dev_name){ struct dax_id *dax_id; lockdep_assert_held(&dax_bus_lock); list_for_each_entry(dax_id, &dax_drv->ids, list) if (sysfs_streq(dax_id->dev_name, dev_name)) return dax_id; return NULL;} static int dax_match_id(const struct dax_device_driver *dax_drv, struct device *dev){ int match; mutex_lock(&dax_bus_lock); match = !!__dax_match_id(dax_drv, dev_name(dev)); mutex_unlock(&dax_bus_lock); return match;} static int dax_match_type(const struct dax_device_driver *dax_drv, struct device *dev){ enum dax_driver_type type = DAXDRV_DEVICE_TYPE; struct dev_dax *dev_dax = to_dev_dax(dev); if (dev_dax->region->res.flags & IORESOURCE_DAX_KMEM) type = DAXDRV_KMEM_TYPE; if (dax_drv->type == type) return 1; /* default to device mode if dax_kmem is disabled */ if (dax_drv->type == DAXDRV_DEVICE_TYPE && !IS_ENABLED(CONFIG_DEV_DAX_KMEM)) return 1; return 0;} enum id_action { ID_REMOVE, ID_ADD,}; static ssize_t do_id_store(struct device_driver *drv, const char *buf, size_t count, enum id_action action){ struct dax_device_driver *dax_drv = to_dax_drv(drv); unsigned int region_id, id; char devname[DAX_NAME_LEN]; struct dax_id *dax_id; ssize_t rc = count; int fields; fields = sscanf(buf, "dax%d.%d", ®ion_id, &id); if (fields != 2) return -EINVAL; sprintf(devname, "dax%d.%d", region_id, id); if (!sysfs_streq(buf, devname)) return -EINVAL; mutex_lock(&dax_bus_lock); dax_id = __dax_match_id(dax_drv, buf); if (!dax_id) { if (action == ID_ADD) { dax_id = kzalloc_obj(*dax_id); if (dax_id) { strscpy(dax_id->dev_name, buf, DAX_NAME_LEN); list_add(&dax_id->list, &dax_drv->ids); } else rc = -ENOMEM; } } else if (action == ID_REMOVE) { list_del(&dax_id->list); kfree(dax_id); } mutex_unlock(&dax_bus_lock); if (rc < 0) return rc; if (action == ID_ADD) rc = driver_attach(drv); if (rc) return rc; return count;} static ssize_t new_id_store(struct device_driver *drv, const char *buf, size_t count){ return do_id_store(drv, buf, count, ID_ADD);}static DRIVER_ATTR_WO(new_id); static ssize_t remove_id_store(struct device_driver *drv, const char *buf, size_t count){ return do_id_store(drv, buf, count, ID_REMOVE);}static DRIVER_ATTR_WO(remove_id); static struct attribute *dax_drv_attrs[] = { &driver_attr_new_id.attr, &driver_attr_remove_id.attr, NULL,};ATTRIBUTE_GROUPS(dax_drv); static int dax_bus_match(struct device *dev, const struct device_driver *drv); /* * Static dax regions are regions created by an external subsystem * nvdimm where a single range is assigned. Its boundaries are by the external * subsystem and are usually limited to one physical memory range. For example, * for PMEM it is usually defined by NVDIMM Namespace boundaries (i.e. a * single contiguous range) * * On dynamic dax regions, the assigned region can be partitioned by dax core * into multiple subdivisions. A subdivision is represented into one * /dev/daxN.M device composed by one or more potentially discontiguous ranges. * * When allocating a dax region, drivers must set whether it's static * (IORESOURCE_DAX_STATIC). On static dax devices, the @pgmap is pre-assigned * to dax core when calling devm_create_dev_dax(), whereas in dynamic dax * devices it is NULL but afterwards allocated by dax core on device ->probe(). * Care is needed to make sure that dynamic dax devices are torn down with a * cleared @pgmap field (see kill_dev_dax()). */static bool is_static(struct dax_region *dax_region){ return (dax_region->res.flags & IORESOURCE_DAX_STATIC) != 0;} bool static_dev_dax(struct dev_dax *dev_dax){ return is_static(dev_dax->region);}EXPORT_SYMBOL_GPL(static_dev_dax); static u64 dev_dax_size(struct dev_dax *dev_dax){ u64 size = 0; int i; lockdep_assert_held(&dax_dev_rwsem); for (i = 0; i < dev_dax->nr_range; i++) size += range_len(&dev_dax->ranges[i].range); return size;} static int dax_bus_probe(struct device *dev){ struct dax_device_driver *dax_drv = to_dax_drv(dev->driver); struct dev_dax *dev_dax = to_dev_dax(dev); struct dax_region *dax_region = dev_dax->region; int rc; u64 size; rc = down_read_interruptible(&dax_dev_rwsem); if (rc) return rc; size = dev_dax_size(dev_dax); up_read(&dax_dev_rwsem); if (size == 0 || dev_dax->id < 0) return -ENXIO; rc = dax_drv->probe(dev_dax); if (rc || is_static(dax_region)) return rc; /* * Track new seed creation only after successful probe of the * previous seed. */ if (dax_region->seed == dev) dax_region->seed = NULL; return 0;} static void dax_bus_remove(struct device *dev){ struct dax_device_driver *dax_drv = to_dax_drv(dev->driver); struct dev_dax *dev_dax = to_dev_dax(dev); if (dax_drv->remove) dax_drv->remove(dev_dax);} static const struct bus_type dax_bus_type = { .name = "dax", .uevent = dax_bus_uevent, .match = dax_bus_match, .probe = dax_bus_probe, .remove = dax_bus_remove, .drv_groups = dax_drv_groups,}; static int dax_bus_match(struct device *dev, const struct device_driver *drv){ const struct dax_device_driver *dax_drv = to_dax_drv(drv); if (dax_match_id(dax_drv, dev)) return 1; return dax_match_type(dax_drv, dev);} /* * Rely on the fact that drvdata is set before the attributes are * registered, and that the attributes are unregistered before drvdata * is cleared to assume that drvdata is always valid. */static ssize_t id_show(struct device *dev, struct device_attribute *attr, char *buf){ struct dax_region *dax_region = dev_get_drvdata(dev); return sysfs_emit(buf, "%d\n", dax_region->id);}static DEVICE_ATTR_RO(id); static ssize_t region_size_show(struct device *dev, struct device_attribute *attr, char *buf){ struct dax_region *dax_region = dev_get_drvdata(dev); return sysfs_emit(buf, "%llu\n", (unsigned long long)resource_size(&dax_region->res));}static struct device_attribute dev_attr_region_size = __ATTR(size, 0444, region_size_show, NULL); static ssize_t region_align_show(struct device *dev, struct device_attribute *attr, char *buf){ struct dax_region *dax_region = dev_get_drvdata(dev); return sysfs_emit(buf, "%u\n", dax_region->align);}static struct device_attribute dev_attr_region_align = __ATTR(align, 0400, region_align_show, NULL); #define for_each_dax_region_resource(dax_region, res) \ for (res = (dax_region)->res.child; res; res = res->sibling) static unsigned long long dax_region_avail_size(struct dax_region *dax_region){ resource_size_t size = resource_size(&dax_region->res); struct resource *res; lockdep_assert_held(&dax_region_rwsem); for_each_dax_region_resource(dax_region, res) size -= resource_size(res); return size;} static ssize_t available_size_show(struct device *dev, struct device_attribute *attr, char *buf){ struct dax_region *dax_region = dev_get_drvdata(dev); unsigned long long size; int rc; rc = down_read_interruptible(&dax_region_rwsem); if (rc) return rc; size = dax_region_avail_size(dax_region); up_read(&dax_region_rwsem); return sysfs_emit(buf, "%llu\n", size);}static DEVICE_ATTR_RO(available_size); static ssize_t seed_show(struct device *dev, struct device_attribute *attr, char *buf){ struct dax_region *dax_region = dev_get_drvdata(dev); struct device *seed; ssize_t rc; if (is_static(dax_region)) return -EINVAL; rc = down_read_interruptible(&dax_region_rwsem); if (rc) return rc; seed = dax_region->seed; rc = sysfs_emit(buf, "%s\n", seed ? dev_name(seed) : ""); up_read(&dax_region_rwsem); return rc;}static DEVICE_ATTR_RO(seed); static ssize_t create_show(struct device *dev, struct device_attribute *attr, char *buf){ struct dax_region *dax_region = dev_get_drvdata(dev); struct device *youngest; ssize_t rc; if (is_static(dax_region)) return -EINVAL; rc = down_read_interruptible(&dax_region_rwsem); if (rc) return rc; youngest = dax_region->youngest; rc = sysfs_emit(buf, "%s\n", youngest ? dev_name(youngest) : ""); up_read(&dax_region_rwsem); return rc;} static struct dev_dax *__devm_create_dev_dax(struct dev_dax_data *data); static ssize_t create_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t len){ struct dax_region *dax_region = dev_get_drvdata(dev); unsigned long long avail; ssize_t rc; int val; if (is_static(dax_region)) return -EINVAL; rc = kstrtoint(buf, 0, &val); if (rc) return rc; if (val != 1) return -EINVAL; rc = down_write_killable(&dax_region_rwsem); if (rc) return rc; avail = dax_region_avail_size(dax_region); if (avail == 0) rc = -ENOSPC; else { struct dev_dax_data data = { .dax_region = dax_region, .size = 0, .id = -1, .memmap_on_memory = false, }; struct dev_dax *dev_dax = __devm_create_dev_dax(&data); if (IS_ERR(dev_dax)) rc = PTR_ERR(dev_dax); else { /* * In support of crafting multiple new devices * simultaneously multiple seeds can be created, * but only the first one that has not been * successfully bound is tracked as the region * seed. */ if (!dax_region->seed) dax_region->seed = &dev_dax->dev; dax_region->youngest = &dev_dax->dev; rc = len; } } up_write(&dax_region_rwsem); return rc;}static DEVICE_ATTR_RW(create); void kill_dev_dax(struct dev_dax *dev_dax){ struct dax_device *dax_dev = dev_dax->dax_dev; struct inode *inode = dax_inode(dax_dev); kill_dax(dax_dev); unmap_mapping_range(inode->i_mapping, 0, 0, 1); /* * Dynamic dax region have the pgmap allocated via dev_kzalloc() * and thus freed by devm. Clear the pgmap to not have stale pgmap * ranges on probe() from previous reconfigurations of region devices. */ if (!static_dev_dax(dev_dax)) dev_dax->pgmap = NULL;}EXPORT_SYMBOL_GPL(kill_dev_dax); static void trim_dev_dax_range(struct dev_dax *dev_dax){ int i = dev_dax->nr_range - 1; struct range *range = &dev_dax->ranges[i].range; struct dax_region *dax_region = dev_dax->region; lockdep_assert_held_write(&dax_region_rwsem); dev_dbg(&dev_dax->dev, "delete range[%d]: %#llx:%#llx\n", i, (unsigned long long)range->start, (unsigned long long)range->end); __release_region(&dax_region->res, range->start, range_len(range)); if (--dev_dax->nr_range == 0) { kfree(dev_dax->ranges); dev_dax->ranges = NULL; }} static void free_dev_dax_ranges(struct dev_dax *dev_dax){ while (dev_dax->nr_range) trim_dev_dax_range(dev_dax);} static void unregister_dev_dax(void *dev){ struct dev_dax *dev_dax = to_dev_dax(dev); dev_dbg(dev, "%s\n", __func__); down_write(&dax_region_rwsem); kill_dev_dax(dev_dax); device_del(dev); free_dev_dax_ranges(dev_dax); put_device(dev); up_write(&dax_region_rwsem);} 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; int rc = dev_dax->id; lockdep_assert_held_write(&dax_dev_rwsem); if (!dev_dax->dyn_id || dev_dax->id < 0) return -1;Lines 1–500 of 1625. Load more
// SPDX-License-Identifier: GPL-2.0 /* Copyright(c) 2017-2018 Intel Corporation. All rights reserved. */ #include <linux/memremap.h> #include <linux/device.h> #include <linux/mutex.h> #include <linux/list.h> #include <linux/slab.h> #include <linux/dax.h> #include <linux/io.h> #include "dax-private.h" #include "bus.h" static struct resource dax_regions = DEFINE_RES_MEM_NAMED(0, -1, "DAX Regions"); static DEFINE_MUTEX(dax_bus_lock); /* * All changes to the dax region configuration occur with this lock held * for write. */ DECLARE_RWSEM(dax_region_rwsem); /* * All changes to the dax device configuration occur with this lock held * for write. */ DECLARE_RWSEM(dax_dev_rwsem); #define DAX_NAME_LEN 30 struct dax_id { struct list_head list; char dev_name[DAX_NAME_LEN]; }; static int dax_bus_uevent(const struct device *dev, struct kobj_uevent_env *env) { /* * We only ever expect to handle device-dax instances, i.e. the * @type argument to MODULE_ALIAS_DAX_DEVICE() is always zero */ return add_uevent_var(env, "MODALIAS=" DAX_DEVICE_MODALIAS_FMT, 0); } static struct dax_id *__dax_match_id(const struct dax_device_driver *dax_drv, const char *dev_name) { struct dax_id *dax_id; lockdep_assert_held(&dax_bus_lock); list_for_each_entry(dax_id, &dax_drv->ids, list) if (sysfs_streq(dax_id->dev_name, dev_name)) return dax_id; return NULL; } static int dax_match_id(const struct dax_device_driver *dax_drv, struct device *dev) { int match; mutex_lock(&dax_bus_lock); match = !!__dax_match_id(dax_drv, dev_name(dev)); mutex_unlock(&dax_bus_lock); return match; } static int dax_match_type(const struct dax_device_driver *dax_drv, struct device *dev) { enum dax_driver_type type = DAXDRV_DEVICE_TYPE; struct dev_dax *dev_dax = to_dev_dax(dev); if (dev_dax->region->res.flags & IORESOURCE_DAX_KMEM) type = DAXDRV_KMEM_TYPE; if (dax_drv->type == type) return 1; /* default to device mode if dax_kmem is disabled */ if (dax_drv->type == DAXDRV_DEVICE_TYPE && !IS_ENABLED(CONFIG_DEV_DAX_KMEM)) return 1; return 0; } enum id_action { ID_REMOVE, ID_ADD, }; static ssize_t do_id_store(struct device_driver *drv, const char *buf, size_t count, enum id_action action) { struct dax_device_driver *dax_drv = to_dax_drv(drv); unsigned int region_id, id; char devname[DAX_NAME_LEN]; struct dax_id *dax_id; ssize_t rc = count; int fields; fields = sscanf(buf, "dax%d.%d", ®ion_id, &id); if (fields != 2) return -EINVAL; sprintf(devname, "dax%d.%d", region_id, id); if (!sysfs_streq(buf, devname)) return -EINVAL; mutex_lock(&dax_bus_lock); dax_id = __dax_match_id(dax_drv, buf); if (!dax_id) { if (action == ID_ADD) { dax_id = kzalloc_obj(*dax_id); if (dax_id) { strscpy(dax_id->dev_name, buf, DAX_NAME_LEN); list_add(&dax_id->list, &dax_drv->ids); } else rc = -ENOMEM; } } else if (action == ID_REMOVE) { list_del(&dax_id->list); kfree(dax_id); } mutex_unlock(&dax_bus_lock); if (rc < 0) return rc; if (action == ID_ADD) rc = driver_attach(drv); if (rc) return rc; return count; } static ssize_t new_id_store(struct device_driver *drv, const char *buf, size_t count) { return do_id_store(drv, buf, count, ID_ADD); } static DRIVER_ATTR_WO(new_id); static ssize_t remove_id_store(struct device_driver *drv, const char *buf, size_t count) { return do_id_store(drv, buf, count, ID_REMOVE); } static DRIVER_ATTR_WO(remove_id); static struct attribute *dax_drv_attrs[] = { &driver_attr_new_id.attr, &driver_attr_remove_id.attr, NULL, }; ATTRIBUTE_GROUPS(dax_drv); static int dax_bus_match(struct device *dev, const struct device_driver *drv); /* * Static dax regions are regions created by an external subsystem * nvdimm where a single range is assigned. Its boundaries are by the external * subsystem and are usually limited to one physical memory range. For example, * for PMEM it is usually defined by NVDIMM Namespace boundaries (i.e. a * single contiguous range) * * On dynamic dax regions, the assigned region can be partitioned by dax core * into multiple subdivisions. A subdivision is represented into one * /dev/daxN.M device composed by one or more potentially discontiguous ranges. * * When allocating a dax region, drivers must set whether it's static * (IORESOURCE_DAX_STATIC). On static dax devices, the @pgmap is pre-assigned * to dax core when calling devm_create_dev_dax(), whereas in dynamic dax * devices it is NULL but afterwards allocated by dax core on device ->probe(). * Care is needed to make sure that dynamic dax devices are torn down with a * cleared @pgmap field (see kill_dev_dax()). */ static bool is_static(struct dax_region *dax_region) { return (dax_region->res.flags & IORESOURCE_DAX_STATIC) != 0; } bool static_dev_dax(struct dev_dax *dev_dax) { return is_static(dev_dax->region); } EXPORT_SYMBOL_GPL(static_dev_dax); static u64 dev_dax_size(struct dev_dax *dev_dax) { u64 size = 0; int i; lockdep_assert_held(&dax_dev_rwsem); for (i = 0; i < dev_dax->nr_range; i++) size += range_len(&dev_dax->ranges[i].range); return size; } static int dax_bus_probe(struct device *dev) { struct dax_device_driver *dax_drv = to_dax_drv(dev->driver); struct dev_dax *dev_dax = to_dev_dax(dev); struct dax_region *dax_region = dev_dax->region; int rc; u64 size; rc = down_read_interruptible(&dax_dev_rwsem); if (rc) return rc; size = dev_dax_size(dev_dax); up_read(&dax_dev_rwsem); if (size == 0 || dev_dax->id < 0) return -ENXIO; rc = dax_drv->probe(dev_dax); if (rc || is_static(dax_region)) return rc; /* * Track new seed creation only after successful probe of the * previous seed. */ if (dax_region->seed == dev) dax_region->seed = NULL; return 0; } static void dax_bus_remove(struct device *dev) { struct dax_device_driver *dax_drv = to_dax_drv(dev->driver); struct dev_dax *dev_dax = to_dev_dax(dev); if (dax_drv->remove) dax_drv->remove(dev_dax); } static const struct bus_type dax_bus_type = { .name = "dax", .uevent = dax_bus_uevent, .match = dax_bus_match, .probe = dax_bus_probe, .remove = dax_bus_remove, .drv_groups = dax_drv_groups, }; static int dax_bus_match(struct device *dev, const struct device_driver *drv) { const struct dax_device_driver *dax_drv = to_dax_drv(drv); if (dax_match_id(dax_drv, dev)) return 1; return dax_match_type(dax_drv, dev); } /* * Rely on the fact that drvdata is set before the attributes are * registered, and that the attributes are unregistered before drvdata * is cleared to assume that drvdata is always valid. */ static ssize_t id_show(struct device *dev, struct device_attribute *attr, char *buf) { struct dax_region *dax_region = dev_get_drvdata(dev); return sysfs_emit(buf, "%d\n", dax_region->id); } static DEVICE_ATTR_RO(id); static ssize_t region_size_show(struct device *dev, struct device_attribute *attr, char *buf) { struct dax_region *dax_region = dev_get_drvdata(dev); return sysfs_emit(buf, "%llu\n", (unsigned long long)resource_size(&dax_region->res)); } static struct device_attribute dev_attr_region_size = __ATTR(size, 0444, region_size_show, NULL); static ssize_t region_align_show(struct device *dev, struct device_attribute *attr, char *buf) { struct dax_region *dax_region = dev_get_drvdata(dev); return sysfs_emit(buf, "%u\n", dax_region->align); } static struct device_attribute dev_attr_region_align = __ATTR(align, 0400, region_align_show, NULL); #define for_each_dax_region_resource(dax_region, res) \ for (res = (dax_region)->res.child; res; res = res->sibling) static unsigned long long dax_region_avail_size(struct dax_region *dax_region) { resource_size_t size = resource_size(&dax_region->res); struct resource *res; lockdep_assert_held(&dax_region_rwsem); for_each_dax_region_resource(dax_region, res) size -= resource_size(res); return size; } static ssize_t available_size_show(struct device *dev, struct device_attribute *attr, char *buf) { struct dax_region *dax_region = dev_get_drvdata(dev); unsigned long long size; int rc; rc = down_read_interruptible(&dax_region_rwsem); if (rc) return rc; size = dax_region_avail_size(dax_region); up_read(&dax_region_rwsem); return sysfs_emit(buf, "%llu\n", size); } static DEVICE_ATTR_RO(available_size); static ssize_t seed_show(struct device *dev, struct device_attribute *attr, char *buf) { struct dax_region *dax_region = dev_get_drvdata(dev); struct device *seed; ssize_t rc; if (is_static(dax_region)) return -EINVAL; rc = down_read_interruptible(&dax_region_rwsem); if (rc) return rc; seed = dax_region->seed; rc = sysfs_emit(buf, "%s\n", seed ? dev_name(seed) : ""); up_read(&dax_region_rwsem); return rc; } static DEVICE_ATTR_RO(seed); static ssize_t create_show(struct device *dev, struct device_attribute *attr, char *buf) { struct dax_region *dax_region = dev_get_drvdata(dev); struct device *youngest; ssize_t rc; if (is_static(dax_region)) return -EINVAL; rc = down_read_interruptible(&dax_region_rwsem); if (rc) return rc; youngest = dax_region->youngest; rc = sysfs_emit(buf, "%s\n", youngest ? dev_name(youngest) : ""); up_read(&dax_region_rwsem); return rc; } static struct dev_dax *__devm_create_dev_dax(struct dev_dax_data *data); static ssize_t create_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t len) { struct dax_region *dax_region = dev_get_drvdata(dev); unsigned long long avail; ssize_t rc; int val; if (is_static(dax_region)) return -EINVAL; rc = kstrtoint(buf, 0, &val); if (rc) return rc; if (val != 1) return -EINVAL; rc = down_write_killable(&dax_region_rwsem); if (rc) return rc; avail = dax_region_avail_size(dax_region); if (avail == 0) rc = -ENOSPC; else { struct dev_dax_data data = { .dax_region = dax_region, .size = 0, .id = -1, .memmap_on_memory = false, }; struct dev_dax *dev_dax = __devm_create_dev_dax(&data); if (IS_ERR(dev_dax)) rc = PTR_ERR(dev_dax); else { /* * In support of crafting multiple new devices * simultaneously multiple seeds can be created, * but only the first one that has not been * successfully bound is tracked as the region * seed. */ if (!dax_region->seed) dax_region->seed = &dev_dax->dev; dax_region->youngest = &dev_dax->dev; rc = len; } } up_write(&dax_region_rwsem); return rc; } static DEVICE_ATTR_RW(create); void kill_dev_dax(struct dev_dax *dev_dax) { struct dax_device *dax_dev = dev_dax->dax_dev; struct inode *inode = dax_inode(dax_dev); kill_dax(dax_dev); unmap_mapping_range(inode->i_mapping, 0, 0, 1); /* * Dynamic dax region have the pgmap allocated via dev_kzalloc() * and thus freed by devm. Clear the pgmap to not have stale pgmap * ranges on probe() from previous reconfigurations of region devices. */ if (!static_dev_dax(dev_dax)) dev_dax->pgmap = NULL; } EXPORT_SYMBOL_GPL(kill_dev_dax); static void trim_dev_dax_range(struct dev_dax *dev_dax) { int i = dev_dax->nr_range - 1; struct range *range = &dev_dax->ranges[i].range; struct dax_region *dax_region = dev_dax->region; lockdep_assert_held_write(&dax_region_rwsem); dev_dbg(&dev_dax->dev, "delete range[%d]: %#llx:%#llx\n", i, (unsigned long long)range->start, (unsigned long long)range->end); __release_region(&dax_region->res, range->start, range_len(range)); if (--dev_dax->nr_range == 0) { kfree(dev_dax->ranges); dev_dax->ranges = NULL; } } static void free_dev_dax_ranges(struct dev_dax *dev_dax) { while (dev_dax->nr_range) trim_dev_dax_range(dev_dax); } static void unregister_dev_dax(void *dev) { struct dev_dax *dev_dax = to_dev_dax(dev); dev_dbg(dev, "%s\n", __func__); down_write(&dax_region_rwsem); kill_dev_dax(dev_dax); device_del(dev); free_dev_dax_ranges(dev_dax); put_device(dev); up_write(&dax_region_rwsem); } 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; int rc = dev_dax->id; lockdep_assert_held_write(&dax_dev_rwsem); if (!dev_dax->dyn_id || dev_dax->id < 0) return -1;