Subject: Patch "asm-generic/tlb: avoid potential double flush" has been added to the 4.19-stable tree

From: <[email-masked]>
Date: Mon, 04 Jan 2021 13:55:43 +0100

This is a note to let you know that I've just added the patch titled

    asm-generic/tlb: avoid potential double flush

to the 4.19-stable tree which can be found at:
    http://www.kernel.org/git/?p=linux/kernel/git/stable/stable-queue.git;a=summary

The filename of the patch is:
     asm-generic-tlb-avoid-potential-double-flush.patch
and it can be found in the queue-4.19 subdirectory.

If you, or anyone else, feels it should not be added to the stable tree,
please let <[email-masked]> know about it.

>From foo@baz Mon Jan  4 01:45:29 PM CET 2021
From: Santosh Sivaraj <[email-masked]>
Date: Thu, 12 Mar 2020 18:57:40 +0530
Subject: asm-generic/tlb: avoid potential double flush
To: <[email-masked]>, linuxppc-dev <[email-masked]>
Cc: Michael Ellerman <[email-masked]>, Greg KH <[email-masked]>, Sasha Levin <[email-masked]>, Peter Zijlstra <[email-masked]>, "Aneesh Kumar K . V" <[email-masked]>
Message-ID: <[email-masked]>

From: Peter Zijlstra <[email-masked]>

commit 0758cd8304942292e95a0f750c374533db378b32 upstream.

Aneesh reported that:

	tlb_flush_mmu()
	  tlb_flush_mmu_tlbonly()
	    tlb_flush()			<-- #1
	  tlb_flush_mmu_free()
	    tlb_table_flush()
	      tlb_table_invalidate()
		tlb_flush_mmu_tlbonly()
		  tlb_flush()		<-- #2

does two TLBIs when tlb->fullmm, because __tlb_reset_range() will not
clear tlb->end in that case.

Observe that any caller to __tlb_adjust_range() also sets at least one of
the tlb->freed_tables || tlb->cleared_p* bits, and those are
unconditionally cleared by __tlb_reset_range().

Change the condition for actually issuing TLBI to having one of those bits
set, as opposed to having tlb->end != 0.

Link: http://lkml.kernel.org/r/[email-masked]
Signed-off-by: Peter Zijlstra (Intel) <[email-masked]>
Signed-off-by: Aneesh Kumar K.V <[email-masked]>
Reported-by: "Aneesh Kumar K.V" <[email-masked]>
Cc: <[email-masked]>  # 4.19
Signed-off-by: Santosh Sivaraj <[email-masked]>
[santosh: backported to 4.19 stable]
Signed-off-by: Greg Kroah-Hartman <[email-masked]>
---
 include/asm-generic/tlb.h |    7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)

--- a/include/asm-generic/tlb.h
+++ b/include/asm-generic/tlb.h
@@ -179,7 +179,12 @@ static inline void __tlb_reset_range(str
 
 static inline void tlb_flush_mmu_tlbonly(struct mmu_gather *tlb)
 {
-	if (!tlb->end)
+	/*
+	 * Anything calling __tlb_adjust_range() also sets at least one of
+	 * these bits.
+	 */
+	if (!(tlb->freed_tables || tlb->cleared_ptes || tlb->cleared_pmds ||
+	      tlb->cleared_puds || tlb->cleared_p4ds))
 		return;
 
 	tlb_flush(tlb);

Patches currently in stable-queue which might be from [email-masked] are

queue-4.19/asm-generic-tlb-track-which-levels-of-the-page-tables-have-been-cleared.patch
queue-4.19/asm-generic-tlb-track-freeing-of-page-table-directories-in-struct-mmu_gather.patch
queue-4.19/asm-generic-tlb-avoid-potential-double-flush.patch
queue-4.19/mm-mmu_gather-invalidate-tlb-correctly-on-batch-allocation-failure-and-flush.patch
queue-4.19/powerpc-mmu_gather-enable-rcu_table_free-even-for-smp-case.patch
queue-4.19/asm-generic-tlb-arch-invert-config_have_rcu_table_invalidate.patch