blk-mq: fix leak of hctx->ctx_map
[linux-2.6/btrfs-unstable.git] / mm / vmacache.c
blobd4224b397c0e4e4492fa135c3c5ee9b224872c07
1 /*
2 * Copyright (C) 2014 Davidlohr Bueso.
3 */
4 #include <linux/sched.h>
5 #include <linux/mm.h>
6 #include <linux/vmacache.h>
8 /*
9 * Flush vma caches for threads that share a given mm.
11 * The operation is safe because the caller holds the mmap_sem
12 * exclusively and other threads accessing the vma cache will
13 * have mmap_sem held at least for read, so no extra locking
14 * is required to maintain the vma cache.
16 void vmacache_flush_all(struct mm_struct *mm)
18 struct task_struct *g, *p;
20 rcu_read_lock();
21 for_each_process_thread(g, p) {
23 * Only flush the vmacache pointers as the
24 * mm seqnum is already set and curr's will
25 * be set upon invalidation when the next
26 * lookup is done.
28 if (mm == p->mm)
29 vmacache_flush(p);
31 rcu_read_unlock();
35 * This task may be accessing a foreign mm via (for example)
36 * get_user_pages()->find_vma(). The vmacache is task-local and this
37 * task's vmacache pertains to a different mm (ie, its own). There is
38 * nothing we can do here.
40 * Also handle the case where a kernel thread has adopted this mm via use_mm().
41 * That kernel thread's vmacache is not applicable to this mm.
43 static bool vmacache_valid_mm(struct mm_struct *mm)
45 return current->mm == mm && !(current->flags & PF_KTHREAD);
48 void vmacache_update(unsigned long addr, struct vm_area_struct *newvma)
50 if (vmacache_valid_mm(newvma->vm_mm))
51 current->vmacache[VMACACHE_HASH(addr)] = newvma;
54 static bool vmacache_valid(struct mm_struct *mm)
56 struct task_struct *curr;
58 if (!vmacache_valid_mm(mm))
59 return false;
61 curr = current;
62 if (mm->vmacache_seqnum != curr->vmacache_seqnum) {
64 * First attempt will always be invalid, initialize
65 * the new cache for this task here.
67 curr->vmacache_seqnum = mm->vmacache_seqnum;
68 vmacache_flush(curr);
69 return false;
71 return true;
74 struct vm_area_struct *vmacache_find(struct mm_struct *mm, unsigned long addr)
76 int i;
78 if (!vmacache_valid(mm))
79 return NULL;
81 for (i = 0; i < VMACACHE_SIZE; i++) {
82 struct vm_area_struct *vma = current->vmacache[i];
84 if (vma && vma->vm_start <= addr && vma->vm_end > addr) {
85 BUG_ON(vma->vm_mm != mm);
86 return vma;
90 return NULL;
93 #ifndef CONFIG_MMU
94 struct vm_area_struct *vmacache_find_exact(struct mm_struct *mm,
95 unsigned long start,
96 unsigned long end)
98 int i;
100 if (!vmacache_valid(mm))
101 return NULL;
103 for (i = 0; i < VMACACHE_SIZE; i++) {
104 struct vm_area_struct *vma = current->vmacache[i];
106 if (vma && vma->vm_start == start && vma->vm_end == end)
107 return vma;
110 return NULL;
112 #endif