From patchwork Fri Mar 22 08:36: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: 13599728 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 46728C54E71 for ; Fri, 22 Mar 2024 08:34:39 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id C57296B008A; Fri, 22 Mar 2024 04:34:38 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id BDFCA6B008C; Fri, 22 Mar 2024 04:34:38 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id AA8136B0092; Fri, 22 Mar 2024 04:34:38 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 9881F6B008A for ; Fri, 22 Mar 2024 04:34:38 -0400 (EDT) Received: from smtpin22.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 65A1012112A for ; Fri, 22 Mar 2024 08:34:38 +0000 (UTC) X-FDA: 81924013836.22.2595368 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf24.hostedemail.com (Postfix) with ESMTP id 5520318000D for ; Fri, 22 Mar 2024 08:34:35 +0000 (UTC) Authentication-Results: imf24.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=UAsYbdgL; spf=pass (imf24.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=1711096475; 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=lv4bMyhL95/nxMNEOkGX0YBTLBK3IDnPRoHWqaBZgPFrNkgNuq9AkaCF1FrdYU9d+qBx+W TUMSACIHZtPmeczLpz/jaSlHpHsSi15qsqMpoZ4dbmQ3bU+Y2ojId5PMLyqGoZqJvNJBwf 35UZuo8LWBNvGx00SMmWkKzKijK6OIs= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1711096475; a=rsa-sha256; cv=none; b=zusiaRimATXcepCs/QQ9C5aOPDKRiLEhSinPuNllVEmcM6XR1FppBAwwslcm+uFT1hjOT2 SyhZophd824x/3L1V9xnWxnr/wvfkcGQXCVWvzyj18yuGTwRiIr8JWUXoQqXJNHfhBxx3u GAnOVikfBB7UPmuxZ6OE7o8fyeVJCCc= ARC-Authentication-Results: i=1; imf24.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=UAsYbdgL; spf=pass (imf24.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 7B5DB61333; Fri, 22 Mar 2024 08:34:34 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id E7644C433C7; Fri, 22 Mar 2024 08:34:31 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1711096474; bh=QDhgdKPLSZ+LCN9CDxfi2X7NfAWh6m7wtgm+INCh864=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=UAsYbdgLPm6ColwLuGvdCRPIly5CPWzeKDimqEmd+ahSq/lji8PuZyO5ikptMXNRE 0tN4+9vjCPhdJSaDLadCTU3io0sosPse0dDeI6FKQ2pgfwAemBf0qoA+v3qWYHKnEL 7MznxUdS1LckQw/aHwXlA3ZFmNKMQguN28vs2gLoNP1+0neQAeiUc0mcZ4dwY64YiL 5gypatqFtmUoPs7+xuYnnWR+CdUeeLwrrRtJKVaxpvTom1yRJ8lGxDHp4jBIdacHTo pU3UuLwbIwtPPqIK3iAJwZJ+psijiC4YyAKdZEB5LFMfWXVtIN3kitnpzNMn9SOplM P9GkJonvDhM3g== 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 01/14] mm/ksm: add ksm_get_folio Date: Fri, 22 Mar 2024 16:36:48 +0800 Message-ID: <20240322083703.232364-2-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: 5520318000D X-Rspam-User: X-Stat-Signature: i5mmzahgq5pkdgyo9xyofk9h9r9p8n3m X-Rspamd-Server: rspam03 X-HE-Tag: 1711096475-960796 X-HE-Meta: U2FsdGVkX1/HYQp/s6MUA6pix8cGKd/Ksm9z9vxcZ/2EumS6vDK7f+ZMFIA1GHT9w64OUPYh1DCi30nFElYTIqU8IwNcsxC6ri/ejqSNHLSsB26msjeTxl7sYcmtGRHlF17NHpUH6OxOzZdXtbz7RwXZM51ngc+QjWfSBd/oqWHWFyYt3Jq8HPThska6gI2AFK0kTXA9rxWTQLZ/YLgS/Z5AznwwJUXK8ZGyHR6PueVKZf+LICrCKbUuCvCssz9s6l+EpnD/G+X3wfYg8U9KJa25mBQrBxRVeIoHu6938B+OTBDhOaH8zJtuYfX4GD62rxVyGi+Hx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XM3qXvyDK1LSIZn7kGS5+9J/dRwOpWzHme4IKoZOCYAwxE1sZOMhuB8oy7R7ASPiv11RqwgbcV 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 --- 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.