/* watch out for wraparound */
        start_vaddr = end_vaddr;
        if (svpfn <= (ULONG_MAX >> PAGE_SHIFT)) {
+               unsigned long end;
+
                ret = mmap_read_lock_killable(mm);
                if (ret)
                        goto out_free;
                start_vaddr = untagged_addr_remote(mm, svpfn << PAGE_SHIFT);
                mmap_read_unlock(mm);
+
+               end = start_vaddr + ((count / PM_ENTRY_BYTES) << PAGE_SHIFT);
+               if (end >= start_vaddr && end < mm->task_size)
+                       end_vaddr = end;
        }
 
        /* Ensure the address is inside the task */
        if (start_vaddr > mm->task_size)
                start_vaddr = end_vaddr;
 
-       /*
-        * The odds are that this will stop walking way
-        * before end_vaddr, because the length of the
-        * user buffer is tracked in "pm", and the walk
-        * will stop when we hit the end of the buffer.
-        */
        ret = 0;
        while (count && (start_vaddr < end_vaddr)) {
                int len;