From patchwork Wed Feb 1 08:17:36 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yin Fengwei X-Patchwork-Id: 13123912 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 203B1C05027 for ; Wed, 1 Feb 2023 08:16:02 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 918956B007D; Wed, 1 Feb 2023 03:16:00 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 8A3BF6B007E; Wed, 1 Feb 2023 03:16:00 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 60B276B0080; Wed, 1 Feb 2023 03:16:00 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 2D1156B007E for ; Wed, 1 Feb 2023 03:16:00 -0500 (EST) Received: from smtpin26.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 0D1FD120D6A for ; Wed, 1 Feb 2023 08:16:00 +0000 (UTC) X-FDA: 80418014880.26.CE03422 Received: from mga06.intel.com (mga06b.intel.com [134.134.136.31]) by imf17.hostedemail.com (Postfix) with ESMTP id 07D984001F for ; Wed, 1 Feb 2023 08:15:57 +0000 (UTC) Authentication-Results: imf17.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=ZBq1DstY; spf=pass (imf17.hostedemail.com: domain of fengwei.yin@intel.com designates 134.134.136.31 as permitted sender) smtp.mailfrom=fengwei.yin@intel.com; dmarc=pass (policy=none) header.from=intel.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1675239358; 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=fDcWKoFwFljsPK3KsT6Yo+8RTqtHxchd2WBzZ7mkuNA=; b=k/xadlq4KCZ2Q3t3jIBYlG03r0pmBiiAhMQL70oTLplAXwpio7q4ub2PWZ6eiKEfwOPP5l z64zOHP/W0XEBLcqpuTlZgJz7o0w1XNN2gntffWcgBKQJDKb+bUZUwT5Ld5cheeNFVMDvx pcjY3V4QsEKG0Gmz6rLHG/dsPNjkPDY= ARC-Authentication-Results: i=1; imf17.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=ZBq1DstY; spf=pass (imf17.hostedemail.com: domain of fengwei.yin@intel.com designates 134.134.136.31 as permitted sender) smtp.mailfrom=fengwei.yin@intel.com; dmarc=pass (policy=none) header.from=intel.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1675239358; a=rsa-sha256; cv=none; b=lyMtBp0mE+IACCiL2AmDZvGQgimQA6xCynfDyVNa+GAxg0xXNpQbcncqj2mFKEJokytAMy Ni/3nypJxBqTTWzeIrfuZXOm4uDPm2Ttpa9sqjlRDmeuVOPDO7d0umSA/fG+g4jckvlLAl G4GFOx/911ntaxHvRLilPdkSLOTQK0A= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1675239358; x=1706775358; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=Clv/GELshdHOgJDdTOOm5URWceoffx0lE1daZT1k8p0=; b=ZBq1DstYgpVMkYmWmDSiiYBKV06NYBor0ajzLlabET4zgPhvI+HpKLQZ kWihZmXjr3WiDP8xIHja2RkW4goFNtO9DJFjYVkDFoHTKCgLGezOlvVnW dWagyNA4kD+eygUUzAHsBGP5EyEVwpfH4DluENnnik9dxDM9av1fDBWJM WYdcYSXgqR9n8HIh2tLfPgdNftDWXhgR1R87YifqzZ6rGsD9yswN6J6Kc 297iTanJ1ovHfLgY11pESkSS4ztEJL1x9PM92zio8n0dhkRgsY8leV2s6 QgcC9BFAYwXI06ccCQrhyoDuuLo87+80fT7Ml0A5LoLOAipQlm80+apsz w==; X-IronPort-AV: E=McAfee;i="6500,9779,10607"; a="390471699" X-IronPort-AV: E=Sophos;i="5.97,263,1669104000"; d="scan'208";a="390471699" Received: from fmsmga003.fm.intel.com ([10.253.24.29]) by orsmga104.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 01 Feb 2023 00:15:56 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6500,9779,10607"; a="753580356" X-IronPort-AV: E=Sophos;i="5.97,263,1669104000"; d="scan'208";a="753580356" Received: from fyin-dev.sh.intel.com ([10.239.159.32]) by FMSMGA003.fm.intel.com with ESMTP; 01 Feb 2023 00:15:54 -0800 From: Yin Fengwei To: willy@infradead.org, david@redhat.com, linux-mm@kvack.org Cc: dave.hansen@intel.com, tim.c.chen@intel.com, ying.huang@intel.com, fengwei.yin@intel.com Subject: [RFC PATCH v2 4/5] mm: add do_set_pte_range() Date: Wed, 1 Feb 2023 16:17:36 +0800 Message-Id: <20230201081737.2330141-5-fengwei.yin@intel.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20230201081737.2330141-1-fengwei.yin@intel.com> References: <20230201081737.2330141-1-fengwei.yin@intel.com> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam03 X-Stat-Signature: apq8zzz191judynsqfowe3onz4qm55qy X-Rspamd-Queue-Id: 07D984001F X-HE-Tag: 1675239357-236278 X-HE-Meta: U2FsdGVkX1/4b2qbs2y2uAgwssTd+0kgNUOdU8E38I1puyjtdiJMaaaVGhw4QwmONAYS3U+yBOrSfdXXwHjNqNND0ZMgVdgn6my5MlJgXOJVeIfqznOMYUfh+vUzMxuJTiuxciiR1KbZyiSE/g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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: do_set_pte_range() allows to setup page table entries for a specific range. It calls page_add_file_rmap_range() to take advantage of batched rmap update for large folio. Signed-off-by: Yin Fengwei --- include/linux/mm.h | 2 ++ mm/filemap.c | 1 - mm/memory.c | 60 ++++++++++++++++++++++++++++++++++++---------- 3 files changed, 49 insertions(+), 14 deletions(-) diff --git a/include/linux/mm.h b/include/linux/mm.h index d6f8f41514cc..96e08fcdce24 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -1162,6 +1162,8 @@ static inline pte_t maybe_mkwrite(pte_t pte, struct vm_area_struct *vma) vm_fault_t do_set_pmd(struct vm_fault *vmf, struct page *page); void do_set_pte(struct vm_fault *vmf, struct page *page, unsigned long addr); +void do_set_pte_range(struct vm_fault *vmf, struct folio *folio, + unsigned long start, unsigned long addr, unsigned int nr); vm_fault_t finish_fault(struct vm_fault *vmf); vm_fault_t finish_mkwrite_fault(struct vm_fault *vmf); diff --git a/mm/filemap.c b/mm/filemap.c index 9cc5edd8f998..95f634d11581 100644 --- a/mm/filemap.c +++ b/mm/filemap.c @@ -3386,7 +3386,6 @@ static vm_fault_t filemap_map_folio_range(struct vm_fault *vmf, ref_count++; do_set_pte(vmf, page, addr); - update_mmu_cache(vma, addr, vmf->pte); } while (vmf->pte++, page++, addr += PAGE_SIZE, ++count < nr_pages); /* diff --git a/mm/memory.c b/mm/memory.c index 51c04bb60724..7e41142e1e4f 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -4257,7 +4257,8 @@ vm_fault_t do_set_pmd(struct vm_fault *vmf, struct page *page) } #endif -void do_set_pte(struct vm_fault *vmf, struct page *page, unsigned long addr) +static void do_set_pte_entry(struct vm_fault *vmf, struct page *page, + unsigned long addr) { struct vm_area_struct *vma = vmf->vma; bool uffd_wp = pte_marker_uffd_wp(vmf->orig_pte); @@ -4277,16 +4278,52 @@ void do_set_pte(struct vm_fault *vmf, struct page *page, unsigned long addr) entry = maybe_mkwrite(pte_mkdirty(entry), vma); if (unlikely(uffd_wp)) entry = pte_mkuffd_wp(entry); - /* copy-on-write page */ - if (write && !(vma->vm_flags & VM_SHARED)) { - inc_mm_counter(vma->vm_mm, MM_ANONPAGES); - page_add_new_anon_rmap(page, vma, addr); - lru_cache_add_inactive_or_unevictable(page, vma); - } else { - inc_mm_counter(vma->vm_mm, mm_counter_file(page)); - page_add_file_rmap(page, vma, false); - } set_pte_at(vma->vm_mm, addr, vmf->pte, entry); + + /* no need to invalidate: a not-present page won't be cached */ + update_mmu_cache(vma, addr, vmf->pte); +} + +void do_set_pte_range(struct vm_fault *vmf, struct folio *folio, + unsigned long start, unsigned long addr, unsigned int nr) +{ + unsigned int i = 0; + struct page *page = folio_page(folio, start); + struct vm_area_struct *vma = vmf->vma; + bool cow = (vmf->flags & FAULT_FLAG_WRITE) && + !(vma->vm_flags & VM_SHARED); + + /* + * file page: batched update rmap, mm counter. + * copy-on-write page: batched update mm counter. + */ + if (!cow) { + page_add_file_rmap_range(folio, start, nr, vma, false); + add_mm_counter(vma->vm_mm, mm_counter_file(page), nr); + } else + add_mm_counter(vma->vm_mm, MM_ANONPAGES, nr); + + do { + if (cow) { + page_add_new_anon_rmap(page, vma, addr); + lru_cache_add_inactive_or_unevictable(page, vma); + } + + do_set_pte_entry(vmf, page, addr); + } while (vmf->pte++, page++, addr += PAGE_SIZE, ++i < nr); +} + +void do_set_pte(struct vm_fault *vmf, struct page *page, unsigned long addr) +{ + struct folio *folio = page_folio(page); + + do_set_pte_range(vmf, folio, folio_page_idx(folio, page), addr, 1); + + /* + * do_set_pte_range changes vmf->pte. Restore it back as + * do_set_pte doesn't expect the change of vmf->pte. + */ + vmf->pte--; } static bool vmf_pte_changed(struct vm_fault *vmf) @@ -4361,9 +4398,6 @@ vm_fault_t finish_fault(struct vm_fault *vmf) if (likely(!vmf_pte_changed(vmf))) { do_set_pte(vmf, page, vmf->address); - /* no need to invalidate: a not-present page won't be cached */ - update_mmu_cache(vma, vmf->address, vmf->pte); - ret = 0; } else { update_mmu_tlb(vma, vmf->address, vmf->pte);