[PATCH v2 25/35] mm: add mmu_notifier_trylock() and mmu_notifier_unlock()

From: Michel Lespinasse
Date: Fri Jan 28 2022 - 08:20:20 EST


These new functions are to be used when firing MMU notifications
without holding any of the mmap or rmap locks, as is the case with
speculative page fault handlers.

Signed-off-by: Michel Lespinasse <michel@xxxxxxxxxxxxxx>
---
include/linux/mmu_notifier.h | 25 +++++++++++++++++++++++++
1 file changed, 25 insertions(+)

diff --git a/include/linux/mmu_notifier.h b/include/linux/mmu_notifier.h
index ace76fe91c0c..d0430410fdd8 100644
--- a/include/linux/mmu_notifier.h
+++ b/include/linux/mmu_notifier.h
@@ -772,4 +772,29 @@ static inline void mmu_notifier_synchronize(void)

#endif /* CONFIG_MMU_NOTIFIER */

+#if defined(CONFIG_MMU_NOTIFIER) && defined(CONFIG_SPECULATIVE_PAGE_FAULT)
+
+static inline bool mmu_notifier_trylock(struct mm_struct *mm)
+{
+ return percpu_down_read_trylock(mm->mmu_notifier_lock);
+}
+
+static inline void mmu_notifier_unlock(struct mm_struct *mm)
+{
+ percpu_up_read(mm->mmu_notifier_lock);
+}
+
+#else
+
+static inline bool mmu_notifier_trylock(struct mm_struct *mm)
+{
+ return true;
+}
+
+static inline void mmu_notifier_unlock(struct mm_struct *mm)
+{
+}
+
+#endif
+
#endif /* _LINUX_MMU_NOTIFIER_H */
--
2.20.1