From nobody Fri Feb 13 03:18:45 2026 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 E8C1814036B for ; Tue, 4 Jun 2024 04:21:46 +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=1717474907; cv=none; b=RXHIIXTr37HHCn5paPq6TAHj3N7XCc+VMWCqwHqyUPMO4bqmqYonrmSGfDeWNRy/6znNPePIhPnyr9bJM4beIYTiumioia5psV1qGmnvgE4Q3DJi+8v7Z+qIMSg51MWXUwL3sKEdTOM1l37wHOzF7sDJzv7HencRX0OfxJgsw38= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717474907; c=relaxed/simple; bh=coNrFyNql7rzA+Wu1zS+Imisztx1VEYHkdkeqtcLPJ0=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=ezmnEAc1eLdIaBdDttsjMccv6OOGSqY0bBzGgzUf+ecId+BFIWRgl/ZR0rtDz7j4lxWAxYh94Wt2YmRvrdhDMbRBSYrCyBB+srBPa7vnF6jSRD2GvGxiM7YBoFmCnVvRIhjGJeOLg9AAgq8/sf2oxx63DLnCyAeC7e19s3Im0WU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=MDSl5xy6; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="MDSl5xy6" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 7F76EC2BBFC; Tue, 4 Jun 2024 04:21:44 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1717474906; bh=coNrFyNql7rzA+Wu1zS+Imisztx1VEYHkdkeqtcLPJ0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=MDSl5xy6AfvB2jwDYUYciKw0WAPH6HVMDLpvUtKv4MiMW2nJPsr8687kDmSuMgS2i 8ir4mEf4IzJsZxyqCpwMRG7zPuqEQoO2FcqBJt1Toe9V6ugm2H3oSFrRFxB0nJRYvO 6ZjV1r2iscSWsLM8U1FBXXxPO9kLwGNyw46O7MPHow2J0CcrcBysxPOMtS9OSJHTXn kLsG0fd7lCw5iMa882oNvtx9mV5k5rsta8bJgKJcmH9Tjy3MLvJBxnK5D0J6VcqtKB /pWRYqxUdW7oaEdmPbUUkTDWV9VnNFT8B2tYE9n58p6TzwY1lajVhpANa7W9F7pRhx S/9+Par00zx4g== From: alexs@kernel.org To: Andrew Morton , linux-mm@kvack.org, linux-kernel@vger.kernel.org, izik.eidus@ravellosystems.com, willy@infradead.org, aarcange@redhat.com, chrisw@sous-sol.org, hughd@google.com, david@redhat.com Cc: "Alex Shi (tencent)" Subject: [PATCH 05/10] mm/ksm: use folio in stable_tree_search Date: Tue, 4 Jun 2024 12:24:47 +0800 Message-ID: <20240604042454.2012091-6-alexs@kernel.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240604042454.2012091-1-alexs@kernel.org> References: <20240604042454.2012091-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: quoted-printable Content-Type: text/plain; charset="utf-8" From: "Alex Shi (tencent)" The stable_tree_search() function is actually using folio internal, just pass the folio parameter from cmp_and_merge_page(). And in cmp_and_merge_page(), the parameter 'page' is actually a folio too, so use folio to save a few compound check in necessnary context. Signed-off-by: Alex Shi (tencent) --- mm/ksm.c | 36 ++++++++++++++++++------------------ 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/mm/ksm.c b/mm/ksm.c index 2e4790842515..f68779651841 100644 --- a/mm/ksm.c +++ b/mm/ksm.c @@ -1825,7 +1825,7 @@ static __always_inline struct folio *chain(struct ksm= _stable_node **s_n_d, * This function returns the stable tree node of identical content if foun= d, * NULL otherwise. */ -static struct page *stable_tree_search(struct page *page) +static struct folio *stable_tree_search(struct folio *folio) { int nid; struct rb_root *root; @@ -1833,14 +1833,12 @@ 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; =20 - folio =3D page_folio(page); page_node =3D folio_stable_node(folio); if (page_node && page_node->head !=3D &migrate_nodes) { /* ksm page forked */ folio_get(folio); - return &folio->page; + return folio; } =20 nid =3D get_kpfn_nid(folio_pfn(folio)); @@ -1907,7 +1905,7 @@ static struct page *stable_tree_search(struct page *p= age) goto again; } =20 - ret =3D memcmp_pages(page, &tree_folio->page); + ret =3D memcmp_pages(folio_page(folio, 0), &tree_folio->page); folio_put(tree_folio); =20 parent =3D *new; @@ -1973,7 +1971,7 @@ static struct page *stable_tree_search(struct page *p= age) folio_put(tree_folio); goto replace; } - return &tree_folio->page; + return tree_folio; } } =20 @@ -1987,7 +1985,7 @@ static struct page *stable_tree_search(struct page *p= age) out: if (is_page_sharing_candidate(page_node)) { folio_get(folio); - return &folio->page; + return folio; } else return NULL; =20 @@ -2037,7 +2035,7 @@ static struct page *stable_tree_search(struct page *p= age) } stable_node_dup->head =3D &migrate_nodes; list_add(&stable_node_dup->list, stable_node_dup->head); - return &folio->page; + return folio; =20 chain_append: /* stable_node_dup could be null if it reached the limit */ @@ -2324,6 +2322,7 @@ static void cmp_and_merge_page(struct page *page, str= uct ksm_rmap_item *rmap_ite unsigned int checksum; int err; bool max_page_sharing_bypass =3D false; + struct folio *folio, *kfolio; =20 stable_node =3D page_stable_node(page); if (stable_node) { @@ -2346,30 +2345,31 @@ static void cmp_and_merge_page(struct page *page, s= truct ksm_rmap_item *rmap_ite } =20 /* We first start with searching the page inside the stable tree */ - kpage =3D stable_tree_search(page); - if (kpage =3D=3D page && rmap_item->head =3D=3D stable_node) { - put_page(kpage); + folio =3D page_folio(page); + kfolio =3D stable_tree_search(folio); + if (kfolio =3D=3D folio && rmap_item->head =3D=3D stable_node) { + folio_put(kfolio); return; } =20 remove_rmap_item_from_tree(rmap_item); =20 - if (kpage) { - if (PTR_ERR(kpage) =3D=3D -EBUSY) + if (kfolio) { + if (kfolio =3D=3D ERR_PTR(-EBUSY)) return; =20 - err =3D try_to_merge_with_ksm_page(rmap_item, page, kpage); + err =3D try_to_merge_with_ksm_page(rmap_item, page, folio_page(kfolio, 0= )); if (!err) { /* * The page was successfully merged: * add its rmap_item to the stable tree. */ - lock_page(kpage); - stable_tree_append(rmap_item, page_stable_node(kpage), + folio_lock(kfolio); + stable_tree_append(rmap_item, folio_stable_node(kfolio), max_page_sharing_bypass); - unlock_page(kpage); + folio_unlock(kfolio); } - put_page(kpage); + folio_put(kfolio); return; } =20 --=20 2.43.0