Skip to contentSkip to conversation

userfaultfd: reset err when move_pages_ptes succeeded

PATCH New Foxie Flakey 1 participant 1 message Aug 15

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 userfaultfd: reset err when move_pages_ptes succeeded

When UFFDIO_MOVE moves non huge pages and temporarily fails, then the outer loop retries again and succeeded but in that path "err" isn't reset to 0. Later, when checked for EAGAIN error, the loop mistakenly retries moving same page again which will fail because its already moved.

When the UFFDIO_MOVE returned, this is what userspace observed.

Source:      [ .. unmapped  .. ][ .. mapped    ..]
Destination: [ .. mapped    .. ][ .. unmapped  ..]

^ ^
\ Kernel moved this far in actuality
What is reported to userspace on struct uffdio_move's move field

When the previous behaviour is

Source:      [ .. unmapped  .. ][ .. mapped    ..]
Destination: [ .. mapped    .. ][ .. unmapped  ..]

^
Reported to user space via uffdio_move's move field

Fixes 50944692052b ("userfaultfd: opportunistic TLB-flush batching for present pages in MOVE")
Assisted-by ChatGPT:GPT-5.6-Luna
---
 mm/userfaultfd.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
mm/userfaultfd.c +4 -2
index c3adedaaf7d5..595e7e232f90 100644
@@ -2069,10 +2069,12 @@ static ssize_t move_pages(struct userfaultfd_ctx *ctx, unsigned long dst_start,
 			ret = move_pages_ptes(mm, dst_pmd, src_pmd,
 					      dst_vma, src_vma, dst_addr,
 					      src_addr, src_end - src_addr, mode);
-			if (ret < 0)
+			if (ret < 0) {
 				err = ret;
-			else
+			} else {
+				err = 0;
 				step_size = ret;
+			}
 		}
 
 		cond_resched();

base-commit: 62cc90241548d5570ee68e01aaba6506964e9811

Signature · 1 line
2.55.0