shmem: open code the page cache lookup in shmem_get_folio_gfp
authorChristoph Hellwig <hch@lst.de>
Tue, 7 Mar 2023 14:34:08 +0000 (15:34 +0100)
committerAndrew Morton <akpm@linux-foundation.org>
Thu, 6 Apr 2023 02:42:42 +0000 (19:42 -0700)
Use the very low level filemap_get_entry helper to look up the entry in
the xarray, and then:

 - don't bother locking the folio if only doing a userfault notification
 - open code locking the page and checking for truncation in a related
   code block

This will allow to eventually remove the FGP_ENTRY flag.

[hughd@google.com: adjust the new comment line]
Link: https://lkml.kernel.org/r/af178ebb-1076-a38c-1dc1-2a37ccce4a3@google.com
Link: https://lkml.kernel.org/r/20230307143410.28031-6-hch@lst.de
Signed-off-by: Christoph Hellwig <hch@lst.de>
Signed-off-by: Hugh Dickins <hughd@google.com>
Acked-by: Hugh Dickins <hughd@google.com>
Cc: Andreas Gruenbacher <agruenba@redhat.com>
Cc: Matthew Wilcox (Oracle) <willy@infradead.org>
Cc: Ryusuke Konishi <konishi.ryusuke@gmail.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
mm/shmem.c

index 1549866c208f5c797c626e70ab828dc2a2d54f3b..93cb39852a16ed30c2c7ec0ec1dc7bbcebc9a3fe 100644 (file)
@@ -1880,12 +1880,10 @@ repeat:
        sbinfo = SHMEM_SB(inode->i_sb);
        charge_mm = vma ? vma->vm_mm : NULL;
 
-       folio = __filemap_get_folio(mapping, index, FGP_ENTRY | FGP_LOCK, 0);
+       folio = filemap_get_entry(mapping, index);
        if (folio && vma && userfaultfd_minor(vma)) {
-               if (!xa_is_value(folio)) {
-                       folio_unlock(folio);
+               if (!xa_is_value(folio))
                        folio_put(folio);
-               }
                *fault_type = handle_userfault(vmf, VM_UFFD_MINOR);
                return 0;
        }
@@ -1901,6 +1899,14 @@ repeat:
        }
 
        if (folio) {
+               folio_lock(folio);
+
+               /* Has the folio been truncated or swapped out? */
+               if (unlikely(folio->mapping != mapping)) {
+                       folio_unlock(folio);
+                       folio_put(folio);
+                       goto repeat;
+               }
                if (sgp == SGP_WRITE)
                        folio_mark_accessed(folio);
                if (folio_test_uptodate(folio))