blob: 658ed3b3e38d32a2c210d43163554cb23e6ac173 [file] [log] [blame]
Davidlohr Bueso615d6e82014-04-07 15:37:25 -07001/*
2 * Copyright (C) 2014 Davidlohr Bueso.
3 */
4#include <linux/sched.h>
5#include <linux/mm.h>
6#include <linux/vmacache.h>
7
8/*
9 * Flush vma caches for threads that share a given mm.
10 *
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.
15 */
16void vmacache_flush_all(struct mm_struct *mm)
17{
18 struct task_struct *g, *p;
19
20 rcu_read_lock();
21 for_each_process_thread(g, p) {
22 /*
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.
27 */
28 if (mm == p->mm)
29 vmacache_flush(p);
30 }
31 rcu_read_unlock();
32}
33
34/*
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.
39 *
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.
42 */
43static bool vmacache_valid_mm(struct mm_struct *mm)
44{
45 return current->mm == mm && !(current->flags & PF_KTHREAD);
46}
47
48void vmacache_update(unsigned long addr, struct vm_area_struct *newvma)
49{
50 if (vmacache_valid_mm(newvma->vm_mm))
51 current->vmacache[VMACACHE_HASH(addr)] = newvma;
52}
53
54static bool vmacache_valid(struct mm_struct *mm)
55{
56 struct task_struct *curr;
57
58 if (!vmacache_valid_mm(mm))
59 return false;
60
61 curr = current;
62 if (mm->vmacache_seqnum != curr->vmacache_seqnum) {
63 /*
64 * First attempt will always be invalid, initialize
65 * the new cache for this task here.
66 */
67 curr->vmacache_seqnum = mm->vmacache_seqnum;
68 vmacache_flush(curr);
69 return false;
70 }
71 return true;
72}
73
74struct vm_area_struct *vmacache_find(struct mm_struct *mm, unsigned long addr)
75{
76 int i;
77
78 if (!vmacache_valid(mm))
79 return NULL;
80
Davidlohr Bueso4f115142014-06-04 16:06:46 -070081 count_vm_vmacache_event(VMACACHE_FIND_CALLS);
82
Davidlohr Bueso615d6e82014-04-07 15:37:25 -070083 for (i = 0; i < VMACACHE_SIZE; i++) {
84 struct vm_area_struct *vma = current->vmacache[i];
85
Linus Torvalds50f5aa82014-04-28 14:24:09 -070086 if (!vma)
87 continue;
88 if (WARN_ON_ONCE(vma->vm_mm != mm))
89 break;
Davidlohr Bueso4f115142014-06-04 16:06:46 -070090 if (vma->vm_start <= addr && vma->vm_end > addr) {
91 count_vm_vmacache_event(VMACACHE_FIND_HITS);
Davidlohr Bueso615d6e82014-04-07 15:37:25 -070092 return vma;
Davidlohr Bueso4f115142014-06-04 16:06:46 -070093 }
Davidlohr Bueso615d6e82014-04-07 15:37:25 -070094 }
95
96 return NULL;
97}
98
99#ifndef CONFIG_MMU
100struct vm_area_struct *vmacache_find_exact(struct mm_struct *mm,
101 unsigned long start,
102 unsigned long end)
103{
104 int i;
105
106 if (!vmacache_valid(mm))
107 return NULL;
108
Davidlohr Bueso4f115142014-06-04 16:06:46 -0700109 count_vm_vmacache_event(VMACACHE_FIND_CALLS);
110
Davidlohr Bueso615d6e82014-04-07 15:37:25 -0700111 for (i = 0; i < VMACACHE_SIZE; i++) {
112 struct vm_area_struct *vma = current->vmacache[i];
113
Davidlohr Bueso4f115142014-06-04 16:06:46 -0700114 if (vma && vma->vm_start == start && vma->vm_end == end) {
115 count_vm_vmacache_event(VMACACHE_FIND_HITS);
Davidlohr Bueso615d6e82014-04-07 15:37:25 -0700116 return vma;
Davidlohr Bueso4f115142014-06-04 16:06:46 -0700117 }
Davidlohr Bueso615d6e82014-04-07 15:37:25 -0700118 }
119
120 return NULL;
121}
122#endif