fs.c 19 KB

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