mm/shmem: fix race in shmem_undo_range w/THP
[linux-2.6-block.git] / mm / shmem.c
index 91e2620148b2f6d789420e6736daef7a53e2cc5c..0d1ce70bce38028970802375b2409e9e6400364f 100644 (file)
@@ -1080,7 +1080,24 @@ whole_folios:
                                }
                                VM_BUG_ON_FOLIO(folio_test_writeback(folio),
                                                folio);
-                               truncate_inode_folio(mapping, folio);
+
+                               if (!folio_test_large(folio)) {
+                                       truncate_inode_folio(mapping, folio);
+                               } else if (truncate_inode_partial_folio(folio, lstart, lend)) {
+                                       /*
+                                        * If we split a page, reset the loop so
+                                        * that we pick up the new sub pages.
+                                        * Otherwise the THP was entirely
+                                        * dropped or the target range was
+                                        * zeroed, so just continue the loop as
+                                        * is.
+                                        */
+                                       if (!folio_test_large(folio)) {
+                                               folio_unlock(folio);
+                                               index = start;
+                                               break;
+                                       }
+                               }
                        }
                        folio_unlock(folio);
                }