LCOV - code coverage report
Current view: top level - kernel/gcov - fs.c (source / functions) Hit Total Coverage
Test: landlock.info Lines: 170 333 51.1 %
Date: 2021-04-22 12:43:58 Functions: 18 29 62.1 %

          Line data    Source code
       1             : // SPDX-License-Identifier: GPL-2.0
       2             : /*
       3             :  *  This code exports profiling data as debugfs files to userspace.
       4             :  *
       5             :  *    Copyright IBM Corp. 2009
       6             :  *    Author(s): Peter Oberparleiter <oberpar@linux.vnet.ibm.com>
       7             :  *
       8             :  *    Uses gcc-internal data definitions.
       9             :  *    Based on the gcov-kernel patch by:
      10             :  *               Hubertus Franke <frankeh@us.ibm.com>
      11             :  *               Nigel Hinds <nhinds@us.ibm.com>
      12             :  *               Rajan Ravindran <rajancr@us.ibm.com>
      13             :  *               Peter Oberparleiter <oberpar@linux.vnet.ibm.com>
      14             :  *               Paul Larson
      15             :  *               Yi CDL Yang
      16             :  */
      17             : 
      18             : #define pr_fmt(fmt)     "gcov: " fmt
      19             : 
      20             : #include <linux/init.h>
      21             : #include <linux/module.h>
      22             : #include <linux/debugfs.h>
      23             : #include <linux/fs.h>
      24             : #include <linux/list.h>
      25             : #include <linux/string.h>
      26             : #include <linux/slab.h>
      27             : #include <linux/mutex.h>
      28             : #include <linux/seq_file.h>
      29             : #include "gcov.h"
      30             : 
      31             : /**
      32             :  * struct gcov_node - represents a debugfs entry
      33             :  * @list: list head for child node list
      34             :  * @children: child nodes
      35             :  * @all: list head for list of all nodes
      36             :  * @parent: parent node
      37             :  * @loaded_info: array of pointers to profiling data sets for loaded object
      38             :  *   files.
      39             :  * @num_loaded: number of profiling data sets for loaded object files.
      40             :  * @unloaded_info: accumulated copy of profiling data sets for unloaded
      41             :  *   object files. Used only when gcov_persist=1.
      42             :  * @dentry: main debugfs entry, either a directory or data file
      43             :  * @links: associated symbolic links
      44             :  * @name: data file basename
      45             :  *
      46             :  * struct gcov_node represents an entity within the gcov/ subdirectory
      47             :  * of debugfs. There are directory and data file nodes. The latter represent
      48             :  * the actual synthesized data file plus any associated symbolic links which
      49             :  * are needed by the gcov tool to work correctly.
      50             :  */
      51             : struct gcov_node {
      52             :         struct list_head list;
      53             :         struct list_head children;
      54             :         struct list_head all;
      55             :         struct gcov_node *parent;
      56             :         struct gcov_info **loaded_info;
      57             :         struct gcov_info *unloaded_info;
      58             :         struct dentry *dentry;
      59             :         struct dentry **links;
      60             :         int num_loaded;
      61             :         char name[];
      62             : };
      63             : 
      64             : static const char objtree[] = OBJTREE;
      65             : static const char srctree[] = SRCTREE;
      66             : static struct gcov_node root_node;
      67             : static LIST_HEAD(all_head);
      68             : static DEFINE_MUTEX(node_lock);
      69             : 
      70             : /* If non-zero, keep copies of profiling data for unloaded modules. */
      71             : static int gcov_persist = 1;
      72             : 
      73           0 : static int __init gcov_persist_setup(char *str)
      74             : {
      75           0 :         unsigned long val;
      76             : 
      77           0 :         if (kstrtoul(str, 0, &val)) {
      78           0 :                 pr_warn("invalid gcov_persist parameter '%s'\n", str);
      79           0 :                 return 0;
      80             :         }
      81           0 :         gcov_persist = val;
      82           0 :         pr_info("setting gcov_persist to %d\n", gcov_persist);
      83             : 
      84           0 :         return 1;
      85             : }
      86             : __setup("gcov_persist=", gcov_persist_setup);
      87             : 
      88             : /*
      89             :  * seq_file.start() implementation for gcov data files. Note that the
      90             :  * gcov_iterator interface is designed to be more restrictive than seq_file
      91             :  * (no start from arbitrary position, etc.), to simplify the iterator
      92             :  * implementation.
      93             :  */
      94         356 : static void *gcov_seq_start(struct seq_file *seq, loff_t *pos)
      95             : {
      96         356 :         loff_t i;
      97             : 
      98         356 :         gcov_iter_start(seq->private);
      99         917 :         for (i = 0; i < *pos; i++) {
     100         339 :                 if (gcov_iter_next(seq->private))
     101             :                         return NULL;
     102             :         }
     103         222 :         return seq->private;
     104             : }
     105             : 
     106             : /* seq_file.next() implementation for gcov data files. */
     107         222 : static void *gcov_seq_next(struct seq_file *seq, void *data, loff_t *pos)
     108             : {
     109         222 :         struct gcov_iterator *iter = data;
     110             : 
     111         222 :         (*pos)++;
     112         222 :         if (gcov_iter_next(iter))
     113         134 :                 return NULL;
     114             : 
     115             :         return iter;
     116             : }
     117             : 
     118             : /* seq_file.show() implementation for gcov data files. */
     119         310 : static int gcov_seq_show(struct seq_file *seq, void *data)
     120             : {
     121         310 :         struct gcov_iterator *iter = data;
     122             : 
     123         310 :         if (gcov_iter_write(iter, seq))
     124           0 :                 return -EINVAL;
     125             :         return 0;
     126             : }
     127             : 
     128         356 : static void gcov_seq_stop(struct seq_file *seq, void *data)
     129             : {
     130             :         /* Unused. */
     131         356 : }
     132             : 
     133             : static const struct seq_operations gcov_seq_ops = {
     134             :         .start  = gcov_seq_start,
     135             :         .next   = gcov_seq_next,
     136             :         .show   = gcov_seq_show,
     137             :         .stop   = gcov_seq_stop,
     138             : };
     139             : 
     140             : /*
     141             :  * Return a profiling data set associated with the given node. This is
     142             :  * either a data set for a loaded object file or a data set copy in case
     143             :  * all associated object files have been unloaded.
     144             :  */
     145      511981 : static struct gcov_info *get_node_info(struct gcov_node *node)
     146             : {
     147      511981 :         if (node->num_loaded > 0)
     148      456490 :                 return node->loaded_info[0];
     149             : 
     150       55491 :         return node->unloaded_info;
     151             : }
     152             : 
     153             : /*
     154             :  * Return a newly allocated profiling data set which contains the sum of
     155             :  * all profiling data associated with the given node.
     156             :  */
     157         135 : static struct gcov_info *get_accumulated_info(struct gcov_node *node)
     158             : {
     159         135 :         struct gcov_info *info;
     160         135 :         int i = 0;
     161             : 
     162         135 :         if (node->unloaded_info)
     163           0 :                 info = gcov_info_dup(node->unloaded_info);
     164             :         else
     165         135 :                 info = gcov_info_dup(node->loaded_info[i++]);
     166         134 :         if (!info)
     167             :                 return NULL;
     168         134 :         for (; i < node->num_loaded; i++)
     169           0 :                 gcov_info_add(info, node->loaded_info[i]);
     170             : 
     171             :         return info;
     172             : }
     173             : 
     174             : /*
     175             :  * open() implementation for gcov data files. Create a copy of the profiling
     176             :  * data set and initialize the iterator and seq_file interface.
     177             :  */
     178         135 : static int gcov_seq_open(struct inode *inode, struct file *file)
     179             : {
     180         135 :         struct gcov_node *node = inode->i_private;
     181         135 :         struct gcov_iterator *iter;
     182         135 :         struct seq_file *seq;
     183         135 :         struct gcov_info *info;
     184         135 :         int rc = -ENOMEM;
     185             : 
     186         135 :         mutex_lock(&node_lock);
     187             :         /*
     188             :          * Read from a profiling data copy to minimize reference tracking
     189             :          * complexity and concurrent access and to keep accumulating multiple
     190             :          * profiling data sets associated with one node simple.
     191             :          */
     192         135 :         info = get_accumulated_info(node);
     193         134 :         if (!info)
     194           0 :                 goto out_unlock;
     195         134 :         iter = gcov_iter_new(info);
     196         134 :         if (!iter)
     197           0 :                 goto err_free_info;
     198         134 :         rc = seq_open(file, &gcov_seq_ops);
     199         134 :         if (rc)
     200           0 :                 goto err_free_iter_info;
     201         134 :         seq = file->private_data;
     202         134 :         seq->private = iter;
     203         134 : out_unlock:
     204         134 :         mutex_unlock(&node_lock);
     205         134 :         return rc;
     206             : 
     207           0 : err_free_iter_info:
     208           0 :         gcov_iter_free(iter);
     209           0 : err_free_info:
     210           0 :         gcov_info_free(info);
     211           0 :         goto out_unlock;
     212             : }
     213             : 
     214             : /*
     215             :  * release() implementation for gcov data files. Release resources allocated
     216             :  * by open().
     217             :  */
     218         134 : static int gcov_seq_release(struct inode *inode, struct file *file)
     219             : {
     220         134 :         struct gcov_iterator *iter;
     221         134 :         struct gcov_info *info;
     222         134 :         struct seq_file *seq;
     223             : 
     224         134 :         seq = file->private_data;
     225         134 :         iter = seq->private;
     226         134 :         info = gcov_iter_get_info(iter);
     227         134 :         gcov_iter_free(iter);
     228         134 :         gcov_info_free(info);
     229         134 :         seq_release(inode, file);
     230             : 
     231         134 :         return 0;
     232             : }
     233             : 
     234             : /*
     235             :  * Find a node by the associated data file name. Needs to be called with
     236             :  * node_lock held.
     237             :  */
     238         955 : static struct gcov_node *get_node_by_name(const char *name)
     239             : {
     240         955 :         struct gcov_node *node;
     241         955 :         struct gcov_info *info;
     242             : 
     243      511981 :         list_for_each_entry(node, &all_head, all) {
     244      511026 :                 info = get_node_info(node);
     245      511026 :                 if (info && (strcmp(gcov_info_filename(info), name) == 0))
     246           0 :                         return node;
     247             :         }
     248             : 
     249             :         return NULL;
     250             : }
     251             : 
     252             : /*
     253             :  * Reset all profiling data associated with the specified node.
     254             :  */
     255           0 : static void reset_node(struct gcov_node *node)
     256             : {
     257           0 :         int i;
     258             : 
     259           0 :         if (node->unloaded_info)
     260           0 :                 gcov_info_reset(node->unloaded_info);
     261           0 :         for (i = 0; i < node->num_loaded; i++)
     262           0 :                 gcov_info_reset(node->loaded_info[i]);
     263           0 : }
     264             : 
     265             : static void remove_node(struct gcov_node *node);
     266             : 
     267             : /*
     268             :  * write() implementation for gcov data files. Reset profiling data for the
     269             :  * corresponding file. If all associated object files have been unloaded,
     270             :  * remove the debug fs node as well.
     271             :  */
     272           0 : static ssize_t gcov_seq_write(struct file *file, const char __user *addr,
     273             :                               size_t len, loff_t *pos)
     274             : {
     275           0 :         struct seq_file *seq;
     276           0 :         struct gcov_info *info;
     277           0 :         struct gcov_node *node;
     278             : 
     279           0 :         seq = file->private_data;
     280           0 :         info = gcov_iter_get_info(seq->private);
     281           0 :         mutex_lock(&node_lock);
     282           0 :         node = get_node_by_name(gcov_info_filename(info));
     283           0 :         if (node) {
     284             :                 /* Reset counts or remove node for unloaded modules. */
     285           0 :                 if (node->num_loaded == 0)
     286           0 :                         remove_node(node);
     287             :                 else
     288           0 :                         reset_node(node);
     289             :         }
     290             :         /* Reset counts for open file. */
     291           0 :         gcov_info_reset(info);
     292           0 :         mutex_unlock(&node_lock);
     293             : 
     294           0 :         return len;
     295             : }
     296             : 
     297             : /*
     298             :  * Given a string <path> representing a file path of format:
     299             :  *   path/to/file.gcda
     300             :  * construct and return a new string:
     301             :  *   <dir/>path/to/file.<ext>
     302             :  */
     303         955 : static char *link_target(const char *dir, const char *path, const char *ext)
     304             : {
     305         955 :         char *target;
     306         955 :         char *old_ext;
     307         955 :         char *copy;
     308             : 
     309         955 :         copy = kstrdup(path, GFP_KERNEL);
     310         955 :         if (!copy)
     311             :                 return NULL;
     312         955 :         old_ext = strrchr(copy, '.');
     313         955 :         if (old_ext)
     314         955 :                 *old_ext = '\0';
     315         955 :         if (dir)
     316           0 :                 target = kasprintf(GFP_KERNEL, "%s/%s.%s", dir, copy, ext);
     317             :         else
     318         955 :                 target = kasprintf(GFP_KERNEL, "%s.%s", copy, ext);
     319         955 :         kfree(copy);
     320             : 
     321         955 :         return target;
     322             : }
     323             : 
     324             : /*
     325             :  * Construct a string representing the symbolic link target for the given
     326             :  * gcov data file name and link type. Depending on the link type and the
     327             :  * location of the data file, the link target can either point to a
     328             :  * subdirectory of srctree, objtree or in an external location.
     329             :  */
     330         955 : static char *get_link_target(const char *filename, const struct gcov_link *ext)
     331             : {
     332         955 :         const char *rel;
     333         955 :         char *result;
     334             : 
     335         955 :         if (strncmp(filename, objtree, strlen(objtree)) == 0) {
     336           0 :                 rel = filename + strlen(objtree) + 1;
     337           0 :                 if (ext->dir == SRC_TREE)
     338           0 :                         result = link_target(srctree, rel, ext->ext);
     339             :                 else
     340           0 :                         result = link_target(objtree, rel, ext->ext);
     341             :         } else {
     342             :                 /* External compilation. */
     343         955 :                 result = link_target(NULL, filename, ext->ext);
     344             :         }
     345             : 
     346         955 :         return result;
     347             : }
     348             : 
     349             : #define SKEW_PREFIX     ".tmp_"
     350             : 
     351             : /*
     352             :  * For a filename .tmp_filename.ext return filename.ext. Needed to compensate
     353             :  * for filename skewing caused by the mod-versioning mechanism.
     354             :  */
     355        1910 : static const char *deskew(const char *basename)
     356             : {
     357        1910 :         if (strncmp(basename, SKEW_PREFIX, sizeof(SKEW_PREFIX) - 1) == 0)
     358           0 :                 return basename + sizeof(SKEW_PREFIX) - 1;
     359             :         return basename;
     360             : }
     361             : 
     362             : /*
     363             :  * Create links to additional files (usually .c and .gcno files) which the
     364             :  * gcov tool expects to find in the same directory as the gcov data file.
     365             :  */
     366         955 : static void add_links(struct gcov_node *node, struct dentry *parent)
     367             : {
     368         955 :         const char *basename;
     369         955 :         char *target;
     370         955 :         int num;
     371         955 :         int i;
     372             : 
     373        1910 :         for (num = 0; gcov_link[num].ext; num++)
     374         955 :                 /* Nothing. */;
     375         955 :         node->links = kcalloc(num, sizeof(struct dentry *), GFP_KERNEL);
     376         955 :         if (!node->links)
     377             :                 return;
     378        1910 :         for (i = 0; i < num; i++) {
     379        1910 :                 target = get_link_target(
     380             :                                 gcov_info_filename(get_node_info(node)),
     381             :                                 &gcov_link[i]);
     382         955 :                 if (!target)
     383           0 :                         goto out_err;
     384         955 :                 basename = kbasename(target);
     385         955 :                 if (basename == target)
     386           0 :                         goto out_err;
     387         955 :                 node->links[i] = debugfs_create_symlink(deskew(basename),
     388             :                                                         parent, target);
     389         955 :                 kfree(target);
     390             :         }
     391             : 
     392             :         return;
     393           0 : out_err:
     394           0 :         kfree(target);
     395           0 :         while (i-- > 0)
     396           0 :                 debugfs_remove(node->links[i]);
     397           0 :         kfree(node->links);
     398           0 :         node->links = NULL;
     399             : }
     400             : 
     401             : static const struct file_operations gcov_data_fops = {
     402             :         .open           = gcov_seq_open,
     403             :         .release        = gcov_seq_release,
     404             :         .read           = seq_read,
     405             :         .llseek         = seq_lseek,
     406             :         .write          = gcov_seq_write,
     407             : };
     408             : 
     409             : /* Basic initialization of a new node. */
     410        1058 : static void init_node(struct gcov_node *node, struct gcov_info *info,
     411             :                       const char *name, struct gcov_node *parent)
     412             : {
     413        1057 :         INIT_LIST_HEAD(&node->list);
     414        1058 :         INIT_LIST_HEAD(&node->children);
     415        1058 :         INIT_LIST_HEAD(&node->all);
     416        1058 :         if (node->loaded_info) {
     417         955 :                 node->loaded_info[0] = info;
     418         955 :                 node->num_loaded = 1;
     419             :         }
     420        1058 :         node->parent = parent;
     421        1057 :         if (name)
     422        1057 :                 strcpy(node->name, name);
     423        1057 : }
     424             : 
     425             : /*
     426             :  * Create a new node and associated debugfs entry. Needs to be called with
     427             :  * node_lock held.
     428             :  */
     429        1057 : static struct gcov_node *new_node(struct gcov_node *parent,
     430             :                                   struct gcov_info *info, const char *name)
     431             : {
     432        1057 :         struct gcov_node *node;
     433             : 
     434        1057 :         node = kzalloc(sizeof(struct gcov_node) + strlen(name) + 1, GFP_KERNEL);
     435        1057 :         if (!node)
     436           0 :                 goto err_nomem;
     437        1057 :         if (info) {
     438         955 :                 node->loaded_info = kcalloc(1, sizeof(struct gcov_info *),
     439             :                                            GFP_KERNEL);
     440         955 :                 if (!node->loaded_info)
     441           0 :                         goto err_nomem;
     442             :         }
     443        1057 :         init_node(node, info, name, parent);
     444             :         /* Differentiate between gcov data file nodes and directory nodes. */
     445        1057 :         if (info) {
     446         955 :                 node->dentry = debugfs_create_file(deskew(node->name), 0600,
     447             :                                         parent->dentry, node, &gcov_data_fops);
     448             :         } else
     449         102 :                 node->dentry = debugfs_create_dir(node->name, parent->dentry);
     450        1057 :         if (info)
     451         955 :                 add_links(node, parent->dentry);
     452        1057 :         list_add(&node->list, &parent->children);
     453        1057 :         list_add(&node->all, &all_head);
     454             : 
     455        1057 :         return node;
     456             : 
     457           0 : err_nomem:
     458           0 :         kfree(node);
     459           0 :         pr_warn("out of memory\n");
     460           0 :         return NULL;
     461             : }
     462             : 
     463             : /* Remove symbolic links associated with node. */
     464           0 : static void remove_links(struct gcov_node *node)
     465             : {
     466           0 :         int i;
     467             : 
     468           0 :         if (!node->links)
     469             :                 return;
     470           0 :         for (i = 0; gcov_link[i].ext; i++)
     471           0 :                 debugfs_remove(node->links[i]);
     472           0 :         kfree(node->links);
     473           0 :         node->links = NULL;
     474             : }
     475             : 
     476             : /*
     477             :  * Remove node from all lists and debugfs and release associated resources.
     478             :  * Needs to be called with node_lock held.
     479             :  */
     480           0 : static void release_node(struct gcov_node *node)
     481             : {
     482           0 :         list_del(&node->list);
     483           0 :         list_del(&node->all);
     484           0 :         debugfs_remove(node->dentry);
     485           0 :         remove_links(node);
     486           0 :         kfree(node->loaded_info);
     487           0 :         if (node->unloaded_info)
     488           0 :                 gcov_info_free(node->unloaded_info);
     489           0 :         kfree(node);
     490           0 : }
     491             : 
     492             : /* Release node and empty parents. Needs to be called with node_lock held. */
     493           0 : static void remove_node(struct gcov_node *node)
     494             : {
     495           0 :         struct gcov_node *parent;
     496             : 
     497           0 :         while ((node != &root_node) && list_empty(&node->children)) {
     498           0 :                 parent = node->parent;
     499           0 :                 release_node(node);
     500           0 :                 node = parent;
     501             :         }
     502           0 : }
     503             : 
     504             : /*
     505             :  * Find child node with given basename. Needs to be called with node_lock
     506             :  * held.
     507             :  */
     508        6617 : static struct gcov_node *get_child_by_name(struct gcov_node *parent,
     509             :                                            const char *name)
     510             : {
     511        6617 :         struct gcov_node *node;
     512             : 
     513        9080 :         list_for_each_entry(node, &parent->children, list) {
     514        8978 :                 if (strcmp(node->name, name) == 0)
     515        6515 :                         return node;
     516             :         }
     517             : 
     518             :         return NULL;
     519             : }
     520             : 
     521             : /*
     522             :  * write() implementation for reset file. Reset all profiling data to zero
     523             :  * and remove nodes for which all associated object files are unloaded.
     524             :  */
     525           0 : static ssize_t reset_write(struct file *file, const char __user *addr,
     526             :                            size_t len, loff_t *pos)
     527             : {
     528           0 :         struct gcov_node *node;
     529             : 
     530           0 :         mutex_lock(&node_lock);
     531           0 : restart:
     532           0 :         list_for_each_entry(node, &all_head, all) {
     533           0 :                 if (node->num_loaded > 0)
     534           0 :                         reset_node(node);
     535           0 :                 else if (list_empty(&node->children)) {
     536           0 :                         remove_node(node);
     537             :                         /* Several nodes may have gone - restart loop. */
     538           0 :                         goto restart;
     539             :                 }
     540             :         }
     541           0 :         mutex_unlock(&node_lock);
     542             : 
     543           0 :         return len;
     544             : }
     545             : 
     546             : /* read() implementation for reset file. Unused. */
     547           0 : static ssize_t reset_read(struct file *file, char __user *addr, size_t len,
     548             :                           loff_t *pos)
     549             : {
     550             :         /* Allow read operation so that a recursive copy won't fail. */
     551           0 :         return 0;
     552             : }
     553             : 
     554             : static const struct file_operations gcov_reset_fops = {
     555             :         .write  = reset_write,
     556             :         .read   = reset_read,
     557             :         .llseek = noop_llseek,
     558             : };
     559             : 
     560             : /*
     561             :  * Create a node for a given profiling data set and add it to all lists and
     562             :  * debugfs. Needs to be called with node_lock held.
     563             :  */
     564         955 : static void add_node(struct gcov_info *info)
     565             : {
     566         955 :         char *filename;
     567         955 :         char *curr;
     568         955 :         char *next;
     569         955 :         struct gcov_node *parent;
     570         955 :         struct gcov_node *node;
     571             : 
     572         955 :         filename = kstrdup(gcov_info_filename(info), GFP_KERNEL);
     573         955 :         if (!filename)
     574             :                 return;
     575             :         parent = &root_node;
     576             :         /* Create directory nodes along the path. */
     577        8527 :         for (curr = filename; (next = strchr(curr, '/')); curr = next + 1) {
     578        7572 :                 if (curr == next)
     579         955 :                         continue;
     580        6617 :                 *next = 0;
     581        6617 :                 if (strcmp(curr, ".") == 0)
     582           0 :                         continue;
     583        6617 :                 if (strcmp(curr, "..") == 0) {
     584           0 :                         if (!parent->parent)
     585           0 :                                 goto err_remove;
     586           0 :                         parent = parent->parent;
     587           0 :                         continue;
     588             :                 }
     589        6617 :                 node = get_child_by_name(parent, curr);
     590        6617 :                 if (!node) {
     591         102 :                         node = new_node(parent, NULL, curr);
     592         102 :                         if (!node)
     593           0 :                                 goto err_remove;
     594             :                 }
     595             :                 parent = node;
     596             :         }
     597             :         /* Create file node. */
     598         955 :         node = new_node(parent, info, curr);
     599         955 :         if (!node)
     600           0 :                 goto err_remove;
     601         955 : out:
     602         955 :         kfree(filename);
     603         955 :         return;
     604             : 
     605           0 : err_remove:
     606           0 :         remove_node(parent);
     607           0 :         goto out;
     608             : }
     609             : 
     610             : /*
     611             :  * Associate a profiling data set with an existing node. Needs to be called
     612             :  * with node_lock held.
     613             :  */
     614           0 : static void add_info(struct gcov_node *node, struct gcov_info *info)
     615             : {
     616           0 :         struct gcov_info **loaded_info;
     617           0 :         int num = node->num_loaded;
     618             : 
     619             :         /*
     620             :          * Prepare new array. This is done first to simplify cleanup in
     621             :          * case the new data set is incompatible, the node only contains
     622             :          * unloaded data sets and there's not enough memory for the array.
     623             :          */
     624           0 :         loaded_info = kcalloc(num + 1, sizeof(struct gcov_info *), GFP_KERNEL);
     625           0 :         if (!loaded_info) {
     626           0 :                 pr_warn("could not add '%s' (out of memory)\n",
     627             :                         gcov_info_filename(info));
     628           0 :                 return;
     629             :         }
     630           0 :         memcpy(loaded_info, node->loaded_info,
     631             :                num * sizeof(struct gcov_info *));
     632           0 :         loaded_info[num] = info;
     633             :         /* Check if the new data set is compatible. */
     634           0 :         if (num == 0) {
     635             :                 /*
     636             :                  * A module was unloaded, modified and reloaded. The new
     637             :                  * data set replaces the copy of the last one.
     638             :                  */
     639           0 :                 if (!gcov_info_is_compatible(node->unloaded_info, info)) {
     640           0 :                         pr_warn("discarding saved data for %s "
     641             :                                 "(incompatible version)\n",
     642             :                                 gcov_info_filename(info));
     643           0 :                         gcov_info_free(node->unloaded_info);
     644           0 :                         node->unloaded_info = NULL;
     645             :                 }
     646             :         } else {
     647             :                 /*
     648             :                  * Two different versions of the same object file are loaded.
     649             :                  * The initial one takes precedence.
     650             :                  */
     651           0 :                 if (!gcov_info_is_compatible(node->loaded_info[0], info)) {
     652           0 :                         pr_warn("could not add '%s' (incompatible "
     653             :                                 "version)\n", gcov_info_filename(info));
     654           0 :                         kfree(loaded_info);
     655           0 :                         return;
     656             :                 }
     657             :         }
     658             :         /* Overwrite previous array. */
     659           0 :         kfree(node->loaded_info);
     660           0 :         node->loaded_info = loaded_info;
     661           0 :         node->num_loaded = num + 1;
     662             : }
     663             : 
     664             : /*
     665             :  * Return the index of a profiling data set associated with a node.
     666             :  */
     667           0 : static int get_info_index(struct gcov_node *node, struct gcov_info *info)
     668             : {
     669           0 :         int i;
     670             : 
     671           0 :         for (i = 0; i < node->num_loaded; i++) {
     672           0 :                 if (node->loaded_info[i] == info)
     673             :                         return i;
     674             :         }
     675             :         return -ENOENT;
     676             : }
     677             : 
     678             : /*
     679             :  * Save the data of a profiling data set which is being unloaded.
     680             :  */
     681           0 : static void save_info(struct gcov_node *node, struct gcov_info *info)
     682             : {
     683           0 :         if (node->unloaded_info)
     684           0 :                 gcov_info_add(node->unloaded_info, info);
     685             :         else {
     686           0 :                 node->unloaded_info = gcov_info_dup(info);
     687           0 :                 if (!node->unloaded_info) {
     688           0 :                         pr_warn("could not save data for '%s' "
     689             :                                 "(out of memory)\n",
     690             :                                 gcov_info_filename(info));
     691             :                 }
     692             :         }
     693           0 : }
     694             : 
     695             : /*
     696             :  * Disassociate a profiling data set from a node. Needs to be called with
     697             :  * node_lock held.
     698             :  */
     699           0 : static void remove_info(struct gcov_node *node, struct gcov_info *info)
     700             : {
     701           0 :         int i;
     702             : 
     703           0 :         i = get_info_index(node, info);
     704           0 :         if (i < 0) {
     705           0 :                 pr_warn("could not remove '%s' (not found)\n",
     706             :                         gcov_info_filename(info));
     707           0 :                 return;
     708             :         }
     709           0 :         if (gcov_persist)
     710           0 :                 save_info(node, info);
     711             :         /* Shrink array. */
     712           0 :         node->loaded_info[i] = node->loaded_info[node->num_loaded - 1];
     713           0 :         node->num_loaded--;
     714           0 :         if (node->num_loaded > 0)
     715             :                 return;
     716             :         /* Last loaded data set was removed. */
     717           0 :         kfree(node->loaded_info);
     718           0 :         node->loaded_info = NULL;
     719           0 :         node->num_loaded = 0;
     720           0 :         if (!node->unloaded_info)
     721           0 :                 remove_node(node);
     722             : }
     723             : 
     724             : /*
     725             :  * Callback to create/remove profiling files when code compiled with
     726             :  * -fprofile-arcs is loaded/unloaded.
     727             :  */
     728         955 : void gcov_event(enum gcov_action action, struct gcov_info *info)
     729             : {
     730         955 :         struct gcov_node *node;
     731             : 
     732         955 :         mutex_lock(&node_lock);
     733         955 :         node = get_node_by_name(gcov_info_filename(info));
     734         955 :         switch (action) {
     735         955 :         case GCOV_ADD:
     736         955 :                 if (node)
     737           0 :                         add_info(node, info);
     738             :                 else
     739         955 :                         add_node(info);
     740             :                 break;
     741           0 :         case GCOV_REMOVE:
     742           0 :                 if (node)
     743           0 :                         remove_info(node, info);
     744             :                 else {
     745           0 :                         pr_warn("could not remove '%s' (not found)\n",
     746             :                                 gcov_info_filename(info));
     747             :                 }
     748             :                 break;
     749             :         }
     750         955 :         mutex_unlock(&node_lock);
     751         955 : }
     752             : 
     753             : /* Create debugfs entries. */
     754           1 : static __init int gcov_fs_init(void)
     755             : {
     756           1 :         init_node(&root_node, NULL, NULL, NULL);
     757             :         /*
     758             :          * /sys/kernel/debug/gcov will be parent for the reset control file
     759             :          * and all profiling files.
     760             :          */
     761           1 :         root_node.dentry = debugfs_create_dir("gcov", NULL);
     762             :         /*
     763             :          * Create reset file which resets all profiling counts when written
     764             :          * to.
     765             :          */
     766           1 :         debugfs_create_file("reset", 0600, root_node.dentry, NULL,
     767             :                             &gcov_reset_fops);
     768             :         /* Replay previous events to get our fs hierarchy up-to-date. */
     769           1 :         gcov_enable_events();
     770           1 :         return 0;
     771             : }
     772             : device_initcall(gcov_fs_init);

Generated by: LCOV version 1.14