A later patch makes the MGLRU aging walk lock VMAs one at a time instead of holding mmap_lock for the whole mm. The walk only tries to read-lock each VMA it finds under RCU, and skips the VMA if its lock is held for write, rather than waiting for the writer. It does so in two places: - While iterating the VMA tree under RCU. - While holding a PTE lock, inside the RCU read side critical section of pte_offset_map(), to hand the VMA lock off to the next VMA in the same PTE table without dropping the PTE lock. Waiting there could deadlock: a writer such as mprotect() takes the PTE lock after the VMA write lock. vma_start_read() does not fit the second place, and is awkward in the first. When it fails, it leaves the RCU read side critical section, which the PTE table mapped by pte_offset_map() depends on, and the iteration has to look the next VMA up again. If the VMA has been recycled for another mm, it also mmdrop()s that mm, and __mmdrop() is heavy enough that vma_start_read() already defers it until after RCU; it should not run under a PTE lock either. lock_vma_under_rcu() and lock_next_vma() build on vma_start_read() and, on failure, retry or fall back to mmap_lock. The walk cannot open code the locking, which relies on helpers private to mm/mmap_lock.c. Move the body of vma_start_read() into __vma_start_read(), which stays in the RCU read side critical section and returns the other mm through @drop_mm instead of dropping it, and rebuild vma_start_read() on top of it with the same behavior. Add vma_start_read_rcu() on top of __vma_start_read(). It returns NULL both when the VMA cannot be locked and when it is detached, since the walk skips it either way. The caller mmdrop()s the mm returned in @drop_mm after dropping its spinlocks and leaving RCU, and, as with any VMA found under RCU, checks that the locked VMA still covers the range it expects. No functional change. Signed-off-by: Chuanhua Han --- include/linux/mmap_lock.h | 3 ++ mm/mmap_lock.c | 75 ++++++++++++++++++++++++++++----------- 2 files changed, 58 insertions(+), 20 deletions(-) diff --git a/include/linux/mmap_lock.h b/include/linux/mmap_lock.h index bada7a03ddf4..04bab16e3de2 100644 --- a/include/linux/mmap_lock.h +++ b/include/linux/mmap_lock.h @@ -536,6 +536,9 @@ static inline void vma_mark_detached(struct vm_area_struct *vma) struct vm_area_struct *lock_vma_under_rcu(struct mm_struct *mm, unsigned long address); +struct vm_area_struct *vma_start_read_rcu(struct mm_struct *mm, + struct vm_area_struct *vma, + struct mm_struct **drop_mm); static inline void vma_rcu_iter_init(struct vma_rcu_iter *rvmi, struct mm_struct *mm, struct vma_iterator *vmi, diff --git a/mm/mmap_lock.c b/mm/mmap_lock.c index 073c5125b424..028a41d6804a 100644 --- a/mm/mmap_lock.c +++ b/mm/mmap_lock.c @@ -220,11 +220,13 @@ static bool vma_seqcount_is_write_locked(unsigned int vma_seq, unsigned int mm_s * Returns the vma on success, NULL on failure to lock and EAGAIN if vma got * detached. * - * IMPORTANT: RCU lock must be held upon entering the function, but upon error - * IT IS RELEASED. The caller must handle this correctly. + * RCU lock must be held, and is kept held. The function does not sleep. If the + * vma got attached to another mm, that mm is grabbed and returned in @drop_mm, + * and the caller must mmdrop() it, outside the RCU read side critical section. */ -static struct vm_area_struct *vma_start_read(struct mm_struct *mm, - struct vm_area_struct *vma) +static struct vm_area_struct *__vma_start_read(struct mm_struct *mm, + struct vm_area_struct *vma, + struct mm_struct **drop_mm) { unsigned int vma_seq; unsigned int mm_seq; @@ -232,6 +234,7 @@ static struct vm_area_struct *vma_start_read(struct mm_struct *mm, int oldcnt; RCU_LOCKDEP_WARN(!rcu_read_lock_held(), "no rcu lock held"); + *drop_mm = NULL; /* * Check before locking. A race might cause false locked result. * We can use READ_ONCE() for the mm_lock_seq here, and don't need @@ -241,10 +244,8 @@ static struct vm_area_struct *vma_start_read(struct mm_struct *mm, */ vma_seq = READ_ONCE(vma->vm_lock_seq); mm_seq = READ_ONCE(mm->mm_lock_seq.sequence); - if (vma_seqcount_is_write_locked(vma_seq, mm_seq)) { - vma = NULL; - goto err; - } + if (vma_seqcount_is_write_locked(vma_seq, mm_seq)) + return NULL; /* * If VM_REFCNT_EXCLUDE_READERS_FLAG is set, @@ -257,8 +258,7 @@ static struct vm_area_struct *vma_start_read(struct mm_struct *mm, if (unlikely(!__refcount_inc_not_zero_limited_acquire(&vma->vm_refcnt, &oldcnt, VM_REFCNT_LIMIT))) { /* return EAGAIN if vma got detached from under us */ - vma = oldcnt ? NULL : ERR_PTR(-EAGAIN); - goto err; + return oldcnt ? NULL : ERR_PTR(-EAGAIN); } __vma_lockdep_acquire_read(vma); @@ -288,14 +288,9 @@ static struct vm_area_struct *vma_start_read(struct mm_struct *mm, mm_seq = raw_read_seqcount(&mm->mm_lock_seq); if (unlikely(vma_seqcount_is_write_locked(vma_seq, mm_seq))) { vma_refcount_put(vma); - vma = NULL; - goto err; + return NULL; } - return vma; -err: - rcu_read_unlock(); - return vma; err_unstable: /* @@ -305,16 +300,56 @@ static struct vm_area_struct *vma_start_read(struct mm_struct *mm, * releasing vma->vm_refcnt. */ other_mm = vma->vm_mm; /* use a copy as vma can be freed after we drop vm_refcnt */ - - /* __mmdrop() is a heavy operation, do it after dropping RCU lock. */ - rcu_read_unlock(); mmgrab(other_mm); vma_refcount_put(vma); - mmdrop(other_mm); + /* __mmdrop() is a heavy operation, leave it to the caller. */ + *drop_mm = other_mm; return NULL; } +/* + * As __vma_start_read(), but upon error the RCU lock IS RELEASED. The caller + * must handle this correctly. + */ +static struct vm_area_struct *vma_start_read(struct mm_struct *mm, + struct vm_area_struct *vma) +{ + struct mm_struct *drop_mm; + + vma = __vma_start_read(mm, vma, &drop_mm); + if (IS_ERR_OR_NULL(vma)) { + rcu_read_unlock(); + if (drop_mm) + mmdrop(drop_mm); + } + + return vma; +} + +/** + * vma_start_read_rcu() - Read-lock a VMA found under RCU, staying under RCU. + * @mm: the mm_struct the VMA is expected to belong to + * @vma: the VMA, found under the RCU lock held by the caller + * @drop_mm: set to an mm the caller must mmdrop() after dropping its spinlocks + * and leaving the RCU read side critical section, or NULL + * + * Unlike vma_start_read(), this does not leave the RCU read side critical + * section on failure, nor mmdrop() a recycled VMA's mm, so it may be called + * with a spinlock, e.g., a page table lock, held. Failures are not retried. + * + * Return: @vma read-locked, or NULL if it could not be locked or got detached. + * The caller must check that the VMA still covers the range it expects. + */ +struct vm_area_struct *vma_start_read_rcu(struct mm_struct *mm, + struct vm_area_struct *vma, + struct mm_struct **drop_mm) +{ + vma = __vma_start_read(mm, vma, drop_mm); + + return IS_ERR(vma) ? NULL : vma; +} + /* * Lookup and lock a VMA under RCU protection. Returned VMA is guaranteed to be * stable and not isolated. If the VMA is not found or is being modified the -- 2.34.1