xref: /linux/drivers/dax/bus.c (revision f5516ec5efb9fe0f426a46eeef25d389d3c2f988)
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 "dax-private.h"
10 #include "bus.h"
11 
12 static struct class *dax_class;
13 
14 static DEFINE_MUTEX(dax_bus_lock);
15 
16 #define DAX_NAME_LEN 30
17 struct dax_id {
18 	struct list_head list;
19 	char dev_name[DAX_NAME_LEN];
20 };
21 
22 static int dax_bus_uevent(struct device *dev, struct kobj_uevent_env *env)
23 {
24 	/*
25 	 * We only ever expect to handle device-dax instances, i.e. the
26 	 * @type argument to MODULE_ALIAS_DAX_DEVICE() is always zero
27 	 */
28 	return add_uevent_var(env, "MODALIAS=" DAX_DEVICE_MODALIAS_FMT, 0);
29 }
30 
31 static struct dax_device_driver *to_dax_drv(struct device_driver *drv)
32 {
33 	return container_of(drv, struct dax_device_driver, drv);
34 }
35 
36 static struct dax_id *__dax_match_id(struct dax_device_driver *dax_drv,
37 		const char *dev_name)
38 {
39 	struct dax_id *dax_id;
40 
41 	lockdep_assert_held(&dax_bus_lock);
42 
43 	list_for_each_entry(dax_id, &dax_drv->ids, list)
44 		if (sysfs_streq(dax_id->dev_name, dev_name))
45 			return dax_id;
46 	return NULL;
47 }
48 
49 static int dax_match_id(struct dax_device_driver *dax_drv, struct device *dev)
50 {
51 	int match;
52 
53 	mutex_lock(&dax_bus_lock);
54 	match = !!__dax_match_id(dax_drv, dev_name(dev));
55 	mutex_unlock(&dax_bus_lock);
56 
57 	return match;
58 }
59 
60 enum id_action {
61 	ID_REMOVE,
62 	ID_ADD,
63 };
64 
65 static ssize_t do_id_store(struct device_driver *drv, const char *buf,
66 		size_t count, enum id_action action)
67 {
68 	struct dax_device_driver *dax_drv = to_dax_drv(drv);
69 	unsigned int region_id, id;
70 	char devname[DAX_NAME_LEN];
71 	struct dax_id *dax_id;
72 	ssize_t rc = count;
73 	int fields;
74 
75 	fields = sscanf(buf, "dax%d.%d", &region_id, &id);
76 	if (fields != 2)
77 		return -EINVAL;
78 	sprintf(devname, "dax%d.%d", region_id, id);
79 	if (!sysfs_streq(buf, devname))
80 		return -EINVAL;
81 
82 	mutex_lock(&dax_bus_lock);
83 	dax_id = __dax_match_id(dax_drv, buf);
84 	if (!dax_id) {
85 		if (action == ID_ADD) {
86 			dax_id = kzalloc(sizeof(*dax_id), GFP_KERNEL);
87 			if (dax_id) {
88 				strncpy(dax_id->dev_name, buf, DAX_NAME_LEN);
89 				list_add(&dax_id->list, &dax_drv->ids);
90 			} else
91 				rc = -ENOMEM;
92 		} else
93 			/* nothing to remove */;
94 	} else if (action == ID_REMOVE) {
95 		list_del(&dax_id->list);
96 		kfree(dax_id);
97 	} else
98 		/* dax_id already added */;
99 	mutex_unlock(&dax_bus_lock);
100 
101 	if (rc < 0)
102 		return rc;
103 	if (action == ID_ADD)
104 		rc = driver_attach(drv);
105 	if (rc)
106 		return rc;
107 	return count;
108 }
109 
110 static ssize_t new_id_store(struct device_driver *drv, const char *buf,
111 		size_t count)
112 {
113 	return do_id_store(drv, buf, count, ID_ADD);
114 }
115 static DRIVER_ATTR_WO(new_id);
116 
117 static ssize_t remove_id_store(struct device_driver *drv, const char *buf,
118 		size_t count)
119 {
120 	return do_id_store(drv, buf, count, ID_REMOVE);
121 }
122 static DRIVER_ATTR_WO(remove_id);
123 
124 static struct attribute *dax_drv_attrs[] = {
125 	&driver_attr_new_id.attr,
126 	&driver_attr_remove_id.attr,
127 	NULL,
128 };
129 ATTRIBUTE_GROUPS(dax_drv);
130 
131 static int dax_bus_match(struct device *dev, struct device_driver *drv);
132 
133 static struct bus_type dax_bus_type = {
134 	.name = "dax",
135 	.uevent = dax_bus_uevent,
136 	.match = dax_bus_match,
137 	.drv_groups = dax_drv_groups,
138 };
139 
140 static int dax_bus_match(struct device *dev, struct device_driver *drv)
141 {
142 	struct dax_device_driver *dax_drv = to_dax_drv(drv);
143 
144 	/*
145 	 * All but the 'device-dax' driver, which has 'match_always'
146 	 * set, requires an exact id match.
147 	 */
148 	if (dax_drv->match_always)
149 		return 1;
150 
151 	return dax_match_id(dax_drv, dev);
152 }
153 
154 /*
155  * Rely on the fact that drvdata is set before the attributes are
156  * registered, and that the attributes are unregistered before drvdata
157  * is cleared to assume that drvdata is always valid.
158  */
159 static ssize_t id_show(struct device *dev,
160 		struct device_attribute *attr, char *buf)
161 {
162 	struct dax_region *dax_region = dev_get_drvdata(dev);
163 
164 	return sprintf(buf, "%d\n", dax_region->id);
165 }
166 static DEVICE_ATTR_RO(id);
167 
168 static ssize_t region_size_show(struct device *dev,
169 		struct device_attribute *attr, char *buf)
170 {
171 	struct dax_region *dax_region = dev_get_drvdata(dev);
172 
173 	return sprintf(buf, "%llu\n", (unsigned long long)
174 			resource_size(&dax_region->res));
175 }
176 static struct device_attribute dev_attr_region_size = __ATTR(size, 0444,
177 		region_size_show, NULL);
178 
179 static ssize_t align_show(struct device *dev,
180 		struct device_attribute *attr, char *buf)
181 {
182 	struct dax_region *dax_region = dev_get_drvdata(dev);
183 
184 	return sprintf(buf, "%u\n", dax_region->align);
185 }
186 static DEVICE_ATTR_RO(align);
187 
188 static struct attribute *dax_region_attributes[] = {
189 	&dev_attr_region_size.attr,
190 	&dev_attr_align.attr,
191 	&dev_attr_id.attr,
192 	NULL,
193 };
194 
195 static const struct attribute_group dax_region_attribute_group = {
196 	.name = "dax_region",
197 	.attrs = dax_region_attributes,
198 };
199 
200 static const struct attribute_group *dax_region_attribute_groups[] = {
201 	&dax_region_attribute_group,
202 	NULL,
203 };
204 
205 static void dax_region_free(struct kref *kref)
206 {
207 	struct dax_region *dax_region;
208 
209 	dax_region = container_of(kref, struct dax_region, kref);
210 	kfree(dax_region);
211 }
212 
213 void dax_region_put(struct dax_region *dax_region)
214 {
215 	kref_put(&dax_region->kref, dax_region_free);
216 }
217 EXPORT_SYMBOL_GPL(dax_region_put);
218 
219 static void dax_region_unregister(void *region)
220 {
221 	struct dax_region *dax_region = region;
222 
223 	sysfs_remove_groups(&dax_region->dev->kobj,
224 			dax_region_attribute_groups);
225 	dax_region_put(dax_region);
226 }
227 
228 struct dax_region *alloc_dax_region(struct device *parent, int region_id,
229 		struct resource *res, int target_node, unsigned int align)
230 {
231 	struct dax_region *dax_region;
232 
233 	/*
234 	 * The DAX core assumes that it can store its private data in
235 	 * parent->driver_data. This WARN is a reminder / safeguard for
236 	 * developers of device-dax drivers.
237 	 */
238 	if (dev_get_drvdata(parent)) {
239 		dev_WARN(parent, "dax core failed to setup private data\n");
240 		return NULL;
241 	}
242 
243 	if (!IS_ALIGNED(res->start, align)
244 			|| !IS_ALIGNED(resource_size(res), align))
245 		return NULL;
246 
247 	dax_region = kzalloc(sizeof(*dax_region), GFP_KERNEL);
248 	if (!dax_region)
249 		return NULL;
250 
251 	dev_set_drvdata(parent, dax_region);
252 	memcpy(&dax_region->res, res, sizeof(*res));
253 	kref_init(&dax_region->kref);
254 	dax_region->id = region_id;
255 	dax_region->align = align;
256 	dax_region->dev = parent;
257 	dax_region->target_node = target_node;
258 	if (sysfs_create_groups(&parent->kobj, dax_region_attribute_groups)) {
259 		kfree(dax_region);
260 		return NULL;
261 	}
262 
263 	kref_get(&dax_region->kref);
264 	if (devm_add_action_or_reset(parent, dax_region_unregister, dax_region))
265 		return NULL;
266 	return dax_region;
267 }
268 EXPORT_SYMBOL_GPL(alloc_dax_region);
269 
270 static ssize_t size_show(struct device *dev,
271 		struct device_attribute *attr, char *buf)
272 {
273 	struct dev_dax *dev_dax = to_dev_dax(dev);
274 	unsigned long long size = range_len(&dev_dax->range);
275 
276 	return sprintf(buf, "%llu\n", size);
277 }
278 static DEVICE_ATTR_RO(size);
279 
280 static int dev_dax_target_node(struct dev_dax *dev_dax)
281 {
282 	struct dax_region *dax_region = dev_dax->region;
283 
284 	return dax_region->target_node;
285 }
286 
287 static ssize_t target_node_show(struct device *dev,
288 		struct device_attribute *attr, char *buf)
289 {
290 	struct dev_dax *dev_dax = to_dev_dax(dev);
291 
292 	return sprintf(buf, "%d\n", dev_dax_target_node(dev_dax));
293 }
294 static DEVICE_ATTR_RO(target_node);
295 
296 static ssize_t resource_show(struct device *dev,
297 		struct device_attribute *attr, char *buf)
298 {
299 	struct dev_dax *dev_dax = to_dev_dax(dev);
300 
301 	return sprintf(buf, "%#llx\n", dev_dax->range.start);
302 }
303 static DEVICE_ATTR(resource, 0400, resource_show, NULL);
304 
305 static ssize_t modalias_show(struct device *dev, struct device_attribute *attr,
306 		char *buf)
307 {
308 	/*
309 	 * We only ever expect to handle device-dax instances, i.e. the
310 	 * @type argument to MODULE_ALIAS_DAX_DEVICE() is always zero
311 	 */
312 	return sprintf(buf, DAX_DEVICE_MODALIAS_FMT "\n", 0);
313 }
314 static DEVICE_ATTR_RO(modalias);
315 
316 static ssize_t numa_node_show(struct device *dev,
317 		struct device_attribute *attr, char *buf)
318 {
319 	return sprintf(buf, "%d\n", dev_to_node(dev));
320 }
321 static DEVICE_ATTR_RO(numa_node);
322 
323 static umode_t dev_dax_visible(struct kobject *kobj, struct attribute *a, int n)
324 {
325 	struct device *dev = container_of(kobj, struct device, kobj);
326 	struct dev_dax *dev_dax = to_dev_dax(dev);
327 
328 	if (a == &dev_attr_target_node.attr && dev_dax_target_node(dev_dax) < 0)
329 		return 0;
330 	if (a == &dev_attr_numa_node.attr && !IS_ENABLED(CONFIG_NUMA))
331 		return 0;
332 	return a->mode;
333 }
334 
335 static struct attribute *dev_dax_attributes[] = {
336 	&dev_attr_modalias.attr,
337 	&dev_attr_size.attr,
338 	&dev_attr_target_node.attr,
339 	&dev_attr_resource.attr,
340 	&dev_attr_numa_node.attr,
341 	NULL,
342 };
343 
344 static const struct attribute_group dev_dax_attribute_group = {
345 	.attrs = dev_dax_attributes,
346 	.is_visible = dev_dax_visible,
347 };
348 
349 static const struct attribute_group *dax_attribute_groups[] = {
350 	&dev_dax_attribute_group,
351 	NULL,
352 };
353 
354 void kill_dev_dax(struct dev_dax *dev_dax)
355 {
356 	struct dax_device *dax_dev = dev_dax->dax_dev;
357 	struct inode *inode = dax_inode(dax_dev);
358 
359 	kill_dax(dax_dev);
360 	unmap_mapping_range(inode->i_mapping, 0, 0, 1);
361 }
362 EXPORT_SYMBOL_GPL(kill_dev_dax);
363 
364 static void dev_dax_release(struct device *dev)
365 {
366 	struct dev_dax *dev_dax = to_dev_dax(dev);
367 	struct dax_region *dax_region = dev_dax->region;
368 	struct dax_device *dax_dev = dev_dax->dax_dev;
369 
370 	dax_region_put(dax_region);
371 	put_dax(dax_dev);
372 	kfree(dev_dax->pgmap);
373 	kfree(dev_dax);
374 }
375 
376 static const struct device_type dev_dax_type = {
377 	.release = dev_dax_release,
378 	.groups = dax_attribute_groups,
379 };
380 
381 static void unregister_dev_dax(void *dev)
382 {
383 	struct dev_dax *dev_dax = to_dev_dax(dev);
384 
385 	dev_dbg(dev, "%s\n", __func__);
386 
387 	kill_dev_dax(dev_dax);
388 	device_del(dev);
389 	put_device(dev);
390 }
391 
392 struct dev_dax *devm_create_dev_dax(struct dev_dax_data *data)
393 {
394 	struct dax_region *dax_region = data->dax_region;
395 	struct device *parent = dax_region->dev;
396 	struct dax_device *dax_dev;
397 	struct dev_dax *dev_dax;
398 	struct inode *inode;
399 	struct device *dev;
400 	int rc = -ENOMEM;
401 
402 	if (data->id < 0)
403 		return ERR_PTR(-EINVAL);
404 
405 	dev_dax = kzalloc(sizeof(*dev_dax), GFP_KERNEL);
406 	if (!dev_dax)
407 		return ERR_PTR(-ENOMEM);
408 
409 	if (data->pgmap) {
410 		dev_dax->pgmap = kmemdup(data->pgmap,
411 				sizeof(struct dev_pagemap), GFP_KERNEL);
412 		if (!dev_dax->pgmap)
413 			goto err_pgmap;
414 	}
415 
416 	/*
417 	 * No 'host' or dax_operations since there is no access to this
418 	 * device outside of mmap of the resulting character device.
419 	 */
420 	dax_dev = alloc_dax(dev_dax, NULL, NULL, DAXDEV_F_SYNC);
421 	if (IS_ERR(dax_dev)) {
422 		rc = PTR_ERR(dax_dev);
423 		goto err_alloc_dax;
424 	}
425 
426 	/* a device_dax instance is dead while the driver is not attached */
427 	kill_dax(dax_dev);
428 
429 	/* from here on we're committed to teardown via dev_dax_release() */
430 	dev = &dev_dax->dev;
431 	device_initialize(dev);
432 
433 	dev_dax->dax_dev = dax_dev;
434 	dev_dax->region = dax_region;
435 	dev_dax->range = data->range;
436 	dev_dax->target_node = dax_region->target_node;
437 	kref_get(&dax_region->kref);
438 
439 	inode = dax_inode(dax_dev);
440 	dev->devt = inode->i_rdev;
441 	if (data->subsys == DEV_DAX_BUS)
442 		dev->bus = &dax_bus_type;
443 	else
444 		dev->class = dax_class;
445 	dev->parent = parent;
446 	dev->type = &dev_dax_type;
447 	dev_set_name(dev, "dax%d.%d", dax_region->id, data->id);
448 
449 	rc = device_add(dev);
450 	if (rc) {
451 		kill_dev_dax(dev_dax);
452 		put_device(dev);
453 		return ERR_PTR(rc);
454 	}
455 
456 	rc = devm_add_action_or_reset(dax_region->dev, unregister_dev_dax, dev);
457 	if (rc)
458 		return ERR_PTR(rc);
459 
460 	return dev_dax;
461 err_alloc_dax:
462 	kfree(dev_dax->pgmap);
463 err_pgmap:
464 	kfree(dev_dax);
465 
466 	return ERR_PTR(rc);
467 }
468 EXPORT_SYMBOL_GPL(devm_create_dev_dax);
469 
470 static int match_always_count;
471 
472 int __dax_driver_register(struct dax_device_driver *dax_drv,
473 		struct module *module, const char *mod_name)
474 {
475 	struct device_driver *drv = &dax_drv->drv;
476 	int rc = 0;
477 
478 	INIT_LIST_HEAD(&dax_drv->ids);
479 	drv->owner = module;
480 	drv->name = mod_name;
481 	drv->mod_name = mod_name;
482 	drv->bus = &dax_bus_type;
483 
484 	/* there can only be one default driver */
485 	mutex_lock(&dax_bus_lock);
486 	match_always_count += dax_drv->match_always;
487 	if (match_always_count > 1) {
488 		match_always_count--;
489 		WARN_ON(1);
490 		rc = -EINVAL;
491 	}
492 	mutex_unlock(&dax_bus_lock);
493 	if (rc)
494 		return rc;
495 	return driver_register(drv);
496 }
497 EXPORT_SYMBOL_GPL(__dax_driver_register);
498 
499 void dax_driver_unregister(struct dax_device_driver *dax_drv)
500 {
501 	struct device_driver *drv = &dax_drv->drv;
502 	struct dax_id *dax_id, *_id;
503 
504 	mutex_lock(&dax_bus_lock);
505 	match_always_count -= dax_drv->match_always;
506 	list_for_each_entry_safe(dax_id, _id, &dax_drv->ids, list) {
507 		list_del(&dax_id->list);
508 		kfree(dax_id);
509 	}
510 	mutex_unlock(&dax_bus_lock);
511 	driver_unregister(drv);
512 }
513 EXPORT_SYMBOL_GPL(dax_driver_unregister);
514 
515 int __init dax_bus_init(void)
516 {
517 	int rc;
518 
519 	if (IS_ENABLED(CONFIG_DEV_DAX_PMEM_COMPAT)) {
520 		dax_class = class_create(THIS_MODULE, "dax");
521 		if (IS_ERR(dax_class))
522 			return PTR_ERR(dax_class);
523 	}
524 
525 	rc = bus_register(&dax_bus_type);
526 	if (rc)
527 		class_destroy(dax_class);
528 	return rc;
529 }
530 
531 void __exit dax_bus_exit(void)
532 {
533 	bus_unregister(&dax_bus_type);
534 	class_destroy(dax_class);
535 }
536