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