Received: by 2002:a05:6a10:d5a5:0:0:0:0 with SMTP id gn37csp4475212pxb; Tue, 5 Oct 2021 04:01:57 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwDC+o4QE4IG4Mrh+Jis3Oq539G78iypIpIE9WznWdy6QZqA0D4vz48Bmqu8d8G91adxu1y X-Received: by 2002:aa7:cf06:: with SMTP id a6mr25451298edy.108.1633431717452; Tue, 05 Oct 2021 04:01:57 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1633431717; cv=none; d=google.com; s=arc-20160816; b=PUIQF1nkmq4OGQ6cxEHiO8V1EPfxp/gL9QGeNexK2k6EcWJQEhXtau7eS9ShLm+8kN 0I0/Rj/A7ruTmAh8yiRoRTc1uktcM/e3vgTldDMbrZzOnyrENSfvuOC/HPRnrAzCncgg vkJePnmsR/KUKwStGxuI34o4tR7V8E8JTOS/MePJ0HIa2FI4bW7lL3bC9W2UIVv4GnP4 VExExFJab73Q6fdnFQt+kzJ3mKseb+2smJpn35W5WTfb0bq1s19/GQaUWvLV9Pi4LTTi uT0Ymfy0wpa1Jix4RYUn4iG/Hu0ydLmn0mqfGBc+nvSTPbMhj7vqxGvsKFjyLmECk9is bOrw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:references:mime-version :message-id:in-reply-to:date:dkim-signature; bh=kUUBsv4TZ+s6mxdftD0CD4qwCQSHLXZlKlhseQGR4HI=; b=FMf6d+UupydPmIBwtpleu0tsfN4ZMIJBiTTSte5CRFNqKB/4PFOWmWdtAX1xPHloMg g8sUQOpKVRNDLJA1V1fb8JyZcihbXWTytiYDwWU+Yyt5nYkbASLqF0239/xd+uRvkpRA 3nTQbKliu+PsU9Iivph0y5aUMLmYfeLM/aTrH7rBYziR1EGsIrz1sucmNN1IgRH3NU14 VVbA8+OjO7hyzHsTPWyZr/CTijzhjmZJktLmvdezgrtXR0+GPIqtSXtRAgYh79PheOBk g+FfbFwOEV1vDr7BQjyuAPnt6aef6VxEMB722sRUaTfMTkcgewpVnuV+K32XkzLIUR97 IbNA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=rEZQ6gHE; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id 26si15481280ejr.774.2021.10.05.04.01.31; Tue, 05 Oct 2021 04:01:57 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=rEZQ6gHE; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234262AbhJELBs (ORCPT + 99 others); Tue, 5 Oct 2021 07:01:48 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51978 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234310AbhJELBl (ORCPT ); Tue, 5 Oct 2021 07:01:41 -0400 Received: from mail-yb1-xb49.google.com (mail-yb1-xb49.google.com [IPv6:2607:f8b0:4864:20::b49]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A1584C061760 for ; Tue, 5 Oct 2021 03:59:50 -0700 (PDT) Received: by mail-yb1-xb49.google.com with SMTP id 81-20020a251254000000b005b6220d81efso27605462ybs.12 for ; Tue, 05 Oct 2021 03:59:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=kUUBsv4TZ+s6mxdftD0CD4qwCQSHLXZlKlhseQGR4HI=; b=rEZQ6gHEpvFmnU3JJ2KadwYQcU/JSBtWD9kFPefHUesYhWgD+zWgArmWGmRaRUoRtk VDKK8Clkd1IXKKUXE7uGOCQiR/a8VLzgq14e4z3yFqsyEMB8J+yA2fLfPmDZ1cgwu7IA hxWJQYHsv0nxh98TCmcq99vXZHi81m/dePuKk+O8qPlob0rH4fJEMk6xuMnX/JJw3dJK w+/HEGUet8yHVB4XffG2TDeLzpNjvUlMe1PO08esWDxORp5yjfb6ZGwfrmryknbMGoVf IweJBPgmp5Gr9RwWgQoSzOGRDC7O4CUXO3DyTZtoWRfCwGXfU1t0hBdbgyv0icSZytvz vwrw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=kUUBsv4TZ+s6mxdftD0CD4qwCQSHLXZlKlhseQGR4HI=; b=1Oy9wI6ehHLwrckIttAL7zCIGKBUmByS5KpgU6PNnISJ8H80NW6D+HJWaxEuib3ofP T9JgP6ryUKfm3p2LMQ1wbMYI/BQlhRdxq29bRUuEo5+KF1Af5RpeMzUBJqyj2dceKeH9 2LttvR9P/gr4k5FH0vl5GlMK2EyHOWp1IKCZGsmSdBpOShBSwp8/ttw7VxHCZWBaWJ/7 PTNOCDD21pc9vNDwsA91ENf0HB7SIZ/6f2pUKH6ulznAqfpKFnbqu4LiFaVAIERJvl5k ZP38tY1XrilQz8dqNxacw3R1R3mIbpF+c3naul3P3SJc4B8hKLbHrjyS5AudeMClukck eWFg== X-Gm-Message-State: AOAM5313KKOHKM9xCyYeT3gpYzX6itqs1EDXJFjOgUE5P6EedhX20u2s xWTH+qJawcYfyqBa1wCsaot2Ts6gAA== X-Received: from elver.muc.corp.google.com ([2a00:79e0:15:13:e44f:5054:55f8:fcb8]) (user=elver job=sendgmr) by 2002:a5b:f03:: with SMTP id x3mr20807187ybr.546.1633431589883; Tue, 05 Oct 2021 03:59:49 -0700 (PDT) Date: Tue, 5 Oct 2021 12:58:45 +0200 In-Reply-To: <20211005105905.1994700-1-elver@google.com> Message-Id: <20211005105905.1994700-4-elver@google.com> Mime-Version: 1.0 References: <20211005105905.1994700-1-elver@google.com> X-Mailer: git-send-email 2.33.0.800.g4c38ced690-goog Subject: [PATCH -rcu/kcsan 03/23] kcsan: Avoid checking scoped accesses from nested contexts From: Marco Elver To: elver@google.com, "Paul E . McKenney" Cc: Alexander Potapenko , Boqun Feng , Borislav Petkov , Dmitry Vyukov , Ingo Molnar , Josh Poimboeuf , Mark Rutland , Peter Zijlstra , Thomas Gleixner , Waiman Long , Will Deacon , kasan-dev@googlegroups.com, linux-arch@vger.kernel.org, linux-doc@vger.kernel.org, linux-kbuild@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, x86@kernel.org Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Avoid checking scoped accesses from nested contexts (such as nested interrupts or in scheduler code) which share the same kcsan_ctx. This is to avoid detecting false positive races of accesses in the same thread with currently scoped accesses: consider setting up a watchpoint for a non-scoped (normal) access that also "conflicts" with a current scoped access. In a nested interrupt (or in the scheduler), which shares the same kcsan_ctx, we cannot check scoped accesses set up in the parent context -- simply ignore them in this case. With the introduction of kcsan_ctx::disable_scoped, we can also clean up kcsan_check_scoped_accesses()'s recursion guard, and do not need to modify the list's prev pointer. Signed-off-by: Marco Elver --- include/linux/kcsan.h | 1 + kernel/kcsan/core.c | 18 +++++++++++++++--- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/include/linux/kcsan.h b/include/linux/kcsan.h index fc266ecb2a4d..13cef3458fed 100644 --- a/include/linux/kcsan.h +++ b/include/linux/kcsan.h @@ -21,6 +21,7 @@ */ struct kcsan_ctx { int disable_count; /* disable counter */ + int disable_scoped; /* disable scoped access counter */ int atomic_next; /* number of following atomic ops */ /* diff --git a/kernel/kcsan/core.c b/kernel/kcsan/core.c index e34a1710b7bc..bd359f8ee63a 100644 --- a/kernel/kcsan/core.c +++ b/kernel/kcsan/core.c @@ -204,15 +204,17 @@ check_access(const volatile void *ptr, size_t size, int type, unsigned long ip); static noinline void kcsan_check_scoped_accesses(void) { struct kcsan_ctx *ctx = get_ctx(); - struct list_head *prev_save = ctx->scoped_accesses.prev; struct kcsan_scoped_access *scoped_access; - ctx->scoped_accesses.prev = NULL; /* Avoid recursion. */ + if (ctx->disable_scoped) + return; + + ctx->disable_scoped++; list_for_each_entry(scoped_access, &ctx->scoped_accesses, list) { check_access(scoped_access->ptr, scoped_access->size, scoped_access->type, scoped_access->ip); } - ctx->scoped_accesses.prev = prev_save; + ctx->disable_scoped--; } /* Rules for generic atomic accesses. Called from fast-path. */ @@ -465,6 +467,15 @@ kcsan_setup_watchpoint(const volatile void *ptr, size_t size, int type, unsigned goto out; } + /* + * Avoid races of scoped accesses from nested interrupts (or scheduler). + * Assume setting up a watchpoint for a non-scoped (normal) access that + * also conflicts with a current scoped access. In a nested interrupt, + * which shares the context, it would check a conflicting scoped access. + * To avoid, disable scoped access checking. + */ + ctx->disable_scoped++; + /* * Save and restore the IRQ state trace touched by KCSAN, since KCSAN's * runtime is entered for every memory access, and potentially useful @@ -578,6 +589,7 @@ kcsan_setup_watchpoint(const volatile void *ptr, size_t size, int type, unsigned if (!kcsan_interrupt_watcher) local_irq_restore(irq_flags); kcsan_restore_irqtrace(current); + ctx->disable_scoped--; out: user_access_restore(ua_flags); } -- 2.33.0.800.g4c38ced690-goog