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