bus.c 34.5 KB
Newer Older
1 2
// SPDX-License-Identifier: GPL-2.0
/* Copyright(c) 2017-2018 Intel Corporation. All rights reserved. */
3
#include <linux/memremap.h>
4
#include <linux/device.h>
5 6
#include <linux/mutex.h>
#include <linux/list.h>
7 8
#include <linux/slab.h>
#include <linux/dax.h>
D
Dan Williams 已提交
9
#include <linux/io.h>
10 11 12
#include "dax-private.h"
#include "bus.h"

13 14
static struct class *dax_class;

15 16 17 18 19 20 21 22
static DEFINE_MUTEX(dax_bus_lock);

#define DAX_NAME_LEN 30
struct dax_id {
	struct list_head list;
	char dev_name[DAX_NAME_LEN];
};

23 24 25 26 27 28 29 30 31
static int dax_bus_uevent(struct device *dev, struct kobj_uevent_env *env)
{
	/*
	 * We only ever expect to handle device-dax instances, i.e. the
	 * @type argument to MODULE_ALIAS_DAX_DEVICE() is always zero
	 */
	return add_uevent_var(env, "MODALIAS=" DAX_DEVICE_MODALIAS_FMT, 0);
}

32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
static struct dax_device_driver *to_dax_drv(struct device_driver *drv)
{
	return container_of(drv, struct dax_device_driver, drv);
}

static struct dax_id *__dax_match_id(struct dax_device_driver *dax_drv,
		const char *dev_name)
{
	struct dax_id *dax_id;

	lockdep_assert_held(&dax_bus_lock);

	list_for_each_entry(dax_id, &dax_drv->ids, list)
		if (sysfs_streq(dax_id->dev_name, dev_name))
			return dax_id;
	return NULL;
}

static int dax_match_id(struct dax_device_driver *dax_drv, struct device *dev)
{
	int match;

	mutex_lock(&dax_bus_lock);
	match = !!__dax_match_id(dax_drv, dev_name(dev));
	mutex_unlock(&dax_bus_lock);

	return match;
}

61 62 63 64 65
enum id_action {
	ID_REMOVE,
	ID_ADD,
};

66
static ssize_t do_id_store(struct device_driver *drv, const char *buf,
67
		size_t count, enum id_action action)
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
{
	struct dax_device_driver *dax_drv = to_dax_drv(drv);
	unsigned int region_id, id;
	char devname[DAX_NAME_LEN];
	struct dax_id *dax_id;
	ssize_t rc = count;
	int fields;

	fields = sscanf(buf, "dax%d.%d", &region_id, &id);
	if (fields != 2)
		return -EINVAL;
	sprintf(devname, "dax%d.%d", region_id, id);
	if (!sysfs_streq(buf, devname))
		return -EINVAL;

	mutex_lock(&dax_bus_lock);
	dax_id = __dax_match_id(dax_drv, buf);
	if (!dax_id) {
86
		if (action == ID_ADD) {
87 88 89 90 91 92 93 94
			dax_id = kzalloc(sizeof(*dax_id), GFP_KERNEL);
			if (dax_id) {
				strncpy(dax_id->dev_name, buf, DAX_NAME_LEN);
				list_add(&dax_id->list, &dax_drv->ids);
			} else
				rc = -ENOMEM;
		} else
			/* nothing to remove */;
95
	} else if (action == ID_REMOVE) {
96 97 98 99 100
		list_del(&dax_id->list);
		kfree(dax_id);
	} else
		/* dax_id already added */;
	mutex_unlock(&dax_bus_lock);
101 102 103 104 105 106 107 108

	if (rc < 0)
		return rc;
	if (action == ID_ADD)
		rc = driver_attach(drv);
	if (rc)
		return rc;
	return count;
109 110 111 112 113
}

static ssize_t new_id_store(struct device_driver *drv, const char *buf,
		size_t count)
{
114
	return do_id_store(drv, buf, count, ID_ADD);
115 116 117 118 119 120
}
static DRIVER_ATTR_WO(new_id);

static ssize_t remove_id_store(struct device_driver *drv, const char *buf,
		size_t count)
{
121
	return do_id_store(drv, buf, count, ID_REMOVE);
122 123 124 125 126 127 128 129 130 131
}
static DRIVER_ATTR_WO(remove_id);

static struct attribute *dax_drv_attrs[] = {
	&driver_attr_new_id.attr,
	&driver_attr_remove_id.attr,
	NULL,
};
ATTRIBUTE_GROUPS(dax_drv);

132 133
static int dax_bus_match(struct device *dev, struct device_driver *drv);

134 135 136 137 138
static bool is_static(struct dax_region *dax_region)
{
	return (dax_region->res.flags & IORESOURCE_DAX_STATIC) != 0;
}

139 140 141 142 143 144 145 146 147 148 149 150 151
static u64 dev_dax_size(struct dev_dax *dev_dax)
{
	u64 size = 0;
	int i;

	device_lock_assert(&dev_dax->dev);

	for (i = 0; i < dev_dax->nr_range; i++)
		size += range_len(&dev_dax->ranges[i].range);

	return size;
}

152 153 154 155
static int dax_bus_probe(struct device *dev)
{
	struct dax_device_driver *dax_drv = to_dax_drv(dev->driver);
	struct dev_dax *dev_dax = to_dev_dax(dev);
156 157 158
	struct dax_region *dax_region = dev_dax->region;
	int rc;

159
	if (dev_dax_size(dev_dax) == 0 || dev_dax->id < 0)
160 161 162 163 164 165 166 167 168 169 170 171 172
		return -ENXIO;

	rc = dax_drv->probe(dev_dax);

	if (rc || is_static(dax_region))
		return rc;

	/*
	 * Track new seed creation only after successful probe of the
	 * previous seed.
	 */
	if (dax_region->seed == dev)
		dax_region->seed = NULL;
173

174
	return 0;
175 176 177 178 179 180 181 182 183 184
}

static int dax_bus_remove(struct device *dev)
{
	struct dax_device_driver *dax_drv = to_dax_drv(dev->driver);
	struct dev_dax *dev_dax = to_dev_dax(dev);

	return dax_drv->remove(dev_dax);
}

185 186 187 188
static struct bus_type dax_bus_type = {
	.name = "dax",
	.uevent = dax_bus_uevent,
	.match = dax_bus_match,
189 190
	.probe = dax_bus_probe,
	.remove = dax_bus_remove,
191
	.drv_groups = dax_drv_groups,
192 193 194 195
};

static int dax_bus_match(struct device *dev, struct device_driver *drv)
{
196 197
	struct dax_device_driver *dax_drv = to_dax_drv(drv);

198
	/*
199 200
	 * All but the 'device-dax' driver, which has 'match_always'
	 * set, requires an exact id match.
201
	 */
202 203 204 205
	if (dax_drv->match_always)
		return 1;

	return dax_match_id(dax_drv, dev);
206 207
}

208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
/*
 * Rely on the fact that drvdata is set before the attributes are
 * registered, and that the attributes are unregistered before drvdata
 * is cleared to assume that drvdata is always valid.
 */
static ssize_t id_show(struct device *dev,
		struct device_attribute *attr, char *buf)
{
	struct dax_region *dax_region = dev_get_drvdata(dev);

	return sprintf(buf, "%d\n", dax_region->id);
}
static DEVICE_ATTR_RO(id);

static ssize_t region_size_show(struct device *dev,
		struct device_attribute *attr, char *buf)
{
	struct dax_region *dax_region = dev_get_drvdata(dev);

	return sprintf(buf, "%llu\n", (unsigned long long)
			resource_size(&dax_region->res));
}
static struct device_attribute dev_attr_region_size = __ATTR(size, 0444,
		region_size_show, NULL);

233
static ssize_t region_align_show(struct device *dev,
234 235 236 237 238 239
		struct device_attribute *attr, char *buf)
{
	struct dax_region *dax_region = dev_get_drvdata(dev);

	return sprintf(buf, "%u\n", dax_region->align);
}
240 241
static struct device_attribute dev_attr_region_align =
		__ATTR(align, 0400, region_align_show, NULL);
242

243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
#define for_each_dax_region_resource(dax_region, res) \
	for (res = (dax_region)->res.child; res; res = res->sibling)

static unsigned long long dax_region_avail_size(struct dax_region *dax_region)
{
	resource_size_t size = resource_size(&dax_region->res);
	struct resource *res;

	device_lock_assert(dax_region->dev);

	for_each_dax_region_resource(dax_region, res)
		size -= resource_size(res);
	return size;
}

static ssize_t available_size_show(struct device *dev,
		struct device_attribute *attr, char *buf)
{
	struct dax_region *dax_region = dev_get_drvdata(dev);
	unsigned long long size;

	device_lock(dev);
	size = dax_region_avail_size(dax_region);
	device_unlock(dev);

	return sprintf(buf, "%llu\n", size);
}
static DEVICE_ATTR_RO(available_size);

272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369
static ssize_t seed_show(struct device *dev,
		struct device_attribute *attr, char *buf)
{
	struct dax_region *dax_region = dev_get_drvdata(dev);
	struct device *seed;
	ssize_t rc;

	if (is_static(dax_region))
		return -EINVAL;

	device_lock(dev);
	seed = dax_region->seed;
	rc = sprintf(buf, "%s\n", seed ? dev_name(seed) : "");
	device_unlock(dev);

	return rc;
}
static DEVICE_ATTR_RO(seed);

static ssize_t create_show(struct device *dev,
		struct device_attribute *attr, char *buf)
{
	struct dax_region *dax_region = dev_get_drvdata(dev);
	struct device *youngest;
	ssize_t rc;

	if (is_static(dax_region))
		return -EINVAL;

	device_lock(dev);
	youngest = dax_region->youngest;
	rc = sprintf(buf, "%s\n", youngest ? dev_name(youngest) : "");
	device_unlock(dev);

	return rc;
}

static ssize_t create_store(struct device *dev, struct device_attribute *attr,
		const char *buf, size_t len)
{
	struct dax_region *dax_region = dev_get_drvdata(dev);
	unsigned long long avail;
	ssize_t rc;
	int val;

	if (is_static(dax_region))
		return -EINVAL;

	rc = kstrtoint(buf, 0, &val);
	if (rc)
		return rc;
	if (val != 1)
		return -EINVAL;

	device_lock(dev);
	avail = dax_region_avail_size(dax_region);
	if (avail == 0)
		rc = -ENOSPC;
	else {
		struct dev_dax_data data = {
			.dax_region = dax_region,
			.size = 0,
			.id = -1,
		};
		struct dev_dax *dev_dax = devm_create_dev_dax(&data);

		if (IS_ERR(dev_dax))
			rc = PTR_ERR(dev_dax);
		else {
			/*
			 * In support of crafting multiple new devices
			 * simultaneously multiple seeds can be created,
			 * but only the first one that has not been
			 * successfully bound is tracked as the region
			 * seed.
			 */
			if (!dax_region->seed)
				dax_region->seed = &dev_dax->dev;
			dax_region->youngest = &dev_dax->dev;
			rc = len;
		}
	}
	device_unlock(dev);

	return rc;
}
static DEVICE_ATTR_RW(create);

void kill_dev_dax(struct dev_dax *dev_dax)
{
	struct dax_device *dax_dev = dev_dax->dax_dev;
	struct inode *inode = dax_inode(dax_dev);

	kill_dax(dax_dev);
	unmap_mapping_range(inode->i_mapping, 0, 0, 1);
}
EXPORT_SYMBOL_GPL(kill_dev_dax);

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 377 378
	for (i = 0; i < dev_dax->nr_range; i++) {
		struct range *range = &dev_dax->ranges[i].range;

379 380
		__release_region(&dax_region->res, range->start,
				range_len(range));
381 382
	}
	dev_dax->nr_range = 0;
383 384 385 386 387 388 389 390 391
}

static void unregister_dev_dax(void *dev)
{
	struct dev_dax *dev_dax = to_dev_dax(dev);

	dev_dbg(dev, "%s\n", __func__);

	kill_dev_dax(dev_dax);
392
	free_dev_dax_ranges(dev_dax);
393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442
	device_del(dev);
	put_device(dev);
}

/* a return value >= 0 indicates this invocation invalidated the id */
static int __free_dev_dax_id(struct dev_dax *dev_dax)
{
	struct dax_region *dax_region = dev_dax->region;
	struct device *dev = &dev_dax->dev;
	int rc = dev_dax->id;

	device_lock_assert(dev);

	if (is_static(dax_region) || dev_dax->id < 0)
		return -1;
	ida_free(&dax_region->ida, dev_dax->id);
	dev_dax->id = -1;
	return rc;
}

static int free_dev_dax_id(struct dev_dax *dev_dax)
{
	struct device *dev = &dev_dax->dev;
	int rc;

	device_lock(dev);
	rc = __free_dev_dax_id(dev_dax);
	device_unlock(dev);
	return rc;
}

static ssize_t delete_store(struct device *dev, struct device_attribute *attr,
		const char *buf, size_t len)
{
	struct dax_region *dax_region = dev_get_drvdata(dev);
	struct dev_dax *dev_dax;
	struct device *victim;
	bool do_del = false;
	int rc;

	if (is_static(dax_region))
		return -EINVAL;

	victim = device_find_child_by_name(dax_region->dev, buf);
	if (!victim)
		return -ENXIO;

	device_lock(dev);
	device_lock(victim);
	dev_dax = to_dev_dax(victim);
443
	if (victim->driver || dev_dax_size(dev_dax))
444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473
		rc = -EBUSY;
	else {
		/*
		 * Invalidate the device so it does not become active
		 * again, but always preserve device-id-0 so that
		 * /sys/bus/dax/ is guaranteed to be populated while any
		 * dax_region is registered.
		 */
		if (dev_dax->id > 0) {
			do_del = __free_dev_dax_id(dev_dax) >= 0;
			rc = len;
			if (dax_region->seed == victim)
				dax_region->seed = NULL;
			if (dax_region->youngest == victim)
				dax_region->youngest = NULL;
		} else
			rc = -EBUSY;
	}
	device_unlock(victim);

	/* won the race to invalidate the device, clean it up */
	if (do_del)
		devm_release_action(dev, unregister_dev_dax, victim);
	device_unlock(dev);
	put_device(victim);

	return rc;
}
static DEVICE_ATTR_WO(delete);

474 475 476 477 478 479
static umode_t dax_region_visible(struct kobject *kobj, struct attribute *a,
		int n)
{
	struct device *dev = container_of(kobj, struct device, kobj);
	struct dax_region *dax_region = dev_get_drvdata(dev);

480 481 482 483 484 485
	if (is_static(dax_region))
		if (a == &dev_attr_available_size.attr
				|| a == &dev_attr_create.attr
				|| a == &dev_attr_seed.attr
				|| a == &dev_attr_delete.attr)
			return 0;
486 487 488
	return a->mode;
}

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 494 495
	&dev_attr_create.attr,
	&dev_attr_seed.attr,
	&dev_attr_delete.attr,
496 497 498 499 500 501 502
	&dev_attr_id.attr,
	NULL,
};

static const struct attribute_group dax_region_attribute_group = {
	.name = "dax_region",
	.attrs = dax_region_attributes,
503
	.is_visible = dax_region_visible,
504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534
};

static const struct attribute_group *dax_region_attribute_groups[] = {
	&dax_region_attribute_group,
	NULL,
};

static void dax_region_free(struct kref *kref)
{
	struct dax_region *dax_region;

	dax_region = container_of(kref, struct dax_region, kref);
	kfree(dax_region);
}

void dax_region_put(struct dax_region *dax_region)
{
	kref_put(&dax_region->kref, dax_region_free);
}
EXPORT_SYMBOL_GPL(dax_region_put);

static void dax_region_unregister(void *region)
{
	struct dax_region *dax_region = region;

	sysfs_remove_groups(&dax_region->dev->kobj,
			dax_region_attribute_groups);
	dax_region_put(dax_region);
}

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 539 540 541 542 543 544 545 546 547 548 549
{
	struct dax_region *dax_region;

	/*
	 * The DAX core assumes that it can store its private data in
	 * parent->driver_data. This WARN is a reminder / safeguard for
	 * developers of device-dax drivers.
	 */
	if (dev_get_drvdata(parent)) {
		dev_WARN(parent, "dax core failed to setup private data\n");
		return NULL;
	}

550 551
	if (!IS_ALIGNED(range->start, align)
			|| !IS_ALIGNED(range_len(range), align))
552 553 554 555 556 557 558 559 560 561 562
		return NULL;

	dax_region = kzalloc(sizeof(*dax_region), GFP_KERNEL);
	if (!dax_region)
		return NULL;

	dev_set_drvdata(parent, dax_region);
	kref_init(&dax_region->kref);
	dax_region->id = region_id;
	dax_region->align = align;
	dax_region->dev = parent;
563
	dax_region->target_node = target_node;
564
	ida_init(&dax_region->ida);
565
	dax_region->res = (struct resource) {
566 567
		.start = range->start,
		.end = range->end,
568 569 570
		.flags = IORESOURCE_MEM | flags,
	};

571 572 573 574 575 576 577 578 579 580 581 582
	if (sysfs_create_groups(&parent->kobj, dax_region_attribute_groups)) {
		kfree(dax_region);
		return NULL;
	}

	kref_get(&dax_region->kref);
	if (devm_add_action_or_reset(parent, dax_region_unregister, dax_region))
		return NULL;
	return dax_region;
}
EXPORT_SYMBOL_GPL(alloc_dax_region);

583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743
static void dax_mapping_release(struct device *dev)
{
	struct dax_mapping *mapping = to_dax_mapping(dev);
	struct dev_dax *dev_dax = to_dev_dax(dev->parent);

	ida_free(&dev_dax->ida, mapping->id);
	kfree(mapping);
}

static void unregister_dax_mapping(void *data)
{
	struct device *dev = data;
	struct dax_mapping *mapping = to_dax_mapping(dev);
	struct dev_dax *dev_dax = to_dev_dax(dev->parent);
	struct dax_region *dax_region = dev_dax->region;

	dev_dbg(dev, "%s\n", __func__);

	device_lock_assert(dax_region->dev);

	dev_dax->ranges[mapping->range_id].mapping = NULL;
	mapping->range_id = -1;

	device_del(dev);
	put_device(dev);
}

static struct dev_dax_range *get_dax_range(struct device *dev)
{
	struct dax_mapping *mapping = to_dax_mapping(dev);
	struct dev_dax *dev_dax = to_dev_dax(dev->parent);
	struct dax_region *dax_region = dev_dax->region;

	device_lock(dax_region->dev);
	if (mapping->range_id < 0) {
		device_unlock(dax_region->dev);
		return NULL;
	}

	return &dev_dax->ranges[mapping->range_id];
}

static void put_dax_range(struct dev_dax_range *dax_range)
{
	struct dax_mapping *mapping = dax_range->mapping;
	struct dev_dax *dev_dax = to_dev_dax(mapping->dev.parent);
	struct dax_region *dax_region = dev_dax->region;

	device_unlock(dax_region->dev);
}

static ssize_t start_show(struct device *dev,
		struct device_attribute *attr, char *buf)
{
	struct dev_dax_range *dax_range;
	ssize_t rc;

	dax_range = get_dax_range(dev);
	if (!dax_range)
		return -ENXIO;
	rc = sprintf(buf, "%#llx\n", dax_range->range.start);
	put_dax_range(dax_range);

	return rc;
}
static DEVICE_ATTR(start, 0400, start_show, NULL);

static ssize_t end_show(struct device *dev,
		struct device_attribute *attr, char *buf)
{
	struct dev_dax_range *dax_range;
	ssize_t rc;

	dax_range = get_dax_range(dev);
	if (!dax_range)
		return -ENXIO;
	rc = sprintf(buf, "%#llx\n", dax_range->range.end);
	put_dax_range(dax_range);

	return rc;
}
static DEVICE_ATTR(end, 0400, end_show, NULL);

static ssize_t pgoff_show(struct device *dev,
		struct device_attribute *attr, char *buf)
{
	struct dev_dax_range *dax_range;
	ssize_t rc;

	dax_range = get_dax_range(dev);
	if (!dax_range)
		return -ENXIO;
	rc = sprintf(buf, "%#lx\n", dax_range->pgoff);
	put_dax_range(dax_range);

	return rc;
}
static DEVICE_ATTR(page_offset, 0400, pgoff_show, NULL);

static struct attribute *dax_mapping_attributes[] = {
	&dev_attr_start.attr,
	&dev_attr_end.attr,
	&dev_attr_page_offset.attr,
	NULL,
};

static const struct attribute_group dax_mapping_attribute_group = {
	.attrs = dax_mapping_attributes,
};

static const struct attribute_group *dax_mapping_attribute_groups[] = {
	&dax_mapping_attribute_group,
	NULL,
};

static struct device_type dax_mapping_type = {
	.release = dax_mapping_release,
	.groups = dax_mapping_attribute_groups,
};

static int devm_register_dax_mapping(struct dev_dax *dev_dax, int range_id)
{
	struct dax_region *dax_region = dev_dax->region;
	struct dax_mapping *mapping;
	struct device *dev;
	int rc;

	device_lock_assert(dax_region->dev);

	if (dev_WARN_ONCE(&dev_dax->dev, !dax_region->dev->driver,
				"region disabled\n"))
		return -ENXIO;

	mapping = kzalloc(sizeof(*mapping), GFP_KERNEL);
	if (!mapping)
		return -ENOMEM;
	mapping->range_id = range_id;
	mapping->id = ida_alloc(&dev_dax->ida, GFP_KERNEL);
	if (mapping->id < 0) {
		kfree(mapping);
		return -ENOMEM;
	}
	dev_dax->ranges[range_id].mapping = mapping;
	dev = &mapping->dev;
	device_initialize(dev);
	dev->parent = &dev_dax->dev;
	dev->type = &dax_mapping_type;
	dev_set_name(dev, "mapping%d", mapping->id);
	rc = device_add(dev);
	if (rc) {
		put_device(dev);
		return rc;
	}

	rc = devm_add_action_or_reset(dax_region->dev, unregister_dax_mapping,
			dev);
	if (rc)
		return rc;
	return 0;
}

D
Dan Williams 已提交
744 745
static int alloc_dev_dax_range(struct dev_dax *dev_dax, u64 start,
		resource_size_t size)
746 747 748 749
{
	struct dax_region *dax_region = dev_dax->region;
	struct resource *res = &dax_region->res;
	struct device *dev = &dev_dax->dev;
750 751
	struct dev_dax_range *ranges;
	unsigned long pgoff = 0;
752
	struct resource *alloc;
753
	int i, rc;
754 755 756

	device_lock_assert(dax_region->dev);

757 758
	/* handle the seed alloc special case */
	if (!size) {
759 760 761 762
		if (dev_WARN_ONCE(dev, dev_dax->nr_range,
					"0-size allocation must be first\n"))
			return -EBUSY;
		/* nr_range == 0 is elsewhere special cased as 0-size device */
763 764 765
		return 0;
	}

766 767 768 769 770
	ranges = krealloc(dev_dax->ranges, sizeof(*ranges)
			* (dev_dax->nr_range + 1), GFP_KERNEL);
	if (!ranges)
		return -ENOMEM;

D
Dan Williams 已提交
771
	alloc = __request_region(res, start, size, dev_name(dev), 0);
772 773 774 775 776 777 778 779 780 781
	if (!alloc) {
		/*
		 * If this was an empty set of ranges nothing else
		 * will release @ranges, so do it now.
		 */
		if (!dev_dax->nr_range) {
			kfree(ranges);
			ranges = NULL;
		}
		dev_dax->ranges = ranges;
782
		return -ENOMEM;
783
	}
784

785 786 787 788 789 790 791 792 793
	for (i = 0; i < dev_dax->nr_range; i++)
		pgoff += PHYS_PFN(range_len(&ranges[i].range));
	dev_dax->ranges = ranges;
	ranges[dev_dax->nr_range++] = (struct dev_dax_range) {
		.pgoff = pgoff,
		.range = {
			.start = alloc->start,
			.end = alloc->end,
		},
794 795
	};

796 797
	dev_dbg(dev, "alloc range[%d]: %pa:%pa\n", dev_dax->nr_range - 1,
			&alloc->start, &alloc->end);
798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813
	/*
	 * A dev_dax instance must be registered before mapping device
	 * children can be added. Defer to devm_create_dev_dax() to add
	 * the initial mapping device.
	 */
	if (!device_is_registered(&dev_dax->dev))
		return 0;

	rc = devm_register_dax_mapping(dev_dax, dev_dax->nr_range - 1);
	if (rc) {
		dev_dbg(dev, "delete range[%d]: %pa:%pa\n", dev_dax->nr_range - 1,
				&alloc->start, &alloc->end);
		dev_dax->nr_range--;
		__release_region(res, alloc->start, resource_size(alloc));
		return rc;
	}
814

815 816 817
	return 0;
}

D
Dan Williams 已提交
818 819
static int adjust_dev_dax_range(struct dev_dax *dev_dax, struct resource *res, resource_size_t size)
{
820 821
	int last_range = dev_dax->nr_range - 1;
	struct dev_dax_range *dax_range = &dev_dax->ranges[last_range];
D
Dan Williams 已提交
822
	struct dax_region *dax_region = dev_dax->region;
823 824 825 826
	bool is_shrink = resource_size(res) > size;
	struct range *range = &dax_range->range;
	struct device *dev = &dev_dax->dev;
	int rc;
D
Dan Williams 已提交
827 828 829

	device_lock_assert(dax_region->dev);

830 831 832 833
	if (dev_WARN_ONCE(dev, !size, "deletion is handled by dev_dax_shrink\n"))
		return -EINVAL;

	rc = adjust_resource(res, range->start, size);
D
Dan Williams 已提交
834 835 836
	if (rc)
		return rc;

837
	*range = (struct range) {
D
Dan Williams 已提交
838 839 840 841
		.start = range->start,
		.end = range->start + size - 1,
	};

842 843 844 845
	dev_dbg(dev, "%s range[%d]: %#llx:%#llx\n", is_shrink ? "shrink" : "extend",
			last_range, (unsigned long long) range->start,
			(unsigned long long) range->end);

D
Dan Williams 已提交
846 847 848
	return 0;
}

849 850 851 852
static ssize_t size_show(struct device *dev,
		struct device_attribute *attr, char *buf)
{
	struct dev_dax *dev_dax = to_dev_dax(dev);
853 854 855 856 857
	unsigned long long size;

	device_lock(dev);
	size = dev_dax_size(dev_dax);
	device_unlock(dev);
858 859 860

	return sprintf(buf, "%llu\n", size);
}
D
Dan Williams 已提交
861

862
static bool alloc_is_aligned(struct dev_dax *dev_dax, resource_size_t size)
D
Dan Williams 已提交
863 864 865 866 867
{
	/*
	 * The minimum mapping granularity for a device instance is a
	 * single subsection, unless the arch says otherwise.
	 */
868
	return IS_ALIGNED(size, max_t(unsigned long, dev_dax->align, memremap_compat_align()));
D
Dan Williams 已提交
869 870 871 872
}

static int dev_dax_shrink(struct dev_dax *dev_dax, resource_size_t size)
{
873
	resource_size_t to_shrink = dev_dax_size(dev_dax) - size;
D
Dan Williams 已提交
874 875
	struct dax_region *dax_region = dev_dax->region;
	struct device *dev = &dev_dax->dev;
876 877 878 879
	int i;

	for (i = dev_dax->nr_range - 1; i >= 0; i--) {
		struct range *range = &dev_dax->ranges[i].range;
880
		struct dax_mapping *mapping = dev_dax->ranges[i].mapping;
881 882 883 884 885
		struct resource *adjust = NULL, *res;
		resource_size_t shrink;

		shrink = min_t(u64, to_shrink, range_len(range));
		if (shrink >= range_len(range)) {
886 887
			devm_release_action(dax_region->dev,
					unregister_dax_mapping, &mapping->dev);
888 889 890 891 892 893 894 895 896 897
			__release_region(&dax_region->res, range->start,
					range_len(range));
			dev_dax->nr_range--;
			dev_dbg(dev, "delete range[%d]: %#llx:%#llx\n", i,
					(unsigned long long) range->start,
					(unsigned long long) range->end);
			to_shrink -= shrink;
			if (!to_shrink)
				break;
			continue;
D
Dan Williams 已提交
898 899
		}

900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939
		for_each_dax_region_resource(dax_region, res)
			if (strcmp(res->name, dev_name(dev)) == 0
					&& res->start == range->start) {
				adjust = res;
				break;
			}

		if (dev_WARN_ONCE(dev, !adjust || i != dev_dax->nr_range - 1,
					"failed to find matching resource\n"))
			return -ENXIO;
		return adjust_dev_dax_range(dev_dax, adjust, range_len(range)
				- shrink);
	}
	return 0;
}

/*
 * Only allow adjustments that preserve the relative pgoff of existing
 * allocations. I.e. the dev_dax->ranges array is ordered by increasing pgoff.
 */
static bool adjust_ok(struct dev_dax *dev_dax, struct resource *res)
{
	struct dev_dax_range *last;
	int i;

	if (dev_dax->nr_range == 0)
		return false;
	if (strcmp(res->name, dev_name(&dev_dax->dev)) != 0)
		return false;
	last = &dev_dax->ranges[dev_dax->nr_range - 1];
	if (last->range.start != res->start || last->range.end != res->end)
		return false;
	for (i = 0; i < dev_dax->nr_range - 1; i++) {
		struct dev_dax_range *dax_range = &dev_dax->ranges[i];

		if (dax_range->pgoff > last->pgoff)
			return false;
	}

	return true;
D
Dan Williams 已提交
940 941 942 943 944 945
}

static ssize_t dev_dax_resize(struct dax_region *dax_region,
		struct dev_dax *dev_dax, resource_size_t size)
{
	resource_size_t avail = dax_region_avail_size(dax_region), to_alloc;
946
	resource_size_t dev_size = dev_dax_size(dev_dax);
D
Dan Williams 已提交
947 948 949
	struct resource *region_res = &dax_region->res;
	struct device *dev = &dev_dax->dev;
	struct resource *res, *first;
950 951
	resource_size_t alloc = 0;
	int rc;
D
Dan Williams 已提交
952 953 954 955 956 957 958 959 960 961 962

	if (dev->driver)
		return -EBUSY;
	if (size == dev_size)
		return 0;
	if (size > dev_size && size - dev_size > avail)
		return -ENOSPC;
	if (size < dev_size)
		return dev_dax_shrink(dev_dax, size);

	to_alloc = size - dev_size;
963
	if (dev_WARN_ONCE(dev, !alloc_is_aligned(dev_dax, to_alloc),
D
Dan Williams 已提交
964 965 966 967 968 969 970 971
			"resize of %pa misaligned\n", &to_alloc))
		return -ENXIO;

	/*
	 * Expand the device into the unused portion of the region. This
	 * may involve adjusting the end of an existing resource, or
	 * allocating a new resource.
	 */
972
retry:
D
Dan Williams 已提交
973 974 975
	first = region_res->child;
	if (!first)
		return alloc_dev_dax_range(dev_dax, dax_region->res.start, to_alloc);
976 977 978

	rc = -ENOSPC;
	for (res = first; res; res = res->sibling) {
D
Dan Williams 已提交
979 980 981
		struct resource *next = res->sibling;

		/* space at the beginning of the region */
982 983 984 985 986
		if (res == first && res->start > dax_region->res.start) {
			alloc = min(res->start - dax_region->res.start, to_alloc);
			rc = alloc_dev_dax_range(dev_dax, dax_region->res.start, alloc);
			break;
		}
D
Dan Williams 已提交
987

988
		alloc = 0;
D
Dan Williams 已提交
989 990
		/* space between allocations */
		if (next && next->start > res->end + 1)
991
			alloc = min(next->start - (res->end + 1), to_alloc);
D
Dan Williams 已提交
992 993

		/* space at the end of the region */
994 995
		if (!alloc && !next && res->end < region_res->end)
			alloc = min(region_res->end - res->end, to_alloc);
D
Dan Williams 已提交
996

997 998 999 1000 1001 1002 1003 1004 1005
		if (!alloc)
			continue;

		if (adjust_ok(dev_dax, res)) {
			rc = adjust_dev_dax_range(dev_dax, res, resource_size(res) + alloc);
			break;
		}
		rc = alloc_dev_dax_range(dev_dax, res->end + 1, alloc);
		break;
D
Dan Williams 已提交
1006
	}
1007 1008 1009 1010 1011 1012
	if (rc)
		return rc;
	to_alloc -= alloc;
	if (to_alloc)
		goto retry;
	return 0;
D
Dan Williams 已提交
1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026
}

static ssize_t size_store(struct device *dev, struct device_attribute *attr,
		const char *buf, size_t len)
{
	ssize_t rc;
	unsigned long long val;
	struct dev_dax *dev_dax = to_dev_dax(dev);
	struct dax_region *dax_region = dev_dax->region;

	rc = kstrtoull(buf, 0, &val);
	if (rc)
		return rc;

1027
	if (!alloc_is_aligned(dev_dax, val)) {
D
Dan Williams 已提交
1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044
		dev_dbg(dev, "%s: size: %lld misaligned\n", __func__, val);
		return -EINVAL;
	}

	device_lock(dax_region->dev);
	if (!dax_region->dev->driver) {
		device_unlock(dax_region->dev);
		return -ENXIO;
	}
	device_lock(dev);
	rc = dev_dax_resize(dax_region, dev_dax, val);
	device_unlock(dev);
	device_unlock(dax_region->dev);

	return rc == 0 ? len : rc;
}
static DEVICE_ATTR_RW(size);
1045

1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106
static ssize_t range_parse(const char *opt, size_t len, struct range *range)
{
	unsigned long long addr = 0;
	char *start, *end, *str;
	ssize_t rc = EINVAL;

	str = kstrdup(opt, GFP_KERNEL);
	if (!str)
		return rc;

	end = str;
	start = strsep(&end, "-");
	if (!start || !end)
		goto err;

	rc = kstrtoull(start, 16, &addr);
	if (rc)
		goto err;
	range->start = addr;

	rc = kstrtoull(end, 16, &addr);
	if (rc)
		goto err;
	range->end = addr;

err:
	kfree(str);
	return rc;
}

static ssize_t mapping_store(struct device *dev, struct device_attribute *attr,
		const char *buf, size_t len)
{
	struct dev_dax *dev_dax = to_dev_dax(dev);
	struct dax_region *dax_region = dev_dax->region;
	size_t to_alloc;
	struct range r;
	ssize_t rc;

	rc = range_parse(buf, len, &r);
	if (rc)
		return rc;

	rc = -ENXIO;
	device_lock(dax_region->dev);
	if (!dax_region->dev->driver) {
		device_unlock(dax_region->dev);
		return rc;
	}
	device_lock(dev);

	to_alloc = range_len(&r);
	if (alloc_is_aligned(dev_dax, to_alloc))
		rc = alloc_dev_dax_range(dev_dax, r.start, to_alloc);
	device_unlock(dev);
	device_unlock(dax_region->dev);

	return rc == 0 ? len : rc;
}
static DEVICE_ATTR_WO(mapping);

1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178
static ssize_t align_show(struct device *dev,
		struct device_attribute *attr, char *buf)
{
	struct dev_dax *dev_dax = to_dev_dax(dev);

	return sprintf(buf, "%d\n", dev_dax->align);
}

static ssize_t dev_dax_validate_align(struct dev_dax *dev_dax)
{
	resource_size_t dev_size = dev_dax_size(dev_dax);
	struct device *dev = &dev_dax->dev;
	int i;

	if (dev_size > 0 && !alloc_is_aligned(dev_dax, dev_size)) {
		dev_dbg(dev, "%s: align %u invalid for size %pa\n",
			__func__, dev_dax->align, &dev_size);
		return -EINVAL;
	}

	for (i = 0; i < dev_dax->nr_range; i++) {
		size_t len = range_len(&dev_dax->ranges[i].range);

		if (!alloc_is_aligned(dev_dax, len)) {
			dev_dbg(dev, "%s: align %u invalid for range %d\n",
				__func__, dev_dax->align, i);
			return -EINVAL;
		}
	}

	return 0;
}

static ssize_t align_store(struct device *dev, struct device_attribute *attr,
		const char *buf, size_t len)
{
	struct dev_dax *dev_dax = to_dev_dax(dev);
	struct dax_region *dax_region = dev_dax->region;
	unsigned long val, align_save;
	ssize_t rc;

	rc = kstrtoul(buf, 0, &val);
	if (rc)
		return -ENXIO;

	if (!dax_align_valid(val))
		return -EINVAL;

	device_lock(dax_region->dev);
	if (!dax_region->dev->driver) {
		device_unlock(dax_region->dev);
		return -ENXIO;
	}

	device_lock(dev);
	if (dev->driver) {
		rc = -EBUSY;
		goto out_unlock;
	}

	align_save = dev_dax->align;
	dev_dax->align = val;
	rc = dev_dax_validate_align(dev_dax);
	if (rc)
		dev_dax->align = align_save;
out_unlock:
	device_unlock(dev);
	device_unlock(dax_region->dev);
	return rc == 0 ? len : rc;
}
static DEVICE_ATTR_RW(align);

1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194
static int dev_dax_target_node(struct dev_dax *dev_dax)
{
	struct dax_region *dax_region = dev_dax->region;

	return dax_region->target_node;
}

static ssize_t target_node_show(struct device *dev,
		struct device_attribute *attr, char *buf)
{
	struct dev_dax *dev_dax = to_dev_dax(dev);

	return sprintf(buf, "%d\n", dev_dax_target_node(dev_dax));
}
static DEVICE_ATTR_RO(target_node);

1195 1196 1197 1198
static ssize_t resource_show(struct device *dev,
		struct device_attribute *attr, char *buf)
{
	struct dev_dax *dev_dax = to_dev_dax(dev);
1199 1200 1201 1202 1203 1204 1205
	struct dax_region *dax_region = dev_dax->region;
	unsigned long long start;

	if (dev_dax->nr_range < 1)
		start = dax_region->res.start;
	else
		start = dev_dax->ranges[0].range.start;
1206

1207
	return sprintf(buf, "%#llx\n", start);
1208
}
1209
static DEVICE_ATTR(resource, 0400, resource_show, NULL);
1210

1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221
static ssize_t modalias_show(struct device *dev, struct device_attribute *attr,
		char *buf)
{
	/*
	 * We only ever expect to handle device-dax instances, i.e. the
	 * @type argument to MODULE_ALIAS_DAX_DEVICE() is always zero
	 */
	return sprintf(buf, DAX_DEVICE_MODALIAS_FMT "\n", 0);
}
static DEVICE_ATTR_RO(modalias);

1222 1223 1224 1225 1226 1227 1228
static ssize_t numa_node_show(struct device *dev,
		struct device_attribute *attr, char *buf)
{
	return sprintf(buf, "%d\n", dev_to_node(dev));
}
static DEVICE_ATTR_RO(numa_node);

1229 1230 1231 1232
static umode_t dev_dax_visible(struct kobject *kobj, struct attribute *a, int n)
{
	struct device *dev = container_of(kobj, struct device, kobj);
	struct dev_dax *dev_dax = to_dev_dax(dev);
D
Dan Williams 已提交
1233
	struct dax_region *dax_region = dev_dax->region;
1234 1235 1236

	if (a == &dev_attr_target_node.attr && dev_dax_target_node(dev_dax) < 0)
		return 0;
1237 1238
	if (a == &dev_attr_numa_node.attr && !IS_ENABLED(CONFIG_NUMA))
		return 0;
1239 1240
	if (a == &dev_attr_mapping.attr && is_static(dax_region))
		return 0;
1241 1242
	if ((a == &dev_attr_align.attr ||
	     a == &dev_attr_size.attr) && is_static(dax_region))
D
Dan Williams 已提交
1243
		return 0444;
1244 1245 1246
	return a->mode;
}

1247
static struct attribute *dev_dax_attributes[] = {
1248
	&dev_attr_modalias.attr,
1249
	&dev_attr_size.attr,
1250
	&dev_attr_mapping.attr,
1251
	&dev_attr_target_node.attr,
1252
	&dev_attr_align.attr,
1253
	&dev_attr_resource.attr,
1254
	&dev_attr_numa_node.attr,
1255 1256 1257 1258 1259
	NULL,
};

static const struct attribute_group dev_dax_attribute_group = {
	.attrs = dev_dax_attributes,
1260
	.is_visible = dev_dax_visible,
1261 1262
};

1263
static const struct attribute_group *dax_attribute_groups[] = {
1264 1265 1266 1267
	&dev_dax_attribute_group,
	NULL,
};

1268
static void dev_dax_release(struct device *dev)
1269 1270
{
	struct dev_dax *dev_dax = to_dev_dax(dev);
1271
	struct dax_region *dax_region = dev_dax->region;
1272 1273
	struct dax_device *dax_dev = dev_dax->dax_dev;

1274
	put_dax(dax_dev);
1275 1276
	free_dev_dax_id(dev_dax);
	dax_region_put(dax_region);
1277
	kfree(dev_dax->ranges);
1278
	kfree(dev_dax->pgmap);
1279 1280 1281
	kfree(dev_dax);
}

D
Dan Williams 已提交
1282 1283 1284 1285 1286
static const struct device_type dev_dax_type = {
	.release = dev_dax_release,
	.groups = dax_attribute_groups,
};

1287
struct dev_dax *devm_create_dev_dax(struct dev_dax_data *data)
1288
{
1289
	struct dax_region *dax_region = data->dax_region;
1290 1291 1292 1293 1294
	struct device *parent = dax_region->dev;
	struct dax_device *dax_dev;
	struct dev_dax *dev_dax;
	struct inode *inode;
	struct device *dev;
1295
	int rc;
1296 1297 1298 1299 1300

	dev_dax = kzalloc(sizeof(*dev_dax), GFP_KERNEL);
	if (!dev_dax)
		return ERR_PTR(-ENOMEM);

1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321
	if (is_static(dax_region)) {
		if (dev_WARN_ONCE(parent, data->id < 0,
				"dynamic id specified to static region\n")) {
			rc = -EINVAL;
			goto err_id;
		}

		dev_dax->id = data->id;
	} else {
		if (dev_WARN_ONCE(parent, data->id >= 0,
				"static id specified to dynamic region\n")) {
			rc = -EINVAL;
			goto err_id;
		}

		rc = ida_alloc(&dax_region->ida, GFP_KERNEL);
		if (rc < 0)
			goto err_id;
		dev_dax->id = rc;
	}

1322 1323 1324
	dev_dax->region = dax_region;
	dev = &dev_dax->dev;
	device_initialize(dev);
1325
	dev_set_name(dev, "dax%d.%d", dax_region->id, dev_dax->id);
1326

D
Dan Williams 已提交
1327
	rc = alloc_dev_dax_range(dev_dax, dax_region->res.start, data->size);
1328 1329 1330
	if (rc)
		goto err_range;

1331
	if (data->pgmap) {
1332 1333 1334
		dev_WARN_ONCE(parent, !is_static(dax_region),
			"custom dev_pagemap requires a static dax_region\n");

1335 1336
		dev_dax->pgmap = kmemdup(data->pgmap,
				sizeof(struct dev_pagemap), GFP_KERNEL);
1337 1338
		if (!dev_dax->pgmap) {
			rc = -ENOMEM;
1339
			goto err_pgmap;
1340
		}
1341
	}
1342

1343 1344 1345 1346
	/*
	 * No 'host' or dax_operations since there is no access to this
	 * device outside of mmap of the resulting character device.
	 */
P
Pankaj Gupta 已提交
1347
	dax_dev = alloc_dax(dev_dax, NULL, NULL, DAXDEV_F_SYNC);
1348 1349
	if (IS_ERR(dax_dev)) {
		rc = PTR_ERR(dax_dev);
1350
		goto err_alloc_dax;
1351
	}
1352 1353 1354 1355 1356

	/* a device_dax instance is dead while the driver is not attached */
	kill_dax(dax_dev);

	dev_dax->dax_dev = dax_dev;
1357
	dev_dax->target_node = dax_region->target_node;
1358
	dev_dax->align = dax_region->align;
1359
	ida_init(&dev_dax->ida);
1360 1361 1362 1363
	kref_get(&dax_region->kref);

	inode = dax_inode(dax_dev);
	dev->devt = inode->i_rdev;
1364
	if (data->subsys == DEV_DAX_BUS)
1365 1366 1367
		dev->bus = &dax_bus_type;
	else
		dev->class = dax_class;
1368
	dev->parent = parent;
D
Dan Williams 已提交
1369
	dev->type = &dev_dax_type;
1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381

	rc = device_add(dev);
	if (rc) {
		kill_dev_dax(dev_dax);
		put_device(dev);
		return ERR_PTR(rc);
	}

	rc = devm_add_action_or_reset(dax_region->dev, unregister_dev_dax, dev);
	if (rc)
		return ERR_PTR(rc);

1382 1383 1384 1385 1386 1387 1388
	/* register mapping device for the initial allocation range */
	if (dev_dax->nr_range && range_len(&dev_dax->ranges[0].range)) {
		rc = devm_register_dax_mapping(dev_dax, 0);
		if (rc)
			return ERR_PTR(rc);
	}

1389
	return dev_dax;
1390

1391 1392 1393
err_alloc_dax:
	kfree(dev_dax->pgmap);
err_pgmap:
1394
	free_dev_dax_ranges(dev_dax);
1395
err_range:
1396 1397
	free_dev_dax_id(dev_dax);
err_id:
1398 1399 1400 1401
	kfree(dev_dax);

	return ERR_PTR(rc);
}
1402
EXPORT_SYMBOL_GPL(devm_create_dev_dax);
1403

1404 1405 1406
static int match_always_count;

int __dax_driver_register(struct dax_device_driver *dax_drv,
1407 1408
		struct module *module, const char *mod_name)
{
1409 1410 1411 1412
	struct device_driver *drv = &dax_drv->drv;
	int rc = 0;

	INIT_LIST_HEAD(&dax_drv->ids);
1413 1414 1415 1416
	drv->owner = module;
	drv->name = mod_name;
	drv->mod_name = mod_name;
	drv->bus = &dax_bus_type;
1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428

	/* there can only be one default driver */
	mutex_lock(&dax_bus_lock);
	match_always_count += dax_drv->match_always;
	if (match_always_count > 1) {
		match_always_count--;
		WARN_ON(1);
		rc = -EINVAL;
	}
	mutex_unlock(&dax_bus_lock);
	if (rc)
		return rc;
1429 1430 1431 1432
	return driver_register(drv);
}
EXPORT_SYMBOL_GPL(__dax_driver_register);

1433 1434
void dax_driver_unregister(struct dax_device_driver *dax_drv)
{
1435
	struct device_driver *drv = &dax_drv->drv;
1436 1437 1438 1439 1440 1441 1442 1443 1444
	struct dax_id *dax_id, *_id;

	mutex_lock(&dax_bus_lock);
	match_always_count -= dax_drv->match_always;
	list_for_each_entry_safe(dax_id, _id, &dax_drv->ids, list) {
		list_del(&dax_id->list);
		kfree(dax_id);
	}
	mutex_unlock(&dax_bus_lock);
1445
	driver_unregister(drv);
1446 1447 1448
}
EXPORT_SYMBOL_GPL(dax_driver_unregister);

1449 1450
int __init dax_bus_init(void)
{
1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462
	int rc;

	if (IS_ENABLED(CONFIG_DEV_DAX_PMEM_COMPAT)) {
		dax_class = class_create(THIS_MODULE, "dax");
		if (IS_ERR(dax_class))
			return PTR_ERR(dax_class);
	}

	rc = bus_register(&dax_bus_type);
	if (rc)
		class_destroy(dax_class);
	return rc;
1463 1464 1465 1466 1467
}

void __exit dax_bus_exit(void)
{
	bus_unregister(&dax_bus_type);
1468
	class_destroy(dax_class);
1469
}