From nobody Sat Feb 7 08:58:41 2026 Received: from mail-yw1-f201.google.com (mail-yw1-f201.google.com [209.85.128.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id EEDD93FBB2 for ; Wed, 14 Aug 2024 03:54:58 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723607700; cv=none; b=XOeIRjhhsk8VYJgo/edRHPCyK641pwCE9YBKiOdp4m6w1aC5ShVKacRIzJbgH7I14USzdyvQpOHZF2hqFmZUxpr1r0SnnS7Dos/pTlDJmsSxpN/SslQaKIPr+mQNxFHSlZ9VVHy4xEDu9sB1t1xf0/X+aZAykM3n6DUQCqAC8fc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723607700; c=relaxed/simple; bh=pUCvHNndpaTv519hWrkis78L9CpJ/pGAHkS248phQmI=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=TR3/D1YPsUZC8Sryc/oQ/gPDlJ3kWP6Vx+1+pmOlV57SSKDs+s+EZSu6t3bAT3hiRX/M3bOcai3typ7DB8HTgo5q+UTk/Cn9y587+XMiyTaoRbdzgN/d9t3lKVrGoi0I2Csg3n8HGs5kX6Xw1YQ9E8zFVJPFzza/V9dEBES5mYk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--yuzhao.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=qG9cDT9L; arc=none smtp.client-ip=209.85.128.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--yuzhao.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="qG9cDT9L" Received: by mail-yw1-f201.google.com with SMTP id 00721157ae682-690404fd230so130355247b3.3 for ; Tue, 13 Aug 2024 20:54:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1723607698; x=1724212498; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=BGqmRlGLO1UPrtqcDwqRzTwlBOD38mCYYr0sMrb4o6s=; b=qG9cDT9LjKSNAngpMSHW+eu0FszoLgWSiFC9vrsOTc2LXTM06ubUKmWDEPuUeikNEz JuA3OmyPSOqyGyUOi1n5ud1jFLyLLvfQCp4NEGySmpRCG+DFiG0JoHIRgCBiqjoe8VTW vporll9areUmZGrSOYq1jagzBa9quFQ9TpLfE1Egg84t0wUMOZHEJsORE8Al+t5LYRS6 JJCg7hoJgxlAkw6oITAZsdR4h1oYyF7jds0vaTSmxBV34usMbiLT8XzuGX94J5VX3XfB PDQ4DJ6L4lA0F4Y3lTm733yq8wILu0jzVa5kNjuPjTxsvSfWEZIK4PdPhnPMAOkxH6ff VqTA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723607698; x=1724212498; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=BGqmRlGLO1UPrtqcDwqRzTwlBOD38mCYYr0sMrb4o6s=; b=SEXAhfmdnjZIY2HYmAtI5BWRZXs8k82LDI/kKiO5SF8I6DD6pw7lkMy2jK/bF1i7Pi neFhC+oX2h0z06+usL9fLkMqgqr4iIfbYXAtHVm+srZ8EpLLRo/vRrocjYSecJrtHOgX 1d2ZzenBqeSg6DvsOFE5l+JucSc50R0BNQZZR3gOVjFYaKPebcWiT9Wvi5CAu8xtCLKv alp3PmSN6MHnNYo7SQoEFnRuBY0gVL3K3xHvB/e3+bQ7QJBESr5IXJdXOtPbvOkuI3To wFrqQvmNascCw5OplgyoXCJCnqZ/c0nDSX6Xi5iQAx3j/NBV6AQ7ih/qOYenTkx/Vwq+ PuuQ== X-Forwarded-Encrypted: i=1; AJvYcCWq0XlaQcO8CO32VULZSbRi7oYkpHjmcyfFWuBB5IUQv9cPDtmeRO253kjFFpF9J4n6FXQvWC/3hEVg0Vw=@vger.kernel.org X-Gm-Message-State: AOJu0YxOdheE0rLo1wGW588rI0YbIrv+m0swRZh5cqbaXdGFBI1K4LaE p7SkSaTMnLna+luW4aJvVz+SLF/nZ+FNnEPY65QOiV/0ncLJiAaIl4mW2ZgNkbDMn2iAmlthkiu RTw== X-Google-Smtp-Source: AGHT+IHF7I7tjKFaxxVwyOf6J4cLNGcmd7dtJ8TUkzqBnzt3BcUh4JMTZUA9JOZpfjEC0zAqGoYWe8MmT6c= X-Received: from yuzhao2.bld.corp.google.com ([2a00:79e0:2e28:6:c8ad:fcc7:f7cf:f2fd]) (user=yuzhao job=sendgmr) by 2002:a05:690c:2f09:b0:689:315b:cbfc with SMTP id 00721157ae682-6ac9b7b1a2fmr44757b3.6.1723607697910; Tue, 13 Aug 2024 20:54:57 -0700 (PDT) Date: Tue, 13 Aug 2024 21:54:49 -0600 In-Reply-To: <20240814035451.773331-1-yuzhao@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240814035451.773331-1-yuzhao@google.com> X-Mailer: git-send-email 2.46.0.76.ge559c4bf1a-goog Message-ID: <20240814035451.773331-2-yuzhao@google.com> Subject: [PATCH mm-unstable v2 1/3] mm/contig_alloc: support __GFP_COMP From: Yu Zhao To: Andrew Morton , Muchun Song Cc: "Matthew Wilcox (Oracle)" , Zi Yan , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Yu Zhao Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Support __GFP_COMP in alloc_contig_range(). When the flag is set, upon success the function returns a large folio prepared by prep_new_page(), rather than a range of order-0 pages prepared by split_free_pages() (which is renamed from split_map_pages()). alloc_contig_range() can be used to allocate folios larger than MAX_PAGE_ORDER, e.g., gigantic hugeTLB folios. So on the free path, free_one_page() needs to handle that by split_large_buddy(). Signed-off-by: Yu Zhao --- include/linux/gfp.h | 23 +++++++++ mm/compaction.c | 41 ++-------------- mm/page_alloc.c | 111 +++++++++++++++++++++++++++++++------------- 3 files changed, 108 insertions(+), 67 deletions(-) diff --git a/include/linux/gfp.h b/include/linux/gfp.h index f53f76e0b17e..59266df56aeb 100644 --- a/include/linux/gfp.h +++ b/include/linux/gfp.h @@ -446,4 +446,27 @@ extern struct page *alloc_contig_pages_noprof(unsigned= long nr_pages, gfp_t gfp_ #endif void free_contig_range(unsigned long pfn, unsigned long nr_pages); =20 +#ifdef CONFIG_CONTIG_ALLOC +static inline struct folio *folio_alloc_gigantic_noprof(int order, gfp_t g= fp, + int nid, nodemask_t *node) +{ + struct page *page; + + if (WARN_ON(!order || !(gfp | __GFP_COMP))) + return NULL; + + page =3D alloc_contig_pages_noprof(1 << order, gfp, nid, node); + + return page ? page_folio(page) : NULL; +} +#else +static inline struct folio *folio_alloc_gigantic_noprof(int order, gfp_t g= fp, + int nid, nodemask_t *node) +{ + return NULL; +} +#endif +/* This should be paired with folio_put() rather than free_contig_range().= */ +#define folio_alloc_gigantic(...) alloc_hooks(folio_alloc_gigantic_noprof(= __VA_ARGS__)) + #endif /* __LINUX_GFP_H */ diff --git a/mm/compaction.c b/mm/compaction.c index eb95e9b435d0..d1041fbce679 100644 --- a/mm/compaction.c +++ b/mm/compaction.c @@ -86,33 +86,6 @@ static struct page *mark_allocated_noprof(struct page *p= age, unsigned int order, } #define mark_allocated(...) alloc_hooks(mark_allocated_noprof(__VA_ARGS__)) =20 -static void split_map_pages(struct list_head *freepages) -{ - unsigned int i, order; - struct page *page, *next; - LIST_HEAD(tmp_list); - - for (order =3D 0; order < NR_PAGE_ORDERS; order++) { - list_for_each_entry_safe(page, next, &freepages[order], lru) { - unsigned int nr_pages; - - list_del(&page->lru); - - nr_pages =3D 1 << order; - - mark_allocated(page, order, __GFP_MOVABLE); - if (order) - split_page(page, order); - - for (i =3D 0; i < nr_pages; i++) { - list_add(&page->lru, &tmp_list); - page++; - } - } - list_splice_init(&tmp_list, &freepages[0]); - } -} - static unsigned long release_free_list(struct list_head *freepages) { int order; @@ -742,11 +715,11 @@ static unsigned long isolate_freepages_block(struct c= ompact_control *cc, * * Non-free pages, invalid PFNs, or zone boundaries within the * [start_pfn, end_pfn) range are considered errors, cause function to - * undo its actions and return zero. + * undo its actions and return zero. cc->freepages[] are empty. * * Otherwise, function returns one-past-the-last PFN of isolated page * (which may be greater then end_pfn if end fell in a middle of - * a free page). + * a free page). cc->freepages[] contain free pages isolated. */ unsigned long isolate_freepages_range(struct compact_control *cc, @@ -754,10 +727,9 @@ isolate_freepages_range(struct compact_control *cc, { unsigned long isolated, pfn, block_start_pfn, block_end_pfn; int order; - struct list_head tmp_freepages[NR_PAGE_ORDERS]; =20 for (order =3D 0; order < NR_PAGE_ORDERS; order++) - INIT_LIST_HEAD(&tmp_freepages[order]); + INIT_LIST_HEAD(&cc->freepages[order]); =20 pfn =3D start_pfn; block_start_pfn =3D pageblock_start_pfn(pfn); @@ -788,7 +760,7 @@ isolate_freepages_range(struct compact_control *cc, break; =20 isolated =3D isolate_freepages_block(cc, &isolate_start_pfn, - block_end_pfn, tmp_freepages, 0, true); + block_end_pfn, cc->freepages, 0, true); =20 /* * In strict mode, isolate_freepages_block() returns 0 if @@ -807,13 +779,10 @@ isolate_freepages_range(struct compact_control *cc, =20 if (pfn < end_pfn) { /* Loop terminated early, cleanup. */ - release_free_list(tmp_freepages); + release_free_list(cc->freepages); return 0; } =20 - /* __isolate_free_page() does not map the pages */ - split_map_pages(tmp_freepages); - /* We don't use freelists for anything. */ return pfn; } diff --git a/mm/page_alloc.c b/mm/page_alloc.c index 5841bbea482a..0a43e4ea29e4 100644 --- a/mm/page_alloc.c +++ b/mm/page_alloc.c @@ -1197,16 +1197,36 @@ static void free_pcppages_bulk(struct zone *zone, i= nt count, spin_unlock_irqrestore(&zone->lock, flags); } =20 +/* Split a multi-block free page into its individual pageblocks. */ +static void split_large_buddy(struct zone *zone, struct page *page, + unsigned long pfn, int order, fpi_t fpi) +{ + unsigned long end =3D pfn + (1 << order); + + VM_WARN_ON_ONCE(!IS_ALIGNED(pfn, 1 << order)); + /* Caller removed page from freelist, buddy info cleared! */ + VM_WARN_ON_ONCE(PageBuddy(page)); + + if (order > pageblock_order) + order =3D pageblock_order; + + while (pfn !=3D end) { + int mt =3D get_pfnblock_migratetype(page, pfn); + + __free_one_page(page, pfn, zone, order, mt, fpi); + pfn +=3D 1 << order; + page =3D pfn_to_page(pfn); + } +} + static void free_one_page(struct zone *zone, struct page *page, unsigned long pfn, unsigned int order, fpi_t fpi_flags) { unsigned long flags; - int migratetype; =20 spin_lock_irqsave(&zone->lock, flags); - migratetype =3D get_pfnblock_migratetype(page, pfn); - __free_one_page(page, pfn, zone, order, migratetype, fpi_flags); + split_large_buddy(zone, page, pfn, order, fpi_flags); spin_unlock_irqrestore(&zone->lock, flags); } =20 @@ -1698,27 +1718,6 @@ static unsigned long find_large_buddy(unsigned long = start_pfn) return start_pfn; } =20 -/* Split a multi-block free page into its individual pageblocks */ -static void split_large_buddy(struct zone *zone, struct page *page, - unsigned long pfn, int order) -{ - unsigned long end_pfn =3D pfn + (1 << order); - - VM_WARN_ON_ONCE(order <=3D pageblock_order); - VM_WARN_ON_ONCE(pfn & (pageblock_nr_pages - 1)); - - /* Caller removed page from freelist, buddy info cleared! */ - VM_WARN_ON_ONCE(PageBuddy(page)); - - while (pfn !=3D end_pfn) { - int mt =3D get_pfnblock_migratetype(page, pfn); - - __free_one_page(page, pfn, zone, pageblock_order, mt, FPI_NONE); - pfn +=3D pageblock_nr_pages; - page =3D pfn_to_page(pfn); - } -} - /** * move_freepages_block_isolate - move free pages in block for page isolat= ion * @zone: the zone @@ -1759,7 +1758,7 @@ bool move_freepages_block_isolate(struct zone *zone, = struct page *page, del_page_from_free_list(buddy, zone, order, get_pfnblock_migratetype(buddy, pfn)); set_pageblock_migratetype(page, migratetype); - split_large_buddy(zone, buddy, pfn, order); + split_large_buddy(zone, buddy, pfn, order, FPI_NONE); return true; } =20 @@ -1770,7 +1769,7 @@ bool move_freepages_block_isolate(struct zone *zone, = struct page *page, del_page_from_free_list(page, zone, order, get_pfnblock_migratetype(page, pfn)); set_pageblock_migratetype(page, migratetype); - split_large_buddy(zone, page, pfn, order); + split_large_buddy(zone, page, pfn, order, FPI_NONE); return true; } move: @@ -6440,6 +6439,31 @@ int __alloc_contig_migrate_range(struct compact_cont= rol *cc, return (ret < 0) ? ret : 0; } =20 +static void split_free_pages(struct list_head *list) +{ + int order; + + for (order =3D 0; order < NR_PAGE_ORDERS; order++) { + struct page *page, *next; + int nr_pages =3D 1 << order; + + list_for_each_entry_safe(page, next, &list[order], lru) { + int i; + + post_alloc_hook(page, order, __GFP_MOVABLE); + if (!order) + continue; + + split_page(page, order); + + /* Add all subpages to the order-0 head, in sequence. */ + list_del(&page->lru); + for (i =3D 0; i < nr_pages; i++) + list_add_tail(&page[i].lru, &list[0]); + } + } +} + /** * alloc_contig_range() -- tries to allocate given range of pages * @start: start PFN to allocate @@ -6552,12 +6576,25 @@ int alloc_contig_range_noprof(unsigned long start, = unsigned long end, goto done; } =20 - /* Free head and tail (if any) */ - if (start !=3D outer_start) - free_contig_range(outer_start, start - outer_start); - if (end !=3D outer_end) - free_contig_range(end, outer_end - end); + if (!(gfp_mask & __GFP_COMP)) { + split_free_pages(cc.freepages); =20 + /* Free head and tail (if any) */ + if (start !=3D outer_start) + free_contig_range(outer_start, start - outer_start); + if (end !=3D outer_end) + free_contig_range(end, outer_end - end); + } else if (start =3D=3D outer_start && end =3D=3D outer_end && is_power_o= f_2(end - start)) { + struct page *head =3D pfn_to_page(start); + int order =3D ilog2(end - start); + + check_new_pages(head, order); + prep_new_page(head, order, gfp_mask, 0); + } else { + ret =3D -EINVAL; + WARN(true, "PFN range: requested [%lu, %lu), allocated [%lu, %lu)\n", + start, end, outer_start, outer_end); + } done: undo_isolate_page_range(start, end, migratetype); return ret; @@ -6666,6 +6703,18 @@ struct page *alloc_contig_pages_noprof(unsigned long= nr_pages, gfp_t gfp_mask, void free_contig_range(unsigned long pfn, unsigned long nr_pages) { unsigned long count =3D 0; + struct folio *folio =3D pfn_folio(pfn); + + if (folio_test_large(folio)) { + int expected =3D folio_nr_pages(folio); + + if (nr_pages =3D=3D expected) + folio_put(folio); + else + WARN(true, "PFN %lu: nr_pages %lu !=3D expected %d\n", + pfn, nr_pages, expected); + return; + } =20 for (; nr_pages--; pfn++) { struct page *page =3D pfn_to_page(pfn); --=20 2.46.0.76.ge559c4bf1a-goog From nobody Sat Feb 7 08:58:41 2026 Received: from mail-yb1-f201.google.com (mail-yb1-f201.google.com [209.85.219.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id BC7964D8BC for ; Wed, 14 Aug 2024 03:55:01 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723607703; cv=none; b=RqFYTYkr5BCJxK586ez+Vj8EEaDXO+obcBKRdcgWcIzAQWvQYBVM8t8RJn2ukPdc33PGB7a8Ykn3SR3R9+5NxN8ZMUOXzJNadpMPeYDMBF4H1ImnTgnoUk6PXnGTKDlxatrKHuPGVLbuedQk/QtRsdnXK4WNdOUOwSk1LO1RrKE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723607703; c=relaxed/simple; bh=gkuj+QORVAEFJswm0cFxxi72cquXt3ymPdC/VTEwA2k=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=X+HwrgUUZqo0phHq3/cY6OzOc1gAEd9zmnfV9WOtMea6QTgdQnZczBbifl9yrH5Xi+WAzu305Bat7SleSQvk+W/amKDzgeYbkNqMwxAO/7KbbwxyApzrws2l9VixgAFuTkB+4vLYemghsh5HI89La6FVclJaS0WSAsBf56LVvl4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--yuzhao.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=QnvzCcST; arc=none smtp.client-ip=209.85.219.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--yuzhao.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="QnvzCcST" Received: by mail-yb1-f201.google.com with SMTP id 3f1490d57ef6-e087ed145caso10458528276.3 for ; Tue, 13 Aug 2024 20:55:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1723607700; x=1724212500; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=KXVmwVL4hZRiA8cTojFTWKVHpRNtdM/qQOsCSMq7lmE=; b=QnvzCcSTnXYwW6iM7Saf+7wpdOtcjeqD6bpan3oSNLy6QuXWIMU2ktMlYF2kP966qR tX6zy3sb+NJWWxUtQ9OiMgOAUMFrb6k7uwefkbfBmBjU7XrSSCq/ErT70dWHGcdNZTCY 347RD0fF+xg85FLtOiwpxPuPQckHi/3hjSeeOI6fZ+3Agvsv3qeAR/NVYIXGV3ainXDl jfNP03/gMMc6C9yJOBI+FWdTHWqomZ6NsZ65mOXMDJjQFk97uxVbyjeo3iflLIoeXivr HHsWmLimEPFc6MoR0PbYRvstbLNMSBBdhhUv1dLfB+U7AGZXlYL40wIcPCo4WdcOVGiv LvEA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723607700; x=1724212500; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=KXVmwVL4hZRiA8cTojFTWKVHpRNtdM/qQOsCSMq7lmE=; b=H451a9xT55N7BE9oAk/6xhUgB5X1aEmBK/ahIro9q8sEUJOncF8Wl0q7OjageBECqm Hm+jyn0cXfhB2+csmwTjVnzMjgD98cP2uVocGTymQH1RPPVSijPltlPEFMt50yQlzIFS gxPVj1OMraFqQMtgnxgDNq0gmn9/7dR3ZL8umE1UzOrLpawCmi2exUE6CaUySo10fWLT /Kc6sDlQ6rWQBTzouzR+sKy5aOGZzpxy/rAAQ4AO4MFSqt/1MKLHRcdtXjR9A+Rd/mrq sFAVu9x1qnPbODIxDku0PMobxoFQF89TIRzJwf6lNjHQ2lDKVkAj7yc8ogcnWdwttoCO LJlQ== X-Forwarded-Encrypted: i=1; AJvYcCVbIsmACW37/4Rpzcc+5z4+l5LBSWHMxvuuR2C+td4dMBDpUDd6TY+eIgLr4iOqJssR+Mrq7E6y5jbOXInxg/Rp4HK2gtg60z7xsNWX X-Gm-Message-State: AOJu0YwQqWmCMOPbIfSgkxBL+zb8iR3H7A/lJyQYL/Q9IkDb19wX5Ie4 vK6AeiuhadVa6Cm5D+sk5MzZGcD62X8Gb3OJgfmy3hH4vVWIfZt1RNSAPzLaRkZtuW56/KFJ9k3 /IA== X-Google-Smtp-Source: AGHT+IHdxjtU6GE8e6uUmdDhkDkgKFsun57md8iAQGJ8zy2FVnTlUkIoTwELvFGK+N9W879dEyGV8DeAgrM= X-Received: from yuzhao2.bld.corp.google.com ([2a00:79e0:2e28:6:c8ad:fcc7:f7cf:f2fd]) (user=yuzhao job=sendgmr) by 2002:a25:c788:0:b0:e03:62dc:63de with SMTP id 3f1490d57ef6-e1155ada202mr81659276.6.1723607700515; Tue, 13 Aug 2024 20:55:00 -0700 (PDT) Date: Tue, 13 Aug 2024 21:54:50 -0600 In-Reply-To: <20240814035451.773331-1-yuzhao@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240814035451.773331-1-yuzhao@google.com> X-Mailer: git-send-email 2.46.0.76.ge559c4bf1a-goog Message-ID: <20240814035451.773331-3-yuzhao@google.com> Subject: [PATCH mm-unstable v2 2/3] mm/cma: add cma_{alloc,free}_folio() From: Yu Zhao To: Andrew Morton , Muchun Song Cc: "Matthew Wilcox (Oracle)" , Zi Yan , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Yu Zhao Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" With alloc_contig_range() and free_contig_range() supporting large folios, CMA can allocate and free large folios too, by cma_alloc_folio() and cma_free_folio(). Signed-off-by: Yu Zhao --- include/linux/cma.h | 16 +++++++++++++ mm/cma.c | 55 ++++++++++++++++++++++++++++++++------------- 2 files changed, 56 insertions(+), 15 deletions(-) diff --git a/include/linux/cma.h b/include/linux/cma.h index 9db877506ea8..d15b64f51336 100644 --- a/include/linux/cma.h +++ b/include/linux/cma.h @@ -52,4 +52,20 @@ extern bool cma_release(struct cma *cma, const struct pa= ge *pages, unsigned long extern int cma_for_each_area(int (*it)(struct cma *cma, void *data), void = *data); =20 extern void cma_reserve_pages_on_error(struct cma *cma); + +#ifdef CONFIG_CMA +struct folio *cma_alloc_folio(struct cma *cma, int order, gfp_t gfp); +bool cma_free_folio(struct cma *cma, const struct folio *folio); +#else +static inline struct folio *cma_alloc_folio(struct cma *cma, int order, gf= p_t gfp) +{ + return NULL; +} + +static inline bool cma_free_folio(struct cma *cma, const struct folio *fol= io) +{ + return false; +} +#endif + #endif diff --git a/mm/cma.c b/mm/cma.c index 95d6950e177b..4354823d28cf 100644 --- a/mm/cma.c +++ b/mm/cma.c @@ -403,18 +403,8 @@ static void cma_debug_show_areas(struct cma *cma) spin_unlock_irq(&cma->lock); } =20 -/** - * cma_alloc() - allocate pages from contiguous area - * @cma: Contiguous memory region for which the allocation is performed. - * @count: Requested number of pages. - * @align: Requested alignment of pages (in PAGE_SIZE order). - * @no_warn: Avoid printing message about failed allocation - * - * This function allocates part of contiguous memory on specific - * contiguous memory area. - */ -struct page *cma_alloc(struct cma *cma, unsigned long count, - unsigned int align, bool no_warn) +static struct page *__cma_alloc(struct cma *cma, unsigned long count, + unsigned int align, gfp_t gfp) { unsigned long mask, offset; unsigned long pfn =3D -1; @@ -463,8 +453,7 @@ struct page *cma_alloc(struct cma *cma, unsigned long c= ount, =20 pfn =3D cma->base_pfn + (bitmap_no << cma->order_per_bit); mutex_lock(&cma_mutex); - ret =3D alloc_contig_range(pfn, pfn + count, MIGRATE_CMA, - GFP_KERNEL | (no_warn ? __GFP_NOWARN : 0)); + ret =3D alloc_contig_range(pfn, pfn + count, MIGRATE_CMA, gfp); mutex_unlock(&cma_mutex); if (ret =3D=3D 0) { page =3D pfn_to_page(pfn); @@ -494,7 +483,7 @@ struct page *cma_alloc(struct cma *cma, unsigned long c= ount, page_kasan_tag_reset(nth_page(page, i)); } =20 - if (ret && !no_warn) { + if (ret && !(gfp & __GFP_NOWARN)) { pr_err_ratelimited("%s: %s: alloc failed, req-size: %lu pages, ret: %d\n= ", __func__, cma->name, count, ret); cma_debug_show_areas(cma); @@ -513,6 +502,34 @@ struct page *cma_alloc(struct cma *cma, unsigned long = count, return page; } =20 +/** + * cma_alloc() - allocate pages from contiguous area + * @cma: Contiguous memory region for which the allocation is performed. + * @count: Requested number of pages. + * @align: Requested alignment of pages (in PAGE_SIZE order). + * @no_warn: Avoid printing message about failed allocation + * + * This function allocates part of contiguous memory on specific + * contiguous memory area. + */ +struct page *cma_alloc(struct cma *cma, unsigned long count, + unsigned int align, bool no_warn) +{ + return __cma_alloc(cma, count, align, GFP_KERNEL | (no_warn ? __GFP_NOWAR= N : 0)); +} + +struct folio *cma_alloc_folio(struct cma *cma, int order, gfp_t gfp) +{ + struct page *page; + + if (WARN_ON(!order || !(gfp | __GFP_COMP))) + return NULL; + + page =3D __cma_alloc(cma, 1 << order, order, gfp); + + return page ? page_folio(page) : NULL; +} + bool cma_pages_valid(struct cma *cma, const struct page *pages, unsigned long count) { @@ -564,6 +581,14 @@ bool cma_release(struct cma *cma, const struct page *p= ages, return true; } =20 +bool cma_free_folio(struct cma *cma, const struct folio *folio) +{ + if (WARN_ON(!folio_test_large(folio))) + return false; + + return cma_release(cma, &folio->page, folio_nr_pages(folio)); +} + int cma_for_each_area(int (*it)(struct cma *cma, void *data), void *data) { int i; --=20 2.46.0.76.ge559c4bf1a-goog From nobody Sat Feb 7 08:58:41 2026 Received: from mail-yw1-f201.google.com (mail-yw1-f201.google.com [209.85.128.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 4A4FA55897 for ; Wed, 14 Aug 2024 03:55:03 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723607706; cv=none; b=nJsVUhZfhza+2ScxiYGYLZ5X8nsFG3RDW60I/OA3tycra5yTdJfXvgrWuWPGAvl6G52Vp7HnW6T90CXKphu5/Hv3uuVm0hWCBewIugc4jE+09i7+zjaYmkf+7g1HL0l92HylIdkJ7jEBSpaNOz35cDHGOb2V3K6q9dVs3Fvg3tE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723607706; c=relaxed/simple; bh=liwPWDuJdhKGA9jzCfGi1YsMlcAe/sxxhEhTvOTxuh8=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=Ehub0hyUoClmoKid8Si64b4FmkjdEvz6qR++1I2n99jkdKGVHiVV0/yagwZIhBQj//n2nd8Y897oVovoiaez0aTRqRlwL3cAm6rSKc2b2xXDyBgcTnyCvFS6uUnOcv0emDDYSP7INHedTYWT9CVaPJCAk1l85lU686NObBWchNc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--yuzhao.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=fGnquox3; arc=none smtp.client-ip=209.85.128.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--yuzhao.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="fGnquox3" Received: by mail-yw1-f201.google.com with SMTP id 00721157ae682-664916e5b40so7253627b3.1 for ; Tue, 13 Aug 2024 20:55:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1723607703; x=1724212503; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=HkJWh1CGI1FKnB7gI7WJokGF2tPUwAHyBE8neIj6WOo=; b=fGnquox3WU98lTwyWpfEKOPZmHFcbSyTlEuinaj7GVD1c7kCAmBvXHJn5xAW8DztiF 9Iqn3wRCJrATLJBBmc0ocrPf8XdEqWgAqs9pwACCs3Ft2BCu0v5d576SK+b/+aCkenkA QmO0YswZzNkfeOmLb9KOVA98PfIjfEDi5NG1H273eQ2tz/mrxPj6iglNopeaFkAtKbNR eBnvsdWQl6USmTP3UHjETBzP7dF7DzUXTbq5e9xUoHnDKHYC9ONzQg/75wGCi74Qj1tu 0MWhUe4ZgwVAXKr87m+l7JwBi+T1jM5N9guitpAZHev2pQYTBbYDJdObYb3VqgS06f5u qG9g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723607703; x=1724212503; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=HkJWh1CGI1FKnB7gI7WJokGF2tPUwAHyBE8neIj6WOo=; b=sRe/mrcI7qErEsN/F6KDmCX+soBOTCLv9/YzuaQivft+Hbd6sbBWH64GxvAJj4jrI2 VqIu5Pg5vxX9LaoK9MuHKSm5XplNCGxKdxsj4W38OeaPHBmxz36fbWuLWEcDr3JJnybe Ngah875R06ZqZE4aU3QuNGG2v0SPbdMILxIckf483GdQPEu8KCBJeZQdLKmdPZ1m8sPd B7cVozf4KVEZH9AboUwq+C32+Ahses19w+quvlNG21ZwzNfCsJIrekRQOaA/TcTf82NZ g7EytiWkBs198E5p2HLkNGyNTnYUMPUibZ/qjewR3xU8k1o1CrmXR8GSPz8d1ARcT/1d zdIg== X-Forwarded-Encrypted: i=1; AJvYcCXYiiVWhgREh5GEtGRVRoHD1FrzqHBIJOBKlY4LQ07ci7JPBeMgclZUOhI8Qg4gxYFvswYaQeGQc+fN9L6PcKR2CQ96me9rEpLRJMQ1 X-Gm-Message-State: AOJu0YwKhlJdNBbXicGDAjDxBMfGfP6wkvKZ/BltqKHT1l5L6qrwIa7g sUZEk+cNz4WqCjyTNG9/++Dzm4xpCnuvQ131cimeqSEh31upzSso8+cVYcMFPMkrJ5aiQYC0S5l Awg== X-Google-Smtp-Source: AGHT+IHlK/IkcXuFcqXLClLnBVvhuHrceOZTwz1/3YMEoi3KsLnVOrd8HqQm1xPKhmDUfIMOuGUHSIQI8zs= X-Received: from yuzhao2.bld.corp.google.com ([2a00:79e0:2e28:6:c8ad:fcc7:f7cf:f2fd]) (user=yuzhao job=sendgmr) by 2002:a0d:d147:0:b0:651:2eea:4dfe with SMTP id 00721157ae682-6a9df63c480mr685617b3.0.1723607703133; Tue, 13 Aug 2024 20:55:03 -0700 (PDT) Date: Tue, 13 Aug 2024 21:54:51 -0600 In-Reply-To: <20240814035451.773331-1-yuzhao@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240814035451.773331-1-yuzhao@google.com> X-Mailer: git-send-email 2.46.0.76.ge559c4bf1a-goog Message-ID: <20240814035451.773331-4-yuzhao@google.com> Subject: [PATCH mm-unstable v2 3/3] mm/hugetlb: use __GFP_COMP for gigantic folios From: Yu Zhao To: Andrew Morton , Muchun Song Cc: "Matthew Wilcox (Oracle)" , Zi Yan , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Yu Zhao , Frank van der Linden Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Use __GFP_COMP for gigantic folios to greatly reduce not only the amount of code but also the allocation and free time. LOC (approximately): +60, -240 Allocate and free 500 1GB hugeTLB memory without HVO by: time echo 500 >/sys/kernel/mm/hugepages/hugepages-1048576kB/nr_hugepages time echo 0 >/sys/kernel/mm/hugepages/hugepages-1048576kB/nr_hugepages Before After Alloc ~13s ~10s Free ~15s <1s The above magnitude generally holds for multiple x86 and arm64 CPU models. Signed-off-by: Yu Zhao Reported-by: Frank van der Linden Acked-by: Zi Yan --- include/linux/hugetlb.h | 9 +- mm/hugetlb.c | 293 ++++++++-------------------------------- 2 files changed, 62 insertions(+), 240 deletions(-) diff --git a/include/linux/hugetlb.h b/include/linux/hugetlb.h index 3100a52ceb73..98c47c394b89 100644 --- a/include/linux/hugetlb.h +++ b/include/linux/hugetlb.h @@ -896,10 +896,11 @@ static inline bool hugepage_movable_supported(struct = hstate *h) /* Movability of hugepages depends on migration support. */ static inline gfp_t htlb_alloc_mask(struct hstate *h) { - if (hugepage_movable_supported(h)) - return GFP_HIGHUSER_MOVABLE; - else - return GFP_HIGHUSER; + gfp_t gfp =3D __GFP_COMP | __GFP_NOWARN; + + gfp |=3D hugepage_movable_supported(h) ? GFP_HIGHUSER_MOVABLE : GFP_HIGHU= SER; + + return gfp; } =20 static inline gfp_t htlb_modify_alloc_mask(struct hstate *h, gfp_t gfp_mas= k) diff --git a/mm/hugetlb.c b/mm/hugetlb.c index 71d469c8e711..efa77ce87dcc 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -56,16 +56,6 @@ struct hstate hstates[HUGE_MAX_HSTATE]; #ifdef CONFIG_CMA static struct cma *hugetlb_cma[MAX_NUMNODES]; static unsigned long hugetlb_cma_size_in_node[MAX_NUMNODES] __initdata; -static bool hugetlb_cma_folio(struct folio *folio, unsigned int order) -{ - return cma_pages_valid(hugetlb_cma[folio_nid(folio)], &folio->page, - 1 << order); -} -#else -static bool hugetlb_cma_folio(struct folio *folio, unsigned int order) -{ - return false; -} #endif static unsigned long hugetlb_cma_size __initdata; =20 @@ -100,6 +90,17 @@ static void hugetlb_unshare_pmds(struct vm_area_struct = *vma, unsigned long start, unsigned long end); static struct resv_map *vma_resv_map(struct vm_area_struct *vma); =20 +static void hugetlb_free_folio(struct folio *folio) +{ +#ifdef CONFIG_CMA + int nid =3D folio_nid(folio); + + if (cma_free_folio(hugetlb_cma[nid], folio)) + return; +#endif + folio_put(folio); +} + static inline bool subpool_is_free(struct hugepage_subpool *spool) { if (spool->count) @@ -1512,95 +1513,54 @@ static int hstate_next_node_to_free(struct hstate *= h, nodemask_t *nodes_allowed) ((node =3D hstate_next_node_to_free(hs, mask)) || 1); \ nr_nodes--) =20 -/* used to demote non-gigantic_huge pages as well */ -static void __destroy_compound_gigantic_folio(struct folio *folio, - unsigned int order, bool demote) -{ - int i; - int nr_pages =3D 1 << order; - struct page *p; - - atomic_set(&folio->_entire_mapcount, 0); - atomic_set(&folio->_large_mapcount, 0); - atomic_set(&folio->_pincount, 0); - - for (i =3D 1; i < nr_pages; i++) { - p =3D folio_page(folio, i); - p->flags &=3D ~PAGE_FLAGS_CHECK_AT_FREE; - p->mapping =3D NULL; - clear_compound_head(p); - if (!demote) - set_page_refcounted(p); - } - - __folio_clear_head(folio); -} - -static void destroy_compound_hugetlb_folio_for_demote(struct folio *folio, - unsigned int order) -{ - __destroy_compound_gigantic_folio(folio, order, true); -} - #ifdef CONFIG_ARCH_HAS_GIGANTIC_PAGE -static void destroy_compound_gigantic_folio(struct folio *folio, - unsigned int order) -{ - __destroy_compound_gigantic_folio(folio, order, false); -} - -static void free_gigantic_folio(struct folio *folio, unsigned int order) -{ - /* - * If the page isn't allocated using the cma allocator, - * cma_release() returns false. - */ -#ifdef CONFIG_CMA - int nid =3D folio_nid(folio); - - if (cma_release(hugetlb_cma[nid], &folio->page, 1 << order)) - return; -#endif - - free_contig_range(folio_pfn(folio), 1 << order); -} - #ifdef CONFIG_CONTIG_ALLOC static struct folio *alloc_gigantic_folio(struct hstate *h, gfp_t gfp_mask, int nid, nodemask_t *nodemask) { - struct page *page; - unsigned long nr_pages =3D pages_per_huge_page(h); + struct folio *folio; + int order =3D huge_page_order(h); + bool retried =3D false; + if (nid =3D=3D NUMA_NO_NODE) nid =3D numa_mem_id(); - +retry: + folio =3D NULL; #ifdef CONFIG_CMA { int node; =20 - if (hugetlb_cma[nid]) { - page =3D cma_alloc(hugetlb_cma[nid], nr_pages, - huge_page_order(h), true); - if (page) - return page_folio(page); - } + if (hugetlb_cma[nid]) + folio =3D cma_alloc_folio(hugetlb_cma[nid], order, gfp_mask); =20 - if (!(gfp_mask & __GFP_THISNODE)) { + if (!folio && !(gfp_mask & __GFP_THISNODE)) { for_each_node_mask(node, *nodemask) { if (node =3D=3D nid || !hugetlb_cma[node]) continue; =20 - page =3D cma_alloc(hugetlb_cma[node], nr_pages, - huge_page_order(h), true); - if (page) - return page_folio(page); + folio =3D cma_alloc_folio(hugetlb_cma[node], order, gfp_mask); + if (folio) + break; } } } #endif + if (!folio) { + folio =3D folio_alloc_gigantic(order, gfp_mask, nid, nodemask); + if (!folio) + return NULL; + } =20 - page =3D alloc_contig_pages(nr_pages, gfp_mask, nid, nodemask); - return page ? page_folio(page) : NULL; + if (folio_ref_freeze(folio, 1)) + return folio; + + pr_warn("HugeTLB: unexpected refcount on PFN %lu\n", folio_pfn(folio)); + hugetlb_free_folio(folio); + if (!retried) { + retried =3D true; + goto retry; + } + return NULL; } =20 #else /* !CONFIG_CONTIG_ALLOC */ @@ -1617,10 +1577,6 @@ static struct folio *alloc_gigantic_folio(struct hst= ate *h, gfp_t gfp_mask, { return NULL; } -static inline void free_gigantic_folio(struct folio *folio, - unsigned int order) { } -static inline void destroy_compound_gigantic_folio(struct folio *folio, - unsigned int order) { } #endif =20 /* @@ -1747,20 +1703,9 @@ static void __update_and_free_hugetlb_folio(struct h= state *h, folio_clear_hugetlb_hwpoison(folio); =20 folio_ref_unfreeze(folio, 1); - - /* - * Non-gigantic pages demoted from CMA allocated gigantic pages - * need to be given back to CMA in free_gigantic_folio. - */ - if (hstate_is_gigantic(h) || - hugetlb_cma_folio(folio, huge_page_order(h))) { - destroy_compound_gigantic_folio(folio, huge_page_order(h)); - free_gigantic_folio(folio, huge_page_order(h)); - } else { - INIT_LIST_HEAD(&folio->_deferred_list); - folio_clear_partially_mapped(folio); - folio_put(folio); - } + INIT_LIST_HEAD(&folio->_deferred_list); + folio_clear_partially_mapped(folio); + hugetlb_free_folio(folio); } =20 /* @@ -2033,95 +1978,6 @@ static void prep_new_hugetlb_folio(struct hstate *h,= struct folio *folio, int ni spin_unlock_irq(&hugetlb_lock); } =20 -static bool __prep_compound_gigantic_folio(struct folio *folio, - unsigned int order, bool demote) -{ - int i, j; - int nr_pages =3D 1 << order; - struct page *p; - - __folio_clear_reserved(folio); - for (i =3D 0; i < nr_pages; i++) { - p =3D folio_page(folio, i); - - /* - * For gigantic hugepages allocated through bootmem at - * boot, it's safer to be consistent with the not-gigantic - * hugepages and clear the PG_reserved bit from all tail pages - * too. Otherwise drivers using get_user_pages() to access tail - * pages may get the reference counting wrong if they see - * PG_reserved set on a tail page (despite the head page not - * having PG_reserved set). Enforcing this consistency between - * head and tail pages allows drivers to optimize away a check - * on the head page when they need know if put_page() is needed - * after get_user_pages(). - */ - if (i !=3D 0) /* head page cleared above */ - __ClearPageReserved(p); - /* - * Subtle and very unlikely - * - * Gigantic 'page allocators' such as memblock or cma will - * return a set of pages with each page ref counted. We need - * to turn this set of pages into a compound page with tail - * page ref counts set to zero. Code such as speculative page - * cache adding could take a ref on a 'to be' tail page. - * We need to respect any increased ref count, and only set - * the ref count to zero if count is currently 1. If count - * is not 1, we return an error. An error return indicates - * the set of pages can not be converted to a gigantic page. - * The caller who allocated the pages should then discard the - * pages using the appropriate free interface. - * - * In the case of demote, the ref count will be zero. - */ - if (!demote) { - if (!page_ref_freeze(p, 1)) { - pr_warn("HugeTLB page can not be used due to unexpected inflated ref c= ount\n"); - goto out_error; - } - } else { - VM_BUG_ON_PAGE(page_count(p), p); - } - if (i !=3D 0) - set_compound_head(p, &folio->page); - } - __folio_set_head(folio); - /* we rely on prep_new_hugetlb_folio to set the hugetlb flag */ - folio_set_order(folio, order); - atomic_set(&folio->_entire_mapcount, -1); - atomic_set(&folio->_large_mapcount, -1); - atomic_set(&folio->_pincount, 0); - return true; - -out_error: - /* undo page modifications made above */ - for (j =3D 0; j < i; j++) { - p =3D folio_page(folio, j); - if (j !=3D 0) - clear_compound_head(p); - set_page_refcounted(p); - } - /* need to clear PG_reserved on remaining tail pages */ - for (; j < nr_pages; j++) { - p =3D folio_page(folio, j); - __ClearPageReserved(p); - } - return false; -} - -static bool prep_compound_gigantic_folio(struct folio *folio, - unsigned int order) -{ - return __prep_compound_gigantic_folio(folio, order, false); -} - -static bool prep_compound_gigantic_folio_for_demote(struct folio *folio, - unsigned int order) -{ - return __prep_compound_gigantic_folio(folio, order, true); -} - /* * Find and lock address space (mapping) in write mode. * @@ -2160,7 +2016,6 @@ static struct folio *alloc_buddy_hugetlb_folio(struct= hstate *h, */ if (node_alloc_noretry && node_isset(nid, *node_alloc_noretry)) alloc_try_hard =3D false; - gfp_mask |=3D __GFP_COMP|__GFP_NOWARN; if (alloc_try_hard) gfp_mask |=3D __GFP_RETRY_MAYFAIL; if (nid =3D=3D NUMA_NO_NODE) @@ -2207,48 +2062,16 @@ static struct folio *alloc_buddy_hugetlb_folio(stru= ct hstate *h, return folio; } =20 -static struct folio *__alloc_fresh_hugetlb_folio(struct hstate *h, - gfp_t gfp_mask, int nid, nodemask_t *nmask, - nodemask_t *node_alloc_noretry) -{ - struct folio *folio; - bool retry =3D false; - -retry: - if (hstate_is_gigantic(h)) - folio =3D alloc_gigantic_folio(h, gfp_mask, nid, nmask); - else - folio =3D alloc_buddy_hugetlb_folio(h, gfp_mask, - nid, nmask, node_alloc_noretry); - if (!folio) - return NULL; - - if (hstate_is_gigantic(h)) { - if (!prep_compound_gigantic_folio(folio, huge_page_order(h))) { - /* - * Rare failure to convert pages to compound page. - * Free pages and try again - ONCE! - */ - free_gigantic_folio(folio, huge_page_order(h)); - if (!retry) { - retry =3D true; - goto retry; - } - return NULL; - } - } - - return folio; -} - static struct folio *only_alloc_fresh_hugetlb_folio(struct hstate *h, gfp_t gfp_mask, int nid, nodemask_t *nmask, nodemask_t *node_alloc_noretry) { struct folio *folio; =20 - folio =3D __alloc_fresh_hugetlb_folio(h, gfp_mask, nid, nmask, - node_alloc_noretry); + if (hstate_is_gigantic(h)) + folio =3D alloc_gigantic_folio(h, gfp_mask, nid, nmask); + else + folio =3D alloc_buddy_hugetlb_folio(h, gfp_mask, nid, nmask, node_alloc_= noretry); if (folio) init_new_hugetlb_folio(h, folio); return folio; @@ -2266,7 +2089,10 @@ static struct folio *alloc_fresh_hugetlb_folio(struc= t hstate *h, { struct folio *folio; =20 - folio =3D __alloc_fresh_hugetlb_folio(h, gfp_mask, nid, nmask, NULL); + if (hstate_is_gigantic(h)) + folio =3D alloc_gigantic_folio(h, gfp_mask, nid, nmask); + else + folio =3D alloc_buddy_hugetlb_folio(h, gfp_mask, nid, nmask, NULL); if (!folio) return NULL; =20 @@ -2550,9 +2376,8 @@ struct folio *alloc_buddy_hugetlb_folio_with_mpol(str= uct hstate *h, =20 nid =3D huge_node(vma, addr, gfp_mask, &mpol, &nodemask); if (mpol_is_preferred_many(mpol)) { - gfp_t gfp =3D gfp_mask | __GFP_NOWARN; + gfp_t gfp =3D gfp_mask & ~(__GFP_DIRECT_RECLAIM | __GFP_NOFAIL); =20 - gfp &=3D ~(__GFP_DIRECT_RECLAIM | __GFP_NOFAIL); folio =3D alloc_surplus_hugetlb_folio(h, gfp, nid, nodemask); =20 /* Fallback to all nodes if page=3D=3DNULL */ @@ -3334,6 +3159,7 @@ static void __init hugetlb_folio_init_tail_vmemmap(st= ruct folio *folio, for (pfn =3D head_pfn + start_page_number; pfn < end_pfn; pfn++) { struct page *page =3D pfn_to_page(pfn); =20 + __ClearPageReserved(folio_page(folio, pfn - head_pfn)); __init_single_page(page, pfn, zone, nid); prep_compound_tail((struct page *)folio, pfn - head_pfn); ret =3D page_ref_freeze(page, 1); @@ -3950,21 +3776,16 @@ static long demote_free_hugetlb_folios(struct hstat= e *src, struct hstate *dst, continue; =20 list_del(&folio->lru); - /* - * Use destroy_compound_hugetlb_folio_for_demote for all huge page - * sizes as it will not ref count folios. - */ - destroy_compound_hugetlb_folio_for_demote(folio, huge_page_order(src)); + + split_page_owner(&folio->page, huge_page_order(src), huge_page_order(dst= )); + pgalloc_tag_split(&folio->page, 1 << huge_page_order(src)); =20 for (i =3D 0; i < pages_per_huge_page(src); i +=3D pages_per_huge_page(d= st)) { struct page *page =3D folio_page(folio, i); =20 - if (hstate_is_gigantic(dst)) - prep_compound_gigantic_folio_for_demote(page_folio(page), - dst->order); - else - prep_compound_page(page, dst->order); - set_page_private(page, 0); + page->mapping =3D NULL; + clear_compound_head(page); + prep_compound_page(page, dst->order); =20 init_new_hugetlb_folio(dst, page_folio(page)); list_add(&page->lru, &dst_list); --=20 2.46.0.76.ge559c4bf1a-goog