fs.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793
  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. 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 = strrchr(target, '/');
  338. if (!basename)
  339. goto out_err;
  340. basename++;
  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. if (root_node.dentry)
  707. debugfs_remove(root_node.dentry);
  708. return rc;
  709. }
  710. device_initcall(gcov_fs_init);