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
llvm_gcov_init(llvm_gcov_callback writeout,llvm_gcov_callback flush)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
llvm_gcda_start_file(const char * orig_filename,const char version[4],u32 checksum)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(¤t_info->version, version, sizeof(current_info->version));
118 current_info->checksum = checksum;
119 }
120 EXPORT_SYMBOL(llvm_gcda_start_file);
121 #else
llvm_gcda_start_file(const char * orig_filename,u32 version,u32 checksum)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
llvm_gcda_emit_function(u32 ident,const char * function_name,u32 func_checksum,u8 use_extra_checksum,u32 cfg_checksum)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, ¤t_info->functions);
149 }
150 #else
llvm_gcda_emit_function(u32 ident,u32 func_checksum,u32 cfg_checksum)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, ¤t_info->functions);
163 }
164 #endif
165 EXPORT_SYMBOL(llvm_gcda_emit_function);
166
llvm_gcda_emit_arcs(u32 num_counters,u64 * counters)167 void llvm_gcda_emit_arcs(u32 num_counters, u64 *counters)
168 {
169 struct gcov_fn_info *info = list_last_entry(¤t_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
llvm_gcda_summary_info(void)177 void llvm_gcda_summary_info(void)
178 {
179 }
180 EXPORT_SYMBOL(llvm_gcda_summary_info);
181
llvm_gcda_end_file(void)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 */
gcov_info_filename(struct gcov_info * info)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 */
gcov_info_version(struct gcov_info * info)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 */
gcov_info_next(struct gcov_info * info)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 */
gcov_info_link(struct gcov_info * info)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 */
gcov_info_unlink(struct gcov_info * prev,struct gcov_info * info)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 */
gcov_info_within_module(struct gcov_info * info,struct module * mod)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 */
gcov_info_reset(struct gcov_info * info)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 */
gcov_info_is_compatible(struct gcov_info * info1,struct gcov_info * info2)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 */
gcov_info_add(struct gcov_info * dst,struct gcov_info * src)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 sfn_ptr = list_next_entry(sfn_ptr, head);
332 }
333 }
334
335 #if CONFIG_CLANG_VERSION < 110000
gcov_fn_info_dup(struct gcov_fn_info * fn)336 static struct gcov_fn_info *gcov_fn_info_dup(struct gcov_fn_info *fn)
337 {
338 size_t cv_size; /* counter values size */
339 struct gcov_fn_info *fn_dup = kmemdup(fn, sizeof(*fn),
340 GFP_KERNEL);
341 if (!fn_dup)
342 return NULL;
343 INIT_LIST_HEAD(&fn_dup->head);
344
345 fn_dup->function_name = kstrdup(fn->function_name, GFP_KERNEL);
346 if (!fn_dup->function_name)
347 goto err_name;
348
349 cv_size = fn->num_counters * sizeof(fn->counters[0]);
350 fn_dup->counters = vmalloc(cv_size);
351 if (!fn_dup->counters)
352 goto err_counters;
353 memcpy(fn_dup->counters, fn->counters, cv_size);
354
355 return fn_dup;
356
357 err_counters:
358 kfree(fn_dup->function_name);
359 err_name:
360 kfree(fn_dup);
361 return NULL;
362 }
363 #else
gcov_fn_info_dup(struct gcov_fn_info * fn)364 static struct gcov_fn_info *gcov_fn_info_dup(struct gcov_fn_info *fn)
365 {
366 size_t cv_size; /* counter values size */
367 struct gcov_fn_info *fn_dup = kmemdup(fn, sizeof(*fn),
368 GFP_KERNEL);
369 if (!fn_dup)
370 return NULL;
371 INIT_LIST_HEAD(&fn_dup->head);
372
373 cv_size = fn->num_counters * sizeof(fn->counters[0]);
374 fn_dup->counters = vmalloc(cv_size);
375 if (!fn_dup->counters) {
376 kfree(fn_dup);
377 return NULL;
378 }
379
380 memcpy(fn_dup->counters, fn->counters, cv_size);
381
382 return fn_dup;
383 }
384 #endif
385
386 /**
387 * gcov_info_dup - duplicate profiling data set
388 * @info: profiling data set to duplicate
389 *
390 * Return newly allocated duplicate on success, %NULL on error.
391 */
gcov_info_dup(struct gcov_info * info)392 struct gcov_info *gcov_info_dup(struct gcov_info *info)
393 {
394 struct gcov_info *dup;
395 struct gcov_fn_info *fn;
396
397 dup = kmemdup(info, sizeof(*dup), GFP_KERNEL);
398 if (!dup)
399 return NULL;
400 INIT_LIST_HEAD(&dup->head);
401 INIT_LIST_HEAD(&dup->functions);
402 dup->filename = kstrdup(info->filename, GFP_KERNEL);
403 if (!dup->filename)
404 goto err;
405
406 list_for_each_entry(fn, &info->functions, head) {
407 struct gcov_fn_info *fn_dup = gcov_fn_info_dup(fn);
408
409 if (!fn_dup)
410 goto err;
411 list_add_tail(&fn_dup->head, &dup->functions);
412 }
413
414 return dup;
415
416 err:
417 gcov_info_free(dup);
418 return NULL;
419 }
420
421 /**
422 * gcov_info_free - release memory for profiling data set duplicate
423 * @info: profiling data set duplicate to free
424 */
425 #if CONFIG_CLANG_VERSION < 110000
gcov_info_free(struct gcov_info * info)426 void gcov_info_free(struct gcov_info *info)
427 {
428 struct gcov_fn_info *fn, *tmp;
429
430 list_for_each_entry_safe(fn, tmp, &info->functions, head) {
431 kfree(fn->function_name);
432 vfree(fn->counters);
433 list_del(&fn->head);
434 kfree(fn);
435 }
436 kfree(info->filename);
437 kfree(info);
438 }
439 #else
gcov_info_free(struct gcov_info * info)440 void gcov_info_free(struct gcov_info *info)
441 {
442 struct gcov_fn_info *fn, *tmp;
443
444 list_for_each_entry_safe(fn, tmp, &info->functions, head) {
445 vfree(fn->counters);
446 list_del(&fn->head);
447 kfree(fn);
448 }
449 kfree(info->filename);
450 kfree(info);
451 }
452 #endif
453
454 #define ITER_STRIDE PAGE_SIZE
455
456 /**
457 * struct gcov_iterator - specifies current file position in logical records
458 * @info: associated profiling data
459 * @buffer: buffer containing file data
460 * @size: size of buffer
461 * @pos: current position in file
462 */
463 struct gcov_iterator {
464 struct gcov_info *info;
465 void *buffer;
466 size_t size;
467 loff_t pos;
468 };
469
470 /**
471 * store_gcov_u32 - store 32 bit number in gcov format to buffer
472 * @buffer: target buffer or NULL
473 * @off: offset into the buffer
474 * @v: value to be stored
475 *
476 * Number format defined by gcc: numbers are recorded in the 32 bit
477 * unsigned binary form of the endianness of the machine generating the
478 * file. Returns the number of bytes stored. If @buffer is %NULL, doesn't
479 * store anything.
480 */
store_gcov_u32(void * buffer,size_t off,u32 v)481 static size_t store_gcov_u32(void *buffer, size_t off, u32 v)
482 {
483 u32 *data;
484
485 if (buffer) {
486 data = buffer + off;
487 *data = v;
488 }
489
490 return sizeof(*data);
491 }
492
493 /**
494 * store_gcov_u64 - store 64 bit number in gcov format to buffer
495 * @buffer: target buffer or NULL
496 * @off: offset into the buffer
497 * @v: value to be stored
498 *
499 * Number format defined by gcc: numbers are recorded in the 32 bit
500 * unsigned binary form of the endianness of the machine generating the
501 * file. 64 bit numbers are stored as two 32 bit numbers, the low part
502 * first. Returns the number of bytes stored. If @buffer is %NULL, doesn't store
503 * anything.
504 */
store_gcov_u64(void * buffer,size_t off,u64 v)505 static size_t store_gcov_u64(void *buffer, size_t off, u64 v)
506 {
507 u32 *data;
508
509 if (buffer) {
510 data = buffer + off;
511
512 data[0] = (v & 0xffffffffUL);
513 data[1] = (v >> 32);
514 }
515
516 return sizeof(*data) * 2;
517 }
518
519 /**
520 * convert_to_gcda - convert profiling data set to gcda file format
521 * @buffer: the buffer to store file data or %NULL if no data should be stored
522 * @info: profiling data set to be converted
523 *
524 * Returns the number of bytes that were/would have been stored into the buffer.
525 */
convert_to_gcda(char * buffer,struct gcov_info * info)526 static size_t convert_to_gcda(char *buffer, struct gcov_info *info)
527 {
528 struct gcov_fn_info *fi_ptr;
529 size_t pos = 0;
530
531 /* File header. */
532 pos += store_gcov_u32(buffer, pos, GCOV_DATA_MAGIC);
533 pos += store_gcov_u32(buffer, pos, info->version);
534 pos += store_gcov_u32(buffer, pos, info->checksum);
535
536 list_for_each_entry(fi_ptr, &info->functions, head) {
537 u32 i;
538
539 pos += store_gcov_u32(buffer, pos, GCOV_TAG_FUNCTION);
540 #if CONFIG_CLANG_VERSION < 110000
541 pos += store_gcov_u32(buffer, pos,
542 fi_ptr->use_extra_checksum ? 3 : 2);
543 #else
544 pos += store_gcov_u32(buffer, pos, 3);
545 #endif
546 pos += store_gcov_u32(buffer, pos, fi_ptr->ident);
547 pos += store_gcov_u32(buffer, pos, fi_ptr->checksum);
548 #if CONFIG_CLANG_VERSION < 110000
549 if (fi_ptr->use_extra_checksum)
550 pos += store_gcov_u32(buffer, pos, fi_ptr->cfg_checksum);
551 #else
552 pos += store_gcov_u32(buffer, pos, fi_ptr->cfg_checksum);
553 #endif
554
555 pos += store_gcov_u32(buffer, pos, GCOV_TAG_COUNTER_BASE);
556 pos += store_gcov_u32(buffer, pos, fi_ptr->num_counters * 2);
557 for (i = 0; i < fi_ptr->num_counters; i++)
558 pos += store_gcov_u64(buffer, pos, fi_ptr->counters[i]);
559 }
560
561 return pos;
562 }
563
564 /**
565 * gcov_iter_new - allocate and initialize profiling data iterator
566 * @info: profiling data set to be iterated
567 *
568 * Return file iterator on success, %NULL otherwise.
569 */
gcov_iter_new(struct gcov_info * info)570 struct gcov_iterator *gcov_iter_new(struct gcov_info *info)
571 {
572 struct gcov_iterator *iter;
573
574 iter = kzalloc(sizeof(struct gcov_iterator), GFP_KERNEL);
575 if (!iter)
576 goto err_free;
577
578 iter->info = info;
579 /* Dry-run to get the actual buffer size. */
580 iter->size = convert_to_gcda(NULL, info);
581 iter->buffer = vmalloc(iter->size);
582 if (!iter->buffer)
583 goto err_free;
584
585 convert_to_gcda(iter->buffer, info);
586
587 return iter;
588
589 err_free:
590 kfree(iter);
591 return NULL;
592 }
593
594
595 /**
596 * gcov_iter_get_info - return profiling data set for given file iterator
597 * @iter: file iterator
598 */
gcov_iter_free(struct gcov_iterator * iter)599 void gcov_iter_free(struct gcov_iterator *iter)
600 {
601 vfree(iter->buffer);
602 kfree(iter);
603 }
604
605 /**
606 * gcov_iter_get_info - return profiling data set for given file iterator
607 * @iter: file iterator
608 */
gcov_iter_get_info(struct gcov_iterator * iter)609 struct gcov_info *gcov_iter_get_info(struct gcov_iterator *iter)
610 {
611 return iter->info;
612 }
613
614 /**
615 * gcov_iter_start - reset file iterator to starting position
616 * @iter: file iterator
617 */
gcov_iter_start(struct gcov_iterator * iter)618 void gcov_iter_start(struct gcov_iterator *iter)
619 {
620 iter->pos = 0;
621 }
622
623 /**
624 * gcov_iter_next - advance file iterator to next logical record
625 * @iter: file iterator
626 *
627 * Return zero if new position is valid, non-zero if iterator has reached end.
628 */
gcov_iter_next(struct gcov_iterator * iter)629 int gcov_iter_next(struct gcov_iterator *iter)
630 {
631 if (iter->pos < iter->size)
632 iter->pos += ITER_STRIDE;
633
634 if (iter->pos >= iter->size)
635 return -EINVAL;
636
637 return 0;
638 }
639
640 /**
641 * gcov_iter_write - write data for current pos to seq_file
642 * @iter: file iterator
643 * @seq: seq_file handle
644 *
645 * Return zero on success, non-zero otherwise.
646 */
gcov_iter_write(struct gcov_iterator * iter,struct seq_file * seq)647 int gcov_iter_write(struct gcov_iterator *iter, struct seq_file *seq)
648 {
649 size_t len;
650
651 if (iter->pos >= iter->size)
652 return -EINVAL;
653
654 len = ITER_STRIDE;
655 if (iter->pos + len > iter->size)
656 len = iter->size - iter->pos;
657
658 seq_write(seq, iter->buffer + iter->pos, len);
659
660 return 0;
661 }
662