fs.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792
  1. /*
  2. * This code exports profiling data as debugfs files to userspace.
  3. *
  4. * Copyright IBM Corp. 2009
  5. * Author(s): Peter Oberparleiter <oberpar@linux.vnet.ibm.com>
  6. *
  7. * Uses gcc-internal data definitions.
  8. * Based on the gcov-kernel patch by:
  9. * Hubertus Franke <frankeh@us.ibm.com>
  10. * Nigel Hinds <nhinds@us.ibm.com>
  11. * Rajan Ravindran <rajancr@us.ibm.com>
  12. * Peter Oberparleiter <oberpar@linux.vnet.ibm.com>
  13. * Paul Larson
  14. * Yi CDL Yang
  15. */
  16. #define pr_fmt(fmt) "gcov: " fmt
  17. #include <linux/init.h>
  18. #include <linux/module.h>
  19. #include <linux/debugfs.h>
  20. #include <linux/fs.h>
  21. #include <linux/list.h>
  22. #include <linux/string.h>
  23. #include <linux/slab.h>
  24. #include <linux/mutex.h>
  25. #include <linux/seq_file.h>
  26. #include "gcov.h"
  27. /**
  28. * struct gcov_node - represents a debugfs entry
  29. * @list: list head for child node list
  30. * @children: child nodes
  31. * @all: list head for list of all nodes
  32. * @parent: parent node
  33. * @loaded_info: array of pointers to profiling data sets for loaded object
  34. * files.
  35. * @num_loaded: number of profiling data sets for loaded object files.
  36. * @unloaded_info: accumulated copy of profiling data sets for unloaded
  37. * object files. Used only when gcov_persist=1.
  38. * @dentry: main debugfs entry, either a directory or data file
  39. * @links: associated symbolic links
  40. * @name: data file basename
  41. *
  42. * struct gcov_node represents an entity within the gcov/ subdirectory
  43. * of debugfs. There are directory and data file nodes. The latter represent
  44. * the actual synthesized data file plus any associated symbolic links which
  45. * are needed by the gcov tool to work correctly.
  46. */
  47. struct gcov_node {
  48. struct list_head list;
  49. struct list_head children;
  50. struct list_head all;
  51. struct gcov_node *parent;
  52. struct gcov_info **loaded_info;
  53. struct gcov_info *unloaded_info;
  54. struct dentry *dentry;
  55. struct dentry **links;
  56. int num_loaded;
  57. char name[0];
  58. };
  59. static const char objtree[] = OBJTREE;
  60. static const char srctree[] = SRCTREE;
  61. static struct gcov_node root_node;
  62. static struct dentry *reset_dentry;
  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. if (gcov_iter_next(iter))
  100. return NULL;
  101. (*pos)++;
  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. if (!node->links[i])
  343. goto out_err;
  344. kfree(target);
  345. }
  346. return;
  347. out_err:
  348. kfree(target);
  349. while (i-- > 0)
  350. debugfs_remove(node->links[i]);
  351. kfree(node->links);
  352. node->links = NULL;
  353. }
  354. static const struct file_operations gcov_data_fops = {
  355. .open = gcov_seq_open,
  356. .release = gcov_seq_release,
  357. .read = seq_read,
  358. .llseek = seq_lseek,
  359. .write = gcov_seq_write,
  360. };
  361. /* Basic initialization of a new node. */
  362. static void init_node(struct gcov_node *node, struct gcov_info *info,
  363. const char *name, struct gcov_node *parent)
  364. {
  365. INIT_LIST_HEAD(&node->list);
  366. INIT_LIST_HEAD(&node->children);
  367. INIT_LIST_HEAD(&node->all);
  368. if (node->loaded_info) {
  369. node->loaded_info[0] = info;
  370. node->num_loaded = 1;
  371. }
  372. node->parent = parent;
  373. if (name)
  374. strcpy(node->name, name);
  375. }
  376. /*
  377. * Create a new node and associated debugfs entry. Needs to be called with
  378. * node_lock held.
  379. */
  380. static struct gcov_node *new_node(struct gcov_node *parent,
  381. struct gcov_info *info, const char *name)
  382. {
  383. struct gcov_node *node;
  384. node = kzalloc(sizeof(struct gcov_node) + strlen(name) + 1, GFP_KERNEL);
  385. if (!node)
  386. goto err_nomem;
  387. if (info) {
  388. node->loaded_info = kcalloc(1, sizeof(struct gcov_info *),
  389. GFP_KERNEL);
  390. if (!node->loaded_info)
  391. goto err_nomem;
  392. }
  393. init_node(node, info, name, parent);
  394. /* Differentiate between gcov data file nodes and directory nodes. */
  395. if (info) {
  396. node->dentry = debugfs_create_file(deskew(node->name), 0600,
  397. parent->dentry, node, &gcov_data_fops);
  398. } else
  399. node->dentry = debugfs_create_dir(node->name, parent->dentry);
  400. if (!node->dentry) {
  401. pr_warn("could not create file\n");
  402. kfree(node);
  403. return NULL;
  404. }
  405. if (info)
  406. add_links(node, parent->dentry);
  407. list_add(&node->list, &parent->children);
  408. list_add(&node->all, &all_head);
  409. return node;
  410. err_nomem:
  411. kfree(node);
  412. pr_warn("out of memory\n");
  413. return NULL;
  414. }
  415. /* Remove symbolic links associated with node. */
  416. static void remove_links(struct gcov_node *node)
  417. {
  418. int i;
  419. if (!node->links)
  420. return;
  421. for (i = 0; gcov_link[i].ext; i++)
  422. debugfs_remove(node->links[i]);
  423. kfree(node->links);
  424. node->links = NULL;
  425. }
  426. /*
  427. * Remove node from all lists and debugfs and release associated resources.
  428. * Needs to be called with node_lock held.
  429. */
  430. static void release_node(struct gcov_node *node)
  431. {
  432. list_del(&node->list);
  433. list_del(&node->all);
  434. debugfs_remove(node->dentry);
  435. remove_links(node);
  436. kfree(node->loaded_info);
  437. if (node->unloaded_info)
  438. gcov_info_free(node->unloaded_info);
  439. kfree(node);
  440. }
  441. /* Release node and empty parents. Needs to be called with node_lock held. */
  442. static void remove_node(struct gcov_node *node)
  443. {
  444. struct gcov_node *parent;
  445. while ((node != &root_node) && list_empty(&node->children)) {
  446. parent = node->parent;
  447. release_node(node);
  448. node = parent;
  449. }
  450. }
  451. /*
  452. * Find child node with given basename. Needs to be called with node_lock
  453. * held.
  454. */
  455. static struct gcov_node *get_child_by_name(struct gcov_node *parent,
  456. const char *name)
  457. {
  458. struct gcov_node *node;
  459. list_for_each_entry(node, &parent->children, list) {
  460. if (strcmp(node->name, name) == 0)
  461. return node;
  462. }
  463. return NULL;
  464. }
  465. /*
  466. * write() implementation for reset file. Reset all profiling data to zero
  467. * and remove nodes for which all associated object files are unloaded.
  468. */
  469. static ssize_t reset_write(struct file *file, const char __user *addr,
  470. size_t len, loff_t *pos)
  471. {
  472. struct gcov_node *node;
  473. mutex_lock(&node_lock);
  474. restart:
  475. list_for_each_entry(node, &all_head, all) {
  476. if (node->num_loaded > 0)
  477. reset_node(node);
  478. else if (list_empty(&node->children)) {
  479. remove_node(node);
  480. /* Several nodes may have gone - restart loop. */
  481. goto restart;
  482. }
  483. }
  484. mutex_unlock(&node_lock);
  485. return len;
  486. }
  487. /* read() implementation for reset file. Unused. */
  488. static ssize_t reset_read(struct file *file, char __user *addr, size_t len,
  489. loff_t *pos)
  490. {
  491. /* Allow read operation so that a recursive copy won't fail. */
  492. return 0;
  493. }
  494. static const struct file_operations gcov_reset_fops = {
  495. .write = reset_write,
  496. .read = reset_read,
  497. .llseek = noop_llseek,
  498. };
  499. /*
  500. * Create a node for a given profiling data set and add it to all lists and
  501. * debugfs. Needs to be called with node_lock held.
  502. */
  503. static void add_node(struct gcov_info *info)
  504. {
  505. char *filename;
  506. char *curr;
  507. char *next;
  508. struct gcov_node *parent;
  509. struct gcov_node *node;
  510. filename = kstrdup(gcov_info_filename(info), GFP_KERNEL);
  511. if (!filename)
  512. return;
  513. parent = &root_node;
  514. /* Create directory nodes along the path. */
  515. for (curr = filename; (next = strchr(curr, '/')); curr = next + 1) {
  516. if (curr == next)
  517. continue;
  518. *next = 0;
  519. if (strcmp(curr, ".") == 0)
  520. continue;
  521. if (strcmp(curr, "..") == 0) {
  522. if (!parent->parent)
  523. goto err_remove;
  524. parent = parent->parent;
  525. continue;
  526. }
  527. node = get_child_by_name(parent, curr);
  528. if (!node) {
  529. node = new_node(parent, NULL, curr);
  530. if (!node)
  531. goto err_remove;
  532. }
  533. parent = node;
  534. }
  535. /* Create file node. */
  536. node = new_node(parent, info, curr);
  537. if (!node)
  538. goto err_remove;
  539. out:
  540. kfree(filename);
  541. return;
  542. err_remove:
  543. remove_node(parent);
  544. goto out;
  545. }
  546. /*
  547. * Associate a profiling data set with an existing node. Needs to be called
  548. * with node_lock held.
  549. */
  550. static void add_info(struct gcov_node *node, struct gcov_info *info)
  551. {
  552. struct gcov_info **loaded_info;
  553. int num = node->num_loaded;
  554. /*
  555. * Prepare new array. This is done first to simplify cleanup in
  556. * case the new data set is incompatible, the node only contains
  557. * unloaded data sets and there's not enough memory for the array.
  558. */
  559. loaded_info = kcalloc(num + 1, sizeof(struct gcov_info *), GFP_KERNEL);
  560. if (!loaded_info) {
  561. pr_warn("could not add '%s' (out of memory)\n",
  562. gcov_info_filename(info));
  563. return;
  564. }
  565. memcpy(loaded_info, node->loaded_info,
  566. num * sizeof(struct gcov_info *));
  567. loaded_info[num] = info;
  568. /* Check if the new data set is compatible. */
  569. if (num == 0) {
  570. /*
  571. * A module was unloaded, modified and reloaded. The new
  572. * data set replaces the copy of the last one.
  573. */
  574. if (!gcov_info_is_compatible(node->unloaded_info, info)) {
  575. pr_warn("discarding saved data for %s "
  576. "(incompatible version)\n",
  577. gcov_info_filename(info));
  578. gcov_info_free(node->unloaded_info);
  579. node->unloaded_info = NULL;
  580. }
  581. } else {
  582. /*
  583. * Two different versions of the same object file are loaded.
  584. * The initial one takes precedence.
  585. */
  586. if (!gcov_info_is_compatible(node->loaded_info[0], info)) {
  587. pr_warn("could not add '%s' (incompatible "
  588. "version)\n", gcov_info_filename(info));
  589. kfree(loaded_info);
  590. return;
  591. }
  592. }
  593. /* Overwrite previous array. */
  594. kfree(node->loaded_info);
  595. node->loaded_info = loaded_info;
  596. node->num_loaded = num + 1;
  597. }
  598. /*
  599. * Return the index of a profiling data set associated with a node.
  600. */
  601. static int get_info_index(struct gcov_node *node, struct gcov_info *info)
  602. {
  603. int i;
  604. for (i = 0; i < node->num_loaded; i++) {
  605. if (node->loaded_info[i] == info)
  606. return i;
  607. }
  608. return -ENOENT;
  609. }
  610. /*
  611. * Save the data of a profiling data set which is being unloaded.
  612. */
  613. static void save_info(struct gcov_node *node, struct gcov_info *info)
  614. {
  615. if (node->unloaded_info)
  616. gcov_info_add(node->unloaded_info, info);
  617. else {
  618. node->unloaded_info = gcov_info_dup(info);
  619. if (!node->unloaded_info) {
  620. pr_warn("could not save data for '%s' "
  621. "(out of memory)\n",
  622. gcov_info_filename(info));
  623. }
  624. }
  625. }
  626. /*
  627. * Disassociate a profiling data set from a node. Needs to be called with
  628. * node_lock held.
  629. */
  630. static void remove_info(struct gcov_node *node, struct gcov_info *info)
  631. {
  632. int i;
  633. i = get_info_index(node, info);
  634. if (i < 0) {
  635. pr_warn("could not remove '%s' (not found)\n",
  636. gcov_info_filename(info));
  637. return;
  638. }
  639. if (gcov_persist)
  640. save_info(node, info);
  641. /* Shrink array. */
  642. node->loaded_info[i] = node->loaded_info[node->num_loaded - 1];
  643. node->num_loaded--;
  644. if (node->num_loaded > 0)
  645. return;
  646. /* Last loaded data set was removed. */
  647. kfree(node->loaded_info);
  648. node->loaded_info = NULL;
  649. node->num_loaded = 0;
  650. if (!node->unloaded_info)
  651. remove_node(node);
  652. }
  653. /*
  654. * Callback to create/remove profiling files when code compiled with
  655. * -fprofile-arcs is loaded/unloaded.
  656. */
  657. void gcov_event(enum gcov_action action, struct gcov_info *info)
  658. {
  659. struct gcov_node *node;
  660. mutex_lock(&node_lock);
  661. node = get_node_by_name(gcov_info_filename(info));
  662. switch (action) {
  663. case GCOV_ADD:
  664. if (node)
  665. add_info(node, info);
  666. else
  667. add_node(info);
  668. break;
  669. case GCOV_REMOVE:
  670. if (node)
  671. remove_info(node, info);
  672. else {
  673. pr_warn("could not remove '%s' (not found)\n",
  674. gcov_info_filename(info));
  675. }
  676. break;
  677. }
  678. mutex_unlock(&node_lock);
  679. }
  680. /* Create debugfs entries. */
  681. static __init int gcov_fs_init(void)
  682. {
  683. int rc = -EIO;
  684. init_node(&root_node, NULL, NULL, NULL);
  685. /*
  686. * /sys/kernel/debug/gcov will be parent for the reset control file
  687. * and all profiling files.
  688. */
  689. root_node.dentry = debugfs_create_dir("gcov", NULL);
  690. if (!root_node.dentry)
  691. goto err_remove;
  692. /*
  693. * Create reset file which resets all profiling counts when written
  694. * to.
  695. */
  696. reset_dentry = debugfs_create_file("reset", 0600, root_node.dentry,
  697. NULL, &gcov_reset_fops);
  698. if (!reset_dentry)
  699. goto err_remove;
  700. /* Replay previous events to get our fs hierarchy up-to-date. */
  701. gcov_enable_events();
  702. return 0;
  703. err_remove:
  704. pr_err("init failed\n");
  705. debugfs_remove(root_node.dentry);
  706. return rc;
  707. }
  708. device_initcall(gcov_fs_init);