From patchwork Wed Mar 20 07:40:42 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: 13597420 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 2E28DC54E67 for ; Wed, 20 Mar 2024 07:38:02 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id A9F7E6B009D; Wed, 20 Mar 2024 03:38:01 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id A27136B009E; Wed, 20 Mar 2024 03:38:01 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 8A18C6B009F; Wed, 20 Mar 2024 03:38:01 -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 6F20D6B009D for ; Wed, 20 Mar 2024 03:38:01 -0400 (EDT) Received: from smtpin03.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 475A1120351 for ; Wed, 20 Mar 2024 07:38:01 +0000 (UTC) X-FDA: 81916613562.03.ABEB6C0 Received: from sin.source.kernel.org (sin.source.kernel.org [145.40.73.55]) by imf11.hostedemail.com (Postfix) with ESMTP id 3EE8840015 for ; Wed, 20 Mar 2024 07:37:58 +0000 (UTC) Authentication-Results: imf11.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=kDzTtQlW; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf11.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=1710920279; 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=6OGvgHX/9Iv74u35Uk/YlQ0Iz4C2uQ3pGHId60/YHtU=; b=ugeDx/1zXaoHDr8BEBtHcr8HOcsxMVo/TRHYpMwnTYzhEOkuJ8+XY1ZIIx5YVH22qiDt2m cyt5UC0jyZi4OlBk92ibSNqbtiBSt86nIIpHoaMztqcSCB06cGc+qT8ys0wsn+YUCdLipg D6aPrwCT/Lgwiw8xcSiwMKpbnvDMpqc= ARC-Authentication-Results: i=1; imf11.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=kDzTtQlW; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf11.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=1710920279; a=rsa-sha256; cv=none; b=OY5+qWA7bJHc2TI02LrUe/Hgjy1eo2HIwpyprjg3zoMkbeTVDDTyiKpbl8wza0f5Cosz4N ZnbPLBLfCl+NBm/8kPCsIsauxWWOw8ffXjPr+HW36jDa1exfUxO1/dmBoqyDjf0FfAY0tI IeNpckbGv/9LyJbguSopUUFF7ZnK/a4= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by sin.source.kernel.org (Postfix) with ESMTP id 59803CE10AF; Wed, 20 Mar 2024 07:37:56 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 778E8C433F1; Wed, 20 Mar 2024 07:37:53 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1710920276; bh=19yPEARCpp8Ts03v7RxMxCBtzle77h5p1XW4PvUaw5E=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=kDzTtQlW47rs4jpnYM++UgOMgMjc6JVlGxhM/IaBtklgkLeG6r4Tnxl1Xk+XlC8O/ KQHON/2ylQa9Yuyx0B8AKyEW5/TRrTxN4xBejiHEVqucOuTU7IwSba7JJ+kXWreOOF R4+4Zyn9/Nev/5bFVVQ4UkccMEbHgMbDM6uTF739A2wUe83+/7uDOexjTk7NkSdnO7 dcnb3ZAD1eZm7Vkx6yt8emCSnB7fcs+Nq9lIWGKaeEwRTnfw1B+r3bex6R9HSkNFbI s/2hdV/dzXTTLvLx7rtB70nOF7m9hfqY35R7h5eY35GCu8RyZf6Qj9ygfbX6Qednoz MHs21FcRRCPOQ== From: alexs@kernel.org To: Izik Eidus , Matthew Wilcox , Andrea Arcangeli , Hugh Dickins , Chris Wright , kasong@tencent.com, Andrew Morton , linux-mm@kvack.org (open list:MEMORY MANAGEMENT), linux-kernel@vger.kernel.org (open list) Cc: linux-kernel@vger.kernel.org, "Alex Shi (tencent)" Subject: [PATCH 06/11] mm/ksm: use folio in write_protect_page Date: Wed, 20 Mar 2024 15:40:42 +0800 Message-ID: <20240320074049.4130552-7-alexs@kernel.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240320074049.4130552-1-alexs@kernel.org> References: <20240320074049.4130552-1-alexs@kernel.org> MIME-Version: 1.0 X-Rspam-User: X-Stat-Signature: tj7wm96rend9m397jhm7sas4cc4qzzxy X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: 3EE8840015 X-HE-Tag: 1710920278-117759 X-HE-Meta: U2FsdGVkX1/qpd9w9t+pD8vQUkU19wugoqRMy7b28JsMXDalOW2xechDlriYYGQHdI4v15EdYa6bJJ6GvV3lvvXmkfS1q8VctxYomUuwuip9ltnECiSOdSrvCRiWK8/LHDgqq2nzqnvmitdSVile9wTglade28q5sJhsUv2+qE8690Rh/MCeySgZ9nNNcX/df0uHlbHNXqhFJYaRGZQ8MN/h1P3bfcPJ+YcfFMzQZe2Rin4wa9PzJi0NoUAOkqDgLwIOQ1ipwQ7ig3B37dvT47d3nTJJRM4EjVm2DcGoo8+DyLrtaKL52KMynq4ecGKLZpuKMjZ0LELTrzUVIjc2v4IKFM0B7Nl8CUeSq1MC1RsLs33WWEbU3Rv+rLTz65KZxAOC2pcNURb3+Xkh5W1gkxtwaP7fjJbspSGrdC777mz6e2bUVkpk9yrgWwoNW/Ztxdp539O2ziZsgKEo9M6lfK9fMsIBWVfTSql8c9t4ylKpzuWCskR3N8jX/NQbeDSleklOxg3PEPtZsqDpfyszgsV0qe1tn3Dv+7dWgzislFE1tdrPev+HOis8TEz2vt1URJUC217BSdDwcoHqUWrKmHCD26Gyc2uWpsq7oA6/Lv5/xjvwqVbu6+Ti2F3BW88v2aUaYtU8JC2t4G+KbgOc/w+oDFGmniJzB6dSAhLCshB/BdxA+dl2cq70z6TBjRbU63eSiJ4TRaX/irUwAfr0k8CK9a0KAnRrAqvT5BTtdjMyWMDYcu1DXM1RwKy8S/WOu6/0hk8ekHBlPEv60h5y/OTVghMJXZFxC9TY9taNVhwPw2rZm1x5b4+10cID6mA6YQcIRYNlETAu9EF2SvKdFBlFq+tiP2A9rC7M1CC6thmR1f1pM7njCQkM5N5aUEL7MS2VbAt7ScY1vtueQSqFpsDgrg6F2Zbsj7WQZKvsq2JNjtzIDOY4RvsyWbcz7rxmJObYk4JXm83bsLlVIyC 28PH0VOW AN0m9 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)" Compound page is checked and skipped before write_protect_page() called, use folio to save few compound_head checking and also remove duplicated compound checking again in the func. Signed-off-by: Alex Shi (tencent) Cc: Izik Eidus Cc: Matthew Wilcox Cc: Andrea Arcangeli Cc: Hugh Dickins Cc: Chris Wright --- mm/ksm.c | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/mm/ksm.c b/mm/ksm.c index 165a3e4162bf..ad3a0294a2ec 100644 --- a/mm/ksm.c +++ b/mm/ksm.c @@ -1275,23 +1275,21 @@ static u32 calc_checksum(struct page *page) return checksum; } -static int write_protect_page(struct vm_area_struct *vma, struct page *page, +static int write_protect_page(struct vm_area_struct *vma, struct folio *folio, pte_t *orig_pte) { struct mm_struct *mm = vma->vm_mm; - DEFINE_PAGE_VMA_WALK(pvmw, page, vma, 0, 0); + DEFINE_PAGE_VMA_WALK(pvmw, &folio->page, vma, 0, 0); int swapped; int err = -EFAULT; struct mmu_notifier_range range; bool anon_exclusive; pte_t entry; - pvmw.address = page_address_in_vma(page, vma); + pvmw.address = page_address_in_vma(&folio->page, vma); if (pvmw.address == -EFAULT) goto out; - BUG_ON(PageTransCompound(page)); - mmu_notifier_range_init(&range, MMU_NOTIFY_CLEAR, 0, mm, pvmw.address, pvmw.address + PAGE_SIZE); mmu_notifier_invalidate_range_start(&range); @@ -1301,12 +1299,12 @@ static int write_protect_page(struct vm_area_struct *vma, struct page *page, if (WARN_ONCE(!pvmw.pte, "Unexpected PMD mapping?")) goto out_unlock; - anon_exclusive = PageAnonExclusive(page); + anon_exclusive = PageAnonExclusive(&folio->page); entry = ptep_get(pvmw.pte); if (pte_write(entry) || pte_dirty(entry) || anon_exclusive || mm_tlb_flush_pending(mm)) { - swapped = PageSwapCache(page); - flush_cache_page(vma, pvmw.address, page_to_pfn(page)); + swapped = folio_test_swapcache(folio); + flush_cache_page(vma, pvmw.address, folio_pfn(folio)); /* * Ok this is tricky, when get_user_pages_fast() run it doesn't * take any lock, therefore the check that we are going to make @@ -1326,20 +1324,20 @@ static int write_protect_page(struct vm_area_struct *vma, struct page *page, * Check that no O_DIRECT or similar I/O is in progress on the * page */ - if (page_mapcount(page) + 1 + swapped != page_count(page)) { + if (folio_mapcount(folio) + 1 + swapped != folio_ref_count(folio)) { set_pte_at(mm, pvmw.address, pvmw.pte, entry); goto out_unlock; } /* See folio_try_share_anon_rmap_pte(): clear PTE first. */ if (anon_exclusive && - folio_try_share_anon_rmap_pte(page_folio(page), page)) { + folio_try_share_anon_rmap_pte(folio, &folio->page)) { set_pte_at(mm, pvmw.address, pvmw.pte, entry); goto out_unlock; } if (pte_dirty(entry)) - set_page_dirty(page); + folio_mark_dirty(folio); entry = pte_mkclean(entry); if (pte_write(entry)) @@ -1505,7 +1503,7 @@ static int try_to_merge_one_page(struct vm_area_struct *vma, * ptes are necessarily already write-protected. But in either * case, we need to lock and check page_count is not raised. */ - if (write_protect_page(vma, page, &orig_pte) == 0) { + if (write_protect_page(vma, (struct folio *)page, &orig_pte) == 0) { if (!kpage) { /* * While we hold page lock, upgrade page from