Re: [PATCH v2] kcov: properly check if we are in an interrupt

From: Dmitry Vyukov
Date: Mon Oct 10 2016 - 13:20:09 EST


On Mon, Oct 10, 2016 at 6:10 PM, Andrey Konovalov <andreyknvl@xxxxxxxxxx> wrote:
> in_interrupt() returns a nonzero value when we are either in an
> interrupt or have bh disabled via local_bh_disable(). Since we are
> interested in only ignoring coverage from actual interrupts, do a
> proper check instead of just calling in_interrupt().
>
> Signed-off-by: Andrey Konovalov <andreyknvl@xxxxxxxxxx>

FWIW
Acked-by: Dmitry Vyukov <dvyukov@xxxxxxxxxx>

This fixes a very real problem for us.
As per discussion in v1, other solution would involve auditing all
uses of in_interrupt() which needs knowledge about all drivers.

> ---
> Changes in v2:
> - Add a comment explaining why the check is open-coded.
>
> kernel/kcov.c | 9 ++++++++-
> 1 file changed, 8 insertions(+), 1 deletion(-)
>
> diff --git a/kernel/kcov.c b/kernel/kcov.c
> index 8d44b3f..30e6d05 100644
> --- a/kernel/kcov.c
> +++ b/kernel/kcov.c
> @@ -53,8 +53,15 @@ void notrace __sanitizer_cov_trace_pc(void)
> /*
> * We are interested in code coverage as a function of a syscall inputs,
> * so we ignore code executed in interrupts.
> + * The checks for whether we are in an interrupt are open-coded, because
> + * 1. We can't use in_interrupt() here, since it also returns true
> + * when we are inside local_bh_disable() section.
> + * 2. We don't want to use (in_irq() | in_serving_softirq() | in_nmi()),
> + * since that leads to slower generated code (three separate tests,
> + * one for each of the flags).
> */
> - if (!t || in_interrupt())
> + if (!t || (preempt_count() & (HARDIRQ_MASK | SOFTIRQ_OFFSET
> + | NMI_MASK)))
> return;
> mode = READ_ONCE(t->kcov_mode);
> if (mode == KCOV_MODE_TRACE) {
> --
> 2.8.0.rc3.226.g39d4020
>