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 417FBC4167D for ; Tue, 14 Nov 2023 12:36:42 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id B71AE8D0041; Tue, 14 Nov 2023 07:36:41 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id B21988D002E; Tue, 14 Nov 2023 07:36:41 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A11488D0041; Tue, 14 Nov 2023 07:36:41 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 929938D002E for ; Tue, 14 Nov 2023 07:36:41 -0500 (EST) Received: from smtpin15.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 65699C035B for ; Tue, 14 Nov 2023 12:36:41 +0000 (UTC) X-FDA: 81456508602.15.70E3F2F Received: from mxhk.zte.com.cn (mxhk.zte.com.cn [63.216.63.35]) by imf05.hostedemail.com (Postfix) with ESMTP id A705A100009 for ; Tue, 14 Nov 2023 12:36:37 +0000 (UTC) Authentication-Results: imf05.hostedemail.com; dkim=none; dmarc=pass (policy=none) header.from=zte.com.cn; spf=pass (imf05.hostedemail.com: domain of yang.yang29@zte.com.cn designates 63.216.63.35 as permitted sender) smtp.mailfrom=yang.yang29@zte.com.cn ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1699965398; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding:in-reply-to: references; bh=r3pZbpvx+eMrnwi3b7DCyVz9oZbOtGVK8k7Jiw5A3wg=; b=AWbk2Cn7+xsKw/kaUXitCAk/lw5JDWBjV12whUDxXo2oRDwHn+EGXj2lNsgYaMa1nkWf7v fxcS12LFsqpNGYnicVWEOJ6s73J+65kt88tuz4ndBzrWASVvU5J9VSS2UWLPCoPIBxv46c jaELCjuXQcUfHBMeTEWEnbP6al1rg7s= ARC-Authentication-Results: i=1; imf05.hostedemail.com; dkim=none; dmarc=pass (policy=none) header.from=zte.com.cn; spf=pass (imf05.hostedemail.com: domain of yang.yang29@zte.com.cn designates 63.216.63.35 as permitted sender) smtp.mailfrom=yang.yang29@zte.com.cn ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1699965398; a=rsa-sha256; cv=none; b=DzfUavZgHBxx6I+DtIrng2W5CFQsv3v/h9OIl+CVkXEGn6cLp/Ue4Xg4qNKgFry56KsHQt +AsDvzd+CU+NrrMEU+mwhsVJQo54G0MSLrExbQE16PkHSR2eVvhrq2r5BqtToEpjytkKSb JqO6FhqiuynoVaWvELt06GoSFBEaQgY= Received: from mse-fl2.zte.com.cn (unknown [10.5.228.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mxhk.zte.com.cn (FangMail) with ESMTPS id 4SV5Q82TyLz4xPYj; Tue, 14 Nov 2023 20:36:32 +0800 (CST) Received: from szxlzmapp04.zte.com.cn ([10.5.231.166]) by mse-fl2.zte.com.cn with SMTP id 3AECaR48053112; Tue, 14 Nov 2023 20:36:27 +0800 (+08) (envelope-from yang.yang29@zte.com.cn) Received: from mapi (szxlzmapp01[null]) by mapi (Zmail) with MAPI id mid14; Tue, 14 Nov 2023 20:36:30 +0800 (CST) Date: Tue, 14 Nov 2023 20:36:30 +0800 (CST) X-Zmail-TransId: 2b03655369ceffffffff932-85ee3 X-Mailer: Zmail v1.0 Message-ID: <202311142036302357580@zte.com.cn> Mime-Version: 1.0 From: To: , Cc: , , , , , , , , Subject: =?UTF-8?B?W1BBVENIXSBrc206IGRlbGF5IHRoZSBjaGVjayBvZiBzcGxpdHRpbmcgY29tcG91bmQgcGFnZXM=?= Content-Type: text/plain; charset="UTF-8" X-MAIL:mse-fl2.zte.com.cn 3AECaR48053112 X-Fangmail-Gw-Spam-Type: 0 X-Fangmail-Anti-Spam-Filtered: true X-Fangmail-MID-QID: 655369D0.000/4SV5Q82TyLz4xPYj X-Rspamd-Server: rspam09 X-Rspamd-Queue-Id: A705A100009 X-Stat-Signature: wtpnpddio3x3x3j4gzp6rry6ebxchbre X-Rspam-User: X-HE-Tag: 1699965397-861933 X-HE-Meta: U2FsdGVkX1/6YGqsSR4ENTezn1+OhPdhb7QJ9UYFc/0+trVqFocz7pL0p0i7pFR02NPoS86oFFo2uhFoL7QUo0Sr9LMPRAY/6hgnhfXIyNCPumBWkY04LAVz1NV/eZ8xNOXx8fmtREkSS2KkAINuTPzIeWNcGR6iNlMQVnpR5CWyUEDczJ54r9ejB9uVXik3YWOoyUHD+QhTtt2VbSkTLlbumUyIuKRddPbkcqbrtR+f1/i885UvTollMp2lSpEbeSOh0s83C8Lu5Es6xh6PWYL/2lPsSnyDj6NThynuG/MxaP0SUZqlYKrGcUdNfuwb1RPXz63fb+abmILnaintf36MO8L866t+TtfrBRb4xrFLstdIxxM08UklNy4zoFkzYNN10LO5PR7mvHEJhANwM8LNSTY80YCwe8JZT4levSOqE4kC8cYbVWJhpsAElEr2bguJQtROOIVbOY+HQj05p/AQ8clyHlE6Bb+TPgG7SOJmkLTYxnLII+U5nFK2Bu7v8S1z2IlpGxzVwCJG8EPuDbmm9sIVUfUUVAQbOFxJYPzMdaYOFrxJuKlvJw+IxET4qgzSl2GrVuvDKfdW2aJRlNE7BNDN6Y/b48X9HWWGNk1Mle+k7fDKrDFj73xnd6ks3rqw+sposiU3gL1anaQ1nFvUWDG+g35bKUIBRZGNujQQzQ6kpJeajRdeVj7X43lMKsbqSI8m3yIj0oe9O0sYgt4T3vQsRvQBsbuudgujUdeBxL7DoyDX6aXOwUK46f9kXVrH7J+ia0wNq7ea7DYxv6BnywPKxW6nQRZbSyfhVjht5SYjGqPdv+FUcJB61oCv+aBZh61ZbOWZ56hNSDJ4iv+Xpglvcxj9uzcI99VEFG4TOdNWoXYDvT0CdB9wcoc0PulV/ZAnxUCDV7LU8zT0NUMubj8Js5QE2/04H6pi/PDElNKaRfLsN9fxXogStsLU5exOLXCM+HBM38BFGSw 5bu/wzdk 3Fgxzv4H0fa17FrDrNLfNXuZ5Yn2jMWU+0IJJGptDIrh6WaBWdAy+ci8uDjXMyn9eGK+xn0zyOOFp65RZnX+ypAXlzcIomUFU7hjim5YSV4sBheUYF2rZ3zZzXC0ic9rPbpxIO9kEjUpy/eKXPnmDHtMaVFRd0xkcdv+wk2hF5mgiX+7Ujj4g/y4oyA5/aGCtsEKT/nHkcDWtUDqnARrCZp274+2/SHNVBPGPNkyQ2xMichdzTRwdje91qukb0NDfmFm1HA21q9QXBk9//3MPtUpnWj4ow8els630 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: List-Subscribe: List-Unsubscribe: From: xu xin Background ========== When trying to merge two pages, it may fail because the two pages belongs to the same compound page and split_huge_page fails due to the incorrect reference to the page. To solve the problem, the commit 77da2ba0648a4 ("mm/ksm: fix interaction with THP") tries to split the compound page after try_to_merge_two_pages() fails and put_page in that case. However it is too early to calculate of the variable 'split' which indicates whether the two pages belongs to the same compound page. What to do ========== If try_to_merge_two_pages() succeeds, there is no need to check whether to splitting compound pages. So we delay the check of splitting compound pages until try_to_merge_two_pages() fails, which can improve the processing efficiency of cmp_and_merge_page() a little. Signed-off-by: xu xin Reviewed-by: Yang Yang --- mm/ksm.c | 36 ++++++++++++++++++++---------------- 1 file changed, 20 insertions(+), 16 deletions(-) diff --git a/mm/ksm.c b/mm/ksm.c index 7efcc68ccc6e..c952fe5d9e43 100644 --- a/mm/ksm.c +++ b/mm/ksm.c @@ -2229,24 +2229,10 @@ static void cmp_and_merge_page(struct page *page, struct ksm_rmap_item *rmap_ite tree_rmap_item = unstable_tree_search_insert(rmap_item, page, &tree_page); if (tree_rmap_item) { - bool split; - kpage = try_to_merge_two_pages(rmap_item, page, tree_rmap_item, tree_page); - /* - * If both pages we tried to merge belong to the same compound - * page, then we actually ended up increasing the reference - * count of the same compound page twice, and split_huge_page - * failed. - * Here we set a flag if that happened, and we use it later to - * try split_huge_page again. Since we call put_page right - * afterwards, the reference count will be correct and - * split_huge_page should succeed. - */ - split = PageTransCompound(page) - && compound_head(page) == compound_head(tree_page); - put_page(tree_page); if (kpage) { + put_page(tree_page); /* * The pages were successfully merged: insert new * node in the stable tree and add both rmap_items. @@ -2271,7 +2257,25 @@ static void cmp_and_merge_page(struct page *page, struct ksm_rmap_item *rmap_ite break_cow(tree_rmap_item); break_cow(rmap_item); } - } else if (split) { + } else { + bool split; + /* + * If both pages we tried to merge belong to the same compound + * page, then we actually ended up increasing the reference + * count of the same compound page twice, and split_huge_page + * failed. + * Here we set a flag if that happened, and we use it later to + * try split_huge_page again. Since we call put_page right + * afterwards, the reference count will be correct and + * split_huge_page should succeed. + */ + + split = PageTransCompound(page) + && compound_head(page) == compound_head(tree_page); + put_page(tree_page); + + if (!split) + return; /* * We are here if we tried to merge two pages and * failed because they both belonged to the same -- 2.15.2