Received: by 2002:ab2:620c:0:b0:1ef:ffd0:ce49 with SMTP id o12csp1351229lqt; Wed, 20 Mar 2024 00:39:29 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCWvwY4CCUIU0L7oy/SydEqEiyST0ndOvDJBkPvZ/PbcqiMoffb9Y6pa9lIYek1GQ9+xTnIiuovAtsAn7AdQPP0klLlViS8Wq0v3x3BSMQ== X-Google-Smtp-Source: AGHT+IFvg/92nW7eOBMU3SAPeR8KuI1hCOqSTaHjxWfdvlr1v4wLAtGsHlEgJpkSsa/9b+7pAU5d X-Received: by 2002:a0c:e248:0:b0:691:2466:51f4 with SMTP id x8-20020a0ce248000000b00691246651f4mr16668848qvl.48.1710920369125; Wed, 20 Mar 2024 00:39:29 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1710920369; cv=pass; d=google.com; s=arc-20160816; b=ZKzlRPxIyuDACpqO1J/FyVW9QNW4mY6imeddEAcG8UwX/EedGUFL1tOrRMIUNEfKCC 4AB/l6TTBgAyf8jqkfacvHQvJcNDlwoxFodPv8P7YEWZzbxmatd+5N1bGSzUKaHhTdNH nDGRnGdxE9+u1QapO6w8YbZZ8mqv9h3/mGwGy6gaNkyqGBWyddzDZxZJgXX4LYcOmxCj ctTDFwIHxwTGVsayn0s/4WXoeUGpYe4KOHozsgNIiHSLV1IO2hTwVk8Hi1vHQOu8FDcI /mng70Q4l/nlsV0vpcPlnmRPAYP/aGfmr+b8jHWhMsBTShkBYiqSGg0djScbH3jDO/W6 E6IQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=EfnUB+zh6Jkuo8KxC5rPnusQaj/Ro7vi77DeDl1FMzg=; fh=HTsbfkBy5TsRnyWiG7NOinEtk+/LWD8E51EQEMbLwTM=; b=0wFEB9JTsKUh03gGYfSxsLL7PM5eOw1r+0iTz6qQZJgFGPmju2kzwfkE8YGL/20GFb TOkzrp+zYzKh9TINp1Nn+ekkN431sU9GEUp7hgPkzFw6FR/8EFZzAlNtwqTt44ioi13/ qQ2E0QM1gHIaOZ5o+wOlpyBkNb5LNLQyzfeuG73TNxU+MA9xnqWRnElHEzZwb3XuhMAa mE/HhK6UABwDSFmPYQKoY6nCz922KvTvWrpfJYLRc4X2rbxyKWlR3OCRTeXkNKjtMzJp uTmRrxumxL+VzmJOL/oOiAvMAMH/vRtLv2sz8HDOYMBhBogOBfEbRDJ75P7JYyD2Nk/i of0w==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=QnyMHcy7; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-108558-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-108558-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id fv1-20020a056214240100b00690de2bac5csi12788143qvb.341.2024.03.20.00.39.28 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 20 Mar 2024 00:39:29 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-108558-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) client-ip=147.75.199.223; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=QnyMHcy7; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-108558-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-108558-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id C54A21C22778 for ; Wed, 20 Mar 2024 07:39:28 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id CDCDB3D549; Wed, 20 Mar 2024 07:38:06 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="QnyMHcy7" Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id DF6803D0AD for ; Wed, 20 Mar 2024 07:38:05 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710920286; cv=none; b=lc8SSXU7Dtysu5Jh+d21NADqCKz6BZSOAlYOxKhgBsLC7Pwi3Gu1ZORlowPiqzEgCZbWWLBTIrgKM3FfCggqjxF04doS0ATRMp3uSh0K9R/9cZf7+8f6tnKS85NBUleesYcL6wMNYF8Pnb7LhHUPe464u/DaKxcIWb6UCe+b5HA= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710920286; c=relaxed/simple; bh=tjVPu2LfV6/oJL+/cGFCPeE7wstp4Ud62/5nB+tu7Fo=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=ujnrxEeS0vTA/UMIe7QyHa6/vZGnN6NoCYHft7B26AtBiIOakwfKFTQRsec1eVOQRUaXU581diac2FK2xk+GnytJC4NekC0aKO91Yk1Pw2z9YPJ1y0x9PEcn2JPG3jGYBUpkcdNLD2TdvC1V5SFDheMolRhJ7epILyz10QIZN8s= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=QnyMHcy7; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id F12D5C43390; Wed, 20 Mar 2024 07:38:02 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1710920285; bh=tjVPu2LfV6/oJL+/cGFCPeE7wstp4Ud62/5nB+tu7Fo=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=QnyMHcy70fzp8mjnSBi4o1+7TO69JWuiRoHjNyhFVoYTtNUcOygYYs1aYzSwHZ4tP B1BEQoxvGpCI5WgyIKpDPphqywUn0C0GwKZ9osVoCfI7FwSszybl7JM+X7kspzNj1U ZaUyDOXUqJMrYPlAP3tjgfC0glaUofbiUSDuv3ZHGJJXB3urIxTdY8dqSOJpLF7Q4J +0xpRLKV7XWf7OvDi6wMx13J5ewLV5vUggPB+1o1apGUrBvZIVUI7PM1LA3+YdCGy4 16PM0s7aJeQoqB4eV7jgHGwGxedDMFcl/prNrGkefrYvjHT9OTNC9gi84ELn/jrimA l05LCkNlt1caA== From: alexs@kernel.org To: Izik Eidus , Matthew Wilcox , Andrea Arcangeli , Hugh Dickins , Chris Wright , kasong@tencent.com, Andrew Morton , linux-mm@kvack.org (open list:MEMORY MANAGEMENT), linux-kernel@vger.kernel.org (open list) Cc: linux-kernel@vger.kernel.org, "Alex Shi (tencent)" Subject: [PATCH 09/11] mm/ksm: Convert stable_tree_search to use folio Date: Wed, 20 Mar 2024 15:40:45 +0800 Message-ID: <20240320074049.4130552-10-alexs@kernel.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240320074049.4130552-1-alexs@kernel.org> References: <20240320074049.4130552-1-alexs@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit From: "Alex Shi (tencent)" Although, the func may pass a tail page to check its contents, but only single page exist in KSM stable tree, so we still can use folio in stable_tree_search() to save a few compound_head calls. Signed-off-by: Alex Shi (tencent) Cc: Izik Eidus Cc: Matthew Wilcox Cc: Andrea Arcangeli Cc: Hugh Dickins Cc: Chris Wright --- mm/ksm.c | 60 +++++++++++++++++++++++++++++--------------------------- 1 file changed, 31 insertions(+), 29 deletions(-) diff --git a/mm/ksm.c b/mm/ksm.c index 71d1a52f344d..75401b3bae5c 100644 --- a/mm/ksm.c +++ b/mm/ksm.c @@ -1812,7 +1812,7 @@ static __always_inline void *chain(struct ksm_stable_node **s_n_d, * This function returns the stable tree node of identical content if found, * NULL otherwise. */ -static struct page *stable_tree_search(struct page *page) +static void *stable_tree_search(struct page *page) { int nid; struct rb_root *root; @@ -1820,28 +1820,30 @@ static struct page *stable_tree_search(struct page *page) struct rb_node *parent; struct ksm_stable_node *stable_node, *stable_node_dup, *stable_node_any; struct ksm_stable_node *page_node; + struct folio *folio; - page_node = page_stable_node(page); + folio = (struct folio *)page; + page_node = folio_stable_node(folio); if (page_node && page_node->head != &migrate_nodes) { /* ksm page forked */ - get_page(page); - return page; + folio_get(folio); + return folio; } - nid = get_kpfn_nid(page_to_pfn(page)); + nid = get_kpfn_nid(folio_pfn(folio)); root = root_stable_tree + nid; again: new = &root->rb_node; parent = NULL; while (*new) { - struct page *tree_page; + struct folio *tree_folio; int ret; cond_resched(); stable_node = rb_entry(*new, struct ksm_stable_node, node); stable_node_any = NULL; - tree_page = chain_prune(&stable_node_dup, &stable_node, root); + tree_folio = chain_prune(&stable_node_dup, &stable_node, root); /* * NOTE: stable_node may have been freed by * chain_prune() if the returned stable_node_dup is @@ -1875,11 +1877,11 @@ static struct page *stable_tree_search(struct page *page) * write protected at all times. Any will work * fine to continue the walk. */ - tree_page = get_ksm_page(stable_node_any, - GET_KSM_PAGE_NOLOCK); + tree_folio = get_ksm_page(stable_node_any, + GET_KSM_PAGE_NOLOCK); } VM_BUG_ON(!stable_node_dup ^ !!stable_node_any); - if (!tree_page) { + if (!tree_folio) { /* * If we walked over a stale stable_node, * get_ksm_page() will call rb_erase() and it @@ -1892,8 +1894,8 @@ static struct page *stable_tree_search(struct page *page) goto again; } - ret = memcmp_pages(page, tree_page); - put_page(tree_page); + ret = memcmp_pages(&folio->page, &tree_folio->page); + folio_put(tree_folio); parent = *new; if (ret < 0) @@ -1936,26 +1938,26 @@ static struct page *stable_tree_search(struct page *page) * It would be more elegant to return stable_node * than kpage, but that involves more changes. */ - tree_page = get_ksm_page(stable_node_dup, - GET_KSM_PAGE_TRYLOCK); + tree_folio = get_ksm_page(stable_node_dup, + GET_KSM_PAGE_TRYLOCK); - if (PTR_ERR(tree_page) == -EBUSY) + if (PTR_ERR(tree_folio) == -EBUSY) return ERR_PTR(-EBUSY); - if (unlikely(!tree_page)) + if (unlikely(!tree_folio)) /* * The tree may have been rebalanced, * so re-evaluate parent and new. */ goto again; - unlock_page(tree_page); + folio_unlock(tree_folio); if (get_kpfn_nid(stable_node_dup->kpfn) != NUMA(stable_node_dup->nid)) { - put_page(tree_page); + folio_put(tree_folio); goto replace; } - return tree_page; + return tree_folio; } } @@ -1968,8 +1970,8 @@ static struct page *stable_tree_search(struct page *page) rb_insert_color(&page_node->node, root); out: if (is_page_sharing_candidate(page_node)) { - get_page(page); - return page; + folio_get(folio); + return folio; } else return NULL; @@ -1994,12 +1996,12 @@ static struct page *stable_tree_search(struct page *page) &page_node->node, root); if (is_page_sharing_candidate(page_node)) - get_page(page); + folio_get(folio); else - page = NULL; + folio = NULL; } else { rb_erase(&stable_node_dup->node, root); - page = NULL; + folio = NULL; } } else { VM_BUG_ON(!is_stable_node_chain(stable_node)); @@ -2010,16 +2012,16 @@ static struct page *stable_tree_search(struct page *page) DO_NUMA(page_node->nid = nid); stable_node_chain_add_dup(page_node, stable_node); if (is_page_sharing_candidate(page_node)) - get_page(page); + folio_get(folio); else - page = NULL; + folio = NULL; } else { - page = NULL; + folio = NULL; } } stable_node_dup->head = &migrate_nodes; list_add(&stable_node_dup->list, stable_node_dup->head); - return page; + return folio; chain_append: /* stable_node_dup could be null if it reached the limit */ @@ -2109,7 +2111,7 @@ static struct ksm_stable_node *stable_tree_insert(struct folio *kfolio) * fine to continue the walk. */ tree_folio = get_ksm_page(stable_node_any, - GET_KSM_PAGE_NOLOCK); + GET_KSM_PAGE_NOLOCK); } VM_BUG_ON(!stable_node_dup ^ !!stable_node_any); if (!tree_folio) { -- 2.43.0