From patchwork Fri Mar 22 08:36:57 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: alexs@kernel.org X-Patchwork-Id: 13599737 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 37E48C47DD9 for ; Fri, 22 Mar 2024 08:35:09 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id B98546B007B; Fri, 22 Mar 2024 04:35:03 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id AAB766B00A1; Fri, 22 Mar 2024 04:35:03 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 8D6106B00A2; Fri, 22 Mar 2024 04:35:03 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 72C786B007B for ; Fri, 22 Mar 2024 04:35:03 -0400 (EDT) Received: from smtpin18.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 4BCDA40C0A for ; Fri, 22 Mar 2024 08:35:03 +0000 (UTC) X-FDA: 81924014886.18.E69F204 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf10.hostedemail.com (Postfix) with ESMTP id 913C2C0022 for ; Fri, 22 Mar 2024 08:35:01 +0000 (UTC) Authentication-Results: imf10.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=NxgLxxat; spf=pass (imf10.hostedemail.com: domain of alexs@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=alexs@kernel.org; dmarc=pass (policy=none) header.from=kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1711096501; 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=O4+CNYBiV8ESo7ClEjsaF8u2fKS65m2uzzLH53JtmBs=; b=tYXhImA5RN0PPDnQyo2Q4QYgl6rqyJoD1IebV6A7WN/XrhGfqDWsuFFPgO7LGc/waBQKip WMF6EQNoCCDZsOlHkKXZpq/5ctDGSLXON6B/WC3dLwSfSHBHZqXqvAOdRKmSuimRxh4JtI 72geE6hyt5IarZ+zs6C80A7SnX5JOwc= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1711096501; a=rsa-sha256; cv=none; b=3jafrSPCQGxeldIOpHxSHCZQ3tkg9aYhIAm0YelSKRZvSpS2dp2ZwnQynhTtgMu6DgCFUd oSezazk7C8Gjr7BtVmXtTfgu40/utgC2+qsYJQQVSV9ZIpnCwMu+mhm1eAeG1riwpa7lLa CL5qs1S4++ICHQoHpRFUd9MubGSvO08= ARC-Authentication-Results: i=1; imf10.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=NxgLxxat; spf=pass (imf10.hostedemail.com: domain of alexs@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=alexs@kernel.org; dmarc=pass (policy=none) header.from=kernel.org Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by dfw.source.kernel.org (Postfix) with ESMTP id D6AA761426; Fri, 22 Mar 2024 08:35:00 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 2ECFEC43394; Fri, 22 Mar 2024 08:34:57 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1711096500; bh=tE7d8xapWcivudbf59T+rNoo9gKt46dsnpm72y6dZEY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=NxgLxxat8Cj4ildJ74x/Fd/oyvTl1lC8ltXuHcrbg8hA9W1zCZaJwjeSNy0rn0GOA SnyH5kIw50hurn5c6Vaan+FsIrUW+N5/afbMHvwifwhnnWZeEQNmCSQf6BpVtapgJr e1vIoiyqsT+Fd/JEnk5QypnCg23X1NgbzqS3mBCE267Cpla60qvsZX7Dz4cQHrkxMX 6kmj9zi4Y/iNjz2q/n5RXZtusItg5eiJ1Xl8TAeDTXPKOXSrjWXweEa1YAH6GP6x4t 7vVZuVpgCZBXUunmMK/veTVNgUvUx4zqzzis58a/Ce05HwTSQ5nmNZVG8giIaaqX+W tclwJALa4D3TQ== From: alexs@kernel.org To: Matthew Wilcox , Andrea Arcangeli , Izik Eidus , david@redhat.com, Andrew Morton , linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: "Alex Shi (tencent)" , Hugh Dickins , Chris Wright Subject: [PATCH v2 10/14] mm/ksm: Convert stable_tree_search to use folio Date: Fri, 22 Mar 2024 16:36:57 +0800 Message-ID: <20240322083703.232364-11-alexs@kernel.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240322083703.232364-1-alexs@kernel.org> References: <20240322083703.232364-1-alexs@kernel.org> MIME-Version: 1.0 X-Rspamd-Queue-Id: 913C2C0022 X-Rspam-User: X-Rspamd-Server: rspam11 X-Stat-Signature: tijwxdcdzo6x9k1gidju94tmpp93sspm X-HE-Tag: 1711096501-566841 X-HE-Meta: U2FsdGVkX18MAThZGwMiodwfXI0JLgf6wbNkvFCiuwXVy7nE0lqvHRwHpH9GWyKoWtfJAVKMFKjKJZoPxGMxd7pYx64W39cmf3WcjVhqxzigVYeP7S706sEo+BkcEOWMHwxzGUid6rpr3fQ3fPRm0Z2PmbDmsrzRnhkLKpBA181Hec294eZGpBMY0J6EJqKPVXn+ppXJvLN+ZSx5w22TmKWJXSQibcR9T7nNaBzzTYVsrUYy4IKpoJAnmEygWFHfxcybU0/hNPtsbojSj3q4H8aqaHwb4SYYrOIWWTg5ZO5FqoCiNH2+KcDRmTnEmSj5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lFv3phPulkxhF+w7fHBEmD/uHW5u8S0h1Xw6JkLaf7LtfkRO0nU38E2IwhKN4ongtE/MSPTf1e3yRdmhkTuSlpEryh4PUQMCN 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: "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 | 58 +++++++++++++++++++++++++++++--------------------------- 1 file changed, 30 insertions(+), 28 deletions(-) diff --git a/mm/ksm.c b/mm/ksm.c index e6837e615ef0..0692bede5ca6 100644 --- a/mm/ksm.c +++ b/mm/ksm.c @@ -1828,7 +1828,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; @@ -1836,28 +1836,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 = page_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 @@ -1891,11 +1893,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 = ksm_get_folio(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 @@ -1908,8 +1910,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(page, &tree_folio->page); + folio_put(tree_folio); parent = *new; if (ret < 0) @@ -1952,26 +1954,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 = ksm_get_folio(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; } } @@ -1984,8 +1986,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; @@ -2010,12 +2012,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)); @@ -2026,16 +2028,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 */