Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

sched/core / kcov: avoid kcov_area during task switch

During a context switch, we first switch_mm() to the next task's mm,
then switch_to() that new task. This means that vmalloc'd regions which
had previously been faulted in can transiently disappear in the context
of the prev task.

Functions instrumented by KCOV may try to access a vmalloc'd kcov_area
during this window, and as the fault handling code is instrumented, this
results in a recursive fault.

We must avoid accessing any kcov_area during this window. We can do so
with a new flag in kcov_mode, set prior to switching the mm, and cleared
once the new task is live. Since task_struct::kcov_mode isn't always a
specific enum kcov_mode value, this is made an unsigned int.

The manipulation is hidden behind kcov_{prepare,finish}_switch() helpers,
which are empty for !CONFIG_KCOV kernels.

The code uses macros because I can't use static inline functions without a
circular include dependency between <linux/sched.h> and <linux/kcov.h>,
since the definition of task_struct uses things defined in <linux/kcov.h>

Link: http://lkml.kernel.org/r/20180504135535.53744-4-mark.rutland@arm.com
Signed-off-by: Mark Rutland <mark.rutland@arm.com>
Acked-by: Andrey Ryabinin <aryabinin@virtuozzo.com>
Cc: Dmitry Vyukov <dvyukov@google.com>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>

authored by

Mark Rutland and committed by
Linus Torvalds
0ed557aa dc55daff

+20 -2
+14
include/linux/kcov.h
··· 22 22 KCOV_MODE_TRACE_CMP = 3, 23 23 }; 24 24 25 + #define KCOV_IN_CTXSW (1 << 30) 26 + 25 27 void kcov_task_init(struct task_struct *t); 26 28 void kcov_task_exit(struct task_struct *t); 29 + 30 + #define kcov_prepare_switch(t) \ 31 + do { \ 32 + (t)->kcov_mode |= KCOV_IN_CTXSW; \ 33 + } while (0) 34 + 35 + #define kcov_finish_switch(t) \ 36 + do { \ 37 + (t)->kcov_mode &= ~KCOV_IN_CTXSW; \ 38 + } while (0) 27 39 28 40 #else 29 41 30 42 static inline void kcov_task_init(struct task_struct *t) {} 31 43 static inline void kcov_task_exit(struct task_struct *t) {} 44 + static inline void kcov_prepare_switch(struct task_struct *t) {} 45 + static inline void kcov_finish_switch(struct task_struct *t) {} 32 46 33 47 #endif /* CONFIG_KCOV */ 34 48 #endif /* _LINUX_KCOV_H */
+1 -1
include/linux/sched.h
··· 1130 1130 1131 1131 #ifdef CONFIG_KCOV 1132 1132 /* Coverage collection mode enabled for this task (0 if disabled): */ 1133 - enum kcov_mode kcov_mode; 1133 + unsigned int kcov_mode; 1134 1134 1135 1135 /* Size of the kcov_area: */ 1136 1136 unsigned int kcov_size;
+1 -1
kernel/kcov.c
··· 58 58 59 59 static bool check_kcov_mode(enum kcov_mode needed_mode, struct task_struct *t) 60 60 { 61 - enum kcov_mode mode; 61 + unsigned int mode; 62 62 63 63 /* 64 64 * We are interested in code coverage as a function of a syscall inputs,
+4
kernel/sched/core.c
··· 10 10 #include <linux/kthread.h> 11 11 #include <linux/nospec.h> 12 12 13 + #include <linux/kcov.h> 14 + 13 15 #include <asm/switch_to.h> 14 16 #include <asm/tlb.h> 15 17 ··· 2635 2633 prepare_task_switch(struct rq *rq, struct task_struct *prev, 2636 2634 struct task_struct *next) 2637 2635 { 2636 + kcov_prepare_switch(prev); 2638 2637 sched_info_switch(rq, prev, next); 2639 2638 perf_event_task_sched_out(prev, next); 2640 2639 rseq_preempt(prev); ··· 2705 2702 finish_task(prev); 2706 2703 finish_lock_switch(rq); 2707 2704 finish_arch_post_lock_switch(); 2705 + kcov_finish_switch(current); 2708 2706 2709 2707 fire_sched_in_preempt_notifiers(current); 2710 2708 /*