From patchwork Tue Apr 9 09:28:16 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: 13622060 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 EB162CD1284 for ; Tue, 9 Apr 2024 09:25:23 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 79B776B008C; Tue, 9 Apr 2024 05:25:23 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 721246B0092; Tue, 9 Apr 2024 05:25:23 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 4FF8B6B0093; Tue, 9 Apr 2024 05:25:23 -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 209146B008C for ; Tue, 9 Apr 2024 05:25:23 -0400 (EDT) Received: from smtpin10.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id D4491802BF for ; Tue, 9 Apr 2024 09:25:22 +0000 (UTC) X-FDA: 81989460084.10.3B8F2BA Received: from sin.source.kernel.org (sin.source.kernel.org [145.40.73.55]) by imf24.hostedemail.com (Postfix) with ESMTP id 89E9118001F for ; Tue, 9 Apr 2024 09:25:20 +0000 (UTC) Authentication-Results: imf24.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=DRFcqFN9; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf24.hostedemail.com: domain of alexs@kernel.org designates 145.40.73.55 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=1712654721; 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=qZVGZwY9DtxMHMolatICyCd8jVx09nsfE645jS/598A=; b=yOsXliOE+z7NNdpmPhLKdEY6VsYkUI3kDU0Rif0RiVLghGHdMM7DZ9zEDzczePE+CgLz9N OYUzccKAnvzH62YvMhOdvX7AXQajy8tQwhGbXD7RERiVcdy5AHAWoJOBockApH2LW52+rn +3s1WrL+b5NxKGAvzddltO9uTinQThI= ARC-Authentication-Results: i=1; imf24.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=DRFcqFN9; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf24.hostedemail.com: domain of alexs@kernel.org designates 145.40.73.55 as permitted sender) smtp.mailfrom=alexs@kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1712654721; a=rsa-sha256; cv=none; b=YFSXmjQ9MILSrvqvfr3Kc/UjBFWcULLRb3gpsS0gpb5LAhinLgrzY/I9BqauR+y8z2ubYV msNZi7iEpUseyV9HbYPqDukpPrNcY+9Lpz1AOPnVMSDXd55g+2etQFNeulChufXXs0aDb6 CP8eqozoeXKI9cMlCafXFWx2Datgn+c= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by sin.source.kernel.org (Postfix) with ESMTP id CCCF8CE1DF4; Tue, 9 Apr 2024 09:25:17 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id F0EFEC43390; Tue, 9 Apr 2024 09:25:14 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1712654717; bh=jy6PhQURlzWnemsEhuISNb873RtM8zjTy52Z9fJnaQo=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=DRFcqFN9/XtdCn1Sbvo2Ddn+Ka5gBb/M2Y/Q0SD9k4n7tUVbGSJveLmsl9M8J9cDy 20+EmBJsTN2SUT/vt59KF9BxtksTeVWOSZs3E6BIXoMeScBM1ZfqUx8Fq783/yp8m2 u2oEPEGPPrgCI+XsJXITtUUH8VIM0MlCkXPcy/2EKPzxthn2kzLzMcE5U/a8+L1EYa 91TJ5ltdVOR3EDcHvQdeGI9BJJ5T96tgJZTnePSgKHwFYMpzrqD4vZmzICwhNWKQZk liyNGwApsCciLgWPQ6zoLlKRFvNaQytldVXlzCIsJC8KqkYZJEqkChrbG38FjG9//8 /dOweJU2fevgQ== From: alexs@kernel.org To: Andrew Morton , linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: "Alex Shi (tencent)" , Izik Eidus , Matthew Wilcox , Andrea Arcangeli , Hugh Dickins , Chris Wright , David Hildenbrand Subject: [PATCH v4 1/9] mm/ksm: add ksm_get_folio Date: Tue, 9 Apr 2024 17:28:16 +0800 Message-ID: <20240409092826.1733637-2-alexs@kernel.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240409092826.1733637-1-alexs@kernel.org> References: <20240409092826.1733637-1-alexs@kernel.org> MIME-Version: 1.0 X-Rspamd-Server: rspam09 X-Rspamd-Queue-Id: 89E9118001F X-Stat-Signature: 5t6drey9ubd6ntmwme9q1to7juj3fmt4 X-Rspam-User: X-HE-Tag: 1712654720-16501 X-HE-Meta: U2FsdGVkX1+VBEwuXR7J6T8yXVD6Tcpk1H2bUHvJQAyvO4GhxFBad7sYoiSayEJEqD3ovUvbaWPv80S2RAyRY8seDmx1tp5IR/9R0Aya0JwhEyYCjbkNhkjoalgkg8m67E/u1NdEQvtyHdtyHvVxJ17KxPV8omuo+l3uEE9Op8FFOMv/8JT6K9CeyfIbIa3vg1l80/VHvLAcHbsYAQVrflHtVsRgSZM5jlRzJCOikIG9yVWbHLFehdFtauk9iOp58aQtWBYFOYh9i0Vi7DmLnKa2WkwbKK9XaqinkORadr/HSAww2c5a6J6/qlDHm3HDUWix0ZcFX7zSC/PD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VHhacufy/ExC9wKHUysi28v/a+MnrqYGZpC+kay8fde/Qpb634SLfR1JRC6zn0UR72KvETCZJVUEccc/Oo1Ybt5+ZzzQiN1OIv1oHjYLFe/GJuiS+c5v2vEoqzPuycX1wJn76Fz+194duFFn5xAoV0w== 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) To: Andrew Morton 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.