From patchwork Tue Jan 2 13:12:43 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Gang Li X-Patchwork-Id: 13509012 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 816BDC47074 for ; Tue, 2 Jan 2024 13:13:23 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 14AA18D0009; Tue, 2 Jan 2024 08:13:23 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 0D35F8D0006; Tue, 2 Jan 2024 08:13:23 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id EB6D68D0009; Tue, 2 Jan 2024 08:13:22 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id D534F8D0006 for ; Tue, 2 Jan 2024 08:13:22 -0500 (EST) Received: from smtpin23.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id A67FC807DF for ; Tue, 2 Jan 2024 13:13:22 +0000 (UTC) X-FDA: 81634412244.23.6EDDBFA Received: from out-187.mta1.migadu.com (out-187.mta1.migadu.com [95.215.58.187]) by imf02.hostedemail.com (Postfix) with ESMTP id DEF5C80012 for ; Tue, 2 Jan 2024 13:13:20 +0000 (UTC) Authentication-Results: imf02.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=wXNKNFN5; spf=pass (imf02.hostedemail.com: domain of gang.li@linux.dev designates 95.215.58.187 as permitted sender) smtp.mailfrom=gang.li@linux.dev; dmarc=pass (policy=none) header.from=linux.dev ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1704201201; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=5jUJr4R6zMb6dInOd1l8MHQjoDUi83oQkc19tydUOUM=; b=QrMKw0NUBrvDVy0XD73jQlEaKLATgjtZovFcLfcCUhIQ1CLHQt1SE/0UhVign0dFkjDSE/ fr+WMkvaTVxq6L0fzSkn3uQMEY7ZhrShdXP8wkzc98gXLMLALEbDZQeZf4TvWRsrtfCNWW t4bc4gM9J3aDrsGAPZ0NiWGksemhrkg= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1704201201; a=rsa-sha256; cv=none; b=nuDJ/3J+1vfDtejqxQN0W1KwTo/UjJGpxViMOQb/VM/RXC8uN3GTfv3gZuVawdpW6cMxs5 ziiOds6qla1wFeKYvlKK9PYJu1mrh/QInKS9Lo3i68YwLZCqvSsz5fPg7G17XTufZM47Wz XcV/WvSIbzNLihNPQn82Nx/44tcV8BM= ARC-Authentication-Results: i=1; imf02.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=wXNKNFN5; spf=pass (imf02.hostedemail.com: domain of gang.li@linux.dev designates 95.215.58.187 as permitted sender) smtp.mailfrom=gang.li@linux.dev; dmarc=pass (policy=none) header.from=linux.dev X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1704201199; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=5jUJr4R6zMb6dInOd1l8MHQjoDUi83oQkc19tydUOUM=; b=wXNKNFN5V2QUIIEBJ0cDhA/1XejAaQRMJMF38Uej4HkKHGocMOKkJyR8FJHUYIBh8b/xd0 hARFT4JBoor5YBUOWh0h26mYqSlEI0jo+wKgsAE/hBQ5trdzV5u88ffrxWuDT82wCOF+uR okbyj35zpon6VTuBdHmYdXzMfgwdPIQ= From: Gang Li To: David Hildenbrand , David Rientjes , Mike Kravetz , Muchun Song , Andrew Morton , Tim Chen Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, ligang.bdlg@bytedance.com, Gang Li Subject: [PATCH v3 1/7] hugetlb: code clean for hugetlb_hstate_alloc_pages Date: Tue, 2 Jan 2024 21:12:43 +0800 Message-Id: <20240102131249.76622-2-gang.li@linux.dev> In-Reply-To: <20240102131249.76622-1-gang.li@linux.dev> References: <20240102131249.76622-1-gang.li@linux.dev> MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT X-Rspamd-Queue-Id: DEF5C80012 X-Rspam-User: X-Rspamd-Server: rspam11 X-Stat-Signature: 48orwb678ypj1p3g8ggu81dpgn8amqao X-HE-Tag: 1704201200-955974 X-HE-Meta: U2FsdGVkX1/GUiHt2F7IP10gaCj+B3hlNHQvAanepnZCqECOpfVCNlPYt1l2d9FtaqA8DFcgb0mTrga3JTnOZ6k8ctH9IMx55ThQFTQ9AxafhhWePif+nLoRiydaQxSur5alNJNuMv1oG2i+0k0nSCiytF85zjpo/hZfQpVMUPSdgWPo8C32eXrVDcQa00SkDnh5FCPJkFUUkcrlezifkfEKRCMoHLlSDYkHddkgXRvEoF3GaLubEmEtNAUkhOMnwJQqFWoGA8rNVGcHG3j/inq6s266rFDDVxqpfFf+JdonYyR9nVtE6ThArvubdE+CoKY9QLgQrbROrcWQvKhgnuAB/AcmBI3hKceWWcfWdk1JcL8oYEHBpMBJRw2MNB/lBruRoLtmrLCZw+k2op4QwAgiDQMeUaPaEW1sFoBhQQ/nrNbsoRg9Xy8k4tih96jJMY8tdvll0xC0Zfc91SkV1GJeKPiAxlz2JI21jBzUb3oGWReeLvRcMcsFViiVzYZdNnuJhGr+AtLLpHZ0RiDMXWG8n9qJXbUkKY7wZQewaQqLb6WtY33kTJUqFEDvaOeW7ueNT4ULbZlVM+OwkFii54FlhPN3k9xTYI6GXYhgg8IpXMe8zEgpnH2GYD8v3IcqTQJK9ANjF2YkSZWHTVpEaQIEVwcuhDr0mxXza1UkmmgdPSQTd+Pe83jvFUA2s5bn6aMP+7+urqQTqeJyiHw1F+sHwfWt3eAru5Vx8HCNJsUXdqo8FBtVL+dQwcf0PrmFePz2kvbo+MUUuZo54OHTwOkMaDLUSfGfCs1RSM8qOWNCVOTBjHSafvoaifCRTYoKAB3cA+F/4Jgsag30EBq2AtePBMLFXS6e1ZlPxrCuYF0cwcbkq2w87MaJAXLIczRIvDgRCjj5iXXE3dnLDmoVOJIP8y/UVVrvaGhHspSkOk/Rn9n11bF6jgQ5qlOf+CVbxRb79odyUZBtpEt00xY 0lqxh1fm Lx+GvimXcnqOMoVHWfoDfQ+5iu+F6G4FttTdQgEkg1wZl5D5DbEk3DMUBvQLNNOoy7i1lNRhcyFpXl/8Q40VA6J56ytyBzGvzvaHeeF4po/27GY8M0BKtfTTZXyoj36rzUpTT 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: List-Subscribe: List-Unsubscribe: The readability of `hugetlb_hstate_alloc_pages` is poor. By cleaning the code, its readability can be improved, facilitating future modifications. This patch extracts two functions to reduce the complexity of `hugetlb_hstate_alloc_pages` and has no functional changes. - hugetlb_hstate_alloc_pages_node_specific() to handle iterates through each online node and performs allocation if necessary. - hugetlb_hstate_alloc_pages_report() report error during allocation. And the value of h->max_huge_pages is updated accordingly. Signed-off-by: Gang Li Reviewed-by: Muchun Song Reviewed-by: Tim Chen --- mm/hugetlb.c | 46 +++++++++++++++++++++++++++++----------------- 1 file changed, 29 insertions(+), 17 deletions(-) diff --git a/mm/hugetlb.c b/mm/hugetlb.c index ed1581b670d42..2606135ec55e6 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -3482,6 +3482,33 @@ static void __init hugetlb_hstate_alloc_pages_onenode(struct hstate *h, int nid) h->max_huge_pages_node[nid] = i; } +static bool __init hugetlb_hstate_alloc_pages_node_specific(struct hstate *h) +{ + int i; + bool node_specific_alloc = false; + + for_each_online_node(i) { + if (h->max_huge_pages_node[i] > 0) { + hugetlb_hstate_alloc_pages_onenode(h, i); + node_specific_alloc = true; + } + } + + return node_specific_alloc; +} + +static void __init hugetlb_hstate_alloc_pages_report(unsigned long allocated, struct hstate *h) +{ + if (allocated < h->max_huge_pages) { + char buf[32]; + + string_get_size(huge_page_size(h), 1, STRING_UNITS_2, buf, 32); + pr_warn("HugeTLB: allocating %lu of page size %s failed. Only allocated %lu hugepages.\n", + h->max_huge_pages, buf, allocated); + h->max_huge_pages = allocated; + } +} + /* * NOTE: this routine is called in different contexts for gigantic and * non-gigantic pages. @@ -3499,7 +3526,6 @@ static void __init hugetlb_hstate_alloc_pages(struct hstate *h) struct folio *folio; LIST_HEAD(folio_list); nodemask_t *node_alloc_noretry; - bool node_specific_alloc = false; /* skip gigantic hugepages allocation if hugetlb_cma enabled */ if (hstate_is_gigantic(h) && hugetlb_cma_size) { @@ -3508,14 +3534,7 @@ static void __init hugetlb_hstate_alloc_pages(struct hstate *h) } /* do node specific alloc */ - for_each_online_node(i) { - if (h->max_huge_pages_node[i] > 0) { - hugetlb_hstate_alloc_pages_onenode(h, i); - node_specific_alloc = true; - } - } - - if (node_specific_alloc) + if (hugetlb_hstate_alloc_pages_node_specific(h)) return; /* below will do all node balanced alloc */ @@ -3558,14 +3577,7 @@ static void __init hugetlb_hstate_alloc_pages(struct hstate *h) /* list will be empty if hstate_is_gigantic */ prep_and_add_allocated_folios(h, &folio_list); - if (i < h->max_huge_pages) { - char buf[32]; - - string_get_size(huge_page_size(h), 1, STRING_UNITS_2, buf, 32); - pr_warn("HugeTLB: allocating %lu of page size %s failed. Only allocated %lu hugepages.\n", - h->max_huge_pages, buf, i); - h->max_huge_pages = i; - } + hugetlb_hstate_alloc_pages_report(i, h); kfree(node_alloc_noretry); }