From patchwork Mon Sep 7 13:40:45 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marco Elver X-Patchwork-Id: 11761047 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 37798138E for ; Mon, 7 Sep 2020 13:41:35 +0000 (UTC) Received: from merlin.infradead.org (merlin.infradead.org [205.233.59.134]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 05B3F21481 for ; Mon, 7 Sep 2020 13:41:34 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=lists.infradead.org header.i=@lists.infradead.org header.b="sEOkF/s2"; dkim=fail reason="signature verification failed" (2048-bit key) header.d=google.com header.i=@google.com header.b="Gi64PDyC" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 05B3F21481 Authentication-Results: mail.kernel.org; dmarc=fail (p=reject dis=none) header.from=google.com Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=linux-arm-kernel-bounces+patchwork-linux-arm=patchwork.kernel.org@lists.infradead.org DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=merlin.20170209; h=Sender:Content-Transfer-Encoding: Content-Type:Cc:List-Subscribe:List-Help:List-Post:List-Archive: List-Unsubscribe:List-Id:To:From:Subject:Mime-Version:Message-Id:Date: Reply-To:Content-ID:Content-Description:Resent-Date:Resent-From:Resent-Sender :Resent-To:Resent-Cc:Resent-Message-ID:In-Reply-To:References:List-Owner; bh=sQmcJjM6cqt/cEWa25vX/4Y4K3vkQHxs5H9Y+mTmelA=; b=sEOkF/s286/mHDr/V9h8qorPOm gJtBY1qc3iK+bZe/8a3bjazFJtuLYkYDwU1Tfq8a34p3AiVoe4ibLDYST1MMLB6Fs829LUMYkHSsg ZGs/Kny4rD8iBuK+dqfGzsqWzlPs8Is3O2isYJjS4OhA+wLA/WqH+gZxiHCgCT6tCmP38PsOL7td2 +I9LZK5aGmGdypcEx17/s6WUEKzPnA+KoKotxqhdJ1SIXD2Z7p94Cvs5lNyCVhRS1VVd3mQNSKfoM fA3DXwRt4/3eTp5/9xsMQFnIq0xGJdf2emgIX/W/xKbRRdb8RXjOiHMtRmuldX8mZf4CvzUYAKuZh G/tOzOKQ==; Received: from localhost ([::1] helo=merlin.infradead.org) by merlin.infradead.org with esmtp (Exim 4.92.3 #3 (Red Hat Linux)) id 1kFHOW-0003Wz-KZ; Mon, 07 Sep 2020 13:41:16 +0000 Received: from mail-wr1-x449.google.com ([2a00:1450:4864:20::449]) by merlin.infradead.org with esmtps (Exim 4.92.3 #3 (Red Hat Linux)) id 1kFHOT-0003VR-2T for linux-arm-kernel@lists.infradead.org; Mon, 07 Sep 2020 13:41:14 +0000 Received: by mail-wr1-x449.google.com with SMTP id l15so5716942wro.10 for ; Mon, 07 Sep 2020 06:41:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=sender:date:message-id:mime-version:subject:from:to:cc; bh=06/chDIM1D/CQyVkMXWbiHKfJ0qcUiQ/wlOCjUIfV4g=; b=Gi64PDyCJvCp/tt4Z0/hjKNv06G6HgiODtWEhrPq/9718UaxP4j1aMYDXi14PDx4GM LqTk2jESKOu67g0eDwANYR/VFP70rFlrilGv5cwaNXAoImiLI9KgqBDdfi4rgpuaWRbE 3lSqsYubl65SjuLWTAqZCN92vqnoLX4nyGD26p3UeGSBdSNHD30ruAfJ5DebQn+WDZG0 mz5+7YDvAfM6oL8bGuQc4llyrn0xm57Ntt5SwSkzahoT5PLL94LxKF31f8ekJDjdeEgK /t09asBE9p+CmE277jxbz+b5azRphPdc1VO0TlF1ylp/UDv2Aolea0uGa0WlcWO5/oQh /DHQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:date:message-id:mime-version:subject:from :to:cc; bh=06/chDIM1D/CQyVkMXWbiHKfJ0qcUiQ/wlOCjUIfV4g=; b=eMGHOkvxAf77Z6KG0+GZSqW7gCxs8VaODQx/JUE9s7vjZqo8xF3gAdN+ADdijsn5Z8 9MyIOkMALqMD9FfYUeC5WNe16ugaO/k8TY7SsdEtNpaGq2ITN02+pSeBePm5BrTdkcwF L1dv615UtPTYtTXgXVyH9fhX3ZjGTP3lO4mXxyOOjeTUy/CADHdODxR411WWNVmQjowi EN96g+DT2kDKY88i/hyhjxmbadhtPgR5vwVimzsxpbxU3uC4ykhGliv/nJuaNP3F1bNU EdzQYwfZdz5xhC513iLeWEWi7V+Q2mRCpFy03yRiXVhY/QQdWAMrBYbvIYYfHvGzvMJp lYJg== X-Gm-Message-State: AOAM533PVW5kLaZ8k1Mk9Pf1G+/ZxLZI+BQ9sBkd4GA1O6MtTHUmT2CM U8tQd/nLPtGXVoaLyWmhnuLyuq0Jzg== X-Google-Smtp-Source: ABdhPJyA//VxoSplqPrkXv/zugF4TAybenik7vBycKUrpvQnl+gUJlrGweOUts2LFD7+sLTPAyx8DGpKqg== X-Received: from elver.muc.corp.google.com ([2a00:79e0:15:13:f693:9fff:fef4:2449]) (user=elver job=sendgmr) by 2002:a7b:ce0d:: with SMTP id m13mr22014038wmc.83.1599486069496; Mon, 07 Sep 2020 06:41:09 -0700 (PDT) Date: Mon, 7 Sep 2020 15:40:45 +0200 Message-Id: <20200907134055.2878499-1-elver@google.com> Mime-Version: 1.0 X-Mailer: git-send-email 2.28.0.526.ge36021eeef-goog Subject: [PATCH RFC 00/10] KFENCE: A low-overhead sampling-based memory safety error detector From: Marco Elver To: elver@google.com, glider@google.com, akpm@linux-foundation.org, catalin.marinas@arm.com, cl@linux.com, rientjes@google.com, iamjoonsoo.kim@lge.com, mark.rutland@arm.com, penberg@kernel.org X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20200907_094113_202704_AF0D9702 X-CRM114-Status: GOOD ( 16.60 ) X-Spam-Score: -7.7 (-------) X-Spam-Report: SpamAssassin version 3.4.4 on merlin.infradead.org summary: Content analysis details: (-7.7 points) pts rule name description ---- ---------------------- -------------------------------------------------- -0.0 RCVD_IN_DNSWL_NONE RBL: Sender listed at https://www.dnswl.org/, no trust [2a00:1450:4864:20:0:0:0:449 listed in] [list.dnswl.org] -0.0 SPF_PASS SPF: sender matches SPF record -7.5 USER_IN_DEF_DKIM_WL From: address is in the default DKIM white-list 0.0 SPF_HELO_NONE SPF: HELO does not publish an SPF Record 0.1 DKIM_SIGNED Message has a DKIM or DK signature, not necessarily valid -0.1 DKIM_VALID_AU Message has a valid DKIM or DK signature from author's domain -0.1 DKIM_VALID Message has at least one valid DKIM or DK signature -0.1 DKIM_VALID_EF Message has a valid DKIM or DK signature from envelope-from domain -0.0 DKIMWL_WL_MED DKIMwl.org - Medium sender X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: linux-doc@vger.kernel.org, peterz@infradead.org, dave.hansen@linux.intel.com, linux-mm@kvack.org, edumazet@google.com, hpa@zytor.com, will@kernel.org, corbet@lwn.net, x86@kernel.org, kasan-dev@googlegroups.com, mingo@redhat.com, linux-arm-kernel@lists.infradead.org, aryabinin@virtuozzo.com, keescook@chromium.org, paulmck@kernel.org, jannh@google.com, andreyknvl@google.com, cai@lca.pw, luto@kernel.org, tglx@linutronix.de, dvyukov@google.com, gregkh@linuxfoundation.org, linux-kernel@vger.kernel.org, bp@alien8.de Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+patchwork-linux-arm=patchwork.kernel.org@lists.infradead.org This adds the Kernel Electric-Fence (KFENCE) infrastructure. KFENCE is a low-overhead sampling-based memory safety error detector of heap use-after-free, invalid-free, and out-of-bounds access errors. This series enables KFENCE for the x86 and arm64 architectures, and adds KFENCE hooks to the SLAB and SLUB allocators. KFENCE is designed to be enabled in production kernels, and has near zero performance overhead. Compared to KASAN, KFENCE trades performance for precision. The main motivation behind KFENCE's design, is that with enough total uptime KFENCE will detect bugs in code paths not typically exercised by non-production test workloads. One way to quickly achieve a large enough total uptime is when the tool is deployed across a large fleet of machines. KFENCE objects each reside on a dedicated page, at either the left or right page boundaries. The pages to the left and right of the object page are "guard pages", whose attributes are changed to a protected state, and cause page faults on any attempted access to them. Such page faults are then intercepted by KFENCE, which handles the fault gracefully by reporting a memory access error. Guarded allocations are set up based on a sample interval (can be set via kfence.sample_interval). After expiration of the sample interval, a guarded allocation from the KFENCE object pool is returned to the main allocator (SLAB or SLUB). At this point, the timer is reset, and the next allocation is set up after the expiration of the interval. To enable/disable a KFENCE allocation through the main allocator's fast-path without overhead, KFENCE relies on static branches via the static keys infrastructure. The static branch is toggled to redirect the allocation to KFENCE. We have verified by running synthetic benchmarks (sysbench I/O, hackbench) that a kernel with KFENCE is performance-neutral compared to a non-KFENCE baseline kernel. KFENCE is inspired by GWP-ASan [1], a userspace tool with similar properties. The name "KFENCE" is a homage to the Electric Fence Malloc Debugger [2]. For more details, see Documentation/dev-tools/kfence.rst added in the series -- also viewable here: https://raw.githubusercontent.com/google/kasan/kfence/Documentation/dev-tools/kfence.rst [1] http://llvm.org/docs/GwpAsan.html [2] https://linux.die.net/man/3/efence Alexander Potapenko (6): mm: add Kernel Electric-Fence infrastructure x86, kfence: enable KFENCE for x86 mm, kfence: insert KFENCE hooks for SLAB mm, kfence: insert KFENCE hooks for SLUB kfence, kasan: make KFENCE compatible with KASAN kfence, kmemleak: make KFENCE compatible with KMEMLEAK Marco Elver (4): arm64, kfence: enable KFENCE for ARM64 kfence, lockdep: make KFENCE compatible with lockdep kfence, Documentation: add KFENCE documentation kfence: add test suite Documentation/dev-tools/index.rst | 1 + Documentation/dev-tools/kfence.rst | 285 +++++++++++ MAINTAINERS | 11 + arch/arm64/Kconfig | 1 + arch/arm64/include/asm/kfence.h | 39 ++ arch/arm64/mm/fault.c | 4 + arch/x86/Kconfig | 2 + arch/x86/include/asm/kfence.h | 60 +++ arch/x86/mm/fault.c | 4 + include/linux/kfence.h | 174 +++++++ init/main.c | 2 + kernel/locking/lockdep.c | 8 + lib/Kconfig.debug | 1 + lib/Kconfig.kfence | 70 +++ mm/Makefile | 1 + mm/kasan/common.c | 7 + mm/kfence/Makefile | 6 + mm/kfence/core.c | 730 +++++++++++++++++++++++++++ mm/kfence/kfence-test.c | 777 +++++++++++++++++++++++++++++ mm/kfence/kfence.h | 104 ++++ mm/kfence/report.c | 201 ++++++++ mm/kmemleak.c | 11 + mm/slab.c | 46 +- mm/slab_common.c | 6 +- mm/slub.c | 72 ++- 25 files changed, 2591 insertions(+), 32 deletions(-) create mode 100644 Documentation/dev-tools/kfence.rst create mode 100644 arch/arm64/include/asm/kfence.h create mode 100644 arch/x86/include/asm/kfence.h create mode 100644 include/linux/kfence.h create mode 100644 lib/Kconfig.kfence create mode 100644 mm/kfence/Makefile create mode 100644 mm/kfence/core.c create mode 100644 mm/kfence/kfence-test.c create mode 100644 mm/kfence/kfence.h create mode 100644 mm/kfence/report.c