2 * Copyright (C) 2014 Davidlohr Bueso.
4 #include <linux/sched.h>
6 #include <linux/vmacache.h>
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 count_vm_vmacache_event(VMACACHE_FULL_FLUSHES
);
23 * Single threaded tasks need not iterate the entire
24 * list of process. We can avoid the flushing as well
25 * since the mm's seqnum was increased and don't have
26 * to worry about other threads' seqnum. Current's
27 * flush will occur upon the next lookup.
29 if (atomic_read(&mm
->mm_users
) == 1)
33 for_each_process_thread(g
, p
) {
35 * Only flush the vmacache pointers as the
36 * mm seqnum is already set and curr's will
37 * be set upon invalidation when the next
47 * This task may be accessing a foreign mm via (for example)
48 * get_user_pages()->find_vma(). The vmacache is task-local and this
49 * task's vmacache pertains to a different mm (ie, its own). There is
50 * nothing we can do here.
52 * Also handle the case where a kernel thread has adopted this mm via use_mm().
53 * That kernel thread's vmacache is not applicable to this mm.
55 static bool vmacache_valid_mm(struct mm_struct
*mm
)
57 return current
->mm
== mm
&& !(current
->flags
& PF_KTHREAD
);
60 void vmacache_update(unsigned long addr
, struct vm_area_struct
*newvma
)
62 if (vmacache_valid_mm(newvma
->vm_mm
))
63 current
->vmacache
[VMACACHE_HASH(addr
)] = newvma
;
66 static bool vmacache_valid(struct mm_struct
*mm
)
68 struct task_struct
*curr
;
70 if (!vmacache_valid_mm(mm
))
74 if (mm
->vmacache_seqnum
!= curr
->vmacache_seqnum
) {
76 * First attempt will always be invalid, initialize
77 * the new cache for this task here.
79 curr
->vmacache_seqnum
= mm
->vmacache_seqnum
;
86 struct vm_area_struct
*vmacache_find(struct mm_struct
*mm
, unsigned long addr
)
90 if (!vmacache_valid(mm
))
93 count_vm_vmacache_event(VMACACHE_FIND_CALLS
);
95 for (i
= 0; i
< VMACACHE_SIZE
; i
++) {
96 struct vm_area_struct
*vma
= current
->vmacache
[i
];
100 if (WARN_ON_ONCE(vma
->vm_mm
!= mm
))
102 if (vma
->vm_start
<= addr
&& vma
->vm_end
> addr
) {
103 count_vm_vmacache_event(VMACACHE_FIND_HITS
);
112 struct vm_area_struct
*vmacache_find_exact(struct mm_struct
*mm
,
118 if (!vmacache_valid(mm
))
121 count_vm_vmacache_event(VMACACHE_FIND_CALLS
);
123 for (i
= 0; i
< VMACACHE_SIZE
; i
++) {
124 struct vm_area_struct
*vma
= current
->vmacache
[i
];
126 if (vma
&& vma
->vm_start
== start
&& vma
->vm_end
== end
) {
127 count_vm_vmacache_event(VMACACHE_FIND_HITS
);