xref: /linux/drivers/dax/bus.c (revision 23c996fc2bc1978a02c64eddb90b4ab5d309c8df)
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>
8 #include <linux/dax.h>
9 #include <linux/io.h>
10 #include "dax-private.h"
11 #include "bus.h"
12 
13 static DEFINE_MUTEX(dax_bus_lock);
14 
15 /*
16  * All changes to the dax region configuration occur with this lock held
17  * for write.
18  */
19 DECLARE_RWSEM(dax_region_rwsem);
20 
21 /*
22  * All changes to the dax device configuration occur with this lock held
23  * for write.
24  */
25 DECLARE_RWSEM(dax_dev_rwsem);
26 
27 #define DAX_NAME_LEN 30
28 struct dax_id {
29 	struct list_head list;
30 	char dev_name[DAX_NAME_LEN];
31 };
32 
33 static int dax_bus_uevent(const struct device *dev, struct kobj_uevent_env *env)
34 {
35 	/*
36 	 * We only ever expect to handle device-dax instances, i.e. the
37 	 * @type argument to MODULE_ALIAS_DAX_DEVICE() is always zero
38 	 */
39 	return add_uevent_var(env, "MODALIAS=" DAX_DEVICE_MODALIAS_FMT, 0);
40 }
41 
42 static struct dax_device_driver *to_dax_drv(struct device_driver *drv)
43 {
44 	return container_of(drv, struct dax_device_driver, drv);
45 }
46 
47 static struct dax_id *__dax_match_id(struct dax_device_driver *dax_drv,
48 		const char *dev_name)
49 {
50 	struct dax_id *dax_id;
51 
52 	lockdep_assert_held(&dax_bus_lock);
53 
54 	list_for_each_entry(dax_id, &dax_drv->ids, list)
55 		if (sysfs_streq(dax_id->dev_name, dev_name))
56 			return dax_id;
57 	return NULL;
58 }
59 
60 static int dax_match_id(struct dax_device_driver *dax_drv, struct device *dev)
61 {
62 	int match;
63 
64 	mutex_lock(&dax_bus_lock);
65 	match = !!__dax_match_id(dax_drv, dev_name(dev));
66 	mutex_unlock(&dax_bus_lock);
67 
68 	return match;
69 }
70 
71 static int dax_match_type(struct dax_device_driver *dax_drv, struct device *dev)
72 {
73 	enum dax_driver_type type = DAXDRV_DEVICE_TYPE;
74 	struct dev_dax *dev_dax = to_dev_dax(dev);
75 
76 	if (dev_dax->region->res.flags & IORESOURCE_DAX_KMEM)
77 		type = DAXDRV_KMEM_TYPE;
78 
79 	if (dax_drv->type == type)
80 		return 1;
81 
82 	/* default to device mode if dax_kmem is disabled */
83 	if (dax_drv->type == DAXDRV_DEVICE_TYPE &&
84 	    !IS_ENABLED(CONFIG_DEV_DAX_KMEM))
85 		return 1;
86 
87 	return 0;
88 }
89 
90 enum id_action {
91 	ID_REMOVE,
92 	ID_ADD,
93 };
94 
95 static ssize_t do_id_store(struct device_driver *drv, const char *buf,
96 		size_t count, enum id_action action)
97 {
98 	struct dax_device_driver *dax_drv = to_dax_drv(drv);
99 	unsigned int region_id, id;
100 	char devname[DAX_NAME_LEN];
101 	struct dax_id *dax_id;
102 	ssize_t rc = count;
103 	int fields;
104 
105 	fields = sscanf(buf, "dax%d.%d", &region_id, &id);
106 	if (fields != 2)
107 		return -EINVAL;
108 	sprintf(devname, "dax%d.%d", region_id, id);
109 	if (!sysfs_streq(buf, devname))
110 		return -EINVAL;
111 
112 	mutex_lock(&dax_bus_lock);
113 	dax_id = __dax_match_id(dax_drv, buf);
114 	if (!dax_id) {
115 		if (action == ID_ADD) {
116 			dax_id = kzalloc(sizeof(*dax_id), GFP_KERNEL);
117 			if (dax_id) {
118 				strscpy(dax_id->dev_name, buf, DAX_NAME_LEN);
119 				list_add(&dax_id->list, &dax_drv->ids);
120 			} else
121 				rc = -ENOMEM;
122 		}
123 	} else if (action == ID_REMOVE) {
124 		list_del(&dax_id->list);
125 		kfree(dax_id);
126 	}
127 	mutex_unlock(&dax_bus_lock);
128 
129 	if (rc < 0)
130 		return rc;
131 	if (action == ID_ADD)
132 		rc = driver_attach(drv);
133 	if (rc)
134 		return rc;
135 	return count;
136 }
137 
138 static ssize_t new_id_store(struct device_driver *drv, const char *buf,
139 		size_t count)
140 {
141 	return do_id_store(drv, buf, count, ID_ADD);
142 }
143 static DRIVER_ATTR_WO(new_id);
144 
145 static ssize_t remove_id_store(struct device_driver *drv, const char *buf,
146 		size_t count)
147 {
148 	return do_id_store(drv, buf, count, ID_REMOVE);
149 }
150 static DRIVER_ATTR_WO(remove_id);
151 
152 static struct attribute *dax_drv_attrs[] = {
153 	&driver_attr_new_id.attr,
154 	&driver_attr_remove_id.attr,
155 	NULL,
156 };
157 ATTRIBUTE_GROUPS(dax_drv);
158 
159 static int dax_bus_match(struct device *dev, struct device_driver *drv);
160 
161 /*
162  * Static dax regions are regions created by an external subsystem
163  * nvdimm where a single range is assigned. Its boundaries are by the external
164  * subsystem and are usually limited to one physical memory range. For example,
165  * for PMEM it is usually defined by NVDIMM Namespace boundaries (i.e. a
166  * single contiguous range)
167  *
168  * On dynamic dax regions, the assigned region can be partitioned by dax core
169  * into multiple subdivisions. A subdivision is represented into one
170  * /dev/daxN.M device composed by one or more potentially discontiguous ranges.
171  *
172  * When allocating a dax region, drivers must set whether it's static
173  * (IORESOURCE_DAX_STATIC).  On static dax devices, the @pgmap is pre-assigned
174  * to dax core when calling devm_create_dev_dax(), whereas in dynamic dax
175  * devices it is NULL but afterwards allocated by dax core on device ->probe().
176  * Care is needed to make sure that dynamic dax devices are torn down with a
177  * cleared @pgmap field (see kill_dev_dax()).
178  */
179 static bool is_static(struct dax_region *dax_region)
180 {
181 	return (dax_region->res.flags & IORESOURCE_DAX_STATIC) != 0;
182 }
183 
184 bool static_dev_dax(struct dev_dax *dev_dax)
185 {
186 	return is_static(dev_dax->region);
187 }
188 EXPORT_SYMBOL_GPL(static_dev_dax);
189 
190 static u64 dev_dax_size(struct dev_dax *dev_dax)
191 {
192 	u64 size = 0;
193 	int i;
194 
195 	lockdep_assert_held(&dax_dev_rwsem);
196 
197 	for (i = 0; i < dev_dax->nr_range; i++)
198 		size += range_len(&dev_dax->ranges[i].range);
199 
200 	return size;
201 }
202 
203 static int dax_bus_probe(struct device *dev)
204 {
205 	struct dax_device_driver *dax_drv = to_dax_drv(dev->driver);
206 	struct dev_dax *dev_dax = to_dev_dax(dev);
207 	struct dax_region *dax_region = dev_dax->region;
208 	int rc;
209 	u64 size;
210 
211 	rc = down_read_interruptible(&dax_dev_rwsem);
212 	if (rc)
213 		return rc;
214 	size = dev_dax_size(dev_dax);
215 	up_read(&dax_dev_rwsem);
216 
217 	if (size == 0 || dev_dax->id < 0)
218 		return -ENXIO;
219 
220 	rc = dax_drv->probe(dev_dax);
221 
222 	if (rc || is_static(dax_region))
223 		return rc;
224 
225 	/*
226 	 * Track new seed creation only after successful probe of the
227 	 * previous seed.
228 	 */
229 	if (dax_region->seed == dev)
230 		dax_region->seed = NULL;
231 
232 	return 0;
233 }
234 
235 static void dax_bus_remove(struct device *dev)
236 {
237 	struct dax_device_driver *dax_drv = to_dax_drv(dev->driver);
238 	struct dev_dax *dev_dax = to_dev_dax(dev);
239 
240 	if (dax_drv->remove)
241 		dax_drv->remove(dev_dax);
242 }
243 
244 static const struct bus_type dax_bus_type = {
245 	.name = "dax",
246 	.uevent = dax_bus_uevent,
247 	.match = dax_bus_match,
248 	.probe = dax_bus_probe,
249 	.remove = dax_bus_remove,
250 	.drv_groups = dax_drv_groups,
251 };
252 
253 static int dax_bus_match(struct device *dev, struct device_driver *drv)
254 {
255 	struct dax_device_driver *dax_drv = to_dax_drv(drv);
256 
257 	if (dax_match_id(dax_drv, dev))
258 		return 1;
259 	return dax_match_type(dax_drv, dev);
260 }
261 
262 /*
263  * Rely on the fact that drvdata is set before the attributes are
264  * registered, and that the attributes are unregistered before drvdata
265  * is cleared to assume that drvdata is always valid.
266  */
267 static ssize_t id_show(struct device *dev,
268 		struct device_attribute *attr, char *buf)
269 {
270 	struct dax_region *dax_region = dev_get_drvdata(dev);
271 
272 	return sysfs_emit(buf, "%d\n", dax_region->id);
273 }
274 static DEVICE_ATTR_RO(id);
275 
276 static ssize_t region_size_show(struct device *dev,
277 		struct device_attribute *attr, char *buf)
278 {
279 	struct dax_region *dax_region = dev_get_drvdata(dev);
280 
281 	return sysfs_emit(buf, "%llu\n",
282 			  (unsigned long long)resource_size(&dax_region->res));
283 }
284 static struct device_attribute dev_attr_region_size = __ATTR(size, 0444,
285 		region_size_show, NULL);
286 
287 static ssize_t region_align_show(struct device *dev,
288 		struct device_attribute *attr, char *buf)
289 {
290 	struct dax_region *dax_region = dev_get_drvdata(dev);
291 
292 	return sysfs_emit(buf, "%u\n", dax_region->align);
293 }
294 static struct device_attribute dev_attr_region_align =
295 		__ATTR(align, 0400, region_align_show, NULL);
296 
297 #define for_each_dax_region_resource(dax_region, res) \
298 	for (res = (dax_region)->res.child; res; res = res->sibling)
299 
300 static unsigned long long dax_region_avail_size(struct dax_region *dax_region)
301 {
302 	resource_size_t size = resource_size(&dax_region->res);
303 	struct resource *res;
304 
305 	lockdep_assert_held(&dax_region_rwsem);
306 
307 	for_each_dax_region_resource(dax_region, res)
308 		size -= resource_size(res);
309 	return size;
310 }
311 
312 static ssize_t available_size_show(struct device *dev,
313 		struct device_attribute *attr, char *buf)
314 {
315 	struct dax_region *dax_region = dev_get_drvdata(dev);
316 	unsigned long long size;
317 	int rc;
318 
319 	rc = down_read_interruptible(&dax_region_rwsem);
320 	if (rc)
321 		return rc;
322 	size = dax_region_avail_size(dax_region);
323 	up_read(&dax_region_rwsem);
324 
325 	return sysfs_emit(buf, "%llu\n", size);
326 }
327 static DEVICE_ATTR_RO(available_size);
328 
329 static ssize_t seed_show(struct device *dev,
330 		struct device_attribute *attr, char *buf)
331 {
332 	struct dax_region *dax_region = dev_get_drvdata(dev);
333 	struct device *seed;
334 	ssize_t rc;
335 
336 	if (is_static(dax_region))
337 		return -EINVAL;
338 
339 	rc = down_read_interruptible(&dax_region_rwsem);
340 	if (rc)
341 		return rc;
342 	seed = dax_region->seed;
343 	rc = sysfs_emit(buf, "%s\n", seed ? dev_name(seed) : "");
344 	up_read(&dax_region_rwsem);
345 
346 	return rc;
347 }
348 static DEVICE_ATTR_RO(seed);
349 
350 static ssize_t create_show(struct device *dev,
351 		struct device_attribute *attr, char *buf)
352 {
353 	struct dax_region *dax_region = dev_get_drvdata(dev);
354 	struct device *youngest;
355 	ssize_t rc;
356 
357 	if (is_static(dax_region))
358 		return -EINVAL;
359 
360 	rc = down_read_interruptible(&dax_region_rwsem);
361 	if (rc)
362 		return rc;
363 	youngest = dax_region->youngest;
364 	rc = sysfs_emit(buf, "%s\n", youngest ? dev_name(youngest) : "");
365 	up_read(&dax_region_rwsem);
366 
367 	return rc;
368 }
369 
370 static struct dev_dax *__devm_create_dev_dax(struct dev_dax_data *data);
371 
372 static ssize_t create_store(struct device *dev, struct device_attribute *attr,
373 		const char *buf, size_t len)
374 {
375 	struct dax_region *dax_region = dev_get_drvdata(dev);
376 	unsigned long long avail;
377 	ssize_t rc;
378 	int val;
379 
380 	if (is_static(dax_region))
381 		return -EINVAL;
382 
383 	rc = kstrtoint(buf, 0, &val);
384 	if (rc)
385 		return rc;
386 	if (val != 1)
387 		return -EINVAL;
388 
389 	rc = down_write_killable(&dax_region_rwsem);
390 	if (rc)
391 		return rc;
392 	avail = dax_region_avail_size(dax_region);
393 	if (avail == 0)
394 		rc = -ENOSPC;
395 	else {
396 		struct dev_dax_data data = {
397 			.dax_region = dax_region,
398 			.size = 0,
399 			.id = -1,
400 			.memmap_on_memory = false,
401 		};
402 		struct dev_dax *dev_dax = __devm_create_dev_dax(&data);
403 
404 		if (IS_ERR(dev_dax))
405 			rc = PTR_ERR(dev_dax);
406 		else {
407 			/*
408 			 * In support of crafting multiple new devices
409 			 * simultaneously multiple seeds can be created,
410 			 * but only the first one that has not been
411 			 * successfully bound is tracked as the region
412 			 * seed.
413 			 */
414 			if (!dax_region->seed)
415 				dax_region->seed = &dev_dax->dev;
416 			dax_region->youngest = &dev_dax->dev;
417 			rc = len;
418 		}
419 	}
420 	up_write(&dax_region_rwsem);
421 
422 	return rc;
423 }
424 static DEVICE_ATTR_RW(create);
425 
426 void kill_dev_dax(struct dev_dax *dev_dax)
427 {
428 	struct dax_device *dax_dev = dev_dax->dax_dev;
429 	struct inode *inode = dax_inode(dax_dev);
430 
431 	kill_dax(dax_dev);
432 	unmap_mapping_range(inode->i_mapping, 0, 0, 1);
433 
434 	/*
435 	 * Dynamic dax region have the pgmap allocated via dev_kzalloc()
436 	 * and thus freed by devm. Clear the pgmap to not have stale pgmap
437 	 * ranges on probe() from previous reconfigurations of region devices.
438 	 */
439 	if (!static_dev_dax(dev_dax))
440 		dev_dax->pgmap = NULL;
441 }
442 EXPORT_SYMBOL_GPL(kill_dev_dax);
443 
444 static void trim_dev_dax_range(struct dev_dax *dev_dax)
445 {
446 	int i = dev_dax->nr_range - 1;
447 	struct range *range = &dev_dax->ranges[i].range;
448 	struct dax_region *dax_region = dev_dax->region;
449 
450 	lockdep_assert_held_write(&dax_region_rwsem);
451 	dev_dbg(&dev_dax->dev, "delete range[%d]: %#llx:%#llx\n", i,
452 		(unsigned long long)range->start,
453 		(unsigned long long)range->end);
454 
455 	__release_region(&dax_region->res, range->start, range_len(range));
456 	if (--dev_dax->nr_range == 0) {
457 		kfree(dev_dax->ranges);
458 		dev_dax->ranges = NULL;
459 	}
460 }
461 
462 static void free_dev_dax_ranges(struct dev_dax *dev_dax)
463 {
464 	while (dev_dax->nr_range)
465 		trim_dev_dax_range(dev_dax);
466 }
467 
468 static void unregister_dev_dax(void *dev)
469 {
470 	struct dev_dax *dev_dax = to_dev_dax(dev);
471 
472 	dev_dbg(dev, "%s\n", __func__);
473 
474 	down_write(&dax_region_rwsem);
475 	kill_dev_dax(dev_dax);
476 	device_del(dev);
477 	free_dev_dax_ranges(dev_dax);
478 	put_device(dev);
479 	up_write(&dax_region_rwsem);
480 }
481 
482 static void dax_region_free(struct kref *kref)
483 {
484 	struct dax_region *dax_region;
485 
486 	dax_region = container_of(kref, struct dax_region, kref);
487 	kfree(dax_region);
488 }
489 
490 static void dax_region_put(struct dax_region *dax_region)
491 {
492 	kref_put(&dax_region->kref, dax_region_free);
493 }
494 
495 /* a return value >= 0 indicates this invocation invalidated the id */
496 static int __free_dev_dax_id(struct dev_dax *dev_dax)
497 {
498 	struct dax_region *dax_region;
499 	int rc = dev_dax->id;
500 
501 	lockdep_assert_held_write(&dax_dev_rwsem);
502 
503 	if (!dev_dax->dyn_id || dev_dax->id < 0)
504 		return -1;
505 	dax_region = dev_dax->region;
506 	ida_free(&dax_region->ida, dev_dax->id);
507 	dax_region_put(dax_region);
508 	dev_dax->id = -1;
509 	return rc;
510 }
511 
512 static int free_dev_dax_id(struct dev_dax *dev_dax)
513 {
514 	int rc;
515 
516 	rc = down_write_killable(&dax_dev_rwsem);
517 	if (rc)
518 		return rc;
519 	rc = __free_dev_dax_id(dev_dax);
520 	up_write(&dax_dev_rwsem);
521 	return rc;
522 }
523 
524 static int alloc_dev_dax_id(struct dev_dax *dev_dax)
525 {
526 	struct dax_region *dax_region = dev_dax->region;
527 	int id;
528 
529 	id = ida_alloc(&dax_region->ida, GFP_KERNEL);
530 	if (id < 0)
531 		return id;
532 	kref_get(&dax_region->kref);
533 	dev_dax->dyn_id = true;
534 	dev_dax->id = id;
535 	return id;
536 }
537 
538 static ssize_t delete_store(struct device *dev, struct device_attribute *attr,
539 		const char *buf, size_t len)
540 {
541 	struct dax_region *dax_region = dev_get_drvdata(dev);
542 	struct dev_dax *dev_dax;
543 	struct device *victim;
544 	bool do_del = false;
545 	int rc;
546 
547 	if (is_static(dax_region))
548 		return -EINVAL;
549 
550 	victim = device_find_child_by_name(dax_region->dev, buf);
551 	if (!victim)
552 		return -ENXIO;
553 
554 	device_lock(dev);
555 	device_lock(victim);
556 	dev_dax = to_dev_dax(victim);
557 	down_write(&dax_dev_rwsem);
558 	if (victim->driver || dev_dax_size(dev_dax))
559 		rc = -EBUSY;
560 	else {
561 		/*
562 		 * Invalidate the device so it does not become active
563 		 * again, but always preserve device-id-0 so that
564 		 * /sys/bus/dax/ is guaranteed to be populated while any
565 		 * dax_region is registered.
566 		 */
567 		if (dev_dax->id > 0) {
568 			do_del = __free_dev_dax_id(dev_dax) >= 0;
569 			rc = len;
570 			if (dax_region->seed == victim)
571 				dax_region->seed = NULL;
572 			if (dax_region->youngest == victim)
573 				dax_region->youngest = NULL;
574 		} else
575 			rc = -EBUSY;
576 	}
577 	up_write(&dax_dev_rwsem);
578 	device_unlock(victim);
579 
580 	/* won the race to invalidate the device, clean it up */
581 	if (do_del)
582 		devm_release_action(dev, unregister_dev_dax, victim);
583 	device_unlock(dev);
584 	put_device(victim);
585 
586 	return rc;
587 }
588 static DEVICE_ATTR_WO(delete);
589 
590 static umode_t dax_region_visible(struct kobject *kobj, struct attribute *a,
591 		int n)
592 {
593 	struct device *dev = container_of(kobj, struct device, kobj);
594 	struct dax_region *dax_region = dev_get_drvdata(dev);
595 
596 	if (is_static(dax_region))
597 		if (a == &dev_attr_available_size.attr
598 				|| a == &dev_attr_create.attr
599 				|| a == &dev_attr_seed.attr
600 				|| a == &dev_attr_delete.attr)
601 			return 0;
602 	return a->mode;
603 }
604 
605 static struct attribute *dax_region_attributes[] = {
606 	&dev_attr_available_size.attr,
607 	&dev_attr_region_size.attr,
608 	&dev_attr_region_align.attr,
609 	&dev_attr_create.attr,
610 	&dev_attr_seed.attr,
611 	&dev_attr_delete.attr,
612 	&dev_attr_id.attr,
613 	NULL,
614 };
615 
616 static const struct attribute_group dax_region_attribute_group = {
617 	.name = "dax_region",
618 	.attrs = dax_region_attributes,
619 	.is_visible = dax_region_visible,
620 };
621 
622 static const struct attribute_group *dax_region_attribute_groups[] = {
623 	&dax_region_attribute_group,
624 	NULL,
625 };
626 
627 static void dax_region_unregister(void *region)
628 {
629 	struct dax_region *dax_region = region;
630 
631 	sysfs_remove_groups(&dax_region->dev->kobj,
632 			dax_region_attribute_groups);
633 	dax_region_put(dax_region);
634 }
635 
636 struct dax_region *alloc_dax_region(struct device *parent, int region_id,
637 		struct range *range, int target_node, unsigned int align,
638 		unsigned long flags)
639 {
640 	struct dax_region *dax_region;
641 
642 	/*
643 	 * The DAX core assumes that it can store its private data in
644 	 * parent->driver_data. This WARN is a reminder / safeguard for
645 	 * developers of device-dax drivers.
646 	 */
647 	if (dev_get_drvdata(parent)) {
648 		dev_WARN(parent, "dax core failed to setup private data\n");
649 		return NULL;
650 	}
651 
652 	if (!IS_ALIGNED(range->start, align)
653 			|| !IS_ALIGNED(range_len(range), align))
654 		return NULL;
655 
656 	dax_region = kzalloc(sizeof(*dax_region), GFP_KERNEL);
657 	if (!dax_region)
658 		return NULL;
659 
660 	dev_set_drvdata(parent, dax_region);
661 	kref_init(&dax_region->kref);
662 	dax_region->id = region_id;
663 	dax_region->align = align;
664 	dax_region->dev = parent;
665 	dax_region->target_node = target_node;
666 	ida_init(&dax_region->ida);
667 	dax_region->res = (struct resource) {
668 		.start = range->start,
669 		.end = range->end,
670 		.flags = IORESOURCE_MEM | flags,
671 	};
672 
673 	if (sysfs_create_groups(&parent->kobj, dax_region_attribute_groups)) {
674 		kfree(dax_region);
675 		return NULL;
676 	}
677 
678 	if (devm_add_action_or_reset(parent, dax_region_unregister, dax_region))
679 		return NULL;
680 	return dax_region;
681 }
682 EXPORT_SYMBOL_GPL(alloc_dax_region);
683 
684 static void dax_mapping_release(struct device *dev)
685 {
686 	struct dax_mapping *mapping = to_dax_mapping(dev);
687 	struct device *parent = dev->parent;
688 	struct dev_dax *dev_dax = to_dev_dax(parent);
689 
690 	ida_free(&dev_dax->ida, mapping->id);
691 	kfree(mapping);
692 	put_device(parent);
693 }
694 
695 static void unregister_dax_mapping(void *data)
696 {
697 	struct device *dev = data;
698 	struct dax_mapping *mapping = to_dax_mapping(dev);
699 	struct dev_dax *dev_dax = to_dev_dax(dev->parent);
700 
701 	dev_dbg(dev, "%s\n", __func__);
702 
703 	dev_dax->ranges[mapping->range_id].mapping = NULL;
704 	mapping->range_id = -1;
705 
706 	device_unregister(dev);
707 }
708 
709 static struct dev_dax_range *get_dax_range(struct device *dev)
710 {
711 	struct dax_mapping *mapping = to_dax_mapping(dev);
712 	struct dev_dax *dev_dax = to_dev_dax(dev->parent);
713 	int rc;
714 
715 	rc = down_write_killable(&dax_region_rwsem);
716 	if (rc)
717 		return NULL;
718 	if (mapping->range_id < 0) {
719 		up_write(&dax_region_rwsem);
720 		return NULL;
721 	}
722 
723 	return &dev_dax->ranges[mapping->range_id];
724 }
725 
726 static void put_dax_range(void)
727 {
728 	up_write(&dax_region_rwsem);
729 }
730 
731 static ssize_t start_show(struct device *dev,
732 		struct device_attribute *attr, char *buf)
733 {
734 	struct dev_dax_range *dax_range;
735 	ssize_t rc;
736 
737 	dax_range = get_dax_range(dev);
738 	if (!dax_range)
739 		return -ENXIO;
740 	rc = sysfs_emit(buf, "%#llx\n", dax_range->range.start);
741 	put_dax_range();
742 
743 	return rc;
744 }
745 static DEVICE_ATTR(start, 0400, start_show, NULL);
746 
747 static ssize_t end_show(struct device *dev,
748 		struct device_attribute *attr, char *buf)
749 {
750 	struct dev_dax_range *dax_range;
751 	ssize_t rc;
752 
753 	dax_range = get_dax_range(dev);
754 	if (!dax_range)
755 		return -ENXIO;
756 	rc = sysfs_emit(buf, "%#llx\n", dax_range->range.end);
757 	put_dax_range();
758 
759 	return rc;
760 }
761 static DEVICE_ATTR(end, 0400, end_show, NULL);
762 
763 static ssize_t pgoff_show(struct device *dev,
764 		struct device_attribute *attr, char *buf)
765 {
766 	struct dev_dax_range *dax_range;
767 	ssize_t rc;
768 
769 	dax_range = get_dax_range(dev);
770 	if (!dax_range)
771 		return -ENXIO;
772 	rc = sysfs_emit(buf, "%#lx\n", dax_range->pgoff);
773 	put_dax_range();
774 
775 	return rc;
776 }
777 static DEVICE_ATTR(page_offset, 0400, pgoff_show, NULL);
778 
779 static struct attribute *dax_mapping_attributes[] = {
780 	&dev_attr_start.attr,
781 	&dev_attr_end.attr,
782 	&dev_attr_page_offset.attr,
783 	NULL,
784 };
785 
786 static const struct attribute_group dax_mapping_attribute_group = {
787 	.attrs = dax_mapping_attributes,
788 };
789 
790 static const struct attribute_group *dax_mapping_attribute_groups[] = {
791 	&dax_mapping_attribute_group,
792 	NULL,
793 };
794 
795 static const struct device_type dax_mapping_type = {
796 	.release = dax_mapping_release,
797 	.groups = dax_mapping_attribute_groups,
798 };
799 
800 static int devm_register_dax_mapping(struct dev_dax *dev_dax, int range_id)
801 {
802 	struct dax_region *dax_region = dev_dax->region;
803 	struct dax_mapping *mapping;
804 	struct device *dev;
805 	int rc;
806 
807 	lockdep_assert_held_write(&dax_region_rwsem);
808 
809 	if (dev_WARN_ONCE(&dev_dax->dev, !dax_region->dev->driver,
810 				"region disabled\n"))
811 		return -ENXIO;
812 
813 	mapping = kzalloc(sizeof(*mapping), GFP_KERNEL);
814 	if (!mapping)
815 		return -ENOMEM;
816 	mapping->range_id = range_id;
817 	mapping->id = ida_alloc(&dev_dax->ida, GFP_KERNEL);
818 	if (mapping->id < 0) {
819 		kfree(mapping);
820 		return -ENOMEM;
821 	}
822 	dev_dax->ranges[range_id].mapping = mapping;
823 	dev = &mapping->dev;
824 	device_initialize(dev);
825 	dev->parent = &dev_dax->dev;
826 	get_device(dev->parent);
827 	dev->type = &dax_mapping_type;
828 	dev_set_name(dev, "mapping%d", mapping->id);
829 	rc = device_add(dev);
830 	if (rc) {
831 		put_device(dev);
832 		return rc;
833 	}
834 
835 	rc = devm_add_action_or_reset(dax_region->dev, unregister_dax_mapping,
836 			dev);
837 	if (rc)
838 		return rc;
839 	return 0;
840 }
841 
842 static int alloc_dev_dax_range(struct dev_dax *dev_dax, u64 start,
843 		resource_size_t size)
844 {
845 	struct dax_region *dax_region = dev_dax->region;
846 	struct resource *res = &dax_region->res;
847 	struct device *dev = &dev_dax->dev;
848 	struct dev_dax_range *ranges;
849 	unsigned long pgoff = 0;
850 	struct resource *alloc;
851 	int i, rc;
852 
853 	lockdep_assert_held_write(&dax_region_rwsem);
854 
855 	/* handle the seed alloc special case */
856 	if (!size) {
857 		if (dev_WARN_ONCE(dev, dev_dax->nr_range,
858 					"0-size allocation must be first\n"))
859 			return -EBUSY;
860 		/* nr_range == 0 is elsewhere special cased as 0-size device */
861 		return 0;
862 	}
863 
864 	alloc = __request_region(res, start, size, dev_name(dev), 0);
865 	if (!alloc)
866 		return -ENOMEM;
867 
868 	ranges = krealloc(dev_dax->ranges, sizeof(*ranges)
869 			* (dev_dax->nr_range + 1), GFP_KERNEL);
870 	if (!ranges) {
871 		__release_region(res, alloc->start, resource_size(alloc));
872 		return -ENOMEM;
873 	}
874 
875 	for (i = 0; i < dev_dax->nr_range; i++)
876 		pgoff += PHYS_PFN(range_len(&ranges[i].range));
877 	dev_dax->ranges = ranges;
878 	ranges[dev_dax->nr_range++] = (struct dev_dax_range) {
879 		.pgoff = pgoff,
880 		.range = {
881 			.start = alloc->start,
882 			.end = alloc->end,
883 		},
884 	};
885 
886 	dev_dbg(dev, "alloc range[%d]: %pa:%pa\n", dev_dax->nr_range - 1,
887 			&alloc->start, &alloc->end);
888 	/*
889 	 * A dev_dax instance must be registered before mapping device
890 	 * children can be added. Defer to devm_create_dev_dax() to add
891 	 * the initial mapping device.
892 	 */
893 	if (!device_is_registered(&dev_dax->dev))
894 		return 0;
895 
896 	rc = devm_register_dax_mapping(dev_dax, dev_dax->nr_range - 1);
897 	if (rc)
898 		trim_dev_dax_range(dev_dax);
899 
900 	return rc;
901 }
902 
903 static int adjust_dev_dax_range(struct dev_dax *dev_dax, struct resource *res, resource_size_t size)
904 {
905 	int last_range = dev_dax->nr_range - 1;
906 	struct dev_dax_range *dax_range = &dev_dax->ranges[last_range];
907 	bool is_shrink = resource_size(res) > size;
908 	struct range *range = &dax_range->range;
909 	struct device *dev = &dev_dax->dev;
910 	int rc;
911 
912 	lockdep_assert_held_write(&dax_region_rwsem);
913 
914 	if (dev_WARN_ONCE(dev, !size, "deletion is handled by dev_dax_shrink\n"))
915 		return -EINVAL;
916 
917 	rc = adjust_resource(res, range->start, size);
918 	if (rc)
919 		return rc;
920 
921 	*range = (struct range) {
922 		.start = range->start,
923 		.end = range->start + size - 1,
924 	};
925 
926 	dev_dbg(dev, "%s range[%d]: %#llx:%#llx\n", is_shrink ? "shrink" : "extend",
927 			last_range, (unsigned long long) range->start,
928 			(unsigned long long) range->end);
929 
930 	return 0;
931 }
932 
933 static ssize_t size_show(struct device *dev,
934 		struct device_attribute *attr, char *buf)
935 {
936 	struct dev_dax *dev_dax = to_dev_dax(dev);
937 	unsigned long long size;
938 	int rc;
939 
940 	rc = down_read_interruptible(&dax_dev_rwsem);
941 	if (rc)
942 		return rc;
943 	size = dev_dax_size(dev_dax);
944 	up_read(&dax_dev_rwsem);
945 
946 	return sysfs_emit(buf, "%llu\n", size);
947 }
948 
949 static bool alloc_is_aligned(struct dev_dax *dev_dax, resource_size_t size)
950 {
951 	/*
952 	 * The minimum mapping granularity for a device instance is a
953 	 * single subsection, unless the arch says otherwise.
954 	 */
955 	return IS_ALIGNED(size, max_t(unsigned long, dev_dax->align, memremap_compat_align()));
956 }
957 
958 static int dev_dax_shrink(struct dev_dax *dev_dax, resource_size_t size)
959 {
960 	resource_size_t to_shrink = dev_dax_size(dev_dax) - size;
961 	struct dax_region *dax_region = dev_dax->region;
962 	struct device *dev = &dev_dax->dev;
963 	int i;
964 
965 	for (i = dev_dax->nr_range - 1; i >= 0; i--) {
966 		struct range *range = &dev_dax->ranges[i].range;
967 		struct dax_mapping *mapping = dev_dax->ranges[i].mapping;
968 		struct resource *adjust = NULL, *res;
969 		resource_size_t shrink;
970 
971 		shrink = min_t(u64, to_shrink, range_len(range));
972 		if (shrink >= range_len(range)) {
973 			devm_release_action(dax_region->dev,
974 					unregister_dax_mapping, &mapping->dev);
975 			trim_dev_dax_range(dev_dax);
976 			to_shrink -= shrink;
977 			if (!to_shrink)
978 				break;
979 			continue;
980 		}
981 
982 		for_each_dax_region_resource(dax_region, res)
983 			if (strcmp(res->name, dev_name(dev)) == 0
984 					&& res->start == range->start) {
985 				adjust = res;
986 				break;
987 			}
988 
989 		if (dev_WARN_ONCE(dev, !adjust || i != dev_dax->nr_range - 1,
990 					"failed to find matching resource\n"))
991 			return -ENXIO;
992 		return adjust_dev_dax_range(dev_dax, adjust, range_len(range)
993 				- shrink);
994 	}
995 	return 0;
996 }
997 
998 /*
999  * Only allow adjustments that preserve the relative pgoff of existing
1000  * allocations. I.e. the dev_dax->ranges array is ordered by increasing pgoff.
1001  */
1002 static bool adjust_ok(struct dev_dax *dev_dax, struct resource *res)
1003 {
1004 	struct dev_dax_range *last;
1005 	int i;
1006 
1007 	if (dev_dax->nr_range == 0)
1008 		return false;
1009 	if (strcmp(res->name, dev_name(&dev_dax->dev)) != 0)
1010 		return false;
1011 	last = &dev_dax->ranges[dev_dax->nr_range - 1];
1012 	if (last->range.start != res->start || last->range.end != res->end)
1013 		return false;
1014 	for (i = 0; i < dev_dax->nr_range - 1; i++) {
1015 		struct dev_dax_range *dax_range = &dev_dax->ranges[i];
1016 
1017 		if (dax_range->pgoff > last->pgoff)
1018 			return false;
1019 	}
1020 
1021 	return true;
1022 }
1023 
1024 static ssize_t dev_dax_resize(struct dax_region *dax_region,
1025 		struct dev_dax *dev_dax, resource_size_t size)
1026 {
1027 	resource_size_t avail = dax_region_avail_size(dax_region), to_alloc;
1028 	resource_size_t dev_size = dev_dax_size(dev_dax);
1029 	struct resource *region_res = &dax_region->res;
1030 	struct device *dev = &dev_dax->dev;
1031 	struct resource *res, *first;
1032 	resource_size_t alloc = 0;
1033 	int rc;
1034 
1035 	if (dev->driver)
1036 		return -EBUSY;
1037 	if (size == dev_size)
1038 		return 0;
1039 	if (size > dev_size && size - dev_size > avail)
1040 		return -ENOSPC;
1041 	if (size < dev_size)
1042 		return dev_dax_shrink(dev_dax, size);
1043 
1044 	to_alloc = size - dev_size;
1045 	if (dev_WARN_ONCE(dev, !alloc_is_aligned(dev_dax, to_alloc),
1046 			"resize of %pa misaligned\n", &to_alloc))
1047 		return -ENXIO;
1048 
1049 	/*
1050 	 * Expand the device into the unused portion of the region. This
1051 	 * may involve adjusting the end of an existing resource, or
1052 	 * allocating a new resource.
1053 	 */
1054 retry:
1055 	first = region_res->child;
1056 	if (!first)
1057 		return alloc_dev_dax_range(dev_dax, dax_region->res.start, to_alloc);
1058 
1059 	rc = -ENOSPC;
1060 	for (res = first; res; res = res->sibling) {
1061 		struct resource *next = res->sibling;
1062 
1063 		/* space at the beginning of the region */
1064 		if (res == first && res->start > dax_region->res.start) {
1065 			alloc = min(res->start - dax_region->res.start, to_alloc);
1066 			rc = alloc_dev_dax_range(dev_dax, dax_region->res.start, alloc);
1067 			break;
1068 		}
1069 
1070 		alloc = 0;
1071 		/* space between allocations */
1072 		if (next && next->start > res->end + 1)
1073 			alloc = min(next->start - (res->end + 1), to_alloc);
1074 
1075 		/* space at the end of the region */
1076 		if (!alloc && !next && res->end < region_res->end)
1077 			alloc = min(region_res->end - res->end, to_alloc);
1078 
1079 		if (!alloc)
1080 			continue;
1081 
1082 		if (adjust_ok(dev_dax, res)) {
1083 			rc = adjust_dev_dax_range(dev_dax, res, resource_size(res) + alloc);
1084 			break;
1085 		}
1086 		rc = alloc_dev_dax_range(dev_dax, res->end + 1, alloc);
1087 		break;
1088 	}
1089 	if (rc)
1090 		return rc;
1091 	to_alloc -= alloc;
1092 	if (to_alloc)
1093 		goto retry;
1094 	return 0;
1095 }
1096 
1097 static ssize_t size_store(struct device *dev, struct device_attribute *attr,
1098 		const char *buf, size_t len)
1099 {
1100 	ssize_t rc;
1101 	unsigned long long val;
1102 	struct dev_dax *dev_dax = to_dev_dax(dev);
1103 	struct dax_region *dax_region = dev_dax->region;
1104 
1105 	rc = kstrtoull(buf, 0, &val);
1106 	if (rc)
1107 		return rc;
1108 
1109 	if (!alloc_is_aligned(dev_dax, val)) {
1110 		dev_dbg(dev, "%s: size: %lld misaligned\n", __func__, val);
1111 		return -EINVAL;
1112 	}
1113 
1114 	rc = down_write_killable(&dax_region_rwsem);
1115 	if (rc)
1116 		return rc;
1117 	if (!dax_region->dev->driver) {
1118 		rc = -ENXIO;
1119 		goto err_region;
1120 	}
1121 	rc = down_write_killable(&dax_dev_rwsem);
1122 	if (rc)
1123 		goto err_dev;
1124 
1125 	rc = dev_dax_resize(dax_region, dev_dax, val);
1126 
1127 err_dev:
1128 	up_write(&dax_dev_rwsem);
1129 err_region:
1130 	up_write(&dax_region_rwsem);
1131 
1132 	if (rc == 0)
1133 		return len;
1134 	return rc;
1135 }
1136 static DEVICE_ATTR_RW(size);
1137 
1138 static ssize_t range_parse(const char *opt, size_t len, struct range *range)
1139 {
1140 	unsigned long long addr = 0;
1141 	char *start, *end, *str;
1142 	ssize_t rc = -EINVAL;
1143 
1144 	str = kstrdup(opt, GFP_KERNEL);
1145 	if (!str)
1146 		return rc;
1147 
1148 	end = str;
1149 	start = strsep(&end, "-");
1150 	if (!start || !end)
1151 		goto err;
1152 
1153 	rc = kstrtoull(start, 16, &addr);
1154 	if (rc)
1155 		goto err;
1156 	range->start = addr;
1157 
1158 	rc = kstrtoull(end, 16, &addr);
1159 	if (rc)
1160 		goto err;
1161 	range->end = addr;
1162 
1163 err:
1164 	kfree(str);
1165 	return rc;
1166 }
1167 
1168 static ssize_t mapping_store(struct device *dev, struct device_attribute *attr,
1169 		const char *buf, size_t len)
1170 {
1171 	struct dev_dax *dev_dax = to_dev_dax(dev);
1172 	struct dax_region *dax_region = dev_dax->region;
1173 	size_t to_alloc;
1174 	struct range r;
1175 	ssize_t rc;
1176 
1177 	rc = range_parse(buf, len, &r);
1178 	if (rc)
1179 		return rc;
1180 
1181 	rc = down_write_killable(&dax_region_rwsem);
1182 	if (rc)
1183 		return rc;
1184 	if (!dax_region->dev->driver) {
1185 		up_write(&dax_region_rwsem);
1186 		return rc;
1187 	}
1188 	rc = down_write_killable(&dax_dev_rwsem);
1189 	if (rc) {
1190 		up_write(&dax_region_rwsem);
1191 		return rc;
1192 	}
1193 
1194 	to_alloc = range_len(&r);
1195 	if (alloc_is_aligned(dev_dax, to_alloc))
1196 		rc = alloc_dev_dax_range(dev_dax, r.start, to_alloc);
1197 	up_write(&dax_dev_rwsem);
1198 	up_write(&dax_region_rwsem);
1199 
1200 	return rc == 0 ? len : rc;
1201 }
1202 static DEVICE_ATTR_WO(mapping);
1203 
1204 static ssize_t align_show(struct device *dev,
1205 		struct device_attribute *attr, char *buf)
1206 {
1207 	struct dev_dax *dev_dax = to_dev_dax(dev);
1208 
1209 	return sysfs_emit(buf, "%d\n", dev_dax->align);
1210 }
1211 
1212 static ssize_t dev_dax_validate_align(struct dev_dax *dev_dax)
1213 {
1214 	struct device *dev = &dev_dax->dev;
1215 	int i;
1216 
1217 	for (i = 0; i < dev_dax->nr_range; i++) {
1218 		size_t len = range_len(&dev_dax->ranges[i].range);
1219 
1220 		if (!alloc_is_aligned(dev_dax, len)) {
1221 			dev_dbg(dev, "%s: align %u invalid for range %d\n",
1222 				__func__, dev_dax->align, i);
1223 			return -EINVAL;
1224 		}
1225 	}
1226 
1227 	return 0;
1228 }
1229 
1230 static ssize_t align_store(struct device *dev, struct device_attribute *attr,
1231 		const char *buf, size_t len)
1232 {
1233 	struct dev_dax *dev_dax = to_dev_dax(dev);
1234 	struct dax_region *dax_region = dev_dax->region;
1235 	unsigned long val, align_save;
1236 	ssize_t rc;
1237 
1238 	rc = kstrtoul(buf, 0, &val);
1239 	if (rc)
1240 		return -ENXIO;
1241 
1242 	if (!dax_align_valid(val))
1243 		return -EINVAL;
1244 
1245 	rc = down_write_killable(&dax_region_rwsem);
1246 	if (rc)
1247 		return rc;
1248 	if (!dax_region->dev->driver) {
1249 		up_write(&dax_region_rwsem);
1250 		return -ENXIO;
1251 	}
1252 
1253 	rc = down_write_killable(&dax_dev_rwsem);
1254 	if (rc) {
1255 		up_write(&dax_region_rwsem);
1256 		return rc;
1257 	}
1258 	if (dev->driver) {
1259 		rc = -EBUSY;
1260 		goto out_unlock;
1261 	}
1262 
1263 	align_save = dev_dax->align;
1264 	dev_dax->align = val;
1265 	rc = dev_dax_validate_align(dev_dax);
1266 	if (rc)
1267 		dev_dax->align = align_save;
1268 out_unlock:
1269 	up_write(&dax_dev_rwsem);
1270 	up_write(&dax_region_rwsem);
1271 	return rc == 0 ? len : rc;
1272 }
1273 static DEVICE_ATTR_RW(align);
1274 
1275 static int dev_dax_target_node(struct dev_dax *dev_dax)
1276 {
1277 	struct dax_region *dax_region = dev_dax->region;
1278 
1279 	return dax_region->target_node;
1280 }
1281 
1282 static ssize_t target_node_show(struct device *dev,
1283 		struct device_attribute *attr, char *buf)
1284 {
1285 	struct dev_dax *dev_dax = to_dev_dax(dev);
1286 
1287 	return sysfs_emit(buf, "%d\n", dev_dax_target_node(dev_dax));
1288 }
1289 static DEVICE_ATTR_RO(target_node);
1290 
1291 static ssize_t resource_show(struct device *dev,
1292 		struct device_attribute *attr, char *buf)
1293 {
1294 	struct dev_dax *dev_dax = to_dev_dax(dev);
1295 	struct dax_region *dax_region = dev_dax->region;
1296 	unsigned long long start;
1297 
1298 	if (dev_dax->nr_range < 1)
1299 		start = dax_region->res.start;
1300 	else
1301 		start = dev_dax->ranges[0].range.start;
1302 
1303 	return sysfs_emit(buf, "%#llx\n", start);
1304 }
1305 static DEVICE_ATTR(resource, 0400, resource_show, NULL);
1306 
1307 static ssize_t modalias_show(struct device *dev, struct device_attribute *attr,
1308 		char *buf)
1309 {
1310 	/*
1311 	 * We only ever expect to handle device-dax instances, i.e. the
1312 	 * @type argument to MODULE_ALIAS_DAX_DEVICE() is always zero
1313 	 */
1314 	return sysfs_emit(buf, DAX_DEVICE_MODALIAS_FMT "\n", 0);
1315 }
1316 static DEVICE_ATTR_RO(modalias);
1317 
1318 static ssize_t numa_node_show(struct device *dev,
1319 		struct device_attribute *attr, char *buf)
1320 {
1321 	return sysfs_emit(buf, "%d\n", dev_to_node(dev));
1322 }
1323 static DEVICE_ATTR_RO(numa_node);
1324 
1325 static ssize_t memmap_on_memory_show(struct device *dev,
1326 				     struct device_attribute *attr, char *buf)
1327 {
1328 	struct dev_dax *dev_dax = to_dev_dax(dev);
1329 
1330 	return sysfs_emit(buf, "%d\n", dev_dax->memmap_on_memory);
1331 }
1332 
1333 static ssize_t memmap_on_memory_store(struct device *dev,
1334 				      struct device_attribute *attr,
1335 				      const char *buf, size_t len)
1336 {
1337 	struct dev_dax *dev_dax = to_dev_dax(dev);
1338 	bool val;
1339 	int rc;
1340 
1341 	rc = kstrtobool(buf, &val);
1342 	if (rc)
1343 		return rc;
1344 
1345 	if (val == true && !mhp_supports_memmap_on_memory()) {
1346 		dev_dbg(dev, "memmap_on_memory is not available\n");
1347 		return -EOPNOTSUPP;
1348 	}
1349 
1350 	rc = down_write_killable(&dax_dev_rwsem);
1351 	if (rc)
1352 		return rc;
1353 
1354 	if (dev_dax->memmap_on_memory != val && dev->driver &&
1355 	    to_dax_drv(dev->driver)->type == DAXDRV_KMEM_TYPE) {
1356 		up_write(&dax_dev_rwsem);
1357 		return -EBUSY;
1358 	}
1359 
1360 	dev_dax->memmap_on_memory = val;
1361 	up_write(&dax_dev_rwsem);
1362 
1363 	return len;
1364 }
1365 static DEVICE_ATTR_RW(memmap_on_memory);
1366 
1367 static umode_t dev_dax_visible(struct kobject *kobj, struct attribute *a, int n)
1368 {
1369 	struct device *dev = container_of(kobj, struct device, kobj);
1370 	struct dev_dax *dev_dax = to_dev_dax(dev);
1371 	struct dax_region *dax_region = dev_dax->region;
1372 
1373 	if (a == &dev_attr_target_node.attr && dev_dax_target_node(dev_dax) < 0)
1374 		return 0;
1375 	if (a == &dev_attr_numa_node.attr && !IS_ENABLED(CONFIG_NUMA))
1376 		return 0;
1377 	if (a == &dev_attr_mapping.attr && is_static(dax_region))
1378 		return 0;
1379 	if ((a == &dev_attr_align.attr ||
1380 	     a == &dev_attr_size.attr) && is_static(dax_region))
1381 		return 0444;
1382 	return a->mode;
1383 }
1384 
1385 static struct attribute *dev_dax_attributes[] = {
1386 	&dev_attr_modalias.attr,
1387 	&dev_attr_size.attr,
1388 	&dev_attr_mapping.attr,
1389 	&dev_attr_target_node.attr,
1390 	&dev_attr_align.attr,
1391 	&dev_attr_resource.attr,
1392 	&dev_attr_numa_node.attr,
1393 	&dev_attr_memmap_on_memory.attr,
1394 	NULL,
1395 };
1396 
1397 static const struct attribute_group dev_dax_attribute_group = {
1398 	.attrs = dev_dax_attributes,
1399 	.is_visible = dev_dax_visible,
1400 };
1401 
1402 static const struct attribute_group *dax_attribute_groups[] = {
1403 	&dev_dax_attribute_group,
1404 	NULL,
1405 };
1406 
1407 static void dev_dax_release(struct device *dev)
1408 {
1409 	struct dev_dax *dev_dax = to_dev_dax(dev);
1410 	struct dax_device *dax_dev = dev_dax->dax_dev;
1411 
1412 	put_dax(dax_dev);
1413 	free_dev_dax_id(dev_dax);
1414 	kfree(dev_dax->pgmap);
1415 	kfree(dev_dax);
1416 }
1417 
1418 static const struct device_type dev_dax_type = {
1419 	.release = dev_dax_release,
1420 	.groups = dax_attribute_groups,
1421 };
1422 
1423 static struct dev_dax *__devm_create_dev_dax(struct dev_dax_data *data)
1424 {
1425 	struct dax_region *dax_region = data->dax_region;
1426 	struct device *parent = dax_region->dev;
1427 	struct dax_device *dax_dev;
1428 	struct dev_dax *dev_dax;
1429 	struct inode *inode;
1430 	struct device *dev;
1431 	int rc;
1432 
1433 	dev_dax = kzalloc(sizeof(*dev_dax), GFP_KERNEL);
1434 	if (!dev_dax)
1435 		return ERR_PTR(-ENOMEM);
1436 
1437 	dev_dax->region = dax_region;
1438 	if (is_static(dax_region)) {
1439 		if (dev_WARN_ONCE(parent, data->id < 0,
1440 				"dynamic id specified to static region\n")) {
1441 			rc = -EINVAL;
1442 			goto err_id;
1443 		}
1444 
1445 		dev_dax->id = data->id;
1446 	} else {
1447 		if (dev_WARN_ONCE(parent, data->id >= 0,
1448 				"static id specified to dynamic region\n")) {
1449 			rc = -EINVAL;
1450 			goto err_id;
1451 		}
1452 
1453 		rc = alloc_dev_dax_id(dev_dax);
1454 		if (rc < 0)
1455 			goto err_id;
1456 	}
1457 
1458 	dev = &dev_dax->dev;
1459 	device_initialize(dev);
1460 	dev_set_name(dev, "dax%d.%d", dax_region->id, dev_dax->id);
1461 
1462 	rc = alloc_dev_dax_range(dev_dax, dax_region->res.start, data->size);
1463 	if (rc)
1464 		goto err_range;
1465 
1466 	if (data->pgmap) {
1467 		dev_WARN_ONCE(parent, !is_static(dax_region),
1468 			"custom dev_pagemap requires a static dax_region\n");
1469 
1470 		dev_dax->pgmap = kmemdup(data->pgmap,
1471 				sizeof(struct dev_pagemap), GFP_KERNEL);
1472 		if (!dev_dax->pgmap) {
1473 			rc = -ENOMEM;
1474 			goto err_pgmap;
1475 		}
1476 	}
1477 
1478 	/*
1479 	 * No dax_operations since there is no access to this device outside of
1480 	 * mmap of the resulting character device.
1481 	 */
1482 	dax_dev = alloc_dax(dev_dax, NULL);
1483 	if (IS_ERR(dax_dev)) {
1484 		rc = PTR_ERR(dax_dev);
1485 		goto err_alloc_dax;
1486 	}
1487 	set_dax_synchronous(dax_dev);
1488 	set_dax_nocache(dax_dev);
1489 	set_dax_nomc(dax_dev);
1490 
1491 	/* a device_dax instance is dead while the driver is not attached */
1492 	kill_dax(dax_dev);
1493 
1494 	dev_dax->dax_dev = dax_dev;
1495 	dev_dax->target_node = dax_region->target_node;
1496 	dev_dax->align = dax_region->align;
1497 	ida_init(&dev_dax->ida);
1498 
1499 	dev_dax->memmap_on_memory = data->memmap_on_memory;
1500 
1501 	inode = dax_inode(dax_dev);
1502 	dev->devt = inode->i_rdev;
1503 	dev->bus = &dax_bus_type;
1504 	dev->parent = parent;
1505 	dev->type = &dev_dax_type;
1506 
1507 	rc = device_add(dev);
1508 	if (rc) {
1509 		kill_dev_dax(dev_dax);
1510 		put_device(dev);
1511 		return ERR_PTR(rc);
1512 	}
1513 
1514 	rc = devm_add_action_or_reset(dax_region->dev, unregister_dev_dax, dev);
1515 	if (rc)
1516 		return ERR_PTR(rc);
1517 
1518 	/* register mapping device for the initial allocation range */
1519 	if (dev_dax->nr_range && range_len(&dev_dax->ranges[0].range)) {
1520 		rc = devm_register_dax_mapping(dev_dax, 0);
1521 		if (rc)
1522 			return ERR_PTR(rc);
1523 	}
1524 
1525 	return dev_dax;
1526 
1527 err_alloc_dax:
1528 	kfree(dev_dax->pgmap);
1529 err_pgmap:
1530 	free_dev_dax_ranges(dev_dax);
1531 err_range:
1532 	free_dev_dax_id(dev_dax);
1533 err_id:
1534 	kfree(dev_dax);
1535 
1536 	return ERR_PTR(rc);
1537 }
1538 
1539 struct dev_dax *devm_create_dev_dax(struct dev_dax_data *data)
1540 {
1541 	struct dev_dax *dev_dax;
1542 
1543 	down_write(&dax_region_rwsem);
1544 	dev_dax = __devm_create_dev_dax(data);
1545 	up_write(&dax_region_rwsem);
1546 
1547 	return dev_dax;
1548 }
1549 EXPORT_SYMBOL_GPL(devm_create_dev_dax);
1550 
1551 int __dax_driver_register(struct dax_device_driver *dax_drv,
1552 		struct module *module, const char *mod_name)
1553 {
1554 	struct device_driver *drv = &dax_drv->drv;
1555 
1556 	/*
1557 	 * dax_bus_probe() calls dax_drv->probe() unconditionally.
1558 	 * So better be safe than sorry and ensure it is provided.
1559 	 */
1560 	if (!dax_drv->probe)
1561 		return -EINVAL;
1562 
1563 	INIT_LIST_HEAD(&dax_drv->ids);
1564 	drv->owner = module;
1565 	drv->name = mod_name;
1566 	drv->mod_name = mod_name;
1567 	drv->bus = &dax_bus_type;
1568 
1569 	return driver_register(drv);
1570 }
1571 EXPORT_SYMBOL_GPL(__dax_driver_register);
1572 
1573 void dax_driver_unregister(struct dax_device_driver *dax_drv)
1574 {
1575 	struct device_driver *drv = &dax_drv->drv;
1576 	struct dax_id *dax_id, *_id;
1577 
1578 	mutex_lock(&dax_bus_lock);
1579 	list_for_each_entry_safe(dax_id, _id, &dax_drv->ids, list) {
1580 		list_del(&dax_id->list);
1581 		kfree(dax_id);
1582 	}
1583 	mutex_unlock(&dax_bus_lock);
1584 	driver_unregister(drv);
1585 }
1586 EXPORT_SYMBOL_GPL(dax_driver_unregister);
1587 
1588 int __init dax_bus_init(void)
1589 {
1590 	return bus_register(&dax_bus_type);
1591 }
1592 
1593 void __exit dax_bus_exit(void)
1594 {
1595 	bus_unregister(&dax_bus_type);
1596 }
1597