xref: /linux/arch/s390/kvm/gmap/dat.c (revision e20789439160f34315be89b50cff4a9bab958678)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  *  KVM guest address space mapping code
4  *
5  *    Copyright IBM Corp. 2007, 2020, 2024
6  *    Author(s): Claudio Imbrenda <imbrenda@linux.ibm.com>
7  *		 Martin Schwidefsky <schwidefsky@de.ibm.com>
8  *		 David Hildenbrand <david@redhat.com>
9  *		 Janosch Frank <frankja@linux.ibm.com>
10  */
11 
12 #include <linux/kernel.h>
13 #include <linux/pagewalk.h>
14 #include <linux/swap.h>
15 #include <linux/smp.h>
16 #include <linux/spinlock.h>
17 #include <linux/slab.h>
18 #include <linux/swapops.h>
19 #include <linux/ksm.h>
20 #include <linux/mm.h>
21 #include <linux/mman.h>
22 #include <linux/pgtable.h>
23 #include <linux/kvm_types.h>
24 #include <linux/kvm_host.h>
25 #include <linux/pgalloc.h>
26 
27 #include <asm/page-states.h>
28 #include <asm/tlb.h>
29 #include "dat.h"
30 
31 int kvm_s390_mmu_cache_topup(struct kvm_s390_mmu_cache *mc)
32 {
33 	void *o;
34 
35 	for ( ; mc->n_crsts < KVM_S390_MMU_CACHE_N_CRSTS; mc->n_crsts++) {
36 		o = (void *)__get_free_pages(GFP_KERNEL_ACCOUNT | __GFP_COMP, CRST_ALLOC_ORDER);
37 		if (!o)
38 			return -ENOMEM;
39 		mc->crsts[mc->n_crsts] = o;
40 	}
41 	for ( ; mc->n_pts < KVM_S390_MMU_CACHE_N_PTS; mc->n_pts++) {
42 		o = (void *)__get_free_page(GFP_KERNEL_ACCOUNT);
43 		if (!o)
44 			return -ENOMEM;
45 		mc->pts[mc->n_pts] = o;
46 	}
47 	for ( ; mc->n_rmaps < KVM_S390_MMU_CACHE_N_RMAPS; mc->n_rmaps++) {
48 		o = kzalloc_obj(struct vsie_rmap, GFP_KERNEL_ACCOUNT);
49 		if (!o)
50 			return -ENOMEM;
51 		mc->rmaps[mc->n_rmaps] = o;
52 	}
53 	return 0;
54 }
55 
56 static inline struct page_table *dat_alloc_pt_noinit(struct kvm_s390_mmu_cache *mc)
57 {
58 	struct page_table *res;
59 
60 	res = kvm_s390_mmu_cache_alloc_pt(mc);
61 	if (res)
62 		__arch_set_page_dat(res, 1);
63 	return res;
64 }
65 
66 static inline struct crst_table *dat_alloc_crst_noinit(struct kvm_s390_mmu_cache *mc)
67 {
68 	struct crst_table *res;
69 
70 	res = kvm_s390_mmu_cache_alloc_crst(mc);
71 	if (res)
72 		__arch_set_page_dat(res, 1UL << CRST_ALLOC_ORDER);
73 	return res;
74 }
75 
76 struct crst_table *dat_alloc_crst_sleepable(unsigned long init)
77 {
78 	struct page *page;
79 	void *virt;
80 
81 	page = alloc_pages(GFP_KERNEL_ACCOUNT | __GFP_COMP, CRST_ALLOC_ORDER);
82 	if (!page)
83 		return NULL;
84 	virt = page_to_virt(page);
85 	__arch_set_page_dat(virt, 1UL << CRST_ALLOC_ORDER);
86 	crst_table_init(virt, init);
87 	return virt;
88 }
89 
90 void dat_free_level(struct crst_table *table, bool owns_ptes)
91 {
92 	unsigned int i;
93 
94 	for (i = 0; i < _CRST_ENTRIES; i++) {
95 		if (table->crstes[i].h.fc || table->crstes[i].h.i)
96 			continue;
97 		if (!is_pmd(table->crstes[i]))
98 			dat_free_level(dereference_crste(table->crstes[i]), owns_ptes);
99 		else if (owns_ptes)
100 			dat_free_pt(dereference_pmd(table->crstes[i].pmd));
101 	}
102 	dat_free_crst(table);
103 }
104 
105 int dat_set_asce_limit(struct kvm_s390_mmu_cache *mc, union asce *asce, int newtype)
106 {
107 	struct crst_table *table;
108 	union crste crste;
109 
110 	while (asce->dt > newtype) {
111 		table = dereference_asce(*asce);
112 		crste = table->crstes[0];
113 		if (crste.h.fc)
114 			return 0;
115 		if (!crste.h.i) {
116 			asce->rsto = crste.h.fc0.to;
117 			dat_free_crst(table);
118 		} else {
119 			crste.h.tt--;
120 			crst_table_init((void *)table, crste.val);
121 		}
122 		asce->dt--;
123 	}
124 	while (asce->dt < newtype) {
125 		crste = _crste_fc0(asce->rsto, asce->dt + 1);
126 		table = dat_alloc_crst_noinit(mc);
127 		if (!table)
128 			return -ENOMEM;
129 		crst_table_init((void *)table, _CRSTE_HOLE(crste.h.tt).val);
130 		table->crstes[0] = crste;
131 		asce->rsto = __pa(table) >> PAGE_SHIFT;
132 		asce->dt++;
133 	}
134 	return 0;
135 }
136 
137 /**
138  * dat_crstep_xchg_atomic() - Atomically exchange a gmap CRSTE with another.
139  * @crstep: Pointer to the CRST entry.
140  * @old: Expected old value.
141  * @new: Replacement entry.
142  * @gfn: The affected guest address.
143  * @asce: The asce of the address space.
144  *
145  * This function is needed to atomically exchange a CRSTE that potentially
146  * maps a prefix area, without having to invalidate it inbetween.
147  *
148  * Context: This function is assumed to be called with kvm->mmu_lock held.
149  *
150  * Return: %true if the exchange was successful.
151  */
152 bool __must_check dat_crstep_xchg_atomic(union crste *crstep, union crste old, union crste new,
153 					 gfn_t gfn, union asce asce)
154 {
155 	if (old.h.i)
156 		return arch_try_cmpxchg((long *)crstep, &old.val, new.val);
157 	if (cpu_has_edat2())
158 		return crdte_crste(crstep, old, new, gfn, asce);
159 	return cspg_crste(crstep, old, new);
160 }
161 
162 union pgste __dat_ptep_xchg(union pte *ptep, union pgste pgste, union pte new, gfn_t gfn,
163 			    union asce asce, bool uses_skeys)
164 {
165 	union pte old = READ_ONCE(*ptep);
166 
167 	/* Updating only the software bits while holding the pgste lock. */
168 	if (!((ptep->val ^ new.val) & ~_PAGE_SW_BITS)) {
169 		WRITE_ONCE(ptep->swbyte, new.swbyte);
170 		return pgste;
171 	}
172 
173 	if (!old.h.i) {
174 		unsigned long opts = IPTE_GUEST_ASCE | (pgste.nodat ? IPTE_NODAT : 0);
175 
176 		if (machine_has_tlb_guest())
177 			__ptep_ipte(gfn_to_gpa(gfn), (void *)ptep, opts, asce.val, IPTE_GLOBAL);
178 		else
179 			__ptep_ipte(gfn_to_gpa(gfn), (void *)ptep, 0, 0, IPTE_GLOBAL);
180 	}
181 
182 	if (uses_skeys) {
183 		if (old.h.i && !new.h.i)
184 			/* Invalid to valid: restore storage keys from PGSTE. */
185 			dat_set_storage_key_from_pgste(new, pgste);
186 		else if (!old.h.i && new.h.i)
187 			/* Valid to invalid: save storage keys to PGSTE. */
188 			pgste = dat_save_storage_key_into_pgste(old, pgste);
189 		else if (!old.h.i && !new.h.i)
190 			/* Valid to valid: move storage keys. */
191 			if (old.h.pfra != new.h.pfra)
192 				dat_move_storage_key(old, new);
193 		/* Invalid to invalid: nothing to do. */
194 	}
195 
196 	WRITE_ONCE(*ptep, new);
197 	return pgste;
198 }
199 
200 /*
201  * dat_split_ste() - Split a segment table entry into page table entries.
202  *
203  * Context: This function is assumed to be called with kvm->mmu_lock held.
204  *
205  * Return: 0 in case of success, -ENOMEM if running out of memory.
206  */
207 static int dat_split_ste(struct kvm_s390_mmu_cache *mc, union pmd *pmdp, gfn_t gfn,
208 			 union asce asce, bool uses_skeys)
209 {
210 	union pgste pgste_init;
211 	struct page_table *pt;
212 	union pmd new, old;
213 	union pte init;
214 	int i;
215 
216 	BUG_ON(!mc);
217 	old = READ_ONCE(*pmdp);
218 
219 	/* Already split, nothing to do. */
220 	if (!old.h.i && !old.h.fc)
221 		return 0;
222 
223 	pt = dat_alloc_pt_noinit(mc);
224 	if (!pt)
225 		return -ENOMEM;
226 	new.val = virt_to_phys(pt);
227 
228 	while (old.h.i || old.h.fc) {
229 		init.val = pmd_origin_large(old);
230 		init.h.p = old.h.p;
231 		init.h.i = old.h.i;
232 		init.s.d = old.s.fc1.d;
233 		init.s.w = old.s.fc1.w;
234 		init.s.y = old.s.fc1.y;
235 		init.s.sd = old.s.fc1.sd;
236 		init.s.pr = old.s.fc1.pr;
237 		pgste_init.val = 0;
238 		if (old.h.fc) {
239 			for (i = 0; i < _PAGE_ENTRIES; i++)
240 				pt->ptes[i].val = init.val | i * PAGE_SIZE;
241 			/* No need to take locks as the page table is not installed yet. */
242 			pgste_init.prefix_notif = old.s.fc1.prefix_notif;
243 			pgste_init.vsie_notif = old.s.fc1.vsie_notif;
244 			pgste_init.vsie_gmem = old.s.fc1.vsie_notif;
245 			pgste_init.pcl = uses_skeys && init.h.i;
246 			dat_init_pgstes(pt, pgste_init.val);
247 		} else {
248 			dat_init_page_table(pt, init.val, 0);
249 		}
250 
251 		if (dat_pmdp_xchg_atomic(pmdp, old, new, gfn, asce)) {
252 			if (!pgste_init.pcl)
253 				return 0;
254 			for (i = 0; i < _PAGE_ENTRIES; i++) {
255 				union pgste pgste = pt->pgstes[i];
256 
257 				pgste = dat_save_storage_key_into_pgste(pt->ptes[i], pgste);
258 				pgste_set_unlock(pt->ptes + i, pgste);
259 			}
260 			return 0;
261 		}
262 		old = READ_ONCE(*pmdp);
263 	}
264 
265 	dat_free_pt(pt);
266 	return 0;
267 }
268 
269 /*
270  * dat_split_crste() - Split a crste into smaller crstes.
271  *
272  * Context: This function is assumed to be called with kvm->mmu_lock held.
273  *
274  * Return: %0 in case of success, %-ENOMEM if running out of memory.
275  */
276 static int dat_split_crste(struct kvm_s390_mmu_cache *mc, union crste *crstep,
277 			   gfn_t gfn, union asce asce, bool uses_skeys)
278 {
279 	struct crst_table *table;
280 	union crste old, new, init;
281 	int i;
282 
283 	old = READ_ONCE(*crstep);
284 	if (is_pmd(old))
285 		return dat_split_ste(mc, &crstep->pmd, gfn, asce, uses_skeys);
286 
287 	BUG_ON(!mc);
288 
289 	/* Already split, nothing to do. */
290 	if (!old.h.i && !old.h.fc)
291 		return 0;
292 
293 	table = dat_alloc_crst_noinit(mc);
294 	if (!table)
295 		return -ENOMEM;
296 
297 	new.val = virt_to_phys(table);
298 	new.h.tt = old.h.tt;
299 	new.h.fc0.tl = _REGION_ENTRY_LENGTH;
300 
301 	while (old.h.i || old.h.fc) {
302 		init = old;
303 		init.h.tt--;
304 		if (old.h.fc) {
305 			for (i = 0; i < _CRST_ENTRIES; i++)
306 				table->crstes[i].val = init.val | i * HPAGE_SIZE;
307 		} else {
308 			crst_table_init((void *)table, init.val);
309 		}
310 		if (dat_crstep_xchg_atomic(crstep, old, new, gfn, asce))
311 			return 0;
312 		old = READ_ONCE(*crstep);
313 	}
314 
315 	dat_free_crst(table);
316 	return 0;
317 }
318 
319 /**
320  * dat_entry_walk() - Walk the gmap page tables.
321  * @mc: Cache to use to allocate dat tables, if needed; can be NULL if neither
322  *      %DAT_WALK_SPLIT or %DAT_WALK_ALLOC is specified in @flags.
323  * @gfn: Guest frame.
324  * @asce: The ASCE of the address space.
325  * @flags: Flags from WALK_* macros.
326  * @walk_level: Level to walk to, from LEVEL_* macros.
327  * @last: Will be filled the last visited non-pte DAT entry.
328  * @ptepp: Will be filled the last visited pte entry, if any, otherwise NULL.
329  *
330  * Returns a table entry pointer for the given guest address and @walk_level.
331  *
332  * The @flags have the following meanings:
333  * * %DAT_WALK_IGN_HOLES: consider holes as normal table entries
334  * * %DAT_WALK_ALLOC: allocate new tables to reach the requested level, if needed
335  * * %DAT_WALK_SPLIT: split existing large pages to reach the requested level, if needed
336  * * %DAT_WALK_LEAF: return successfully whenever a large page is encountered
337  * * %DAT_WALK_ANY: return successfully even if the requested level could not be reached
338  * * %DAT_WALK_CONTINUE: walk to the requested level with the specified flags, and then try to
339  *                       continue walking to ptes with only DAT_WALK_ANY
340  * * %DAT_WALK_USES_SKEYS: storage keys are in use
341  *
342  * Context: called with kvm->mmu_lock held.
343  *
344  * Return:
345  * * %PGM_ADDRESSING if the requested address lies outside memory
346  * * a PIC number if the requested address lies in a memory hole of type _DAT_TOKEN_PIC
347  * * %-EFAULT if the requested address lies inside a memory hole of a different type
348  * * %-EINVAL if the given ASCE is not compatible with the requested level
349  * * %-EFBIG if the requested level could not be reached because a larger frame was found
350  * * %-ENOENT if the requested level could not be reached for other reasons
351  * * %-ENOMEM if running out of memory while allocating or splitting a table
352  */
353 int dat_entry_walk(struct kvm_s390_mmu_cache *mc, gfn_t gfn, union asce asce, int flags,
354 		   int walk_level, union crste **last, union pte **ptepp)
355 {
356 	union vaddress vaddr = { .addr = gfn_to_gpa(gfn) };
357 	bool continue_anyway = flags & DAT_WALK_CONTINUE;
358 	bool uses_skeys = flags & DAT_WALK_USES_SKEYS;
359 	bool ign_holes = flags & DAT_WALK_IGN_HOLES;
360 	bool allocate = flags & DAT_WALK_ALLOC;
361 	bool split = flags & DAT_WALK_SPLIT;
362 	bool leaf = flags & DAT_WALK_LEAF;
363 	bool any = flags & DAT_WALK_ANY;
364 	struct page_table *pgtable;
365 	struct crst_table *table;
366 	union crste entry;
367 	int rc;
368 
369 	*last = NULL;
370 	*ptepp = NULL;
371 	if (WARN_ON_ONCE(unlikely(!asce.val)))
372 		return -EINVAL;
373 	if (WARN_ON_ONCE(unlikely(walk_level > asce.dt)))
374 		return -EINVAL;
375 	if (!asce_contains_gfn(asce, gfn))
376 		return PGM_ADDRESSING;
377 
378 	table = dereference_asce(asce);
379 	if (asce.dt >= ASCE_TYPE_REGION1) {
380 		*last = table->crstes + vaddr.rfx;
381 		entry = READ_ONCE(**last);
382 		if (WARN_ON_ONCE(entry.h.tt != TABLE_TYPE_REGION1))
383 			return -EINVAL;
384 		if (crste_hole(entry) && !ign_holes)
385 			return entry.tok.type == _DAT_TOKEN_PIC ? entry.tok.par : -EFAULT;
386 		if (walk_level == TABLE_TYPE_REGION1)
387 			return 0;
388 		if (entry.pgd.h.i) {
389 			if (!allocate)
390 				return any ? 0 : -ENOENT;
391 			rc = dat_split_crste(mc, *last, gfn, asce, uses_skeys);
392 			if (rc)
393 				return rc;
394 			entry = READ_ONCE(**last);
395 		}
396 		table = dereference_crste(entry.pgd);
397 	}
398 
399 	if (asce.dt >= ASCE_TYPE_REGION2) {
400 		*last = table->crstes + vaddr.rsx;
401 		entry = READ_ONCE(**last);
402 		if (WARN_ON_ONCE(entry.h.tt != TABLE_TYPE_REGION2))
403 			return -EINVAL;
404 		if (crste_hole(entry) && !ign_holes)
405 			return entry.tok.type == _DAT_TOKEN_PIC ? entry.tok.par : -EFAULT;
406 		if (walk_level == TABLE_TYPE_REGION2)
407 			return 0;
408 		if (entry.p4d.h.i) {
409 			if (!allocate)
410 				return any ? 0 : -ENOENT;
411 			rc = dat_split_crste(mc, *last, gfn, asce, uses_skeys);
412 			if (rc)
413 				return rc;
414 			entry = READ_ONCE(**last);
415 		}
416 		table = dereference_crste(entry.p4d);
417 	}
418 
419 	if (asce.dt >= ASCE_TYPE_REGION3) {
420 		*last = table->crstes + vaddr.rtx;
421 		entry = READ_ONCE(**last);
422 		if (WARN_ON_ONCE(entry.h.tt != TABLE_TYPE_REGION3))
423 			return -EINVAL;
424 		if (crste_hole(entry) && !ign_holes)
425 			return entry.tok.type == _DAT_TOKEN_PIC ? entry.tok.par : -EFAULT;
426 		if (walk_level == TABLE_TYPE_REGION3 &&
427 		    continue_anyway && !entry.pud.h.fc && !entry.h.i) {
428 			walk_level = TABLE_TYPE_PAGE_TABLE;
429 			allocate = false;
430 		}
431 		if (walk_level == TABLE_TYPE_REGION3 || ((leaf || any) && entry.pud.h.fc))
432 			return 0;
433 		if (entry.pud.h.i && !entry.pud.h.fc) {
434 			if (!allocate)
435 				return any ? 0 : -ENOENT;
436 			rc = dat_split_crste(mc, *last, gfn, asce, uses_skeys);
437 			if (rc)
438 				return rc;
439 			entry = READ_ONCE(**last);
440 		}
441 		if (walk_level <= TABLE_TYPE_SEGMENT && entry.pud.h.fc) {
442 			if (!split)
443 				return -EFBIG;
444 			rc = dat_split_crste(mc, *last, gfn, asce, uses_skeys);
445 			if (rc)
446 				return rc;
447 			entry = READ_ONCE(**last);
448 		}
449 		table = dereference_crste(entry.pud);
450 	}
451 
452 	*last = table->crstes + vaddr.sx;
453 	entry = READ_ONCE(**last);
454 	if (WARN_ON_ONCE(entry.h.tt != TABLE_TYPE_SEGMENT))
455 		return -EINVAL;
456 	if (crste_hole(entry) && !ign_holes)
457 		return entry.tok.type == _DAT_TOKEN_PIC ? entry.tok.par : -EFAULT;
458 	if (continue_anyway && !entry.pmd.h.fc && !entry.h.i) {
459 		walk_level = TABLE_TYPE_PAGE_TABLE;
460 		allocate = false;
461 	}
462 	if (walk_level == TABLE_TYPE_SEGMENT || ((leaf || any) && entry.pmd.h.fc))
463 		return 0;
464 
465 	if (entry.pmd.h.i && !entry.pmd.h.fc) {
466 		if (!allocate)
467 			return any ? 0 : -ENOENT;
468 		rc = dat_split_ste(mc, &(*last)->pmd, gfn, asce, uses_skeys);
469 		if (rc)
470 			return rc;
471 		entry = READ_ONCE(**last);
472 	}
473 	if (walk_level <= TABLE_TYPE_PAGE_TABLE && entry.pmd.h.fc) {
474 		if (!split)
475 			return -EFBIG;
476 		rc = dat_split_ste(mc, &(*last)->pmd, gfn, asce, uses_skeys);
477 		if (rc)
478 			return rc;
479 		entry = READ_ONCE(**last);
480 	}
481 	pgtable = dereference_pmd(entry.pmd);
482 	*ptepp = pgtable->ptes + vaddr.px;
483 	if (pte_hole(**ptepp) && !ign_holes)
484 		return (*ptepp)->tok.type == _DAT_TOKEN_PIC ? (*ptepp)->tok.par : -EFAULT;
485 	return 0;
486 }
487 
488 static long dat_pte_walk_range(gfn_t gfn, gfn_t end, struct page_table *table, struct dat_walk *w)
489 {
490 	unsigned int idx = gfn & (_PAGE_ENTRIES - 1);
491 	long rc = 0;
492 
493 	for ( ; gfn < end; idx++, gfn++) {
494 		if (pte_hole(READ_ONCE(table->ptes[idx]))) {
495 			if (!(w->flags & DAT_WALK_IGN_HOLES))
496 				return -EFAULT;
497 			if (!(w->flags & DAT_WALK_ANY))
498 				continue;
499 		}
500 
501 		rc = w->ops->pte_entry(table->ptes + idx, gfn, gfn + 1, w);
502 		if (rc)
503 			break;
504 	}
505 	return rc;
506 }
507 
508 static long dat_crste_walk_range(gfn_t start, gfn_t end, struct crst_table *table,
509 				 struct dat_walk *walk)
510 {
511 	unsigned long idx, cur_shift, cur_size;
512 	dat_walk_op the_op;
513 	union crste crste;
514 	gfn_t cur, next;
515 	long rc = 0;
516 
517 	cur_shift = 8 + table->crstes[0].h.tt * 11;
518 	idx = (start >> cur_shift) & (_CRST_ENTRIES - 1);
519 	cur_size = 1UL << cur_shift;
520 
521 	for (cur = ALIGN_DOWN(start, cur_size); cur < end; idx++, cur = next) {
522 		next = cur + cur_size;
523 		walk->last = table->crstes + idx;
524 		crste = READ_ONCE(*walk->last);
525 
526 		if (crste_hole(crste)) {
527 			if (!(walk->flags & DAT_WALK_IGN_HOLES))
528 				return -EFAULT;
529 			if (!(walk->flags & DAT_WALK_ANY))
530 				continue;
531 		}
532 
533 		the_op = walk->ops->crste_ops[crste.h.tt];
534 		if (the_op) {
535 			rc = the_op(walk->last, cur, next, walk);
536 			crste = READ_ONCE(*walk->last);
537 		}
538 		if (rc)
539 			break;
540 		if (!crste.h.i && !crste.h.fc) {
541 			if (!is_pmd(crste))
542 				rc = dat_crste_walk_range(max(start, cur), min(end, next),
543 							  _dereference_crste(crste), walk);
544 			else if (walk->ops->pte_entry)
545 				rc = dat_pte_walk_range(max(start, cur), min(end, next),
546 							dereference_pmd(crste.pmd), walk);
547 			if (rc)
548 				break;
549 		}
550 	}
551 	return rc;
552 }
553 
554 /**
555  * _dat_walk_gfn_range() - Walk DAT tables.
556  * @start: The first guest page frame to walk.
557  * @end: The guest page frame immediately after the last one to walk.
558  * @asce: The ASCE of the guest mapping.
559  * @ops: The gmap_walk_ops that will be used to perform the walk.
560  * @flags: Flags from WALK_* (currently only WALK_IGN_HOLES is supported).
561  * @priv: Will be passed as-is to the callbacks.
562  *
563  * Any callback returning non-zero causes the walk to stop immediately.
564  *
565  * Return: %-EINVAL in case of error, %-EFAULT if @start is too high for the
566  *         given ASCE unless the DAT_WALK_IGN_HOLES flag is specified,
567  *         otherwise it returns whatever the callbacks return.
568  */
569 long _dat_walk_gfn_range(gfn_t start, gfn_t end, union asce asce,
570 			 const struct dat_walk_ops *ops, int flags, void *priv)
571 {
572 	struct crst_table *table = dereference_asce(asce);
573 	struct dat_walk walk = {
574 		.ops	= ops,
575 		.asce	= asce,
576 		.priv	= priv,
577 		.flags	= flags,
578 		.start	= start,
579 		.end	= end,
580 	};
581 
582 	if (WARN_ON_ONCE(unlikely(!asce.val)))
583 		return -EINVAL;
584 	if (!asce_contains_gfn(asce, start))
585 		return (flags & DAT_WALK_IGN_HOLES) ? 0 : -EFAULT;
586 
587 	return dat_crste_walk_range(start, min(end, asce_end(asce)), table, &walk);
588 }
589 
590 #if KVM_S390_MANAGES_S390_GUEST
591 int dat_get_storage_key(union asce asce, gfn_t gfn, union skey *skey)
592 {
593 	union crste *crstep;
594 	union pgste pgste;
595 	union pte *ptep;
596 	int rc;
597 
598 again:
599 	skey->skey = 0;
600 	rc = dat_entry_walk(NULL, gfn, asce, DAT_WALK_ANY, TABLE_TYPE_PAGE_TABLE, &crstep, &ptep);
601 	if (rc)
602 		return rc;
603 
604 	if (!ptep) {
605 		union crste crste = READ_ONCE(*crstep);
606 
607 		if (!crste_leaf(crste) && !crste.h.i)
608 			goto again;
609 		if (!crste.s.fc1.pr)
610 			return 0;
611 
612 		skey->skey = page_get_storage_key(large_crste_to_phys(crste, gfn));
613 		return 0;
614 	}
615 	pgste = pgste_get_lock(ptep);
616 	if (ptep->h.i) {
617 		skey->acc = pgste.acc;
618 		skey->fp = pgste.fp;
619 	} else {
620 		skey->skey = page_get_storage_key(pte_origin(*ptep));
621 	}
622 	skey->r |= pgste.gr;
623 	skey->c |= pgste.gc;
624 	pgste_set_unlock(ptep, pgste);
625 	return 0;
626 }
627 
628 static void dat_update_ptep_sd(union pgste old, union pgste pgste, union pte *ptep)
629 {
630 	if (pgste.acc != old.acc || pgste.fp != old.fp || pgste.gr != old.gr || pgste.gc != old.gc)
631 		__atomic64_or(_PAGE_SD, &ptep->val);
632 }
633 
634 int dat_set_storage_key(struct kvm_s390_mmu_cache *mc, union asce asce, gfn_t gfn,
635 			union skey skey, bool nq)
636 {
637 	union pgste pgste, old;
638 	union crste *crstep;
639 	union pte *ptep;
640 	int rc;
641 
642 again:
643 	rc = dat_entry_walk(mc, gfn, asce, DAT_WALK_LEAF_ALLOC, TABLE_TYPE_PAGE_TABLE,
644 			    &crstep, &ptep);
645 	if (rc)
646 		return rc;
647 
648 	if (!ptep) {
649 		union crste crste = READ_ONCE(*crstep);
650 
651 		/* A large page has been split concurrently, try again */
652 		if (!crste_leaf(crste))
653 			goto again;
654 
655 		page_set_storage_key(large_crste_to_phys(crste, gfn), skey.skey, !nq);
656 		return 0;
657 	}
658 
659 	old = pgste_get_lock(ptep);
660 	pgste = old;
661 
662 	pgste.acc = skey.acc;
663 	pgste.fp = skey.fp;
664 	pgste.gc = skey.c;
665 	pgste.gr = skey.r;
666 
667 	if (!ptep->h.i) {
668 		union skey old_skey;
669 
670 		old_skey.skey = page_get_storage_key(pte_origin(*ptep));
671 		pgste.hc |= old_skey.c;
672 		pgste.hr |= old_skey.r;
673 		old_skey.c = old.gc;
674 		old_skey.r = old.gr;
675 		skey.r = 0;
676 		skey.c = 0;
677 		page_set_storage_key(pte_origin(*ptep), skey.skey, !nq);
678 	}
679 
680 	dat_update_ptep_sd(old, pgste, ptep);
681 	pgste_set_unlock(ptep, pgste);
682 	return 0;
683 }
684 
685 static bool page_cond_set_storage_key(phys_addr_t paddr, union skey skey, union skey *oldkey,
686 				      bool nq, bool mr, bool mc)
687 {
688 	oldkey->skey = page_get_storage_key(paddr);
689 	if (oldkey->acc == skey.acc && oldkey->fp == skey.fp &&
690 	    (oldkey->r == skey.r || mr) && (oldkey->c == skey.c || mc))
691 		return false;
692 	page_set_storage_key(paddr, skey.skey, !nq);
693 	return true;
694 }
695 
696 int dat_cond_set_storage_key(struct kvm_s390_mmu_cache *mmc, union asce asce, gfn_t gfn,
697 			     union skey skey, union skey *oldkey, bool nq, bool mr, bool mc)
698 {
699 	union pgste pgste, old;
700 	union crste *crstep;
701 	union skey prev;
702 	union pte *ptep;
703 	int rc;
704 
705 again:
706 	rc = dat_entry_walk(mmc, gfn, asce, DAT_WALK_LEAF_ALLOC, TABLE_TYPE_PAGE_TABLE,
707 			    &crstep, &ptep);
708 	if (rc)
709 		return rc;
710 
711 	if (!ptep) {
712 		union crste crste = READ_ONCE(*crstep);
713 
714 		/* A large page has been split concurrently, try again */
715 		if (!crste_leaf(crste))
716 			goto again;
717 		if (!oldkey)
718 			oldkey = &prev;
719 
720 		return page_cond_set_storage_key(large_crste_to_phys(crste, gfn), skey, oldkey,
721 						 nq, mr, mc);
722 	}
723 
724 	old = pgste_get_lock(ptep);
725 	pgste = old;
726 
727 	rc = 1;
728 	pgste.acc = skey.acc;
729 	pgste.fp = skey.fp;
730 	pgste.gc = skey.c;
731 	pgste.gr = skey.r;
732 	prev.skey = 0;
733 
734 	if (!ptep->h.i) {
735 		rc = page_cond_set_storage_key(pte_origin(*ptep), skey, &prev, nq, mr, mc);
736 		pgste.hc |= prev.c;
737 		pgste.hr |= prev.r;
738 		prev.c |= old.gc;
739 		prev.r |= old.gr;
740 	} else {
741 		prev.acc = old.acc;
742 		prev.fp = old.fp;
743 		prev.c = old.gc;
744 		prev.r = old.gr;
745 	}
746 	if (oldkey)
747 		*oldkey = prev;
748 
749 	dat_update_ptep_sd(old, pgste, ptep);
750 	pgste_set_unlock(ptep, pgste);
751 	return rc;
752 }
753 
754 int dat_reset_reference_bit(union asce asce, gfn_t gfn, union skey *skey)
755 {
756 	union pgste pgste, old;
757 	union crste *crstep;
758 	union pte *ptep;
759 	int rc;
760 
761 	skey->skey = 0;
762 again:
763 	rc = dat_entry_walk(NULL, gfn, asce, DAT_WALK_ANY, TABLE_TYPE_PAGE_TABLE, &crstep, &ptep);
764 	if (rc)
765 		return rc;
766 
767 	if (!ptep) {
768 		union crste crste = READ_ONCE(*crstep);
769 
770 		/* A large page has been split concurrently, try again */
771 		if (!crste_leaf(crste) && !crste.h.i)
772 			goto again;
773 		if (!crste.s.fc1.pr)
774 			return 0;
775 		skey->skey = page_reset_referenced(large_crste_to_phys(crste, gfn)) << 1;
776 		return 0;
777 	}
778 	old = pgste_get_lock(ptep);
779 	pgste = old;
780 
781 	if (!ptep->h.i) {
782 		skey->skey = page_reset_referenced(pte_origin(*ptep)) << 1;
783 		pgste.hr = skey->r;
784 	}
785 	skey->r |= pgste.gr;
786 	skey->c |= pgste.gc;
787 	pgste.gr = 0;
788 
789 	dat_update_ptep_sd(old, pgste, ptep);
790 	pgste_set_unlock(ptep, pgste);
791 	return 0;
792 }
793 
794 static long dat_reset_skeys_pte(union pte *ptep, gfn_t gfn, gfn_t next, struct dat_walk *walk)
795 {
796 	union pgste pgste;
797 
798 	pgste = pgste_get_lock(ptep);
799 	pgste.acc = 0;
800 	pgste.fp = 0;
801 	pgste.gr = 0;
802 	pgste.gc = 0;
803 	if (ptep->s.pr)
804 		page_set_storage_key(pte_origin(*ptep), PAGE_DEFAULT_KEY, 1);
805 	pgste_set_unlock(ptep, pgste);
806 
807 	if (need_resched())
808 		return next;
809 	return 0;
810 }
811 
812 static long dat_reset_skeys_crste(union crste *crstep, gfn_t gfn, gfn_t next, struct dat_walk *walk)
813 {
814 	phys_addr_t addr, end, origin = crste_origin_large(*crstep);
815 
816 	if (!crstep->h.fc || !crstep->s.fc1.pr)
817 		return 0;
818 
819 	addr = ((max(gfn, walk->start) - gfn) << PAGE_SHIFT) + origin;
820 	end = ((min(next, walk->end) - gfn) << PAGE_SHIFT) + origin;
821 	while (ALIGN(addr + 1, _SEGMENT_SIZE) <= end)
822 		addr = sske_frame(addr, PAGE_DEFAULT_KEY);
823 	for ( ; addr < end; addr += PAGE_SIZE)
824 		page_set_storage_key(addr, PAGE_DEFAULT_KEY, 1);
825 
826 	if (need_resched())
827 		return next;
828 	return 0;
829 }
830 
831 long dat_reset_skeys(union asce asce, gfn_t start)
832 {
833 	const struct dat_walk_ops ops = {
834 		.pte_entry = dat_reset_skeys_pte,
835 		.pmd_entry = dat_reset_skeys_crste,
836 		.pud_entry = dat_reset_skeys_crste,
837 	};
838 
839 	return _dat_walk_gfn_range(start, asce_end(asce), asce, &ops, DAT_WALK_IGN_HOLES, NULL);
840 }
841 #endif /* KVM_S390_MANAGES_S390_GUEST */
842 
843 static long _dat_slot_pte(union pte *ptep, gfn_t gfn, gfn_t next, struct dat_walk *walk)
844 {
845 	union pte new_pte, pte = READ_ONCE(*ptep);
846 	union pgste pgste;
847 
848 	new_pte = walk->priv ? _PTE_EMPTY : _PTE_TOK(_DAT_TOKEN_PIC, PGM_ADDRESSING);
849 
850 	/* Table entry already in the desired state. */
851 	if (pte.val == new_pte.val)
852 		return 0;
853 
854 	pgste = pgste_get_lock(ptep);
855 	pgste = __dat_ptep_xchg(ptep, pgste, new_pte, gfn, walk->asce, false);
856 	pgste.cmma_d = 0;
857 	pgste_set_unlock(ptep, pgste);
858 
859 	return 0;
860 }
861 
862 static long _dat_slot_crste(union crste *crstep, gfn_t gfn, gfn_t next, struct dat_walk *walk)
863 {
864 	union crste new_crste, crste = READ_ONCE(*crstep);
865 	struct kvm_s390_mmu_cache *mc = walk->priv;
866 
867 	new_crste = mc ? _CRSTE_EMPTY(crste.h.tt) : _CRSTE_HOLE(crste.h.tt);
868 
869 	/* Table entry already in the desired state. */
870 	if (crste.val == new_crste.val)
871 		return 0;
872 
873 	/* This table entry needs to be updated. */
874 	if (walk->start <= gfn && walk->end >= next) {
875 		if (!dat_crstep_xchg_atomic(crstep, crste, new_crste, gfn, walk->asce))
876 			return -EINVAL;
877 		/* A lower level table was present, needs to be freed. */
878 		if (!crste.h.fc && !crste.h.i) {
879 			if (is_pmd(crste))
880 				dat_free_pt(dereference_pmd(crste.pmd));
881 			else
882 				dat_free_level(dereference_crste(crste), true);
883 		}
884 		return 0;
885 	}
886 
887 	/* A lower level table is present, things will handled there. */
888 	if (!crste.h.fc && !crste.h.i)
889 		return 0;
890 	/* Split (install a lower level table), and handle things there. */
891 	if (mc)
892 		return dat_split_crste(mc, crstep, gfn, walk->asce, false);
893 	/* A large page should never cross memslots boundaries */
894 	return -EINVAL;
895 }
896 
897 static const struct dat_walk_ops dat_slot_ops = {
898 	.pte_entry = _dat_slot_pte,
899 	.crste_ops = { _dat_slot_crste, _dat_slot_crste, _dat_slot_crste, _dat_slot_crste, },
900 };
901 
902 int dat_set_slot(struct kvm_s390_mmu_cache *mc, union asce asce, gfn_t start, gfn_t end)
903 {
904 	return _dat_walk_gfn_range(start, end, asce, &dat_slot_ops,
905 				   DAT_WALK_IGN_HOLES | DAT_WALK_ANY, mc);
906 }
907 
908 static void pgste_set_unlock_multiple(union pte *first, int n, union pgste *pgstes)
909 {
910 	int i;
911 
912 	for (i = 0; i < n; i++)
913 		pgste_set_unlock(first + i, pgstes[i]);
914 }
915 
916 static bool pgste_get_trylock_multiple(union pte *first, int n, union pgste *pgstes)
917 {
918 	int i;
919 
920 	for (i = 0; i < n; i++) {
921 		if (!pgste_get_trylock(first + i, pgstes + i))
922 			break;
923 	}
924 	if (i == n)
925 		return true;
926 	pgste_set_unlock_multiple(first, i, pgstes);
927 	return false;
928 }
929 
930 unsigned long dat_get_ptval(struct page_table *table, struct ptval_param param)
931 {
932 	union pgste pgstes[4] = {};
933 	unsigned long res = 0;
934 	int i, n;
935 
936 	n = param.len + 1;
937 
938 	while (!pgste_get_trylock_multiple(table->ptes + param.offset, n, pgstes))
939 		cpu_relax();
940 
941 	for (i = 0; i < n; i++)
942 		res = res << 16 | pgstes[i].val16;
943 
944 	pgste_set_unlock_multiple(table->ptes + param.offset, n, pgstes);
945 	return res;
946 }
947 
948 void dat_set_ptval(struct page_table *table, struct ptval_param param, unsigned long val)
949 {
950 	union pgste pgstes[4] = {};
951 	int i, n;
952 
953 	n = param.len + 1;
954 
955 	while (!pgste_get_trylock_multiple(table->ptes + param.offset, n, pgstes))
956 		cpu_relax();
957 
958 	for (i = param.len; i >= 0; i--) {
959 		pgstes[i].val16 = val;
960 		val = val >> 16;
961 	}
962 
963 	pgste_set_unlock_multiple(table->ptes + param.offset, n, pgstes);
964 }
965 
966 static long _dat_test_young_pte(union pte *ptep, gfn_t start, gfn_t end, struct dat_walk *walk)
967 {
968 	return ptep->s.y;
969 }
970 
971 static long _dat_test_young_crste(union crste *crstep, gfn_t start, gfn_t end,
972 				  struct dat_walk *walk)
973 {
974 	return crstep->h.fc && crstep->s.fc1.y;
975 }
976 
977 static const struct dat_walk_ops test_age_ops = {
978 	.pte_entry = _dat_test_young_pte,
979 	.pmd_entry = _dat_test_young_crste,
980 	.pud_entry = _dat_test_young_crste,
981 };
982 
983 /**
984  * dat_test_age_gfn() - Test young.
985  * @asce: The ASCE whose address range is to be tested.
986  * @start: The first guest frame of the range to check.
987  * @end: The guest frame after the last in the range.
988  *
989  * Context: called by KVM common code with the kvm mmu write lock held.
990  *
991  * Return: %true if any page in the given range is young, otherwise %false.
992  */
993 bool dat_test_age_gfn(union asce asce, gfn_t start, gfn_t end)
994 {
995 	return _dat_walk_gfn_range(start, end, asce, &test_age_ops, 0, NULL) > 0;
996 }
997 
998 #if KVM_S390_MANAGES_S390_GUEST
999 static long dat_set_pn_crste(union crste *crstep, gfn_t gfn, gfn_t next, struct dat_walk *walk)
1000 {
1001 	union crste newcrste, oldcrste;
1002 	int *n = walk->priv;
1003 
1004 	do {
1005 		oldcrste = READ_ONCE(*crstep);
1006 		if (!oldcrste.h.fc || oldcrste.h.i || oldcrste.h.p)
1007 			return 0;
1008 		if (oldcrste.s.fc1.prefix_notif)
1009 			break;
1010 		newcrste = oldcrste;
1011 		newcrste.s.fc1.prefix_notif = 1;
1012 	} while (!dat_crstep_xchg_atomic(crstep, oldcrste, newcrste, gfn, walk->asce));
1013 	*n = 2;
1014 	return 0;
1015 }
1016 
1017 static long dat_set_pn_pte(union pte *ptep, gfn_t gfn, gfn_t next, struct dat_walk *walk)
1018 {
1019 	int *n = walk->priv;
1020 	union pgste pgste;
1021 
1022 	pgste = pgste_get_lock(ptep);
1023 	if (!ptep->h.i && !ptep->h.p) {
1024 		pgste.prefix_notif = 1;
1025 		*n += 1;
1026 	}
1027 	pgste_set_unlock(ptep, pgste);
1028 	return 0;
1029 }
1030 
1031 int dat_set_prefix_notif_bit(union asce asce, gfn_t gfn)
1032 {
1033 	static const struct dat_walk_ops ops = {
1034 		.pte_entry = dat_set_pn_pte,
1035 		.pmd_entry = dat_set_pn_crste,
1036 		.pud_entry = dat_set_pn_crste,
1037 	};
1038 
1039 	int n = 0;
1040 
1041 	_dat_walk_gfn_range(gfn, gfn + 2, asce, &ops, DAT_WALK_IGN_HOLES, &n);
1042 	if (n != 2)
1043 		return -EAGAIN;
1044 	return 0;
1045 }
1046 
1047 /**
1048  * dat_perform_essa() - Perform ESSA actions on the PGSTE.
1049  * @asce: The asce to operate on.
1050  * @gfn: The guest page frame to operate on.
1051  * @orc: The specific action to perform, see the ESSA_SET_* macros.
1052  * @state: The storage attributes to be returned to the guest.
1053  * @dirty: Returns whether the function dirtied a previously clean entry.
1054  *
1055  * Context: Called with kvm->mmu_lock held.
1056  *
1057  * Return:
1058  * * %1 if the page state has been altered and the page is to be added to the CBRL
1059  * * %0 if the page state has been altered, but the page is not to be added to the CBRL
1060  * * %-1 if the page state has not been altered and the page is not to be added to the CBRL
1061  * * %PGM_ADDRESSING if the guest page-table walk failed
1062  */
1063 int dat_perform_essa(union asce asce, gfn_t gfn, int orc, union essa_state *state, bool *dirty)
1064 {
1065 	union crste *crstep;
1066 	union pgste pgste;
1067 	union pte *ptep;
1068 	int res = 0;
1069 
1070 	if (dat_entry_walk(NULL, gfn, asce, DAT_WALK_ANY, TABLE_TYPE_PAGE_TABLE, &crstep, &ptep))
1071 		return PGM_ADDRESSING;
1072 	if (!ptep) {
1073 		*state = (union essa_state) { .exception = 1 };
1074 		return -1;
1075 	}
1076 
1077 	pgste = pgste_get_lock(ptep);
1078 
1079 	*state = (union essa_state) {
1080 		.content = (ptep->h.i << 1) + (ptep->h.i && pgste.zero),
1081 		.nodat = pgste.nodat,
1082 		.usage = pgste.usage,
1083 		};
1084 
1085 	switch (orc) {
1086 	case ESSA_GET_STATE:
1087 		res = -1;
1088 		break;
1089 	case ESSA_SET_STABLE:
1090 		pgste.usage = PGSTE_GPS_USAGE_STABLE;
1091 		pgste.nodat = 0;
1092 		break;
1093 	case ESSA_SET_UNUSED:
1094 		pgste.usage = PGSTE_GPS_USAGE_UNUSED;
1095 		if (ptep->h.i)
1096 			res = 1;
1097 		break;
1098 	case ESSA_SET_VOLATILE:
1099 		pgste.usage = PGSTE_GPS_USAGE_VOLATILE;
1100 		if (ptep->h.i)
1101 			res = 1;
1102 		break;
1103 	case ESSA_SET_POT_VOLATILE:
1104 		if (!ptep->h.i) {
1105 			pgste.usage = PGSTE_GPS_USAGE_POT_VOLATILE;
1106 		} else if (pgste.zero) {
1107 			pgste.usage = PGSTE_GPS_USAGE_VOLATILE;
1108 		} else if (!pgste.gc) {
1109 			pgste.usage = PGSTE_GPS_USAGE_VOLATILE;
1110 			res = 1;
1111 		}
1112 		break;
1113 	case ESSA_SET_STABLE_RESIDENT:
1114 		pgste.usage = PGSTE_GPS_USAGE_STABLE;
1115 		/*
1116 		 * Since the resident state can go away any time after this
1117 		 * call, we will not make this page resident. We can revisit
1118 		 * this decision if a guest will ever start using this.
1119 		 */
1120 		break;
1121 	case ESSA_SET_STABLE_IF_RESIDENT:
1122 		if (!ptep->h.i)
1123 			pgste.usage = PGSTE_GPS_USAGE_STABLE;
1124 		break;
1125 	case ESSA_SET_STABLE_NODAT:
1126 		pgste.usage = PGSTE_GPS_USAGE_STABLE;
1127 		pgste.nodat = 1;
1128 		break;
1129 	default:
1130 		WARN_ONCE(1, "Invalid ORC!");
1131 		res = -1;
1132 		break;
1133 	}
1134 	/* If we are discarding a page, set it to logical zero. */
1135 	pgste.zero = res == 1;
1136 	if (orc > 0) {
1137 		*dirty = !pgste.cmma_d;
1138 		pgste.cmma_d = 1;
1139 	}
1140 
1141 	pgste_set_unlock(ptep, pgste);
1142 
1143 	return res;
1144 }
1145 
1146 static long dat_reset_cmma_pte(union pte *ptep, gfn_t gfn, gfn_t next, struct dat_walk *walk)
1147 {
1148 	union pgste pgste;
1149 
1150 	pgste = pgste_get_lock(ptep);
1151 	pgste.usage = 0;
1152 	pgste.nodat = 0;
1153 	pgste.cmma_d = 0;
1154 	pgste_set_unlock(ptep, pgste);
1155 	if (need_resched())
1156 		return next;
1157 	return 0;
1158 }
1159 
1160 long dat_reset_cmma(union asce asce, gfn_t start)
1161 {
1162 	const struct dat_walk_ops dat_reset_cmma_ops = {
1163 		.pte_entry = dat_reset_cmma_pte,
1164 	};
1165 
1166 	return _dat_walk_gfn_range(start, asce_end(asce), asce, &dat_reset_cmma_ops,
1167 				   DAT_WALK_IGN_HOLES, NULL);
1168 }
1169 
1170 struct dat_get_cmma_state {
1171 	gfn_t start;
1172 	gfn_t end;
1173 	unsigned int count;
1174 	u8 *values;
1175 	atomic64_t *remaining;
1176 };
1177 
1178 static long __dat_peek_cmma_pte(union pte *ptep, gfn_t gfn, gfn_t next, struct dat_walk *walk)
1179 {
1180 	struct dat_get_cmma_state *state = walk->priv;
1181 	union pgste pgste;
1182 
1183 	pgste = pgste_get_lock(ptep);
1184 	state->values[gfn - walk->start] = pgste.usage | (pgste.nodat << 6);
1185 	pgste_set_unlock(ptep, pgste);
1186 	state->end = next;
1187 
1188 	return 0;
1189 }
1190 
1191 static long __dat_peek_cmma_crste(union crste *crstep, gfn_t gfn, gfn_t next, struct dat_walk *walk)
1192 {
1193 	struct dat_get_cmma_state *state = walk->priv;
1194 
1195 	if (crstep->h.i)
1196 		state->end = min(walk->end, next);
1197 	return 0;
1198 }
1199 
1200 int dat_peek_cmma(gfn_t start, union asce asce, unsigned int *count, u8 *values)
1201 {
1202 	const struct dat_walk_ops ops = {
1203 		.pte_entry = __dat_peek_cmma_pte,
1204 		.pmd_entry = __dat_peek_cmma_crste,
1205 		.pud_entry = __dat_peek_cmma_crste,
1206 		.p4d_entry = __dat_peek_cmma_crste,
1207 		.pgd_entry = __dat_peek_cmma_crste,
1208 	};
1209 	struct dat_get_cmma_state state = { .values = values, };
1210 	int rc;
1211 
1212 	rc = _dat_walk_gfn_range(start, start + *count, asce, &ops, DAT_WALK_DEFAULT, &state);
1213 	*count = state.end >= start ? state.end - start : 0;
1214 	/* Return success if at least one value was saved, otherwise an error. */
1215 	return (rc == -EFAULT && *count > 0) ? 0 : rc;
1216 }
1217 
1218 static long __dat_get_cmma_pte(union pte *ptep, gfn_t gfn, gfn_t next, struct dat_walk *walk)
1219 {
1220 	struct dat_get_cmma_state *state = walk->priv;
1221 	union pgste pgste;
1222 
1223 	if (state->start != -1) {
1224 		if ((gfn - state->end) > KVM_S390_MAX_BIT_DISTANCE)
1225 			return 1;
1226 		if (gfn - state->start >= state->count)
1227 			return 1;
1228 	}
1229 
1230 	if (!READ_ONCE(*pgste_of(ptep)).cmma_d)
1231 		return 0;
1232 
1233 	pgste = pgste_get_lock(ptep);
1234 	if (pgste.cmma_d) {
1235 		if (state->start == -1)
1236 			state->start = gfn;
1237 		pgste.cmma_d = 0;
1238 		atomic64_dec(state->remaining);
1239 		state->values[gfn - state->start] = pgste.usage | pgste.nodat << 6;
1240 		state->end = next;
1241 	}
1242 	pgste_set_unlock(ptep, pgste);
1243 	return 0;
1244 }
1245 
1246 int dat_get_cmma(union asce asce, gfn_t *start, unsigned int *count, u8 *values, atomic64_t *rem)
1247 {
1248 	const struct dat_walk_ops ops = { .pte_entry = __dat_get_cmma_pte, };
1249 	struct dat_get_cmma_state state = {
1250 		.remaining = rem,
1251 		.values = values,
1252 		.count = *count,
1253 		.start = -1,
1254 	};
1255 
1256 	_dat_walk_gfn_range(*start, asce_end(asce), asce, &ops, DAT_WALK_IGN_HOLES, &state);
1257 	/* If no dirty pages were found, wrap around and continue searching */
1258 	if (*start && state.start == -1)
1259 		_dat_walk_gfn_range(0, *start, asce, &ops, DAT_WALK_IGN_HOLES, &state);
1260 
1261 	if (state.start == -1) {
1262 		*count = 0;
1263 	} else {
1264 		*count = state.end - state.start;
1265 		*start = state.start;
1266 	}
1267 
1268 	return 0;
1269 }
1270 
1271 struct dat_set_cmma_state {
1272 	unsigned long mask;
1273 	const u8 *bits;
1274 };
1275 
1276 static long __dat_set_cmma_pte(union pte *ptep, gfn_t gfn, gfn_t next, struct dat_walk *walk)
1277 {
1278 	struct dat_set_cmma_state *state = walk->priv;
1279 	union pgste pgste, tmp;
1280 
1281 	tmp.val = (state->bits[gfn - walk->start] << 24) & state->mask;
1282 
1283 	pgste = pgste_get_lock(ptep);
1284 	pgste.usage = tmp.usage;
1285 	pgste.nodat = tmp.nodat;
1286 	pgste_set_unlock(ptep, pgste);
1287 
1288 	return 0;
1289 }
1290 
1291 /**
1292  * dat_set_cmma_bits() - Set CMMA bits for a range of guest pages.
1293  * @mc: Cache used for allocations.
1294  * @asce: The ASCE of the guest.
1295  * @gfn: The guest frame of the fist page whose CMMA bits are to set.
1296  * @count: How many pages need to be processed.
1297  * @mask: Which PGSTE bits should be set.
1298  * @bits: Points to an array with the CMMA attributes.
1299  *
1300  * This function sets the CMMA attributes for the given pages. If the input
1301  * buffer has zero length, no action is taken, otherwise the attributes are
1302  * set and the mm->context.uses_cmm flag is set.
1303  *
1304  * Each byte in @bits contains new values for bits 32-39 of the PGSTE.
1305  * Currently, only the fields NT and US are applied.
1306  *
1307  * Return: %0 in case of success, a negative error value otherwise.
1308  */
1309 int dat_set_cmma_bits(struct kvm_s390_mmu_cache *mc, union asce asce, gfn_t gfn,
1310 		      unsigned long count, unsigned long mask, const uint8_t *bits)
1311 {
1312 	const struct dat_walk_ops ops = { .pte_entry = __dat_set_cmma_pte, };
1313 	struct dat_set_cmma_state state = { .mask = mask, .bits = bits, };
1314 	union crste *crstep;
1315 	union pte *ptep;
1316 	gfn_t cur;
1317 	int rc;
1318 
1319 	for (cur = ALIGN_DOWN(gfn, _PAGE_ENTRIES); cur < gfn + count; cur += _PAGE_ENTRIES) {
1320 		rc = dat_entry_walk(mc, cur, asce, DAT_WALK_ALLOC, TABLE_TYPE_PAGE_TABLE,
1321 				    &crstep, &ptep);
1322 		if (rc)
1323 			return rc;
1324 	}
1325 	return _dat_walk_gfn_range(gfn, gfn + count, asce, &ops, DAT_WALK_IGN_HOLES, &state);
1326 }
1327 #endif /* KVM_S390_MANAGES_S390_GUEST */
1328