From patchwork Thu Nov 23 13:30:36 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Gang Li X-Patchwork-Id: 168933 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:ce62:0:b0:403:3b70:6f57 with SMTP id o2csp450418vqx; Thu, 23 Nov 2023 05:37:09 -0800 (PST) X-Google-Smtp-Source: AGHT+IEKEXkh6LY5PaY+1WOjBLSd1eJpVPPAit6cEty82TcQIXYUpcxji7gIA2ESQdwtAPm5KxmG X-Received: by 2002:a05:6a20:441a:b0:18b:30d1:fcab with SMTP id ce26-20020a056a20441a00b0018b30d1fcabmr6130289pzb.59.1700746629198; Thu, 23 Nov 2023 05:37:09 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1700746629; cv=none; d=google.com; s=arc-20160816; b=es9Cw8OXVVj0eXs3d/EobGb+XS3dGvvqE+U502EyV8ncvzvmL/5Gdj7RqDUjRiuf2c 6jIWKXopIj4NVIYp3SkQuhw+CwNJQbv9LR2Oy6XCPetXh4UwB3NS9b7joCYfdVz6razF awAx00g0B+n/zNzHRAxnJ3iznfByKhIbUlvQ2iEw/SJybtcFL0DAIweb0hDcC1Gd8wdk nqEPogl4HtmL7BqNu4xH64cBmqmhcvrVBayOlS9geN2zpu+7/jqKpplsMLAeLv1tX+QI KEqCyQ1EkW9AW3OHEAnzNW13qDd9BqPlhqV0ugZtZuhhncBP1OETJq/Zn/tWPDWWV3/Y 9GdA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=SVmsj9TN/aj38n19fqlW2b7zQkW3VvgWG56LLSCGces=; fh=Mfp6tXIfhm0wKwJBfrCMEq/ET4PmqmqmqGe1ZOy3t1A=; b=JJbUCsqzNs7Nh544jJIu2NXmmGLLlLVftKslulDrWpefkrdzO5cxPdPWkVmtMKOPuF s2gfP3VDiiHd79mR/ykcKPowLArsyHhPhg+DBTRWKHy3L9UlMnxCiezbS7JEnPxkBpJC 4GliiLihQCiJLCFx19bb2pWxwP8cmV3nA8MWg+EjP10XGZboh96i42/4X9J2zurV8ctm dYrTI+rz0tAW+gjuuBSPRkBDxy05gSt8KI7HwVQgQKiQ7zJzEDW6UJD2C8KCK9+ey7aY DfmaEnRD6ItqokeD19aatR5HcRtgEvxDHD4fJxidUeElSSf62lbGYBnyRZVbAhetyr2f OVPA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linux.dev header.s=key1 header.b=Y71JssED; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.35 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.dev Received: from groat.vger.email (groat.vger.email. [23.128.96.35]) by mx.google.com with ESMTPS id z24-20020a17090acb1800b00285479be838si1291842pjt.179.2023.11.23.05.37.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 23 Nov 2023 05:37:09 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.35 as permitted sender) client-ip=23.128.96.35; Authentication-Results: mx.google.com; dkim=pass header.i=@linux.dev header.s=key1 header.b=Y71JssED; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.35 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.dev Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by groat.vger.email (Postfix) with ESMTP id 119BA8329A28; Thu, 23 Nov 2023 05:36:36 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at groat.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1345509AbjKWNg1 (ORCPT + 99 others); Thu, 23 Nov 2023 08:36:27 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47672 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1345468AbjKWNg0 (ORCPT ); Thu, 23 Nov 2023 08:36:26 -0500 X-Greylist: delayed 326 seconds by postgrey-1.37 at lindbergh.monkeyblade.net; Thu, 23 Nov 2023 05:36:31 PST Received: from out-186.mta0.migadu.com (out-186.mta0.migadu.com [IPv6:2001:41d0:1004:224b::ba]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A04B81A8 for ; Thu, 23 Nov 2023 05:36:31 -0800 (PST) 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=1700746275; 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=SVmsj9TN/aj38n19fqlW2b7zQkW3VvgWG56LLSCGces=; b=Y71JssEDzrYWYNV34cyzjaSirgHK0BrpKRWoKYtD+NlDt+B0BHOaS3CWA9Uyhz5yKbgj6g PimBt6l7h2P4kLFfkV0n8MPPjcmR9a/NNorQu2gY5FzktxNGVTBcPtxXi8OV6XSHGDx82i xNn/ddoatbelzCFAmHSUzrB3UJs23Jc= From: Gang Li To: Mike Kravetz , Muchun Song , Andrew Morton Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, Gang Li Subject: [RFC PATCH v1 4/4] hugetlb: parallelize hugetlb page allocation Date: Thu, 23 Nov 2023 21:30:36 +0800 Message-Id: <20231123133036.68540-5-gang.li@linux.dev> In-Reply-To: <20231123133036.68540-1-gang.li@linux.dev> References: <20231123133036.68540-1-gang.li@linux.dev> MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on groat.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (groat.vger.email [0.0.0.0]); Thu, 23 Nov 2023 05:36:36 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1783362097482201422 X-GMAIL-MSGID: 1783362097482201422 From: Gang Li By distributing the allocation across threads, large hugetlb configurations can allocate pages faster, improving boot speed. Signed-off-by: Gang Li --- mm/hugetlb.c | 89 +++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 74 insertions(+), 15 deletions(-) diff --git a/mm/hugetlb.c b/mm/hugetlb.c index ac8558724cc2..df3fbe95989e 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -3509,6 +3509,55 @@ static void __init hugetlb_hstate_alloc_pages_report(unsigned long allocated, st } } +struct hugetlb_work { + struct work_struct work; + struct hstate *h; + int num; + int nid; +}; + +static atomic_t hugetlb_hstate_alloc_n_undone __initdata; +static __initdata DECLARE_COMPLETION(hugetlb_hstate_alloc_comp); + +static void __init hugetlb_alloc_node(struct work_struct *w) +{ + struct hugetlb_work *hw = container_of(w, struct hugetlb_work, work); + struct hstate *h = hw->h; + int i, num = hw->num; + nodemask_t node_alloc_noretry; + unsigned long flags; + + /* Bit mask controlling how hard we retry per-node allocations.*/ + nodes_clear(node_alloc_noretry); + + for (i = 0; i < num; ++i) { + struct folio *folio = alloc_pool_huge_folio(h, &node_states[N_MEMORY], + &node_alloc_noretry); + if (!folio) + break; + spin_lock_irqsave(&hugetlb_lock, flags); + __prep_account_new_huge_page(h, folio_nid(folio)); + enqueue_hugetlb_folio(h, folio); + spin_unlock_irqrestore(&hugetlb_lock, flags); + cond_resched(); + } + + if (atomic_dec_and_test(&hugetlb_hstate_alloc_n_undone)) + complete(&hugetlb_hstate_alloc_comp); +} + +static void __init hugetlb_vmemmap_optimize_node(struct work_struct *w) +{ + struct hugetlb_work *hw = container_of(w, struct hugetlb_work, work); + struct hstate *h = hw->h; + int nid = hw->nid; + + hugetlb_vmemmap_optimize_folios(h, &h->hugepage_freelists[nid]); + + if (atomic_dec_and_test(&hugetlb_hstate_alloc_n_undone)) + complete(&hugetlb_hstate_alloc_comp); +} + static unsigned long __init hugetlb_hstate_alloc_pages_gigantic(struct hstate *h) { unsigned long i; @@ -3528,26 +3577,36 @@ static unsigned long __init hugetlb_hstate_alloc_pages_gigantic(struct hstate *h static unsigned long __init hugetlb_hstate_alloc_pages_non_gigantic(struct hstate *h) { - unsigned long i; - struct folio *folio; - LIST_HEAD(folio_list); - nodemask_t node_alloc_noretry; + int nid; + struct hugetlb_work *works; - /* Bit mask controlling how hard we retry per-node allocations.*/ - nodes_clear(node_alloc_noretry); + works = kcalloc(num_node_state(N_MEMORY), sizeof(*works), GFP_KERNEL); + if (works == NULL) { + pr_warn("HugeTLB: allocating struct hugetlb_work failed.\n"); + return 0; + } - for (i = 0; i < h->max_huge_pages; ++i) { - folio = alloc_pool_huge_folio(h, &node_states[N_MEMORY], - &node_alloc_noretry); - if (!folio) - break; - list_add(&folio->lru, &folio_list); - cond_resched(); + atomic_set(&hugetlb_hstate_alloc_n_undone, num_node_state(N_MEMORY)); + for_each_node_state(nid, N_MEMORY) { + works[nid].h = h; + works[nid].num = h->max_huge_pages/num_node_state(N_MEMORY); + if (nid == 0) + works[nid].num += h->max_huge_pages % num_node_state(N_MEMORY); + INIT_WORK(&works[nid].work, hugetlb_alloc_node); + queue_work_node(nid, system_unbound_wq, &works[nid].work); } + wait_for_completion(&hugetlb_hstate_alloc_comp); - prep_and_add_allocated_folios(h, &folio_list); + atomic_set(&hugetlb_hstate_alloc_n_undone, num_node_state(N_MEMORY)); + for_each_node_state(nid, N_MEMORY) { + works[nid].nid = nid; + INIT_WORK(&works[nid].work, hugetlb_vmemmap_optimize_node); + queue_work_node(nid, system_unbound_wq, &works[nid].work); + } + wait_for_completion(&hugetlb_hstate_alloc_comp); - return i; + kfree(works); + return h->nr_huge_pages; } /*