1 // SPDX-License-Identifier: GPL-2.0
3 * Functions for working with device tree overlays
5 * Copyright (C) 2012 Pantelis Antoniou <panto@antoniou-consulting.com>
6 * Copyright (C) 2012 Texas Instruments Inc.
9 #define pr_fmt(fmt) "OF: overlay: " fmt
11 #include <linux/kernel.h>
12 #include <linux/module.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>
22 #include "of_private.h"
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
30 struct device_node
*target
;
31 struct device_node
*overlay
;
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
43 struct overlay_changeset
{
45 struct list_head ovcs_list
;
46 struct device_node
*overlay_tree
;
48 struct fragment
*fragments
;
49 bool symbols_fragment
;
50 struct of_changeset cset
;
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
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.
63 static int devicetree_corrupt(void)
65 return devicetree_state_flags
&
66 (DTSF_APPLY_FAIL
| DTSF_REVERT_FAIL
);
69 static int build_changeset_next_level(struct overlay_changeset
*ovcs
,
70 struct device_node
*target_node
,
71 const struct device_node
*overlay_node
);
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()
80 * mutex_unlock(&of_overlay_phandle_mutex)
82 static DEFINE_MUTEX(of_overlay_phandle_mutex
);
84 void of_overlay_mutex_lock(void)
86 mutex_lock(&of_overlay_phandle_mutex
);
89 void of_overlay_mutex_unlock(void)
91 mutex_unlock(&of_overlay_phandle_mutex
);
95 static LIST_HEAD(ovcs_list
);
96 static DEFINE_IDR(ovcs_idr
);
98 static BLOCKING_NOTIFIER_HEAD(overlay_notify_chain
);
100 int of_overlay_notifier_register(struct notifier_block
*nb
)
102 return blocking_notifier_chain_register(&overlay_notify_chain
, nb
);
104 EXPORT_SYMBOL_GPL(of_overlay_notifier_register
);
106 int of_overlay_notifier_unregister(struct notifier_block
*nb
)
108 return blocking_notifier_chain_unregister(&overlay_notify_chain
, nb
);
110 EXPORT_SYMBOL_GPL(of_overlay_notifier_unregister
);
112 static char *of_overlay_action_name
[] = {
119 static int overlay_notify(struct overlay_changeset
*ovcs
,
120 enum of_overlay_notify_action action
)
122 struct of_overlay_notify_data nd
;
125 for (i
= 0; i
< ovcs
->count
; i
++) {
126 struct fragment
*fragment
= &ovcs
->fragments
[i
];
128 nd
.target
= fragment
->target
;
129 nd
.overlay
= fragment
->overlay
;
131 ret
= blocking_notifier_call_chain(&overlay_notify_chain
,
133 if (ret
== NOTIFY_OK
|| ret
== NOTIFY_STOP
)
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
);
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.
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.
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.
159 static struct property
*dup_and_fixup_symbol_prop(
160 struct overlay_changeset
*ovcs
, const struct property
*prop
)
162 struct fragment
*fragment
;
163 struct property
*new_prop
;
164 struct device_node
*fragment_node
;
165 struct device_node
*overlay_node
;
167 const char *path_tail
;
168 const char *target_path
;
170 int overlay_name_len
;
177 if (strnlen(prop
->value
, prop
->length
) >= prop
->length
)
180 path_len
= strlen(path
);
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
);
189 for (k
= 0; k
< ovcs
->count
; k
++) {
190 fragment
= &ovcs
->fragments
[k
];
191 if (fragment
->overlay
== overlay_node
)
194 if (k
>= ovcs
->count
)
197 overlay_name_len
= snprintf(NULL
, 0, "%pOF", fragment
->overlay
);
199 if (overlay_name_len
> path_len
)
201 path_tail
= path
+ overlay_name_len
;
202 path_tail_len
= strlen(path_tail
);
204 target_path
= kasprintf(GFP_KERNEL
, "%pOF", fragment
->target
);
207 target_path_len
= strlen(target_path
);
209 new_prop
= kzalloc(sizeof(*new_prop
), GFP_KERNEL
);
211 goto err_free_target_path
;
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
;
219 strcpy(new_prop
->value
, target_path
);
220 strcpy(new_prop
->value
+ target_path_len
, path_tail
);
222 of_property_set_flag(new_prop
, OF_DYNAMIC
);
227 kfree(new_prop
->name
);
228 kfree(new_prop
->value
);
230 err_free_target_path
:
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__"
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.
247 * Some special properties are not updated (no error returned).
249 * Update of property in symbols node is not allowed.
251 * Returns 0 on success, -ENOMEM if memory allocation failure, or -EINVAL if
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
)
259 struct property
*new_prop
= NULL
, *prop
;
262 prop
= of_find_property(target_node
, overlay_prop
->name
, NULL
);
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"))
269 if (is_symbols_prop
) {
272 new_prop
= dup_and_fixup_symbol_prop(ovcs
, overlay_prop
);
274 new_prop
= __of_prop_dup(overlay_prop
, GFP_KERNEL
);
281 ret
= of_changeset_add_property(&ovcs
->cset
, target_node
,
284 ret
= of_changeset_update_property(&ovcs
->cset
, target_node
,
288 kfree(new_prop
->name
);
289 kfree(new_prop
->value
);
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
301 * If @node does not already exist in @target_node, add changeset entry
302 * to add @node in @target_node.
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.
307 * If @node has child nodes, add the children recursively via
308 * build_changeset_next_level().
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.
317 * TODO: add integrity check to ensure that multiple fragments do not
318 * create the same node.
320 * Returns 0 on success, -ENOMEM if memory allocation failure, or -EINVAL if
323 static int add_changeset_node(struct overlay_changeset
*ovcs
,
324 struct device_node
*target_node
, struct device_node
*node
)
326 const char *node_kbasename
;
327 struct device_node
*tchild
;
330 node_kbasename
= kbasename(node
->full_name
);
332 for_each_child_of_node(target_node
, tchild
)
333 if (!of_node_cmp(node_kbasename
, kbasename(tchild
->full_name
)))
337 tchild
= __of_node_dup(node
, "%pOF/%s",
338 target_node
, node_kbasename
);
342 tchild
->parent
= target_node
;
344 ret
= of_changeset_attach_node(&ovcs
->cset
, tchild
);
348 return build_changeset_next_level(ovcs
, tchild
, node
);
351 if (node
->phandle
&& tchild
->phandle
)
354 ret
= build_changeset_next_level(ovcs
, tchild
, node
);
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
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.
370 * Do not allow symbols node to have any children.
372 * Returns 0 on success, -ENOMEM if memory allocation failure, or -EINVAL if
373 * invalid @overlay_node.
375 static int build_changeset_next_level(struct overlay_changeset
*ovcs
,
376 struct device_node
*target_node
,
377 const struct device_node
*overlay_node
)
379 struct device_node
*child
;
380 struct property
*prop
;
383 for_each_property_of_node(overlay_node
, prop
) {
384 ret
= add_changeset_property(ovcs
, target_node
, prop
, 0);
386 pr_debug("Failed to apply prop @%pOF/%s, err=%d\n",
387 target_node
, prop
->name
, ret
);
392 for_each_child_of_node(overlay_node
, child
) {
393 ret
= add_changeset_node(ovcs
, target_node
, child
);
395 pr_debug("Failed to apply node @%pOF/%s, err=%d\n",
396 target_node
, child
->name
, ret
);
406 * Add the properties from __overlay__ node to the @ovcs->cset changeset.
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
)
412 struct property
*prop
;
415 for_each_property_of_node(overlay_symbols_node
, prop
) {
416 ret
= add_changeset_property(ovcs
, target_node
, prop
, 1);
418 pr_debug("Failed to apply prop @%pOF/%s, err=%d\n",
419 target_node
, prop
->name
, ret
);
428 * build_changeset() - populate overlay changeset in @ovcs from @ovcs->fragments
429 * @ovcs: Overlay changeset
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
436 * Returns 0 on success, -ENOMEM if memory allocation failure, or -EINVAL if
437 * invalid overlay in @ovcs->fragments[].
439 static int build_changeset(struct overlay_changeset
*ovcs
)
441 struct fragment
*fragment
;
442 int fragments_count
, i
, ret
;
445 * if there is a symbols fragment in ovcs->fragments[i] it is
446 * the final element in the array
448 if (ovcs
->symbols_fragment
)
449 fragments_count
= ovcs
->count
- 1;
451 fragments_count
= ovcs
->count
;
453 for (i
= 0; i
< fragments_count
; i
++) {
454 fragment
= &ovcs
->fragments
[i
];
456 ret
= build_changeset_next_level(ovcs
, fragment
->target
,
459 pr_debug("apply failed '%pOF'\n", fragment
->target
);
464 if (ovcs
->symbols_fragment
) {
465 fragment
= &ovcs
->fragments
[ovcs
->count
- 1];
466 ret
= build_changeset_symbols_node(ovcs
, fragment
->target
,
469 pr_debug("apply failed '%pOF'\n", fragment
->target
);
478 * Find the target node using a number of different strategies
479 * in order of preference:
481 * 1) "target" property containing the phandle of the target
482 * 2) "target-path" property containing the path of the target
484 static struct device_node
*find_target_node(struct device_node
*info_node
)
490 ret
= of_property_read_u32(info_node
, "target", &val
);
492 return of_find_node_by_phandle(val
);
494 ret
= of_property_read_string(info_node
, "target-path", &path
);
496 return of_find_node_by_path(path
);
498 pr_err("Failed to find target for node %p (%s)\n",
499 info_node
, info_node
->name
);
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
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.
513 * Returns 0 on success, -ENOMEM if memory allocation failure, -EINVAL if error
514 * detected in @tree, or -ENOSPC if idr_alloc() error.
516 static int init_overlay_changeset(struct overlay_changeset
*ovcs
,
517 struct device_node
*tree
)
519 struct device_node
*node
, *overlay_node
;
520 struct fragment
*fragment
;
521 struct fragment
*fragments
;
525 * Warn for some issues. Can not return -EINVAL for these until
526 * of_unittest_apply_overlay() is fixed to pass these checks.
528 if (!of_node_check_flag(tree
, OF_DYNAMIC
))
529 pr_debug("%s() tree is not dynamic\n", __func__
);
531 if (!of_node_check_flag(tree
, OF_DETACHED
))
532 pr_debug("%s() tree is not detached\n", __func__
);
534 if (!of_node_is_root(tree
))
535 pr_debug("%s() tree is not root\n", __func__
);
537 ovcs
->overlay_tree
= tree
;
539 INIT_LIST_HEAD(&ovcs
->ovcs_list
);
541 of_changeset_init(&ovcs
->cset
);
543 id
= idr_alloc(&ovcs_idr
, ovcs
, 1, 0, GFP_KERNEL
);
550 for_each_child_of_node(tree
, node
) {
551 overlay_node
= of_get_child_by_name(node
, "__overlay__");
554 of_node_put(overlay_node
);
558 node
= of_get_child_by_name(tree
, "__symbols__");
564 fragments
= kcalloc(cnt
, sizeof(*fragments
), GFP_KERNEL
);
571 for_each_child_of_node(tree
, node
) {
572 overlay_node
= of_get_child_by_name(node
, "__overlay__");
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
);
582 goto err_free_fragments
;
589 * if there is a symbols fragment in ovcs->fragments[i] it is
590 * the final element in the array
592 node
= of_get_child_by_name(tree
, "__symbols__");
594 ovcs
->symbols_fragment
= 1;
595 fragment
= &fragments
[cnt
];
596 fragment
->overlay
= node
;
597 fragment
->target
= of_find_node_by_path("/__symbols__");
599 if (!fragment
->target
) {
600 pr_err("symbols in overlay, but not in live tree\n");
602 goto err_free_fragments
;
610 goto err_free_fragments
;
615 ovcs
->fragments
= fragments
;
622 idr_remove(&ovcs_idr
, id
);
624 pr_err("%s() failed, ret = %d\n", __func__
, ret
);
629 static void free_overlay_changeset(struct overlay_changeset
*ovcs
)
633 if (ovcs
->cset
.entries
.next
)
634 of_changeset_destroy(&ovcs
->cset
);
637 idr_remove(&ovcs_idr
, ovcs
->id
);
639 for (i
= 0; i
< ovcs
->count
; i
++) {
640 of_node_put(ovcs
->fragments
[i
].target
);
641 of_node_put(ovcs
->fragments
[i
].overlay
);
643 kfree(ovcs
->fragments
);
649 * of_overlay_apply() - Create and apply an overlay changeset
650 * @tree: Expanded overlay device tree
651 * @ovcs_id: Pointer to overlay changeset id
653 * Creates and applies an overlay changeset.
655 * If an error occurs in a pre-apply notifier, then no changes are made
656 * to the device tree.
659 * A non-zero return value will not have created the changeset if error is from:
661 * - building the changeset
662 * - overlay changeset pre-apply notifier
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.
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
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.
674 * If more than one notifier returns an error, then the last notifier
675 * error to occur is returned.
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
684 * Returns 0 on success, or a negative error number. Overlay changeset
685 * id is returned to *ovcs_id.
688 int of_overlay_apply(struct device_node
*tree
, int *ovcs_id
)
690 struct overlay_changeset
*ovcs
;
691 int ret
= 0, ret_revert
, ret_tmp
;
695 if (devicetree_corrupt()) {
696 pr_err("devicetree state suspect, refuse to apply overlay\n");
701 ovcs
= kzalloc(sizeof(*ovcs
), GFP_KERNEL
);
707 of_overlay_mutex_lock();
708 mutex_lock(&of_mutex
);
710 ret
= of_resolve_phandles(tree
);
712 goto err_free_overlay_changeset
;
714 ret
= init_overlay_changeset(ovcs
, tree
);
716 goto err_free_overlay_changeset
;
718 ret
= overlay_notify(ovcs
, OF_OVERLAY_PRE_APPLY
);
720 pr_err("overlay changeset pre-apply notify error %d\n", ret
);
721 goto err_free_overlay_changeset
;
724 ret
= build_changeset(ovcs
);
726 goto err_free_overlay_changeset
;
729 ret
= __of_changeset_apply_entries(&ovcs
->cset
, &ret_revert
);
732 pr_debug("overlay changeset revert error %d\n",
734 devicetree_state_flags
|= DTSF_APPLY_FAIL
;
736 goto err_free_overlay_changeset
;
739 ret
= __of_changeset_apply_notify(&ovcs
->cset
);
741 pr_err("overlay changeset entry notify error %d\n", ret
);
742 /* notify failure is not fatal, continue */
744 list_add_tail(&ovcs
->ovcs_list
, &ovcs_list
);
747 ret_tmp
= overlay_notify(ovcs
, OF_OVERLAY_POST_APPLY
);
749 pr_err("overlay changeset post-apply notify error %d\n",
757 err_free_overlay_changeset
:
758 free_overlay_changeset(ovcs
);
761 mutex_unlock(&of_mutex
);
762 of_overlay_mutex_unlock();
765 pr_debug("%s() err=%d\n", __func__
, ret
);
769 EXPORT_SYMBOL_GPL(of_overlay_apply
);
774 * Returns 1 if @np is @tree or is contained in @tree, else 0
776 static int find_node(struct device_node
*tree
, struct device_node
*np
)
778 struct device_node
*child
;
783 for_each_child_of_node(tree
, child
) {
784 if (find_node(child
, np
)) {
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?
797 * Returns 1 if found, else 0
799 static int node_overlaps_later_cs(struct overlay_changeset
*remove_ovcs
,
800 struct device_node
*remove_ce_node
)
802 struct overlay_changeset
*ovcs
;
803 struct of_changeset_entry
*ce
;
805 list_for_each_entry_reverse(ovcs
, &ovcs_list
, ovcs_list
) {
806 if (ovcs
== remove_ovcs
)
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
,
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
,
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.
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.
838 static int overlay_removal_is_ok(struct overlay_changeset
*remove_ovcs
)
840 struct of_changeset_entry
*remove_ce
;
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
);
853 * of_overlay_remove() - Revert and free an overlay changeset
854 * @ovcs_id: Pointer to overlay changeset id
856 * Removes an overlay if it is permissible. @ovcs_id was previously returned
857 * by of_overlay_apply().
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.
865 * A non-zero return value will not revert the changeset if error is from:
867 * - overlay changeset pre-remove notifier
868 * - overlay changeset entry revert
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.
873 * If more than one notifier returns an error, then the last notifier
874 * error to occur is returned.
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
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.
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.
886 int of_overlay_remove(int *ovcs_id
)
888 struct overlay_changeset
*ovcs
;
889 int ret
, ret_apply
, ret_tmp
;
893 if (devicetree_corrupt()) {
894 pr_err("suspect devicetree state, refuse to remove overlay\n");
899 mutex_lock(&of_mutex
);
901 ovcs
= idr_find(&ovcs_idr
, *ovcs_id
);
904 pr_err("remove: Could not find overlay #%d\n", *ovcs_id
);
908 if (!overlay_removal_is_ok(ovcs
)) {
913 ret
= overlay_notify(ovcs
, OF_OVERLAY_PRE_REMOVE
);
915 pr_err("overlay changeset pre-remove notify error %d\n", ret
);
919 list_del(&ovcs
->ovcs_list
);
922 ret
= __of_changeset_revert_entries(&ovcs
->cset
, &ret_apply
);
925 devicetree_state_flags
|= DTSF_REVERT_FAIL
;
929 ret
= __of_changeset_revert_notify(&ovcs
->cset
);
931 pr_err("overlay changeset entry notify error %d\n", ret
);
932 /* notify failure is not fatal, continue */
936 ret_tmp
= overlay_notify(ovcs
, OF_OVERLAY_POST_REMOVE
);
938 pr_err("overlay changeset post-remove notify error %d\n",
944 free_overlay_changeset(ovcs
);
947 mutex_unlock(&of_mutex
);
950 pr_debug("%s() err=%d\n", __func__
, ret
);
954 EXPORT_SYMBOL_GPL(of_overlay_remove
);
957 * of_overlay_remove_all() - Reverts and frees all overlay changesets
959 * Removes all overlays from the system in the correct order.
961 * Returns 0 on success, or a negative error number
963 int of_overlay_remove_all(void)
965 struct overlay_changeset
*ovcs
, *ovcs_n
;
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
);
977 EXPORT_SYMBOL_GPL(of_overlay_remove_all
);