2 * Functions for working with device tree overlays
4 * Copyright (C) 2012 Pantelis Antoniou <panto@antoniou-consulting.com>
5 * Copyright (C) 2012 Texas Instruments Inc.
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * version 2 as published by the Free Software Foundation.
12 #define pr_fmt(fmt) "OF: overlay: " fmt
14 #include <linux/kernel.h>
15 #include <linux/module.h>
17 #include <linux/of_device.h>
18 #include <linux/string.h>
19 #include <linux/ctype.h>
20 #include <linux/errno.h>
21 #include <linux/slab.h>
22 #include <linux/err.h>
23 #include <linux/idr.h>
25 #include "of_private.h"
28 * struct target - info about current target node as recursing through overlay
29 * @np: node where current level of overlay will be applied
30 * @in_livetree: @np is a node in the live devicetree
32 * Used in the algorithm to create the portion of a changeset that describes
33 * an overlay fragment, which is a devicetree subtree. Initially @np is a node
34 * in the live devicetree where the overlay subtree is targeted to be grafted
35 * into. When recursing to the next level of the overlay subtree, the target
36 * also recurses to the next level of the live devicetree, as long as overlay
37 * subtree node also exists in the live devicetree. When a node in the overlay
38 * subtree does not exist at the same level in the live devicetree, target->np
39 * points to a newly allocated node, and all subsequent targets in the subtree
40 * will be newly allocated nodes.
43 struct device_node
*np
;
48 * struct fragment - info about fragment nodes in overlay expanded device tree
49 * @target: target of the overlay operation
50 * @overlay: pointer to the __overlay__ node
53 struct device_node
*target
;
54 struct device_node
*overlay
;
58 * struct overlay_changeset
59 * @ovcs_list: list on which we are located
60 * @overlay_tree: expanded device tree that contains the fragment nodes
61 * @count: count of fragment structures
62 * @fragments: fragment nodes in the overlay expanded device tree
63 * @symbols_fragment: last element of @fragments[] is the __symbols__ node
64 * @cset: changeset to apply fragments to live device tree
66 struct overlay_changeset
{
68 struct list_head ovcs_list
;
69 struct device_node
*overlay_tree
;
71 struct fragment
*fragments
;
72 bool symbols_fragment
;
73 struct of_changeset cset
;
76 /* flags are sticky - once set, do not reset */
77 static int devicetree_state_flags
;
78 #define DTSF_APPLY_FAIL 0x01
79 #define DTSF_REVERT_FAIL 0x02
82 * If a changeset apply or revert encounters an error, an attempt will
83 * be made to undo partial changes, but may fail. If the undo fails
84 * we do not know the state of the devicetree.
86 static int devicetree_corrupt(void)
88 return devicetree_state_flags
&
89 (DTSF_APPLY_FAIL
| DTSF_REVERT_FAIL
);
92 static int build_changeset_next_level(struct overlay_changeset
*ovcs
,
93 struct target
*target
, const struct device_node
*overlay_node
);
96 * of_resolve_phandles() finds the largest phandle in the live tree.
97 * of_overlay_apply() may add a larger phandle to the live tree.
98 * Do not allow race between two overlays being applied simultaneously:
99 * mutex_lock(&of_overlay_phandle_mutex)
100 * of_resolve_phandles()
102 * mutex_unlock(&of_overlay_phandle_mutex)
104 static DEFINE_MUTEX(of_overlay_phandle_mutex
);
106 void of_overlay_mutex_lock(void)
108 mutex_lock(&of_overlay_phandle_mutex
);
111 void of_overlay_mutex_unlock(void)
113 mutex_unlock(&of_overlay_phandle_mutex
);
117 static LIST_HEAD(ovcs_list
);
118 static DEFINE_IDR(ovcs_idr
);
120 static BLOCKING_NOTIFIER_HEAD(overlay_notify_chain
);
122 int of_overlay_notifier_register(struct notifier_block
*nb
)
124 return blocking_notifier_chain_register(&overlay_notify_chain
, nb
);
126 EXPORT_SYMBOL_GPL(of_overlay_notifier_register
);
128 int of_overlay_notifier_unregister(struct notifier_block
*nb
)
130 return blocking_notifier_chain_unregister(&overlay_notify_chain
, nb
);
132 EXPORT_SYMBOL_GPL(of_overlay_notifier_unregister
);
134 static char *of_overlay_action_name
[] = {
141 static int overlay_notify(struct overlay_changeset
*ovcs
,
142 enum of_overlay_notify_action action
)
144 struct of_overlay_notify_data nd
;
147 for (i
= 0; i
< ovcs
->count
; i
++) {
148 struct fragment
*fragment
= &ovcs
->fragments
[i
];
150 nd
.target
= fragment
->target
;
151 nd
.overlay
= fragment
->overlay
;
153 ret
= blocking_notifier_call_chain(&overlay_notify_chain
,
155 if (ret
== NOTIFY_OK
|| ret
== NOTIFY_STOP
)
158 ret
= notifier_to_errno(ret
);
159 pr_err("overlay changeset %s notifier error %d, target: %pOF\n",
160 of_overlay_action_name
[action
], ret
, nd
.target
);
169 * The values of properties in the "/__symbols__" node are paths in
170 * the ovcs->overlay_tree. When duplicating the properties, the paths
171 * need to be adjusted to be the correct path for the live device tree.
173 * The paths refer to a node in the subtree of a fragment node's "__overlay__"
174 * node, for example "/fragment@0/__overlay__/symbol_path_tail",
175 * where symbol_path_tail can be a single node or it may be a multi-node path.
177 * The duplicated property value will be modified by replacing the
178 * "/fragment_name/__overlay/" portion of the value with the target
179 * path from the fragment node.
181 static struct property
*dup_and_fixup_symbol_prop(
182 struct overlay_changeset
*ovcs
, const struct property
*prop
)
184 struct fragment
*fragment
;
185 struct property
*new_prop
;
186 struct device_node
*fragment_node
;
187 struct device_node
*overlay_node
;
189 const char *path_tail
;
190 const char *target_path
;
192 int overlay_name_len
;
199 if (strnlen(prop
->value
, prop
->length
) >= prop
->length
)
202 path_len
= strlen(path
);
206 fragment_node
= __of_find_node_by_path(ovcs
->overlay_tree
, path
+ 1);
207 overlay_node
= __of_find_node_by_path(fragment_node
, "__overlay__/");
208 of_node_put(fragment_node
);
209 of_node_put(overlay_node
);
211 for (k
= 0; k
< ovcs
->count
; k
++) {
212 fragment
= &ovcs
->fragments
[k
];
213 if (fragment
->overlay
== overlay_node
)
216 if (k
>= ovcs
->count
)
219 overlay_name_len
= snprintf(NULL
, 0, "%pOF", fragment
->overlay
);
221 if (overlay_name_len
> path_len
)
223 path_tail
= path
+ overlay_name_len
;
224 path_tail_len
= strlen(path_tail
);
226 target_path
= kasprintf(GFP_KERNEL
, "%pOF", fragment
->target
);
229 target_path_len
= strlen(target_path
);
231 new_prop
= kzalloc(sizeof(*new_prop
), GFP_KERNEL
);
233 goto err_free_target_path
;
235 new_prop
->name
= kstrdup(prop
->name
, GFP_KERNEL
);
236 new_prop
->length
= target_path_len
+ path_tail_len
+ 1;
237 new_prop
->value
= kzalloc(new_prop
->length
, GFP_KERNEL
);
238 if (!new_prop
->name
|| !new_prop
->value
)
239 goto err_free_new_prop
;
241 strcpy(new_prop
->value
, target_path
);
242 strcpy(new_prop
->value
+ target_path_len
, path_tail
);
244 of_property_set_flag(new_prop
, OF_DYNAMIC
);
249 kfree(new_prop
->name
);
250 kfree(new_prop
->value
);
252 err_free_target_path
:
259 * add_changeset_property() - add @overlay_prop to overlay changeset
260 * @ovcs: overlay changeset
261 * @target: where @overlay_prop will be placed
262 * @overlay_prop: property to add or update, from overlay tree
263 * @is_symbols_prop: 1 if @overlay_prop is from node "/__symbols__"
265 * If @overlay_prop does not already exist in live devicetree, add changeset
266 * entry to add @overlay_prop in @target, else add changeset entry to update
267 * value of @overlay_prop.
269 * @target may be either in the live devicetree or in a new subtree that
270 * is contained in the changeset.
272 * Some special properties are not added or updated (no error returned):
273 * "name", "phandle", "linux,phandle".
275 * Properties "#address-cells" and "#size-cells" are not updated if they
276 * are already in the live tree, but if present in the live tree, the values
277 * in the overlay must match the values in the live tree.
279 * Update of property in symbols node is not allowed.
281 * Returns 0 on success, -ENOMEM if memory allocation failure, or -EINVAL if
284 static int add_changeset_property(struct overlay_changeset
*ovcs
,
285 struct target
*target
, struct property
*overlay_prop
,
286 bool is_symbols_prop
)
288 struct property
*new_prop
= NULL
, *prop
;
291 if (target
->in_livetree
)
292 if (!of_prop_cmp(overlay_prop
->name
, "name") ||
293 !of_prop_cmp(overlay_prop
->name
, "phandle") ||
294 !of_prop_cmp(overlay_prop
->name
, "linux,phandle"))
297 if (target
->in_livetree
)
298 prop
= of_find_property(target
->np
, overlay_prop
->name
, NULL
);
303 if (!of_prop_cmp(prop
->name
, "#address-cells")) {
304 if (!of_prop_val_eq(prop
, overlay_prop
)) {
305 pr_err("ERROR: changing value of #address-cells is not allowed in %pOF\n",
311 } else if (!of_prop_cmp(prop
->name
, "#size-cells")) {
312 if (!of_prop_val_eq(prop
, overlay_prop
)) {
313 pr_err("ERROR: changing value of #size-cells is not allowed in %pOF\n",
321 if (is_symbols_prop
) {
324 new_prop
= dup_and_fixup_symbol_prop(ovcs
, overlay_prop
);
326 new_prop
= __of_prop_dup(overlay_prop
, GFP_KERNEL
);
333 if (!target
->in_livetree
) {
334 new_prop
->next
= target
->np
->deadprops
;
335 target
->np
->deadprops
= new_prop
;
337 ret
= of_changeset_add_property(&ovcs
->cset
, target
->np
,
340 ret
= of_changeset_update_property(&ovcs
->cset
, target
->np
,
344 if (!of_node_check_flag(target
->np
, OF_OVERLAY
))
345 pr_err("WARNING: memory leak will occur if overlay removed, property: %pOF/%s\n",
346 target
->np
, new_prop
->name
);
349 kfree(new_prop
->name
);
350 kfree(new_prop
->value
);
357 * add_changeset_node() - add @node (and children) to overlay changeset
358 * @ovcs: overlay changeset
359 * @target: where @node will be placed in live tree or changeset
360 * @node: node from within overlay device tree fragment
362 * If @node does not already exist in @target, add changeset entry
363 * to add @node in @target.
365 * If @node already exists in @target, and the existing node has
366 * a phandle, the overlay node is not allowed to have a phandle.
368 * If @node has child nodes, add the children recursively via
369 * build_changeset_next_level().
371 * NOTE: Multiple mods of created nodes not supported.
372 * If more than one fragment contains a node that does not already exist
373 * in the live tree, then for each fragment of_changeset_attach_node()
374 * will add a changeset entry to add the node. When the changeset is
375 * applied, __of_attach_node() will attach the node twice (once for
376 * each fragment). At this point the device tree will be corrupted.
378 * TODO: add integrity check to ensure that multiple fragments do not
379 * create the same node.
381 * Returns 0 on success, -ENOMEM if memory allocation failure, or -EINVAL if
384 static int add_changeset_node(struct overlay_changeset
*ovcs
,
385 struct target
*target
, struct device_node
*node
)
387 const char *node_kbasename
;
388 const __be32
*phandle
;
389 struct device_node
*tchild
;
390 struct target target_child
;
393 node_kbasename
= kbasename(node
->full_name
);
395 for_each_child_of_node(target
->np
, tchild
)
396 if (!of_node_cmp(node_kbasename
, kbasename(tchild
->full_name
)))
400 tchild
= __of_node_dup(NULL
, "%pOF/%s",
401 target
->np
, node_kbasename
);
405 tchild
->parent
= target
->np
;
406 tchild
->name
= __of_get_property(node
, "name", NULL
);
407 tchild
->type
= __of_get_property(node
, "device_type", NULL
);
410 tchild
->name
= "<NULL>";
412 tchild
->type
= "<NULL>";
414 /* ignore obsolete "linux,phandle" */
415 phandle
= __of_get_property(node
, "phandle", &size
);
416 if (phandle
&& (size
== 4))
417 tchild
->phandle
= be32_to_cpup(phandle
);
419 of_node_set_flag(tchild
, OF_OVERLAY
);
421 ret
= of_changeset_attach_node(&ovcs
->cset
, tchild
);
425 target_child
.np
= tchild
;
426 target_child
.in_livetree
= false;
428 ret
= build_changeset_next_level(ovcs
, &target_child
, node
);
433 if (node
->phandle
&& tchild
->phandle
) {
436 target_child
.np
= tchild
;
437 target_child
.in_livetree
= target
->in_livetree
;
438 ret
= build_changeset_next_level(ovcs
, &target_child
, node
);
446 * build_changeset_next_level() - add level of overlay changeset
447 * @ovcs: overlay changeset
448 * @target: where to place @overlay_node in live tree
449 * @overlay_node: node from within an overlay device tree fragment
451 * Add the properties (if any) and nodes (if any) from @overlay_node to the
452 * @ovcs->cset changeset. If an added node has child nodes, they will
453 * be added recursively.
455 * Do not allow symbols node to have any children.
457 * Returns 0 on success, -ENOMEM if memory allocation failure, or -EINVAL if
458 * invalid @overlay_node.
460 static int build_changeset_next_level(struct overlay_changeset
*ovcs
,
461 struct target
*target
, const struct device_node
*overlay_node
)
463 struct device_node
*child
;
464 struct property
*prop
;
467 for_each_property_of_node(overlay_node
, prop
) {
468 ret
= add_changeset_property(ovcs
, target
, prop
, 0);
470 pr_debug("Failed to apply prop @%pOF/%s, err=%d\n",
471 target
->np
, prop
->name
, ret
);
476 for_each_child_of_node(overlay_node
, child
) {
477 ret
= add_changeset_node(ovcs
, target
, child
);
479 pr_debug("Failed to apply node @%pOF/%pOFn, err=%d\n",
480 target
->np
, child
, ret
);
490 * Add the properties from __overlay__ node to the @ovcs->cset changeset.
492 static int build_changeset_symbols_node(struct overlay_changeset
*ovcs
,
493 struct target
*target
,
494 const struct device_node
*overlay_symbols_node
)
496 struct property
*prop
;
499 for_each_property_of_node(overlay_symbols_node
, prop
) {
500 ret
= add_changeset_property(ovcs
, target
, prop
, 1);
502 pr_debug("Failed to apply prop @%pOF/%s, err=%d\n",
503 target
->np
, prop
->name
, ret
);
512 * build_changeset() - populate overlay changeset in @ovcs from @ovcs->fragments
513 * @ovcs: Overlay changeset
515 * Create changeset @ovcs->cset to contain the nodes and properties of the
516 * overlay device tree fragments in @ovcs->fragments[]. If an error occurs,
517 * any portions of the changeset that were successfully created will remain
520 * Returns 0 on success, -ENOMEM if memory allocation failure, or -EINVAL if
521 * invalid overlay in @ovcs->fragments[].
523 static int build_changeset(struct overlay_changeset
*ovcs
)
525 struct fragment
*fragment
;
526 struct target target
;
527 int fragments_count
, i
, ret
;
530 * if there is a symbols fragment in ovcs->fragments[i] it is
531 * the final element in the array
533 if (ovcs
->symbols_fragment
)
534 fragments_count
= ovcs
->count
- 1;
536 fragments_count
= ovcs
->count
;
538 for (i
= 0; i
< fragments_count
; i
++) {
539 fragment
= &ovcs
->fragments
[i
];
541 target
.np
= fragment
->target
;
542 target
.in_livetree
= true;
543 ret
= build_changeset_next_level(ovcs
, &target
,
546 pr_debug("apply failed '%pOF'\n", fragment
->target
);
551 if (ovcs
->symbols_fragment
) {
552 fragment
= &ovcs
->fragments
[ovcs
->count
- 1];
554 target
.np
= fragment
->target
;
555 target
.in_livetree
= true;
556 ret
= build_changeset_symbols_node(ovcs
, &target
,
559 pr_debug("apply failed '%pOF'\n", fragment
->target
);
568 * Find the target node using a number of different strategies
569 * in order of preference:
571 * 1) "target" property containing the phandle of the target
572 * 2) "target-path" property containing the path of the target
574 static struct device_node
*find_target(struct device_node
*info_node
)
580 ret
= of_property_read_u32(info_node
, "target", &val
);
582 return of_find_node_by_phandle(val
);
584 ret
= of_property_read_string(info_node
, "target-path", &path
);
586 return of_find_node_by_path(path
);
588 pr_err("Failed to find target for node %p (%s)\n",
589 info_node
, info_node
->name
);
595 * init_overlay_changeset() - initialize overlay changeset from overlay tree
596 * @ovcs Overlay changeset to build
597 * @tree: Contains all the overlay fragments and overlay fixup nodes
599 * Initialize @ovcs. Populate @ovcs->fragments with node information from
600 * the top level of @tree. The relevant top level nodes are the fragment
601 * nodes and the __symbols__ node. Any other top level node will be ignored.
603 * Returns 0 on success, -ENOMEM if memory allocation failure, -EINVAL if error
604 * detected in @tree, or -ENOSPC if idr_alloc() error.
606 static int init_overlay_changeset(struct overlay_changeset
*ovcs
,
607 struct device_node
*tree
)
609 struct device_node
*node
, *overlay_node
;
610 struct fragment
*fragment
;
611 struct fragment
*fragments
;
615 * Warn for some issues. Can not return -EINVAL for these until
616 * of_unittest_apply_overlay() is fixed to pass these checks.
618 if (!of_node_check_flag(tree
, OF_DYNAMIC
))
619 pr_debug("%s() tree is not dynamic\n", __func__
);
621 if (!of_node_check_flag(tree
, OF_DETACHED
))
622 pr_debug("%s() tree is not detached\n", __func__
);
624 if (!of_node_is_root(tree
))
625 pr_debug("%s() tree is not root\n", __func__
);
627 ovcs
->overlay_tree
= tree
;
629 INIT_LIST_HEAD(&ovcs
->ovcs_list
);
631 of_changeset_init(&ovcs
->cset
);
633 id
= idr_alloc(&ovcs_idr
, ovcs
, 1, 0, GFP_KERNEL
);
640 for_each_child_of_node(tree
, node
) {
641 overlay_node
= of_get_child_by_name(node
, "__overlay__");
644 of_node_put(overlay_node
);
648 node
= of_get_child_by_name(tree
, "__symbols__");
654 fragments
= kcalloc(cnt
, sizeof(*fragments
), GFP_KERNEL
);
661 for_each_child_of_node(tree
, node
) {
662 overlay_node
= of_get_child_by_name(node
, "__overlay__");
666 fragment
= &fragments
[cnt
];
667 fragment
->overlay
= overlay_node
;
668 fragment
->target
= find_target(node
);
669 if (!fragment
->target
) {
670 of_node_put(fragment
->overlay
);
672 goto err_free_fragments
;
679 * if there is a symbols fragment in ovcs->fragments[i] it is
680 * the final element in the array
682 node
= of_get_child_by_name(tree
, "__symbols__");
684 ovcs
->symbols_fragment
= 1;
685 fragment
= &fragments
[cnt
];
686 fragment
->overlay
= node
;
687 fragment
->target
= of_find_node_by_path("/__symbols__");
689 if (!fragment
->target
) {
690 pr_err("symbols in overlay, but not in live tree\n");
692 goto err_free_fragments
;
700 goto err_free_fragments
;
705 ovcs
->fragments
= fragments
;
712 idr_remove(&ovcs_idr
, id
);
714 pr_err("%s() failed, ret = %d\n", __func__
, ret
);
719 static void free_overlay_changeset(struct overlay_changeset
*ovcs
)
723 if (ovcs
->cset
.entries
.next
)
724 of_changeset_destroy(&ovcs
->cset
);
727 idr_remove(&ovcs_idr
, ovcs
->id
);
729 for (i
= 0; i
< ovcs
->count
; i
++) {
730 of_node_put(ovcs
->fragments
[i
].target
);
731 of_node_put(ovcs
->fragments
[i
].overlay
);
733 kfree(ovcs
->fragments
);
739 * of_overlay_apply() - Create and apply an overlay changeset
740 * @tree: Expanded overlay device tree
741 * @ovcs_id: Pointer to overlay changeset id
743 * Creates and applies an overlay changeset.
745 * If an error occurs in a pre-apply notifier, then no changes are made
746 * to the device tree.
749 * A non-zero return value will not have created the changeset if error is from:
751 * - building the changeset
752 * - overlay changeset pre-apply notifier
754 * If an error is returned by an overlay changeset pre-apply notifier
755 * then no further overlay changeset pre-apply notifier will be called.
757 * A non-zero return value will have created the changeset if error is from:
758 * - overlay changeset entry notifier
759 * - overlay changeset post-apply notifier
761 * If an error is returned by an overlay changeset post-apply notifier
762 * then no further overlay changeset post-apply notifier will be called.
764 * If more than one notifier returns an error, then the last notifier
765 * error to occur is returned.
767 * If an error occurred while applying the overlay changeset, then an
768 * attempt is made to revert any changes that were made to the
769 * device tree. If there were any errors during the revert attempt
770 * then the state of the device tree can not be determined, and any
771 * following attempt to apply or remove an overlay changeset will be
774 * Returns 0 on success, or a negative error number. Overlay changeset
775 * id is returned to *ovcs_id.
778 int of_overlay_apply(struct device_node
*tree
, int *ovcs_id
)
780 struct overlay_changeset
*ovcs
;
781 int ret
= 0, ret_revert
, ret_tmp
;
785 if (devicetree_corrupt()) {
786 pr_err("devicetree state suspect, refuse to apply overlay\n");
791 ovcs
= kzalloc(sizeof(*ovcs
), GFP_KERNEL
);
797 of_overlay_mutex_lock();
798 mutex_lock(&of_mutex
);
800 ret
= of_resolve_phandles(tree
);
802 goto err_free_overlay_changeset
;
804 ret
= init_overlay_changeset(ovcs
, tree
);
806 goto err_free_overlay_changeset
;
808 ret
= overlay_notify(ovcs
, OF_OVERLAY_PRE_APPLY
);
810 pr_err("overlay changeset pre-apply notify error %d\n", ret
);
811 goto err_free_overlay_changeset
;
814 ret
= build_changeset(ovcs
);
816 goto err_free_overlay_changeset
;
819 ret
= __of_changeset_apply_entries(&ovcs
->cset
, &ret_revert
);
822 pr_debug("overlay changeset revert error %d\n",
824 devicetree_state_flags
|= DTSF_APPLY_FAIL
;
826 goto err_free_overlay_changeset
;
829 ret
= __of_changeset_apply_notify(&ovcs
->cset
);
831 pr_err("overlay changeset entry notify error %d\n", ret
);
832 /* notify failure is not fatal, continue */
834 list_add_tail(&ovcs
->ovcs_list
, &ovcs_list
);
837 ret_tmp
= overlay_notify(ovcs
, OF_OVERLAY_POST_APPLY
);
839 pr_err("overlay changeset post-apply notify error %d\n",
847 err_free_overlay_changeset
:
848 free_overlay_changeset(ovcs
);
851 mutex_unlock(&of_mutex
);
852 of_overlay_mutex_unlock();
855 pr_debug("%s() err=%d\n", __func__
, ret
);
859 EXPORT_SYMBOL_GPL(of_overlay_apply
);
864 * Returns 1 if @np is @tree or is contained in @tree, else 0
866 static int find_node(struct device_node
*tree
, struct device_node
*np
)
868 struct device_node
*child
;
873 for_each_child_of_node(tree
, child
) {
874 if (find_node(child
, np
)) {
884 * Is @remove_ce_node a child of, a parent of, or the same as any
885 * node in an overlay changeset more topmost than @remove_ovcs?
887 * Returns 1 if found, else 0
889 static int node_overlaps_later_cs(struct overlay_changeset
*remove_ovcs
,
890 struct device_node
*remove_ce_node
)
892 struct overlay_changeset
*ovcs
;
893 struct of_changeset_entry
*ce
;
895 list_for_each_entry_reverse(ovcs
, &ovcs_list
, ovcs_list
) {
896 if (ovcs
== remove_ovcs
)
899 list_for_each_entry(ce
, &ovcs
->cset
.entries
, node
) {
900 if (find_node(ce
->np
, remove_ce_node
)) {
901 pr_err("%s: #%d overlaps with #%d @%pOF\n",
902 __func__
, remove_ovcs
->id
, ovcs
->id
,
906 if (find_node(remove_ce_node
, ce
->np
)) {
907 pr_err("%s: #%d overlaps with #%d @%pOF\n",
908 __func__
, remove_ovcs
->id
, ovcs
->id
,
919 * We can safely remove the overlay only if it's the top-most one.
920 * Newly applied overlays are inserted at the tail of the overlay list,
921 * so a top most overlay is the one that is closest to the tail.
923 * The topmost check is done by exploiting this property. For each
924 * affected device node in the log list we check if this overlay is
925 * the one closest to the tail. If another overlay has affected this
926 * device node and is closest to the tail, then removal is not permited.
928 static int overlay_removal_is_ok(struct overlay_changeset
*remove_ovcs
)
930 struct of_changeset_entry
*remove_ce
;
932 list_for_each_entry(remove_ce
, &remove_ovcs
->cset
.entries
, node
) {
933 if (node_overlaps_later_cs(remove_ovcs
, remove_ce
->np
)) {
934 pr_err("overlay #%d is not topmost\n", remove_ovcs
->id
);
943 * of_overlay_remove() - Revert and free an overlay changeset
944 * @ovcs_id: Pointer to overlay changeset id
946 * Removes an overlay if it is permissible. @ovcs_id was previously returned
947 * by of_overlay_apply().
949 * If an error occurred while attempting to revert the overlay changeset,
950 * then an attempt is made to re-apply any changeset entry that was
951 * reverted. If an error occurs on re-apply then the state of the device
952 * tree can not be determined, and any following attempt to apply or remove
953 * an overlay changeset will be refused.
955 * A non-zero return value will not revert the changeset if error is from:
957 * - overlay changeset pre-remove notifier
958 * - overlay changeset entry revert
960 * If an error is returned by an overlay changeset pre-remove notifier
961 * then no further overlay changeset pre-remove notifier will be called.
963 * If more than one notifier returns an error, then the last notifier
964 * error to occur is returned.
966 * A non-zero return value will revert the changeset if error is from:
967 * - overlay changeset entry notifier
968 * - overlay changeset post-remove notifier
970 * If an error is returned by an overlay changeset post-remove notifier
971 * then no further overlay changeset post-remove notifier will be called.
973 * Returns 0 on success, or a negative error number. *ovcs_id is set to
974 * zero after reverting the changeset, even if a subsequent error occurs.
976 int of_overlay_remove(int *ovcs_id
)
978 struct overlay_changeset
*ovcs
;
979 int ret
, ret_apply
, ret_tmp
;
983 if (devicetree_corrupt()) {
984 pr_err("suspect devicetree state, refuse to remove overlay\n");
989 mutex_lock(&of_mutex
);
991 ovcs
= idr_find(&ovcs_idr
, *ovcs_id
);
994 pr_err("remove: Could not find overlay #%d\n", *ovcs_id
);
998 if (!overlay_removal_is_ok(ovcs
)) {
1003 ret
= overlay_notify(ovcs
, OF_OVERLAY_PRE_REMOVE
);
1005 pr_err("overlay changeset pre-remove notify error %d\n", ret
);
1009 list_del(&ovcs
->ovcs_list
);
1012 ret
= __of_changeset_revert_entries(&ovcs
->cset
, &ret_apply
);
1015 devicetree_state_flags
|= DTSF_REVERT_FAIL
;
1019 ret
= __of_changeset_revert_notify(&ovcs
->cset
);
1021 pr_err("overlay changeset entry notify error %d\n", ret
);
1022 /* notify failure is not fatal, continue */
1026 ret_tmp
= overlay_notify(ovcs
, OF_OVERLAY_POST_REMOVE
);
1028 pr_err("overlay changeset post-remove notify error %d\n",
1034 free_overlay_changeset(ovcs
);
1037 mutex_unlock(&of_mutex
);
1040 pr_debug("%s() err=%d\n", __func__
, ret
);
1044 EXPORT_SYMBOL_GPL(of_overlay_remove
);
1047 * of_overlay_remove_all() - Reverts and frees all overlay changesets
1049 * Removes all overlays from the system in the correct order.
1051 * Returns 0 on success, or a negative error number
1053 int of_overlay_remove_all(void)
1055 struct overlay_changeset
*ovcs
, *ovcs_n
;
1058 /* the tail of list is guaranteed to be safe to remove */
1059 list_for_each_entry_safe_reverse(ovcs
, ovcs_n
, &ovcs_list
, ovcs_list
) {
1060 ret
= of_overlay_remove(&ovcs
->id
);
1067 EXPORT_SYMBOL_GPL(of_overlay_remove_all
);