From patchwork Mon Mar 25 12:48: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: 13602025 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 751AFC54E64 for ; Mon, 25 Mar 2024 12:46:19 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id C367C6B00A5; Mon, 25 Mar 2024 08:46:16 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id BE3866B00A6; Mon, 25 Mar 2024 08:46:16 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A5F5C6B00A7; Mon, 25 Mar 2024 08:46:16 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 90A8D6B00A5 for ; Mon, 25 Mar 2024 08:46:16 -0400 (EDT) Received: from smtpin21.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 64382A06A6 for ; Mon, 25 Mar 2024 12:46:16 +0000 (UTC) X-FDA: 81935534352.21.21D1F6B Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf22.hostedemail.com (Postfix) with ESMTP id A8D70C001F for ; Mon, 25 Mar 2024 12:46:14 +0000 (UTC) Authentication-Results: imf22.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=Dz0MergX; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf22.hostedemail.com: domain of alexs@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=alexs@kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1711370774; 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=G/BHHnxF2+HS53kVpC8L/Km83UHVHT7XhuCqK8Fcp5I=; b=rOV69dfe9YpjkL8g5DW3FoKmblKfsTMiIsnvfBo6ODksIVlLrjOOj6bOgXHhuGYUwSRKpE tjfCZYZI+SceJq/lwn3/4KcquhaQl+nzmfxdR2+GMxpn7mJGIohMnD/kN1WbRL+ZDyZfAu gGll8ZkPRjjzjn9Ja+eDVrWkbX47eTo= ARC-Authentication-Results: i=1; imf22.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=Dz0MergX; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf22.hostedemail.com: domain of alexs@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=alexs@kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1711370774; a=rsa-sha256; cv=none; b=x0TOo/4hkANnR41A+ObPfYYb+aZn7y71YnXiiQx0ug52h1eCQippw5zF7gKyXAoKnWc2F6 URCznBYPKh1qKu6zo1Ccn7gwvVBTLXEOy+QLZZGYqboFT7rUZOqIK8KmMZ/NxNyzDzjwUp wIpWZq+pzQaS+7CX8yjIQDle0ah5yqE= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by dfw.source.kernel.org (Postfix) with ESMTP id DF89660F8B; Mon, 25 Mar 2024 12:46:13 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 40281C43390; Mon, 25 Mar 2024 12:46:11 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1711370773; bh=2lgvFI0wNn9RTCMEzEWib7Gn+j7luBL+Jg2E4ycFUf0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Dz0MergXwbNpE5qypVG9l++oEAh9GgKrjYVi2WZATBEUMf/h2vvKvfWuuN2rYay9Q mvBr0LRQln4jeWSRsqlLU8WM7vzf95ttjzjXBJIPtHyIR8fB+mcbA6L6yjQILNW13E EGLVB0l5Zlthhdi3jucQwfl7boWT3sAKrIJNV9XNKm1YRkYBe9NGv019lOFQyoxQuA 1IBBuXPnLgXvLTeau3NR1///K8RNQnj6GrTVhWQbmN6pYAV+qwXdZ2HD5AKz62NVjT KcjwNL2irL8wTYaKKGnUMzndCBL9R0lq+wEpVRtUACFl77GcxTnbkuJk6hE9dx6Hwk JDbfAotIewksw== 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, ryncsn@gmail.com Cc: "Alex Shi (tencent)" , Hugh Dickins , Chris Wright Subject: [PATCH v3 10/14] mm/ksm: Convert stable_tree_search to use folio Date: Mon, 25 Mar 2024 20:48:57 +0800 Message-ID: <20240325124904.398913-11-alexs@kernel.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240325124904.398913-1-alexs@kernel.org> References: <20240325124904.398913-1-alexs@kernel.org> MIME-Version: 1.0 X-Rspamd-Queue-Id: A8D70C001F X-Rspam-User: X-Rspamd-Server: rspam02 X-Stat-Signature: 73yageyx6kar45ewtb6xaw7qkzcscdcq X-HE-Tag: 1711370774-196901 X-HE-Meta: U2FsdGVkX1+46AS+RXz3B/Rnj+cis8uCeg4KrkIydefEgboJnIPIG7k93EFlHFRRgs/crwi8wCjFmzbr7N78q8D4ET1hNPgI+iEk51atNKWWsFEzpb/2DEv7KhQM9G91jloUFBctJpwl2Y734+v3naaXaLkZmRDkB6Kv+2CPXgmqtZadMjhzTzjJwCKBXNfOD3waQDodzkiCWEfzLqxWcJiRzw7cIRlUzGvCGUGRL8No/yZozqSpwObEu/aJ0udlSZuP/QCAt+dQdwA/XHZveEKOxVj1yIRmw9YmrAfEGPPd1YB/5sE/Fezix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HUBTekRGJx1SjJtOBECoI/jtVUtEFbGFUaNRy6VRtAjxFYq/60q1p2hTxbeuyb8znwJK3jzn/Xm3ZsQRKo+KxH5P96oKS5d 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 c2afe2f926db..e92445f29685 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 */