Skip to content

Commit 3021e69

Browse files
xairytorvalds
authored andcommitted
kcov: check kcov_softirq in kcov_remote_stop()
kcov_remote_stop() should check that the corresponding kcov_remote_start() actually found the specified remote handle and started collecting coverage. This is done by checking the per thread kcov_softirq flag. A particular failure scenario where this was observed involved a softirq with a remote coverage collection section coming between check_kcov_mode() and the access to t->kcov_area in __sanitizer_cov_trace_pc(). In that softirq kcov_remote_start() bailed out after kcov_remote_find() check, but the matching kcov_remote_stop() didn't check if kcov_remote_start() succeeded, and overwrote per thread kcov parameters with invalid (zero) values. Fixes: 5ff3b30 ("kcov: collect coverage from interrupts") Signed-off-by: Andrey Konovalov <[email protected]> Signed-off-by: Andrew Morton <[email protected]> Reviewed-by: Dmitry Vyukov <[email protected]> Cc: Alexander Potapenko <[email protected]> Cc: Marco Elver <[email protected]> Cc: Tetsuo Handa <[email protected]> Link: http://lkml.kernel.org/r/fcd1cd16eac1d2c01a66befd8ea4afc6f8d09833.1591576806.git.andreyknvl@google.com Signed-off-by: Linus Torvalds <[email protected]>
1 parent 4059066 commit 3021e69

File tree

1 file changed

+18
-8
lines changed

1 file changed

+18
-8
lines changed

kernel/kcov.c

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -427,7 +427,8 @@ void kcov_task_exit(struct task_struct *t)
427427
* WARN_ON(!kcov->remote && kcov->t != t);
428428
*
429429
* For KCOV_REMOTE_ENABLE devices, the exiting task is either:
430-
* 2. A remote task between kcov_remote_start() and kcov_remote_stop().
430+
*
431+
* 1. A remote task between kcov_remote_start() and kcov_remote_stop().
431432
* In this case we should print a warning right away, since a task
432433
* shouldn't be exiting when it's in a kcov coverage collection
433434
* section. Here t points to the task that is collecting remote
@@ -437,7 +438,7 @@ void kcov_task_exit(struct task_struct *t)
437438
* WARN_ON(kcov->remote && kcov->t != t);
438439
*
439440
* 2. The task that created kcov exiting without calling KCOV_DISABLE,
440-
* and then again we can make sure that t->kcov->t == t:
441+
* and then again we make sure that t->kcov->t == t:
441442
* WARN_ON(kcov->remote && kcov->t != t);
442443
*
443444
* By combining all three checks into one we get:
@@ -764,7 +765,7 @@ static const struct file_operations kcov_fops = {
764765
* Internally, kcov_remote_start() looks up the kcov device associated with the
765766
* provided handle, allocates an area for coverage collection, and saves the
766767
* pointers to kcov and area into the current task_struct to allow coverage to
767-
* be collected via __sanitizer_cov_trace_pc()
768+
* be collected via __sanitizer_cov_trace_pc().
768769
* In turns kcov_remote_stop() clears those pointers from task_struct to stop
769770
* collecting coverage and copies all collected coverage into the kcov area.
770771
*/
@@ -972,16 +973,25 @@ void kcov_remote_stop(void)
972973
local_irq_restore(flags);
973974
return;
974975
}
975-
kcov = t->kcov;
976-
area = t->kcov_area;
977-
size = t->kcov_size;
978-
sequence = t->kcov_sequence;
979-
976+
/*
977+
* When in softirq, check if the corresponding kcov_remote_start()
978+
* actually found the remote handle and started collecting coverage.
979+
*/
980+
if (in_serving_softirq() && !t->kcov_softirq) {
981+
local_irq_restore(flags);
982+
return;
983+
}
984+
/* Make sure that kcov_softirq is only set when in softirq. */
980985
if (WARN_ON(!in_serving_softirq() && t->kcov_softirq)) {
981986
local_irq_restore(flags);
982987
return;
983988
}
984989

990+
kcov = t->kcov;
991+
area = t->kcov_area;
992+
size = t->kcov_size;
993+
sequence = t->kcov_sequence;
994+
985995
kcov_stop(t);
986996
if (in_serving_softirq()) {
987997
t->kcov_softirq = 0;

0 commit comments

Comments
 (0)