From patchwork Thu Apr 11 06:17:02 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: 13625439 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 C9ED4CD1292 for ; Thu, 11 Apr 2024 06:14:24 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 228266B0087; Thu, 11 Apr 2024 02:14:24 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 1D7D36B0088; Thu, 11 Apr 2024 02:14:24 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 09F966B0089; Thu, 11 Apr 2024 02:14:24 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id DEE7F6B0087 for ; Thu, 11 Apr 2024 02:14:23 -0400 (EDT) Received: from smtpin29.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 783F6140A7B for ; Thu, 11 Apr 2024 06:14:23 +0000 (UTC) X-FDA: 81996236406.29.7FB7F06 Received: from sin.source.kernel.org (sin.source.kernel.org [145.40.73.55]) by imf21.hostedemail.com (Postfix) with ESMTP id 380BF1C0005 for ; Thu, 11 Apr 2024 06:14:20 +0000 (UTC) Authentication-Results: imf21.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=XgJlk6qU; spf=pass (imf21.hostedemail.com: domain of alexs@kernel.org designates 145.40.73.55 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=1712816061; 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=ziOYdijR/WepKC3J3licszNW8MOaRGCBasaGIcou9Eg=; b=SOLbGJpVTAG/RDajrDt7B6N5DjZqj9kK2OAMNTzZawkYxoCFEzarOVMIbmqAObnBZqnC1o 2Ih82OAVF6ie3BEBdelzGTpAuniMbrT6UPdCavk/KccGx8TpJs2zA3wLA9AblSVQdYjkfC rgYoH6M3fo7IPKII6jXXeMfjhUM+IcE= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1712816061; a=rsa-sha256; cv=none; b=UrHT9J9D31jWACvfndLZTY3UsqzXIPkOjHszVIFQr+qIshIOPM+u7V6NHZm7yh9uBjFO2T jPo3Zvl7nH9OYF3f0mRLct5RBVYfkLfwRG0rMS4y6U+2dZ2wvu2AevfegoWEGY0/hBSIe8 B8yClsaqTZZVrj2saxJJN/fH5vjsVcc= ARC-Authentication-Results: i=1; imf21.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=XgJlk6qU; spf=pass (imf21.hostedemail.com: domain of alexs@kernel.org designates 145.40.73.55 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 sin.source.kernel.org (Postfix) with ESMTP id 97978CE2F04; Thu, 11 Apr 2024 06:14:17 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id E5A9EC433C7; Thu, 11 Apr 2024 06:14:14 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1712816057; bh=oezv8n190iRAYJbqG1rSb+HEO3aakytr9BMxPO0jqOc=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=XgJlk6qUo71If1Hxiz9eQsY0pFcpm4jNK7/rtAzAt/q4vCYQU03Bl1aZ5ZiY8T50U 3SNtQ75PjlVB+Y0VNKaGnr0UqLF1Phb9fwrNgXo18gnLvsJT7FChrz2OuTpUa9k6+A 7CLGzC5F3kZwMwngHWwW42Ddk91bRjqfI4zoGb//eWcMRxIqkPPFrsOWo8vv28pzB9 k5BOFJGgkRmvIxtiA3fPAT1wRfDn8OJXuOrXGutaJNGVLD6BdVs4HxQmCaTC15xVPX PyauH5xarZ2NzpCIuJlDGmEQXiWk3VzrrCtxAlc/AQ7XFAIZNY/yS3T5v/PSPTCZOl Ld+oDpm/7WNhg== From: alexs@kernel.org To: Andrew Morton , linux-mm@kvack.org, linux-kernel@vger.kernel.org, zik.eidus@ravellosystems.com, willy@infradead.org, aarcange@redhat.com, hughd@google.com, chrisw@sous-sol.org, david@redhat.com Cc: "Alex Shi (tencent)" , Izik Eidus Subject: [PATCH v5 01/10] mm/ksm: add ksm_get_folio Date: Thu, 11 Apr 2024 14:17:02 +0800 Message-ID: <20240411061713.1847574-2-alexs@kernel.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240411061713.1847574-1-alexs@kernel.org> References: <20240411061713.1847574-1-alexs@kernel.org> MIME-Version: 1.0 X-Rspamd-Queue-Id: 380BF1C0005 X-Rspam-User: X-Rspamd-Server: rspam11 X-Stat-Signature: xccqgif5dytgj8anjz6dxdtg59a7dae6 X-HE-Tag: 1712816060-374621 X-HE-Meta: U2FsdGVkX194BqqU2fjD9KxPdUP/N2PUepTDFQ+iqVnbQPGgUyJDR8bMcoxulqHoaYLmn1Jv9kpJxwEwUlKb2f79Z1SsYEKIZJtPdhLt3Jt51r4z4UyGCAGFGimZ9r+TJmgK4Vl6OIRBSwvzK0NF3TcYu0ddGYPBipgQd8jcIgCUiH3DSLDqXAVCKX6cC0VA3KlD7mwSxyIZMDg+plExzU/bbCmDDcm2OIjMurPuA7i2K40Bi0fcuyoDoIRfBkasA7eV+n1pY/wQgL5CpKm6U8N5uR9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b8/WNpTHY7y5zC+ZoscPiZIxVo36ibvByoPvb+qTulVk/MB10Chxwk+96GN7EeWni6YfpQF/63A57jftDEouY+piPE4tROU2301nwMOTC2IKfHg+dIQCvGWN3tumZkG0 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. Cc: Izik Eidus Cc: Matthew Wilcox Cc: Andrea Arcangeli Cc: Hugh Dickins Cc: Chris Wright Signed-off-by: Alex Shi (tencent) Reviewed-by: David Hildenbrand --- mm/ksm.c | 42 +++++++++++++++++++++++++----------------- 1 file changed, 25 insertions(+), 17 deletions(-) diff --git a/mm/ksm.c b/mm/ksm.c index 8c001819cf10..ac126a4c245c 100644 --- a/mm/ksm.c +++ b/mm/ksm.c @@ -897,7 +897,7 @@ enum get_ksm_page_flags { }; /* - * get_ksm_page: checks if the page indicated by the stable node + * ksm_get_folio: checks if the page indicated by the stable node * is still its ksm page, despite having held no reference to it. * In which case we can trust the content of the page, and it * returns the gotten page; but if the page has now been zapped, @@ -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.