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