Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

lazy tlb: allow lazy tlb mm refcounting to be configurable

Add CONFIG_MMU_TLB_REFCOUNT which enables refcounting of the lazy tlb mm
when it is context switched. This can be disabled by architectures that
don't require this refcounting if they clean up lazy tlb mms when the last
refcount is dropped. Currently this is always enabled, so the patch
introduces no functional change.

Link: https://lkml.kernel.org/r/20230203071837.1136453-4-npiggin@gmail.com
Signed-off-by: Nicholas Piggin <npiggin@gmail.com>
Acked-by: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Andy Lutomirski <luto@kernel.org>
Cc: Catalin Marinas <catalin.marinas@arm.com>
Cc: Christophe Leroy <christophe.leroy@csgroup.eu>
Cc: Dave Hansen <dave.hansen@linux.intel.com>
Cc: Michael Ellerman <mpe@ellerman.id.au>
Cc: Nadav Amit <nadav.amit@gmail.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Rik van Riel <riel@redhat.com>
Cc: Will Deacon <will@kernel.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>

authored by

Nicholas Piggin and committed by
Andrew Morton
88e3009b aa464ba9

+38 -3
+6
Documentation/mm/active_mm.rst
··· 2 2 Active MM 3 3 ========= 4 4 5 + Note, the mm_count refcount may no longer include the "lazy" users 6 + (running tasks with ->active_mm == mm && ->mm == NULL) on kernels 7 + with CONFIG_MMU_LAZY_TLB_REFCOUNT=n. Taking and releasing these lazy 8 + references must be done with mmgrab_lazy_tlb() and mmdrop_lazy_tlb() 9 + helpers, which abstract this config option. 10 + 5 11 :: 6 12 7 13 List: linux-kernel
+17
arch/Kconfig
··· 465 465 irqs disabled over activate_mm. Architectures that do IPI based TLB 466 466 shootdowns should enable this. 467 467 468 + # Use normal mm refcounting for MMU_LAZY_TLB kernel thread references. 469 + # MMU_LAZY_TLB_REFCOUNT=n can improve the scalability of context switching 470 + # to/from kernel threads when the same mm is running on a lot of CPUs (a large 471 + # multi-threaded application), by reducing contention on the mm refcount. 472 + # 473 + # This can be disabled if the architecture ensures no CPUs are using an mm as a 474 + # "lazy tlb" beyond its final refcount (i.e., by the time __mmdrop frees the mm 475 + # or its kernel page tables). This could be arranged by arch_exit_mmap(), or 476 + # final exit(2) TLB flush, for example. 477 + # 478 + # To implement this, an arch *must*: 479 + # Ensure the _lazy_tlb variants of mmgrab/mmdrop are used when manipulating 480 + # the lazy tlb reference of a kthread's ->active_mm (non-arch code has been 481 + # converted already). 482 + config MMU_LAZY_TLB_REFCOUNT 483 + def_bool y 484 + 468 485 config ARCH_HAVE_NMI_SAFE_CMPXCHG 469 486 bool 470 487
+15 -3
include/linux/sched/mm.h
··· 82 82 /* Helpers for lazy TLB mm refcounting */ 83 83 static inline void mmgrab_lazy_tlb(struct mm_struct *mm) 84 84 { 85 - mmgrab(mm); 85 + if (IS_ENABLED(CONFIG_MMU_LAZY_TLB_REFCOUNT)) 86 + mmgrab(mm); 86 87 } 87 88 88 89 static inline void mmdrop_lazy_tlb(struct mm_struct *mm) 89 90 { 90 - mmdrop(mm); 91 + if (IS_ENABLED(CONFIG_MMU_LAZY_TLB_REFCOUNT)) { 92 + mmdrop(mm); 93 + } else { 94 + /* 95 + * mmdrop_lazy_tlb must provide a full memory barrier, see the 96 + * membarrier comment finish_task_switch which relies on this. 97 + */ 98 + smp_mb(); 99 + } 91 100 } 92 101 93 102 static inline void mmdrop_lazy_tlb_sched(struct mm_struct *mm) 94 103 { 95 - mmdrop_sched(mm); 104 + if (IS_ENABLED(CONFIG_MMU_LAZY_TLB_REFCOUNT)) 105 + mmdrop_sched(mm); 106 + else 107 + smp_mb(); /* see mmdrop_lazy_tlb() above */ 96 108 } 97 109 98 110 /**