1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Functions for working with device tree overlays 4 * 5 * Copyright (C) 2012 Pantelis Antoniou <panto@antoniou-consulting.com> 6 * Copyright (C) 2012 Texas Instruments Inc. 7 */ 8 9 #define pr_fmt(fmt) "OF: overlay: " fmt 10 11 #include <linux/kernel.h> 12 #include <linux/module.h> 13 #include <linux/of.h> 14 #include <linux/of_device.h> 15 #include <linux/string.h> 16 #include <linux/ctype.h> 17 #include <linux/errno.h> 18 #include <linux/slab.h> 19 #include <linux/err.h> 20 #include <linux/idr.h> 21 22 #include "of_private.h" 23 24 /** 25 * struct fragment - info about fragment nodes in overlay expanded device tree 26 * @target: target of the overlay operation 27 * @overlay: pointer to the __overlay__ node 28 */ 29 struct fragment { 30 struct device_node *target; 31 struct device_node *overlay; 32 }; 33 34 /** 35 * struct overlay_changeset 36 * @ovcs_list: list on which we are located 37 * @overlay_tree: expanded device tree that contains the fragment nodes 38 * @count: count of fragment structures 39 * @fragments: fragment nodes in the overlay expanded device tree 40 * @symbols_fragment: last element of @fragments[] is the __symbols__ node 41 * @cset: changeset to apply fragments to live device tree 42 */ 43 struct overlay_changeset { 44 int id; 45 struct list_head ovcs_list; 46 struct device_node *overlay_tree; 47 int count; 48 struct fragment *fragments; 49 bool symbols_fragment; 50 struct of_changeset cset; 51 }; 52 53 /* flags are sticky - once set, do not reset */ 54 static int devicetree_state_flags; 55 #define DTSF_APPLY_FAIL 0x01 56 #define DTSF_REVERT_FAIL 0x02 57 58 /* 59 * If a changeset apply or revert encounters an error, an attempt will 60 * be made to undo partial changes, but may fail. If the undo fails 61 * we do not know the state of the devicetree. 62 */ 63 static int devicetree_corrupt(void) 64 { 65 return devicetree_state_flags & 66 (DTSF_APPLY_FAIL | DTSF_REVERT_FAIL); 67 } 68 69 static int build_changeset_next_level(struct overlay_changeset *ovcs, 70 struct device_node *target_node, 71 const struct device_node *overlay_node); 72 73 /* 74 * of_resolve_phandles() finds the largest phandle in the live tree. 75 * of_overlay_apply() may add a larger phandle to the live tree. 76 * Do not allow race between two overlays being applied simultaneously: 77 * mutex_lock(&of_overlay_phandle_mutex) 78 * of_resolve_phandles() 79 * of_overlay_apply() 80 * mutex_unlock(&of_overlay_phandle_mutex) 81 */ 82 static DEFINE_MUTEX(of_overlay_phandle_mutex); 83 84 void of_overlay_mutex_lock(void) 85 { 86 mutex_lock(&of_overlay_phandle_mutex); 87 } 88 89 void of_overlay_mutex_unlock(void) 90 { 91 mutex_unlock(&of_overlay_phandle_mutex); 92 } 93 94 95 static LIST_HEAD(ovcs_list); 96 static DEFINE_IDR(ovcs_idr); 97 98 static BLOCKING_NOTIFIER_HEAD(overlay_notify_chain); 99 100 int of_overlay_notifier_register(struct notifier_block *nb) 101 { 102 return blocking_notifier_chain_register(&overlay_notify_chain, nb); 103 } 104 EXPORT_SYMBOL_GPL(of_overlay_notifier_register); 105 106 int of_overlay_notifier_unregister(struct notifier_block *nb) 107 { 108 return blocking_notifier_chain_unregister(&overlay_notify_chain, nb); 109 } 110 EXPORT_SYMBOL_GPL(of_overlay_notifier_unregister); 111 112 static char *of_overlay_action_name[] = { 113 "pre-apply", 114 "post-apply", 115 "pre-remove", 116 "post-remove", 117 }; 118 119 static int overlay_notify(struct overlay_changeset *ovcs, 120 enum of_overlay_notify_action action) 121 { 122 struct of_overlay_notify_data nd; 123 int i, ret; 124 125 for (i = 0; i < ovcs->count; i++) { 126 struct fragment *fragment = &ovcs->fragments[i]; 127 128 nd.target = fragment->target; 129 nd.overlay = fragment->overlay; 130 131 ret = blocking_notifier_call_chain(&overlay_notify_chain, 132 action, &nd); 133 if (ret == NOTIFY_OK || ret == NOTIFY_STOP) 134 return 0; 135 if (ret) { 136 ret = notifier_to_errno(ret); 137 pr_err("overlay changeset %s notifier error %d, target: %pOF\n", 138 of_overlay_action_name[action], ret, nd.target); 139 return ret; 140 } 141 } 142 143 return 0; 144 } 145 146 /* 147 * The values of properties in the "/__symbols__" node are paths in 148 * the ovcs->overlay_tree. When duplicating the properties, the paths 149 * need to be adjusted to be the correct path for the live device tree. 150 * 151 * The paths refer to a node in the subtree of a fragment node's "__overlay__" 152 * node, for example "/fragment@0/__overlay__/symbol_path_tail", 153 * where symbol_path_tail can be a single node or it may be a multi-node path. 154 * 155 * The duplicated property value will be modified by replacing the 156 * "/fragment_name/__overlay/" portion of the value with the target 157 * path from the fragment node. 158 */ 159 static struct property *dup_and_fixup_symbol_prop( 160 struct overlay_changeset *ovcs, const struct property *prop) 161 { 162 struct fragment *fragment; 163 struct property *new_prop; 164 struct device_node *fragment_node; 165 struct device_node *overlay_node; 166 const char *path; 167 const char *path_tail; 168 const char *target_path; 169 int k; 170 int overlay_name_len; 171 int path_len; 172 int path_tail_len; 173 int target_path_len; 174 175 if (!prop->value) 176 return NULL; 177 if (strnlen(prop->value, prop->length) >= prop->length) 178 return NULL; 179 path = prop->value; 180 path_len = strlen(path); 181 182 if (path_len < 1) 183 return NULL; 184 fragment_node = __of_find_node_by_path(ovcs->overlay_tree, path + 1); 185 overlay_node = __of_find_node_by_path(fragment_node, "__overlay__/"); 186 of_node_put(fragment_node); 187 of_node_put(overlay_node); 188 189 for (k = 0; k < ovcs->count; k++) { 190 fragment = &ovcs->fragments[k]; 191 if (fragment->overlay == overlay_node) 192 break; 193 } 194 if (k >= ovcs->count) 195 return NULL; 196 197 overlay_name_len = snprintf(NULL, 0, "%pOF", fragment->overlay); 198 199 if (overlay_name_len > path_len) 200 return NULL; 201 path_tail = path + overlay_name_len; 202 path_tail_len = strlen(path_tail); 203 204 target_path = kasprintf(GFP_KERNEL, "%pOF", fragment->target); 205 if (!target_path) 206 return NULL; 207 target_path_len = strlen(target_path); 208 209 new_prop = kzalloc(sizeof(*new_prop), GFP_KERNEL); 210 if (!new_prop) 211 goto err_free_target_path; 212 213 new_prop->name = kstrdup(prop->name, GFP_KERNEL); 214 new_prop->length = target_path_len + path_tail_len + 1; 215 new_prop->value = kzalloc(new_prop->length, GFP_KERNEL); 216 if (!new_prop->name || !new_prop->value) 217 goto err_free_new_prop; 218 219 strcpy(new_prop->value, target_path); 220 strcpy(new_prop->value + target_path_len, path_tail); 221 222 of_property_set_flag(new_prop, OF_DYNAMIC); 223 224 return new_prop; 225 226 err_free_new_prop: 227 kfree(new_prop->name); 228 kfree(new_prop->value); 229 kfree(new_prop); 230 err_free_target_path: 231 kfree(target_path); 232 233 return NULL; 234 } 235 236 /** 237 * add_changeset_property() - add @overlay_prop to overlay changeset 238 * @ovcs: overlay changeset 239 * @target_node: where to place @overlay_prop in live tree 240 * @overlay_prop: property to add or update, from overlay tree 241 * @is_symbols_prop: 1 if @overlay_prop is from node "/__symbols__" 242 * 243 * If @overlay_prop does not already exist in @target_node, add changeset entry 244 * to add @overlay_prop in @target_node, else add changeset entry to update 245 * value of @overlay_prop. 246 * 247 * Some special properties are not updated (no error returned). 248 * 249 * Update of property in symbols node is not allowed. 250 * 251 * Returns 0 on success, -ENOMEM if memory allocation failure, or -EINVAL if 252 * invalid @overlay. 253 */ 254 static int add_changeset_property(struct overlay_changeset *ovcs, 255 struct device_node *target_node, 256 struct property *overlay_prop, 257 bool is_symbols_prop) 258 { 259 struct property *new_prop = NULL, *prop; 260 int ret = 0; 261 262 prop = of_find_property(target_node, overlay_prop->name, NULL); 263 264 if (!of_prop_cmp(overlay_prop->name, "name") || 265 !of_prop_cmp(overlay_prop->name, "phandle") || 266 !of_prop_cmp(overlay_prop->name, "linux,phandle")) 267 return 0; 268 269 if (is_symbols_prop) { 270 if (prop) 271 return -EINVAL; 272 new_prop = dup_and_fixup_symbol_prop(ovcs, overlay_prop); 273 } else { 274 new_prop = __of_prop_dup(overlay_prop, GFP_KERNEL); 275 } 276 277 if (!new_prop) 278 return -ENOMEM; 279 280 if (!prop) 281 ret = of_changeset_add_property(&ovcs->cset, target_node, 282 new_prop); 283 else 284 ret = of_changeset_update_property(&ovcs->cset, target_node, 285 new_prop); 286 287 if (ret) { 288 kfree(new_prop->name); 289 kfree(new_prop->value); 290 kfree(new_prop); 291 } 292 return ret; 293 } 294 295 /** 296 * add_changeset_node() - add @node (and children) to overlay changeset 297 * @ovcs: overlay changeset 298 * @target_node: where to place @node in live tree 299 * @node: node from within overlay device tree fragment 300 * 301 * If @node does not already exist in @target_node, add changeset entry 302 * to add @node in @target_node. 303 * 304 * If @node already exists in @target_node, and the existing node has 305 * a phandle, the overlay node is not allowed to have a phandle. 306 * 307 * If @node has child nodes, add the children recursively via 308 * build_changeset_next_level(). 309 * 310 * NOTE: Multiple mods of created nodes not supported. 311 * If more than one fragment contains a node that does not already exist 312 * in the live tree, then for each fragment of_changeset_attach_node() 313 * will add a changeset entry to add the node. When the changeset is 314 * applied, __of_attach_node() will attach the node twice (once for 315 * each fragment). At this point the device tree will be corrupted. 316 * 317 * TODO: add integrity check to ensure that multiple fragments do not 318 * create the same node. 319 * 320 * Returns 0 on success, -ENOMEM if memory allocation failure, or -EINVAL if 321 * invalid @overlay. 322 */ 323 static int add_changeset_node(struct overlay_changeset *ovcs, 324 struct device_node *target_node, struct device_node *node) 325 { 326 const char *node_kbasename; 327 struct device_node *tchild; 328 int ret = 0; 329 330 node_kbasename = kbasename(node->full_name); 331 332 for_each_child_of_node(target_node, tchild) 333 if (!of_node_cmp(node_kbasename, kbasename(tchild->full_name))) 334 break; 335 336 if (!tchild) { 337 tchild = __of_node_dup(node, "%pOF/%s", 338 target_node, node_kbasename); 339 if (!tchild) 340 return -ENOMEM; 341 342 tchild->parent = target_node; 343 344 ret = of_changeset_attach_node(&ovcs->cset, tchild); 345 if (ret) 346 return ret; 347 348 return build_changeset_next_level(ovcs, tchild, node); 349 } 350 351 if (node->phandle && tchild->phandle) 352 ret = -EINVAL; 353 else 354 ret = build_changeset_next_level(ovcs, tchild, node); 355 of_node_put(tchild); 356 357 return ret; 358 } 359 360 /** 361 * build_changeset_next_level() - add level of overlay changeset 362 * @ovcs: overlay changeset 363 * @target_node: where to place @overlay_node in live tree 364 * @overlay_node: node from within an overlay device tree fragment 365 * 366 * Add the properties (if any) and nodes (if any) from @overlay_node to the 367 * @ovcs->cset changeset. If an added node has child nodes, they will 368 * be added recursively. 369 * 370 * Do not allow symbols node to have any children. 371 * 372 * Returns 0 on success, -ENOMEM if memory allocation failure, or -EINVAL if 373 * invalid @overlay_node. 374 */ 375 static int build_changeset_next_level(struct overlay_changeset *ovcs, 376 struct device_node *target_node, 377 const struct device_node *overlay_node) 378 { 379 struct device_node *child; 380 struct property *prop; 381 int ret; 382 383 for_each_property_of_node(overlay_node, prop) { 384 ret = add_changeset_property(ovcs, target_node, prop, 0); 385 if (ret) { 386 pr_debug("Failed to apply prop @%pOF/%s, err=%d\n", 387 target_node, prop->name, ret); 388 return ret; 389 } 390 } 391 392 for_each_child_of_node(overlay_node, child) { 393 ret = add_changeset_node(ovcs, target_node, child); 394 if (ret) { 395 pr_debug("Failed to apply node @%pOF/%s, err=%d\n", 396 target_node, child->name, ret); 397 of_node_put(child); 398 return ret; 399 } 400 } 401 402 return 0; 403 } 404 405 /* 406 * Add the properties from __overlay__ node to the @ovcs->cset changeset. 407 */ 408 static int build_changeset_symbols_node(struct overlay_changeset *ovcs, 409 struct device_node *target_node, 410 const struct device_node *overlay_symbols_node) 411 { 412 struct property *prop; 413 int ret; 414 415 for_each_property_of_node(overlay_symbols_node, prop) { 416 ret = add_changeset_property(ovcs, target_node, prop, 1); 417 if (ret) { 418 pr_debug("Failed to apply prop @%pOF/%s, err=%d\n", 419 target_node, prop->name, ret); 420 return ret; 421 } 422 } 423 424 return 0; 425 } 426 427 /** 428 * build_changeset() - populate overlay changeset in @ovcs from @ovcs->fragments 429 * @ovcs: Overlay changeset 430 * 431 * Create changeset @ovcs->cset to contain the nodes and properties of the 432 * overlay device tree fragments in @ovcs->fragments[]. If an error occurs, 433 * any portions of the changeset that were successfully created will remain 434 * in @ovcs->cset. 435 * 436 * Returns 0 on success, -ENOMEM if memory allocation failure, or -EINVAL if 437 * invalid overlay in @ovcs->fragments[]. 438 */ 439 static int build_changeset(struct overlay_changeset *ovcs) 440 { 441 struct fragment *fragment; 442 int fragments_count, i, ret; 443 444 /* 445 * if there is a symbols fragment in ovcs->fragments[i] it is 446 * the final element in the array 447 */ 448 if (ovcs->symbols_fragment) 449 fragments_count = ovcs->count - 1; 450 else 451 fragments_count = ovcs->count; 452 453 for (i = 0; i < fragments_count; i++) { 454 fragment = &ovcs->fragments[i]; 455 456 ret = build_changeset_next_level(ovcs, fragment->target, 457 fragment->overlay); 458 if (ret) { 459 pr_debug("apply failed '%pOF'\n", fragment->target); 460 return ret; 461 } 462 } 463 464 if (ovcs->symbols_fragment) { 465 fragment = &ovcs->fragments[ovcs->count - 1]; 466 ret = build_changeset_symbols_node(ovcs, fragment->target, 467 fragment->overlay); 468 if (ret) { 469 pr_debug("apply failed '%pOF'\n", fragment->target); 470 return ret; 471 } 472 } 473 474 return 0; 475 } 476 477 /* 478 * Find the target node using a number of different strategies 479 * in order of preference: 480 * 481 * 1) "target" property containing the phandle of the target 482 * 2) "target-path" property containing the path of the target 483 */ 484 static struct device_node *find_target_node(struct device_node *info_node) 485 { 486 const char *path; 487 u32 val; 488 int ret; 489 490 ret = of_property_read_u32(info_node, "target", &val); 491 if (!ret) 492 return of_find_node_by_phandle(val); 493 494 ret = of_property_read_string(info_node, "target-path", &path); 495 if (!ret) 496 return of_find_node_by_path(path); 497 498 pr_err("Failed to find target for node %p (%s)\n", 499 info_node, info_node->name); 500 501 return NULL; 502 } 503 504 /** 505 * init_overlay_changeset() - initialize overlay changeset from overlay tree 506 * @ovcs Overlay changeset to build 507 * @tree: Contains all the overlay fragments and overlay fixup nodes 508 * 509 * Initialize @ovcs. Populate @ovcs->fragments with node information from 510 * the top level of @tree. The relevant top level nodes are the fragment 511 * nodes and the __symbols__ node. Any other top level node will be ignored. 512 * 513 * Returns 0 on success, -ENOMEM if memory allocation failure, -EINVAL if error 514 * detected in @tree, or -ENOSPC if idr_alloc() error. 515 */ 516 static int init_overlay_changeset(struct overlay_changeset *ovcs, 517 struct device_node *tree) 518 { 519 struct device_node *node, *overlay_node; 520 struct fragment *fragment; 521 struct fragment *fragments; 522 int cnt, id, ret; 523 524 /* 525 * Warn for some issues. Can not return -EINVAL for these until 526 * of_unittest_apply_overlay() is fixed to pass these checks. 527 */ 528 if (!of_node_check_flag(tree, OF_DYNAMIC)) 529 pr_debug("%s() tree is not dynamic\n", __func__); 530 531 if (!of_node_check_flag(tree, OF_DETACHED)) 532 pr_debug("%s() tree is not detached\n", __func__); 533 534 if (!of_node_is_root(tree)) 535 pr_debug("%s() tree is not root\n", __func__); 536 537 ovcs->overlay_tree = tree; 538 539 INIT_LIST_HEAD(&ovcs->ovcs_list); 540 541 of_changeset_init(&ovcs->cset); 542 543 id = idr_alloc(&ovcs_idr, ovcs, 1, 0, GFP_KERNEL); 544 if (id <= 0) 545 return id; 546 547 cnt = 0; 548 549 /* fragment nodes */ 550 for_each_child_of_node(tree, node) { 551 overlay_node = of_get_child_by_name(node, "__overlay__"); 552 if (overlay_node) { 553 cnt++; 554 of_node_put(overlay_node); 555 } 556 } 557 558 node = of_get_child_by_name(tree, "__symbols__"); 559 if (node) { 560 cnt++; 561 of_node_put(node); 562 } 563 564 fragments = kcalloc(cnt, sizeof(*fragments), GFP_KERNEL); 565 if (!fragments) { 566 ret = -ENOMEM; 567 goto err_free_idr; 568 } 569 570 cnt = 0; 571 for_each_child_of_node(tree, node) { 572 overlay_node = of_get_child_by_name(node, "__overlay__"); 573 if (!overlay_node) 574 continue; 575 576 fragment = &fragments[cnt]; 577 fragment->overlay = overlay_node; 578 fragment->target = find_target_node(node); 579 if (!fragment->target) { 580 of_node_put(fragment->overlay); 581 ret = -EINVAL; 582 goto err_free_fragments; 583 } 584 585 cnt++; 586 } 587 588 /* 589 * if there is a symbols fragment in ovcs->fragments[i] it is 590 * the final element in the array 591 */ 592 node = of_get_child_by_name(tree, "__symbols__"); 593 if (node) { 594 ovcs->symbols_fragment = 1; 595 fragment = &fragments[cnt]; 596 fragment->overlay = node; 597 fragment->target = of_find_node_by_path("/__symbols__"); 598 599 if (!fragment->target) { 600 pr_err("symbols in overlay, but not in live tree\n"); 601 ret = -EINVAL; 602 goto err_free_fragments; 603 } 604 605 cnt++; 606 } 607 608 if (!cnt) { 609 ret = -EINVAL; 610 goto err_free_fragments; 611 } 612 613 ovcs->id = id; 614 ovcs->count = cnt; 615 ovcs->fragments = fragments; 616 617 return 0; 618 619 err_free_fragments: 620 kfree(fragments); 621 err_free_idr: 622 idr_remove(&ovcs_idr, id); 623 624 pr_err("%s() failed, ret = %d\n", __func__, ret); 625 626 return ret; 627 } 628 629 static void free_overlay_changeset(struct overlay_changeset *ovcs) 630 { 631 int i; 632 633 if (ovcs->cset.entries.next) 634 of_changeset_destroy(&ovcs->cset); 635 636 if (ovcs->id) 637 idr_remove(&ovcs_idr, ovcs->id); 638 639 for (i = 0; i < ovcs->count; i++) { 640 of_node_put(ovcs->fragments[i].target); 641 of_node_put(ovcs->fragments[i].overlay); 642 } 643 kfree(ovcs->fragments); 644 645 kfree(ovcs); 646 } 647 648 /** 649 * of_overlay_apply() - Create and apply an overlay changeset 650 * @tree: Expanded overlay device tree 651 * @ovcs_id: Pointer to overlay changeset id 652 * 653 * Creates and applies an overlay changeset. 654 * 655 * If an error occurs in a pre-apply notifier, then no changes are made 656 * to the device tree. 657 * 658 659 * A non-zero return value will not have created the changeset if error is from: 660 * - parameter checks 661 * - building the changeset 662 * - overlay changeset pre-apply notifier 663 * 664 * If an error is returned by an overlay changeset pre-apply notifier 665 * then no further overlay changeset pre-apply notifier will be called. 666 * 667 * A non-zero return value will have created the changeset if error is from: 668 * - overlay changeset entry notifier 669 * - overlay changeset post-apply notifier 670 * 671 * If an error is returned by an overlay changeset post-apply notifier 672 * then no further overlay changeset post-apply notifier will be called. 673 * 674 * If more than one notifier returns an error, then the last notifier 675 * error to occur is returned. 676 * 677 * If an error occurred while applying the overlay changeset, then an 678 * attempt is made to revert any changes that were made to the 679 * device tree. If there were any errors during the revert attempt 680 * then the state of the device tree can not be determined, and any 681 * following attempt to apply or remove an overlay changeset will be 682 * refused. 683 * 684 * Returns 0 on success, or a negative error number. Overlay changeset 685 * id is returned to *ovcs_id. 686 */ 687 688 int of_overlay_apply(struct device_node *tree, int *ovcs_id) 689 { 690 struct overlay_changeset *ovcs; 691 int ret = 0, ret_revert, ret_tmp; 692 693 *ovcs_id = 0; 694 695 if (devicetree_corrupt()) { 696 pr_err("devicetree state suspect, refuse to apply overlay\n"); 697 ret = -EBUSY; 698 goto out; 699 } 700 701 ovcs = kzalloc(sizeof(*ovcs), GFP_KERNEL); 702 if (!ovcs) { 703 ret = -ENOMEM; 704 goto out; 705 } 706 707 of_overlay_mutex_lock(); 708 mutex_lock(&of_mutex); 709 710 ret = of_resolve_phandles(tree); 711 if (ret) 712 goto err_free_overlay_changeset; 713 714 ret = init_overlay_changeset(ovcs, tree); 715 if (ret) 716 goto err_free_overlay_changeset; 717 718 ret = overlay_notify(ovcs, OF_OVERLAY_PRE_APPLY); 719 if (ret) { 720 pr_err("overlay changeset pre-apply notify error %d\n", ret); 721 goto err_free_overlay_changeset; 722 } 723 724 ret = build_changeset(ovcs); 725 if (ret) 726 goto err_free_overlay_changeset; 727 728 ret_revert = 0; 729 ret = __of_changeset_apply_entries(&ovcs->cset, &ret_revert); 730 if (ret) { 731 if (ret_revert) { 732 pr_debug("overlay changeset revert error %d\n", 733 ret_revert); 734 devicetree_state_flags |= DTSF_APPLY_FAIL; 735 } 736 goto err_free_overlay_changeset; 737 } 738 739 ret = __of_changeset_apply_notify(&ovcs->cset); 740 if (ret) 741 pr_err("overlay changeset entry notify error %d\n", ret); 742 /* notify failure is not fatal, continue */ 743 744 list_add_tail(&ovcs->ovcs_list, &ovcs_list); 745 *ovcs_id = ovcs->id; 746 747 ret_tmp = overlay_notify(ovcs, OF_OVERLAY_POST_APPLY); 748 if (ret_tmp) { 749 pr_err("overlay changeset post-apply notify error %d\n", 750 ret_tmp); 751 if (!ret) 752 ret = ret_tmp; 753 } 754 755 goto out_unlock; 756 757 err_free_overlay_changeset: 758 free_overlay_changeset(ovcs); 759 760 out_unlock: 761 mutex_unlock(&of_mutex); 762 of_overlay_mutex_unlock(); 763 764 out: 765 pr_debug("%s() err=%d\n", __func__, ret); 766 767 return ret; 768 } 769 EXPORT_SYMBOL_GPL(of_overlay_apply); 770 771 /* 772 * Find @np in @tree. 773 * 774 * Returns 1 if @np is @tree or is contained in @tree, else 0 775 */ 776 static int find_node(struct device_node *tree, struct device_node *np) 777 { 778 struct device_node *child; 779 780 if (tree == np) 781 return 1; 782 783 for_each_child_of_node(tree, child) { 784 if (find_node(child, np)) { 785 of_node_put(child); 786 return 1; 787 } 788 } 789 790 return 0; 791 } 792 793 /* 794 * Is @remove_ce_node a child of, a parent of, or the same as any 795 * node in an overlay changeset more topmost than @remove_ovcs? 796 * 797 * Returns 1 if found, else 0 798 */ 799 static int node_overlaps_later_cs(struct overlay_changeset *remove_ovcs, 800 struct device_node *remove_ce_node) 801 { 802 struct overlay_changeset *ovcs; 803 struct of_changeset_entry *ce; 804 805 list_for_each_entry_reverse(ovcs, &ovcs_list, ovcs_list) { 806 if (ovcs == remove_ovcs) 807 break; 808 809 list_for_each_entry(ce, &ovcs->cset.entries, node) { 810 if (find_node(ce->np, remove_ce_node)) { 811 pr_err("%s: #%d overlaps with #%d @%pOF\n", 812 __func__, remove_ovcs->id, ovcs->id, 813 remove_ce_node); 814 return 1; 815 } 816 if (find_node(remove_ce_node, ce->np)) { 817 pr_err("%s: #%d overlaps with #%d @%pOF\n", 818 __func__, remove_ovcs->id, ovcs->id, 819 remove_ce_node); 820 return 1; 821 } 822 } 823 } 824 825 return 0; 826 } 827 828 /* 829 * We can safely remove the overlay only if it's the top-most one. 830 * Newly applied overlays are inserted at the tail of the overlay list, 831 * so a top most overlay is the one that is closest to the tail. 832 * 833 * The topmost check is done by exploiting this property. For each 834 * affected device node in the log list we check if this overlay is 835 * the one closest to the tail. If another overlay has affected this 836 * device node and is closest to the tail, then removal is not permited. 837 */ 838 static int overlay_removal_is_ok(struct overlay_changeset *remove_ovcs) 839 { 840 struct of_changeset_entry *remove_ce; 841 842 list_for_each_entry(remove_ce, &remove_ovcs->cset.entries, node) { 843 if (node_overlaps_later_cs(remove_ovcs, remove_ce->np)) { 844 pr_err("overlay #%d is not topmost\n", remove_ovcs->id); 845 return 0; 846 } 847 } 848 849 return 1; 850 } 851 852 /** 853 * of_overlay_remove() - Revert and free an overlay changeset 854 * @ovcs_id: Pointer to overlay changeset id 855 * 856 * Removes an overlay if it is permissible. @ovcs_id was previously returned 857 * by of_overlay_apply(). 858 * 859 * If an error occurred while attempting to revert the overlay changeset, 860 * then an attempt is made to re-apply any changeset entry that was 861 * reverted. If an error occurs on re-apply then the state of the device 862 * tree can not be determined, and any following attempt to apply or remove 863 * an overlay changeset will be refused. 864 * 865 * A non-zero return value will not revert the changeset if error is from: 866 * - parameter checks 867 * - overlay changeset pre-remove notifier 868 * - overlay changeset entry revert 869 * 870 * If an error is returned by an overlay changeset pre-remove notifier 871 * then no further overlay changeset pre-remove notifier will be called. 872 * 873 * If more than one notifier returns an error, then the last notifier 874 * error to occur is returned. 875 * 876 * A non-zero return value will revert the changeset if error is from: 877 * - overlay changeset entry notifier 878 * - overlay changeset post-remove notifier 879 * 880 * If an error is returned by an overlay changeset post-remove notifier 881 * then no further overlay changeset post-remove notifier will be called. 882 * 883 * Returns 0 on success, or a negative error number. *ovcs_id is set to 884 * zero after reverting the changeset, even if a subsequent error occurs. 885 */ 886 int of_overlay_remove(int *ovcs_id) 887 { 888 struct overlay_changeset *ovcs; 889 int ret, ret_apply, ret_tmp; 890 891 ret = 0; 892 893 if (devicetree_corrupt()) { 894 pr_err("suspect devicetree state, refuse to remove overlay\n"); 895 ret = -EBUSY; 896 goto out; 897 } 898 899 mutex_lock(&of_mutex); 900 901 ovcs = idr_find(&ovcs_idr, *ovcs_id); 902 if (!ovcs) { 903 ret = -ENODEV; 904 pr_err("remove: Could not find overlay #%d\n", *ovcs_id); 905 goto out_unlock; 906 } 907 908 if (!overlay_removal_is_ok(ovcs)) { 909 ret = -EBUSY; 910 goto out_unlock; 911 } 912 913 ret = overlay_notify(ovcs, OF_OVERLAY_PRE_REMOVE); 914 if (ret) { 915 pr_err("overlay changeset pre-remove notify error %d\n", ret); 916 goto out_unlock; 917 } 918 919 list_del(&ovcs->ovcs_list); 920 921 ret_apply = 0; 922 ret = __of_changeset_revert_entries(&ovcs->cset, &ret_apply); 923 if (ret) { 924 if (ret_apply) 925 devicetree_state_flags |= DTSF_REVERT_FAIL; 926 goto out_unlock; 927 } 928 929 ret = __of_changeset_revert_notify(&ovcs->cset); 930 if (ret) 931 pr_err("overlay changeset entry notify error %d\n", ret); 932 /* notify failure is not fatal, continue */ 933 934 *ovcs_id = 0; 935 936 ret_tmp = overlay_notify(ovcs, OF_OVERLAY_POST_REMOVE); 937 if (ret_tmp) { 938 pr_err("overlay changeset post-remove notify error %d\n", 939 ret_tmp); 940 if (!ret) 941 ret = ret_tmp; 942 } 943 944 free_overlay_changeset(ovcs); 945 946 out_unlock: 947 mutex_unlock(&of_mutex); 948 949 out: 950 pr_debug("%s() err=%d\n", __func__, ret); 951 952 return ret; 953 } 954 EXPORT_SYMBOL_GPL(of_overlay_remove); 955 956 /** 957 * of_overlay_remove_all() - Reverts and frees all overlay changesets 958 * 959 * Removes all overlays from the system in the correct order. 960 * 961 * Returns 0 on success, or a negative error number 962 */ 963 int of_overlay_remove_all(void) 964 { 965 struct overlay_changeset *ovcs, *ovcs_n; 966 int ret; 967 968 /* the tail of list is guaranteed to be safe to remove */ 969 list_for_each_entry_safe_reverse(ovcs, ovcs_n, &ovcs_list, ovcs_list) { 970 ret = of_overlay_remove(&ovcs->id); 971 if (ret) 972 return ret; 973 } 974 975 return 0; 976 } 977 EXPORT_SYMBOL_GPL(of_overlay_remove_all); 978