xref: /openbmc/linux/kernel/gcov/clang.c (revision 8b0adbe3e38dbe5aae9edf6f5159ffdca7cfbdf1)
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 	u8 use_extra_checksum;
74 	u32 cfg_checksum;
75 
76 	u32 num_counters;
77 	u64 *counters;
78 #if CONFIG_CLANG_VERSION < 110000
79 	const char *function_name;
80 #endif
81 };
82 
83 static struct gcov_info *current_info;
84 
85 static LIST_HEAD(clang_gcov_list);
86 
87 void llvm_gcov_init(llvm_gcov_callback writeout, llvm_gcov_callback flush)
88 {
89 	struct gcov_info *info = kzalloc(sizeof(*info), GFP_KERNEL);
90 
91 	if (!info)
92 		return;
93 
94 	INIT_LIST_HEAD(&info->head);
95 	INIT_LIST_HEAD(&info->functions);
96 
97 	mutex_lock(&gcov_lock);
98 
99 	list_add_tail(&info->head, &clang_gcov_list);
100 	current_info = info;
101 	writeout();
102 	current_info = NULL;
103 	if (gcov_events_enabled)
104 		gcov_event(GCOV_ADD, info);
105 
106 	mutex_unlock(&gcov_lock);
107 }
108 EXPORT_SYMBOL(llvm_gcov_init);
109 
110 #if CONFIG_CLANG_VERSION < 110000
111 void llvm_gcda_start_file(const char *orig_filename, const char version[4],
112 		u32 checksum)
113 {
114 	current_info->filename = orig_filename;
115 	memcpy(&current_info->version, version, sizeof(current_info->version));
116 	current_info->checksum = checksum;
117 }
118 EXPORT_SYMBOL(llvm_gcda_start_file);
119 #else
120 void llvm_gcda_start_file(const char *orig_filename, u32 version, u32 checksum)
121 {
122 	current_info->filename = orig_filename;
123 	current_info->version = version;
124 	current_info->checksum = checksum;
125 }
126 EXPORT_SYMBOL(llvm_gcda_start_file);
127 #endif
128 
129 #if CONFIG_CLANG_VERSION < 110000
130 void llvm_gcda_emit_function(u32 ident, const char *function_name,
131 		u32 func_checksum, u8 use_extra_checksum, u32 cfg_checksum)
132 {
133 	struct gcov_fn_info *info = kzalloc(sizeof(*info), GFP_KERNEL);
134 
135 	if (!info)
136 		return;
137 
138 	INIT_LIST_HEAD(&info->head);
139 	info->ident = ident;
140 	info->checksum = func_checksum;
141 	info->use_extra_checksum = use_extra_checksum;
142 	info->cfg_checksum = cfg_checksum;
143 	if (function_name)
144 		info->function_name = kstrdup(function_name, GFP_KERNEL);
145 
146 	list_add_tail(&info->head, &current_info->functions);
147 }
148 EXPORT_SYMBOL(llvm_gcda_emit_function);
149 #else
150 void llvm_gcda_emit_function(u32 ident, u32 func_checksum,
151 		u8 use_extra_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->use_extra_checksum = use_extra_checksum;
162 	info->cfg_checksum = cfg_checksum;
163 	list_add_tail(&info->head, &current_info->functions);
164 }
165 EXPORT_SYMBOL(llvm_gcda_emit_function);
166 #endif
167 
168 void llvm_gcda_emit_arcs(u32 num_counters, u64 *counters)
169 {
170 	struct gcov_fn_info *info = list_last_entry(&current_info->functions,
171 			struct gcov_fn_info, head);
172 
173 	info->num_counters = num_counters;
174 	info->counters = counters;
175 }
176 EXPORT_SYMBOL(llvm_gcda_emit_arcs);
177 
178 void llvm_gcda_summary_info(void)
179 {
180 }
181 EXPORT_SYMBOL(llvm_gcda_summary_info);
182 
183 void llvm_gcda_end_file(void)
184 {
185 }
186 EXPORT_SYMBOL(llvm_gcda_end_file);
187 
188 /**
189  * gcov_info_filename - return info filename
190  * @info: profiling data set
191  */
192 const char *gcov_info_filename(struct gcov_info *info)
193 {
194 	return info->filename;
195 }
196 
197 /**
198  * gcov_info_version - return info version
199  * @info: profiling data set
200  */
201 unsigned int gcov_info_version(struct gcov_info *info)
202 {
203 	return info->version;
204 }
205 
206 /**
207  * gcov_info_next - return next profiling data set
208  * @info: profiling data set
209  *
210  * Returns next gcov_info following @info or first gcov_info in the chain if
211  * @info is %NULL.
212  */
213 struct gcov_info *gcov_info_next(struct gcov_info *info)
214 {
215 	if (!info)
216 		return list_first_entry_or_null(&clang_gcov_list,
217 				struct gcov_info, head);
218 	if (list_is_last(&info->head, &clang_gcov_list))
219 		return NULL;
220 	return list_next_entry(info, head);
221 }
222 
223 /**
224  * gcov_info_link - link/add profiling data set to the list
225  * @info: profiling data set
226  */
227 void gcov_info_link(struct gcov_info *info)
228 {
229 	list_add_tail(&info->head, &clang_gcov_list);
230 }
231 
232 /**
233  * gcov_info_unlink - unlink/remove profiling data set from the list
234  * @prev: previous profiling data set
235  * @info: profiling data set
236  */
237 void gcov_info_unlink(struct gcov_info *prev, struct gcov_info *info)
238 {
239 	/* Generic code unlinks while iterating. */
240 	__list_del_entry(&info->head);
241 }
242 
243 /**
244  * gcov_info_within_module - check if a profiling data set belongs to a module
245  * @info: profiling data set
246  * @mod: module
247  *
248  * Returns true if profiling data belongs module, false otherwise.
249  */
250 bool gcov_info_within_module(struct gcov_info *info, struct module *mod)
251 {
252 	return within_module((unsigned long)info->filename, mod);
253 }
254 
255 /* Symbolic links to be created for each profiling data file. */
256 const struct gcov_link gcov_link[] = {
257 	{ OBJ_TREE, "gcno" },	/* Link to .gcno file in $(objtree). */
258 	{ 0, NULL},
259 };
260 
261 /**
262  * gcov_info_reset - reset profiling data to zero
263  * @info: profiling data set
264  */
265 void gcov_info_reset(struct gcov_info *info)
266 {
267 	struct gcov_fn_info *fn;
268 
269 	list_for_each_entry(fn, &info->functions, head)
270 		memset(fn->counters, 0,
271 				sizeof(fn->counters[0]) * fn->num_counters);
272 }
273 
274 /**
275  * gcov_info_is_compatible - check if profiling data can be added
276  * @info1: first profiling data set
277  * @info2: second profiling data set
278  *
279  * Returns non-zero if profiling data can be added, zero otherwise.
280  */
281 int gcov_info_is_compatible(struct gcov_info *info1, struct gcov_info *info2)
282 {
283 	struct gcov_fn_info *fn_ptr1 = list_first_entry_or_null(
284 			&info1->functions, struct gcov_fn_info, head);
285 	struct gcov_fn_info *fn_ptr2 = list_first_entry_or_null(
286 			&info2->functions, struct gcov_fn_info, head);
287 
288 	if (info1->checksum != info2->checksum)
289 		return false;
290 	if (!fn_ptr1)
291 		return fn_ptr1 == fn_ptr2;
292 	while (!list_is_last(&fn_ptr1->head, &info1->functions) &&
293 		!list_is_last(&fn_ptr2->head, &info2->functions)) {
294 		if (fn_ptr1->checksum != fn_ptr2->checksum)
295 			return false;
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 		fn_ptr1 = list_next_entry(fn_ptr1, head);
302 		fn_ptr2 = list_next_entry(fn_ptr2, head);
303 	}
304 	return list_is_last(&fn_ptr1->head, &info1->functions) &&
305 		list_is_last(&fn_ptr2->head, &info2->functions);
306 }
307 
308 /**
309  * gcov_info_add - add up profiling data
310  * @dest: profiling data set to which data is added
311  * @source: profiling data set which is added
312  *
313  * Adds profiling counts of @source to @dest.
314  */
315 void gcov_info_add(struct gcov_info *dst, struct gcov_info *src)
316 {
317 	struct gcov_fn_info *dfn_ptr;
318 	struct gcov_fn_info *sfn_ptr = list_first_entry_or_null(&src->functions,
319 			struct gcov_fn_info, head);
320 
321 	list_for_each_entry(dfn_ptr, &dst->functions, head) {
322 		u32 i;
323 
324 		for (i = 0; i < sfn_ptr->num_counters; i++)
325 			dfn_ptr->counters[i] += sfn_ptr->counters[i];
326 	}
327 }
328 
329 #if CONFIG_CLANG_VERSION < 110000
330 static struct gcov_fn_info *gcov_fn_info_dup(struct gcov_fn_info *fn)
331 {
332 	size_t cv_size; /* counter values size */
333 	struct gcov_fn_info *fn_dup = kmemdup(fn, sizeof(*fn),
334 			GFP_KERNEL);
335 	if (!fn_dup)
336 		return NULL;
337 	INIT_LIST_HEAD(&fn_dup->head);
338 
339 	fn_dup->function_name = kstrdup(fn->function_name, GFP_KERNEL);
340 	if (!fn_dup->function_name)
341 		goto err_name;
342 
343 	cv_size = fn->num_counters * sizeof(fn->counters[0]);
344 	fn_dup->counters = vmalloc(cv_size);
345 	if (!fn_dup->counters)
346 		goto err_counters;
347 	memcpy(fn_dup->counters, fn->counters, cv_size);
348 
349 	return fn_dup;
350 
351 err_counters:
352 	kfree(fn_dup->function_name);
353 err_name:
354 	kfree(fn_dup);
355 	return NULL;
356 }
357 #else
358 static struct gcov_fn_info *gcov_fn_info_dup(struct gcov_fn_info *fn)
359 {
360 	size_t cv_size; /* counter values size */
361 	struct gcov_fn_info *fn_dup = kmemdup(fn, sizeof(*fn),
362 			GFP_KERNEL);
363 	if (!fn_dup)
364 		return NULL;
365 	INIT_LIST_HEAD(&fn_dup->head);
366 
367 	cv_size = fn->num_counters * sizeof(fn->counters[0]);
368 	fn_dup->counters = vmalloc(cv_size);
369 	if (!fn_dup->counters) {
370 		kfree(fn_dup);
371 		return NULL;
372 	}
373 
374 	memcpy(fn_dup->counters, fn->counters, cv_size);
375 
376 	return fn_dup;
377 }
378 #endif
379 
380 /**
381  * gcov_info_dup - duplicate profiling data set
382  * @info: profiling data set to duplicate
383  *
384  * Return newly allocated duplicate on success, %NULL on error.
385  */
386 struct gcov_info *gcov_info_dup(struct gcov_info *info)
387 {
388 	struct gcov_info *dup;
389 	struct gcov_fn_info *fn;
390 
391 	dup = kmemdup(info, sizeof(*dup), GFP_KERNEL);
392 	if (!dup)
393 		return NULL;
394 	INIT_LIST_HEAD(&dup->head);
395 	INIT_LIST_HEAD(&dup->functions);
396 	dup->filename = kstrdup(info->filename, GFP_KERNEL);
397 	if (!dup->filename)
398 		goto err;
399 
400 	list_for_each_entry(fn, &info->functions, head) {
401 		struct gcov_fn_info *fn_dup = gcov_fn_info_dup(fn);
402 
403 		if (!fn_dup)
404 			goto err;
405 		list_add_tail(&fn_dup->head, &dup->functions);
406 	}
407 
408 	return dup;
409 
410 err:
411 	gcov_info_free(dup);
412 	return NULL;
413 }
414 
415 /**
416  * gcov_info_free - release memory for profiling data set duplicate
417  * @info: profiling data set duplicate to free
418  */
419 #if CONFIG_CLANG_VERSION < 110000
420 void gcov_info_free(struct gcov_info *info)
421 {
422 	struct gcov_fn_info *fn, *tmp;
423 
424 	list_for_each_entry_safe(fn, tmp, &info->functions, head) {
425 		kfree(fn->function_name);
426 		vfree(fn->counters);
427 		list_del(&fn->head);
428 		kfree(fn);
429 	}
430 	kfree(info->filename);
431 	kfree(info);
432 }
433 #else
434 void gcov_info_free(struct gcov_info *info)
435 {
436 	struct gcov_fn_info *fn, *tmp;
437 
438 	list_for_each_entry_safe(fn, tmp, &info->functions, head) {
439 		vfree(fn->counters);
440 		list_del(&fn->head);
441 		kfree(fn);
442 	}
443 	kfree(info->filename);
444 	kfree(info);
445 }
446 #endif
447 
448 #define ITER_STRIDE	PAGE_SIZE
449 
450 /**
451  * struct gcov_iterator - specifies current file position in logical records
452  * @info: associated profiling data
453  * @buffer: buffer containing file data
454  * @size: size of buffer
455  * @pos: current position in file
456  */
457 struct gcov_iterator {
458 	struct gcov_info *info;
459 	void *buffer;
460 	size_t size;
461 	loff_t pos;
462 };
463 
464 /**
465  * store_gcov_u32 - store 32 bit number in gcov format to buffer
466  * @buffer: target buffer or NULL
467  * @off: offset into the buffer
468  * @v: value to be stored
469  *
470  * Number format defined by gcc: numbers are recorded in the 32 bit
471  * unsigned binary form of the endianness of the machine generating the
472  * file. Returns the number of bytes stored. If @buffer is %NULL, doesn't
473  * store anything.
474  */
475 static size_t store_gcov_u32(void *buffer, size_t off, u32 v)
476 {
477 	u32 *data;
478 
479 	if (buffer) {
480 		data = buffer + off;
481 		*data = v;
482 	}
483 
484 	return sizeof(*data);
485 }
486 
487 /**
488  * store_gcov_u64 - store 64 bit number in gcov format to buffer
489  * @buffer: target buffer or NULL
490  * @off: offset into the buffer
491  * @v: value to be stored
492  *
493  * Number format defined by gcc: numbers are recorded in the 32 bit
494  * unsigned binary form of the endianness of the machine generating the
495  * file. 64 bit numbers are stored as two 32 bit numbers, the low part
496  * first. Returns the number of bytes stored. If @buffer is %NULL, doesn't store
497  * anything.
498  */
499 static size_t store_gcov_u64(void *buffer, size_t off, u64 v)
500 {
501 	u32 *data;
502 
503 	if (buffer) {
504 		data = buffer + off;
505 
506 		data[0] = (v & 0xffffffffUL);
507 		data[1] = (v >> 32);
508 	}
509 
510 	return sizeof(*data) * 2;
511 }
512 
513 /**
514  * convert_to_gcda - convert profiling data set to gcda file format
515  * @buffer: the buffer to store file data or %NULL if no data should be stored
516  * @info: profiling data set to be converted
517  *
518  * Returns the number of bytes that were/would have been stored into the buffer.
519  */
520 static size_t convert_to_gcda(char *buffer, struct gcov_info *info)
521 {
522 	struct gcov_fn_info *fi_ptr;
523 	size_t pos = 0;
524 
525 	/* File header. */
526 	pos += store_gcov_u32(buffer, pos, GCOV_DATA_MAGIC);
527 	pos += store_gcov_u32(buffer, pos, info->version);
528 	pos += store_gcov_u32(buffer, pos, info->checksum);
529 
530 	list_for_each_entry(fi_ptr, &info->functions, head) {
531 		u32 i;
532 		u32 len = 2;
533 
534 		if (fi_ptr->use_extra_checksum)
535 			len++;
536 
537 		pos += store_gcov_u32(buffer, pos, GCOV_TAG_FUNCTION);
538 		pos += store_gcov_u32(buffer, pos, len);
539 		pos += store_gcov_u32(buffer, pos, fi_ptr->ident);
540 		pos += store_gcov_u32(buffer, pos, fi_ptr->checksum);
541 		if (fi_ptr->use_extra_checksum)
542 			pos += store_gcov_u32(buffer, pos, fi_ptr->cfg_checksum);
543 
544 		pos += store_gcov_u32(buffer, pos, GCOV_TAG_COUNTER_BASE);
545 		pos += store_gcov_u32(buffer, pos, fi_ptr->num_counters * 2);
546 		for (i = 0; i < fi_ptr->num_counters; i++)
547 			pos += store_gcov_u64(buffer, pos, fi_ptr->counters[i]);
548 	}
549 
550 	return pos;
551 }
552 
553 /**
554  * gcov_iter_new - allocate and initialize profiling data iterator
555  * @info: profiling data set to be iterated
556  *
557  * Return file iterator on success, %NULL otherwise.
558  */
559 struct gcov_iterator *gcov_iter_new(struct gcov_info *info)
560 {
561 	struct gcov_iterator *iter;
562 
563 	iter = kzalloc(sizeof(struct gcov_iterator), GFP_KERNEL);
564 	if (!iter)
565 		goto err_free;
566 
567 	iter->info = info;
568 	/* Dry-run to get the actual buffer size. */
569 	iter->size = convert_to_gcda(NULL, info);
570 	iter->buffer = vmalloc(iter->size);
571 	if (!iter->buffer)
572 		goto err_free;
573 
574 	convert_to_gcda(iter->buffer, info);
575 
576 	return iter;
577 
578 err_free:
579 	kfree(iter);
580 	return NULL;
581 }
582 
583 
584 /**
585  * gcov_iter_get_info - return profiling data set for given file iterator
586  * @iter: file iterator
587  */
588 void gcov_iter_free(struct gcov_iterator *iter)
589 {
590 	vfree(iter->buffer);
591 	kfree(iter);
592 }
593 
594 /**
595  * gcov_iter_get_info - return profiling data set for given file iterator
596  * @iter: file iterator
597  */
598 struct gcov_info *gcov_iter_get_info(struct gcov_iterator *iter)
599 {
600 	return iter->info;
601 }
602 
603 /**
604  * gcov_iter_start - reset file iterator to starting position
605  * @iter: file iterator
606  */
607 void gcov_iter_start(struct gcov_iterator *iter)
608 {
609 	iter->pos = 0;
610 }
611 
612 /**
613  * gcov_iter_next - advance file iterator to next logical record
614  * @iter: file iterator
615  *
616  * Return zero if new position is valid, non-zero if iterator has reached end.
617  */
618 int gcov_iter_next(struct gcov_iterator *iter)
619 {
620 	if (iter->pos < iter->size)
621 		iter->pos += ITER_STRIDE;
622 
623 	if (iter->pos >= iter->size)
624 		return -EINVAL;
625 
626 	return 0;
627 }
628 
629 /**
630  * gcov_iter_write - write data for current pos to seq_file
631  * @iter: file iterator
632  * @seq: seq_file handle
633  *
634  * Return zero on success, non-zero otherwise.
635  */
636 int gcov_iter_write(struct gcov_iterator *iter, struct seq_file *seq)
637 {
638 	size_t len;
639 
640 	if (iter->pos >= iter->size)
641 		return -EINVAL;
642 
643 	len = ITER_STRIDE;
644 	if (iter->pos + len > iter->size)
645 		len = iter->size - iter->pos;
646 
647 	seq_write(seq, iter->buffer + iter->pos, len);
648 
649 	return 0;
650 }
651