From patchwork Mon Mar 25 12:48:48 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: 13602016 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 1ED4FC54E64 for ; Mon, 25 Mar 2024 12:45:51 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id A37706B0095; Mon, 25 Mar 2024 08:45:50 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 9E8C86B0096; Mon, 25 Mar 2024 08:45:50 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 888916B0098; Mon, 25 Mar 2024 08:45:50 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 763B66B0095 for ; Mon, 25 Mar 2024 08:45:50 -0400 (EDT) Received: from smtpin25.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 2824C160173 for ; Mon, 25 Mar 2024 12:45:50 +0000 (UTC) X-FDA: 81935533260.25.EBB6692 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf04.hostedemail.com (Postfix) with ESMTP id 9DB7040010 for ; Mon, 25 Mar 2024 12:45:48 +0000 (UTC) Authentication-Results: imf04.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=nqD8rAwq; spf=pass (imf04.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=1711370748; 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=j5O9mkfNcbBoqVBmXr9pW1yRb70AyJM+UXLoLRkPGFM=; b=322wKu9XdyiV3wk/jqkrIxoz/icHhIRP3WNHv4KksOD2uy+r1RVj59qbpNQOyiP4B/6XUi 6LnFlnODR4LQUxTkxTn/pyfRIzlt2OvpaMo0bZrufmSj4RehUhcy+vilBjeZH8WeLJMWI8 /N5QaQE635O+Cz98rPd0UIyF9WSIK0w= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1711370748; a=rsa-sha256; cv=none; b=ifjqSEXqrdPhgDAkFp2lv68560QpDJ/fi6U6wGfPMuHCOPiib/GkmBzK6xlnwr8H74CFDf c16F8WVBz/jBW52gVCeAXDmPJ4VKJTWCXmiezmuYxl/1T2vLg9S1wHKy1B7ryGnmTFAHh8 HVEh5SZU3DBW9o5gQ9c23aUFBILz79c= ARC-Authentication-Results: i=1; imf04.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=nqD8rAwq; spf=pass (imf04.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 E24CD60FA2; Mon, 25 Mar 2024 12:45:47 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 4269BC43394; Mon, 25 Mar 2024 12:45:45 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1711370747; bh=QDhgdKPLSZ+LCN9CDxfi2X7NfAWh6m7wtgm+INCh864=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=nqD8rAwqqd1rZlggHYvDueBQdTe6WYxAdUuXriWjNgenrTHHVxgd7GnHtwUzXPlhH Euyl3J3vJqwRgkj3fMPW6SSRO52ygC73Q37XEga85/ltaHFKcyi6/C2rE8khfLB5PY bxE3SiDge/MfBeeZCQnSRaw+A7+4HaJJVcT/0uxumAh0+nAx5acDnUKKC4zVEo1HSD 9RBSBXjM3odN+trHUYLuYZ2raubRH4AfZKu7VHIhfapDctVyDJdBckiKsEpDWGh90q aPKP7sDOtjUpNAHzVOTGKEQnQnT3FK+ONVRVirrIwynhumCYaAlKkfN748A6RM0AbW pKbYUkce4rmmg== 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 01/14] mm/ksm: add ksm_get_folio Date: Mon, 25 Mar 2024 20:48:48 +0800 Message-ID: <20240325124904.398913-2-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: 9DB7040010 X-Rspam-User: X-Rspamd-Server: rspam11 X-Stat-Signature: gi63ir7wtfk45ufmnf4pf9ens7jn5wtm X-HE-Tag: 1711370748-20133 X-HE-Meta: U2FsdGVkX1/aHASeTRb+r6LwD1xYXaqu7p4TPrr8Mw6YBhxZ2EudFYjm6L+VGSjo5SBQvFETHz8gyEoZ5u69aqqwvW0myZ/ixHfUbZtP7d+hNEqoJntGJGq3rlPB53jH2zf8/gYNL52z1R20ipAcspRDzeyGowzwYDRTf/8926uSUYrp6kIj+bGoyq92f/U+vuK4fdcCkmVtV6AhM8puKVsjzoMsvP5E66PmLv5iwvbzqKC8r1UmPxSClPx+40yXkBxhQSdDMZZq59bNAG9knksDmjW3jDwN4mXj0zL1TraPfRKQs0C3qF+dTx9b0EMBgnEA2Ty8EFVyFgcjEY0lzYLTVecpwDnxqYkpg21GjnOfIK550xDVOammuOe8uYPUdkdB3eq2wC/Lg3Cx/Z3uc+QjbL6ZSbns7SOn2CtcX5W+322jrB8vK5RDmB1mhsb3h34vtKeYxLP3w4GMOIn5xxHxvobFLqK/kpzy63IlkhBT7HvarnxuiMaqIbjiUJPIEmxnEpH0nCRzxSIoUFTljJzU8siLYYQ0Wej3CPml2CIHSjtNHjcmuuKHAKGJCoFK53VH1pKOM0EEE7eT8XmkDYronuoO21G/MqkXASS3XHqr8K2vxtACRc7RiJtkZ0l+5ooAXit1LbibkXMY6FssczhwbiM+n1F0RLxFh64XnvzN2MWuW6C9cDcIjGnpJ6ugeJ3wDptoM2t2Ucm6BU579Pl82LSMWAQrB2KCZ3s1E37yiJBllwWgELtnfaTI//V5+P54N1hA4HJlUsTFLx7RkHGPtTGZUqyQoljYYrzZBudig+xQtq4oa3OFuGHbQs5yd3RvdOgcPOUTmMbCwWZRv9yb8yAee2IUTtPsm9GryZA+illOKsnNDCnyMvAurC+RLYrOOw53Sb8cY5umrMWkwiZWKs8KoXYbt3nwXoK+7Qc8ofWC1sTFYgKk3MdWpaW2fWAVXVJiqsIZPswf588 4JImGGmB ZCIh7 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)" The ksm only contains single pages, so we could add a new func ksm_get_folio for get_ksm_page to use folio instead of pages to save a couple of compound_head calls. After all caller replaced, get_ksm_page will be removed. Signed-off-by: Alex Shi (tencent) Cc: Izik Eidus Cc: Matthew Wilcox Cc: Andrea Arcangeli Cc: Hugh Dickins Cc: Chris Wright Reviewed-by: David Hildenbrand --- mm/ksm.c | 40 ++++++++++++++++++++++++---------------- 1 file changed, 24 insertions(+), 16 deletions(-) diff --git a/mm/ksm.c b/mm/ksm.c index 8c001819cf10..ac080235b002 100644 --- a/mm/ksm.c +++ b/mm/ksm.c @@ -915,10 +915,10 @@ enum get_ksm_page_flags { * a page to put something that might look like our key in page->mapping. * is on its way to being freed; but it is an anomaly to bear in mind. */ -static struct page *get_ksm_page(struct ksm_stable_node *stable_node, +static struct folio *ksm_get_folio(struct ksm_stable_node *stable_node, enum get_ksm_page_flags flags) { - struct page *page; + struct folio *folio; void *expected_mapping; unsigned long kpfn; @@ -926,8 +926,8 @@ static struct page *get_ksm_page(struct ksm_stable_node *stable_node, PAGE_MAPPING_KSM); again: kpfn = READ_ONCE(stable_node->kpfn); /* Address dependency. */ - page = pfn_to_page(kpfn); - if (READ_ONCE(page->mapping) != expected_mapping) + folio = pfn_folio(kpfn); + if (READ_ONCE(folio->mapping) != expected_mapping) goto stale; /* @@ -940,41 +940,41 @@ static struct page *get_ksm_page(struct ksm_stable_node *stable_node, * in folio_migrate_mapping(), it might still be our page, * in which case it's essential to keep the node. */ - while (!get_page_unless_zero(page)) { + while (!folio_try_get(folio)) { /* * Another check for page->mapping != expected_mapping would * work here too. We have chosen the !PageSwapCache test to * optimize the common case, when the page is or is about to * be freed: PageSwapCache is cleared (under spin_lock_irq) * in the ref_freeze section of __remove_mapping(); but Anon - * page->mapping reset to NULL later, in free_pages_prepare(). + * folio->mapping reset to NULL later, in free_pages_prepare(). */ - if (!PageSwapCache(page)) + if (!folio_test_swapcache(folio)) goto stale; cpu_relax(); } - if (READ_ONCE(page->mapping) != expected_mapping) { - put_page(page); + if (READ_ONCE(folio->mapping) != expected_mapping) { + folio_put(folio); goto stale; } if (flags == GET_KSM_PAGE_TRYLOCK) { - if (!trylock_page(page)) { - put_page(page); + if (!folio_trylock(folio)) { + folio_put(folio); return ERR_PTR(-EBUSY); } } else if (flags == GET_KSM_PAGE_LOCK) - lock_page(page); + folio_lock(folio); if (flags != GET_KSM_PAGE_NOLOCK) { - if (READ_ONCE(page->mapping) != expected_mapping) { - unlock_page(page); - put_page(page); + if (READ_ONCE(folio->mapping) != expected_mapping) { + folio_unlock(folio); + folio_put(folio); goto stale; } } - return page; + return folio; stale: /* @@ -990,6 +990,14 @@ static struct page *get_ksm_page(struct ksm_stable_node *stable_node, return NULL; } +static struct page *get_ksm_page(struct ksm_stable_node *stable_node, + enum get_ksm_page_flags flags) +{ + struct folio *folio = ksm_get_folio(stable_node, flags); + + return &folio->page; +} + /* * Removing rmap_item from stable or unstable tree. * This function will clean the information from the stable/unstable tree.