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 57A99C2FC04 for ; Thu, 17 Aug 2023 02:18:23 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 2B0D2280020; Wed, 16 Aug 2023 22:18:22 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 2612928001D; Wed, 16 Aug 2023 22:18:22 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 0DACC280020; Wed, 16 Aug 2023 22:18:22 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id EABAD28001D for ; Wed, 16 Aug 2023 22:18:21 -0400 (EDT) Received: from smtpin09.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id AB4CA1C8EB1 for ; Thu, 17 Aug 2023 02:18:21 +0000 (UTC) X-FDA: 81131987202.09.97C5877 Received: from mail-pf1-f171.google.com (mail-pf1-f171.google.com [209.85.210.171]) by imf05.hostedemail.com (Postfix) with ESMTP id CC903100002 for ; Thu, 17 Aug 2023 02:18:19 +0000 (UTC) Authentication-Results: imf05.hostedemail.com; dkim=pass header.d=gmail.com header.s=20221208 header.b="K4Tqy/Ni"; spf=pass (imf05.hostedemail.com: domain of haoxing990@gmail.com designates 209.85.210.171 as permitted sender) smtp.mailfrom=haoxing990@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1692238699; 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-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=e/jL6vE8e7cZE2wlM9qF59fOXPl84ghnOWw+2qbiejA=; b=CRRytc57Hlp/+eobgiguGTI2meW2vqYgmBFv8/qU0d6RaAfAnLCrPUuxiOVGuou7wjj85P 0p5FLnNfW12uQy4g8DSbKogh78lnhCdZtW0IcApZ2hwAbvNDt1pWkZ8qdymUFK4UeJz9nW eNzZxX1BUPDyjdrv7mqlFzRI6Rbq7ww= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1692238699; a=rsa-sha256; cv=none; b=KJkx79HonU2xKtV242gy+qtbdU+wI77eAMCT0hB9HT7onvWfWXIRp9zQykS4rbIBa2qPBB gSPoh0Oljjk5Rwxi5lsn8sZLqDXlJUZGBUPetm1jlV4gBYQgVmCRSVm/6l358QNHTTnokt qrrJWDpDtrDiGwgmBcxeavV/B3yfTy0= ARC-Authentication-Results: i=1; imf05.hostedemail.com; dkim=pass header.d=gmail.com header.s=20221208 header.b="K4Tqy/Ni"; spf=pass (imf05.hostedemail.com: domain of haoxing990@gmail.com designates 209.85.210.171 as permitted sender) smtp.mailfrom=haoxing990@gmail.com; dmarc=pass (policy=none) header.from=gmail.com Received: by mail-pf1-f171.google.com with SMTP id d2e1a72fcca58-68842ebdcf7so2912822b3a.0 for ; Wed, 16 Aug 2023 19:18:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1692238698; x=1692843498; h=in-reply-to:from:references:cc:to:subject:user-agent:mime-version :date:message-id:from:to:cc:subject:date:message-id:reply-to; bh=e/jL6vE8e7cZE2wlM9qF59fOXPl84ghnOWw+2qbiejA=; b=K4Tqy/NiHJaQ55a2xsaeF6+jOH3WcbUrS5RPaABJAtqt7p5Y7av0aloVfEmJB3lPHf Oxsi4AvrSmgKqQLshlDYxZTl5Lq19jKl3BoXEqWXEtJjBUEWZgj8iLJBY0I6VFFRWOej sl17Q9vqfJ8fJYV672/Eo51o8O1CnMh7oDQrpLaJ0ebl/0bcB2E9JVwvGu+E9oIMn1CO aTdaDhNRWAOVgO+SysIHSTclZFDL+HzpnZNqT4BTyowSq8f2A/ti3bxG2VLCLwRjhPl/ hbKmBS8JVyuGjd4Yg+4sF3eBUm71LJjzX66uEy00nDwzsr/D2w8vDq+P5RQaySthv3kQ bEpg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1692238698; x=1692843498; h=in-reply-to:from:references:cc:to:subject:user-agent:mime-version :date:message-id:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=e/jL6vE8e7cZE2wlM9qF59fOXPl84ghnOWw+2qbiejA=; b=lZPdg021tZSxnIvtDBomXkq1tBxXLSvMqWhgXAMDd7YzDFNudrMhJ+ed+wb5USGl4h LIVsC1kRcSAlbrGl1p5zTo06BIKVMmGLWg74qnz6zY9qNjBmGgvNm6tDEZwpqF4kTQJq u4Utdhci86T01i4kxVWUbKkwBD1+teRmUaDfQvDexAeCN0Rutc/8YCCpvBF6BVhOKENK uGFM4a3OwUV2hN4JMR/IGgZhSnJsxVtwMqPmnvCuAdvWKuRy7mLRHfYCJ7Olr76WXrCG oABmNpwWz/yLGaRcfCGR43Rfz74w27+Gec2tfpzVZRrL0T60Oqj6rFtNaoaS8XCN010K d7pQ== X-Gm-Message-State: AOJu0Yw98eNGDOZ/++j8VXyPjTNZMwH0Fl0RHG/4YiHFsb78OoceU3R3 cxauS2rWkFe5JPQrVHrW8HI= X-Google-Smtp-Source: AGHT+IFRhLkjXQ7ZyE9YFTdpZmnHDU4Ke6q/zc7CLL/4nMS2oduvd+hLLOSejIg681BYTk1h1rQyew== X-Received: by 2002:a05:6a21:2781:b0:134:76f6:e9e2 with SMTP id rn1-20020a056a21278100b0013476f6e9e2mr3710106pzb.58.1692238698409; Wed, 16 Aug 2023 19:18:18 -0700 (PDT) Received: from [192.168.255.10] ([103.7.29.31]) by smtp.gmail.com with ESMTPSA id k17-20020aa792d1000000b00686236718d8sm11503415pfa.41.2023.08.16.19.18.15 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 16 Aug 2023 19:18:17 -0700 (PDT) Content-Type: multipart/alternative; boundary="------------Tc2UZi3fsjk3XpJGVOS04BXJ" Message-ID: Date: Thu, 17 Aug 2023 10:18:10 +0800 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [RFC 1/2] mm/rmap: Recognize non-writable TLB entries during TLB batch flush To: Byungchul Park , linux-kernel@vger.kernel.org, linux-mm@kvack.org Cc: kernel_team@skhynix.com, akpm@linux-foundation.org, ying.huang@intel.com, namit@vmware.com, xhao@linux.alibaba.com, mgorman@techsingularity.net, hughd@google.com, willy@infradead.org, david@redhat.com References: <20230804061850.21498-1-byungchul@sk.com> <20230804061850.21498-2-byungchul@sk.com> From: Xin Hao In-Reply-To: <20230804061850.21498-2-byungchul@sk.com> X-Rspamd-Queue-Id: CC903100002 X-Rspam-User: X-Stat-Signature: wc7p37xrkgfqpqsb64s5imps43w4pwr4 X-Rspamd-Server: rspam03 X-HE-Tag: 1692238699-579529 X-HE-Meta: U2FsdGVkX1/NZnVXjGrrjld8P81QO5bIpGBe+IL1675dsWNKXLQpAL1U9gNSR6EX5o0ZlD5m9b586+k+ngNs8USWcZZmv0ZlPRmyo/m43LcLER+i7esuGrpvO5vM3biURzjEju040XbSjFvsuqq69j0sUNCm62MttgiKZyoPU24i9Gig411eXtFyWdZlRfmH1hMvSlNLISEnyZ+njG1vMqux4S1CVrKytOJWHHnZ1BvqRe+2WKUHrDrn6gAz9xnEKIv+T4aY7W9/zdYI0S5hcfrziK9j6/I8CxRrR+L2laxoWCriBRRzLcMoFlCFbtZslkI6lH8oStKJzOQIYZ+YzoGPAZ4kRY7kI35dsuiIC5avk8LQaDSD64Aj3wOaWh1IiWfmz2YaS+4m7Mvoj4cTmNQS3Nmrag/yzVwpE+k08Ep8vcM0UE7+VIT1MQTgS9mZNi3GNkiPAdAr3tAJWueN9I/ZnPtKSxdFk+U6sJA6F7T459lbYvVkIPrv1YbWSCQ41KzRjZ81sgFTQJHEJRBJwkKf9S02sJYFhdL4JLa4ksSIYdyWv38cVTkvm/0h/O84/QZySE+cNe6pydAXtk+Rg3hjpCbtan/+R/6M5JCPGL1iUMnlmR+hwOjAKYVtTPzEPuo3AnOIqTclSTv4CZOkqBBvlkOun41ZOSR7g2IgCy2weBFeUusFN1Uu0GEYRQLFPsu7dq6ItRPSpANVvq08ilzc5Qv/gVpDF1Xn0pzg7sL4rPwV9UBl1e5/qOx9zERZTyfR8yBE4EPuWecSUJ49SfVGsyCDBDJFyYuDshpXG7oqWFSJhgvIlqAU6y4YIM1juKZDeLmXDw82CPV5yjBHqFcsEtLirZX4mRMrh/VcsD98par78LoL/OTri90PPraTrjWoGRNcoEQpjrcvo9weHVNY+C609FBgfNuXm6f2nm1IFapdUa5b6PZpffTPo6zJDEPxkbBUVoPRzOSp3xY kidONPsp 6RlyZLW/2e+iyyYPrZrY/oSBmOhajZZlx5+U9Sq7PamgB91t1EVQ9WGTMqJZcHybqrV0EZdvb21UsQEgXk1heXYjJdvcPLTNWKZunK007ThEzO6nv9pB8pgccRJLYVUq1BhP+r8msDwpzv3adfMwj+y3qoCgFfwWKN8DGwVsQMUsTACZvdxiOeDidEBoEzX4KXwRdn6JyII/1CZirkydpsxBnF0QJh5j1BwoQCBM2+QY5FoBjEcjwZCArm7cSBwZ8yS8tBJbLxOGJt2UPEjrVROfbXzektsp5IEaU6CYP/KA0buGy3IyJI32Nr36iKaVOdLRPV2ea3Es586DOHynbM76sAVBNfgRr44xCsL6lYoazSTjfLlnHOAb6dlOA0wZhYVSGt5tg+L+byFXm3LEGPRx9/xEiiekXQBaNAfqXwFT+d1uqRFepk8Nxat7uHzkoNhkzZB9E7cNQAThRtJnMxZLoIISBMlg/1XOTeZYFU3Cvh8GuR8kAMCWtsmBfYHva/pfpTS4Dr+e+FMPxgLSw9KDAWJhLqZlZv9cwa45CBYHoRqbJPJU7wjhjEMuyhzAj9pvAzhY+Hoy/mJ8pX1us8TsFpLzcXjzG+4ekwBOcsU53pj9gLUDTc+3PbOSxzXG1eqpsNi9hs9wcvK8DmNqZxLer5/9fqNz9vKMAumy75NkuPVfp+8Lv79VESBIBXp+4CID4 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: This is a multi-part message in MIME format. --------------Tc2UZi3fsjk3XpJGVOS04BXJ Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit 在 2023/8/4 14:18, Byungchul Park 写道: > Functionally, no change. This is a preparation for CONFIG_MIGRC that > requires to recognize non-writable TLB entries and makes use of them to > batch more aggressively or even skip TLB flushes. > > While at it, changed struct tlbflush_unmap's ->flush_required(boolean) > to ->nr_flush_required(int) in order to take account of not only whether > it has been requested or not, but also the exact number of the requests. > That will be used in CONFIG_MIGRC implementation too. > > Signed-off-by: Byungchul Park > --- > arch/x86/include/asm/tlbflush.h | 2 ++ > arch/x86/mm/tlb.c | 7 +++++++ > include/linux/mm_types_task.h | 4 ++-- > include/linux/sched.h | 1 + > mm/internal.h | 4 ++++ > mm/rmap.c | 29 ++++++++++++++++++++++++----- > 6 files changed, 40 insertions(+), 7 deletions(-) > > diff --git a/arch/x86/include/asm/tlbflush.h b/arch/x86/include/asm/tlbflush.h > index 75bfaa421030..63504cde364b 100644 > --- a/arch/x86/include/asm/tlbflush.h > +++ b/arch/x86/include/asm/tlbflush.h > @@ -279,6 +279,8 @@ static inline void arch_tlbbatch_add_mm(struct arch_tlbflush_unmap_batch *batch, > } > > extern void arch_tlbbatch_flush(struct arch_tlbflush_unmap_batch *batch); > +extern void arch_tlbbatch_fold(struct arch_tlbflush_unmap_batch *bdst, > + struct arch_tlbflush_unmap_batch *bsrc); > > static inline bool pte_flags_need_flush(unsigned long oldflags, > unsigned long newflags, > diff --git a/arch/x86/mm/tlb.c b/arch/x86/mm/tlb.c > index 267acf27480a..69d145f1fff1 100644 > --- a/arch/x86/mm/tlb.c > +++ b/arch/x86/mm/tlb.c > @@ -1265,6 +1265,13 @@ void arch_tlbbatch_flush(struct arch_tlbflush_unmap_batch *batch) > put_cpu(); > } > > +void arch_tlbbatch_fold(struct arch_tlbflush_unmap_batch *bdst, > + struct arch_tlbflush_unmap_batch *bsrc) > +{ > + cpumask_or(&bdst->cpumask, &bdst->cpumask, &bsrc->cpumask); > + cpumask_clear(&bsrc->cpumask); > +} > + > /* > * Blindly accessing user memory from NMI context can be dangerous > * if we're in the middle of switching the current user task or > diff --git a/include/linux/mm_types_task.h b/include/linux/mm_types_task.h > index 5414b5c6a103..6f3bb757eb46 100644 > --- a/include/linux/mm_types_task.h > +++ b/include/linux/mm_types_task.h > @@ -59,8 +59,8 @@ struct tlbflush_unmap_batch { > */ > struct arch_tlbflush_unmap_batch arch; > > - /* True if a flush is needed. */ > - bool flush_required; > + /* The number of flush requested. */ > + int nr_flush_required; > > /* > * If true then the PTE was dirty when unmapped. The entry must be > diff --git a/include/linux/sched.h b/include/linux/sched.h > index eed5d65b8d1f..2232b2cdfce8 100644 > --- a/include/linux/sched.h > +++ b/include/linux/sched.h > @@ -1322,6 +1322,7 @@ struct task_struct { > #endif > > struct tlbflush_unmap_batch tlb_ubc; > + struct tlbflush_unmap_batch tlb_ubc_nowr; Maybe 'tlb_ubc_nowr' can replace 'tlb_ubc' later. > > /* Cache last used pipe for splice(): */ > struct pipe_inode_info *splice_pipe; > diff --git a/mm/internal.h b/mm/internal.h > index 68410c6d97ac..b90d516ad41f 100644 > --- a/mm/internal.h > +++ b/mm/internal.h > @@ -840,6 +840,7 @@ extern struct workqueue_struct *mm_percpu_wq; > void try_to_unmap_flush(void); > void try_to_unmap_flush_dirty(void); > void flush_tlb_batched_pending(struct mm_struct *mm); > +void fold_ubc_nowr(void); > #else > static inline void try_to_unmap_flush(void) > { > @@ -850,6 +851,9 @@ static inline void try_to_unmap_flush_dirty(void) > static inline void flush_tlb_batched_pending(struct mm_struct *mm) > { > } > +static inline void fold_ubc_nowr(void) > +{ > +} > #endif /* CONFIG_ARCH_WANT_BATCHED_UNMAP_TLB_FLUSH */ > > extern const struct trace_print_flags pageflag_names[]; > diff --git a/mm/rmap.c b/mm/rmap.c > index 19392e090bec..d18460a48485 100644 > --- a/mm/rmap.c > +++ b/mm/rmap.c > @@ -605,6 +605,22 @@ struct anon_vma *folio_lock_anon_vma_read(struct folio *folio, > } > > #ifdef CONFIG_ARCH_WANT_BATCHED_UNMAP_TLB_FLUSH > + > +void fold_ubc_nowr(void) > +{ > + struct tlbflush_unmap_batch *tlb_ubc = ¤t->tlb_ubc; > + struct tlbflush_unmap_batch *tlb_ubc_nowr = ¤t->tlb_ubc_nowr; > + > + if (!tlb_ubc_nowr->nr_flush_required) > + return; > + > + arch_tlbbatch_fold(&tlb_ubc->arch, &tlb_ubc_nowr->arch); > + tlb_ubc->writable = tlb_ubc->writable || tlb_ubc_nowr->writable; > + tlb_ubc->nr_flush_required += tlb_ubc_nowr->nr_flush_required; > + tlb_ubc_nowr->nr_flush_required = 0; > + tlb_ubc_nowr->writable = false; > +} > + > /* > * Flush TLB entries for recently unmapped pages from remote CPUs. It is > * important if a PTE was dirty when it was unmapped that it's flushed > @@ -615,11 +631,12 @@ void try_to_unmap_flush(void) > { > struct tlbflush_unmap_batch *tlb_ubc = ¤t->tlb_ubc; > > - if (!tlb_ubc->flush_required) > + fold_ubc_nowr(); > + if (!tlb_ubc->nr_flush_required) > return; > > arch_tlbbatch_flush(&tlb_ubc->arch); > - tlb_ubc->flush_required = false; > + tlb_ubc->nr_flush_required = 0; > tlb_ubc->writable = false; > } > > @@ -627,8 +644,9 @@ void try_to_unmap_flush(void) > void try_to_unmap_flush_dirty(void) > { > struct tlbflush_unmap_batch *tlb_ubc = ¤t->tlb_ubc; > + struct tlbflush_unmap_batch *tlb_ubc_nowr = ¤t->tlb_ubc_nowr; > > - if (tlb_ubc->writable) > + if (tlb_ubc->writable || tlb_ubc_nowr->writable) > try_to_unmap_flush(); > } > > @@ -644,15 +662,16 @@ void try_to_unmap_flush_dirty(void) > > static void set_tlb_ubc_flush_pending(struct mm_struct *mm, pte_t pteval) > { > - struct tlbflush_unmap_batch *tlb_ubc = ¤t->tlb_ubc; > + struct tlbflush_unmap_batch *tlb_ubc; > int batch; > bool writable = pte_dirty(pteval); > > if (!pte_accessible(mm, pteval)) > return; > > + tlb_ubc = pte_write(pteval) ? ¤t->tlb_ubc : ¤t->tlb_ubc_nowr; > arch_tlbbatch_add_mm(&tlb_ubc->arch, mm); > - tlb_ubc->flush_required = true; > + tlb_ubc->nr_flush_required += 1; > > /* > * Ensure compiler does not re-order the setting of tlb_flush_batched --------------Tc2UZi3fsjk3XpJGVOS04BXJ Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: 8bit


在 2023/8/4 14:18, Byungchul Park 写道:
Functionally, no change. This is a preparation for CONFIG_MIGRC that
requires to recognize non-writable TLB entries and makes use of them to
batch more aggressively or even skip TLB flushes.

While at it, changed struct tlbflush_unmap's ->flush_required(boolean)
to ->nr_flush_required(int) in order to take account of not only whether
it has been requested or not, but also the exact number of the requests.
That will be used in CONFIG_MIGRC implementation too.

Signed-off-by: Byungchul Park <byungchul@sk.com>
---
 arch/x86/include/asm/tlbflush.h |  2 ++
 arch/x86/mm/tlb.c               |  7 +++++++
 include/linux/mm_types_task.h   |  4 ++--
 include/linux/sched.h           |  1 +
 mm/internal.h                   |  4 ++++
 mm/rmap.c                       | 29 ++++++++++++++++++++++++-----
 6 files changed, 40 insertions(+), 7 deletions(-)

diff --git a/arch/x86/include/asm/tlbflush.h b/arch/x86/include/asm/tlbflush.h
index 75bfaa421030..63504cde364b 100644
--- a/arch/x86/include/asm/tlbflush.h
+++ b/arch/x86/include/asm/tlbflush.h
@@ -279,6 +279,8 @@ static inline void arch_tlbbatch_add_mm(struct arch_tlbflush_unmap_batch *batch,
 }
 
 extern void arch_tlbbatch_flush(struct arch_tlbflush_unmap_batch *batch);
+extern void arch_tlbbatch_fold(struct arch_tlbflush_unmap_batch *bdst,
+			       struct arch_tlbflush_unmap_batch *bsrc);
 
 static inline bool pte_flags_need_flush(unsigned long oldflags,
 					unsigned long newflags,
diff --git a/arch/x86/mm/tlb.c b/arch/x86/mm/tlb.c
index 267acf27480a..69d145f1fff1 100644
--- a/arch/x86/mm/tlb.c
+++ b/arch/x86/mm/tlb.c
@@ -1265,6 +1265,13 @@ void arch_tlbbatch_flush(struct arch_tlbflush_unmap_batch *batch)
 	put_cpu();
 }
 
+void arch_tlbbatch_fold(struct arch_tlbflush_unmap_batch *bdst,
+			struct arch_tlbflush_unmap_batch *bsrc)
+{
+	cpumask_or(&bdst->cpumask, &bdst->cpumask, &bsrc->cpumask);
+	cpumask_clear(&bsrc->cpumask);
+}
+
 /*
  * Blindly accessing user memory from NMI context can be dangerous
  * if we're in the middle of switching the current user task or
diff --git a/include/linux/mm_types_task.h b/include/linux/mm_types_task.h
index 5414b5c6a103..6f3bb757eb46 100644
--- a/include/linux/mm_types_task.h
+++ b/include/linux/mm_types_task.h
@@ -59,8 +59,8 @@ struct tlbflush_unmap_batch {
 	 */
 	struct arch_tlbflush_unmap_batch arch;
 
-	/* True if a flush is needed. */
-	bool flush_required;
+	/* The number of flush requested. */
+	int nr_flush_required;
 
 	/*
 	 * If true then the PTE was dirty when unmapped. The entry must be
diff --git a/include/linux/sched.h b/include/linux/sched.h
index eed5d65b8d1f..2232b2cdfce8 100644
--- a/include/linux/sched.h
+++ b/include/linux/sched.h
@@ -1322,6 +1322,7 @@ struct task_struct {
 #endif
 
 	struct tlbflush_unmap_batch	tlb_ubc;
+	struct tlbflush_unmap_batch	tlb_ubc_nowr;
Maybe 'tlb_ubc_nowr' can replace 'tlb_ubc' later.
 
 	/* Cache last used pipe for splice(): */
 	struct pipe_inode_info		*splice_pipe;
diff --git a/mm/internal.h b/mm/internal.h
index 68410c6d97ac..b90d516ad41f 100644
--- a/mm/internal.h
+++ b/mm/internal.h
@@ -840,6 +840,7 @@ extern struct workqueue_struct *mm_percpu_wq;
 void try_to_unmap_flush(void);
 void try_to_unmap_flush_dirty(void);
 void flush_tlb_batched_pending(struct mm_struct *mm);
+void fold_ubc_nowr(void);
 #else
 static inline void try_to_unmap_flush(void)
 {
@@ -850,6 +851,9 @@ static inline void try_to_unmap_flush_dirty(void)
 static inline void flush_tlb_batched_pending(struct mm_struct *mm)
 {
 }
+static inline void fold_ubc_nowr(void)
+{
+}
 #endif /* CONFIG_ARCH_WANT_BATCHED_UNMAP_TLB_FLUSH */
 
 extern const struct trace_print_flags pageflag_names[];
diff --git a/mm/rmap.c b/mm/rmap.c
index 19392e090bec..d18460a48485 100644
--- a/mm/rmap.c
+++ b/mm/rmap.c
@@ -605,6 +605,22 @@ struct anon_vma *folio_lock_anon_vma_read(struct folio *folio,
 }
 
 #ifdef CONFIG_ARCH_WANT_BATCHED_UNMAP_TLB_FLUSH
+
+void fold_ubc_nowr(void)
+{
+	struct tlbflush_unmap_batch *tlb_ubc = &current->tlb_ubc;
+	struct tlbflush_unmap_batch *tlb_ubc_nowr = &current->tlb_ubc_nowr;
+
+	if (!tlb_ubc_nowr->nr_flush_required)
+		return;
+
+	arch_tlbbatch_fold(&tlb_ubc->arch, &tlb_ubc_nowr->arch);
+	tlb_ubc->writable = tlb_ubc->writable || tlb_ubc_nowr->writable;
+	tlb_ubc->nr_flush_required += tlb_ubc_nowr->nr_flush_required;
+	tlb_ubc_nowr->nr_flush_required = 0;
+	tlb_ubc_nowr->writable = false;
+}
+
 /*
  * Flush TLB entries for recently unmapped pages from remote CPUs. It is
  * important if a PTE was dirty when it was unmapped that it's flushed
@@ -615,11 +631,12 @@ void try_to_unmap_flush(void)
 {
 	struct tlbflush_unmap_batch *tlb_ubc = &current->tlb_ubc;
 
-	if (!tlb_ubc->flush_required)
+	fold_ubc_nowr();
+	if (!tlb_ubc->nr_flush_required)
 		return;
 
 	arch_tlbbatch_flush(&tlb_ubc->arch);
-	tlb_ubc->flush_required = false;
+	tlb_ubc->nr_flush_required = 0;
 	tlb_ubc->writable = false;
 }
 
@@ -627,8 +644,9 @@ void try_to_unmap_flush(void)
 void try_to_unmap_flush_dirty(void)
 {
 	struct tlbflush_unmap_batch *tlb_ubc = &current->tlb_ubc;
+	struct tlbflush_unmap_batch *tlb_ubc_nowr = &current->tlb_ubc_nowr;
 
-	if (tlb_ubc->writable)
+	if (tlb_ubc->writable || tlb_ubc_nowr->writable)
 		try_to_unmap_flush();
 }
 
@@ -644,15 +662,16 @@ void try_to_unmap_flush_dirty(void)
 
 static void set_tlb_ubc_flush_pending(struct mm_struct *mm, pte_t pteval)
 {
-	struct tlbflush_unmap_batch *tlb_ubc = &current->tlb_ubc;
+	struct tlbflush_unmap_batch *tlb_ubc;
 	int batch;
 	bool writable = pte_dirty(pteval);
 
 	if (!pte_accessible(mm, pteval))
 		return;
 
+	tlb_ubc = pte_write(pteval) ? &current->tlb_ubc : &current->tlb_ubc_nowr;
 	arch_tlbbatch_add_mm(&tlb_ubc->arch, mm);
-	tlb_ubc->flush_required = true;
+	tlb_ubc->nr_flush_required += 1;
 
 	/*
 	 * Ensure compiler does not re-order the setting of tlb_flush_batched
--------------Tc2UZi3fsjk3XpJGVOS04BXJ--