1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2014 Davidlohr Bueso.
5 #include <linux/sched/signal.h>
6 #include <linux/sched/task.h>
8 #include <linux/vmacache.h>
9 #include <asm/pgtable.h>
12 * Hash based on the pmd of addr if configured with MMU, which provides a good
13 * hit rate for workloads with spatial locality. Otherwise, use pages.
16 #define VMACACHE_SHIFT PMD_SHIFT
18 #define VMACACHE_SHIFT PAGE_SHIFT
20 #define VMACACHE_HASH(addr) ((addr >> VMACACHE_SHIFT) & VMACACHE_MASK)
23 * Flush vma caches for threads that share a given mm.
25 * The operation is safe because the caller holds the mmap_sem
26 * exclusively and other threads accessing the vma cache will
27 * have mmap_sem held at least for read, so no extra locking
28 * is required to maintain the vma cache.
30 void vmacache_flush_all(struct mm_struct
*mm
)
32 struct task_struct
*g
, *p
;
34 count_vm_vmacache_event(VMACACHE_FULL_FLUSHES
);
37 * Single threaded tasks need not iterate the entire
38 * list of process. We can avoid the flushing as well
39 * since the mm's seqnum was increased and don't have
40 * to worry about other threads' seqnum. Current's
41 * flush will occur upon the next lookup.
43 if (atomic_read(&mm
->mm_users
) == 1)
47 for_each_process_thread(g
, p
) {
49 * Only flush the vmacache pointers as the
50 * mm seqnum is already set and curr's will
51 * be set upon invalidation when the next
61 * This task may be accessing a foreign mm via (for example)
62 * get_user_pages()->find_vma(). The vmacache is task-local and this
63 * task's vmacache pertains to a different mm (ie, its own). There is
64 * nothing we can do here.
66 * Also handle the case where a kernel thread has adopted this mm via use_mm().
67 * That kernel thread's vmacache is not applicable to this mm.
69 static inline bool vmacache_valid_mm(struct mm_struct
*mm
)
71 return current
->mm
== mm
&& !(current
->flags
& PF_KTHREAD
);
74 void vmacache_update(unsigned long addr
, struct vm_area_struct
*newvma
)
76 if (vmacache_valid_mm(newvma
->vm_mm
))
77 current
->vmacache
.vmas
[VMACACHE_HASH(addr
)] = newvma
;
80 static bool vmacache_valid(struct mm_struct
*mm
)
82 struct task_struct
*curr
;
84 if (!vmacache_valid_mm(mm
))
88 if (mm
->vmacache_seqnum
!= curr
->vmacache
.seqnum
) {
90 * First attempt will always be invalid, initialize
91 * the new cache for this task here.
93 curr
->vmacache
.seqnum
= mm
->vmacache_seqnum
;
100 struct vm_area_struct
*vmacache_find(struct mm_struct
*mm
, unsigned long addr
)
102 int idx
= VMACACHE_HASH(addr
);
105 count_vm_vmacache_event(VMACACHE_FIND_CALLS
);
107 if (!vmacache_valid(mm
))
110 for (i
= 0; i
< VMACACHE_SIZE
; i
++) {
111 struct vm_area_struct
*vma
= current
->vmacache
.vmas
[idx
];
114 #ifdef CONFIG_DEBUG_VM_VMACACHE
115 if (WARN_ON_ONCE(vma
->vm_mm
!= mm
))
118 if (vma
->vm_start
<= addr
&& vma
->vm_end
> addr
) {
119 count_vm_vmacache_event(VMACACHE_FIND_HITS
);
123 if (++idx
== VMACACHE_SIZE
)
131 struct vm_area_struct
*vmacache_find_exact(struct mm_struct
*mm
,
135 int idx
= VMACACHE_HASH(start
);
138 count_vm_vmacache_event(VMACACHE_FIND_CALLS
);
140 if (!vmacache_valid(mm
))
143 for (i
= 0; i
< VMACACHE_SIZE
; i
++) {
144 struct vm_area_struct
*vma
= current
->vmacache
.vmas
[idx
];
146 if (vma
&& vma
->vm_start
== start
&& vma
->vm_end
== end
) {
147 count_vm_vmacache_event(VMACACHE_FIND_HITS
);
150 if (++idx
== VMACACHE_SIZE
)