[Devel] [PATCH RHEL9 COMMIT] ms/mm/shmem: fix race in shmem_undo_range w/THP

Konstantin Khorenko khorenko at virtuozzo.com
Tue Feb 27 17:36:26 MSK 2024


The commit is pushed to "branch-rh9-5.14.0-362.18.1.vz9.40.x-ovz" and will appear at https://src.openvz.org/scm/ovz/vzkernel.git
after rh9-5.14.0-362.18.1.vz9.40.1
------>
commit 3961da0b9522818e9f8b017ee42d65d3b3c033e6
Author: David Stevens <stevensd at chromium.org>
Date:   Tue Apr 18 17:40:31 2023 +0900

    ms/mm/shmem: fix race in shmem_undo_range w/THP
    
    Split folios during the second loop of shmem_undo_range.  It's not
    sufficient to only split folios when dealing with partial pages, since
    it's possible for a THP to be faulted in after that point.  Calling
    truncate_inode_folio in that situation can result in throwing away data
    outside of the range being targeted.
    
    [akpm at linux-foundation.org: tidy up comment layout]
    Link: https://lkml.kernel.org/r/20230418084031.3439795-1-stevensd@google.com
    mFixes: b9a8a4195c7d ("truncate,shmem: Handle truncates that split large folios")
    Signed-off-by: David Stevens <stevensd at chromium.org>
    Cc: Matthew Wilcox (Oracle) <willy at infradead.org>
    Cc: Suleiman Souhlal <suleiman at google.com>
    Cc: <stable at vger.kernel.org>
    Signed-off-by: Andrew Morton <akpm at linux-foundation.org>
    
    https://virtuozzo.atlassian.net/browse/PSBM-154569
    
    (cherry picked from commit 55ac8bbe358bdd2f3c044c12f249fd22d48fe015)
    Signed-off-by: Konstantin Khorenko <khorenko at virtuozzo.com>
    
    Feature: fix ms/mm
---
 mm/shmem.c | 19 ++++++++++++++++++-
 1 file changed, 18 insertions(+), 1 deletion(-)

diff --git a/mm/shmem.c b/mm/shmem.c
index fbfecb73e740..a9b36d089340 100644
--- a/mm/shmem.c
+++ b/mm/shmem.c
@@ -1051,7 +1051,24 @@ static void shmem_undo_range(struct inode *inode, loff_t lstart, loff_t lend,
 				}
 				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;
+					}
+				}
 			}
 			index = folio->index + folio_nr_pages(folio) - 1;
 			folio_unlock(folio);


More information about the Devel mailing list