From nobody Fri Dec 19 18:47:05 2025 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 43FE4EE49A5 for ; Mon, 21 Aug 2023 18:37:52 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229813AbjHUShw (ORCPT ); Mon, 21 Aug 2023 14:37:52 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51394 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229789AbjHUShu (ORCPT ); Mon, 21 Aug 2023 14:37:50 -0400 Received: from mail-oi1-x233.google.com (mail-oi1-x233.google.com [IPv6:2607:f8b0:4864:20::233]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C7D5157A99 for ; Mon, 21 Aug 2023 11:37:45 -0700 (PDT) Received: by mail-oi1-x233.google.com with SMTP id 5614622812f47-3a850f07fadso1383539b6e.2 for ; Mon, 21 Aug 2023 11:37:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cmpxchg-org.20221208.gappssmtp.com; s=20221208; t=1692643063; x=1693247863; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=3E/b9O2HlTOi/IQwrvX2RmX6ugx51n+ANVahrlt1y9E=; b=bs78ZLqFzdCBNP5WOZIQbmPxf0MB2IdxbI0+wTHjK3eA21DMWZU3ExKk5aSnGUtSGv VxXOxMtw21FDXY9J49x9bWd5kOiUDCL8lWn93rQTeUBXsM20t9bNh7PH/yUTZL5+Chzj r2jw52FRRe82audJSSNDAz3Dir5MIn2g0TgWAgipTEF9Pte+5X+Fm3UfzQo1qgUAV9i4 Vquc0HF8tqg2Xl3K08/Bc4G/ic+rkduZ24lP/J7PZMU1qeh2WaPyWWJMLjcOglEig1p5 mp+7h9SeFU5gRyklMRXybBl3jNbOKpbAw+pKb/mvWIH0jGBjP4LK4u0bxrbPXxhKO8aB h4KQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1692643063; x=1693247863; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=3E/b9O2HlTOi/IQwrvX2RmX6ugx51n+ANVahrlt1y9E=; b=hWWB4uuuSGKJykENPjxQvidxc7m2mIyvdy3FBIqbIBHh0uBDef7RDucux6sf7mMQ66 o5W+kNJvozkvUYFlSfqbRw6Vz6X9UOBvG2F9GZHygiQG9iZxGzWWQukFST7rCspS2um3 /VrJNlj0CAgPVN1pl20QfijzORHAh48LH6k7ZXVs9PtiozeNH3xVMwsr5FVENSLN22xb T6ZWH2ST8hRBGK5vB4BmZEyPfKV0A2+oxJcaGtLJIZdI3OqCOE9jyTXnKRM4qGjkDgIq T5Va1LPsl/oqSrZJxi4tAXjwPDfyDiJeWzBZ1BndD9VeKS5nvb/U+Mo+cG6N16v0TD38 +/dA== X-Gm-Message-State: AOJu0Yyjeqped4/KnyqyLkmjPJmwNsGyBJdYXMzHx+BTGhJB0YCSFg4L ey5+Pjnk4lnHJSc6aFuGjRbthQ== X-Google-Smtp-Source: AGHT+IGBSQnPB0tu7RVp53PncovhzE7SzTwHvY1Hwz+2KHc+6UHEFujdqz0TZEhwDQ+r7m42MeMWvg== X-Received: by 2002:a05:6358:90e:b0:130:df70:b9cd with SMTP id r14-20020a056358090e00b00130df70b9cdmr5101720rwi.12.1692643063580; Mon, 21 Aug 2023 11:37:43 -0700 (PDT) Received: from localhost (2603-7000-0c01-2716-699c-6fe1-d2a8-6a30.res6.spectrum.com. [2603:7000:c01:2716:699c:6fe1:d2a8:6a30]) by smtp.gmail.com with ESMTPSA id m18-20020ae9e712000000b0076d4bb714afsm2634476qka.50.2023.08.21.11.37.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 21 Aug 2023 11:37:43 -0700 (PDT) From: Johannes Weiner To: Andrew Morton Cc: Vlastimil Babka , Mel Gorman , linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [PATCH 1/8] mm: page_alloc: use get_pfnblock_migratetype where pfn available Date: Mon, 21 Aug 2023 14:33:33 -0400 Message-ID: <20230821183733.106619-2-hannes@cmpxchg.org> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20230821183733.106619-1-hannes@cmpxchg.org> References: <20230821183733.106619-1-hannes@cmpxchg.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Save a pfn_to_page() lookup when the pfn is right there already. Signed-off-by: Johannes Weiner --- mm/page_alloc.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/mm/page_alloc.c b/mm/page_alloc.c index 977bb4d5e8e1..e430ac45df7c 100644 --- a/mm/page_alloc.c +++ b/mm/page_alloc.c @@ -824,7 +824,7 @@ static inline void __free_one_page(struct page *page, * pageblock isolation could cause incorrect freepage or CMA * accounting or HIGHATOMIC accounting. */ - int buddy_mt =3D get_pageblock_migratetype(buddy); + int buddy_mt =3D get_pfnblock_migratetype(buddy, buddy_pfn); =20 if (migratetype !=3D buddy_mt && (!migratetype_is_mergeable(migratetype) || @@ -900,7 +900,7 @@ int split_free_page(struct page *free_page, goto out; } =20 - mt =3D get_pageblock_migratetype(free_page); + mt =3D get_pfnblock_migratetype(free_page, free_page_pfn); if (likely(!is_migrate_isolate(mt))) __mod_zone_freepage_state(zone, -(1UL << order), mt); =20 --=20 2.41.0 From nobody Fri Dec 19 18:47:05 2025 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 EFF72EE4996 for ; Mon, 21 Aug 2023 18:37:57 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229844AbjHUSh5 (ORCPT ); Mon, 21 Aug 2023 14:37:57 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39702 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229794AbjHUShy (ORCPT ); Mon, 21 Aug 2023 14:37:54 -0400 Received: from mail-qk1-x731.google.com (mail-qk1-x731.google.com [IPv6:2607:f8b0:4864:20::731]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3B00457AAF for ; Mon, 21 Aug 2023 11:37:47 -0700 (PDT) Received: by mail-qk1-x731.google.com with SMTP id af79cd13be357-76da22c090dso121504685a.2 for ; Mon, 21 Aug 2023 11:37:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cmpxchg-org.20221208.gappssmtp.com; s=20221208; t=1692643065; x=1693247865; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=+OXDzqobi26eycvYhGCWs85e727Laa8S1QvcjFdxFeg=; b=lgZ1kcSOohYpCTrIyKsC93cc4kIpl8ate6SPXQoHEqFEqxph+VzaCZQQveAtX6GI9/ bgOak/kNwG6ziWfjfOg6N67Hz/+yuzH8lI580AFM1epn21zvL8vi83X1L7YUHwzpGVvx YA01iPY8Lqhd9V73PD1m67deaPbteFXaTthOU6cezFmYs3ymg6qw5Hh/iNtsrcu+USPT CxJVbwndANUPXW6bupkU45WerFNpyqEIBPD+cdXldw73OZyfaNREF/atCX7sgh1JYBKf xoH6pjR9xo7d8NggsYfek3/nDHteNHpaNgQMI5zY7J4ai1BX6DtyFoJ3TFZZ+p3Oijh6 WKIA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1692643065; x=1693247865; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=+OXDzqobi26eycvYhGCWs85e727Laa8S1QvcjFdxFeg=; b=WPjBOulg4iuqo+GRtPrSjKTbQv6Kb2NRVb/uCnel6n6fEZCBUCsF0woQq6FubFEV9P NIgFknyGf6PMfCFOfgsLe2kXdfv7B6n+6Nc6bdNy8728Jv8BvXWt4cZ9xYoCT6Lymxyf Aj6bpxwiTewduI3pYGe3d+w6sBT1f/uDPl/O57SmNRzqi27Zdny8WdBpnnqXcnxE+ZVO 1zBGqN0+bwlUxXoOlXgWCZunGa/6ExrVsyJ3mI8xcmDA6Hich0WRo12R8N0ItC8D371N C64MoJ9G97PGhoijuRVzjJVwNrFu3UaXn4WgjGBwpYOHV8VN+8BrA0wedLGcVKncoB0+ W0yw== X-Gm-Message-State: AOJu0YxrAj2RgxA4s5G0n20g+EpmNGdkVgGIHcjzhr4TYPkGSnx+SDZ5 2DaB1LY5/efR8Xvi80zzdemObw8LdISRnH6Db0nHJA== X-Google-Smtp-Source: AGHT+IGJZ+zhp1f2bcF5REuEixNaaCLEE9Htf14o6is5FNd/PZdXWJXddgqEaBhHppgDrV4sWl0WmA== X-Received: by 2002:a05:620a:4493:b0:76c:e764:508a with SMTP id x19-20020a05620a449300b0076ce764508amr11514961qkp.3.1692643064872; Mon, 21 Aug 2023 11:37:44 -0700 (PDT) Received: from localhost (2603-7000-0c01-2716-699c-6fe1-d2a8-6a30.res6.spectrum.com. [2603:7000:c01:2716:699c:6fe1:d2a8:6a30]) by smtp.gmail.com with ESMTPSA id os33-20020a05620a812100b007682af2c8aasm2664017qkn.126.2023.08.21.11.37.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 21 Aug 2023 11:37:44 -0700 (PDT) From: Johannes Weiner To: Andrew Morton Cc: Vlastimil Babka , Mel Gorman , linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [PATCH 2/8] mm: page_alloc: remove pcppage migratetype caching Date: Mon, 21 Aug 2023 14:33:34 -0400 Message-ID: <20230821183733.106619-3-hannes@cmpxchg.org> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20230821183733.106619-1-hannes@cmpxchg.org> References: <20230821183733.106619-1-hannes@cmpxchg.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" The idea behind the cache is to save get_pageblock_migratetype() lookups during bulk freeing. A microbenchmark suggests this isn't helping, though. The pcp migratetype can get stale, which means that bulk freeing has an extra branch to check if the pageblock was isolated while on the pcp. While the variance overlaps, the cache write and the branch seem to make this a net negative. The following test allocates and frees batches of 10,000 pages (~3x the pcp high marks to trigger flushing): Before: 8,668.48 msec task-clock # 99.735 CPUs ut= ilized ( +- 2.90% ) 19 context-switches # 4.341 /sec = ( +- 3.24% ) 0 cpu-migrations # 0.000 /sec 17,440 page-faults # 3.984 K/sec = ( +- 2.90% ) 41,758,692,473 cycles # 9.541 GHz = ( +- 2.90% ) 126,201,294,231 instructions # 5.98 insn pe= r cycle ( +- 2.90% ) 25,348,098,335 branches # 5.791 G/sec = ( +- 2.90% ) 33,436,921 branch-misses # 0.26% of all = branches ( +- 2.90% ) 0.0869148 +- 0.0000302 seconds time elapsed ( +- 0.03% ) After: 8,444.81 msec task-clock # 99.726 CPUs ut= ilized ( +- 2.90% ) 22 context-switches # 5.160 /sec = ( +- 3.23% ) 0 cpu-migrations # 0.000 /sec 17,443 page-faults # 4.091 K/sec = ( +- 2.90% ) 40,616,738,355 cycles # 9.527 GHz = ( +- 2.90% ) 126,383,351,792 instructions # 6.16 insn pe= r cycle ( +- 2.90% ) 25,224,985,153 branches # 5.917 G/sec = ( +- 2.90% ) 32,236,793 branch-misses # 0.25% of all = branches ( +- 2.90% ) 0.0846799 +- 0.0000412 seconds time elapsed ( +- 0.05% ) A side effect is that this also ensures that pages whose pageblock gets stolen while on the pcplist end up on the right freelist and we don't perform potentially type-incompatible buddy merges (or skip merges when we shouldn't), whis is likely beneficial to long-term fragmentation management, although the effects would be harder to measure. Settle for simpler and faster code as justification here. Signed-off-by: Johannes Weiner --- mm/page_alloc.c | 61 ++++++++++++------------------------------------- 1 file changed, 14 insertions(+), 47 deletions(-) diff --git a/mm/page_alloc.c b/mm/page_alloc.c index e430ac45df7c..20973887999b 100644 --- a/mm/page_alloc.c +++ b/mm/page_alloc.c @@ -204,24 +204,6 @@ EXPORT_SYMBOL(node_states); =20 gfp_t gfp_allowed_mask __read_mostly =3D GFP_BOOT_MASK; =20 -/* - * A cached value of the page's pageblock's migratetype, used when the pag= e is - * put on a pcplist. Used to avoid the pageblock migratetype lookup when - * freeing from pcplists in most cases, at the cost of possibly becoming s= tale. - * Also the migratetype set in the page does not necessarily match the pcp= list - * index, e.g. page might have MIGRATE_CMA set but be on a pcplist with any - * other index - this ensures that it will be put on the correct CMA freel= ist. - */ -static inline int get_pcppage_migratetype(struct page *page) -{ - return page->index; -} - -static inline void set_pcppage_migratetype(struct page *page, int migratet= ype) -{ - page->index =3D migratetype; -} - #ifdef CONFIG_HUGETLB_PAGE_SIZE_VARIABLE unsigned int pageblock_order __read_mostly; #endif @@ -1213,7 +1195,6 @@ static void free_pcppages_bulk(struct zone *zone, int= count, int min_pindex =3D 0; int max_pindex =3D NR_PCP_LISTS - 1; unsigned int order; - bool isolated_pageblocks; struct page *page; =20 /* @@ -1226,7 +1207,6 @@ static void free_pcppages_bulk(struct zone *zone, int= count, pindex =3D pindex - 1; =20 spin_lock_irqsave(&zone->lock, flags); - isolated_pageblocks =3D has_isolate_pageblock(zone); =20 while (count > 0) { struct list_head *list; @@ -1249,10 +1229,12 @@ static void free_pcppages_bulk(struct zone *zone, i= nt count, order =3D pindex_to_order(pindex); nr_pages =3D 1 << order; do { + unsigned long pfn; int mt; =20 page =3D list_last_entry(list, struct page, pcp_list); - mt =3D get_pcppage_migratetype(page); + pfn =3D page_to_pfn(page); + mt =3D get_pfnblock_migratetype(page, pfn); =20 /* must delete to avoid corrupting pcp list */ list_del(&page->pcp_list); @@ -1261,11 +1243,8 @@ static void free_pcppages_bulk(struct zone *zone, in= t count, =20 /* MIGRATE_ISOLATE page should not go to pcplists */ VM_BUG_ON_PAGE(is_migrate_isolate(mt), page); - /* Pageblock could have been isolated meanwhile */ - if (unlikely(isolated_pageblocks)) - mt =3D get_pageblock_migratetype(page); =20 - __free_one_page(page, page_to_pfn(page), zone, order, mt, FPI_NONE); + __free_one_page(page, pfn, zone, order, mt, FPI_NONE); trace_mm_page_pcpu_drain(page, order, mt); } while (count > 0 && !list_empty(list)); } @@ -1611,7 +1590,6 @@ struct page *__rmqueue_smallest(struct zone *zone, un= signed int order, continue; del_page_from_free_list(page, zone, current_order); expand(zone, page, order, current_order, migratetype); - set_pcppage_migratetype(page, migratetype); trace_mm_page_alloc_zone_locked(page, order, migratetype, pcp_allowed_order(order) && migratetype < MIGRATE_PCPTYPES); @@ -2181,7 +2159,7 @@ static int rmqueue_bulk(struct zone *zone, unsigned i= nt order, * pages are ordered properly. */ list_add_tail(&page->pcp_list, list); - if (is_migrate_cma(get_pcppage_migratetype(page))) + if (is_migrate_cma(get_pageblock_migratetype(page))) __mod_zone_page_state(zone, NR_FREE_CMA_PAGES, -(1 << order)); } @@ -2340,19 +2318,6 @@ void drain_all_pages(struct zone *zone) __drain_all_pages(zone, false); } =20 -static bool free_unref_page_prepare(struct page *page, unsigned long pfn, - unsigned int order) -{ - int migratetype; - - if (!free_pages_prepare(page, order, FPI_NONE)) - return false; - - migratetype =3D get_pfnblock_migratetype(page, pfn); - set_pcppage_migratetype(page, migratetype); - return true; -} - static int nr_pcp_free(struct per_cpu_pages *pcp, int high, int batch, bool free_high) { @@ -2440,7 +2405,7 @@ void free_unref_page(struct page *page, unsigned int = order) unsigned long pfn =3D page_to_pfn(page); int migratetype; =20 - if (!free_unref_page_prepare(page, pfn, order)) + if (!free_pages_prepare(page, order, FPI_NONE)) return; =20 /* @@ -2450,7 +2415,7 @@ void free_unref_page(struct page *page, unsigned int = order) * areas back if necessary. Otherwise, we may have to free * excessively into the page allocator */ - migratetype =3D get_pcppage_migratetype(page); + migratetype =3D get_pfnblock_migratetype(page, pfn); if (unlikely(migratetype >=3D MIGRATE_PCPTYPES)) { if (unlikely(is_migrate_isolate(migratetype))) { free_one_page(page_zone(page), page, pfn, order, migratetype, FPI_NONE); @@ -2486,7 +2451,8 @@ void free_unref_page_list(struct list_head *list) /* Prepare pages for freeing */ list_for_each_entry_safe(page, next, list, lru) { unsigned long pfn =3D page_to_pfn(page); - if (!free_unref_page_prepare(page, pfn, 0)) { + + if (!free_pages_prepare(page, 0, FPI_NONE)) { list_del(&page->lru); continue; } @@ -2495,7 +2461,7 @@ void free_unref_page_list(struct list_head *list) * Free isolated pages directly to the allocator, see * comment in free_unref_page. */ - migratetype =3D get_pcppage_migratetype(page); + migratetype =3D get_pfnblock_migratetype(page, pfn); if (unlikely(is_migrate_isolate(migratetype))) { list_del(&page->lru); free_one_page(page_zone(page), page, pfn, 0, migratetype, FPI_NONE); @@ -2504,10 +2470,11 @@ void free_unref_page_list(struct list_head *list) } =20 list_for_each_entry_safe(page, next, list, lru) { + unsigned long pfn =3D page_to_pfn(page); struct zone *zone =3D page_zone(page); =20 list_del(&page->lru); - migratetype =3D get_pcppage_migratetype(page); + migratetype =3D get_pfnblock_migratetype(page, pfn); =20 /* * Either different zone requiring a different pcp lock or @@ -2530,7 +2497,7 @@ void free_unref_page_list(struct list_head *list) pcp =3D pcp_spin_trylock(zone->per_cpu_pageset); if (unlikely(!pcp)) { pcp_trylock_finish(UP_flags); - free_one_page(zone, page, page_to_pfn(page), + free_one_page(zone, page, pfn, 0, migratetype, FPI_NONE); locked_zone =3D NULL; continue; @@ -2705,7 +2672,7 @@ struct page *rmqueue_buddy(struct zone *preferred_zon= e, struct zone *zone, } } __mod_zone_freepage_state(zone, -(1 << order), - get_pcppage_migratetype(page)); + get_pageblock_migratetype(page)); spin_unlock_irqrestore(&zone->lock, flags); } while (check_new_pages(page, order)); =20 --=20 2.41.0 From nobody Fri Dec 19 18:47:05 2025 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 04B43EE49A5 for ; Mon, 21 Aug 2023 18:38:00 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229861AbjHUSh7 (ORCPT ); Mon, 21 Aug 2023 14:37:59 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39714 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229789AbjHUShz (ORCPT ); Mon, 21 Aug 2023 14:37:55 -0400 Received: from mail-qk1-x730.google.com (mail-qk1-x730.google.com [IPv6:2607:f8b0:4864:20::730]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A7C215116 for ; Mon, 21 Aug 2023 11:37:49 -0700 (PDT) Received: by mail-qk1-x730.google.com with SMTP id af79cd13be357-76d9a79e2fdso161348585a.1 for ; Mon, 21 Aug 2023 11:37:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cmpxchg-org.20221208.gappssmtp.com; s=20221208; t=1692643066; x=1693247866; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=pMLPduHfWGGT6ewh4Swe7Rx903irp7oZR0cyociTE+I=; b=zgi7MRwUxuxU6l+6YWjAy02b4tuqcRki3BC55feePlInbqdpdKi7StCxg8XQ8rUlHV qmL0t/87Bm5h3OxPZpMbVir3PnUDYIcuJ4FTPTwg2FPhSmFY8fKiY88CHgVjPTbYLZIG 2DoN2pg+fmyDdEaeyvxyZ3ave1nYE95eRU9EGOtwqubG3M0SowaS80jCKCoaP+xr8d/F u/BS/51tAH69n0fVHUIkW3sy1+PKVJM5fl3G4a4v1K5sZmTdaxUBjIqf1pDgxrGW407t enI3crDHWIDAWXZiLOIWDD7vKqkHVmkYAgjZ7o57kJL6DPyevUUImgJQN1FC2BK0aIa7 0h+w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1692643066; x=1693247866; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=pMLPduHfWGGT6ewh4Swe7Rx903irp7oZR0cyociTE+I=; b=MGTwUR5XGNNSZ9D7agds2S4AwBNiaPwTk1jgX430s/oulJENcCzQ6b5aqKG0DAyT18 ESbzF45iK3TbN/6aKn75Dr7XyDtUUKCxgyOuvQ1PlLM3/DzTu7QOxt2CMg+TkrBdVsdk 0oc9Ey+PVeNriC0lx83TvkZkV/7iDU6DbFdyZZzjfmG65iYDTTnCtXS6tB/TSo8Lcv0k qWy75jnUmEEYxQvI6Im8ZdM8AyHHvh/jZBDwd76gx5+P9u96OMhqyHnB/Y6m6hewz67K ZxShOPk1z+2stsnqdVGaKKXfCUctPoIEcbyg5SIyGRi9wyNqa5EXYEQhVKMUTUtkaPI1 fuQA== X-Gm-Message-State: AOJu0YzeOVJjiGQR9MAV3TMRnDwXX0/AMc1NrGHMCy3vQFpSQTiiCBtc rvSG59g6FjltKZltHsSadsv17w== X-Google-Smtp-Source: AGHT+IEfqgsX5taJcI3/zF6FUgi9VLti8jni51wseVE9vjrqfDRao4TKvZ/QW/DivW2ebVFb/vqJVg== X-Received: by 2002:a05:620a:4550:b0:76c:af30:3281 with SMTP id u16-20020a05620a455000b0076caf303281mr10650217qkp.10.1692643066118; Mon, 21 Aug 2023 11:37:46 -0700 (PDT) Received: from localhost (2603-7000-0c01-2716-699c-6fe1-d2a8-6a30.res6.spectrum.com. [2603:7000:c01:2716:699c:6fe1:d2a8:6a30]) by smtp.gmail.com with ESMTPSA id w13-20020a05620a148d00b0076d25b11b62sm2388467qkj.38.2023.08.21.11.37.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 21 Aug 2023 11:37:45 -0700 (PDT) From: Johannes Weiner To: Andrew Morton Cc: Vlastimil Babka , Mel Gorman , linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [PATCH 3/8] mm: page_alloc: fix highatomic landing on the wrong buddy list Date: Mon, 21 Aug 2023 14:33:35 -0400 Message-ID: <20230821183733.106619-4-hannes@cmpxchg.org> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20230821183733.106619-1-hannes@cmpxchg.org> References: <20230821183733.106619-1-hannes@cmpxchg.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" The following triggers from a custom debug check: [ 89.401754] page type is 3, passed migratetype is 1 (nr=3D8) [ 89.407930] WARNING: CPU: 2 PID: 75 at mm/page_alloc.c:706 __free_one_pa= ge+0x5ea/0x6b0 [ 89.415847] Modules linked in: [ 89.418902] CPU: 2 PID: 75 Comm: kswapd0 Not tainted 6.5.0-rc1-00013-g42= be896e9f77-dirty #233 [ 89.427415] Hardware name: Micro-Star International Co., Ltd. MS-7B98/Z3= 90-A PRO (MS-7B98), BIOS 1.80 12/25/2019 [ 89.437572] RIP: 0010:__free_one_page+0x5ea/0x6b0 [ 89.442271] Code: [ 89.461003] RSP: 0000:ffffc900001acea8 EFLAGS: 00010092 [ 89.466221] RAX: 0000000000000036 RBX: 0000000000000003 RCX: 00000000000= 00000 [ 89.473349] RDX: 0000000000000106 RSI: 0000000000000027 RDI: 00000000fff= fffff [ 89.480478] RBP: ffffffff82ca4780 R08: 0000000000000001 R09: 00000000000= 00000 [ 89.487601] R10: ffffffff8285d1e0 R11: ffffffff8285d1e0 R12: 00000000000= 00000 [ 89.494725] R13: 0000000000063448 R14: ffffea00018d1200 R15: 00000000000= 63401 [ 89.501853] FS: 0000000000000000(0000) GS:ffff88806e680000(0000) knlGS:= 0000000000000000 [ 89.509930] CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033 [ 89.515671] CR2: 00007fc66488b006 CR3: 00000000190b5001 CR4: 00000000003= 706e0 [ 89.522798] DR0: 0000000000000000 DR1: 0000000000000000 DR2: 00000000000= 00000 [ 89.529924] DR3: 0000000000000000 DR6: 00000000fffe0ff0 DR7: 00000000000= 00400 [ 89.537048] Call Trace: [ 89.539498] [ 89.541517] ? __free_one_page+0x5ea/0x6b0 [ 89.545619] ? __warn+0x7d/0x130 [ 89.548852] ? __free_one_page+0x5ea/0x6b0 [ 89.552946] ? report_bug+0x18d/0x1c0 [ 89.556607] ? handle_bug+0x3a/0x70 [ 89.560097] ? exc_invalid_op+0x13/0x60 [ 89.563933] ? asm_exc_invalid_op+0x16/0x20 [ 89.568113] ? __free_one_page+0x5ea/0x6b0 [ 89.572210] ? __free_one_page+0x5ea/0x6b0 [ 89.576306] ? refill_obj_stock+0xf5/0x1c0 [ 89.580399] free_one_page.constprop.0+0x5c/0xe0 This is a HIGHATOMIC page being freed to the MOVABLE buddy list. Highatomic pages have their own buddy freelists, but not their own pcplist. free_one_page() adjusts the migratetype so they can hitchhike on the MOVABLE pcplist. However, when the pcp trylock then fails, they're fed directly to the buddy list - with the incorrect type. Use MIGRATE_MOVABLE only for the pcp, not for the buddy bypass. Signed-off-by: Johannes Weiner --- mm/page_alloc.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/mm/page_alloc.c b/mm/page_alloc.c index 20973887999b..a5e36d186893 100644 --- a/mm/page_alloc.c +++ b/mm/page_alloc.c @@ -2403,7 +2403,7 @@ void free_unref_page(struct page *page, unsigned int = order) struct per_cpu_pages *pcp; struct zone *zone; unsigned long pfn =3D page_to_pfn(page); - int migratetype; + int migratetype, pcpmigratetype; =20 if (!free_pages_prepare(page, order, FPI_NONE)) return; @@ -2415,20 +2415,20 @@ void free_unref_page(struct page *page, unsigned in= t order) * areas back if necessary. Otherwise, we may have to free * excessively into the page allocator */ - migratetype =3D get_pfnblock_migratetype(page, pfn); + migratetype =3D pcpmigratetype =3D get_pfnblock_migratetype(page, pfn); if (unlikely(migratetype >=3D MIGRATE_PCPTYPES)) { if (unlikely(is_migrate_isolate(migratetype))) { free_one_page(page_zone(page), page, pfn, order, migratetype, FPI_NONE); return; } - migratetype =3D MIGRATE_MOVABLE; + pcpmigratetype =3D MIGRATE_MOVABLE; } =20 zone =3D page_zone(page); pcp_trylock_prepare(UP_flags); pcp =3D pcp_spin_trylock(zone->per_cpu_pageset); if (pcp) { - free_unref_page_commit(zone, pcp, page, migratetype, order); + free_unref_page_commit(zone, pcp, page, pcpmigratetype, order); pcp_spin_unlock(pcp); } else { free_one_page(zone, page, pfn, order, migratetype, FPI_NONE); --=20 2.41.0 From nobody Fri Dec 19 18:47:05 2025 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 2043BEE49A5 for ; Mon, 21 Aug 2023 18:38:04 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229878AbjHUSiE (ORCPT ); Mon, 21 Aug 2023 14:38:04 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39742 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229819AbjHUSh4 (ORCPT ); Mon, 21 Aug 2023 14:37:56 -0400 Received: from mail-qv1-xf2f.google.com (mail-qv1-xf2f.google.com [IPv6:2607:f8b0:4864:20::f2f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AEB845100 for ; Mon, 21 Aug 2023 11:37:50 -0700 (PDT) Received: by mail-qv1-xf2f.google.com with SMTP id 6a1803df08f44-64c1d487e72so19158606d6.0 for ; Mon, 21 Aug 2023 11:37:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cmpxchg-org.20221208.gappssmtp.com; s=20221208; t=1692643067; x=1693247867; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=lLUpAEhwdHses5nFOU0azVoQ7mhHkrTPDw+MACyT3Uo=; b=uu324QBc+rtb2rb6ojdEsAGPTmfCn1pVoKqBeTm/7P0dPa53c/018QiTZAxvjfk4tR gd3isXbWBnvmXwKnm/HQBQdv4CAkaT2elzDp9mjtKLdLitCtyeNEdOyJDD8OSdoNt9gt mDcPb4pJbQH+QjEmQqQQM2JBTmXebCbCMtWni1qZHwHNDASVRcK5jqvoscTv3vRHHDcn EEIaW0f4LPbjd1nU7A5uR+22sHHRvxqew4mEd3GgeorJsCcDp+bzTfVNcKiiZjTxI3MX GCyYm3Z2VbUNvA1yCSYnin8EXYwtYy2a0maw6rJNQldN7CHPfZ3e+BwVlaLApl+QVHTl V/3g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1692643067; x=1693247867; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=lLUpAEhwdHses5nFOU0azVoQ7mhHkrTPDw+MACyT3Uo=; b=X6521Q9yfSzaVAqnBtGfEzIve8HSnGnv0jG50H+qs/GINMgIw9qat/JZOxbzqtqGl0 8Ort+BX3oU53rTFV0hjfx4nX1VhwDqVAZSJR3jMMelqfgf1l4gs+TUblq2FSXnqPRcQ0 CznxGHHwjL8SfxIrWByAGnbYkXa/rmqEjXMr1emkyZTU6q50YN/5xLpudDeHCIQUN4O7 s6fEsaXJqliFoc96i2bSyH6eodzUJrYuqFlLjhaD+eXo+Rer7b6OgqSfULTqI6CKREEX c49xsYgPuDk4iO0EltAnaIXgknYB4BP+LXK0lMGpJSeC1z3Dj5sEcD0FJE2cA2+D8HHA pKEw== X-Gm-Message-State: AOJu0Yy8EsxnOGKaVSOzVOsojaP2iaLQ843PwvA2WSCEOY/1FUNoUMWb Y6HRZuJyvRYc6EfkqPqZjd5GUA== X-Google-Smtp-Source: AGHT+IEJOOhim3Cp793d/K2ahJpfEq1PXlo2PZVHOqjBu5gwxhsUCzov2/4CrV0FJmCuIF2w2QYyuA== X-Received: by 2002:a05:6214:192c:b0:647:39f1:5237 with SMTP id es12-20020a056214192c00b0064739f15237mr7472065qvb.47.1692643067350; Mon, 21 Aug 2023 11:37:47 -0700 (PDT) Received: from localhost (2603-7000-0c01-2716-699c-6fe1-d2a8-6a30.res6.spectrum.com. [2603:7000:c01:2716:699c:6fe1:d2a8:6a30]) by smtp.gmail.com with ESMTPSA id d1-20020a0cf0c1000000b0064f46c719fasm547661qvl.31.2023.08.21.11.37.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 21 Aug 2023 11:37:47 -0700 (PDT) From: Johannes Weiner To: Andrew Morton Cc: Vlastimil Babka , Mel Gorman , linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [PATCH 4/8] mm: page_alloc: fix up block types when merging compatible blocks Date: Mon, 21 Aug 2023 14:33:36 -0400 Message-ID: <20230821183733.106619-5-hannes@cmpxchg.org> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20230821183733.106619-1-hannes@cmpxchg.org> References: <20230821183733.106619-1-hannes@cmpxchg.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" The buddy allocator coalesces compatible blocks during freeing, but it doesn't update the types of the subblocks to match. When an allocation later breaks the chunk down again, its pieces will be put on freelists of the wrong type. This encourages incompatible page mixing (ask for one type, get another), and thus long-term fragmentation. Update the subblocks when merging a larger chunk, such that a later expand() will maintain freelist type hygiene. Signed-off-by: Johannes Weiner --- mm/page_alloc.c | 37 ++++++++++++++++++++++--------------- 1 file changed, 22 insertions(+), 15 deletions(-) diff --git a/mm/page_alloc.c b/mm/page_alloc.c index a5e36d186893..6c9f565b2613 100644 --- a/mm/page_alloc.c +++ b/mm/page_alloc.c @@ -438,6 +438,17 @@ void set_pageblock_migratetype(struct page *page, int = migratetype) page_to_pfn(page), MIGRATETYPE_MASK); } =20 +static void change_pageblock_range(struct page *pageblock_page, + int start_order, int migratetype) +{ + int nr_pageblocks =3D 1 << (start_order - pageblock_order); + + while (nr_pageblocks--) { + set_pageblock_migratetype(pageblock_page, migratetype); + pageblock_page +=3D pageblock_nr_pages; + } +} + #ifdef CONFIG_DEBUG_VM static int page_outside_zone_boundaries(struct zone *zone, struct page *pa= ge) { @@ -808,10 +819,17 @@ static inline void __free_one_page(struct page *page, */ int buddy_mt =3D get_pfnblock_migratetype(buddy, buddy_pfn); =20 - if (migratetype !=3D buddy_mt - && (!migratetype_is_mergeable(migratetype) || - !migratetype_is_mergeable(buddy_mt))) - goto done_merging; + if (migratetype !=3D buddy_mt) { + if (!migratetype_is_mergeable(migratetype) || + !migratetype_is_mergeable(buddy_mt)) + goto done_merging; + /* + * Match buddy type. This ensures that + * an expand() down the line puts the + * sub-blocks on the right freelists. + */ + set_pageblock_migratetype(buddy, migratetype); + } } =20 /* @@ -1687,17 +1705,6 @@ int move_freepages_block(struct zone *zone, struct p= age *page, num_movable); } =20 -static void change_pageblock_range(struct page *pageblock_page, - int start_order, int migratetype) -{ - int nr_pageblocks =3D 1 << (start_order - pageblock_order); - - while (nr_pageblocks--) { - set_pageblock_migratetype(pageblock_page, migratetype); - pageblock_page +=3D pageblock_nr_pages; - } -} - /* * When we are falling back to another migratetype during allocation, try = to * steal extra free pages from the same pageblocks to satisfy further --=20 2.41.0 From nobody Fri Dec 19 18:47:05 2025 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 05A2EEE49A6 for ; Mon, 21 Aug 2023 18:38:03 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229886AbjHUSiC (ORCPT ); Mon, 21 Aug 2023 14:38:02 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39736 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229790AbjHUSh4 (ORCPT ); Mon, 21 Aug 2023 14:37:56 -0400 Received: from mail-qv1-xf2f.google.com (mail-qv1-xf2f.google.com [IPv6:2607:f8b0:4864:20::f2f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CF4281BFE for ; Mon, 21 Aug 2023 11:37:51 -0700 (PDT) Received: by mail-qv1-xf2f.google.com with SMTP id 6a1803df08f44-649921ec030so22012706d6.1 for ; Mon, 21 Aug 2023 11:37:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cmpxchg-org.20221208.gappssmtp.com; s=20221208; t=1692643068; x=1693247868; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=ZngYrrpTqTVsoxTWaftFpu0t3D1kL65SFXqeVP8LffY=; b=tTKq6l6aHUSFxqQAsy9JwsqI5eXHQGdiD0FODZRCZUUeuFzk7B/PGOmI2Vs9szLK+G dEC4uoaY+bYkZk7tXrTTN84Ya1UWN1T1ntmz4BwiWpAaTDv/KjLtiU8xN7cCLXugYnxj LJbOr/gs/OUqybReoZoBcLxK7V0Pt3TD22y3t7PTjTmBG3+EYcaWeiiG5KKdHp8G7xu4 gGQs8hxtzZGVFfnPYT9iv+Ahh/wRyldbmPvTOfU+kCq2MYvvfryQ4MlloTXLfKY2VBzt kO8BdLOxDu9kD/rqKeB1BSsNV0xnrL1jQgNg74XoHP6TFhE2QWb0o5caPuzLs4GeMiZG eWpA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1692643068; x=1693247868; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=ZngYrrpTqTVsoxTWaftFpu0t3D1kL65SFXqeVP8LffY=; b=Djzx/egmaC5AXsqoBzTHjEMkaSx0T3QGylLhmjZd6aEKlcXgK0A21VF6F0x/4yrXX0 GSEdvHi7g9KW7y5JX6Q/l/KnOQ5GPRG+DbkPrv4Du9JL14F4dr6t5NMDNP1XQEUBIAbA /lXBZB+vbdqEU8eTsgmoygCS1+b5qaKlLRNMp8ZAP+zrAgaWbW7zvrWkh8wzSVVl0+1e aN8UhA5l1hAgU1pfRlIP2Bcsbc09suPknb1xYEKfkhiFawirAegBoaI9EQXqUkXFSkd6 QH+dwjbEXgaA5jLihL9whurcVCwcXiVRtuva6mSlYpnnMqsCzrz99v9mFfoSP6QZJm4w 5sxQ== X-Gm-Message-State: AOJu0YydjYK6qFFbBRNAM5VNI/jw9JhHUAMeBXlE6m7n6+SI1wINqn0/ sdRFJVdC2nfcqB5h5ijSGnBKSw== X-Google-Smtp-Source: AGHT+IEE9dNMT5yZrqH0G+cbatGuZK0wFc+nYEOict7vJbVhdu8eLSChdSchtQ2QFxRxmtKi+qryTw== X-Received: by 2002:a0c:f092:0:b0:63f:bf70:678f with SMTP id g18-20020a0cf092000000b0063fbf70678fmr8857363qvk.59.1692643068549; Mon, 21 Aug 2023 11:37:48 -0700 (PDT) Received: from localhost (2603-7000-0c01-2716-699c-6fe1-d2a8-6a30.res6.spectrum.com. [2603:7000:c01:2716:699c:6fe1:d2a8:6a30]) by smtp.gmail.com with ESMTPSA id h16-20020ae9ec10000000b0076cc7219ac9sm2650582qkg.7.2023.08.21.11.37.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 21 Aug 2023 11:37:48 -0700 (PDT) From: Johannes Weiner To: Andrew Morton Cc: Vlastimil Babka , Mel Gorman , linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [PATCH 5/8] mm: page_alloc: move free pages when converting block during isolation Date: Mon, 21 Aug 2023 14:33:37 -0400 Message-ID: <20230821183733.106619-6-hannes@cmpxchg.org> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20230821183733.106619-1-hannes@cmpxchg.org> References: <20230821183733.106619-1-hannes@cmpxchg.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" When claiming a block during compaction isolation, move any remaining free pages to the correct freelists as well, instead of stranding them on the wrong list. Otherwise, this encourages incompatible page mixing down the line, and thus long-term fragmentation. Signed-off-by: Johannes Weiner --- mm/page_alloc.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/mm/page_alloc.c b/mm/page_alloc.c index 6c9f565b2613..6a4004f07123 100644 --- a/mm/page_alloc.c +++ b/mm/page_alloc.c @@ -2586,9 +2586,12 @@ int __isolate_free_page(struct page *page, unsigned = int order) * Only change normal pageblocks (i.e., they can merge * with others) */ - if (migratetype_is_mergeable(mt)) + if (migratetype_is_mergeable(mt)) { set_pageblock_migratetype(page, MIGRATE_MOVABLE); + move_freepages_block(zone, page, + MIGRATE_MOVABLE, NULL); + } } } =20 --=20 2.41.0 From nobody Fri Dec 19 18:47:05 2025 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 DE718EE4996 for ; Mon, 21 Aug 2023 18:38:06 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229908AbjHUSiF (ORCPT ); Mon, 21 Aug 2023 14:38:05 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39808 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229794AbjHUSh6 (ORCPT ); Mon, 21 Aug 2023 14:37:58 -0400 Received: from mail-qk1-x733.google.com (mail-qk1-x733.google.com [IPv6:2607:f8b0:4864:20::733]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 890BE58EE1 for ; Mon, 21 Aug 2023 11:37:55 -0700 (PDT) Received: by mail-qk1-x733.google.com with SMTP id af79cd13be357-76d873c6e8aso240609785a.1 for ; Mon, 21 Aug 2023 11:37:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cmpxchg-org.20221208.gappssmtp.com; s=20221208; t=1692643070; x=1693247870; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=OQIFQGhOFbBlf64eoEhx/6zam9QXvcRrDKvqgUYhMRM=; b=t+wh1mGsSpug9tSfvVbtQ0+ZT7w0zsIr5mQvD0qoxacPfoOB+g2smHBIaDUAnqGCFs nIfnUdLtFYhQBZzhdHCTMaFYxsrPCFH1JfaNCChGC9YbwVfc039vhqD/6cfA3MYQnjXH VnrReFpdUByuwNsccH6RNDOmypzGn9sVNUCpx57arQCc0x4tVqKXgIGYkDFV9KBaqjrQ pxv3g/whFqiiwVnugwxvXEqJIJv2e0lbpLu7n6wEiONmvwSrNdWgKg0k0M5tjjCMTiqP 8klZP0Q9u44cm9eKrDYTJStOMjv0qClBRgbJJF2O0USOGCxQqFT+VQ3RhL0tCbvB0de1 67lw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1692643070; x=1693247870; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=OQIFQGhOFbBlf64eoEhx/6zam9QXvcRrDKvqgUYhMRM=; b=HOKsugsnqNYHgfnv73uKdLGGRqKevon6PpGDhiUHxwZoLKk+SiGV0ou6gUiSC5bfx4 VhkmCt4WdIGzp49J8+xeXDbbi8iWAf6F1mdd0gfl4Pp/yLbr6rCuO8GOrn26m3Wfzu7H Q7k/YkBxRl5xiK5+6puYAR9P94O2cTg97EqTJYn009DgVPrfDRTgRc0QwWRA/P8SFbKH qgUq11CM1dvZKYEbfhQyyb2h2BA3Ibhc4Ukt4KuMb7zn++T4mCREycTLMIgVVLDq5zKg kQyu3VUDG+4w9WCHPAg8tT34N1uV1+t2LvUNT7R2BJeL/s7Gw/XufC9bZkkOkF/TJPGI jLQw== X-Gm-Message-State: AOJu0Yzs+QzPhc+uzkoFj87XFGjUHqWyp+1B9VPz+IFpH8l5q/MVHPtI 1FLZFCybZ/DB3/cs6pLkisbWwkxtxN6B/55BIQUnWw== X-Google-Smtp-Source: AGHT+IE91JFTBmOR/4UxoEL0MhPPZyAeK/R20HWilTQEJg9pfeBIBxkRJCzCQ4B/j3c4553iIqhoEg== X-Received: by 2002:a05:620a:4407:b0:76c:c90d:2ef0 with SMTP id v7-20020a05620a440700b0076cc90d2ef0mr10398847qkp.32.1692643069805; Mon, 21 Aug 2023 11:37:49 -0700 (PDT) Received: from localhost (2603-7000-0c01-2716-699c-6fe1-d2a8-6a30.res6.spectrum.com. [2603:7000:c01:2716:699c:6fe1:d2a8:6a30]) by smtp.gmail.com with ESMTPSA id o10-20020a05620a130a00b00767c961eb47sm2648001qkj.43.2023.08.21.11.37.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 21 Aug 2023 11:37:49 -0700 (PDT) From: Johannes Weiner To: Andrew Morton Cc: Vlastimil Babka , Mel Gorman , linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [PATCH 6/8] mm: page_alloc: fix move_freepages_block() range error Date: Mon, 21 Aug 2023 14:33:38 -0400 Message-ID: <20230821183733.106619-7-hannes@cmpxchg.org> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20230821183733.106619-1-hannes@cmpxchg.org> References: <20230821183733.106619-1-hannes@cmpxchg.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" When a block is partially outside the zone of the cursor page, the function cuts the range to the pivot page instead of the zone start. This can leave large parts of the block behind, which encourages incompatible page mixing down the line (ask for one type, get another), and thus long-term fragmentation. Signed-off-by: Johannes Weiner --- mm/page_alloc.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mm/page_alloc.c b/mm/page_alloc.c index 6a4004f07123..6fcda8e96f16 100644 --- a/mm/page_alloc.c +++ b/mm/page_alloc.c @@ -1697,7 +1697,7 @@ int move_freepages_block(struct zone *zone, struct pa= ge *page, =20 /* Do not cross zone boundaries */ if (!zone_spans_pfn(zone, start_pfn)) - start_pfn =3D pfn; + start_pfn =3D zone->zone_start_pfn; if (!zone_spans_pfn(zone, end_pfn)) return 0; =20 --=20 2.41.0 From nobody Fri Dec 19 18:47:05 2025 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 5744BEE49A6 for ; Mon, 21 Aug 2023 18:38:09 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229461AbjHUSiH (ORCPT ); Mon, 21 Aug 2023 14:38:07 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39848 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229865AbjHUSiA (ORCPT ); Mon, 21 Aug 2023 14:38:00 -0400 Received: from mail-vs1-xe32.google.com (mail-vs1-xe32.google.com [IPv6:2607:f8b0:4864:20::e32]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C7DD25708D for ; Mon, 21 Aug 2023 11:37:56 -0700 (PDT) Received: by mail-vs1-xe32.google.com with SMTP id ada2fe7eead31-44bfe8bcf78so809264137.2 for ; Mon, 21 Aug 2023 11:37:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cmpxchg-org.20221208.gappssmtp.com; s=20221208; t=1692643071; x=1693247871; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=8oIQcd8rRXP7RBwvXlPFe1yS+MOvZC6u1A+9FyFyqFE=; b=kgMeXntxKQR8tuNiSCez1fknacAOE/h+FkxoUZt3pChYph14NTG2kuLWqggZuVBkgz 1TPK0+Ib0YstWWTPm5g5jrkWXyurGI517D2VqyFPKaUup4SiiUF/iYe6sozGLCAbGusy Cuk/X/7jw5JoTa4djmj1/aXJFiK3LI5OM9K8lOShOFTz4qROpE3sQQ4oE8kOOxG2S7TT 36JcX9dQCxx4kGv+up4S225n4jA/0jes8XxNH9tHySvNQwEOa8lCM6bYsyxq+iKXNyki Z2sTPO3iUOXqC/u+HUrO5E8bFfklkWce1iO9kAY5GZ36G3sBYdwZeb6JsZTJ16647Ac3 wblg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1692643071; x=1693247871; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=8oIQcd8rRXP7RBwvXlPFe1yS+MOvZC6u1A+9FyFyqFE=; b=KJVJDlNq8eLEe94fhlnQc6OpVjbecLVTdX2gGIkt6AHoEXvN5NvvkqfpA1T7l5J5JU uUXnW+oFeMwn+RPOVX1lRgOZt8an1NBWElhbrBv6A0F1ULnGN6h+/n70SYQMyfkY3rGC /dej14VOVbZf2gE88vSRF3FIesJLxVX6YCOBADPkbjnHQ3jX1NXF65b1bHh+zOcYCTQ8 zkJ/QXQNidgTYMS5mD30HrsVMputskZlIgLWzhBv2I2zjvMTthJNjGqzGtQZpYtfN+3J SydtML7JQDz4nyfbTA0YuOnCZloCVNSBroYj8oWfUz6wSHH9CrM9EGxy7SQUXZzJuXwM jHiQ== X-Gm-Message-State: AOJu0YxlgV5+fO8BXRsOjgpgnzog2GrpENHKNpIjowhjvLo5dnWj8Cqr iM+0T0dy2UqXwy80vAQNss1SRw== X-Google-Smtp-Source: AGHT+IHy2SwpHDcKAyBf07pwHL85neqFey+ZV007pcfVvgRBdnkAqr4gl581JRzxxc13F3O06WJlyg== X-Received: by 2002:a67:ebd7:0:b0:44d:4553:4fd8 with SMTP id y23-20020a67ebd7000000b0044d45534fd8mr2148296vso.18.1692643071064; Mon, 21 Aug 2023 11:37:51 -0700 (PDT) Received: from localhost (2603-7000-0c01-2716-699c-6fe1-d2a8-6a30.res6.spectrum.com. [2603:7000:c01:2716:699c:6fe1:d2a8:6a30]) by smtp.gmail.com with ESMTPSA id c16-20020a0cca10000000b0064b502fdeecsm2625786qvk.68.2023.08.21.11.37.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 21 Aug 2023 11:37:50 -0700 (PDT) From: Johannes Weiner To: Andrew Morton Cc: Vlastimil Babka , Mel Gorman , linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [PATCH 7/8] mm: page_alloc: fix freelist movement during block conversion Date: Mon, 21 Aug 2023 14:33:39 -0400 Message-ID: <20230821183733.106619-8-hannes@cmpxchg.org> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20230821183733.106619-1-hannes@cmpxchg.org> References: <20230821183733.106619-1-hannes@cmpxchg.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Currently, page block type conversion during fallbacks, atomic reservations and isolation can strand various amounts of free pages on incorrect freelists. For example, fallback stealing moves free pages in the block to the new type's freelists, but then may not actually claim the block for that type if there aren't enough compatible pages already allocated. In all cases, free page moving might fail if the block straddles more than one zone, in which case no free pages are moved at all, but the block type is changed anyway. This is detrimental to type hygiene on the freelists. It encourages incompatible page mixing down the line (ask for one type, get another) and thus contributes to long-term fragmentation. Split the process into a proper transaction: check first if conversion will happen, then try to move the free pages, and only if that was successful convert the block to the new type. Signed-off-by: Johannes Weiner --- include/linux/page-isolation.h | 3 +- mm/page_alloc.c | 176 ++++++++++++++++++++------------- mm/page_isolation.c | 22 +++-- 3 files changed, 121 insertions(+), 80 deletions(-) diff --git a/include/linux/page-isolation.h b/include/linux/page-isolation.h index 4ac34392823a..8550b3c91480 100644 --- a/include/linux/page-isolation.h +++ b/include/linux/page-isolation.h @@ -34,8 +34,7 @@ static inline bool is_migrate_isolate(int migratetype) #define REPORT_FAILURE 0x2 =20 void set_pageblock_migratetype(struct page *page, int migratetype); -int move_freepages_block(struct zone *zone, struct page *page, - int migratetype, int *num_movable); +int move_freepages_block(struct zone *zone, struct page *page, int migrate= type); =20 int start_isolate_page_range(unsigned long start_pfn, unsigned long end_pf= n, int migratetype, int flags, gfp_t gfp_flags); diff --git a/mm/page_alloc.c b/mm/page_alloc.c index 6fcda8e96f16..42b62832323f 100644 --- a/mm/page_alloc.c +++ b/mm/page_alloc.c @@ -1646,9 +1646,8 @@ static inline struct page *__rmqueue_cma_fallback(str= uct zone *zone, * Note that start_page and end_pages are not aligned on a pageblock * boundary. If alignment is required, use move_freepages_block() */ -static int move_freepages(struct zone *zone, - unsigned long start_pfn, unsigned long end_pfn, - int migratetype, int *num_movable) +static int move_freepages(struct zone *zone, unsigned long start_pfn, + unsigned long end_pfn, int migratetype) { struct page *page; unsigned long pfn; @@ -1658,14 +1657,6 @@ static int move_freepages(struct zone *zone, for (pfn =3D start_pfn; pfn <=3D end_pfn;) { page =3D pfn_to_page(pfn); if (!PageBuddy(page)) { - /* - * We assume that pages that could be isolated for - * migration are movable. But we don't actually try - * isolating, as that would be expensive. - */ - if (num_movable && - (PageLRU(page) || __PageMovable(page))) - (*num_movable)++; pfn++; continue; } @@ -1683,26 +1674,62 @@ static int move_freepages(struct zone *zone, return pages_moved; } =20 -int move_freepages_block(struct zone *zone, struct page *page, - int migratetype, int *num_movable) +static bool prep_move_freepages_block(struct zone *zone, struct page *page, + unsigned long *start_pfn, + unsigned long *end_pfn, + int *num_free, int *num_movable) { - unsigned long start_pfn, end_pfn, pfn; - - if (num_movable) - *num_movable =3D 0; + unsigned long pfn, start, end; =20 pfn =3D page_to_pfn(page); - start_pfn =3D pageblock_start_pfn(pfn); - end_pfn =3D pageblock_end_pfn(pfn) - 1; + start =3D pageblock_start_pfn(pfn); + end =3D pageblock_end_pfn(pfn) - 1; =20 /* Do not cross zone boundaries */ - if (!zone_spans_pfn(zone, start_pfn)) - start_pfn =3D zone->zone_start_pfn; - if (!zone_spans_pfn(zone, end_pfn)) - return 0; + if (!zone_spans_pfn(zone, start)) + start =3D zone->zone_start_pfn; + if (!zone_spans_pfn(zone, end)) + return false; + + *start_pfn =3D start; + *end_pfn =3D end; + + if (num_free) { + *num_free =3D 0; + *num_movable =3D 0; + for (pfn =3D start; pfn <=3D end;) { + page =3D pfn_to_page(pfn); + if (PageBuddy(page)) { + int nr =3D 1 << buddy_order(page); + + *num_free +=3D nr; + pfn +=3D nr; + continue; + } + /* + * We assume that pages that could be isolated for + * migration are movable. But we don't actually try + * isolating, as that would be expensive. + */ + if (PageLRU(page) || __PageMovable(page)) + (*num_movable)++; + pfn++; + } + } + + return true; +} =20 - return move_freepages(zone, start_pfn, end_pfn, migratetype, - num_movable); +int move_freepages_block(struct zone *zone, struct page *page, + int migratetype) +{ + unsigned long start_pfn, end_pfn; + + if (!prep_move_freepages_block(zone, page, &start_pfn, &end_pfn, + NULL, NULL)) + return -1; + + return move_freepages(zone, start_pfn, end_pfn, migratetype); } =20 /* @@ -1776,33 +1803,36 @@ static inline bool boost_watermark(struct zone *zon= e) } =20 /* - * This function implements actual steal behaviour. If order is large enou= gh, - * we can steal whole pageblock. If not, we first move freepages in this - * pageblock to our migratetype and determine how many already-allocated p= ages - * are there in the pageblock with a compatible migratetype. If at least h= alf - * of pages are free or compatible, we can change migratetype of the pageb= lock - * itself, so pages freed in the future will be put on the correct free li= st. + * This function implements actual steal behaviour. If order is large enou= gh, we + * can claim the whole pageblock for the requested migratetype. If not, we= check + * the pageblock for constituent pages; if at least half of the pages are = free + * or compatible, we can still claim the whole block, so pages freed in the + * future will be put on the correct free list. Otherwise, we isolate exac= tly + * the order we need from the fallback block and leave its migratetype alo= ne. */ static void steal_suitable_fallback(struct zone *zone, struct page *page, - unsigned int alloc_flags, int start_type, bool whole_block) + int current_order, int order, int start_type, + unsigned int alloc_flags, bool whole_block) { - unsigned int current_order =3D buddy_order(page); int free_pages, movable_pages, alike_pages; - int old_block_type; + unsigned long start_pfn, end_pfn; + int block_type; =20 - old_block_type =3D get_pageblock_migratetype(page); + block_type =3D get_pageblock_migratetype(page); =20 /* * This can happen due to races and we want to prevent broken * highatomic accounting. */ - if (is_migrate_highatomic(old_block_type)) + if (is_migrate_highatomic(block_type)) goto single_page; =20 /* Take ownership for orders >=3D pageblock_order */ if (current_order >=3D pageblock_order) { + del_page_from_free_list(page, zone, current_order); change_pageblock_range(page, current_order, start_type); - goto single_page; + expand(zone, page, order, current_order, start_type); + return; } =20 /* @@ -1817,8 +1847,11 @@ static void steal_suitable_fallback(struct zone *zon= e, struct page *page, if (!whole_block) goto single_page; =20 - free_pages =3D move_freepages_block(zone, page, start_type, - &movable_pages); + /* moving whole block can fail due to zone boundary conditions */ + if (!prep_move_freepages_block(zone, page, &start_pfn, &end_pfn, + &free_pages, &movable_pages)) + goto single_page; + /* * Determine how many pages are compatible with our allocation. * For movable allocation, it's the number of movable pages which @@ -1834,29 +1867,27 @@ static void steal_suitable_fallback(struct zone *zo= ne, struct page *page, * vice versa, be conservative since we can't distinguish the * exact migratetype of non-movable pages. */ - if (old_block_type =3D=3D MIGRATE_MOVABLE) + if (block_type =3D=3D MIGRATE_MOVABLE) alike_pages =3D pageblock_nr_pages - (free_pages + movable_pages); else alike_pages =3D 0; } =20 - /* moving whole block can fail due to zone boundary conditions */ - if (!free_pages) - goto single_page; - /* * If a sufficient number of pages in the block are either free or of * comparable migratability as our allocation, claim the whole block. */ if (free_pages + alike_pages >=3D (1 << (pageblock_order-1)) || - page_group_by_mobility_disabled) + page_group_by_mobility_disabled) { + move_freepages(zone, start_pfn, end_pfn, start_type); set_pageblock_migratetype(page, start_type); - - return; + block_type =3D start_type; + } =20 single_page: - move_to_free_list(page, zone, current_order, start_type); + del_page_from_free_list(page, zone, current_order); + expand(zone, page, order, current_order, block_type); } =20 /* @@ -1921,9 +1952,10 @@ static void reserve_highatomic_pageblock(struct page= *page, struct zone *zone, mt =3D get_pageblock_migratetype(page); /* Only reserve normal pageblocks (i.e., they can merge with others) */ if (migratetype_is_mergeable(mt)) { - zone->nr_reserved_highatomic +=3D pageblock_nr_pages; - set_pageblock_migratetype(page, MIGRATE_HIGHATOMIC); - move_freepages_block(zone, page, MIGRATE_HIGHATOMIC, NULL); + if (move_freepages_block(zone, page, MIGRATE_HIGHATOMIC) !=3D -1) { + set_pageblock_migratetype(page, MIGRATE_HIGHATOMIC); + zone->nr_reserved_highatomic +=3D pageblock_nr_pages; + } } =20 out_unlock: @@ -1948,7 +1980,7 @@ static bool unreserve_highatomic_pageblock(const stru= ct alloc_context *ac, struct zone *zone; struct page *page; int order; - bool ret; + int ret; =20 for_each_zone_zonelist_nodemask(zone, z, zonelist, ac->highest_zoneidx, ac->nodemask) { @@ -1997,10 +2029,14 @@ static bool unreserve_highatomic_pageblock(const st= ruct alloc_context *ac, * of pageblocks that cannot be completely freed * may increase. */ + ret =3D move_freepages_block(zone, page, ac->migratetype); + /* + * Reserving this block already succeeded, so this should + * not fail on zone boundaries. + */ + WARN_ON_ONCE(ret =3D=3D -1); set_pageblock_migratetype(page, ac->migratetype); - ret =3D move_freepages_block(zone, page, ac->migratetype, - NULL); - if (ret) { + if (ret > 0) { spin_unlock_irqrestore(&zone->lock, flags); return ret; } @@ -2021,7 +2057,7 @@ static bool unreserve_highatomic_pageblock(const stru= ct alloc_context *ac, * deviation from the rest of this file, to make the for loop * condition simpler. */ -static __always_inline bool +static __always_inline struct page * __rmqueue_fallback(struct zone *zone, int order, int start_migratetype, unsigned int alloc_flags) { @@ -2068,7 +2104,7 @@ __rmqueue_fallback(struct zone *zone, int order, int = start_migratetype, goto do_steal; } =20 - return false; + return NULL; =20 find_smallest: for (current_order =3D order; current_order <=3D MAX_ORDER; @@ -2089,13 +2125,14 @@ __rmqueue_fallback(struct zone *zone, int order, in= t start_migratetype, do_steal: page =3D get_page_from_free_area(area, fallback_mt); =20 - steal_suitable_fallback(zone, page, alloc_flags, start_migratetype, - can_steal); + /* take off list, maybe claim block, expand remainder */ + steal_suitable_fallback(zone, page, current_order, order, + start_migratetype, alloc_flags, can_steal); =20 trace_mm_page_alloc_extfrag(page, order, current_order, start_migratetype, fallback_mt); =20 - return true; + return page; =20 } =20 @@ -2123,15 +2160,14 @@ __rmqueue(struct zone *zone, unsigned int order, in= t migratetype, return page; } } -retry: + page =3D __rmqueue_smallest(zone, order, migratetype); if (unlikely(!page)) { if (alloc_flags & ALLOC_CMA) page =3D __rmqueue_cma_fallback(zone, order); - - if (!page && __rmqueue_fallback(zone, order, migratetype, - alloc_flags)) - goto retry; + else + page =3D __rmqueue_fallback(zone, order, migratetype, + alloc_flags); } return page; } @@ -2586,12 +2622,10 @@ int __isolate_free_page(struct page *page, unsigned= int order) * Only change normal pageblocks (i.e., they can merge * with others) */ - if (migratetype_is_mergeable(mt)) { - set_pageblock_migratetype(page, - MIGRATE_MOVABLE); - move_freepages_block(zone, page, - MIGRATE_MOVABLE, NULL); - } + if (migratetype_is_mergeable(mt) && + move_freepages_block(zone, page, + MIGRATE_MOVABLE) !=3D -1) + set_pageblock_migratetype(page, MIGRATE_MOVABLE); } } =20 diff --git a/mm/page_isolation.c b/mm/page_isolation.c index 6599cc965e21..f5e4d8676b36 100644 --- a/mm/page_isolation.c +++ b/mm/page_isolation.c @@ -178,15 +178,18 @@ static int set_migratetype_isolate(struct page *page,= int migratetype, int isol_ unmovable =3D has_unmovable_pages(check_unmovable_start, check_unmovable_= end, migratetype, isol_flags); if (!unmovable) { - unsigned long nr_pages; + int nr_pages; int mt =3D get_pageblock_migratetype(page); =20 + nr_pages =3D move_freepages_block(zone, page, MIGRATE_ISOLATE); + /* Block spans zone boundaries? */ + if (nr_pages =3D=3D -1) { + spin_unlock_irqrestore(&zone->lock, flags); + return -EBUSY; + } + __mod_zone_freepage_state(zone, -nr_pages, mt); set_pageblock_migratetype(page, MIGRATE_ISOLATE); zone->nr_isolate_pageblock++; - nr_pages =3D move_freepages_block(zone, page, MIGRATE_ISOLATE, - NULL); - - __mod_zone_freepage_state(zone, -nr_pages, mt); spin_unlock_irqrestore(&zone->lock, flags); return 0; } @@ -206,7 +209,7 @@ static int set_migratetype_isolate(struct page *page, i= nt migratetype, int isol_ static void unset_migratetype_isolate(struct page *page, int migratetype) { struct zone *zone; - unsigned long flags, nr_pages; + unsigned long flags; bool isolated_page =3D false; unsigned int order; struct page *buddy; @@ -252,7 +255,12 @@ static void unset_migratetype_isolate(struct page *pag= e, int migratetype) * allocation. */ if (!isolated_page) { - nr_pages =3D move_freepages_block(zone, page, migratetype, NULL); + int nr_pages =3D move_freepages_block(zone, page, migratetype); + /* + * Isolating this block already succeeded, so this + * should not fail on zone boundaries. + */ + WARN_ON_ONCE(nr_pages =3D=3D -1); __mod_zone_freepage_state(zone, nr_pages, migratetype); } set_pageblock_migratetype(page, migratetype); --=20 2.41.0 From nobody Fri Dec 19 18:47:05 2025 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 E9378EE4996 for ; Mon, 21 Aug 2023 18:38:11 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229669AbjHUSiL (ORCPT ); Mon, 21 Aug 2023 14:38:11 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39782 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229899AbjHUSiC (ORCPT ); Mon, 21 Aug 2023 14:38:02 -0400 Received: from mail-qk1-x730.google.com (mail-qk1-x730.google.com [IPv6:2607:f8b0:4864:20::730]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BABC959C82 for ; Mon, 21 Aug 2023 11:37:57 -0700 (PDT) Received: by mail-qk1-x730.google.com with SMTP id af79cd13be357-76d97f2e4d9so192659485a.2 for ; Mon, 21 Aug 2023 11:37:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cmpxchg-org.20221208.gappssmtp.com; s=20221208; t=1692643072; x=1693247872; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=KxZ7t08TIrc+nfomCwl0X3uqrfJJ7PwywgjcrMZlbi4=; b=Hv6JrnJ6V8xuEDJa3YO+/EAdKgVXjUawjQKVhtDEApvSkf61q9dP+cH0fMYe//Q6zE hGU8CwcaZ0GrasBu602xcZAM1F3oD18xLDTx9i8YuzkFW4j2/Ifokl44IiOM6yi1IGVx FSNA9GqWSffR/n2ILgpDYKnD9UlztfhC4NUmWu3FML/ayyN4rKP5NPlM7t99J+p6Dwqj Tmvs+6gxkJqwN+yzbbK3P6eStBj8lWYzEszbNyxh3GDwqyvJd5YIIVv0h3wHg9O8zJUi QG9KJ2AFzvEWI+WaN8MIhqeyMdkwB1nYuiHU/QvtBr9MBVT0WUDH5G4C4LhL0LXOHih8 wVDQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1692643072; x=1693247872; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=KxZ7t08TIrc+nfomCwl0X3uqrfJJ7PwywgjcrMZlbi4=; b=WqR5WhzGbZw7c/5hYYBisxJj5UclPUXMjOpoFQpwCqcfsuCcJE/LerHN45S+mAt1el yGgL94jhrHAb16uXEP4QtV+JwuZhPgSBCT1CCfI4i5zyY5urDJ3VQ4NDOof7emenj2n3 LYETYaJ2pjDodCSYjST+szCDOs1b8SZghETVvuW38+JFTvoUm9s8G6nOEAFi1YkEKq16 oKwwVEGS5r69TaedOjMEu+Fj+6TJuc+FSIIcNoHOkW6LQWbA/KpkValvhenVaMj1lsg/ iirLd7mFi+lrYzfV+LcgNchuOMe8RmPdfdLuKmfe3Z62s8PPlBtYTLNAA+Kvjagn1bCO hvNg== X-Gm-Message-State: AOJu0YwJswiMSOoXCL1AJHx3xBD1xHm8T+XI/FsTsLmCNpL/60TleUmH mM/znaSpoaunFRU9clCqL+9SyQ== X-Google-Smtp-Source: AGHT+IGf/VsA5bYRO1YSeXVGrKwk/5rrwKmMa2FfmY7ONUlt87KHnSp0Wr2la0XDsPTpH9xoZBIv3A== X-Received: by 2002:a05:620a:45ab:b0:76c:7f5e:3888 with SMTP id bp43-20020a05620a45ab00b0076c7f5e3888mr10747099qkb.50.1692643072396; Mon, 21 Aug 2023 11:37:52 -0700 (PDT) Received: from localhost (2603-7000-0c01-2716-699c-6fe1-d2a8-6a30.res6.spectrum.com. [2603:7000:c01:2716:699c:6fe1:d2a8:6a30]) by smtp.gmail.com with ESMTPSA id c14-20020a05620a11ae00b0075b2af4a076sm2664314qkk.16.2023.08.21.11.37.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 21 Aug 2023 11:37:52 -0700 (PDT) From: Johannes Weiner To: Andrew Morton Cc: Vlastimil Babka , Mel Gorman , linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [PATCH 8/8] mm: page_alloc: consolidate free page accounting Date: Mon, 21 Aug 2023 14:33:40 -0400 Message-ID: <20230821183733.106619-9-hannes@cmpxchg.org> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20230821183733.106619-1-hannes@cmpxchg.org> References: <20230821183733.106619-1-hannes@cmpxchg.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Free page accounting currently happens a bit too high up the call stack, where it has to deal with guard pages, compaction capturing, block stealing and even page isolation. This is subtle and fragile, and makes it difficult to hack on the code. Push the accounting down to where pages enter and leave the physical freelists, where all these higher-level exceptions are of no concern. v2: - fix CONFIG_DEBUG_PAGEALLOC build (Mel) Signed-off-by: Johannes Weiner --- include/linux/mm.h | 18 ++--- include/linux/page-isolation.h | 3 +- include/linux/vmstat.h | 8 -- mm/debug_page_alloc.c | 12 +-- mm/internal.h | 5 -- mm/page_alloc.c | 131 ++++++++++++++++++--------------- mm/page_isolation.c | 7 +- 7 files changed, 88 insertions(+), 96 deletions(-) diff --git a/include/linux/mm.h b/include/linux/mm.h index 406ab9ea818f..950c400ac53b 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -3550,24 +3550,22 @@ static inline bool page_is_guard(struct page *page) return PageGuard(page); } =20 -bool __set_page_guard(struct zone *zone, struct page *page, unsigned int o= rder, - int migratetype); +bool __set_page_guard(struct zone *zone, struct page *page, unsigned int o= rder); static inline bool set_page_guard(struct zone *zone, struct page *page, - unsigned int order, int migratetype) + unsigned int order) { if (!debug_guardpage_enabled()) return false; - return __set_page_guard(zone, page, order, migratetype); + return __set_page_guard(zone, page, order); } =20 -void __clear_page_guard(struct zone *zone, struct page *page, unsigned int= order, - int migratetype); +void __clear_page_guard(struct zone *zone, struct page *page, unsigned int= order); static inline void clear_page_guard(struct zone *zone, struct page *page, - unsigned int order, int migratetype) + unsigned int order) { if (!debug_guardpage_enabled()) return; - __clear_page_guard(zone, page, order, migratetype); + __clear_page_guard(zone, page, order); } =20 #else /* CONFIG_DEBUG_PAGEALLOC */ @@ -3577,9 +3575,9 @@ static inline unsigned int debug_guardpage_minorder(v= oid) { return 0; } static inline bool debug_guardpage_enabled(void) { return false; } static inline bool page_is_guard(struct page *page) { return false; } static inline bool set_page_guard(struct zone *zone, struct page *page, - unsigned int order, int migratetype) { return false; } + unsigned int order) { return false; } static inline void clear_page_guard(struct zone *zone, struct page *page, - unsigned int order, int migratetype) {} + unsigned int order) {} #endif /* CONFIG_DEBUG_PAGEALLOC */ =20 #ifdef __HAVE_ARCH_GATE_AREA diff --git a/include/linux/page-isolation.h b/include/linux/page-isolation.h index 8550b3c91480..901915747960 100644 --- a/include/linux/page-isolation.h +++ b/include/linux/page-isolation.h @@ -34,7 +34,8 @@ static inline bool is_migrate_isolate(int migratetype) #define REPORT_FAILURE 0x2 =20 void set_pageblock_migratetype(struct page *page, int migratetype); -int move_freepages_block(struct zone *zone, struct page *page, int migrate= type); +int move_freepages_block(struct zone *zone, struct page *page, + int old_mt, int new_mt); =20 int start_isolate_page_range(unsigned long start_pfn, unsigned long end_pf= n, int migratetype, int flags, gfp_t gfp_flags); diff --git a/include/linux/vmstat.h b/include/linux/vmstat.h index fed855bae6d8..a4eae03f6094 100644 --- a/include/linux/vmstat.h +++ b/include/linux/vmstat.h @@ -487,14 +487,6 @@ static inline void node_stat_sub_folio(struct folio *f= olio, mod_node_page_state(folio_pgdat(folio), item, -folio_nr_pages(folio)); } =20 -static inline void __mod_zone_freepage_state(struct zone *zone, int nr_pag= es, - int migratetype) -{ - __mod_zone_page_state(zone, NR_FREE_PAGES, nr_pages); - if (is_migrate_cma(migratetype)) - __mod_zone_page_state(zone, NR_FREE_CMA_PAGES, nr_pages); -} - extern const char * const vmstat_text[]; =20 static inline const char *zone_stat_name(enum zone_stat_item item) diff --git a/mm/debug_page_alloc.c b/mm/debug_page_alloc.c index f9d145730fd1..03a810927d0a 100644 --- a/mm/debug_page_alloc.c +++ b/mm/debug_page_alloc.c @@ -32,8 +32,7 @@ static int __init debug_guardpage_minorder_setup(char *bu= f) } early_param("debug_guardpage_minorder", debug_guardpage_minorder_setup); =20 -bool __set_page_guard(struct zone *zone, struct page *page, unsigned int o= rder, - int migratetype) +bool __set_page_guard(struct zone *zone, struct page *page, unsigned int o= rder) { if (order >=3D debug_guardpage_minorder()) return false; @@ -41,19 +40,12 @@ bool __set_page_guard(struct zone *zone, struct page *p= age, unsigned int order, __SetPageGuard(page); INIT_LIST_HEAD(&page->buddy_list); set_page_private(page, order); - /* Guard pages are not available for any usage */ - if (!is_migrate_isolate(migratetype)) - __mod_zone_freepage_state(zone, -(1 << order), migratetype); =20 return true; } =20 -void __clear_page_guard(struct zone *zone, struct page *page, unsigned int= order, - int migratetype) +void __clear_page_guard(struct zone *zone, struct page *page, unsigned int= order) { __ClearPageGuard(page); - set_page_private(page, 0); - if (!is_migrate_isolate(migratetype)) - __mod_zone_freepage_state(zone, (1 << order), migratetype); } diff --git a/mm/internal.h b/mm/internal.h index a7d9e980429a..d86fd621880e 100644 --- a/mm/internal.h +++ b/mm/internal.h @@ -865,11 +865,6 @@ static inline bool is_migrate_highatomic(enum migratet= ype migratetype) return migratetype =3D=3D MIGRATE_HIGHATOMIC; } =20 -static inline bool is_migrate_highatomic_page(struct page *page) -{ - return get_pageblock_migratetype(page) =3D=3D MIGRATE_HIGHATOMIC; -} - void setup_zone_pageset(struct zone *zone); =20 struct migration_target_control { diff --git a/mm/page_alloc.c b/mm/page_alloc.c index 42b62832323f..e7e790a64237 100644 --- a/mm/page_alloc.c +++ b/mm/page_alloc.c @@ -676,24 +676,36 @@ compaction_capture(struct capture_control *capc, stru= ct page *page, } #endif /* CONFIG_COMPACTION */ =20 -/* Used for pages not on another list */ -static inline void add_to_free_list(struct page *page, struct zone *zone, - unsigned int order, int migratetype) +static inline void account_freepages(struct page *page, struct zone *zone, + int nr_pages, int migratetype) { - struct free_area *area =3D &zone->free_area[order]; + if (is_migrate_isolate(migratetype)) + return; =20 - list_add(&page->buddy_list, &area->free_list[migratetype]); - area->nr_free++; + __mod_zone_page_state(zone, NR_FREE_PAGES, nr_pages); + + if (is_migrate_cma(migratetype)) + __mod_zone_page_state(zone, NR_FREE_CMA_PAGES, nr_pages); } =20 /* Used for pages not on another list */ -static inline void add_to_free_list_tail(struct page *page, struct zone *z= one, - unsigned int order, int migratetype) +static inline void add_to_free_list(struct page *page, struct zone *zone, + unsigned int order, int migratetype, + bool tail) { struct free_area *area =3D &zone->free_area[order]; =20 - list_add_tail(&page->buddy_list, &area->free_list[migratetype]); + VM_WARN_ONCE(get_pageblock_migratetype(page) !=3D migratetype, + "page type is %lu, passed migratetype is %d (nr=3D%d)\n", + get_pageblock_migratetype(page), migratetype, 1 << order); + + if (tail) + list_add_tail(&page->buddy_list, &area->free_list[migratetype]); + else + list_add(&page->buddy_list, &area->free_list[migratetype]); area->nr_free++; + + account_freepages(page, zone, 1 << order, migratetype); } =20 /* @@ -702,16 +714,28 @@ static inline void add_to_free_list_tail(struct page = *page, struct zone *zone, * allocation again (e.g., optimization for memory onlining). */ static inline void move_to_free_list(struct page *page, struct zone *zone, - unsigned int order, int migratetype) + unsigned int order, int old_mt, int new_mt) { struct free_area *area =3D &zone->free_area[order]; =20 - list_move_tail(&page->buddy_list, &area->free_list[migratetype]); + /* Free page moving can fail, so it happens before the type update */ + VM_WARN_ONCE(get_pageblock_migratetype(page) !=3D old_mt, + "page type is %lu, passed migratetype is %d (nr=3D%d)\n", + get_pageblock_migratetype(page), old_mt, 1 << order); + + list_move_tail(&page->buddy_list, &area->free_list[new_mt]); + + account_freepages(page, zone, -(1 << order), old_mt); + account_freepages(page, zone, 1 << order, new_mt); } =20 static inline void del_page_from_free_list(struct page *page, struct zone = *zone, - unsigned int order) + unsigned int order, int migratetype) { + VM_WARN_ONCE(get_pageblock_migratetype(page) !=3D migratetype, + "page type is %lu, passed migratetype is %d (nr=3D%d)\n", + get_pageblock_migratetype(page), migratetype, 1 << order); + /* clear reported state and update reported page count */ if (page_reported(page)) __ClearPageReported(page); @@ -720,6 +744,8 @@ static inline void del_page_from_free_list(struct page = *page, struct zone *zone, __ClearPageBuddy(page); set_page_private(page, 0); zone->free_area[order].nr_free--; + + account_freepages(page, zone, -(1 << order), migratetype); } =20 static inline struct page *get_page_from_free_area(struct free_area *area, @@ -793,23 +819,21 @@ static inline void __free_one_page(struct page *page, VM_BUG_ON_PAGE(page->flags & PAGE_FLAGS_CHECK_AT_PREP, page); =20 VM_BUG_ON(migratetype =3D=3D -1); - if (likely(!is_migrate_isolate(migratetype))) - __mod_zone_freepage_state(zone, 1 << order, migratetype); - VM_BUG_ON_PAGE(pfn & ((1 << order) - 1), page); VM_BUG_ON_PAGE(bad_range(zone, page), page); =20 while (order < MAX_ORDER) { - if (compaction_capture(capc, page, order, migratetype)) { - __mod_zone_freepage_state(zone, -(1 << order), - migratetype); + int buddy_mt; + + if (compaction_capture(capc, page, order, migratetype)) return; - } =20 buddy =3D find_buddy_page_pfn(page, pfn, order, &buddy_pfn); if (!buddy) goto done_merging; =20 + buddy_mt =3D get_pfnblock_migratetype(buddy, buddy_pfn); + if (unlikely(order >=3D pageblock_order)) { /* * We want to prevent merge between freepages on pageblock @@ -837,9 +861,9 @@ static inline void __free_one_page(struct page *page, * merge with it and move up one order. */ if (page_is_guard(buddy)) - clear_page_guard(zone, buddy, order, migratetype); + clear_page_guard(zone, buddy, order); else - del_page_from_free_list(buddy, zone, order); + del_page_from_free_list(buddy, zone, order, buddy_mt); combined_pfn =3D buddy_pfn & pfn; page =3D page + (combined_pfn - pfn); pfn =3D combined_pfn; @@ -856,10 +880,7 @@ static inline void __free_one_page(struct page *page, else to_tail =3D buddy_merge_likely(pfn, buddy_pfn, page, order); =20 - if (to_tail) - add_to_free_list_tail(page, zone, order, migratetype); - else - add_to_free_list(page, zone, order, migratetype); + add_to_free_list(page, zone, order, migratetype, to_tail); =20 /* Notify page reporting subsystem of freed page */ if (!(fpi_flags & FPI_SKIP_REPORT_NOTIFY)) @@ -901,10 +922,8 @@ int split_free_page(struct page *free_page, } =20 mt =3D get_pfnblock_migratetype(free_page, free_page_pfn); - if (likely(!is_migrate_isolate(mt))) - __mod_zone_freepage_state(zone, -(1UL << order), mt); + del_page_from_free_list(free_page, zone, order, mt); =20 - del_page_from_free_list(free_page, zone, order); for (pfn =3D free_page_pfn; pfn < free_page_pfn + (1UL << order);) { int mt =3D get_pfnblock_migratetype(pfn_to_page(pfn), pfn); @@ -1433,10 +1452,10 @@ static inline void expand(struct zone *zone, struct= page *page, * Corresponding page table entries will not be touched, * pages will stay not present in virtual address space */ - if (set_page_guard(zone, &page[size], high, migratetype)) + if (set_page_guard(zone, &page[size], high)) continue; =20 - add_to_free_list(&page[size], zone, high, migratetype); + add_to_free_list(&page[size], zone, high, migratetype, false); set_buddy_order(&page[size], high); } } @@ -1606,7 +1625,7 @@ struct page *__rmqueue_smallest(struct zone *zone, un= signed int order, page =3D get_page_from_free_area(area, migratetype); if (!page) continue; - del_page_from_free_list(page, zone, current_order); + del_page_from_free_list(page, zone, current_order, migratetype); expand(zone, page, order, current_order, migratetype); trace_mm_page_alloc_zone_locked(page, order, migratetype, pcp_allowed_order(order) && @@ -1647,7 +1666,7 @@ static inline struct page *__rmqueue_cma_fallback(str= uct zone *zone, * boundary. If alignment is required, use move_freepages_block() */ static int move_freepages(struct zone *zone, unsigned long start_pfn, - unsigned long end_pfn, int migratetype) + unsigned long end_pfn, int old_mt, int new_mt) { struct page *page; unsigned long pfn; @@ -1666,7 +1685,7 @@ static int move_freepages(struct zone *zone, unsigned= long start_pfn, VM_BUG_ON_PAGE(page_zone(page) !=3D zone, page); =20 order =3D buddy_order(page); - move_to_free_list(page, zone, order, migratetype); + move_to_free_list(page, zone, order, old_mt, new_mt); pfn +=3D 1 << order; pages_moved +=3D 1 << order; } @@ -1721,7 +1740,7 @@ static bool prep_move_freepages_block(struct zone *zo= ne, struct page *page, } =20 int move_freepages_block(struct zone *zone, struct page *page, - int migratetype) + int old_mt, int new_mt) { unsigned long start_pfn, end_pfn; =20 @@ -1729,7 +1748,7 @@ int move_freepages_block(struct zone *zone, struct pa= ge *page, NULL, NULL)) return -1; =20 - return move_freepages(zone, start_pfn, end_pfn, migratetype); + return move_freepages(zone, start_pfn, end_pfn, old_mt, new_mt); } =20 /* @@ -1829,7 +1848,7 @@ static void steal_suitable_fallback(struct zone *zone= , struct page *page, =20 /* Take ownership for orders >=3D pageblock_order */ if (current_order >=3D pageblock_order) { - del_page_from_free_list(page, zone, current_order); + del_page_from_free_list(page, zone, current_order, block_type); change_pageblock_range(page, current_order, start_type); expand(zone, page, order, current_order, start_type); return; @@ -1880,13 +1899,13 @@ static void steal_suitable_fallback(struct zone *zo= ne, struct page *page, */ if (free_pages + alike_pages >=3D (1 << (pageblock_order-1)) || page_group_by_mobility_disabled) { - move_freepages(zone, start_pfn, end_pfn, start_type); + move_freepages(zone, start_pfn, end_pfn, block_type, start_type); set_pageblock_migratetype(page, start_type); block_type =3D start_type; } =20 single_page: - del_page_from_free_list(page, zone, current_order); + del_page_from_free_list(page, zone, current_order, block_type); expand(zone, page, order, current_order, block_type); } =20 @@ -1952,7 +1971,8 @@ static void reserve_highatomic_pageblock(struct page = *page, struct zone *zone, mt =3D get_pageblock_migratetype(page); /* Only reserve normal pageblocks (i.e., they can merge with others) */ if (migratetype_is_mergeable(mt)) { - if (move_freepages_block(zone, page, MIGRATE_HIGHATOMIC) !=3D -1) { + if (move_freepages_block(zone, page, + mt, MIGRATE_HIGHATOMIC) !=3D -1) { set_pageblock_migratetype(page, MIGRATE_HIGHATOMIC); zone->nr_reserved_highatomic +=3D pageblock_nr_pages; } @@ -1995,11 +2015,13 @@ static bool unreserve_highatomic_pageblock(const st= ruct alloc_context *ac, spin_lock_irqsave(&zone->lock, flags); for (order =3D 0; order <=3D MAX_ORDER; order++) { struct free_area *area =3D &(zone->free_area[order]); + int mt; =20 page =3D get_page_from_free_area(area, MIGRATE_HIGHATOMIC); if (!page) continue; =20 + mt =3D get_pageblock_migratetype(page); /* * In page freeing path, migratetype change is racy so * we can counter several free pages in a pageblock @@ -2007,7 +2029,7 @@ static bool unreserve_highatomic_pageblock(const stru= ct alloc_context *ac, * from highatomic to ac->migratetype. So we should * adjust the count once. */ - if (is_migrate_highatomic_page(page)) { + if (is_migrate_highatomic(mt)) { /* * It should never happen but changes to * locking could inadvertently allow a per-cpu @@ -2029,7 +2051,8 @@ static bool unreserve_highatomic_pageblock(const stru= ct alloc_context *ac, * of pageblocks that cannot be completely freed * may increase. */ - ret =3D move_freepages_block(zone, page, ac->migratetype); + ret =3D move_freepages_block(zone, page, mt, + ac->migratetype); /* * Reserving this block already succeeded, so this should * not fail on zone boundaries. @@ -2202,12 +2225,7 @@ static int rmqueue_bulk(struct zone *zone, unsigned = int order, * pages are ordered properly. */ list_add_tail(&page->pcp_list, list); - if (is_migrate_cma(get_pageblock_migratetype(page))) - __mod_zone_page_state(zone, NR_FREE_CMA_PAGES, - -(1 << order)); } - - __mod_zone_page_state(zone, NR_FREE_PAGES, -(i << order)); spin_unlock_irqrestore(&zone->lock, flags); =20 return i; @@ -2604,11 +2622,9 @@ int __isolate_free_page(struct page *page, unsigned = int order) watermark =3D zone->_watermark[WMARK_MIN] + (1UL << order); if (!zone_watermark_ok(zone, 0, watermark, 0, ALLOC_CMA)) return 0; - - __mod_zone_freepage_state(zone, -(1UL << order), mt); } =20 - del_page_from_free_list(page, zone, order); + del_page_from_free_list(page, zone, order, mt); =20 /* * Set the pageblock if the isolated page is at least half of a @@ -2623,7 +2639,7 @@ int __isolate_free_page(struct page *page, unsigned i= nt order) * with others) */ if (migratetype_is_mergeable(mt) && - move_freepages_block(zone, page, + move_freepages_block(zone, page, mt, MIGRATE_MOVABLE) !=3D -1) set_pageblock_migratetype(page, MIGRATE_MOVABLE); } @@ -2715,8 +2731,6 @@ struct page *rmqueue_buddy(struct zone *preferred_zon= e, struct zone *zone, return NULL; } } - __mod_zone_freepage_state(zone, -(1 << order), - get_pageblock_migratetype(page)); spin_unlock_irqrestore(&zone->lock, flags); } while (check_new_pages(page, order)); =20 @@ -6488,8 +6502,9 @@ void __offline_isolated_pages(unsigned long start_pfn= , unsigned long end_pfn) =20 BUG_ON(page_count(page)); BUG_ON(!PageBuddy(page)); + VM_WARN_ON(get_pageblock_migratetype(page) !=3D MIGRATE_ISOLATE); order =3D buddy_order(page); - del_page_from_free_list(page, zone, order); + del_page_from_free_list(page, zone, order, MIGRATE_ISOLATE); pfn +=3D (1 << order); } spin_unlock_irqrestore(&zone->lock, flags); @@ -6540,11 +6555,12 @@ static void break_down_buddy_pages(struct zone *zon= e, struct page *page, current_buddy =3D page + size; } =20 - if (set_page_guard(zone, current_buddy, high, migratetype)) + if (set_page_guard(zone, current_buddy, high)) continue; =20 if (current_buddy !=3D target) { - add_to_free_list(current_buddy, zone, high, migratetype); + add_to_free_list(current_buddy, zone, high, + migratetype, false); set_buddy_order(current_buddy, high); page =3D next_page; } @@ -6572,12 +6588,11 @@ bool take_page_off_buddy(struct page *page) int migratetype =3D get_pfnblock_migratetype(page_head, pfn_head); =20 - del_page_from_free_list(page_head, zone, page_order); + del_page_from_free_list(page_head, zone, page_order, + migratetype); break_down_buddy_pages(zone, page_head, page, 0, page_order, migratetype); SetPageHWPoisonTakenOff(page); - if (!is_migrate_isolate(migratetype)) - __mod_zone_freepage_state(zone, -1, migratetype); ret =3D true; break; } diff --git a/mm/page_isolation.c b/mm/page_isolation.c index f5e4d8676b36..b0705e709973 100644 --- a/mm/page_isolation.c +++ b/mm/page_isolation.c @@ -181,13 +181,12 @@ static int set_migratetype_isolate(struct page *page,= int migratetype, int isol_ int nr_pages; int mt =3D get_pageblock_migratetype(page); =20 - nr_pages =3D move_freepages_block(zone, page, MIGRATE_ISOLATE); + nr_pages =3D move_freepages_block(zone, page, mt, MIGRATE_ISOLATE); /* Block spans zone boundaries? */ if (nr_pages =3D=3D -1) { spin_unlock_irqrestore(&zone->lock, flags); return -EBUSY; } - __mod_zone_freepage_state(zone, -nr_pages, mt); set_pageblock_migratetype(page, MIGRATE_ISOLATE); zone->nr_isolate_pageblock++; spin_unlock_irqrestore(&zone->lock, flags); @@ -255,13 +254,13 @@ static void unset_migratetype_isolate(struct page *pa= ge, int migratetype) * allocation. */ if (!isolated_page) { - int nr_pages =3D move_freepages_block(zone, page, migratetype); + int nr_pages =3D move_freepages_block(zone, page, MIGRATE_ISOLATE, + migratetype); /* * Isolating this block already succeeded, so this * should not fail on zone boundaries. */ WARN_ON_ONCE(nr_pages =3D=3D -1); - __mod_zone_freepage_state(zone, nr_pages, migratetype); } set_pageblock_migratetype(page, migratetype); if (isolated_page) --=20 2.41.0