• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *    Copyright IBM Corp. 2007, 2011
3  *    Author(s): Martin Schwidefsky <schwidefsky@de.ibm.com>
4  */
5 
6 #include <linux/sched.h>
7 #include <linux/kernel.h>
8 #include <linux/errno.h>
9 #include <linux/gfp.h>
10 #include <linux/mm.h>
11 #include <linux/swap.h>
12 #include <linux/smp.h>
13 #include <linux/spinlock.h>
14 #include <linux/rcupdate.h>
15 #include <linux/slab.h>
16 #include <linux/swapops.h>
17 #include <linux/sysctl.h>
18 #include <linux/ksm.h>
19 #include <linux/mman.h>
20 
21 #include <asm/pgtable.h>
22 #include <asm/pgalloc.h>
23 #include <asm/tlb.h>
24 #include <asm/tlbflush.h>
25 #include <asm/mmu_context.h>
26 
crst_table_alloc(struct mm_struct * mm)27 unsigned long *crst_table_alloc(struct mm_struct *mm)
28 {
29 	struct page *page = alloc_pages(GFP_KERNEL, 2);
30 
31 	if (!page)
32 		return NULL;
33 	return (unsigned long *) page_to_phys(page);
34 }
35 
crst_table_free(struct mm_struct * mm,unsigned long * table)36 void crst_table_free(struct mm_struct *mm, unsigned long *table)
37 {
38 	free_pages((unsigned long) table, 2);
39 }
40 
__crst_table_upgrade(void * arg)41 static void __crst_table_upgrade(void *arg)
42 {
43 	struct mm_struct *mm = arg;
44 
45 	if (current->active_mm == mm) {
46 		clear_user_asce();
47 		set_user_asce(mm);
48 	}
49 	__tlb_flush_local();
50 }
51 
crst_table_upgrade(struct mm_struct * mm)52 int crst_table_upgrade(struct mm_struct *mm)
53 {
54 	unsigned long *table, *pgd;
55 
56 	/* upgrade should only happen from 3 to 4 levels */
57 	BUG_ON(mm->context.asce_limit != (1UL << 42));
58 
59 	table = crst_table_alloc(mm);
60 	if (!table)
61 		return -ENOMEM;
62 
63 	spin_lock_bh(&mm->page_table_lock);
64 	pgd = (unsigned long *) mm->pgd;
65 	crst_table_init(table, _REGION2_ENTRY_EMPTY);
66 	pgd_populate(mm, (pgd_t *) table, (pud_t *) pgd);
67 	mm->pgd = (pgd_t *) table;
68 	mm->context.asce_limit = 1UL << 53;
69 	mm->context.asce = __pa(mm->pgd) | _ASCE_TABLE_LENGTH |
70 			   _ASCE_USER_BITS | _ASCE_TYPE_REGION2;
71 	mm->task_size = mm->context.asce_limit;
72 	spin_unlock_bh(&mm->page_table_lock);
73 
74 	on_each_cpu(__crst_table_upgrade, mm, 0);
75 	return 0;
76 }
77 
crst_table_downgrade(struct mm_struct * mm)78 void crst_table_downgrade(struct mm_struct *mm)
79 {
80 	pgd_t *pgd;
81 
82 	/* downgrade should only happen from 3 to 2 levels (compat only) */
83 	BUG_ON(mm->context.asce_limit != (1UL << 42));
84 
85 	if (current->active_mm == mm) {
86 		clear_user_asce();
87 		__tlb_flush_mm(mm);
88 	}
89 
90 	pgd = mm->pgd;
91 	mm->pgd = (pgd_t *) (pgd_val(*pgd) & _REGION_ENTRY_ORIGIN);
92 	mm->context.asce_limit = 1UL << 31;
93 	mm->context.asce = __pa(mm->pgd) | _ASCE_TABLE_LENGTH |
94 			   _ASCE_USER_BITS | _ASCE_TYPE_SEGMENT;
95 	mm->task_size = mm->context.asce_limit;
96 	crst_table_free(mm, (unsigned long *) pgd);
97 
98 	if (current->active_mm == mm)
99 		set_user_asce(mm);
100 }
101 
102 #ifdef CONFIG_PGSTE
103 
104 /**
105  * gmap_alloc - allocate a guest address space
106  * @mm: pointer to the parent mm_struct
107  * @limit: maximum size of the gmap address space
108  *
109  * Returns a guest address space structure.
110  */
gmap_alloc(struct mm_struct * mm,unsigned long limit)111 struct gmap *gmap_alloc(struct mm_struct *mm, unsigned long limit)
112 {
113 	struct gmap *gmap;
114 	struct page *page;
115 	unsigned long *table;
116 	unsigned long etype, atype;
117 
118 	if (limit < (1UL << 31)) {
119 		limit = (1UL << 31) - 1;
120 		atype = _ASCE_TYPE_SEGMENT;
121 		etype = _SEGMENT_ENTRY_EMPTY;
122 	} else if (limit < (1UL << 42)) {
123 		limit = (1UL << 42) - 1;
124 		atype = _ASCE_TYPE_REGION3;
125 		etype = _REGION3_ENTRY_EMPTY;
126 	} else if (limit < (1UL << 53)) {
127 		limit = (1UL << 53) - 1;
128 		atype = _ASCE_TYPE_REGION2;
129 		etype = _REGION2_ENTRY_EMPTY;
130 	} else {
131 		limit = -1UL;
132 		atype = _ASCE_TYPE_REGION1;
133 		etype = _REGION1_ENTRY_EMPTY;
134 	}
135 	gmap = kzalloc(sizeof(struct gmap), GFP_KERNEL);
136 	if (!gmap)
137 		goto out;
138 	INIT_LIST_HEAD(&gmap->crst_list);
139 	INIT_RADIX_TREE(&gmap->guest_to_host, GFP_KERNEL);
140 	INIT_RADIX_TREE(&gmap->host_to_guest, GFP_ATOMIC);
141 	spin_lock_init(&gmap->guest_table_lock);
142 	gmap->mm = mm;
143 	page = alloc_pages(GFP_KERNEL, 2);
144 	if (!page)
145 		goto out_free;
146 	page->index = 0;
147 	list_add(&page->lru, &gmap->crst_list);
148 	table = (unsigned long *) page_to_phys(page);
149 	crst_table_init(table, etype);
150 	gmap->table = table;
151 	gmap->asce = atype | _ASCE_TABLE_LENGTH |
152 		_ASCE_USER_BITS | __pa(table);
153 	gmap->asce_end = limit;
154 	down_write(&mm->mmap_sem);
155 	list_add(&gmap->list, &mm->context.gmap_list);
156 	up_write(&mm->mmap_sem);
157 	return gmap;
158 
159 out_free:
160 	kfree(gmap);
161 out:
162 	return NULL;
163 }
164 EXPORT_SYMBOL_GPL(gmap_alloc);
165 
gmap_flush_tlb(struct gmap * gmap)166 static void gmap_flush_tlb(struct gmap *gmap)
167 {
168 	if (MACHINE_HAS_IDTE)
169 		__tlb_flush_idte(gmap->asce);
170 	else
171 		__tlb_flush_global();
172 }
173 
gmap_radix_tree_free(struct radix_tree_root * root)174 static void gmap_radix_tree_free(struct radix_tree_root *root)
175 {
176 	struct radix_tree_iter iter;
177 	unsigned long indices[16];
178 	unsigned long index;
179 	void **slot;
180 	int i, nr;
181 
182 	/* A radix tree is freed by deleting all of its entries */
183 	index = 0;
184 	do {
185 		nr = 0;
186 		radix_tree_for_each_slot(slot, root, &iter, index) {
187 			indices[nr] = iter.index;
188 			if (++nr == 16)
189 				break;
190 		}
191 		for (i = 0; i < nr; i++) {
192 			index = indices[i];
193 			radix_tree_delete(root, index);
194 		}
195 	} while (nr > 0);
196 }
197 
198 /**
199  * gmap_free - free a guest address space
200  * @gmap: pointer to the guest address space structure
201  */
gmap_free(struct gmap * gmap)202 void gmap_free(struct gmap *gmap)
203 {
204 	struct page *page, *next;
205 
206 	/* Flush tlb. */
207 	if (MACHINE_HAS_IDTE)
208 		__tlb_flush_idte(gmap->asce);
209 	else
210 		__tlb_flush_global();
211 
212 	/* Free all segment & region tables. */
213 	list_for_each_entry_safe(page, next, &gmap->crst_list, lru)
214 		__free_pages(page, 2);
215 	gmap_radix_tree_free(&gmap->guest_to_host);
216 	gmap_radix_tree_free(&gmap->host_to_guest);
217 	down_write(&gmap->mm->mmap_sem);
218 	list_del(&gmap->list);
219 	up_write(&gmap->mm->mmap_sem);
220 	kfree(gmap);
221 }
222 EXPORT_SYMBOL_GPL(gmap_free);
223 
224 /**
225  * gmap_enable - switch primary space to the guest address space
226  * @gmap: pointer to the guest address space structure
227  */
gmap_enable(struct gmap * gmap)228 void gmap_enable(struct gmap *gmap)
229 {
230 	S390_lowcore.gmap = (unsigned long) gmap;
231 }
232 EXPORT_SYMBOL_GPL(gmap_enable);
233 
234 /**
235  * gmap_disable - switch back to the standard primary address space
236  * @gmap: pointer to the guest address space structure
237  */
gmap_disable(struct gmap * gmap)238 void gmap_disable(struct gmap *gmap)
239 {
240 	S390_lowcore.gmap = 0UL;
241 }
242 EXPORT_SYMBOL_GPL(gmap_disable);
243 
244 /*
245  * gmap_alloc_table is assumed to be called with mmap_sem held
246  */
gmap_alloc_table(struct gmap * gmap,unsigned long * table,unsigned long init,unsigned long gaddr)247 static int gmap_alloc_table(struct gmap *gmap, unsigned long *table,
248 			    unsigned long init, unsigned long gaddr)
249 {
250 	struct page *page;
251 	unsigned long *new;
252 
253 	/* since we dont free the gmap table until gmap_free we can unlock */
254 	page = alloc_pages(GFP_KERNEL, 2);
255 	if (!page)
256 		return -ENOMEM;
257 	new = (unsigned long *) page_to_phys(page);
258 	crst_table_init(new, init);
259 	spin_lock(&gmap->mm->page_table_lock);
260 	if (*table & _REGION_ENTRY_INVALID) {
261 		list_add(&page->lru, &gmap->crst_list);
262 		*table = (unsigned long) new | _REGION_ENTRY_LENGTH |
263 			(*table & _REGION_ENTRY_TYPE_MASK);
264 		page->index = gaddr;
265 		page = NULL;
266 	}
267 	spin_unlock(&gmap->mm->page_table_lock);
268 	if (page)
269 		__free_pages(page, 2);
270 	return 0;
271 }
272 
273 /**
274  * __gmap_segment_gaddr - find virtual address from segment pointer
275  * @entry: pointer to a segment table entry in the guest address space
276  *
277  * Returns the virtual address in the guest address space for the segment
278  */
__gmap_segment_gaddr(unsigned long * entry)279 static unsigned long __gmap_segment_gaddr(unsigned long *entry)
280 {
281 	struct page *page;
282 	unsigned long offset, mask;
283 
284 	offset = (unsigned long) entry / sizeof(unsigned long);
285 	offset = (offset & (PTRS_PER_PMD - 1)) * PMD_SIZE;
286 	mask = ~(PTRS_PER_PMD * sizeof(pmd_t) - 1);
287 	page = virt_to_page((void *)((unsigned long) entry & mask));
288 	return page->index + offset;
289 }
290 
291 /**
292  * __gmap_unlink_by_vmaddr - unlink a single segment via a host address
293  * @gmap: pointer to the guest address space structure
294  * @vmaddr: address in the host process address space
295  *
296  * Returns 1 if a TLB flush is required
297  */
__gmap_unlink_by_vmaddr(struct gmap * gmap,unsigned long vmaddr)298 static int __gmap_unlink_by_vmaddr(struct gmap *gmap, unsigned long vmaddr)
299 {
300 	unsigned long *entry;
301 	int flush = 0;
302 
303 	spin_lock(&gmap->guest_table_lock);
304 	entry = radix_tree_delete(&gmap->host_to_guest, vmaddr >> PMD_SHIFT);
305 	if (entry) {
306 		flush = (*entry != _SEGMENT_ENTRY_INVALID);
307 		*entry = _SEGMENT_ENTRY_INVALID;
308 	}
309 	spin_unlock(&gmap->guest_table_lock);
310 	return flush;
311 }
312 
313 /**
314  * __gmap_unmap_by_gaddr - unmap a single segment via a guest address
315  * @gmap: pointer to the guest address space structure
316  * @gaddr: address in the guest address space
317  *
318  * Returns 1 if a TLB flush is required
319  */
__gmap_unmap_by_gaddr(struct gmap * gmap,unsigned long gaddr)320 static int __gmap_unmap_by_gaddr(struct gmap *gmap, unsigned long gaddr)
321 {
322 	unsigned long vmaddr;
323 
324 	vmaddr = (unsigned long) radix_tree_delete(&gmap->guest_to_host,
325 						   gaddr >> PMD_SHIFT);
326 	return vmaddr ? __gmap_unlink_by_vmaddr(gmap, vmaddr) : 0;
327 }
328 
329 /**
330  * gmap_unmap_segment - unmap segment from the guest address space
331  * @gmap: pointer to the guest address space structure
332  * @to: address in the guest address space
333  * @len: length of the memory area to unmap
334  *
335  * Returns 0 if the unmap succeeded, -EINVAL if not.
336  */
gmap_unmap_segment(struct gmap * gmap,unsigned long to,unsigned long len)337 int gmap_unmap_segment(struct gmap *gmap, unsigned long to, unsigned long len)
338 {
339 	unsigned long off;
340 	int flush;
341 
342 	if ((to | len) & (PMD_SIZE - 1))
343 		return -EINVAL;
344 	if (len == 0 || to + len < to)
345 		return -EINVAL;
346 
347 	flush = 0;
348 	down_write(&gmap->mm->mmap_sem);
349 	for (off = 0; off < len; off += PMD_SIZE)
350 		flush |= __gmap_unmap_by_gaddr(gmap, to + off);
351 	up_write(&gmap->mm->mmap_sem);
352 	if (flush)
353 		gmap_flush_tlb(gmap);
354 	return 0;
355 }
356 EXPORT_SYMBOL_GPL(gmap_unmap_segment);
357 
358 /**
359  * gmap_mmap_segment - map a segment to the guest address space
360  * @gmap: pointer to the guest address space structure
361  * @from: source address in the parent address space
362  * @to: target address in the guest address space
363  * @len: length of the memory area to map
364  *
365  * Returns 0 if the mmap succeeded, -EINVAL or -ENOMEM if not.
366  */
gmap_map_segment(struct gmap * gmap,unsigned long from,unsigned long to,unsigned long len)367 int gmap_map_segment(struct gmap *gmap, unsigned long from,
368 		     unsigned long to, unsigned long len)
369 {
370 	unsigned long off;
371 	int flush;
372 
373 	if ((from | to | len) & (PMD_SIZE - 1))
374 		return -EINVAL;
375 	if (len == 0 || from + len < from || to + len < to ||
376 	    from + len > TASK_MAX_SIZE || to + len > gmap->asce_end)
377 		return -EINVAL;
378 
379 	flush = 0;
380 	down_write(&gmap->mm->mmap_sem);
381 	for (off = 0; off < len; off += PMD_SIZE) {
382 		/* Remove old translation */
383 		flush |= __gmap_unmap_by_gaddr(gmap, to + off);
384 		/* Store new translation */
385 		if (radix_tree_insert(&gmap->guest_to_host,
386 				      (to + off) >> PMD_SHIFT,
387 				      (void *) from + off))
388 			break;
389 	}
390 	up_write(&gmap->mm->mmap_sem);
391 	if (flush)
392 		gmap_flush_tlb(gmap);
393 	if (off >= len)
394 		return 0;
395 	gmap_unmap_segment(gmap, to, len);
396 	return -ENOMEM;
397 }
398 EXPORT_SYMBOL_GPL(gmap_map_segment);
399 
400 /**
401  * __gmap_translate - translate a guest address to a user space address
402  * @gmap: pointer to guest mapping meta data structure
403  * @gaddr: guest address
404  *
405  * Returns user space address which corresponds to the guest address or
406  * -EFAULT if no such mapping exists.
407  * This function does not establish potentially missing page table entries.
408  * The mmap_sem of the mm that belongs to the address space must be held
409  * when this function gets called.
410  */
__gmap_translate(struct gmap * gmap,unsigned long gaddr)411 unsigned long __gmap_translate(struct gmap *gmap, unsigned long gaddr)
412 {
413 	unsigned long vmaddr;
414 
415 	vmaddr = (unsigned long)
416 		radix_tree_lookup(&gmap->guest_to_host, gaddr >> PMD_SHIFT);
417 	return vmaddr ? (vmaddr | (gaddr & ~PMD_MASK)) : -EFAULT;
418 }
419 EXPORT_SYMBOL_GPL(__gmap_translate);
420 
421 /**
422  * gmap_translate - translate a guest address to a user space address
423  * @gmap: pointer to guest mapping meta data structure
424  * @gaddr: guest address
425  *
426  * Returns user space address which corresponds to the guest address or
427  * -EFAULT if no such mapping exists.
428  * This function does not establish potentially missing page table entries.
429  */
gmap_translate(struct gmap * gmap,unsigned long gaddr)430 unsigned long gmap_translate(struct gmap *gmap, unsigned long gaddr)
431 {
432 	unsigned long rc;
433 
434 	down_read(&gmap->mm->mmap_sem);
435 	rc = __gmap_translate(gmap, gaddr);
436 	up_read(&gmap->mm->mmap_sem);
437 	return rc;
438 }
439 EXPORT_SYMBOL_GPL(gmap_translate);
440 
441 /**
442  * gmap_unlink - disconnect a page table from the gmap shadow tables
443  * @gmap: pointer to guest mapping meta data structure
444  * @table: pointer to the host page table
445  * @vmaddr: vm address associated with the host page table
446  */
gmap_unlink(struct mm_struct * mm,unsigned long * table,unsigned long vmaddr)447 static void gmap_unlink(struct mm_struct *mm, unsigned long *table,
448 			unsigned long vmaddr)
449 {
450 	struct gmap *gmap;
451 	int flush;
452 
453 	list_for_each_entry(gmap, &mm->context.gmap_list, list) {
454 		flush = __gmap_unlink_by_vmaddr(gmap, vmaddr);
455 		if (flush)
456 			gmap_flush_tlb(gmap);
457 	}
458 }
459 
460 /**
461  * gmap_link - set up shadow page tables to connect a host to a guest address
462  * @gmap: pointer to guest mapping meta data structure
463  * @gaddr: guest address
464  * @vmaddr: vm address
465  *
466  * Returns 0 on success, -ENOMEM for out of memory conditions, and -EFAULT
467  * if the vm address is already mapped to a different guest segment.
468  * The mmap_sem of the mm that belongs to the address space must be held
469  * when this function gets called.
470  */
__gmap_link(struct gmap * gmap,unsigned long gaddr,unsigned long vmaddr)471 int __gmap_link(struct gmap *gmap, unsigned long gaddr, unsigned long vmaddr)
472 {
473 	struct mm_struct *mm;
474 	unsigned long *table;
475 	spinlock_t *ptl;
476 	pgd_t *pgd;
477 	pud_t *pud;
478 	pmd_t *pmd;
479 	int rc;
480 
481 	/* Create higher level tables in the gmap page table */
482 	table = gmap->table;
483 	if ((gmap->asce & _ASCE_TYPE_MASK) >= _ASCE_TYPE_REGION1) {
484 		table += (gaddr >> 53) & 0x7ff;
485 		if ((*table & _REGION_ENTRY_INVALID) &&
486 		    gmap_alloc_table(gmap, table, _REGION2_ENTRY_EMPTY,
487 				     gaddr & 0xffe0000000000000UL))
488 			return -ENOMEM;
489 		table = (unsigned long *)(*table & _REGION_ENTRY_ORIGIN);
490 	}
491 	if ((gmap->asce & _ASCE_TYPE_MASK) >= _ASCE_TYPE_REGION2) {
492 		table += (gaddr >> 42) & 0x7ff;
493 		if ((*table & _REGION_ENTRY_INVALID) &&
494 		    gmap_alloc_table(gmap, table, _REGION3_ENTRY_EMPTY,
495 				     gaddr & 0xfffffc0000000000UL))
496 			return -ENOMEM;
497 		table = (unsigned long *)(*table & _REGION_ENTRY_ORIGIN);
498 	}
499 	if ((gmap->asce & _ASCE_TYPE_MASK) >= _ASCE_TYPE_REGION3) {
500 		table += (gaddr >> 31) & 0x7ff;
501 		if ((*table & _REGION_ENTRY_INVALID) &&
502 		    gmap_alloc_table(gmap, table, _SEGMENT_ENTRY_EMPTY,
503 				     gaddr & 0xffffffff80000000UL))
504 			return -ENOMEM;
505 		table = (unsigned long *)(*table & _REGION_ENTRY_ORIGIN);
506 	}
507 	table += (gaddr >> 20) & 0x7ff;
508 	/* Walk the parent mm page table */
509 	mm = gmap->mm;
510 	pgd = pgd_offset(mm, vmaddr);
511 	VM_BUG_ON(pgd_none(*pgd));
512 	pud = pud_offset(pgd, vmaddr);
513 	VM_BUG_ON(pud_none(*pud));
514 	pmd = pmd_offset(pud, vmaddr);
515 	VM_BUG_ON(pmd_none(*pmd));
516 	/* large pmds cannot yet be handled */
517 	if (pmd_large(*pmd))
518 		return -EFAULT;
519 	/* Link gmap segment table entry location to page table. */
520 	rc = radix_tree_preload(GFP_KERNEL);
521 	if (rc)
522 		return rc;
523 	ptl = pmd_lock(mm, pmd);
524 	spin_lock(&gmap->guest_table_lock);
525 	if (*table == _SEGMENT_ENTRY_INVALID) {
526 		rc = radix_tree_insert(&gmap->host_to_guest,
527 				       vmaddr >> PMD_SHIFT, table);
528 		if (!rc)
529 			*table = pmd_val(*pmd);
530 	} else
531 		rc = 0;
532 	spin_unlock(&gmap->guest_table_lock);
533 	spin_unlock(ptl);
534 	radix_tree_preload_end();
535 	return rc;
536 }
537 
538 /**
539  * gmap_fault - resolve a fault on a guest address
540  * @gmap: pointer to guest mapping meta data structure
541  * @gaddr: guest address
542  * @fault_flags: flags to pass down to handle_mm_fault()
543  *
544  * Returns 0 on success, -ENOMEM for out of memory conditions, and -EFAULT
545  * if the vm address is already mapped to a different guest segment.
546  */
gmap_fault(struct gmap * gmap,unsigned long gaddr,unsigned int fault_flags)547 int gmap_fault(struct gmap *gmap, unsigned long gaddr,
548 	       unsigned int fault_flags)
549 {
550 	unsigned long vmaddr;
551 	int rc;
552 
553 	down_read(&gmap->mm->mmap_sem);
554 	vmaddr = __gmap_translate(gmap, gaddr);
555 	if (IS_ERR_VALUE(vmaddr)) {
556 		rc = vmaddr;
557 		goto out_up;
558 	}
559 	if (fixup_user_fault(current, gmap->mm, vmaddr, fault_flags)) {
560 		rc = -EFAULT;
561 		goto out_up;
562 	}
563 	rc = __gmap_link(gmap, gaddr, vmaddr);
564 out_up:
565 	up_read(&gmap->mm->mmap_sem);
566 	return rc;
567 }
568 EXPORT_SYMBOL_GPL(gmap_fault);
569 
gmap_zap_swap_entry(swp_entry_t entry,struct mm_struct * mm)570 static void gmap_zap_swap_entry(swp_entry_t entry, struct mm_struct *mm)
571 {
572 	if (!non_swap_entry(entry))
573 		dec_mm_counter(mm, MM_SWAPENTS);
574 	else if (is_migration_entry(entry)) {
575 		struct page *page = migration_entry_to_page(entry);
576 
577 		if (PageAnon(page))
578 			dec_mm_counter(mm, MM_ANONPAGES);
579 		else
580 			dec_mm_counter(mm, MM_FILEPAGES);
581 	}
582 	free_swap_and_cache(entry);
583 }
584 
585 /*
586  * this function is assumed to be called with mmap_sem held
587  */
__gmap_zap(struct gmap * gmap,unsigned long gaddr)588 void __gmap_zap(struct gmap *gmap, unsigned long gaddr)
589 {
590 	unsigned long vmaddr, ptev, pgstev;
591 	pte_t *ptep, pte;
592 	spinlock_t *ptl;
593 	pgste_t pgste;
594 
595 	/* Find the vm address for the guest address */
596 	vmaddr = (unsigned long) radix_tree_lookup(&gmap->guest_to_host,
597 						   gaddr >> PMD_SHIFT);
598 	if (!vmaddr)
599 		return;
600 	vmaddr |= gaddr & ~PMD_MASK;
601 	/* Get pointer to the page table entry */
602 	ptep = get_locked_pte(gmap->mm, vmaddr, &ptl);
603 	if (unlikely(!ptep))
604 		return;
605 	pte = *ptep;
606 	if (!pte_swap(pte))
607 		goto out_pte;
608 	/* Zap unused and logically-zero pages */
609 	pgste = pgste_get_lock(ptep);
610 	pgstev = pgste_val(pgste);
611 	ptev = pte_val(pte);
612 	if (((pgstev & _PGSTE_GPS_USAGE_MASK) == _PGSTE_GPS_USAGE_UNUSED) ||
613 	    ((pgstev & _PGSTE_GPS_ZERO) && (ptev & _PAGE_INVALID))) {
614 		gmap_zap_swap_entry(pte_to_swp_entry(pte), gmap->mm);
615 		pte_clear(gmap->mm, vmaddr, ptep);
616 	}
617 	pgste_set_unlock(ptep, pgste);
618 out_pte:
619 	pte_unmap_unlock(ptep, ptl);
620 }
621 EXPORT_SYMBOL_GPL(__gmap_zap);
622 
gmap_discard(struct gmap * gmap,unsigned long from,unsigned long to)623 void gmap_discard(struct gmap *gmap, unsigned long from, unsigned long to)
624 {
625 	unsigned long gaddr, vmaddr, size;
626 	struct vm_area_struct *vma;
627 
628 	down_read(&gmap->mm->mmap_sem);
629 	for (gaddr = from; gaddr < to;
630 	     gaddr = (gaddr + PMD_SIZE) & PMD_MASK) {
631 		/* Find the vm address for the guest address */
632 		vmaddr = (unsigned long)
633 			radix_tree_lookup(&gmap->guest_to_host,
634 					  gaddr >> PMD_SHIFT);
635 		if (!vmaddr)
636 			continue;
637 		vmaddr |= gaddr & ~PMD_MASK;
638 		/* Find vma in the parent mm */
639 		vma = find_vma(gmap->mm, vmaddr);
640 		if (!vma)
641 			continue;
642 		size = min(to - gaddr, PMD_SIZE - (gaddr & ~PMD_MASK));
643 		zap_page_range(vma, vmaddr, size, NULL);
644 	}
645 	up_read(&gmap->mm->mmap_sem);
646 }
647 EXPORT_SYMBOL_GPL(gmap_discard);
648 
649 static LIST_HEAD(gmap_notifier_list);
650 static DEFINE_SPINLOCK(gmap_notifier_lock);
651 
652 /**
653  * gmap_register_ipte_notifier - register a pte invalidation callback
654  * @nb: pointer to the gmap notifier block
655  */
gmap_register_ipte_notifier(struct gmap_notifier * nb)656 void gmap_register_ipte_notifier(struct gmap_notifier *nb)
657 {
658 	spin_lock(&gmap_notifier_lock);
659 	list_add(&nb->list, &gmap_notifier_list);
660 	spin_unlock(&gmap_notifier_lock);
661 }
662 EXPORT_SYMBOL_GPL(gmap_register_ipte_notifier);
663 
664 /**
665  * gmap_unregister_ipte_notifier - remove a pte invalidation callback
666  * @nb: pointer to the gmap notifier block
667  */
gmap_unregister_ipte_notifier(struct gmap_notifier * nb)668 void gmap_unregister_ipte_notifier(struct gmap_notifier *nb)
669 {
670 	spin_lock(&gmap_notifier_lock);
671 	list_del_init(&nb->list);
672 	spin_unlock(&gmap_notifier_lock);
673 }
674 EXPORT_SYMBOL_GPL(gmap_unregister_ipte_notifier);
675 
676 /**
677  * gmap_ipte_notify - mark a range of ptes for invalidation notification
678  * @gmap: pointer to guest mapping meta data structure
679  * @gaddr: virtual address in the guest address space
680  * @len: size of area
681  *
682  * Returns 0 if for each page in the given range a gmap mapping exists and
683  * the invalidation notification could be set. If the gmap mapping is missing
684  * for one or more pages -EFAULT is returned. If no memory could be allocated
685  * -ENOMEM is returned. This function establishes missing page table entries.
686  */
gmap_ipte_notify(struct gmap * gmap,unsigned long gaddr,unsigned long len)687 int gmap_ipte_notify(struct gmap *gmap, unsigned long gaddr, unsigned long len)
688 {
689 	unsigned long addr;
690 	spinlock_t *ptl;
691 	pte_t *ptep, entry;
692 	pgste_t pgste;
693 	int rc = 0;
694 
695 	if ((gaddr & ~PAGE_MASK) || (len & ~PAGE_MASK))
696 		return -EINVAL;
697 	down_read(&gmap->mm->mmap_sem);
698 	while (len) {
699 		/* Convert gmap address and connect the page tables */
700 		addr = __gmap_translate(gmap, gaddr);
701 		if (IS_ERR_VALUE(addr)) {
702 			rc = addr;
703 			break;
704 		}
705 		/* Get the page mapped */
706 		if (fixup_user_fault(current, gmap->mm, addr, FAULT_FLAG_WRITE)) {
707 			rc = -EFAULT;
708 			break;
709 		}
710 		rc = __gmap_link(gmap, gaddr, addr);
711 		if (rc)
712 			break;
713 		/* Walk the process page table, lock and get pte pointer */
714 		ptep = get_locked_pte(gmap->mm, addr, &ptl);
715 		VM_BUG_ON(!ptep);
716 		/* Set notification bit in the pgste of the pte */
717 		entry = *ptep;
718 		if ((pte_val(entry) & (_PAGE_INVALID | _PAGE_PROTECT)) == 0) {
719 			pgste = pgste_get_lock(ptep);
720 			pgste_val(pgste) |= PGSTE_IN_BIT;
721 			pgste_set_unlock(ptep, pgste);
722 			gaddr += PAGE_SIZE;
723 			len -= PAGE_SIZE;
724 		}
725 		pte_unmap_unlock(ptep, ptl);
726 	}
727 	up_read(&gmap->mm->mmap_sem);
728 	return rc;
729 }
730 EXPORT_SYMBOL_GPL(gmap_ipte_notify);
731 
732 /**
733  * gmap_do_ipte_notify - call all invalidation callbacks for a specific pte.
734  * @mm: pointer to the process mm_struct
735  * @addr: virtual address in the process address space
736  * @pte: pointer to the page table entry
737  *
738  * This function is assumed to be called with the page table lock held
739  * for the pte to notify.
740  */
gmap_do_ipte_notify(struct mm_struct * mm,unsigned long vmaddr,pte_t * pte)741 void gmap_do_ipte_notify(struct mm_struct *mm, unsigned long vmaddr, pte_t *pte)
742 {
743 	unsigned long offset, gaddr;
744 	unsigned long *table;
745 	struct gmap_notifier *nb;
746 	struct gmap *gmap;
747 
748 	offset = ((unsigned long) pte) & (255 * sizeof(pte_t));
749 	offset = offset * (4096 / sizeof(pte_t));
750 	spin_lock(&gmap_notifier_lock);
751 	list_for_each_entry(gmap, &mm->context.gmap_list, list) {
752 		table = radix_tree_lookup(&gmap->host_to_guest,
753 					  vmaddr >> PMD_SHIFT);
754 		if (!table)
755 			continue;
756 		gaddr = __gmap_segment_gaddr(table) + offset;
757 		list_for_each_entry(nb, &gmap_notifier_list, list)
758 			nb->notifier_call(gmap, gaddr);
759 	}
760 	spin_unlock(&gmap_notifier_lock);
761 }
762 EXPORT_SYMBOL_GPL(gmap_do_ipte_notify);
763 
set_guest_storage_key(struct mm_struct * mm,unsigned long addr,unsigned long key,bool nq)764 int set_guest_storage_key(struct mm_struct *mm, unsigned long addr,
765 			  unsigned long key, bool nq)
766 {
767 	spinlock_t *ptl;
768 	pgste_t old, new;
769 	pte_t *ptep;
770 
771 	down_read(&mm->mmap_sem);
772 retry:
773 	ptep = get_locked_pte(mm, addr, &ptl);
774 	if (unlikely(!ptep)) {
775 		up_read(&mm->mmap_sem);
776 		return -EFAULT;
777 	}
778 	if (!(pte_val(*ptep) & _PAGE_INVALID) &&
779 	     (pte_val(*ptep) & _PAGE_PROTECT)) {
780 		pte_unmap_unlock(ptep, ptl);
781 		if (fixup_user_fault(current, mm, addr, FAULT_FLAG_WRITE)) {
782 			up_read(&mm->mmap_sem);
783 			return -EFAULT;
784 		}
785 		goto retry;
786 	}
787 
788 	new = old = pgste_get_lock(ptep);
789 	pgste_val(new) &= ~(PGSTE_GR_BIT | PGSTE_GC_BIT |
790 			    PGSTE_ACC_BITS | PGSTE_FP_BIT);
791 	pgste_val(new) |= (key & (_PAGE_CHANGED | _PAGE_REFERENCED)) << 48;
792 	pgste_val(new) |= (key & (_PAGE_ACC_BITS | _PAGE_FP_BIT)) << 56;
793 	if (!(pte_val(*ptep) & _PAGE_INVALID)) {
794 		unsigned long address, bits, skey;
795 
796 		address = pte_val(*ptep) & PAGE_MASK;
797 		skey = (unsigned long) page_get_storage_key(address);
798 		bits = skey & (_PAGE_CHANGED | _PAGE_REFERENCED);
799 		skey = key & (_PAGE_ACC_BITS | _PAGE_FP_BIT);
800 		/* Set storage key ACC and FP */
801 		page_set_storage_key(address, skey, !nq);
802 		/* Merge host changed & referenced into pgste  */
803 		pgste_val(new) |= bits << 52;
804 	}
805 	/* changing the guest storage key is considered a change of the page */
806 	if ((pgste_val(new) ^ pgste_val(old)) &
807 	    (PGSTE_ACC_BITS | PGSTE_FP_BIT | PGSTE_GR_BIT | PGSTE_GC_BIT))
808 		pgste_val(new) |= PGSTE_UC_BIT;
809 
810 	pgste_set_unlock(ptep, new);
811 	pte_unmap_unlock(ptep, ptl);
812 	up_read(&mm->mmap_sem);
813 	return 0;
814 }
815 EXPORT_SYMBOL(set_guest_storage_key);
816 
get_guest_storage_key(struct mm_struct * mm,unsigned long addr)817 unsigned long get_guest_storage_key(struct mm_struct *mm, unsigned long addr)
818 {
819 	spinlock_t *ptl;
820 	pgste_t pgste;
821 	pte_t *ptep;
822 	uint64_t physaddr;
823 	unsigned long key = 0;
824 
825 	down_read(&mm->mmap_sem);
826 	ptep = get_locked_pte(mm, addr, &ptl);
827 	if (unlikely(!ptep)) {
828 		up_read(&mm->mmap_sem);
829 		return -EFAULT;
830 	}
831 	pgste = pgste_get_lock(ptep);
832 
833 	if (pte_val(*ptep) & _PAGE_INVALID) {
834 		key |= (pgste_val(pgste) & PGSTE_ACC_BITS) >> 56;
835 		key |= (pgste_val(pgste) & PGSTE_FP_BIT) >> 56;
836 		key |= (pgste_val(pgste) & PGSTE_GR_BIT) >> 48;
837 		key |= (pgste_val(pgste) & PGSTE_GC_BIT) >> 48;
838 	} else {
839 		physaddr = pte_val(*ptep) & PAGE_MASK;
840 		key = page_get_storage_key(physaddr);
841 
842 		/* Reflect guest's logical view, not physical */
843 		if (pgste_val(pgste) & PGSTE_GR_BIT)
844 			key |= _PAGE_REFERENCED;
845 		if (pgste_val(pgste) & PGSTE_GC_BIT)
846 			key |= _PAGE_CHANGED;
847 	}
848 
849 	pgste_set_unlock(ptep, pgste);
850 	pte_unmap_unlock(ptep, ptl);
851 	up_read(&mm->mmap_sem);
852 	return key;
853 }
854 EXPORT_SYMBOL(get_guest_storage_key);
855 
856 static int page_table_allocate_pgste_min = 0;
857 static int page_table_allocate_pgste_max = 1;
858 int page_table_allocate_pgste = 0;
859 EXPORT_SYMBOL(page_table_allocate_pgste);
860 
861 static struct ctl_table page_table_sysctl[] = {
862 	{
863 		.procname	= "allocate_pgste",
864 		.data		= &page_table_allocate_pgste,
865 		.maxlen		= sizeof(int),
866 		.mode		= S_IRUGO | S_IWUSR,
867 		.proc_handler	= proc_dointvec,
868 		.extra1		= &page_table_allocate_pgste_min,
869 		.extra2		= &page_table_allocate_pgste_max,
870 	},
871 	{ }
872 };
873 
874 static struct ctl_table page_table_sysctl_dir[] = {
875 	{
876 		.procname	= "vm",
877 		.maxlen		= 0,
878 		.mode		= 0555,
879 		.child		= page_table_sysctl,
880 	},
881 	{ }
882 };
883 
page_table_register_sysctl(void)884 static int __init page_table_register_sysctl(void)
885 {
886 	return register_sysctl_table(page_table_sysctl_dir) ? 0 : -ENOMEM;
887 }
888 __initcall(page_table_register_sysctl);
889 
890 #else /* CONFIG_PGSTE */
891 
gmap_unlink(struct mm_struct * mm,unsigned long * table,unsigned long vmaddr)892 static inline void gmap_unlink(struct mm_struct *mm, unsigned long *table,
893 			unsigned long vmaddr)
894 {
895 }
896 
897 #endif /* CONFIG_PGSTE */
898 
atomic_xor_bits(atomic_t * v,unsigned int bits)899 static inline unsigned int atomic_xor_bits(atomic_t *v, unsigned int bits)
900 {
901 	unsigned int old, new;
902 
903 	do {
904 		old = atomic_read(v);
905 		new = old ^ bits;
906 	} while (atomic_cmpxchg(v, old, new) != old);
907 	return new;
908 }
909 
910 /*
911  * page table entry allocation/free routines.
912  */
page_table_alloc(struct mm_struct * mm)913 unsigned long *page_table_alloc(struct mm_struct *mm)
914 {
915 	unsigned long *table;
916 	struct page *page;
917 	unsigned int mask, bit;
918 
919 	/* Try to get a fragment of a 4K page as a 2K page table */
920 	if (!mm_alloc_pgste(mm)) {
921 		table = NULL;
922 		spin_lock_bh(&mm->context.list_lock);
923 		if (!list_empty(&mm->context.pgtable_list)) {
924 			page = list_first_entry(&mm->context.pgtable_list,
925 						struct page, lru);
926 			mask = atomic_read(&page->_mapcount);
927 			mask = (mask | (mask >> 4)) & 3;
928 			if (mask != 3) {
929 				table = (unsigned long *) page_to_phys(page);
930 				bit = mask & 1;		/* =1 -> second 2K */
931 				if (bit)
932 					table += PTRS_PER_PTE;
933 				atomic_xor_bits(&page->_mapcount, 1U << bit);
934 				list_del(&page->lru);
935 			}
936 		}
937 		spin_unlock_bh(&mm->context.list_lock);
938 		if (table)
939 			return table;
940 	}
941 	/* Allocate a fresh page */
942 	page = alloc_page(GFP_KERNEL|__GFP_REPEAT);
943 	if (!page)
944 		return NULL;
945 	if (!pgtable_page_ctor(page)) {
946 		__free_page(page);
947 		return NULL;
948 	}
949 	/* Initialize page table */
950 	table = (unsigned long *) page_to_phys(page);
951 	if (mm_alloc_pgste(mm)) {
952 		/* Return 4K page table with PGSTEs */
953 		atomic_set(&page->_mapcount, 3);
954 		clear_table(table, _PAGE_INVALID, PAGE_SIZE/2);
955 		clear_table(table + PTRS_PER_PTE, 0, PAGE_SIZE/2);
956 	} else {
957 		/* Return the first 2K fragment of the page */
958 		atomic_set(&page->_mapcount, 1);
959 		clear_table(table, _PAGE_INVALID, PAGE_SIZE);
960 		spin_lock_bh(&mm->context.list_lock);
961 		list_add(&page->lru, &mm->context.pgtable_list);
962 		spin_unlock_bh(&mm->context.list_lock);
963 	}
964 	return table;
965 }
966 
page_table_free(struct mm_struct * mm,unsigned long * table)967 void page_table_free(struct mm_struct *mm, unsigned long *table)
968 {
969 	struct page *page;
970 	unsigned int bit, mask;
971 
972 	page = pfn_to_page(__pa(table) >> PAGE_SHIFT);
973 	if (!mm_alloc_pgste(mm)) {
974 		/* Free 2K page table fragment of a 4K page */
975 		bit = (__pa(table) & ~PAGE_MASK)/(PTRS_PER_PTE*sizeof(pte_t));
976 		spin_lock_bh(&mm->context.list_lock);
977 		mask = atomic_xor_bits(&page->_mapcount, 1U << bit);
978 		if (mask & 3)
979 			list_add(&page->lru, &mm->context.pgtable_list);
980 		else
981 			list_del(&page->lru);
982 		spin_unlock_bh(&mm->context.list_lock);
983 		if (mask != 0)
984 			return;
985 	}
986 
987 	pgtable_page_dtor(page);
988 	atomic_set(&page->_mapcount, -1);
989 	__free_page(page);
990 }
991 
page_table_free_rcu(struct mmu_gather * tlb,unsigned long * table,unsigned long vmaddr)992 void page_table_free_rcu(struct mmu_gather *tlb, unsigned long *table,
993 			 unsigned long vmaddr)
994 {
995 	struct mm_struct *mm;
996 	struct page *page;
997 	unsigned int bit, mask;
998 
999 	mm = tlb->mm;
1000 	page = pfn_to_page(__pa(table) >> PAGE_SHIFT);
1001 	if (mm_alloc_pgste(mm)) {
1002 		gmap_unlink(mm, table, vmaddr);
1003 		table = (unsigned long *) (__pa(table) | 3);
1004 		tlb_remove_table(tlb, table);
1005 		return;
1006 	}
1007 	bit = (__pa(table) & ~PAGE_MASK) / (PTRS_PER_PTE*sizeof(pte_t));
1008 	spin_lock_bh(&mm->context.list_lock);
1009 	mask = atomic_xor_bits(&page->_mapcount, 0x11U << bit);
1010 	if (mask & 3)
1011 		list_add_tail(&page->lru, &mm->context.pgtable_list);
1012 	else
1013 		list_del(&page->lru);
1014 	spin_unlock_bh(&mm->context.list_lock);
1015 	table = (unsigned long *) (__pa(table) | (1U << bit));
1016 	tlb_remove_table(tlb, table);
1017 }
1018 
__tlb_remove_table(void * _table)1019 static void __tlb_remove_table(void *_table)
1020 {
1021 	unsigned int mask = (unsigned long) _table & 3;
1022 	void *table = (void *)((unsigned long) _table ^ mask);
1023 	struct page *page = pfn_to_page(__pa(table) >> PAGE_SHIFT);
1024 
1025 	switch (mask) {
1026 	case 0:		/* pmd or pud */
1027 		free_pages((unsigned long) table, 2);
1028 		break;
1029 	case 1:		/* lower 2K of a 4K page table */
1030 	case 2:		/* higher 2K of a 4K page table */
1031 		if (atomic_xor_bits(&page->_mapcount, mask << 4) != 0)
1032 			break;
1033 		/* fallthrough */
1034 	case 3:		/* 4K page table with pgstes */
1035 		pgtable_page_dtor(page);
1036 		atomic_set(&page->_mapcount, -1);
1037 		__free_page(page);
1038 		break;
1039 	}
1040 }
1041 
tlb_remove_table_smp_sync(void * arg)1042 static void tlb_remove_table_smp_sync(void *arg)
1043 {
1044 	/* Simply deliver the interrupt */
1045 }
1046 
tlb_remove_table_one(void * table)1047 static void tlb_remove_table_one(void *table)
1048 {
1049 	/*
1050 	 * This isn't an RCU grace period and hence the page-tables cannot be
1051 	 * assumed to be actually RCU-freed.
1052 	 *
1053 	 * It is however sufficient for software page-table walkers that rely
1054 	 * on IRQ disabling. See the comment near struct mmu_table_batch.
1055 	 */
1056 	smp_call_function(tlb_remove_table_smp_sync, NULL, 1);
1057 	__tlb_remove_table(table);
1058 }
1059 
tlb_remove_table_rcu(struct rcu_head * head)1060 static void tlb_remove_table_rcu(struct rcu_head *head)
1061 {
1062 	struct mmu_table_batch *batch;
1063 	int i;
1064 
1065 	batch = container_of(head, struct mmu_table_batch, rcu);
1066 
1067 	for (i = 0; i < batch->nr; i++)
1068 		__tlb_remove_table(batch->tables[i]);
1069 
1070 	free_page((unsigned long)batch);
1071 }
1072 
tlb_table_flush(struct mmu_gather * tlb)1073 void tlb_table_flush(struct mmu_gather *tlb)
1074 {
1075 	struct mmu_table_batch **batch = &tlb->batch;
1076 
1077 	if (*batch) {
1078 		call_rcu_sched(&(*batch)->rcu, tlb_remove_table_rcu);
1079 		*batch = NULL;
1080 	}
1081 }
1082 
tlb_remove_table(struct mmu_gather * tlb,void * table)1083 void tlb_remove_table(struct mmu_gather *tlb, void *table)
1084 {
1085 	struct mmu_table_batch **batch = &tlb->batch;
1086 
1087 	tlb->mm->context.flush_mm = 1;
1088 	if (*batch == NULL) {
1089 		*batch = (struct mmu_table_batch *)
1090 			__get_free_page(GFP_NOWAIT | __GFP_NOWARN);
1091 		if (*batch == NULL) {
1092 			__tlb_flush_mm_lazy(tlb->mm);
1093 			tlb_remove_table_one(table);
1094 			return;
1095 		}
1096 		(*batch)->nr = 0;
1097 	}
1098 	(*batch)->tables[(*batch)->nr++] = table;
1099 	if ((*batch)->nr == MAX_TABLE_BATCH)
1100 		tlb_flush_mmu(tlb);
1101 }
1102 
1103 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
thp_split_vma(struct vm_area_struct * vma)1104 static inline void thp_split_vma(struct vm_area_struct *vma)
1105 {
1106 	unsigned long addr;
1107 
1108 	for (addr = vma->vm_start; addr < vma->vm_end; addr += PAGE_SIZE)
1109 		follow_page(vma, addr, FOLL_SPLIT);
1110 }
1111 
thp_split_mm(struct mm_struct * mm)1112 static inline void thp_split_mm(struct mm_struct *mm)
1113 {
1114 	struct vm_area_struct *vma;
1115 
1116 	for (vma = mm->mmap; vma != NULL; vma = vma->vm_next) {
1117 		thp_split_vma(vma);
1118 		vma->vm_flags &= ~VM_HUGEPAGE;
1119 		vma->vm_flags |= VM_NOHUGEPAGE;
1120 	}
1121 	mm->def_flags |= VM_NOHUGEPAGE;
1122 }
1123 #else
thp_split_mm(struct mm_struct * mm)1124 static inline void thp_split_mm(struct mm_struct *mm)
1125 {
1126 }
1127 #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
1128 
1129 /*
1130  * switch on pgstes for its userspace process (for kvm)
1131  */
s390_enable_sie(void)1132 int s390_enable_sie(void)
1133 {
1134 	struct mm_struct *mm = current->mm;
1135 
1136 	/* Do we have pgstes? if yes, we are done */
1137 	if (mm_has_pgste(mm))
1138 		return 0;
1139 	/* Fail if the page tables are 2K */
1140 	if (!mm_alloc_pgste(mm))
1141 		return -EINVAL;
1142 	down_write(&mm->mmap_sem);
1143 	mm->context.has_pgste = 1;
1144 	/* split thp mappings and disable thp for future mappings */
1145 	thp_split_mm(mm);
1146 	up_write(&mm->mmap_sem);
1147 	return 0;
1148 }
1149 EXPORT_SYMBOL_GPL(s390_enable_sie);
1150 
1151 /*
1152  * Enable storage key handling from now on and initialize the storage
1153  * keys with the default key.
1154  */
__s390_enable_skey(pte_t * pte,unsigned long addr,unsigned long next,struct mm_walk * walk)1155 static int __s390_enable_skey(pte_t *pte, unsigned long addr,
1156 			      unsigned long next, struct mm_walk *walk)
1157 {
1158 	unsigned long ptev;
1159 	pgste_t pgste;
1160 
1161 	pgste = pgste_get_lock(pte);
1162 	/*
1163 	 * Remove all zero page mappings,
1164 	 * after establishing a policy to forbid zero page mappings
1165 	 * following faults for that page will get fresh anonymous pages
1166 	 */
1167 	if (is_zero_pfn(pte_pfn(*pte))) {
1168 		ptep_flush_direct(walk->mm, addr, pte);
1169 		pte_val(*pte) = _PAGE_INVALID;
1170 	}
1171 	/* Clear storage key */
1172 	pgste_val(pgste) &= ~(PGSTE_ACC_BITS | PGSTE_FP_BIT |
1173 			      PGSTE_GR_BIT | PGSTE_GC_BIT);
1174 	ptev = pte_val(*pte);
1175 	if (!(ptev & _PAGE_INVALID) && (ptev & _PAGE_WRITE))
1176 		page_set_storage_key(ptev & PAGE_MASK, PAGE_DEFAULT_KEY, 1);
1177 	pgste_set_unlock(pte, pgste);
1178 	return 0;
1179 }
1180 
s390_enable_skey(void)1181 int s390_enable_skey(void)
1182 {
1183 	struct mm_walk walk = { .pte_entry = __s390_enable_skey };
1184 	struct mm_struct *mm = current->mm;
1185 	struct vm_area_struct *vma;
1186 	int rc = 0;
1187 
1188 	down_write(&mm->mmap_sem);
1189 	if (mm_use_skey(mm))
1190 		goto out_up;
1191 
1192 	mm->context.use_skey = 1;
1193 	for (vma = mm->mmap; vma; vma = vma->vm_next) {
1194 		if (ksm_madvise(vma, vma->vm_start, vma->vm_end,
1195 				MADV_UNMERGEABLE, &vma->vm_flags)) {
1196 			mm->context.use_skey = 0;
1197 			rc = -ENOMEM;
1198 			goto out_up;
1199 		}
1200 	}
1201 	mm->def_flags &= ~VM_MERGEABLE;
1202 
1203 	walk.mm = mm;
1204 	walk_page_range(0, TASK_SIZE, &walk);
1205 
1206 out_up:
1207 	up_write(&mm->mmap_sem);
1208 	return rc;
1209 }
1210 EXPORT_SYMBOL_GPL(s390_enable_skey);
1211 
1212 /*
1213  * Reset CMMA state, make all pages stable again.
1214  */
__s390_reset_cmma(pte_t * pte,unsigned long addr,unsigned long next,struct mm_walk * walk)1215 static int __s390_reset_cmma(pte_t *pte, unsigned long addr,
1216 			     unsigned long next, struct mm_walk *walk)
1217 {
1218 	pgste_t pgste;
1219 
1220 	pgste = pgste_get_lock(pte);
1221 	pgste_val(pgste) &= ~_PGSTE_GPS_USAGE_MASK;
1222 	pgste_set_unlock(pte, pgste);
1223 	return 0;
1224 }
1225 
s390_reset_cmma(struct mm_struct * mm)1226 void s390_reset_cmma(struct mm_struct *mm)
1227 {
1228 	struct mm_walk walk = { .pte_entry = __s390_reset_cmma };
1229 
1230 	down_write(&mm->mmap_sem);
1231 	walk.mm = mm;
1232 	walk_page_range(0, TASK_SIZE, &walk);
1233 	up_write(&mm->mmap_sem);
1234 }
1235 EXPORT_SYMBOL_GPL(s390_reset_cmma);
1236 
1237 /*
1238  * Test and reset if a guest page is dirty
1239  */
gmap_test_and_clear_dirty(unsigned long address,struct gmap * gmap)1240 bool gmap_test_and_clear_dirty(unsigned long address, struct gmap *gmap)
1241 {
1242 	pgd_t *pgd;
1243 	pud_t *pud;
1244 	pmd_t *pmd;
1245 	pte_t *pte;
1246 	spinlock_t *ptl;
1247 	bool dirty = false;
1248 
1249 	pgd = pgd_offset(gmap->mm, address);
1250 	pud = pud_alloc(gmap->mm, pgd, address);
1251 	if (!pud)
1252 		return false;
1253 	pmd = pmd_alloc(gmap->mm, pud, address);
1254 	if (!pmd)
1255 		return false;
1256 	/* We can't run guests backed by huge pages, but userspace can
1257 	 * still set them up and then try to migrate them without any
1258 	 * migration support.
1259 	 */
1260 	if (pmd_large(*pmd))
1261 		return true;
1262 
1263 	pte = pte_alloc_map_lock(gmap->mm, pmd, address, &ptl);
1264 	if (unlikely(!pte))
1265 		return false;
1266 
1267 	if (ptep_test_and_clear_user_dirty(gmap->mm, address, pte))
1268 		dirty = true;
1269 
1270 	spin_unlock(ptl);
1271 	return dirty;
1272 }
1273 EXPORT_SYMBOL_GPL(gmap_test_and_clear_dirty);
1274 
1275 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
pmdp_clear_flush_young(struct vm_area_struct * vma,unsigned long address,pmd_t * pmdp)1276 int pmdp_clear_flush_young(struct vm_area_struct *vma, unsigned long address,
1277 			   pmd_t *pmdp)
1278 {
1279 	VM_BUG_ON(address & ~HPAGE_PMD_MASK);
1280 	/* No need to flush TLB
1281 	 * On s390 reference bits are in storage key and never in TLB */
1282 	return pmdp_test_and_clear_young(vma, address, pmdp);
1283 }
1284 
pmdp_set_access_flags(struct vm_area_struct * vma,unsigned long address,pmd_t * pmdp,pmd_t entry,int dirty)1285 int pmdp_set_access_flags(struct vm_area_struct *vma,
1286 			  unsigned long address, pmd_t *pmdp,
1287 			  pmd_t entry, int dirty)
1288 {
1289 	VM_BUG_ON(address & ~HPAGE_PMD_MASK);
1290 
1291 	entry = pmd_mkyoung(entry);
1292 	if (dirty)
1293 		entry = pmd_mkdirty(entry);
1294 	if (pmd_same(*pmdp, entry))
1295 		return 0;
1296 	pmdp_invalidate(vma, address, pmdp);
1297 	set_pmd_at(vma->vm_mm, address, pmdp, entry);
1298 	return 1;
1299 }
1300 
pmdp_splitting_flush_sync(void * arg)1301 static void pmdp_splitting_flush_sync(void *arg)
1302 {
1303 	/* Simply deliver the interrupt */
1304 }
1305 
pmdp_splitting_flush(struct vm_area_struct * vma,unsigned long address,pmd_t * pmdp)1306 void pmdp_splitting_flush(struct vm_area_struct *vma, unsigned long address,
1307 			  pmd_t *pmdp)
1308 {
1309 	VM_BUG_ON(address & ~HPAGE_PMD_MASK);
1310 	if (!test_and_set_bit(_SEGMENT_ENTRY_SPLIT_BIT,
1311 			      (unsigned long *) pmdp)) {
1312 		/* need to serialize against gup-fast (IRQ disabled) */
1313 		smp_call_function(pmdp_splitting_flush_sync, NULL, 1);
1314 	}
1315 }
1316 
pgtable_trans_huge_deposit(struct mm_struct * mm,pmd_t * pmdp,pgtable_t pgtable)1317 void pgtable_trans_huge_deposit(struct mm_struct *mm, pmd_t *pmdp,
1318 				pgtable_t pgtable)
1319 {
1320 	struct list_head *lh = (struct list_head *) pgtable;
1321 
1322 	assert_spin_locked(pmd_lockptr(mm, pmdp));
1323 
1324 	/* FIFO */
1325 	if (!pmd_huge_pte(mm, pmdp))
1326 		INIT_LIST_HEAD(lh);
1327 	else
1328 		list_add(lh, (struct list_head *) pmd_huge_pte(mm, pmdp));
1329 	pmd_huge_pte(mm, pmdp) = pgtable;
1330 }
1331 
pgtable_trans_huge_withdraw(struct mm_struct * mm,pmd_t * pmdp)1332 pgtable_t pgtable_trans_huge_withdraw(struct mm_struct *mm, pmd_t *pmdp)
1333 {
1334 	struct list_head *lh;
1335 	pgtable_t pgtable;
1336 	pte_t *ptep;
1337 
1338 	assert_spin_locked(pmd_lockptr(mm, pmdp));
1339 
1340 	/* FIFO */
1341 	pgtable = pmd_huge_pte(mm, pmdp);
1342 	lh = (struct list_head *) pgtable;
1343 	if (list_empty(lh))
1344 		pmd_huge_pte(mm, pmdp) = NULL;
1345 	else {
1346 		pmd_huge_pte(mm, pmdp) = (pgtable_t) lh->next;
1347 		list_del(lh);
1348 	}
1349 	ptep = (pte_t *) pgtable;
1350 	pte_val(*ptep) = _PAGE_INVALID;
1351 	ptep++;
1352 	pte_val(*ptep) = _PAGE_INVALID;
1353 	return pgtable;
1354 }
1355 #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
1356