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 struct class *dax_class;
15 static DEFINE_MUTEX(dax_bus_lock);
17 #define DAX_NAME_LEN 30
19 struct list_head list;
20 char dev_name[DAX_NAME_LEN];
23 static int dax_bus_uevent(struct device *dev, struct kobj_uevent_env *env)
26 * We only ever expect to handle device-dax instances, i.e. the
27 * @type argument to MODULE_ALIAS_DAX_DEVICE() is always zero
29 return add_uevent_var(env, "MODALIAS=" DAX_DEVICE_MODALIAS_FMT, 0);
32 static struct dax_device_driver *to_dax_drv(struct device_driver *drv)
34 return container_of(drv, struct dax_device_driver, drv);
37 static struct dax_id *__dax_match_id(struct dax_device_driver *dax_drv,
40 struct dax_id *dax_id;
42 lockdep_assert_held(&dax_bus_lock);
44 list_for_each_entry(dax_id, &dax_drv->ids, list)
45 if (sysfs_streq(dax_id->dev_name, dev_name))
50 static int dax_match_id(struct dax_device_driver *dax_drv, struct device *dev)
54 mutex_lock(&dax_bus_lock);
55 match = !!__dax_match_id(dax_drv, dev_name(dev));
56 mutex_unlock(&dax_bus_lock);
66 static ssize_t do_id_store(struct device_driver *drv, const char *buf,
67 size_t count, enum id_action action)
69 struct dax_device_driver *dax_drv = to_dax_drv(drv);
70 unsigned int region_id, id;
71 char devname[DAX_NAME_LEN];
72 struct dax_id *dax_id;
76 fields = sscanf(buf, "dax%d.%d", ®ion_id, &id);
79 sprintf(devname, "dax%d.%d", region_id, id);
80 if (!sysfs_streq(buf, devname))
83 mutex_lock(&dax_bus_lock);
84 dax_id = __dax_match_id(dax_drv, buf);
86 if (action == ID_ADD) {
87 dax_id = kzalloc(sizeof(*dax_id), GFP_KERNEL);
89 strncpy(dax_id->dev_name, buf, DAX_NAME_LEN);
90 list_add(&dax_id->list, &dax_drv->ids);
94 /* nothing to remove */;
95 } else if (action == ID_REMOVE) {
96 list_del(&dax_id->list);
99 /* dax_id already added */;
100 mutex_unlock(&dax_bus_lock);
104 if (action == ID_ADD)
105 rc = driver_attach(drv);
111 static ssize_t new_id_store(struct device_driver *drv, const char *buf,
114 return do_id_store(drv, buf, count, ID_ADD);
116 static DRIVER_ATTR_WO(new_id);
118 static ssize_t remove_id_store(struct device_driver *drv, const char *buf,
121 return do_id_store(drv, buf, count, ID_REMOVE);
123 static DRIVER_ATTR_WO(remove_id);
125 static struct attribute *dax_drv_attrs[] = {
126 &driver_attr_new_id.attr,
127 &driver_attr_remove_id.attr,
130 ATTRIBUTE_GROUPS(dax_drv);
132 static int dax_bus_match(struct device *dev, struct device_driver *drv);
134 static bool is_static(struct dax_region *dax_region)
136 return (dax_region->res.flags & IORESOURCE_DAX_STATIC) != 0;
139 static u64 dev_dax_size(struct dev_dax *dev_dax)
144 device_lock_assert(&dev_dax->dev);
146 for (i = 0; i < dev_dax->nr_range; i++)
147 size += range_len(&dev_dax->ranges[i].range);
152 static int dax_bus_probe(struct device *dev)
154 struct dax_device_driver *dax_drv = to_dax_drv(dev->driver);
155 struct dev_dax *dev_dax = to_dev_dax(dev);
156 struct dax_region *dax_region = dev_dax->region;
159 if (dev_dax_size(dev_dax) == 0 || dev_dax->id < 0)
162 rc = dax_drv->probe(dev_dax);
164 if (rc || is_static(dax_region))
168 * Track new seed creation only after successful probe of the
171 if (dax_region->seed == dev)
172 dax_region->seed = NULL;
177 static int dax_bus_remove(struct device *dev)
179 struct dax_device_driver *dax_drv = to_dax_drv(dev->driver);
180 struct dev_dax *dev_dax = to_dev_dax(dev);
182 return dax_drv->remove(dev_dax);
185 static struct bus_type dax_bus_type = {
187 .uevent = dax_bus_uevent,
188 .match = dax_bus_match,
189 .probe = dax_bus_probe,
190 .remove = dax_bus_remove,
191 .drv_groups = dax_drv_groups,
194 static int dax_bus_match(struct device *dev, struct device_driver *drv)
196 struct dax_device_driver *dax_drv = to_dax_drv(drv);
199 * All but the 'device-dax' driver, which has 'match_always'
200 * set, requires an exact id match.
202 if (dax_drv->match_always)
205 return dax_match_id(dax_drv, dev);
209 * Rely on the fact that drvdata is set before the attributes are
210 * registered, and that the attributes are unregistered before drvdata
211 * is cleared to assume that drvdata is always valid.
213 static ssize_t id_show(struct device *dev,
214 struct device_attribute *attr, char *buf)
216 struct dax_region *dax_region = dev_get_drvdata(dev);
218 return sprintf(buf, "%d\n", dax_region->id);
220 static DEVICE_ATTR_RO(id);
222 static ssize_t region_size_show(struct device *dev,
223 struct device_attribute *attr, char *buf)
225 struct dax_region *dax_region = dev_get_drvdata(dev);
227 return sprintf(buf, "%llu\n", (unsigned long long)
228 resource_size(&dax_region->res));
230 static struct device_attribute dev_attr_region_size = __ATTR(size, 0444,
231 region_size_show, NULL);
233 static ssize_t align_show(struct device *dev,
234 struct device_attribute *attr, char *buf)
236 struct dax_region *dax_region = dev_get_drvdata(dev);
238 return sprintf(buf, "%u\n", dax_region->align);
240 static DEVICE_ATTR_RO(align);
242 #define for_each_dax_region_resource(dax_region, res) \
243 for (res = (dax_region)->res.child; res; res = res->sibling)
245 static unsigned long long dax_region_avail_size(struct dax_region *dax_region)
247 resource_size_t size = resource_size(&dax_region->res);
248 struct resource *res;
250 device_lock_assert(dax_region->dev);
252 for_each_dax_region_resource(dax_region, res)
253 size -= resource_size(res);
257 static ssize_t available_size_show(struct device *dev,
258 struct device_attribute *attr, char *buf)
260 struct dax_region *dax_region = dev_get_drvdata(dev);
261 unsigned long long size;
264 size = dax_region_avail_size(dax_region);
267 return sprintf(buf, "%llu\n", size);
269 static DEVICE_ATTR_RO(available_size);
271 static ssize_t seed_show(struct device *dev,
272 struct device_attribute *attr, char *buf)
274 struct dax_region *dax_region = dev_get_drvdata(dev);
278 if (is_static(dax_region))
282 seed = dax_region->seed;
283 rc = sprintf(buf, "%s\n", seed ? dev_name(seed) : "");
288 static DEVICE_ATTR_RO(seed);
290 static ssize_t create_show(struct device *dev,
291 struct device_attribute *attr, char *buf)
293 struct dax_region *dax_region = dev_get_drvdata(dev);
294 struct device *youngest;
297 if (is_static(dax_region))
301 youngest = dax_region->youngest;
302 rc = sprintf(buf, "%s\n", youngest ? dev_name(youngest) : "");
308 static ssize_t create_store(struct device *dev, struct device_attribute *attr,
309 const char *buf, size_t len)
311 struct dax_region *dax_region = dev_get_drvdata(dev);
312 unsigned long long avail;
316 if (is_static(dax_region))
319 rc = kstrtoint(buf, 0, &val);
326 avail = dax_region_avail_size(dax_region);
330 struct dev_dax_data data = {
331 .dax_region = dax_region,
335 struct dev_dax *dev_dax = devm_create_dev_dax(&data);
338 rc = PTR_ERR(dev_dax);
341 * In support of crafting multiple new devices
342 * simultaneously multiple seeds can be created,
343 * but only the first one that has not been
344 * successfully bound is tracked as the region
347 if (!dax_region->seed)
348 dax_region->seed = &dev_dax->dev;
349 dax_region->youngest = &dev_dax->dev;
357 static DEVICE_ATTR_RW(create);
359 void kill_dev_dax(struct dev_dax *dev_dax)
361 struct dax_device *dax_dev = dev_dax->dax_dev;
362 struct inode *inode = dax_inode(dax_dev);
365 unmap_mapping_range(inode->i_mapping, 0, 0, 1);
367 EXPORT_SYMBOL_GPL(kill_dev_dax);
369 static void free_dev_dax_ranges(struct dev_dax *dev_dax)
371 struct dax_region *dax_region = dev_dax->region;
374 device_lock_assert(dax_region->dev);
375 for (i = 0; i < dev_dax->nr_range; i++) {
376 struct range *range = &dev_dax->ranges[i].range;
378 __release_region(&dax_region->res, range->start,
381 dev_dax->nr_range = 0;
384 static void unregister_dev_dax(void *dev)
386 struct dev_dax *dev_dax = to_dev_dax(dev);
388 dev_dbg(dev, "%s\n", __func__);
390 kill_dev_dax(dev_dax);
391 free_dev_dax_ranges(dev_dax);
396 /* a return value >= 0 indicates this invocation invalidated the id */
397 static int __free_dev_dax_id(struct dev_dax *dev_dax)
399 struct dax_region *dax_region = dev_dax->region;
400 struct device *dev = &dev_dax->dev;
401 int rc = dev_dax->id;
403 device_lock_assert(dev);
405 if (is_static(dax_region) || dev_dax->id < 0)
407 ida_free(&dax_region->ida, dev_dax->id);
412 static int free_dev_dax_id(struct dev_dax *dev_dax)
414 struct device *dev = &dev_dax->dev;
418 rc = __free_dev_dax_id(dev_dax);
423 static ssize_t delete_store(struct device *dev, struct device_attribute *attr,
424 const char *buf, size_t len)
426 struct dax_region *dax_region = dev_get_drvdata(dev);
427 struct dev_dax *dev_dax;
428 struct device *victim;
432 if (is_static(dax_region))
435 victim = device_find_child_by_name(dax_region->dev, buf);
441 dev_dax = to_dev_dax(victim);
442 if (victim->driver || dev_dax_size(dev_dax))
446 * Invalidate the device so it does not become active
447 * again, but always preserve device-id-0 so that
448 * /sys/bus/dax/ is guaranteed to be populated while any
449 * dax_region is registered.
451 if (dev_dax->id > 0) {
452 do_del = __free_dev_dax_id(dev_dax) >= 0;
454 if (dax_region->seed == victim)
455 dax_region->seed = NULL;
456 if (dax_region->youngest == victim)
457 dax_region->youngest = NULL;
461 device_unlock(victim);
463 /* won the race to invalidate the device, clean it up */
465 devm_release_action(dev, unregister_dev_dax, victim);
471 static DEVICE_ATTR_WO(delete);
473 static umode_t dax_region_visible(struct kobject *kobj, struct attribute *a,
476 struct device *dev = container_of(kobj, struct device, kobj);
477 struct dax_region *dax_region = dev_get_drvdata(dev);
479 if (is_static(dax_region))
480 if (a == &dev_attr_available_size.attr
481 || a == &dev_attr_create.attr
482 || a == &dev_attr_seed.attr
483 || a == &dev_attr_delete.attr)
488 static struct attribute *dax_region_attributes[] = {
489 &dev_attr_available_size.attr,
490 &dev_attr_region_size.attr,
491 &dev_attr_align.attr,
492 &dev_attr_create.attr,
494 &dev_attr_delete.attr,
499 static const struct attribute_group dax_region_attribute_group = {
500 .name = "dax_region",
501 .attrs = dax_region_attributes,
502 .is_visible = dax_region_visible,
505 static const struct attribute_group *dax_region_attribute_groups[] = {
506 &dax_region_attribute_group,
510 static void dax_region_free(struct kref *kref)
512 struct dax_region *dax_region;
514 dax_region = container_of(kref, struct dax_region, kref);
518 void dax_region_put(struct dax_region *dax_region)
520 kref_put(&dax_region->kref, dax_region_free);
522 EXPORT_SYMBOL_GPL(dax_region_put);
524 static void dax_region_unregister(void *region)
526 struct dax_region *dax_region = region;
528 sysfs_remove_groups(&dax_region->dev->kobj,
529 dax_region_attribute_groups);
530 dax_region_put(dax_region);
533 struct dax_region *alloc_dax_region(struct device *parent, int region_id,
534 struct range *range, int target_node, unsigned int align,
537 struct dax_region *dax_region;
540 * The DAX core assumes that it can store its private data in
541 * parent->driver_data. This WARN is a reminder / safeguard for
542 * developers of device-dax drivers.
544 if (dev_get_drvdata(parent)) {
545 dev_WARN(parent, "dax core failed to setup private data\n");
549 if (!IS_ALIGNED(range->start, align)
550 || !IS_ALIGNED(range_len(range), align))
553 dax_region = kzalloc(sizeof(*dax_region), GFP_KERNEL);
557 dev_set_drvdata(parent, dax_region);
558 kref_init(&dax_region->kref);
559 dax_region->id = region_id;
560 dax_region->align = align;
561 dax_region->dev = parent;
562 dax_region->target_node = target_node;
563 ida_init(&dax_region->ida);
564 dax_region->res = (struct resource) {
565 .start = range->start,
567 .flags = IORESOURCE_MEM | flags,
570 if (sysfs_create_groups(&parent->kobj, dax_region_attribute_groups)) {
575 kref_get(&dax_region->kref);
576 if (devm_add_action_or_reset(parent, dax_region_unregister, dax_region))
580 EXPORT_SYMBOL_GPL(alloc_dax_region);
582 static void dax_mapping_release(struct device *dev)
584 struct dax_mapping *mapping = to_dax_mapping(dev);
585 struct dev_dax *dev_dax = to_dev_dax(dev->parent);
587 ida_free(&dev_dax->ida, mapping->id);
591 static void unregister_dax_mapping(void *data)
593 struct device *dev = data;
594 struct dax_mapping *mapping = to_dax_mapping(dev);
595 struct dev_dax *dev_dax = to_dev_dax(dev->parent);
596 struct dax_region *dax_region = dev_dax->region;
598 dev_dbg(dev, "%s\n", __func__);
600 device_lock_assert(dax_region->dev);
602 dev_dax->ranges[mapping->range_id].mapping = NULL;
603 mapping->range_id = -1;
609 static struct dev_dax_range *get_dax_range(struct device *dev)
611 struct dax_mapping *mapping = to_dax_mapping(dev);
612 struct dev_dax *dev_dax = to_dev_dax(dev->parent);
613 struct dax_region *dax_region = dev_dax->region;
615 device_lock(dax_region->dev);
616 if (mapping->range_id < 0) {
617 device_unlock(dax_region->dev);
621 return &dev_dax->ranges[mapping->range_id];
624 static void put_dax_range(struct dev_dax_range *dax_range)
626 struct dax_mapping *mapping = dax_range->mapping;
627 struct dev_dax *dev_dax = to_dev_dax(mapping->dev.parent);
628 struct dax_region *dax_region = dev_dax->region;
630 device_unlock(dax_region->dev);
633 static ssize_t start_show(struct device *dev,
634 struct device_attribute *attr, char *buf)
636 struct dev_dax_range *dax_range;
639 dax_range = get_dax_range(dev);
642 rc = sprintf(buf, "%#llx\n", dax_range->range.start);
643 put_dax_range(dax_range);
647 static DEVICE_ATTR(start, 0400, start_show, NULL);
649 static ssize_t end_show(struct device *dev,
650 struct device_attribute *attr, char *buf)
652 struct dev_dax_range *dax_range;
655 dax_range = get_dax_range(dev);
658 rc = sprintf(buf, "%#llx\n", dax_range->range.end);
659 put_dax_range(dax_range);
663 static DEVICE_ATTR(end, 0400, end_show, NULL);
665 static ssize_t pgoff_show(struct device *dev,
666 struct device_attribute *attr, char *buf)
668 struct dev_dax_range *dax_range;
671 dax_range = get_dax_range(dev);
674 rc = sprintf(buf, "%#lx\n", dax_range->pgoff);
675 put_dax_range(dax_range);
679 static DEVICE_ATTR(page_offset, 0400, pgoff_show, NULL);
681 static struct attribute *dax_mapping_attributes[] = {
682 &dev_attr_start.attr,
684 &dev_attr_page_offset.attr,
688 static const struct attribute_group dax_mapping_attribute_group = {
689 .attrs = dax_mapping_attributes,
692 static const struct attribute_group *dax_mapping_attribute_groups[] = {
693 &dax_mapping_attribute_group,
697 static struct device_type dax_mapping_type = {
698 .release = dax_mapping_release,
699 .groups = dax_mapping_attribute_groups,
702 static int devm_register_dax_mapping(struct dev_dax *dev_dax, int range_id)
704 struct dax_region *dax_region = dev_dax->region;
705 struct dax_mapping *mapping;
709 device_lock_assert(dax_region->dev);
711 if (dev_WARN_ONCE(&dev_dax->dev, !dax_region->dev->driver,
712 "region disabled\n"))
715 mapping = kzalloc(sizeof(*mapping), GFP_KERNEL);
718 mapping->range_id = range_id;
719 mapping->id = ida_alloc(&dev_dax->ida, GFP_KERNEL);
720 if (mapping->id < 0) {
724 dev_dax->ranges[range_id].mapping = mapping;
726 device_initialize(dev);
727 dev->parent = &dev_dax->dev;
728 dev->type = &dax_mapping_type;
729 dev_set_name(dev, "mapping%d", mapping->id);
730 rc = device_add(dev);
736 rc = devm_add_action_or_reset(dax_region->dev, unregister_dax_mapping,
743 static int alloc_dev_dax_range(struct dev_dax *dev_dax, u64 start,
744 resource_size_t size)
746 struct dax_region *dax_region = dev_dax->region;
747 struct resource *res = &dax_region->res;
748 struct device *dev = &dev_dax->dev;
749 struct dev_dax_range *ranges;
750 unsigned long pgoff = 0;
751 struct resource *alloc;
754 device_lock_assert(dax_region->dev);
756 /* handle the seed alloc special case */
758 if (dev_WARN_ONCE(dev, dev_dax->nr_range,
759 "0-size allocation must be first\n"))
761 /* nr_range == 0 is elsewhere special cased as 0-size device */
765 ranges = krealloc(dev_dax->ranges, sizeof(*ranges)
766 * (dev_dax->nr_range + 1), GFP_KERNEL);
770 alloc = __request_region(res, start, size, dev_name(dev), 0);
773 * If this was an empty set of ranges nothing else
774 * will release @ranges, so do it now.
776 if (!dev_dax->nr_range) {
780 dev_dax->ranges = ranges;
784 for (i = 0; i < dev_dax->nr_range; i++)
785 pgoff += PHYS_PFN(range_len(&ranges[i].range));
786 dev_dax->ranges = ranges;
787 ranges[dev_dax->nr_range++] = (struct dev_dax_range) {
790 .start = alloc->start,
795 dev_dbg(dev, "alloc range[%d]: %pa:%pa\n", dev_dax->nr_range - 1,
796 &alloc->start, &alloc->end);
798 * A dev_dax instance must be registered before mapping device
799 * children can be added. Defer to devm_create_dev_dax() to add
800 * the initial mapping device.
802 if (!device_is_registered(&dev_dax->dev))
805 rc = devm_register_dax_mapping(dev_dax, dev_dax->nr_range - 1);
807 dev_dbg(dev, "delete range[%d]: %pa:%pa\n", dev_dax->nr_range - 1,
808 &alloc->start, &alloc->end);
810 __release_region(res, alloc->start, resource_size(alloc));
817 static int adjust_dev_dax_range(struct dev_dax *dev_dax, struct resource *res, resource_size_t size)
819 int last_range = dev_dax->nr_range - 1;
820 struct dev_dax_range *dax_range = &dev_dax->ranges[last_range];
821 struct dax_region *dax_region = dev_dax->region;
822 bool is_shrink = resource_size(res) > size;
823 struct range *range = &dax_range->range;
824 struct device *dev = &dev_dax->dev;
827 device_lock_assert(dax_region->dev);
829 if (dev_WARN_ONCE(dev, !size, "deletion is handled by dev_dax_shrink\n"))
832 rc = adjust_resource(res, range->start, size);
836 *range = (struct range) {
837 .start = range->start,
838 .end = range->start + size - 1,
841 dev_dbg(dev, "%s range[%d]: %#llx:%#llx\n", is_shrink ? "shrink" : "extend",
842 last_range, (unsigned long long) range->start,
843 (unsigned long long) range->end);
848 static ssize_t size_show(struct device *dev,
849 struct device_attribute *attr, char *buf)
851 struct dev_dax *dev_dax = to_dev_dax(dev);
852 unsigned long long size;
855 size = dev_dax_size(dev_dax);
858 return sprintf(buf, "%llu\n", size);
861 static bool alloc_is_aligned(struct dax_region *dax_region,
862 resource_size_t size)
865 * The minimum mapping granularity for a device instance is a
866 * single subsection, unless the arch says otherwise.
868 return IS_ALIGNED(size, max_t(unsigned long, dax_region->align,
869 memremap_compat_align()));
872 static int dev_dax_shrink(struct dev_dax *dev_dax, resource_size_t size)
874 resource_size_t to_shrink = dev_dax_size(dev_dax) - size;
875 struct dax_region *dax_region = dev_dax->region;
876 struct device *dev = &dev_dax->dev;
879 for (i = dev_dax->nr_range - 1; i >= 0; i--) {
880 struct range *range = &dev_dax->ranges[i].range;
881 struct dax_mapping *mapping = dev_dax->ranges[i].mapping;
882 struct resource *adjust = NULL, *res;
883 resource_size_t shrink;
885 shrink = min_t(u64, to_shrink, range_len(range));
886 if (shrink >= range_len(range)) {
887 devm_release_action(dax_region->dev,
888 unregister_dax_mapping, &mapping->dev);
889 __release_region(&dax_region->res, range->start,
892 dev_dbg(dev, "delete range[%d]: %#llx:%#llx\n", i,
893 (unsigned long long) range->start,
894 (unsigned long long) range->end);
901 for_each_dax_region_resource(dax_region, res)
902 if (strcmp(res->name, dev_name(dev)) == 0
903 && res->start == range->start) {
908 if (dev_WARN_ONCE(dev, !adjust || i != dev_dax->nr_range - 1,
909 "failed to find matching resource\n"))
911 return adjust_dev_dax_range(dev_dax, adjust, range_len(range)
918 * Only allow adjustments that preserve the relative pgoff of existing
919 * allocations. I.e. the dev_dax->ranges array is ordered by increasing pgoff.
921 static bool adjust_ok(struct dev_dax *dev_dax, struct resource *res)
923 struct dev_dax_range *last;
926 if (dev_dax->nr_range == 0)
928 if (strcmp(res->name, dev_name(&dev_dax->dev)) != 0)
930 last = &dev_dax->ranges[dev_dax->nr_range - 1];
931 if (last->range.start != res->start || last->range.end != res->end)
933 for (i = 0; i < dev_dax->nr_range - 1; i++) {
934 struct dev_dax_range *dax_range = &dev_dax->ranges[i];
936 if (dax_range->pgoff > last->pgoff)
943 static ssize_t dev_dax_resize(struct dax_region *dax_region,
944 struct dev_dax *dev_dax, resource_size_t size)
946 resource_size_t avail = dax_region_avail_size(dax_region), to_alloc;
947 resource_size_t dev_size = dev_dax_size(dev_dax);
948 struct resource *region_res = &dax_region->res;
949 struct device *dev = &dev_dax->dev;
950 struct resource *res, *first;
951 resource_size_t alloc = 0;
956 if (size == dev_size)
958 if (size > dev_size && size - dev_size > avail)
961 return dev_dax_shrink(dev_dax, size);
963 to_alloc = size - dev_size;
964 if (dev_WARN_ONCE(dev, !alloc_is_aligned(dax_region, to_alloc),
965 "resize of %pa misaligned\n", &to_alloc))
969 * Expand the device into the unused portion of the region. This
970 * may involve adjusting the end of an existing resource, or
971 * allocating a new resource.
974 first = region_res->child;
976 return alloc_dev_dax_range(dev_dax, dax_region->res.start, to_alloc);
979 for (res = first; res; res = res->sibling) {
980 struct resource *next = res->sibling;
982 /* space at the beginning of the region */
983 if (res == first && res->start > dax_region->res.start) {
984 alloc = min(res->start - dax_region->res.start, to_alloc);
985 rc = alloc_dev_dax_range(dev_dax, dax_region->res.start, alloc);
990 /* space between allocations */
991 if (next && next->start > res->end + 1)
992 alloc = min(next->start - (res->end + 1), to_alloc);
994 /* space at the end of the region */
995 if (!alloc && !next && res->end < region_res->end)
996 alloc = min(region_res->end - res->end, to_alloc);
1001 if (adjust_ok(dev_dax, res)) {
1002 rc = adjust_dev_dax_range(dev_dax, res, resource_size(res) + alloc);
1005 rc = alloc_dev_dax_range(dev_dax, res->end + 1, alloc);
1016 static ssize_t size_store(struct device *dev, struct device_attribute *attr,
1017 const char *buf, size_t len)
1020 unsigned long long val;
1021 struct dev_dax *dev_dax = to_dev_dax(dev);
1022 struct dax_region *dax_region = dev_dax->region;
1024 rc = kstrtoull(buf, 0, &val);
1028 if (!alloc_is_aligned(dax_region, val)) {
1029 dev_dbg(dev, "%s: size: %lld misaligned\n", __func__, val);
1033 device_lock(dax_region->dev);
1034 if (!dax_region->dev->driver) {
1035 device_unlock(dax_region->dev);
1039 rc = dev_dax_resize(dax_region, dev_dax, val);
1041 device_unlock(dax_region->dev);
1043 return rc == 0 ? len : rc;
1045 static DEVICE_ATTR_RW(size);
1047 static int dev_dax_target_node(struct dev_dax *dev_dax)
1049 struct dax_region *dax_region = dev_dax->region;
1051 return dax_region->target_node;
1054 static ssize_t target_node_show(struct device *dev,
1055 struct device_attribute *attr, char *buf)
1057 struct dev_dax *dev_dax = to_dev_dax(dev);
1059 return sprintf(buf, "%d\n", dev_dax_target_node(dev_dax));
1061 static DEVICE_ATTR_RO(target_node);
1063 static ssize_t resource_show(struct device *dev,
1064 struct device_attribute *attr, char *buf)
1066 struct dev_dax *dev_dax = to_dev_dax(dev);
1067 struct dax_region *dax_region = dev_dax->region;
1068 unsigned long long start;
1070 if (dev_dax->nr_range < 1)
1071 start = dax_region->res.start;
1073 start = dev_dax->ranges[0].range.start;
1075 return sprintf(buf, "%#llx\n", start);
1077 static DEVICE_ATTR(resource, 0400, resource_show, NULL);
1079 static ssize_t modalias_show(struct device *dev, struct device_attribute *attr,
1083 * We only ever expect to handle device-dax instances, i.e. the
1084 * @type argument to MODULE_ALIAS_DAX_DEVICE() is always zero
1086 return sprintf(buf, DAX_DEVICE_MODALIAS_FMT "\n", 0);
1088 static DEVICE_ATTR_RO(modalias);
1090 static ssize_t numa_node_show(struct device *dev,
1091 struct device_attribute *attr, char *buf)
1093 return sprintf(buf, "%d\n", dev_to_node(dev));
1095 static DEVICE_ATTR_RO(numa_node);
1097 static umode_t dev_dax_visible(struct kobject *kobj, struct attribute *a, int n)
1099 struct device *dev = container_of(kobj, struct device, kobj);
1100 struct dev_dax *dev_dax = to_dev_dax(dev);
1101 struct dax_region *dax_region = dev_dax->region;
1103 if (a == &dev_attr_target_node.attr && dev_dax_target_node(dev_dax) < 0)
1105 if (a == &dev_attr_numa_node.attr && !IS_ENABLED(CONFIG_NUMA))
1107 if (a == &dev_attr_size.attr && is_static(dax_region))
1112 static struct attribute *dev_dax_attributes[] = {
1113 &dev_attr_modalias.attr,
1114 &dev_attr_size.attr,
1115 &dev_attr_target_node.attr,
1116 &dev_attr_resource.attr,
1117 &dev_attr_numa_node.attr,
1121 static const struct attribute_group dev_dax_attribute_group = {
1122 .attrs = dev_dax_attributes,
1123 .is_visible = dev_dax_visible,
1126 static const struct attribute_group *dax_attribute_groups[] = {
1127 &dev_dax_attribute_group,
1131 static void dev_dax_release(struct device *dev)
1133 struct dev_dax *dev_dax = to_dev_dax(dev);
1134 struct dax_region *dax_region = dev_dax->region;
1135 struct dax_device *dax_dev = dev_dax->dax_dev;
1138 free_dev_dax_id(dev_dax);
1139 dax_region_put(dax_region);
1140 kfree(dev_dax->ranges);
1141 kfree(dev_dax->pgmap);
1145 static const struct device_type dev_dax_type = {
1146 .release = dev_dax_release,
1147 .groups = dax_attribute_groups,
1150 struct dev_dax *devm_create_dev_dax(struct dev_dax_data *data)
1152 struct dax_region *dax_region = data->dax_region;
1153 struct device *parent = dax_region->dev;
1154 struct dax_device *dax_dev;
1155 struct dev_dax *dev_dax;
1156 struct inode *inode;
1160 dev_dax = kzalloc(sizeof(*dev_dax), GFP_KERNEL);
1162 return ERR_PTR(-ENOMEM);
1164 if (is_static(dax_region)) {
1165 if (dev_WARN_ONCE(parent, data->id < 0,
1166 "dynamic id specified to static region\n")) {
1171 dev_dax->id = data->id;
1173 if (dev_WARN_ONCE(parent, data->id >= 0,
1174 "static id specified to dynamic region\n")) {
1179 rc = ida_alloc(&dax_region->ida, GFP_KERNEL);
1185 dev_dax->region = dax_region;
1186 dev = &dev_dax->dev;
1187 device_initialize(dev);
1188 dev_set_name(dev, "dax%d.%d", dax_region->id, dev_dax->id);
1190 rc = alloc_dev_dax_range(dev_dax, dax_region->res.start, data->size);
1195 dev_WARN_ONCE(parent, !is_static(dax_region),
1196 "custom dev_pagemap requires a static dax_region\n");
1198 dev_dax->pgmap = kmemdup(data->pgmap,
1199 sizeof(struct dev_pagemap), GFP_KERNEL);
1200 if (!dev_dax->pgmap) {
1207 * No 'host' or dax_operations since there is no access to this
1208 * device outside of mmap of the resulting character device.
1210 dax_dev = alloc_dax(dev_dax, NULL, NULL, DAXDEV_F_SYNC);
1211 if (IS_ERR(dax_dev)) {
1212 rc = PTR_ERR(dax_dev);
1216 /* a device_dax instance is dead while the driver is not attached */
1219 dev_dax->dax_dev = dax_dev;
1220 dev_dax->target_node = dax_region->target_node;
1221 dev_dax->align = dax_region->align;
1222 ida_init(&dev_dax->ida);
1223 kref_get(&dax_region->kref);
1225 inode = dax_inode(dax_dev);
1226 dev->devt = inode->i_rdev;
1227 if (data->subsys == DEV_DAX_BUS)
1228 dev->bus = &dax_bus_type;
1230 dev->class = dax_class;
1231 dev->parent = parent;
1232 dev->type = &dev_dax_type;
1234 rc = device_add(dev);
1236 kill_dev_dax(dev_dax);
1241 rc = devm_add_action_or_reset(dax_region->dev, unregister_dev_dax, dev);
1245 /* register mapping device for the initial allocation range */
1246 if (dev_dax->nr_range && range_len(&dev_dax->ranges[0].range)) {
1247 rc = devm_register_dax_mapping(dev_dax, 0);
1255 kfree(dev_dax->pgmap);
1257 free_dev_dax_ranges(dev_dax);
1259 free_dev_dax_id(dev_dax);
1265 EXPORT_SYMBOL_GPL(devm_create_dev_dax);
1267 static int match_always_count;
1269 int __dax_driver_register(struct dax_device_driver *dax_drv,
1270 struct module *module, const char *mod_name)
1272 struct device_driver *drv = &dax_drv->drv;
1275 INIT_LIST_HEAD(&dax_drv->ids);
1276 drv->owner = module;
1277 drv->name = mod_name;
1278 drv->mod_name = mod_name;
1279 drv->bus = &dax_bus_type;
1281 /* there can only be one default driver */
1282 mutex_lock(&dax_bus_lock);
1283 match_always_count += dax_drv->match_always;
1284 if (match_always_count > 1) {
1285 match_always_count--;
1289 mutex_unlock(&dax_bus_lock);
1292 return driver_register(drv);
1294 EXPORT_SYMBOL_GPL(__dax_driver_register);
1296 void dax_driver_unregister(struct dax_device_driver *dax_drv)
1298 struct device_driver *drv = &dax_drv->drv;
1299 struct dax_id *dax_id, *_id;
1301 mutex_lock(&dax_bus_lock);
1302 match_always_count -= dax_drv->match_always;
1303 list_for_each_entry_safe(dax_id, _id, &dax_drv->ids, list) {
1304 list_del(&dax_id->list);
1307 mutex_unlock(&dax_bus_lock);
1308 driver_unregister(drv);
1310 EXPORT_SYMBOL_GPL(dax_driver_unregister);
1312 int __init dax_bus_init(void)
1316 if (IS_ENABLED(CONFIG_DEV_DAX_PMEM_COMPAT)) {
1317 dax_class = class_create(THIS_MODULE, "dax");
1318 if (IS_ERR(dax_class))
1319 return PTR_ERR(dax_class);
1322 rc = bus_register(&dax_bus_type);
1324 class_destroy(dax_class);
1328 void __exit dax_bus_exit(void)
1330 bus_unregister(&dax_bus_type);
1331 class_destroy(dax_class);