1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright(c) 2017-2018 Intel Corporation. All rights reserved. */
3 #include <linux/memremap.h>
4 #include <linux/device.h>
5 #include <linux/mutex.h>
6 #include <linux/list.h>
7 #include <linux/slab.h>
10 #include "dax-private.h"
13 static DEFINE_MUTEX(dax_bus_lock);
15 #define DAX_NAME_LEN 30
17 struct list_head list;
18 char dev_name[DAX_NAME_LEN];
21 static int dax_bus_uevent(struct device *dev, struct kobj_uevent_env *env)
24 * We only ever expect to handle device-dax instances, i.e. the
25 * @type argument to MODULE_ALIAS_DAX_DEVICE() is always zero
27 return add_uevent_var(env, "MODALIAS=" DAX_DEVICE_MODALIAS_FMT, 0);
30 static struct dax_device_driver *to_dax_drv(struct device_driver *drv)
32 return container_of(drv, struct dax_device_driver, drv);
35 static struct dax_id *__dax_match_id(struct dax_device_driver *dax_drv,
38 struct dax_id *dax_id;
40 lockdep_assert_held(&dax_bus_lock);
42 list_for_each_entry(dax_id, &dax_drv->ids, list)
43 if (sysfs_streq(dax_id->dev_name, dev_name))
48 static int dax_match_id(struct dax_device_driver *dax_drv, struct device *dev)
52 mutex_lock(&dax_bus_lock);
53 match = !!__dax_match_id(dax_drv, dev_name(dev));
54 mutex_unlock(&dax_bus_lock);
64 static ssize_t do_id_store(struct device_driver *drv, const char *buf,
65 size_t count, enum id_action action)
67 struct dax_device_driver *dax_drv = to_dax_drv(drv);
68 unsigned int region_id, id;
69 char devname[DAX_NAME_LEN];
70 struct dax_id *dax_id;
74 fields = sscanf(buf, "dax%d.%d", ®ion_id, &id);
77 sprintf(devname, "dax%d.%d", region_id, id);
78 if (!sysfs_streq(buf, devname))
81 mutex_lock(&dax_bus_lock);
82 dax_id = __dax_match_id(dax_drv, buf);
84 if (action == ID_ADD) {
85 dax_id = kzalloc(sizeof(*dax_id), GFP_KERNEL);
87 strncpy(dax_id->dev_name, buf, DAX_NAME_LEN);
88 list_add(&dax_id->list, &dax_drv->ids);
92 } else if (action == ID_REMOVE) {
93 list_del(&dax_id->list);
96 mutex_unlock(&dax_bus_lock);
100 if (action == ID_ADD)
101 rc = driver_attach(drv);
107 static ssize_t new_id_store(struct device_driver *drv, const char *buf,
110 return do_id_store(drv, buf, count, ID_ADD);
112 static DRIVER_ATTR_WO(new_id);
114 static ssize_t remove_id_store(struct device_driver *drv, const char *buf,
117 return do_id_store(drv, buf, count, ID_REMOVE);
119 static DRIVER_ATTR_WO(remove_id);
121 static struct attribute *dax_drv_attrs[] = {
122 &driver_attr_new_id.attr,
123 &driver_attr_remove_id.attr,
126 ATTRIBUTE_GROUPS(dax_drv);
128 static int dax_bus_match(struct device *dev, struct device_driver *drv);
131 * Static dax regions are regions created by an external subsystem
132 * nvdimm where a single range is assigned. Its boundaries are by the external
133 * subsystem and are usually limited to one physical memory range. For example,
134 * for PMEM it is usually defined by NVDIMM Namespace boundaries (i.e. a
135 * single contiguous range)
137 * On dynamic dax regions, the assigned region can be partitioned by dax core
138 * into multiple subdivisions. A subdivision is represented into one
139 * /dev/daxN.M device composed by one or more potentially discontiguous ranges.
141 * When allocating a dax region, drivers must set whether it's static
142 * (IORESOURCE_DAX_STATIC). On static dax devices, the @pgmap is pre-assigned
143 * to dax core when calling devm_create_dev_dax(), whereas in dynamic dax
144 * devices it is NULL but afterwards allocated by dax core on device ->probe().
145 * Care is needed to make sure that dynamic dax devices are torn down with a
146 * cleared @pgmap field (see kill_dev_dax()).
148 static bool is_static(struct dax_region *dax_region)
150 return (dax_region->res.flags & IORESOURCE_DAX_STATIC) != 0;
153 bool static_dev_dax(struct dev_dax *dev_dax)
155 return is_static(dev_dax->region);
157 EXPORT_SYMBOL_GPL(static_dev_dax);
159 static u64 dev_dax_size(struct dev_dax *dev_dax)
164 device_lock_assert(&dev_dax->dev);
166 for (i = 0; i < dev_dax->nr_range; i++)
167 size += range_len(&dev_dax->ranges[i].range);
172 static int dax_bus_probe(struct device *dev)
174 struct dax_device_driver *dax_drv = to_dax_drv(dev->driver);
175 struct dev_dax *dev_dax = to_dev_dax(dev);
176 struct dax_region *dax_region = dev_dax->region;
179 if (dev_dax_size(dev_dax) == 0 || dev_dax->id < 0)
182 rc = dax_drv->probe(dev_dax);
184 if (rc || is_static(dax_region))
188 * Track new seed creation only after successful probe of the
191 if (dax_region->seed == dev)
192 dax_region->seed = NULL;
197 static void dax_bus_remove(struct device *dev)
199 struct dax_device_driver *dax_drv = to_dax_drv(dev->driver);
200 struct dev_dax *dev_dax = to_dev_dax(dev);
203 dax_drv->remove(dev_dax);
206 static struct bus_type dax_bus_type = {
208 .uevent = dax_bus_uevent,
209 .match = dax_bus_match,
210 .probe = dax_bus_probe,
211 .remove = dax_bus_remove,
212 .drv_groups = dax_drv_groups,
215 static int dax_bus_match(struct device *dev, struct device_driver *drv)
217 struct dax_device_driver *dax_drv = to_dax_drv(drv);
220 * All but the 'device-dax' driver, which has 'match_always'
221 * set, requires an exact id match.
223 if (dax_drv->match_always)
226 return dax_match_id(dax_drv, dev);
230 * Rely on the fact that drvdata is set before the attributes are
231 * registered, and that the attributes are unregistered before drvdata
232 * is cleared to assume that drvdata is always valid.
234 static ssize_t id_show(struct device *dev,
235 struct device_attribute *attr, char *buf)
237 struct dax_region *dax_region = dev_get_drvdata(dev);
239 return sprintf(buf, "%d\n", dax_region->id);
241 static DEVICE_ATTR_RO(id);
243 static ssize_t region_size_show(struct device *dev,
244 struct device_attribute *attr, char *buf)
246 struct dax_region *dax_region = dev_get_drvdata(dev);
248 return sprintf(buf, "%llu\n", (unsigned long long)
249 resource_size(&dax_region->res));
251 static struct device_attribute dev_attr_region_size = __ATTR(size, 0444,
252 region_size_show, NULL);
254 static ssize_t region_align_show(struct device *dev,
255 struct device_attribute *attr, char *buf)
257 struct dax_region *dax_region = dev_get_drvdata(dev);
259 return sprintf(buf, "%u\n", dax_region->align);
261 static struct device_attribute dev_attr_region_align =
262 __ATTR(align, 0400, region_align_show, NULL);
264 #define for_each_dax_region_resource(dax_region, res) \
265 for (res = (dax_region)->res.child; res; res = res->sibling)
267 static unsigned long long dax_region_avail_size(struct dax_region *dax_region)
269 resource_size_t size = resource_size(&dax_region->res);
270 struct resource *res;
272 device_lock_assert(dax_region->dev);
274 for_each_dax_region_resource(dax_region, res)
275 size -= resource_size(res);
279 static ssize_t available_size_show(struct device *dev,
280 struct device_attribute *attr, char *buf)
282 struct dax_region *dax_region = dev_get_drvdata(dev);
283 unsigned long long size;
286 size = dax_region_avail_size(dax_region);
289 return sprintf(buf, "%llu\n", size);
291 static DEVICE_ATTR_RO(available_size);
293 static ssize_t seed_show(struct device *dev,
294 struct device_attribute *attr, char *buf)
296 struct dax_region *dax_region = dev_get_drvdata(dev);
300 if (is_static(dax_region))
304 seed = dax_region->seed;
305 rc = sprintf(buf, "%s\n", seed ? dev_name(seed) : "");
310 static DEVICE_ATTR_RO(seed);
312 static ssize_t create_show(struct device *dev,
313 struct device_attribute *attr, char *buf)
315 struct dax_region *dax_region = dev_get_drvdata(dev);
316 struct device *youngest;
319 if (is_static(dax_region))
323 youngest = dax_region->youngest;
324 rc = sprintf(buf, "%s\n", youngest ? dev_name(youngest) : "");
330 static ssize_t create_store(struct device *dev, struct device_attribute *attr,
331 const char *buf, size_t len)
333 struct dax_region *dax_region = dev_get_drvdata(dev);
334 unsigned long long avail;
338 if (is_static(dax_region))
341 rc = kstrtoint(buf, 0, &val);
348 avail = dax_region_avail_size(dax_region);
352 struct dev_dax_data data = {
353 .dax_region = dax_region,
357 struct dev_dax *dev_dax = devm_create_dev_dax(&data);
360 rc = PTR_ERR(dev_dax);
363 * In support of crafting multiple new devices
364 * simultaneously multiple seeds can be created,
365 * but only the first one that has not been
366 * successfully bound is tracked as the region
369 if (!dax_region->seed)
370 dax_region->seed = &dev_dax->dev;
371 dax_region->youngest = &dev_dax->dev;
379 static DEVICE_ATTR_RW(create);
381 void kill_dev_dax(struct dev_dax *dev_dax)
383 struct dax_device *dax_dev = dev_dax->dax_dev;
384 struct inode *inode = dax_inode(dax_dev);
387 unmap_mapping_range(inode->i_mapping, 0, 0, 1);
390 * Dynamic dax region have the pgmap allocated via dev_kzalloc()
391 * and thus freed by devm. Clear the pgmap to not have stale pgmap
392 * ranges on probe() from previous reconfigurations of region devices.
394 if (!static_dev_dax(dev_dax))
395 dev_dax->pgmap = NULL;
397 EXPORT_SYMBOL_GPL(kill_dev_dax);
399 static void trim_dev_dax_range(struct dev_dax *dev_dax)
401 int i = dev_dax->nr_range - 1;
402 struct range *range = &dev_dax->ranges[i].range;
403 struct dax_region *dax_region = dev_dax->region;
405 device_lock_assert(dax_region->dev);
406 dev_dbg(&dev_dax->dev, "delete range[%d]: %#llx:%#llx\n", i,
407 (unsigned long long)range->start,
408 (unsigned long long)range->end);
410 __release_region(&dax_region->res, range->start, range_len(range));
411 if (--dev_dax->nr_range == 0) {
412 kfree(dev_dax->ranges);
413 dev_dax->ranges = NULL;
417 static void free_dev_dax_ranges(struct dev_dax *dev_dax)
419 while (dev_dax->nr_range)
420 trim_dev_dax_range(dev_dax);
423 static void unregister_dev_dax(void *dev)
425 struct dev_dax *dev_dax = to_dev_dax(dev);
427 dev_dbg(dev, "%s\n", __func__);
429 kill_dev_dax(dev_dax);
430 free_dev_dax_ranges(dev_dax);
435 /* a return value >= 0 indicates this invocation invalidated the id */
436 static int __free_dev_dax_id(struct dev_dax *dev_dax)
438 struct dax_region *dax_region = dev_dax->region;
439 struct device *dev = &dev_dax->dev;
440 int rc = dev_dax->id;
442 device_lock_assert(dev);
444 if (is_static(dax_region) || dev_dax->id < 0)
446 ida_free(&dax_region->ida, dev_dax->id);
451 static int free_dev_dax_id(struct dev_dax *dev_dax)
453 struct device *dev = &dev_dax->dev;
457 rc = __free_dev_dax_id(dev_dax);
462 static ssize_t delete_store(struct device *dev, struct device_attribute *attr,
463 const char *buf, size_t len)
465 struct dax_region *dax_region = dev_get_drvdata(dev);
466 struct dev_dax *dev_dax;
467 struct device *victim;
471 if (is_static(dax_region))
474 victim = device_find_child_by_name(dax_region->dev, buf);
480 dev_dax = to_dev_dax(victim);
481 if (victim->driver || dev_dax_size(dev_dax))
485 * Invalidate the device so it does not become active
486 * again, but always preserve device-id-0 so that
487 * /sys/bus/dax/ is guaranteed to be populated while any
488 * dax_region is registered.
490 if (dev_dax->id > 0) {
491 do_del = __free_dev_dax_id(dev_dax) >= 0;
493 if (dax_region->seed == victim)
494 dax_region->seed = NULL;
495 if (dax_region->youngest == victim)
496 dax_region->youngest = NULL;
500 device_unlock(victim);
502 /* won the race to invalidate the device, clean it up */
504 devm_release_action(dev, unregister_dev_dax, victim);
510 static DEVICE_ATTR_WO(delete);
512 static umode_t dax_region_visible(struct kobject *kobj, struct attribute *a,
515 struct device *dev = container_of(kobj, struct device, kobj);
516 struct dax_region *dax_region = dev_get_drvdata(dev);
518 if (is_static(dax_region))
519 if (a == &dev_attr_available_size.attr
520 || a == &dev_attr_create.attr
521 || a == &dev_attr_seed.attr
522 || a == &dev_attr_delete.attr)
527 static struct attribute *dax_region_attributes[] = {
528 &dev_attr_available_size.attr,
529 &dev_attr_region_size.attr,
530 &dev_attr_region_align.attr,
531 &dev_attr_create.attr,
533 &dev_attr_delete.attr,
538 static const struct attribute_group dax_region_attribute_group = {
539 .name = "dax_region",
540 .attrs = dax_region_attributes,
541 .is_visible = dax_region_visible,
544 static const struct attribute_group *dax_region_attribute_groups[] = {
545 &dax_region_attribute_group,
549 static void dax_region_free(struct kref *kref)
551 struct dax_region *dax_region;
553 dax_region = container_of(kref, struct dax_region, kref);
557 void dax_region_put(struct dax_region *dax_region)
559 kref_put(&dax_region->kref, dax_region_free);
561 EXPORT_SYMBOL_GPL(dax_region_put);
563 static void dax_region_unregister(void *region)
565 struct dax_region *dax_region = region;
567 sysfs_remove_groups(&dax_region->dev->kobj,
568 dax_region_attribute_groups);
569 dax_region_put(dax_region);
572 struct dax_region *alloc_dax_region(struct device *parent, int region_id,
573 struct range *range, int target_node, unsigned int align,
576 struct dax_region *dax_region;
579 * The DAX core assumes that it can store its private data in
580 * parent->driver_data. This WARN is a reminder / safeguard for
581 * developers of device-dax drivers.
583 if (dev_get_drvdata(parent)) {
584 dev_WARN(parent, "dax core failed to setup private data\n");
588 if (!IS_ALIGNED(range->start, align)
589 || !IS_ALIGNED(range_len(range), align))
592 dax_region = kzalloc(sizeof(*dax_region), GFP_KERNEL);
596 dev_set_drvdata(parent, dax_region);
597 kref_init(&dax_region->kref);
598 dax_region->id = region_id;
599 dax_region->align = align;
600 dax_region->dev = parent;
601 dax_region->target_node = target_node;
602 ida_init(&dax_region->ida);
603 dax_region->res = (struct resource) {
604 .start = range->start,
606 .flags = IORESOURCE_MEM | flags,
609 if (sysfs_create_groups(&parent->kobj, dax_region_attribute_groups)) {
614 kref_get(&dax_region->kref);
615 if (devm_add_action_or_reset(parent, dax_region_unregister, dax_region))
619 EXPORT_SYMBOL_GPL(alloc_dax_region);
621 static void dax_mapping_release(struct device *dev)
623 struct dax_mapping *mapping = to_dax_mapping(dev);
624 struct dev_dax *dev_dax = to_dev_dax(dev->parent);
626 ida_free(&dev_dax->ida, mapping->id);
630 static void unregister_dax_mapping(void *data)
632 struct device *dev = data;
633 struct dax_mapping *mapping = to_dax_mapping(dev);
634 struct dev_dax *dev_dax = to_dev_dax(dev->parent);
635 struct dax_region *dax_region = dev_dax->region;
637 dev_dbg(dev, "%s\n", __func__);
639 device_lock_assert(dax_region->dev);
641 dev_dax->ranges[mapping->range_id].mapping = NULL;
642 mapping->range_id = -1;
648 static struct dev_dax_range *get_dax_range(struct device *dev)
650 struct dax_mapping *mapping = to_dax_mapping(dev);
651 struct dev_dax *dev_dax = to_dev_dax(dev->parent);
652 struct dax_region *dax_region = dev_dax->region;
654 device_lock(dax_region->dev);
655 if (mapping->range_id < 0) {
656 device_unlock(dax_region->dev);
660 return &dev_dax->ranges[mapping->range_id];
663 static void put_dax_range(struct dev_dax_range *dax_range)
665 struct dax_mapping *mapping = dax_range->mapping;
666 struct dev_dax *dev_dax = to_dev_dax(mapping->dev.parent);
667 struct dax_region *dax_region = dev_dax->region;
669 device_unlock(dax_region->dev);
672 static ssize_t start_show(struct device *dev,
673 struct device_attribute *attr, char *buf)
675 struct dev_dax_range *dax_range;
678 dax_range = get_dax_range(dev);
681 rc = sprintf(buf, "%#llx\n", dax_range->range.start);
682 put_dax_range(dax_range);
686 static DEVICE_ATTR(start, 0400, start_show, NULL);
688 static ssize_t end_show(struct device *dev,
689 struct device_attribute *attr, char *buf)
691 struct dev_dax_range *dax_range;
694 dax_range = get_dax_range(dev);
697 rc = sprintf(buf, "%#llx\n", dax_range->range.end);
698 put_dax_range(dax_range);
702 static DEVICE_ATTR(end, 0400, end_show, NULL);
704 static ssize_t pgoff_show(struct device *dev,
705 struct device_attribute *attr, char *buf)
707 struct dev_dax_range *dax_range;
710 dax_range = get_dax_range(dev);
713 rc = sprintf(buf, "%#lx\n", dax_range->pgoff);
714 put_dax_range(dax_range);
718 static DEVICE_ATTR(page_offset, 0400, pgoff_show, NULL);
720 static struct attribute *dax_mapping_attributes[] = {
721 &dev_attr_start.attr,
723 &dev_attr_page_offset.attr,
727 static const struct attribute_group dax_mapping_attribute_group = {
728 .attrs = dax_mapping_attributes,
731 static const struct attribute_group *dax_mapping_attribute_groups[] = {
732 &dax_mapping_attribute_group,
736 static struct device_type dax_mapping_type = {
737 .release = dax_mapping_release,
738 .groups = dax_mapping_attribute_groups,
741 static int devm_register_dax_mapping(struct dev_dax *dev_dax, int range_id)
743 struct dax_region *dax_region = dev_dax->region;
744 struct dax_mapping *mapping;
748 device_lock_assert(dax_region->dev);
750 if (dev_WARN_ONCE(&dev_dax->dev, !dax_region->dev->driver,
751 "region disabled\n"))
754 mapping = kzalloc(sizeof(*mapping), GFP_KERNEL);
757 mapping->range_id = range_id;
758 mapping->id = ida_alloc(&dev_dax->ida, GFP_KERNEL);
759 if (mapping->id < 0) {
763 dev_dax->ranges[range_id].mapping = mapping;
765 device_initialize(dev);
766 dev->parent = &dev_dax->dev;
767 dev->type = &dax_mapping_type;
768 dev_set_name(dev, "mapping%d", mapping->id);
769 rc = device_add(dev);
775 rc = devm_add_action_or_reset(dax_region->dev, unregister_dax_mapping,
782 static int alloc_dev_dax_range(struct dev_dax *dev_dax, u64 start,
783 resource_size_t size)
785 struct dax_region *dax_region = dev_dax->region;
786 struct resource *res = &dax_region->res;
787 struct device *dev = &dev_dax->dev;
788 struct dev_dax_range *ranges;
789 unsigned long pgoff = 0;
790 struct resource *alloc;
793 device_lock_assert(dax_region->dev);
795 /* handle the seed alloc special case */
797 if (dev_WARN_ONCE(dev, dev_dax->nr_range,
798 "0-size allocation must be first\n"))
800 /* nr_range == 0 is elsewhere special cased as 0-size device */
804 alloc = __request_region(res, start, size, dev_name(dev), 0);
808 ranges = krealloc(dev_dax->ranges, sizeof(*ranges)
809 * (dev_dax->nr_range + 1), GFP_KERNEL);
811 __release_region(res, alloc->start, resource_size(alloc));
815 for (i = 0; i < dev_dax->nr_range; i++)
816 pgoff += PHYS_PFN(range_len(&ranges[i].range));
817 dev_dax->ranges = ranges;
818 ranges[dev_dax->nr_range++] = (struct dev_dax_range) {
821 .start = alloc->start,
826 dev_dbg(dev, "alloc range[%d]: %pa:%pa\n", dev_dax->nr_range - 1,
827 &alloc->start, &alloc->end);
829 * A dev_dax instance must be registered before mapping device
830 * children can be added. Defer to devm_create_dev_dax() to add
831 * the initial mapping device.
833 if (!device_is_registered(&dev_dax->dev))
836 rc = devm_register_dax_mapping(dev_dax, dev_dax->nr_range - 1);
838 trim_dev_dax_range(dev_dax);
843 static int adjust_dev_dax_range(struct dev_dax *dev_dax, struct resource *res, resource_size_t size)
845 int last_range = dev_dax->nr_range - 1;
846 struct dev_dax_range *dax_range = &dev_dax->ranges[last_range];
847 struct dax_region *dax_region = dev_dax->region;
848 bool is_shrink = resource_size(res) > size;
849 struct range *range = &dax_range->range;
850 struct device *dev = &dev_dax->dev;
853 device_lock_assert(dax_region->dev);
855 if (dev_WARN_ONCE(dev, !size, "deletion is handled by dev_dax_shrink\n"))
858 rc = adjust_resource(res, range->start, size);
862 *range = (struct range) {
863 .start = range->start,
864 .end = range->start + size - 1,
867 dev_dbg(dev, "%s range[%d]: %#llx:%#llx\n", is_shrink ? "shrink" : "extend",
868 last_range, (unsigned long long) range->start,
869 (unsigned long long) range->end);
874 static ssize_t size_show(struct device *dev,
875 struct device_attribute *attr, char *buf)
877 struct dev_dax *dev_dax = to_dev_dax(dev);
878 unsigned long long size;
881 size = dev_dax_size(dev_dax);
884 return sprintf(buf, "%llu\n", size);
887 static bool alloc_is_aligned(struct dev_dax *dev_dax, resource_size_t size)
890 * The minimum mapping granularity for a device instance is a
891 * single subsection, unless the arch says otherwise.
893 return IS_ALIGNED(size, max_t(unsigned long, dev_dax->align, memremap_compat_align()));
896 static int dev_dax_shrink(struct dev_dax *dev_dax, resource_size_t size)
898 resource_size_t to_shrink = dev_dax_size(dev_dax) - size;
899 struct dax_region *dax_region = dev_dax->region;
900 struct device *dev = &dev_dax->dev;
903 for (i = dev_dax->nr_range - 1; i >= 0; i--) {
904 struct range *range = &dev_dax->ranges[i].range;
905 struct dax_mapping *mapping = dev_dax->ranges[i].mapping;
906 struct resource *adjust = NULL, *res;
907 resource_size_t shrink;
909 shrink = min_t(u64, to_shrink, range_len(range));
910 if (shrink >= range_len(range)) {
911 devm_release_action(dax_region->dev,
912 unregister_dax_mapping, &mapping->dev);
913 trim_dev_dax_range(dev_dax);
920 for_each_dax_region_resource(dax_region, res)
921 if (strcmp(res->name, dev_name(dev)) == 0
922 && res->start == range->start) {
927 if (dev_WARN_ONCE(dev, !adjust || i != dev_dax->nr_range - 1,
928 "failed to find matching resource\n"))
930 return adjust_dev_dax_range(dev_dax, adjust, range_len(range)
937 * Only allow adjustments that preserve the relative pgoff of existing
938 * allocations. I.e. the dev_dax->ranges array is ordered by increasing pgoff.
940 static bool adjust_ok(struct dev_dax *dev_dax, struct resource *res)
942 struct dev_dax_range *last;
945 if (dev_dax->nr_range == 0)
947 if (strcmp(res->name, dev_name(&dev_dax->dev)) != 0)
949 last = &dev_dax->ranges[dev_dax->nr_range - 1];
950 if (last->range.start != res->start || last->range.end != res->end)
952 for (i = 0; i < dev_dax->nr_range - 1; i++) {
953 struct dev_dax_range *dax_range = &dev_dax->ranges[i];
955 if (dax_range->pgoff > last->pgoff)
962 static ssize_t dev_dax_resize(struct dax_region *dax_region,
963 struct dev_dax *dev_dax, resource_size_t size)
965 resource_size_t avail = dax_region_avail_size(dax_region), to_alloc;
966 resource_size_t dev_size = dev_dax_size(dev_dax);
967 struct resource *region_res = &dax_region->res;
968 struct device *dev = &dev_dax->dev;
969 struct resource *res, *first;
970 resource_size_t alloc = 0;
975 if (size == dev_size)
977 if (size > dev_size && size - dev_size > avail)
980 return dev_dax_shrink(dev_dax, size);
982 to_alloc = size - dev_size;
983 if (dev_WARN_ONCE(dev, !alloc_is_aligned(dev_dax, to_alloc),
984 "resize of %pa misaligned\n", &to_alloc))
988 * Expand the device into the unused portion of the region. This
989 * may involve adjusting the end of an existing resource, or
990 * allocating a new resource.
993 first = region_res->child;
995 return alloc_dev_dax_range(dev_dax, dax_region->res.start, to_alloc);
998 for (res = first; res; res = res->sibling) {
999 struct resource *next = res->sibling;
1001 /* space at the beginning of the region */
1002 if (res == first && res->start > dax_region->res.start) {
1003 alloc = min(res->start - dax_region->res.start, to_alloc);
1004 rc = alloc_dev_dax_range(dev_dax, dax_region->res.start, alloc);
1009 /* space between allocations */
1010 if (next && next->start > res->end + 1)
1011 alloc = min(next->start - (res->end + 1), to_alloc);
1013 /* space at the end of the region */
1014 if (!alloc && !next && res->end < region_res->end)
1015 alloc = min(region_res->end - res->end, to_alloc);
1020 if (adjust_ok(dev_dax, res)) {
1021 rc = adjust_dev_dax_range(dev_dax, res, resource_size(res) + alloc);
1024 rc = alloc_dev_dax_range(dev_dax, res->end + 1, alloc);
1035 static ssize_t size_store(struct device *dev, struct device_attribute *attr,
1036 const char *buf, size_t len)
1039 unsigned long long val;
1040 struct dev_dax *dev_dax = to_dev_dax(dev);
1041 struct dax_region *dax_region = dev_dax->region;
1043 rc = kstrtoull(buf, 0, &val);
1047 if (!alloc_is_aligned(dev_dax, val)) {
1048 dev_dbg(dev, "%s: size: %lld misaligned\n", __func__, val);
1052 device_lock(dax_region->dev);
1053 if (!dax_region->dev->driver) {
1054 device_unlock(dax_region->dev);
1058 rc = dev_dax_resize(dax_region, dev_dax, val);
1060 device_unlock(dax_region->dev);
1062 return rc == 0 ? len : rc;
1064 static DEVICE_ATTR_RW(size);
1066 static ssize_t range_parse(const char *opt, size_t len, struct range *range)
1068 unsigned long long addr = 0;
1069 char *start, *end, *str;
1070 ssize_t rc = -EINVAL;
1072 str = kstrdup(opt, GFP_KERNEL);
1077 start = strsep(&end, "-");
1081 rc = kstrtoull(start, 16, &addr);
1084 range->start = addr;
1086 rc = kstrtoull(end, 16, &addr);
1096 static ssize_t mapping_store(struct device *dev, struct device_attribute *attr,
1097 const char *buf, size_t len)
1099 struct dev_dax *dev_dax = to_dev_dax(dev);
1100 struct dax_region *dax_region = dev_dax->region;
1105 rc = range_parse(buf, len, &r);
1110 device_lock(dax_region->dev);
1111 if (!dax_region->dev->driver) {
1112 device_unlock(dax_region->dev);
1117 to_alloc = range_len(&r);
1118 if (alloc_is_aligned(dev_dax, to_alloc))
1119 rc = alloc_dev_dax_range(dev_dax, r.start, to_alloc);
1121 device_unlock(dax_region->dev);
1123 return rc == 0 ? len : rc;
1125 static DEVICE_ATTR_WO(mapping);
1127 static ssize_t align_show(struct device *dev,
1128 struct device_attribute *attr, char *buf)
1130 struct dev_dax *dev_dax = to_dev_dax(dev);
1132 return sprintf(buf, "%d\n", dev_dax->align);
1135 static ssize_t dev_dax_validate_align(struct dev_dax *dev_dax)
1137 struct device *dev = &dev_dax->dev;
1140 for (i = 0; i < dev_dax->nr_range; i++) {
1141 size_t len = range_len(&dev_dax->ranges[i].range);
1143 if (!alloc_is_aligned(dev_dax, len)) {
1144 dev_dbg(dev, "%s: align %u invalid for range %d\n",
1145 __func__, dev_dax->align, i);
1153 static ssize_t align_store(struct device *dev, struct device_attribute *attr,
1154 const char *buf, size_t len)
1156 struct dev_dax *dev_dax = to_dev_dax(dev);
1157 struct dax_region *dax_region = dev_dax->region;
1158 unsigned long val, align_save;
1161 rc = kstrtoul(buf, 0, &val);
1165 if (!dax_align_valid(val))
1168 device_lock(dax_region->dev);
1169 if (!dax_region->dev->driver) {
1170 device_unlock(dax_region->dev);
1180 align_save = dev_dax->align;
1181 dev_dax->align = val;
1182 rc = dev_dax_validate_align(dev_dax);
1184 dev_dax->align = align_save;
1187 device_unlock(dax_region->dev);
1188 return rc == 0 ? len : rc;
1190 static DEVICE_ATTR_RW(align);
1192 static int dev_dax_target_node(struct dev_dax *dev_dax)
1194 struct dax_region *dax_region = dev_dax->region;
1196 return dax_region->target_node;
1199 static ssize_t target_node_show(struct device *dev,
1200 struct device_attribute *attr, char *buf)
1202 struct dev_dax *dev_dax = to_dev_dax(dev);
1204 return sprintf(buf, "%d\n", dev_dax_target_node(dev_dax));
1206 static DEVICE_ATTR_RO(target_node);
1208 static ssize_t resource_show(struct device *dev,
1209 struct device_attribute *attr, char *buf)
1211 struct dev_dax *dev_dax = to_dev_dax(dev);
1212 struct dax_region *dax_region = dev_dax->region;
1213 unsigned long long start;
1215 if (dev_dax->nr_range < 1)
1216 start = dax_region->res.start;
1218 start = dev_dax->ranges[0].range.start;
1220 return sprintf(buf, "%#llx\n", start);
1222 static DEVICE_ATTR(resource, 0400, resource_show, NULL);
1224 static ssize_t modalias_show(struct device *dev, struct device_attribute *attr,
1228 * We only ever expect to handle device-dax instances, i.e. the
1229 * @type argument to MODULE_ALIAS_DAX_DEVICE() is always zero
1231 return sprintf(buf, DAX_DEVICE_MODALIAS_FMT "\n", 0);
1233 static DEVICE_ATTR_RO(modalias);
1235 static ssize_t numa_node_show(struct device *dev,
1236 struct device_attribute *attr, char *buf)
1238 return sprintf(buf, "%d\n", dev_to_node(dev));
1240 static DEVICE_ATTR_RO(numa_node);
1242 static umode_t dev_dax_visible(struct kobject *kobj, struct attribute *a, int n)
1244 struct device *dev = container_of(kobj, struct device, kobj);
1245 struct dev_dax *dev_dax = to_dev_dax(dev);
1246 struct dax_region *dax_region = dev_dax->region;
1248 if (a == &dev_attr_target_node.attr && dev_dax_target_node(dev_dax) < 0)
1250 if (a == &dev_attr_numa_node.attr && !IS_ENABLED(CONFIG_NUMA))
1252 if (a == &dev_attr_mapping.attr && is_static(dax_region))
1254 if ((a == &dev_attr_align.attr ||
1255 a == &dev_attr_size.attr) && is_static(dax_region))
1260 static struct attribute *dev_dax_attributes[] = {
1261 &dev_attr_modalias.attr,
1262 &dev_attr_size.attr,
1263 &dev_attr_mapping.attr,
1264 &dev_attr_target_node.attr,
1265 &dev_attr_align.attr,
1266 &dev_attr_resource.attr,
1267 &dev_attr_numa_node.attr,
1271 static const struct attribute_group dev_dax_attribute_group = {
1272 .attrs = dev_dax_attributes,
1273 .is_visible = dev_dax_visible,
1276 static const struct attribute_group *dax_attribute_groups[] = {
1277 &dev_dax_attribute_group,
1281 static void dev_dax_release(struct device *dev)
1283 struct dev_dax *dev_dax = to_dev_dax(dev);
1284 struct dax_region *dax_region = dev_dax->region;
1285 struct dax_device *dax_dev = dev_dax->dax_dev;
1288 free_dev_dax_id(dev_dax);
1289 dax_region_put(dax_region);
1290 kfree(dev_dax->pgmap);
1294 static const struct device_type dev_dax_type = {
1295 .release = dev_dax_release,
1296 .groups = dax_attribute_groups,
1299 struct dev_dax *devm_create_dev_dax(struct dev_dax_data *data)
1301 struct dax_region *dax_region = data->dax_region;
1302 struct device *parent = dax_region->dev;
1303 struct dax_device *dax_dev;
1304 struct dev_dax *dev_dax;
1305 struct inode *inode;
1309 dev_dax = kzalloc(sizeof(*dev_dax), GFP_KERNEL);
1311 return ERR_PTR(-ENOMEM);
1313 if (is_static(dax_region)) {
1314 if (dev_WARN_ONCE(parent, data->id < 0,
1315 "dynamic id specified to static region\n")) {
1320 dev_dax->id = data->id;
1322 if (dev_WARN_ONCE(parent, data->id >= 0,
1323 "static id specified to dynamic region\n")) {
1328 rc = ida_alloc(&dax_region->ida, GFP_KERNEL);
1334 dev_dax->region = dax_region;
1335 dev = &dev_dax->dev;
1336 device_initialize(dev);
1337 dev_set_name(dev, "dax%d.%d", dax_region->id, dev_dax->id);
1339 rc = alloc_dev_dax_range(dev_dax, dax_region->res.start, data->size);
1344 dev_WARN_ONCE(parent, !is_static(dax_region),
1345 "custom dev_pagemap requires a static dax_region\n");
1347 dev_dax->pgmap = kmemdup(data->pgmap,
1348 sizeof(struct dev_pagemap), GFP_KERNEL);
1349 if (!dev_dax->pgmap) {
1356 * No dax_operations since there is no access to this device outside of
1357 * mmap of the resulting character device.
1359 dax_dev = alloc_dax(dev_dax, NULL);
1360 if (IS_ERR(dax_dev)) {
1361 rc = PTR_ERR(dax_dev);
1364 set_dax_synchronous(dax_dev);
1365 set_dax_nocache(dax_dev);
1366 set_dax_nomc(dax_dev);
1368 /* a device_dax instance is dead while the driver is not attached */
1371 dev_dax->dax_dev = dax_dev;
1372 dev_dax->target_node = dax_region->target_node;
1373 dev_dax->align = dax_region->align;
1374 ida_init(&dev_dax->ida);
1375 kref_get(&dax_region->kref);
1377 inode = dax_inode(dax_dev);
1378 dev->devt = inode->i_rdev;
1379 dev->bus = &dax_bus_type;
1380 dev->parent = parent;
1381 dev->type = &dev_dax_type;
1383 rc = device_add(dev);
1385 kill_dev_dax(dev_dax);
1390 rc = devm_add_action_or_reset(dax_region->dev, unregister_dev_dax, dev);
1394 /* register mapping device for the initial allocation range */
1395 if (dev_dax->nr_range && range_len(&dev_dax->ranges[0].range)) {
1396 rc = devm_register_dax_mapping(dev_dax, 0);
1404 kfree(dev_dax->pgmap);
1406 free_dev_dax_ranges(dev_dax);
1408 free_dev_dax_id(dev_dax);
1414 EXPORT_SYMBOL_GPL(devm_create_dev_dax);
1416 static int match_always_count;
1418 int __dax_driver_register(struct dax_device_driver *dax_drv,
1419 struct module *module, const char *mod_name)
1421 struct device_driver *drv = &dax_drv->drv;
1425 * dax_bus_probe() calls dax_drv->probe() unconditionally.
1426 * So better be safe than sorry and ensure it is provided.
1428 if (!dax_drv->probe)
1431 INIT_LIST_HEAD(&dax_drv->ids);
1432 drv->owner = module;
1433 drv->name = mod_name;
1434 drv->mod_name = mod_name;
1435 drv->bus = &dax_bus_type;
1437 /* there can only be one default driver */
1438 mutex_lock(&dax_bus_lock);
1439 match_always_count += dax_drv->match_always;
1440 if (match_always_count > 1) {
1441 match_always_count--;
1445 mutex_unlock(&dax_bus_lock);
1449 rc = driver_register(drv);
1450 if (rc && dax_drv->match_always) {
1451 mutex_lock(&dax_bus_lock);
1452 match_always_count -= dax_drv->match_always;
1453 mutex_unlock(&dax_bus_lock);
1458 EXPORT_SYMBOL_GPL(__dax_driver_register);
1460 void dax_driver_unregister(struct dax_device_driver *dax_drv)
1462 struct device_driver *drv = &dax_drv->drv;
1463 struct dax_id *dax_id, *_id;
1465 mutex_lock(&dax_bus_lock);
1466 match_always_count -= dax_drv->match_always;
1467 list_for_each_entry_safe(dax_id, _id, &dax_drv->ids, list) {
1468 list_del(&dax_id->list);
1471 mutex_unlock(&dax_bus_lock);
1472 driver_unregister(drv);
1474 EXPORT_SYMBOL_GPL(dax_driver_unregister);
1476 int __init dax_bus_init(void)
1478 return bus_register(&dax_bus_type);
1481 void __exit dax_bus_exit(void)
1483 bus_unregister(&dax_bus_type);