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 region_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 struct device_attribute dev_attr_region_align =
241 __ATTR(align, 0400, region_align_show, NULL);
243 #define for_each_dax_region_resource(dax_region, res) \
244 for (res = (dax_region)->res.child; res; res = res->sibling)
246 static unsigned long long dax_region_avail_size(struct dax_region *dax_region)
248 resource_size_t size = resource_size(&dax_region->res);
249 struct resource *res;
251 device_lock_assert(dax_region->dev);
253 for_each_dax_region_resource(dax_region, res)
254 size -= resource_size(res);
258 static ssize_t available_size_show(struct device *dev,
259 struct device_attribute *attr, char *buf)
261 struct dax_region *dax_region = dev_get_drvdata(dev);
262 unsigned long long size;
265 size = dax_region_avail_size(dax_region);
268 return sprintf(buf, "%llu\n", size);
270 static DEVICE_ATTR_RO(available_size);
272 static ssize_t seed_show(struct device *dev,
273 struct device_attribute *attr, char *buf)
275 struct dax_region *dax_region = dev_get_drvdata(dev);
279 if (is_static(dax_region))
283 seed = dax_region->seed;
284 rc = sprintf(buf, "%s\n", seed ? dev_name(seed) : "");
289 static DEVICE_ATTR_RO(seed);
291 static ssize_t create_show(struct device *dev,
292 struct device_attribute *attr, char *buf)
294 struct dax_region *dax_region = dev_get_drvdata(dev);
295 struct device *youngest;
298 if (is_static(dax_region))
302 youngest = dax_region->youngest;
303 rc = sprintf(buf, "%s\n", youngest ? dev_name(youngest) : "");
309 static ssize_t create_store(struct device *dev, struct device_attribute *attr,
310 const char *buf, size_t len)
312 struct dax_region *dax_region = dev_get_drvdata(dev);
313 unsigned long long avail;
317 if (is_static(dax_region))
320 rc = kstrtoint(buf, 0, &val);
327 avail = dax_region_avail_size(dax_region);
331 struct dev_dax_data data = {
332 .dax_region = dax_region,
336 struct dev_dax *dev_dax = devm_create_dev_dax(&data);
339 rc = PTR_ERR(dev_dax);
342 * In support of crafting multiple new devices
343 * simultaneously multiple seeds can be created,
344 * but only the first one that has not been
345 * successfully bound is tracked as the region
348 if (!dax_region->seed)
349 dax_region->seed = &dev_dax->dev;
350 dax_region->youngest = &dev_dax->dev;
358 static DEVICE_ATTR_RW(create);
360 void kill_dev_dax(struct dev_dax *dev_dax)
362 struct dax_device *dax_dev = dev_dax->dax_dev;
363 struct inode *inode = dax_inode(dax_dev);
366 unmap_mapping_range(inode->i_mapping, 0, 0, 1);
368 EXPORT_SYMBOL_GPL(kill_dev_dax);
370 static void trim_dev_dax_range(struct dev_dax *dev_dax)
372 int i = dev_dax->nr_range - 1;
373 struct range *range = &dev_dax->ranges[i].range;
374 struct dax_region *dax_region = dev_dax->region;
376 device_lock_assert(dax_region->dev);
377 dev_dbg(&dev_dax->dev, "delete range[%d]: %#llx:%#llx\n", i,
378 (unsigned long long)range->start,
379 (unsigned long long)range->end);
381 __release_region(&dax_region->res, range->start, range_len(range));
382 if (--dev_dax->nr_range == 0) {
383 kfree(dev_dax->ranges);
384 dev_dax->ranges = NULL;
388 static void free_dev_dax_ranges(struct dev_dax *dev_dax)
390 while (dev_dax->nr_range)
391 trim_dev_dax_range(dev_dax);
394 static void unregister_dev_dax(void *dev)
396 struct dev_dax *dev_dax = to_dev_dax(dev);
398 dev_dbg(dev, "%s\n", __func__);
400 kill_dev_dax(dev_dax);
402 free_dev_dax_ranges(dev_dax);
406 static void dax_region_free(struct kref *kref)
408 struct dax_region *dax_region;
410 dax_region = container_of(kref, struct dax_region, kref);
414 void dax_region_put(struct dax_region *dax_region)
416 kref_put(&dax_region->kref, dax_region_free);
418 EXPORT_SYMBOL_GPL(dax_region_put);
420 /* a return value >= 0 indicates this invocation invalidated the id */
421 static int __free_dev_dax_id(struct dev_dax *dev_dax)
423 struct device *dev = &dev_dax->dev;
424 struct dax_region *dax_region;
425 int rc = dev_dax->id;
427 device_lock_assert(dev);
429 if (!dev_dax->dyn_id || dev_dax->id < 0)
431 dax_region = dev_dax->region;
432 ida_free(&dax_region->ida, dev_dax->id);
433 dax_region_put(dax_region);
438 static int free_dev_dax_id(struct dev_dax *dev_dax)
440 struct device *dev = &dev_dax->dev;
444 rc = __free_dev_dax_id(dev_dax);
449 static int alloc_dev_dax_id(struct dev_dax *dev_dax)
451 struct dax_region *dax_region = dev_dax->region;
454 id = ida_alloc(&dax_region->ida, GFP_KERNEL);
457 kref_get(&dax_region->kref);
458 dev_dax->dyn_id = true;
463 static ssize_t delete_store(struct device *dev, struct device_attribute *attr,
464 const char *buf, size_t len)
466 struct dax_region *dax_region = dev_get_drvdata(dev);
467 struct dev_dax *dev_dax;
468 struct device *victim;
472 if (is_static(dax_region))
475 victim = device_find_child_by_name(dax_region->dev, buf);
481 dev_dax = to_dev_dax(victim);
482 if (victim->driver || dev_dax_size(dev_dax))
486 * Invalidate the device so it does not become active
487 * again, but always preserve device-id-0 so that
488 * /sys/bus/dax/ is guaranteed to be populated while any
489 * dax_region is registered.
491 if (dev_dax->id > 0) {
492 do_del = __free_dev_dax_id(dev_dax) >= 0;
494 if (dax_region->seed == victim)
495 dax_region->seed = NULL;
496 if (dax_region->youngest == victim)
497 dax_region->youngest = NULL;
501 device_unlock(victim);
503 /* won the race to invalidate the device, clean it up */
505 devm_release_action(dev, unregister_dev_dax, victim);
511 static DEVICE_ATTR_WO(delete);
513 static umode_t dax_region_visible(struct kobject *kobj, struct attribute *a,
516 struct device *dev = container_of(kobj, struct device, kobj);
517 struct dax_region *dax_region = dev_get_drvdata(dev);
519 if (is_static(dax_region))
520 if (a == &dev_attr_available_size.attr
521 || a == &dev_attr_create.attr
522 || a == &dev_attr_seed.attr
523 || a == &dev_attr_delete.attr)
528 static struct attribute *dax_region_attributes[] = {
529 &dev_attr_available_size.attr,
530 &dev_attr_region_size.attr,
531 &dev_attr_region_align.attr,
532 &dev_attr_create.attr,
534 &dev_attr_delete.attr,
539 static const struct attribute_group dax_region_attribute_group = {
540 .name = "dax_region",
541 .attrs = dax_region_attributes,
542 .is_visible = dax_region_visible,
545 static const struct attribute_group *dax_region_attribute_groups[] = {
546 &dax_region_attribute_group,
550 static void dax_region_unregister(void *region)
552 struct dax_region *dax_region = region;
554 sysfs_remove_groups(&dax_region->dev->kobj,
555 dax_region_attribute_groups);
556 dax_region_put(dax_region);
559 struct dax_region *alloc_dax_region(struct device *parent, int region_id,
560 struct range *range, int target_node, unsigned int align,
563 struct dax_region *dax_region;
566 * The DAX core assumes that it can store its private data in
567 * parent->driver_data. This WARN is a reminder / safeguard for
568 * developers of device-dax drivers.
570 if (dev_get_drvdata(parent)) {
571 dev_WARN(parent, "dax core failed to setup private data\n");
575 if (!IS_ALIGNED(range->start, align)
576 || !IS_ALIGNED(range_len(range), align))
579 dax_region = kzalloc(sizeof(*dax_region), GFP_KERNEL);
583 dev_set_drvdata(parent, dax_region);
584 kref_init(&dax_region->kref);
585 dax_region->id = region_id;
586 dax_region->align = align;
587 dax_region->dev = parent;
588 dax_region->target_node = target_node;
589 ida_init(&dax_region->ida);
590 dax_region->res = (struct resource) {
591 .start = range->start,
593 .flags = IORESOURCE_MEM | flags,
596 if (sysfs_create_groups(&parent->kobj, dax_region_attribute_groups)) {
601 kref_get(&dax_region->kref);
602 if (devm_add_action_or_reset(parent, dax_region_unregister, dax_region))
606 EXPORT_SYMBOL_GPL(alloc_dax_region);
608 static void dax_mapping_release(struct device *dev)
610 struct dax_mapping *mapping = to_dax_mapping(dev);
611 struct device *parent = dev->parent;
612 struct dev_dax *dev_dax = to_dev_dax(parent);
614 ida_free(&dev_dax->ida, mapping->id);
619 static void unregister_dax_mapping(void *data)
621 struct device *dev = data;
622 struct dax_mapping *mapping = to_dax_mapping(dev);
623 struct dev_dax *dev_dax = to_dev_dax(dev->parent);
624 struct dax_region *dax_region = dev_dax->region;
626 dev_dbg(dev, "%s\n", __func__);
628 device_lock_assert(dax_region->dev);
630 dev_dax->ranges[mapping->range_id].mapping = NULL;
631 mapping->range_id = -1;
637 static struct dev_dax_range *get_dax_range(struct device *dev)
639 struct dax_mapping *mapping = to_dax_mapping(dev);
640 struct dev_dax *dev_dax = to_dev_dax(dev->parent);
641 struct dax_region *dax_region = dev_dax->region;
643 device_lock(dax_region->dev);
644 if (mapping->range_id < 0) {
645 device_unlock(dax_region->dev);
649 return &dev_dax->ranges[mapping->range_id];
652 static void put_dax_range(struct dev_dax_range *dax_range)
654 struct dax_mapping *mapping = dax_range->mapping;
655 struct dev_dax *dev_dax = to_dev_dax(mapping->dev.parent);
656 struct dax_region *dax_region = dev_dax->region;
658 device_unlock(dax_region->dev);
661 static ssize_t start_show(struct device *dev,
662 struct device_attribute *attr, char *buf)
664 struct dev_dax_range *dax_range;
667 dax_range = get_dax_range(dev);
670 rc = sprintf(buf, "%#llx\n", dax_range->range.start);
671 put_dax_range(dax_range);
675 static DEVICE_ATTR(start, 0400, start_show, NULL);
677 static ssize_t end_show(struct device *dev,
678 struct device_attribute *attr, char *buf)
680 struct dev_dax_range *dax_range;
683 dax_range = get_dax_range(dev);
686 rc = sprintf(buf, "%#llx\n", dax_range->range.end);
687 put_dax_range(dax_range);
691 static DEVICE_ATTR(end, 0400, end_show, NULL);
693 static ssize_t pgoff_show(struct device *dev,
694 struct device_attribute *attr, char *buf)
696 struct dev_dax_range *dax_range;
699 dax_range = get_dax_range(dev);
702 rc = sprintf(buf, "%#lx\n", dax_range->pgoff);
703 put_dax_range(dax_range);
707 static DEVICE_ATTR(page_offset, 0400, pgoff_show, NULL);
709 static struct attribute *dax_mapping_attributes[] = {
710 &dev_attr_start.attr,
712 &dev_attr_page_offset.attr,
716 static const struct attribute_group dax_mapping_attribute_group = {
717 .attrs = dax_mapping_attributes,
720 static const struct attribute_group *dax_mapping_attribute_groups[] = {
721 &dax_mapping_attribute_group,
725 static struct device_type dax_mapping_type = {
726 .release = dax_mapping_release,
727 .groups = dax_mapping_attribute_groups,
730 static int devm_register_dax_mapping(struct dev_dax *dev_dax, int range_id)
732 struct dax_region *dax_region = dev_dax->region;
733 struct dax_mapping *mapping;
737 device_lock_assert(dax_region->dev);
739 if (dev_WARN_ONCE(&dev_dax->dev, !dax_region->dev->driver,
740 "region disabled\n"))
743 mapping = kzalloc(sizeof(*mapping), GFP_KERNEL);
746 mapping->range_id = range_id;
747 mapping->id = ida_alloc(&dev_dax->ida, GFP_KERNEL);
748 if (mapping->id < 0) {
752 dev_dax->ranges[range_id].mapping = mapping;
754 device_initialize(dev);
755 dev->parent = &dev_dax->dev;
756 get_device(dev->parent);
757 dev->type = &dax_mapping_type;
758 dev_set_name(dev, "mapping%d", mapping->id);
759 rc = device_add(dev);
765 rc = devm_add_action_or_reset(dax_region->dev, unregister_dax_mapping,
772 static int alloc_dev_dax_range(struct dev_dax *dev_dax, u64 start,
773 resource_size_t size)
775 struct dax_region *dax_region = dev_dax->region;
776 struct resource *res = &dax_region->res;
777 struct device *dev = &dev_dax->dev;
778 struct dev_dax_range *ranges;
779 unsigned long pgoff = 0;
780 struct resource *alloc;
783 device_lock_assert(dax_region->dev);
785 /* handle the seed alloc special case */
787 if (dev_WARN_ONCE(dev, dev_dax->nr_range,
788 "0-size allocation must be first\n"))
790 /* nr_range == 0 is elsewhere special cased as 0-size device */
794 ranges = krealloc(dev_dax->ranges, sizeof(*ranges)
795 * (dev_dax->nr_range + 1), GFP_KERNEL);
799 alloc = __request_region(res, start, size, dev_name(dev), 0);
802 * If this was an empty set of ranges nothing else
803 * will release @ranges, so do it now.
805 if (!dev_dax->nr_range) {
809 dev_dax->ranges = ranges;
813 for (i = 0; i < dev_dax->nr_range; i++)
814 pgoff += PHYS_PFN(range_len(&ranges[i].range));
815 dev_dax->ranges = ranges;
816 ranges[dev_dax->nr_range++] = (struct dev_dax_range) {
819 .start = alloc->start,
824 dev_dbg(dev, "alloc range[%d]: %pa:%pa\n", dev_dax->nr_range - 1,
825 &alloc->start, &alloc->end);
827 * A dev_dax instance must be registered before mapping device
828 * children can be added. Defer to devm_create_dev_dax() to add
829 * the initial mapping device.
831 if (!device_is_registered(&dev_dax->dev))
834 rc = devm_register_dax_mapping(dev_dax, dev_dax->nr_range - 1);
836 trim_dev_dax_range(dev_dax);
841 static int adjust_dev_dax_range(struct dev_dax *dev_dax, struct resource *res, resource_size_t size)
843 int last_range = dev_dax->nr_range - 1;
844 struct dev_dax_range *dax_range = &dev_dax->ranges[last_range];
845 struct dax_region *dax_region = dev_dax->region;
846 bool is_shrink = resource_size(res) > size;
847 struct range *range = &dax_range->range;
848 struct device *dev = &dev_dax->dev;
851 device_lock_assert(dax_region->dev);
853 if (dev_WARN_ONCE(dev, !size, "deletion is handled by dev_dax_shrink\n"))
856 rc = adjust_resource(res, range->start, size);
860 *range = (struct range) {
861 .start = range->start,
862 .end = range->start + size - 1,
865 dev_dbg(dev, "%s range[%d]: %#llx:%#llx\n", is_shrink ? "shrink" : "extend",
866 last_range, (unsigned long long) range->start,
867 (unsigned long long) range->end);
872 static ssize_t size_show(struct device *dev,
873 struct device_attribute *attr, char *buf)
875 struct dev_dax *dev_dax = to_dev_dax(dev);
876 unsigned long long size;
879 size = dev_dax_size(dev_dax);
882 return sprintf(buf, "%llu\n", size);
885 static bool alloc_is_aligned(struct dev_dax *dev_dax, resource_size_t size)
888 * The minimum mapping granularity for a device instance is a
889 * single subsection, unless the arch says otherwise.
891 return IS_ALIGNED(size, max_t(unsigned long, dev_dax->align, memremap_compat_align()));
894 static int dev_dax_shrink(struct dev_dax *dev_dax, resource_size_t size)
896 resource_size_t to_shrink = dev_dax_size(dev_dax) - size;
897 struct dax_region *dax_region = dev_dax->region;
898 struct device *dev = &dev_dax->dev;
901 for (i = dev_dax->nr_range - 1; i >= 0; i--) {
902 struct range *range = &dev_dax->ranges[i].range;
903 struct dax_mapping *mapping = dev_dax->ranges[i].mapping;
904 struct resource *adjust = NULL, *res;
905 resource_size_t shrink;
907 shrink = min_t(u64, to_shrink, range_len(range));
908 if (shrink >= range_len(range)) {
909 devm_release_action(dax_region->dev,
910 unregister_dax_mapping, &mapping->dev);
911 trim_dev_dax_range(dev_dax);
918 for_each_dax_region_resource(dax_region, res)
919 if (strcmp(res->name, dev_name(dev)) == 0
920 && res->start == range->start) {
925 if (dev_WARN_ONCE(dev, !adjust || i != dev_dax->nr_range - 1,
926 "failed to find matching resource\n"))
928 return adjust_dev_dax_range(dev_dax, adjust, range_len(range)
935 * Only allow adjustments that preserve the relative pgoff of existing
936 * allocations. I.e. the dev_dax->ranges array is ordered by increasing pgoff.
938 static bool adjust_ok(struct dev_dax *dev_dax, struct resource *res)
940 struct dev_dax_range *last;
943 if (dev_dax->nr_range == 0)
945 if (strcmp(res->name, dev_name(&dev_dax->dev)) != 0)
947 last = &dev_dax->ranges[dev_dax->nr_range - 1];
948 if (last->range.start != res->start || last->range.end != res->end)
950 for (i = 0; i < dev_dax->nr_range - 1; i++) {
951 struct dev_dax_range *dax_range = &dev_dax->ranges[i];
953 if (dax_range->pgoff > last->pgoff)
960 static ssize_t dev_dax_resize(struct dax_region *dax_region,
961 struct dev_dax *dev_dax, resource_size_t size)
963 resource_size_t avail = dax_region_avail_size(dax_region), to_alloc;
964 resource_size_t dev_size = dev_dax_size(dev_dax);
965 struct resource *region_res = &dax_region->res;
966 struct device *dev = &dev_dax->dev;
967 struct resource *res, *first;
968 resource_size_t alloc = 0;
973 if (size == dev_size)
975 if (size > dev_size && size - dev_size > avail)
978 return dev_dax_shrink(dev_dax, size);
980 to_alloc = size - dev_size;
981 if (dev_WARN_ONCE(dev, !alloc_is_aligned(dev_dax, to_alloc),
982 "resize of %pa misaligned\n", &to_alloc))
986 * Expand the device into the unused portion of the region. This
987 * may involve adjusting the end of an existing resource, or
988 * allocating a new resource.
991 first = region_res->child;
993 return alloc_dev_dax_range(dev_dax, dax_region->res.start, to_alloc);
996 for (res = first; res; res = res->sibling) {
997 struct resource *next = res->sibling;
999 /* space at the beginning of the region */
1000 if (res == first && res->start > dax_region->res.start) {
1001 alloc = min(res->start - dax_region->res.start, to_alloc);
1002 rc = alloc_dev_dax_range(dev_dax, dax_region->res.start, alloc);
1007 /* space between allocations */
1008 if (next && next->start > res->end + 1)
1009 alloc = min(next->start - (res->end + 1), to_alloc);
1011 /* space at the end of the region */
1012 if (!alloc && !next && res->end < region_res->end)
1013 alloc = min(region_res->end - res->end, to_alloc);
1018 if (adjust_ok(dev_dax, res)) {
1019 rc = adjust_dev_dax_range(dev_dax, res, resource_size(res) + alloc);
1022 rc = alloc_dev_dax_range(dev_dax, res->end + 1, alloc);
1033 static ssize_t size_store(struct device *dev, struct device_attribute *attr,
1034 const char *buf, size_t len)
1037 unsigned long long val;
1038 struct dev_dax *dev_dax = to_dev_dax(dev);
1039 struct dax_region *dax_region = dev_dax->region;
1041 rc = kstrtoull(buf, 0, &val);
1045 if (!alloc_is_aligned(dev_dax, val)) {
1046 dev_dbg(dev, "%s: size: %lld misaligned\n", __func__, val);
1050 device_lock(dax_region->dev);
1051 if (!dax_region->dev->driver) {
1052 device_unlock(dax_region->dev);
1056 rc = dev_dax_resize(dax_region, dev_dax, val);
1058 device_unlock(dax_region->dev);
1060 return rc == 0 ? len : rc;
1062 static DEVICE_ATTR_RW(size);
1064 static ssize_t range_parse(const char *opt, size_t len, struct range *range)
1066 unsigned long long addr = 0;
1067 char *start, *end, *str;
1068 ssize_t rc = -EINVAL;
1070 str = kstrdup(opt, GFP_KERNEL);
1075 start = strsep(&end, "-");
1079 rc = kstrtoull(start, 16, &addr);
1082 range->start = addr;
1084 rc = kstrtoull(end, 16, &addr);
1094 static ssize_t mapping_store(struct device *dev, struct device_attribute *attr,
1095 const char *buf, size_t len)
1097 struct dev_dax *dev_dax = to_dev_dax(dev);
1098 struct dax_region *dax_region = dev_dax->region;
1103 rc = range_parse(buf, len, &r);
1108 device_lock(dax_region->dev);
1109 if (!dax_region->dev->driver) {
1110 device_unlock(dax_region->dev);
1115 to_alloc = range_len(&r);
1116 if (alloc_is_aligned(dev_dax, to_alloc))
1117 rc = alloc_dev_dax_range(dev_dax, r.start, to_alloc);
1119 device_unlock(dax_region->dev);
1121 return rc == 0 ? len : rc;
1123 static DEVICE_ATTR_WO(mapping);
1125 static ssize_t align_show(struct device *dev,
1126 struct device_attribute *attr, char *buf)
1128 struct dev_dax *dev_dax = to_dev_dax(dev);
1130 return sprintf(buf, "%d\n", dev_dax->align);
1133 static ssize_t dev_dax_validate_align(struct dev_dax *dev_dax)
1135 resource_size_t dev_size = dev_dax_size(dev_dax);
1136 struct device *dev = &dev_dax->dev;
1139 if (dev_size > 0 && !alloc_is_aligned(dev_dax, dev_size)) {
1140 dev_dbg(dev, "%s: align %u invalid for size %pa\n",
1141 __func__, dev_dax->align, &dev_size);
1145 for (i = 0; i < dev_dax->nr_range; i++) {
1146 size_t len = range_len(&dev_dax->ranges[i].range);
1148 if (!alloc_is_aligned(dev_dax, len)) {
1149 dev_dbg(dev, "%s: align %u invalid for range %d\n",
1150 __func__, dev_dax->align, i);
1158 static ssize_t align_store(struct device *dev, struct device_attribute *attr,
1159 const char *buf, size_t len)
1161 struct dev_dax *dev_dax = to_dev_dax(dev);
1162 struct dax_region *dax_region = dev_dax->region;
1163 unsigned long val, align_save;
1166 rc = kstrtoul(buf, 0, &val);
1170 if (!dax_align_valid(val))
1173 device_lock(dax_region->dev);
1174 if (!dax_region->dev->driver) {
1175 device_unlock(dax_region->dev);
1185 align_save = dev_dax->align;
1186 dev_dax->align = val;
1187 rc = dev_dax_validate_align(dev_dax);
1189 dev_dax->align = align_save;
1192 device_unlock(dax_region->dev);
1193 return rc == 0 ? len : rc;
1195 static DEVICE_ATTR_RW(align);
1197 static int dev_dax_target_node(struct dev_dax *dev_dax)
1199 struct dax_region *dax_region = dev_dax->region;
1201 return dax_region->target_node;
1204 static ssize_t target_node_show(struct device *dev,
1205 struct device_attribute *attr, char *buf)
1207 struct dev_dax *dev_dax = to_dev_dax(dev);
1209 return sprintf(buf, "%d\n", dev_dax_target_node(dev_dax));
1211 static DEVICE_ATTR_RO(target_node);
1213 static ssize_t resource_show(struct device *dev,
1214 struct device_attribute *attr, char *buf)
1216 struct dev_dax *dev_dax = to_dev_dax(dev);
1217 struct dax_region *dax_region = dev_dax->region;
1218 unsigned long long start;
1220 if (dev_dax->nr_range < 1)
1221 start = dax_region->res.start;
1223 start = dev_dax->ranges[0].range.start;
1225 return sprintf(buf, "%#llx\n", start);
1227 static DEVICE_ATTR(resource, 0400, resource_show, NULL);
1229 static ssize_t modalias_show(struct device *dev, struct device_attribute *attr,
1233 * We only ever expect to handle device-dax instances, i.e. the
1234 * @type argument to MODULE_ALIAS_DAX_DEVICE() is always zero
1236 return sprintf(buf, DAX_DEVICE_MODALIAS_FMT "\n", 0);
1238 static DEVICE_ATTR_RO(modalias);
1240 static ssize_t numa_node_show(struct device *dev,
1241 struct device_attribute *attr, char *buf)
1243 return sprintf(buf, "%d\n", dev_to_node(dev));
1245 static DEVICE_ATTR_RO(numa_node);
1247 static umode_t dev_dax_visible(struct kobject *kobj, struct attribute *a, int n)
1249 struct device *dev = container_of(kobj, struct device, kobj);
1250 struct dev_dax *dev_dax = to_dev_dax(dev);
1251 struct dax_region *dax_region = dev_dax->region;
1253 if (a == &dev_attr_target_node.attr && dev_dax_target_node(dev_dax) < 0)
1255 if (a == &dev_attr_numa_node.attr && !IS_ENABLED(CONFIG_NUMA))
1257 if (a == &dev_attr_mapping.attr && is_static(dax_region))
1259 if ((a == &dev_attr_align.attr ||
1260 a == &dev_attr_size.attr) && is_static(dax_region))
1265 static struct attribute *dev_dax_attributes[] = {
1266 &dev_attr_modalias.attr,
1267 &dev_attr_size.attr,
1268 &dev_attr_mapping.attr,
1269 &dev_attr_target_node.attr,
1270 &dev_attr_align.attr,
1271 &dev_attr_resource.attr,
1272 &dev_attr_numa_node.attr,
1276 static const struct attribute_group dev_dax_attribute_group = {
1277 .attrs = dev_dax_attributes,
1278 .is_visible = dev_dax_visible,
1281 static const struct attribute_group *dax_attribute_groups[] = {
1282 &dev_dax_attribute_group,
1286 static void dev_dax_release(struct device *dev)
1288 struct dev_dax *dev_dax = to_dev_dax(dev);
1289 struct dax_device *dax_dev = dev_dax->dax_dev;
1292 free_dev_dax_id(dev_dax);
1293 kfree(dev_dax->pgmap);
1297 static const struct device_type dev_dax_type = {
1298 .release = dev_dax_release,
1299 .groups = dax_attribute_groups,
1302 struct dev_dax *devm_create_dev_dax(struct dev_dax_data *data)
1304 struct dax_region *dax_region = data->dax_region;
1305 struct device *parent = dax_region->dev;
1306 struct dax_device *dax_dev;
1307 struct dev_dax *dev_dax;
1308 struct inode *inode;
1312 dev_dax = kzalloc(sizeof(*dev_dax), GFP_KERNEL);
1314 return ERR_PTR(-ENOMEM);
1316 dev_dax->region = dax_region;
1317 if (is_static(dax_region)) {
1318 if (dev_WARN_ONCE(parent, data->id < 0,
1319 "dynamic id specified to static region\n")) {
1324 dev_dax->id = data->id;
1326 if (dev_WARN_ONCE(parent, data->id >= 0,
1327 "static id specified to dynamic region\n")) {
1332 rc = alloc_dev_dax_id(dev_dax);
1337 dev = &dev_dax->dev;
1338 device_initialize(dev);
1339 dev_set_name(dev, "dax%d.%d", dax_region->id, dev_dax->id);
1341 rc = alloc_dev_dax_range(dev_dax, dax_region->res.start, data->size);
1346 dev_WARN_ONCE(parent, !is_static(dax_region),
1347 "custom dev_pagemap requires a static dax_region\n");
1349 dev_dax->pgmap = kmemdup(data->pgmap,
1350 sizeof(struct dev_pagemap), GFP_KERNEL);
1351 if (!dev_dax->pgmap) {
1358 * No 'host' or dax_operations since there is no access to this
1359 * device outside of mmap of the resulting character device.
1361 dax_dev = alloc_dax(dev_dax, NULL, NULL, DAXDEV_F_SYNC);
1362 if (IS_ERR(dax_dev)) {
1363 rc = PTR_ERR(dax_dev);
1367 /* a device_dax instance is dead while the driver is not attached */
1370 dev_dax->dax_dev = dax_dev;
1371 dev_dax->target_node = dax_region->target_node;
1372 dev_dax->align = dax_region->align;
1373 ida_init(&dev_dax->ida);
1375 inode = dax_inode(dax_dev);
1376 dev->devt = inode->i_rdev;
1377 if (data->subsys == DEV_DAX_BUS)
1378 dev->bus = &dax_bus_type;
1380 dev->class = dax_class;
1381 dev->parent = parent;
1382 dev->type = &dev_dax_type;
1384 rc = device_add(dev);
1386 kill_dev_dax(dev_dax);
1391 rc = devm_add_action_or_reset(dax_region->dev, unregister_dev_dax, dev);
1395 /* register mapping device for the initial allocation range */
1396 if (dev_dax->nr_range && range_len(&dev_dax->ranges[0].range)) {
1397 rc = devm_register_dax_mapping(dev_dax, 0);
1405 kfree(dev_dax->pgmap);
1407 free_dev_dax_ranges(dev_dax);
1409 free_dev_dax_id(dev_dax);
1415 EXPORT_SYMBOL_GPL(devm_create_dev_dax);
1417 static int match_always_count;
1419 int __dax_driver_register(struct dax_device_driver *dax_drv,
1420 struct module *module, const char *mod_name)
1422 struct device_driver *drv = &dax_drv->drv;
1425 INIT_LIST_HEAD(&dax_drv->ids);
1426 drv->owner = module;
1427 drv->name = mod_name;
1428 drv->mod_name = mod_name;
1429 drv->bus = &dax_bus_type;
1431 /* there can only be one default driver */
1432 mutex_lock(&dax_bus_lock);
1433 match_always_count += dax_drv->match_always;
1434 if (match_always_count > 1) {
1435 match_always_count--;
1439 mutex_unlock(&dax_bus_lock);
1442 return driver_register(drv);
1444 EXPORT_SYMBOL_GPL(__dax_driver_register);
1446 void dax_driver_unregister(struct dax_device_driver *dax_drv)
1448 struct device_driver *drv = &dax_drv->drv;
1449 struct dax_id *dax_id, *_id;
1451 mutex_lock(&dax_bus_lock);
1452 match_always_count -= dax_drv->match_always;
1453 list_for_each_entry_safe(dax_id, _id, &dax_drv->ids, list) {
1454 list_del(&dax_id->list);
1457 mutex_unlock(&dax_bus_lock);
1458 driver_unregister(drv);
1460 EXPORT_SYMBOL_GPL(dax_driver_unregister);
1462 int __init dax_bus_init(void)
1466 if (IS_ENABLED(CONFIG_DEV_DAX_PMEM_COMPAT)) {
1467 dax_class = class_create(THIS_MODULE, "dax");
1468 if (IS_ERR(dax_class))
1469 return PTR_ERR(dax_class);
1472 rc = bus_register(&dax_bus_type);
1474 class_destroy(dax_class);
1478 void __exit dax_bus_exit(void)
1480 bus_unregister(&dax_bus_type);
1481 class_destroy(dax_class);