1515#include <linux/pagemap.h>
1616#include <linux/swap.h>
1717#include <linux/pagewalk.h>
18+ #include <linux/backing-dev.h>
1819#include <asm/facility.h>
1920#include <asm/sections.h>
2021#include <asm/uv.h>
@@ -135,7 +136,7 @@ int uv_destroy_folio(struct folio *folio)
135136{
136137 int rc ;
137138
138- /* See gmap_make_secure(): large folios cannot be secure */
139+ /* Large folios cannot be secure */
139140 if (unlikely (folio_test_large (folio )))
140141 return 0 ;
141142
@@ -184,7 +185,7 @@ int uv_convert_from_secure_folio(struct folio *folio)
184185{
185186 int rc ;
186187
187- /* See gmap_make_secure(): large folios cannot be secure */
188+ /* Large folios cannot be secure */
188189 if (unlikely (folio_test_large (folio )))
189190 return 0 ;
190191
@@ -324,32 +325,87 @@ static int make_folio_secure(struct mm_struct *mm, struct folio *folio, struct u
324325}
325326
326327/**
327- * s390_wiggle_split_folio() - try to drain extra references to a folio and optionally split.
328+ * s390_wiggle_split_folio() - try to drain extra references to a folio and
329+ * split the folio if it is large.
328330 * @mm: the mm containing the folio to work on
329331 * @folio: the folio
330- * @split: whether to split a large folio
331332 *
332333 * Context: Must be called while holding an extra reference to the folio;
333334 * the mm lock should not be held.
334- * Return: 0 if the folio was split successfully ;
335- * -EAGAIN if the folio was not split successfully but another attempt
336- * can be made, or if @split was set to false ;
337- * -EINVAL in case of other errors. See split_folio().
335+ * Return: 0 if the operation was successful ;
336+ * -EAGAIN if splitting the large folio was not successful,
337+ * but another attempt can be made;
338+ * -EINVAL in case of other folio splitting errors. See split_folio().
338339 */
339- static int s390_wiggle_split_folio (struct mm_struct * mm , struct folio * folio , bool split )
340+ static int s390_wiggle_split_folio (struct mm_struct * mm , struct folio * folio )
340341{
341- int rc ;
342+ int rc , tried_splits ;
342343
343344 lockdep_assert_not_held (& mm -> mmap_lock );
344345 folio_wait_writeback (folio );
345346 lru_add_drain_all ();
346- if (split ) {
347+
348+ if (!folio_test_large (folio ))
349+ return 0 ;
350+
351+ for (tried_splits = 0 ; tried_splits < 2 ; tried_splits ++ ) {
352+ struct address_space * mapping ;
353+ loff_t lstart , lend ;
354+ struct inode * inode ;
355+
347356 folio_lock (folio );
348357 rc = split_folio (folio );
358+ if (rc != - EBUSY ) {
359+ folio_unlock (folio );
360+ return rc ;
361+ }
362+
363+ /*
364+ * Splitting with -EBUSY can fail for various reasons, but we
365+ * have to handle one case explicitly for now: some mappings
366+ * don't allow for splitting dirty folios; writeback will
367+ * mark them clean again, including marking all page table
368+ * entries mapping the folio read-only, to catch future write
369+ * attempts.
370+ *
371+ * While the system should be writing back dirty folios in the
372+ * background, we obtained this folio by looking up a writable
373+ * page table entry. On these problematic mappings, writable
374+ * page table entries imply dirty folios, preventing the
375+ * split in the first place.
376+ *
377+ * To prevent a livelock when trigger writeback manually and
378+ * letting the caller look up the folio again in the page
379+ * table (turning it dirty), immediately try to split again.
380+ *
381+ * This is only a problem for some mappings (e.g., XFS);
382+ * mappings that do not support writeback (e.g., shmem) do not
383+ * apply.
384+ */
385+ if (!folio_test_dirty (folio ) || folio_test_anon (folio ) ||
386+ !folio -> mapping || !mapping_can_writeback (folio -> mapping )) {
387+ folio_unlock (folio );
388+ break ;
389+ }
390+
391+ /*
392+ * Ideally, we'd only trigger writeback on this exact folio. But
393+ * there is no easy way to do that, so we'll stabilize the
394+ * mapping while we still hold the folio lock, so we can drop
395+ * the folio lock to trigger writeback on the range currently
396+ * covered by the folio instead.
397+ */
398+ mapping = folio -> mapping ;
399+ lstart = folio_pos (folio );
400+ lend = lstart + folio_size (folio ) - 1 ;
401+ inode = igrab (mapping -> host );
349402 folio_unlock (folio );
350403
351- if (rc != - EBUSY )
352- return rc ;
404+ if (unlikely (!inode ))
405+ break ;
406+
407+ filemap_write_and_wait_range (mapping , lstart , lend );
408+ iput (mapping -> host );
353409 }
354410 return - EAGAIN ;
355411}
@@ -393,8 +449,11 @@ int make_hva_secure(struct mm_struct *mm, unsigned long hva, struct uv_cb_header
393449 folio_walk_end (& fw , vma );
394450 mmap_read_unlock (mm );
395451
396- if (rc == - E2BIG || rc == - EBUSY )
397- rc = s390_wiggle_split_folio (mm , folio , rc == - E2BIG );
452+ if (rc == - E2BIG || rc == - EBUSY ) {
453+ rc = s390_wiggle_split_folio (mm , folio );
454+ if (!rc )
455+ rc = - EAGAIN ;
456+ }
398457 folio_put (folio );
399458
400459 return rc ;
@@ -403,15 +462,15 @@ EXPORT_SYMBOL_GPL(make_hva_secure);
403462
404463/*
405464 * To be called with the folio locked or with an extra reference! This will
406- * prevent gmap_make_secure from touching the folio concurrently. Having 2
407- * parallel arch_make_folio_accessible is fine, as the UV calls will become a
408- * no-op if the folio is already exported.
465+ * prevent kvm_s390_pv_make_secure() from touching the folio concurrently.
466+ * Having 2 parallel arch_make_folio_accessible is fine, as the UV calls will
467+ * become a no-op if the folio is already exported.
409468 */
410469int arch_make_folio_accessible (struct folio * folio )
411470{
412471 int rc = 0 ;
413472
414- /* See gmap_make_secure(): large folios cannot be secure */
473+ /* Large folios cannot be secure */
415474 if (unlikely (folio_test_large (folio )))
416475 return 0 ;
417476
0 commit comments