Skip to content

Commit eef88fe

Browse files
davidhildenbrandAlexander Gordeev
authored andcommitted
s390/uv: Split large folios in gmap_make_secure()
While s390x makes sure to never have PMD-mapped THP in processes that use KVM -- by remapping them using PTEs in thp_split_walk_pmd_entry()->split_huge_pmd() -- there is still the possibility of having PTE-mapped THPs (large folios) mapped into guest memory. This would happen if user space allocates memory before calling KVM_CREATE_VM (which would call s390_enable_sie()). With upstream QEMU, this currently doesn't happen, because guest memory is setup and conditionally preallocated after KVM_CREATE_VM. Could it happen with shmem/file-backed memory when another process allocated memory in the pagecache? Likely, although currently not a common setup. Trying to split any PTE-mapped large folios sounds like the right and future-proof thing to do here. So let's call split_folio() and handle the return values accordingly. Reviewed-by: Claudio Imbrenda <[email protected]> Signed-off-by: David Hildenbrand <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Heiko Carstens <[email protected]> Signed-off-by: Alexander Gordeev <[email protected]>
1 parent 68ad474 commit eef88fe

File tree

1 file changed

+25
-6
lines changed
  • arch/s390/kernel

1 file changed

+25
-6
lines changed

arch/s390/kernel/uv.c

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -338,11 +338,10 @@ int gmap_make_secure(struct gmap *gmap, unsigned long gaddr, void *uvcb)
338338
goto out;
339339
if (pte_present(*ptep) && !(pte_val(*ptep) & _PAGE_INVALID) && pte_write(*ptep)) {
340340
folio = page_folio(pte_page(*ptep));
341-
rc = -EINVAL;
342-
if (folio_test_large(folio))
343-
goto unlock;
344341
rc = -EAGAIN;
345-
if (folio_trylock(folio)) {
342+
if (folio_test_large(folio)) {
343+
rc = -E2BIG;
344+
} else if (folio_trylock(folio)) {
346345
if (should_export_before_import(uvcb, gmap->mm))
347346
uv_convert_from_secure(PFN_PHYS(folio_pfn(folio)));
348347
rc = make_folio_secure(folio, uvcb);
@@ -353,15 +352,35 @@ int gmap_make_secure(struct gmap *gmap, unsigned long gaddr, void *uvcb)
353352
* Once we drop the PTL, the folio may get unmapped and
354353
* freed immediately. We need a temporary reference.
355354
*/
356-
if (rc == -EAGAIN)
355+
if (rc == -EAGAIN || rc == -E2BIG)
357356
folio_get(folio);
358357
}
359-
unlock:
360358
pte_unmap_unlock(ptep, ptelock);
361359
out:
362360
mmap_read_unlock(gmap->mm);
363361

364362
switch (rc) {
363+
case -E2BIG:
364+
folio_lock(folio);
365+
rc = split_folio(folio);
366+
folio_unlock(folio);
367+
folio_put(folio);
368+
369+
switch (rc) {
370+
case 0:
371+
/* Splitting succeeded, try again immediately. */
372+
goto again;
373+
case -EAGAIN:
374+
/* Additional folio references. */
375+
if (drain_lru(&drain_lru_called))
376+
goto again;
377+
return -EAGAIN;
378+
case -EBUSY:
379+
/* Unexpected race. */
380+
return -EAGAIN;
381+
}
382+
WARN_ON_ONCE(1);
383+
return -ENXIO;
365384
case -EAGAIN:
366385
/*
367386
* If we are here because the UVC returned busy or partial

0 commit comments

Comments
 (0)