Loading...
1// SPDX-License-Identifier: GPL-2.0
2/*
3 * Copyright IBM Corp. 2007, 2011
4 * Author(s): Martin Schwidefsky <schwidefsky@de.ibm.com>
5 */
6
7#include <linux/sched.h>
8#include <linux/kernel.h>
9#include <linux/errno.h>
10#include <linux/gfp.h>
11#include <linux/mm.h>
12#include <linux/swap.h>
13#include <linux/smp.h>
14#include <linux/spinlock.h>
15#include <linux/rcupdate.h>
16#include <linux/slab.h>
17#include <linux/swapops.h>
18#include <linux/sysctl.h>
19#include <linux/ksm.h>
20#include <linux/mman.h>
21
22#include <asm/pgtable.h>
23#include <asm/pgalloc.h>
24#include <asm/tlb.h>
25#include <asm/tlbflush.h>
26#include <asm/mmu_context.h>
27#include <asm/page-states.h>
28
29static inline void ptep_ipte_local(struct mm_struct *mm, unsigned long addr,
30 pte_t *ptep, int nodat)
31{
32 unsigned long opt, asce;
33
34 if (MACHINE_HAS_TLB_GUEST) {
35 opt = 0;
36 asce = READ_ONCE(mm->context.gmap_asce);
37 if (asce == 0UL || nodat)
38 opt |= IPTE_NODAT;
39 if (asce != -1UL) {
40 asce = asce ? : mm->context.asce;
41 opt |= IPTE_GUEST_ASCE;
42 }
43 __ptep_ipte(addr, ptep, opt, asce, IPTE_LOCAL);
44 } else {
45 __ptep_ipte(addr, ptep, 0, 0, IPTE_LOCAL);
46 }
47}
48
49static inline void ptep_ipte_global(struct mm_struct *mm, unsigned long addr,
50 pte_t *ptep, int nodat)
51{
52 unsigned long opt, asce;
53
54 if (MACHINE_HAS_TLB_GUEST) {
55 opt = 0;
56 asce = READ_ONCE(mm->context.gmap_asce);
57 if (asce == 0UL || nodat)
58 opt |= IPTE_NODAT;
59 if (asce != -1UL) {
60 asce = asce ? : mm->context.asce;
61 opt |= IPTE_GUEST_ASCE;
62 }
63 __ptep_ipte(addr, ptep, opt, asce, IPTE_GLOBAL);
64 } else {
65 __ptep_ipte(addr, ptep, 0, 0, IPTE_GLOBAL);
66 }
67}
68
69static inline pte_t ptep_flush_direct(struct mm_struct *mm,
70 unsigned long addr, pte_t *ptep,
71 int nodat)
72{
73 pte_t old;
74
75 old = *ptep;
76 if (unlikely(pte_val(old) & _PAGE_INVALID))
77 return old;
78 atomic_inc(&mm->context.flush_count);
79 if (MACHINE_HAS_TLB_LC &&
80 cpumask_equal(mm_cpumask(mm), cpumask_of(smp_processor_id())))
81 ptep_ipte_local(mm, addr, ptep, nodat);
82 else
83 ptep_ipte_global(mm, addr, ptep, nodat);
84 atomic_dec(&mm->context.flush_count);
85 return old;
86}
87
88static inline pte_t ptep_flush_lazy(struct mm_struct *mm,
89 unsigned long addr, pte_t *ptep,
90 int nodat)
91{
92 pte_t old;
93
94 old = *ptep;
95 if (unlikely(pte_val(old) & _PAGE_INVALID))
96 return old;
97 atomic_inc(&mm->context.flush_count);
98 if (cpumask_equal(&mm->context.cpu_attach_mask,
99 cpumask_of(smp_processor_id()))) {
100 pte_val(*ptep) |= _PAGE_INVALID;
101 mm->context.flush_mm = 1;
102 } else
103 ptep_ipte_global(mm, addr, ptep, nodat);
104 atomic_dec(&mm->context.flush_count);
105 return old;
106}
107
108static inline pgste_t pgste_get_lock(pte_t *ptep)
109{
110 unsigned long new = 0;
111#ifdef CONFIG_PGSTE
112 unsigned long old;
113
114 asm(
115 " lg %0,%2\n"
116 "0: lgr %1,%0\n"
117 " nihh %0,0xff7f\n" /* clear PCL bit in old */
118 " oihh %1,0x0080\n" /* set PCL bit in new */
119 " csg %0,%1,%2\n"
120 " jl 0b\n"
121 : "=&d" (old), "=&d" (new), "=Q" (ptep[PTRS_PER_PTE])
122 : "Q" (ptep[PTRS_PER_PTE]) : "cc", "memory");
123#endif
124 return __pgste(new);
125}
126
127static inline void pgste_set_unlock(pte_t *ptep, pgste_t pgste)
128{
129#ifdef CONFIG_PGSTE
130 asm(
131 " nihh %1,0xff7f\n" /* clear PCL bit */
132 " stg %1,%0\n"
133 : "=Q" (ptep[PTRS_PER_PTE])
134 : "d" (pgste_val(pgste)), "Q" (ptep[PTRS_PER_PTE])
135 : "cc", "memory");
136#endif
137}
138
139static inline pgste_t pgste_get(pte_t *ptep)
140{
141 unsigned long pgste = 0;
142#ifdef CONFIG_PGSTE
143 pgste = *(unsigned long *)(ptep + PTRS_PER_PTE);
144#endif
145 return __pgste(pgste);
146}
147
148static inline void pgste_set(pte_t *ptep, pgste_t pgste)
149{
150#ifdef CONFIG_PGSTE
151 *(pgste_t *)(ptep + PTRS_PER_PTE) = pgste;
152#endif
153}
154
155static inline pgste_t pgste_update_all(pte_t pte, pgste_t pgste,
156 struct mm_struct *mm)
157{
158#ifdef CONFIG_PGSTE
159 unsigned long address, bits, skey;
160
161 if (!mm_uses_skeys(mm) || pte_val(pte) & _PAGE_INVALID)
162 return pgste;
163 address = pte_val(pte) & PAGE_MASK;
164 skey = (unsigned long) page_get_storage_key(address);
165 bits = skey & (_PAGE_CHANGED | _PAGE_REFERENCED);
166 /* Transfer page changed & referenced bit to guest bits in pgste */
167 pgste_val(pgste) |= bits << 48; /* GR bit & GC bit */
168 /* Copy page access key and fetch protection bit to pgste */
169 pgste_val(pgste) &= ~(PGSTE_ACC_BITS | PGSTE_FP_BIT);
170 pgste_val(pgste) |= (skey & (_PAGE_ACC_BITS | _PAGE_FP_BIT)) << 56;
171#endif
172 return pgste;
173
174}
175
176static inline void pgste_set_key(pte_t *ptep, pgste_t pgste, pte_t entry,
177 struct mm_struct *mm)
178{
179#ifdef CONFIG_PGSTE
180 unsigned long address;
181 unsigned long nkey;
182
183 if (!mm_uses_skeys(mm) || pte_val(entry) & _PAGE_INVALID)
184 return;
185 VM_BUG_ON(!(pte_val(*ptep) & _PAGE_INVALID));
186 address = pte_val(entry) & PAGE_MASK;
187 /*
188 * Set page access key and fetch protection bit from pgste.
189 * The guest C/R information is still in the PGSTE, set real
190 * key C/R to 0.
191 */
192 nkey = (pgste_val(pgste) & (PGSTE_ACC_BITS | PGSTE_FP_BIT)) >> 56;
193 nkey |= (pgste_val(pgste) & (PGSTE_GR_BIT | PGSTE_GC_BIT)) >> 48;
194 page_set_storage_key(address, nkey, 0);
195#endif
196}
197
198static inline pgste_t pgste_set_pte(pte_t *ptep, pgste_t pgste, pte_t entry)
199{
200#ifdef CONFIG_PGSTE
201 if ((pte_val(entry) & _PAGE_PRESENT) &&
202 (pte_val(entry) & _PAGE_WRITE) &&
203 !(pte_val(entry) & _PAGE_INVALID)) {
204 if (!MACHINE_HAS_ESOP) {
205 /*
206 * Without enhanced suppression-on-protection force
207 * the dirty bit on for all writable ptes.
208 */
209 pte_val(entry) |= _PAGE_DIRTY;
210 pte_val(entry) &= ~_PAGE_PROTECT;
211 }
212 if (!(pte_val(entry) & _PAGE_PROTECT))
213 /* This pte allows write access, set user-dirty */
214 pgste_val(pgste) |= PGSTE_UC_BIT;
215 }
216#endif
217 *ptep = entry;
218 return pgste;
219}
220
221static inline pgste_t pgste_pte_notify(struct mm_struct *mm,
222 unsigned long addr,
223 pte_t *ptep, pgste_t pgste)
224{
225#ifdef CONFIG_PGSTE
226 unsigned long bits;
227
228 bits = pgste_val(pgste) & (PGSTE_IN_BIT | PGSTE_VSIE_BIT);
229 if (bits) {
230 pgste_val(pgste) ^= bits;
231 ptep_notify(mm, addr, ptep, bits);
232 }
233#endif
234 return pgste;
235}
236
237static inline pgste_t ptep_xchg_start(struct mm_struct *mm,
238 unsigned long addr, pte_t *ptep)
239{
240 pgste_t pgste = __pgste(0);
241
242 if (mm_has_pgste(mm)) {
243 pgste = pgste_get_lock(ptep);
244 pgste = pgste_pte_notify(mm, addr, ptep, pgste);
245 }
246 return pgste;
247}
248
249static inline pte_t ptep_xchg_commit(struct mm_struct *mm,
250 unsigned long addr, pte_t *ptep,
251 pgste_t pgste, pte_t old, pte_t new)
252{
253 if (mm_has_pgste(mm)) {
254 if (pte_val(old) & _PAGE_INVALID)
255 pgste_set_key(ptep, pgste, new, mm);
256 if (pte_val(new) & _PAGE_INVALID) {
257 pgste = pgste_update_all(old, pgste, mm);
258 if ((pgste_val(pgste) & _PGSTE_GPS_USAGE_MASK) ==
259 _PGSTE_GPS_USAGE_UNUSED)
260 pte_val(old) |= _PAGE_UNUSED;
261 }
262 pgste = pgste_set_pte(ptep, pgste, new);
263 pgste_set_unlock(ptep, pgste);
264 } else {
265 *ptep = new;
266 }
267 return old;
268}
269
270pte_t ptep_xchg_direct(struct mm_struct *mm, unsigned long addr,
271 pte_t *ptep, pte_t new)
272{
273 pgste_t pgste;
274 pte_t old;
275 int nodat;
276
277 preempt_disable();
278 pgste = ptep_xchg_start(mm, addr, ptep);
279 nodat = !!(pgste_val(pgste) & _PGSTE_GPS_NODAT);
280 old = ptep_flush_direct(mm, addr, ptep, nodat);
281 old = ptep_xchg_commit(mm, addr, ptep, pgste, old, new);
282 preempt_enable();
283 return old;
284}
285EXPORT_SYMBOL(ptep_xchg_direct);
286
287pte_t ptep_xchg_lazy(struct mm_struct *mm, unsigned long addr,
288 pte_t *ptep, pte_t new)
289{
290 pgste_t pgste;
291 pte_t old;
292 int nodat;
293
294 preempt_disable();
295 pgste = ptep_xchg_start(mm, addr, ptep);
296 nodat = !!(pgste_val(pgste) & _PGSTE_GPS_NODAT);
297 old = ptep_flush_lazy(mm, addr, ptep, nodat);
298 old = ptep_xchg_commit(mm, addr, ptep, pgste, old, new);
299 preempt_enable();
300 return old;
301}
302EXPORT_SYMBOL(ptep_xchg_lazy);
303
304pte_t ptep_modify_prot_start(struct vm_area_struct *vma, unsigned long addr,
305 pte_t *ptep)
306{
307 pgste_t pgste;
308 pte_t old;
309 int nodat;
310 struct mm_struct *mm = vma->vm_mm;
311
312 preempt_disable();
313 pgste = ptep_xchg_start(mm, addr, ptep);
314 nodat = !!(pgste_val(pgste) & _PGSTE_GPS_NODAT);
315 old = ptep_flush_lazy(mm, addr, ptep, nodat);
316 if (mm_has_pgste(mm)) {
317 pgste = pgste_update_all(old, pgste, mm);
318 pgste_set(ptep, pgste);
319 }
320 return old;
321}
322
323void ptep_modify_prot_commit(struct vm_area_struct *vma, unsigned long addr,
324 pte_t *ptep, pte_t old_pte, pte_t pte)
325{
326 pgste_t pgste;
327 struct mm_struct *mm = vma->vm_mm;
328
329 if (!MACHINE_HAS_NX)
330 pte_val(pte) &= ~_PAGE_NOEXEC;
331 if (mm_has_pgste(mm)) {
332 pgste = pgste_get(ptep);
333 pgste_set_key(ptep, pgste, pte, mm);
334 pgste = pgste_set_pte(ptep, pgste, pte);
335 pgste_set_unlock(ptep, pgste);
336 } else {
337 *ptep = pte;
338 }
339 preempt_enable();
340}
341
342static inline void pmdp_idte_local(struct mm_struct *mm,
343 unsigned long addr, pmd_t *pmdp)
344{
345 if (MACHINE_HAS_TLB_GUEST)
346 __pmdp_idte(addr, pmdp, IDTE_NODAT | IDTE_GUEST_ASCE,
347 mm->context.asce, IDTE_LOCAL);
348 else
349 __pmdp_idte(addr, pmdp, 0, 0, IDTE_LOCAL);
350 if (mm_has_pgste(mm) && mm->context.allow_gmap_hpage_1m)
351 gmap_pmdp_idte_local(mm, addr);
352}
353
354static inline void pmdp_idte_global(struct mm_struct *mm,
355 unsigned long addr, pmd_t *pmdp)
356{
357 if (MACHINE_HAS_TLB_GUEST) {
358 __pmdp_idte(addr, pmdp, IDTE_NODAT | IDTE_GUEST_ASCE,
359 mm->context.asce, IDTE_GLOBAL);
360 if (mm_has_pgste(mm) && mm->context.allow_gmap_hpage_1m)
361 gmap_pmdp_idte_global(mm, addr);
362 } else if (MACHINE_HAS_IDTE) {
363 __pmdp_idte(addr, pmdp, 0, 0, IDTE_GLOBAL);
364 if (mm_has_pgste(mm) && mm->context.allow_gmap_hpage_1m)
365 gmap_pmdp_idte_global(mm, addr);
366 } else {
367 __pmdp_csp(pmdp);
368 if (mm_has_pgste(mm) && mm->context.allow_gmap_hpage_1m)
369 gmap_pmdp_csp(mm, addr);
370 }
371}
372
373static inline pmd_t pmdp_flush_direct(struct mm_struct *mm,
374 unsigned long addr, pmd_t *pmdp)
375{
376 pmd_t old;
377
378 old = *pmdp;
379 if (pmd_val(old) & _SEGMENT_ENTRY_INVALID)
380 return old;
381 atomic_inc(&mm->context.flush_count);
382 if (MACHINE_HAS_TLB_LC &&
383 cpumask_equal(mm_cpumask(mm), cpumask_of(smp_processor_id())))
384 pmdp_idte_local(mm, addr, pmdp);
385 else
386 pmdp_idte_global(mm, addr, pmdp);
387 atomic_dec(&mm->context.flush_count);
388 return old;
389}
390
391static inline pmd_t pmdp_flush_lazy(struct mm_struct *mm,
392 unsigned long addr, pmd_t *pmdp)
393{
394 pmd_t old;
395
396 old = *pmdp;
397 if (pmd_val(old) & _SEGMENT_ENTRY_INVALID)
398 return old;
399 atomic_inc(&mm->context.flush_count);
400 if (cpumask_equal(&mm->context.cpu_attach_mask,
401 cpumask_of(smp_processor_id()))) {
402 pmd_val(*pmdp) |= _SEGMENT_ENTRY_INVALID;
403 mm->context.flush_mm = 1;
404 if (mm_has_pgste(mm))
405 gmap_pmdp_invalidate(mm, addr);
406 } else {
407 pmdp_idte_global(mm, addr, pmdp);
408 }
409 atomic_dec(&mm->context.flush_count);
410 return old;
411}
412
413#ifdef CONFIG_PGSTE
414static pmd_t *pmd_alloc_map(struct mm_struct *mm, unsigned long addr)
415{
416 pgd_t *pgd;
417 p4d_t *p4d;
418 pud_t *pud;
419 pmd_t *pmd;
420
421 pgd = pgd_offset(mm, addr);
422 p4d = p4d_alloc(mm, pgd, addr);
423 if (!p4d)
424 return NULL;
425 pud = pud_alloc(mm, p4d, addr);
426 if (!pud)
427 return NULL;
428 pmd = pmd_alloc(mm, pud, addr);
429 return pmd;
430}
431#endif
432
433pmd_t pmdp_xchg_direct(struct mm_struct *mm, unsigned long addr,
434 pmd_t *pmdp, pmd_t new)
435{
436 pmd_t old;
437
438 preempt_disable();
439 old = pmdp_flush_direct(mm, addr, pmdp);
440 *pmdp = new;
441 preempt_enable();
442 return old;
443}
444EXPORT_SYMBOL(pmdp_xchg_direct);
445
446pmd_t pmdp_xchg_lazy(struct mm_struct *mm, unsigned long addr,
447 pmd_t *pmdp, pmd_t new)
448{
449 pmd_t old;
450
451 preempt_disable();
452 old = pmdp_flush_lazy(mm, addr, pmdp);
453 *pmdp = new;
454 preempt_enable();
455 return old;
456}
457EXPORT_SYMBOL(pmdp_xchg_lazy);
458
459static inline void pudp_idte_local(struct mm_struct *mm,
460 unsigned long addr, pud_t *pudp)
461{
462 if (MACHINE_HAS_TLB_GUEST)
463 __pudp_idte(addr, pudp, IDTE_NODAT | IDTE_GUEST_ASCE,
464 mm->context.asce, IDTE_LOCAL);
465 else
466 __pudp_idte(addr, pudp, 0, 0, IDTE_LOCAL);
467}
468
469static inline void pudp_idte_global(struct mm_struct *mm,
470 unsigned long addr, pud_t *pudp)
471{
472 if (MACHINE_HAS_TLB_GUEST)
473 __pudp_idte(addr, pudp, IDTE_NODAT | IDTE_GUEST_ASCE,
474 mm->context.asce, IDTE_GLOBAL);
475 else if (MACHINE_HAS_IDTE)
476 __pudp_idte(addr, pudp, 0, 0, IDTE_GLOBAL);
477 else
478 /*
479 * Invalid bit position is the same for pmd and pud, so we can
480 * re-use _pmd_csp() here
481 */
482 __pmdp_csp((pmd_t *) pudp);
483}
484
485static inline pud_t pudp_flush_direct(struct mm_struct *mm,
486 unsigned long addr, pud_t *pudp)
487{
488 pud_t old;
489
490 old = *pudp;
491 if (pud_val(old) & _REGION_ENTRY_INVALID)
492 return old;
493 atomic_inc(&mm->context.flush_count);
494 if (MACHINE_HAS_TLB_LC &&
495 cpumask_equal(mm_cpumask(mm), cpumask_of(smp_processor_id())))
496 pudp_idte_local(mm, addr, pudp);
497 else
498 pudp_idte_global(mm, addr, pudp);
499 atomic_dec(&mm->context.flush_count);
500 return old;
501}
502
503pud_t pudp_xchg_direct(struct mm_struct *mm, unsigned long addr,
504 pud_t *pudp, pud_t new)
505{
506 pud_t old;
507
508 preempt_disable();
509 old = pudp_flush_direct(mm, addr, pudp);
510 *pudp = new;
511 preempt_enable();
512 return old;
513}
514EXPORT_SYMBOL(pudp_xchg_direct);
515
516#ifdef CONFIG_TRANSPARENT_HUGEPAGE
517void pgtable_trans_huge_deposit(struct mm_struct *mm, pmd_t *pmdp,
518 pgtable_t pgtable)
519{
520 struct list_head *lh = (struct list_head *) pgtable;
521
522 assert_spin_locked(pmd_lockptr(mm, pmdp));
523
524 /* FIFO */
525 if (!pmd_huge_pte(mm, pmdp))
526 INIT_LIST_HEAD(lh);
527 else
528 list_add(lh, (struct list_head *) pmd_huge_pte(mm, pmdp));
529 pmd_huge_pte(mm, pmdp) = pgtable;
530}
531
532pgtable_t pgtable_trans_huge_withdraw(struct mm_struct *mm, pmd_t *pmdp)
533{
534 struct list_head *lh;
535 pgtable_t pgtable;
536 pte_t *ptep;
537
538 assert_spin_locked(pmd_lockptr(mm, pmdp));
539
540 /* FIFO */
541 pgtable = pmd_huge_pte(mm, pmdp);
542 lh = (struct list_head *) pgtable;
543 if (list_empty(lh))
544 pmd_huge_pte(mm, pmdp) = NULL;
545 else {
546 pmd_huge_pte(mm, pmdp) = (pgtable_t) lh->next;
547 list_del(lh);
548 }
549 ptep = (pte_t *) pgtable;
550 pte_val(*ptep) = _PAGE_INVALID;
551 ptep++;
552 pte_val(*ptep) = _PAGE_INVALID;
553 return pgtable;
554}
555#endif /* CONFIG_TRANSPARENT_HUGEPAGE */
556
557#ifdef CONFIG_PGSTE
558void ptep_set_pte_at(struct mm_struct *mm, unsigned long addr,
559 pte_t *ptep, pte_t entry)
560{
561 pgste_t pgste;
562
563 /* the mm_has_pgste() check is done in set_pte_at() */
564 preempt_disable();
565 pgste = pgste_get_lock(ptep);
566 pgste_val(pgste) &= ~_PGSTE_GPS_ZERO;
567 pgste_set_key(ptep, pgste, entry, mm);
568 pgste = pgste_set_pte(ptep, pgste, entry);
569 pgste_set_unlock(ptep, pgste);
570 preempt_enable();
571}
572
573void ptep_set_notify(struct mm_struct *mm, unsigned long addr, pte_t *ptep)
574{
575 pgste_t pgste;
576
577 preempt_disable();
578 pgste = pgste_get_lock(ptep);
579 pgste_val(pgste) |= PGSTE_IN_BIT;
580 pgste_set_unlock(ptep, pgste);
581 preempt_enable();
582}
583
584/**
585 * ptep_force_prot - change access rights of a locked pte
586 * @mm: pointer to the process mm_struct
587 * @addr: virtual address in the guest address space
588 * @ptep: pointer to the page table entry
589 * @prot: indicates guest access rights: PROT_NONE, PROT_READ or PROT_WRITE
590 * @bit: pgste bit to set (e.g. for notification)
591 *
592 * Returns 0 if the access rights were changed and -EAGAIN if the current
593 * and requested access rights are incompatible.
594 */
595int ptep_force_prot(struct mm_struct *mm, unsigned long addr,
596 pte_t *ptep, int prot, unsigned long bit)
597{
598 pte_t entry;
599 pgste_t pgste;
600 int pte_i, pte_p, nodat;
601
602 pgste = pgste_get_lock(ptep);
603 entry = *ptep;
604 /* Check pte entry after all locks have been acquired */
605 pte_i = pte_val(entry) & _PAGE_INVALID;
606 pte_p = pte_val(entry) & _PAGE_PROTECT;
607 if ((pte_i && (prot != PROT_NONE)) ||
608 (pte_p && (prot & PROT_WRITE))) {
609 pgste_set_unlock(ptep, pgste);
610 return -EAGAIN;
611 }
612 /* Change access rights and set pgste bit */
613 nodat = !!(pgste_val(pgste) & _PGSTE_GPS_NODAT);
614 if (prot == PROT_NONE && !pte_i) {
615 ptep_flush_direct(mm, addr, ptep, nodat);
616 pgste = pgste_update_all(entry, pgste, mm);
617 pte_val(entry) |= _PAGE_INVALID;
618 }
619 if (prot == PROT_READ && !pte_p) {
620 ptep_flush_direct(mm, addr, ptep, nodat);
621 pte_val(entry) &= ~_PAGE_INVALID;
622 pte_val(entry) |= _PAGE_PROTECT;
623 }
624 pgste_val(pgste) |= bit;
625 pgste = pgste_set_pte(ptep, pgste, entry);
626 pgste_set_unlock(ptep, pgste);
627 return 0;
628}
629
630int ptep_shadow_pte(struct mm_struct *mm, unsigned long saddr,
631 pte_t *sptep, pte_t *tptep, pte_t pte)
632{
633 pgste_t spgste, tpgste;
634 pte_t spte, tpte;
635 int rc = -EAGAIN;
636
637 if (!(pte_val(*tptep) & _PAGE_INVALID))
638 return 0; /* already shadowed */
639 spgste = pgste_get_lock(sptep);
640 spte = *sptep;
641 if (!(pte_val(spte) & _PAGE_INVALID) &&
642 !((pte_val(spte) & _PAGE_PROTECT) &&
643 !(pte_val(pte) & _PAGE_PROTECT))) {
644 pgste_val(spgste) |= PGSTE_VSIE_BIT;
645 tpgste = pgste_get_lock(tptep);
646 pte_val(tpte) = (pte_val(spte) & PAGE_MASK) |
647 (pte_val(pte) & _PAGE_PROTECT);
648 /* don't touch the storage key - it belongs to parent pgste */
649 tpgste = pgste_set_pte(tptep, tpgste, tpte);
650 pgste_set_unlock(tptep, tpgste);
651 rc = 1;
652 }
653 pgste_set_unlock(sptep, spgste);
654 return rc;
655}
656
657void ptep_unshadow_pte(struct mm_struct *mm, unsigned long saddr, pte_t *ptep)
658{
659 pgste_t pgste;
660 int nodat;
661
662 pgste = pgste_get_lock(ptep);
663 /* notifier is called by the caller */
664 nodat = !!(pgste_val(pgste) & _PGSTE_GPS_NODAT);
665 ptep_flush_direct(mm, saddr, ptep, nodat);
666 /* don't touch the storage key - it belongs to parent pgste */
667 pgste = pgste_set_pte(ptep, pgste, __pte(_PAGE_INVALID));
668 pgste_set_unlock(ptep, pgste);
669}
670
671static void ptep_zap_swap_entry(struct mm_struct *mm, swp_entry_t entry)
672{
673 if (!non_swap_entry(entry))
674 dec_mm_counter(mm, MM_SWAPENTS);
675 else if (is_migration_entry(entry)) {
676 struct page *page = migration_entry_to_page(entry);
677
678 dec_mm_counter(mm, mm_counter(page));
679 }
680 free_swap_and_cache(entry);
681}
682
683void ptep_zap_unused(struct mm_struct *mm, unsigned long addr,
684 pte_t *ptep, int reset)
685{
686 unsigned long pgstev;
687 pgste_t pgste;
688 pte_t pte;
689
690 /* Zap unused and logically-zero pages */
691 preempt_disable();
692 pgste = pgste_get_lock(ptep);
693 pgstev = pgste_val(pgste);
694 pte = *ptep;
695 if (!reset && pte_swap(pte) &&
696 ((pgstev & _PGSTE_GPS_USAGE_MASK) == _PGSTE_GPS_USAGE_UNUSED ||
697 (pgstev & _PGSTE_GPS_ZERO))) {
698 ptep_zap_swap_entry(mm, pte_to_swp_entry(pte));
699 pte_clear(mm, addr, ptep);
700 }
701 if (reset)
702 pgste_val(pgste) &= ~_PGSTE_GPS_USAGE_MASK;
703 pgste_set_unlock(ptep, pgste);
704 preempt_enable();
705}
706
707void ptep_zap_key(struct mm_struct *mm, unsigned long addr, pte_t *ptep)
708{
709 unsigned long ptev;
710 pgste_t pgste;
711
712 /* Clear storage key ACC and F, but set R/C */
713 preempt_disable();
714 pgste = pgste_get_lock(ptep);
715 pgste_val(pgste) &= ~(PGSTE_ACC_BITS | PGSTE_FP_BIT);
716 pgste_val(pgste) |= PGSTE_GR_BIT | PGSTE_GC_BIT;
717 ptev = pte_val(*ptep);
718 if (!(ptev & _PAGE_INVALID) && (ptev & _PAGE_WRITE))
719 page_set_storage_key(ptev & PAGE_MASK, PAGE_DEFAULT_KEY, 1);
720 pgste_set_unlock(ptep, pgste);
721 preempt_enable();
722}
723
724/*
725 * Test and reset if a guest page is dirty
726 */
727bool ptep_test_and_clear_uc(struct mm_struct *mm, unsigned long addr,
728 pte_t *ptep)
729{
730 pgste_t pgste;
731 pte_t pte;
732 bool dirty;
733 int nodat;
734
735 pgste = pgste_get_lock(ptep);
736 dirty = !!(pgste_val(pgste) & PGSTE_UC_BIT);
737 pgste_val(pgste) &= ~PGSTE_UC_BIT;
738 pte = *ptep;
739 if (dirty && (pte_val(pte) & _PAGE_PRESENT)) {
740 pgste = pgste_pte_notify(mm, addr, ptep, pgste);
741 nodat = !!(pgste_val(pgste) & _PGSTE_GPS_NODAT);
742 ptep_ipte_global(mm, addr, ptep, nodat);
743 if (MACHINE_HAS_ESOP || !(pte_val(pte) & _PAGE_WRITE))
744 pte_val(pte) |= _PAGE_PROTECT;
745 else
746 pte_val(pte) |= _PAGE_INVALID;
747 *ptep = pte;
748 }
749 pgste_set_unlock(ptep, pgste);
750 return dirty;
751}
752EXPORT_SYMBOL_GPL(ptep_test_and_clear_uc);
753
754int set_guest_storage_key(struct mm_struct *mm, unsigned long addr,
755 unsigned char key, bool nq)
756{
757 unsigned long keyul, paddr;
758 spinlock_t *ptl;
759 pgste_t old, new;
760 pmd_t *pmdp;
761 pte_t *ptep;
762
763 pmdp = pmd_alloc_map(mm, addr);
764 if (unlikely(!pmdp))
765 return -EFAULT;
766
767 ptl = pmd_lock(mm, pmdp);
768 if (!pmd_present(*pmdp)) {
769 spin_unlock(ptl);
770 return -EFAULT;
771 }
772
773 if (pmd_large(*pmdp)) {
774 paddr = pmd_val(*pmdp) & HPAGE_MASK;
775 paddr |= addr & ~HPAGE_MASK;
776 /*
777 * Huge pmds need quiescing operations, they are
778 * always mapped.
779 */
780 page_set_storage_key(paddr, key, 1);
781 spin_unlock(ptl);
782 return 0;
783 }
784 spin_unlock(ptl);
785
786 ptep = pte_alloc_map_lock(mm, pmdp, addr, &ptl);
787 if (unlikely(!ptep))
788 return -EFAULT;
789
790 new = old = pgste_get_lock(ptep);
791 pgste_val(new) &= ~(PGSTE_GR_BIT | PGSTE_GC_BIT |
792 PGSTE_ACC_BITS | PGSTE_FP_BIT);
793 keyul = (unsigned long) key;
794 pgste_val(new) |= (keyul & (_PAGE_CHANGED | _PAGE_REFERENCED)) << 48;
795 pgste_val(new) |= (keyul & (_PAGE_ACC_BITS | _PAGE_FP_BIT)) << 56;
796 if (!(pte_val(*ptep) & _PAGE_INVALID)) {
797 unsigned long bits, skey;
798
799 paddr = pte_val(*ptep) & PAGE_MASK;
800 skey = (unsigned long) page_get_storage_key(paddr);
801 bits = skey & (_PAGE_CHANGED | _PAGE_REFERENCED);
802 skey = key & (_PAGE_ACC_BITS | _PAGE_FP_BIT);
803 /* Set storage key ACC and FP */
804 page_set_storage_key(paddr, skey, !nq);
805 /* Merge host changed & referenced into pgste */
806 pgste_val(new) |= bits << 52;
807 }
808 /* changing the guest storage key is considered a change of the page */
809 if ((pgste_val(new) ^ pgste_val(old)) &
810 (PGSTE_ACC_BITS | PGSTE_FP_BIT | PGSTE_GR_BIT | PGSTE_GC_BIT))
811 pgste_val(new) |= PGSTE_UC_BIT;
812
813 pgste_set_unlock(ptep, new);
814 pte_unmap_unlock(ptep, ptl);
815 return 0;
816}
817EXPORT_SYMBOL(set_guest_storage_key);
818
819/**
820 * Conditionally set a guest storage key (handling csske).
821 * oldkey will be updated when either mr or mc is set and a pointer is given.
822 *
823 * Returns 0 if a guests storage key update wasn't necessary, 1 if the guest
824 * storage key was updated and -EFAULT on access errors.
825 */
826int cond_set_guest_storage_key(struct mm_struct *mm, unsigned long addr,
827 unsigned char key, unsigned char *oldkey,
828 bool nq, bool mr, bool mc)
829{
830 unsigned char tmp, mask = _PAGE_ACC_BITS | _PAGE_FP_BIT;
831 int rc;
832
833 /* we can drop the pgste lock between getting and setting the key */
834 if (mr | mc) {
835 rc = get_guest_storage_key(current->mm, addr, &tmp);
836 if (rc)
837 return rc;
838 if (oldkey)
839 *oldkey = tmp;
840 if (!mr)
841 mask |= _PAGE_REFERENCED;
842 if (!mc)
843 mask |= _PAGE_CHANGED;
844 if (!((tmp ^ key) & mask))
845 return 0;
846 }
847 rc = set_guest_storage_key(current->mm, addr, key, nq);
848 return rc < 0 ? rc : 1;
849}
850EXPORT_SYMBOL(cond_set_guest_storage_key);
851
852/**
853 * Reset a guest reference bit (rrbe), returning the reference and changed bit.
854 *
855 * Returns < 0 in case of error, otherwise the cc to be reported to the guest.
856 */
857int reset_guest_reference_bit(struct mm_struct *mm, unsigned long addr)
858{
859 spinlock_t *ptl;
860 unsigned long paddr;
861 pgste_t old, new;
862 pmd_t *pmdp;
863 pte_t *ptep;
864 int cc = 0;
865
866 pmdp = pmd_alloc_map(mm, addr);
867 if (unlikely(!pmdp))
868 return -EFAULT;
869
870 ptl = pmd_lock(mm, pmdp);
871 if (!pmd_present(*pmdp)) {
872 spin_unlock(ptl);
873 return -EFAULT;
874 }
875
876 if (pmd_large(*pmdp)) {
877 paddr = pmd_val(*pmdp) & HPAGE_MASK;
878 paddr |= addr & ~HPAGE_MASK;
879 cc = page_reset_referenced(paddr);
880 spin_unlock(ptl);
881 return cc;
882 }
883 spin_unlock(ptl);
884
885 ptep = pte_alloc_map_lock(mm, pmdp, addr, &ptl);
886 if (unlikely(!ptep))
887 return -EFAULT;
888
889 new = old = pgste_get_lock(ptep);
890 /* Reset guest reference bit only */
891 pgste_val(new) &= ~PGSTE_GR_BIT;
892
893 if (!(pte_val(*ptep) & _PAGE_INVALID)) {
894 paddr = pte_val(*ptep) & PAGE_MASK;
895 cc = page_reset_referenced(paddr);
896 /* Merge real referenced bit into host-set */
897 pgste_val(new) |= ((unsigned long) cc << 53) & PGSTE_HR_BIT;
898 }
899 /* Reflect guest's logical view, not physical */
900 cc |= (pgste_val(old) & (PGSTE_GR_BIT | PGSTE_GC_BIT)) >> 49;
901 /* Changing the guest storage key is considered a change of the page */
902 if ((pgste_val(new) ^ pgste_val(old)) & PGSTE_GR_BIT)
903 pgste_val(new) |= PGSTE_UC_BIT;
904
905 pgste_set_unlock(ptep, new);
906 pte_unmap_unlock(ptep, ptl);
907 return cc;
908}
909EXPORT_SYMBOL(reset_guest_reference_bit);
910
911int get_guest_storage_key(struct mm_struct *mm, unsigned long addr,
912 unsigned char *key)
913{
914 unsigned long paddr;
915 spinlock_t *ptl;
916 pgste_t pgste;
917 pmd_t *pmdp;
918 pte_t *ptep;
919
920 pmdp = pmd_alloc_map(mm, addr);
921 if (unlikely(!pmdp))
922 return -EFAULT;
923
924 ptl = pmd_lock(mm, pmdp);
925 if (!pmd_present(*pmdp)) {
926 /* Not yet mapped memory has a zero key */
927 spin_unlock(ptl);
928 *key = 0;
929 return 0;
930 }
931
932 if (pmd_large(*pmdp)) {
933 paddr = pmd_val(*pmdp) & HPAGE_MASK;
934 paddr |= addr & ~HPAGE_MASK;
935 *key = page_get_storage_key(paddr);
936 spin_unlock(ptl);
937 return 0;
938 }
939 spin_unlock(ptl);
940
941 ptep = pte_alloc_map_lock(mm, pmdp, addr, &ptl);
942 if (unlikely(!ptep))
943 return -EFAULT;
944
945 pgste = pgste_get_lock(ptep);
946 *key = (pgste_val(pgste) & (PGSTE_ACC_BITS | PGSTE_FP_BIT)) >> 56;
947 paddr = pte_val(*ptep) & PAGE_MASK;
948 if (!(pte_val(*ptep) & _PAGE_INVALID))
949 *key = page_get_storage_key(paddr);
950 /* Reflect guest's logical view, not physical */
951 *key |= (pgste_val(pgste) & (PGSTE_GR_BIT | PGSTE_GC_BIT)) >> 48;
952 pgste_set_unlock(ptep, pgste);
953 pte_unmap_unlock(ptep, ptl);
954 return 0;
955}
956EXPORT_SYMBOL(get_guest_storage_key);
957
958/**
959 * pgste_perform_essa - perform ESSA actions on the PGSTE.
960 * @mm: the memory context. It must have PGSTEs, no check is performed here!
961 * @hva: the host virtual address of the page whose PGSTE is to be processed
962 * @orc: the specific action to perform, see the ESSA_SET_* macros.
963 * @oldpte: the PTE will be saved there if the pointer is not NULL.
964 * @oldpgste: the old PGSTE will be saved there if the pointer is not NULL.
965 *
966 * Return: 1 if the page is to be added to the CBRL, otherwise 0,
967 * or < 0 in case of error. -EINVAL is returned for invalid values
968 * of orc, -EFAULT for invalid addresses.
969 */
970int pgste_perform_essa(struct mm_struct *mm, unsigned long hva, int orc,
971 unsigned long *oldpte, unsigned long *oldpgste)
972{
973 unsigned long pgstev;
974 spinlock_t *ptl;
975 pgste_t pgste;
976 pte_t *ptep;
977 int res = 0;
978
979 WARN_ON_ONCE(orc > ESSA_MAX);
980 if (unlikely(orc > ESSA_MAX))
981 return -EINVAL;
982 ptep = get_locked_pte(mm, hva, &ptl);
983 if (unlikely(!ptep))
984 return -EFAULT;
985 pgste = pgste_get_lock(ptep);
986 pgstev = pgste_val(pgste);
987 if (oldpte)
988 *oldpte = pte_val(*ptep);
989 if (oldpgste)
990 *oldpgste = pgstev;
991
992 switch (orc) {
993 case ESSA_GET_STATE:
994 break;
995 case ESSA_SET_STABLE:
996 pgstev &= ~(_PGSTE_GPS_USAGE_MASK | _PGSTE_GPS_NODAT);
997 pgstev |= _PGSTE_GPS_USAGE_STABLE;
998 break;
999 case ESSA_SET_UNUSED:
1000 pgstev &= ~_PGSTE_GPS_USAGE_MASK;
1001 pgstev |= _PGSTE_GPS_USAGE_UNUSED;
1002 if (pte_val(*ptep) & _PAGE_INVALID)
1003 res = 1;
1004 break;
1005 case ESSA_SET_VOLATILE:
1006 pgstev &= ~_PGSTE_GPS_USAGE_MASK;
1007 pgstev |= _PGSTE_GPS_USAGE_VOLATILE;
1008 if (pte_val(*ptep) & _PAGE_INVALID)
1009 res = 1;
1010 break;
1011 case ESSA_SET_POT_VOLATILE:
1012 pgstev &= ~_PGSTE_GPS_USAGE_MASK;
1013 if (!(pte_val(*ptep) & _PAGE_INVALID)) {
1014 pgstev |= _PGSTE_GPS_USAGE_POT_VOLATILE;
1015 break;
1016 }
1017 if (pgstev & _PGSTE_GPS_ZERO) {
1018 pgstev |= _PGSTE_GPS_USAGE_VOLATILE;
1019 break;
1020 }
1021 if (!(pgstev & PGSTE_GC_BIT)) {
1022 pgstev |= _PGSTE_GPS_USAGE_VOLATILE;
1023 res = 1;
1024 break;
1025 }
1026 break;
1027 case ESSA_SET_STABLE_RESIDENT:
1028 pgstev &= ~_PGSTE_GPS_USAGE_MASK;
1029 pgstev |= _PGSTE_GPS_USAGE_STABLE;
1030 /*
1031 * Since the resident state can go away any time after this
1032 * call, we will not make this page resident. We can revisit
1033 * this decision if a guest will ever start using this.
1034 */
1035 break;
1036 case ESSA_SET_STABLE_IF_RESIDENT:
1037 if (!(pte_val(*ptep) & _PAGE_INVALID)) {
1038 pgstev &= ~_PGSTE_GPS_USAGE_MASK;
1039 pgstev |= _PGSTE_GPS_USAGE_STABLE;
1040 }
1041 break;
1042 case ESSA_SET_STABLE_NODAT:
1043 pgstev &= ~_PGSTE_GPS_USAGE_MASK;
1044 pgstev |= _PGSTE_GPS_USAGE_STABLE | _PGSTE_GPS_NODAT;
1045 break;
1046 default:
1047 /* we should never get here! */
1048 break;
1049 }
1050 /* If we are discarding a page, set it to logical zero */
1051 if (res)
1052 pgstev |= _PGSTE_GPS_ZERO;
1053
1054 pgste_val(pgste) = pgstev;
1055 pgste_set_unlock(ptep, pgste);
1056 pte_unmap_unlock(ptep, ptl);
1057 return res;
1058}
1059EXPORT_SYMBOL(pgste_perform_essa);
1060
1061/**
1062 * set_pgste_bits - set specific PGSTE bits.
1063 * @mm: the memory context. It must have PGSTEs, no check is performed here!
1064 * @hva: the host virtual address of the page whose PGSTE is to be processed
1065 * @bits: a bitmask representing the bits that will be touched
1066 * @value: the values of the bits to be written. Only the bits in the mask
1067 * will be written.
1068 *
1069 * Return: 0 on success, < 0 in case of error.
1070 */
1071int set_pgste_bits(struct mm_struct *mm, unsigned long hva,
1072 unsigned long bits, unsigned long value)
1073{
1074 spinlock_t *ptl;
1075 pgste_t new;
1076 pte_t *ptep;
1077
1078 ptep = get_locked_pte(mm, hva, &ptl);
1079 if (unlikely(!ptep))
1080 return -EFAULT;
1081 new = pgste_get_lock(ptep);
1082
1083 pgste_val(new) &= ~bits;
1084 pgste_val(new) |= value & bits;
1085
1086 pgste_set_unlock(ptep, new);
1087 pte_unmap_unlock(ptep, ptl);
1088 return 0;
1089}
1090EXPORT_SYMBOL(set_pgste_bits);
1091
1092/**
1093 * get_pgste - get the current PGSTE for the given address.
1094 * @mm: the memory context. It must have PGSTEs, no check is performed here!
1095 * @hva: the host virtual address of the page whose PGSTE is to be processed
1096 * @pgstep: will be written with the current PGSTE for the given address.
1097 *
1098 * Return: 0 on success, < 0 in case of error.
1099 */
1100int get_pgste(struct mm_struct *mm, unsigned long hva, unsigned long *pgstep)
1101{
1102 spinlock_t *ptl;
1103 pte_t *ptep;
1104
1105 ptep = get_locked_pte(mm, hva, &ptl);
1106 if (unlikely(!ptep))
1107 return -EFAULT;
1108 *pgstep = pgste_val(pgste_get(ptep));
1109 pte_unmap_unlock(ptep, ptl);
1110 return 0;
1111}
1112EXPORT_SYMBOL(get_pgste);
1113#endif
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/highmem.h>
14#include <linux/pagemap.h>
15#include <linux/spinlock.h>
16#include <linux/module.h>
17#include <linux/quicklist.h>
18#include <linux/rcupdate.h>
19#include <linux/slab.h>
20#include <linux/swapops.h>
21
22#include <asm/pgtable.h>
23#include <asm/pgalloc.h>
24#include <asm/tlb.h>
25#include <asm/tlbflush.h>
26#include <asm/mmu_context.h>
27
28#ifndef CONFIG_64BIT
29#define ALLOC_ORDER 1
30#define FRAG_MASK 0x0f
31#else
32#define ALLOC_ORDER 2
33#define FRAG_MASK 0x03
34#endif
35
36
37unsigned long *crst_table_alloc(struct mm_struct *mm)
38{
39 struct page *page = alloc_pages(GFP_KERNEL, ALLOC_ORDER);
40
41 if (!page)
42 return NULL;
43 return (unsigned long *) page_to_phys(page);
44}
45
46void crst_table_free(struct mm_struct *mm, unsigned long *table)
47{
48 free_pages((unsigned long) table, ALLOC_ORDER);
49}
50
51#ifdef CONFIG_64BIT
52static void __crst_table_upgrade(void *arg)
53{
54 struct mm_struct *mm = arg;
55
56 if (current->active_mm == mm)
57 update_user_asce(mm, 1);
58 __tlb_flush_local();
59}
60
61int crst_table_upgrade(struct mm_struct *mm, unsigned long limit)
62{
63 unsigned long *table, *pgd;
64 unsigned long entry;
65 int flush;
66
67 BUG_ON(limit > (1UL << 53));
68 flush = 0;
69repeat:
70 table = crst_table_alloc(mm);
71 if (!table)
72 return -ENOMEM;
73 spin_lock_bh(&mm->page_table_lock);
74 if (mm->context.asce_limit < limit) {
75 pgd = (unsigned long *) mm->pgd;
76 if (mm->context.asce_limit <= (1UL << 31)) {
77 entry = _REGION3_ENTRY_EMPTY;
78 mm->context.asce_limit = 1UL << 42;
79 mm->context.asce_bits = _ASCE_TABLE_LENGTH |
80 _ASCE_USER_BITS |
81 _ASCE_TYPE_REGION3;
82 } else {
83 entry = _REGION2_ENTRY_EMPTY;
84 mm->context.asce_limit = 1UL << 53;
85 mm->context.asce_bits = _ASCE_TABLE_LENGTH |
86 _ASCE_USER_BITS |
87 _ASCE_TYPE_REGION2;
88 }
89 crst_table_init(table, entry);
90 pgd_populate(mm, (pgd_t *) table, (pud_t *) pgd);
91 mm->pgd = (pgd_t *) table;
92 mm->task_size = mm->context.asce_limit;
93 table = NULL;
94 flush = 1;
95 }
96 spin_unlock_bh(&mm->page_table_lock);
97 if (table)
98 crst_table_free(mm, table);
99 if (mm->context.asce_limit < limit)
100 goto repeat;
101 if (flush)
102 on_each_cpu(__crst_table_upgrade, mm, 0);
103 return 0;
104}
105
106void crst_table_downgrade(struct mm_struct *mm, unsigned long limit)
107{
108 pgd_t *pgd;
109
110 if (current->active_mm == mm) {
111 clear_user_asce(mm, 1);
112 __tlb_flush_mm(mm);
113 }
114 while (mm->context.asce_limit > limit) {
115 pgd = mm->pgd;
116 switch (pgd_val(*pgd) & _REGION_ENTRY_TYPE_MASK) {
117 case _REGION_ENTRY_TYPE_R2:
118 mm->context.asce_limit = 1UL << 42;
119 mm->context.asce_bits = _ASCE_TABLE_LENGTH |
120 _ASCE_USER_BITS |
121 _ASCE_TYPE_REGION3;
122 break;
123 case _REGION_ENTRY_TYPE_R3:
124 mm->context.asce_limit = 1UL << 31;
125 mm->context.asce_bits = _ASCE_TABLE_LENGTH |
126 _ASCE_USER_BITS |
127 _ASCE_TYPE_SEGMENT;
128 break;
129 default:
130 BUG();
131 }
132 mm->pgd = (pgd_t *) (pgd_val(*pgd) & _REGION_ENTRY_ORIGIN);
133 mm->task_size = mm->context.asce_limit;
134 crst_table_free(mm, (unsigned long *) pgd);
135 }
136 if (current->active_mm == mm)
137 update_user_asce(mm, 1);
138}
139#endif
140
141#ifdef CONFIG_PGSTE
142
143/**
144 * gmap_alloc - allocate a guest address space
145 * @mm: pointer to the parent mm_struct
146 *
147 * Returns a guest address space structure.
148 */
149struct gmap *gmap_alloc(struct mm_struct *mm)
150{
151 struct gmap *gmap;
152 struct page *page;
153 unsigned long *table;
154
155 gmap = kzalloc(sizeof(struct gmap), GFP_KERNEL);
156 if (!gmap)
157 goto out;
158 INIT_LIST_HEAD(&gmap->crst_list);
159 gmap->mm = mm;
160 page = alloc_pages(GFP_KERNEL, ALLOC_ORDER);
161 if (!page)
162 goto out_free;
163 list_add(&page->lru, &gmap->crst_list);
164 table = (unsigned long *) page_to_phys(page);
165 crst_table_init(table, _REGION1_ENTRY_EMPTY);
166 gmap->table = table;
167 gmap->asce = _ASCE_TYPE_REGION1 | _ASCE_TABLE_LENGTH |
168 _ASCE_USER_BITS | __pa(table);
169 list_add(&gmap->list, &mm->context.gmap_list);
170 return gmap;
171
172out_free:
173 kfree(gmap);
174out:
175 return NULL;
176}
177EXPORT_SYMBOL_GPL(gmap_alloc);
178
179static int gmap_unlink_segment(struct gmap *gmap, unsigned long *table)
180{
181 struct gmap_pgtable *mp;
182 struct gmap_rmap *rmap;
183 struct page *page;
184
185 if (*table & _SEGMENT_ENTRY_INVALID)
186 return 0;
187 page = pfn_to_page(*table >> PAGE_SHIFT);
188 mp = (struct gmap_pgtable *) page->index;
189 list_for_each_entry(rmap, &mp->mapper, list) {
190 if (rmap->entry != table)
191 continue;
192 list_del(&rmap->list);
193 kfree(rmap);
194 break;
195 }
196 *table = mp->vmaddr | _SEGMENT_ENTRY_INVALID | _SEGMENT_ENTRY_PROTECT;
197 return 1;
198}
199
200static void gmap_flush_tlb(struct gmap *gmap)
201{
202 if (MACHINE_HAS_IDTE)
203 __tlb_flush_asce(gmap->mm, (unsigned long) gmap->table |
204 _ASCE_TYPE_REGION1);
205 else
206 __tlb_flush_global();
207}
208
209/**
210 * gmap_free - free a guest address space
211 * @gmap: pointer to the guest address space structure
212 */
213void gmap_free(struct gmap *gmap)
214{
215 struct page *page, *next;
216 unsigned long *table;
217 int i;
218
219
220 /* Flush tlb. */
221 if (MACHINE_HAS_IDTE)
222 __tlb_flush_asce(gmap->mm, (unsigned long) gmap->table |
223 _ASCE_TYPE_REGION1);
224 else
225 __tlb_flush_global();
226
227 /* Free all segment & region tables. */
228 down_read(&gmap->mm->mmap_sem);
229 spin_lock(&gmap->mm->page_table_lock);
230 list_for_each_entry_safe(page, next, &gmap->crst_list, lru) {
231 table = (unsigned long *) page_to_phys(page);
232 if ((*table & _REGION_ENTRY_TYPE_MASK) == 0)
233 /* Remove gmap rmap structures for segment table. */
234 for (i = 0; i < PTRS_PER_PMD; i++, table++)
235 gmap_unlink_segment(gmap, table);
236 __free_pages(page, ALLOC_ORDER);
237 }
238 spin_unlock(&gmap->mm->page_table_lock);
239 up_read(&gmap->mm->mmap_sem);
240 list_del(&gmap->list);
241 kfree(gmap);
242}
243EXPORT_SYMBOL_GPL(gmap_free);
244
245/**
246 * gmap_enable - switch primary space to the guest address space
247 * @gmap: pointer to the guest address space structure
248 */
249void gmap_enable(struct gmap *gmap)
250{
251 S390_lowcore.gmap = (unsigned long) gmap;
252}
253EXPORT_SYMBOL_GPL(gmap_enable);
254
255/**
256 * gmap_disable - switch back to the standard primary address space
257 * @gmap: pointer to the guest address space structure
258 */
259void gmap_disable(struct gmap *gmap)
260{
261 S390_lowcore.gmap = 0UL;
262}
263EXPORT_SYMBOL_GPL(gmap_disable);
264
265/*
266 * gmap_alloc_table is assumed to be called with mmap_sem held
267 */
268static int gmap_alloc_table(struct gmap *gmap,
269 unsigned long *table, unsigned long init)
270 __releases(&gmap->mm->page_table_lock)
271 __acquires(&gmap->mm->page_table_lock)
272{
273 struct page *page;
274 unsigned long *new;
275
276 /* since we dont free the gmap table until gmap_free we can unlock */
277 spin_unlock(&gmap->mm->page_table_lock);
278 page = alloc_pages(GFP_KERNEL, ALLOC_ORDER);
279 spin_lock(&gmap->mm->page_table_lock);
280 if (!page)
281 return -ENOMEM;
282 new = (unsigned long *) page_to_phys(page);
283 crst_table_init(new, init);
284 if (*table & _REGION_ENTRY_INVALID) {
285 list_add(&page->lru, &gmap->crst_list);
286 *table = (unsigned long) new | _REGION_ENTRY_LENGTH |
287 (*table & _REGION_ENTRY_TYPE_MASK);
288 } else
289 __free_pages(page, ALLOC_ORDER);
290 return 0;
291}
292
293/**
294 * gmap_unmap_segment - unmap segment from the guest address space
295 * @gmap: pointer to the guest address space structure
296 * @addr: address in the guest address space
297 * @len: length of the memory area to unmap
298 *
299 * Returns 0 if the unmap succeeded, -EINVAL if not.
300 */
301int gmap_unmap_segment(struct gmap *gmap, unsigned long to, unsigned long len)
302{
303 unsigned long *table;
304 unsigned long off;
305 int flush;
306
307 if ((to | len) & (PMD_SIZE - 1))
308 return -EINVAL;
309 if (len == 0 || to + len < to)
310 return -EINVAL;
311
312 flush = 0;
313 down_read(&gmap->mm->mmap_sem);
314 spin_lock(&gmap->mm->page_table_lock);
315 for (off = 0; off < len; off += PMD_SIZE) {
316 /* Walk the guest addr space page table */
317 table = gmap->table + (((to + off) >> 53) & 0x7ff);
318 if (*table & _REGION_ENTRY_INVALID)
319 goto out;
320 table = (unsigned long *)(*table & _REGION_ENTRY_ORIGIN);
321 table = table + (((to + off) >> 42) & 0x7ff);
322 if (*table & _REGION_ENTRY_INVALID)
323 goto out;
324 table = (unsigned long *)(*table & _REGION_ENTRY_ORIGIN);
325 table = table + (((to + off) >> 31) & 0x7ff);
326 if (*table & _REGION_ENTRY_INVALID)
327 goto out;
328 table = (unsigned long *)(*table & _REGION_ENTRY_ORIGIN);
329 table = table + (((to + off) >> 20) & 0x7ff);
330
331 /* Clear segment table entry in guest address space. */
332 flush |= gmap_unlink_segment(gmap, table);
333 *table = _SEGMENT_ENTRY_INVALID;
334 }
335out:
336 spin_unlock(&gmap->mm->page_table_lock);
337 up_read(&gmap->mm->mmap_sem);
338 if (flush)
339 gmap_flush_tlb(gmap);
340 return 0;
341}
342EXPORT_SYMBOL_GPL(gmap_unmap_segment);
343
344/**
345 * gmap_mmap_segment - map a segment to the guest address space
346 * @gmap: pointer to the guest address space structure
347 * @from: source address in the parent address space
348 * @to: target address in the guest address space
349 *
350 * Returns 0 if the mmap succeeded, -EINVAL or -ENOMEM if not.
351 */
352int gmap_map_segment(struct gmap *gmap, unsigned long from,
353 unsigned long to, unsigned long len)
354{
355 unsigned long *table;
356 unsigned long off;
357 int flush;
358
359 if ((from | to | len) & (PMD_SIZE - 1))
360 return -EINVAL;
361 if (len == 0 || from + len > TASK_MAX_SIZE ||
362 from + len < from || to + len < to)
363 return -EINVAL;
364
365 flush = 0;
366 down_read(&gmap->mm->mmap_sem);
367 spin_lock(&gmap->mm->page_table_lock);
368 for (off = 0; off < len; off += PMD_SIZE) {
369 /* Walk the gmap address space page table */
370 table = gmap->table + (((to + off) >> 53) & 0x7ff);
371 if ((*table & _REGION_ENTRY_INVALID) &&
372 gmap_alloc_table(gmap, table, _REGION2_ENTRY_EMPTY))
373 goto out_unmap;
374 table = (unsigned long *)(*table & _REGION_ENTRY_ORIGIN);
375 table = table + (((to + off) >> 42) & 0x7ff);
376 if ((*table & _REGION_ENTRY_INVALID) &&
377 gmap_alloc_table(gmap, table, _REGION3_ENTRY_EMPTY))
378 goto out_unmap;
379 table = (unsigned long *)(*table & _REGION_ENTRY_ORIGIN);
380 table = table + (((to + off) >> 31) & 0x7ff);
381 if ((*table & _REGION_ENTRY_INVALID) &&
382 gmap_alloc_table(gmap, table, _SEGMENT_ENTRY_EMPTY))
383 goto out_unmap;
384 table = (unsigned long *) (*table & _REGION_ENTRY_ORIGIN);
385 table = table + (((to + off) >> 20) & 0x7ff);
386
387 /* Store 'from' address in an invalid segment table entry. */
388 flush |= gmap_unlink_segment(gmap, table);
389 *table = (from + off) | (_SEGMENT_ENTRY_INVALID |
390 _SEGMENT_ENTRY_PROTECT);
391 }
392 spin_unlock(&gmap->mm->page_table_lock);
393 up_read(&gmap->mm->mmap_sem);
394 if (flush)
395 gmap_flush_tlb(gmap);
396 return 0;
397
398out_unmap:
399 spin_unlock(&gmap->mm->page_table_lock);
400 up_read(&gmap->mm->mmap_sem);
401 gmap_unmap_segment(gmap, to, len);
402 return -ENOMEM;
403}
404EXPORT_SYMBOL_GPL(gmap_map_segment);
405
406static unsigned long *gmap_table_walk(unsigned long address, struct gmap *gmap)
407{
408 unsigned long *table;
409
410 table = gmap->table + ((address >> 53) & 0x7ff);
411 if (unlikely(*table & _REGION_ENTRY_INVALID))
412 return ERR_PTR(-EFAULT);
413 table = (unsigned long *)(*table & _REGION_ENTRY_ORIGIN);
414 table = table + ((address >> 42) & 0x7ff);
415 if (unlikely(*table & _REGION_ENTRY_INVALID))
416 return ERR_PTR(-EFAULT);
417 table = (unsigned long *)(*table & _REGION_ENTRY_ORIGIN);
418 table = table + ((address >> 31) & 0x7ff);
419 if (unlikely(*table & _REGION_ENTRY_INVALID))
420 return ERR_PTR(-EFAULT);
421 table = (unsigned long *)(*table & _REGION_ENTRY_ORIGIN);
422 table = table + ((address >> 20) & 0x7ff);
423 return table;
424}
425
426/**
427 * __gmap_translate - translate a guest address to a user space address
428 * @address: guest address
429 * @gmap: pointer to guest mapping meta data structure
430 *
431 * Returns user space address which corresponds to the guest address or
432 * -EFAULT if no such mapping exists.
433 * This function does not establish potentially missing page table entries.
434 * The mmap_sem of the mm that belongs to the address space must be held
435 * when this function gets called.
436 */
437unsigned long __gmap_translate(unsigned long address, struct gmap *gmap)
438{
439 unsigned long *segment_ptr, vmaddr, segment;
440 struct gmap_pgtable *mp;
441 struct page *page;
442
443 current->thread.gmap_addr = address;
444 segment_ptr = gmap_table_walk(address, gmap);
445 if (IS_ERR(segment_ptr))
446 return PTR_ERR(segment_ptr);
447 /* Convert the gmap address to an mm address. */
448 segment = *segment_ptr;
449 if (!(segment & _SEGMENT_ENTRY_INVALID)) {
450 page = pfn_to_page(segment >> PAGE_SHIFT);
451 mp = (struct gmap_pgtable *) page->index;
452 return mp->vmaddr | (address & ~PMD_MASK);
453 } else if (segment & _SEGMENT_ENTRY_PROTECT) {
454 vmaddr = segment & _SEGMENT_ENTRY_ORIGIN;
455 return vmaddr | (address & ~PMD_MASK);
456 }
457 return -EFAULT;
458}
459EXPORT_SYMBOL_GPL(__gmap_translate);
460
461/**
462 * gmap_translate - translate a guest address to a user space address
463 * @address: guest address
464 * @gmap: pointer to guest mapping meta data structure
465 *
466 * Returns user space address which corresponds to the guest address or
467 * -EFAULT if no such mapping exists.
468 * This function does not establish potentially missing page table entries.
469 */
470unsigned long gmap_translate(unsigned long address, struct gmap *gmap)
471{
472 unsigned long rc;
473
474 down_read(&gmap->mm->mmap_sem);
475 rc = __gmap_translate(address, gmap);
476 up_read(&gmap->mm->mmap_sem);
477 return rc;
478}
479EXPORT_SYMBOL_GPL(gmap_translate);
480
481static int gmap_connect_pgtable(unsigned long address, unsigned long segment,
482 unsigned long *segment_ptr, struct gmap *gmap)
483{
484 unsigned long vmaddr;
485 struct vm_area_struct *vma;
486 struct gmap_pgtable *mp;
487 struct gmap_rmap *rmap;
488 struct mm_struct *mm;
489 struct page *page;
490 pgd_t *pgd;
491 pud_t *pud;
492 pmd_t *pmd;
493
494 mm = gmap->mm;
495 vmaddr = segment & _SEGMENT_ENTRY_ORIGIN;
496 vma = find_vma(mm, vmaddr);
497 if (!vma || vma->vm_start > vmaddr)
498 return -EFAULT;
499 /* Walk the parent mm page table */
500 pgd = pgd_offset(mm, vmaddr);
501 pud = pud_alloc(mm, pgd, vmaddr);
502 if (!pud)
503 return -ENOMEM;
504 pmd = pmd_alloc(mm, pud, vmaddr);
505 if (!pmd)
506 return -ENOMEM;
507 if (!pmd_present(*pmd) &&
508 __pte_alloc(mm, vma, pmd, vmaddr))
509 return -ENOMEM;
510 /* large pmds cannot yet be handled */
511 if (pmd_large(*pmd))
512 return -EFAULT;
513 /* pmd now points to a valid segment table entry. */
514 rmap = kmalloc(sizeof(*rmap), GFP_KERNEL|__GFP_REPEAT);
515 if (!rmap)
516 return -ENOMEM;
517 /* Link gmap segment table entry location to page table. */
518 page = pmd_page(*pmd);
519 mp = (struct gmap_pgtable *) page->index;
520 rmap->gmap = gmap;
521 rmap->entry = segment_ptr;
522 rmap->vmaddr = address & PMD_MASK;
523 spin_lock(&mm->page_table_lock);
524 if (*segment_ptr == segment) {
525 list_add(&rmap->list, &mp->mapper);
526 /* Set gmap segment table entry to page table. */
527 *segment_ptr = pmd_val(*pmd) & PAGE_MASK;
528 rmap = NULL;
529 }
530 spin_unlock(&mm->page_table_lock);
531 kfree(rmap);
532 return 0;
533}
534
535static void gmap_disconnect_pgtable(struct mm_struct *mm, unsigned long *table)
536{
537 struct gmap_rmap *rmap, *next;
538 struct gmap_pgtable *mp;
539 struct page *page;
540 int flush;
541
542 flush = 0;
543 spin_lock(&mm->page_table_lock);
544 page = pfn_to_page(__pa(table) >> PAGE_SHIFT);
545 mp = (struct gmap_pgtable *) page->index;
546 list_for_each_entry_safe(rmap, next, &mp->mapper, list) {
547 *rmap->entry = mp->vmaddr | (_SEGMENT_ENTRY_INVALID |
548 _SEGMENT_ENTRY_PROTECT);
549 list_del(&rmap->list);
550 kfree(rmap);
551 flush = 1;
552 }
553 spin_unlock(&mm->page_table_lock);
554 if (flush)
555 __tlb_flush_global();
556}
557
558/*
559 * this function is assumed to be called with mmap_sem held
560 */
561unsigned long __gmap_fault(unsigned long address, struct gmap *gmap)
562{
563 unsigned long *segment_ptr, segment;
564 struct gmap_pgtable *mp;
565 struct page *page;
566 int rc;
567
568 current->thread.gmap_addr = address;
569 segment_ptr = gmap_table_walk(address, gmap);
570 if (IS_ERR(segment_ptr))
571 return -EFAULT;
572 /* Convert the gmap address to an mm address. */
573 while (1) {
574 segment = *segment_ptr;
575 if (!(segment & _SEGMENT_ENTRY_INVALID)) {
576 /* Page table is present */
577 page = pfn_to_page(segment >> PAGE_SHIFT);
578 mp = (struct gmap_pgtable *) page->index;
579 return mp->vmaddr | (address & ~PMD_MASK);
580 }
581 if (!(segment & _SEGMENT_ENTRY_PROTECT))
582 /* Nothing mapped in the gmap address space. */
583 break;
584 rc = gmap_connect_pgtable(address, segment, segment_ptr, gmap);
585 if (rc)
586 return rc;
587 }
588 return -EFAULT;
589}
590
591unsigned long gmap_fault(unsigned long address, struct gmap *gmap)
592{
593 unsigned long rc;
594
595 down_read(&gmap->mm->mmap_sem);
596 rc = __gmap_fault(address, gmap);
597 up_read(&gmap->mm->mmap_sem);
598
599 return rc;
600}
601EXPORT_SYMBOL_GPL(gmap_fault);
602
603static void gmap_zap_swap_entry(swp_entry_t entry, struct mm_struct *mm)
604{
605 if (!non_swap_entry(entry))
606 dec_mm_counter(mm, MM_SWAPENTS);
607 else if (is_migration_entry(entry)) {
608 struct page *page = migration_entry_to_page(entry);
609
610 if (PageAnon(page))
611 dec_mm_counter(mm, MM_ANONPAGES);
612 else
613 dec_mm_counter(mm, MM_FILEPAGES);
614 }
615 free_swap_and_cache(entry);
616}
617
618/**
619 * The mm->mmap_sem lock must be held
620 */
621static void gmap_zap_unused(struct mm_struct *mm, unsigned long address)
622{
623 unsigned long ptev, pgstev;
624 spinlock_t *ptl;
625 pgste_t pgste;
626 pte_t *ptep, pte;
627
628 ptep = get_locked_pte(mm, address, &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), mm);
641 pte_clear(mm, address, ptep);
642 }
643 pgste_set_unlock(ptep, pgste);
644out_pte:
645 pte_unmap_unlock(*ptep, ptl);
646}
647
648/*
649 * this function is assumed to be called with mmap_sem held
650 */
651void __gmap_zap(unsigned long address, struct gmap *gmap)
652{
653 unsigned long *table, *segment_ptr;
654 unsigned long segment, pgstev, ptev;
655 struct gmap_pgtable *mp;
656 struct page *page;
657
658 segment_ptr = gmap_table_walk(address, gmap);
659 if (IS_ERR(segment_ptr))
660 return;
661 segment = *segment_ptr;
662 if (segment & _SEGMENT_ENTRY_INVALID)
663 return;
664 page = pfn_to_page(segment >> PAGE_SHIFT);
665 mp = (struct gmap_pgtable *) page->index;
666 address = mp->vmaddr | (address & ~PMD_MASK);
667 /* Page table is present */
668 table = (unsigned long *)(segment & _SEGMENT_ENTRY_ORIGIN);
669 table = table + ((address >> 12) & 0xff);
670 pgstev = table[PTRS_PER_PTE];
671 ptev = table[0];
672 /* quick check, checked again with locks held */
673 if (((pgstev & _PGSTE_GPS_USAGE_MASK) == _PGSTE_GPS_USAGE_UNUSED) ||
674 ((pgstev & _PGSTE_GPS_ZERO) && (ptev & _PAGE_INVALID)))
675 gmap_zap_unused(gmap->mm, address);
676}
677EXPORT_SYMBOL_GPL(__gmap_zap);
678
679void gmap_discard(unsigned long from, unsigned long to, struct gmap *gmap)
680{
681
682 unsigned long *table, address, size;
683 struct vm_area_struct *vma;
684 struct gmap_pgtable *mp;
685 struct page *page;
686
687 down_read(&gmap->mm->mmap_sem);
688 address = from;
689 while (address < to) {
690 /* Walk the gmap address space page table */
691 table = gmap->table + ((address >> 53) & 0x7ff);
692 if (unlikely(*table & _REGION_ENTRY_INVALID)) {
693 address = (address + PMD_SIZE) & PMD_MASK;
694 continue;
695 }
696 table = (unsigned long *)(*table & _REGION_ENTRY_ORIGIN);
697 table = table + ((address >> 42) & 0x7ff);
698 if (unlikely(*table & _REGION_ENTRY_INVALID)) {
699 address = (address + PMD_SIZE) & PMD_MASK;
700 continue;
701 }
702 table = (unsigned long *)(*table & _REGION_ENTRY_ORIGIN);
703 table = table + ((address >> 31) & 0x7ff);
704 if (unlikely(*table & _REGION_ENTRY_INVALID)) {
705 address = (address + PMD_SIZE) & PMD_MASK;
706 continue;
707 }
708 table = (unsigned long *)(*table & _REGION_ENTRY_ORIGIN);
709 table = table + ((address >> 20) & 0x7ff);
710 if (unlikely(*table & _SEGMENT_ENTRY_INVALID)) {
711 address = (address + PMD_SIZE) & PMD_MASK;
712 continue;
713 }
714 page = pfn_to_page(*table >> PAGE_SHIFT);
715 mp = (struct gmap_pgtable *) page->index;
716 vma = find_vma(gmap->mm, mp->vmaddr);
717 size = min(to - address, PMD_SIZE - (address & ~PMD_MASK));
718 zap_page_range(vma, mp->vmaddr | (address & ~PMD_MASK),
719 size, NULL);
720 address = (address + PMD_SIZE) & PMD_MASK;
721 }
722 up_read(&gmap->mm->mmap_sem);
723}
724EXPORT_SYMBOL_GPL(gmap_discard);
725
726static LIST_HEAD(gmap_notifier_list);
727static DEFINE_SPINLOCK(gmap_notifier_lock);
728
729/**
730 * gmap_register_ipte_notifier - register a pte invalidation callback
731 * @nb: pointer to the gmap notifier block
732 */
733void gmap_register_ipte_notifier(struct gmap_notifier *nb)
734{
735 spin_lock(&gmap_notifier_lock);
736 list_add(&nb->list, &gmap_notifier_list);
737 spin_unlock(&gmap_notifier_lock);
738}
739EXPORT_SYMBOL_GPL(gmap_register_ipte_notifier);
740
741/**
742 * gmap_unregister_ipte_notifier - remove a pte invalidation callback
743 * @nb: pointer to the gmap notifier block
744 */
745void gmap_unregister_ipte_notifier(struct gmap_notifier *nb)
746{
747 spin_lock(&gmap_notifier_lock);
748 list_del_init(&nb->list);
749 spin_unlock(&gmap_notifier_lock);
750}
751EXPORT_SYMBOL_GPL(gmap_unregister_ipte_notifier);
752
753/**
754 * gmap_ipte_notify - mark a range of ptes for invalidation notification
755 * @gmap: pointer to guest mapping meta data structure
756 * @start: virtual address in the guest address space
757 * @len: size of area
758 *
759 * Returns 0 if for each page in the given range a gmap mapping exists and
760 * the invalidation notification could be set. If the gmap mapping is missing
761 * for one or more pages -EFAULT is returned. If no memory could be allocated
762 * -ENOMEM is returned. This function establishes missing page table entries.
763 */
764int gmap_ipte_notify(struct gmap *gmap, unsigned long start, unsigned long len)
765{
766 unsigned long addr;
767 spinlock_t *ptl;
768 pte_t *ptep, entry;
769 pgste_t pgste;
770 int rc = 0;
771
772 if ((start & ~PAGE_MASK) || (len & ~PAGE_MASK))
773 return -EINVAL;
774 down_read(&gmap->mm->mmap_sem);
775 while (len) {
776 /* Convert gmap address and connect the page tables */
777 addr = __gmap_fault(start, gmap);
778 if (IS_ERR_VALUE(addr)) {
779 rc = addr;
780 break;
781 }
782 /* Get the page mapped */
783 if (fixup_user_fault(current, gmap->mm, addr, FAULT_FLAG_WRITE)) {
784 rc = -EFAULT;
785 break;
786 }
787 /* Walk the process page table, lock and get pte pointer */
788 ptep = get_locked_pte(gmap->mm, addr, &ptl);
789 if (unlikely(!ptep))
790 continue;
791 /* Set notification bit in the pgste of the pte */
792 entry = *ptep;
793 if ((pte_val(entry) & (_PAGE_INVALID | _PAGE_PROTECT)) == 0) {
794 pgste = pgste_get_lock(ptep);
795 pgste_val(pgste) |= PGSTE_IN_BIT;
796 pgste_set_unlock(ptep, pgste);
797 start += PAGE_SIZE;
798 len -= PAGE_SIZE;
799 }
800 spin_unlock(ptl);
801 }
802 up_read(&gmap->mm->mmap_sem);
803 return rc;
804}
805EXPORT_SYMBOL_GPL(gmap_ipte_notify);
806
807/**
808 * gmap_do_ipte_notify - call all invalidation callbacks for a specific pte.
809 * @mm: pointer to the process mm_struct
810 * @pte: pointer to the page table entry
811 *
812 * This function is assumed to be called with the page table lock held
813 * for the pte to notify.
814 */
815void gmap_do_ipte_notify(struct mm_struct *mm, pte_t *pte)
816{
817 unsigned long segment_offset;
818 struct gmap_notifier *nb;
819 struct gmap_pgtable *mp;
820 struct gmap_rmap *rmap;
821 struct page *page;
822
823 segment_offset = ((unsigned long) pte) & (255 * sizeof(pte_t));
824 segment_offset = segment_offset * (4096 / sizeof(pte_t));
825 page = pfn_to_page(__pa(pte) >> PAGE_SHIFT);
826 mp = (struct gmap_pgtable *) page->index;
827 spin_lock(&gmap_notifier_lock);
828 list_for_each_entry(rmap, &mp->mapper, list) {
829 list_for_each_entry(nb, &gmap_notifier_list, list)
830 nb->notifier_call(rmap->gmap,
831 rmap->vmaddr + segment_offset);
832 }
833 spin_unlock(&gmap_notifier_lock);
834}
835
836static inline int page_table_with_pgste(struct page *page)
837{
838 return atomic_read(&page->_mapcount) == 0;
839}
840
841static inline unsigned long *page_table_alloc_pgste(struct mm_struct *mm,
842 unsigned long vmaddr)
843{
844 struct page *page;
845 unsigned long *table;
846 struct gmap_pgtable *mp;
847
848 page = alloc_page(GFP_KERNEL|__GFP_REPEAT);
849 if (!page)
850 return NULL;
851 mp = kmalloc(sizeof(*mp), GFP_KERNEL|__GFP_REPEAT);
852 if (!mp) {
853 __free_page(page);
854 return NULL;
855 }
856 if (!pgtable_page_ctor(page)) {
857 kfree(mp);
858 __free_page(page);
859 return NULL;
860 }
861 mp->vmaddr = vmaddr & PMD_MASK;
862 INIT_LIST_HEAD(&mp->mapper);
863 page->index = (unsigned long) mp;
864 atomic_set(&page->_mapcount, 0);
865 table = (unsigned long *) page_to_phys(page);
866 clear_table(table, _PAGE_INVALID, PAGE_SIZE/2);
867 clear_table(table + PTRS_PER_PTE, PGSTE_HR_BIT | PGSTE_HC_BIT,
868 PAGE_SIZE/2);
869 return table;
870}
871
872static inline void page_table_free_pgste(unsigned long *table)
873{
874 struct page *page;
875 struct gmap_pgtable *mp;
876
877 page = pfn_to_page(__pa(table) >> PAGE_SHIFT);
878 mp = (struct gmap_pgtable *) page->index;
879 BUG_ON(!list_empty(&mp->mapper));
880 pgtable_page_dtor(page);
881 atomic_set(&page->_mapcount, -1);
882 kfree(mp);
883 __free_page(page);
884}
885
886static inline unsigned long page_table_reset_pte(struct mm_struct *mm,
887 pmd_t *pmd, unsigned long addr, unsigned long end)
888{
889 pte_t *start_pte, *pte;
890 spinlock_t *ptl;
891 pgste_t pgste;
892
893 start_pte = pte_offset_map_lock(mm, pmd, addr, &ptl);
894 pte = start_pte;
895 do {
896 pgste = pgste_get_lock(pte);
897 pgste_val(pgste) &= ~_PGSTE_GPS_USAGE_MASK;
898 pgste_set_unlock(pte, pgste);
899 } while (pte++, addr += PAGE_SIZE, addr != end);
900 pte_unmap_unlock(start_pte, ptl);
901
902 return addr;
903}
904
905static inline unsigned long page_table_reset_pmd(struct mm_struct *mm,
906 pud_t *pud, unsigned long addr, unsigned long end)
907{
908 unsigned long next;
909 pmd_t *pmd;
910
911 pmd = pmd_offset(pud, addr);
912 do {
913 next = pmd_addr_end(addr, end);
914 if (pmd_none_or_clear_bad(pmd))
915 continue;
916 next = page_table_reset_pte(mm, pmd, addr, next);
917 } while (pmd++, addr = next, addr != end);
918
919 return addr;
920}
921
922static inline unsigned long page_table_reset_pud(struct mm_struct *mm,
923 pgd_t *pgd, unsigned long addr, unsigned long end)
924{
925 unsigned long next;
926 pud_t *pud;
927
928 pud = pud_offset(pgd, addr);
929 do {
930 next = pud_addr_end(addr, end);
931 if (pud_none_or_clear_bad(pud))
932 continue;
933 next = page_table_reset_pmd(mm, pud, addr, next);
934 } while (pud++, addr = next, addr != end);
935
936 return addr;
937}
938
939void page_table_reset_pgste(struct mm_struct *mm,
940 unsigned long start, unsigned long end)
941{
942 unsigned long addr, next;
943 pgd_t *pgd;
944
945 addr = start;
946 down_read(&mm->mmap_sem);
947 pgd = pgd_offset(mm, addr);
948 do {
949 next = pgd_addr_end(addr, end);
950 if (pgd_none_or_clear_bad(pgd))
951 continue;
952 next = page_table_reset_pud(mm, pgd, addr, next);
953 } while (pgd++, addr = next, addr != end);
954 up_read(&mm->mmap_sem);
955}
956EXPORT_SYMBOL(page_table_reset_pgste);
957
958int set_guest_storage_key(struct mm_struct *mm, unsigned long addr,
959 unsigned long key, bool nq)
960{
961 spinlock_t *ptl;
962 pgste_t old, new;
963 pte_t *ptep;
964
965 down_read(&mm->mmap_sem);
966 ptep = get_locked_pte(current->mm, addr, &ptl);
967 if (unlikely(!ptep)) {
968 up_read(&mm->mmap_sem);
969 return -EFAULT;
970 }
971
972 new = old = pgste_get_lock(ptep);
973 pgste_val(new) &= ~(PGSTE_GR_BIT | PGSTE_GC_BIT |
974 PGSTE_ACC_BITS | PGSTE_FP_BIT);
975 pgste_val(new) |= (key & (_PAGE_CHANGED | _PAGE_REFERENCED)) << 48;
976 pgste_val(new) |= (key & (_PAGE_ACC_BITS | _PAGE_FP_BIT)) << 56;
977 if (!(pte_val(*ptep) & _PAGE_INVALID)) {
978 unsigned long address, bits, skey;
979
980 address = pte_val(*ptep) & PAGE_MASK;
981 skey = (unsigned long) page_get_storage_key(address);
982 bits = skey & (_PAGE_CHANGED | _PAGE_REFERENCED);
983 skey = key & (_PAGE_ACC_BITS | _PAGE_FP_BIT);
984 /* Set storage key ACC and FP */
985 page_set_storage_key(address, skey, !nq);
986 /* Merge host changed & referenced into pgste */
987 pgste_val(new) |= bits << 52;
988 }
989 /* changing the guest storage key is considered a change of the page */
990 if ((pgste_val(new) ^ pgste_val(old)) &
991 (PGSTE_ACC_BITS | PGSTE_FP_BIT | PGSTE_GR_BIT | PGSTE_GC_BIT))
992 pgste_val(new) |= PGSTE_HC_BIT;
993
994 pgste_set_unlock(ptep, new);
995 pte_unmap_unlock(*ptep, ptl);
996 up_read(&mm->mmap_sem);
997 return 0;
998}
999EXPORT_SYMBOL(set_guest_storage_key);
1000
1001#else /* CONFIG_PGSTE */
1002
1003static inline int page_table_with_pgste(struct page *page)
1004{
1005 return 0;
1006}
1007
1008static inline unsigned long *page_table_alloc_pgste(struct mm_struct *mm,
1009 unsigned long vmaddr)
1010{
1011 return NULL;
1012}
1013
1014static inline void page_table_free_pgste(unsigned long *table)
1015{
1016}
1017
1018static inline void gmap_disconnect_pgtable(struct mm_struct *mm,
1019 unsigned long *table)
1020{
1021}
1022
1023#endif /* CONFIG_PGSTE */
1024
1025static inline unsigned int atomic_xor_bits(atomic_t *v, unsigned int bits)
1026{
1027 unsigned int old, new;
1028
1029 do {
1030 old = atomic_read(v);
1031 new = old ^ bits;
1032 } while (atomic_cmpxchg(v, old, new) != old);
1033 return new;
1034}
1035
1036/*
1037 * page table entry allocation/free routines.
1038 */
1039unsigned long *page_table_alloc(struct mm_struct *mm, unsigned long vmaddr)
1040{
1041 unsigned long *uninitialized_var(table);
1042 struct page *uninitialized_var(page);
1043 unsigned int mask, bit;
1044
1045 if (mm_has_pgste(mm))
1046 return page_table_alloc_pgste(mm, vmaddr);
1047 /* Allocate fragments of a 4K page as 1K/2K page table */
1048 spin_lock_bh(&mm->context.list_lock);
1049 mask = FRAG_MASK;
1050 if (!list_empty(&mm->context.pgtable_list)) {
1051 page = list_first_entry(&mm->context.pgtable_list,
1052 struct page, lru);
1053 table = (unsigned long *) page_to_phys(page);
1054 mask = atomic_read(&page->_mapcount);
1055 mask = mask | (mask >> 4);
1056 }
1057 if ((mask & FRAG_MASK) == FRAG_MASK) {
1058 spin_unlock_bh(&mm->context.list_lock);
1059 page = alloc_page(GFP_KERNEL|__GFP_REPEAT);
1060 if (!page)
1061 return NULL;
1062 if (!pgtable_page_ctor(page)) {
1063 __free_page(page);
1064 return NULL;
1065 }
1066 atomic_set(&page->_mapcount, 1);
1067 table = (unsigned long *) page_to_phys(page);
1068 clear_table(table, _PAGE_INVALID, PAGE_SIZE);
1069 spin_lock_bh(&mm->context.list_lock);
1070 list_add(&page->lru, &mm->context.pgtable_list);
1071 } else {
1072 for (bit = 1; mask & bit; bit <<= 1)
1073 table += PTRS_PER_PTE;
1074 mask = atomic_xor_bits(&page->_mapcount, bit);
1075 if ((mask & FRAG_MASK) == FRAG_MASK)
1076 list_del(&page->lru);
1077 }
1078 spin_unlock_bh(&mm->context.list_lock);
1079 return table;
1080}
1081
1082void page_table_free(struct mm_struct *mm, unsigned long *table)
1083{
1084 struct page *page;
1085 unsigned int bit, mask;
1086
1087 page = pfn_to_page(__pa(table) >> PAGE_SHIFT);
1088 if (page_table_with_pgste(page)) {
1089 gmap_disconnect_pgtable(mm, table);
1090 return page_table_free_pgste(table);
1091 }
1092 /* Free 1K/2K page table fragment of a 4K page */
1093 bit = 1 << ((__pa(table) & ~PAGE_MASK)/(PTRS_PER_PTE*sizeof(pte_t)));
1094 spin_lock_bh(&mm->context.list_lock);
1095 if ((atomic_read(&page->_mapcount) & FRAG_MASK) != FRAG_MASK)
1096 list_del(&page->lru);
1097 mask = atomic_xor_bits(&page->_mapcount, bit);
1098 if (mask & FRAG_MASK)
1099 list_add(&page->lru, &mm->context.pgtable_list);
1100 spin_unlock_bh(&mm->context.list_lock);
1101 if (mask == 0) {
1102 pgtable_page_dtor(page);
1103 atomic_set(&page->_mapcount, -1);
1104 __free_page(page);
1105 }
1106}
1107
1108static void __page_table_free_rcu(void *table, unsigned bit)
1109{
1110 struct page *page;
1111
1112 if (bit == FRAG_MASK)
1113 return page_table_free_pgste(table);
1114 /* Free 1K/2K page table fragment of a 4K page */
1115 page = pfn_to_page(__pa(table) >> PAGE_SHIFT);
1116 if (atomic_xor_bits(&page->_mapcount, bit) == 0) {
1117 pgtable_page_dtor(page);
1118 atomic_set(&page->_mapcount, -1);
1119 __free_page(page);
1120 }
1121}
1122
1123void page_table_free_rcu(struct mmu_gather *tlb, unsigned long *table)
1124{
1125 struct mm_struct *mm;
1126 struct page *page;
1127 unsigned int bit, mask;
1128
1129 mm = tlb->mm;
1130 page = pfn_to_page(__pa(table) >> PAGE_SHIFT);
1131 if (page_table_with_pgste(page)) {
1132 gmap_disconnect_pgtable(mm, table);
1133 table = (unsigned long *) (__pa(table) | FRAG_MASK);
1134 tlb_remove_table(tlb, table);
1135 return;
1136 }
1137 bit = 1 << ((__pa(table) & ~PAGE_MASK) / (PTRS_PER_PTE*sizeof(pte_t)));
1138 spin_lock_bh(&mm->context.list_lock);
1139 if ((atomic_read(&page->_mapcount) & FRAG_MASK) != FRAG_MASK)
1140 list_del(&page->lru);
1141 mask = atomic_xor_bits(&page->_mapcount, bit | (bit << 4));
1142 if (mask & FRAG_MASK)
1143 list_add_tail(&page->lru, &mm->context.pgtable_list);
1144 spin_unlock_bh(&mm->context.list_lock);
1145 table = (unsigned long *) (__pa(table) | (bit << 4));
1146 tlb_remove_table(tlb, table);
1147}
1148
1149static void __tlb_remove_table(void *_table)
1150{
1151 const unsigned long mask = (FRAG_MASK << 4) | FRAG_MASK;
1152 void *table = (void *)((unsigned long) _table & ~mask);
1153 unsigned type = (unsigned long) _table & mask;
1154
1155 if (type)
1156 __page_table_free_rcu(table, type);
1157 else
1158 free_pages((unsigned long) table, ALLOC_ORDER);
1159}
1160
1161static void tlb_remove_table_smp_sync(void *arg)
1162{
1163 /* Simply deliver the interrupt */
1164}
1165
1166static void tlb_remove_table_one(void *table)
1167{
1168 /*
1169 * This isn't an RCU grace period and hence the page-tables cannot be
1170 * assumed to be actually RCU-freed.
1171 *
1172 * It is however sufficient for software page-table walkers that rely
1173 * on IRQ disabling. See the comment near struct mmu_table_batch.
1174 */
1175 smp_call_function(tlb_remove_table_smp_sync, NULL, 1);
1176 __tlb_remove_table(table);
1177}
1178
1179static void tlb_remove_table_rcu(struct rcu_head *head)
1180{
1181 struct mmu_table_batch *batch;
1182 int i;
1183
1184 batch = container_of(head, struct mmu_table_batch, rcu);
1185
1186 for (i = 0; i < batch->nr; i++)
1187 __tlb_remove_table(batch->tables[i]);
1188
1189 free_page((unsigned long)batch);
1190}
1191
1192void tlb_table_flush(struct mmu_gather *tlb)
1193{
1194 struct mmu_table_batch **batch = &tlb->batch;
1195
1196 if (*batch) {
1197 call_rcu_sched(&(*batch)->rcu, tlb_remove_table_rcu);
1198 *batch = NULL;
1199 }
1200}
1201
1202void tlb_remove_table(struct mmu_gather *tlb, void *table)
1203{
1204 struct mmu_table_batch **batch = &tlb->batch;
1205
1206 tlb->mm->context.flush_mm = 1;
1207 if (*batch == NULL) {
1208 *batch = (struct mmu_table_batch *)
1209 __get_free_page(GFP_NOWAIT | __GFP_NOWARN);
1210 if (*batch == NULL) {
1211 __tlb_flush_mm_lazy(tlb->mm);
1212 tlb_remove_table_one(table);
1213 return;
1214 }
1215 (*batch)->nr = 0;
1216 }
1217 (*batch)->tables[(*batch)->nr++] = table;
1218 if ((*batch)->nr == MAX_TABLE_BATCH)
1219 tlb_flush_mmu(tlb);
1220}
1221
1222#ifdef CONFIG_TRANSPARENT_HUGEPAGE
1223static inline void thp_split_vma(struct vm_area_struct *vma)
1224{
1225 unsigned long addr;
1226
1227 for (addr = vma->vm_start; addr < vma->vm_end; addr += PAGE_SIZE)
1228 follow_page(vma, addr, FOLL_SPLIT);
1229}
1230
1231static inline void thp_split_mm(struct mm_struct *mm)
1232{
1233 struct vm_area_struct *vma;
1234
1235 for (vma = mm->mmap; vma != NULL; vma = vma->vm_next) {
1236 thp_split_vma(vma);
1237 vma->vm_flags &= ~VM_HUGEPAGE;
1238 vma->vm_flags |= VM_NOHUGEPAGE;
1239 }
1240 mm->def_flags |= VM_NOHUGEPAGE;
1241}
1242#else
1243static inline void thp_split_mm(struct mm_struct *mm)
1244{
1245}
1246#endif /* CONFIG_TRANSPARENT_HUGEPAGE */
1247
1248static unsigned long page_table_realloc_pmd(struct mmu_gather *tlb,
1249 struct mm_struct *mm, pud_t *pud,
1250 unsigned long addr, unsigned long end)
1251{
1252 unsigned long next, *table, *new;
1253 struct page *page;
1254 pmd_t *pmd;
1255
1256 pmd = pmd_offset(pud, addr);
1257 do {
1258 next = pmd_addr_end(addr, end);
1259again:
1260 if (pmd_none_or_clear_bad(pmd))
1261 continue;
1262 table = (unsigned long *) pmd_deref(*pmd);
1263 page = pfn_to_page(__pa(table) >> PAGE_SHIFT);
1264 if (page_table_with_pgste(page))
1265 continue;
1266 /* Allocate new page table with pgstes */
1267 new = page_table_alloc_pgste(mm, addr);
1268 if (!new)
1269 return -ENOMEM;
1270
1271 spin_lock(&mm->page_table_lock);
1272 if (likely((unsigned long *) pmd_deref(*pmd) == table)) {
1273 /* Nuke pmd entry pointing to the "short" page table */
1274 pmdp_flush_lazy(mm, addr, pmd);
1275 pmd_clear(pmd);
1276 /* Copy ptes from old table to new table */
1277 memcpy(new, table, PAGE_SIZE/2);
1278 clear_table(table, _PAGE_INVALID, PAGE_SIZE/2);
1279 /* Establish new table */
1280 pmd_populate(mm, pmd, (pte_t *) new);
1281 /* Free old table with rcu, there might be a walker! */
1282 page_table_free_rcu(tlb, table);
1283 new = NULL;
1284 }
1285 spin_unlock(&mm->page_table_lock);
1286 if (new) {
1287 page_table_free_pgste(new);
1288 goto again;
1289 }
1290 } while (pmd++, addr = next, addr != end);
1291
1292 return addr;
1293}
1294
1295static unsigned long page_table_realloc_pud(struct mmu_gather *tlb,
1296 struct mm_struct *mm, pgd_t *pgd,
1297 unsigned long addr, unsigned long end)
1298{
1299 unsigned long next;
1300 pud_t *pud;
1301
1302 pud = pud_offset(pgd, addr);
1303 do {
1304 next = pud_addr_end(addr, end);
1305 if (pud_none_or_clear_bad(pud))
1306 continue;
1307 next = page_table_realloc_pmd(tlb, mm, pud, addr, next);
1308 if (unlikely(IS_ERR_VALUE(next)))
1309 return next;
1310 } while (pud++, addr = next, addr != end);
1311
1312 return addr;
1313}
1314
1315static unsigned long page_table_realloc(struct mmu_gather *tlb, struct mm_struct *mm,
1316 unsigned long addr, unsigned long end)
1317{
1318 unsigned long next;
1319 pgd_t *pgd;
1320
1321 pgd = pgd_offset(mm, addr);
1322 do {
1323 next = pgd_addr_end(addr, end);
1324 if (pgd_none_or_clear_bad(pgd))
1325 continue;
1326 next = page_table_realloc_pud(tlb, mm, pgd, addr, next);
1327 if (unlikely(IS_ERR_VALUE(next)))
1328 return next;
1329 } while (pgd++, addr = next, addr != end);
1330
1331 return 0;
1332}
1333
1334/*
1335 * switch on pgstes for its userspace process (for kvm)
1336 */
1337int s390_enable_sie(void)
1338{
1339 struct task_struct *tsk = current;
1340 struct mm_struct *mm = tsk->mm;
1341 struct mmu_gather tlb;
1342
1343 /* Do we have pgstes? if yes, we are done */
1344 if (mm_has_pgste(tsk->mm))
1345 return 0;
1346
1347 down_write(&mm->mmap_sem);
1348 /* split thp mappings and disable thp for future mappings */
1349 thp_split_mm(mm);
1350 /* Reallocate the page tables with pgstes */
1351 tlb_gather_mmu(&tlb, mm, 0, TASK_SIZE);
1352 if (!page_table_realloc(&tlb, mm, 0, TASK_SIZE))
1353 mm->context.has_pgste = 1;
1354 tlb_finish_mmu(&tlb, 0, TASK_SIZE);
1355 up_write(&mm->mmap_sem);
1356 return mm->context.has_pgste ? 0 : -ENOMEM;
1357}
1358EXPORT_SYMBOL_GPL(s390_enable_sie);
1359
1360#ifdef CONFIG_TRANSPARENT_HUGEPAGE
1361int pmdp_clear_flush_young(struct vm_area_struct *vma, unsigned long address,
1362 pmd_t *pmdp)
1363{
1364 VM_BUG_ON(address & ~HPAGE_PMD_MASK);
1365 /* No need to flush TLB
1366 * On s390 reference bits are in storage key and never in TLB */
1367 return pmdp_test_and_clear_young(vma, address, pmdp);
1368}
1369
1370int pmdp_set_access_flags(struct vm_area_struct *vma,
1371 unsigned long address, pmd_t *pmdp,
1372 pmd_t entry, int dirty)
1373{
1374 VM_BUG_ON(address & ~HPAGE_PMD_MASK);
1375
1376 if (pmd_same(*pmdp, entry))
1377 return 0;
1378 pmdp_invalidate(vma, address, pmdp);
1379 set_pmd_at(vma->vm_mm, address, pmdp, entry);
1380 return 1;
1381}
1382
1383static void pmdp_splitting_flush_sync(void *arg)
1384{
1385 /* Simply deliver the interrupt */
1386}
1387
1388void pmdp_splitting_flush(struct vm_area_struct *vma, unsigned long address,
1389 pmd_t *pmdp)
1390{
1391 VM_BUG_ON(address & ~HPAGE_PMD_MASK);
1392 if (!test_and_set_bit(_SEGMENT_ENTRY_SPLIT_BIT,
1393 (unsigned long *) pmdp)) {
1394 /* need to serialize against gup-fast (IRQ disabled) */
1395 smp_call_function(pmdp_splitting_flush_sync, NULL, 1);
1396 }
1397}
1398
1399void pgtable_trans_huge_deposit(struct mm_struct *mm, pmd_t *pmdp,
1400 pgtable_t pgtable)
1401{
1402 struct list_head *lh = (struct list_head *) pgtable;
1403
1404 assert_spin_locked(pmd_lockptr(mm, pmdp));
1405
1406 /* FIFO */
1407 if (!pmd_huge_pte(mm, pmdp))
1408 INIT_LIST_HEAD(lh);
1409 else
1410 list_add(lh, (struct list_head *) pmd_huge_pte(mm, pmdp));
1411 pmd_huge_pte(mm, pmdp) = pgtable;
1412}
1413
1414pgtable_t pgtable_trans_huge_withdraw(struct mm_struct *mm, pmd_t *pmdp)
1415{
1416 struct list_head *lh;
1417 pgtable_t pgtable;
1418 pte_t *ptep;
1419
1420 assert_spin_locked(pmd_lockptr(mm, pmdp));
1421
1422 /* FIFO */
1423 pgtable = pmd_huge_pte(mm, pmdp);
1424 lh = (struct list_head *) pgtable;
1425 if (list_empty(lh))
1426 pmd_huge_pte(mm, pmdp) = NULL;
1427 else {
1428 pmd_huge_pte(mm, pmdp) = (pgtable_t) lh->next;
1429 list_del(lh);
1430 }
1431 ptep = (pte_t *) pgtable;
1432 pte_val(*ptep) = _PAGE_INVALID;
1433 ptep++;
1434 pte_val(*ptep) = _PAGE_INVALID;
1435 return pgtable;
1436}
1437#endif /* CONFIG_TRANSPARENT_HUGEPAGE */