[8/8] lazy-pages: don't try to uffd_copy to removed memory regions

Submitted by Mike Rapoport on April 16, 2018, 4:59 p.m.

Details

Message ID 1523897944-30193-9-git-send-email-rppt@linux.vnet.ibm.com
State Accepted
Series "lazy-pages: fixes"
Commit 2bedf8d995bd934d1f0c5bc1f8d7f96029fac51b
Headers show

Commit Message

Mike Rapoport April 16, 2018, 4:59 p.m.
It is possible that when pages request from the remove source arrive, part
of the memory range covered by the request would be already gone because of
madvise(MADV_DONTNEED), mremap() etc.
Ensure we are not trying to uffd_copy more than we are allowed.

Signed-off-by: Mike Rapoport <rppt@linux.vnet.ibm.com>
---
 criu/uffd.c | 9 +++++++++
 1 file changed, 9 insertions(+)

Patch hide | download patch | download mbox

diff --git a/criu/uffd.c b/criu/uffd.c
index 4022fc5..7f6a59c 100644
--- a/criu/uffd.c
+++ b/criu/uffd.c
@@ -839,6 +839,7 @@  static int uffd_io_complete(struct page_read *pr, unsigned long img_addr, int nr
 {
 	struct lazy_pages_info *lpi;
 	unsigned long addr = 0;
+	int req_pages;
 	struct lazy_iov *req;
 
 	lpi = container_of(pr, struct lazy_pages_info, pr);
@@ -864,6 +865,14 @@  static int uffd_io_complete(struct page_read *pr, unsigned long img_addr, int nr
 	if (!addr)
 		return 0;
 
+	/*
+	 * by the time we get the pages from the remote source, parts
+	 * of the request may already be gone because of
+	 * UFFD_EVENT_{REMAP,REMOVE,UNMAP}
+	 */
+	req_pages = (req->end - req->start) / PAGE_SIZE;
+	nr = min(nr, req_pages);
+
 	return uffd_copy(lpi, addr, nr);
 }