]>
Commit | Line | Data |
---|---|---|
b2441318 | 1 | // SPDX-License-Identifier: GPL-2.0 |
615d6e87 DB |
2 | /* |
3 | * Copyright (C) 2014 Davidlohr Bueso. | |
4 | */ | |
3f07c014 | 5 | #include <linux/sched/signal.h> |
9164bb4a | 6 | #include <linux/sched/task.h> |
615d6e87 DB |
7 | #include <linux/mm.h> |
8 | #include <linux/vmacache.h> | |
9 | ||
615d6e87 DB |
10 | /* |
11 | * This task may be accessing a foreign mm via (for example) | |
12 | * get_user_pages()->find_vma(). The vmacache is task-local and this | |
13 | * task's vmacache pertains to a different mm (ie, its own). There is | |
14 | * nothing we can do here. | |
15 | * | |
16 | * Also handle the case where a kernel thread has adopted this mm via use_mm(). | |
17 | * That kernel thread's vmacache is not applicable to this mm. | |
18 | */ | |
a2c1aad3 | 19 | static inline bool vmacache_valid_mm(struct mm_struct *mm) |
615d6e87 DB |
20 | { |
21 | return current->mm == mm && !(current->flags & PF_KTHREAD); | |
22 | } | |
23 | ||
24 | void vmacache_update(unsigned long addr, struct vm_area_struct *newvma) | |
25 | { | |
26 | if (vmacache_valid_mm(newvma->vm_mm)) | |
314ff785 | 27 | current->vmacache.vmas[VMACACHE_HASH(addr)] = newvma; |
615d6e87 DB |
28 | } |
29 | ||
30 | static bool vmacache_valid(struct mm_struct *mm) | |
31 | { | |
32 | struct task_struct *curr; | |
33 | ||
34 | if (!vmacache_valid_mm(mm)) | |
35 | return false; | |
36 | ||
37 | curr = current; | |
314ff785 | 38 | if (mm->vmacache_seqnum != curr->vmacache.seqnum) { |
615d6e87 DB |
39 | /* |
40 | * First attempt will always be invalid, initialize | |
41 | * the new cache for this task here. | |
42 | */ | |
314ff785 | 43 | curr->vmacache.seqnum = mm->vmacache_seqnum; |
615d6e87 DB |
44 | vmacache_flush(curr); |
45 | return false; | |
46 | } | |
47 | return true; | |
48 | } | |
49 | ||
50 | struct vm_area_struct *vmacache_find(struct mm_struct *mm, unsigned long addr) | |
51 | { | |
52 | int i; | |
53 | ||
131ddc5c AD |
54 | count_vm_vmacache_event(VMACACHE_FIND_CALLS); |
55 | ||
615d6e87 DB |
56 | if (!vmacache_valid(mm)) |
57 | return NULL; | |
58 | ||
59 | for (i = 0; i < VMACACHE_SIZE; i++) { | |
314ff785 | 60 | struct vm_area_struct *vma = current->vmacache.vmas[i]; |
615d6e87 | 61 | |
50f5aa8a LT |
62 | if (!vma) |
63 | continue; | |
64 | if (WARN_ON_ONCE(vma->vm_mm != mm)) | |
65 | break; | |
4f115147 DB |
66 | if (vma->vm_start <= addr && vma->vm_end > addr) { |
67 | count_vm_vmacache_event(VMACACHE_FIND_HITS); | |
615d6e87 | 68 | return vma; |
4f115147 | 69 | } |
615d6e87 DB |
70 | } |
71 | ||
72 | return NULL; | |
73 | } | |
74 | ||
75 | #ifndef CONFIG_MMU | |
76 | struct vm_area_struct *vmacache_find_exact(struct mm_struct *mm, | |
77 | unsigned long start, | |
78 | unsigned long end) | |
79 | { | |
80 | int i; | |
81 | ||
131ddc5c AD |
82 | count_vm_vmacache_event(VMACACHE_FIND_CALLS); |
83 | ||
615d6e87 DB |
84 | if (!vmacache_valid(mm)) |
85 | return NULL; | |
86 | ||
87 | for (i = 0; i < VMACACHE_SIZE; i++) { | |
314ff785 | 88 | struct vm_area_struct *vma = current->vmacache.vmas[i]; |
615d6e87 | 89 | |
4f115147 DB |
90 | if (vma && vma->vm_start == start && vma->vm_end == end) { |
91 | count_vm_vmacache_event(VMACACHE_FIND_HITS); | |
615d6e87 | 92 | return vma; |
4f115147 | 93 | } |
615d6e87 DB |
94 | } |
95 | ||
96 | return NULL; | |
97 | } | |
98 | #endif |