From mboxrd@z Thu Jan 1 00:00:00 1970 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 96135C5ACB3 for ; Thu, 16 Nov 2023 12:17:24 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id BBC256B0411; Thu, 16 Nov 2023 07:17:23 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id B6CED6B041A; Thu, 16 Nov 2023 07:17:23 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A33FA6B0421; Thu, 16 Nov 2023 07:17:23 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 934196B0411 for ; Thu, 16 Nov 2023 07:17:23 -0500 (EST) Received: from smtpin07.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 3A32180BFD for ; Thu, 16 Nov 2023 12:17:23 +0000 (UTC) X-FDA: 81463717566.07.08A8C42 Received: from mail-pl1-f195.google.com (mail-pl1-f195.google.com [209.85.214.195]) by imf22.hostedemail.com (Postfix) with ESMTP id 4D020C0005 for ; Thu, 16 Nov 2023 12:17:21 +0000 (UTC) Authentication-Results: imf22.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=QWjDQlga; spf=pass (imf22.hostedemail.com: domain of xu.xin.sc@gmail.com designates 209.85.214.195 as permitted sender) smtp.mailfrom=xu.xin.sc@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1700137041; 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-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=0aq51udqPabDrhMIIqZFnTBT5LeHwsY7uMRmk0JI2FA=; b=q2pCHEPx7vCrtN/P+Ely+gEeVLRzagrVqgKK9k+glU5EhmDmnC6Tug1MAeD6UMN1AG/VPe Z1gyp2neQGWWFxVXTYswLNcNZINUfHgrDGpqM+a/FY9Dn9eviCYGNkJpNmgwdobmmEZ1x2 2bw/hxm3+M+xOYgyb6TNjW8G+vZVRBU= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1700137041; a=rsa-sha256; cv=none; b=u5Qc094hX7+Lr46930Ie59k/AlwVoL8OeY0V2D8FBbigsJcPQ6EnoC0DIyhxSSexMIWVZL JgRBbg/2OChpJHuKwzKJ2jJ7SxM4bgEZx+MecQIO9Fn3HNg8F482DT4nL/w8GEuusfBNeq 1pYiPNmXZRVWwHCcAsBBaeVnu+Bk+b8= ARC-Authentication-Results: i=1; imf22.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=QWjDQlga; spf=pass (imf22.hostedemail.com: domain of xu.xin.sc@gmail.com designates 209.85.214.195 as permitted sender) smtp.mailfrom=xu.xin.sc@gmail.com; dmarc=pass (policy=none) header.from=gmail.com Received: by mail-pl1-f195.google.com with SMTP id d9443c01a7336-1cc921a4632so6776205ad.1 for ; Thu, 16 Nov 2023 04:17:21 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1700137040; x=1700741840; darn=kvack.org; 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=0aq51udqPabDrhMIIqZFnTBT5LeHwsY7uMRmk0JI2FA=; b=QWjDQlgaAz1Pk3yI8iYUeT+DXL6b7CnhJ3xWgBuZLPiNtgMtSmJySKUoFlXYhzN8+E IEHX/IYnoNrVlCCm2Z2LnSkpluSzRK7fxO7DXanjdaw7x2i7qzhWqlk3/IfjhN+h3fd/ HChUqeH2BXy5OS3qWh/hzn1z3HTa1Fb0nH2ytvQTvlM8N6xI1CTrcO5b/JsbdYwbWZB5 QHt0oXfLODXMBMaSrx0H2w4nljUYXnDk6ca5bzfkvT3hr/aNszMTsGK21eh72QFVQAvi ttbd+LxmMqnjL/qwVtuxvS+SWBZla+7LUgK+9CbhLN4XD2hAitUz4DugV/4K4pQwrbI0 zIrA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1700137040; x=1700741840; 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=0aq51udqPabDrhMIIqZFnTBT5LeHwsY7uMRmk0JI2FA=; b=PAuoQTfVoUf2ERu8PS0dEMHWh7v/O3OwnTRANf4T6nL/j76EDpdWfevubnLNp97yCZ JNQzM3qYhaeiqfm3vnuZsZBjICa5Kvld/acM9HbLZ9ixMavQpTw0BqFvr+krcCM9H+qH xQ61J3ATKtElIt6Vv7wDLgDcHvOXjVBLP1F+YFqXq2+whAcMfKDbxyDGj+ckFuBIeg98 PcG9EL8r29JvA+WW5U9po/RpEM8O2jjXRDbB7gOL5aUcVNNVZCeb88IYybCSxLA9oJrn di1e8kt5AN64AK/W9r+WiOkfuo8XVKB6PF5s3eF7tj9alUNT+UpXkCcYUD3MFjb54XnO pbvg== X-Gm-Message-State: AOJu0Yw+RaCTEubUj9xX7LqLpsGBR4Yllnt6qYO/eC3tIJp+X2Z8jeE1 r8E+2QD3d2aD3v114FzygQA= X-Google-Smtp-Source: AGHT+IGccebkPsrncb2428ZzEoT212XQZ/yDquuab+aT+2dHdHsC7S/sg7r7nKcnYlGPujAmIRCKpw== X-Received: by 2002:a17:902:e803:b0:1cc:4985:fc15 with SMTP id u3-20020a170902e80300b001cc4985fc15mr9318166plg.27.1700137040000; Thu, 16 Nov 2023 04:17:20 -0800 (PST) Received: from localhost.localdomain ([193.203.214.57]) by smtp.gmail.com with ESMTPSA id p18-20020a170902e75200b001b8b45b177esm9085780plf.274.2023.11.16.04.17.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 16 Nov 2023 04:17:19 -0800 (PST) From: xu X-Google-Original-From: xu To: david@redhat.com Cc: akpm@linux-foundation.org, imbrenda@linux.ibm.com, jiang.xuexin@zte.com.cn, linux-kernel@vger.kernel.org, linux-mm@kvack.org, ran.xiaokai@zte.com.cn, wang.yong12@zte.com.cn, xu.xin.sc@gmail.com, xu.xin16@zte.com.cn, yang.yang29@zte.com.cn Subject: Re: [PATCH] ksm: delay the check of splitting compound pages Date: Thu, 16 Nov 2023 12:17:15 +0000 Message-Id: <20231116121715.1974713-1-xu.xin16@zte.com.cn> X-Mailer: git-send-email 2.25.1 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Rspamd-Queue-Id: 4D020C0005 X-Rspam-User: X-Rspamd-Server: rspam11 X-Stat-Signature: 6uq5irw1nrfzijem7f55dzarpo9w1jg7 X-HE-Tag: 1700137041-174270 X-HE-Meta: U2FsdGVkX1+psCnn4OU/KJMVxcdqPrbe+o6xjHx0kAdLH3prT1XpwAckQ6Imp57Fjq9pt7Kcfw/DWKM7VPEsGgaGk3+f7KdXaO4+M5zZ1L0tqfBQSmIooPTcROgML+kISJP/hXha18EDNDE3Lx5kf/CUi5ZdqGYIS4tyO3/MZfOv954Ublx2d7RQHN1qQaesYh0rKr8+2AhOEv7G0jX96Y7XQTjYwytIIzj69iWJ68zAargEv9hXbz4UbUO57PilTCG60x5hBVCdsCcj3jBn1rtg37uf7dgmXy/478olyh7S32fw5ZIkAskzuAL+1zaQQLMHdP/jntdtn9dtFgCihUd2xnTSMmt9I+e9JipFbL2/RzFeIY8kqJKywQtPjbsuxQqMnHIc2i3o0F1NJ3flWNLlfWgJ22dPFs8O0qGwzGQFY+ziOyNzzAhZiNUodWN5GG0c9a8WLWE40BSiOY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owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: >>>> @@ -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. >>>> - */ >>> >>> I'm curious, why can't we detect that ahead of time and keep only a >>> single reference? Why do we need the backup code? Anything I am missing? Do you mean like this? --- a/mm/ksm.c +++ b/mm/ksm.c @@ -2229,23 +2229,21 @@ 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; + bool SameCompound; + /* + * If they belongs to the same compound page, its' reference + * get twice, so need to put_page once to avoid that + * split_huge_page fails in try_to_merge_two_pages(). + */ + if (SameCompound = Is_SameCompound(page, tree_page)) + put_page(tree_page); 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 (!SameCompound) + put_page(tree_page); + if (kpage) { /* * The pages were successfully merged: insert new @@ -2271,20 +2269,6 @@ 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) { - /* - * We are here if we tried to merge two pages and - * failed because they both belonged to the same - * compound page. We will split the page now, but no - * merging will take place. - * We do not want to add the cost of a full lock; if - * the page is locked, it is better to skip it and - * perhaps try again later. - */ - if (!trylock_page(page)) - return; - split_huge_page(page); - unlock_page(page); } } } >> >> I don't know the original reason, better ask Claudio Imbrenda . >> Maybe because doing detection that ahead of time will break several funtions' semantic, >> such as try_to_merge_two_pages(), try_to_merge_with_ksm_page() and try_to_merge_one_page() >> >> Adding the backup code don't change the old code and fixing the old problem, it's good. > >It's absolutely counter-intuitive to check for something that cannot >possibly work after the effects. This better has a good reason to make >that code more complicated. >--