From patchwork Tue Apr 9 09:28:22 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: 13622066 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 2BAECCD1297 for ; Tue, 9 Apr 2024 09:25:39 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 725556B009E; Tue, 9 Apr 2024 05:25:36 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 637F46B009F; Tue, 9 Apr 2024 05:25:36 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 4B1696B00A0; Tue, 9 Apr 2024 05:25:36 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 217E96B009E for ; Tue, 9 Apr 2024 05:25:36 -0400 (EDT) Received: from smtpin11.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id E3A2080328 for ; Tue, 9 Apr 2024 09:25:35 +0000 (UTC) X-FDA: 81989460630.11.BB9A00F Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf14.hostedemail.com (Postfix) with ESMTP id 4394A100002 for ; Tue, 9 Apr 2024 09:25:34 +0000 (UTC) Authentication-Results: imf14.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=eXbjGhv4; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf14.hostedemail.com: domain of alexs@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=alexs@kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1712654734; a=rsa-sha256; cv=none; b=SsKPbelV71sycoIQ9j0V+MhHru4tvFSSqwu1bymye5Z7fLW3LJkQ4BwMwD3E701qy89s4Y 8FfCVCl0h2BgqD34fZ+WZLLggcVCqcmVmVrYnc6oaMjP6yClogWgobPbfibm5UQpq4YSp4 0PCAjPDlwppOds0vsY0cyWM7Ek6/LlM= ARC-Authentication-Results: i=1; imf14.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=eXbjGhv4; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf14.hostedemail.com: domain of alexs@kernel.org designates 139.178.84.217 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=1712654734; 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=mxoFzUrGhXaDOktVgy7CK9L3BNl27BfH9gualQu8PvY=; b=1jUE0rY/Catj5PPUGjtJsKSwSw/mGJ63I0US6NoWLFLeQXqs7vcfytr0JDwQeS/Ekz/dXK +1vgehd2IADogm+KieM3o2EX9hJX4Tl9MQHS3VR5tgV+fBzKj7WBDSFsa+WgCnKCmw5vyt aLQErWxAAuhPCuJPY/Lhb+Jr+EM/G9w= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by dfw.source.kernel.org (Postfix) with ESMTP id 8A38061655; Tue, 9 Apr 2024 09:25:33 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 4145FC433F1; Tue, 9 Apr 2024 09:25:31 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1712654733; bh=kQN6FVtHG3hCQ1muL4e6/z5sNInnsnuhNQpT8qXQDKE=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=eXbjGhv4j5QR/j+cdEHV+7W1koJXqWz0Rb1a9u1mejXGAL6dCIqJ9poZ+7mJMyPUQ MehaKYskrhsNlDJYo7S+wZ7vfMjpdOoT6Pq4ShQfZjkvD0QIDex94hl7XsYYf2E9YJ j57kxizvU1otNbZOiGJrxz3y4Q3QnaD794ZR/3Jn8aGO/lzghT3lAFBA1h8WHB26mj VZFsfFvXHGVlVtDCsaupYUkBkOCzGZwUDifeCCNVvwdKV/sRVIA8vQZsnCorU89iyF u9xuYV0nQrm7N4XH8chMU/kdGbvnuUZCWJxqY/AqNRoCE5Rn/gVb0ViQXJgFTgAhdF 6x1JKxmdZ52rA== 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 7/9] mm/ksm: use folio in write_protect_page Date: Tue, 9 Apr 2024 17:28:22 +0800 Message-ID: <20240409092826.1733637-8-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-Rspam-User: X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: 4394A100002 X-Stat-Signature: dgcpufmuqdnbpj1prh6pgui8na7t1xoq X-HE-Tag: 1712654734-288319 X-HE-Meta: U2FsdGVkX1/N9Bnxer9Zx7wGQ7L7eAw3fJ61vt3osgEGPvdSImqaj8o7BeMYjSNXwi+HfmlHRmucYHcXZNw7vBiT0OPiM6WtStXkg3lWPdI23tEpdcuH0RB8AOWBpFMGhAJW+T94CxGJoXmzUBAtFIE1fLQE+xua5sAkvsToQcJPcrxRxWduE6zct2nrEYMlB1aedWdqfkKVqdbv8jFLJ8gUepx0xJf0wiQ5BsgplVVqUpJnjbm0F6I3OW0/3ngFVhNN081eGGRru4kZ1BGVNSsOVZvABFO9HgbqI5OSIveQhl2QMfTcnj5J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il+H+0D27vRQb0beWvLR4NkF3HdpJxB51qoPE/twK3vh+6dSgU83ZyvJVAdD4hchgXO1digwLPLwZXtDvLFWc+WAFB70VuYti5bmvjk5SnMIkvm/mu 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 a few compound_head checking. 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 | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/mm/ksm.c b/mm/ksm.c index 95a487a21eed..776094fab34e 100644 --- a/mm/ksm.c +++ b/mm/ksm.c @@ -1289,23 +1289,24 @@ 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_FOLIO_VMA_WALK(pvmw, folio, 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); + if (WARN_ON_ONCE(folio_test_large(folio))) + return err; + + 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); @@ -1315,12 +1316,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 @@ -1340,20 +1341,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)) @@ -1519,7 +1520,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, page_folio(page), &orig_pte) == 0) { if (!kpage) { /* * While we hold page lock, upgrade page from