Lines Matching +full:i +full:- +full:leak +full:- +full:current

1 // SPDX-License-Identifier: GPL-2.0
5 * Copyright (C) 2012 Pantelis Antoniou <panto@antoniou-consulting.com>
27 * struct target - info about current target node as recursing through overlay
28 * @np: node where current level of overlay will be applied
37 * subtree does not exist at the same level in the live devicetree, target->np
47 * struct fragment - info about fragment nodes in overlay expanded device tree
82 /* flags are sticky - once set, do not reset */
128 * of_overlay_notifier_register() - Register notifier for overlay operations
146 * of_overlay_notifier_unregister() - Unregister notifier for overlay operations
159 int i, ret; in overlay_notify() local
161 ovcs->notify_state = action; in overlay_notify()
163 for (i = 0; i < ovcs->count; i++) { in overlay_notify()
164 struct fragment *fragment = &ovcs->fragments[i]; in overlay_notify()
166 nd.target = fragment->target; in overlay_notify()
167 nd.overlay = fragment->overlay; in overlay_notify()
184 * the ovcs->overlay_root. When duplicating the properties, the paths
189 * where symbol_path_tail can be a single node or it may be a multi-node path.
211 if (!prop->value) in dup_and_fixup_symbol_prop()
213 if (strnlen(prop->value, prop->length) >= prop->length) in dup_and_fixup_symbol_prop()
215 path = prop->value; in dup_and_fixup_symbol_prop()
220 fragment_node = __of_find_node_by_path(ovcs->overlay_root, path + 1); in dup_and_fixup_symbol_prop()
225 for (k = 0; k < ovcs->count; k++) { in dup_and_fixup_symbol_prop()
226 fragment = &ovcs->fragments[k]; in dup_and_fixup_symbol_prop()
227 if (fragment->overlay == overlay_node) in dup_and_fixup_symbol_prop()
230 if (k >= ovcs->count) in dup_and_fixup_symbol_prop()
233 overlay_name_len = snprintf(NULL, 0, "%pOF", fragment->overlay); in dup_and_fixup_symbol_prop()
240 target_path = kasprintf(GFP_KERNEL, "%pOF", fragment->target); in dup_and_fixup_symbol_prop()
249 new_prop->name = kstrdup(prop->name, GFP_KERNEL); in dup_and_fixup_symbol_prop()
250 new_prop->length = target_path_len + path_tail_len + 1; in dup_and_fixup_symbol_prop()
251 new_prop->value = kzalloc(new_prop->length, GFP_KERNEL); in dup_and_fixup_symbol_prop()
252 if (!new_prop->name || !new_prop->value) in dup_and_fixup_symbol_prop()
255 strcpy(new_prop->value, target_path); in dup_and_fixup_symbol_prop()
256 strcpy(new_prop->value + target_path_len, path_tail); in dup_and_fixup_symbol_prop()
273 * add_changeset_property() - add @overlay_prop to overlay changeset
289 * Properties "#address-cells" and "#size-cells" are not updated if they
295 * Return: 0 on success, -ENOMEM if memory allocation failure, or -EINVAL if
305 if (target->in_livetree) in add_changeset_property()
306 if (!of_prop_cmp(overlay_prop->name, "name") || in add_changeset_property()
307 !of_prop_cmp(overlay_prop->name, "phandle") || in add_changeset_property()
308 !of_prop_cmp(overlay_prop->name, "linux,phandle")) in add_changeset_property()
311 if (target->in_livetree) in add_changeset_property()
312 prop = of_find_property(target->np, overlay_prop->name, NULL); in add_changeset_property()
317 if (!of_prop_cmp(prop->name, "#address-cells")) { in add_changeset_property()
319 pr_err("ERROR: changing value of #address-cells is not allowed in %pOF\n", in add_changeset_property()
320 target->np); in add_changeset_property()
321 ret = -EINVAL; in add_changeset_property()
325 } else if (!of_prop_cmp(prop->name, "#size-cells")) { in add_changeset_property()
327 pr_err("ERROR: changing value of #size-cells is not allowed in %pOF\n", in add_changeset_property()
328 target->np); in add_changeset_property()
329 ret = -EINVAL; in add_changeset_property()
337 return -EINVAL; in add_changeset_property()
344 return -ENOMEM; in add_changeset_property()
347 if (!target->in_livetree) { in add_changeset_property()
348 new_prop->next = target->np->deadprops; in add_changeset_property()
349 target->np->deadprops = new_prop; in add_changeset_property()
351 ret = of_changeset_add_property(&ovcs->cset, target->np, in add_changeset_property()
354 ret = of_changeset_update_property(&ovcs->cset, target->np, in add_changeset_property()
358 if (!of_node_check_flag(target->np, OF_OVERLAY)) in add_changeset_property()
359 pr_err("WARNING: memory leak will occur if overlay removed, property: %pOF/%s\n", in add_changeset_property()
360 target->np, new_prop->name); in add_changeset_property()
368 * add_changeset_node() - add @node (and children) to overlay changeset
383 * not contain the full path in node->full_name. Thus an overlay
385 * node->full_name. However, a live devicetree created from Open
386 * Firmware may have the full path in node->full_name.
389 * the full path in node->full_name. Even though it expects the overlay
397 * Return: 0 on success, -ENOMEM if memory allocation failure, or -EINVAL if
409 node_kbasename = kbasename(node->full_name); in add_changeset_node()
411 for_each_child_of_node(target->np, tchild) in add_changeset_node()
412 if (!of_node_cmp(node_kbasename, kbasename(tchild->full_name))) in add_changeset_node()
418 return -ENOMEM; in add_changeset_node()
420 tchild->parent = target->np; in add_changeset_node()
421 tchild->name = __of_get_property(node, "name", NULL); in add_changeset_node()
423 if (!tchild->name) in add_changeset_node()
424 tchild->name = "<NULL>"; in add_changeset_node()
429 tchild->phandle = be32_to_cpup(phandle); in add_changeset_node()
433 ret = of_changeset_attach_node(&ovcs->cset, tchild); in add_changeset_node()
445 if (node->phandle && tchild->phandle) { in add_changeset_node()
446 ret = -EINVAL; in add_changeset_node()
449 target_child.in_livetree = target->in_livetree; in add_changeset_node()
458 * build_changeset_next_level() - add level of overlay changeset
464 * @ovcs->cset changeset. If an added node has child nodes, they will
469 * Return: 0 on success, -ENOMEM if memory allocation failure, or -EINVAL if
482 target->np, prop->name, ret); in build_changeset_next_level()
491 target->np, child, ret); in build_changeset_next_level()
500 * Add the properties from __overlay__ node to the @ovcs->cset changeset.
513 target->np, prop->name, ret); in build_changeset_symbols_node()
528 if (ce_1->action != OF_RECONFIG_ATTACH_NODE && in find_dup_cset_node_entry()
529 ce_1->action != OF_RECONFIG_DETACH_NODE) in find_dup_cset_node_entry()
533 list_for_each_entry_continue(ce_2, &ovcs->cset.entries, node) { in find_dup_cset_node_entry()
534 if ((ce_2->action != OF_RECONFIG_ATTACH_NODE && in find_dup_cset_node_entry()
535 ce_2->action != OF_RECONFIG_DETACH_NODE) || in find_dup_cset_node_entry()
536 of_node_cmp(ce_1->np->full_name, ce_2->np->full_name)) in find_dup_cset_node_entry()
539 fn_1 = kasprintf(GFP_KERNEL, "%pOF", ce_1->np); in find_dup_cset_node_entry()
540 fn_2 = kasprintf(GFP_KERNEL, "%pOF", ce_2->np); in find_dup_cset_node_entry()
546 ce_1->np); in find_dup_cset_node_entry()
547 return -EINVAL; in find_dup_cset_node_entry()
561 if (ce_1->action != OF_RECONFIG_ADD_PROPERTY && in find_dup_cset_prop()
562 ce_1->action != OF_RECONFIG_REMOVE_PROPERTY && in find_dup_cset_prop()
563 ce_1->action != OF_RECONFIG_UPDATE_PROPERTY) in find_dup_cset_prop()
567 list_for_each_entry_continue(ce_2, &ovcs->cset.entries, node) { in find_dup_cset_prop()
568 if ((ce_2->action != OF_RECONFIG_ADD_PROPERTY && in find_dup_cset_prop()
569 ce_2->action != OF_RECONFIG_REMOVE_PROPERTY && in find_dup_cset_prop()
570 ce_2->action != OF_RECONFIG_UPDATE_PROPERTY) || in find_dup_cset_prop()
571 of_node_cmp(ce_1->np->full_name, ce_2->np->full_name)) in find_dup_cset_prop()
574 fn_1 = kasprintf(GFP_KERNEL, "%pOF", ce_1->np); in find_dup_cset_prop()
575 fn_2 = kasprintf(GFP_KERNEL, "%pOF", ce_2->np); in find_dup_cset_prop()
580 !of_prop_cmp(ce_1->prop->name, ce_2->prop->name)) { in find_dup_cset_prop()
582 ce_1->np, ce_1->prop->name); in find_dup_cset_prop()
583 return -EINVAL; in find_dup_cset_prop()
591 * changeset_dup_entry_check() - check for duplicate entries
594 * Check changeset @ovcs->cset for multiple {add or delete} node entries for
598 * Return: 0 on success, or -EINVAL if duplicate changeset entry found.
605 list_for_each_entry(ce_1, &ovcs->cset.entries, node) { in changeset_dup_entry_check()
610 return dup_entry ? -EINVAL : 0; in changeset_dup_entry_check()
614 * build_changeset() - populate overlay changeset in @ovcs from @ovcs->fragments
617 * Create changeset @ovcs->cset to contain the nodes and properties of the
618 * overlay device tree fragments in @ovcs->fragments[]. If an error occurs,
620 * in @ovcs->cset.
622 * Return: 0 on success, -ENOMEM if memory allocation failure, or -EINVAL if
623 * invalid overlay in @ovcs->fragments[].
629 int fragments_count, i, ret; in build_changeset() local
632 * if there is a symbols fragment in ovcs->fragments[i] it is in build_changeset()
635 if (ovcs->symbols_fragment) in build_changeset()
636 fragments_count = ovcs->count - 1; in build_changeset()
638 fragments_count = ovcs->count; in build_changeset()
640 for (i = 0; i < fragments_count; i++) { in build_changeset()
641 fragment = &ovcs->fragments[i]; in build_changeset()
643 target.np = fragment->target; in build_changeset()
646 fragment->overlay); in build_changeset()
649 fragment->target); in build_changeset()
654 if (ovcs->symbols_fragment) { in build_changeset()
655 fragment = &ovcs->fragments[ovcs->count - 1]; in build_changeset()
657 target.np = fragment->target; in build_changeset()
660 fragment->overlay); in build_changeset()
663 fragment->target); in build_changeset()
676 * 2) "target-path" property containing the path of the target
696 ret = of_property_read_string(info_node, "target-path", &path); in find_target()
724 * init_overlay_changeset() - initialize overlay changeset from overlay tree
728 * Initialize @ovcs. Populate @ovcs->fragments with node information from
733 * Return: 0 on success, -ENOMEM if memory allocation failure, -EINVAL if error
753 * Warn for some issues. Can not return -EINVAL for these until in init_overlay_changeset()
756 if (!of_node_check_flag(ovcs->overlay_root, OF_DYNAMIC)) in init_overlay_changeset()
757 pr_debug("%s() ovcs->overlay_root is not dynamic\n", __func__); in init_overlay_changeset()
759 if (!of_node_check_flag(ovcs->overlay_root, OF_DETACHED)) in init_overlay_changeset()
760 pr_debug("%s() ovcs->overlay_root is not detached\n", __func__); in init_overlay_changeset()
762 if (!of_node_is_root(ovcs->overlay_root)) in init_overlay_changeset()
763 pr_debug("%s() ovcs->overlay_root is not root\n", __func__); in init_overlay_changeset()
768 for_each_child_of_node(ovcs->overlay_root, node) { in init_overlay_changeset()
776 node = of_get_child_by_name(ovcs->overlay_root, "__symbols__"); in init_overlay_changeset()
784 ret = -ENOMEM; in init_overlay_changeset()
787 ovcs->fragments = fragments; in init_overlay_changeset()
790 for_each_child_of_node(ovcs->overlay_root, node) { in init_overlay_changeset()
796 fragment->overlay = overlay_node; in init_overlay_changeset()
797 fragment->target = find_target(node, target_base); in init_overlay_changeset()
798 if (!fragment->target) { in init_overlay_changeset()
799 of_node_put(fragment->overlay); in init_overlay_changeset()
800 ret = -EINVAL; in init_overlay_changeset()
809 * if there is a symbols fragment in ovcs->fragments[i] it is in init_overlay_changeset()
812 node = of_get_child_by_name(ovcs->overlay_root, "__symbols__"); in init_overlay_changeset()
814 ovcs->symbols_fragment = 1; in init_overlay_changeset()
816 fragment->overlay = node; in init_overlay_changeset()
817 fragment->target = of_find_node_by_path("/__symbols__"); in init_overlay_changeset()
819 if (!fragment->target) { in init_overlay_changeset()
821 ret = -EINVAL; in init_overlay_changeset()
831 ret = -EINVAL; in init_overlay_changeset()
835 ovcs->count = cnt; in init_overlay_changeset()
847 int i; in free_overlay_changeset() local
849 if (ovcs->cset.entries.next) in free_overlay_changeset()
850 of_changeset_destroy(&ovcs->cset); in free_overlay_changeset()
852 if (ovcs->id) { in free_overlay_changeset()
853 idr_remove(&ovcs_idr, ovcs->id); in free_overlay_changeset()
854 list_del(&ovcs->ovcs_list); in free_overlay_changeset()
855 ovcs->id = 0; in free_overlay_changeset()
859 for (i = 0; i < ovcs->count; i++) { in free_overlay_changeset()
860 of_node_put(ovcs->fragments[i].target); in free_overlay_changeset()
861 of_node_put(ovcs->fragments[i].overlay); in free_overlay_changeset()
863 kfree(ovcs->fragments); in free_overlay_changeset()
866 * There should be no live pointers into ovcs->overlay_mem and in free_overlay_changeset()
867 * ovcs->new_fdt due to the policy that overlay notifiers are not in free_overlay_changeset()
872 * A memory leak will occur here if within the window. in free_overlay_changeset()
875 if (ovcs->notify_state == OF_OVERLAY_INIT || in free_overlay_changeset()
876 ovcs->notify_state == OF_OVERLAY_POST_REMOVE) { in free_overlay_changeset()
877 kfree(ovcs->overlay_mem); in free_overlay_changeset()
878 kfree(ovcs->new_fdt); in free_overlay_changeset()
886 * of_overlay_apply() - Create and apply an overlay changeset
892 * If an error is returned by an overlay changeset pre-apply notifier
893 * then no further overlay changeset pre-apply notifier will be called.
895 * If an error is returned by an overlay changeset post-apply notifier
896 * then no further overlay changeset post-apply notifier will be called.
917 ret = of_resolve_phandles(ovcs->overlay_root); in of_overlay_apply()
934 ret = __of_changeset_apply_entries(&ovcs->cset, &ret_revert); in of_overlay_apply()
944 ret = __of_changeset_apply_notify(&ovcs->cset); in of_overlay_apply()
961 * of_overlay_fdt_apply() - Create and apply an overlay changeset
994 return -EBUSY; in of_overlay_fdt_apply()
1000 return -EINVAL; in of_overlay_fdt_apply()
1005 return -EINVAL; in of_overlay_fdt_apply()
1009 return -ENOMEM; in of_overlay_fdt_apply()
1015 * ovcs->notify_state must be set to OF_OVERLAY_INIT before allocating in of_overlay_fdt_apply()
1019 ovcs->id = idr_alloc(&ovcs_idr, ovcs, 1, 0, GFP_KERNEL); in of_overlay_fdt_apply()
1020 if (ovcs->id <= 0) { in of_overlay_fdt_apply()
1021 ret = ovcs->id; in of_overlay_fdt_apply()
1025 INIT_LIST_HEAD(&ovcs->ovcs_list); in of_overlay_fdt_apply()
1026 list_add_tail(&ovcs->ovcs_list, &ovcs_list); in of_overlay_fdt_apply()
1027 of_changeset_init(&ovcs->cset); in of_overlay_fdt_apply()
1035 ret = -ENOMEM; in of_overlay_fdt_apply()
1038 ovcs->new_fdt = new_fdt; in of_overlay_fdt_apply()
1044 &ovcs->overlay_root); in of_overlay_fdt_apply()
1047 ret = -EINVAL; in of_overlay_fdt_apply()
1050 ovcs->overlay_mem = overlay_mem; in of_overlay_fdt_apply()
1055 * result in a memory leak if the apply partly succeeded, so do NOT in of_overlay_fdt_apply()
1059 *ret_ovcs_id = ovcs->id; in of_overlay_fdt_apply()
1106 list_for_each_entry(ce, &ovcs->cset.entries, node) { in node_overlaps_later_cs()
1107 if (find_node(ce->np, remove_ce_node)) { in node_overlaps_later_cs()
1109 __func__, remove_ovcs->id, ovcs->id, in node_overlaps_later_cs()
1113 if (find_node(remove_ce_node, ce->np)) { in node_overlaps_later_cs()
1115 __func__, remove_ovcs->id, ovcs->id, in node_overlaps_later_cs()
1126 * We can safely remove the overlay only if it's the top-most one.
1139 list_for_each_entry(remove_ce, &remove_ovcs->cset.entries, node) { in overlay_removal_is_ok()
1140 if (node_overlaps_later_cs(remove_ovcs, remove_ce->np)) { in overlay_removal_is_ok()
1141 pr_err("overlay #%d is not topmost\n", remove_ovcs->id); in overlay_removal_is_ok()
1150 * of_overlay_remove() - Revert and free an overlay changeset
1157 * then an attempt is made to re-apply any changeset entry that was
1158 * reverted. If an error occurs on re-apply then the state of the device
1162 * A non-zero return value will not revert the changeset if error is from:
1163 * - parameter checks
1164 * - overlay changeset pre-remove notifier
1165 * - overlay changeset entry revert
1167 * If an error is returned by an overlay changeset pre-remove notifier
1168 * then no further overlay changeset pre-remove notifier will be called.
1173 * A non-zero return value will revert the changeset if error is from:
1174 * - overlay changeset entry notifier
1175 * - overlay changeset post-remove notifier
1177 * If an error is returned by an overlay changeset post-remove notifier
1178 * then no further overlay changeset post-remove notifier will be called.
1190 ret = -EBUSY; in of_overlay_remove()
1198 ret = -ENODEV; in of_overlay_remove()
1204 ret = -EBUSY; in of_overlay_remove()
1213 ret = __of_changeset_revert_entries(&ovcs->cset, &ret_apply); in of_overlay_remove()
1220 ret = __of_changeset_revert_notify(&ovcs->cset); in of_overlay_remove()
1242 * overlay related memory. This is a memory leak unless a subsequent in of_overlay_remove()
1255 * of_overlay_remove_all() - Reverts and frees all overlay changesets
1268 ret = of_overlay_remove(&ovcs->id); in of_overlay_remove_all()