From patchwork Thu Apr 11 06:17:08 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: 13625445 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 5ED89CD1292 for ; Thu, 11 Apr 2024 06:14:40 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id DE9006B009B; Thu, 11 Apr 2024 02:14:39 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id D72216B009C; Thu, 11 Apr 2024 02:14:39 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C12A56B009D; Thu, 11 Apr 2024 02:14:39 -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 9C0F56B009B for ; Thu, 11 Apr 2024 02:14:39 -0400 (EDT) Received: from smtpin25.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 6A77E140A86 for ; Thu, 11 Apr 2024 06:14:39 +0000 (UTC) X-FDA: 81996237078.25.8034809 Received: from sin.source.kernel.org (sin.source.kernel.org [145.40.73.55]) by imf23.hostedemail.com (Postfix) with ESMTP id 230D5140006 for ; Thu, 11 Apr 2024 06:14:36 +0000 (UTC) Authentication-Results: imf23.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=KC283FNT; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf23.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=1712816077; 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=a8fdVXwZIC2+0jfsc23LnNEgvdTm8MtJqB0W/u7VV/I=; b=3O9nvq68hHk8xg69LHQzGgeyTzSHnboIl34bWCG6wSEsXbnzQdDxvqZh7NBPA+Oy4A4Fuy A23FT3xCDH2Nai5HIFaCIHy+wsII4cRIAQlLAixBxlT/5zu0t/xOu0eZ2tUMiTVgDwccvD iaGmhBR8MsAMQ6WvULmq2hzamFydjNM= ARC-Authentication-Results: i=1; imf23.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=KC283FNT; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf23.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=1712816077; a=rsa-sha256; cv=none; b=mFRBtlwdVt6gaDB3EnAdRiXHzLhhRnI9UO9LJq+GHYOF+GmLV4eUDYAKzGdWAwwFkjkGSP bAaanYC36lf243uQgFI0e9OLWTSC1dr074LGmsrpkcf/KvqtQ1eN3I6jKcFBK8MUVGVmU4 ZDI6p+P9fa3DITWrFNXMB0GiJZl39yU= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by sin.source.kernel.org (Postfix) with ESMTP id 421F6CE2F15; Thu, 11 Apr 2024 06:14:34 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id B169CC43390; Thu, 11 Apr 2024 06:14:31 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1712816073; bh=sFkZO7Zaxh50oOXAu6R13zvDS+luqAu687nTeql3yIQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=KC283FNT4ao2MpxFeaTvbWB0emRIG60QNJYtYHqPfmeIsjOwDnje007my1mGCHbr7 nfRzlHgiEt+8nqSnI2Rzz9caW/QhUDbKMurWAV3KSZVTF7Bu0a7LfeQ+o50k7IS9Tu G5siZcOLRKQm0js4dUzp+np8Rd7N+17FhMh/N+FoMO2LSUZn2uFA0Lqweak+78LTb3 NZjPajid42oiT2MINzFrl9LaG0exQ4Ynawq4rEopwlt/4GHR3kwEg6kImr4HFqwzMm Gq3jQqw9sQkIsGqZmUvKCqzJRkfX+JmRuRT797is7pY5H46bZ+ES7NHm55oNuHWOfq JzVnxlijt2ekw== 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 07/10] mm/ksm: use folio in write_protect_page Date: Thu, 11 Apr 2024 14:17:08 +0800 Message-ID: <20240411061713.1847574-8-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: 230D5140006 X-Rspam-User: X-Rspamd-Server: rspam02 X-Stat-Signature: yjnp68xzxmdc6wwqpgc58mu6xhd5syf8 X-HE-Tag: 1712816076-474828 X-HE-Meta: U2FsdGVkX18/V9Kncb4rsj9VONjZCnF89D56Td2URGGJn33uRgTwtISU/WbuIM/SP3DMchl3c1RlX9oXE6/NQM2y9QTmKVuH/n/Av2Ax3uT3VUEbubAS+p3nan7NCz1jm2YCF476G8++K8P3JpCl0h8WTy/3tRi2KFc0rIvJAthva6xM0Kfq2IYXxkUeTohedr8cKSHx1PHpLH7rQW1z+azJ+jAhX0gogGh9VDcAI2sWW+1/VwB9Zmob3IjN0dUxxdFAJTe8CLk8mFSll+quRm8RApMMgANtI3E4sf5+bt7gB2vv62y3UsORzjCXJ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whsoQ4PwN15aw4y+rGO0im83mX2nf3Kzk9TtagjG1WpgKsTIMd4SBUgmpTogs5dJOjpCI15YtMJvPVdK0nkKUAzW1KUY2ePPnhOiLsenJj8 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. 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 | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/mm/ksm.c b/mm/ksm.c index b127d39c9af0..2fdd6586a3a7 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