1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * MMU context allocation for 64-bit kernels.
4 *
5 * Copyright (C) 2004 Anton Blanchard, IBM Corp. <anton@samba.org>
6 */
7
8 #include <linux/sched.h>
9 #include <linux/kernel.h>
10 #include <linux/errno.h>
11 #include <linux/string.h>
12 #include <linux/types.h>
13 #include <linux/mm.h>
14 #include <linux/pkeys.h>
15 #include <linux/spinlock.h>
16 #include <linux/idr.h>
17 #include <linux/export.h>
18 #include <linux/gfp.h>
19 #include <linux/slab.h>
20 #include <linux/cpu.h>
21
22 #include <asm/mmu_context.h>
23 #include <asm/pgalloc.h>
24
25 #include "internal.h"
26
27 static DEFINE_IDA(mmu_context_ida);
28
alloc_context_id(int min_id,int max_id)29 static int alloc_context_id(int min_id, int max_id)
30 {
31 return ida_alloc_range(&mmu_context_ida, min_id, max_id, GFP_KERNEL);
32 }
33
34 #ifdef CONFIG_PPC_64S_HASH_MMU
hash__reserve_context_id(int id)35 void __init hash__reserve_context_id(int id)
36 {
37 int result = ida_alloc_range(&mmu_context_ida, id, id, GFP_KERNEL);
38
39 WARN(result != id, "mmu: Failed to reserve context id %d (rc %d)\n", id, result);
40 }
41
hash__alloc_context_id(void)42 int hash__alloc_context_id(void)
43 {
44 unsigned long max;
45
46 if (mmu_has_feature(MMU_FTR_68_BIT_VA))
47 max = MAX_USER_CONTEXT;
48 else
49 max = MAX_USER_CONTEXT_65BIT_VA;
50
51 return alloc_context_id(MIN_USER_CONTEXT, max);
52 }
53 EXPORT_SYMBOL_GPL(hash__alloc_context_id);
54 #endif
55
56 #ifdef CONFIG_PPC_64S_HASH_MMU
realloc_context_ids(mm_context_t * ctx)57 static int realloc_context_ids(mm_context_t *ctx)
58 {
59 int i, id;
60
61 /*
62 * id 0 (aka. ctx->id) is special, we always allocate a new one, even if
63 * there wasn't one allocated previously (which happens in the exec
64 * case where ctx is newly allocated).
65 *
66 * We have to be a bit careful here. We must keep the existing ids in
67 * the array, so that we can test if they're non-zero to decide if we
68 * need to allocate a new one. However in case of error we must free the
69 * ids we've allocated but *not* any of the existing ones (or risk a
70 * UAF). That's why we decrement i at the start of the error handling
71 * loop, to skip the id that we just tested but couldn't reallocate.
72 */
73 for (i = 0; i < ARRAY_SIZE(ctx->extended_id); i++) {
74 if (i == 0 || ctx->extended_id[i]) {
75 id = hash__alloc_context_id();
76 if (id < 0)
77 goto error;
78
79 ctx->extended_id[i] = id;
80 }
81 }
82
83 /* The caller expects us to return id */
84 return ctx->id;
85
86 error:
87 for (i--; i >= 0; i--) {
88 if (ctx->extended_id[i])
89 ida_free(&mmu_context_ida, ctx->extended_id[i]);
90 }
91
92 return id;
93 }
94
hash__init_new_context(struct mm_struct * mm)95 static int hash__init_new_context(struct mm_struct *mm)
96 {
97 int index;
98
99 mm->context.hash_context = kmalloc_obj(struct hash_mm_context);
100 if (!mm->context.hash_context)
101 return -ENOMEM;
102
103 /*
104 * The old code would re-promote on fork, we don't do that when using
105 * slices as it could cause problem promoting slices that have been
106 * forced down to 4K.
107 *
108 * For book3s we have MMU_NO_CONTEXT set to be ~0. Hence check
109 * explicitly against context.id == 0. This ensures that we properly
110 * initialize context slice details for newly allocated mm's (which will
111 * have id == 0) and don't alter context slice inherited via fork (which
112 * will have id != 0).
113 *
114 * We should not be calling init_new_context() on init_mm. Hence a
115 * check against 0 is OK.
116 */
117 if (mm->context.id == 0) {
118 memset(mm->context.hash_context, 0, sizeof(struct hash_mm_context));
119 slice_init_new_context_exec(mm);
120 } else {
121 /* This is fork. Copy hash_context details from current->mm */
122 memcpy(mm->context.hash_context, current->mm->context.hash_context, sizeof(struct hash_mm_context));
123 #ifdef CONFIG_PPC_SUBPAGE_PROT
124 /* inherit subpage prot details if we have one. */
125 if (current->mm->context.hash_context->spt) {
126 mm->context.hash_context->spt = kmalloc_obj(struct subpage_prot_table);
127 if (!mm->context.hash_context->spt) {
128 kfree(mm->context.hash_context);
129 return -ENOMEM;
130 }
131 }
132 #endif
133 }
134
135 index = realloc_context_ids(&mm->context);
136 if (index < 0) {
137 #ifdef CONFIG_PPC_SUBPAGE_PROT
138 kfree(mm->context.hash_context->spt);
139 #endif
140 kfree(mm->context.hash_context);
141 return index;
142 }
143
144 pkey_mm_init(mm);
145 return index;
146 }
147
hash__setup_new_exec(void)148 void hash__setup_new_exec(void)
149 {
150 slice_setup_new_exec();
151 }
152 #else
hash__init_new_context(struct mm_struct * mm)153 static inline int hash__init_new_context(struct mm_struct *mm)
154 {
155 BUILD_BUG();
156 return 0;
157 }
158 #endif
159
radix__init_new_context(struct mm_struct * mm)160 static int radix__init_new_context(struct mm_struct *mm)
161 {
162 unsigned long rts_field;
163 int index, max_id;
164
165 max_id = (1 << mmu_pid_bits) - 1;
166 index = alloc_context_id(mmu_base_pid, max_id);
167 if (index < 0)
168 return index;
169
170 /*
171 * set the process table entry,
172 */
173 rts_field = radix__get_tree_size();
174 process_tb[index].prtb0 = cpu_to_be64(rts_field | __pa(mm->pgd) | RADIX_PGD_INDEX_SIZE);
175
176 /*
177 * Order the above store with subsequent update of the PID
178 * register (at which point HW can start loading/caching
179 * the entry) and the corresponding load by the MMU from
180 * the L2 cache.
181 */
182 asm volatile("ptesync;isync" : : : "memory");
183
184 #ifdef CONFIG_PPC_64S_HASH_MMU
185 mm->context.hash_context = NULL;
186 #endif
187
188 return index;
189 }
190
init_new_context(struct task_struct * tsk,struct mm_struct * mm)191 int init_new_context(struct task_struct *tsk, struct mm_struct *mm)
192 {
193 int index;
194
195 if (radix_enabled())
196 index = radix__init_new_context(mm);
197 else
198 index = hash__init_new_context(mm);
199
200 if (index < 0)
201 return index;
202
203 mm->context.id = index;
204
205 mm->context.pte_frag = NULL;
206 mm->context.pmd_frag = NULL;
207 #ifdef CONFIG_SPAPR_TCE_IOMMU
208 mm_iommu_init(mm);
209 #endif
210 atomic_set(&mm->context.active_cpus, 0);
211 atomic_set(&mm->context.copros, 0);
212
213 return 0;
214 }
215
__destroy_context(int context_id)216 void __destroy_context(int context_id)
217 {
218 ida_free(&mmu_context_ida, context_id);
219 }
220 EXPORT_SYMBOL_GPL(__destroy_context);
221
destroy_contexts(mm_context_t * ctx)222 static void destroy_contexts(mm_context_t *ctx)
223 {
224 if (radix_enabled()) {
225 ida_free(&mmu_context_ida, ctx->id);
226 } else {
227 #ifdef CONFIG_PPC_64S_HASH_MMU
228 int index, context_id;
229
230 for (index = 0; index < ARRAY_SIZE(ctx->extended_id); index++) {
231 context_id = ctx->extended_id[index];
232 if (context_id)
233 ida_free(&mmu_context_ida, context_id);
234 }
235 kfree(ctx->hash_context);
236 #else
237 BUILD_BUG(); // radix_enabled() should be constant true
238 #endif
239 }
240 }
241
pmd_frag_destroy(void * pmd_frag)242 static void pmd_frag_destroy(void *pmd_frag)
243 {
244 int count;
245 struct ptdesc *ptdesc;
246
247 ptdesc = virt_to_ptdesc(pmd_frag);
248 /* drop all the pending references */
249 count = ((unsigned long)pmd_frag & ~PAGE_MASK) >> PMD_FRAG_SIZE_SHIFT;
250 /* We allow PTE_FRAG_NR fragments from a PTE page */
251 if (atomic_sub_and_test(PMD_FRAG_NR - count, &ptdesc->pt_frag_refcount)) {
252 pagetable_dtor(ptdesc);
253 pagetable_free(ptdesc);
254 }
255 }
256
destroy_pagetable_cache(struct mm_struct * mm)257 static void destroy_pagetable_cache(struct mm_struct *mm)
258 {
259 void *frag;
260
261 frag = mm->context.pte_frag;
262 if (frag)
263 pte_frag_destroy(frag);
264
265 frag = mm->context.pmd_frag;
266 if (frag)
267 pmd_frag_destroy(frag);
268 return;
269 }
270
destroy_context(struct mm_struct * mm)271 void destroy_context(struct mm_struct *mm)
272 {
273 #ifdef CONFIG_SPAPR_TCE_IOMMU
274 WARN_ON_ONCE(!list_empty(&mm->context.iommu_group_mem_list));
275 #endif
276 /*
277 * For tasks which were successfully initialized we end up calling
278 * arch_exit_mmap() which clears the process table entry. And
279 * arch_exit_mmap() is called before the required fullmm TLB flush
280 * which does a RIC=2 flush. Hence for an initialized task, we do clear
281 * any cached process table entries.
282 *
283 * The condition below handles the error case during task init. We have
284 * set the process table entry early and if we fail a task
285 * initialization, we need to ensure the process table entry is zeroed.
286 * We need not worry about process table entry caches because the task
287 * never ran with the PID value.
288 */
289 if (radix_enabled())
290 process_tb[mm->context.id].prtb0 = 0;
291 else
292 subpage_prot_free(mm);
293 destroy_contexts(&mm->context);
294 mm->context.id = MMU_NO_CONTEXT;
295 }
296
arch_exit_mmap(struct mm_struct * mm)297 void arch_exit_mmap(struct mm_struct *mm)
298 {
299 destroy_pagetable_cache(mm);
300
301 if (radix_enabled()) {
302 /*
303 * Radix doesn't have a valid bit in the process table
304 * entries. However we know that at least P9 implementation
305 * will avoid caching an entry with an invalid RTS field,
306 * and 0 is invalid. So this will do.
307 *
308 * This runs before the "fullmm" tlb flush in exit_mmap,
309 * which does a RIC=2 tlbie to clear the process table
310 * entry. See the "fullmm" comments in tlb-radix.c.
311 *
312 * No barrier required here after the store because
313 * this process will do the invalidate, which starts with
314 * ptesync.
315 */
316 process_tb[mm->context.id].prtb0 = 0;
317 }
318 }
319
320 #ifdef CONFIG_PPC_RADIX_MMU
radix__switch_mmu_context(struct mm_struct * prev,struct mm_struct * next)321 void radix__switch_mmu_context(struct mm_struct *prev, struct mm_struct *next)
322 {
323 mtspr(SPRN_PID, next->context.id);
324 isync();
325 }
326 #endif
327
328 /**
329 * cleanup_cpu_mmu_context - Clean up MMU details for this CPU (newly offlined)
330 *
331 * This clears the CPU from mm_cpumask for all processes, and then flushes the
332 * local TLB to ensure TLB coherency in case the CPU is onlined again.
333 *
334 * KVM guest translations are not necessarily flushed here. If KVM started
335 * using mm_cpumask or the Linux APIs which do, this would have to be resolved.
336 */
337 #ifdef CONFIG_HOTPLUG_CPU
cleanup_cpu_mmu_context(void)338 void cleanup_cpu_mmu_context(void)
339 {
340 int cpu = smp_processor_id();
341
342 clear_tasks_mm_cpumask(cpu);
343 tlbiel_all();
344 }
345 #endif
346