From mboxrd@z Thu Jan 1 00:00:00 1970 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=-6.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,HK_RANDOM_FROM,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS autolearn=no 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 52227C433ED for ; Thu, 13 May 2021 06:28:18 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id C5B756140C for ; Thu, 13 May 2021 06:28:17 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org C5B756140C Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id DC2B56B0036; Thu, 13 May 2021 02:28:16 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id D71DA6B006E; Thu, 13 May 2021 02:28:16 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id BEB496B0070; Thu, 13 May 2021 02:28:16 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0173.hostedemail.com [216.40.44.173]) by kanga.kvack.org (Postfix) with ESMTP id 8AFCB6B0036 for ; Thu, 13 May 2021 02:28:16 -0400 (EDT) Received: from smtpin27.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay03.hostedemail.com (Postfix) with ESMTP id 191968249980 for ; Thu, 13 May 2021 06:28:16 +0000 (UTC) X-FDA: 78135228192.27.84E6132 Received: from mail-pl1-f169.google.com (mail-pl1-f169.google.com [209.85.214.169]) by imf05.hostedemail.com (Postfix) with ESMTP id 3DFE5E000113 for ; Thu, 13 May 2021 06:28:09 +0000 (UTC) Received: by mail-pl1-f169.google.com with SMTP id h20so13849112plr.4 for ; Wed, 12 May 2021 23:28:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=AV3i0cyAQqmvIdnehCa8/+iZ0ckotya/+iLpSfcFxfw=; b=OvcxGH/M5KSbpBo10SI4pyH2JolAfNM2UQUBry1oXHf0RC3Z1A/laGmOOn4WbekZxI aZzsp2zx+/4VufxePm7LLdqWXSs3ovcWfuOQ5BIM+SKobZtP3IkpzD3TpL7TC2naWOuz ec9x8OWoxVdePjvP/9jhUDf8uZ+SVGtKWg08qBtMGKJweySYoEb0LDm6teaF/3MNtPRh Ek08dnouA2O4hnxhcRhykLIWk7STiXHK1U1mzBdgbf4i+HA2acY29QcDcGfbs0vPeP/f dkoAr1VGfbG9qUhwf5m8IdZD1acc7mcY6cIcjoIZf3KMrT0zUCrW8ZkIH7QGB0qlUrBc 9m0g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=AV3i0cyAQqmvIdnehCa8/+iZ0ckotya/+iLpSfcFxfw=; b=aKjg0wNfIU7/q71Xqmws3L/znl8CkwRR6CyyvAIneIsuDcn1bX0HghhglM7jo0XKD8 kux3Xh4497KjrcLBEQPo9gwXM9WAeReYqeqNTh/Gk36P2odez+kVOKGz/IYfaqIvx9ma qBoUM+alqKSBOCCNwJCTPevGTuIALPZTskRmSVzC06ggzP4pAhnUxyQ78Q3ezrFGYnu0 WrvXy1XWQITtz7EPjSoW3nylrdzEVM/edcDv5NkdbrSBl/uNSZrD185HRgigtHCznwvp FCeHTyW4wGvv/ShWZL+e1RapGxLLbmM+U86DQgGspRiikoYMS+mDnH76GdFsa0Il9x4c aqfA== X-Gm-Message-State: AOAM5313NW7OTcztLbGkDxSfsQrQ9A1ZoJzEHr2SJ04+oKxNAqvtgpJX r9UMO9OFLUq9l3H3/2NpIFg= X-Google-Smtp-Source: ABdhPJz/1gwa0jMAM7gqj89ueqtaO6w7py+D4zn2NNdpc1Wc72w9aik4ABCiNVyzh1RScVMA2wclMA== X-Received: by 2002:a17:902:aa46:b029:ef:8a47:ca53 with SMTP id c6-20020a170902aa46b02900ef8a47ca53mr5391879plr.74.1620887294619; Wed, 12 May 2021 23:28:14 -0700 (PDT) Received: from hyeyoo ([121.135.181.35]) by smtp.gmail.com with ESMTPSA id x22sm1270767pfa.24.2021.05.12.23.28.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 12 May 2021 23:28:14 -0700 (PDT) Date: Thu, 13 May 2021 15:28:09 +0900 From: Hyeonggon Yoo <42.hyeyoo@gmail.com> To: Andrew Morton Cc: vbabka@suse.cz, iamjoonsoo.kim@lge.com, rientjes@google.com, penberg@kernel.org, cl@linux.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org, Marco Elver , 42.hyeyoo@gmail.com Subject: Re: [PATCH v3] mm, slub: change run-time assertion in kmalloc_index() to compile-time Message-ID: <20210513062809.GA319973@hyeyoo> References: <20210511173448.GA54466@hyeyoo> <20210512195227.245000695c9014242e9a00e5@linux-foundation.org> <20210513031220.GA133011@hyeyoo> <20210512204024.401ff3de38649d7d0f5a45e8@linux-foundation.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20210512204024.401ff3de38649d7d0f5a45e8@linux-foundation.org> Authentication-Results: imf05.hostedemail.com; dkim=pass header.d=gmail.com header.s=20161025 header.b="OvcxGH/M"; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf05.hostedemail.com: domain of 42hyeyoo@gmail.com designates 209.85.214.169 as permitted sender) smtp.mailfrom=42hyeyoo@gmail.com X-Rspamd-Server: rspam05 X-Rspamd-Queue-Id: 3DFE5E000113 X-Stat-Signature: if9q8g54f3mmmu3xudkuus4toh5u771e Received-SPF: none (gmail.com>: No applicable sender policy available) receiver=imf05; identity=mailfrom; envelope-from="<42.hyeyoo@gmail.com>"; helo=mail-pl1-f169.google.com; client-ip=209.85.214.169 X-HE-DKIM-Result: pass/pass X-HE-Tag: 1620887289-672101 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: On Wed, May 12, 2021 at 08:40:24PM -0700, Andrew Morton wrote: > On Thu, 13 May 2021 12:12:20 +0900 Hyeonggon Yoo <42.hyeyoo@gmail.com> wrote: > > > On Wed, May 12, 2021 at 07:52:27PM -0700, Andrew Morton wrote: > > > This explodes in mysterious ways. The patch as I have it is appended, > > > for reference. > > > > > > gcc-10.3.0 allmodconfig. > > > > > > This patch suppresses the error: > > Ah, yes, of course, your patch changes kmalloc_index() to require that > it always is called with a constant `size'. kfence_test doesn't do > that. > > kfence is being a bit naughty here - the other kmalloc_index() callers > only comple up the call after verifying that `size' is a compile-time > constant. > > Would something like this work? > include/linux/slab.h | 12 ++++++++---- > mm/kfence/kfence_test.c | 4 ++-- > 2 files changed, 10 insertions(+), 6 deletions(-) > > --- a/include/linux/slab.h~b > +++ a/include/linux/slab.h > @@ -374,7 +374,8 @@ static __always_inline enum kmalloc_cach > * Note: there's no need to optimize kmalloc_index because it's evaluated > * in compile-time. > */ > -static __always_inline unsigned int kmalloc_index(size_t size) > +static __always_inline unsigned int kmalloc_index(size_t size, > + bool size_is_constant) > { > if (!size) > return 0; > @@ -410,7 +411,10 @@ static __always_inline unsigned int kmal > if (size <= 16 * 1024 * 1024) return 24; > if (size <= 32 * 1024 * 1024) return 25; > > - BUILD_BUG_ON_MSG(1, "unexpected size in kmalloc_index()"); > + if (size_is_constant) > + BUILD_BUG_ON_MSG(1, "unexpected size in kmalloc_index()"); > + else > + BUG(); kfence is randomly generating size. because kfence is using non-constant size, we should do run-time assertion or compile-time assertion depending on situation. I think we can use __builtin_constant_p here. we don't need to modify kmalloc_index's prototype. so what about this? if you think it makes sense, I'll send patch v4. I used KMALLOC_MAX_CACHE_SIZE to assure it's safe size. it's safer than putting BUILD_BUG_ON_MSG(1, ...) to below if statements because KMALLOC_MAX_CACHE_SIZE can be less than 32MB. --- include/linux/slab.h.orig 2021-05-12 17:56:54.504738768 +0900 +++ include/linux/slab.h 2021-05-13 15:06:25.724565850 +0900 @@ -346,9 +346,18 @@ static __always_inline enum kmalloc_cach * 1 = 65 .. 96 bytes * 2 = 129 .. 192 bytes * n = 2^(n-1)+1 .. 2^n + * + * Note: there's no need to optimize kmalloc_index because it's evaluated + * in compile-time. */ static __always_inline unsigned int kmalloc_index(size_t size) { + if (__builtin_constant_p(size)) { + BUILD_BUG_ON_MSG(size > KMALLOC_MAX_CACHE_SIZE , "unexpected size in kmalloc_index()"); + } else if (size > KMALLOC_MAX_CACHE_SIZE) { + BUG(); + } + if (!size) return 0; @@ -382,8 +391,6 @@ static __always_inline unsigned int kmal if (size <= 8 * 1024 * 1024) return 23; if (size <= 16 * 1024 * 1024) return 24; if (size <= 32 * 1024 * 1024) return 25; - if (size <= 64 * 1024 * 1024) return 26; - BUG(); /* Will never be reached. Needed because the compiler may complain */ return -1;