From patchwork Tue Mar 2 03:39:08 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yejune Deng X-Patchwork-Id: 12110909 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=-13.5 required=3.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED,DKIM_INVALID,DKIM_SIGNED,FREEMAIL_FORGED_FROMDOMAIN, FREEMAIL_FROM,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT 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 607EBC433DB for ; Tue, 2 Mar 2021 03:39:19 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id DDA5064D9C for ; Tue, 2 Mar 2021 03:39:18 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org DDA5064D9C 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 56F748D00D1; Mon, 1 Mar 2021 22:39:18 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 51FBA8D0063; Mon, 1 Mar 2021 22:39:18 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 4369F8D00D1; Mon, 1 Mar 2021 22:39:18 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0243.hostedemail.com [216.40.44.243]) by kanga.kvack.org (Postfix) with ESMTP id 2C9A38D0063 for ; Mon, 1 Mar 2021 22:39:18 -0500 (EST) Received: from smtpin20.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay05.hostedemail.com (Postfix) with ESMTP id D7603181AF5D0 for ; Tue, 2 Mar 2021 03:39:17 +0000 (UTC) X-FDA: 77873528754.20.4A288B5 Received: from mail-pl1-f176.google.com (mail-pl1-f176.google.com [209.85.214.176]) by imf25.hostedemail.com (Postfix) with ESMTP id 855B2600249F for ; Tue, 2 Mar 2021 03:39:16 +0000 (UTC) Received: by mail-pl1-f176.google.com with SMTP id d11so11210453plo.8 for ; Mon, 01 Mar 2021 19:39:17 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=hqISnWngL+yQot4HH6Rw8UIVFO0OHNLi2++GZbUw8x0=; b=aMghwdOS3Go0j4qpk4OnvKE5BOndXBfRD0V41XsskYioATt09VpvPPcks+FdVK16pk KulRMIOMb/yCU9Jdd+P3JBF9jabJpgt6qm7NOkdPpvQMO8nTbd1oJU06Qtz2VIODwfbj TZ0X5IPFz4OYA4zzpBwJnjV2cv3BeOLuSwJlQWDa0SW8LQggyDmrghtSGWkfc14gJ3UA DYvLWFUcA+keAzvaZaUx9qt50MtEYPlUg10MgxqmPPbSHbb9mJcQFuC91hjYjPH5a9MO AjJjeHkuGw9GR+24wbPO3PEgnJ+l7weFF0xn38OwIutgH1JxuREPY4VyWkkjwziZ1YH0 io8Q== 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:mime-version :content-transfer-encoding; bh=hqISnWngL+yQot4HH6Rw8UIVFO0OHNLi2++GZbUw8x0=; b=W7Nh26MwU/6oLwC2QLCYDnjcql+BzXVk5RO/88W/defXYLJGTWwsnyYuPIt0iLY+gm jlc9m54RNq5eirTVFGqUcQ7lMmAjVv/2Bwnd7rFAhqx8RuN+Qw0jnZiS6cBYYVrJRErG dhbj8gwIHYkMdzDTSnUcTdsCnJ64prRSZE63G2yoFSB8jp3UdAqa2KbMKhTBVZkw8xdq ub9bXComIk7XlvtLULTOgZ2S70YRmVnlf14VUJsfO3MGy/6uYEPBLRACaQs0H/qyCHZV n7PCbcIkRXXrcUal4QZgOi901HKtqNejBcJTjTKFYjKh3uDD6Na2A6CjOA8fLk1gxpiP SMzw== X-Gm-Message-State: AOAM533AMu0hz5n9XFU02JKJqnrf1xts1yA5vIrtjlLw2aWzbsQoaD+Q j7WCUyV1i1sejZ4itqglrvA= X-Google-Smtp-Source: ABdhPJxyBAbKjQr0x2APdr0DOIyqRnTN9IgEeNa2mU5134gItPqpC4gYjWU3BtzHgAd+hAuEQbmZOQ== X-Received: by 2002:a17:902:e8d4:b029:e4:a950:c832 with SMTP id v20-20020a170902e8d4b02900e4a950c832mr1747714plg.3.1614656356447; Mon, 01 Mar 2021 19:39:16 -0800 (PST) Received: from localhost.localdomain ([122.10.161.207]) by smtp.gmail.com with ESMTPSA id c30sm1556589pgn.88.2021.03.01.19.39.14 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Mon, 01 Mar 2021 19:39:16 -0800 (PST) From: Yejune Deng To: cl@linux.com, penberg@kernel.org, rientjes@google.com, iamjoonsoo.kim@lge.com, akpm@linux-foundation.org, vbabka@suse.cz Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, yejune.deng@gmail.com Subject: [PATCH] include/linux/slab.h: use for() and left shift to calculate Date: Tue, 2 Mar 2021 11:39:08 +0800 Message-Id: <20210302033908.51706-1-yejune.deng@gmail.com> X-Mailer: git-send-email 2.29.0 MIME-Version: 1.0 X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: 855B2600249F X-Stat-Signature: oe4d9c9ycc3ejy3kc9ro4eai43p5ofmb Received-SPF: none (gmail.com>: No applicable sender policy available) receiver=imf25; identity=mailfrom; envelope-from=""; helo=mail-pl1-f176.google.com; client-ip=209.85.214.176 X-HE-DKIM-Result: pass/pass X-HE-Tag: 1614656356-424926 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: use for() and left shift to calculate the value that compared with size. Signed-off-by: Yejune Deng --- include/linux/slab.h | 32 ++++++++------------------------ 1 file changed, 8 insertions(+), 24 deletions(-) diff --git a/include/linux/slab.h b/include/linux/slab.h index 7ae604076767..0411f57930fb 100644 --- a/include/linux/slab.h +++ b/include/linux/slab.h @@ -347,6 +347,8 @@ static __always_inline enum kmalloc_cache_type kmalloc_type(gfp_t flags) */ static __always_inline unsigned int kmalloc_index(size_t size) { + int i; + if (!size) return 0; @@ -357,30 +359,12 @@ static __always_inline unsigned int kmalloc_index(size_t size) return 1; if (KMALLOC_MIN_SIZE <= 64 && size > 128 && size <= 192) return 2; - if (size <= 8) return 3; - if (size <= 16) return 4; - if (size <= 32) return 5; - if (size <= 64) return 6; - if (size <= 128) return 7; - if (size <= 256) return 8; - if (size <= 512) return 9; - if (size <= 1024) return 10; - if (size <= 2 * 1024) return 11; - if (size <= 4 * 1024) return 12; - if (size <= 8 * 1024) return 13; - if (size <= 16 * 1024) return 14; - if (size <= 32 * 1024) return 15; - if (size <= 64 * 1024) return 16; - if (size <= 128 * 1024) return 17; - if (size <= 256 * 1024) return 18; - if (size <= 512 * 1024) return 19; - if (size <= 1024 * 1024) return 20; - if (size <= 2 * 1024 * 1024) return 21; - if (size <= 4 * 1024 * 1024) return 22; - 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; + + /* The maximum of size is 2^26 = 64 * 1024 * 1024 */ + for (i = 3; i <= 26; i++) { + if (size <= (1 << i)) + return i; + } BUG(); /* Will never be reached. Needed because the compiler may complain */