Skip to contentSkip to conversation

mm/fadvise.c: do not discard partial pages with POSIX_FADV_DONTNEED

PATCH New [email protected] 1 participant 1 message Jun 8, 2016

Subsystems (2)
lore
Share & tools
lore

[email protected]

author maintainer
#0 lore raw

PATCH 7/7 mm/fadvise.c: do not discard partial pages with POSIX_FADV_DONTNEED

From: Oleg Drokin <[email protected]>
Subject: mm/fadvise.c: do not discard partial pages with POSIX_FADV_DONTNEED

I noticed that the logic in the fadvise64_64 syscall is incorrect for partial pages. While first page of the region is correctly skipped if it is partial, the last page of the region is mistakenly discarded. This leads to problems for applications that read data in non-page-aligned chunks discarding already processed data between the reads.

A somewhat misguided application that does something like write(XX bytes (non-page-alligned)); drop the data it just wrote; repeat gets a significant penalty in performance as a result.

---

 mm/fadvise.c |   11 +++++++++++
 1 file changed, 11 insertions(+)
mm/fadvise.c renamed from mm/fadvise.c~mm-do-not-discard-partial-pages-with-posix_fadv_dontneed +11 -0
diff -puN mm/fadvise.c~mm-do-not-discard-partial-pages-with-posix_fadv_dontneed mm/fadvise.c
@@ -126,6 +126,17 @@ SYSCALL_DEFINE4(fadvise64_64, int, fd, l
 		 */
 		start_index = (offset+(PAGE_SIZE-1)) >> PAGE_SHIFT;
 		end_index = (endbyte >> PAGE_SHIFT);
+		if ((endbyte & ~PAGE_MASK) != ~PAGE_MASK) {
+			/* First page is tricky as 0 - 1 = -1, but pgoff_t
+			 * is unsigned, so the end_index >= start_index
+			 * check below would be true and we'll discard the whole
+			 * file cache which is not what was asked.
+			 */
+			if (end_index == 0)
+				break;
+
+			end_index--;
+		}
 
 		if (end_index >= start_index) {
 			unsigned long count = invalidate_mapping_pages(mapping,

_