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>
9 #include "dax-private.h"
12 static struct class *dax_class;
14 static DEFINE_MUTEX(dax_bus_lock);
16 #define DAX_NAME_LEN 30
18 struct list_head list;
19 char dev_name[DAX_NAME_LEN];
22 static int dax_bus_uevent(struct device *dev, struct kobj_uevent_env *env)
25 * We only ever expect to handle device-dax instances, i.e. the
26 * @type argument to MODULE_ALIAS_DAX_DEVICE() is always zero
28 return add_uevent_var(env, "MODALIAS=" DAX_DEVICE_MODALIAS_FMT, 0);
31 static struct dax_device_driver *to_dax_drv(struct device_driver *drv)
33 return container_of(drv, struct dax_device_driver, drv);
36 static struct dax_id *__dax_match_id(struct dax_device_driver *dax_drv,
39 struct dax_id *dax_id;
41 lockdep_assert_held(&dax_bus_lock);
43 list_for_each_entry(dax_id, &dax_drv->ids, list)
44 if (sysfs_streq(dax_id->dev_name, dev_name))
49 static int dax_match_id(struct dax_device_driver *dax_drv, struct device *dev)
53 mutex_lock(&dax_bus_lock);
54 match = !!__dax_match_id(dax_drv, dev_name(dev));
55 mutex_unlock(&dax_bus_lock);
65 static ssize_t do_id_store(struct device_driver *drv, const char *buf,
66 size_t count, enum id_action action)
68 struct dax_device_driver *dax_drv = to_dax_drv(drv);
69 unsigned int region_id, id;
70 char devname[DAX_NAME_LEN];
71 struct dax_id *dax_id;
75 fields = sscanf(buf, "dax%d.%d", ®ion_id, &id);
78 sprintf(devname, "dax%d.%d", region_id, id);
79 if (!sysfs_streq(buf, devname))
82 mutex_lock(&dax_bus_lock);
83 dax_id = __dax_match_id(dax_drv, buf);
85 if (action == ID_ADD) {
86 dax_id = kzalloc(sizeof(*dax_id), GFP_KERNEL);
88 strncpy(dax_id->dev_name, buf, DAX_NAME_LEN);
89 list_add(&dax_id->list, &dax_drv->ids);
93 /* nothing to remove */;
94 } else if (action == ID_REMOVE) {
95 list_del(&dax_id->list);
98 /* dax_id already added */;
99 mutex_unlock(&dax_bus_lock);
103 if (action == ID_ADD)
104 rc = driver_attach(drv);
110 static ssize_t new_id_store(struct device_driver *drv, const char *buf,
113 return do_id_store(drv, buf, count, ID_ADD);
115 static DRIVER_ATTR_WO(new_id);
117 static ssize_t remove_id_store(struct device_driver *drv, const char *buf,
120 return do_id_store(drv, buf, count, ID_REMOVE);
122 static DRIVER_ATTR_WO(remove_id);
124 static struct attribute *dax_drv_attrs[] = {
125 &driver_attr_new_id.attr,
126 &driver_attr_remove_id.attr,
129 ATTRIBUTE_GROUPS(dax_drv);
131 static int dax_bus_match(struct device *dev, struct device_driver *drv);
133 static bool is_static(struct dax_region *dax_region)
135 return (dax_region->res.flags & IORESOURCE_DAX_STATIC) != 0;
138 static int dax_bus_probe(struct device *dev)
140 struct dax_device_driver *dax_drv = to_dax_drv(dev->driver);
141 struct dev_dax *dev_dax = to_dev_dax(dev);
142 struct dax_region *dax_region = dev_dax->region;
143 struct range *range = &dev_dax->range;
146 if (range_len(range) == 0 || dev_dax->id < 0)
149 rc = dax_drv->probe(dev_dax);
151 if (rc || is_static(dax_region))
155 * Track new seed creation only after successful probe of the
158 if (dax_region->seed == dev)
159 dax_region->seed = NULL;
164 static int dax_bus_remove(struct device *dev)
166 struct dax_device_driver *dax_drv = to_dax_drv(dev->driver);
167 struct dev_dax *dev_dax = to_dev_dax(dev);
169 return dax_drv->remove(dev_dax);
172 static struct bus_type dax_bus_type = {
174 .uevent = dax_bus_uevent,
175 .match = dax_bus_match,
176 .probe = dax_bus_probe,
177 .remove = dax_bus_remove,
178 .drv_groups = dax_drv_groups,
181 static int dax_bus_match(struct device *dev, struct device_driver *drv)
183 struct dax_device_driver *dax_drv = to_dax_drv(drv);
186 * All but the 'device-dax' driver, which has 'match_always'
187 * set, requires an exact id match.
189 if (dax_drv->match_always)
192 return dax_match_id(dax_drv, dev);
196 * Rely on the fact that drvdata is set before the attributes are
197 * registered, and that the attributes are unregistered before drvdata
198 * is cleared to assume that drvdata is always valid.
200 static ssize_t id_show(struct device *dev,
201 struct device_attribute *attr, char *buf)
203 struct dax_region *dax_region = dev_get_drvdata(dev);
205 return sprintf(buf, "%d\n", dax_region->id);
207 static DEVICE_ATTR_RO(id);
209 static ssize_t region_size_show(struct device *dev,
210 struct device_attribute *attr, char *buf)
212 struct dax_region *dax_region = dev_get_drvdata(dev);
214 return sprintf(buf, "%llu\n", (unsigned long long)
215 resource_size(&dax_region->res));
217 static struct device_attribute dev_attr_region_size = __ATTR(size, 0444,
218 region_size_show, NULL);
220 static ssize_t align_show(struct device *dev,
221 struct device_attribute *attr, char *buf)
223 struct dax_region *dax_region = dev_get_drvdata(dev);
225 return sprintf(buf, "%u\n", dax_region->align);
227 static DEVICE_ATTR_RO(align);
229 #define for_each_dax_region_resource(dax_region, res) \
230 for (res = (dax_region)->res.child; res; res = res->sibling)
232 static unsigned long long dax_region_avail_size(struct dax_region *dax_region)
234 resource_size_t size = resource_size(&dax_region->res);
235 struct resource *res;
237 device_lock_assert(dax_region->dev);
239 for_each_dax_region_resource(dax_region, res)
240 size -= resource_size(res);
244 static ssize_t available_size_show(struct device *dev,
245 struct device_attribute *attr, char *buf)
247 struct dax_region *dax_region = dev_get_drvdata(dev);
248 unsigned long long size;
251 size = dax_region_avail_size(dax_region);
254 return sprintf(buf, "%llu\n", size);
256 static DEVICE_ATTR_RO(available_size);
258 static ssize_t seed_show(struct device *dev,
259 struct device_attribute *attr, char *buf)
261 struct dax_region *dax_region = dev_get_drvdata(dev);
265 if (is_static(dax_region))
269 seed = dax_region->seed;
270 rc = sprintf(buf, "%s\n", seed ? dev_name(seed) : "");
275 static DEVICE_ATTR_RO(seed);
277 static ssize_t create_show(struct device *dev,
278 struct device_attribute *attr, char *buf)
280 struct dax_region *dax_region = dev_get_drvdata(dev);
281 struct device *youngest;
284 if (is_static(dax_region))
288 youngest = dax_region->youngest;
289 rc = sprintf(buf, "%s\n", youngest ? dev_name(youngest) : "");
295 static ssize_t create_store(struct device *dev, struct device_attribute *attr,
296 const char *buf, size_t len)
298 struct dax_region *dax_region = dev_get_drvdata(dev);
299 unsigned long long avail;
303 if (is_static(dax_region))
306 rc = kstrtoint(buf, 0, &val);
313 avail = dax_region_avail_size(dax_region);
317 struct dev_dax_data data = {
318 .dax_region = dax_region,
322 struct dev_dax *dev_dax = devm_create_dev_dax(&data);
325 rc = PTR_ERR(dev_dax);
328 * In support of crafting multiple new devices
329 * simultaneously multiple seeds can be created,
330 * but only the first one that has not been
331 * successfully bound is tracked as the region
334 if (!dax_region->seed)
335 dax_region->seed = &dev_dax->dev;
336 dax_region->youngest = &dev_dax->dev;
344 static DEVICE_ATTR_RW(create);
346 void kill_dev_dax(struct dev_dax *dev_dax)
348 struct dax_device *dax_dev = dev_dax->dax_dev;
349 struct inode *inode = dax_inode(dax_dev);
352 unmap_mapping_range(inode->i_mapping, 0, 0, 1);
354 EXPORT_SYMBOL_GPL(kill_dev_dax);
356 static void free_dev_dax_range(struct dev_dax *dev_dax)
358 struct dax_region *dax_region = dev_dax->region;
359 struct range *range = &dev_dax->range;
361 device_lock_assert(dax_region->dev);
362 if (range_len(range))
363 __release_region(&dax_region->res, range->start,
367 static void unregister_dev_dax(void *dev)
369 struct dev_dax *dev_dax = to_dev_dax(dev);
371 dev_dbg(dev, "%s\n", __func__);
373 kill_dev_dax(dev_dax);
374 free_dev_dax_range(dev_dax);
379 /* a return value >= 0 indicates this invocation invalidated the id */
380 static int __free_dev_dax_id(struct dev_dax *dev_dax)
382 struct dax_region *dax_region = dev_dax->region;
383 struct device *dev = &dev_dax->dev;
384 int rc = dev_dax->id;
386 device_lock_assert(dev);
388 if (is_static(dax_region) || dev_dax->id < 0)
390 ida_free(&dax_region->ida, dev_dax->id);
395 static int free_dev_dax_id(struct dev_dax *dev_dax)
397 struct device *dev = &dev_dax->dev;
401 rc = __free_dev_dax_id(dev_dax);
406 static ssize_t delete_store(struct device *dev, struct device_attribute *attr,
407 const char *buf, size_t len)
409 struct dax_region *dax_region = dev_get_drvdata(dev);
410 struct dev_dax *dev_dax;
411 struct device *victim;
415 if (is_static(dax_region))
418 victim = device_find_child_by_name(dax_region->dev, buf);
424 dev_dax = to_dev_dax(victim);
425 if (victim->driver || range_len(&dev_dax->range))
429 * Invalidate the device so it does not become active
430 * again, but always preserve device-id-0 so that
431 * /sys/bus/dax/ is guaranteed to be populated while any
432 * dax_region is registered.
434 if (dev_dax->id > 0) {
435 do_del = __free_dev_dax_id(dev_dax) >= 0;
437 if (dax_region->seed == victim)
438 dax_region->seed = NULL;
439 if (dax_region->youngest == victim)
440 dax_region->youngest = NULL;
444 device_unlock(victim);
446 /* won the race to invalidate the device, clean it up */
448 devm_release_action(dev, unregister_dev_dax, victim);
454 static DEVICE_ATTR_WO(delete);
456 static umode_t dax_region_visible(struct kobject *kobj, struct attribute *a,
459 struct device *dev = container_of(kobj, struct device, kobj);
460 struct dax_region *dax_region = dev_get_drvdata(dev);
462 if (is_static(dax_region))
463 if (a == &dev_attr_available_size.attr
464 || a == &dev_attr_create.attr
465 || a == &dev_attr_seed.attr
466 || a == &dev_attr_delete.attr)
471 static struct attribute *dax_region_attributes[] = {
472 &dev_attr_available_size.attr,
473 &dev_attr_region_size.attr,
474 &dev_attr_align.attr,
475 &dev_attr_create.attr,
477 &dev_attr_delete.attr,
482 static const struct attribute_group dax_region_attribute_group = {
483 .name = "dax_region",
484 .attrs = dax_region_attributes,
485 .is_visible = dax_region_visible,
488 static const struct attribute_group *dax_region_attribute_groups[] = {
489 &dax_region_attribute_group,
493 static void dax_region_free(struct kref *kref)
495 struct dax_region *dax_region;
497 dax_region = container_of(kref, struct dax_region, kref);
501 void dax_region_put(struct dax_region *dax_region)
503 kref_put(&dax_region->kref, dax_region_free);
505 EXPORT_SYMBOL_GPL(dax_region_put);
507 static void dax_region_unregister(void *region)
509 struct dax_region *dax_region = region;
511 sysfs_remove_groups(&dax_region->dev->kobj,
512 dax_region_attribute_groups);
513 dax_region_put(dax_region);
516 struct dax_region *alloc_dax_region(struct device *parent, int region_id,
517 struct resource *res, int target_node, unsigned int align,
520 struct dax_region *dax_region;
523 * The DAX core assumes that it can store its private data in
524 * parent->driver_data. This WARN is a reminder / safeguard for
525 * developers of device-dax drivers.
527 if (dev_get_drvdata(parent)) {
528 dev_WARN(parent, "dax core failed to setup private data\n");
532 if (!IS_ALIGNED(res->start, align)
533 || !IS_ALIGNED(resource_size(res), align))
536 dax_region = kzalloc(sizeof(*dax_region), GFP_KERNEL);
540 dev_set_drvdata(parent, dax_region);
541 kref_init(&dax_region->kref);
542 dax_region->id = region_id;
543 dax_region->align = align;
544 dax_region->dev = parent;
545 dax_region->target_node = target_node;
546 ida_init(&dax_region->ida);
547 dax_region->res = (struct resource) {
550 .flags = IORESOURCE_MEM | flags,
553 if (sysfs_create_groups(&parent->kobj, dax_region_attribute_groups)) {
558 kref_get(&dax_region->kref);
559 if (devm_add_action_or_reset(parent, dax_region_unregister, dax_region))
563 EXPORT_SYMBOL_GPL(alloc_dax_region);
565 static int alloc_dev_dax_range(struct dev_dax *dev_dax, resource_size_t size)
567 struct dax_region *dax_region = dev_dax->region;
568 struct resource *res = &dax_region->res;
569 struct device *dev = &dev_dax->dev;
570 struct resource *alloc;
572 device_lock_assert(dax_region->dev);
574 /* handle the seed alloc special case */
576 dev_dax->range = (struct range) {
578 .end = res->start - 1,
583 /* TODO: handle multiple allocations per region */
587 alloc = __request_region(res, res->start, size, dev_name(dev), 0);
592 dev_dax->range = (struct range) {
593 .start = alloc->start,
600 static ssize_t size_show(struct device *dev,
601 struct device_attribute *attr, char *buf)
603 struct dev_dax *dev_dax = to_dev_dax(dev);
604 unsigned long long size = range_len(&dev_dax->range);
606 return sprintf(buf, "%llu\n", size);
608 static DEVICE_ATTR_RO(size);
610 static int dev_dax_target_node(struct dev_dax *dev_dax)
612 struct dax_region *dax_region = dev_dax->region;
614 return dax_region->target_node;
617 static ssize_t target_node_show(struct device *dev,
618 struct device_attribute *attr, char *buf)
620 struct dev_dax *dev_dax = to_dev_dax(dev);
622 return sprintf(buf, "%d\n", dev_dax_target_node(dev_dax));
624 static DEVICE_ATTR_RO(target_node);
626 static ssize_t resource_show(struct device *dev,
627 struct device_attribute *attr, char *buf)
629 struct dev_dax *dev_dax = to_dev_dax(dev);
631 return sprintf(buf, "%#llx\n", dev_dax->range.start);
633 static DEVICE_ATTR(resource, 0400, resource_show, NULL);
635 static ssize_t modalias_show(struct device *dev, struct device_attribute *attr,
639 * We only ever expect to handle device-dax instances, i.e. the
640 * @type argument to MODULE_ALIAS_DAX_DEVICE() is always zero
642 return sprintf(buf, DAX_DEVICE_MODALIAS_FMT "\n", 0);
644 static DEVICE_ATTR_RO(modalias);
646 static ssize_t numa_node_show(struct device *dev,
647 struct device_attribute *attr, char *buf)
649 return sprintf(buf, "%d\n", dev_to_node(dev));
651 static DEVICE_ATTR_RO(numa_node);
653 static umode_t dev_dax_visible(struct kobject *kobj, struct attribute *a, int n)
655 struct device *dev = container_of(kobj, struct device, kobj);
656 struct dev_dax *dev_dax = to_dev_dax(dev);
658 if (a == &dev_attr_target_node.attr && dev_dax_target_node(dev_dax) < 0)
660 if (a == &dev_attr_numa_node.attr && !IS_ENABLED(CONFIG_NUMA))
665 static struct attribute *dev_dax_attributes[] = {
666 &dev_attr_modalias.attr,
668 &dev_attr_target_node.attr,
669 &dev_attr_resource.attr,
670 &dev_attr_numa_node.attr,
674 static const struct attribute_group dev_dax_attribute_group = {
675 .attrs = dev_dax_attributes,
676 .is_visible = dev_dax_visible,
679 static const struct attribute_group *dax_attribute_groups[] = {
680 &dev_dax_attribute_group,
684 static void dev_dax_release(struct device *dev)
686 struct dev_dax *dev_dax = to_dev_dax(dev);
687 struct dax_region *dax_region = dev_dax->region;
688 struct dax_device *dax_dev = dev_dax->dax_dev;
691 free_dev_dax_id(dev_dax);
692 dax_region_put(dax_region);
693 kfree(dev_dax->pgmap);
697 static const struct device_type dev_dax_type = {
698 .release = dev_dax_release,
699 .groups = dax_attribute_groups,
702 struct dev_dax *devm_create_dev_dax(struct dev_dax_data *data)
704 struct dax_region *dax_region = data->dax_region;
705 struct device *parent = dax_region->dev;
706 struct dax_device *dax_dev;
707 struct dev_dax *dev_dax;
712 dev_dax = kzalloc(sizeof(*dev_dax), GFP_KERNEL);
714 return ERR_PTR(-ENOMEM);
716 if (is_static(dax_region)) {
717 if (dev_WARN_ONCE(parent, data->id < 0,
718 "dynamic id specified to static region\n")) {
723 dev_dax->id = data->id;
725 if (dev_WARN_ONCE(parent, data->id >= 0,
726 "static id specified to dynamic region\n")) {
731 rc = ida_alloc(&dax_region->ida, GFP_KERNEL);
737 dev_dax->region = dax_region;
739 device_initialize(dev);
740 dev_set_name(dev, "dax%d.%d", dax_region->id, dev_dax->id);
742 rc = alloc_dev_dax_range(dev_dax, data->size);
747 dev_WARN_ONCE(parent, !is_static(dax_region),
748 "custom dev_pagemap requires a static dax_region\n");
750 dev_dax->pgmap = kmemdup(data->pgmap,
751 sizeof(struct dev_pagemap), GFP_KERNEL);
752 if (!dev_dax->pgmap) {
759 * No 'host' or dax_operations since there is no access to this
760 * device outside of mmap of the resulting character device.
762 dax_dev = alloc_dax(dev_dax, NULL, NULL, DAXDEV_F_SYNC);
763 if (IS_ERR(dax_dev)) {
764 rc = PTR_ERR(dax_dev);
768 /* a device_dax instance is dead while the driver is not attached */
771 /* from here on we're committed to teardown via dev_dax_release() */
772 dev_dax->dax_dev = dax_dev;
773 dev_dax->target_node = dax_region->target_node;
774 kref_get(&dax_region->kref);
776 inode = dax_inode(dax_dev);
777 dev->devt = inode->i_rdev;
778 if (data->subsys == DEV_DAX_BUS)
779 dev->bus = &dax_bus_type;
781 dev->class = dax_class;
782 dev->parent = parent;
783 dev->type = &dev_dax_type;
785 rc = device_add(dev);
787 kill_dev_dax(dev_dax);
792 rc = devm_add_action_or_reset(dax_region->dev, unregister_dev_dax, dev);
799 kfree(dev_dax->pgmap);
801 free_dev_dax_range(dev_dax);
803 free_dev_dax_id(dev_dax);
809 EXPORT_SYMBOL_GPL(devm_create_dev_dax);
811 static int match_always_count;
813 int __dax_driver_register(struct dax_device_driver *dax_drv,
814 struct module *module, const char *mod_name)
816 struct device_driver *drv = &dax_drv->drv;
819 INIT_LIST_HEAD(&dax_drv->ids);
821 drv->name = mod_name;
822 drv->mod_name = mod_name;
823 drv->bus = &dax_bus_type;
825 /* there can only be one default driver */
826 mutex_lock(&dax_bus_lock);
827 match_always_count += dax_drv->match_always;
828 if (match_always_count > 1) {
829 match_always_count--;
833 mutex_unlock(&dax_bus_lock);
836 return driver_register(drv);
838 EXPORT_SYMBOL_GPL(__dax_driver_register);
840 void dax_driver_unregister(struct dax_device_driver *dax_drv)
842 struct device_driver *drv = &dax_drv->drv;
843 struct dax_id *dax_id, *_id;
845 mutex_lock(&dax_bus_lock);
846 match_always_count -= dax_drv->match_always;
847 list_for_each_entry_safe(dax_id, _id, &dax_drv->ids, list) {
848 list_del(&dax_id->list);
851 mutex_unlock(&dax_bus_lock);
852 driver_unregister(drv);
854 EXPORT_SYMBOL_GPL(dax_driver_unregister);
856 int __init dax_bus_init(void)
860 if (IS_ENABLED(CONFIG_DEV_DAX_PMEM_COMPAT)) {
861 dax_class = class_create(THIS_MODULE, "dax");
862 if (IS_ERR(dax_class))
863 return PTR_ERR(dax_class);
866 rc = bus_register(&dax_bus_type);
868 class_destroy(dax_class);
872 void __exit dax_bus_exit(void)
874 bus_unregister(&dax_bus_type);
875 class_destroy(dax_class);