From patchwork Fri Apr 1 13:58:20 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Zi Yan X-Patchwork-Id: 12798316 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 B288FC433F5 for ; Fri, 1 Apr 2022 13:59:04 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 59EFF6B0075; Fri, 1 Apr 2022 09:58:40 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 54EE46B0074; Fri, 1 Apr 2022 09:58:40 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 3F11E8D0001; Fri, 1 Apr 2022 09:58:40 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0131.hostedemail.com [216.40.44.131]) by kanga.kvack.org (Postfix) with ESMTP id 331A76B0073 for ; Fri, 1 Apr 2022 09:58:40 -0400 (EDT) Received: from smtpin27.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay05.hostedemail.com (Postfix) with ESMTP id DAA591828D92B for ; Fri, 1 Apr 2022 13:58:29 +0000 (UTC) X-FDA: 79308465138.27.AE20B8A Received: from wout2-smtp.messagingengine.com (wout2-smtp.messagingengine.com [64.147.123.25]) by imf26.hostedemail.com (Postfix) with ESMTP id 6D9E714000C for ; Fri, 1 Apr 2022 13:58:28 +0000 (UTC) Received: from compute5.internal (compute5.nyi.internal [10.202.2.45]) by mailout.west.internal (Postfix) with ESMTP id A709E3201F86; Fri, 1 Apr 2022 09:58:26 -0400 (EDT) Received: from mailfrontend1 ([10.202.2.162]) by compute5.internal (MEProxy); Fri, 01 Apr 2022 09:58:27 -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=iwnEykB1b2/b8D mQ8le6vvZj3QpvHIfY2VKFpdl/8nY=; b=g+Om+fuhzju2leUInivnh8C2qIQaHL XF/QPDkiMuLNqKa+wFapEwVQFlOZkN/a+AoTHMZa0aazl59SumO7kj2k93ahIhk+ 5hvvLFY7rTHb3WGdwUVmEKzMCGuBuggvz8tABe9SM0dT7F5ZK3ez2aw14Tmt36p+ Zj2bvWR2+IhplHrSsfqziPOM0zHu9W/F0HPt6fk9L2sJhyPcnsCK3+s6Y8uw/w/F o9v9K5BOr+JircZo655rMCdFJjMQLsk4yor09WmYaySLsWgbcuaZGZ/VxaW1zJlr 76SbN8ownfqTjhGe1BWmwnYw6UYrvCpm1rq7TQf/y7AERgNnK3bR+hMw== 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=iwnEykB1b2/b8DmQ8le6vvZj3QpvHIfY2VKFpdl/8nY=; b=UE0Zvq/N YUG7Z+ibUqLWwT4//+gQfQ9p51b0xLhv15bY3/NlXphUDo27/IbWth5BBfaZBlTa f5hkOaBY2i9FM8yCs1uef82Ae9SEc1zbfpAPRdtZzKnibrWO6FclcX68Z4MxKOe3 61SvbwUu/Ej1KyBkIZwlbXHMbjHdjame3xyw5lxXv8eHQlhfuMFxythCqWKGObGb 5yslizwwayQwLoyfrG2dKMUP/hOT9/LmKyFvtzLgOA6F9t7p3zNNPa4wXerotpwO qYqvLJqqLSf073rjx3Ft3qv0gQ+LeKYUYEcRFQtwLCC2F8WEIMND7p/wH0V1uooI jKxcG3a4vYxTLg== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvvddrudeiiedgjeduucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhephffvufffkffojghfrhgggfestdhqredtredttdenucfhrhhomhepkghiucgj rghnuceoiihirdihrghnsehsvghnthdrtghomheqnecuggftrfgrthhtvghrnhepkeeiue elvddtteeujeehtdegheejledvtdetgfeileejfeeghfeftdeuffefleevnecuffhomhgr ihhnpehkvghrnhgvlhdrohhrghenucevlhhushhtvghrufhiiigvpedtnecurfgrrhgrmh epmhgrihhlfhhrohhmpeiiihdrhigrnhesshgvnhhtrdgtohhm X-ME-Proxy: Received: by mail.messagingengine.com (Postfix) with ESMTPA; Fri, 1 Apr 2022 09:58:25 -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 2/2] mm: wrap __find_buddy_pfn() with a necessary buddy page validation. Date: Fri, 1 Apr 2022 09:58:20 -0400 Message-Id: <20220401135820.1453829-2-zi.yan@sent.com> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220401135820.1453829-1-zi.yan@sent.com> References: <20220401135820.1453829-1-zi.yan@sent.com> Reply-To: Zi Yan MIME-Version: 1.0 Authentication-Results: imf26.hostedemail.com; dkim=pass header.d=sent.com header.s=fm3 header.b=g+Om+fuh; dkim=pass header.d=messagingengine.com header.s=fm3 header.b="UE0Zvq/N"; spf=pass (imf26.hostedemail.com: domain of zi.yan@sent.com designates 64.147.123.25 as permitted sender) smtp.mailfrom=zi.yan@sent.com; dmarc=pass (policy=none) header.from=sent.com X-Stat-Signature: wnswi1mi1sr9xg1fpat7n3w35emrbcex X-Rspam-User: X-Rspamd-Server: rspam12 X-Rspamd-Queue-Id: 6D9E714000C X-HE-Tag: 1648821508-256569 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 | 24 ++--------------- mm/page_alloc.c | 63 ++++++++++++++++++++++++++++++++++++++------- mm/page_isolation.c | 9 +++---- 3 files changed, 59 insertions(+), 37 deletions(-) diff --git a/mm/internal.h b/mm/internal.h index 876e66237c89..791653c95bf1 100644 --- a/mm/internal.h +++ b/mm/internal.h @@ -211,28 +211,8 @@ 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 bool find_buddy_page_pfn(struct page *page, unsigned int order, + struct page **buddy, unsigned long *buddy_pfn); 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..89490b9a19ef 100644 --- a/mm/page_alloc.c +++ b/mm/page_alloc.c @@ -998,6 +998,54 @@ 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: Order of the input page + * @buddy: Output pointer to the buddy page + * @buddy_pfn: Output pointer to the buddy pfn + * + * 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: true if the found buddy page is valid or false if not. + * + */ +bool find_buddy_page_pfn(struct page *page, unsigned int order, + struct page **buddy, unsigned long *buddy_pfn) +{ + unsigned long pfn = page_to_pfn(page); + + *buddy_pfn = __find_buddy_pfn(pfn, order); + *buddy = page + (*buddy_pfn - pfn); + + return page_is_buddy(page, *buddy, order); +} + /* * 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 @@ -1011,17 +1059,16 @@ 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; + 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, + &higher_buddy, &buddy_pfn); } /* @@ -1075,10 +1122,8 @@ 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)) + if (!find_buddy_page_pfn(page, order, &buddy, &buddy_pfn)) goto done_merging; if (unlikely(order >= pageblock_order)) { diff --git a/mm/page_isolation.c b/mm/page_isolation.c index f67c4c70f17f..743c52f489f5 100644 --- a/mm/page_isolation.c +++ b/mm/page_isolation.c @@ -70,7 +70,7 @@ 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; + unsigned long buddy_pfn; struct page *buddy; zone = page_zone(page); @@ -89,11 +89,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)) { + if (find_buddy_page_pfn(page, order, &buddy, &buddy_pfn) && + !is_migrate_isolate_page(buddy)) { isolated_page = !!__isolate_free_page(page, order); /* * Isolating a free page in an isolated pageblock