123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677
#define CREATE_TRACE_POINTS
#include <trace/events/mmap_lock.h>
#include <linux/mm.h>
#include <linux/cgroup.h>
#include <linux/memcontrol.h>
#include <linux/mmap_lock.h>
#include <linux/mutex.h>
#include <linux/percpu.h>
#include <linux/rcupdate.h>
#include <linux/smp.h>
#include <linux/trace_events.h>
#include <linux/local_lock.h>
#include "internal.h"
EXPORT_TRACEPOINT_SYMBOL(mmap_lock_start_locking);
EXPORT_TRACEPOINT_SYMBOL(mmap_lock_acquire_returned);
EXPORT_TRACEPOINT_SYMBOL(mmap_lock_released);
#ifdef CONFIG_TRACING
void __mmap_lock_do_trace_start_locking(struct mm_struct *mm, bool write)
{
trace_mmap_lock_start_locking(mm, write);
}
EXPORT_SYMBOL(__mmap_lock_do_trace_start_locking);
void __mmap_lock_do_trace_acquire_returned(struct mm_struct *mm, bool write,
bool success)
{
trace_mmap_lock_acquire_returned(mm, write, success);
}
EXPORT_SYMBOL(__mmap_lock_do_trace_acquire_returned);
void __mmap_lock_do_trace_released(struct mm_struct *mm, bool write)
{
trace_mmap_lock_released(mm, write);
}
EXPORT_SYMBOL(__mmap_lock_do_trace_released);
#endif /* CONFIG_TRACING */
struct vma_exclude_readers_state {
struct vm_area_struct *vma;
int state;
bool detaching;
bool detached;
bool exclusive;
};
static void __vma_end_exclude_readers(struct vma_exclude_readers_state *ves)
{
struct vm_area_struct *vma = ves->vma;
VM_WARN_ON_ONCE(ves->detached);
ves->detached = refcount_sub_and_test(VM_REFCNT_EXCLUDE_READERS_FLAG,
&vma->vm_refcnt);
__vma_lockdep_release_exclusive(vma);
}
static unsigned int get_target_refcnt(struct vma_exclude_readers_state *ves)
{
const unsigned int tgt = ves->detaching ? 0 : 1;
return tgt | VM_REFCNT_EXCLUDE_READERS_FLAG;
}
static int __vma_start_exclude_readers(struct vma_exclude_readers_state *ves)
{
struct vm_area_struct *vma = ves->vma;
unsigned int tgt_refcnt = get_target_refcnt(ves);
int err = 0;
mmap_assert_write_locked(vma->vm_mm);
if (!refcount_add_not_zero(VM_REFCNT_EXCLUDE_READERS_FLAG, &vma->vm_refcnt)) {
ves->detached = true;
return 0;
}
__vma_lockdep_acquire_exclusive(vma);
err = rcuwait_wait_event(&vma->vm_mm->vma_writer_wait,
refcount_read(&vma->vm_refcnt) == tgt_refcnt,
ves->state);
if (err) {
__vma_end_exclude_readers(ves);
return err;
}
__vma_lockdep_stat_mark_acquired(vma);
ves->exclusive = true;
return 0;
}
int __vma_start_write(struct vm_area_struct *vma, int state)
{
const unsigned int mm_lock_seq = __vma_raw_mm_seqnum(vma);
struct vma_exclude_readers_state ves = {
.vma = vma,
.state = state,
};
int err;
err = __vma_start_exclude_readers(&ves);
if (err) {
WARN_ON_ONCE(ves.detached);
return err;
}
WRITE_ONCE(vma->vm_lock_seq, mm_lock_seq);
if (ves.exclusive) {
__vma_end_exclude_readers(&ves);
WARN_ON_ONCE(ves.detached);
}
return 0;
}
EXPORT_SYMBOL_GPL(__vma_start_write);
void __vma_exclude_readers_for_detach(struct vm_area_struct *vma)
{
struct vma_exclude_readers_state ves = {
.vma = vma,
.state = TASK_UNINTERRUPTIBLE,
.detaching = true,
};
int err;
err = __vma_start_exclude_readers(&ves);
if (!err && ves.exclusive) {
__vma_end_exclude_readers(&ves);
}
WARN_ON_ONCE(!ves.detached);
}
static bool vma_seqcount_is_write_locked(unsigned int vma_seq, unsigned int mm_seq)
{
if (vma_seq != mm_seq)
return false;
return vma_seq & 1;
}
static struct vm_area_struct *vma_start_read(struct mm_struct *mm,
struct vm_area_struct *vma)
{
unsigned int vma_seq;
unsigned int mm_seq;
struct mm_struct *other_mm;
int oldcnt;
RCU_LOCKDEP_WARN(!rcu_read_lock_held(), "no rcu lock held");
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 (unlikely(!__refcount_inc_not_zero_limited_acquire(&vma->vm_refcnt, &oldcnt,
VM_REFCNT_LIMIT))) {
vma = oldcnt ? NULL : ERR_PTR(-EAGAIN);
goto err;
}
__vma_lockdep_acquire_read(vma);
if (unlikely(vma->vm_mm != mm))
goto err_unstable;
vma_seq = smp_load_acquire(&vma->vm_lock_seq);
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 vma;
err:
rcu_read_unlock();
return vma;
err_unstable:
other_mm = vma->vm_mm;
rcu_read_unlock();
mmgrab(other_mm);
vma_refcount_put(vma);
mmdrop(other_mm);
return NULL;
}
struct vm_area_struct *lock_vma_under_rcu(struct mm_struct *mm,
unsigned long address)
{
MA_STATE(mas, &mm->mm_mt, address, address);
struct vm_area_struct *vma;
if (!IS_ENABLED(CONFIG_MMU))
return NULL;
retry:
rcu_read_lock();
vma = mas_walk(&mas);
if (!vma) {
rcu_read_unlock();
goto inval;
}
vma = vma_start_read(mm, vma);
if (IS_ERR_OR_NULL(vma)) {
if (PTR_ERR(vma) == -EAGAIN) {
count_vm_vma_lock_event(VMA_LOCK_MISS);
mas_set(&mas, address);
goto retry;
}
goto inval;
}
rcu_read_unlock();
if (unlikely(address < vma->vm_start || address >= vma->vm_end)) {
vma_end_read(vma);
goto inval;
}
return vma;
inval:
count_vm_vma_lock_event(VMA_LOCK_ABORT);
return NULL;
}
struct vm_area_struct *vma_start_read_unlocked(struct mm_struct *mm,
unsigned long address)
{
struct vm_area_struct *vma;
vma = lock_vma_under_rcu(mm, address);
if (vma)
return vma;
mmap_read_lock(mm);
vma = vma_lookup(mm, address);
if (vma && !vma_start_read_locked(vma))
vma = NULL;
mmap_read_unlock(mm);
return vma;
}
static struct vm_area_struct *lock_next_vma_under_mmap_lock(struct mm_struct *mm,
struct vma_iterator *vmi,
unsigned long from_addr)
{
struct vm_area_struct *vma;
int ret;
ret = mmap_read_lock_killable(mm);
if (ret)
return ERR_PTR(ret);
vma_iter_set(vmi, from_addr);
vma = vma_next(vmi);
if (vma) {
if (unlikely(!vma_start_read_locked(vma)))
vma = ERR_PTR(-EAGAIN);
}
mmap_read_unlock(mm);
return vma;
}
static bool vma_range_is_empty(struct mm_struct *mm, unsigned long start,
unsigned long last)
{
VMA_ITERATOR(vmi, mm, start);
return !vma_iter_load(&vmi) && vma_iter_end(&vmi) - 1 >= last;
}
static bool vma_rcu_iter_maybe_changed(const struct vma_rcu_iter *rvmi)
{
return (rvmi->mm_wr_seq & 1) ||
mmap_lock_speculate_retry(rvmi->mm, rvmi->mm_wr_seq);
}
static bool vma_rcu_iter_may_have_skipped(const struct vma_rcu_iter *rvmi,
unsigned long from, unsigned long to)
{
return vma_rcu_iter_maybe_changed(rvmi) &&
!vma_range_is_empty(rvmi->mm, from, to);
}
struct vm_area_struct *lock_next_vma(struct vma_rcu_iter *rvmi,
unsigned long from_addr)
{
struct mm_struct *mm = rvmi->mm;
struct vma_iterator *vmi = rvmi->vmi;
struct vm_area_struct *vma;
RCU_LOCKDEP_WARN(!rcu_read_lock_held(), "no rcu read lock held");
retry:
if (vma_rcu_iter_maybe_changed(rvmi)) {
vma_iter_set(vmi, from_addr);
mmap_lock_speculate_try_begin(mm, &rvmi->mm_wr_seq);
}
vma = vma_next(vmi);
if (!vma) {
if (!vma_rcu_iter_may_have_skipped(rvmi, from_addr, ULONG_MAX))
return NULL;
rcu_read_unlock();
goto fallback;
}
vma = vma_start_read(mm, vma);
if (IS_ERR_OR_NULL(vma)) {
if (PTR_ERR(vma) == -EAGAIN) {
rcu_read_lock();
vma_iter_set(vmi, from_addr);
goto retry;
}
goto fallback;
}
if (unlikely(from_addr >= vma->vm_end))
goto fallback_unlock;
if (from_addr < vma->vm_start &&
vma_rcu_iter_may_have_skipped(rvmi, from_addr, vma->vm_start - 1))
goto fallback_unlock;
return vma;
fallback_unlock:
rcu_read_unlock();
vma_end_read(vma);
fallback:
vma = lock_next_vma_under_mmap_lock(mm, vmi, from_addr);
rcu_read_lock();
vma_iter_set(vmi, IS_ERR_OR_NULL(vma) ? from_addr : vma->vm_end);
return vma;
}
void __mm_reset_vma_seqcounts(struct mm_struct *mm)
{
struct vm_area_struct *vma;
VMA_ITERATOR(vmi, mm, 0);
mmap_assert_write_locked(mm);
VM_WARN_ON_ONCE(mm->mm_lock_seq.sequence);
for_each_vma(vmi, vma)
WRITE_ONCE(vma->vm_lock_seq, 0);
}
EXPORT_SYMBOL(__mm_reset_vma_seqcounts);
#ifdef CONFIG_LOCK_MM_AND_FIND_VMA
#include <linux/extable.h>
static inline bool get_mmap_lock_carefully(struct mm_struct *mm, struct pt_regs *regs)
{
if (likely(mmap_read_trylock(mm)))
return true;
if (regs && !user_mode(regs)) {
unsigned long ip = exception_ip(regs);
if (!search_exception_tables(ip))
return false;
}
return !mmap_read_lock_killable(mm);
}
static inline bool mmap_upgrade_trylock(struct mm_struct *mm)
{
return false;
}
static inline bool upgrade_mmap_lock_carefully(struct mm_struct *mm, struct pt_regs *regs)
{
mmap_read_unlock(mm);
if (regs && !user_mode(regs)) {
unsigned long ip = exception_ip(regs);
if (!search_exception_tables(ip))
return false;
}
return !mmap_write_lock_killable(mm);
}
struct vm_area_struct *lock_mm_and_find_vma(struct mm_struct *mm,
unsigned long addr, struct pt_regs *regs)
{
struct vm_area_struct *vma;
if (!get_mmap_lock_carefully(mm, regs))
return NULL;
vma = find_vma(mm, addr);
if (likely(vma && (vma->vm_start <= addr)))
return vma;
if (!vma || !(vma->vm_flags & VM_GROWSDOWN)) {
mmap_read_unlock(mm);
return NULL;
}
if (!mmap_upgrade_trylock(mm)) {
if (!upgrade_mmap_lock_carefully(mm, regs))
return NULL;
vma = find_vma(mm, addr);
if (!vma)
goto fail;
if (vma->vm_start <= addr)
goto success;
if (!(vma->vm_flags & VM_GROWSDOWN))
goto fail;
}
if (expand_stack_locked(vma, addr))
goto fail;
success:
mmap_write_downgrade(mm);
return vma;
fail:
mmap_write_unlock(mm);
return NULL;
}
#endif /* CONFIG_LOCK_MM_AND_FIND_VMA */