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