LCOV - code coverage report
Current view: top level - drivers/of - overlay.c (source / functions) Hit Total Coverage
Test: gcov_data_raspi2_real_modules_combined.info Lines: 0 385 0.0 %
Date: 2020-09-30 20:25:40 Functions: 0 24 0.0 %
Branches: 0 244 0.0 %

           Branch data     Line data    Source code
       1                 :            : // SPDX-License-Identifier: GPL-2.0
       2                 :            : /*
       3                 :            :  * Functions for working with device tree overlays
       4                 :            :  *
       5                 :            :  * Copyright (C) 2012 Pantelis Antoniou <panto@antoniou-consulting.com>
       6                 :            :  * Copyright (C) 2012 Texas Instruments Inc.
       7                 :            :  */
       8                 :            : 
       9                 :            : #define pr_fmt(fmt)     "OF: overlay: " fmt
      10                 :            : 
      11                 :            : #include <linux/kernel.h>
      12                 :            : #include <linux/module.h>
      13                 :            : #include <linux/of.h>
      14                 :            : #include <linux/of_device.h>
      15                 :            : #include <linux/of_fdt.h>
      16                 :            : #include <linux/string.h>
      17                 :            : #include <linux/ctype.h>
      18                 :            : #include <linux/errno.h>
      19                 :            : #include <linux/slab.h>
      20                 :            : #include <linux/libfdt.h>
      21                 :            : #include <linux/err.h>
      22                 :            : #include <linux/idr.h>
      23                 :            : 
      24                 :            : #include "of_private.h"
      25                 :            : 
      26                 :            : /**
      27                 :            :  * struct target - info about current target node as recursing through overlay
      28                 :            :  * @np:                 node where current level of overlay will be applied
      29                 :            :  * @in_livetree:        @np is a node in the live devicetree
      30                 :            :  *
      31                 :            :  * Used in the algorithm to create the portion of a changeset that describes
      32                 :            :  * an overlay fragment, which is a devicetree subtree.  Initially @np is a node
      33                 :            :  * in the live devicetree where the overlay subtree is targeted to be grafted
      34                 :            :  * into.  When recursing to the next level of the overlay subtree, the target
      35                 :            :  * also recurses to the next level of the live devicetree, as long as overlay
      36                 :            :  * subtree node also exists in the live devicetree.  When a node in the overlay
      37                 :            :  * subtree does not exist at the same level in the live devicetree, target->np
      38                 :            :  * points to a newly allocated node, and all subsequent targets in the subtree
      39                 :            :  * will be newly allocated nodes.
      40                 :            :  */
      41                 :            : struct target {
      42                 :            :         struct device_node *np;
      43                 :            :         bool in_livetree;
      44                 :            : };
      45                 :            : 
      46                 :            : /**
      47                 :            :  * struct fragment - info about fragment nodes in overlay expanded device tree
      48                 :            :  * @target:     target of the overlay operation
      49                 :            :  * @overlay:    pointer to the __overlay__ node
      50                 :            :  */
      51                 :            : struct fragment {
      52                 :            :         struct device_node *overlay;
      53                 :            :         struct device_node *target;
      54                 :            : };
      55                 :            : 
      56                 :            : /**
      57                 :            :  * struct overlay_changeset
      58                 :            :  * @id:                 changeset identifier
      59                 :            :  * @ovcs_list:          list on which we are located
      60                 :            :  * @fdt:                FDT that was unflattened to create @overlay_tree
      61                 :            :  * @overlay_tree:       expanded device tree that contains the fragment nodes
      62                 :            :  * @count:              count of fragment structures
      63                 :            :  * @fragments:          fragment nodes in the overlay expanded device tree
      64                 :            :  * @symbols_fragment:   last element of @fragments[] is the  __symbols__ node
      65                 :            :  * @cset:               changeset to apply fragments to live device tree
      66                 :            :  */
      67                 :            : struct overlay_changeset {
      68                 :            :         int id;
      69                 :            :         struct list_head ovcs_list;
      70                 :            :         const void *fdt;
      71                 :            :         struct device_node *overlay_tree;
      72                 :            :         int count;
      73                 :            :         struct fragment *fragments;
      74                 :            :         bool symbols_fragment;
      75                 :            :         struct of_changeset cset;
      76                 :            : };
      77                 :            : 
      78                 :            : /* flags are sticky - once set, do not reset */
      79                 :            : static int devicetree_state_flags;
      80                 :            : #define DTSF_APPLY_FAIL         0x01
      81                 :            : #define DTSF_REVERT_FAIL        0x02
      82                 :            : 
      83                 :            : /*
      84                 :            :  * If a changeset apply or revert encounters an error, an attempt will
      85                 :            :  * be made to undo partial changes, but may fail.  If the undo fails
      86                 :            :  * we do not know the state of the devicetree.
      87                 :            :  */
      88                 :            : static int devicetree_corrupt(void)
      89                 :            : {
      90                 :          0 :         return devicetree_state_flags &
      91                 :            :                 (DTSF_APPLY_FAIL | DTSF_REVERT_FAIL);
      92                 :            : }
      93                 :            : 
      94                 :            : static int build_changeset_next_level(struct overlay_changeset *ovcs,
      95                 :            :                 struct target *target, const struct device_node *overlay_node);
      96                 :            : 
      97                 :            : /*
      98                 :            :  * of_resolve_phandles() finds the largest phandle in the live tree.
      99                 :            :  * of_overlay_apply() may add a larger phandle to the live tree.
     100                 :            :  * Do not allow race between two overlays being applied simultaneously:
     101                 :            :  *    mutex_lock(&of_overlay_phandle_mutex)
     102                 :            :  *    of_resolve_phandles()
     103                 :            :  *    of_overlay_apply()
     104                 :            :  *    mutex_unlock(&of_overlay_phandle_mutex)
     105                 :            :  */
     106                 :            : static DEFINE_MUTEX(of_overlay_phandle_mutex);
     107                 :            : 
     108                 :          0 : void of_overlay_mutex_lock(void)
     109                 :            : {
     110                 :          0 :         mutex_lock(&of_overlay_phandle_mutex);
     111                 :          0 : }
     112                 :            : 
     113                 :          0 : void of_overlay_mutex_unlock(void)
     114                 :            : {
     115                 :          0 :         mutex_unlock(&of_overlay_phandle_mutex);
     116                 :          0 : }
     117                 :            : 
     118                 :            : 
     119                 :            : static LIST_HEAD(ovcs_list);
     120                 :            : static DEFINE_IDR(ovcs_idr);
     121                 :            : 
     122                 :            : static BLOCKING_NOTIFIER_HEAD(overlay_notify_chain);
     123                 :            : 
     124                 :            : /**
     125                 :            :  * of_overlay_notifier_register() - Register notifier for overlay operations
     126                 :            :  * @nb:         Notifier block to register
     127                 :            :  *
     128                 :            :  * Register for notification on overlay operations on device tree nodes. The
     129                 :            :  * reported actions definied by @of_reconfig_change. The notifier callback
     130                 :            :  * furthermore receives a pointer to the affected device tree node.
     131                 :            :  *
     132                 :            :  * Note that a notifier callback is not supposed to store pointers to a device
     133                 :            :  * tree node or its content beyond @OF_OVERLAY_POST_REMOVE corresponding to the
     134                 :            :  * respective node it received.
     135                 :            :  */
     136                 :          0 : int of_overlay_notifier_register(struct notifier_block *nb)
     137                 :            : {
     138                 :          0 :         return blocking_notifier_chain_register(&overlay_notify_chain, nb);
     139                 :            : }
     140                 :            : EXPORT_SYMBOL_GPL(of_overlay_notifier_register);
     141                 :            : 
     142                 :            : /**
     143                 :            :  * of_overlay_notifier_register() - Unregister notifier for overlay operations
     144                 :            :  * @nb:         Notifier block to unregister
     145                 :            :  */
     146                 :          0 : int of_overlay_notifier_unregister(struct notifier_block *nb)
     147                 :            : {
     148                 :          0 :         return blocking_notifier_chain_unregister(&overlay_notify_chain, nb);
     149                 :            : }
     150                 :            : EXPORT_SYMBOL_GPL(of_overlay_notifier_unregister);
     151                 :            : 
     152                 :            : static char *of_overlay_action_name[] = {
     153                 :            :         "pre-apply",
     154                 :            :         "post-apply",
     155                 :            :         "pre-remove",
     156                 :            :         "post-remove",
     157                 :            : };
     158                 :            : 
     159                 :          0 : static int overlay_notify(struct overlay_changeset *ovcs,
     160                 :            :                 enum of_overlay_notify_action action)
     161                 :            : {
     162                 :            :         struct of_overlay_notify_data nd;
     163                 :            :         int i, ret;
     164                 :            : 
     165         [ #  # ]:          0 :         for (i = 0; i < ovcs->count; i++) {
     166                 :          0 :                 struct fragment *fragment = &ovcs->fragments[i];
     167                 :            : 
     168                 :          0 :                 nd.target = fragment->target;
     169                 :          0 :                 nd.overlay = fragment->overlay;
     170                 :            : 
     171                 :          0 :                 ret = blocking_notifier_call_chain(&overlay_notify_chain,
     172                 :            :                                                    action, &nd);
     173         [ #  # ]:          0 :                 if (ret == NOTIFY_OK || ret == NOTIFY_STOP)
     174                 :            :                         return 0;
     175         [ #  # ]:          0 :                 if (ret) {
     176                 :            :                         ret = notifier_to_errno(ret);
     177                 :          0 :                         pr_err("overlay changeset %s notifier error %d, target: %pOF\n",
     178                 :            :                                of_overlay_action_name[action], ret, nd.target);
     179                 :          0 :                         return ret;
     180                 :            :                 }
     181                 :            :         }
     182                 :            : 
     183                 :            :         return 0;
     184                 :            : }
     185                 :            : 
     186                 :            : /*
     187                 :            :  * The values of properties in the "/__symbols__" node are paths in
     188                 :            :  * the ovcs->overlay_tree.  When duplicating the properties, the paths
     189                 :            :  * need to be adjusted to be the correct path for the live device tree.
     190                 :            :  *
     191                 :            :  * The paths refer to a node in the subtree of a fragment node's "__overlay__"
     192                 :            :  * node, for example "/fragment@0/__overlay__/symbol_path_tail",
     193                 :            :  * where symbol_path_tail can be a single node or it may be a multi-node path.
     194                 :            :  *
     195                 :            :  * The duplicated property value will be modified by replacing the
     196                 :            :  * "/fragment_name/__overlay/" portion of the value  with the target
     197                 :            :  * path from the fragment node.
     198                 :            :  */
     199                 :          0 : static struct property *dup_and_fixup_symbol_prop(
     200                 :            :                 struct overlay_changeset *ovcs, const struct property *prop)
     201                 :            : {
     202                 :            :         struct fragment *fragment;
     203                 :            :         struct property *new_prop;
     204                 :            :         struct device_node *fragment_node;
     205                 :            :         struct device_node *overlay_node;
     206                 :            :         const char *path;
     207                 :            :         const char *path_tail;
     208                 :            :         const char *target_path;
     209                 :            :         int k;
     210                 :            :         int overlay_name_len;
     211                 :            :         int path_len;
     212                 :            :         int path_tail_len;
     213                 :            :         int target_path_len;
     214                 :            : 
     215         [ #  # ]:          0 :         if (!prop->value)
     216                 :            :                 return NULL;
     217         [ #  # ]:          0 :         if (strnlen(prop->value, prop->length) >= prop->length)
     218                 :            :                 return NULL;
     219                 :          0 :         path = prop->value;
     220                 :          0 :         path_len = strlen(path);
     221                 :            : 
     222         [ #  # ]:          0 :         if (path_len < 1)
     223                 :            :                 return NULL;
     224                 :          0 :         fragment_node = __of_find_node_by_path(ovcs->overlay_tree, path + 1);
     225                 :          0 :         overlay_node = __of_find_node_by_path(fragment_node, "__overlay__/");
     226                 :          0 :         of_node_put(fragment_node);
     227                 :          0 :         of_node_put(overlay_node);
     228                 :            : 
     229         [ #  # ]:          0 :         for (k = 0; k < ovcs->count; k++) {
     230                 :          0 :                 fragment = &ovcs->fragments[k];
     231         [ #  # ]:          0 :                 if (fragment->overlay == overlay_node)
     232                 :            :                         break;
     233                 :            :         }
     234         [ #  # ]:          0 :         if (k >= ovcs->count)
     235                 :            :                 return NULL;
     236                 :            : 
     237                 :          0 :         overlay_name_len = snprintf(NULL, 0, "%pOF", fragment->overlay);
     238                 :            : 
     239         [ #  # ]:          0 :         if (overlay_name_len > path_len)
     240                 :            :                 return NULL;
     241                 :          0 :         path_tail = path + overlay_name_len;
     242                 :          0 :         path_tail_len = strlen(path_tail);
     243                 :            : 
     244                 :          0 :         target_path = kasprintf(GFP_KERNEL, "%pOF", fragment->target);
     245         [ #  # ]:          0 :         if (!target_path)
     246                 :            :                 return NULL;
     247                 :          0 :         target_path_len = strlen(target_path);
     248         [ #  # ]:          0 :         if (!strcmp(target_path, "/"))
     249                 :            :                 target_path_len = 0;
     250                 :            : 
     251                 :          0 :         new_prop = kzalloc(sizeof(*new_prop), GFP_KERNEL);
     252         [ #  # ]:          0 :         if (!new_prop)
     253                 :            :                 goto err_free_target_path;
     254                 :            : 
     255                 :          0 :         new_prop->name = kstrdup(prop->name, GFP_KERNEL);
     256                 :          0 :         new_prop->length = target_path_len + path_tail_len + 1;
     257                 :          0 :         new_prop->value = kzalloc(new_prop->length, GFP_KERNEL);
     258   [ #  #  #  # ]:          0 :         if (!new_prop->name || !new_prop->value)
     259                 :            :                 goto err_free_new_prop;
     260                 :            : 
     261                 :          0 :         strcpy(new_prop->value, target_path);
     262                 :          0 :         strcpy(new_prop->value + target_path_len, path_tail);
     263                 :            : 
     264                 :            :         of_property_set_flag(new_prop, OF_DYNAMIC);
     265                 :            : 
     266                 :          0 :         kfree(target_path);
     267                 :            : 
     268                 :          0 :         return new_prop;
     269                 :            : 
     270                 :            : err_free_new_prop:
     271                 :          0 :         kfree(new_prop->name);
     272                 :          0 :         kfree(new_prop->value);
     273                 :          0 :         kfree(new_prop);
     274                 :            : err_free_target_path:
     275                 :          0 :         kfree(target_path);
     276                 :            : 
     277                 :          0 :         return NULL;
     278                 :            : }
     279                 :            : 
     280                 :            : /**
     281                 :            :  * add_changeset_property() - add @overlay_prop to overlay changeset
     282                 :            :  * @ovcs:               overlay changeset
     283                 :            :  * @target:             where @overlay_prop will be placed
     284                 :            :  * @overlay_prop:       property to add or update, from overlay tree
     285                 :            :  * @is_symbols_prop:    1 if @overlay_prop is from node "/__symbols__"
     286                 :            :  *
     287                 :            :  * If @overlay_prop does not already exist in live devicetree, add changeset
     288                 :            :  * entry to add @overlay_prop in @target, else add changeset entry to update
     289                 :            :  * value of @overlay_prop.
     290                 :            :  *
     291                 :            :  * @target may be either in the live devicetree or in a new subtree that
     292                 :            :  * is contained in the changeset.
     293                 :            :  *
     294                 :            :  * Some special properties are not added or updated (no error returned):
     295                 :            :  * "name", "phandle", "linux,phandle".
     296                 :            :  *
     297                 :            :  * Properties "#address-cells" and "#size-cells" are not updated if they
     298                 :            :  * are already in the live tree, but if present in the live tree, the values
     299                 :            :  * in the overlay must match the values in the live tree.
     300                 :            :  *
     301                 :            :  * Update of property in symbols node is not allowed.
     302                 :            :  *
     303                 :            :  * Returns 0 on success, -ENOMEM if memory allocation failure, or -EINVAL if
     304                 :            :  * invalid @overlay.
     305                 :            :  */
     306                 :          0 : static int add_changeset_property(struct overlay_changeset *ovcs,
     307                 :            :                 struct target *target, struct property *overlay_prop,
     308                 :            :                 bool is_symbols_prop)
     309                 :            : {
     310                 :            :         struct property *new_prop = NULL, *prop;
     311                 :            :         int ret = 0;
     312                 :            : 
     313         [ #  # ]:          0 :         if (target->in_livetree)
     314   [ #  #  #  # ]:          0 :                 if (!of_prop_cmp(overlay_prop->name, "name") ||
     315         [ #  # ]:          0 :                     !of_prop_cmp(overlay_prop->name, "phandle") ||
     316                 :          0 :                     !of_prop_cmp(overlay_prop->name, "linux,phandle"))
     317                 :            :                         return 0;
     318                 :            : 
     319         [ #  # ]:          0 :         if (target->in_livetree)
     320                 :          0 :                 prop = of_find_property(target->np, overlay_prop->name, NULL);
     321                 :            :         else
     322                 :            :                 prop = NULL;
     323                 :            : 
     324         [ #  # ]:          0 :         if (prop) {
     325         [ #  # ]:          0 :                 if (!of_prop_cmp(prop->name, "#address-cells")) {
     326         [ #  # ]:          0 :                         if (!of_prop_val_eq(prop, overlay_prop)) {
     327                 :          0 :                                 pr_err("ERROR: changing value of #address-cells is not allowed in %pOF\n",
     328                 :            :                                        target->np);
     329                 :            :                                 ret = -EINVAL;
     330                 :            :                         }
     331                 :          0 :                         return ret;
     332                 :            : 
     333         [ #  # ]:          0 :                 } else if (!of_prop_cmp(prop->name, "#size-cells")) {
     334         [ #  # ]:          0 :                         if (!of_prop_val_eq(prop, overlay_prop)) {
     335                 :          0 :                                 pr_err("ERROR: changing value of #size-cells is not allowed in %pOF\n",
     336                 :            :                                        target->np);
     337                 :            :                                 ret = -EINVAL;
     338                 :            :                         }
     339                 :          0 :                         return ret;
     340                 :            :                 }
     341                 :            :         }
     342                 :            : 
     343         [ #  # ]:          0 :         if (is_symbols_prop) {
     344         [ #  # ]:          0 :                 if (prop)
     345                 :            :                         return -EINVAL;
     346                 :          0 :                 new_prop = dup_and_fixup_symbol_prop(ovcs, overlay_prop);
     347                 :            :         } else {
     348                 :          0 :                 new_prop = __of_prop_dup(overlay_prop, GFP_KERNEL);
     349                 :            :         }
     350                 :            : 
     351         [ #  # ]:          0 :         if (!new_prop)
     352                 :            :                 return -ENOMEM;
     353                 :            : 
     354         [ #  # ]:          0 :         if (!prop) {
     355         [ #  # ]:          0 :                 if (!target->in_livetree) {
     356                 :          0 :                         new_prop->next = target->np->deadprops;
     357                 :          0 :                         target->np->deadprops = new_prop;
     358                 :            :                 }
     359                 :          0 :                 ret = of_changeset_add_property(&ovcs->cset, target->np,
     360                 :            :                                                 new_prop);
     361                 :            :         } else {
     362                 :          0 :                 ret = of_changeset_update_property(&ovcs->cset, target->np,
     363                 :            :                                                    new_prop);
     364                 :            :         }
     365                 :            : 
     366         [ #  # ]:          0 :         if (!of_node_check_flag(target->np, OF_OVERLAY))
     367                 :          0 :                 pr_err("WARNING: memory leak will occur if overlay removed, property: %pOF/%s\n",
     368                 :            :                        target->np, new_prop->name);
     369                 :            : 
     370         [ #  # ]:          0 :         if (ret) {
     371                 :          0 :                 kfree(new_prop->name);
     372                 :          0 :                 kfree(new_prop->value);
     373                 :          0 :                 kfree(new_prop);
     374                 :            :         }
     375                 :          0 :         return ret;
     376                 :            : }
     377                 :            : 
     378                 :            : /**
     379                 :            :  * add_changeset_node() - add @node (and children) to overlay changeset
     380                 :            :  * @ovcs:       overlay changeset
     381                 :            :  * @target:     where @node will be placed in live tree or changeset
     382                 :            :  * @node:       node from within overlay device tree fragment
     383                 :            :  *
     384                 :            :  * If @node does not already exist in @target, add changeset entry
     385                 :            :  * to add @node in @target.
     386                 :            :  *
     387                 :            :  * If @node already exists in @target, and the existing node has
     388                 :            :  * a phandle, the overlay node is not allowed to have a phandle.
     389                 :            :  *
     390                 :            :  * If @node has child nodes, add the children recursively via
     391                 :            :  * build_changeset_next_level().
     392                 :            :  *
     393                 :            :  * NOTE_1: A live devicetree created from a flattened device tree (FDT) will
     394                 :            :  *       not contain the full path in node->full_name.  Thus an overlay
     395                 :            :  *       created from an FDT also will not contain the full path in
     396                 :            :  *       node->full_name.  However, a live devicetree created from Open
     397                 :            :  *       Firmware may have the full path in node->full_name.
     398                 :            :  *
     399                 :            :  *       add_changeset_node() follows the FDT convention and does not include
     400                 :            :  *       the full path in node->full_name.  Even though it expects the overlay
     401                 :            :  *       to not contain the full path, it uses kbasename() to remove the
     402                 :            :  *       full path should it exist.  It also uses kbasename() in comparisons
     403                 :            :  *       to nodes in the live devicetree so that it can apply an overlay to
     404                 :            :  *       a live devicetree created from Open Firmware.
     405                 :            :  *
     406                 :            :  * NOTE_2: Multiple mods of created nodes not supported.
     407                 :            :  *
     408                 :            :  * Returns 0 on success, -ENOMEM if memory allocation failure, or -EINVAL if
     409                 :            :  * invalid @overlay.
     410                 :            :  */
     411                 :          0 : static int add_changeset_node(struct overlay_changeset *ovcs,
     412                 :            :                 struct target *target, struct device_node *node)
     413                 :            : {
     414                 :            :         const char *node_kbasename;
     415                 :            :         const __be32 *phandle;
     416                 :            :         struct device_node *tchild;
     417                 :            :         struct target target_child;
     418                 :            :         int ret = 0, size;
     419                 :            : 
     420                 :          0 :         node_kbasename = kbasename(node->full_name);
     421                 :            : 
     422         [ #  # ]:          0 :         for_each_child_of_node(target->np, tchild)
     423         [ #  # ]:          0 :                 if (!of_node_cmp(node_kbasename, kbasename(tchild->full_name)))
     424                 :            :                         break;
     425                 :            : 
     426         [ #  # ]:          0 :         if (!tchild) {
     427                 :          0 :                 tchild = __of_node_dup(NULL, node_kbasename);
     428         [ #  # ]:          0 :                 if (!tchild)
     429                 :            :                         return -ENOMEM;
     430                 :            : 
     431                 :          0 :                 tchild->parent = target->np;
     432                 :          0 :                 tchild->name = __of_get_property(node, "name", NULL);
     433                 :            : 
     434         [ #  # ]:          0 :                 if (!tchild->name)
     435                 :          0 :                         tchild->name = "<NULL>";
     436                 :            : 
     437                 :            :                 /* ignore obsolete "linux,phandle" */
     438                 :          0 :                 phandle = __of_get_property(node, "phandle", &size);
     439   [ #  #  #  # ]:          0 :                 if (phandle && (size == 4))
     440                 :          0 :                         tchild->phandle = be32_to_cpup(phandle);
     441                 :            : 
     442                 :            :                 of_node_set_flag(tchild, OF_OVERLAY);
     443                 :            : 
     444                 :          0 :                 ret = of_changeset_attach_node(&ovcs->cset, tchild);
     445         [ #  # ]:          0 :                 if (ret)
     446                 :            :                         return ret;
     447                 :            : 
     448                 :          0 :                 target_child.np = tchild;
     449                 :          0 :                 target_child.in_livetree = false;
     450                 :            : 
     451                 :          0 :                 ret = build_changeset_next_level(ovcs, &target_child, node);
     452                 :          0 :                 of_node_put(tchild);
     453                 :          0 :                 return ret;
     454                 :            :         }
     455                 :            : 
     456   [ #  #  #  # ]:          0 :         if (node->phandle && tchild->phandle) {
     457                 :            :                 ret = -EINVAL;
     458                 :            :         } else {
     459                 :          0 :                 target_child.np = tchild;
     460                 :          0 :                 target_child.in_livetree = target->in_livetree;
     461                 :          0 :                 ret = build_changeset_next_level(ovcs, &target_child, node);
     462                 :            :         }
     463                 :          0 :         of_node_put(tchild);
     464                 :            : 
     465                 :          0 :         return ret;
     466                 :            : }
     467                 :            : 
     468                 :            : /**
     469                 :            :  * build_changeset_next_level() - add level of overlay changeset
     470                 :            :  * @ovcs:               overlay changeset
     471                 :            :  * @target:             where to place @overlay_node in live tree
     472                 :            :  * @overlay_node:       node from within an overlay device tree fragment
     473                 :            :  *
     474                 :            :  * Add the properties (if any) and nodes (if any) from @overlay_node to the
     475                 :            :  * @ovcs->cset changeset.  If an added node has child nodes, they will
     476                 :            :  * be added recursively.
     477                 :            :  *
     478                 :            :  * Do not allow symbols node to have any children.
     479                 :            :  *
     480                 :            :  * Returns 0 on success, -ENOMEM if memory allocation failure, or -EINVAL if
     481                 :            :  * invalid @overlay_node.
     482                 :            :  */
     483                 :          0 : static int build_changeset_next_level(struct overlay_changeset *ovcs,
     484                 :            :                 struct target *target, const struct device_node *overlay_node)
     485                 :            : {
     486                 :            :         struct device_node *child;
     487                 :            :         struct property *prop;
     488                 :            :         int ret;
     489                 :            : 
     490         [ #  # ]:          0 :         for_each_property_of_node(overlay_node, prop) {
     491                 :          0 :                 ret = add_changeset_property(ovcs, target, prop, 0);
     492         [ #  # ]:          0 :                 if (ret) {
     493                 :            :                         pr_debug("Failed to apply prop @%pOF/%s, err=%d\n",
     494                 :            :                                  target->np, prop->name, ret);
     495                 :          0 :                         return ret;
     496                 :            :                 }
     497                 :            :         }
     498                 :            : 
     499         [ #  # ]:          0 :         for_each_child_of_node(overlay_node, child) {
     500                 :          0 :                 ret = add_changeset_node(ovcs, target, child);
     501         [ #  # ]:          0 :                 if (ret) {
     502                 :            :                         pr_debug("Failed to apply node @%pOF/%pOFn, err=%d\n",
     503                 :            :                                  target->np, child, ret);
     504                 :          0 :                         of_node_put(child);
     505                 :          0 :                         return ret;
     506                 :            :                 }
     507                 :            :         }
     508                 :            : 
     509                 :            :         return 0;
     510                 :            : }
     511                 :            : 
     512                 :            : /*
     513                 :            :  * Add the properties from __overlay__ node to the @ovcs->cset changeset.
     514                 :            :  */
     515                 :          0 : static int build_changeset_symbols_node(struct overlay_changeset *ovcs,
     516                 :            :                 struct target *target,
     517                 :            :                 const struct device_node *overlay_symbols_node)
     518                 :            : {
     519                 :            :         struct property *prop;
     520                 :            :         int ret;
     521                 :            : 
     522         [ #  # ]:          0 :         for_each_property_of_node(overlay_symbols_node, prop) {
     523                 :          0 :                 ret = add_changeset_property(ovcs, target, prop, 1);
     524         [ #  # ]:          0 :                 if (ret) {
     525                 :            :                         pr_debug("Failed to apply symbols prop @%pOF/%s, err=%d\n",
     526                 :            :                                  target->np, prop->name, ret);
     527                 :          0 :                         return ret;
     528                 :            :                 }
     529                 :            :         }
     530                 :            : 
     531                 :            :         return 0;
     532                 :            : }
     533                 :            : 
     534                 :          0 : static int find_dup_cset_node_entry(struct overlay_changeset *ovcs,
     535                 :            :                 struct of_changeset_entry *ce_1)
     536                 :            : {
     537                 :            :         struct of_changeset_entry *ce_2;
     538                 :            :         char *fn_1, *fn_2;
     539                 :            :         int node_path_match;
     540                 :            : 
     541         [ #  # ]:          0 :         if (ce_1->action != OF_RECONFIG_ATTACH_NODE &&
     542                 :            :             ce_1->action != OF_RECONFIG_DETACH_NODE)
     543                 :            :                 return 0;
     544                 :            : 
     545                 :            :         ce_2 = ce_1;
     546         [ #  # ]:          0 :         list_for_each_entry_continue(ce_2, &ovcs->cset.entries, node) {
     547         [ #  # ]:          0 :                 if ((ce_2->action != OF_RECONFIG_ATTACH_NODE &&
     548         [ #  # ]:          0 :                      ce_2->action != OF_RECONFIG_DETACH_NODE) ||
     549                 :          0 :                     of_node_cmp(ce_1->np->full_name, ce_2->np->full_name))
     550                 :          0 :                         continue;
     551                 :            : 
     552                 :          0 :                 fn_1 = kasprintf(GFP_KERNEL, "%pOF", ce_1->np);
     553                 :          0 :                 fn_2 = kasprintf(GFP_KERNEL, "%pOF", ce_2->np);
     554                 :          0 :                 node_path_match = !strcmp(fn_1, fn_2);
     555                 :          0 :                 kfree(fn_1);
     556                 :          0 :                 kfree(fn_2);
     557         [ #  # ]:          0 :                 if (node_path_match) {
     558                 :          0 :                         pr_err("ERROR: multiple fragments add and/or delete node %pOF\n",
     559                 :            :                                ce_1->np);
     560                 :          0 :                         return -EINVAL;
     561                 :            :                 }
     562                 :            :         }
     563                 :            : 
     564                 :            :         return 0;
     565                 :            : }
     566                 :            : 
     567                 :          0 : static int find_dup_cset_prop(struct overlay_changeset *ovcs,
     568                 :            :                 struct of_changeset_entry *ce_1)
     569                 :            : {
     570                 :            :         struct of_changeset_entry *ce_2;
     571                 :            :         char *fn_1, *fn_2;
     572                 :            :         int node_path_match;
     573                 :            : 
     574         [ #  # ]:          0 :         if (ce_1->action != OF_RECONFIG_ADD_PROPERTY &&
     575                 :          0 :             ce_1->action != OF_RECONFIG_REMOVE_PROPERTY &&
     576                 :            :             ce_1->action != OF_RECONFIG_UPDATE_PROPERTY)
     577                 :            :                 return 0;
     578                 :            : 
     579                 :            :         ce_2 = ce_1;
     580         [ #  # ]:          0 :         list_for_each_entry_continue(ce_2, &ovcs->cset.entries, node) {
     581         [ #  # ]:          0 :                 if ((ce_2->action != OF_RECONFIG_ADD_PROPERTY &&
     582                 :          0 :                      ce_2->action != OF_RECONFIG_REMOVE_PROPERTY &&
     583         [ #  # ]:          0 :                      ce_2->action != OF_RECONFIG_UPDATE_PROPERTY) ||
     584                 :          0 :                     of_node_cmp(ce_1->np->full_name, ce_2->np->full_name))
     585                 :          0 :                         continue;
     586                 :            : 
     587                 :          0 :                 fn_1 = kasprintf(GFP_KERNEL, "%pOF", ce_1->np);
     588                 :          0 :                 fn_2 = kasprintf(GFP_KERNEL, "%pOF", ce_2->np);
     589                 :          0 :                 node_path_match = !strcmp(fn_1, fn_2);
     590                 :          0 :                 kfree(fn_1);
     591                 :          0 :                 kfree(fn_2);
     592   [ #  #  #  # ]:          0 :                 if (node_path_match &&
     593                 :          0 :                     !of_prop_cmp(ce_1->prop->name, ce_2->prop->name)) {
     594                 :          0 :                         pr_err("ERROR: multiple fragments add, update, and/or delete property %pOF/%s\n",
     595                 :            :                                ce_1->np, ce_1->prop->name);
     596                 :          0 :                         return -EINVAL;
     597                 :            :                 }
     598                 :            :         }
     599                 :            : 
     600                 :            :         return 0;
     601                 :            : }
     602                 :            : 
     603                 :            : /**
     604                 :            :  * changeset_dup_entry_check() - check for duplicate entries
     605                 :            :  * @ovcs:       Overlay changeset
     606                 :            :  *
     607                 :            :  * Check changeset @ovcs->cset for multiple {add or delete} node entries for
     608                 :            :  * the same node or duplicate {add, delete, or update} properties entries
     609                 :            :  * for the same property.
     610                 :            :  *
     611                 :            :  * Returns 0 on success, or -EINVAL if duplicate changeset entry found.
     612                 :            :  */
     613                 :          0 : static int changeset_dup_entry_check(struct overlay_changeset *ovcs)
     614                 :            : {
     615                 :            :         struct of_changeset_entry *ce_1;
     616                 :            :         int dup_entry = 0;
     617                 :            : 
     618         [ #  # ]:          0 :         list_for_each_entry(ce_1, &ovcs->cset.entries, node) {
     619                 :          0 :                 dup_entry |= find_dup_cset_node_entry(ovcs, ce_1);
     620                 :          0 :                 dup_entry |= find_dup_cset_prop(ovcs, ce_1);
     621                 :            :         }
     622                 :            : 
     623         [ #  # ]:          0 :         return dup_entry ? -EINVAL : 0;
     624                 :            : }
     625                 :            : 
     626                 :            : /**
     627                 :            :  * build_changeset() - populate overlay changeset in @ovcs from @ovcs->fragments
     628                 :            :  * @ovcs:       Overlay changeset
     629                 :            :  *
     630                 :            :  * Create changeset @ovcs->cset to contain the nodes and properties of the
     631                 :            :  * overlay device tree fragments in @ovcs->fragments[].  If an error occurs,
     632                 :            :  * any portions of the changeset that were successfully created will remain
     633                 :            :  * in @ovcs->cset.
     634                 :            :  *
     635                 :            :  * Returns 0 on success, -ENOMEM if memory allocation failure, or -EINVAL if
     636                 :            :  * invalid overlay in @ovcs->fragments[].
     637                 :            :  */
     638                 :          0 : static int build_changeset(struct overlay_changeset *ovcs)
     639                 :            : {
     640                 :            :         struct fragment *fragment;
     641                 :            :         struct target target;
     642                 :            :         int fragments_count, i, ret;
     643                 :            : 
     644                 :            :         /*
     645                 :            :          * if there is a symbols fragment in ovcs->fragments[i] it is
     646                 :            :          * the final element in the array
     647                 :            :          */
     648         [ #  # ]:          0 :         if (ovcs->symbols_fragment)
     649                 :          0 :                 fragments_count = ovcs->count - 1;
     650                 :            :         else
     651                 :          0 :                 fragments_count = ovcs->count;
     652                 :            : 
     653         [ #  # ]:          0 :         for (i = 0; i < fragments_count; i++) {
     654                 :          0 :                 fragment = &ovcs->fragments[i];
     655                 :            : 
     656                 :          0 :                 target.np = fragment->target;
     657                 :          0 :                 target.in_livetree = true;
     658                 :          0 :                 ret = build_changeset_next_level(ovcs, &target,
     659                 :          0 :                                                  fragment->overlay);
     660         [ #  # ]:          0 :                 if (ret) {
     661                 :            :                         pr_debug("fragment apply failed '%pOF'\n",
     662                 :            :                                  fragment->target);
     663                 :          0 :                         return ret;
     664                 :            :                 }
     665                 :            :         }
     666                 :            : 
     667         [ #  # ]:          0 :         if (ovcs->symbols_fragment) {
     668                 :          0 :                 fragment = &ovcs->fragments[ovcs->count - 1];
     669                 :            : 
     670                 :          0 :                 target.np = fragment->target;
     671                 :          0 :                 target.in_livetree = true;
     672                 :          0 :                 ret = build_changeset_symbols_node(ovcs, &target,
     673                 :          0 :                                                    fragment->overlay);
     674         [ #  # ]:          0 :                 if (ret) {
     675                 :            :                         pr_debug("symbols fragment apply failed '%pOF'\n",
     676                 :            :                                  fragment->target);
     677                 :            :                         return ret;
     678                 :            :                 }
     679                 :            :         }
     680                 :            : 
     681                 :          0 :         return changeset_dup_entry_check(ovcs);
     682                 :            : }
     683                 :            : 
     684                 :            : /*
     685                 :            :  * Find the target node using a number of different strategies
     686                 :            :  * in order of preference:
     687                 :            :  *
     688                 :            :  * 1) "target" property containing the phandle of the target
     689                 :            :  * 2) "target-path" property containing the path of the target
     690                 :            :  */
     691                 :          0 : static struct device_node *find_target(struct device_node *info_node)
     692                 :            : {
     693                 :            :         struct device_node *node;
     694                 :            :         const char *path;
     695                 :            :         u32 val;
     696                 :            :         int ret;
     697                 :            : 
     698                 :            :         ret = of_property_read_u32(info_node, "target", &val);
     699         [ #  # ]:          0 :         if (!ret) {
     700                 :          0 :                 node = of_find_node_by_phandle(val);
     701         [ #  # ]:          0 :                 if (!node)
     702                 :          0 :                         pr_err("find target, node: %pOF, phandle 0x%x not found\n",
     703                 :            :                                info_node, val);
     704                 :          0 :                 return node;
     705                 :            :         }
     706                 :            : 
     707                 :          0 :         ret = of_property_read_string(info_node, "target-path", &path);
     708         [ #  # ]:          0 :         if (!ret) {
     709                 :          0 :                 node =  of_find_node_by_path(path);
     710         [ #  # ]:          0 :                 if (!node)
     711                 :          0 :                         pr_err("find target, node: %pOF, path '%s' not found\n",
     712                 :            :                                info_node, path);
     713                 :          0 :                 return node;
     714                 :            :         }
     715                 :            : 
     716                 :          0 :         pr_err("find target, node: %pOF, no target property\n", info_node);
     717                 :            : 
     718                 :          0 :         return NULL;
     719                 :            : }
     720                 :            : 
     721                 :            : /**
     722                 :            :  * init_overlay_changeset() - initialize overlay changeset from overlay tree
     723                 :            :  * @ovcs:       Overlay changeset to build
     724                 :            :  * @fdt:        the FDT that was unflattened to create @tree
     725                 :            :  * @tree:       Contains all the overlay fragments and overlay fixup nodes
     726                 :            :  *
     727                 :            :  * Initialize @ovcs.  Populate @ovcs->fragments with node information from
     728                 :            :  * the top level of @tree.  The relevant top level nodes are the fragment
     729                 :            :  * nodes and the __symbols__ node.  Any other top level node will be ignored.
     730                 :            :  *
     731                 :            :  * Returns 0 on success, -ENOMEM if memory allocation failure, -EINVAL if error
     732                 :            :  * detected in @tree, or -ENOSPC if idr_alloc() error.
     733                 :            :  */
     734                 :          0 : static int init_overlay_changeset(struct overlay_changeset *ovcs,
     735                 :            :                 const void *fdt, struct device_node *tree)
     736                 :            : {
     737                 :            :         struct device_node *node, *overlay_node;
     738                 :            :         struct fragment *fragment;
     739                 :            :         struct fragment *fragments;
     740                 :            :         int cnt, id, ret;
     741                 :            : 
     742                 :            :         /*
     743                 :            :          * Warn for some issues.  Can not return -EINVAL for these until
     744                 :            :          * of_unittest_apply_overlay() is fixed to pass these checks.
     745                 :            :          */
     746                 :            :         if (!of_node_check_flag(tree, OF_DYNAMIC))
     747                 :            :                 pr_debug("%s() tree is not dynamic\n", __func__);
     748                 :            : 
     749                 :            :         if (!of_node_check_flag(tree, OF_DETACHED))
     750                 :            :                 pr_debug("%s() tree is not detached\n", __func__);
     751                 :            : 
     752                 :            :         if (!of_node_is_root(tree))
     753                 :            :                 pr_debug("%s() tree is not root\n", __func__);
     754                 :            : 
     755                 :          0 :         ovcs->overlay_tree = tree;
     756                 :          0 :         ovcs->fdt = fdt;
     757                 :            : 
     758                 :          0 :         INIT_LIST_HEAD(&ovcs->ovcs_list);
     759                 :            : 
     760                 :          0 :         of_changeset_init(&ovcs->cset);
     761                 :            : 
     762                 :          0 :         id = idr_alloc(&ovcs_idr, ovcs, 1, 0, GFP_KERNEL);
     763         [ #  # ]:          0 :         if (id <= 0)
     764                 :            :                 return id;
     765                 :            : 
     766                 :            :         cnt = 0;
     767                 :            : 
     768                 :            :         /* fragment nodes */
     769         [ #  # ]:          0 :         for_each_child_of_node(tree, node) {
     770                 :          0 :                 overlay_node = of_get_child_by_name(node, "__overlay__");
     771         [ #  # ]:          0 :                 if (overlay_node) {
     772                 :          0 :                         cnt++;
     773                 :          0 :                         of_node_put(overlay_node);
     774                 :            :                 }
     775                 :            :         }
     776                 :            : 
     777                 :          0 :         node = of_get_child_by_name(tree, "__symbols__");
     778         [ #  # ]:          0 :         if (node) {
     779                 :          0 :                 cnt++;
     780                 :          0 :                 of_node_put(node);
     781                 :            :         }
     782                 :            : 
     783                 :          0 :         fragments = kcalloc(cnt, sizeof(*fragments), GFP_KERNEL);
     784         [ #  # ]:          0 :         if (!fragments) {
     785                 :            :                 ret = -ENOMEM;
     786                 :            :                 goto err_free_idr;
     787                 :            :         }
     788                 :            : 
     789                 :            :         cnt = 0;
     790         [ #  # ]:          0 :         for_each_child_of_node(tree, node) {
     791                 :          0 :                 overlay_node = of_get_child_by_name(node, "__overlay__");
     792         [ #  # ]:          0 :                 if (!overlay_node)
     793                 :          0 :                         continue;
     794                 :            : 
     795                 :          0 :                 fragment = &fragments[cnt];
     796                 :          0 :                 fragment->overlay = overlay_node;
     797                 :          0 :                 fragment->target = find_target(node);
     798         [ #  # ]:          0 :                 if (!fragment->target) {
     799                 :          0 :                         of_node_put(fragment->overlay);
     800                 :            :                         ret = -EINVAL;
     801                 :          0 :                         goto err_free_fragments;
     802                 :            :                 }
     803                 :            : 
     804                 :          0 :                 cnt++;
     805                 :            :         }
     806                 :            : 
     807                 :            :         /*
     808                 :            :          * if there is a symbols fragment in ovcs->fragments[i] it is
     809                 :            :          * the final element in the array
     810                 :            :          */
     811                 :          0 :         node = of_get_child_by_name(tree, "__symbols__");
     812         [ #  # ]:          0 :         if (node) {
     813                 :          0 :                 ovcs->symbols_fragment = 1;
     814                 :          0 :                 fragment = &fragments[cnt];
     815                 :          0 :                 fragment->overlay = node;
     816                 :          0 :                 fragment->target = of_find_node_by_path("/__symbols__");
     817                 :            : 
     818         [ #  # ]:          0 :                 if (!fragment->target) {
     819                 :          0 :                         pr_err("symbols in overlay, but not in live tree\n");
     820                 :            :                         ret = -EINVAL;
     821                 :          0 :                         goto err_free_fragments;
     822                 :            :                 }
     823                 :            : 
     824                 :          0 :                 cnt++;
     825                 :            :         }
     826                 :            : 
     827         [ #  # ]:          0 :         if (!cnt) {
     828                 :          0 :                 pr_err("no fragments or symbols in overlay\n");
     829                 :            :                 ret = -EINVAL;
     830                 :          0 :                 goto err_free_fragments;
     831                 :            :         }
     832                 :            : 
     833                 :          0 :         ovcs->id = id;
     834                 :          0 :         ovcs->count = cnt;
     835                 :          0 :         ovcs->fragments = fragments;
     836                 :            : 
     837                 :          0 :         return 0;
     838                 :            : 
     839                 :            : err_free_fragments:
     840                 :          0 :         kfree(fragments);
     841                 :            : err_free_idr:
     842                 :          0 :         idr_remove(&ovcs_idr, id);
     843                 :            : 
     844                 :          0 :         pr_err("%s() failed, ret = %d\n", __func__, ret);
     845                 :            : 
     846                 :          0 :         return ret;
     847                 :            : }
     848                 :            : 
     849                 :          0 : static void free_overlay_changeset(struct overlay_changeset *ovcs)
     850                 :            : {
     851                 :            :         int i;
     852                 :            : 
     853         [ #  # ]:          0 :         if (ovcs->cset.entries.next)
     854                 :          0 :                 of_changeset_destroy(&ovcs->cset);
     855                 :            : 
     856         [ #  # ]:          0 :         if (ovcs->id)
     857                 :          0 :                 idr_remove(&ovcs_idr, ovcs->id);
     858                 :            : 
     859         [ #  # ]:          0 :         for (i = 0; i < ovcs->count; i++) {
     860                 :          0 :                 of_node_put(ovcs->fragments[i].target);
     861                 :          0 :                 of_node_put(ovcs->fragments[i].overlay);
     862                 :            :         }
     863                 :          0 :         kfree(ovcs->fragments);
     864                 :            :         /*
     865                 :            :          * There should be no live pointers into ovcs->overlay_tree and
     866                 :            :          * ovcs->fdt due to the policy that overlay notifiers are not allowed
     867                 :            :          * to retain pointers into the overlay devicetree.
     868                 :            :          */
     869                 :          0 :         kfree(ovcs->overlay_tree);
     870                 :          0 :         kfree(ovcs->fdt);
     871                 :          0 :         kfree(ovcs);
     872                 :          0 : }
     873                 :            : 
     874                 :            : /*
     875                 :            :  * internal documentation
     876                 :            :  *
     877                 :            :  * of_overlay_apply() - Create and apply an overlay changeset
     878                 :            :  * @fdt:        the FDT that was unflattened to create @tree
     879                 :            :  * @tree:       Expanded overlay device tree
     880                 :            :  * @ovcs_id:    Pointer to overlay changeset id
     881                 :            :  *
     882                 :            :  * Creates and applies an overlay changeset.
     883                 :            :  *
     884                 :            :  * If an error occurs in a pre-apply notifier, then no changes are made
     885                 :            :  * to the device tree.
     886                 :            :  *
     887                 :            : 
     888                 :            :  * A non-zero return value will not have created the changeset if error is from:
     889                 :            :  *   - parameter checks
     890                 :            :  *   - building the changeset
     891                 :            :  *   - overlay changeset pre-apply notifier
     892                 :            :  *
     893                 :            :  * If an error is returned by an overlay changeset pre-apply notifier
     894                 :            :  * then no further overlay changeset pre-apply notifier will be called.
     895                 :            :  *
     896                 :            :  * A non-zero return value will have created the changeset if error is from:
     897                 :            :  *   - overlay changeset entry notifier
     898                 :            :  *   - overlay changeset post-apply notifier
     899                 :            :  *
     900                 :            :  * If an error is returned by an overlay changeset post-apply notifier
     901                 :            :  * then no further overlay changeset post-apply notifier will be called.
     902                 :            :  *
     903                 :            :  * If more than one notifier returns an error, then the last notifier
     904                 :            :  * error to occur is returned.
     905                 :            :  *
     906                 :            :  * If an error occurred while applying the overlay changeset, then an
     907                 :            :  * attempt is made to revert any changes that were made to the
     908                 :            :  * device tree.  If there were any errors during the revert attempt
     909                 :            :  * then the state of the device tree can not be determined, and any
     910                 :            :  * following attempt to apply or remove an overlay changeset will be
     911                 :            :  * refused.
     912                 :            :  *
     913                 :            :  * Returns 0 on success, or a negative error number.  Overlay changeset
     914                 :            :  * id is returned to *ovcs_id.
     915                 :            :  */
     916                 :            : 
     917                 :          0 : static int of_overlay_apply(const void *fdt, struct device_node *tree,
     918                 :            :                 int *ovcs_id)
     919                 :            : {
     920                 :            :         struct overlay_changeset *ovcs;
     921                 :            :         int ret = 0, ret_revert, ret_tmp;
     922                 :            : 
     923                 :            :         /*
     924                 :            :          * As of this point, fdt and tree belong to the overlay changeset.
     925                 :            :          * overlay changeset code is responsible for freeing them.
     926                 :            :          */
     927                 :            : 
     928         [ #  # ]:          0 :         if (devicetree_corrupt()) {
     929                 :          0 :                 pr_err("devicetree state suspect, refuse to apply overlay\n");
     930                 :          0 :                 kfree(fdt);
     931                 :          0 :                 kfree(tree);
     932                 :            :                 ret = -EBUSY;
     933                 :          0 :                 goto out;
     934                 :            :         }
     935                 :            : 
     936                 :          0 :         ovcs = kzalloc(sizeof(*ovcs), GFP_KERNEL);
     937         [ #  # ]:          0 :         if (!ovcs) {
     938                 :          0 :                 kfree(fdt);
     939                 :          0 :                 kfree(tree);
     940                 :            :                 ret = -ENOMEM;
     941                 :          0 :                 goto out;
     942                 :            :         }
     943                 :            : 
     944                 :            :         of_overlay_mutex_lock();
     945                 :          0 :         mutex_lock(&of_mutex);
     946                 :            : 
     947                 :          0 :         ret = of_resolve_phandles(tree);
     948         [ #  # ]:          0 :         if (ret)
     949                 :            :                 goto err_free_tree;
     950                 :            : 
     951                 :          0 :         ret = init_overlay_changeset(ovcs, fdt, tree);
     952         [ #  # ]:          0 :         if (ret)
     953                 :            :                 goto err_free_tree;
     954                 :            : 
     955                 :            :         /*
     956                 :            :          * after overlay_notify(), ovcs->overlay_tree related pointers may have
     957                 :            :          * leaked to drivers, so can not kfree() tree, aka ovcs->overlay_tree;
     958                 :            :          * and can not free fdt, aka ovcs->fdt
     959                 :            :          */
     960                 :          0 :         ret = overlay_notify(ovcs, OF_OVERLAY_PRE_APPLY);
     961         [ #  # ]:          0 :         if (ret) {
     962                 :          0 :                 pr_err("overlay changeset pre-apply notify error %d\n", ret);
     963                 :          0 :                 goto err_free_overlay_changeset;
     964                 :            :         }
     965                 :            : 
     966                 :          0 :         ret = build_changeset(ovcs);
     967         [ #  # ]:          0 :         if (ret)
     968                 :            :                 goto err_free_overlay_changeset;
     969                 :            : 
     970                 :          0 :         ret_revert = 0;
     971                 :          0 :         ret = __of_changeset_apply_entries(&ovcs->cset, &ret_revert);
     972         [ #  # ]:          0 :         if (ret) {
     973         [ #  # ]:          0 :                 if (ret_revert) {
     974                 :            :                         pr_debug("overlay changeset revert error %d\n",
     975                 :            :                                  ret_revert);
     976                 :          0 :                         devicetree_state_flags |= DTSF_APPLY_FAIL;
     977                 :            :                 }
     978                 :            :                 goto err_free_overlay_changeset;
     979                 :            :         }
     980                 :            : 
     981                 :          0 :         of_populate_phandle_cache();
     982                 :            : 
     983                 :          0 :         ret = __of_changeset_apply_notify(&ovcs->cset);
     984         [ #  # ]:          0 :         if (ret)
     985                 :          0 :                 pr_err("overlay apply changeset entry notify error %d\n", ret);
     986                 :            :         /* notify failure is not fatal, continue */
     987                 :            : 
     988                 :          0 :         list_add_tail(&ovcs->ovcs_list, &ovcs_list);
     989                 :          0 :         *ovcs_id = ovcs->id;
     990                 :            : 
     991                 :          0 :         ret_tmp = overlay_notify(ovcs, OF_OVERLAY_POST_APPLY);
     992         [ #  # ]:          0 :         if (ret_tmp) {
     993                 :          0 :                 pr_err("overlay changeset post-apply notify error %d\n",
     994                 :            :                        ret_tmp);
     995         [ #  # ]:          0 :                 if (!ret)
     996                 :            :                         ret = ret_tmp;
     997                 :            :         }
     998                 :            : 
     999                 :            :         goto out_unlock;
    1000                 :            : 
    1001                 :            : err_free_tree:
    1002                 :          0 :         kfree(fdt);
    1003                 :          0 :         kfree(tree);
    1004                 :            : 
    1005                 :            : err_free_overlay_changeset:
    1006                 :          0 :         free_overlay_changeset(ovcs);
    1007                 :            : 
    1008                 :            : out_unlock:
    1009                 :          0 :         mutex_unlock(&of_mutex);
    1010                 :            :         of_overlay_mutex_unlock();
    1011                 :            : 
    1012                 :            : out:
    1013                 :            :         pr_debug("%s() err=%d\n", __func__, ret);
    1014                 :            : 
    1015                 :          0 :         return ret;
    1016                 :            : }
    1017                 :            : 
    1018                 :          0 : int of_overlay_fdt_apply(const void *overlay_fdt, u32 overlay_fdt_size,
    1019                 :            :                          int *ovcs_id)
    1020                 :            : {
    1021                 :            :         const void *new_fdt;
    1022                 :            :         int ret;
    1023                 :            :         u32 size;
    1024                 :            :         struct device_node *overlay_root;
    1025                 :            : 
    1026                 :          0 :         *ovcs_id = 0;
    1027                 :            :         ret = 0;
    1028                 :            : 
    1029   [ #  #  #  # ]:          0 :         if (overlay_fdt_size < sizeof(struct fdt_header) ||
    1030                 :          0 :             fdt_check_header(overlay_fdt)) {
    1031                 :          0 :                 pr_err("Invalid overlay_fdt header\n");
    1032                 :          0 :                 return -EINVAL;
    1033                 :            :         }
    1034                 :            : 
    1035                 :            :         size = fdt_totalsize(overlay_fdt);
    1036         [ #  # ]:          0 :         if (overlay_fdt_size < size)
    1037                 :            :                 return -EINVAL;
    1038                 :            : 
    1039                 :            :         /*
    1040                 :            :          * Must create permanent copy of FDT because of_fdt_unflatten_tree()
    1041                 :            :          * will create pointers to the passed in FDT in the unflattened tree.
    1042                 :            :          */
    1043                 :          0 :         new_fdt = kmemdup(overlay_fdt, size, GFP_KERNEL);
    1044         [ #  # ]:          0 :         if (!new_fdt)
    1045                 :            :                 return -ENOMEM;
    1046                 :            : 
    1047                 :          0 :         of_fdt_unflatten_tree(new_fdt, NULL, &overlay_root);
    1048         [ #  # ]:          0 :         if (!overlay_root) {
    1049                 :          0 :                 pr_err("unable to unflatten overlay_fdt\n");
    1050                 :            :                 ret = -EINVAL;
    1051                 :            :                 goto out_free_new_fdt;
    1052                 :            :         }
    1053                 :            : 
    1054                 :          0 :         ret = of_overlay_apply(new_fdt, overlay_root, ovcs_id);
    1055         [ #  # ]:          0 :         if (ret < 0) {
    1056                 :            :                 /*
    1057                 :            :                  * new_fdt and overlay_root now belong to the overlay
    1058                 :            :                  * changeset.
    1059                 :            :                  * overlay changeset code is responsible for freeing them.
    1060                 :            :                  */
    1061                 :            :                 goto out;
    1062                 :            :         }
    1063                 :            : 
    1064                 :            :         return 0;
    1065                 :            : 
    1066                 :            : 
    1067                 :            : out_free_new_fdt:
    1068                 :          0 :         kfree(new_fdt);
    1069                 :            : 
    1070                 :            : out:
    1071                 :          0 :         return ret;
    1072                 :            : }
    1073                 :            : EXPORT_SYMBOL_GPL(of_overlay_fdt_apply);
    1074                 :            : 
    1075                 :            : /*
    1076                 :            :  * Find @np in @tree.
    1077                 :            :  *
    1078                 :            :  * Returns 1 if @np is @tree or is contained in @tree, else 0
    1079                 :            :  */
    1080                 :          0 : static int find_node(struct device_node *tree, struct device_node *np)
    1081                 :            : {
    1082                 :            :         struct device_node *child;
    1083                 :            : 
    1084         [ #  # ]:          0 :         if (tree == np)
    1085                 :            :                 return 1;
    1086                 :            : 
    1087         [ #  # ]:          0 :         for_each_child_of_node(tree, child) {
    1088         [ #  # ]:          0 :                 if (find_node(child, np)) {
    1089                 :          0 :                         of_node_put(child);
    1090                 :          0 :                         return 1;
    1091                 :            :                 }
    1092                 :            :         }
    1093                 :            : 
    1094                 :            :         return 0;
    1095                 :            : }
    1096                 :            : 
    1097                 :            : /*
    1098                 :            :  * Is @remove_ce_node a child of, a parent of, or the same as any
    1099                 :            :  * node in an overlay changeset more topmost than @remove_ovcs?
    1100                 :            :  *
    1101                 :            :  * Returns 1 if found, else 0
    1102                 :            :  */
    1103                 :          0 : static int node_overlaps_later_cs(struct overlay_changeset *remove_ovcs,
    1104                 :            :                 struct device_node *remove_ce_node)
    1105                 :            : {
    1106                 :            :         struct overlay_changeset *ovcs;
    1107                 :            :         struct of_changeset_entry *ce;
    1108                 :            : 
    1109         [ #  # ]:          0 :         list_for_each_entry_reverse(ovcs, &ovcs_list, ovcs_list) {
    1110         [ #  # ]:          0 :                 if (ovcs == remove_ovcs)
    1111                 :            :                         break;
    1112                 :            : 
    1113         [ #  # ]:          0 :                 list_for_each_entry(ce, &ovcs->cset.entries, node) {
    1114         [ #  # ]:          0 :                         if (find_node(ce->np, remove_ce_node)) {
    1115                 :          0 :                                 pr_err("%s: #%d overlaps with #%d @%pOF\n",
    1116                 :            :                                         __func__, remove_ovcs->id, ovcs->id,
    1117                 :            :                                         remove_ce_node);
    1118                 :          0 :                                 return 1;
    1119                 :            :                         }
    1120         [ #  # ]:          0 :                         if (find_node(remove_ce_node, ce->np)) {
    1121                 :          0 :                                 pr_err("%s: #%d overlaps with #%d @%pOF\n",
    1122                 :            :                                         __func__, remove_ovcs->id, ovcs->id,
    1123                 :            :                                         remove_ce_node);
    1124                 :          0 :                                 return 1;
    1125                 :            :                         }
    1126                 :            :                 }
    1127                 :            :         }
    1128                 :            : 
    1129                 :            :         return 0;
    1130                 :            : }
    1131                 :            : 
    1132                 :            : /*
    1133                 :            :  * We can safely remove the overlay only if it's the top-most one.
    1134                 :            :  * Newly applied overlays are inserted at the tail of the overlay list,
    1135                 :            :  * so a top most overlay is the one that is closest to the tail.
    1136                 :            :  *
    1137                 :            :  * The topmost check is done by exploiting this property. For each
    1138                 :            :  * affected device node in the log list we check if this overlay is
    1139                 :            :  * the one closest to the tail. If another overlay has affected this
    1140                 :            :  * device node and is closest to the tail, then removal is not permited.
    1141                 :            :  */
    1142                 :          0 : static int overlay_removal_is_ok(struct overlay_changeset *remove_ovcs)
    1143                 :            : {
    1144                 :            :         struct of_changeset_entry *remove_ce;
    1145                 :            : 
    1146         [ #  # ]:          0 :         list_for_each_entry(remove_ce, &remove_ovcs->cset.entries, node) {
    1147         [ #  # ]:          0 :                 if (node_overlaps_later_cs(remove_ovcs, remove_ce->np)) {
    1148                 :          0 :                         pr_err("overlay #%d is not topmost\n", remove_ovcs->id);
    1149                 :          0 :                         return 0;
    1150                 :            :                 }
    1151                 :            :         }
    1152                 :            : 
    1153                 :            :         return 1;
    1154                 :            : }
    1155                 :            : 
    1156                 :            : /**
    1157                 :            :  * of_overlay_remove() - Revert and free an overlay changeset
    1158                 :            :  * @ovcs_id:    Pointer to overlay changeset id
    1159                 :            :  *
    1160                 :            :  * Removes an overlay if it is permissible.  @ovcs_id was previously returned
    1161                 :            :  * by of_overlay_fdt_apply().
    1162                 :            :  *
    1163                 :            :  * If an error occurred while attempting to revert the overlay changeset,
    1164                 :            :  * then an attempt is made to re-apply any changeset entry that was
    1165                 :            :  * reverted.  If an error occurs on re-apply then the state of the device
    1166                 :            :  * tree can not be determined, and any following attempt to apply or remove
    1167                 :            :  * an overlay changeset will be refused.
    1168                 :            :  *
    1169                 :            :  * A non-zero return value will not revert the changeset if error is from:
    1170                 :            :  *   - parameter checks
    1171                 :            :  *   - overlay changeset pre-remove notifier
    1172                 :            :  *   - overlay changeset entry revert
    1173                 :            :  *
    1174                 :            :  * If an error is returned by an overlay changeset pre-remove notifier
    1175                 :            :  * then no further overlay changeset pre-remove notifier will be called.
    1176                 :            :  *
    1177                 :            :  * If more than one notifier returns an error, then the last notifier
    1178                 :            :  * error to occur is returned.
    1179                 :            :  *
    1180                 :            :  * A non-zero return value will revert the changeset if error is from:
    1181                 :            :  *   - overlay changeset entry notifier
    1182                 :            :  *   - overlay changeset post-remove notifier
    1183                 :            :  *
    1184                 :            :  * If an error is returned by an overlay changeset post-remove notifier
    1185                 :            :  * then no further overlay changeset post-remove notifier will be called.
    1186                 :            :  *
    1187                 :            :  * Returns 0 on success, or a negative error number.  *ovcs_id is set to
    1188                 :            :  * zero after reverting the changeset, even if a subsequent error occurs.
    1189                 :            :  */
    1190                 :          0 : int of_overlay_remove(int *ovcs_id)
    1191                 :            : {
    1192                 :            :         struct overlay_changeset *ovcs;
    1193                 :            :         int ret, ret_apply, ret_tmp;
    1194                 :            : 
    1195                 :            :         ret = 0;
    1196                 :            : 
    1197         [ #  # ]:          0 :         if (devicetree_corrupt()) {
    1198                 :          0 :                 pr_err("suspect devicetree state, refuse to remove overlay\n");
    1199                 :            :                 ret = -EBUSY;
    1200                 :          0 :                 goto out;
    1201                 :            :         }
    1202                 :            : 
    1203                 :          0 :         mutex_lock(&of_mutex);
    1204                 :            : 
    1205                 :          0 :         ovcs = idr_find(&ovcs_idr, *ovcs_id);
    1206         [ #  # ]:          0 :         if (!ovcs) {
    1207                 :            :                 ret = -ENODEV;
    1208                 :          0 :                 pr_err("remove: Could not find overlay #%d\n", *ovcs_id);
    1209                 :          0 :                 goto out_unlock;
    1210                 :            :         }
    1211                 :            : 
    1212         [ #  # ]:          0 :         if (!overlay_removal_is_ok(ovcs)) {
    1213                 :            :                 ret = -EBUSY;
    1214                 :            :                 goto out_unlock;
    1215                 :            :         }
    1216                 :            : 
    1217                 :          0 :         ret = overlay_notify(ovcs, OF_OVERLAY_PRE_REMOVE);
    1218         [ #  # ]:          0 :         if (ret) {
    1219                 :          0 :                 pr_err("overlay changeset pre-remove notify error %d\n", ret);
    1220                 :          0 :                 goto out_unlock;
    1221                 :            :         }
    1222                 :            : 
    1223                 :            :         list_del(&ovcs->ovcs_list);
    1224                 :            : 
    1225                 :            :         /*
    1226                 :            :          * Disable phandle cache.  Avoids race condition that would arise
    1227                 :            :          * from removing cache entry when the associated node is deleted.
    1228                 :            :          */
    1229                 :          0 :         of_free_phandle_cache();
    1230                 :            : 
    1231                 :          0 :         ret_apply = 0;
    1232                 :          0 :         ret = __of_changeset_revert_entries(&ovcs->cset, &ret_apply);
    1233                 :            : 
    1234                 :          0 :         of_populate_phandle_cache();
    1235                 :            : 
    1236         [ #  # ]:          0 :         if (ret) {
    1237         [ #  # ]:          0 :                 if (ret_apply)
    1238                 :          0 :                         devicetree_state_flags |= DTSF_REVERT_FAIL;
    1239                 :            :                 goto out_unlock;
    1240                 :            :         }
    1241                 :            : 
    1242                 :          0 :         ret = __of_changeset_revert_notify(&ovcs->cset);
    1243         [ #  # ]:          0 :         if (ret)
    1244                 :          0 :                 pr_err("overlay remove changeset entry notify error %d\n", ret);
    1245                 :            :         /* notify failure is not fatal, continue */
    1246                 :            : 
    1247                 :          0 :         *ovcs_id = 0;
    1248                 :            : 
    1249                 :          0 :         ret_tmp = overlay_notify(ovcs, OF_OVERLAY_POST_REMOVE);
    1250         [ #  # ]:          0 :         if (ret_tmp) {
    1251                 :          0 :                 pr_err("overlay changeset post-remove notify error %d\n",
    1252                 :            :                        ret_tmp);
    1253         [ #  # ]:          0 :                 if (!ret)
    1254                 :            :                         ret = ret_tmp;
    1255                 :            :         }
    1256                 :            : 
    1257                 :          0 :         free_overlay_changeset(ovcs);
    1258                 :            : 
    1259                 :            : out_unlock:
    1260                 :          0 :         mutex_unlock(&of_mutex);
    1261                 :            : 
    1262                 :            : out:
    1263                 :            :         pr_debug("%s() err=%d\n", __func__, ret);
    1264                 :            : 
    1265                 :          0 :         return ret;
    1266                 :            : }
    1267                 :            : EXPORT_SYMBOL_GPL(of_overlay_remove);
    1268                 :            : 
    1269                 :            : /**
    1270                 :            :  * of_overlay_remove_all() - Reverts and frees all overlay changesets
    1271                 :            :  *
    1272                 :            :  * Removes all overlays from the system in the correct order.
    1273                 :            :  *
    1274                 :            :  * Returns 0 on success, or a negative error number
    1275                 :            :  */
    1276                 :          0 : int of_overlay_remove_all(void)
    1277                 :            : {
    1278                 :            :         struct overlay_changeset *ovcs, *ovcs_n;
    1279                 :            :         int ret;
    1280                 :            : 
    1281                 :            :         /* the tail of list is guaranteed to be safe to remove */
    1282         [ #  # ]:          0 :         list_for_each_entry_safe_reverse(ovcs, ovcs_n, &ovcs_list, ovcs_list) {
    1283                 :          0 :                 ret = of_overlay_remove(&ovcs->id);
    1284         [ #  # ]:          0 :                 if (ret)
    1285                 :          0 :                         return ret;
    1286                 :            :         }
    1287                 :            : 
    1288                 :            :         return 0;
    1289                 :            : }
    1290                 :            : EXPORT_SYMBOL_GPL(of_overlay_remove_all);

Generated by: LCOV version 1.14