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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 3F1BBC433F5 for ; Thu, 9 Dec 2021 23:04:47 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234129AbhLIXIR (ORCPT ); Thu, 9 Dec 2021 18:08:17 -0500 Received: from new1-smtp.messagingengine.com ([66.111.4.221]:37603 "EHLO new1-smtp.messagingengine.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232827AbhLIXIO (ORCPT ); Thu, 9 Dec 2021 18:08:14 -0500 Received: from compute6.internal (compute6.nyi.internal [10.202.2.46]) by mailnew.nyi.internal (Postfix) with ESMTP id 4FFEC58025E; Thu, 9 Dec 2021 18:04:40 -0500 (EST) Received: from mailfrontend2 ([10.202.2.163]) by compute6.internal (MEProxy); Thu, 09 Dec 2021 18:04:40 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sent.com; h=from :to:cc:subject:date:message-id:in-reply-to:references:reply-to :mime-version:content-transfer-encoding; s=fm2; bh=2s2DRFCWo6SJF dg2WL/KkkfiW4T/Z/OJbwdjocA6xrw=; b=vkmJpXnCf/WYOZLob1cmUKqPEQnql TAGXNhH/A8TLl24AyGbNBZve34nQFnkTVUvHf7QFcnIlG4r7mGoshZi3w2tcOu+Q 9059RYX9uvMWah0MpXh6W9+QeNoAiq0rZOo/9/KRJkBkqkj45rJpzrlsDfGjyZPJ 9aYPrguNfM7T9SV/bX8iJebMz5XxN+yn0VLb5CNnY93AgcsN9qL49k4C7U984IX4 hhPp9YgU6Y8JgcZtTd6+KZbJiQf5/o8S1M/6sBWTjzxnveuVrTYQCZgFwyt2fo8N coPI+6JPT2Fp427yluN4cOOLzJXhyOaMgUs6Bt5AASHHG6t6807lcB+4w== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-transfer-encoding:date:from :in-reply-to:message-id:mime-version:references:reply-to:subject :to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s= fm1; bh=2s2DRFCWo6SJFdg2WL/KkkfiW4T/Z/OJbwdjocA6xrw=; b=oDj/PPWi goi2C10Sr8rHKrLfBl/gXc2ccO/WAbm6gknAc4VEgDN8DeAF31JETt+CVO7LlnlO PfT1nwLzlc5E7DVUjaAf3EALSriWa7U6J9v/z6pYk0NZ351PSauP45pAl1e081jY +mRHG9gjs561z3Fvj1zv5pnP0UWTpDl8WhY6GWuE8JOjNPTqYLl9qcSNmc7bTgYQ qQ1PdYBnX5RxfaBkNea0838agImx3wEn6eTGhfLL8EJgxj2UhzcRGjpMyquxcy58 LXs7BDepfWDPPFmhVJd+Y5p85xjrG2Jo07q9pnhy0StrkV8h7GEEe2xtmhdO26ym NSyYfMZD0NwFSg== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvuddrkedugddtgecutefuodetggdotefrodftvf curfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfghnecu uegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmdenuc fjughrpefhvffufffkofgjfhhrggfgsedtqhertdertddtnecuhfhrohhmpegkihcujggr nhcuoeiiihdrhigrnhesshgvnhhtrdgtohhmqeenucggtffrrghtthgvrhhnpeeijeeuvd euudeuhfeghfehieeuvdetvdeugfeigeevteeuieeuhedtgeduheefleenucevlhhushht vghrufhiiigvpedtnecurfgrrhgrmhepmhgrihhlfhhrohhmpeiiihdrhigrnhesshgvnh htrdgtohhm X-ME-Proxy: Received: by mail.messagingengine.com (Postfix) with ESMTPA; Thu, 9 Dec 2021 18:04:39 -0500 (EST) From: Zi Yan To: David Hildenbrand , linux-mm@kvack.org Cc: linux-kernel@vger.kernel.org, Michael Ellerman , Christoph Hellwig , Marek Szyprowski , Robin Murphy , linuxppc-dev@lists.ozlabs.org, virtualization@lists.linux-foundation.org, iommu@lists.linux-foundation.org, Vlastimil Babka , Mel Gorman , Eric Ren , Zi Yan Subject: [RFC PATCH v2 4/7] mm: make alloc_contig_range work at pageblock granularity Date: Thu, 9 Dec 2021 18:04:11 -0500 Message-Id: <20211209230414.2766515-5-zi.yan@sent.com> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20211209230414.2766515-1-zi.yan@sent.com> References: <20211209230414.2766515-1-zi.yan@sent.com> Reply-To: Zi Yan MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Zi Yan alloc_contig_range() worked at MAX_ORDER-1 granularity to avoid merging pageblocks with different migratetypes. It might unnecessarily convert extra pageblocks at the beginning and at the end of the range. Change alloc_contig_range() to work at pageblock granularity. It is done by restoring pageblock types and split >pageblock_order free pages after isolating at MAX_ORDER-1 granularity and migrating pages away at pageblock granularity. The reason for this process is that during isolation, some pages, either free or in-use, might have >pageblock sizes and isolating part of them can cause free accounting issues. Restoring the migratetypes of the pageblocks not in the interesting range later is much easier. Signed-off-by: Zi Yan --- mm/page_alloc.c | 169 ++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 149 insertions(+), 20 deletions(-) diff --git a/mm/page_alloc.c b/mm/page_alloc.c index 107a5f186d3b..5ffbeb1b7512 100644 --- a/mm/page_alloc.c +++ b/mm/page_alloc.c @@ -8981,8 +8981,8 @@ struct page *has_unmovable_pages(struct zone *zone, s= truct page *page, #ifdef CONFIG_CONTIG_ALLOC static unsigned long pfn_max_align_down(unsigned long pfn) { - return pfn & ~(max_t(unsigned long, MAX_ORDER_NR_PAGES, - pageblock_nr_pages) - 1); + return ALIGN_DOWN(pfn, max_t(unsigned long, MAX_ORDER_NR_PAGES, + pageblock_nr_pages)); } =20 static unsigned long pfn_max_align_up(unsigned long pfn) @@ -9071,6 +9071,52 @@ static int __alloc_contig_migrate_range(struct compa= ct_control *cc, return 0; } =20 +static inline int save_migratetypes(unsigned char *migratetypes, + unsigned long start_pfn, unsigned long end_pfn) +{ + unsigned long pfn =3D start_pfn; + int num =3D 0; + + while (pfn < end_pfn) { + migratetypes[num] =3D get_pageblock_migratetype(pfn_to_page(pfn)); + num++; + pfn +=3D pageblock_nr_pages; + } + return num; +} + +static inline int restore_migratetypes(unsigned char *migratetypes, + unsigned long start_pfn, unsigned long end_pfn) +{ + unsigned long pfn =3D start_pfn; + int num =3D 0; + + while (pfn < end_pfn) { + set_pageblock_migratetype(pfn_to_page(pfn), migratetypes[num]); + num++; + pfn +=3D pageblock_nr_pages; + } + return num; +} + +static inline void split_free_page_into_pageblocks(struct page *free_page, + int order, struct zone *zone) +{ + unsigned long pfn; + + spin_lock(&zone->lock); + del_page_from_free_list(free_page, zone, order); + for (pfn =3D page_to_pfn(free_page); + pfn < page_to_pfn(free_page) + (1UL << order); + pfn +=3D pageblock_nr_pages) { + int mt =3D get_pfnblock_migratetype(pfn_to_page(pfn), pfn); + + __free_one_page(pfn_to_page(pfn), pfn, zone, pageblock_order, + mt, FPI_NONE); + } + spin_unlock(&zone->lock); +} + /** * alloc_contig_range() -- tries to allocate given range of pages * @start: start PFN to allocate @@ -9096,8 +9142,15 @@ int alloc_contig_range(unsigned long start, unsigned= long end, unsigned migratetype, gfp_t gfp_mask) { unsigned long outer_start, outer_end; + unsigned long isolate_start =3D pfn_max_align_down(start); + unsigned long isolate_end =3D pfn_max_align_up(end); + unsigned long alloc_start =3D ALIGN_DOWN(start, pageblock_nr_pages); + unsigned long alloc_end =3D ALIGN(end, pageblock_nr_pages); + unsigned long num_pageblock_to_save; unsigned int order; int ret =3D 0; + unsigned char *saved_mt; + int num; =20 struct compact_control cc =3D { .nr_migratepages =3D 0, @@ -9111,11 +9164,30 @@ int alloc_contig_range(unsigned long start, unsigne= d long end, }; INIT_LIST_HEAD(&cc.migratepages); =20 + /* + * TODO: make MIGRATE_ISOLATE a standalone bit to avoid overwriting + * the exiting migratetype. Then, we will not need the save and restore + * process here. + */ + + /* Save the migratepages of the pageblocks before start and after end */ + num_pageblock_to_save =3D (alloc_start - isolate_start) / pageblock_nr_pa= ges + + (isolate_end - alloc_end) / pageblock_nr_pages; + saved_mt =3D + kmalloc_array(num_pageblock_to_save, + sizeof(unsigned char), GFP_KERNEL); + if (!saved_mt) + return -ENOMEM; + + num =3D save_migratetypes(saved_mt, isolate_start, alloc_start); + + num =3D save_migratetypes(&saved_mt[num], alloc_end, isolate_end); + /* * What we do here is we mark all pageblocks in range as * MIGRATE_ISOLATE. Because pageblock and max order pages may * have different sizes, and due to the way page allocator - * work, we align the range to biggest of the two pages so + * work, we align the isolation range to biggest of the two so * that page allocator won't try to merge buddies from * different pageblocks and change MIGRATE_ISOLATE to some * other migration type. @@ -9125,6 +9197,20 @@ int alloc_contig_range(unsigned long start, unsigned= long end, * we are interested in). This will put all the pages in * range back to page allocator as MIGRATE_ISOLATE. * + * Afterwards, we restore the migratetypes of the pageblocks not + * in range, split free pages spanning outside the range, + * and put split free pages (at pageblock_order) to the right + * migratetype list. + * + * NOTE: the above approach is used because it can cause free + * page accounting issues during isolation, if a page, either + * free or in-use, contains multiple pageblocks and we only + * isolate a subset of them. For example, if only the second + * pageblock is isolated from a page with 2 pageblocks, after + * the page is free, it will be put in the first pageblock + * migratetype list instead of having 2 pageblocks in two + * separate migratetype lists. + * * When this is done, we take the pages in range from page * allocator removing them from the buddy system. This way * page allocator will never consider using them. @@ -9135,10 +9221,9 @@ int alloc_contig_range(unsigned long start, unsigned= long end, * put back to page allocator so that buddy can use them. */ =20 - ret =3D start_isolate_page_range(pfn_max_align_down(start), - pfn_max_align_up(end), migratetype, 0); + ret =3D start_isolate_page_range(isolate_start, isolate_end, migratetype,= 0); if (ret) - return ret; + goto done; =20 drain_all_pages(cc.zone); =20 @@ -9174,6 +9259,19 @@ int alloc_contig_range(unsigned long start, unsigned= long end, * isolated thus they won't get removed from buddy. */ =20 + /* + * Restore migratetypes of pageblocks outside [start, end) + * TODO: remove it when MIGRATE_ISOLATE becomes a standalone bit + */ + + num =3D restore_migratetypes(saved_mt, isolate_start, alloc_start); + + num =3D restore_migratetypes(&saved_mt[num], alloc_end, isolate_end); + + /* + * Split free page spanning [isolate_start, alloc_start) and put the + * pageblocks in the right migratetype lists. + */ order =3D 0; outer_start =3D start; while (!PageBuddy(pfn_to_page(outer_start))) { @@ -9188,37 +9286,68 @@ int alloc_contig_range(unsigned long start, unsigne= d long end, order =3D buddy_order(pfn_to_page(outer_start)); =20 /* - * outer_start page could be small order buddy page and - * it doesn't include start page. Adjust outer_start - * in this case to report failed page properly - * on tracepoint in test_pages_isolated() + * split the free page has start page and put the pageblocks + * in the right migratetype list */ - if (outer_start + (1UL << order) <=3D start) - outer_start =3D start; + if (outer_start + (1UL << order) > start) { + struct page *free_page =3D pfn_to_page(outer_start); + + split_free_page_into_pageblocks(free_page, order, cc.zone); + } + } + + /* + * Split free page spanning [alloc_end, isolate_end) and put the + * pageblocks in the right migratetype list + */ + order =3D 0; + outer_end =3D end; + while (!PageBuddy(pfn_to_page(outer_end))) { + if (++order >=3D MAX_ORDER) { + outer_end =3D end; + break; + } + outer_end &=3D ~0UL << order; + } + + if (outer_end !=3D end) { + order =3D buddy_order(pfn_to_page(outer_end)); + + /* + * split the free page has start page and put the pageblocks + * in the right migratetype list + */ + VM_BUG_ON(outer_end + (1UL << order) <=3D end); + { + struct page *free_page =3D pfn_to_page(outer_end); + + split_free_page_into_pageblocks(free_page, order, cc.zone); + } } =20 /* Make sure the range is really isolated. */ - if (test_pages_isolated(outer_start, end, 0)) { + if (test_pages_isolated(alloc_start, alloc_end, 0)) { ret =3D -EBUSY; goto done; } =20 /* Grab isolated pages from freelists. */ - outer_end =3D isolate_freepages_range(&cc, outer_start, end); + outer_end =3D isolate_freepages_range(&cc, alloc_start, alloc_end); if (!outer_end) { ret =3D -EBUSY; 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 (start !=3D alloc_start) + free_contig_range(alloc_start, start - alloc_start); + if (end !=3D alloc_end) + free_contig_range(end, alloc_end - end); =20 done: - undo_isolate_page_range(pfn_max_align_down(start), - pfn_max_align_up(end), migratetype); + kfree(saved_mt); + undo_isolate_page_range(alloc_start, + alloc_end, migratetype); return ret; } EXPORT_SYMBOL(alloc_contig_range); --=20 2.33.0