mm/truncate.c: fix THP handling in invalidate_mapping_pages()
authorJan Kara <jack@suse.cz>
Mon, 10 Jul 2017 22:48:59 +0000 (15:48 -0700)
committerLinus Torvalds <torvalds@linux-foundation.org>
Mon, 10 Jul 2017 23:32:32 +0000 (16:32 -0700)
The condition checking for THP straddling end of invalidated range is
wrong - it checks 'index' against 'end' but 'index' has been already
advanced to point to the end of THP and thus the condition can never be
true.  As a result THP straddling 'end' has been fully invalidated.
Given the nature of invalidate_mapping_pages(), this could be only
performance issue.  In fact, we are lucky the condition is wrong because
if it was ever true, we'd leave locked page behind.

Fix the condition checking for THP straddling 'end' and also properly
unlock the page.  Also update the comment before the condition to
explain why we decide not to invalidate the page as it was not clear to
me and I had to ask Kirill.

Link: http://lkml.kernel.org/r/20170619124723.21656-1-jack@suse.cz
Signed-off-by: Jan Kara <jack@suse.cz>
Acked-by: Kirill A. Shutemov <kirill.shutemov@linux.intel.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
mm/truncate.c

index 6479ed2afc53fb9dd8d9719051ea77e7a5b200af..2330223841fbbdf40c4e50764a11a557d9c7b426 100644 (file)
@@ -530,9 +530,15 @@ unsigned long invalidate_mapping_pages(struct address_space *mapping,
                        } else if (PageTransHuge(page)) {
                                index += HPAGE_PMD_NR - 1;
                                i += HPAGE_PMD_NR - 1;
                        } else if (PageTransHuge(page)) {
                                index += HPAGE_PMD_NR - 1;
                                i += HPAGE_PMD_NR - 1;
-                               /* 'end' is in the middle of THP */
-                               if (index ==  round_down(end, HPAGE_PMD_NR))
+                               /*
+                                * 'end' is in the middle of THP. Don't
+                                * invalidate the page as the part outside of
+                                * 'end' could be still useful.
+                                */
+                               if (index > end) {
+                                       unlock_page(page);
                                        continue;
                                        continue;
+                               }
                        }
 
                        ret = invalidate_inode_page(page);
                        }
 
                        ret = invalidate_inode_page(page);