fs.c 16 KB

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