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