Skip to contentSkip to conversation

mm/khugepaged: fix dirty page handling for MADV_COLLAPSE

PATCH Superseded Shivank Garg 4 participants 13 messages Nov 20, 2025 – Nov 21, 2025

Subsystems (3)

v2 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

v2 cover mm/khugepaged: fix dirty page handling for MADV_COLLAPSE

MADV_COLLAPSE on file-backed mappings fails with -EINVAL when TEXT pages are dirty. This may affect real scenarios: package/container updates, executing binaries immediately after writing them, etc.

The issue is that collapse_file() triggers async writeback and returns SCAN_FAIL (maps to -EINVAL), expecting khugepaged to revisit later. But MADV_COLLAPSE is synchronous and userspace expects immediate success or a clear retry signal.

Reproduction:
- Copy 2MB-aligned executable to freshly mounted XFS/ext4
- Call MADV_COLLAPSE on .text section
- First call fails with -EINVAL (text pages dirty from copy)
- Second call succeeds (async writeback completed)

Issue Report:
https://lore.kernel.org/all/[email protected]

v2:
- Move writeback to madvise_collapse() (better abstraction, proper mmap_lock handling and does VMA revalidation after I/O) (Lorenzo)
- Rename to SCAN_PAGE_DIRTY to SCAN_PAGE_NOT_CLEAN and extend its use for all dirty/writeback folio cases that previously returned incorrect results (Dev)

v1: https://lore.kernel.org/all/[email protected]

Shivank Garg (2):
mm/khugepaged: do synchronous writeback for MADV_COLLAPSE mm/khugepaged: map dirty/writeback pages failures to EAGAIN

 include/trace/events/huge_memory.h |  3 ++-
 mm/khugepaged.c                    | 34 +++++++++++++++++++++++++++---
 2 files changed, 33 insertions(+), 4 deletions(-)

base-commit: 4a3f8fc3adb7046e44bd1feb2f5c5fe95296894f

Signature · 1 line
2.43.0
#1 lore raw

PATCH v2 1/2 mm/khugepaged: do synchronous writeback for MADV_COLLAPSE

When MADV_COLLAPSE is called on file-backed mappings (e.g., executable text sections), the pages may still be dirty from recent writes and cause collapse to fail with -EINVAL. This is particularly problematic for freshly copied executables on filesystems, where page cache folios remain dirty until background writeback completes.

The current code in collapse_file() triggers async writeback via filemap_flush() and expects khugepaged to revisit the page later. However, MADV_COLLAPSE is a synchronous operation where userspace expects immediate results.

Perform synchronous writeback in madvise_collapse() before attempting collapse to avoid failing on first attempt.

Fixes 34488399fa08 ("mm/madvise: add file and shmem support to MADV_COLLAPSE")
---
 mm/khugepaged.c | 26 ++++++++++++++++++++++++++
 1 file changed, 26 insertions(+)
mm/khugepaged.c +26 -0
index 97d1b2824386..066a332c76ad 100644
@@ -22,6 +22,7 @@
 #include <linux/dax.h>
 #include <linux/ksm.h>
 #include <linux/pgalloc.h>
+#include <linux/backing-dev.h>
 
 #include <asm/tlb.h>
 #include "internal.h"
@@ -2784,6 +2785,31 @@ int madvise_collapse(struct vm_area_struct *vma, unsigned long start,
 	hstart = (start + ~HPAGE_PMD_MASK) & HPAGE_PMD_MASK;
 	hend = end & HPAGE_PMD_MASK;
 
+	/*
+	 * For file-backed VMAs, perform synchronous writeback to ensure
+	 * dirty folios are flushed before attempting collapse. This avoids
+	 * failing on the first attempt when freshly-written executable text
+	 * is still dirty in the page cache.
+	 */
+	if (!vma_is_anonymous(vma) && vma->vm_file) {
+		struct address_space *mapping = vma->vm_file->f_mapping;
+
+		if (mapping_can_writeback(mapping)) {
+			pgoff_t pgoff_start = linear_page_index(vma, hstart);
+			pgoff_t pgoff_end = linear_page_index(vma, hend);
+			loff_t lstart = (loff_t)pgoff_start << PAGE_SHIFT;
+			loff_t lend = ((loff_t)pgoff_end << PAGE_SHIFT) - 1;
+
+			mmap_read_unlock(mm);
+			mmap_locked = false;
+
+			if (filemap_write_and_wait_range(mapping, lstart, lend)) {
+				last_fail = SCAN_FAIL;
+				goto out_maybelock;
+			}
+		}
+	}
+
 	for (addr = hstart; addr < hend; addr += HPAGE_PMD_SIZE) {
 		int result = SCAN_FAIL;
 
Signature · 1 line
2.43.0
reviewer
#2 lore raw

Re: [PATCH V2 1/2] mm/khugepaged: do synchronous writeback for MADV_COLLAPSE

Quoted 6 lines · Shivank Garg

When MADV_COLLAPSE is called on file-backed mappings (e.g., executable text sections), the pages may still be dirty from recent writes and cause collapse to fail with -EINVAL. This is particularly problematic for freshly copied executables on filesystems, where page cache folios remain dirty until background writeback completes.

Quoted 7 lines

The current code in collapse_file() triggers async writeback via filemap_flush() and expects khugepaged to revisit the page later. However, MADV_COLLAPSE is a synchronous operation where userspace expects immediate results.

Perform synchronous writeback in madvise_collapse() before attempting collapse to avoid failing on first attempt.

Thanks!

Quoted 30 lines
Fixes 34488399fa08 ("mm/madvise: add file and shmem support to MADV_COLLAPSE")
---
  mm/khugepaged.c | 26 ++++++++++++++++++++++++++
  1 file changed, 26 insertions(+)
mm/khugepaged.c +5 -0
index 97d1b2824386..066a332c76ad 100644
@@ -22,6 +22,7 @@
  #include <linux/dax.h>
  #include <linux/ksm.h>
  #include <linux/pgalloc.h>
+#include <linux/backing-dev.h>
 
  #include <asm/tlb.h>
  #include "internal.h"
@@ -2784,6 +2785,31 @@ int madvise_collapse(struct vm_area_struct *vma, unsigned long start,
  	hstart = (start + ~HPAGE_PMD_MASK) & HPAGE_PMD_MASK;
  	hend = end & HPAGE_PMD_MASK;
 
+	/*
+	 * For file-backed VMAs, perform synchronous writeback to ensure
+	 * dirty folios are flushed before attempting collapse. This avoids
+	 * failing on the first attempt when freshly-written executable text

mm/khugepaged.c int madvise_collapse(struct vm_area_struct *vma, unsigned long start, · line 2801

Quoted 10 lines
mm/khugepaged.c +10 -0
+	 * is still dirty in the page cache.
+	 */
+	if (!vma_is_anonymous(vma) && vma->vm_file) {
+		struct address_space *mapping = vma->vm_file->f_mapping;
+
+		if (mapping_can_writeback(mapping)) {
+			pgoff_t pgoff_start = linear_page_index(vma, hstart);
+			pgoff_t pgoff_end = linear_page_index(vma, hend);
+			loff_t lstart = (loff_t)pgoff_start << PAGE_SHIFT;
+			loff_t lend = ((loff_t)pgoff_end << PAGE_SHIFT) - 1;

It looks like we need to hold a reference to the file here before dropping the mmap lock :)

			file = get_file(vma->vm_file);

Without it, the vma could be destroyed by a concurrent munmap() while we are waiting in filemap_write_and_wait_range(), leading to a UAF on mapping, IIUC ...

mm/khugepaged.c int madvise_collapse(struct vm_area_struct *vma, unsigned long start, · line 2806

Quoted 5 lines
mm/khugepaged.c +5 -0
+
+			mmap_read_unlock(mm);
+			mmap_locked = false;
+
+			if (filemap_write_and_wait_range(mapping, lstart, lend)) {

And drop the reference :)

				fput(file);

mm/khugepaged.c int madvise_collapse(struct vm_area_struct *vma, unsigned long start, · line 2809

Quoted 3 lines
mm/khugepaged.c +3 -0
+				last_fail = SCAN_FAIL;
+				goto out_maybelock;
+			}

Same here :)

			fput(file);

mm/khugepaged.c int madvise_collapse(struct vm_area_struct *vma, unsigned long start, · line 2815

Quoted 6 lines
mm/khugepaged.c +3 -0
+		}
+	}
+
  	for (addr = hstart; addr < hend; addr += HPAGE_PMD_SIZE) {
  		int result = SCAN_FAIL;
 

Cheers,
Lance

#3 lore raw

Re: [PATCH V2 1/2] mm/khugepaged: do synchronous writeback for MADV_COLLAPSE

Quoted 61 lines · Lance Yang
Quoted 6 lines · Shivank Garg

When MADV_COLLAPSE is called on file-backed mappings (e.g., executable text sections), the pages may still be dirty from recent writes and cause collapse to fail with -EINVAL. This is particularly problematic for freshly copied executables on filesystems, where page cache folios remain dirty until background writeback completes.

Quoted 7 lines

The current code in collapse_file() triggers async writeback via filemap_flush() and expects khugepaged to revisit the page later. However, MADV_COLLAPSE is a synchronous operation where userspace expects immediate results.

Perform synchronous writeback in madvise_collapse() before attempting collapse to avoid failing on first attempt.

Thanks!

Quoted 30 lines
Fixes 34488399fa08 ("mm/madvise: add file and shmem support to MADV_COLLAPSE")

---
  mm/khugepaged.c | 26 ++++++++++++++++++++++++++

  1 file changed, 26 insertions(+)
mm/khugepaged.c +1 -0
index 97d1b2824386..066a332c76ad 100644
@@ -22,6 +22,7 @@
   #include <linux/dax.h>
   #include <linux/ksm.h>
   #include <linux/pgalloc.h>
+#include <linux/backing-dev.h>
     #include <asm/tlb.h>
   #include "internal.h"
@@ -2784,6 +2785,31 @@ int madvise_collapse(struct vm_area_struct *vma, unsigned long start,
       hstart = (start + ~HPAGE_PMD_MASK) & HPAGE_PMD_MASK;
       hend = end & HPAGE_PMD_MASK;

  +    /*
+     * For file-backed VMAs, perform synchronous writeback to ensure +     * dirty folios are flushed before attempting collapse. This avoids +     * failing on the first attempt when freshly-written executable text +     * is still dirty in the page cache.
+     */

Quoted 8 lines

+    if (!vma_is_anonymous(vma) && vma->vm_file) {
+        struct address_space *mapping = vma->vm_file->f_mapping; +
+        if (mapping_can_writeback(mapping)) {
+            pgoff_t pgoff_start = linear_page_index(vma, hstart); +            pgoff_t pgoff_end = linear_page_index(vma, hend); +            loff_t lstart = (loff_t)pgoff_start << PAGE_SHIFT; +            loff_t lend = ((loff_t)pgoff_end << PAGE_SHIFT) - 1;

It looks like we need to hold a reference to the file here before dropping the mmap lock :)

Quoted 5 lines

            file = get_file(vma->vm_file);

Without it, the vma could be destroyed by a concurrent munmap() while we are waiting in filemap_write_and_wait_range(), leading to a UAF on mapping, IIUC ...

Excellent catch!
Thanks for saving me from this nasty bug. I'll be more careful on file ref handling in next version.

Best Regards,
Shivank

Quoted 30 lines

mm/khugepaged.c int madvise_collapse(struct vm_area_struct *vma, unsigned long start, · line 2806

Quoted 6 lines
mm/khugepaged.c +5 -0
+
+            mmap_read_unlock(mm);
+            mmap_locked = false;
+
+            if (filemap_write_and_wait_range(mapping, lstart, lend)) {

And drop the reference :)

                fput(file);

Quoted 3 lines

+                last_fail = SCAN_FAIL;
+                goto out_maybelock;
+            }

Same here :)

            fput(file);

Quoted 6 lines

+        }
+    }
+
      for (addr = hstart; addr < hend; addr += HPAGE_PMD_SIZE) { int result = SCAN_FAIL;

Cheers,
Lance

#4 lore raw

Re: [PATCH V2 1/2] mm/khugepaged: do synchronous writeback for MADV_COLLAPSE

In reply to Shivank Garg
Quoted 44 lines · Shivank Garg

When MADV_COLLAPSE is called on file-backed mappings (e.g., executable text sections), the pages may still be dirty from recent writes and cause collapse to fail with -EINVAL. This is particularly problematic for freshly copied executables on filesystems, where page cache folios remain dirty until background writeback completes.

The current code in collapse_file() triggers async writeback via filemap_flush() and expects khugepaged to revisit the page later. However, MADV_COLLAPSE is a synchronous operation where userspace expects immediate results.

Perform synchronous writeback in madvise_collapse() before attempting collapse to avoid failing on first attempt.

Fixes 34488399fa08 ("mm/madvise: add file and shmem support to MADV_COLLAPSE")
---
  mm/khugepaged.c | 26 ++++++++++++++++++++++++++
  1 file changed, 26 insertions(+)
mm/khugepaged.c +6 -0
index 97d1b2824386..066a332c76ad 100644
@@ -22,6 +22,7 @@
  #include <linux/dax.h>
  #include <linux/ksm.h>
  #include <linux/pgalloc.h>
+#include <linux/backing-dev.h>
 
  #include <asm/tlb.h>
  #include "internal.h"
@@ -2784,6 +2785,31 @@ int madvise_collapse(struct vm_area_struct *vma, unsigned long start,
  	hstart = (start + ~HPAGE_PMD_MASK) & HPAGE_PMD_MASK;
  	hend = end & HPAGE_PMD_MASK;
 
+	/*
+	 * For file-backed VMAs, perform synchronous writeback to ensure
+	 * dirty folios are flushed before attempting collapse. This avoids
+	 * failing on the first attempt when freshly-written executable text
+	 * is still dirty in the page cache.

mm/khugepaged.c int madvise_collapse(struct vm_area_struct *vma, unsigned long start, · line 2802

Quoted 10 lines
mm/khugepaged.c +10 -0
+	 */
+	if (!vma_is_anonymous(vma) && vma->vm_file) {
+		struct address_space *mapping = vma->vm_file->f_mapping;
+
+		if (mapping_can_writeback(mapping)) {
+			pgoff_t pgoff_start = linear_page_index(vma, hstart);
+			pgoff_t pgoff_end = linear_page_index(vma, hend);
+			loff_t lstart = (loff_t)pgoff_start << PAGE_SHIFT;
+			loff_t lend = ((loff_t)pgoff_end << PAGE_SHIFT) - 1;
+

Hm, so we always do that, without any indication that there actually is something dirty there.

Internally filemap_write_and_wait_range() uses something called mapping_needs_writeback(), but it also applies to the complete file, not a range.

Wouldn't it be better do do that only if we detect that there is actually a dirty folio in the range?

That is, if we find any dirty folio in hpage_collapse_scan_file() and we are in madvise, do that dance here and retry?

Signature · 3 lines
Cheers

David
#5 lore raw

Re: [PATCH V2 1/2] mm/khugepaged: do synchronous writeback for MADV_COLLAPSE

Quoted 56 lines · David Hildenbrand (Red Hat)
Quoted 45 lines · Shivank Garg

When MADV_COLLAPSE is called on file-backed mappings (e.g., executable text sections), the pages may still be dirty from recent writes and cause collapse to fail with -EINVAL. This is particularly problematic for freshly copied executables on filesystems, where page cache folios remain dirty until background writeback completes.

The current code in collapse_file() triggers async writeback via filemap_flush() and expects khugepaged to revisit the page later. However, MADV_COLLAPSE is a synchronous operation where userspace expects immediate results.

Perform synchronous writeback in madvise_collapse() before attempting collapse to avoid failing on first attempt.

Fixes 34488399fa08 ("mm/madvise: add file and shmem support to MADV_COLLAPSE")

---
  mm/khugepaged.c | 26 ++++++++++++++++++++++++++

  1 file changed, 26 insertions(+)
mm/khugepaged.c +1 -0
index 97d1b2824386..066a332c76ad 100644
@@ -22,6 +22,7 @@
   #include <linux/dax.h>
   #include <linux/ksm.h>
   #include <linux/pgalloc.h>
+#include <linux/backing-dev.h>
     #include <asm/tlb.h>
   #include "internal.h"
@@ -2784,6 +2785,31 @@ int madvise_collapse(struct vm_area_struct *vma, unsigned long start,
       hstart = (start + ~HPAGE_PMD_MASK) & HPAGE_PMD_MASK;
       hend = end & HPAGE_PMD_MASK;

  +    /*
+     * For file-backed VMAs, perform synchronous writeback to ensure +     * dirty folios are flushed before attempting collapse. This avoids +     * failing on the first attempt when freshly-written executable text +     * is still dirty in the page cache.
+     */
+    if (!vma_is_anonymous(vma) && vma->vm_file) {
+        struct address_space *mapping = vma->vm_file->f_mapping;

mm/khugepaged.c int madvise_collapse(struct vm_area_struct *vma, unsigned long start, · line 2802

Quoted 7 lines
mm/khugepaged.c +7 -0
+
+        if (mapping_can_writeback(mapping)) {
+            pgoff_t pgoff_start = linear_page_index(vma, hstart);
+            pgoff_t pgoff_end = linear_page_index(vma, hend);
+            loff_t lstart = (loff_t)pgoff_start << PAGE_SHIFT;
+            loff_t lend = ((loff_t)pgoff_end << PAGE_SHIFT) - 1;
+

Hm, so we always do that, without any indication that there actually is something dirty there.

Quoted 6 lines

Internally filemap_write_and_wait_range() uses something called mapping_needs_writeback(), but it also applies to the complete file, not a range.

Wouldn't it be better do do that only if we detect that there is actually a dirty folio in the range?

That is, if we find any dirty folio in hpage_collapse_scan_file() and we are in madvise, do that dance here and retry?

Good point! This makes sense to me.
I'll send V3 with this approach.

Thanks,
Shivank

#6 lore raw

PATCH v2 2/2 mm/khugepaged: map dirty/writeback pages failures to EAGAIN

In reply to Shivank Garg

When collapse_file encounters dirty or writeback pages in file-backed mappings, it currently SCAN_FAIL which maps to -EINVAL. This is misleading as EINVAL suggests invalid arguments, whereas dirty/writeback pages represent transient conditions that may resolve on retry.

Introduce SCAN_PAGE_NOT_CLEAN to cover both dirty and writeback states, mapping it to -EAGAIN. For MADV_COLLAPSE, this provides userspace with a clear signal that retry may succeed after writeback completes, making -EAGAIN semantically correct. For khugepaged, this is harmless as it will naturally revisit the range during periodic scans after async writeback completes.

---
 include/trace/events/huge_memory.h | 3 ++-
 mm/khugepaged.c                    | 8 +++++---
 2 files changed, 7 insertions(+), 4 deletions(-)
include/trace/events/huge_memory.h +2 -1
index 4cde53b45a85..1caf24b951e1 100644
@@ -37,7 +37,8 @@
 	EM( SCAN_PAGE_HAS_PRIVATE,	"page_has_private")		\
 	EM( SCAN_STORE_FAILED,		"store_failed")			\
 	EM( SCAN_COPY_MC,		"copy_poisoned_page")		\
-	EMe(SCAN_PAGE_FILLED,		"page_filled")
+	EM( SCAN_PAGE_FILLED,		"page_filled")			\
+	EMe(SCAN_PAGE_NOT_CLEAN,	"page_not_clean")
 
 #undef EM
 #undef EMe
mm/khugepaged.c +5 -3
index 066a332c76ad..282b413d17e8 100644
@@ -59,6 +59,7 @@ enum scan_result {
 	SCAN_STORE_FAILED,
 	SCAN_COPY_MC,
 	SCAN_PAGE_FILLED,
+	SCAN_PAGE_NOT_CLEAN,
 };
 
 #define CREATE_TRACE_POINTS
@@ -1968,11 +1969,11 @@ static int collapse_file(struct mm_struct *mm, unsigned long addr,
 				 */
 				xas_unlock_irq(&xas);
 				filemap_flush(mapping);
-				result = SCAN_FAIL;
+				result = SCAN_PAGE_NOT_CLEAN;
 				goto xa_unlocked;
 			} else if (folio_test_writeback(folio)) {
 				xas_unlock_irq(&xas);
-				result = SCAN_FAIL;
+				result = SCAN_PAGE_NOT_CLEAN;
 				goto xa_unlocked;
 			} else if (folio_trylock(folio)) {
 				folio_get(folio);
@@ -2019,7 +2020,7 @@ static int collapse_file(struct mm_struct *mm, unsigned long addr,
 			 * folio is dirty because it hasn't been flushed
 			 * since first write.
 			 */
-			result = SCAN_FAIL;
+			result = SCAN_PAGE_NOT_CLEAN;
 			goto out_unlock;
 		}
 
@@ -2748,6 +2749,7 @@ static int madvise_collapse_errno(enum scan_result r)
 	case SCAN_PAGE_LRU:
 	case SCAN_DEL_PAGE_LRU:
 	case SCAN_PAGE_FILLED:
+	case SCAN_PAGE_NOT_CLEAN:
 		return -EAGAIN;
 	/*
 	 * Other: Trying again likely not to succeed / error intrinsic to
Signature · 1 line
2.43.0
reviewer Reviewed-by on v2 2/2
#7 lore raw

Re: [PATCH V2 2/2] mm/khugepaged: map dirty/writeback pages failures to EAGAIN

Quoted 68 lines · Shivank Garg

When collapse_file encounters dirty or writeback pages in file-backed mappings, it currently SCAN_FAIL which maps to -EINVAL. This is misleading as EINVAL suggests invalid arguments, whereas dirty/writeback pages represent transient conditions that may resolve on retry.

Introduce SCAN_PAGE_NOT_CLEAN to cover both dirty and writeback states, mapping it to -EAGAIN. For MADV_COLLAPSE, this provides userspace with a clear signal that retry may succeed after writeback completes, making -EAGAIN semantically correct. For khugepaged, this is harmless as it will naturally revisit the range during periodic scans after async writeback completes.

---
  include/trace/events/huge_memory.h | 3 ++-
  mm/khugepaged.c                    | 8 +++++---
  2 files changed, 7 insertions(+), 4 deletions(-)
include/trace/events/huge_memory.h +2 -1
index 4cde53b45a85..1caf24b951e1 100644
@@ -37,7 +37,8 @@
  	EM( SCAN_PAGE_HAS_PRIVATE,	"page_has_private")		\
  	EM( SCAN_STORE_FAILED,		"store_failed")			\
  	EM( SCAN_COPY_MC,		"copy_poisoned_page")		\
-	EMe(SCAN_PAGE_FILLED,		"page_filled")
+	EM( SCAN_PAGE_FILLED,		"page_filled")			\
+	EMe(SCAN_PAGE_NOT_CLEAN,	"page_not_clean")
 
  #undef EM
  #undef EMe
mm/khugepaged.c +4 -3
index 066a332c76ad..282b413d17e8 100644
@@ -59,6 +59,7 @@ enum scan_result {
  	SCAN_STORE_FAILED,
  	SCAN_COPY_MC,
  	SCAN_PAGE_FILLED,
+	SCAN_PAGE_NOT_CLEAN,
  };
 
  #define CREATE_TRACE_POINTS
@@ -1968,11 +1969,11 @@ static int collapse_file(struct mm_struct *mm, unsigned long addr,
  				 */
  				xas_unlock_irq(&xas);
  				filemap_flush(mapping);
-				result = SCAN_FAIL;
+				result = SCAN_PAGE_NOT_CLEAN;
  				goto xa_unlocked;
  			} else if (folio_test_writeback(folio)) {
  				xas_unlock_irq(&xas);
-				result = SCAN_FAIL;
+				result = SCAN_PAGE_NOT_CLEAN;
  				goto xa_unlocked;
  			} else if (folio_trylock(folio)) {
  				folio_get(folio);
@@ -2019,7 +2020,7 @@ static int collapse_file(struct mm_struct *mm, unsigned long addr,
  			 * folio is dirty because it hasn't been flushed
  			 * since first write.
  			 */
-			result = SCAN_FAIL;
+			result = SCAN_PAGE_NOT_CLEAN;
  			goto out_unlock;
  		}
 

mm/khugepaged.c static int madvise_collapse_errno(enum scan_result r) · line 2755

Quoted 7 lines
mm/khugepaged.c +1 -0
@@ -2748,6 +2749,7 @@ static int madvise_collapse_errno(enum scan_result r)
  	case SCAN_PAGE_LRU:
  	case SCAN_DEL_PAGE_LRU:
  	case SCAN_PAGE_FILLED:
+	case SCAN_PAGE_NOT_CLEAN:
  		return -EAGAIN;
  	/*
  	 * Other: Trying again likely not to succeed / error intrinsic to

SCAN_PAGE_NOT_CLEAN is confusing - NOT_CLEAN literally means dirty, so why not SCAN_PAGE_DIRTY? Or SCAN_PAGE_DIRTY_OR_UNDER_WRITEBACK? Since folio_test_writeback() is true as a result of the folio being dirty, maybe just SCAN_PAGE_DIRTY can do.

#8 lore raw

Re: [PATCH V2 2/2] mm/khugepaged: map dirty/writeback pages failures to EAGAIN

Quoted 9 lines · Dev Jain

On 20/11/25 12:20 pm, Shivank Garg wrote:

SCAN_PAGE_NOT_CLEAN is confusing - NOT_CLEAN literally means dirty, so why not SCAN_PAGE_DIRTY? Or SCAN_PAGE_DIRTY_OR_UNDER_WRITEBACK? Since folio_test_writeback() is true as a result of the folio being dirty, maybe just SCAN_PAGE_DIRTY can do.

Thanks for the review.

I chose not to use SCAN_PAGE_DIRTY because dirty and writeback have different meanings[1]:

Dirty: Memory that is waiting to be written back to disk
Writeback: Memory that is actively being written back to disk

[1] https://www.kernel.org/doc/Documentation/filesystems/proc.txt

IIUC, a page under writeback is no longer dirty, so using SCAN_PAGE_DIRTY would be misleading for pages in the writeback state.

I considered SCAN_PAGE_DIRTY_OR_WRITEBACK initially but felt it was too long.

SCAN_PAGE_NOT_CLEAN covers both states that indicate the page is not in a clean/stable state suitable for collapse.

[1] https://www.kernel.org/doc/Documentation/filesystems/proc.txt

Thanks,
Shivank

reviewer
#9 lore raw

Re: [PATCH V2 2/2] mm/khugepaged: map dirty/writeback pages failures to EAGAIN

Quoted 21 lines · Shivank
Quoted 7 lines · Dev Jain

On 20/11/25 12:20 pm, Shivank Garg wrote:
SCAN_PAGE_NOT_CLEAN is confusing - NOT_CLEAN literally means dirty, so why not SCAN_PAGE_DIRTY? Or SCAN_PAGE_DIRTY_OR_UNDER_WRITEBACK? Since folio_test_writeback() is true as a result of the folio being dirty, maybe just SCAN_PAGE_DIRTY can do.

Thanks for the review.

I chose not to use SCAN_PAGE_DIRTY because dirty and writeback have different meanings[1]:

Dirty: Memory that is waiting to be written back to disk
Writeback: Memory that is actively being written back to disk

[1] https://www.kernel.org/doc/Documentation/filesystems/proc.txt

IIUC, a page under writeback is no longer dirty, so using SCAN_PAGE_DIRTY would be misleading for pages in the writeback state.

Quoted 6 lines

I considered SCAN_PAGE_DIRTY_OR_WRITEBACK initially but felt it was too long.

SCAN_PAGE_NOT_CLEAN covers both states that indicate the page is not in a clean/stable state suitable for collapse.

[1] https://www.kernel.org/doc/Documentation/filesystems/proc.txt

Alright, makes sense.

Quoted 3 lines

Thanks,
Shivank

reviewer Reviewed-by
#10 lore raw

Re: [PATCH V2 2/2] mm/khugepaged: map dirty/writeback pages failures to EAGAIN

In reply to Garg, Shivank
Quoted 19 lines · Shivank
Quoted 9 lines · Dev Jain

On 20/11/25 12:20 pm, Shivank Garg wrote:

SCAN_PAGE_NOT_CLEAN is confusing - NOT_CLEAN literally means dirty, so why not SCAN_PAGE_DIRTY? Or SCAN_PAGE_DIRTY_OR_UNDER_WRITEBACK? Since folio_test_writeback() is true as a result of the folio being dirty, maybe just SCAN_PAGE_DIRTY can do.

Thanks for the review.

I chose not to use SCAN_PAGE_DIRTY because dirty and writeback have different meanings[1]:

Dirty: Memory that is waiting to be written back to disk
Writeback: Memory that is actively being written back to disk

Quoted 6 lines

[1] https://www.kernel.org/doc/Documentation/filesystems/proc.txt

IIUC, a page under writeback is no longer dirty, so using SCAN_PAGE_DIRTY would be misleading for pages in the writeback state.

I considered SCAN_PAGE_DIRTY_OR_WRITEBACK initially but felt it was too long.

Nit: If SCAN_PAGE_DIRTY_OR_WRITEBACK is too verbose, how about SCAN_PAGE_DIRTY_WB?

It keeps the specificity without the length, and is arguably more descriptive than NOT_CLEAN ;)

That said, LGTM.

Quoted 8 lines

SCAN_PAGE_NOT_CLEAN covers both states that indicate the page is not in a clean/stable state suitable for collapse.

[1] https://www.kernel.org/doc/Documentation/filesystems/proc.txt

Thanks,
Shivank

#11 lore raw

Re: [PATCH V2 2/2] mm/khugepaged: map dirty/writeback pages failures to EAGAIN

Quoted 22 lines · Lance Yang
Quoted 19 lines · Shivank
Quoted 9 lines · Dev Jain

On 20/11/25 12:20 pm, Shivank Garg wrote:

SCAN_PAGE_NOT_CLEAN is confusing - NOT_CLEAN literally means dirty, so why not SCAN_PAGE_DIRTY? Or SCAN_PAGE_DIRTY_OR_UNDER_WRITEBACK? Since folio_test_writeback() is true as a result of the folio being dirty, maybe just SCAN_PAGE_DIRTY can do.

Thanks for the review.

I chose not to use SCAN_PAGE_DIRTY because dirty and writeback have different meanings[1]:

Dirty: Memory that is waiting to be written back to disk
Writeback: Memory that is actively being written back to disk

Quoted 8 lines
Quoted 6 lines

[1] https://www.kernel.org/doc/Documentation/filesystems/proc.txt

IIUC, a page under writeback is no longer dirty, so using SCAN_PAGE_DIRTY would be misleading for pages in the writeback state.

I considered SCAN_PAGE_DIRTY_OR_WRITEBACK initially but felt it was too long.

Nit: If SCAN_PAGE_DIRTY_OR_WRITEBACK

I would prefer that here.

Signature · 3 lines
Cheers

David
#12 lore raw

Re: [PATCH V2 2/2] mm/khugepaged: map dirty/writeback pages failures to EAGAIN

Quoted 25 lines · David Hildenbrand (Red Hat)
Quoted 24 lines · Lance Yang
Quoted 21 lines · Shivank
Quoted 9 lines · Dev Jain

On 20/11/25 12:20 pm, Shivank Garg wrote:

SCAN_PAGE_NOT_CLEAN is confusing - NOT_CLEAN literally means dirty, so why not SCAN_PAGE_DIRTY? Or SCAN_PAGE_DIRTY_OR_UNDER_WRITEBACK? Since folio_test_writeback() is true as a result of the folio being dirty, maybe just SCAN_PAGE_DIRTY can do.

Thanks for the review.

I chose not to use SCAN_PAGE_DIRTY because dirty and writeback have different meanings[1]:

      Dirty: Memory that is waiting to be written back to disk
Writeback: Memory that is actively being written back to disk

[1] https://www.kernel.org/doc/Documentation/filesystems/proc.txt

Quoted 9 lines
Quoted 6 lines
Quoted 4 lines

IIUC, a page under writeback is no longer dirty, so using SCAN_PAGE_DIRTY would be misleading for pages in the writeback state.

I considered SCAN_PAGE_DIRTY_OR_WRITEBACK initially but felt it was too long.

Nit: If SCAN_PAGE_DIRTY_OR_WRITEBACK

I would prefer that here.

I agree on this.
If the consensus is SCAN_PAGE_DIRTY_OR_WRITEBACK, I'll use it in v3.

Thanks,
Shivank