From patchwork Fri Apr 1 18:11:08 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Zi Yan X-Patchwork-Id: 12798582 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 3E26AC433F5 for ; Fri, 1 Apr 2022 18:11:34 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id AEB0C8D0001; Fri, 1 Apr 2022 14:11:23 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id A9B936B0072; Fri, 1 Apr 2022 14:11:23 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 93B688D0001; Fri, 1 Apr 2022 14:11:23 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (relay.hostedemail.com [64.99.140.27]) by kanga.kvack.org (Postfix) with ESMTP id 80F796B0071 for ; Fri, 1 Apr 2022 14:11:23 -0400 (EDT) Received: from smtpin15.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 4603C2515C for ; Fri, 1 Apr 2022 18:11:13 +0000 (UTC) X-FDA: 79309102026.15.B133A1D Received: from out3-smtp.messagingengine.com (out3-smtp.messagingengine.com [66.111.4.27]) by imf11.hostedemail.com (Postfix) with ESMTP id 88CCD40017 for ; Fri, 1 Apr 2022 18:11:12 +0000 (UTC) Received: from compute4.internal (compute4.nyi.internal [10.202.2.44]) by mailout.nyi.internal (Postfix) with ESMTP id 34C5E5C00FF; Fri, 1 Apr 2022 14:11:12 -0400 (EDT) Received: from mailfrontend1 ([10.202.2.162]) by compute4.internal (MEProxy); Fri, 01 Apr 2022 14:11:12 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sent.com; h=cc :cc:content-transfer-encoding:date:date:from:from:in-reply-to :message-id:mime-version:reply-to:reply-to:sender:subject :subject:to:to; s=fm3; bh=2nU+VZUHjfRgXw79i6wfh74gnFpHiuHpC7Kwwp V6T0c=; b=BvgMvHQH6WqSN3yuESJpc6OgNQ4daniLDMKJIWe1jo/uA7Cslp/2mV cllA9QvnbWCKmXS2RaySyaufLO49J8Qws2cuGY7AxOuaVTZKjGloHUw1Q/PJ7a0M edG+YoR2bPTJB2VZ3KSakhOINl4QNYtEAJva1z8LKeQRONWfOXFi6Iuhm86Ej+fu hTr3YjvD6zX2rbZi6foc4WyiWgaj7dA2b0zZ6JCaOKghiP4sZacxg2nl0ZZ0EcA/ tL49zNzeOFiMOA2xWMae2B1UA6SVlK947tbi0KIye4tlvMnPOKKbrIV0OC2FCJfR dfEBF0+AFMuGSvgJXsDzzjD//0G4L2DQ== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-transfer-encoding:date:date :from:from:in-reply-to:message-id:mime-version:reply-to:reply-to :sender:subject:subject:to:to:x-me-proxy:x-me-proxy:x-me-sender :x-me-sender:x-sasl-enc; s=fm3; bh=2nU+VZUHjfRgXw79i6wfh74gnFpHi uHpC7KwwpV6T0c=; b=Ze8jvykDpD1eKriGlh5yFRWi9dJXlDQ3PsDdAuhzZ2zgU 6BTHhZmxvO3++wVEcN3D5UiJXOtkCnWv9OOQGpE0CfmVBi8l/BG7R7Afj6BFbgrv uMhV3YvDKvuFehGvkFMPP54jKI93dafxPs7cvdUpeidC4/spPjRZkrVGe8nm63ZC 3kGzkfyGAqDFa6R5xxeo+yEpNcabwyUm8A+K6sOWOwwYBfE5LXuHIjH7QP+hkWib nQ7njh7gDxe54iuxBnVmUvkuCuf07CPtdiyjdbzwO5peWkyP3fjax2deq4ylyKWK DWITbSCJa8LMqyu2Bzvh9Tqv8BbNotx5KuOj2fBMw== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvvddrudeiiedguddvvdcutefuodetggdotefrod ftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfgh necuuegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmd enucfjughrpefhvffufffkofhrggfgsedtqhertdertddtnecuhfhrohhmpegkihcujggr nhcuoeiiihdrhigrnhesshgvnhhtrdgtohhmqeenucggtffrrghtthgvrhhnpeetieeitd ejgffhfeeukeejvdeufedtvddulefhteduffeigfefteehgefhvdegudenucffohhmrghi nhepkhgvrhhnvghlrdhorhhgnecuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpe hmrghilhhfrhhomhepiihirdihrghnsehsvghnthdrtghomh X-ME-Proxy: Received: by mail.messagingengine.com (Postfix) with ESMTPA; Fri, 1 Apr 2022 14:11:11 -0400 (EDT) From: Zi Yan To: linux-mm@kvack.org Cc: Linus Torvalds , Steven Rostedt , David Hildenbrand , Vlastimil Babka , Mel Gorman , Mike Rapoport , Oscar Salvador , Andrew Morton , linux-kernel@vger.kernel.org, Zi Yan Subject: [PATCH v2 1/2] mm: page_alloc: simplify pageblock migratetype check in __free_one_page(). Date: Fri, 1 Apr 2022 14:11:08 -0400 Message-Id: <20220401181109.1477354-1-zi.yan@sent.com> X-Mailer: git-send-email 2.35.1 Reply-To: Zi Yan MIME-Version: 1.0 X-Stat-Signature: 75636yxnhmje91mz4u5194d4kx86trsa Authentication-Results: imf11.hostedemail.com; dkim=pass header.d=sent.com header.s=fm3 header.b=BvgMvHQH; dkim=pass header.d=messagingengine.com header.s=fm3 header.b=Ze8jvykD; spf=pass (imf11.hostedemail.com: domain of zi.yan@sent.com designates 66.111.4.27 as permitted sender) smtp.mailfrom=zi.yan@sent.com; dmarc=pass (policy=none) header.from=sent.com X-Rspam-User: X-Rspamd-Server: rspam08 X-Rspamd-Queue-Id: 88CCD40017 X-HE-Tag: 1648836672-610567 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: From: Zi Yan Move pageblock migratetype check code in the while loop to simplify the logic. It also saves redundant buddy page checking code. Suggested-by: Vlastimil Babka Link: https://lore.kernel.org/linux-mm/27ff69f9-60c5-9e59-feb2-295250077551@suse.cz/ Signed-off-by: Zi Yan Acked-by: Vlastimil Babka --- mm/page_alloc.c | 46 +++++++++++++++++----------------------------- 1 file changed, 17 insertions(+), 29 deletions(-) diff --git a/mm/page_alloc.c b/mm/page_alloc.c index 856473e54155..2ea106146686 100644 --- a/mm/page_alloc.c +++ b/mm/page_alloc.c @@ -1054,7 +1054,6 @@ static inline void __free_one_page(struct page *page, int migratetype, fpi_t fpi_flags) { struct capture_control *capc = task_capc(zone); - unsigned int max_order = pageblock_order; unsigned long buddy_pfn; unsigned long combined_pfn; struct page *buddy; @@ -1070,8 +1069,7 @@ static inline void __free_one_page(struct page *page, VM_BUG_ON_PAGE(pfn & ((1 << order) - 1), page); VM_BUG_ON_PAGE(bad_range(zone, page), page); -continue_merging: - while (order < max_order) { + while (order < MAX_ORDER - 1) { if (compaction_capture(capc, page, order, migratetype)) { __mod_zone_freepage_state(zone, -(1 << order), migratetype); @@ -1082,6 +1080,22 @@ static inline void __free_one_page(struct page *page, if (!page_is_buddy(page, buddy, order)) goto done_merging; + + if (unlikely(order >= pageblock_order)) { + /* + * We want to prevent merge between freepages on pageblock + * without fallbacks and normal pageblock. Without this, + * pageblock isolation could cause incorrect freepage or CMA + * accounting or HIGHATOMIC accounting. + */ + int buddy_mt = get_pageblock_migratetype(buddy); + + if (migratetype != buddy_mt + && (!migratetype_is_mergeable(migratetype) || + !migratetype_is_mergeable(buddy_mt))) + goto done_merging; + } + /* * Our buddy is free or it is CONFIG_DEBUG_PAGEALLOC guard page, * merge with it and move up one order. @@ -1095,32 +1109,6 @@ static inline void __free_one_page(struct page *page, pfn = combined_pfn; order++; } - if (order < MAX_ORDER - 1) { - /* If we are here, it means order is >= pageblock_order. - * We want to prevent merge between freepages on pageblock - * without fallbacks and normal pageblock. Without this, - * pageblock isolation could cause incorrect freepage or CMA - * accounting or HIGHATOMIC accounting. - * - * We don't want to hit this code for the more frequent - * low-order merging. - */ - int buddy_mt; - - buddy_pfn = __find_buddy_pfn(pfn, order); - buddy = page + (buddy_pfn - pfn); - - if (!page_is_buddy(page, buddy, order)) - goto done_merging; - buddy_mt = get_pageblock_migratetype(buddy); - - if (migratetype != buddy_mt - && (!migratetype_is_mergeable(migratetype) || - !migratetype_is_mergeable(buddy_mt))) - goto done_merging; - max_order = order + 1; - goto continue_merging; - } done_merging: set_buddy_order(page, order); From patchwork Fri Apr 1 18:11:09 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Zi Yan X-Patchwork-Id: 12798583 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 863ABC433F5 for ; Fri, 1 Apr 2022 18:11:50 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 6A5C96B0071; Fri, 1 Apr 2022 14:11:24 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 655278D0002; Fri, 1 Apr 2022 14:11:24 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 4CF066B0073; Fri, 1 Apr 2022 14:11:24 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (relay.hostedemail.com [64.99.140.25]) by kanga.kvack.org (Postfix) with ESMTP id 3B8276B0071 for ; Fri, 1 Apr 2022 14:11:24 -0400 (EDT) Received: from smtpin14.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 0EA4524DFF for ; Fri, 1 Apr 2022 18:11:14 +0000 (UTC) X-FDA: 79309102068.14.AE5DC85 Received: from out3-smtp.messagingengine.com (out3-smtp.messagingengine.com [66.111.4.27]) by imf06.hostedemail.com (Postfix) with ESMTP id 5DA6218001F for ; Fri, 1 Apr 2022 18:11:13 +0000 (UTC) Received: from compute5.internal (compute5.nyi.internal [10.202.2.45]) by mailout.nyi.internal (Postfix) with ESMTP id 04D0C5C0113; Fri, 1 Apr 2022 14:11:13 -0400 (EDT) Received: from mailfrontend1 ([10.202.2.162]) by compute5.internal (MEProxy); Fri, 01 Apr 2022 14:11:13 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sent.com; h=cc :cc:content-transfer-encoding:date:date:from:from:in-reply-to :in-reply-to:message-id:mime-version:references:reply-to :reply-to:sender:subject:subject:to:to; s=fm3; bh=5Gk8ojAh+kjQww m1eaNCCrbPZRcvgtihcLP51TB7JEA=; b=uA+yxaatWO5NZ2nMetzW6wRY/CK/6D xGdGpsLWzWvmlcrLqan6RpTXw2vgZcQ6wC8A/S+XKpdsJ2QbYHcGt3MID0Go7qfD ZlGF6/GOQn5MEnY8Q5WsCrhJob1VU0nB0RaqTGgf50V5xsXc0ql9SoPWDL2V3e+P pnwyFB8lzQ+5oAxb6h6qBIHUUugGjsEUn9Pa8CX+63e8Oi20gnsbgIFRkr0l4Tnb T77iK2DuGfqNHYH2mR/BOo6zin3iaNwcUqoGOAejg8u8GDdX3FN6CO6MKWc46afQ sp7gwcjsAqgWiaGSAtVXwqCaqQ7DGjD3ZKCACVmbcUW3ZMu4gqTy+BEQ== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-transfer-encoding:date:date :from:from:in-reply-to:in-reply-to:message-id:mime-version :references:reply-to:reply-to:sender:subject:subject:to:to :x-me-proxy:x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s= fm3; bh=5Gk8ojAh+kjQwwm1eaNCCrbPZRcvgtihcLP51TB7JEA=; b=OUFHIpYC QfKFnbI2tJ8Llnbj4As8s4e2eZ7DYzxDfNgtUzxHAOCdB9VPd58LXnbHQLr4Vhj1 kOW7y4+Z6nSMw5fRgyzYds6hAXeUNi9uWD2Oyb6QSIcM7Y3u/T7vEaJmQI5caFff loaC18ghpI+151dyP++fygZs1kGa0eHEy5HR7tOcMKRSwm+RhEYZvN8OBw22bW5G do+8Qusa8jsIYzhr7MckErCRUms8BNKZ/IPtZsS/GJ4yO7+NgdDgyt8QpMJXXHc0 lywV2x4nV7RvBK7A0zqLJuKp/VLbRXPd/vD/yhxtJnFlNI5ayau0xIqsBY7AIRMj M2/gGYvZLRFKnA== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvvddrudeiiedguddvudcutefuodetggdotefrod ftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfgh necuuegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmd enucfjughrpefhvffufffkofgjfhhrggfgsedtqhertdertddtnecuhfhrohhmpegkihcu jggrnhcuoeiiihdrhigrnhesshgvnhhtrdgtohhmqeenucggtffrrghtthgvrhhnpeekie euledvtdetueejhedtgeehjeelvddttefgieeljeefgefhfedtuefffeelveenucffohhm rghinhepkhgvrhhnvghlrdhorhhgnecuvehluhhsthgvrhfuihiivgeptdenucfrrghrrg hmpehmrghilhhfrhhomhepiihirdihrghnsehsvghnthdrtghomh X-ME-Proxy: Received: by mail.messagingengine.com (Postfix) with ESMTPA; Fri, 1 Apr 2022 14:11:12 -0400 (EDT) From: Zi Yan To: linux-mm@kvack.org Cc: Linus Torvalds , Steven Rostedt , David Hildenbrand , Vlastimil Babka , Mel Gorman , Mike Rapoport , Oscar Salvador , Andrew Morton , linux-kernel@vger.kernel.org, Zi Yan Subject: [PATCH v2 2/2] mm: wrap __find_buddy_pfn() with a necessary buddy page validation. Date: Fri, 1 Apr 2022 14:11:09 -0400 Message-Id: <20220401181109.1477354-2-zi.yan@sent.com> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220401181109.1477354-1-zi.yan@sent.com> References: <20220401181109.1477354-1-zi.yan@sent.com> Reply-To: Zi Yan MIME-Version: 1.0 X-Stat-Signature: gojq9btaba176t4ysnh4sukmjmabfwq7 Authentication-Results: imf06.hostedemail.com; dkim=pass header.d=sent.com header.s=fm3 header.b=uA+yxaat; dkim=pass header.d=messagingengine.com header.s=fm3 header.b=OUFHIpYC; spf=pass (imf06.hostedemail.com: domain of zi.yan@sent.com designates 66.111.4.27 as permitted sender) smtp.mailfrom=zi.yan@sent.com; dmarc=pass (policy=none) header.from=sent.com X-Rspam-User: X-Rspamd-Server: rspam08 X-Rspamd-Queue-Id: 5DA6218001F X-HE-Tag: 1648836673-760431 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: From: Zi Yan Whenever the buddy of a page is found from __find_buddy_pfn(), page_is_buddy() should be used to check its validity. Add a helper function find_buddy_page_pfn() to find the buddy page and do the check together. Suggested-by: Linus Torvalds Link: https://lore.kernel.org/linux-mm/CAHk-=wji_AmYygZMTsPMdJ7XksMt7kOur8oDfDdniBRMjm4VkQ@mail.gmail.com/ Signed-off-by: Zi Yan --- mm/internal.h | 23 +---------------- mm/page_alloc.c | 63 ++++++++++++++++++++++++++++++++++++++------- mm/page_isolation.c | 8 ++---- 3 files changed, 56 insertions(+), 38 deletions(-) diff --git a/mm/internal.h b/mm/internal.h index 876e66237c89..f43565ed5ff6 100644 --- a/mm/internal.h +++ b/mm/internal.h @@ -211,28 +211,7 @@ struct alloc_context { bool spread_dirty_pages; }; -/* - * Locate the struct page for both the matching buddy in our - * pair (buddy1) and the combined O(n+1) page they form (page). - * - * 1) Any buddy B1 will have an order O twin B2 which satisfies - * the following equation: - * B2 = B1 ^ (1 << O) - * For example, if the starting buddy (buddy2) is #8 its order - * 1 buddy is #10: - * B2 = 8 ^ (1 << 1) = 8 ^ 2 = 10 - * - * 2) Any buddy B will have an order O+1 parent P which - * satisfies the following equation: - * P = B & ~(1 << O) - * - * Assumption: *_mem_map is contiguous at least up to MAX_ORDER - */ -static inline unsigned long -__find_buddy_pfn(unsigned long page_pfn, unsigned int order) -{ - return page_pfn ^ (1 << order); -} +extern struct page *find_buddy_page_pfn(struct page *page, unsigned int order); extern struct page *__pageblock_pfn_to_page(unsigned long start_pfn, unsigned long end_pfn, struct zone *zone); diff --git a/mm/page_alloc.c b/mm/page_alloc.c index 2ea106146686..8aedc6fbfdd0 100644 --- a/mm/page_alloc.c +++ b/mm/page_alloc.c @@ -998,6 +998,51 @@ static inline void del_page_from_free_list(struct page *page, struct zone *zone, zone->free_area[order].nr_free--; } +/* + * Locate the struct page for both the matching buddy in our + * pair (buddy1) and the combined O(n+1) page they form (page). + * + * 1) Any buddy B1 will have an order O twin B2 which satisfies + * the following equation: + * B2 = B1 ^ (1 << O) + * For example, if the starting buddy (buddy2) is #8 its order + * 1 buddy is #10: + * B2 = 8 ^ (1 << 1) = 8 ^ 2 = 10 + * + * 2) Any buddy B will have an order O+1 parent P which + * satisfies the following equation: + * P = B & ~(1 << O) + * + * Assumption: *_mem_map is contiguous at least up to MAX_ORDER + */ +static inline unsigned long +__find_buddy_pfn(unsigned long page_pfn, unsigned int order) +{ + return page_pfn ^ (1 << order); +} + + +/* + * Find the buddy of @page and validate it. + * @page: The input page + * @order: The order of the input page + * + * The found buddy can be a non PageBuddy, out of @page's zone, or its order is + * not the same as @page. The validation is necessary before use it. + * + * Return: the found buddy page or NULL if not found. + */ +struct page *find_buddy_page_pfn(struct page *page, unsigned int order) +{ + unsigned long pfn = page_to_pfn(page); + unsigned long buddy_pfn = __find_buddy_pfn(pfn, order); + struct page *buddy = page + (buddy_pfn - pfn); + + if (page_is_buddy(page, buddy, order)) + return buddy; + return NULL; +} + /* * If this is not the largest possible page, check if the buddy * of the next-highest order is free. If it is, it's possible @@ -1010,18 +1055,16 @@ static inline bool buddy_merge_likely(unsigned long pfn, unsigned long buddy_pfn, struct page *page, unsigned int order) { - struct page *higher_page, *higher_buddy; - unsigned long combined_pfn; + struct page *higher_page; + unsigned long higher_page_pfn; if (order >= MAX_ORDER - 2) return false; - combined_pfn = buddy_pfn & pfn; - higher_page = page + (combined_pfn - pfn); - buddy_pfn = __find_buddy_pfn(combined_pfn, order + 1); - higher_buddy = higher_page + (buddy_pfn - combined_pfn); + higher_page_pfn = buddy_pfn & pfn; + higher_page = page + (higher_page_pfn - pfn); - return page_is_buddy(higher_page, higher_buddy, order + 1); + return find_buddy_page_pfn(higher_page, order + 1) != NULL; } /* @@ -1075,11 +1118,11 @@ static inline void __free_one_page(struct page *page, migratetype); return; } - buddy_pfn = __find_buddy_pfn(pfn, order); - buddy = page + (buddy_pfn - pfn); - if (!page_is_buddy(page, buddy, order)) + buddy = find_buddy_page_pfn(page, order); + if (!buddy) goto done_merging; + buddy_pfn = page_to_pfn(buddy); if (unlikely(order >= pageblock_order)) { /* diff --git a/mm/page_isolation.c b/mm/page_isolation.c index f67c4c70f17f..7326c9f57d55 100644 --- a/mm/page_isolation.c +++ b/mm/page_isolation.c @@ -70,7 +70,6 @@ static void unset_migratetype_isolate(struct page *page, unsigned migratetype) unsigned long flags, nr_pages; bool isolated_page = false; unsigned int order; - unsigned long pfn, buddy_pfn; struct page *buddy; zone = page_zone(page); @@ -89,11 +88,8 @@ static void unset_migratetype_isolate(struct page *page, unsigned migratetype) if (PageBuddy(page)) { order = buddy_order(page); if (order >= pageblock_order && order < MAX_ORDER - 1) { - pfn = page_to_pfn(page); - buddy_pfn = __find_buddy_pfn(pfn, order); - buddy = page + (buddy_pfn - pfn); - - if (!is_migrate_isolate_page(buddy)) { + buddy = find_buddy_page_pfn(page, order); + if (buddy && !is_migrate_isolate_page(buddy)) { isolated_page = !!__isolate_free_page(page, order); /* * Isolating a free page in an isolated pageblock