From patchwork Thu Apr 1 23:23:43 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kees Cook X-Patchwork-Id: 12179839 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.8 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED, USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 6C061C43461 for ; Thu, 1 Apr 2021 23:26:35 +0000 (UTC) Received: from desiato.infradead.org (desiato.infradead.org [90.155.92.199]) (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 F3E8A61105 for ; Thu, 1 Apr 2021 23:26:34 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org F3E8A61105 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=chromium.org Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=desiato.20200630; h=Sender:Content-Transfer-Encoding :Content-Type:List-Subscribe:List-Help:List-Post:List-Archive: List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To:Message-Id:Date: Subject:Cc:To:From:Reply-To:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=LZEkEXMfB/Y8sKtYO68vHvaHZeKZFfscaamn1tgUX0E=; b=Vl1NkvXU9PUnwX/X1W/vnOIgF 0Rd8ydQQTSV5nH7F1T3Sk7o5hvhv0u1Cv4UqXYy36lZ2DZPB6qh0XDVLZRC4shifm7rO066Gk+s0J 8AkVw52/jHjwn8/krOo+WkKeXyORgGgMmLPa3CSsp7ClzBoBX0tY1OYNl0GnD8PjDCCeo66n/dRKG wwwIzWncLlRkXCWahy1JpOf2Y/nCgdiYD8G8XokAwTo8aVUuFuVKZ/pGMD/TzhF/+ZbiF7pWIYO2K W6hkMOSwMEII/Hf9Oq6vqcyhAkPmWAWByCBWTdReQtwVtkVom2Hc1lFD64d87oMOipaskoYr7f908 ews992CpA==; Received: from localhost ([::1] helo=desiato.infradead.org) by desiato.infradead.org with esmtp (Exim 4.94 #2 (Red Hat Linux)) id 1lS6fn-00BJyr-1y; Thu, 01 Apr 2021 23:24:23 +0000 Received: from mail-pg1-x533.google.com ([2607:f8b0:4864:20::533]) by desiato.infradead.org with esmtps (Exim 4.94 #2 (Red Hat Linux)) id 1lS6fK-00BJny-N4 for linux-arm-kernel@lists.infradead.org; Thu, 01 Apr 2021 23:23:59 +0000 Received: by mail-pg1-x533.google.com with SMTP id v186so2529746pgv.7 for ; Thu, 01 Apr 2021 16:23:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=UG6lpZSAG8t+NfdJici9YkMGF34rxaYTMGPEmVDXXFs=; b=l8S/+ejU94GwXqXJSUyS/eNkcpIc7xdvequeiDmmOj+tYzryNYiSUA6JrVp487CYR2 tnSLdnlF6KzYbHc3HKnMPnO9xgUvAqIc/jkuucyWMc8TbQJbDc6UnjLbWhBVqiQGOha+ aOnrezt7IVhuXE6XI/3E0r3vDeG8jpQfJ2fhs= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=UG6lpZSAG8t+NfdJici9YkMGF34rxaYTMGPEmVDXXFs=; b=OUEODBn00MibWCC5eh1P9+Uox9LHi1hLidYKJecPvOLr9b0Me1DBornBX/XJTVYQaI 1ykfH5xHk1wSOINZlxRAhEF/jsltWsbOrRQY90u7hf+nbupUXaHPZ2Lzuaq/Xi/wVefl 2x0taD5Wrukl6mvG2l7pPaEyN40wK+VOMVy8ybBxzE9bLISvMi2WBvlHaPC4Q4cIiiOw 2Oeuc8OjTrwYQb0FSd7yQR1Tz00jc7456eHXuD3m8++d9Y/xPUHhzw5y7GJ0/qminKTx iTvsbx2M0ujmPuLprPtHRhAWYhs68Sq+OlK0Tvn5I+FJEV4BGcXtkcsRdkKRYLa1Fqx4 kufg== X-Gm-Message-State: AOAM530yMSLUxizuWrQuJYFjt2pysLZspEE1N3bMlcwuwwp7zcKcvH4y Swc5ClaoEGTbaHfgGszPE7UbjA== X-Google-Smtp-Source: ABdhPJzFCy8btNup6SOnGlmYqdVp8w6Awwl6ipqVL9OM241/47URJI/UqOO3mkuVCV70DR9cLI8gaw== X-Received: by 2002:a63:650:: with SMTP id 77mr9469602pgg.190.1617319433279; Thu, 01 Apr 2021 16:23:53 -0700 (PDT) Received: from www.outflux.net (smtp.outflux.net. [198.145.64.163]) by smtp.gmail.com with ESMTPSA id n10sm6363653pjo.15.2021.04.01.16.23.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 01 Apr 2021 16:23:51 -0700 (PDT) From: Kees Cook To: Thomas Gleixner Cc: Kees Cook , Alexander Potapenko , Vlastimil Babka , Elena Reshetova , x86@kernel.org, Andy Lutomirski , Peter Zijlstra , Catalin Marinas , Will Deacon , Mark Rutland , Alexander Popov , Ard Biesheuvel , Jann Horn , David Hildenbrand , Mike Rapoport , Andrew Morton , Jonathan Corbet , Randy Dunlap , kernel-hardening@lists.openwall.com, linux-hardening@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [PATCH v10 2/6] init_on_alloc: Optimize static branches Date: Thu, 1 Apr 2021 16:23:43 -0700 Message-Id: <20210401232347.2791257-3-keescook@chromium.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210401232347.2791257-1-keescook@chromium.org> References: <20210401232347.2791257-1-keescook@chromium.org> MIME-Version: 1.0 X-Patch-Hashes: v=1; h=sha256; g=2f4b59272e09d0180c87e3d8378d95ea375990ec; i=7VrM8Pzr5MGi8vAaDOW4xeiDtVYbjHgJsdLlT/sxDXE=; m=OLLKLAHsfNT1d7+pflxMGJuAvSt8GZaetKIXzfk0ilc=; p=cKq4RSNZSRZ+ASTC07w37jURMQB1hxGsdgqU07adDH0= X-Patch-Sig: m=pgp; i=keescook@chromium.org; s=0x0x8972F4DFDC6DC026; b=iQIzBAABCgAdFiEEpcP2jyKd1g9yPm4TiXL039xtwCYFAmBmVgIACgkQiXL039xtwCaolw//ZCB Logxo6dDfC7433hgQdOfpHxCZxm/7rRC+f4joWq57/QBmOMjF3W073sD+R9vdDDCGsEiNo12ISjVa G+W/a9MmfBRhcDv4JpBDlh+Dw2dFA6Ix4+3Q8R9LCG2x8jUfAyp1yii+WOrGDhliFvL3Pd+RMa9W0 CnwK2yvrB108eSO8Ryikc91HMJnXqE5c4Lp1lW2CJhX2dqrqXZKMNp4o8bjv7UdsSEu/m9mJrpCho 6MeQreVX2J2AcCceALV2gngk2JdeOaO7oHnMm6Bi9NSFB2y6ZP5NK9PVE+28bRCgklWdAxJny2Biz Q+6qRteMMChf4RkN668cPz6+bAzfxX5UIxlU+SK3f0EnDFZFVBy62XJ+j2LoGUyMEU1m3ckvWOhxg Sh2NFhQF++EM5Dp6JmARfwgu1T0w/3Tr8AYh8sC31bdT2rLCwF24VQ+IMnewePUGopvjPDmww+t7X /I1dZST8HC21YoNAddrZtWfXnGe2BZr/j2QXHLUQRRBrw8ey4mj//A/iFrKjTOVftnP1PKOvIaEHP BjsAJO5EAzUSYilh+l8IxTyO/3ek103mtxJs+8/LlJwngI+OmtRSal6hnbv0deFcHZtYYWIi69MNG y3sfIi8Kj3wUjpeBziLMxM+cTUdsKW/aHPjRiOVT6W4tPmNgpwiG7OG3vLnPz98I= X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20210402_002354_921803_FAF3E11C X-CRM114-Status: GOOD ( 16.98 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org The state of CONFIG_INIT_ON_ALLOC_DEFAULT_ON (and ...ON_FREE...) did not change the assembly ordering of the static branches: they were always out of line. Use the new jump_label macros to check the CONFIG settings to default to the "expected" state, which slightly optimizes the resulting assembly code. Reviewed-by: Alexander Potapenko Link: https://lore.kernel.org/lkml/CAG_fn=X0DVwqLaHJTO6Jw7TGcMSm77GKHinrd0m_6y0SzWOrFA@mail.gmail.com/ Acked-by: Vlastimil Babka Link: https://lore.kernel.org/lkml/5d626b9b-5355-be94-e8e2-1be47f880f30@suse.cz Signed-off-by: Kees Cook --- include/linux/mm.h | 10 ++++++---- mm/page_alloc.c | 4 ++-- mm/slab.h | 6 ++++-- 3 files changed, 12 insertions(+), 8 deletions(-) diff --git a/include/linux/mm.h b/include/linux/mm.h index 77e64e3eac80..2ccd856ac0d1 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -2871,18 +2871,20 @@ static inline void kernel_poison_pages(struct page *page, int numpages) { } static inline void kernel_unpoison_pages(struct page *page, int numpages) { } #endif -DECLARE_STATIC_KEY_FALSE(init_on_alloc); +DECLARE_STATIC_KEY_MAYBE(CONFIG_INIT_ON_ALLOC_DEFAULT_ON, init_on_alloc); static inline bool want_init_on_alloc(gfp_t flags) { - if (static_branch_unlikely(&init_on_alloc)) + if (static_branch_maybe(CONFIG_INIT_ON_ALLOC_DEFAULT_ON, + &init_on_alloc)) return true; return flags & __GFP_ZERO; } -DECLARE_STATIC_KEY_FALSE(init_on_free); +DECLARE_STATIC_KEY_MAYBE(CONFIG_INIT_ON_FREE_DEFAULT_ON, init_on_free); static inline bool want_init_on_free(void) { - return static_branch_unlikely(&init_on_free); + return static_branch_maybe(CONFIG_INIT_ON_FREE_DEFAULT_ON, + &init_on_free); } extern bool _debug_pagealloc_enabled_early; diff --git a/mm/page_alloc.c b/mm/page_alloc.c index 3e4b29ee2b1e..267c04b8911d 100644 --- a/mm/page_alloc.c +++ b/mm/page_alloc.c @@ -167,10 +167,10 @@ unsigned long totalcma_pages __read_mostly; int percpu_pagelist_fraction; gfp_t gfp_allowed_mask __read_mostly = GFP_BOOT_MASK; -DEFINE_STATIC_KEY_FALSE(init_on_alloc); +DEFINE_STATIC_KEY_MAYBE(CONFIG_INIT_ON_ALLOC_DEFAULT_ON, init_on_alloc); EXPORT_SYMBOL(init_on_alloc); -DEFINE_STATIC_KEY_FALSE(init_on_free); +DEFINE_STATIC_KEY_MAYBE(CONFIG_INIT_ON_FREE_DEFAULT_ON, init_on_free); EXPORT_SYMBOL(init_on_free); static bool _init_on_alloc_enabled_early __read_mostly diff --git a/mm/slab.h b/mm/slab.h index 076582f58f68..774c7221efdc 100644 --- a/mm/slab.h +++ b/mm/slab.h @@ -601,7 +601,8 @@ static inline void cache_random_seq_destroy(struct kmem_cache *cachep) { } static inline bool slab_want_init_on_alloc(gfp_t flags, struct kmem_cache *c) { - if (static_branch_unlikely(&init_on_alloc)) { + if (static_branch_maybe(CONFIG_INIT_ON_ALLOC_DEFAULT_ON, + &init_on_alloc)) { if (c->ctor) return false; if (c->flags & (SLAB_TYPESAFE_BY_RCU | SLAB_POISON)) @@ -613,7 +614,8 @@ static inline bool slab_want_init_on_alloc(gfp_t flags, struct kmem_cache *c) static inline bool slab_want_init_on_free(struct kmem_cache *c) { - if (static_branch_unlikely(&init_on_free)) + if (static_branch_maybe(CONFIG_INIT_ON_FREE_DEFAULT_ON, + &init_on_free)) return !(c->ctor || (c->flags & (SLAB_TYPESAFE_BY_RCU | SLAB_POISON))); return false;