2017-12-27 18:55:14 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
2014-10-28 20:35:58 +00:00
|
|
|
/*
|
|
|
|
* Functions for working with device tree overlays
|
|
|
|
*
|
|
|
|
* Copyright (C) 2012 Pantelis Antoniou <panto@antoniou-consulting.com>
|
|
|
|
* Copyright (C) 2012 Texas Instruments Inc.
|
|
|
|
*/
|
2016-06-15 13:32:18 +00:00
|
|
|
|
|
|
|
#define pr_fmt(fmt) "OF: overlay: " fmt
|
|
|
|
|
2014-10-28 20:35:58 +00:00
|
|
|
#include <linux/kernel.h>
|
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/of.h>
|
|
|
|
#include <linux/of_device.h>
|
2018-02-12 08:19:42 +00:00
|
|
|
#include <linux/of_fdt.h>
|
2014-10-28 20:35:58 +00:00
|
|
|
#include <linux/string.h>
|
|
|
|
#include <linux/ctype.h>
|
|
|
|
#include <linux/errno.h>
|
|
|
|
#include <linux/slab.h>
|
2018-02-12 08:19:42 +00:00
|
|
|
#include <linux/libfdt.h>
|
2014-10-28 20:35:58 +00:00
|
|
|
#include <linux/err.h>
|
2015-02-17 02:36:58 +00:00
|
|
|
#include <linux/idr.h>
|
2014-10-28 20:35:58 +00:00
|
|
|
|
|
|
|
#include "of_private.h"
|
|
|
|
|
2018-10-05 03:28:08 +00:00
|
|
|
/**
|
|
|
|
* struct target - info about current target node as recursing through overlay
|
|
|
|
* @np: node where current level of overlay will be applied
|
|
|
|
* @in_livetree: @np is a node in the live devicetree
|
|
|
|
*
|
|
|
|
* Used in the algorithm to create the portion of a changeset that describes
|
|
|
|
* an overlay fragment, which is a devicetree subtree. Initially @np is a node
|
|
|
|
* in the live devicetree where the overlay subtree is targeted to be grafted
|
|
|
|
* into. When recursing to the next level of the overlay subtree, the target
|
|
|
|
* also recurses to the next level of the live devicetree, as long as overlay
|
|
|
|
* subtree node also exists in the live devicetree. When a node in the overlay
|
|
|
|
* subtree does not exist at the same level in the live devicetree, target->np
|
|
|
|
* points to a newly allocated node, and all subsequent targets in the subtree
|
|
|
|
* will be newly allocated nodes.
|
|
|
|
*/
|
|
|
|
struct target {
|
|
|
|
struct device_node *np;
|
|
|
|
bool in_livetree;
|
|
|
|
};
|
|
|
|
|
2014-10-28 20:35:58 +00:00
|
|
|
/**
|
2017-10-17 23:36:23 +00:00
|
|
|
* struct fragment - info about fragment nodes in overlay expanded device tree
|
|
|
|
* @overlay: pointer to the __overlay__ node
|
2023-09-28 09:35:39 +00:00
|
|
|
* @target: target of the overlay operation
|
2014-10-28 20:35:58 +00:00
|
|
|
*/
|
2017-10-17 23:36:23 +00:00
|
|
|
struct fragment {
|
2014-10-28 20:35:58 +00:00
|
|
|
struct device_node *overlay;
|
2018-10-05 03:30:40 +00:00
|
|
|
struct device_node *target;
|
2014-10-28 20:35:58 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2017-10-17 23:36:23 +00:00
|
|
|
* struct overlay_changeset
|
2018-02-12 08:19:42 +00:00
|
|
|
* @id: changeset identifier
|
2017-10-17 23:36:30 +00:00
|
|
|
* @ovcs_list: list on which we are located
|
2022-04-20 22:25:04 +00:00
|
|
|
* @new_fdt: Memory allocated to hold unflattened aligned FDT
|
2022-04-20 22:25:05 +00:00
|
|
|
* @overlay_mem: the memory chunk that contains @overlay_root
|
2022-04-20 22:25:04 +00:00
|
|
|
* @overlay_root: expanded device tree that contains the fragment nodes
|
2022-04-20 22:25:05 +00:00
|
|
|
* @notify_state: most recent notify action used on overlay
|
2017-10-17 23:36:30 +00:00
|
|
|
* @count: count of fragment structures
|
|
|
|
* @fragments: fragment nodes in the overlay expanded device tree
|
|
|
|
* @symbols_fragment: last element of @fragments[] is the __symbols__ node
|
|
|
|
* @cset: changeset to apply fragments to live device tree
|
2014-10-28 20:35:58 +00:00
|
|
|
*/
|
2017-10-17 23:36:23 +00:00
|
|
|
struct overlay_changeset {
|
2014-10-28 20:35:58 +00:00
|
|
|
int id;
|
2017-10-17 23:36:23 +00:00
|
|
|
struct list_head ovcs_list;
|
2022-04-20 22:25:04 +00:00
|
|
|
const void *new_fdt;
|
2022-04-20 22:25:05 +00:00
|
|
|
const void *overlay_mem;
|
2022-04-20 22:25:04 +00:00
|
|
|
struct device_node *overlay_root;
|
2022-04-20 22:25:05 +00:00
|
|
|
enum of_overlay_notify_action notify_state;
|
2014-10-28 20:35:58 +00:00
|
|
|
int count;
|
2017-10-17 23:36:23 +00:00
|
|
|
struct fragment *fragments;
|
2017-10-17 23:36:30 +00:00
|
|
|
bool symbols_fragment;
|
2014-10-28 20:35:58 +00:00
|
|
|
struct of_changeset cset;
|
|
|
|
};
|
|
|
|
|
2017-10-17 23:36:26 +00:00
|
|
|
/* flags are sticky - once set, do not reset */
|
|
|
|
static int devicetree_state_flags;
|
|
|
|
#define DTSF_APPLY_FAIL 0x01
|
|
|
|
#define DTSF_REVERT_FAIL 0x02
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If a changeset apply or revert encounters an error, an attempt will
|
|
|
|
* be made to undo partial changes, but may fail. If the undo fails
|
|
|
|
* we do not know the state of the devicetree.
|
|
|
|
*/
|
|
|
|
static int devicetree_corrupt(void)
|
|
|
|
{
|
|
|
|
return devicetree_state_flags &
|
|
|
|
(DTSF_APPLY_FAIL | DTSF_REVERT_FAIL);
|
|
|
|
}
|
|
|
|
|
2017-10-17 23:36:23 +00:00
|
|
|
static int build_changeset_next_level(struct overlay_changeset *ovcs,
|
2018-10-05 03:28:08 +00:00
|
|
|
struct target *target, const struct device_node *overlay_node);
|
2014-10-28 20:35:58 +00:00
|
|
|
|
2017-10-17 23:36:29 +00:00
|
|
|
/*
|
|
|
|
* of_resolve_phandles() finds the largest phandle in the live tree.
|
|
|
|
* of_overlay_apply() may add a larger phandle to the live tree.
|
|
|
|
* Do not allow race between two overlays being applied simultaneously:
|
|
|
|
* mutex_lock(&of_overlay_phandle_mutex)
|
|
|
|
* of_resolve_phandles()
|
|
|
|
* of_overlay_apply()
|
|
|
|
* mutex_unlock(&of_overlay_phandle_mutex)
|
|
|
|
*/
|
|
|
|
static DEFINE_MUTEX(of_overlay_phandle_mutex);
|
|
|
|
|
|
|
|
void of_overlay_mutex_lock(void)
|
|
|
|
{
|
|
|
|
mutex_lock(&of_overlay_phandle_mutex);
|
|
|
|
}
|
|
|
|
|
|
|
|
void of_overlay_mutex_unlock(void)
|
|
|
|
{
|
|
|
|
mutex_unlock(&of_overlay_phandle_mutex);
|
|
|
|
}
|
|
|
|
|
2017-10-17 23:36:25 +00:00
|
|
|
static LIST_HEAD(ovcs_list);
|
|
|
|
static DEFINE_IDR(ovcs_idr);
|
|
|
|
|
2017-10-17 23:36:23 +00:00
|
|
|
static BLOCKING_NOTIFIER_HEAD(overlay_notify_chain);
|
2016-11-01 19:14:22 +00:00
|
|
|
|
2018-04-26 11:00:30 +00:00
|
|
|
/**
|
|
|
|
* of_overlay_notifier_register() - Register notifier for overlay operations
|
|
|
|
* @nb: Notifier block to register
|
|
|
|
*
|
|
|
|
* Register for notification on overlay operations on device tree nodes. The
|
|
|
|
* reported actions definied by @of_reconfig_change. The notifier callback
|
|
|
|
* furthermore receives a pointer to the affected device tree node.
|
|
|
|
*
|
|
|
|
* Note that a notifier callback is not supposed to store pointers to a device
|
|
|
|
* tree node or its content beyond @OF_OVERLAY_POST_REMOVE corresponding to the
|
|
|
|
* respective node it received.
|
|
|
|
*/
|
2016-11-01 19:14:22 +00:00
|
|
|
int of_overlay_notifier_register(struct notifier_block *nb)
|
|
|
|
{
|
2017-10-17 23:36:23 +00:00
|
|
|
return blocking_notifier_chain_register(&overlay_notify_chain, nb);
|
2016-11-01 19:14:22 +00:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(of_overlay_notifier_register);
|
|
|
|
|
2018-04-26 11:00:30 +00:00
|
|
|
/**
|
2021-03-18 10:40:35 +00:00
|
|
|
* of_overlay_notifier_unregister() - Unregister notifier for overlay operations
|
2018-04-26 11:00:30 +00:00
|
|
|
* @nb: Notifier block to unregister
|
|
|
|
*/
|
2016-11-01 19:14:22 +00:00
|
|
|
int of_overlay_notifier_unregister(struct notifier_block *nb)
|
|
|
|
{
|
2017-10-17 23:36:23 +00:00
|
|
|
return blocking_notifier_chain_unregister(&overlay_notify_chain, nb);
|
2016-11-01 19:14:22 +00:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(of_overlay_notifier_unregister);
|
|
|
|
|
2017-10-17 23:36:23 +00:00
|
|
|
static int overlay_notify(struct overlay_changeset *ovcs,
|
|
|
|
enum of_overlay_notify_action action)
|
2016-11-01 19:14:22 +00:00
|
|
|
{
|
|
|
|
struct of_overlay_notify_data nd;
|
|
|
|
int i, ret;
|
|
|
|
|
2022-04-20 22:25:05 +00:00
|
|
|
ovcs->notify_state = action;
|
|
|
|
|
2017-10-17 23:36:23 +00:00
|
|
|
for (i = 0; i < ovcs->count; i++) {
|
|
|
|
struct fragment *fragment = &ovcs->fragments[i];
|
2016-11-01 19:14:22 +00:00
|
|
|
|
2017-10-17 23:36:23 +00:00
|
|
|
nd.target = fragment->target;
|
|
|
|
nd.overlay = fragment->overlay;
|
2016-11-01 19:14:22 +00:00
|
|
|
|
2017-10-17 23:36:23 +00:00
|
|
|
ret = blocking_notifier_call_chain(&overlay_notify_chain,
|
2016-11-01 19:14:22 +00:00
|
|
|
action, &nd);
|
2022-04-20 13:02:05 +00:00
|
|
|
if (notifier_to_errno(ret)) {
|
2017-10-17 23:36:26 +00:00
|
|
|
ret = notifier_to_errno(ret);
|
|
|
|
pr_err("overlay changeset %s notifier error %d, target: %pOF\n",
|
2022-05-02 18:17:40 +00:00
|
|
|
of_overlay_action_name(action), ret, nd.target);
|
2017-10-17 23:36:26 +00:00
|
|
|
return ret;
|
|
|
|
}
|
2016-11-01 19:14:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-10-17 23:36:24 +00:00
|
|
|
/*
|
2017-10-17 23:36:31 +00:00
|
|
|
* The values of properties in the "/__symbols__" node are paths in
|
2022-04-20 22:25:04 +00:00
|
|
|
* the ovcs->overlay_root. When duplicating the properties, the paths
|
2017-10-17 23:36:31 +00:00
|
|
|
* need to be adjusted to be the correct path for the live device tree.
|
2017-10-17 23:36:24 +00:00
|
|
|
*
|
2017-10-17 23:36:31 +00:00
|
|
|
* The paths refer to a node in the subtree of a fragment node's "__overlay__"
|
|
|
|
* node, for example "/fragment@0/__overlay__/symbol_path_tail",
|
|
|
|
* where symbol_path_tail can be a single node or it may be a multi-node path.
|
2017-10-17 23:36:24 +00:00
|
|
|
*
|
|
|
|
* The duplicated property value will be modified by replacing the
|
|
|
|
* "/fragment_name/__overlay/" portion of the value with the target
|
|
|
|
* path from the fragment node.
|
|
|
|
*/
|
2017-10-17 23:36:23 +00:00
|
|
|
static struct property *dup_and_fixup_symbol_prop(
|
|
|
|
struct overlay_changeset *ovcs, const struct property *prop)
|
2017-07-19 16:25:22 +00:00
|
|
|
{
|
2017-10-17 23:36:23 +00:00
|
|
|
struct fragment *fragment;
|
2017-10-17 23:36:31 +00:00
|
|
|
struct property *new_prop;
|
|
|
|
struct device_node *fragment_node;
|
|
|
|
struct device_node *overlay_node;
|
|
|
|
const char *path;
|
|
|
|
const char *path_tail;
|
2017-07-19 16:25:22 +00:00
|
|
|
const char *target_path;
|
|
|
|
int k;
|
|
|
|
int overlay_name_len;
|
2017-10-17 23:36:31 +00:00
|
|
|
int path_len;
|
|
|
|
int path_tail_len;
|
2017-07-19 16:25:22 +00:00
|
|
|
int target_path_len;
|
|
|
|
|
|
|
|
if (!prop->value)
|
|
|
|
return NULL;
|
2017-10-17 23:36:31 +00:00
|
|
|
if (strnlen(prop->value, prop->length) >= prop->length)
|
|
|
|
return NULL;
|
|
|
|
path = prop->value;
|
|
|
|
path_len = strlen(path);
|
2017-07-19 16:25:22 +00:00
|
|
|
|
2017-10-17 23:36:31 +00:00
|
|
|
if (path_len < 1)
|
2017-07-19 16:25:22 +00:00
|
|
|
return NULL;
|
2022-04-20 22:25:04 +00:00
|
|
|
fragment_node = __of_find_node_by_path(ovcs->overlay_root, path + 1);
|
2017-10-17 23:36:31 +00:00
|
|
|
overlay_node = __of_find_node_by_path(fragment_node, "__overlay__/");
|
|
|
|
of_node_put(fragment_node);
|
|
|
|
of_node_put(overlay_node);
|
2017-07-19 16:25:22 +00:00
|
|
|
|
2017-10-17 23:36:23 +00:00
|
|
|
for (k = 0; k < ovcs->count; k++) {
|
|
|
|
fragment = &ovcs->fragments[k];
|
2017-10-17 23:36:31 +00:00
|
|
|
if (fragment->overlay == overlay_node)
|
2017-07-19 16:25:22 +00:00
|
|
|
break;
|
|
|
|
}
|
2017-10-17 23:36:23 +00:00
|
|
|
if (k >= ovcs->count)
|
2017-10-17 23:36:31 +00:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
overlay_name_len = snprintf(NULL, 0, "%pOF", fragment->overlay);
|
2017-07-19 16:25:22 +00:00
|
|
|
|
2017-10-17 23:36:31 +00:00
|
|
|
if (overlay_name_len > path_len)
|
|
|
|
return NULL;
|
|
|
|
path_tail = path + overlay_name_len;
|
|
|
|
path_tail_len = strlen(path_tail);
|
|
|
|
|
|
|
|
target_path = kasprintf(GFP_KERNEL, "%pOF", fragment->target);
|
|
|
|
if (!target_path)
|
|
|
|
return NULL;
|
2017-07-19 16:25:22 +00:00
|
|
|
target_path_len = strlen(target_path);
|
|
|
|
|
2017-10-17 23:36:31 +00:00
|
|
|
new_prop = kzalloc(sizeof(*new_prop), GFP_KERNEL);
|
|
|
|
if (!new_prop)
|
|
|
|
goto err_free_target_path;
|
2017-07-19 16:25:22 +00:00
|
|
|
|
2017-10-17 23:36:31 +00:00
|
|
|
new_prop->name = kstrdup(prop->name, GFP_KERNEL);
|
|
|
|
new_prop->length = target_path_len + path_tail_len + 1;
|
|
|
|
new_prop->value = kzalloc(new_prop->length, GFP_KERNEL);
|
|
|
|
if (!new_prop->name || !new_prop->value)
|
|
|
|
goto err_free_new_prop;
|
2017-07-19 16:25:22 +00:00
|
|
|
|
2017-10-17 23:36:31 +00:00
|
|
|
strcpy(new_prop->value, target_path);
|
|
|
|
strcpy(new_prop->value + target_path_len, path_tail);
|
2017-07-19 16:25:22 +00:00
|
|
|
|
2017-10-17 23:36:31 +00:00
|
|
|
of_property_set_flag(new_prop, OF_DYNAMIC);
|
2017-07-19 16:25:22 +00:00
|
|
|
|
2020-04-16 21:42:49 +00:00
|
|
|
kfree(target_path);
|
|
|
|
|
2017-10-17 23:36:31 +00:00
|
|
|
return new_prop;
|
2017-07-19 16:25:22 +00:00
|
|
|
|
2017-10-17 23:36:31 +00:00
|
|
|
err_free_new_prop:
|
2024-04-09 18:59:39 +00:00
|
|
|
__of_prop_free(new_prop);
|
2017-10-17 23:36:31 +00:00
|
|
|
err_free_target_path:
|
|
|
|
kfree(target_path);
|
2017-07-19 16:25:22 +00:00
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2017-10-17 23:36:23 +00:00
|
|
|
/**
|
|
|
|
* add_changeset_property() - add @overlay_prop to overlay changeset
|
|
|
|
* @ovcs: overlay changeset
|
2018-10-05 03:28:08 +00:00
|
|
|
* @target: where @overlay_prop will be placed
|
2017-10-17 23:36:23 +00:00
|
|
|
* @overlay_prop: property to add or update, from overlay tree
|
2017-10-17 23:36:30 +00:00
|
|
|
* @is_symbols_prop: 1 if @overlay_prop is from node "/__symbols__"
|
2017-10-17 23:36:23 +00:00
|
|
|
*
|
2018-10-05 03:28:08 +00:00
|
|
|
* If @overlay_prop does not already exist in live devicetree, add changeset
|
|
|
|
* entry to add @overlay_prop in @target, else add changeset entry to update
|
2017-10-17 23:36:23 +00:00
|
|
|
* value of @overlay_prop.
|
|
|
|
*
|
2018-10-05 03:28:08 +00:00
|
|
|
* @target may be either in the live devicetree or in a new subtree that
|
|
|
|
* is contained in the changeset.
|
|
|
|
*
|
2018-10-05 03:32:04 +00:00
|
|
|
* Some special properties are not added or updated (no error returned):
|
|
|
|
* "name", "phandle", "linux,phandle".
|
|
|
|
*
|
|
|
|
* Properties "#address-cells" and "#size-cells" are not updated if they
|
|
|
|
* are already in the live tree, but if present in the live tree, the values
|
|
|
|
* in the overlay must match the values in the live tree.
|
2017-10-17 23:36:23 +00:00
|
|
|
*
|
2017-10-17 23:36:21 +00:00
|
|
|
* Update of property in symbols node is not allowed.
|
2017-10-17 23:36:23 +00:00
|
|
|
*
|
2021-03-25 16:47:12 +00:00
|
|
|
* Return: 0 on success, -ENOMEM if memory allocation failure, or -EINVAL if
|
2017-10-17 23:36:23 +00:00
|
|
|
* invalid @overlay.
|
2017-10-17 23:36:21 +00:00
|
|
|
*/
|
2017-10-17 23:36:23 +00:00
|
|
|
static int add_changeset_property(struct overlay_changeset *ovcs,
|
2018-10-05 03:28:08 +00:00
|
|
|
struct target *target, struct property *overlay_prop,
|
2017-10-17 23:36:30 +00:00
|
|
|
bool is_symbols_prop)
|
2014-10-28 20:35:58 +00:00
|
|
|
{
|
2017-10-17 23:36:23 +00:00
|
|
|
struct property *new_prop = NULL, *prop;
|
2017-10-16 09:54:32 +00:00
|
|
|
int ret = 0;
|
2014-10-28 20:35:58 +00:00
|
|
|
|
2018-10-13 02:21:16 +00:00
|
|
|
if (target->in_livetree)
|
|
|
|
if (!of_prop_cmp(overlay_prop->name, "name") ||
|
|
|
|
!of_prop_cmp(overlay_prop->name, "phandle") ||
|
|
|
|
!of_prop_cmp(overlay_prop->name, "linux,phandle"))
|
|
|
|
return 0;
|
2014-10-28 20:35:58 +00:00
|
|
|
|
2018-10-05 03:28:08 +00:00
|
|
|
if (target->in_livetree)
|
|
|
|
prop = of_find_property(target->np, overlay_prop->name, NULL);
|
|
|
|
else
|
|
|
|
prop = NULL;
|
|
|
|
|
2019-11-21 19:16:56 +00:00
|
|
|
if (prop) {
|
|
|
|
if (!of_prop_cmp(prop->name, "#address-cells")) {
|
|
|
|
if (!of_prop_val_eq(prop, overlay_prop)) {
|
|
|
|
pr_err("ERROR: changing value of #address-cells is not allowed in %pOF\n",
|
|
|
|
target->np);
|
|
|
|
ret = -EINVAL;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
} else if (!of_prop_cmp(prop->name, "#size-cells")) {
|
|
|
|
if (!of_prop_val_eq(prop, overlay_prop)) {
|
|
|
|
pr_err("ERROR: changing value of #size-cells is not allowed in %pOF\n",
|
|
|
|
target->np);
|
|
|
|
ret = -EINVAL;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-17 23:36:30 +00:00
|
|
|
if (is_symbols_prop) {
|
2017-10-17 23:36:23 +00:00
|
|
|
if (prop)
|
2017-07-19 16:25:22 +00:00
|
|
|
return -EINVAL;
|
2017-10-17 23:36:23 +00:00
|
|
|
new_prop = dup_and_fixup_symbol_prop(ovcs, overlay_prop);
|
2017-07-19 16:25:22 +00:00
|
|
|
} else {
|
2017-10-17 23:36:23 +00:00
|
|
|
new_prop = __of_prop_dup(overlay_prop, GFP_KERNEL);
|
2017-07-19 16:25:22 +00:00
|
|
|
}
|
|
|
|
|
2017-10-17 23:36:23 +00:00
|
|
|
if (!new_prop)
|
2014-10-28 20:35:58 +00:00
|
|
|
return -ENOMEM;
|
|
|
|
|
2018-10-05 03:32:04 +00:00
|
|
|
if (!prop) {
|
2018-10-13 02:21:16 +00:00
|
|
|
if (!target->in_livetree) {
|
|
|
|
new_prop->next = target->np->deadprops;
|
|
|
|
target->np->deadprops = new_prop;
|
|
|
|
}
|
2018-10-05 03:28:08 +00:00
|
|
|
ret = of_changeset_add_property(&ovcs->cset, target->np,
|
2017-10-17 23:36:23 +00:00
|
|
|
new_prop);
|
2018-10-05 03:32:04 +00:00
|
|
|
} else {
|
2018-10-05 03:28:08 +00:00
|
|
|
ret = of_changeset_update_property(&ovcs->cset, target->np,
|
2017-10-17 23:36:23 +00:00
|
|
|
new_prop);
|
2018-10-05 03:32:04 +00:00
|
|
|
}
|
|
|
|
|
2019-11-21 19:16:56 +00:00
|
|
|
if (!of_node_check_flag(target->np, OF_OVERLAY))
|
2018-10-05 03:32:04 +00:00
|
|
|
pr_err("WARNING: memory leak will occur if overlay removed, property: %pOF/%s\n",
|
|
|
|
target->np, new_prop->name);
|
2017-10-16 09:54:32 +00:00
|
|
|
|
2024-04-09 18:59:39 +00:00
|
|
|
if (ret)
|
|
|
|
__of_prop_free(new_prop);
|
2017-10-16 09:54:32 +00:00
|
|
|
return ret;
|
2014-10-28 20:35:58 +00:00
|
|
|
}
|
|
|
|
|
2017-10-17 23:36:23 +00:00
|
|
|
/**
|
|
|
|
* add_changeset_node() - add @node (and children) to overlay changeset
|
2018-10-05 03:28:08 +00:00
|
|
|
* @ovcs: overlay changeset
|
|
|
|
* @target: where @node will be placed in live tree or changeset
|
|
|
|
* @node: node from within overlay device tree fragment
|
2017-10-17 23:36:23 +00:00
|
|
|
*
|
2018-10-05 03:28:08 +00:00
|
|
|
* If @node does not already exist in @target, add changeset entry
|
|
|
|
* to add @node in @target.
|
2017-10-17 23:36:23 +00:00
|
|
|
*
|
2018-10-05 03:28:08 +00:00
|
|
|
* If @node already exists in @target, and the existing node has
|
2017-10-17 23:36:23 +00:00
|
|
|
* a phandle, the overlay node is not allowed to have a phandle.
|
|
|
|
*
|
|
|
|
* If @node has child nodes, add the children recursively via
|
|
|
|
* build_changeset_next_level().
|
|
|
|
*
|
2018-02-26 22:01:23 +00:00
|
|
|
* NOTE_1: A live devicetree created from a flattened device tree (FDT) will
|
|
|
|
* not contain the full path in node->full_name. Thus an overlay
|
|
|
|
* created from an FDT also will not contain the full path in
|
|
|
|
* node->full_name. However, a live devicetree created from Open
|
|
|
|
* Firmware may have the full path in node->full_name.
|
|
|
|
*
|
|
|
|
* add_changeset_node() follows the FDT convention and does not include
|
|
|
|
* the full path in node->full_name. Even though it expects the overlay
|
|
|
|
* to not contain the full path, it uses kbasename() to remove the
|
|
|
|
* full path should it exist. It also uses kbasename() in comparisons
|
|
|
|
* to nodes in the live devicetree so that it can apply an overlay to
|
|
|
|
* a live devicetree created from Open Firmware.
|
|
|
|
*
|
|
|
|
* NOTE_2: Multiple mods of created nodes not supported.
|
2017-10-17 23:36:23 +00:00
|
|
|
*
|
2021-03-25 16:47:12 +00:00
|
|
|
* Return: 0 on success, -ENOMEM if memory allocation failure, or -EINVAL if
|
2017-10-17 23:36:23 +00:00
|
|
|
* invalid @overlay.
|
|
|
|
*/
|
|
|
|
static int add_changeset_node(struct overlay_changeset *ovcs,
|
2018-10-05 03:28:08 +00:00
|
|
|
struct target *target, struct device_node *node)
|
2014-10-28 20:35:58 +00:00
|
|
|
{
|
2017-10-17 23:36:23 +00:00
|
|
|
const char *node_kbasename;
|
2018-10-13 02:21:16 +00:00
|
|
|
const __be32 *phandle;
|
2015-03-03 13:04:45 +00:00
|
|
|
struct device_node *tchild;
|
2018-10-05 03:28:08 +00:00
|
|
|
struct target target_child;
|
2018-10-13 02:21:16 +00:00
|
|
|
int ret = 0, size;
|
2014-10-28 20:35:58 +00:00
|
|
|
|
2017-10-17 23:36:23 +00:00
|
|
|
node_kbasename = kbasename(node->full_name);
|
2014-10-28 20:35:58 +00:00
|
|
|
|
2018-10-05 03:28:08 +00:00
|
|
|
for_each_child_of_node(target->np, tchild)
|
2017-10-17 23:36:23 +00:00
|
|
|
if (!of_node_cmp(node_kbasename, kbasename(tchild->full_name)))
|
2017-07-19 16:25:21 +00:00
|
|
|
break;
|
|
|
|
|
2017-10-17 23:36:25 +00:00
|
|
|
if (!tchild) {
|
2018-10-05 03:29:01 +00:00
|
|
|
tchild = __of_node_dup(NULL, node_kbasename);
|
2014-10-28 20:35:58 +00:00
|
|
|
if (!tchild)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
2018-10-05 03:28:08 +00:00
|
|
|
tchild->parent = target->np;
|
2018-10-13 02:21:16 +00:00
|
|
|
tchild->name = __of_get_property(node, "name", NULL);
|
|
|
|
|
|
|
|
if (!tchild->name)
|
|
|
|
tchild->name = "<NULL>";
|
|
|
|
|
|
|
|
/* ignore obsolete "linux,phandle" */
|
|
|
|
phandle = __of_get_property(node, "phandle", &size);
|
|
|
|
if (phandle && (size == 4))
|
|
|
|
tchild->phandle = be32_to_cpup(phandle);
|
|
|
|
|
2018-10-05 03:24:17 +00:00
|
|
|
of_node_set_flag(tchild, OF_OVERLAY);
|
2014-10-28 20:35:58 +00:00
|
|
|
|
2017-10-17 23:36:23 +00:00
|
|
|
ret = of_changeset_attach_node(&ovcs->cset, tchild);
|
2014-10-28 20:35:58 +00:00
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
2018-10-05 03:28:08 +00:00
|
|
|
target_child.np = tchild;
|
|
|
|
target_child.in_livetree = false;
|
|
|
|
|
|
|
|
ret = build_changeset_next_level(ovcs, &target_child, node);
|
2018-10-05 03:25:13 +00:00
|
|
|
of_node_put(tchild);
|
|
|
|
return ret;
|
2014-10-28 20:35:58 +00:00
|
|
|
}
|
|
|
|
|
2018-10-05 03:28:08 +00:00
|
|
|
if (node->phandle && tchild->phandle) {
|
2017-10-17 23:36:28 +00:00
|
|
|
ret = -EINVAL;
|
2018-10-05 03:28:08 +00:00
|
|
|
} else {
|
|
|
|
target_child.np = tchild;
|
|
|
|
target_child.in_livetree = target->in_livetree;
|
|
|
|
ret = build_changeset_next_level(ovcs, &target_child, node);
|
|
|
|
}
|
2017-10-17 23:36:25 +00:00
|
|
|
of_node_put(tchild);
|
|
|
|
|
2014-10-28 20:35:58 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-10-17 23:36:23 +00:00
|
|
|
/**
|
|
|
|
* build_changeset_next_level() - add level of overlay changeset
|
|
|
|
* @ovcs: overlay changeset
|
2018-10-05 03:28:08 +00:00
|
|
|
* @target: where to place @overlay_node in live tree
|
2017-10-17 23:36:23 +00:00
|
|
|
* @overlay_node: node from within an overlay device tree fragment
|
2014-10-28 20:35:58 +00:00
|
|
|
*
|
2017-10-17 23:36:23 +00:00
|
|
|
* Add the properties (if any) and nodes (if any) from @overlay_node to the
|
|
|
|
* @ovcs->cset changeset. If an added node has child nodes, they will
|
|
|
|
* be added recursively.
|
2017-10-17 23:36:21 +00:00
|
|
|
*
|
|
|
|
* Do not allow symbols node to have any children.
|
2017-10-17 23:36:23 +00:00
|
|
|
*
|
2021-03-25 16:47:12 +00:00
|
|
|
* Return: 0 on success, -ENOMEM if memory allocation failure, or -EINVAL if
|
2017-10-17 23:36:23 +00:00
|
|
|
* invalid @overlay_node.
|
2014-10-28 20:35:58 +00:00
|
|
|
*/
|
2017-10-17 23:36:23 +00:00
|
|
|
static int build_changeset_next_level(struct overlay_changeset *ovcs,
|
2018-10-05 03:28:08 +00:00
|
|
|
struct target *target, const struct device_node *overlay_node)
|
2014-10-28 20:35:58 +00:00
|
|
|
{
|
|
|
|
struct property *prop;
|
|
|
|
int ret;
|
|
|
|
|
2017-10-17 23:36:23 +00:00
|
|
|
for_each_property_of_node(overlay_node, prop) {
|
2018-10-05 03:28:08 +00:00
|
|
|
ret = add_changeset_property(ovcs, target, prop, 0);
|
2014-10-28 20:35:58 +00:00
|
|
|
if (ret) {
|
2017-10-17 23:36:26 +00:00
|
|
|
pr_debug("Failed to apply prop @%pOF/%s, err=%d\n",
|
2018-10-05 03:28:08 +00:00
|
|
|
target->np, prop->name, ret);
|
2014-10-28 20:35:58 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-26 06:24:06 +00:00
|
|
|
for_each_child_of_node_scoped(overlay_node, child) {
|
2018-10-05 03:28:08 +00:00
|
|
|
ret = add_changeset_node(ovcs, target, child);
|
2017-10-17 23:36:22 +00:00
|
|
|
if (ret) {
|
2018-08-28 01:00:19 +00:00
|
|
|
pr_debug("Failed to apply node @%pOF/%pOFn, err=%d\n",
|
2018-10-05 03:28:08 +00:00
|
|
|
target->np, child, ret);
|
2014-10-28 20:35:58 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-10-17 23:36:30 +00:00
|
|
|
/*
|
|
|
|
* Add the properties from __overlay__ node to the @ovcs->cset changeset.
|
|
|
|
*/
|
|
|
|
static int build_changeset_symbols_node(struct overlay_changeset *ovcs,
|
2018-10-05 03:28:08 +00:00
|
|
|
struct target *target,
|
2017-10-17 23:36:30 +00:00
|
|
|
const struct device_node *overlay_symbols_node)
|
|
|
|
{
|
|
|
|
struct property *prop;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
for_each_property_of_node(overlay_symbols_node, prop) {
|
2018-10-05 03:28:08 +00:00
|
|
|
ret = add_changeset_property(ovcs, target, prop, 1);
|
2017-10-17 23:36:30 +00:00
|
|
|
if (ret) {
|
2018-10-05 03:33:35 +00:00
|
|
|
pr_debug("Failed to apply symbols prop @%pOF/%s, err=%d\n",
|
2018-10-05 03:28:08 +00:00
|
|
|
target->np, prop->name, ret);
|
2017-10-17 23:36:30 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
of: overlay: check prevents multiple fragments touching same property
Add test case of two fragments updating the same property. After
adding the test case, the system hangs at end of boot, after
after slub stack dumps from kfree() in crypto modprobe code.
Multiple overlay fragments adding, modifying, or deleting the same
property is not supported. Add check to detect the attempt and fail
the overlay apply.
Before this patch, the first fragment error would terminate
processing. Allow fragment checking to proceed and report all
of the fragment errors before terminating the overlay apply. This
is not a hot path, thus not a performance issue (the error is not
transient and requires fixing the overlay before attempting to
apply it again).
After applying this patch, the devicetree unittest messages will
include:
OF: overlay: ERROR: multiple fragments add, update, and/or delete property /testcase-data-2/substation@100/motor-1/rpm_avail
...
### dt-test ### end of unittest - 212 passed, 0 failed
The check to detect two fragments updating the same property is
folded into the patch that created the test case to maintain
bisectability.
Tested-by: Alan Tull <atull@kernel.org>
Signed-off-by: Frank Rowand <frank.rowand@sony.com>
2018-10-05 03:36:18 +00:00
|
|
|
static int find_dup_cset_node_entry(struct overlay_changeset *ovcs,
|
|
|
|
struct of_changeset_entry *ce_1)
|
|
|
|
{
|
|
|
|
struct of_changeset_entry *ce_2;
|
|
|
|
char *fn_1, *fn_2;
|
|
|
|
int node_path_match;
|
|
|
|
|
|
|
|
if (ce_1->action != OF_RECONFIG_ATTACH_NODE &&
|
|
|
|
ce_1->action != OF_RECONFIG_DETACH_NODE)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
ce_2 = ce_1;
|
|
|
|
list_for_each_entry_continue(ce_2, &ovcs->cset.entries, node) {
|
|
|
|
if ((ce_2->action != OF_RECONFIG_ATTACH_NODE &&
|
|
|
|
ce_2->action != OF_RECONFIG_DETACH_NODE) ||
|
|
|
|
of_node_cmp(ce_1->np->full_name, ce_2->np->full_name))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
fn_1 = kasprintf(GFP_KERNEL, "%pOF", ce_1->np);
|
|
|
|
fn_2 = kasprintf(GFP_KERNEL, "%pOF", ce_2->np);
|
2022-12-11 02:33:37 +00:00
|
|
|
node_path_match = !fn_1 || !fn_2 || !strcmp(fn_1, fn_2);
|
of: overlay: check prevents multiple fragments touching same property
Add test case of two fragments updating the same property. After
adding the test case, the system hangs at end of boot, after
after slub stack dumps from kfree() in crypto modprobe code.
Multiple overlay fragments adding, modifying, or deleting the same
property is not supported. Add check to detect the attempt and fail
the overlay apply.
Before this patch, the first fragment error would terminate
processing. Allow fragment checking to proceed and report all
of the fragment errors before terminating the overlay apply. This
is not a hot path, thus not a performance issue (the error is not
transient and requires fixing the overlay before attempting to
apply it again).
After applying this patch, the devicetree unittest messages will
include:
OF: overlay: ERROR: multiple fragments add, update, and/or delete property /testcase-data-2/substation@100/motor-1/rpm_avail
...
### dt-test ### end of unittest - 212 passed, 0 failed
The check to detect two fragments updating the same property is
folded into the patch that created the test case to maintain
bisectability.
Tested-by: Alan Tull <atull@kernel.org>
Signed-off-by: Frank Rowand <frank.rowand@sony.com>
2018-10-05 03:36:18 +00:00
|
|
|
kfree(fn_1);
|
|
|
|
kfree(fn_2);
|
|
|
|
if (node_path_match) {
|
|
|
|
pr_err("ERROR: multiple fragments add and/or delete node %pOF\n",
|
|
|
|
ce_1->np);
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int find_dup_cset_prop(struct overlay_changeset *ovcs,
|
|
|
|
struct of_changeset_entry *ce_1)
|
|
|
|
{
|
|
|
|
struct of_changeset_entry *ce_2;
|
|
|
|
char *fn_1, *fn_2;
|
|
|
|
int node_path_match;
|
|
|
|
|
|
|
|
if (ce_1->action != OF_RECONFIG_ADD_PROPERTY &&
|
|
|
|
ce_1->action != OF_RECONFIG_REMOVE_PROPERTY &&
|
|
|
|
ce_1->action != OF_RECONFIG_UPDATE_PROPERTY)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
ce_2 = ce_1;
|
|
|
|
list_for_each_entry_continue(ce_2, &ovcs->cset.entries, node) {
|
|
|
|
if ((ce_2->action != OF_RECONFIG_ADD_PROPERTY &&
|
|
|
|
ce_2->action != OF_RECONFIG_REMOVE_PROPERTY &&
|
|
|
|
ce_2->action != OF_RECONFIG_UPDATE_PROPERTY) ||
|
|
|
|
of_node_cmp(ce_1->np->full_name, ce_2->np->full_name))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
fn_1 = kasprintf(GFP_KERNEL, "%pOF", ce_1->np);
|
|
|
|
fn_2 = kasprintf(GFP_KERNEL, "%pOF", ce_2->np);
|
2022-12-11 02:33:37 +00:00
|
|
|
node_path_match = !fn_1 || !fn_2 || !strcmp(fn_1, fn_2);
|
of: overlay: check prevents multiple fragments touching same property
Add test case of two fragments updating the same property. After
adding the test case, the system hangs at end of boot, after
after slub stack dumps from kfree() in crypto modprobe code.
Multiple overlay fragments adding, modifying, or deleting the same
property is not supported. Add check to detect the attempt and fail
the overlay apply.
Before this patch, the first fragment error would terminate
processing. Allow fragment checking to proceed and report all
of the fragment errors before terminating the overlay apply. This
is not a hot path, thus not a performance issue (the error is not
transient and requires fixing the overlay before attempting to
apply it again).
After applying this patch, the devicetree unittest messages will
include:
OF: overlay: ERROR: multiple fragments add, update, and/or delete property /testcase-data-2/substation@100/motor-1/rpm_avail
...
### dt-test ### end of unittest - 212 passed, 0 failed
The check to detect two fragments updating the same property is
folded into the patch that created the test case to maintain
bisectability.
Tested-by: Alan Tull <atull@kernel.org>
Signed-off-by: Frank Rowand <frank.rowand@sony.com>
2018-10-05 03:36:18 +00:00
|
|
|
kfree(fn_1);
|
|
|
|
kfree(fn_2);
|
|
|
|
if (node_path_match &&
|
|
|
|
!of_prop_cmp(ce_1->prop->name, ce_2->prop->name)) {
|
|
|
|
pr_err("ERROR: multiple fragments add, update, and/or delete property %pOF/%s\n",
|
|
|
|
ce_1->np, ce_1->prop->name);
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-10-05 03:35:14 +00:00
|
|
|
/**
|
of: overlay: check prevents multiple fragments touching same property
Add test case of two fragments updating the same property. After
adding the test case, the system hangs at end of boot, after
after slub stack dumps from kfree() in crypto modprobe code.
Multiple overlay fragments adding, modifying, or deleting the same
property is not supported. Add check to detect the attempt and fail
the overlay apply.
Before this patch, the first fragment error would terminate
processing. Allow fragment checking to proceed and report all
of the fragment errors before terminating the overlay apply. This
is not a hot path, thus not a performance issue (the error is not
transient and requires fixing the overlay before attempting to
apply it again).
After applying this patch, the devicetree unittest messages will
include:
OF: overlay: ERROR: multiple fragments add, update, and/or delete property /testcase-data-2/substation@100/motor-1/rpm_avail
...
### dt-test ### end of unittest - 212 passed, 0 failed
The check to detect two fragments updating the same property is
folded into the patch that created the test case to maintain
bisectability.
Tested-by: Alan Tull <atull@kernel.org>
Signed-off-by: Frank Rowand <frank.rowand@sony.com>
2018-10-05 03:36:18 +00:00
|
|
|
* changeset_dup_entry_check() - check for duplicate entries
|
2018-10-05 03:35:14 +00:00
|
|
|
* @ovcs: Overlay changeset
|
|
|
|
*
|
of: overlay: check prevents multiple fragments touching same property
Add test case of two fragments updating the same property. After
adding the test case, the system hangs at end of boot, after
after slub stack dumps from kfree() in crypto modprobe code.
Multiple overlay fragments adding, modifying, or deleting the same
property is not supported. Add check to detect the attempt and fail
the overlay apply.
Before this patch, the first fragment error would terminate
processing. Allow fragment checking to proceed and report all
of the fragment errors before terminating the overlay apply. This
is not a hot path, thus not a performance issue (the error is not
transient and requires fixing the overlay before attempting to
apply it again).
After applying this patch, the devicetree unittest messages will
include:
OF: overlay: ERROR: multiple fragments add, update, and/or delete property /testcase-data-2/substation@100/motor-1/rpm_avail
...
### dt-test ### end of unittest - 212 passed, 0 failed
The check to detect two fragments updating the same property is
folded into the patch that created the test case to maintain
bisectability.
Tested-by: Alan Tull <atull@kernel.org>
Signed-off-by: Frank Rowand <frank.rowand@sony.com>
2018-10-05 03:36:18 +00:00
|
|
|
* Check changeset @ovcs->cset for multiple {add or delete} node entries for
|
|
|
|
* the same node or duplicate {add, delete, or update} properties entries
|
|
|
|
* for the same property.
|
2018-10-05 03:35:14 +00:00
|
|
|
*
|
2021-03-25 16:47:12 +00:00
|
|
|
* Return: 0 on success, or -EINVAL if duplicate changeset entry found.
|
2018-10-05 03:35:14 +00:00
|
|
|
*/
|
of: overlay: check prevents multiple fragments touching same property
Add test case of two fragments updating the same property. After
adding the test case, the system hangs at end of boot, after
after slub stack dumps from kfree() in crypto modprobe code.
Multiple overlay fragments adding, modifying, or deleting the same
property is not supported. Add check to detect the attempt and fail
the overlay apply.
Before this patch, the first fragment error would terminate
processing. Allow fragment checking to proceed and report all
of the fragment errors before terminating the overlay apply. This
is not a hot path, thus not a performance issue (the error is not
transient and requires fixing the overlay before attempting to
apply it again).
After applying this patch, the devicetree unittest messages will
include:
OF: overlay: ERROR: multiple fragments add, update, and/or delete property /testcase-data-2/substation@100/motor-1/rpm_avail
...
### dt-test ### end of unittest - 212 passed, 0 failed
The check to detect two fragments updating the same property is
folded into the patch that created the test case to maintain
bisectability.
Tested-by: Alan Tull <atull@kernel.org>
Signed-off-by: Frank Rowand <frank.rowand@sony.com>
2018-10-05 03:36:18 +00:00
|
|
|
static int changeset_dup_entry_check(struct overlay_changeset *ovcs)
|
2018-10-05 03:35:14 +00:00
|
|
|
{
|
of: overlay: check prevents multiple fragments touching same property
Add test case of two fragments updating the same property. After
adding the test case, the system hangs at end of boot, after
after slub stack dumps from kfree() in crypto modprobe code.
Multiple overlay fragments adding, modifying, or deleting the same
property is not supported. Add check to detect the attempt and fail
the overlay apply.
Before this patch, the first fragment error would terminate
processing. Allow fragment checking to proceed and report all
of the fragment errors before terminating the overlay apply. This
is not a hot path, thus not a performance issue (the error is not
transient and requires fixing the overlay before attempting to
apply it again).
After applying this patch, the devicetree unittest messages will
include:
OF: overlay: ERROR: multiple fragments add, update, and/or delete property /testcase-data-2/substation@100/motor-1/rpm_avail
...
### dt-test ### end of unittest - 212 passed, 0 failed
The check to detect two fragments updating the same property is
folded into the patch that created the test case to maintain
bisectability.
Tested-by: Alan Tull <atull@kernel.org>
Signed-off-by: Frank Rowand <frank.rowand@sony.com>
2018-10-05 03:36:18 +00:00
|
|
|
struct of_changeset_entry *ce_1;
|
|
|
|
int dup_entry = 0;
|
2018-10-05 03:35:14 +00:00
|
|
|
|
|
|
|
list_for_each_entry(ce_1, &ovcs->cset.entries, node) {
|
of: overlay: check prevents multiple fragments touching same property
Add test case of two fragments updating the same property. After
adding the test case, the system hangs at end of boot, after
after slub stack dumps from kfree() in crypto modprobe code.
Multiple overlay fragments adding, modifying, or deleting the same
property is not supported. Add check to detect the attempt and fail
the overlay apply.
Before this patch, the first fragment error would terminate
processing. Allow fragment checking to proceed and report all
of the fragment errors before terminating the overlay apply. This
is not a hot path, thus not a performance issue (the error is not
transient and requires fixing the overlay before attempting to
apply it again).
After applying this patch, the devicetree unittest messages will
include:
OF: overlay: ERROR: multiple fragments add, update, and/or delete property /testcase-data-2/substation@100/motor-1/rpm_avail
...
### dt-test ### end of unittest - 212 passed, 0 failed
The check to detect two fragments updating the same property is
folded into the patch that created the test case to maintain
bisectability.
Tested-by: Alan Tull <atull@kernel.org>
Signed-off-by: Frank Rowand <frank.rowand@sony.com>
2018-10-05 03:36:18 +00:00
|
|
|
dup_entry |= find_dup_cset_node_entry(ovcs, ce_1);
|
|
|
|
dup_entry |= find_dup_cset_prop(ovcs, ce_1);
|
2018-10-05 03:35:14 +00:00
|
|
|
}
|
|
|
|
|
of: overlay: check prevents multiple fragments touching same property
Add test case of two fragments updating the same property. After
adding the test case, the system hangs at end of boot, after
after slub stack dumps from kfree() in crypto modprobe code.
Multiple overlay fragments adding, modifying, or deleting the same
property is not supported. Add check to detect the attempt and fail
the overlay apply.
Before this patch, the first fragment error would terminate
processing. Allow fragment checking to proceed and report all
of the fragment errors before terminating the overlay apply. This
is not a hot path, thus not a performance issue (the error is not
transient and requires fixing the overlay before attempting to
apply it again).
After applying this patch, the devicetree unittest messages will
include:
OF: overlay: ERROR: multiple fragments add, update, and/or delete property /testcase-data-2/substation@100/motor-1/rpm_avail
...
### dt-test ### end of unittest - 212 passed, 0 failed
The check to detect two fragments updating the same property is
folded into the patch that created the test case to maintain
bisectability.
Tested-by: Alan Tull <atull@kernel.org>
Signed-off-by: Frank Rowand <frank.rowand@sony.com>
2018-10-05 03:36:18 +00:00
|
|
|
return dup_entry ? -EINVAL : 0;
|
2018-10-05 03:35:14 +00:00
|
|
|
}
|
|
|
|
|
2014-10-28 20:35:58 +00:00
|
|
|
/**
|
2017-10-17 23:36:23 +00:00
|
|
|
* build_changeset() - populate overlay changeset in @ovcs from @ovcs->fragments
|
|
|
|
* @ovcs: Overlay changeset
|
2014-10-28 20:35:58 +00:00
|
|
|
*
|
2017-10-17 23:36:23 +00:00
|
|
|
* Create changeset @ovcs->cset to contain the nodes and properties of the
|
|
|
|
* overlay device tree fragments in @ovcs->fragments[]. If an error occurs,
|
|
|
|
* any portions of the changeset that were successfully created will remain
|
|
|
|
* in @ovcs->cset.
|
|
|
|
*
|
2021-03-25 16:47:12 +00:00
|
|
|
* Return: 0 on success, -ENOMEM if memory allocation failure, or -EINVAL if
|
2017-10-17 23:36:23 +00:00
|
|
|
* invalid overlay in @ovcs->fragments[].
|
2014-10-28 20:35:58 +00:00
|
|
|
*/
|
2017-10-17 23:36:23 +00:00
|
|
|
static int build_changeset(struct overlay_changeset *ovcs)
|
2014-10-28 20:35:58 +00:00
|
|
|
{
|
2017-10-17 23:36:30 +00:00
|
|
|
struct fragment *fragment;
|
2018-10-05 03:28:08 +00:00
|
|
|
struct target target;
|
2017-10-17 23:36:30 +00:00
|
|
|
int fragments_count, i, ret;
|
2014-10-28 20:35:58 +00:00
|
|
|
|
2017-10-17 23:36:30 +00:00
|
|
|
/*
|
|
|
|
* if there is a symbols fragment in ovcs->fragments[i] it is
|
|
|
|
* the final element in the array
|
|
|
|
*/
|
|
|
|
if (ovcs->symbols_fragment)
|
|
|
|
fragments_count = ovcs->count - 1;
|
|
|
|
else
|
|
|
|
fragments_count = ovcs->count;
|
|
|
|
|
|
|
|
for (i = 0; i < fragments_count; i++) {
|
|
|
|
fragment = &ovcs->fragments[i];
|
2014-10-28 20:35:58 +00:00
|
|
|
|
2018-10-05 03:28:08 +00:00
|
|
|
target.np = fragment->target;
|
|
|
|
target.in_livetree = true;
|
|
|
|
ret = build_changeset_next_level(ovcs, &target,
|
2017-10-17 23:36:30 +00:00
|
|
|
fragment->overlay);
|
|
|
|
if (ret) {
|
2018-10-05 03:33:35 +00:00
|
|
|
pr_debug("fragment apply failed '%pOF'\n",
|
|
|
|
fragment->target);
|
2017-10-17 23:36:30 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ovcs->symbols_fragment) {
|
|
|
|
fragment = &ovcs->fragments[ovcs->count - 1];
|
2018-10-05 03:28:08 +00:00
|
|
|
|
|
|
|
target.np = fragment->target;
|
|
|
|
target.in_livetree = true;
|
|
|
|
ret = build_changeset_symbols_node(ovcs, &target,
|
2017-10-17 23:36:30 +00:00
|
|
|
fragment->overlay);
|
2017-10-17 23:36:23 +00:00
|
|
|
if (ret) {
|
2018-10-05 03:33:35 +00:00
|
|
|
pr_debug("symbols fragment apply failed '%pOF'\n",
|
|
|
|
fragment->target);
|
2017-10-17 23:36:23 +00:00
|
|
|
return ret;
|
2014-10-28 20:35:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
of: overlay: check prevents multiple fragments touching same property
Add test case of two fragments updating the same property. After
adding the test case, the system hangs at end of boot, after
after slub stack dumps from kfree() in crypto modprobe code.
Multiple overlay fragments adding, modifying, or deleting the same
property is not supported. Add check to detect the attempt and fail
the overlay apply.
Before this patch, the first fragment error would terminate
processing. Allow fragment checking to proceed and report all
of the fragment errors before terminating the overlay apply. This
is not a hot path, thus not a performance issue (the error is not
transient and requires fixing the overlay before attempting to
apply it again).
After applying this patch, the devicetree unittest messages will
include:
OF: overlay: ERROR: multiple fragments add, update, and/or delete property /testcase-data-2/substation@100/motor-1/rpm_avail
...
### dt-test ### end of unittest - 212 passed, 0 failed
The check to detect two fragments updating the same property is
folded into the patch that created the test case to maintain
bisectability.
Tested-by: Alan Tull <atull@kernel.org>
Signed-off-by: Frank Rowand <frank.rowand@sony.com>
2018-10-05 03:36:18 +00:00
|
|
|
return changeset_dup_entry_check(ovcs);
|
2014-10-28 20:35:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Find the target node using a number of different strategies
|
2017-10-17 23:36:21 +00:00
|
|
|
* in order of preference:
|
2014-10-28 20:35:58 +00:00
|
|
|
*
|
2017-10-17 23:36:21 +00:00
|
|
|
* 1) "target" property containing the phandle of the target
|
|
|
|
* 2) "target-path" property containing the path of the target
|
2014-10-28 20:35:58 +00:00
|
|
|
*/
|
2023-08-15 17:19:59 +00:00
|
|
|
static struct device_node *find_target(struct device_node *info_node,
|
|
|
|
struct device_node *target_base)
|
2014-10-28 20:35:58 +00:00
|
|
|
{
|
2018-02-12 08:25:04 +00:00
|
|
|
struct device_node *node;
|
2023-08-15 17:19:59 +00:00
|
|
|
char *target_path;
|
2014-10-28 20:35:58 +00:00
|
|
|
const char *path;
|
|
|
|
u32 val;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = of_property_read_u32(info_node, "target", &val);
|
2018-02-12 08:25:04 +00:00
|
|
|
if (!ret) {
|
|
|
|
node = of_find_node_by_phandle(val);
|
|
|
|
if (!node)
|
|
|
|
pr_err("find target, node: %pOF, phandle 0x%x not found\n",
|
|
|
|
info_node, val);
|
|
|
|
return node;
|
|
|
|
}
|
2014-10-28 20:35:58 +00:00
|
|
|
|
|
|
|
ret = of_property_read_string(info_node, "target-path", &path);
|
2018-02-12 08:25:04 +00:00
|
|
|
if (!ret) {
|
2023-08-15 17:19:59 +00:00
|
|
|
if (target_base) {
|
|
|
|
target_path = kasprintf(GFP_KERNEL, "%pOF%s", target_base, path);
|
|
|
|
if (!target_path)
|
|
|
|
return NULL;
|
|
|
|
node = of_find_node_by_path(target_path);
|
|
|
|
if (!node) {
|
|
|
|
pr_err("find target, node: %pOF, path '%s' not found\n",
|
|
|
|
info_node, target_path);
|
|
|
|
}
|
|
|
|
kfree(target_path);
|
|
|
|
} else {
|
|
|
|
node = of_find_node_by_path(path);
|
|
|
|
if (!node) {
|
|
|
|
pr_err("find target, node: %pOF, path '%s' not found\n",
|
|
|
|
info_node, path);
|
|
|
|
}
|
|
|
|
}
|
2018-02-12 08:25:04 +00:00
|
|
|
return node;
|
|
|
|
}
|
2014-10-28 20:35:58 +00:00
|
|
|
|
2018-02-12 08:25:04 +00:00
|
|
|
pr_err("find target, node: %pOF, no target property\n", info_node);
|
2014-10-28 20:35:58 +00:00
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-10-17 23:36:23 +00:00
|
|
|
* init_overlay_changeset() - initialize overlay changeset from overlay tree
|
2022-04-20 22:25:04 +00:00
|
|
|
* @ovcs: Overlay changeset to build
|
2023-08-15 17:19:59 +00:00
|
|
|
* @target_base: Point to the target node to apply overlay
|
2014-10-28 20:35:58 +00:00
|
|
|
*
|
2017-10-17 23:36:23 +00:00
|
|
|
* Initialize @ovcs. Populate @ovcs->fragments with node information from
|
2022-04-20 22:25:04 +00:00
|
|
|
* the top level of @overlay_root. The relevant top level nodes are the
|
|
|
|
* fragment nodes and the __symbols__ node. Any other top level node will
|
2022-04-20 22:25:05 +00:00
|
|
|
* be ignored. Populate other @ovcs fields.
|
2014-10-28 20:35:58 +00:00
|
|
|
*
|
2021-03-25 16:47:12 +00:00
|
|
|
* Return: 0 on success, -ENOMEM if memory allocation failure, -EINVAL if error
|
2022-04-20 22:25:05 +00:00
|
|
|
* detected in @overlay_root. On error return, the caller of
|
|
|
|
* init_overlay_changeset() must call free_overlay_changeset().
|
2014-10-28 20:35:58 +00:00
|
|
|
*/
|
2023-08-15 17:19:59 +00:00
|
|
|
static int init_overlay_changeset(struct overlay_changeset *ovcs,
|
|
|
|
struct device_node *target_base)
|
2014-10-28 20:35:58 +00:00
|
|
|
{
|
2017-10-17 23:36:25 +00:00
|
|
|
struct device_node *node, *overlay_node;
|
2017-10-17 23:36:23 +00:00
|
|
|
struct fragment *fragment;
|
|
|
|
struct fragment *fragments;
|
2022-04-20 22:25:05 +00:00
|
|
|
int cnt, ret;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* None of the resources allocated by this function will be freed in
|
|
|
|
* the error paths. Instead the caller of this function is required
|
|
|
|
* to call free_overlay_changeset() (which will free the resources)
|
|
|
|
* if error return.
|
|
|
|
*/
|
2014-10-28 20:35:58 +00:00
|
|
|
|
2017-10-17 23:36:26 +00:00
|
|
|
/*
|
|
|
|
* Warn for some issues. Can not return -EINVAL for these until
|
|
|
|
* of_unittest_apply_overlay() is fixed to pass these checks.
|
|
|
|
*/
|
2022-04-20 22:25:05 +00:00
|
|
|
if (!of_node_check_flag(ovcs->overlay_root, OF_DYNAMIC))
|
|
|
|
pr_debug("%s() ovcs->overlay_root is not dynamic\n", __func__);
|
2017-10-17 23:36:26 +00:00
|
|
|
|
2022-04-20 22:25:05 +00:00
|
|
|
if (!of_node_check_flag(ovcs->overlay_root, OF_DETACHED))
|
|
|
|
pr_debug("%s() ovcs->overlay_root is not detached\n", __func__);
|
2017-10-17 23:36:26 +00:00
|
|
|
|
2022-04-20 22:25:05 +00:00
|
|
|
if (!of_node_is_root(ovcs->overlay_root))
|
|
|
|
pr_debug("%s() ovcs->overlay_root is not root\n", __func__);
|
2017-10-17 23:36:25 +00:00
|
|
|
|
2014-10-28 20:35:58 +00:00
|
|
|
cnt = 0;
|
|
|
|
|
2017-10-17 23:36:25 +00:00
|
|
|
/* fragment nodes */
|
2022-04-20 22:25:05 +00:00
|
|
|
for_each_child_of_node(ovcs->overlay_root, node) {
|
2017-10-17 23:36:25 +00:00
|
|
|
overlay_node = of_get_child_by_name(node, "__overlay__");
|
|
|
|
if (overlay_node) {
|
|
|
|
cnt++;
|
|
|
|
of_node_put(overlay_node);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-20 22:25:05 +00:00
|
|
|
node = of_get_child_by_name(ovcs->overlay_root, "__symbols__");
|
2017-10-17 23:36:25 +00:00
|
|
|
if (node) {
|
2017-07-19 16:25:22 +00:00
|
|
|
cnt++;
|
2017-10-17 23:36:25 +00:00
|
|
|
of_node_put(node);
|
|
|
|
}
|
2017-07-19 16:25:22 +00:00
|
|
|
|
2017-10-17 23:36:23 +00:00
|
|
|
fragments = kcalloc(cnt, sizeof(*fragments), GFP_KERNEL);
|
2017-10-17 23:36:25 +00:00
|
|
|
if (!fragments) {
|
|
|
|
ret = -ENOMEM;
|
2022-04-20 22:25:05 +00:00
|
|
|
goto err_out;
|
2017-10-17 23:36:25 +00:00
|
|
|
}
|
2022-04-20 22:25:05 +00:00
|
|
|
ovcs->fragments = fragments;
|
2014-10-28 20:35:58 +00:00
|
|
|
|
|
|
|
cnt = 0;
|
2022-04-20 22:25:05 +00:00
|
|
|
for_each_child_of_node(ovcs->overlay_root, node) {
|
2017-12-08 13:13:02 +00:00
|
|
|
overlay_node = of_get_child_by_name(node, "__overlay__");
|
2017-12-08 13:13:03 +00:00
|
|
|
if (!overlay_node)
|
|
|
|
continue;
|
2017-11-28 08:26:33 +00:00
|
|
|
|
2017-12-08 13:13:03 +00:00
|
|
|
fragment = &fragments[cnt];
|
|
|
|
fragment->overlay = overlay_node;
|
2023-08-15 17:19:59 +00:00
|
|
|
fragment->target = find_target(node, target_base);
|
2017-12-08 13:13:03 +00:00
|
|
|
if (!fragment->target) {
|
|
|
|
of_node_put(fragment->overlay);
|
|
|
|
ret = -EINVAL;
|
2021-03-22 18:21:39 +00:00
|
|
|
of_node_put(node);
|
2022-04-20 22:25:05 +00:00
|
|
|
goto err_out;
|
2017-10-17 23:36:25 +00:00
|
|
|
}
|
2017-12-08 13:13:03 +00:00
|
|
|
|
|
|
|
cnt++;
|
2014-10-28 20:35:58 +00:00
|
|
|
}
|
|
|
|
|
2017-10-17 23:36:30 +00:00
|
|
|
/*
|
|
|
|
* if there is a symbols fragment in ovcs->fragments[i] it is
|
|
|
|
* the final element in the array
|
|
|
|
*/
|
2022-04-20 22:25:05 +00:00
|
|
|
node = of_get_child_by_name(ovcs->overlay_root, "__symbols__");
|
2017-07-19 16:25:22 +00:00
|
|
|
if (node) {
|
2017-10-17 23:36:30 +00:00
|
|
|
ovcs->symbols_fragment = 1;
|
2017-10-17 23:36:23 +00:00
|
|
|
fragment = &fragments[cnt];
|
|
|
|
fragment->overlay = node;
|
|
|
|
fragment->target = of_find_node_by_path("/__symbols__");
|
2017-07-19 16:25:22 +00:00
|
|
|
|
2017-10-17 23:36:23 +00:00
|
|
|
if (!fragment->target) {
|
2017-10-19 21:38:11 +00:00
|
|
|
pr_err("symbols in overlay, but not in live tree\n");
|
2017-10-17 23:36:25 +00:00
|
|
|
ret = -EINVAL;
|
2023-06-02 02:05:02 +00:00
|
|
|
of_node_put(node);
|
2022-04-20 22:25:05 +00:00
|
|
|
goto err_out;
|
2017-07-19 16:25:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
cnt++;
|
|
|
|
}
|
|
|
|
|
2017-10-17 23:36:22 +00:00
|
|
|
if (!cnt) {
|
2018-02-12 08:19:42 +00:00
|
|
|
pr_err("no fragments or symbols in overlay\n");
|
2017-10-17 23:36:25 +00:00
|
|
|
ret = -EINVAL;
|
2022-04-20 22:25:05 +00:00
|
|
|
goto err_out;
|
2014-10-28 20:35:58 +00:00
|
|
|
}
|
|
|
|
|
2017-10-17 23:36:23 +00:00
|
|
|
ovcs->count = cnt;
|
2014-10-28 20:35:58 +00:00
|
|
|
|
|
|
|
return 0;
|
2017-10-17 23:36:25 +00:00
|
|
|
|
2022-04-20 22:25:05 +00:00
|
|
|
err_out:
|
2017-10-17 23:36:26 +00:00
|
|
|
pr_err("%s() failed, ret = %d\n", __func__, ret);
|
|
|
|
|
2017-10-17 23:36:25 +00:00
|
|
|
return ret;
|
2014-10-28 20:35:58 +00:00
|
|
|
}
|
|
|
|
|
2017-10-17 23:36:25 +00:00
|
|
|
static void free_overlay_changeset(struct overlay_changeset *ovcs)
|
2014-10-28 20:35:58 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2017-12-05 15:27:02 +00:00
|
|
|
if (ovcs->cset.entries.next)
|
|
|
|
of_changeset_destroy(&ovcs->cset);
|
2017-10-17 23:36:25 +00:00
|
|
|
|
2022-04-20 22:25:05 +00:00
|
|
|
if (ovcs->id) {
|
2017-10-17 23:36:25 +00:00
|
|
|
idr_remove(&ovcs_idr, ovcs->id);
|
2022-04-20 22:25:05 +00:00
|
|
|
list_del(&ovcs->ovcs_list);
|
|
|
|
ovcs->id = 0;
|
|
|
|
}
|
|
|
|
|
2017-10-17 23:36:25 +00:00
|
|
|
|
|
|
|
for (i = 0; i < ovcs->count; i++) {
|
2017-10-17 23:36:23 +00:00
|
|
|
of_node_put(ovcs->fragments[i].target);
|
|
|
|
of_node_put(ovcs->fragments[i].overlay);
|
2014-10-28 20:35:58 +00:00
|
|
|
}
|
2017-10-17 23:36:23 +00:00
|
|
|
kfree(ovcs->fragments);
|
2022-04-20 22:25:05 +00:00
|
|
|
|
2018-02-12 08:19:42 +00:00
|
|
|
/*
|
2022-04-20 22:25:05 +00:00
|
|
|
* There should be no live pointers into ovcs->overlay_mem and
|
2022-04-20 22:25:04 +00:00
|
|
|
* ovcs->new_fdt due to the policy that overlay notifiers are not
|
2022-04-20 22:25:05 +00:00
|
|
|
* allowed to retain pointers into the overlay devicetree other
|
|
|
|
* than during the window from OF_OVERLAY_PRE_APPLY overlay
|
|
|
|
* notifiers until the OF_OVERLAY_POST_REMOVE overlay notifiers.
|
|
|
|
*
|
|
|
|
* A memory leak will occur here if within the window.
|
2018-02-12 08:19:42 +00:00
|
|
|
*/
|
2022-04-20 22:25:05 +00:00
|
|
|
|
|
|
|
if (ovcs->notify_state == OF_OVERLAY_INIT ||
|
|
|
|
ovcs->notify_state == OF_OVERLAY_POST_REMOVE) {
|
|
|
|
kfree(ovcs->overlay_mem);
|
|
|
|
kfree(ovcs->new_fdt);
|
|
|
|
}
|
2017-10-17 23:36:25 +00:00
|
|
|
kfree(ovcs);
|
|
|
|
}
|
2014-10-28 20:35:58 +00:00
|
|
|
|
2018-02-12 08:19:42 +00:00
|
|
|
/*
|
|
|
|
* internal documentation
|
|
|
|
*
|
2017-10-17 23:36:23 +00:00
|
|
|
* of_overlay_apply() - Create and apply an overlay changeset
|
2022-04-20 22:25:05 +00:00
|
|
|
* @ovcs: overlay changeset
|
2023-08-15 17:19:59 +00:00
|
|
|
* @base: point to the target node to apply overlay
|
2017-10-17 23:36:26 +00:00
|
|
|
*
|
|
|
|
* Creates and applies an overlay changeset.
|
2014-10-28 20:35:58 +00:00
|
|
|
*
|
2017-10-17 23:36:26 +00:00
|
|
|
* If an error is returned by an overlay changeset pre-apply notifier
|
|
|
|
* then no further overlay changeset pre-apply notifier will be called.
|
|
|
|
*
|
|
|
|
* If an error is returned by an overlay changeset post-apply notifier
|
|
|
|
* then no further overlay changeset post-apply notifier will be called.
|
|
|
|
*
|
|
|
|
* If more than one notifier returns an error, then the last notifier
|
|
|
|
* error to occur is returned.
|
|
|
|
*
|
|
|
|
* If an error occurred while applying the overlay changeset, then an
|
|
|
|
* attempt is made to revert any changes that were made to the
|
|
|
|
* device tree. If there were any errors during the revert attempt
|
|
|
|
* then the state of the device tree can not be determined, and any
|
|
|
|
* following attempt to apply or remove an overlay changeset will be
|
|
|
|
* refused.
|
|
|
|
*
|
2022-04-20 22:25:05 +00:00
|
|
|
* Returns 0 on success, or a negative error number. On error return,
|
|
|
|
* the caller of of_overlay_apply() must call free_overlay_changeset().
|
2014-10-28 20:35:58 +00:00
|
|
|
*/
|
2017-10-17 23:36:26 +00:00
|
|
|
|
2023-08-15 17:19:59 +00:00
|
|
|
static int of_overlay_apply(struct overlay_changeset *ovcs,
|
|
|
|
struct device_node *base)
|
2014-10-28 20:35:58 +00:00
|
|
|
{
|
2017-10-17 23:36:26 +00:00
|
|
|
int ret = 0, ret_revert, ret_tmp;
|
|
|
|
|
2022-04-20 22:25:05 +00:00
|
|
|
ret = of_resolve_phandles(ovcs->overlay_root);
|
2017-10-17 23:36:29 +00:00
|
|
|
if (ret)
|
2022-04-20 22:25:05 +00:00
|
|
|
goto out;
|
2014-10-28 20:35:58 +00:00
|
|
|
|
2023-08-15 17:19:59 +00:00
|
|
|
ret = init_overlay_changeset(ovcs, base);
|
2017-10-17 23:36:26 +00:00
|
|
|
if (ret)
|
2022-04-20 22:25:05 +00:00
|
|
|
goto out;
|
2014-10-28 20:35:58 +00:00
|
|
|
|
2017-10-17 23:36:23 +00:00
|
|
|
ret = overlay_notify(ovcs, OF_OVERLAY_PRE_APPLY);
|
2022-05-02 18:17:40 +00:00
|
|
|
if (ret)
|
2022-04-20 22:25:05 +00:00
|
|
|
goto out;
|
2016-11-01 19:14:22 +00:00
|
|
|
|
2017-10-17 23:36:23 +00:00
|
|
|
ret = build_changeset(ovcs);
|
|
|
|
if (ret)
|
2022-04-20 22:25:05 +00:00
|
|
|
goto out;
|
2016-06-15 13:32:18 +00:00
|
|
|
|
2017-10-17 23:36:26 +00:00
|
|
|
ret_revert = 0;
|
|
|
|
ret = __of_changeset_apply_entries(&ovcs->cset, &ret_revert);
|
|
|
|
if (ret) {
|
|
|
|
if (ret_revert) {
|
|
|
|
pr_debug("overlay changeset revert error %d\n",
|
|
|
|
ret_revert);
|
|
|
|
devicetree_state_flags |= DTSF_APPLY_FAIL;
|
|
|
|
}
|
2022-04-20 22:25:05 +00:00
|
|
|
goto out;
|
2017-10-17 23:36:26 +00:00
|
|
|
}
|
2014-10-28 20:35:58 +00:00
|
|
|
|
2017-11-28 08:26:33 +00:00
|
|
|
ret = __of_changeset_apply_notify(&ovcs->cset);
|
|
|
|
if (ret)
|
2018-10-05 03:33:35 +00:00
|
|
|
pr_err("overlay apply changeset entry notify error %d\n", ret);
|
2017-11-28 08:26:33 +00:00
|
|
|
/* notify failure is not fatal, continue */
|
|
|
|
|
2017-10-17 23:36:26 +00:00
|
|
|
ret_tmp = overlay_notify(ovcs, OF_OVERLAY_POST_APPLY);
|
2022-05-02 18:17:40 +00:00
|
|
|
if (ret_tmp)
|
2017-10-17 23:36:26 +00:00
|
|
|
if (!ret)
|
|
|
|
ret = ret_tmp;
|
2016-11-01 19:14:22 +00:00
|
|
|
|
2017-10-17 23:36:26 +00:00
|
|
|
out:
|
|
|
|
pr_debug("%s() err=%d\n", __func__, ret);
|
|
|
|
|
2017-10-17 23:36:23 +00:00
|
|
|
return ret;
|
2014-10-28 20:35:58 +00:00
|
|
|
}
|
2018-02-12 08:19:42 +00:00
|
|
|
|
2023-11-06 13:03:24 +00:00
|
|
|
/**
|
2022-05-02 18:17:42 +00:00
|
|
|
* of_overlay_fdt_apply() - Create and apply an overlay changeset
|
|
|
|
* @overlay_fdt: pointer to overlay FDT
|
|
|
|
* @overlay_fdt_size: number of bytes in @overlay_fdt
|
|
|
|
* @ret_ovcs_id: pointer for returning created changeset id
|
2023-08-15 17:19:59 +00:00
|
|
|
* @base: pointer for the target node to apply overlay
|
2022-05-02 18:17:42 +00:00
|
|
|
*
|
|
|
|
* Creates and applies an overlay changeset.
|
|
|
|
*
|
|
|
|
* See of_overlay_apply() for important behavior information.
|
|
|
|
*
|
|
|
|
* Return: 0 on success, or a negative error number. *@ret_ovcs_id is set to
|
|
|
|
* the value of overlay changeset id, which can be passed to of_overlay_remove()
|
|
|
|
* to remove the overlay.
|
|
|
|
*
|
|
|
|
* On error return, the changeset may be partially applied. This is especially
|
|
|
|
* likely if an OF_OVERLAY_POST_APPLY notifier returns an error. In this case
|
|
|
|
* the caller should call of_overlay_remove() with the value in *@ret_ovcs_id.
|
|
|
|
*/
|
|
|
|
|
2018-02-12 08:19:42 +00:00
|
|
|
int of_overlay_fdt_apply(const void *overlay_fdt, u32 overlay_fdt_size,
|
2023-08-15 17:19:59 +00:00
|
|
|
int *ret_ovcs_id, struct device_node *base)
|
2018-02-12 08:19:42 +00:00
|
|
|
{
|
2021-04-08 20:45:08 +00:00
|
|
|
void *new_fdt;
|
|
|
|
void *new_fdt_align;
|
2022-04-20 22:25:05 +00:00
|
|
|
void *overlay_mem;
|
2018-02-12 08:19:42 +00:00
|
|
|
int ret;
|
|
|
|
u32 size;
|
2022-04-20 22:25:05 +00:00
|
|
|
struct overlay_changeset *ovcs;
|
2018-02-12 08:19:42 +00:00
|
|
|
|
2022-04-20 22:25:05 +00:00
|
|
|
*ret_ovcs_id = 0;
|
2018-02-12 08:19:42 +00:00
|
|
|
|
2022-07-15 14:03:14 +00:00
|
|
|
if (devicetree_corrupt()) {
|
|
|
|
pr_err("devicetree state suspect, refuse to apply overlay\n");
|
|
|
|
return -EBUSY;
|
|
|
|
}
|
|
|
|
|
2018-02-12 08:19:42 +00:00
|
|
|
if (overlay_fdt_size < sizeof(struct fdt_header) ||
|
|
|
|
fdt_check_header(overlay_fdt)) {
|
|
|
|
pr_err("Invalid overlay_fdt header\n");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
size = fdt_totalsize(overlay_fdt);
|
|
|
|
if (overlay_fdt_size < size)
|
|
|
|
return -EINVAL;
|
|
|
|
|
2022-04-20 22:25:05 +00:00
|
|
|
ovcs = kzalloc(sizeof(*ovcs), GFP_KERNEL);
|
|
|
|
if (!ovcs)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
of_overlay_mutex_lock();
|
|
|
|
mutex_lock(&of_mutex);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* ovcs->notify_state must be set to OF_OVERLAY_INIT before allocating
|
|
|
|
* ovcs resources, implicitly set by kzalloc() of ovcs
|
|
|
|
*/
|
|
|
|
|
|
|
|
ovcs->id = idr_alloc(&ovcs_idr, ovcs, 1, 0, GFP_KERNEL);
|
|
|
|
if (ovcs->id <= 0) {
|
|
|
|
ret = ovcs->id;
|
|
|
|
goto err_free_ovcs;
|
|
|
|
}
|
|
|
|
|
|
|
|
INIT_LIST_HEAD(&ovcs->ovcs_list);
|
|
|
|
list_add_tail(&ovcs->ovcs_list, &ovcs_list);
|
2023-07-28 08:50:28 +00:00
|
|
|
of_changeset_init(&ovcs->cset);
|
2022-04-20 22:25:05 +00:00
|
|
|
|
2018-02-12 08:19:42 +00:00
|
|
|
/*
|
|
|
|
* Must create permanent copy of FDT because of_fdt_unflatten_tree()
|
|
|
|
* will create pointers to the passed in FDT in the unflattened tree.
|
|
|
|
*/
|
2021-04-08 20:45:08 +00:00
|
|
|
new_fdt = kmalloc(size + FDT_ALIGN_SIZE, GFP_KERNEL);
|
2022-04-20 22:25:05 +00:00
|
|
|
if (!new_fdt) {
|
|
|
|
ret = -ENOMEM;
|
|
|
|
goto err_free_ovcs;
|
|
|
|
}
|
|
|
|
ovcs->new_fdt = new_fdt;
|
2018-02-12 08:19:42 +00:00
|
|
|
|
2021-04-08 20:45:08 +00:00
|
|
|
new_fdt_align = PTR_ALIGN(new_fdt, FDT_ALIGN_SIZE);
|
|
|
|
memcpy(new_fdt_align, overlay_fdt, size);
|
|
|
|
|
2022-04-20 22:25:05 +00:00
|
|
|
overlay_mem = of_fdt_unflatten_tree(new_fdt_align, NULL,
|
|
|
|
&ovcs->overlay_root);
|
|
|
|
if (!overlay_mem) {
|
2018-02-12 08:19:42 +00:00
|
|
|
pr_err("unable to unflatten overlay_fdt\n");
|
|
|
|
ret = -EINVAL;
|
2022-04-20 22:25:05 +00:00
|
|
|
goto err_free_ovcs;
|
2018-02-12 08:19:42 +00:00
|
|
|
}
|
2022-04-20 22:25:05 +00:00
|
|
|
ovcs->overlay_mem = overlay_mem;
|
2018-02-12 08:19:42 +00:00
|
|
|
|
2023-08-15 17:19:59 +00:00
|
|
|
ret = of_overlay_apply(ovcs, base);
|
2022-05-02 18:17:42 +00:00
|
|
|
/*
|
|
|
|
* If of_overlay_apply() error, calling free_overlay_changeset() may
|
|
|
|
* result in a memory leak if the apply partly succeeded, so do NOT
|
|
|
|
* goto err_free_ovcs. Instead, the caller of of_overlay_fdt_apply()
|
|
|
|
* can call of_overlay_remove();
|
|
|
|
*/
|
2022-04-20 22:25:05 +00:00
|
|
|
*ret_ovcs_id = ovcs->id;
|
2022-07-15 14:03:15 +00:00
|
|
|
goto out_unlock;
|
2018-02-12 08:19:42 +00:00
|
|
|
|
2022-04-20 22:25:05 +00:00
|
|
|
err_free_ovcs:
|
|
|
|
free_overlay_changeset(ovcs);
|
2018-02-12 08:19:42 +00:00
|
|
|
|
2022-07-15 14:03:15 +00:00
|
|
|
out_unlock:
|
2022-04-20 22:25:05 +00:00
|
|
|
mutex_unlock(&of_mutex);
|
|
|
|
of_overlay_mutex_unlock();
|
2018-02-12 08:19:42 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(of_overlay_fdt_apply);
|
2014-10-28 20:35:58 +00:00
|
|
|
|
2017-10-17 23:36:21 +00:00
|
|
|
/*
|
2017-10-17 23:36:23 +00:00
|
|
|
* Find @np in @tree.
|
|
|
|
*
|
|
|
|
* Returns 1 if @np is @tree or is contained in @tree, else 0
|
2017-10-17 23:36:21 +00:00
|
|
|
*/
|
2017-10-17 23:36:23 +00:00
|
|
|
static int find_node(struct device_node *tree, struct device_node *np)
|
2014-10-28 20:35:58 +00:00
|
|
|
{
|
2017-10-17 23:36:23 +00:00
|
|
|
if (tree == np)
|
2014-10-28 20:35:58 +00:00
|
|
|
return 1;
|
|
|
|
|
2024-08-26 06:24:06 +00:00
|
|
|
for_each_child_of_node_scoped(tree, child) {
|
|
|
|
if (find_node(child, np))
|
2014-10-28 20:35:58 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-10-17 23:36:21 +00:00
|
|
|
/*
|
2017-10-17 23:36:27 +00:00
|
|
|
* Is @remove_ce_node a child of, a parent of, or the same as any
|
2017-10-17 23:36:23 +00:00
|
|
|
* node in an overlay changeset more topmost than @remove_ovcs?
|
|
|
|
*
|
|
|
|
* Returns 1 if found, else 0
|
2017-10-17 23:36:21 +00:00
|
|
|
*/
|
2017-10-17 23:36:27 +00:00
|
|
|
static int node_overlaps_later_cs(struct overlay_changeset *remove_ovcs,
|
|
|
|
struct device_node *remove_ce_node)
|
2014-10-28 20:35:58 +00:00
|
|
|
{
|
2017-10-17 23:36:23 +00:00
|
|
|
struct overlay_changeset *ovcs;
|
2014-10-28 20:35:58 +00:00
|
|
|
struct of_changeset_entry *ce;
|
|
|
|
|
2017-10-17 23:36:23 +00:00
|
|
|
list_for_each_entry_reverse(ovcs, &ovcs_list, ovcs_list) {
|
|
|
|
if (ovcs == remove_ovcs)
|
2014-10-28 20:35:58 +00:00
|
|
|
break;
|
|
|
|
|
2017-10-17 23:36:23 +00:00
|
|
|
list_for_each_entry(ce, &ovcs->cset.entries, node) {
|
2017-10-17 23:36:27 +00:00
|
|
|
if (find_node(ce->np, remove_ce_node)) {
|
|
|
|
pr_err("%s: #%d overlaps with #%d @%pOF\n",
|
2017-10-17 23:36:23 +00:00
|
|
|
__func__, remove_ovcs->id, ovcs->id,
|
2017-10-17 23:36:27 +00:00
|
|
|
remove_ce_node);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if (find_node(remove_ce_node, ce->np)) {
|
|
|
|
pr_err("%s: #%d overlaps with #%d @%pOF\n",
|
|
|
|
__func__, remove_ovcs->id, ovcs->id,
|
|
|
|
remove_ce_node);
|
2017-10-17 23:36:23 +00:00
|
|
|
return 1;
|
2014-10-28 20:35:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-17 23:36:23 +00:00
|
|
|
return 0;
|
2014-10-28 20:35:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We can safely remove the overlay only if it's the top-most one.
|
|
|
|
* Newly applied overlays are inserted at the tail of the overlay list,
|
|
|
|
* so a top most overlay is the one that is closest to the tail.
|
|
|
|
*
|
|
|
|
* The topmost check is done by exploiting this property. For each
|
|
|
|
* affected device node in the log list we check if this overlay is
|
|
|
|
* the one closest to the tail. If another overlay has affected this
|
2022-12-20 22:20:32 +00:00
|
|
|
* device node and is closest to the tail, then removal is not permitted.
|
2014-10-28 20:35:58 +00:00
|
|
|
*/
|
2017-10-17 23:36:23 +00:00
|
|
|
static int overlay_removal_is_ok(struct overlay_changeset *remove_ovcs)
|
2014-10-28 20:35:58 +00:00
|
|
|
{
|
2017-10-17 23:36:23 +00:00
|
|
|
struct of_changeset_entry *remove_ce;
|
2014-10-28 20:35:58 +00:00
|
|
|
|
2017-10-17 23:36:23 +00:00
|
|
|
list_for_each_entry(remove_ce, &remove_ovcs->cset.entries, node) {
|
2017-10-17 23:36:27 +00:00
|
|
|
if (node_overlaps_later_cs(remove_ovcs, remove_ce->np)) {
|
2017-10-17 23:36:23 +00:00
|
|
|
pr_err("overlay #%d is not topmost\n", remove_ovcs->id);
|
2014-10-28 20:35:58 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-10-17 23:36:23 +00:00
|
|
|
* of_overlay_remove() - Revert and free an overlay changeset
|
2017-10-17 23:36:26 +00:00
|
|
|
* @ovcs_id: Pointer to overlay changeset id
|
2014-10-28 20:35:58 +00:00
|
|
|
*
|
2017-10-17 23:36:26 +00:00
|
|
|
* Removes an overlay if it is permissible. @ovcs_id was previously returned
|
2018-03-09 10:44:47 +00:00
|
|
|
* by of_overlay_fdt_apply().
|
2014-10-28 20:35:58 +00:00
|
|
|
*
|
2017-10-17 23:36:26 +00:00
|
|
|
* If an error occurred while attempting to revert the overlay changeset,
|
|
|
|
* then an attempt is made to re-apply any changeset entry that was
|
|
|
|
* reverted. If an error occurs on re-apply then the state of the device
|
|
|
|
* tree can not be determined, and any following attempt to apply or remove
|
|
|
|
* an overlay changeset will be refused.
|
|
|
|
*
|
|
|
|
* A non-zero return value will not revert the changeset if error is from:
|
|
|
|
* - parameter checks
|
2017-11-28 08:25:23 +00:00
|
|
|
* - overlay changeset pre-remove notifier
|
2017-10-17 23:36:26 +00:00
|
|
|
* - overlay changeset entry revert
|
|
|
|
*
|
|
|
|
* If an error is returned by an overlay changeset pre-remove notifier
|
|
|
|
* then no further overlay changeset pre-remove notifier will be called.
|
|
|
|
*
|
|
|
|
* If more than one notifier returns an error, then the last notifier
|
|
|
|
* error to occur is returned.
|
|
|
|
*
|
|
|
|
* A non-zero return value will revert the changeset if error is from:
|
|
|
|
* - overlay changeset entry notifier
|
2017-11-28 08:25:23 +00:00
|
|
|
* - overlay changeset post-remove notifier
|
2017-10-17 23:36:26 +00:00
|
|
|
*
|
|
|
|
* If an error is returned by an overlay changeset post-remove notifier
|
|
|
|
* then no further overlay changeset post-remove notifier will be called.
|
|
|
|
*
|
2021-04-21 15:39:36 +00:00
|
|
|
* Return: 0 on success, or a negative error number. *@ovcs_id is set to
|
2017-10-17 23:36:26 +00:00
|
|
|
* zero after reverting the changeset, even if a subsequent error occurs.
|
2014-10-28 20:35:58 +00:00
|
|
|
*/
|
2017-10-17 23:36:26 +00:00
|
|
|
int of_overlay_remove(int *ovcs_id)
|
2014-10-28 20:35:58 +00:00
|
|
|
{
|
2017-10-17 23:36:23 +00:00
|
|
|
struct overlay_changeset *ovcs;
|
2017-10-17 23:36:26 +00:00
|
|
|
int ret, ret_apply, ret_tmp;
|
|
|
|
|
|
|
|
if (devicetree_corrupt()) {
|
|
|
|
pr_err("suspect devicetree state, refuse to remove overlay\n");
|
|
|
|
ret = -EBUSY;
|
|
|
|
goto out;
|
|
|
|
}
|
2014-10-28 20:35:58 +00:00
|
|
|
|
|
|
|
mutex_lock(&of_mutex);
|
|
|
|
|
2017-10-17 23:36:26 +00:00
|
|
|
ovcs = idr_find(&ovcs_idr, *ovcs_id);
|
2017-10-17 23:36:23 +00:00
|
|
|
if (!ovcs) {
|
|
|
|
ret = -ENODEV;
|
2017-10-17 23:36:26 +00:00
|
|
|
pr_err("remove: Could not find overlay #%d\n", *ovcs_id);
|
2022-04-20 22:25:05 +00:00
|
|
|
goto err_unlock;
|
2014-10-28 20:35:58 +00:00
|
|
|
}
|
|
|
|
|
2017-10-17 23:36:23 +00:00
|
|
|
if (!overlay_removal_is_ok(ovcs)) {
|
|
|
|
ret = -EBUSY;
|
2022-04-20 22:25:05 +00:00
|
|
|
goto err_unlock;
|
2014-10-28 20:35:58 +00:00
|
|
|
}
|
|
|
|
|
2017-10-17 23:36:26 +00:00
|
|
|
ret = overlay_notify(ovcs, OF_OVERLAY_PRE_REMOVE);
|
2022-05-02 18:17:40 +00:00
|
|
|
if (ret)
|
2022-04-20 22:25:05 +00:00
|
|
|
goto err_unlock;
|
2017-10-17 23:36:25 +00:00
|
|
|
|
2017-10-17 23:36:26 +00:00
|
|
|
ret_apply = 0;
|
|
|
|
ret = __of_changeset_revert_entries(&ovcs->cset, &ret_apply);
|
|
|
|
if (ret) {
|
|
|
|
if (ret_apply)
|
|
|
|
devicetree_state_flags |= DTSF_REVERT_FAIL;
|
2022-04-20 22:25:05 +00:00
|
|
|
goto err_unlock;
|
2017-10-17 23:36:26 +00:00
|
|
|
}
|
2017-10-17 23:36:25 +00:00
|
|
|
|
2017-11-28 08:26:33 +00:00
|
|
|
ret = __of_changeset_revert_notify(&ovcs->cset);
|
|
|
|
if (ret)
|
2018-10-05 03:33:35 +00:00
|
|
|
pr_err("overlay remove changeset entry notify error %d\n", ret);
|
2017-11-28 08:26:33 +00:00
|
|
|
/* notify failure is not fatal, continue */
|
|
|
|
|
2017-10-17 23:36:26 +00:00
|
|
|
*ovcs_id = 0;
|
|
|
|
|
2022-04-20 22:25:05 +00:00
|
|
|
/*
|
|
|
|
* Note that the overlay memory will be kfree()ed by
|
|
|
|
* free_overlay_changeset() even if the notifier for
|
|
|
|
* OF_OVERLAY_POST_REMOVE returns an error.
|
|
|
|
*/
|
2017-10-17 23:36:26 +00:00
|
|
|
ret_tmp = overlay_notify(ovcs, OF_OVERLAY_POST_REMOVE);
|
2022-05-02 18:17:40 +00:00
|
|
|
if (ret_tmp)
|
2017-10-17 23:36:26 +00:00
|
|
|
if (!ret)
|
|
|
|
ret = ret_tmp;
|
2017-10-17 23:36:25 +00:00
|
|
|
|
|
|
|
free_overlay_changeset(ovcs);
|
2014-10-28 20:35:58 +00:00
|
|
|
|
2022-04-20 22:25:05 +00:00
|
|
|
err_unlock:
|
|
|
|
/*
|
|
|
|
* If jumped over free_overlay_changeset(), then did not kfree()
|
|
|
|
* overlay related memory. This is a memory leak unless a subsequent
|
|
|
|
* of_overlay_remove() of this overlay is successful.
|
|
|
|
*/
|
2014-10-28 20:35:58 +00:00
|
|
|
mutex_unlock(&of_mutex);
|
|
|
|
|
2017-10-17 23:36:26 +00:00
|
|
|
out:
|
|
|
|
pr_debug("%s() err=%d\n", __func__, ret);
|
|
|
|
|
2017-10-17 23:36:23 +00:00
|
|
|
return ret;
|
2014-10-28 20:35:58 +00:00
|
|
|
}
|
2017-10-17 23:36:23 +00:00
|
|
|
EXPORT_SYMBOL_GPL(of_overlay_remove);
|
2014-10-28 20:35:58 +00:00
|
|
|
|
|
|
|
/**
|
2017-10-17 23:36:23 +00:00
|
|
|
* of_overlay_remove_all() - Reverts and frees all overlay changesets
|
2014-10-28 20:35:58 +00:00
|
|
|
*
|
|
|
|
* Removes all overlays from the system in the correct order.
|
|
|
|
*
|
2021-03-25 16:47:12 +00:00
|
|
|
* Return: 0 on success, or a negative error number
|
2014-10-28 20:35:58 +00:00
|
|
|
*/
|
2017-10-17 23:36:23 +00:00
|
|
|
int of_overlay_remove_all(void)
|
2014-10-28 20:35:58 +00:00
|
|
|
{
|
2017-10-17 23:36:23 +00:00
|
|
|
struct overlay_changeset *ovcs, *ovcs_n;
|
2017-10-17 23:36:25 +00:00
|
|
|
int ret;
|
2014-10-28 20:35:58 +00:00
|
|
|
|
|
|
|
/* the tail of list is guaranteed to be safe to remove */
|
2017-10-17 23:36:23 +00:00
|
|
|
list_for_each_entry_safe_reverse(ovcs, ovcs_n, &ovcs_list, ovcs_list) {
|
2017-10-17 23:36:26 +00:00
|
|
|
ret = of_overlay_remove(&ovcs->id);
|
2017-10-17 23:36:25 +00:00
|
|
|
if (ret)
|
|
|
|
return ret;
|
2014-10-28 20:35:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2017-10-17 23:36:23 +00:00
|
|
|
EXPORT_SYMBOL_GPL(of_overlay_remove_all);
|