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=-18.3 required=3.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_IN_DEF_DKIM_WL 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 17C05C433B4 for ; Thu, 13 May 2021 12:29:39 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id EE05B613C5 for ; Thu, 13 May 2021 12:29:38 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233858AbhEMMao (ORCPT ); Thu, 13 May 2021 08:30:44 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44924 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233305AbhEMMah (ORCPT ); Thu, 13 May 2021 08:30:37 -0400 Received: from mail-ot1-x331.google.com (mail-ot1-x331.google.com [IPv6:2607:f8b0:4864:20::331]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0A97FC061574 for ; Thu, 13 May 2021 05:29:26 -0700 (PDT) Received: by mail-ot1-x331.google.com with SMTP id u19-20020a0568302493b02902d61b0d29adso22649651ots.10 for ; Thu, 13 May 2021 05:29:26 -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=+ezfSFfJxSbieIos+EnfafkeKIzxk7khtzg+M9RSJd8=; b=sAcBuSmpbS+D8fp3Ei+oMaQjsQjQNu0BkNztT7iN1crix8HzuIh591hLpJjTikEA/q Dcti+KYeyftxmGmDQBIi0UZM6U6Ul777Uh//0pRa24iQBOeZcCQzr2dsSczINxewDKWr j+FyKybKFwejM6NSknzKgLelnIcBpui7MHSjGSt2rabrKgj9yUwnlbNjrUUUMkCF87zL 4JwLtSzOhMDd++lQUUzwXMnm//wsU+B0XXWQ/dSp2s5YQHv0M6xuON3hXDhLJ2G3D6oZ Gzt7pS+T0F54Pt5r9zapQ55w0XxgvXisPyKJDzab9YscaWVxepHuns2GVgRI6fCW1CUZ 73yg== 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=+ezfSFfJxSbieIos+EnfafkeKIzxk7khtzg+M9RSJd8=; b=OWgiZ1h9K27Sbf3GoARNQPvS4y2ZjkYKyiTyofq78FJ0vq9u8JxA74IgxAtk0Psi3B XWOBjZ5CZlTfMgKYz9lHoXLQEOwK95Jdw8KOKUPDITJaZnPqCaqVo+CwMOIBZc6gAzb0 KDxV12fZ65c4nbhjH3BcCUW0EbXn2cOnm3bev9L8B6QKXuhDz/R+eX84dAh6lwegN59U LydgHyGBoLGW32tvyU+QsBJc4qKuDwQo9m9H7LiMGwKoZmWaRebDk79meJ9n3Vo+HFah 6yBj5JxRfD1kApP9J5GFgiXiqzKdXSUogZvQjEIOT+w8D3msbiFcutMtF3CiJXIiPkxR G6bw== X-Gm-Message-State: AOAM532JDytp5XFJgHGn9jbcgAJmgVq9cr3kjhPEaYO3emOg6/8o1LTv 920Yk2oM4oPmj/nJuZ0v2aKM7/M0CD3ZoPvvA/lRDQ== X-Google-Smtp-Source: ABdhPJxwfJ0VI3VMKc0QUe+/3wr9AdpfCaTuLaJ8HM4mJxTqRiy4fBg7C/PMhd3TFygFaSFrQ/99rSn1iMs3EmDVhJY= X-Received: by 2002:a9d:1ea9:: with SMTP id n38mr36276163otn.233.1620908965175; Thu, 13 May 2021 05:29:25 -0700 (PDT) MIME-Version: 1.0 References: <20210511173448.GA54466@hyeyoo> <20210512195227.245000695c9014242e9a00e5@linux-foundation.org> <20210513031220.GA133011@hyeyoo> <20210512204024.401ff3de38649d7d0f5a45e8@linux-foundation.org> <20210513062809.GA319973@hyeyoo> <20210513120339.GA772931@hyeyoo> In-Reply-To: <20210513120339.GA772931@hyeyoo> From: Marco Elver Date: Thu, 13 May 2021 14:29:13 +0200 Message-ID: Subject: Re: [PATCH v3] mm, slub: change run-time assertion in kmalloc_index() to compile-time To: Hyeonggon Yoo <42.hyeyoo@gmail.com> Cc: Vlastimil Babka , Andrew Morton , Joonsoo Kim , David Rientjes , Pekka Enberg , Christoph Lameter , Linux Memory Management List , LKML Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, 13 May 2021 at 14:03, Hyeonggon Yoo <42.hyeyoo@gmail.com> wrote: > On Thu, May 13, 2021 at 12:31:38PM +0200, Marco Elver wrote: [...] > what about checking size it on top of kmalloc_index? because by definition of > KMALLOC_SHIFT_HIGH, it's not always 25. it can be less than 25. for some > situations. > > below is what I suggested beofre. for just reference: This doesn't solve the problem. We want the compiler to complain whenever kmalloc_index() is used with non-constant in normal code. But it should be possible to use it in allocator tests regardless of size. Either that or export kmalloc_slab(), but I think that's worse. I'll send my patch with an updated comment. > --- 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; 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=-18.3 required=3.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_IN_DEF_DKIM_WL autolearn=ham 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 31AF1C433B4 for ; Thu, 13 May 2021 12:29:28 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 9B40B613BE for ; Thu, 13 May 2021 12:29:27 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 9B40B613BE Authentication-Results: mail.kernel.org; dmarc=fail (p=reject dis=none) header.from=google.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 306A76B006E; Thu, 13 May 2021 08:29:27 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 2DA806B0070; Thu, 13 May 2021 08:29:27 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 092C26B0072; Thu, 13 May 2021 08:29:27 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0212.hostedemail.com [216.40.44.212]) by kanga.kvack.org (Postfix) with ESMTP id CCE126B006E for ; Thu, 13 May 2021 08:29:26 -0400 (EDT) Received: from smtpin06.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay03.hostedemail.com (Postfix) with ESMTP id 705F3824999B for ; Thu, 13 May 2021 12:29:26 +0000 (UTC) X-FDA: 78136138332.06.95269A3 Received: from mail-ot1-f50.google.com (mail-ot1-f50.google.com [209.85.210.50]) by imf16.hostedemail.com (Postfix) with ESMTP id 49E0A80193A8 for ; Thu, 13 May 2021 12:29:13 +0000 (UTC) Received: by mail-ot1-f50.google.com with SMTP id r26-20020a056830121ab02902a5ff1c9b81so23429626otp.11 for ; Thu, 13 May 2021 05:29:25 -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=+ezfSFfJxSbieIos+EnfafkeKIzxk7khtzg+M9RSJd8=; b=sAcBuSmpbS+D8fp3Ei+oMaQjsQjQNu0BkNztT7iN1crix8HzuIh591hLpJjTikEA/q Dcti+KYeyftxmGmDQBIi0UZM6U6Ul777Uh//0pRa24iQBOeZcCQzr2dsSczINxewDKWr j+FyKybKFwejM6NSknzKgLelnIcBpui7MHSjGSt2rabrKgj9yUwnlbNjrUUUMkCF87zL 4JwLtSzOhMDd++lQUUzwXMnm//wsU+B0XXWQ/dSp2s5YQHv0M6xuON3hXDhLJ2G3D6oZ Gzt7pS+T0F54Pt5r9zapQ55w0XxgvXisPyKJDzab9YscaWVxepHuns2GVgRI6fCW1CUZ 73yg== 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=+ezfSFfJxSbieIos+EnfafkeKIzxk7khtzg+M9RSJd8=; b=mEgWlRri70fDliKZP/PuxAIgA/hkk92EZS+e57m9FBC+bUSHfeNZiWdQsgBppS2QoR zFaTiFQT58PTuFmD97VU00jDZS9T3mccO+zPDRKmrrpbQKiMA2ysghbPvSZ7VCTNUONi ts/Ki/TU/ui3o9UvHS5dsxnSSnCHMEGYfCPCZhHkIMUDNibLSJBcCvTX+nOae65E6gXB nei+cOTPPxDkib6pnw1Rr77OXE+UOLBWGKUBFW+U2PFV+JPUlWf3u2o4fpsaedOUEZY2 wKEEQe1M2gc/KRp6kmsOjfwgPRszwS3ijyKv+IKNOHZDA2s7+5ci8QHw4cLj9ykCeTSp VW8A== X-Gm-Message-State: AOAM532ehl3uPu8SfMsSWzHw+Ad5zq7FAyeYmfFEqncMYjzIIcZ7s1KM 2iSXTgREanuzL2VBrsplUixDKIZ11SdHKx0uAFkreg== X-Google-Smtp-Source: ABdhPJxwfJ0VI3VMKc0QUe+/3wr9AdpfCaTuLaJ8HM4mJxTqRiy4fBg7C/PMhd3TFygFaSFrQ/99rSn1iMs3EmDVhJY= X-Received: by 2002:a9d:1ea9:: with SMTP id n38mr36276163otn.233.1620908965175; Thu, 13 May 2021 05:29:25 -0700 (PDT) MIME-Version: 1.0 References: <20210511173448.GA54466@hyeyoo> <20210512195227.245000695c9014242e9a00e5@linux-foundation.org> <20210513031220.GA133011@hyeyoo> <20210512204024.401ff3de38649d7d0f5a45e8@linux-foundation.org> <20210513062809.GA319973@hyeyoo> <20210513120339.GA772931@hyeyoo> In-Reply-To: <20210513120339.GA772931@hyeyoo> From: Marco Elver Date: Thu, 13 May 2021 14:29:13 +0200 Message-ID: Subject: Re: [PATCH v3] mm, slub: change run-time assertion in kmalloc_index() to compile-time To: Hyeonggon Yoo <42.hyeyoo@gmail.com> Cc: Vlastimil Babka , Andrew Morton , Joonsoo Kim , David Rientjes , Pekka Enberg , Christoph Lameter , Linux Memory Management List , LKML Content-Type: text/plain; charset="UTF-8" Authentication-Results: imf16.hostedemail.com; dkim=pass header.d=google.com header.s=20161025 header.b=sAcBuSmp; spf=pass (imf16.hostedemail.com: domain of elver@google.com designates 209.85.210.50 as permitted sender) smtp.mailfrom=elver@google.com; dmarc=pass (policy=reject) header.from=google.com X-Rspamd-Server: rspam01 X-Rspamd-Queue-Id: 49E0A80193A8 X-Stat-Signature: fnsjxtocgs4uzdf78s3k54afhd5m1jc9 Received-SPF: none (google.com>: No applicable sender policy available) receiver=imf16; identity=mailfrom; envelope-from=""; helo=mail-ot1-f50.google.com; client-ip=209.85.210.50 X-HE-DKIM-Result: pass/pass X-HE-Tag: 1620908953-465331 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 Thu, 13 May 2021 at 14:03, Hyeonggon Yoo <42.hyeyoo@gmail.com> wrote: > On Thu, May 13, 2021 at 12:31:38PM +0200, Marco Elver wrote: [...] > what about checking size it on top of kmalloc_index? because by definition of > KMALLOC_SHIFT_HIGH, it's not always 25. it can be less than 25. for some > situations. > > below is what I suggested beofre. for just reference: This doesn't solve the problem. We want the compiler to complain whenever kmalloc_index() is used with non-constant in normal code. But it should be possible to use it in allocator tests regardless of size. Either that or export kmalloc_slab(), but I think that's worse. I'll send my patch with an updated comment. > --- 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;