Received: by 2002:a25:d7c1:0:0:0:0:0 with SMTP id o184csp1098363ybg; Wed, 23 Oct 2019 10:18:44 -0700 (PDT) X-Google-Smtp-Source: APXvYqxzZ2VKodQBExR2SFeNBI1k8P8FAKN6v3F0tSdYQZboncCQ4AodwZbPGuAmBfgSWk4FjFvE X-Received: by 2002:a17:906:7094:: with SMTP id b20mr34304807ejk.134.1571851124169; Wed, 23 Oct 2019 10:18:44 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1571851124; cv=none; d=google.com; s=arc-20160816; b=CvfwQtugzzVb8rIEc/nm0bnWWeaokf/a+imGuy48IETaGKMxPGPolo2QKWEs79a3Q5 8XXat8pWec4ZVKZZ/8GPKogj4iaj2vW+gpaZvNLjMguGZAqIz1YjooYRrCP2dMsV9EVa FX58lVig6E+ERg/1KAtOIG/GNgLQLRK5I5u1B64ilSWvDhV2qcomejbjaX3R32cAS4qs gRbqd6eM+0K4wVkJ1dXsB5oPUj9CHiXL+LUVg8hEI8P3bOmoqxrQe/IpOGeQQujTCo51 GogPDzBIqs3yWPaCr4ioaSrHniPtLVtoa99YL8GxrXxjmElP6Ick//1i0g6HG4ztcy2p P2jg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:dkim-signature; bh=B49NIMe9Qp45m4n7j4yBsM/w0U/Y5dSRw5y61/jmg/U=; b=EUwhD+Ml9D9Q8Bh6bMGHAQppcjQeD3BuZb738iuZcqJdxga5+ZjuwbyLxrZrXMYMdo ceR4X0XXGJ+tfpjiS6e0z+Ace5jXkGBGVgh6T3zRB6h9AXYN3byFrinNiptzCK0aWcMT RO2fZFjgCGyaTrawXBxu7Aoro9kjnFt9SiXLOhEtBGb9g5C7C3LWOtzZFJRAxZnyZNjM G3KjIaSaU0MBfcYzfO0J8doe1yhwgsPUPVAD6u+2v4Re/YLPToDT5HZmwjlZaPJLXu87 okqWm5E43b+wjih0OA29vJSUqYBhrz4K+nhai9VbjGdnx9PniNXgWZnXL33uhsgpWibm PSLA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=C5bfrMkM; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 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. [209.132.180.67]) by mx.google.com with ESMTP id h6si10679580eda.258.2019.10.23.10.18.19; Wed, 23 Oct 2019 10:18:44 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=C5bfrMkM; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 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 S2405328AbfJWMcg (ORCPT + 99 others); Wed, 23 Oct 2019 08:32:36 -0400 Received: from mail-qk1-f194.google.com ([209.85.222.194]:46629 "EHLO mail-qk1-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2391588AbfJWMce (ORCPT ); Wed, 23 Oct 2019 08:32:34 -0400 Received: by mail-qk1-f194.google.com with SMTP id e66so19532968qkf.13 for ; Wed, 23 Oct 2019 05:32:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=B49NIMe9Qp45m4n7j4yBsM/w0U/Y5dSRw5y61/jmg/U=; b=C5bfrMkMZwSNBuaqGvLHQGLvu+WkV3TTh3kdpyy/ob6oFoN9VqPr039NiJ7r8PbBbp i0Ndg1H1PKsVMYNLuBizaGICaR2zwIKJFqesGKj+GHlL+Yo7/uZxSHFBqJi57C5+6WAI yLSx35vCO8IkEpJmYZU0KyQ+UhKSimD8bpgLouFa3SPiviAOlFtIK3olMU0e4mYzxTR6 ri4W3JAkTNY7BOh/AjMrLXlT8nx0TKh2nckATUVkPDUkUMcBXJfpfUlMUYgruercaW4W n/Sm5BbGsBp0U3XPBitQSLz/RMcCyWyqI4R/8ctXEjE0kHygx5YEq/GvmIVT0pEhi0Wm Wq5A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=B49NIMe9Qp45m4n7j4yBsM/w0U/Y5dSRw5y61/jmg/U=; b=rD9V6g4A8FB8W7mXOspmlTBBxfOGiEEOA2f9iBj/zG0H7yXt1rU80CIi3lM6DPoulC paUPkr9rRvDagdkNs0owcRINHJKxvcblVqeoD8hKwGPCpd7wLPf74CXsCgdg4QkrG0K+ QeauBIeOjxVvtOm2fFgVW/uFe1o7BxX8IzxibVZJOtIhns8UlbzbRJay9NgxNLe/Aagu Gi2C+5qbgtLUkLtqbMRX4uoP7eu7Tei3ovgYPjvLSL98hHSl1H33EJTsEdeoHzX9jCjs YCby09AhuFz+WkAFwR8bssvhmIKcG9NyR7iArt6jGqCsqF56XkSz2MnSNgmC/udQMCrB v+Gg== X-Gm-Message-State: APjAAAXJ9p1P1S0NQ3Ffsoky/WWE0LWCo2UN1QdI0F05ZutBwK4Eg01D mqEYjJVzkX4VOpuuufZ64p1AMM5oZMBQqIFDEw18sg== X-Received: by 2002:a37:4a87:: with SMTP id x129mr7970246qka.43.1571833952687; Wed, 23 Oct 2019 05:32:32 -0700 (PDT) MIME-Version: 1.0 References: <20191017141305.146193-1-elver@google.com> <20191017141305.146193-2-elver@google.com> In-Reply-To: <20191017141305.146193-2-elver@google.com> From: Dmitry Vyukov Date: Wed, 23 Oct 2019 14:32:20 +0200 Message-ID: Subject: Re: [PATCH v2 1/8] kcsan: Add Kernel Concurrency Sanitizer infrastructure To: Marco Elver Cc: LKMM Maintainers -- Akira Yokosawa , Alan Stern , Alexander Potapenko , Andrea Parri , Andrey Konovalov , Andy Lutomirski , Ard Biesheuvel , Arnd Bergmann , Boqun Feng , Borislav Petkov , Daniel Axtens , Daniel Lustig , Dave Hansen , David Howells , "H. Peter Anvin" , Ingo Molnar , Jade Alglave , Joel Fernandes , Jonathan Corbet , Josh Poimboeuf , Luc Maranget , Mark Rutland , Nicholas Piggin , "Paul E. McKenney" , Peter Zijlstra , Thomas Gleixner , Will Deacon , kasan-dev , linux-arch , "open list:DOCUMENTATION" , linux-efi@vger.kernel.org, "open list:KERNEL BUILD + fi..." , LKML , Linux-MM , "the arch/x86 maintainers" Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, Oct 17, 2019 at 4:13 PM Marco Elver wrote: > > Kernel Concurrency Sanitizer (KCSAN) is a dynamic data-race detector for > kernel space. KCSAN is a sampling watchpoint-based data-race detector. > See the included Documentation/dev-tools/kcsan.rst for more details. I think there is some significant potential for improving performance. Currently we have __tsan_read8 do 2 function calls, push/pop, the second call is on unpredicted slow path. Then __kcsan_check_watchpoint and __kcsan_setup_watchpoint do full load of spills and lots of loads and checks that are not strictly necessary or can be avoided. Additionally __kcsan_setup_watchpoint calls non-inlined kcsan_is_atomic. I think we need to try to structure it around the fast path as follows: __tsan_read8 does no function calls and no spills on fast path for both checking existing watchpoints and checking if a new watchpoint need to be setup. If it discovers a race with existing watchpoint or needs to setup a new one, that should be non-inlined tail calls to the corresponding slow paths. In particular, global enable/disable can be replaced with occupying/freeing all watchpoints. Per cpu disabled check should be removed from fast path somehow, it's only used around debugging checks or during reporting. There should be a way to check it on a slower path. user_access_save should be removed from fast path, we needed it only if we setup a watchpoint. But I am not sure why we need it at all, we should not be reading any user addresses. should_watch should be restructured to decrement kcsan_skip first, if it hits zero (with unlikely hint), we go to slow path. The slow path resets kcsan_skip to something random. The comment mentions prandom_u32 is too expensive, do I understand it correctly that you tried to call it on the fast path? I would expect it is fine for slow path and will give us better randomness. At this point we should return from __tsan_read8. To measure performance we could either do some synthetic in-kernel benchmarks (e.g. writing something to the debugfs file, which will do a number of memory accesses in a loop). Or you may try these user-space benchmarks: https://github.com/google/sanitizers/blob/master/address-sanitizer/kernel_buildbot/slave/bench_readv.c https://github.com/google/sanitizers/blob/master/address-sanitizer/kernel_buildbot/slave/bench_pipes.c