fs.c 19 KB

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