From patchwork Tue Dec 22 14:24:40 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Muchun Song X-Patchwork-Id: 11986773 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.5 required=3.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,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 77F34C433E0 for ; Tue, 22 Dec 2020 14:31:37 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 0FF8E2312E for ; Tue, 22 Dec 2020 14:31:37 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 0FF8E2312E Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=bytedance.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 877106B00A1; Tue, 22 Dec 2020 09:31:36 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 8007C8D000B; Tue, 22 Dec 2020 09:31:36 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 6C6F88D0009; Tue, 22 Dec 2020 09:31:36 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0156.hostedemail.com [216.40.44.156]) by kanga.kvack.org (Postfix) with ESMTP id 5849E6B00A1 for ; Tue, 22 Dec 2020 09:31:36 -0500 (EST) Received: from smtpin03.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay05.hostedemail.com (Postfix) with ESMTP id 08F45181AEF1E for ; Tue, 22 Dec 2020 14:31:36 +0000 (UTC) X-FDA: 77621156592.03.sun17_4f0f55527460 Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin03.hostedemail.com (Postfix) with ESMTP id DA32628A4E9 for ; Tue, 22 Dec 2020 14:31:35 +0000 (UTC) X-HE-Tag: sun17_4f0f55527460 X-Filterd-Recvd-Size: 6057 Received: from mail-pl1-f170.google.com (mail-pl1-f170.google.com [209.85.214.170]) by imf15.hostedemail.com (Postfix) with ESMTP for ; Tue, 22 Dec 2020 14:31:35 +0000 (UTC) Received: by mail-pl1-f170.google.com with SMTP id x12so7505748plr.10 for ; Tue, 22 Dec 2020 06:31:35 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance-com.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=XzS1srauFLdS7N6FaDP6UmV3KONPJLbFcZMdSU+F95w=; b=Lts37J0+3a2+gZxXrxIlmZsq95Q5atD4QwQiIzeJWwob3euyPog2S4lryq1WXf5hyc NCaQeZhkJrmH58b2gZybUS3oJlbFTtdpGM+qce4CtwbeAn1ahp3WzpJ0vTZSu0lfbnmB cVaMG8N4hFI0uw/+NFbL7XR7c2b7ikU7NvzH2zoSIRXHMNG19+dFYQ/qXqF679zcq6VH Bt+6xbRRGxx8/5jjqzJGP6NnwgnytuTrxp7vDe8IJRChjOZ3xZwOvBzaaMgGIkTYTkCm nz1m8RjpiY90rLrUmPsE/sUVmAjMmncCNmWsh8TAhB0CCtu9zW51z3eNF2KZ/wheaEH0 R09w== 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=XzS1srauFLdS7N6FaDP6UmV3KONPJLbFcZMdSU+F95w=; b=eRp7wp3RZA3F0/qszadbf8286cAP+LgGdAxo61ZYpmjrcX6rZv5URV/SXFQrNVq8a9 BlafCDYZEJdCzYL+oUk6exQuwTG9kPY84KkKq8s5CWQdmX0Wu7q9HZV3/VRlG9NfjyXy +BrGzkH+mPSNKTYwcfdprVv7M5Ofbzbx1SG3ZQlTE4qMMOxoaxs6QrPOco3jTRQT0/7M nNXXidfWmo3fqwC55HXbf46mRPagiES3Bl44HtgEY8U7F/mD1vHb5lcbXQf38ezz7Iq2 63T3fDmB/V+4myvB8vwUZpG6z1zVo8wYEzmgXbSTYhPcAgpznfXQKRANyfKAHnd4Yhh0 cW4g== X-Gm-Message-State: AOAM53101On5rEVRyk7xkOp66THKloXQw1hrWuBv8AGwVAV5o+lxxqYN VHdTaYYzM44ecBjaKFBULCjM4Q== X-Google-Smtp-Source: ABdhPJy8DyVGhNGlLTRjXJsqd1MUxY4ju29rgjY632JH4g9tOkY13BOMltdn/B0pI2vP/UgK9jgUcQ== X-Received: by 2002:a17:902:7592:b029:dc:3c87:1c63 with SMTP id j18-20020a1709027592b02900dc3c871c63mr10339125pll.47.1608647494418; Tue, 22 Dec 2020 06:31:34 -0800 (PST) Received: from localhost.bytedance.net ([139.177.225.247]) by smtp.gmail.com with ESMTPSA id a31sm21182088pgb.93.2020.12.22.06.31.23 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Tue, 22 Dec 2020 06:31:33 -0800 (PST) From: Muchun Song To: corbet@lwn.net, mike.kravetz@oracle.com, tglx@linutronix.de, mingo@redhat.com, bp@alien8.de, x86@kernel.org, hpa@zytor.com, dave.hansen@linux.intel.com, luto@kernel.org, peterz@infradead.org, viro@zeniv.linux.org.uk, akpm@linux-foundation.org, paulmck@kernel.org, mchehab+huawei@kernel.org, pawan.kumar.gupta@linux.intel.com, rdunlap@infradead.org, oneukum@suse.com, anshuman.khandual@arm.com, jroedel@suse.de, almasrymina@google.com, rientjes@google.com, willy@infradead.org, osalvador@suse.de, mhocko@suse.com, song.bao.hua@hisilicon.com, david@redhat.com, naoya.horiguchi@nec.com Cc: duanxiongchun@bytedance.com, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, linux-fsdevel@vger.kernel.org, Muchun Song Subject: [PATCH v11 11/11] mm/hugetlb: Optimize the code with the help of the compiler Date: Tue, 22 Dec 2020 22:24:40 +0800 Message-Id: <20201222142440.28930-12-songmuchun@bytedance.com> X-Mailer: git-send-email 2.21.0 (Apple Git-122) In-Reply-To: <20201222142440.28930-1-songmuchun@bytedance.com> References: <20201222142440.28930-1-songmuchun@bytedance.com> MIME-Version: 1.0 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: We cannot optimize if a "struct page" crosses page boundaries. If it is true, we can optimize the code with the help of a compiler. When free_vmemmap_pages_per_hpage() returns zero, most functions are optimized by the compiler. Signed-off-by: Muchun Song --- include/linux/hugetlb.h | 3 ++- mm/hugetlb_vmemmap.c | 7 +++++++ mm/hugetlb_vmemmap.h | 5 +++-- 3 files changed, 12 insertions(+), 3 deletions(-) diff --git a/include/linux/hugetlb.h b/include/linux/hugetlb.h index 7295f6b3d55e..adc17765e0e9 100644 --- a/include/linux/hugetlb.h +++ b/include/linux/hugetlb.h @@ -791,7 +791,8 @@ extern bool hugetlb_free_vmemmap_enabled; static inline bool is_hugetlb_free_vmemmap_enabled(void) { - return hugetlb_free_vmemmap_enabled; + return hugetlb_free_vmemmap_enabled && + is_power_of_2(sizeof(struct page)); } #else static inline bool is_hugetlb_free_vmemmap_enabled(void) diff --git a/mm/hugetlb_vmemmap.c b/mm/hugetlb_vmemmap.c index 6b8f7bb2273e..5ea12c7507a6 100644 --- a/mm/hugetlb_vmemmap.c +++ b/mm/hugetlb_vmemmap.c @@ -250,6 +250,13 @@ void __init hugetlb_vmemmap_init(struct hstate *h) BUILD_BUG_ON(NR_USED_SUBPAGE >= RESERVE_VMEMMAP_SIZE / sizeof(struct page)); + /* + * The compiler can help us to optimize this function to null + * when the size of the struct page is not power of 2. + */ + if (!is_power_of_2(sizeof(struct page))) + return; + if (!hugetlb_free_vmemmap_enabled) return; diff --git a/mm/hugetlb_vmemmap.h b/mm/hugetlb_vmemmap.h index 8fd9ae113dbd..e8de41295d4d 100644 --- a/mm/hugetlb_vmemmap.h +++ b/mm/hugetlb_vmemmap.h @@ -17,11 +17,12 @@ void hugetlb_vmemmap_init(struct hstate *h); /* * How many vmemmap pages associated with a HugeTLB page that can be freed - * to the buddy allocator. + * to the buddy allocator. The checking of the is_power_of_2() aims to let + * the compiler help us optimize the code as much as possible. */ static inline unsigned int free_vmemmap_pages_per_hpage(struct hstate *h) { - return h->nr_free_vmemmap_pages; + return is_power_of_2(sizeof(struct page)) ? h->nr_free_vmemmap_pages : 0; } #else static inline void alloc_huge_page_vmemmap(struct hstate *h, struct page *head)