Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Davidlohr Bueso | 615d6e8 | 2014-04-07 15:37:25 -0700 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2014 Davidlohr Bueso. |
| 4 | */ |
Ingo Molnar | 3f07c01 | 2017-02-08 18:51:30 +0100 | [diff] [blame] | 5 | #include <linux/sched/signal.h> |
Ingo Molnar | 9164bb4 | 2017-02-04 01:20:53 +0100 | [diff] [blame] | 6 | #include <linux/sched/task.h> |
Davidlohr Bueso | 615d6e8 | 2014-04-07 15:37:25 -0700 | [diff] [blame] | 7 | #include <linux/mm.h> |
| 8 | #include <linux/vmacache.h> |
David Rientjes | ddbf369 | 2018-08-17 15:49:58 -0700 | [diff] [blame] | 9 | |
| 10 | /* |
| 11 | * Hash based on the pmd of addr if configured with MMU, which provides a good |
| 12 | * hit rate for workloads with spatial locality. Otherwise, use pages. |
| 13 | */ |
| 14 | #ifdef CONFIG_MMU |
| 15 | #define VMACACHE_SHIFT PMD_SHIFT |
| 16 | #else |
| 17 | #define VMACACHE_SHIFT PAGE_SHIFT |
| 18 | #endif |
| 19 | #define VMACACHE_HASH(addr) ((addr >> VMACACHE_SHIFT) & VMACACHE_MASK) |
Davidlohr Bueso | 615d6e8 | 2014-04-07 15:37:25 -0700 | [diff] [blame] | 20 | |
| 21 | /* |
Davidlohr Bueso | 615d6e8 | 2014-04-07 15:37:25 -0700 | [diff] [blame] | 22 | * This task may be accessing a foreign mm via (for example) |
| 23 | * get_user_pages()->find_vma(). The vmacache is task-local and this |
| 24 | * task's vmacache pertains to a different mm (ie, its own). There is |
| 25 | * nothing we can do here. |
| 26 | * |
Christoph Hellwig | f5678e7 | 2020-06-10 18:42:06 -0700 | [diff] [blame] | 27 | * Also handle the case where a kernel thread has adopted this mm via |
| 28 | * kthread_use_mm(). That kernel thread's vmacache is not applicable to this mm. |
Davidlohr Bueso | 615d6e8 | 2014-04-07 15:37:25 -0700 | [diff] [blame] | 29 | */ |
Davidlohr Bueso | a2c1aad | 2015-11-05 18:48:52 -0800 | [diff] [blame] | 30 | static inline bool vmacache_valid_mm(struct mm_struct *mm) |
Davidlohr Bueso | 615d6e8 | 2014-04-07 15:37:25 -0700 | [diff] [blame] | 31 | { |
| 32 | return current->mm == mm && !(current->flags & PF_KTHREAD); |
| 33 | } |
| 34 | |
| 35 | void vmacache_update(unsigned long addr, struct vm_area_struct *newvma) |
| 36 | { |
| 37 | if (vmacache_valid_mm(newvma->vm_mm)) |
Ingo Molnar | 314ff78 | 2017-02-03 11:03:31 +0100 | [diff] [blame] | 38 | current->vmacache.vmas[VMACACHE_HASH(addr)] = newvma; |
Davidlohr Bueso | 615d6e8 | 2014-04-07 15:37:25 -0700 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | static bool vmacache_valid(struct mm_struct *mm) |
| 42 | { |
| 43 | struct task_struct *curr; |
| 44 | |
| 45 | if (!vmacache_valid_mm(mm)) |
| 46 | return false; |
| 47 | |
| 48 | curr = current; |
Ingo Molnar | 314ff78 | 2017-02-03 11:03:31 +0100 | [diff] [blame] | 49 | if (mm->vmacache_seqnum != curr->vmacache.seqnum) { |
Davidlohr Bueso | 615d6e8 | 2014-04-07 15:37:25 -0700 | [diff] [blame] | 50 | /* |
| 51 | * First attempt will always be invalid, initialize |
| 52 | * the new cache for this task here. |
| 53 | */ |
Ingo Molnar | 314ff78 | 2017-02-03 11:03:31 +0100 | [diff] [blame] | 54 | curr->vmacache.seqnum = mm->vmacache_seqnum; |
Davidlohr Bueso | 615d6e8 | 2014-04-07 15:37:25 -0700 | [diff] [blame] | 55 | vmacache_flush(curr); |
| 56 | return false; |
| 57 | } |
| 58 | return true; |
| 59 | } |
| 60 | |
| 61 | struct vm_area_struct *vmacache_find(struct mm_struct *mm, unsigned long addr) |
| 62 | { |
David Rientjes | ddbf369 | 2018-08-17 15:49:58 -0700 | [diff] [blame] | 63 | int idx = VMACACHE_HASH(addr); |
Davidlohr Bueso | 615d6e8 | 2014-04-07 15:37:25 -0700 | [diff] [blame] | 64 | int i; |
| 65 | |
Alexey Dobriyan | 131ddc5 | 2016-10-07 16:58:39 -0700 | [diff] [blame] | 66 | count_vm_vmacache_event(VMACACHE_FIND_CALLS); |
| 67 | |
Davidlohr Bueso | 615d6e8 | 2014-04-07 15:37:25 -0700 | [diff] [blame] | 68 | if (!vmacache_valid(mm)) |
| 69 | return NULL; |
| 70 | |
| 71 | for (i = 0; i < VMACACHE_SIZE; i++) { |
David Rientjes | ddbf369 | 2018-08-17 15:49:58 -0700 | [diff] [blame] | 72 | struct vm_area_struct *vma = current->vmacache.vmas[idx]; |
Davidlohr Bueso | 615d6e8 | 2014-04-07 15:37:25 -0700 | [diff] [blame] | 73 | |
David Rientjes | ddbf369 | 2018-08-17 15:49:58 -0700 | [diff] [blame] | 74 | if (vma) { |
| 75 | #ifdef CONFIG_DEBUG_VM_VMACACHE |
| 76 | if (WARN_ON_ONCE(vma->vm_mm != mm)) |
| 77 | break; |
| 78 | #endif |
| 79 | if (vma->vm_start <= addr && vma->vm_end > addr) { |
| 80 | count_vm_vmacache_event(VMACACHE_FIND_HITS); |
| 81 | return vma; |
| 82 | } |
Davidlohr Bueso | 4f11514 | 2014-06-04 16:06:46 -0700 | [diff] [blame] | 83 | } |
David Rientjes | ddbf369 | 2018-08-17 15:49:58 -0700 | [diff] [blame] | 84 | if (++idx == VMACACHE_SIZE) |
| 85 | idx = 0; |
Davidlohr Bueso | 615d6e8 | 2014-04-07 15:37:25 -0700 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | return NULL; |
| 89 | } |
| 90 | |
| 91 | #ifndef CONFIG_MMU |
| 92 | struct vm_area_struct *vmacache_find_exact(struct mm_struct *mm, |
| 93 | unsigned long start, |
| 94 | unsigned long end) |
| 95 | { |
David Rientjes | ddbf369 | 2018-08-17 15:49:58 -0700 | [diff] [blame] | 96 | int idx = VMACACHE_HASH(start); |
Davidlohr Bueso | 615d6e8 | 2014-04-07 15:37:25 -0700 | [diff] [blame] | 97 | int i; |
| 98 | |
Alexey Dobriyan | 131ddc5 | 2016-10-07 16:58:39 -0700 | [diff] [blame] | 99 | count_vm_vmacache_event(VMACACHE_FIND_CALLS); |
| 100 | |
Davidlohr Bueso | 615d6e8 | 2014-04-07 15:37:25 -0700 | [diff] [blame] | 101 | if (!vmacache_valid(mm)) |
| 102 | return NULL; |
| 103 | |
| 104 | for (i = 0; i < VMACACHE_SIZE; i++) { |
David Rientjes | ddbf369 | 2018-08-17 15:49:58 -0700 | [diff] [blame] | 105 | struct vm_area_struct *vma = current->vmacache.vmas[idx]; |
Davidlohr Bueso | 615d6e8 | 2014-04-07 15:37:25 -0700 | [diff] [blame] | 106 | |
Davidlohr Bueso | 4f11514 | 2014-06-04 16:06:46 -0700 | [diff] [blame] | 107 | if (vma && vma->vm_start == start && vma->vm_end == end) { |
| 108 | count_vm_vmacache_event(VMACACHE_FIND_HITS); |
Davidlohr Bueso | 615d6e8 | 2014-04-07 15:37:25 -0700 | [diff] [blame] | 109 | return vma; |
Davidlohr Bueso | 4f11514 | 2014-06-04 16:06:46 -0700 | [diff] [blame] | 110 | } |
David Rientjes | ddbf369 | 2018-08-17 15:49:58 -0700 | [diff] [blame] | 111 | if (++idx == VMACACHE_SIZE) |
| 112 | idx = 0; |
Davidlohr Bueso | 615d6e8 | 2014-04-07 15:37:25 -0700 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | return NULL; |
| 116 | } |
| 117 | #endif |