From 54b12189a696dcb2ee3d12f6e7943387b74aa36e Mon Sep 17 00:00:00 2001 From: Dylan Reid Date: Tue, 25 Aug 2026 14:37:42 -0700 Subject: [PATCH] Fix negative UFFDIO_COPY result handling The kernel can return EAGAIN with uffdio_copy.copy set to -EAGAIN when a non-cooperative memory-map change races with UFFDIO_COPY. Casting that value to usize incorrectly reports a huge partial copy. Only construct PartiallyCopied when copy is positive. Return CopyFailed for zero or negative results. Kernel source: https://github.com/torvalds/linux/blob/95567729173e62e0e60a1f8ad9eb2e1320a8ccac/fs/userfaultfd.c#L1587-L1591 Signed-off-by: Dylan Reid --- src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index 3ce9bb5..467bcd7 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -163,7 +163,7 @@ impl Uffd { let _ = raw::copy(self.as_raw_fd(), &mut copy as *mut raw::uffdio_copy).map_err(|errno| { match errno { - Errno::EAGAIN => Error::PartiallyCopied(copy.copy as usize), + Errno::EAGAIN if copy.copy > 0 => Error::PartiallyCopied(copy.copy as usize), _ => Error::CopyFailed(errno), } })?;