xref: /openbmc/linux/kernel/gcov/clang.c (revision cd6d421e)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (C) 2019 Google, Inc.
4  * modified from kernel/gcov/gcc_4_7.c
5  *
6  * This software is licensed under the terms of the GNU General Public
7  * License version 2, as published by the Free Software Foundation, and
8  * may be copied, distributed, and modified under those terms.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  *
16  * LLVM uses profiling data that's deliberately similar to GCC, but has a
17  * very different way of exporting that data.  LLVM calls llvm_gcov_init() once
18  * per module, and provides a couple of callbacks that we can use to ask for
19  * more data.
20  *
21  * We care about the "writeout" callback, which in turn calls back into
22  * compiler-rt/this module to dump all the gathered coverage data to disk:
23  *
24  *    llvm_gcda_start_file()
25  *      llvm_gcda_emit_function()
26  *      llvm_gcda_emit_arcs()
27  *      llvm_gcda_emit_function()
28  *      llvm_gcda_emit_arcs()
29  *      [... repeats for each function ...]
30  *    llvm_gcda_summary_info()
31  *    llvm_gcda_end_file()
32  *
33  * This design is much more stateless and unstructured than gcc's, and is
34  * intended to run at process exit.  This forces us to keep some local state
35  * about which module we're dealing with at the moment.  On the other hand, it
36  * also means we don't depend as much on how LLVM represents profiling data
37  * internally.
38  *
39  * See LLVM's lib/Transforms/Instrumentation/GCOVProfiling.cpp for more
40  * details on how this works, particularly GCOVProfiler::emitProfileArcs(),
41  * GCOVProfiler::insertCounterWriteout(), and
42  * GCOVProfiler::insertFlush().
43  */
44 
45 #define pr_fmt(fmt)	"gcov: " fmt
46 
47 #include <linux/kernel.h>
48 #include <linux/list.h>
49 #include <linux/printk.h>
50 #include <linux/ratelimit.h>
51 #include <linux/seq_file.h>
52 #include <linux/slab.h>
53 #include <linux/vmalloc.h>
54 #include "gcov.h"
55 
56 typedef void (*llvm_gcov_callback)(void);
57 
58 struct gcov_info {
59 	struct list_head head;
60 
61 	const char *filename;
62 	unsigned int version;
63 	u32 checksum;
64 
65 	struct list_head functions;
66 };
67 
68 struct gcov_fn_info {
69 	struct list_head head;
70 
71 	u32 ident;
72 	u32 checksum;
73 #if CONFIG_CLANG_VERSION < 110000
74 	u8 use_extra_checksum;
75 #endif
76 	u32 cfg_checksum;
77 
78 	u32 num_counters;
79 	u64 *counters;
80 #if CONFIG_CLANG_VERSION < 110000
81 	const char *function_name;
82 #endif
83 };
84 
85 static struct gcov_info *current_info;
86 
87 static LIST_HEAD(clang_gcov_list);
88 
89 void llvm_gcov_init(llvm_gcov_callback writeout, llvm_gcov_callback flush)
90 {
91 	struct gcov_info *info = kzalloc(sizeof(*info), GFP_KERNEL);
92 
93 	if (!info)
94 		return;
95 
96 	INIT_LIST_HEAD(&info->head);
97 	INIT_LIST_HEAD(&info->functions);
98 
99 	mutex_lock(&gcov_lock);
100 
101 	list_add_tail(&info->head, &clang_gcov_list);
102 	current_info = info;
103 	writeout();
104 	current_info = NULL;
105 	if (gcov_events_enabled)
106 		gcov_event(GCOV_ADD, info);
107 
108 	mutex_unlock(&gcov_lock);
109 }
110 EXPORT_SYMBOL(llvm_gcov_init);
111 
112 #if CONFIG_CLANG_VERSION < 110000
113 void llvm_gcda_start_file(const char *orig_filename, const char version[4],
114 		u32 checksum)
115 {
116 	current_info->filename = orig_filename;
117 	memcpy(&current_info->version, version, sizeof(current_info->version));
118 	current_info->checksum = checksum;
119 }
120 EXPORT_SYMBOL(llvm_gcda_start_file);
121 #else
122 void llvm_gcda_start_file(const char *orig_filename, u32 version, u32 checksum)
123 {
124 	current_info->filename = orig_filename;
125 	current_info->version = version;
126 	current_info->checksum = checksum;
127 }
128 EXPORT_SYMBOL(llvm_gcda_start_file);
129 #endif
130 
131 #if CONFIG_CLANG_VERSION < 110000
132 void llvm_gcda_emit_function(u32 ident, const char *function_name,
133 		u32 func_checksum, u8 use_extra_checksum, u32 cfg_checksum)
134 {
135 	struct gcov_fn_info *info = kzalloc(sizeof(*info), GFP_KERNEL);
136 
137 	if (!info)
138 		return;
139 
140 	INIT_LIST_HEAD(&info->head);
141 	info->ident = ident;
142 	info->checksum = func_checksum;
143 	info->use_extra_checksum = use_extra_checksum;
144 	info->cfg_checksum = cfg_checksum;
145 	if (function_name)
146 		info->function_name = kstrdup(function_name, GFP_KERNEL);
147 
148 	list_add_tail(&info->head, &current_info->functions);
149 }
150 #else
151 void llvm_gcda_emit_function(u32 ident, u32 func_checksum, u32 cfg_checksum)
152 {
153 	struct gcov_fn_info *info = kzalloc(sizeof(*info), GFP_KERNEL);
154 
155 	if (!info)
156 		return;
157 
158 	INIT_LIST_HEAD(&info->head);
159 	info->ident = ident;
160 	info->checksum = func_checksum;
161 	info->cfg_checksum = cfg_checksum;
162 	list_add_tail(&info->head, &current_info->functions);
163 }
164 #endif
165 EXPORT_SYMBOL(llvm_gcda_emit_function);
166 
167 void llvm_gcda_emit_arcs(u32 num_counters, u64 *counters)
168 {
169 	struct gcov_fn_info *info = list_last_entry(&current_info->functions,
170 			struct gcov_fn_info, head);
171 
172 	info->num_counters = num_counters;
173 	info->counters = counters;
174 }
175 EXPORT_SYMBOL(llvm_gcda_emit_arcs);
176 
177 void llvm_gcda_summary_info(void)
178 {
179 }
180 EXPORT_SYMBOL(llvm_gcda_summary_info);
181 
182 void llvm_gcda_end_file(void)
183 {
184 }
185 EXPORT_SYMBOL(llvm_gcda_end_file);
186 
187 /**
188  * gcov_info_filename - return info filename
189  * @info: profiling data set
190  */
191 const char *gcov_info_filename(struct gcov_info *info)
192 {
193 	return info->filename;
194 }
195 
196 /**
197  * gcov_info_version - return info version
198  * @info: profiling data set
199  */
200 unsigned int gcov_info_version(struct gcov_info *info)
201 {
202 	return info->version;
203 }
204 
205 /**
206  * gcov_info_next - return next profiling data set
207  * @info: profiling data set
208  *
209  * Returns next gcov_info following @info or first gcov_info in the chain if
210  * @info is %NULL.
211  */
212 struct gcov_info *gcov_info_next(struct gcov_info *info)
213 {
214 	if (!info)
215 		return list_first_entry_or_null(&clang_gcov_list,
216 				struct gcov_info, head);
217 	if (list_is_last(&info->head, &clang_gcov_list))
218 		return NULL;
219 	return list_next_entry(info, head);
220 }
221 
222 /**
223  * gcov_info_link - link/add profiling data set to the list
224  * @info: profiling data set
225  */
226 void gcov_info_link(struct gcov_info *info)
227 {
228 	list_add_tail(&info->head, &clang_gcov_list);
229 }
230 
231 /**
232  * gcov_info_unlink - unlink/remove profiling data set from the list
233  * @prev: previous profiling data set
234  * @info: profiling data set
235  */
236 void gcov_info_unlink(struct gcov_info *prev, struct gcov_info *info)
237 {
238 	/* Generic code unlinks while iterating. */
239 	__list_del_entry(&info->head);
240 }
241 
242 /**
243  * gcov_info_within_module - check if a profiling data set belongs to a module
244  * @info: profiling data set
245  * @mod: module
246  *
247  * Returns true if profiling data belongs module, false otherwise.
248  */
249 bool gcov_info_within_module(struct gcov_info *info, struct module *mod)
250 {
251 	return within_module((unsigned long)info->filename, mod);
252 }
253 
254 /* Symbolic links to be created for each profiling data file. */
255 const struct gcov_link gcov_link[] = {
256 	{ OBJ_TREE, "gcno" },	/* Link to .gcno file in $(objtree). */
257 	{ 0, NULL},
258 };
259 
260 /**
261  * gcov_info_reset - reset profiling data to zero
262  * @info: profiling data set
263  */
264 void gcov_info_reset(struct gcov_info *info)
265 {
266 	struct gcov_fn_info *fn;
267 
268 	list_for_each_entry(fn, &info->functions, head)
269 		memset(fn->counters, 0,
270 				sizeof(fn->counters[0]) * fn->num_counters);
271 }
272 
273 /**
274  * gcov_info_is_compatible - check if profiling data can be added
275  * @info1: first profiling data set
276  * @info2: second profiling data set
277  *
278  * Returns non-zero if profiling data can be added, zero otherwise.
279  */
280 int gcov_info_is_compatible(struct gcov_info *info1, struct gcov_info *info2)
281 {
282 	struct gcov_fn_info *fn_ptr1 = list_first_entry_or_null(
283 			&info1->functions, struct gcov_fn_info, head);
284 	struct gcov_fn_info *fn_ptr2 = list_first_entry_or_null(
285 			&info2->functions, struct gcov_fn_info, head);
286 
287 	if (info1->checksum != info2->checksum)
288 		return false;
289 	if (!fn_ptr1)
290 		return fn_ptr1 == fn_ptr2;
291 	while (!list_is_last(&fn_ptr1->head, &info1->functions) &&
292 		!list_is_last(&fn_ptr2->head, &info2->functions)) {
293 		if (fn_ptr1->checksum != fn_ptr2->checksum)
294 			return false;
295 #if CONFIG_CLANG_VERSION < 110000
296 		if (fn_ptr1->use_extra_checksum != fn_ptr2->use_extra_checksum)
297 			return false;
298 		if (fn_ptr1->use_extra_checksum &&
299 			fn_ptr1->cfg_checksum != fn_ptr2->cfg_checksum)
300 			return false;
301 #else
302 		if (fn_ptr1->cfg_checksum != fn_ptr2->cfg_checksum)
303 			return false;
304 #endif
305 		fn_ptr1 = list_next_entry(fn_ptr1, head);
306 		fn_ptr2 = list_next_entry(fn_ptr2, head);
307 	}
308 	return list_is_last(&fn_ptr1->head, &info1->functions) &&
309 		list_is_last(&fn_ptr2->head, &info2->functions);
310 }
311 
312 /**
313  * gcov_info_add - add up profiling data
314  * @dest: profiling data set to which data is added
315  * @source: profiling data set which is added
316  *
317  * Adds profiling counts of @source to @dest.
318  */
319 void gcov_info_add(struct gcov_info *dst, struct gcov_info *src)
320 {
321 	struct gcov_fn_info *dfn_ptr;
322 	struct gcov_fn_info *sfn_ptr = list_first_entry_or_null(&src->functions,
323 			struct gcov_fn_info, head);
324 
325 	list_for_each_entry(dfn_ptr, &dst->functions, head) {
326 		u32 i;
327 
328 		for (i = 0; i < sfn_ptr->num_counters; i++)
329 			dfn_ptr->counters[i] += sfn_ptr->counters[i];
330 	}
331 }
332 
333 #if CONFIG_CLANG_VERSION < 110000
334 static struct gcov_fn_info *gcov_fn_info_dup(struct gcov_fn_info *fn)
335 {
336 	size_t cv_size; /* counter values size */
337 	struct gcov_fn_info *fn_dup = kmemdup(fn, sizeof(*fn),
338 			GFP_KERNEL);
339 	if (!fn_dup)
340 		return NULL;
341 	INIT_LIST_HEAD(&fn_dup->head);
342 
343 	fn_dup->function_name = kstrdup(fn->function_name, GFP_KERNEL);
344 	if (!fn_dup->function_name)
345 		goto err_name;
346 
347 	cv_size = fn->num_counters * sizeof(fn->counters[0]);
348 	fn_dup->counters = vmalloc(cv_size);
349 	if (!fn_dup->counters)
350 		goto err_counters;
351 	memcpy(fn_dup->counters, fn->counters, cv_size);
352 
353 	return fn_dup;
354 
355 err_counters:
356 	kfree(fn_dup->function_name);
357 err_name:
358 	kfree(fn_dup);
359 	return NULL;
360 }
361 #else
362 static struct gcov_fn_info *gcov_fn_info_dup(struct gcov_fn_info *fn)
363 {
364 	size_t cv_size; /* counter values size */
365 	struct gcov_fn_info *fn_dup = kmemdup(fn, sizeof(*fn),
366 			GFP_KERNEL);
367 	if (!fn_dup)
368 		return NULL;
369 	INIT_LIST_HEAD(&fn_dup->head);
370 
371 	cv_size = fn->num_counters * sizeof(fn->counters[0]);
372 	fn_dup->counters = vmalloc(cv_size);
373 	if (!fn_dup->counters) {
374 		kfree(fn_dup);
375 		return NULL;
376 	}
377 
378 	memcpy(fn_dup->counters, fn->counters, cv_size);
379 
380 	return fn_dup;
381 }
382 #endif
383 
384 /**
385  * gcov_info_dup - duplicate profiling data set
386  * @info: profiling data set to duplicate
387  *
388  * Return newly allocated duplicate on success, %NULL on error.
389  */
390 struct gcov_info *gcov_info_dup(struct gcov_info *info)
391 {
392 	struct gcov_info *dup;
393 	struct gcov_fn_info *fn;
394 
395 	dup = kmemdup(info, sizeof(*dup), GFP_KERNEL);
396 	if (!dup)
397 		return NULL;
398 	INIT_LIST_HEAD(&dup->head);
399 	INIT_LIST_HEAD(&dup->functions);
400 	dup->filename = kstrdup(info->filename, GFP_KERNEL);
401 	if (!dup->filename)
402 		goto err;
403 
404 	list_for_each_entry(fn, &info->functions, head) {
405 		struct gcov_fn_info *fn_dup = gcov_fn_info_dup(fn);
406 
407 		if (!fn_dup)
408 			goto err;
409 		list_add_tail(&fn_dup->head, &dup->functions);
410 	}
411 
412 	return dup;
413 
414 err:
415 	gcov_info_free(dup);
416 	return NULL;
417 }
418 
419 /**
420  * gcov_info_free - release memory for profiling data set duplicate
421  * @info: profiling data set duplicate to free
422  */
423 #if CONFIG_CLANG_VERSION < 110000
424 void gcov_info_free(struct gcov_info *info)
425 {
426 	struct gcov_fn_info *fn, *tmp;
427 
428 	list_for_each_entry_safe(fn, tmp, &info->functions, head) {
429 		kfree(fn->function_name);
430 		vfree(fn->counters);
431 		list_del(&fn->head);
432 		kfree(fn);
433 	}
434 	kfree(info->filename);
435 	kfree(info);
436 }
437 #else
438 void gcov_info_free(struct gcov_info *info)
439 {
440 	struct gcov_fn_info *fn, *tmp;
441 
442 	list_for_each_entry_safe(fn, tmp, &info->functions, head) {
443 		vfree(fn->counters);
444 		list_del(&fn->head);
445 		kfree(fn);
446 	}
447 	kfree(info->filename);
448 	kfree(info);
449 }
450 #endif
451 
452 #define ITER_STRIDE	PAGE_SIZE
453 
454 /**
455  * struct gcov_iterator - specifies current file position in logical records
456  * @info: associated profiling data
457  * @buffer: buffer containing file data
458  * @size: size of buffer
459  * @pos: current position in file
460  */
461 struct gcov_iterator {
462 	struct gcov_info *info;
463 	void *buffer;
464 	size_t size;
465 	loff_t pos;
466 };
467 
468 /**
469  * store_gcov_u32 - store 32 bit number in gcov format to buffer
470  * @buffer: target buffer or NULL
471  * @off: offset into the buffer
472  * @v: value to be stored
473  *
474  * Number format defined by gcc: numbers are recorded in the 32 bit
475  * unsigned binary form of the endianness of the machine generating the
476  * file. Returns the number of bytes stored. If @buffer is %NULL, doesn't
477  * store anything.
478  */
479 static size_t store_gcov_u32(void *buffer, size_t off, u32 v)
480 {
481 	u32 *data;
482 
483 	if (buffer) {
484 		data = buffer + off;
485 		*data = v;
486 	}
487 
488 	return sizeof(*data);
489 }
490 
491 /**
492  * store_gcov_u64 - store 64 bit number in gcov format to buffer
493  * @buffer: target buffer or NULL
494  * @off: offset into the buffer
495  * @v: value to be stored
496  *
497  * Number format defined by gcc: numbers are recorded in the 32 bit
498  * unsigned binary form of the endianness of the machine generating the
499  * file. 64 bit numbers are stored as two 32 bit numbers, the low part
500  * first. Returns the number of bytes stored. If @buffer is %NULL, doesn't store
501  * anything.
502  */
503 static size_t store_gcov_u64(void *buffer, size_t off, u64 v)
504 {
505 	u32 *data;
506 
507 	if (buffer) {
508 		data = buffer + off;
509 
510 		data[0] = (v & 0xffffffffUL);
511 		data[1] = (v >> 32);
512 	}
513 
514 	return sizeof(*data) * 2;
515 }
516 
517 /**
518  * convert_to_gcda - convert profiling data set to gcda file format
519  * @buffer: the buffer to store file data or %NULL if no data should be stored
520  * @info: profiling data set to be converted
521  *
522  * Returns the number of bytes that were/would have been stored into the buffer.
523  */
524 static size_t convert_to_gcda(char *buffer, struct gcov_info *info)
525 {
526 	struct gcov_fn_info *fi_ptr;
527 	size_t pos = 0;
528 
529 	/* File header. */
530 	pos += store_gcov_u32(buffer, pos, GCOV_DATA_MAGIC);
531 	pos += store_gcov_u32(buffer, pos, info->version);
532 	pos += store_gcov_u32(buffer, pos, info->checksum);
533 
534 	list_for_each_entry(fi_ptr, &info->functions, head) {
535 		u32 i;
536 
537 		pos += store_gcov_u32(buffer, pos, GCOV_TAG_FUNCTION);
538 #if CONFIG_CLANG_VERSION < 110000
539 		pos += store_gcov_u32(buffer, pos,
540 			fi_ptr->use_extra_checksum ? 3 : 2);
541 #else
542 		pos += store_gcov_u32(buffer, pos, 3);
543 #endif
544 		pos += store_gcov_u32(buffer, pos, fi_ptr->ident);
545 		pos += store_gcov_u32(buffer, pos, fi_ptr->checksum);
546 #if CONFIG_CLANG_VERSION < 110000
547 		if (fi_ptr->use_extra_checksum)
548 			pos += store_gcov_u32(buffer, pos, fi_ptr->cfg_checksum);
549 #else
550 		pos += store_gcov_u32(buffer, pos, fi_ptr->cfg_checksum);
551 #endif
552 
553 		pos += store_gcov_u32(buffer, pos, GCOV_TAG_COUNTER_BASE);
554 		pos += store_gcov_u32(buffer, pos, fi_ptr->num_counters * 2);
555 		for (i = 0; i < fi_ptr->num_counters; i++)
556 			pos += store_gcov_u64(buffer, pos, fi_ptr->counters[i]);
557 	}
558 
559 	return pos;
560 }
561 
562 /**
563  * gcov_iter_new - allocate and initialize profiling data iterator
564  * @info: profiling data set to be iterated
565  *
566  * Return file iterator on success, %NULL otherwise.
567  */
568 struct gcov_iterator *gcov_iter_new(struct gcov_info *info)
569 {
570 	struct gcov_iterator *iter;
571 
572 	iter = kzalloc(sizeof(struct gcov_iterator), GFP_KERNEL);
573 	if (!iter)
574 		goto err_free;
575 
576 	iter->info = info;
577 	/* Dry-run to get the actual buffer size. */
578 	iter->size = convert_to_gcda(NULL, info);
579 	iter->buffer = vmalloc(iter->size);
580 	if (!iter->buffer)
581 		goto err_free;
582 
583 	convert_to_gcda(iter->buffer, info);
584 
585 	return iter;
586 
587 err_free:
588 	kfree(iter);
589 	return NULL;
590 }
591 
592 
593 /**
594  * gcov_iter_get_info - return profiling data set for given file iterator
595  * @iter: file iterator
596  */
597 void gcov_iter_free(struct gcov_iterator *iter)
598 {
599 	vfree(iter->buffer);
600 	kfree(iter);
601 }
602 
603 /**
604  * gcov_iter_get_info - return profiling data set for given file iterator
605  * @iter: file iterator
606  */
607 struct gcov_info *gcov_iter_get_info(struct gcov_iterator *iter)
608 {
609 	return iter->info;
610 }
611 
612 /**
613  * gcov_iter_start - reset file iterator to starting position
614  * @iter: file iterator
615  */
616 void gcov_iter_start(struct gcov_iterator *iter)
617 {
618 	iter->pos = 0;
619 }
620 
621 /**
622  * gcov_iter_next - advance file iterator to next logical record
623  * @iter: file iterator
624  *
625  * Return zero if new position is valid, non-zero if iterator has reached end.
626  */
627 int gcov_iter_next(struct gcov_iterator *iter)
628 {
629 	if (iter->pos < iter->size)
630 		iter->pos += ITER_STRIDE;
631 
632 	if (iter->pos >= iter->size)
633 		return -EINVAL;
634 
635 	return 0;
636 }
637 
638 /**
639  * gcov_iter_write - write data for current pos to seq_file
640  * @iter: file iterator
641  * @seq: seq_file handle
642  *
643  * Return zero on success, non-zero otherwise.
644  */
645 int gcov_iter_write(struct gcov_iterator *iter, struct seq_file *seq)
646 {
647 	size_t len;
648 
649 	if (iter->pos >= iter->size)
650 		return -EINVAL;
651 
652 	len = ITER_STRIDE;
653 	if (iter->pos + len > iter->size)
654 		len = iter->size - iter->pos;
655 
656 	seq_write(seq, iter->buffer + iter->pos, len);
657 
658 	return 0;
659 }
660