Skip to content

Commit

Permalink
sched/core / kcov: avoid kcov_area during task switch
Browse files Browse the repository at this point in the history
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/[email protected]
Signed-off-by: Mark Rutland <[email protected]>
Acked-by: Andrey Ryabinin <[email protected]>
Cc: Dmitry Vyukov <[email protected]>
Cc: Ingo Molnar <[email protected]>
Cc: Peter Zijlstra <[email protected]>
Signed-off-by: Andrew Morton <[email protected]>
Signed-off-by: Linus Torvalds <[email protected]>
  • Loading branch information
Mark Rutland authored and torvalds committed Jun 14, 2018
1 parent dc55daf commit 0ed557a
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 2 deletions.
14 changes: 14 additions & 0 deletions include/linux/kcov.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,27 @@ enum kcov_mode {
KCOV_MODE_TRACE_CMP = 3,
};

#define KCOV_IN_CTXSW (1 << 30)

void kcov_task_init(struct task_struct *t);
void kcov_task_exit(struct task_struct *t);

#define kcov_prepare_switch(t) \
do { \
(t)->kcov_mode |= KCOV_IN_CTXSW; \
} while (0)

#define kcov_finish_switch(t) \
do { \
(t)->kcov_mode &= ~KCOV_IN_CTXSW; \
} while (0)

#else

static inline void kcov_task_init(struct task_struct *t) {}
static inline void kcov_task_exit(struct task_struct *t) {}
static inline void kcov_prepare_switch(struct task_struct *t) {}
static inline void kcov_finish_switch(struct task_struct *t) {}

#endif /* CONFIG_KCOV */
#endif /* _LINUX_KCOV_H */
2 changes: 1 addition & 1 deletion include/linux/sched.h
Original file line number Diff line number Diff line change
Expand Up @@ -1130,7 +1130,7 @@ struct task_struct {

#ifdef CONFIG_KCOV
/* Coverage collection mode enabled for this task (0 if disabled): */
enum kcov_mode kcov_mode;
unsigned int kcov_mode;

/* Size of the kcov_area: */
unsigned int kcov_size;
Expand Down
2 changes: 1 addition & 1 deletion kernel/kcov.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ struct kcov {

static bool check_kcov_mode(enum kcov_mode needed_mode, struct task_struct *t)
{
enum kcov_mode mode;
unsigned int mode;

/*
* We are interested in code coverage as a function of a syscall inputs,
Expand Down
4 changes: 4 additions & 0 deletions kernel/sched/core.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
#include <linux/kthread.h>
#include <linux/nospec.h>

#include <linux/kcov.h>

#include <asm/switch_to.h>
#include <asm/tlb.h>

Expand Down Expand Up @@ -2633,6 +2635,7 @@ static inline void
prepare_task_switch(struct rq *rq, struct task_struct *prev,
struct task_struct *next)
{
kcov_prepare_switch(prev);
sched_info_switch(rq, prev, next);
perf_event_task_sched_out(prev, next);
rseq_preempt(prev);
Expand Down Expand Up @@ -2702,6 +2705,7 @@ static struct rq *finish_task_switch(struct task_struct *prev)
finish_task(prev);
finish_lock_switch(rq);
finish_arch_post_lock_switch();
kcov_finish_switch(current);

fire_sched_in_preempt_notifiers(current);
/*
Expand Down

0 comments on commit 0ed557a

Please sign in to comment.