2017-12-28 02:55:14 +08:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
2014-10-29 04:35:58 +08: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 21:32:18 +08:00
|
|
|
|
|
|
|
#define pr_fmt(fmt) "OF: overlay: " fmt
|
|
|
|
|
2014-10-29 04:35:58 +08:00
|
|
|
#include <linux/kernel.h>
|
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/of.h>
|
|
|
|
#include <linux/of_device.h>
|
2018-02-12 16:19:42 +08:00
|
|
|
#include <linux/of_fdt.h>
|
2014-10-29 04:35:58 +08:00
|
|
|
#include <linux/string.h>
|
|
|
|
#include <linux/ctype.h>
|
|
|
|
#include <linux/errno.h>
|
|
|
|
#include <linux/slab.h>
|
2018-02-12 16:19:42 +08:00
|
|
|
#include <linux/libfdt.h>
|
2014-10-29 04:35:58 +08:00
|
|
|
#include <linux/err.h>
|
2015-02-17 10:36:58 +08:00
|
|
|
#include <linux/idr.h>
|
2014-10-29 04:35:58 +08:00
|
|
|
|
|
|
|
#include "of_private.h"
|
|
|
|
|
|
|
|
/**
|
2017-10-18 07:36:23 +08:00
|
|
|
* struct fragment - info about fragment nodes in overlay expanded device tree
|
2014-10-29 04:35:58 +08:00
|
|
|
* @target: target of the overlay operation
|
2017-10-18 07:36:23 +08:00
|
|
|
* @overlay: pointer to the __overlay__ node
|
2014-10-29 04:35:58 +08:00
|
|
|
*/
|
2017-10-18 07:36:23 +08:00
|
|
|
struct fragment {
|
2014-10-29 04:35:58 +08:00
|
|
|
struct device_node *target;
|
|
|
|
struct device_node *overlay;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2017-10-18 07:36:23 +08:00
|
|
|
* struct overlay_changeset
|
2018-02-12 16:19:42 +08:00
|
|
|
* @id: changeset identifier
|
2017-10-18 07:36:30 +08:00
|
|
|
* @ovcs_list: list on which we are located
|
2018-02-12 16:19:42 +08:00
|
|
|
* @fdt: FDT that was unflattened to create @overlay_tree
|
2017-10-18 07:36:31 +08:00
|
|
|
* @overlay_tree: expanded device tree that contains the fragment nodes
|
2017-10-18 07:36:30 +08: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-29 04:35:58 +08:00
|
|
|
*/
|
2017-10-18 07:36:23 +08:00
|
|
|
struct overlay_changeset {
|
2014-10-29 04:35:58 +08:00
|
|
|
int id;
|
2017-10-18 07:36:23 +08:00
|
|
|
struct list_head ovcs_list;
|
2018-02-12 16:19:42 +08:00
|
|
|
const void *fdt;
|
2017-10-18 07:36:31 +08:00
|
|
|
struct device_node *overlay_tree;
|
2014-10-29 04:35:58 +08:00
|
|
|
int count;
|
2017-10-18 07:36:23 +08:00
|
|
|
struct fragment *fragments;
|
2017-10-18 07:36:30 +08:00
|
|
|
bool symbols_fragment;
|
2014-10-29 04:35:58 +08:00
|
|
|
struct of_changeset cset;
|
|
|
|
};
|
|
|
|
|
2017-10-18 07:36:26 +08: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-18 07:36:23 +08:00
|
|
|
static int build_changeset_next_level(struct overlay_changeset *ovcs,
|
|
|
|
struct device_node *target_node,
|
2017-10-18 07:36:30 +08:00
|
|
|
const struct device_node *overlay_node);
|
2014-10-29 04:35:58 +08:00
|
|
|
|
2017-10-18 07:36:29 +08: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-18 07:36:25 +08:00
|
|
|
static LIST_HEAD(ovcs_list);
|
|
|
|
static DEFINE_IDR(ovcs_idr);
|
|
|
|
|
2017-10-18 07:36:23 +08:00
|
|
|
static BLOCKING_NOTIFIER_HEAD(overlay_notify_chain);
|
2016-11-02 03:14:22 +08:00
|
|
|
|
|
|
|
int of_overlay_notifier_register(struct notifier_block *nb)
|
|
|
|
{
|
2017-10-18 07:36:23 +08:00
|
|
|
return blocking_notifier_chain_register(&overlay_notify_chain, nb);
|
2016-11-02 03:14:22 +08:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(of_overlay_notifier_register);
|
|
|
|
|
|
|
|
int of_overlay_notifier_unregister(struct notifier_block *nb)
|
|
|
|
{
|
2017-10-18 07:36:23 +08:00
|
|
|
return blocking_notifier_chain_unregister(&overlay_notify_chain, nb);
|
2016-11-02 03:14:22 +08:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(of_overlay_notifier_unregister);
|
|
|
|
|
2017-10-18 07:36:26 +08:00
|
|
|
static char *of_overlay_action_name[] = {
|
|
|
|
"pre-apply",
|
|
|
|
"post-apply",
|
|
|
|
"pre-remove",
|
|
|
|
"post-remove",
|
|
|
|
};
|
|
|
|
|
2017-10-18 07:36:23 +08:00
|
|
|
static int overlay_notify(struct overlay_changeset *ovcs,
|
|
|
|
enum of_overlay_notify_action action)
|
2016-11-02 03:14:22 +08:00
|
|
|
{
|
|
|
|
struct of_overlay_notify_data nd;
|
|
|
|
int i, ret;
|
|
|
|
|
2017-10-18 07:36:23 +08:00
|
|
|
for (i = 0; i < ovcs->count; i++) {
|
|
|
|
struct fragment *fragment = &ovcs->fragments[i];
|
2016-11-02 03:14:22 +08:00
|
|
|
|
2017-10-18 07:36:23 +08:00
|
|
|
nd.target = fragment->target;
|
|
|
|
nd.overlay = fragment->overlay;
|
2016-11-02 03:14:22 +08:00
|
|
|
|
2017-10-18 07:36:23 +08:00
|
|
|
ret = blocking_notifier_call_chain(&overlay_notify_chain,
|
2016-11-02 03:14:22 +08:00
|
|
|
action, &nd);
|
2017-10-20 05:18:27 +08:00
|
|
|
if (ret == NOTIFY_OK || ret == NOTIFY_STOP)
|
2017-10-18 07:36:26 +08:00
|
|
|
return 0;
|
|
|
|
if (ret) {
|
|
|
|
ret = notifier_to_errno(ret);
|
|
|
|
pr_err("overlay changeset %s notifier error %d, target: %pOF\n",
|
|
|
|
of_overlay_action_name[action], ret, nd.target);
|
|
|
|
return ret;
|
|
|
|
}
|
2016-11-02 03:14:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-10-18 07:36:24 +08:00
|
|
|
/*
|
2017-10-18 07:36:31 +08:00
|
|
|
* The values of properties in the "/__symbols__" node are paths in
|
|
|
|
* the ovcs->overlay_tree. When duplicating the properties, the paths
|
|
|
|
* need to be adjusted to be the correct path for the live device tree.
|
2017-10-18 07:36:24 +08:00
|
|
|
*
|
2017-10-18 07:36:31 +08: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-18 07:36:24 +08: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-18 07:36:23 +08:00
|
|
|
static struct property *dup_and_fixup_symbol_prop(
|
|
|
|
struct overlay_changeset *ovcs, const struct property *prop)
|
2017-07-20 00:25:22 +08:00
|
|
|
{
|
2017-10-18 07:36:23 +08:00
|
|
|
struct fragment *fragment;
|
2017-10-18 07:36:31 +08:00
|
|
|
struct property *new_prop;
|
|
|
|
struct device_node *fragment_node;
|
|
|
|
struct device_node *overlay_node;
|
|
|
|
const char *path;
|
|
|
|
const char *path_tail;
|
2017-07-20 00:25:22 +08:00
|
|
|
const char *target_path;
|
|
|
|
int k;
|
|
|
|
int overlay_name_len;
|
2017-10-18 07:36:31 +08:00
|
|
|
int path_len;
|
|
|
|
int path_tail_len;
|
2017-07-20 00:25:22 +08:00
|
|
|
int target_path_len;
|
|
|
|
|
|
|
|
if (!prop->value)
|
|
|
|
return NULL;
|
2017-10-18 07:36:31 +08:00
|
|
|
if (strnlen(prop->value, prop->length) >= prop->length)
|
|
|
|
return NULL;
|
|
|
|
path = prop->value;
|
|
|
|
path_len = strlen(path);
|
2017-07-20 00:25:22 +08:00
|
|
|
|
2017-10-18 07:36:31 +08:00
|
|
|
if (path_len < 1)
|
2017-07-20 00:25:22 +08:00
|
|
|
return NULL;
|
2017-10-18 07:36:31 +08:00
|
|
|
fragment_node = __of_find_node_by_path(ovcs->overlay_tree, path + 1);
|
|
|
|
overlay_node = __of_find_node_by_path(fragment_node, "__overlay__/");
|
|
|
|
of_node_put(fragment_node);
|
|
|
|
of_node_put(overlay_node);
|
2017-07-20 00:25:22 +08:00
|
|
|
|
2017-10-18 07:36:23 +08:00
|
|
|
for (k = 0; k < ovcs->count; k++) {
|
|
|
|
fragment = &ovcs->fragments[k];
|
2017-10-18 07:36:31 +08:00
|
|
|
if (fragment->overlay == overlay_node)
|
2017-07-20 00:25:22 +08:00
|
|
|
break;
|
|
|
|
}
|
2017-10-18 07:36:23 +08:00
|
|
|
if (k >= ovcs->count)
|
2017-10-18 07:36:31 +08:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
overlay_name_len = snprintf(NULL, 0, "%pOF", fragment->overlay);
|
2017-07-20 00:25:22 +08:00
|
|
|
|
2017-10-18 07:36:31 +08: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-20 00:25:22 +08:00
|
|
|
target_path_len = strlen(target_path);
|
|
|
|
|
2017-10-18 07:36:31 +08:00
|
|
|
new_prop = kzalloc(sizeof(*new_prop), GFP_KERNEL);
|
|
|
|
if (!new_prop)
|
|
|
|
goto err_free_target_path;
|
2017-07-20 00:25:22 +08:00
|
|
|
|
2017-10-18 07:36:31 +08: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-20 00:25:22 +08:00
|
|
|
|
2017-10-18 07:36:31 +08:00
|
|
|
strcpy(new_prop->value, target_path);
|
|
|
|
strcpy(new_prop->value + target_path_len, path_tail);
|
2017-07-20 00:25:22 +08:00
|
|
|
|
2017-10-18 07:36:31 +08:00
|
|
|
of_property_set_flag(new_prop, OF_DYNAMIC);
|
2017-07-20 00:25:22 +08:00
|
|
|
|
2017-10-18 07:36:31 +08:00
|
|
|
return new_prop;
|
2017-07-20 00:25:22 +08:00
|
|
|
|
2017-10-18 07:36:31 +08:00
|
|
|
err_free_new_prop:
|
|
|
|
kfree(new_prop->name);
|
|
|
|
kfree(new_prop->value);
|
|
|
|
kfree(new_prop);
|
|
|
|
err_free_target_path:
|
|
|
|
kfree(target_path);
|
2017-07-20 00:25:22 +08:00
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2017-10-18 07:36:23 +08:00
|
|
|
/**
|
|
|
|
* add_changeset_property() - add @overlay_prop to overlay changeset
|
|
|
|
* @ovcs: overlay changeset
|
|
|
|
* @target_node: where to place @overlay_prop in live tree
|
|
|
|
* @overlay_prop: property to add or update, from overlay tree
|
2017-10-18 07:36:30 +08:00
|
|
|
* @is_symbols_prop: 1 if @overlay_prop is from node "/__symbols__"
|
2017-10-18 07:36:23 +08:00
|
|
|
*
|
|
|
|
* If @overlay_prop does not already exist in @target_node, add changeset entry
|
|
|
|
* to add @overlay_prop in @target_node, else add changeset entry to update
|
|
|
|
* value of @overlay_prop.
|
|
|
|
*
|
2017-10-18 07:36:21 +08:00
|
|
|
* Some special properties are not updated (no error returned).
|
2017-10-18 07:36:23 +08:00
|
|
|
*
|
2017-10-18 07:36:21 +08:00
|
|
|
* Update of property in symbols node is not allowed.
|
2017-10-18 07:36:23 +08:00
|
|
|
*
|
|
|
|
* Returns 0 on success, -ENOMEM if memory allocation failure, or -EINVAL if
|
|
|
|
* invalid @overlay.
|
2017-10-18 07:36:21 +08:00
|
|
|
*/
|
2017-10-18 07:36:23 +08:00
|
|
|
static int add_changeset_property(struct overlay_changeset *ovcs,
|
|
|
|
struct device_node *target_node,
|
|
|
|
struct property *overlay_prop,
|
2017-10-18 07:36:30 +08:00
|
|
|
bool is_symbols_prop)
|
2014-10-29 04:35:58 +08:00
|
|
|
{
|
2017-10-18 07:36:23 +08:00
|
|
|
struct property *new_prop = NULL, *prop;
|
2017-10-16 17:54:32 +08:00
|
|
|
int ret = 0;
|
2014-10-29 04:35:58 +08:00
|
|
|
|
2017-10-18 07:36:23 +08:00
|
|
|
prop = of_find_property(target_node, overlay_prop->name, NULL);
|
2014-10-29 04:35:58 +08:00
|
|
|
|
2017-10-18 07:36:23 +08:00
|
|
|
if (!of_prop_cmp(overlay_prop->name, "name") ||
|
|
|
|
!of_prop_cmp(overlay_prop->name, "phandle") ||
|
|
|
|
!of_prop_cmp(overlay_prop->name, "linux,phandle"))
|
2014-10-29 04:35:58 +08:00
|
|
|
return 0;
|
|
|
|
|
2017-10-18 07:36:30 +08:00
|
|
|
if (is_symbols_prop) {
|
2017-10-18 07:36:23 +08:00
|
|
|
if (prop)
|
2017-07-20 00:25:22 +08:00
|
|
|
return -EINVAL;
|
2017-10-18 07:36:23 +08:00
|
|
|
new_prop = dup_and_fixup_symbol_prop(ovcs, overlay_prop);
|
2017-07-20 00:25:22 +08:00
|
|
|
} else {
|
2017-10-18 07:36:23 +08:00
|
|
|
new_prop = __of_prop_dup(overlay_prop, GFP_KERNEL);
|
2017-07-20 00:25:22 +08:00
|
|
|
}
|
|
|
|
|
2017-10-18 07:36:23 +08:00
|
|
|
if (!new_prop)
|
2014-10-29 04:35:58 +08:00
|
|
|
return -ENOMEM;
|
|
|
|
|
2017-10-18 07:36:23 +08:00
|
|
|
if (!prop)
|
|
|
|
ret = of_changeset_add_property(&ovcs->cset, target_node,
|
|
|
|
new_prop);
|
2017-10-18 07:36:21 +08:00
|
|
|
else
|
2017-10-18 07:36:23 +08:00
|
|
|
ret = of_changeset_update_property(&ovcs->cset, target_node,
|
|
|
|
new_prop);
|
2017-10-16 17:54:32 +08:00
|
|
|
|
|
|
|
if (ret) {
|
2017-10-18 07:36:23 +08:00
|
|
|
kfree(new_prop->name);
|
|
|
|
kfree(new_prop->value);
|
|
|
|
kfree(new_prop);
|
2017-10-16 17:54:32 +08:00
|
|
|
}
|
|
|
|
return ret;
|
2014-10-29 04:35:58 +08:00
|
|
|
}
|
|
|
|
|
2017-10-18 07:36:23 +08:00
|
|
|
/**
|
|
|
|
* add_changeset_node() - add @node (and children) to overlay changeset
|
|
|
|
* @ovcs: overlay changeset
|
|
|
|
* @target_node: where to place @node in live tree
|
|
|
|
* @node: node from within overlay device tree fragment
|
|
|
|
*
|
|
|
|
* If @node does not already exist in @target_node, add changeset entry
|
|
|
|
* to add @node in @target_node.
|
|
|
|
*
|
|
|
|
* If @node already exists in @target_node, and the existing node has
|
|
|
|
* 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().
|
|
|
|
*
|
|
|
|
* NOTE: Multiple mods of created nodes not supported.
|
2017-10-18 07:36:26 +08:00
|
|
|
* If more than one fragment contains a node that does not already exist
|
|
|
|
* in the live tree, then for each fragment of_changeset_attach_node()
|
|
|
|
* will add a changeset entry to add the node. When the changeset is
|
|
|
|
* applied, __of_attach_node() will attach the node twice (once for
|
|
|
|
* each fragment). At this point the device tree will be corrupted.
|
|
|
|
*
|
|
|
|
* TODO: add integrity check to ensure that multiple fragments do not
|
|
|
|
* create the same node.
|
2017-10-18 07:36:23 +08:00
|
|
|
*
|
|
|
|
* Returns 0 on success, -ENOMEM if memory allocation failure, or -EINVAL if
|
|
|
|
* invalid @overlay.
|
|
|
|
*/
|
|
|
|
static int add_changeset_node(struct overlay_changeset *ovcs,
|
|
|
|
struct device_node *target_node, struct device_node *node)
|
2014-10-29 04:35:58 +08:00
|
|
|
{
|
2017-10-18 07:36:23 +08:00
|
|
|
const char *node_kbasename;
|
2015-03-03 21:04:45 +08:00
|
|
|
struct device_node *tchild;
|
2014-10-29 04:35:58 +08:00
|
|
|
int ret = 0;
|
|
|
|
|
2017-10-18 07:36:23 +08:00
|
|
|
node_kbasename = kbasename(node->full_name);
|
2014-10-29 04:35:58 +08:00
|
|
|
|
2017-10-18 07:36:23 +08:00
|
|
|
for_each_child_of_node(target_node, tchild)
|
|
|
|
if (!of_node_cmp(node_kbasename, kbasename(tchild->full_name)))
|
2017-07-20 00:25:21 +08:00
|
|
|
break;
|
|
|
|
|
2017-10-18 07:36:25 +08:00
|
|
|
if (!tchild) {
|
2017-10-18 07:36:23 +08:00
|
|
|
tchild = __of_node_dup(node, "%pOF/%s",
|
|
|
|
target_node, node_kbasename);
|
2014-10-29 04:35:58 +08:00
|
|
|
if (!tchild)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
2017-10-18 07:36:23 +08:00
|
|
|
tchild->parent = target_node;
|
2014-10-29 04:35:58 +08:00
|
|
|
|
2017-10-18 07:36:23 +08:00
|
|
|
ret = of_changeset_attach_node(&ovcs->cset, tchild);
|
2014-10-29 04:35:58 +08:00
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
2017-10-18 07:36:30 +08:00
|
|
|
return build_changeset_next_level(ovcs, tchild, node);
|
2014-10-29 04:35:58 +08:00
|
|
|
}
|
|
|
|
|
2017-10-18 07:36:28 +08:00
|
|
|
if (node->phandle && tchild->phandle)
|
|
|
|
ret = -EINVAL;
|
|
|
|
else
|
2017-10-18 07:36:30 +08:00
|
|
|
ret = build_changeset_next_level(ovcs, tchild, node);
|
2017-10-18 07:36:25 +08:00
|
|
|
of_node_put(tchild);
|
|
|
|
|
2014-10-29 04:35:58 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-10-18 07:36:23 +08:00
|
|
|
/**
|
|
|
|
* build_changeset_next_level() - add level of overlay changeset
|
|
|
|
* @ovcs: overlay changeset
|
|
|
|
* @target_node: where to place @overlay_node in live tree
|
|
|
|
* @overlay_node: node from within an overlay device tree fragment
|
2014-10-29 04:35:58 +08:00
|
|
|
*
|
2017-10-18 07:36:23 +08: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-18 07:36:21 +08:00
|
|
|
*
|
|
|
|
* Do not allow symbols node to have any children.
|
2017-10-18 07:36:23 +08:00
|
|
|
*
|
|
|
|
* Returns 0 on success, -ENOMEM if memory allocation failure, or -EINVAL if
|
|
|
|
* invalid @overlay_node.
|
2014-10-29 04:35:58 +08:00
|
|
|
*/
|
2017-10-18 07:36:23 +08:00
|
|
|
static int build_changeset_next_level(struct overlay_changeset *ovcs,
|
|
|
|
struct device_node *target_node,
|
2017-10-18 07:36:30 +08:00
|
|
|
const struct device_node *overlay_node)
|
2014-10-29 04:35:58 +08:00
|
|
|
{
|
|
|
|
struct device_node *child;
|
|
|
|
struct property *prop;
|
|
|
|
int ret;
|
|
|
|
|
2017-10-18 07:36:23 +08:00
|
|
|
for_each_property_of_node(overlay_node, prop) {
|
2017-10-18 07:36:30 +08:00
|
|
|
ret = add_changeset_property(ovcs, target_node, prop, 0);
|
2014-10-29 04:35:58 +08:00
|
|
|
if (ret) {
|
2017-10-18 07:36:26 +08:00
|
|
|
pr_debug("Failed to apply prop @%pOF/%s, err=%d\n",
|
|
|
|
target_node, prop->name, ret);
|
2014-10-29 04:35:58 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-18 07:36:23 +08:00
|
|
|
for_each_child_of_node(overlay_node, child) {
|
|
|
|
ret = add_changeset_node(ovcs, target_node, child);
|
2017-10-18 07:36:22 +08:00
|
|
|
if (ret) {
|
2017-10-18 07:36:26 +08:00
|
|
|
pr_debug("Failed to apply node @%pOF/%s, err=%d\n",
|
|
|
|
target_node, child->name, ret);
|
2015-10-22 17:02:48 +08:00
|
|
|
of_node_put(child);
|
2014-10-29 04:35:58 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-10-18 07:36:30 +08:00
|
|
|
/*
|
|
|
|
* Add the properties from __overlay__ node to the @ovcs->cset changeset.
|
|
|
|
*/
|
|
|
|
static int build_changeset_symbols_node(struct overlay_changeset *ovcs,
|
|
|
|
struct device_node *target_node,
|
|
|
|
const struct device_node *overlay_symbols_node)
|
|
|
|
{
|
|
|
|
struct property *prop;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
for_each_property_of_node(overlay_symbols_node, prop) {
|
|
|
|
ret = add_changeset_property(ovcs, target_node, prop, 1);
|
|
|
|
if (ret) {
|
|
|
|
pr_debug("Failed to apply prop @%pOF/%s, err=%d\n",
|
|
|
|
target_node, prop->name, ret);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-10-29 04:35:58 +08:00
|
|
|
/**
|
2017-10-18 07:36:23 +08:00
|
|
|
* build_changeset() - populate overlay changeset in @ovcs from @ovcs->fragments
|
|
|
|
* @ovcs: Overlay changeset
|
2014-10-29 04:35:58 +08:00
|
|
|
*
|
2017-10-18 07:36:23 +08: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.
|
|
|
|
*
|
|
|
|
* Returns 0 on success, -ENOMEM if memory allocation failure, or -EINVAL if
|
|
|
|
* invalid overlay in @ovcs->fragments[].
|
2014-10-29 04:35:58 +08:00
|
|
|
*/
|
2017-10-18 07:36:23 +08:00
|
|
|
static int build_changeset(struct overlay_changeset *ovcs)
|
2014-10-29 04:35:58 +08:00
|
|
|
{
|
2017-10-18 07:36:30 +08:00
|
|
|
struct fragment *fragment;
|
|
|
|
int fragments_count, i, ret;
|
2014-10-29 04:35:58 +08:00
|
|
|
|
2017-10-18 07:36:30 +08: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-29 04:35:58 +08:00
|
|
|
|
2017-10-18 07:36:23 +08:00
|
|
|
ret = build_changeset_next_level(ovcs, fragment->target,
|
2017-10-18 07:36:30 +08:00
|
|
|
fragment->overlay);
|
|
|
|
if (ret) {
|
|
|
|
pr_debug("apply failed '%pOF'\n", fragment->target);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ovcs->symbols_fragment) {
|
|
|
|
fragment = &ovcs->fragments[ovcs->count - 1];
|
|
|
|
ret = build_changeset_symbols_node(ovcs, fragment->target,
|
|
|
|
fragment->overlay);
|
2017-10-18 07:36:23 +08:00
|
|
|
if (ret) {
|
2017-10-18 07:36:26 +08:00
|
|
|
pr_debug("apply failed '%pOF'\n", fragment->target);
|
2017-10-18 07:36:23 +08:00
|
|
|
return ret;
|
2014-10-29 04:35:58 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Find the target node using a number of different strategies
|
2017-10-18 07:36:21 +08:00
|
|
|
* in order of preference:
|
2014-10-29 04:35:58 +08:00
|
|
|
*
|
2017-10-18 07:36:21 +08:00
|
|
|
* 1) "target" property containing the phandle of the target
|
|
|
|
* 2) "target-path" property containing the path of the target
|
2014-10-29 04:35:58 +08:00
|
|
|
*/
|
|
|
|
static struct device_node *find_target_node(struct device_node *info_node)
|
|
|
|
{
|
2018-02-12 16:25:04 +08:00
|
|
|
struct device_node *node;
|
2014-10-29 04:35:58 +08:00
|
|
|
const char *path;
|
|
|
|
u32 val;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = of_property_read_u32(info_node, "target", &val);
|
2018-02-12 16:25:04 +08: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-29 04:35:58 +08:00
|
|
|
|
|
|
|
ret = of_property_read_string(info_node, "target-path", &path);
|
2018-02-12 16:25:04 +08:00
|
|
|
if (!ret) {
|
|
|
|
node = of_find_node_by_path(path);
|
|
|
|
if (!node)
|
|
|
|
pr_err("find target, node: %pOF, path '%s' not found\n",
|
|
|
|
info_node, path);
|
|
|
|
return node;
|
|
|
|
}
|
2014-10-29 04:35:58 +08:00
|
|
|
|
2018-02-12 16:25:04 +08:00
|
|
|
pr_err("find target, node: %pOF, no target property\n", info_node);
|
2014-10-29 04:35:58 +08:00
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-10-18 07:36:23 +08:00
|
|
|
* init_overlay_changeset() - initialize overlay changeset from overlay tree
|
2018-02-12 16:19:42 +08:00
|
|
|
* @ovcs: Overlay changeset to build
|
|
|
|
* @fdt: the FDT that was unflattened to create @tree
|
2017-10-18 07:36:23 +08:00
|
|
|
* @tree: Contains all the overlay fragments and overlay fixup nodes
|
2014-10-29 04:35:58 +08:00
|
|
|
*
|
2017-10-18 07:36:23 +08:00
|
|
|
* Initialize @ovcs. Populate @ovcs->fragments with node information from
|
|
|
|
* the top level of @tree. The relevant top level nodes are the fragment
|
|
|
|
* nodes and the __symbols__ node. Any other top level node will be ignored.
|
2014-10-29 04:35:58 +08:00
|
|
|
*
|
2017-10-18 07:36:23 +08:00
|
|
|
* Returns 0 on success, -ENOMEM if memory allocation failure, -EINVAL if error
|
2017-10-18 07:36:25 +08:00
|
|
|
* detected in @tree, or -ENOSPC if idr_alloc() error.
|
2014-10-29 04:35:58 +08:00
|
|
|
*/
|
2017-10-18 07:36:23 +08:00
|
|
|
static int init_overlay_changeset(struct overlay_changeset *ovcs,
|
2018-02-12 16:19:42 +08:00
|
|
|
const void *fdt, struct device_node *tree)
|
2014-10-29 04:35:58 +08:00
|
|
|
{
|
2017-10-18 07:36:25 +08:00
|
|
|
struct device_node *node, *overlay_node;
|
2017-10-18 07:36:23 +08:00
|
|
|
struct fragment *fragment;
|
|
|
|
struct fragment *fragments;
|
2017-12-05 23:27:02 +08:00
|
|
|
int cnt, id, ret;
|
2014-10-29 04:35:58 +08:00
|
|
|
|
2017-10-18 07:36:26 +08:00
|
|
|
/*
|
|
|
|
* Warn for some issues. Can not return -EINVAL for these until
|
|
|
|
* of_unittest_apply_overlay() is fixed to pass these checks.
|
|
|
|
*/
|
|
|
|
if (!of_node_check_flag(tree, OF_DYNAMIC))
|
|
|
|
pr_debug("%s() tree is not dynamic\n", __func__);
|
|
|
|
|
|
|
|
if (!of_node_check_flag(tree, OF_DETACHED))
|
|
|
|
pr_debug("%s() tree is not detached\n", __func__);
|
|
|
|
|
|
|
|
if (!of_node_is_root(tree))
|
|
|
|
pr_debug("%s() tree is not root\n", __func__);
|
|
|
|
|
2017-10-18 07:36:31 +08:00
|
|
|
ovcs->overlay_tree = tree;
|
2018-02-12 16:19:42 +08:00
|
|
|
ovcs->fdt = fdt;
|
2017-10-18 07:36:31 +08:00
|
|
|
|
2017-10-18 07:36:25 +08:00
|
|
|
INIT_LIST_HEAD(&ovcs->ovcs_list);
|
|
|
|
|
|
|
|
of_changeset_init(&ovcs->cset);
|
|
|
|
|
2017-12-05 23:27:02 +08:00
|
|
|
id = idr_alloc(&ovcs_idr, ovcs, 1, 0, GFP_KERNEL);
|
|
|
|
if (id <= 0)
|
|
|
|
return id;
|
2017-10-18 07:36:25 +08:00
|
|
|
|
2014-10-29 04:35:58 +08:00
|
|
|
cnt = 0;
|
|
|
|
|
2017-10-18 07:36:25 +08:00
|
|
|
/* fragment nodes */
|
|
|
|
for_each_child_of_node(tree, node) {
|
|
|
|
overlay_node = of_get_child_by_name(node, "__overlay__");
|
|
|
|
if (overlay_node) {
|
|
|
|
cnt++;
|
|
|
|
of_node_put(overlay_node);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
node = of_get_child_by_name(tree, "__symbols__");
|
|
|
|
if (node) {
|
2017-07-20 00:25:22 +08:00
|
|
|
cnt++;
|
2017-10-18 07:36:25 +08:00
|
|
|
of_node_put(node);
|
|
|
|
}
|
2017-07-20 00:25:22 +08:00
|
|
|
|
2017-10-18 07:36:23 +08:00
|
|
|
fragments = kcalloc(cnt, sizeof(*fragments), GFP_KERNEL);
|
2017-10-18 07:36:25 +08:00
|
|
|
if (!fragments) {
|
|
|
|
ret = -ENOMEM;
|
|
|
|
goto err_free_idr;
|
|
|
|
}
|
2014-10-29 04:35:58 +08:00
|
|
|
|
|
|
|
cnt = 0;
|
|
|
|
for_each_child_of_node(tree, node) {
|
2017-12-08 21:13:02 +08:00
|
|
|
overlay_node = of_get_child_by_name(node, "__overlay__");
|
2017-12-08 21:13:03 +08:00
|
|
|
if (!overlay_node)
|
|
|
|
continue;
|
2017-11-28 16:26:33 +08:00
|
|
|
|
2017-12-08 21:13:03 +08:00
|
|
|
fragment = &fragments[cnt];
|
|
|
|
fragment->overlay = overlay_node;
|
|
|
|
fragment->target = find_target_node(node);
|
|
|
|
if (!fragment->target) {
|
|
|
|
of_node_put(fragment->overlay);
|
|
|
|
ret = -EINVAL;
|
|
|
|
goto err_free_fragments;
|
2017-10-18 07:36:25 +08:00
|
|
|
}
|
2017-12-08 21:13:03 +08:00
|
|
|
|
|
|
|
cnt++;
|
2014-10-29 04:35:58 +08:00
|
|
|
}
|
|
|
|
|
2017-10-18 07:36:30 +08:00
|
|
|
/*
|
|
|
|
* if there is a symbols fragment in ovcs->fragments[i] it is
|
|
|
|
* the final element in the array
|
|
|
|
*/
|
2017-07-20 00:25:22 +08:00
|
|
|
node = of_get_child_by_name(tree, "__symbols__");
|
|
|
|
if (node) {
|
2017-10-18 07:36:30 +08:00
|
|
|
ovcs->symbols_fragment = 1;
|
2017-10-18 07:36:23 +08:00
|
|
|
fragment = &fragments[cnt];
|
|
|
|
fragment->overlay = node;
|
|
|
|
fragment->target = of_find_node_by_path("/__symbols__");
|
2017-07-20 00:25:22 +08:00
|
|
|
|
2017-10-18 07:36:23 +08:00
|
|
|
if (!fragment->target) {
|
2017-10-20 05:38:11 +08:00
|
|
|
pr_err("symbols in overlay, but not in live tree\n");
|
2017-10-18 07:36:25 +08:00
|
|
|
ret = -EINVAL;
|
|
|
|
goto err_free_fragments;
|
2017-07-20 00:25:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
cnt++;
|
|
|
|
}
|
|
|
|
|
2017-10-18 07:36:22 +08:00
|
|
|
if (!cnt) {
|
2018-02-12 16:19:42 +08:00
|
|
|
pr_err("no fragments or symbols in overlay\n");
|
2017-10-18 07:36:25 +08:00
|
|
|
ret = -EINVAL;
|
|
|
|
goto err_free_fragments;
|
2014-10-29 04:35:58 +08:00
|
|
|
}
|
|
|
|
|
2017-12-05 23:27:02 +08:00
|
|
|
ovcs->id = id;
|
2017-10-18 07:36:23 +08:00
|
|
|
ovcs->count = cnt;
|
|
|
|
ovcs->fragments = fragments;
|
2014-10-29 04:35:58 +08:00
|
|
|
|
|
|
|
return 0;
|
2017-10-18 07:36:25 +08:00
|
|
|
|
|
|
|
err_free_fragments:
|
|
|
|
kfree(fragments);
|
|
|
|
err_free_idr:
|
2017-12-05 23:27:02 +08:00
|
|
|
idr_remove(&ovcs_idr, id);
|
2017-10-18 07:36:25 +08:00
|
|
|
|
2017-10-18 07:36:26 +08:00
|
|
|
pr_err("%s() failed, ret = %d\n", __func__, ret);
|
|
|
|
|
2017-10-18 07:36:25 +08:00
|
|
|
return ret;
|
2014-10-29 04:35:58 +08:00
|
|
|
}
|
|
|
|
|
2017-10-18 07:36:25 +08:00
|
|
|
static void free_overlay_changeset(struct overlay_changeset *ovcs)
|
2014-10-29 04:35:58 +08:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2017-12-05 23:27:02 +08:00
|
|
|
if (ovcs->cset.entries.next)
|
|
|
|
of_changeset_destroy(&ovcs->cset);
|
2017-10-18 07:36:25 +08:00
|
|
|
|
|
|
|
if (ovcs->id)
|
|
|
|
idr_remove(&ovcs_idr, ovcs->id);
|
|
|
|
|
|
|
|
for (i = 0; i < ovcs->count; i++) {
|
2017-10-18 07:36:23 +08:00
|
|
|
of_node_put(ovcs->fragments[i].target);
|
|
|
|
of_node_put(ovcs->fragments[i].overlay);
|
2014-10-29 04:35:58 +08:00
|
|
|
}
|
2017-10-18 07:36:23 +08:00
|
|
|
kfree(ovcs->fragments);
|
2014-10-29 04:35:58 +08:00
|
|
|
|
2018-02-12 16:19:42 +08:00
|
|
|
/*
|
|
|
|
* TODO
|
|
|
|
*
|
|
|
|
* would like to: kfree(ovcs->overlay_tree);
|
|
|
|
* but can not since drivers may have pointers into this data
|
|
|
|
*
|
|
|
|
* would like to: kfree(ovcs->fdt);
|
|
|
|
* but can not since drivers may have pointers into this data
|
|
|
|
*/
|
|
|
|
|
2017-10-18 07:36:25 +08:00
|
|
|
kfree(ovcs);
|
|
|
|
}
|
2014-10-29 04:35:58 +08:00
|
|
|
|
2018-02-12 16:19:42 +08:00
|
|
|
/*
|
|
|
|
* internal documentation
|
|
|
|
*
|
2017-10-18 07:36:23 +08:00
|
|
|
* of_overlay_apply() - Create and apply an overlay changeset
|
2018-02-12 16:19:42 +08:00
|
|
|
* @fdt: the FDT that was unflattened to create @tree
|
2017-10-18 07:36:23 +08:00
|
|
|
* @tree: Expanded overlay device tree
|
2017-10-18 07:36:26 +08:00
|
|
|
* @ovcs_id: Pointer to overlay changeset id
|
|
|
|
*
|
|
|
|
* Creates and applies an overlay changeset.
|
2014-10-29 04:35:58 +08:00
|
|
|
*
|
2017-10-18 07:36:26 +08:00
|
|
|
* If an error occurs in a pre-apply notifier, then no changes are made
|
|
|
|
* to the device tree.
|
2014-10-29 04:35:58 +08:00
|
|
|
*
|
2017-10-18 07:36:26 +08:00
|
|
|
|
|
|
|
* A non-zero return value will not have created the changeset if error is from:
|
|
|
|
* - parameter checks
|
|
|
|
* - building the changeset
|
2017-11-28 16:25:23 +08:00
|
|
|
* - overlay changeset pre-apply notifier
|
2017-10-18 07:36:26 +08:00
|
|
|
*
|
|
|
|
* If an error is returned by an overlay changeset pre-apply notifier
|
|
|
|
* then no further overlay changeset pre-apply notifier will be called.
|
|
|
|
*
|
|
|
|
* A non-zero return value will have created the changeset if error is from:
|
|
|
|
* - overlay changeset entry notifier
|
2017-11-28 16:25:23 +08:00
|
|
|
* - overlay changeset post-apply notifier
|
2017-10-18 07:36:26 +08:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* Returns 0 on success, or a negative error number. Overlay changeset
|
|
|
|
* id is returned to *ovcs_id.
|
2014-10-29 04:35:58 +08:00
|
|
|
*/
|
2017-10-18 07:36:26 +08:00
|
|
|
|
2018-02-12 16:19:42 +08:00
|
|
|
static int of_overlay_apply(const void *fdt, struct device_node *tree,
|
|
|
|
int *ovcs_id)
|
2014-10-29 04:35:58 +08:00
|
|
|
{
|
2017-10-18 07:36:23 +08:00
|
|
|
struct overlay_changeset *ovcs;
|
2017-10-18 07:36:26 +08:00
|
|
|
int ret = 0, ret_revert, ret_tmp;
|
|
|
|
|
2018-02-12 16:19:42 +08:00
|
|
|
/*
|
|
|
|
* As of this point, fdt and tree belong to the overlay changeset.
|
|
|
|
* overlay changeset code is responsible for freeing them.
|
|
|
|
*/
|
2017-10-18 07:36:26 +08:00
|
|
|
|
|
|
|
if (devicetree_corrupt()) {
|
|
|
|
pr_err("devicetree state suspect, refuse to apply overlay\n");
|
2018-02-12 16:19:42 +08:00
|
|
|
kfree(fdt);
|
|
|
|
kfree(tree);
|
2017-10-18 07:36:26 +08:00
|
|
|
ret = -EBUSY;
|
|
|
|
goto out;
|
|
|
|
}
|
2014-10-29 04:35:58 +08:00
|
|
|
|
2017-10-18 07:36:23 +08:00
|
|
|
ovcs = kzalloc(sizeof(*ovcs), GFP_KERNEL);
|
2017-10-18 07:36:26 +08:00
|
|
|
if (!ovcs) {
|
2018-02-12 16:19:42 +08:00
|
|
|
kfree(fdt);
|
|
|
|
kfree(tree);
|
2017-10-18 07:36:26 +08:00
|
|
|
ret = -ENOMEM;
|
|
|
|
goto out;
|
|
|
|
}
|
2014-10-29 04:35:58 +08:00
|
|
|
|
2017-10-18 07:36:29 +08:00
|
|
|
of_overlay_mutex_lock();
|
of: overlay: Fix (un)locking in of_overlay_apply()
The special overlay mutex is taken first, hence it should be released
last in the error path.
of_resolve_phandles() must be called with of_mutex held. Without it, a
node and new phandle could be added via of_attach_node(), making the max
phandle wrong.
free_overlay_changeset() must be called with of_mutex held, if any
non-trivial cleanup is to be done.
Hence move "mutex_lock(&of_mutex)" up, as suggested by Frank, and merge
the two tail statements of the success and error paths, now they became
identical.
Note that while the two mutexes are adjacent, we still need both:
__of_changeset_apply_notify(), which is called by __of_changeset_apply()
unlocks of_mutex, then does notifications then locks of_mutex. So the
mutex get released in the middle of of_overlay_apply()
Fixes: f948d6d8b792bb90 ("of: overlay: avoid race condition between applying multiple overlays")
Signed-off-by: Geert Uytterhoeven <geert+renesas@glider.be>
Reviewed-by: Frank Rowand <frank.rowand@sony.com>
Signed-off-by: Rob Herring <robh@kernel.org>
2017-12-05 23:27:03 +08:00
|
|
|
mutex_lock(&of_mutex);
|
2017-10-18 07:36:29 +08:00
|
|
|
|
|
|
|
ret = of_resolve_phandles(tree);
|
|
|
|
if (ret)
|
2018-02-12 16:19:42 +08:00
|
|
|
goto err_free_tree;
|
2014-10-29 04:35:58 +08:00
|
|
|
|
2018-02-12 16:19:42 +08:00
|
|
|
ret = init_overlay_changeset(ovcs, fdt, tree);
|
2017-10-18 07:36:26 +08:00
|
|
|
if (ret)
|
2018-02-12 16:19:42 +08:00
|
|
|
goto err_free_tree;
|
2014-10-29 04:35:58 +08:00
|
|
|
|
2018-02-12 16:19:42 +08:00
|
|
|
/*
|
|
|
|
* after overlay_notify(), ovcs->overlay_tree related pointers may have
|
|
|
|
* leaked to drivers, so can not kfree() tree, aka ovcs->overlay_tree;
|
|
|
|
* and can not free fdt, aka ovcs->fdt
|
|
|
|
*/
|
2017-10-18 07:36:23 +08:00
|
|
|
ret = overlay_notify(ovcs, OF_OVERLAY_PRE_APPLY);
|
2017-10-18 07:36:26 +08:00
|
|
|
if (ret) {
|
|
|
|
pr_err("overlay changeset pre-apply notify error %d\n", ret);
|
2017-10-18 07:36:25 +08:00
|
|
|
goto err_free_overlay_changeset;
|
2016-11-02 03:14:22 +08:00
|
|
|
}
|
|
|
|
|
2017-10-18 07:36:23 +08:00
|
|
|
ret = build_changeset(ovcs);
|
|
|
|
if (ret)
|
2017-10-18 07:36:25 +08:00
|
|
|
goto err_free_overlay_changeset;
|
2016-06-15 21:32:18 +08:00
|
|
|
|
2017-10-18 07:36:26 +08: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;
|
|
|
|
}
|
2017-10-18 07:36:25 +08:00
|
|
|
goto err_free_overlay_changeset;
|
2017-10-18 07:36:26 +08:00
|
|
|
}
|
2014-10-29 04:35:58 +08:00
|
|
|
|
2017-11-28 16:26:33 +08:00
|
|
|
ret = __of_changeset_apply_notify(&ovcs->cset);
|
|
|
|
if (ret)
|
|
|
|
pr_err("overlay changeset entry notify error %d\n", ret);
|
|
|
|
/* notify failure is not fatal, continue */
|
|
|
|
|
2017-10-18 07:36:23 +08:00
|
|
|
list_add_tail(&ovcs->ovcs_list, &ovcs_list);
|
2017-10-18 07:36:26 +08:00
|
|
|
*ovcs_id = ovcs->id;
|
|
|
|
|
|
|
|
ret_tmp = overlay_notify(ovcs, OF_OVERLAY_POST_APPLY);
|
|
|
|
if (ret_tmp) {
|
|
|
|
pr_err("overlay changeset post-apply notify error %d\n",
|
|
|
|
ret_tmp);
|
|
|
|
if (!ret)
|
|
|
|
ret = ret_tmp;
|
|
|
|
}
|
2016-11-02 03:14:22 +08:00
|
|
|
|
of: overlay: Fix (un)locking in of_overlay_apply()
The special overlay mutex is taken first, hence it should be released
last in the error path.
of_resolve_phandles() must be called with of_mutex held. Without it, a
node and new phandle could be added via of_attach_node(), making the max
phandle wrong.
free_overlay_changeset() must be called with of_mutex held, if any
non-trivial cleanup is to be done.
Hence move "mutex_lock(&of_mutex)" up, as suggested by Frank, and merge
the two tail statements of the success and error paths, now they became
identical.
Note that while the two mutexes are adjacent, we still need both:
__of_changeset_apply_notify(), which is called by __of_changeset_apply()
unlocks of_mutex, then does notifications then locks of_mutex. So the
mutex get released in the middle of of_overlay_apply()
Fixes: f948d6d8b792bb90 ("of: overlay: avoid race condition between applying multiple overlays")
Signed-off-by: Geert Uytterhoeven <geert+renesas@glider.be>
Reviewed-by: Frank Rowand <frank.rowand@sony.com>
Signed-off-by: Rob Herring <robh@kernel.org>
2017-12-05 23:27:03 +08:00
|
|
|
goto out_unlock;
|
2017-10-18 07:36:29 +08:00
|
|
|
|
2018-02-12 16:19:42 +08:00
|
|
|
err_free_tree:
|
|
|
|
kfree(fdt);
|
|
|
|
kfree(tree);
|
|
|
|
|
2017-10-18 07:36:25 +08:00
|
|
|
err_free_overlay_changeset:
|
|
|
|
free_overlay_changeset(ovcs);
|
2014-10-29 04:35:58 +08:00
|
|
|
|
of: overlay: Fix (un)locking in of_overlay_apply()
The special overlay mutex is taken first, hence it should be released
last in the error path.
of_resolve_phandles() must be called with of_mutex held. Without it, a
node and new phandle could be added via of_attach_node(), making the max
phandle wrong.
free_overlay_changeset() must be called with of_mutex held, if any
non-trivial cleanup is to be done.
Hence move "mutex_lock(&of_mutex)" up, as suggested by Frank, and merge
the two tail statements of the success and error paths, now they became
identical.
Note that while the two mutexes are adjacent, we still need both:
__of_changeset_apply_notify(), which is called by __of_changeset_apply()
unlocks of_mutex, then does notifications then locks of_mutex. So the
mutex get released in the middle of of_overlay_apply()
Fixes: f948d6d8b792bb90 ("of: overlay: avoid race condition between applying multiple overlays")
Signed-off-by: Geert Uytterhoeven <geert+renesas@glider.be>
Reviewed-by: Frank Rowand <frank.rowand@sony.com>
Signed-off-by: Rob Herring <robh@kernel.org>
2017-12-05 23:27:03 +08:00
|
|
|
out_unlock:
|
2014-10-29 04:35:58 +08:00
|
|
|
mutex_unlock(&of_mutex);
|
of: overlay: Fix (un)locking in of_overlay_apply()
The special overlay mutex is taken first, hence it should be released
last in the error path.
of_resolve_phandles() must be called with of_mutex held. Without it, a
node and new phandle could be added via of_attach_node(), making the max
phandle wrong.
free_overlay_changeset() must be called with of_mutex held, if any
non-trivial cleanup is to be done.
Hence move "mutex_lock(&of_mutex)" up, as suggested by Frank, and merge
the two tail statements of the success and error paths, now they became
identical.
Note that while the two mutexes are adjacent, we still need both:
__of_changeset_apply_notify(), which is called by __of_changeset_apply()
unlocks of_mutex, then does notifications then locks of_mutex. So the
mutex get released in the middle of of_overlay_apply()
Fixes: f948d6d8b792bb90 ("of: overlay: avoid race condition between applying multiple overlays")
Signed-off-by: Geert Uytterhoeven <geert+renesas@glider.be>
Reviewed-by: Frank Rowand <frank.rowand@sony.com>
Signed-off-by: Rob Herring <robh@kernel.org>
2017-12-05 23:27:03 +08:00
|
|
|
of_overlay_mutex_unlock();
|
2014-10-29 04:35:58 +08:00
|
|
|
|
2017-10-18 07:36:26 +08:00
|
|
|
out:
|
|
|
|
pr_debug("%s() err=%d\n", __func__, ret);
|
|
|
|
|
2017-10-18 07:36:23 +08:00
|
|
|
return ret;
|
2014-10-29 04:35:58 +08:00
|
|
|
}
|
2018-02-12 16:19:42 +08:00
|
|
|
|
|
|
|
int of_overlay_fdt_apply(const void *overlay_fdt, u32 overlay_fdt_size,
|
|
|
|
int *ovcs_id)
|
|
|
|
{
|
|
|
|
const void *new_fdt;
|
|
|
|
int ret;
|
|
|
|
u32 size;
|
|
|
|
struct device_node *overlay_root;
|
|
|
|
|
|
|
|
*ovcs_id = 0;
|
|
|
|
ret = 0;
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Must create permanent copy of FDT because of_fdt_unflatten_tree()
|
|
|
|
* will create pointers to the passed in FDT in the unflattened tree.
|
|
|
|
*/
|
|
|
|
new_fdt = kmemdup(overlay_fdt, size, GFP_KERNEL);
|
|
|
|
if (!new_fdt)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
of_fdt_unflatten_tree(new_fdt, NULL, &overlay_root);
|
|
|
|
if (!overlay_root) {
|
|
|
|
pr_err("unable to unflatten overlay_fdt\n");
|
|
|
|
ret = -EINVAL;
|
|
|
|
goto out_free_new_fdt;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = of_overlay_apply(new_fdt, overlay_root, ovcs_id);
|
|
|
|
if (ret < 0) {
|
|
|
|
/*
|
|
|
|
* new_fdt and overlay_root now belong to the overlay
|
|
|
|
* changeset.
|
|
|
|
* overlay changeset code is responsible for freeing them.
|
|
|
|
*/
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
|
|
out_free_new_fdt:
|
|
|
|
kfree(new_fdt);
|
|
|
|
|
|
|
|
out:
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(of_overlay_fdt_apply);
|
2014-10-29 04:35:58 +08:00
|
|
|
|
2017-10-18 07:36:21 +08:00
|
|
|
/*
|
2017-10-18 07:36:23 +08:00
|
|
|
* Find @np in @tree.
|
|
|
|
*
|
|
|
|
* Returns 1 if @np is @tree or is contained in @tree, else 0
|
2017-10-18 07:36:21 +08:00
|
|
|
*/
|
2017-10-18 07:36:23 +08:00
|
|
|
static int find_node(struct device_node *tree, struct device_node *np)
|
2014-10-29 04:35:58 +08:00
|
|
|
{
|
|
|
|
struct device_node *child;
|
|
|
|
|
2017-10-18 07:36:23 +08:00
|
|
|
if (tree == np)
|
2014-10-29 04:35:58 +08:00
|
|
|
return 1;
|
|
|
|
|
|
|
|
for_each_child_of_node(tree, child) {
|
2017-10-18 07:36:23 +08:00
|
|
|
if (find_node(child, np)) {
|
2015-10-22 17:02:48 +08:00
|
|
|
of_node_put(child);
|
2014-10-29 04:35:58 +08:00
|
|
|
return 1;
|
2015-10-22 17:02:48 +08:00
|
|
|
}
|
2014-10-29 04:35:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-10-18 07:36:21 +08:00
|
|
|
/*
|
2017-10-18 07:36:27 +08:00
|
|
|
* Is @remove_ce_node a child of, a parent of, or the same as any
|
2017-10-18 07:36:23 +08:00
|
|
|
* node in an overlay changeset more topmost than @remove_ovcs?
|
|
|
|
*
|
|
|
|
* Returns 1 if found, else 0
|
2017-10-18 07:36:21 +08:00
|
|
|
*/
|
2017-10-18 07:36:27 +08:00
|
|
|
static int node_overlaps_later_cs(struct overlay_changeset *remove_ovcs,
|
|
|
|
struct device_node *remove_ce_node)
|
2014-10-29 04:35:58 +08:00
|
|
|
{
|
2017-10-18 07:36:23 +08:00
|
|
|
struct overlay_changeset *ovcs;
|
2014-10-29 04:35:58 +08:00
|
|
|
struct of_changeset_entry *ce;
|
|
|
|
|
2017-10-18 07:36:23 +08:00
|
|
|
list_for_each_entry_reverse(ovcs, &ovcs_list, ovcs_list) {
|
|
|
|
if (ovcs == remove_ovcs)
|
2014-10-29 04:35:58 +08:00
|
|
|
break;
|
|
|
|
|
2017-10-18 07:36:23 +08:00
|
|
|
list_for_each_entry(ce, &ovcs->cset.entries, node) {
|
2017-10-18 07:36:27 +08:00
|
|
|
if (find_node(ce->np, remove_ce_node)) {
|
|
|
|
pr_err("%s: #%d overlaps with #%d @%pOF\n",
|
2017-10-18 07:36:23 +08:00
|
|
|
__func__, remove_ovcs->id, ovcs->id,
|
2017-10-18 07:36:27 +08: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-18 07:36:23 +08:00
|
|
|
return 1;
|
2014-10-29 04:35:58 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-18 07:36:23 +08:00
|
|
|
return 0;
|
2014-10-29 04:35:58 +08: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
|
|
|
|
* device node and is closest to the tail, then removal is not permited.
|
|
|
|
*/
|
2017-10-18 07:36:23 +08:00
|
|
|
static int overlay_removal_is_ok(struct overlay_changeset *remove_ovcs)
|
2014-10-29 04:35:58 +08:00
|
|
|
{
|
2017-10-18 07:36:23 +08:00
|
|
|
struct of_changeset_entry *remove_ce;
|
2014-10-29 04:35:58 +08:00
|
|
|
|
2017-10-18 07:36:23 +08:00
|
|
|
list_for_each_entry(remove_ce, &remove_ovcs->cset.entries, node) {
|
2017-10-18 07:36:27 +08:00
|
|
|
if (node_overlaps_later_cs(remove_ovcs, remove_ce->np)) {
|
2017-10-18 07:36:23 +08:00
|
|
|
pr_err("overlay #%d is not topmost\n", remove_ovcs->id);
|
2014-10-29 04:35:58 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-10-18 07:36:23 +08:00
|
|
|
* of_overlay_remove() - Revert and free an overlay changeset
|
2017-10-18 07:36:26 +08:00
|
|
|
* @ovcs_id: Pointer to overlay changeset id
|
2014-10-29 04:35:58 +08:00
|
|
|
*
|
2017-10-18 07:36:26 +08:00
|
|
|
* Removes an overlay if it is permissible. @ovcs_id was previously returned
|
2017-10-18 07:36:23 +08:00
|
|
|
* by of_overlay_apply().
|
2014-10-29 04:35:58 +08:00
|
|
|
*
|
2017-10-18 07:36:26 +08: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 16:25:23 +08:00
|
|
|
* - overlay changeset pre-remove notifier
|
2017-10-18 07:36:26 +08: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 16:25:23 +08:00
|
|
|
* - overlay changeset post-remove notifier
|
2017-10-18 07:36:26 +08:00
|
|
|
*
|
|
|
|
* If an error is returned by an overlay changeset post-remove notifier
|
|
|
|
* then no further overlay changeset post-remove notifier will be called.
|
|
|
|
*
|
|
|
|
* Returns 0 on success, or a negative error number. *ovcs_id is set to
|
|
|
|
* zero after reverting the changeset, even if a subsequent error occurs.
|
2014-10-29 04:35:58 +08:00
|
|
|
*/
|
2017-10-18 07:36:26 +08:00
|
|
|
int of_overlay_remove(int *ovcs_id)
|
2014-10-29 04:35:58 +08:00
|
|
|
{
|
2017-10-18 07:36:23 +08:00
|
|
|
struct overlay_changeset *ovcs;
|
2017-10-18 07:36:26 +08:00
|
|
|
int ret, ret_apply, ret_tmp;
|
|
|
|
|
|
|
|
ret = 0;
|
|
|
|
|
|
|
|
if (devicetree_corrupt()) {
|
|
|
|
pr_err("suspect devicetree state, refuse to remove overlay\n");
|
|
|
|
ret = -EBUSY;
|
|
|
|
goto out;
|
|
|
|
}
|
2014-10-29 04:35:58 +08:00
|
|
|
|
|
|
|
mutex_lock(&of_mutex);
|
|
|
|
|
2017-10-18 07:36:26 +08:00
|
|
|
ovcs = idr_find(&ovcs_idr, *ovcs_id);
|
2017-10-18 07:36:23 +08:00
|
|
|
if (!ovcs) {
|
|
|
|
ret = -ENODEV;
|
2017-10-18 07:36:26 +08:00
|
|
|
pr_err("remove: Could not find overlay #%d\n", *ovcs_id);
|
|
|
|
goto out_unlock;
|
2014-10-29 04:35:58 +08:00
|
|
|
}
|
|
|
|
|
2017-10-18 07:36:23 +08:00
|
|
|
if (!overlay_removal_is_ok(ovcs)) {
|
|
|
|
ret = -EBUSY;
|
2017-10-18 07:36:26 +08:00
|
|
|
goto out_unlock;
|
2014-10-29 04:35:58 +08:00
|
|
|
}
|
|
|
|
|
2017-10-18 07:36:26 +08:00
|
|
|
ret = overlay_notify(ovcs, OF_OVERLAY_PRE_REMOVE);
|
|
|
|
if (ret) {
|
|
|
|
pr_err("overlay changeset pre-remove notify error %d\n", ret);
|
|
|
|
goto out_unlock;
|
|
|
|
}
|
2017-10-18 07:36:25 +08:00
|
|
|
|
2017-10-18 07:36:23 +08:00
|
|
|
list_del(&ovcs->ovcs_list);
|
2017-10-18 07:36:25 +08:00
|
|
|
|
2017-10-18 07:36:26 +08:00
|
|
|
ret_apply = 0;
|
|
|
|
ret = __of_changeset_revert_entries(&ovcs->cset, &ret_apply);
|
|
|
|
if (ret) {
|
|
|
|
if (ret_apply)
|
|
|
|
devicetree_state_flags |= DTSF_REVERT_FAIL;
|
|
|
|
goto out_unlock;
|
|
|
|
}
|
2017-10-18 07:36:25 +08:00
|
|
|
|
2017-11-28 16:26:33 +08:00
|
|
|
ret = __of_changeset_revert_notify(&ovcs->cset);
|
|
|
|
if (ret)
|
|
|
|
pr_err("overlay changeset entry notify error %d\n", ret);
|
|
|
|
/* notify failure is not fatal, continue */
|
|
|
|
|
2017-10-18 07:36:26 +08:00
|
|
|
*ovcs_id = 0;
|
|
|
|
|
|
|
|
ret_tmp = overlay_notify(ovcs, OF_OVERLAY_POST_REMOVE);
|
|
|
|
if (ret_tmp) {
|
|
|
|
pr_err("overlay changeset post-remove notify error %d\n",
|
|
|
|
ret_tmp);
|
|
|
|
if (!ret)
|
|
|
|
ret = ret_tmp;
|
|
|
|
}
|
2017-10-18 07:36:25 +08:00
|
|
|
|
|
|
|
free_overlay_changeset(ovcs);
|
2014-10-29 04:35:58 +08:00
|
|
|
|
2017-10-18 07:36:26 +08:00
|
|
|
out_unlock:
|
2014-10-29 04:35:58 +08:00
|
|
|
mutex_unlock(&of_mutex);
|
|
|
|
|
2017-10-18 07:36:26 +08:00
|
|
|
out:
|
|
|
|
pr_debug("%s() err=%d\n", __func__, ret);
|
|
|
|
|
2017-10-18 07:36:23 +08:00
|
|
|
return ret;
|
2014-10-29 04:35:58 +08:00
|
|
|
}
|
2017-10-18 07:36:23 +08:00
|
|
|
EXPORT_SYMBOL_GPL(of_overlay_remove);
|
2014-10-29 04:35:58 +08:00
|
|
|
|
|
|
|
/**
|
2017-10-18 07:36:23 +08:00
|
|
|
* of_overlay_remove_all() - Reverts and frees all overlay changesets
|
2014-10-29 04:35:58 +08:00
|
|
|
*
|
|
|
|
* Removes all overlays from the system in the correct order.
|
|
|
|
*
|
2015-05-21 20:10:26 +08:00
|
|
|
* Returns 0 on success, or a negative error number
|
2014-10-29 04:35:58 +08:00
|
|
|
*/
|
2017-10-18 07:36:23 +08:00
|
|
|
int of_overlay_remove_all(void)
|
2014-10-29 04:35:58 +08:00
|
|
|
{
|
2017-10-18 07:36:23 +08:00
|
|
|
struct overlay_changeset *ovcs, *ovcs_n;
|
2017-10-18 07:36:25 +08:00
|
|
|
int ret;
|
2014-10-29 04:35:58 +08:00
|
|
|
|
|
|
|
/* the tail of list is guaranteed to be safe to remove */
|
2017-10-18 07:36:23 +08:00
|
|
|
list_for_each_entry_safe_reverse(ovcs, ovcs_n, &ovcs_list, ovcs_list) {
|
2017-10-18 07:36:26 +08:00
|
|
|
ret = of_overlay_remove(&ovcs->id);
|
2017-10-18 07:36:25 +08:00
|
|
|
if (ret)
|
|
|
|
return ret;
|
2014-10-29 04:35:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2017-10-18 07:36:23 +08:00
|
|
|
EXPORT_SYMBOL_GPL(of_overlay_remove_all);
|