overlay.c 22.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
/*
 * Functions for working with device tree overlays
 *
 * Copyright (C) 2012 Pantelis Antoniou <panto@antoniou-consulting.com>
 * Copyright (C) 2012 Texas Instruments Inc.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * version 2 as published by the Free Software Foundation.
 */
11 12 13

#define pr_fmt(fmt)	"OF: overlay: " fmt

14 15 16 17 18 19 20 21 22
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/of.h>
#include <linux/of_device.h>
#include <linux/string.h>
#include <linux/ctype.h>
#include <linux/errno.h>
#include <linux/slab.h>
#include <linux/err.h>
M
Mark Brown 已提交
23
#include <linux/idr.h>
24 25 26 27

#include "of_private.h"

/**
28
 * struct fragment - info about fragment nodes in overlay expanded device tree
29
 * @target:	target of the overlay operation
30
 * @overlay:	pointer to the __overlay__ node
31
 */
32
struct fragment {
33 34
	struct device_node *target;
	struct device_node *overlay;
35
	bool is_symbols_node;
36 37 38
};

/**
39 40 41 42 43
 * struct overlay_changeset
 * @ovcs_list:	list on which we are located
 * @count:	count of @fragments structures
 * @fragments:	info about fragment nodes in overlay expanded device tree
 * @cset:	changeset to apply fragments to live device tree
44
 */
45
struct overlay_changeset {
46
	int id;
47
	struct list_head ovcs_list;
48
	int count;
49
	struct fragment *fragments;
50 51 52
	struct of_changeset cset;
};

53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
/* flags are sticky - once set, do not reset */
static int devicetree_state_flags;
#define DTSF_APPLY_FAIL		0x01
#define DTSF_REVERT_FAIL	0x02

/*
 * If a changeset apply or revert encounters an error, an attempt will
 * be made to undo partial changes, but may fail.  If the undo fails
 * we do not know the state of the devicetree.
 */
static int devicetree_corrupt(void)
{
	return devicetree_state_flags &
		(DTSF_APPLY_FAIL | DTSF_REVERT_FAIL);
}

69 70 71
static int build_changeset_next_level(struct overlay_changeset *ovcs,
		struct device_node *target_node,
		const struct device_node *overlay_node,
72
		bool is_symbols_node);
73

F
Frank Rowand 已提交
74 75 76
static LIST_HEAD(ovcs_list);
static DEFINE_IDR(ovcs_idr);

77
static BLOCKING_NOTIFIER_HEAD(overlay_notify_chain);
78 79 80

int of_overlay_notifier_register(struct notifier_block *nb)
{
81
	return blocking_notifier_chain_register(&overlay_notify_chain, nb);
82 83 84 85 86
}
EXPORT_SYMBOL_GPL(of_overlay_notifier_register);

int of_overlay_notifier_unregister(struct notifier_block *nb)
{
87
	return blocking_notifier_chain_unregister(&overlay_notify_chain, nb);
88 89 90
}
EXPORT_SYMBOL_GPL(of_overlay_notifier_unregister);

91 92 93 94 95 96 97
static char *of_overlay_action_name[] = {
	"pre-apply",
	"post-apply",
	"pre-remove",
	"post-remove",
};

98 99
static int overlay_notify(struct overlay_changeset *ovcs,
		enum of_overlay_notify_action action)
100 101 102 103
{
	struct of_overlay_notify_data nd;
	int i, ret;

104 105
	for (i = 0; i < ovcs->count; i++) {
		struct fragment *fragment = &ovcs->fragments[i];
106

107 108
		nd.target = fragment->target;
		nd.overlay = fragment->overlay;
109

110
		ret = blocking_notifier_call_chain(&overlay_notify_chain,
111
						   action, &nd);
112 113 114 115 116 117 118 119
		if (ret == NOTIFY_STOP)
			return 0;
		if (ret) {
			ret = notifier_to_errno(ret);
			pr_err("overlay changeset %s notifier error %d, target: %pOF\n",
			       of_overlay_action_name[action], ret, nd.target);
			return ret;
		}
120 121 122 123 124
	}

	return 0;
}

125 126 127 128 129 130 131 132 133 134 135 136
/*
 * The properties in the "/__symbols__" node are "symbols".
 *
 * The value of properties in the "/__symbols__" node is the path of a
 * node in the subtree of a fragment node's "__overlay__" node, for
 * example "/fragment@0/__overlay__/symbol_path_tail".  Symbol_path_tail
 * can be a single node or it may be a multi-node path.
 *
 * The duplicated property value will be modified by replacing the
 * "/fragment_name/__overlay/" portion of the value  with the target
 * path from the fragment node.
 */
137 138
static struct property *dup_and_fixup_symbol_prop(
		struct overlay_changeset *ovcs, const struct property *prop)
139
{
140
	struct fragment *fragment;
141 142
	struct property *new;
	const char *overlay_name;
143
	char *symbol_path_tail;
144 145 146
	char *symbol_path;
	const char *target_path;
	int k;
147
	int symbol_path_tail_len;
148 149 150 151 152 153 154 155 156 157 158
	int overlay_name_len;
	int target_path_len;

	if (!prop->value)
		return NULL;
	symbol_path = prop->value;

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

159 160 161
	for (k = 0; k < ovcs->count; k++) {
		fragment = &ovcs->fragments[k];
		overlay_name = fragment->overlay->full_name;
162 163 164 165 166
		overlay_name_len = strlen(overlay_name);
		if (!strncasecmp(symbol_path, overlay_name, overlay_name_len))
			break;
	}

167
	if (k >= ovcs->count)
168 169
		goto err_free;

170
	target_path = fragment->target->full_name;
171 172
	target_path_len = strlen(target_path);

173 174
	symbol_path_tail = symbol_path + overlay_name_len;
	symbol_path_tail_len = strlen(symbol_path_tail);
175 176

	new->name = kstrdup(prop->name, GFP_KERNEL);
177
	new->length = target_path_len + symbol_path_tail_len + 1;
178 179 180 181 182 183
	new->value = kzalloc(new->length, GFP_KERNEL);

	if (!new->name || !new->value)
		goto err_free;

	strcpy(new->value, target_path);
184
	strcpy(new->value + target_path_len, symbol_path_tail);
185 186 187 188 189 190 191 192 193 194 195 196

	of_property_set_flag(new, OF_DYNAMIC);

	return new;

 err_free:
	kfree(new->name);
	kfree(new->value);
	kfree(new);
	return NULL;
}

197 198 199 200 201 202 203 204 205 206 207
/**
 * add_changeset_property() - add @overlay_prop to overlay changeset
 * @ovcs:		overlay changeset
 * @target_node:	where to place @overlay_prop in live tree
 * @overlay_prop:	property to add or update, from overlay tree
 * is_symbols_node:	1 if @target_node is "/__symbols__"
 *
 * If @overlay_prop does not already exist in @target_node, add changeset entry
 * to add @overlay_prop in @target_node, else add changeset entry to update
 * value of @overlay_prop.
 *
208
 * Some special properties are not updated (no error returned).
209
 *
210
 * Update of property in symbols node is not allowed.
211 212 213
 *
 * Returns 0 on success, -ENOMEM if memory allocation failure, or -EINVAL if
 * invalid @overlay.
214
 */
215 216 217
static int add_changeset_property(struct overlay_changeset *ovcs,
		struct device_node *target_node,
		struct property *overlay_prop,
218
		bool is_symbols_node)
219
{
220
	struct property *new_prop = NULL, *prop;
221
	int ret = 0;
222

223
	prop = of_find_property(target_node, overlay_prop->name, NULL);
224

225 226 227
	if (!of_prop_cmp(overlay_prop->name, "name") ||
	    !of_prop_cmp(overlay_prop->name, "phandle") ||
	    !of_prop_cmp(overlay_prop->name, "linux,phandle"))
228 229
		return 0;

230
	if (is_symbols_node) {
231
		if (prop)
232
			return -EINVAL;
233
		new_prop = dup_and_fixup_symbol_prop(ovcs, overlay_prop);
234
	} else {
235
		new_prop = __of_prop_dup(overlay_prop, GFP_KERNEL);
236 237
	}

238
	if (!new_prop)
239 240
		return -ENOMEM;

241 242 243
	if (!prop)
		ret = of_changeset_add_property(&ovcs->cset, target_node,
						new_prop);
244
	else
245 246
		ret = of_changeset_update_property(&ovcs->cset, target_node,
						   new_prop);
247 248

	if (ret) {
249 250 251
		kfree(new_prop->name);
		kfree(new_prop->value);
		kfree(new_prop);
252 253
	}
	return ret;
254 255
}

256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
/**
 * add_changeset_node() - add @node (and children) to overlay changeset
 * @ovcs:		overlay changeset
 * @target_node:	where to place @node in live tree
 * @node:		node from within overlay device tree fragment
 *
 * If @node does not already exist in @target_node, add changeset entry
 * to add @node in @target_node.
 *
 * If @node already exists in @target_node, and the existing node has
 * a phandle, the overlay node is not allowed to have a phandle.
 *
 * If @node has child nodes, add the children recursively via
 * build_changeset_next_level().
 *
 * NOTE: Multiple mods of created nodes not supported.
272 273 274 275 276 277 278 279
 *       If more than one fragment contains a node that does not already exist
 *       in the live tree, then for each fragment of_changeset_attach_node()
 *       will add a changeset entry to add the node.  When the changeset is
 *       applied, __of_attach_node() will attach the node twice (once for
 *       each fragment).  At this point the device tree will be corrupted.
 *
 *       TODO: add integrity check to ensure that multiple fragments do not
 *             create the same node.
280 281 282 283 284 285
 *
 * Returns 0 on success, -ENOMEM if memory allocation failure, or -EINVAL if
 * invalid @overlay.
 */
static int add_changeset_node(struct overlay_changeset *ovcs,
		struct device_node *target_node, struct device_node *node)
286
{
287
	const char *node_kbasename;
288
	struct device_node *tchild;
289 290
	int ret = 0;

291 292
	node_kbasename = kbasename(node->full_name);
	if (!node_kbasename)
293 294
		return -ENOMEM;

295 296
	for_each_child_of_node(target_node, tchild)
		if (!of_node_cmp(node_kbasename, kbasename(tchild->full_name)))
297 298
			break;

F
Frank Rowand 已提交
299
	if (!tchild) {
300 301
		tchild = __of_node_dup(node, "%pOF/%s",
				       target_node, node_kbasename);
302 303 304
		if (!tchild)
			return -ENOMEM;

305
		tchild->parent = target_node;
306

307
		ret = of_changeset_attach_node(&ovcs->cset, tchild);
308 309 310
		if (ret)
			return ret;

F
Frank Rowand 已提交
311
		return build_changeset_next_level(ovcs, tchild, node, 0);
312 313
	}

F
Frank Rowand 已提交
314 315 316 317 318 319
	if (node->phandle)
		return -EINVAL;

	ret = build_changeset_next_level(ovcs, tchild, node, 0);
	of_node_put(tchild);

320 321 322
	return ret;
}

323 324 325 326 327 328
/**
 * build_changeset_next_level() - add level of overlay changeset
 * @ovcs:		overlay changeset
 * @target_node:	where to place @overlay_node in live tree
 * @overlay_node:	node from within an overlay device tree fragment
 * @is_symbols_node:	@overlay_node is node "/__symbols__"
329
 *
330 331 332
 * Add the properties (if any) and nodes (if any) from @overlay_node to the
 * @ovcs->cset changeset.  If an added node has child nodes, they will
 * be added recursively.
333 334
 *
 * Do not allow symbols node to have any children.
335 336 337
 *
 * Returns 0 on success, -ENOMEM if memory allocation failure, or -EINVAL if
 * invalid @overlay_node.
338
 */
339 340 341
static int build_changeset_next_level(struct overlay_changeset *ovcs,
		struct device_node *target_node,
		const struct device_node *overlay_node,
342
		bool is_symbols_node)
343 344 345 346 347
{
	struct device_node *child;
	struct property *prop;
	int ret;

348 349 350
	for_each_property_of_node(overlay_node, prop) {
		ret = add_changeset_property(ovcs, target_node, prop,
					     is_symbols_node);
351
		if (ret) {
352 353
			pr_debug("Failed to apply prop @%pOF/%s, err=%d\n",
				 target_node, prop->name, ret);
354 355 356 357
			return ret;
		}
	}

358 359 360
	if (is_symbols_node)
		return 0;

361 362
	for_each_child_of_node(overlay_node, child) {
		ret = add_changeset_node(ovcs, target_node, child);
363
		if (ret) {
364 365
			pr_debug("Failed to apply node @%pOF/%s, err=%d\n",
				 target_node, child->name, ret);
J
Julia Lawall 已提交
366
			of_node_put(child);
367 368 369 370 371 372 373 374
			return ret;
		}
	}

	return 0;
}

/**
375 376
 * build_changeset() - populate overlay changeset in @ovcs from @ovcs->fragments
 * @ovcs:	Overlay changeset
377
 *
378 379 380 381 382 383 384
 * Create changeset @ovcs->cset to contain the nodes and properties of the
 * overlay device tree fragments in @ovcs->fragments[].  If an error occurs,
 * any portions of the changeset that were successfully created will remain
 * in @ovcs->cset.
 *
 * Returns 0 on success, -ENOMEM if memory allocation failure, or -EINVAL if
 * invalid overlay in @ovcs->fragments[].
385
 */
386
static int build_changeset(struct overlay_changeset *ovcs)
387
{
388
	int i, ret;
389

390 391
	for (i = 0; i < ovcs->count; i++) {
		struct fragment *fragment = &ovcs->fragments[i];
392

393 394 395 396
		ret = build_changeset_next_level(ovcs, fragment->target,
					       fragment->overlay,
					       fragment->is_symbols_node);
		if (ret) {
397
			pr_debug("apply failed '%pOF'\n", fragment->target);
398
			return ret;
399 400 401 402 403 404 405 406
		}
	}

	return 0;
}

/*
 * Find the target node using a number of different strategies
407
 * in order of preference:
408
 *
409 410
 * 1) "target" property containing the phandle of the target
 * 2) "target-path" property containing the path of the target
411 412 413 414 415 416 417 418
 */
static struct device_node *find_target_node(struct device_node *info_node)
{
	const char *path;
	u32 val;
	int ret;

	ret = of_property_read_u32(info_node, "target", &val);
419
	if (!ret)
420 421 422
		return of_find_node_by_phandle(val);

	ret = of_property_read_string(info_node, "target-path", &path);
423
	if (!ret)
424 425
		return of_find_node_by_path(path);

426
	pr_err("Failed to find target for node %p (%s)\n",
427 428 429 430 431 432
		info_node, info_node->name);

	return NULL;
}

/**
433 434 435
 * init_overlay_changeset() - initialize overlay changeset from overlay tree
 * @ovcs	Overlay changeset to build
 * @tree:	Contains all the overlay fragments and overlay fixup nodes
436
 *
437 438 439
 * Initialize @ovcs.  Populate @ovcs->fragments with node information from
 * the top level of @tree.  The relevant top level nodes are the fragment
 * nodes and the __symbols__ node.  Any other top level node will be ignored.
440
 *
441
 * Returns 0 on success, -ENOMEM if memory allocation failure, -EINVAL if error
F
Frank Rowand 已提交
442
 * detected in @tree, or -ENOSPC if idr_alloc() error.
443
 */
444
static int init_overlay_changeset(struct overlay_changeset *ovcs,
445 446
		struct device_node *tree)
{
F
Frank Rowand 已提交
447
	struct device_node *node, *overlay_node;
448 449 450
	struct fragment *fragment;
	struct fragment *fragments;
	int cnt, ret;
451

452 453 454 455 456 457 458 459 460 461 462 463 464
	/*
	 * Warn for some issues.  Can not return -EINVAL for these until
	 * of_unittest_apply_overlay() is fixed to pass these checks.
	 */
	if (!of_node_check_flag(tree, OF_DYNAMIC))
		pr_debug("%s() tree is not dynamic\n", __func__);

	if (!of_node_check_flag(tree, OF_DETACHED))
		pr_debug("%s() tree is not detached\n", __func__);

	if (!of_node_is_root(tree))
		pr_debug("%s() tree is not root\n", __func__);

F
Frank Rowand 已提交
465 466 467 468 469 470 471 472
	INIT_LIST_HEAD(&ovcs->ovcs_list);

	of_changeset_init(&ovcs->cset);

	ovcs->id = idr_alloc(&ovcs_idr, ovcs, 1, 0, GFP_KERNEL);
	if (ovcs->id <= 0)
		return ovcs->id;

473 474
	cnt = 0;

F
Frank Rowand 已提交
475 476 477 478 479 480 481 482 483 484 485
	/* fragment nodes */
	for_each_child_of_node(tree, node) {
		overlay_node = of_get_child_by_name(node, "__overlay__");
		if (overlay_node) {
			cnt++;
			of_node_put(overlay_node);
		}
	}

	node = of_get_child_by_name(tree, "__symbols__");
	if (node) {
486
		cnt++;
F
Frank Rowand 已提交
487 488
		of_node_put(node);
	}
489

490
	fragments = kcalloc(cnt, sizeof(*fragments), GFP_KERNEL);
F
Frank Rowand 已提交
491 492 493 494
	if (!fragments) {
		ret = -ENOMEM;
		goto err_free_idr;
	}
495 496 497

	cnt = 0;
	for_each_child_of_node(tree, node) {
F
Frank Rowand 已提交
498 499 500 501 502 503 504 505 506 507 508 509
		fragment = &fragments[cnt];
		fragment->overlay = of_get_child_by_name(node, "__overlay__");
		if (fragment->overlay) {
			fragment->target = find_target_node(node);
			if (!fragment->target) {
				of_node_put(fragment->overlay);
				ret = -EINVAL;
				goto err_free_fragments;
			} else {
				cnt++;
			}
		}
510 511
	}

512 513
	node = of_get_child_by_name(tree, "__symbols__");
	if (node) {
514 515 516 517
		fragment = &fragments[cnt];
		fragment->overlay = node;
		fragment->target = of_find_node_by_path("/__symbols__");
		fragment->is_symbols_node = 1;
518

519
		if (!fragment->target) {
520
			pr_err("no symbols in root of device tree.\n");
F
Frank Rowand 已提交
521 522
			ret = -EINVAL;
			goto err_free_fragments;
523 524 525 526 527
		}

		cnt++;
	}

528
	if (!cnt) {
F
Frank Rowand 已提交
529 530
		ret = -EINVAL;
		goto err_free_fragments;
531 532
	}

533 534
	ovcs->count = cnt;
	ovcs->fragments = fragments;
535 536

	return 0;
F
Frank Rowand 已提交
537 538 539 540 541 542

err_free_fragments:
	kfree(fragments);
err_free_idr:
	idr_remove(&ovcs_idr, ovcs->id);

543 544
	pr_err("%s() failed, ret = %d\n", __func__, ret);

F
Frank Rowand 已提交
545
	return ret;
546 547
}

F
Frank Rowand 已提交
548
static void free_overlay_changeset(struct overlay_changeset *ovcs)
549 550 551
{
	int i;

F
Frank Rowand 已提交
552 553 554 555 556 557 558 559
	if (!ovcs->cset.entries.next)
		return;
	of_changeset_destroy(&ovcs->cset);

	if (ovcs->id)
		idr_remove(&ovcs_idr, ovcs->id);

	for (i = 0; i < ovcs->count; i++) {
560 561
		of_node_put(ovcs->fragments[i].target);
		of_node_put(ovcs->fragments[i].overlay);
562
	}
563
	kfree(ovcs->fragments);
564

F
Frank Rowand 已提交
565 566
	kfree(ovcs);
}
567 568

/**
569 570
 * of_overlay_apply() - Create and apply an overlay changeset
 * @tree:	Expanded overlay device tree
571 572 573
 * @ovcs_id:	Pointer to overlay changeset id
 *
 * Creates and applies an overlay changeset.
574
 *
575 576
 * If an error occurs in a pre-apply notifier, then no changes are made
 * to the device tree.
577
 *
578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605

 * A non-zero return value will not have created the changeset if error is from:
 *   - parameter checks
 *   - building the changeset
 *   - overlay changset pre-apply notifier
 *
 * If an error is returned by an overlay changeset pre-apply notifier
 * then no further overlay changeset pre-apply notifier will be called.
 *
 * A non-zero return value will have created the changeset if error is from:
 *   - overlay changeset entry notifier
 *   - overlay changset post-apply notifier
 *
 * If an error is returned by an overlay changeset post-apply notifier
 * then no further overlay changeset post-apply notifier will be called.
 *
 * If more than one notifier returns an error, then the last notifier
 * error to occur is returned.
 *
 * If an error occurred while applying the overlay changeset, then an
 * attempt is made to revert any changes that were made to the
 * device tree.  If there were any errors during the revert attempt
 * then the state of the device tree can not be determined, and any
 * following attempt to apply or remove an overlay changeset will be
 * refused.
 *
 * Returns 0 on success, or a negative error number.  Overlay changeset
 * id is returned to *ovcs_id.
606
 */
607 608

int of_overlay_apply(struct device_node *tree, int *ovcs_id)
609
{
610
	struct overlay_changeset *ovcs;
611 612 613 614 615 616 617 618 619
	int ret = 0, ret_revert, ret_tmp;

	*ovcs_id = 0;

	if (devicetree_corrupt()) {
		pr_err("devicetree state suspect, refuse to apply overlay\n");
		ret = -EBUSY;
		goto out;
	}
620

621
	ovcs = kzalloc(sizeof(*ovcs), GFP_KERNEL);
622 623 624 625
	if (!ovcs) {
		ret = -ENOMEM;
		goto out;
	}
626 627 628

	mutex_lock(&of_mutex);

629
	ret = init_overlay_changeset(ovcs, tree);
630
	if (ret)
F
Frank Rowand 已提交
631
		goto err_free_overlay_changeset;
632

633
	ret = overlay_notify(ovcs, OF_OVERLAY_PRE_APPLY);
634 635
	if (ret) {
		pr_err("overlay changeset pre-apply notify error %d\n", ret);
F
Frank Rowand 已提交
636
		goto err_free_overlay_changeset;
637 638
	}

639 640
	ret = build_changeset(ovcs);
	if (ret)
F
Frank Rowand 已提交
641
		goto err_free_overlay_changeset;
642

643 644 645 646 647 648 649 650
	ret_revert = 0;
	ret = __of_changeset_apply_entries(&ovcs->cset, &ret_revert);
	if (ret) {
		if (ret_revert) {
			pr_debug("overlay changeset revert error %d\n",
				 ret_revert);
			devicetree_state_flags |= DTSF_APPLY_FAIL;
		}
F
Frank Rowand 已提交
651
		goto err_free_overlay_changeset;
652 653 654 655 656 657 658
	} else {
		ret = __of_changeset_apply_notify(&ovcs->cset);
		if (ret)
			pr_err("overlay changeset entry notify error %d\n",
			       ret);
		/* fall through */
	}
659

660
	list_add_tail(&ovcs->ovcs_list, &ovcs_list);
661 662 663 664 665 666 667 668 669
	*ovcs_id = ovcs->id;

	ret_tmp = overlay_notify(ovcs, OF_OVERLAY_POST_APPLY);
	if (ret_tmp) {
		pr_err("overlay changeset post-apply notify error %d\n",
		       ret_tmp);
		if (!ret)
			ret = ret_tmp;
	}
670

671 672
	mutex_unlock(&of_mutex);

673
	goto out;
F
Frank Rowand 已提交
674 675 676

err_free_overlay_changeset:
	free_overlay_changeset(ovcs);
677 678 679

	mutex_unlock(&of_mutex);

680 681 682
out:
	pr_debug("%s() err=%d\n", __func__, ret);

683
	return ret;
684
}
685
EXPORT_SYMBOL_GPL(of_overlay_apply);
686

687
/*
688 689 690
 * Find @np in @tree.
 *
 * Returns 1 if @np is @tree or is contained in @tree, else 0
691
 */
692
static int find_node(struct device_node *tree, struct device_node *np)
693 694 695
{
	struct device_node *child;

696
	if (tree == np)
697 698 699
		return 1;

	for_each_child_of_node(tree, child) {
700
		if (find_node(child, np)) {
J
Julia Lawall 已提交
701
			of_node_put(child);
702
			return 1;
J
Julia Lawall 已提交
703
		}
704 705 706 707 708
	}

	return 0;
}

709
/*
710 711 712 713
 * Is @remove_ce_np a child of or the same as any
 * node in an overlay changeset more topmost than @remove_ovcs?
 *
 * Returns 1 if found, else 0
714
 */
715 716
static int node_in_later_cs(struct overlay_changeset *remove_ovcs,
		struct device_node *remove_ce_np)
717
{
718
	struct overlay_changeset *ovcs;
719 720
	struct of_changeset_entry *ce;

721 722
	list_for_each_entry_reverse(ovcs, &ovcs_list, ovcs_list) {
		if (ovcs == remove_ovcs)
723 724
			break;

725 726
		list_for_each_entry(ce, &ovcs->cset.entries, node) {
			if (find_node(ce->np, remove_ce_np)) {
727
				pr_err("%s: #%d clashes #%d @%pOF\n",
728 729 730
					__func__, remove_ovcs->id, ovcs->id,
					remove_ce_np);
				return 1;
731 732 733 734
			}
		}
	}

735
	return 0;
736 737 738 739 740 741 742 743 744 745 746 747
}

/*
 * We can safely remove the overlay only if it's the top-most one.
 * Newly applied overlays are inserted at the tail of the overlay list,
 * so a top most overlay is the one that is closest to the tail.
 *
 * The topmost check is done by exploiting this property. For each
 * affected device node in the log list we check if this overlay is
 * the one closest to the tail. If another overlay has affected this
 * device node and is closest to the tail, then removal is not permited.
 */
748
static int overlay_removal_is_ok(struct overlay_changeset *remove_ovcs)
749
{
750
	struct of_changeset_entry *remove_ce;
751

752 753 754
	list_for_each_entry(remove_ce, &remove_ovcs->cset.entries, node) {
		if (node_in_later_cs(remove_ovcs, remove_ce->np)) {
			pr_err("overlay #%d is not topmost\n", remove_ovcs->id);
755 756 757 758 759 760 761 762
			return 0;
		}
	}

	return 1;
}

/**
763
 * of_overlay_remove() - Revert and free an overlay changeset
764
 * @ovcs_id:	Pointer to overlay changeset id
765
 *
766
 * Removes an overlay if it is permissible.  @ovcs_id was previously returned
767
 * by of_overlay_apply().
768
 *
769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794
 * If an error occurred while attempting to revert the overlay changeset,
 * then an attempt is made to re-apply any changeset entry that was
 * reverted.  If an error occurs on re-apply then the state of the device
 * tree can not be determined, and any following attempt to apply or remove
 * an overlay changeset will be refused.
 *
 * A non-zero return value will not revert the changeset if error is from:
 *   - parameter checks
 *   - overlay changset pre-remove notifier
 *   - overlay changeset entry revert
 *
 * If an error is returned by an overlay changeset pre-remove notifier
 * then no further overlay changeset pre-remove notifier will be called.
 *
 * If more than one notifier returns an error, then the last notifier
 * error to occur is returned.
 *
 * A non-zero return value will revert the changeset if error is from:
 *   - overlay changeset entry notifier
 *   - overlay changset post-remove notifier
 *
 * If an error is returned by an overlay changeset post-remove notifier
 * then no further overlay changeset post-remove notifier will be called.
 *
 * Returns 0 on success, or a negative error number.  *ovcs_id is set to
 * zero after reverting the changeset, even if a subsequent error occurs.
795
 */
796
int of_overlay_remove(int *ovcs_id)
797
{
798
	struct overlay_changeset *ovcs;
799 800 801 802 803 804 805 806 807
	int ret, ret_apply, ret_tmp;

	ret = 0;

	if (devicetree_corrupt()) {
		pr_err("suspect devicetree state, refuse to remove overlay\n");
		ret = -EBUSY;
		goto out;
	}
808 809 810

	mutex_lock(&of_mutex);

811
	ovcs = idr_find(&ovcs_idr, *ovcs_id);
812 813
	if (!ovcs) {
		ret = -ENODEV;
814 815
		pr_err("remove: Could not find overlay #%d\n", *ovcs_id);
		goto out_unlock;
816 817
	}

818 819
	if (!overlay_removal_is_ok(ovcs)) {
		ret = -EBUSY;
820
		goto out_unlock;
821 822
	}

823 824 825 826 827
	ret = overlay_notify(ovcs, OF_OVERLAY_PRE_REMOVE);
	if (ret) {
		pr_err("overlay changeset pre-remove notify error %d\n", ret);
		goto out_unlock;
	}
F
Frank Rowand 已提交
828

829
	list_del(&ovcs->ovcs_list);
F
Frank Rowand 已提交
830

831 832 833 834 835 836 837 838 839 840 841 842 843 844
	ret_apply = 0;
	ret = __of_changeset_revert_entries(&ovcs->cset, &ret_apply);
	if (ret) {
		if (ret_apply)
			devicetree_state_flags |= DTSF_REVERT_FAIL;
		goto out_unlock;
	} else {
		ret = __of_changeset_revert_notify(&ovcs->cset);
		if (ret) {
			pr_err("overlay changeset entry notify error %d\n",
			       ret);
			/* fall through - changeset was reverted */
		}
	}
F
Frank Rowand 已提交
845

846 847 848 849 850 851 852 853 854
	*ovcs_id = 0;

	ret_tmp = overlay_notify(ovcs, OF_OVERLAY_POST_REMOVE);
	if (ret_tmp) {
		pr_err("overlay changeset post-remove notify error %d\n",
		       ret_tmp);
		if (!ret)
			ret = ret_tmp;
	}
F
Frank Rowand 已提交
855 856

	free_overlay_changeset(ovcs);
857

858
out_unlock:
859 860
	mutex_unlock(&of_mutex);

861 862 863
out:
	pr_debug("%s() err=%d\n", __func__, ret);

864
	return ret;
865
}
866
EXPORT_SYMBOL_GPL(of_overlay_remove);
867 868

/**
869
 * of_overlay_remove_all() - Reverts and frees all overlay changesets
870 871 872
 *
 * Removes all overlays from the system in the correct order.
 *
873
 * Returns 0 on success, or a negative error number
874
 */
875
int of_overlay_remove_all(void)
876
{
877
	struct overlay_changeset *ovcs, *ovcs_n;
F
Frank Rowand 已提交
878
	int ret;
879 880

	/* the tail of list is guaranteed to be safe to remove */
881
	list_for_each_entry_safe_reverse(ovcs, ovcs_n, &ovcs_list, ovcs_list) {
882
		ret = of_overlay_remove(&ovcs->id);
F
Frank Rowand 已提交
883 884
		if (ret)
			return ret;
885 886 887 888
	}

	return 0;
}
889
EXPORT_SYMBOL_GPL(of_overlay_remove_all);