Received: by 2002:a05:6a10:22f:0:0:0:0 with SMTP id 15csp1346488pxk; Fri, 2 Oct 2020 07:24:49 -0700 (PDT) X-Google-Smtp-Source: ABdhPJys9jH1T67Zpk31axtTDk3qFKFrBTIAozdHZLulOcsFVanN+83Kwcsfa3o2ZqlmoY2ToIWR X-Received: by 2002:a50:8e52:: with SMTP id 18mr2636620edx.28.1601648689699; Fri, 02 Oct 2020 07:24:49 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1601648689; cv=none; d=google.com; s=arc-20160816; b=ozJ5oUrtYDOoucPApteUig3kuBUBDoHKpMv0unvHL1klNNRoTcSlQWbDlG0taGjBxy VKN1l8tFIwAgiwLOGoWDwI4RpLQoLHhLCmiI8ljCIObtiiOKjb0WoWL/hfIOXOEhP5Ff AWXb3gYxsc3iz0ENJBmQDXdp/A90qamYp4g4S3QXhO4zxk8JMr7SQXAm6DjrwM0POBly JDkgF3AJsdzMRDhYbu9OqjyhKJ57FqyWM8w77OcWrhKKPeTLClPoc8tzMovE1j+BcXlT r+hG47Nbng/GFD1MPZuGXz8M3KFjguox1SMOTw562xlmYxQhIx4GJq3LjvsITmxxdlo6 Rovw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=CAgelpYpJihRsjmlp4EjmhmaunlYd8C4mVKtF8jDi3w=; b=coKa4F5H6noU9ASJosYR7h1Xy37e8cJyBWylVWfaH+vxyDwTmJWGwJDWXCKDPmVORU Rr/LXS75qOEr86yWfKWR/Q7p0BH7iTrXyR2nkKKHvyr3ss6Fn6rZbDMd7qr+Y09zOAmO foep3ofLZSuGxXJCDzcktv9pJCPpWufQDbAv/IwdzMtbCaw+ObQ81f2E9WCNvaaM8iuc Ab5GAGRj2p73ciTf2KaziK/PCzzRJvt5LYeRed7OIy7MK/yvP5X/8FfYMgnbAtNCLBex zQ7k4KxHrVc+C8RHM6QlrPsH+vCgCZki53ESZ6TD38n2EGF6dBIHPw/hYlnixpmV0d5r E8Ig== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=RKfZWi+R; 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 x100si1054519ede.335.2020.10.02.07.24.26; Fri, 02 Oct 2020 07:24:49 -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=20161025 header.b=RKfZWi+R; 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 S2388019AbgJBOXM (ORCPT + 99 others); Fri, 2 Oct 2020 10:23:12 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53514 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2387688AbgJBOXM (ORCPT ); Fri, 2 Oct 2020 10:23:12 -0400 Received: from mail-qt1-x841.google.com (mail-qt1-x841.google.com [IPv6:2607:f8b0:4864:20::841]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 09569C0613E2 for ; Fri, 2 Oct 2020 07:23:12 -0700 (PDT) Received: by mail-qt1-x841.google.com with SMTP id j22so1310888qtj.8 for ; Fri, 02 Oct 2020 07:23:11 -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=CAgelpYpJihRsjmlp4EjmhmaunlYd8C4mVKtF8jDi3w=; b=RKfZWi+RUXInTmQ3BLHhXRQmW4buor7BLC4IxwXfU0Vqf9mByJNzip/dve4hOmvzk6 EaPUIEdyK1G1ywRnCXUSs6nvcfZgwNa9fxlrgjhNf6Lx1tOsZrRqjN9/KaNIfA3BeviP YAVkCBrNlInA7HUts3canKehhdEDbAXhYkd/3n/HlYQAaC0a6m+AKB7qz+rs84ZfK7A3 ea3Shr+TAS/DiGKVASkTi14XB182x/nbUVybsej3xjWJgec9CX3W/cxbq374DFY9JlTT 3dnQFEJNiGdMLbJxX3RuO43mIUOn+sDon5zqHf5EDY4guonk1AnckfPwU4PkR4lsqbpu n4Uw== 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=CAgelpYpJihRsjmlp4EjmhmaunlYd8C4mVKtF8jDi3w=; b=JEwgZ2Wh/qqvzyWBvVsju4jJq0DFH8wDVVRlQTRtHhRH8o3GSEOZl8dWGPB9EKY6eB 88mSAUPZxNR5xs6gAMb6DaLYf7vsgm/VQSDtQn0IaJcrObc4vLjx7P5fyG6HNrPcf+bn 5Bk9dOGy4INVMXKchlauHBe6aC0Crmfv2JCdnaY02RGUWZo9OagdvbWtrzIkTpuXWAXE x+fx+HaQfNkkcMX+nwSGRU8r0tL/+f9Q4HxTmx/cRmHCgw1VmppeEt6vcxMPAASy38hs AD1tKIdoKwbXaHg/Yu+i6KqwfCF96GMKUWR5hGZJyrlBDi6BE6w10mbTH8VUmm6O6xF+ f3Aw== X-Gm-Message-State: AOAM5334M3h4AK3WmwbpyK6u1Jpelmu0J7TWoeGoM+5jwu0RZwo3gOUB Dgt7+FDgB2VElngVWVSd6X6A1df4WhRdrtHOzNV20A== X-Received: by 2002:ac8:4806:: with SMTP id g6mr2529805qtq.380.1601648590888; Fri, 02 Oct 2020 07:23:10 -0700 (PDT) MIME-Version: 1.0 References: <20200929133814.2834621-1-elver@google.com> <20200929133814.2834621-2-elver@google.com> In-Reply-To: From: Dmitry Vyukov Date: Fri, 2 Oct 2020 16:22:59 +0200 Message-ID: Subject: Re: [PATCH v4 01/11] mm: add Kernel Electric-Fence infrastructure To: Jann Horn Cc: Marco Elver , Andrew Morton , Alexander Potapenko , "H . Peter Anvin" , "Paul E . McKenney" , Andrey Konovalov , Andrey Ryabinin , Andy Lutomirski , Borislav Petkov , Catalin Marinas , Christoph Lameter , Dave Hansen , David Rientjes , Eric Dumazet , Greg Kroah-Hartman , Hillf Danton , Ingo Molnar , Jonathan.Cameron@huawei.com, Jonathan Corbet , Joonsoo Kim , Kees Cook , Mark Rutland , Pekka Enberg , Peter Zijlstra , sjpark@amazon.com, Thomas Gleixner , Vlastimil Babka , Will Deacon , "the arch/x86 maintainers" , "open list:DOCUMENTATION" , kernel list , kasan-dev , Linux ARM , Linux-MM , SeongJae Park Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, Oct 2, 2020 at 9:54 AM Jann Horn wrote: > > On Fri, Oct 2, 2020 at 8:33 AM Jann Horn wrote: > > On Tue, Sep 29, 2020 at 3:38 PM Marco Elver wrote: > > > 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. > > > > > > 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. > [...] > > > +/* > > > + * The pool of pages used for guard pages and objects. If supported, allocated > > > + * statically, so that is_kfence_address() avoids a pointer load, and simply > > > + * compares against a constant address. Assume that if KFENCE is compiled into > > > + * the kernel, it is usually enabled, and the space is to be allocated one way > > > + * or another. > > > + */ > > > > If this actually brings a performance win, the proper way to do this > > would probably be to implement this as generic kernel infrastructure > > that makes the compiler emit large-offset relocations (either through > > compiler support or using inline asm statements that move an immediate > > into a register output and register the location in a special section, > > kinda like how e.g. static keys work) and patches them at boot time, > > or something like that - there are other places in the kernel where > > very hot code uses global pointers that are only ever written once > > during boot, e.g. the dentry cache of the VFS and the futex hash > > table. Those are probably far hotter than the kfence code. > > > > While I understand that that goes beyond the scope of this project, it > > might be something to work on going forward - this kind of > > special-case logic that turns the kernel data section into heap memory > > would not be needed if we had that kind of infrastructure. > > After thinking about it a bit more, I'm not even convinced that this > is a net positive in terms of overall performance - while it allows > you to avoid one level of indirection in some parts of kfence, that > kfence code by design only runs pretty infrequently. And to enable > this indirection avoidance, your x86 arch_kfence_initialize_pool() is > shattering potentially unrelated hugepages in the kernel data section, > which might increase the TLB pressure (and therefore the number of > memory loads that have to fall back to slow page walks) in code that > is much hotter than yours. > > And if this indirection is a real performance problem, that problem > would be many times worse in the VFS and the futex subsystem, so > developing a more generic framework for doing this cleanly would be > far more important than designing special-case code to allow kfence to > do this. > > And from what I've seen, a non-trivial chunk of the code in this > series, especially the arch/ parts, is only necessary to enable this > microoptimization. > > Do you have performance numbers or a description of why you believe > that this part of kfence is exceptionally performance-sensitive? If > not, it might be a good idea to remove this optimization, at least for > the initial version of this code. (And even if the optimization is > worthwhile, it might be a better idea to go for the generic version > immediately.) This check is very hot, it happens on every free. For every freed object we need to understand if it belongs to KFENCE or not. The generic framework for this already exists -- you simply create a global variable ;) KFENCE needs the range to be covered by struct page's and that's what creates problems for arm64. But I would assume most other users don't need that.