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