From 480402e18def5514c9dc8cb04e3c0e7482ff2b86 Mon Sep 17 00:00:00 2001 From: Al Viro Date: Wed, 5 Feb 2014 10:14:01 -0500 Subject: untangling process_vm_..., part 1 we want to massage it to use of iov_iter. This one is an equivalent transformation - just introduce a local variable mirroring lvec + *lvec_current. Signed-off-by: Al Viro --- mm/process_vm_access.c | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) (limited to 'mm/process_vm_access.c') diff --git a/mm/process_vm_access.c b/mm/process_vm_access.c index fd26d0433509..7b8d63e6c30b 100644 --- a/mm/process_vm_access.c +++ b/mm/process_vm_access.c @@ -60,6 +60,7 @@ static int process_vm_rw_pages(struct task_struct *task, int ret; ssize_t bytes_to_copy; ssize_t rc = 0; + const struct iovec *iov = lvec + *lvec_current; *bytes_copied = 0; @@ -81,8 +82,10 @@ static int process_vm_rw_pages(struct task_struct *task, pgs_copied++) { /* Make sure we have a non zero length iovec */ while (*lvec_current < lvec_cnt - && lvec[*lvec_current].iov_len == 0) + && iov->iov_len == 0) { + iov++; (*lvec_current)++; + } if (*lvec_current == lvec_cnt) break; @@ -94,18 +97,18 @@ static int process_vm_rw_pages(struct task_struct *task, bytes_to_copy = min_t(ssize_t, PAGE_SIZE - start_offset, len - *bytes_copied); bytes_to_copy = min_t(ssize_t, bytes_to_copy, - lvec[*lvec_current].iov_len + iov->iov_len - *lvec_offset); target_kaddr = kmap(process_pages[pgs_copied]) + start_offset; if (vm_write) ret = copy_from_user(target_kaddr, - lvec[*lvec_current].iov_base + iov->iov_base + *lvec_offset, bytes_to_copy); else - ret = copy_to_user(lvec[*lvec_current].iov_base + ret = copy_to_user(iov->iov_base + *lvec_offset, target_kaddr, bytes_to_copy); kunmap(process_pages[pgs_copied]); @@ -117,12 +120,13 @@ static int process_vm_rw_pages(struct task_struct *task, } *bytes_copied += bytes_to_copy; *lvec_offset += bytes_to_copy; - if (*lvec_offset == lvec[*lvec_current].iov_len) { + if (*lvec_offset == iov->iov_len) { /* * Need to copy remaining part of page into the * next iovec if there are any bytes left in page */ (*lvec_current)++; + iov++; *lvec_offset = 0; start_offset = (start_offset + bytes_to_copy) % PAGE_SIZE; -- cgit v1.2.3 From c61c70384fad407bfcb066c3fb9271164d630212 Mon Sep 17 00:00:00 2001 From: Al Viro Date: Wed, 5 Feb 2014 10:22:50 -0500 Subject: untangling process_vm_..., part 2 move iov to caller's stack frame; the value we assign to it on the next call of process_vm_rw_pages() is equal to the value it had when the last time we were leaving process_vm_rw_pages(). drop lvec argument of process_vm_rw_pages() - it's not used anymore. Signed-off-by: Al Viro --- mm/process_vm_access.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) (limited to 'mm/process_vm_access.c') diff --git a/mm/process_vm_access.c b/mm/process_vm_access.c index 7b8d63e6c30b..186ec5db6090 100644 --- a/mm/process_vm_access.c +++ b/mm/process_vm_access.c @@ -45,7 +45,7 @@ static int process_vm_rw_pages(struct task_struct *task, unsigned long pa, unsigned long start_offset, unsigned long len, - const struct iovec *lvec, + const struct iovec **iovp, unsigned long lvec_cnt, unsigned long *lvec_current, size_t *lvec_offset, @@ -60,7 +60,7 @@ static int process_vm_rw_pages(struct task_struct *task, int ret; ssize_t bytes_to_copy; ssize_t rc = 0; - const struct iovec *iov = lvec + *lvec_current; + const struct iovec *iov = *iovp; *bytes_copied = 0; @@ -149,6 +149,7 @@ end: put_page(process_pages[j]); } + *iovp = iov; return rc; } @@ -192,6 +193,7 @@ static int process_vm_rw_single_vec(unsigned long addr, unsigned long nr_pages_to_copy; unsigned long max_pages_per_loop = PVM_MAX_KMALLOC_PAGES / sizeof(struct pages *); + const struct iovec *iov = lvec + *lvec_current; *bytes_copied = 0; @@ -206,7 +208,7 @@ static int process_vm_rw_single_vec(unsigned long addr, rc = process_vm_rw_pages(task, mm, process_pages, pa, start_offset, len, - lvec, lvec_cnt, + &iov, lvec_cnt, lvec_current, lvec_offset, vm_write, nr_pages_to_copy, &bytes_copied_loop); -- cgit v1.2.3 From 12e3004e464f554e0d57ddd0b4185060275e1f12 Mon Sep 17 00:00:00 2001 From: Al Viro Date: Wed, 5 Feb 2014 10:28:58 -0500 Subject: untangling process_vm_..., part 3 lift iov one more level out - from process_vm_rw_single_vec to process_vm_rw_core(). Same story as with the previous commit. Signed-off-by: Al Viro --- mm/process_vm_access.c | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) (limited to 'mm/process_vm_access.c') diff --git a/mm/process_vm_access.c b/mm/process_vm_access.c index 186ec5db6090..40dfb396e8ab 100644 --- a/mm/process_vm_access.c +++ b/mm/process_vm_access.c @@ -174,7 +174,7 @@ end: */ static int process_vm_rw_single_vec(unsigned long addr, unsigned long len, - const struct iovec *lvec, + const struct iovec **iovp, unsigned long lvec_cnt, unsigned long *lvec_current, size_t *lvec_offset, @@ -193,7 +193,6 @@ static int process_vm_rw_single_vec(unsigned long addr, unsigned long nr_pages_to_copy; unsigned long max_pages_per_loop = PVM_MAX_KMALLOC_PAGES / sizeof(struct pages *); - const struct iovec *iov = lvec + *lvec_current; *bytes_copied = 0; @@ -208,7 +207,7 @@ static int process_vm_rw_single_vec(unsigned long addr, rc = process_vm_rw_pages(task, mm, process_pages, pa, start_offset, len, - &iov, lvec_cnt, + iovp, lvec_cnt, lvec_current, lvec_offset, vm_write, nr_pages_to_copy, &bytes_copied_loop); @@ -319,7 +318,7 @@ static ssize_t process_vm_rw_core(pid_t pid, const struct iovec *lvec, for (i = 0; i < riovcnt && iov_l_curr_idx < liovcnt; i++) { rc = process_vm_rw_single_vec( (unsigned long)rvec[i].iov_base, rvec[i].iov_len, - lvec, liovcnt, &iov_l_curr_idx, &iov_l_curr_offset, + &lvec, liovcnt, &iov_l_curr_idx, &iov_l_curr_offset, process_pages, mm, task, vm_write, &bytes_copied_loop); bytes_copied += bytes_copied_loop; if (rc != 0) { -- cgit v1.2.3 From 1291afc18115885cf3ff265a5dc836393060e820 Mon Sep 17 00:00:00 2001 From: Al Viro Date: Wed, 5 Feb 2014 10:40:15 -0500 Subject: untangling process_vm_..., part 4 instead of passing vector size (by value) and index (by reference), pass the number of elements remaining. That's all we care about in these functions by that point. Signed-off-by: Al Viro --- mm/process_vm_access.c | 29 +++++++++++++---------------- 1 file changed, 13 insertions(+), 16 deletions(-) (limited to 'mm/process_vm_access.c') diff --git a/mm/process_vm_access.c b/mm/process_vm_access.c index 40dfb396e8ab..4bbd495fcedd 100644 --- a/mm/process_vm_access.c +++ b/mm/process_vm_access.c @@ -46,8 +46,7 @@ static int process_vm_rw_pages(struct task_struct *task, unsigned long start_offset, unsigned long len, const struct iovec **iovp, - unsigned long lvec_cnt, - unsigned long *lvec_current, + unsigned long *left, size_t *lvec_offset, int vm_write, unsigned int nr_pages_to_copy, @@ -78,15 +77,14 @@ static int process_vm_rw_pages(struct task_struct *task, /* Do the copy for each page */ for (pgs_copied = 0; - (pgs_copied < nr_pages_to_copy) && (*lvec_current < lvec_cnt); + (pgs_copied < nr_pages_to_copy) && *left; pgs_copied++) { /* Make sure we have a non zero length iovec */ - while (*lvec_current < lvec_cnt - && iov->iov_len == 0) { + while (*left && iov->iov_len == 0) { iov++; - (*lvec_current)++; + (*left)--; } - if (*lvec_current == lvec_cnt) + if (!*left) break; /* @@ -125,7 +123,7 @@ static int process_vm_rw_pages(struct task_struct *task, * Need to copy remaining part of page into the * next iovec if there are any bytes left in page */ - (*lvec_current)++; + (*left)--; iov++; *lvec_offset = 0; start_offset = (start_offset + bytes_to_copy) @@ -175,8 +173,7 @@ end: static int process_vm_rw_single_vec(unsigned long addr, unsigned long len, const struct iovec **iovp, - unsigned long lvec_cnt, - unsigned long *lvec_current, + unsigned long *left, size_t *lvec_offset, struct page **process_pages, struct mm_struct *mm, @@ -201,14 +198,14 @@ static int process_vm_rw_single_vec(unsigned long addr, return 0; nr_pages = (addr + len - 1) / PAGE_SIZE - addr / PAGE_SIZE + 1; - while ((nr_pages_copied < nr_pages) && (*lvec_current < lvec_cnt)) { + while ((nr_pages_copied < nr_pages) && *left) { nr_pages_to_copy = min(nr_pages - nr_pages_copied, max_pages_per_loop); rc = process_vm_rw_pages(task, mm, process_pages, pa, start_offset, len, - iovp, lvec_cnt, - lvec_current, lvec_offset, + iovp, left, + lvec_offset, vm_write, nr_pages_to_copy, &bytes_copied_loop); start_offset = 0; @@ -259,7 +256,7 @@ static ssize_t process_vm_rw_core(pid_t pid, const struct iovec *lvec, ssize_t bytes_copied = 0; unsigned long nr_pages = 0; unsigned long nr_pages_iov; - unsigned long iov_l_curr_idx = 0; + unsigned long left = liovcnt; size_t iov_l_curr_offset = 0; ssize_t iov_len; @@ -315,10 +312,10 @@ static ssize_t process_vm_rw_core(pid_t pid, const struct iovec *lvec, goto put_task_struct; } - for (i = 0; i < riovcnt && iov_l_curr_idx < liovcnt; i++) { + for (i = 0; i < riovcnt && left; i++) { rc = process_vm_rw_single_vec( (unsigned long)rvec[i].iov_base, rvec[i].iov_len, - &lvec, liovcnt, &iov_l_curr_idx, &iov_l_curr_offset, + &lvec, &left, &iov_l_curr_offset, process_pages, mm, task, vm_write, &bytes_copied_loop); bytes_copied += bytes_copied_loop; if (rc != 0) { -- cgit v1.2.3 From 9f78bdfabf0de0bc8d3cc97d06908651b577a567 Mon Sep 17 00:00:00 2001 From: Al Viro Date: Wed, 5 Feb 2014 11:51:53 -0500 Subject: process_vm_access: switch to iov_iter instead of keeping its pieces in separate variables and passing pointers to all of them... Signed-off-by: Al Viro --- mm/process_vm_access.c | 62 +++++++++++++++++++++++--------------------------- 1 file changed, 28 insertions(+), 34 deletions(-) (limited to 'mm/process_vm_access.c') diff --git a/mm/process_vm_access.c b/mm/process_vm_access.c index 4bbd495fcedd..1e3c81103b2c 100644 --- a/mm/process_vm_access.c +++ b/mm/process_vm_access.c @@ -45,9 +45,7 @@ static int process_vm_rw_pages(struct task_struct *task, unsigned long pa, unsigned long start_offset, unsigned long len, - const struct iovec **iovp, - unsigned long *left, - size_t *lvec_offset, + struct iov_iter *iter, int vm_write, unsigned int nr_pages_to_copy, ssize_t *bytes_copied) @@ -59,7 +57,7 @@ static int process_vm_rw_pages(struct task_struct *task, int ret; ssize_t bytes_to_copy; ssize_t rc = 0; - const struct iovec *iov = *iovp; + const struct iovec *iov = iter->iov; *bytes_copied = 0; @@ -77,14 +75,14 @@ static int process_vm_rw_pages(struct task_struct *task, /* Do the copy for each page */ for (pgs_copied = 0; - (pgs_copied < nr_pages_to_copy) && *left; + (pgs_copied < nr_pages_to_copy) && iter->nr_segs; pgs_copied++) { /* Make sure we have a non zero length iovec */ - while (*left && iov->iov_len == 0) { + while (iter->nr_segs && iov->iov_len == 0) { iov++; - (*left)--; + iter->nr_segs--; } - if (!*left) + if (!iter->nr_segs) break; /* @@ -96,18 +94,18 @@ static int process_vm_rw_pages(struct task_struct *task, len - *bytes_copied); bytes_to_copy = min_t(ssize_t, bytes_to_copy, iov->iov_len - - *lvec_offset); + - iter->iov_offset); target_kaddr = kmap(process_pages[pgs_copied]) + start_offset; if (vm_write) ret = copy_from_user(target_kaddr, iov->iov_base - + *lvec_offset, + + iter->iov_offset, bytes_to_copy); else ret = copy_to_user(iov->iov_base - + *lvec_offset, + + iter->iov_offset, target_kaddr, bytes_to_copy); kunmap(process_pages[pgs_copied]); if (ret) { @@ -117,15 +115,15 @@ static int process_vm_rw_pages(struct task_struct *task, goto end; } *bytes_copied += bytes_to_copy; - *lvec_offset += bytes_to_copy; - if (*lvec_offset == iov->iov_len) { + iter->iov_offset += bytes_to_copy; + if (iter->iov_offset == iov->iov_len) { /* * Need to copy remaining part of page into the * next iovec if there are any bytes left in page */ - (*left)--; + iter->nr_segs--; iov++; - *lvec_offset = 0; + iter->iov_offset = 0; start_offset = (start_offset + bytes_to_copy) % PAGE_SIZE; if (start_offset) @@ -147,7 +145,7 @@ end: put_page(process_pages[j]); } - *iovp = iov; + iter->iov = iov; return rc; } @@ -172,9 +170,7 @@ end: */ static int process_vm_rw_single_vec(unsigned long addr, unsigned long len, - const struct iovec **iovp, - unsigned long *left, - size_t *lvec_offset, + struct iov_iter *iter, struct page **process_pages, struct mm_struct *mm, struct task_struct *task, @@ -198,14 +194,12 @@ static int process_vm_rw_single_vec(unsigned long addr, return 0; nr_pages = (addr + len - 1) / PAGE_SIZE - addr / PAGE_SIZE + 1; - while ((nr_pages_copied < nr_pages) && *left) { + while ((nr_pages_copied < nr_pages) && iter->nr_segs) { nr_pages_to_copy = min(nr_pages - nr_pages_copied, max_pages_per_loop); rc = process_vm_rw_pages(task, mm, process_pages, pa, - start_offset, len, - iovp, left, - lvec_offset, + start_offset, len, iter, vm_write, nr_pages_to_copy, &bytes_copied_loop); start_offset = 0; @@ -240,8 +234,7 @@ static int process_vm_rw_single_vec(unsigned long addr, * return less bytes than expected if an error occurs during the copying * process. */ -static ssize_t process_vm_rw_core(pid_t pid, const struct iovec *lvec, - unsigned long liovcnt, +static ssize_t process_vm_rw_core(pid_t pid, struct iov_iter *iter, const struct iovec *rvec, unsigned long riovcnt, unsigned long flags, int vm_write) @@ -256,8 +249,6 @@ static ssize_t process_vm_rw_core(pid_t pid, const struct iovec *lvec, ssize_t bytes_copied = 0; unsigned long nr_pages = 0; unsigned long nr_pages_iov; - unsigned long left = liovcnt; - size_t iov_l_curr_offset = 0; ssize_t iov_len; /* @@ -312,11 +303,11 @@ static ssize_t process_vm_rw_core(pid_t pid, const struct iovec *lvec, goto put_task_struct; } - for (i = 0; i < riovcnt && left; i++) { + for (i = 0; i < riovcnt && iter->nr_segs; i++) { rc = process_vm_rw_single_vec( (unsigned long)rvec[i].iov_base, rvec[i].iov_len, - &lvec, &left, &iov_l_curr_offset, - process_pages, mm, task, vm_write, &bytes_copied_loop); + iter, process_pages, mm, task, vm_write, + &bytes_copied_loop); bytes_copied += bytes_copied_loop; if (rc != 0) { /* If we have managed to copy any data at all then @@ -365,6 +356,7 @@ static ssize_t process_vm_rw(pid_t pid, struct iovec iovstack_r[UIO_FASTIOV]; struct iovec *iov_l = iovstack_l; struct iovec *iov_r = iovstack_r; + struct iov_iter iter; ssize_t rc; if (flags != 0) @@ -380,13 +372,14 @@ static ssize_t process_vm_rw(pid_t pid, if (rc <= 0) goto free_iovecs; + iov_iter_init(&iter, iov_l, liovcnt, rc, 0); + rc = rw_copy_check_uvector(CHECK_IOVEC_ONLY, rvec, riovcnt, UIO_FASTIOV, iovstack_r, &iov_r); if (rc <= 0) goto free_iovecs; - rc = process_vm_rw_core(pid, iov_l, liovcnt, iov_r, riovcnt, flags, - vm_write); + rc = process_vm_rw_core(pid, &iter, iov_r, riovcnt, flags, vm_write); free_iovecs: if (iov_r != iovstack_r) @@ -426,6 +419,7 @@ compat_process_vm_rw(compat_pid_t pid, struct iovec iovstack_r[UIO_FASTIOV]; struct iovec *iov_l = iovstack_l; struct iovec *iov_r = iovstack_r; + struct iov_iter iter; ssize_t rc = -EFAULT; if (flags != 0) @@ -441,14 +435,14 @@ compat_process_vm_rw(compat_pid_t pid, &iov_l); if (rc <= 0) goto free_iovecs; + iov_iter_init(&iter, iov_l, liovcnt, rc, 0); rc = compat_rw_copy_check_uvector(CHECK_IOVEC_ONLY, rvec, riovcnt, UIO_FASTIOV, iovstack_r, &iov_r); if (rc <= 0) goto free_iovecs; - rc = process_vm_rw_core(pid, iov_l, liovcnt, iov_r, riovcnt, flags, - vm_write); + rc = process_vm_rw_core(pid, &iter, iov_r, riovcnt, flags, vm_write); free_iovecs: if (iov_r != iovstack_r) -- cgit v1.2.3 From 240f3905f513cd5f27cc3f79bf03bf3f88323f41 Mon Sep 17 00:00:00 2001 From: Al Viro Date: Wed, 5 Feb 2014 12:14:11 -0500 Subject: process_vm_access: switch to copy_page_to_iter/iov_iter_copy_from_user ... rather than open-coding those. As a side benefit, we get much saner loop calling those; we can just feed entire pages, instead of the "copy would span the iovec boundary, let's do it in two loop iterations" mess. Signed-off-by: Al Viro --- mm/process_vm_access.c | 91 +++++++++++++------------------------------------- 1 file changed, 23 insertions(+), 68 deletions(-) (limited to 'mm/process_vm_access.c') diff --git a/mm/process_vm_access.c b/mm/process_vm_access.c index 1e3c81103b2c..70ec3156f45a 100644 --- a/mm/process_vm_access.c +++ b/mm/process_vm_access.c @@ -51,13 +51,11 @@ static int process_vm_rw_pages(struct task_struct *task, ssize_t *bytes_copied) { int pages_pinned; - void *target_kaddr; int pgs_copied = 0; int j; int ret; ssize_t bytes_to_copy; ssize_t rc = 0; - const struct iovec *iov = iter->iov; *bytes_copied = 0; @@ -75,77 +73,34 @@ static int process_vm_rw_pages(struct task_struct *task, /* Do the copy for each page */ for (pgs_copied = 0; - (pgs_copied < nr_pages_to_copy) && iter->nr_segs; + (pgs_copied < nr_pages_to_copy) && iov_iter_count(iter); pgs_copied++) { - /* Make sure we have a non zero length iovec */ - while (iter->nr_segs && iov->iov_len == 0) { - iov++; - iter->nr_segs--; + struct page *page = process_pages[pgs_copied]; + bytes_to_copy = min_t(ssize_t, PAGE_SIZE - start_offset, len); + + if (vm_write) { + if (bytes_to_copy > iov_iter_count(iter)) + bytes_to_copy = iov_iter_count(iter); + ret = iov_iter_copy_from_user(page, + iter, start_offset, bytes_to_copy); + iov_iter_advance(iter, ret); + set_page_dirty_lock(page); + } else { + ret = copy_page_to_iter(page, start_offset, + bytes_to_copy, iter); } - if (!iter->nr_segs) - break; - - /* - * Will copy smallest of: - * - bytes remaining in page - * - bytes remaining in destination iovec - */ - bytes_to_copy = min_t(ssize_t, PAGE_SIZE - start_offset, - len - *bytes_copied); - bytes_to_copy = min_t(ssize_t, bytes_to_copy, - iov->iov_len - - iter->iov_offset); - - target_kaddr = kmap(process_pages[pgs_copied]) + start_offset; - - if (vm_write) - ret = copy_from_user(target_kaddr, - iov->iov_base - + iter->iov_offset, - bytes_to_copy); - else - ret = copy_to_user(iov->iov_base - + iter->iov_offset, - target_kaddr, bytes_to_copy); - kunmap(process_pages[pgs_copied]); - if (ret) { - *bytes_copied += bytes_to_copy - ret; - pgs_copied++; + *bytes_copied += ret; + len -= ret; + if (ret < bytes_to_copy && iov_iter_count(iter)) { rc = -EFAULT; - goto end; - } - *bytes_copied += bytes_to_copy; - iter->iov_offset += bytes_to_copy; - if (iter->iov_offset == iov->iov_len) { - /* - * Need to copy remaining part of page into the - * next iovec if there are any bytes left in page - */ - iter->nr_segs--; - iov++; - iter->iov_offset = 0; - start_offset = (start_offset + bytes_to_copy) - % PAGE_SIZE; - if (start_offset) - pgs_copied--; - } else { - start_offset = 0; + break; } + start_offset = 0; } end: - if (vm_write) { - for (j = 0; j < pages_pinned; j++) { - if (j < pgs_copied) - set_page_dirty_lock(process_pages[j]); - put_page(process_pages[j]); - } - } else { - for (j = 0; j < pages_pinned; j++) - put_page(process_pages[j]); - } - - iter->iov = iov; + for (j = 0; j < pages_pinned; j++) + put_page(process_pages[j]); return rc; } @@ -194,7 +149,7 @@ static int process_vm_rw_single_vec(unsigned long addr, return 0; nr_pages = (addr + len - 1) / PAGE_SIZE - addr / PAGE_SIZE + 1; - while ((nr_pages_copied < nr_pages) && iter->nr_segs) { + while ((nr_pages_copied < nr_pages) && iov_iter_count(iter)) { nr_pages_to_copy = min(nr_pages - nr_pages_copied, max_pages_per_loop); @@ -303,7 +258,7 @@ static ssize_t process_vm_rw_core(pid_t pid, struct iov_iter *iter, goto put_task_struct; } - for (i = 0; i < riovcnt && iter->nr_segs; i++) { + for (i = 0; i < riovcnt && iov_iter_count(iter); i++) { rc = process_vm_rw_single_vec( (unsigned long)rvec[i].iov_base, rvec[i].iov_len, iter, process_pages, mm, task, vm_write, -- cgit v1.2.3 From 70eca12d80a8b51800b4ebfbc629e0ee34166779 Mon Sep 17 00:00:00 2001 From: Al Viro Date: Wed, 5 Feb 2014 12:44:24 -0500 Subject: process_vm_access: take get_user_pages/put_pages one level up ... and trim the fuck out of process_vm_rw_pages() argument list. Signed-off-by: Al Viro --- mm/process_vm_access.c | 97 ++++++++++++++++++++------------------------------ 1 file changed, 39 insertions(+), 58 deletions(-) (limited to 'mm/process_vm_access.c') diff --git a/mm/process_vm_access.c b/mm/process_vm_access.c index 70ec3156f45a..c2a1916bacbf 100644 --- a/mm/process_vm_access.c +++ b/mm/process_vm_access.c @@ -39,69 +39,39 @@ * @bytes_copied: returns number of bytes successfully copied * Returns 0 on success, error code otherwise */ -static int process_vm_rw_pages(struct task_struct *task, - struct mm_struct *mm, - struct page **process_pages, - unsigned long pa, - unsigned long start_offset, +static int process_vm_rw_pages(struct page **pages, + unsigned offset, unsigned long len, struct iov_iter *iter, int vm_write, unsigned int nr_pages_to_copy, ssize_t *bytes_copied) { - int pages_pinned; - int pgs_copied = 0; - int j; - int ret; - ssize_t bytes_to_copy; - ssize_t rc = 0; - *bytes_copied = 0; - /* Get the pages we're interested in */ - down_read(&mm->mmap_sem); - pages_pinned = get_user_pages(task, mm, pa, - nr_pages_to_copy, - vm_write, 0, process_pages, NULL); - up_read(&mm->mmap_sem); - - if (pages_pinned != nr_pages_to_copy) { - rc = -EFAULT; - goto end; - } - /* Do the copy for each page */ - for (pgs_copied = 0; - (pgs_copied < nr_pages_to_copy) && iov_iter_count(iter); - pgs_copied++) { - struct page *page = process_pages[pgs_copied]; - bytes_to_copy = min_t(ssize_t, PAGE_SIZE - start_offset, len); + while (iov_iter_count(iter) && nr_pages_to_copy--) { + struct page *page = *pages++; + size_t copy = min_t(ssize_t, PAGE_SIZE - offset, len); + size_t copied; if (vm_write) { - if (bytes_to_copy > iov_iter_count(iter)) - bytes_to_copy = iov_iter_count(iter); - ret = iov_iter_copy_from_user(page, - iter, start_offset, bytes_to_copy); - iov_iter_advance(iter, ret); + if (copy > iov_iter_count(iter)) + copy = iov_iter_count(iter); + copied = iov_iter_copy_from_user(page, iter, + offset, copy); + iov_iter_advance(iter, copied); set_page_dirty_lock(page); } else { - ret = copy_page_to_iter(page, start_offset, - bytes_to_copy, iter); - } - *bytes_copied += ret; - len -= ret; - if (ret < bytes_to_copy && iov_iter_count(iter)) { - rc = -EFAULT; - break; + copied = copy_page_to_iter(page, offset, copy, iter); } - start_offset = 0; + *bytes_copied += copied; + len -= copied; + if (copied < copy && iov_iter_count(iter)) + return -EFAULT; + offset = 0; } - -end: - for (j = 0; j < pages_pinned; j++) - put_page(process_pages[j]); - return rc; + return 0; } /* Maximum number of pages kmalloc'd to hold struct page's during copy */ @@ -138,7 +108,6 @@ static int process_vm_rw_single_vec(unsigned long addr, ssize_t bytes_copied_loop; ssize_t rc = 0; unsigned long nr_pages_copied = 0; - unsigned long nr_pages_to_copy; unsigned long max_pages_per_loop = PVM_MAX_KMALLOC_PAGES / sizeof(struct pages *); @@ -150,23 +119,35 @@ static int process_vm_rw_single_vec(unsigned long addr, nr_pages = (addr + len - 1) / PAGE_SIZE - addr / PAGE_SIZE + 1; while ((nr_pages_copied < nr_pages) && iov_iter_count(iter)) { + int nr_pages_to_copy; + int pages_pinned; nr_pages_to_copy = min(nr_pages - nr_pages_copied, max_pages_per_loop); - rc = process_vm_rw_pages(task, mm, process_pages, pa, + /* Get the pages we're interested in */ + down_read(&mm->mmap_sem); + pages_pinned = get_user_pages(task, mm, pa, + nr_pages_to_copy, + vm_write, 0, process_pages, NULL); + up_read(&mm->mmap_sem); + + if (pages_pinned <= 0) + return -EFAULT; + + rc = process_vm_rw_pages(process_pages, start_offset, len, iter, - vm_write, nr_pages_to_copy, + vm_write, pages_pinned, &bytes_copied_loop); start_offset = 0; *bytes_copied += bytes_copied_loop; + len -= bytes_copied_loop; + nr_pages_copied += pages_pinned; + pa += pages_pinned * PAGE_SIZE; + while (pages_pinned) + put_page(process_pages[--pages_pinned]); - if (rc < 0) { - return rc; - } else { - len -= bytes_copied_loop; - nr_pages_copied += nr_pages_to_copy; - pa += nr_pages_to_copy * PAGE_SIZE; - } + if (rc < 0) + break; } return rc; -- cgit v1.2.3 From e21345f9c3f8a806604910cae886e471a860ab86 Mon Sep 17 00:00:00 2001 From: Al Viro Date: Wed, 5 Feb 2014 12:55:11 -0500 Subject: process_vm_rw_pages(): pass accurate amount of bytes ... makes passing the amount of pages unnecessary Signed-off-by: Al Viro --- mm/process_vm_access.c | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) (limited to 'mm/process_vm_access.c') diff --git a/mm/process_vm_access.c b/mm/process_vm_access.c index c2a1916bacbf..83252d783482 100644 --- a/mm/process_vm_access.c +++ b/mm/process_vm_access.c @@ -41,20 +41,22 @@ */ static int process_vm_rw_pages(struct page **pages, unsigned offset, - unsigned long len, + size_t len, struct iov_iter *iter, int vm_write, - unsigned int nr_pages_to_copy, ssize_t *bytes_copied) { *bytes_copied = 0; /* Do the copy for each page */ - while (iov_iter_count(iter) && nr_pages_to_copy--) { + while (iov_iter_count(iter) && len) { struct page *page = *pages++; - size_t copy = min_t(ssize_t, PAGE_SIZE - offset, len); + size_t copy = PAGE_SIZE - offset; size_t copied; + if (copy > len) + copy = len; + if (vm_write) { if (copy > iov_iter_count(iter)) copy = iov_iter_count(iter); @@ -121,6 +123,7 @@ static int process_vm_rw_single_vec(unsigned long addr, while ((nr_pages_copied < nr_pages) && iov_iter_count(iter)) { int nr_pages_to_copy; int pages_pinned; + size_t n; nr_pages_to_copy = min(nr_pages - nr_pages_copied, max_pages_per_loop); @@ -134,18 +137,21 @@ static int process_vm_rw_single_vec(unsigned long addr, if (pages_pinned <= 0) return -EFAULT; + n = pages_pinned * PAGE_SIZE - start_offset; + if (n > len) + n = len; + rc = process_vm_rw_pages(process_pages, - start_offset, len, iter, - vm_write, pages_pinned, + start_offset, n, iter, + vm_write, &bytes_copied_loop); + len -= n; start_offset = 0; *bytes_copied += bytes_copied_loop; - len -= bytes_copied_loop; nr_pages_copied += pages_pinned; pa += pages_pinned * PAGE_SIZE; while (pages_pinned) put_page(process_pages[--pages_pinned]); - if (rc < 0) break; } -- cgit v1.2.3 From 9acc1a0f9af244826de0eea2c38b78410eb0df6d Mon Sep 17 00:00:00 2001 From: Al Viro Date: Wed, 5 Feb 2014 13:15:28 -0500 Subject: process_vm_access: don't bother with returning the amounts of bytes copied we can calculate that in the caller just fine, TYVM Signed-off-by: Al Viro --- mm/process_vm_access.c | 47 ++++++++++++++++------------------------------- 1 file changed, 16 insertions(+), 31 deletions(-) (limited to 'mm/process_vm_access.c') diff --git a/mm/process_vm_access.c b/mm/process_vm_access.c index 83252d783482..15e4c2f312e8 100644 --- a/mm/process_vm_access.c +++ b/mm/process_vm_access.c @@ -43,13 +43,10 @@ static int process_vm_rw_pages(struct page **pages, unsigned offset, size_t len, struct iov_iter *iter, - int vm_write, - ssize_t *bytes_copied) + int vm_write) { - *bytes_copied = 0; - /* Do the copy for each page */ - while (iov_iter_count(iter) && len) { + while (len && iov_iter_count(iter)) { struct page *page = *pages++; size_t copy = PAGE_SIZE - offset; size_t copied; @@ -67,7 +64,6 @@ static int process_vm_rw_pages(struct page **pages, } else { copied = copy_page_to_iter(page, offset, copy, iter); } - *bytes_copied += copied; len -= copied; if (copied < copy && iov_iter_count(iter)) return -EFAULT; @@ -101,20 +97,16 @@ static int process_vm_rw_single_vec(unsigned long addr, struct page **process_pages, struct mm_struct *mm, struct task_struct *task, - int vm_write, - ssize_t *bytes_copied) + int vm_write) { unsigned long pa = addr & PAGE_MASK; unsigned long start_offset = addr - pa; unsigned long nr_pages; - ssize_t bytes_copied_loop; ssize_t rc = 0; unsigned long nr_pages_copied = 0; unsigned long max_pages_per_loop = PVM_MAX_KMALLOC_PAGES / sizeof(struct pages *); - *bytes_copied = 0; - /* Work out address and page range required */ if (len == 0) return 0; @@ -143,11 +135,9 @@ static int process_vm_rw_single_vec(unsigned long addr, rc = process_vm_rw_pages(process_pages, start_offset, n, iter, - vm_write, - &bytes_copied_loop); + vm_write); len -= n; start_offset = 0; - *bytes_copied += bytes_copied_loop; nr_pages_copied += pages_pinned; pa += pages_pinned * PAGE_SIZE; while (pages_pinned) @@ -187,11 +177,10 @@ static ssize_t process_vm_rw_core(pid_t pid, struct iov_iter *iter, struct mm_struct *mm; unsigned long i; ssize_t rc = 0; - ssize_t bytes_copied_loop; - ssize_t bytes_copied = 0; unsigned long nr_pages = 0; unsigned long nr_pages_iov; ssize_t iov_len; + size_t total_len = iov_iter_count(iter); /* * Work out how many pages of struct pages we're going to need @@ -245,24 +234,20 @@ static ssize_t process_vm_rw_core(pid_t pid, struct iov_iter *iter, goto put_task_struct; } - for (i = 0; i < riovcnt && iov_iter_count(iter); i++) { + for (i = 0; i < riovcnt && iov_iter_count(iter) && !rc; i++) rc = process_vm_rw_single_vec( (unsigned long)rvec[i].iov_base, rvec[i].iov_len, - iter, process_pages, mm, task, vm_write, - &bytes_copied_loop); - bytes_copied += bytes_copied_loop; - if (rc != 0) { - /* If we have managed to copy any data at all then - we return the number of bytes copied. Otherwise - we return the error code */ - if (bytes_copied) - rc = bytes_copied; - goto put_mm; - } - } + iter, process_pages, mm, task, vm_write); + + /* copied = space before - space after */ + total_len -= iov_iter_count(iter); + + /* If we have managed to copy any data at all then + we return the number of bytes copied. Otherwise + we return the error code */ + if (total_len) + rc = total_len; - rc = bytes_copied; -put_mm: mmput(mm); put_task_struct: -- cgit v1.2.3 From 4bafbec7bf60ed56ccbb36a96091bdbd162f075d Mon Sep 17 00:00:00 2001 From: Al Viro Date: Wed, 5 Feb 2014 13:25:32 -0500 Subject: process_vm_access: tidy up a bit saner variable names, update linuxdoc comments, etc. Signed-off-by: Al Viro --- mm/process_vm_access.c | 59 ++++++++++++++++---------------------------------- 1 file changed, 19 insertions(+), 40 deletions(-) (limited to 'mm/process_vm_access.c') diff --git a/mm/process_vm_access.c b/mm/process_vm_access.c index 15e4c2f312e8..d6bd3fdd6925 100644 --- a/mm/process_vm_access.c +++ b/mm/process_vm_access.c @@ -23,20 +23,11 @@ /** * process_vm_rw_pages - read/write pages from task specified - * @task: task to read/write from - * @mm: mm for task - * @process_pages: struct pages area that can store at least - * nr_pages_to_copy struct page pointers - * @pa: address of page in task to start copying from/to + * @pages: array of pointers to pages we want to copy * @start_offset: offset in page to start copying from/to * @len: number of bytes to copy - * @lvec: iovec array specifying where to copy to/from - * @lvec_cnt: number of elements in iovec array - * @lvec_current: index in iovec array we are up to - * @lvec_offset: offset in bytes from current iovec iov_base we are up to + * @iter: where to copy to/from locally * @vm_write: 0 means copy from, 1 means copy to - * @nr_pages_to_copy: number of pages to copy - * @bytes_copied: returns number of bytes successfully copied * Returns 0 on success, error code otherwise */ static int process_vm_rw_pages(struct page **pages, @@ -79,16 +70,12 @@ static int process_vm_rw_pages(struct page **pages, * process_vm_rw_single_vec - read/write pages from task specified * @addr: start memory address of target process * @len: size of area to copy to/from - * @lvec: iovec array specifying where to copy to/from locally - * @lvec_cnt: number of elements in iovec array - * @lvec_current: index in iovec array we are up to - * @lvec_offset: offset in bytes from current iovec iov_base we are up to + * @iter: where to copy to/from locally * @process_pages: struct pages area that can store at least * nr_pages_to_copy struct page pointers * @mm: mm for task * @task: task to read/write from * @vm_write: 0 means copy from, 1 means copy to - * @bytes_copied: returns number of bytes successfully copied * Returns 0 on success or on failure error code */ static int process_vm_rw_single_vec(unsigned long addr, @@ -103,7 +90,6 @@ static int process_vm_rw_single_vec(unsigned long addr, unsigned long start_offset = addr - pa; unsigned long nr_pages; ssize_t rc = 0; - unsigned long nr_pages_copied = 0; unsigned long max_pages_per_loop = PVM_MAX_KMALLOC_PAGES / sizeof(struct pages *); @@ -112,38 +98,32 @@ static int process_vm_rw_single_vec(unsigned long addr, return 0; nr_pages = (addr + len - 1) / PAGE_SIZE - addr / PAGE_SIZE + 1; - while ((nr_pages_copied < nr_pages) && iov_iter_count(iter)) { - int nr_pages_to_copy; - int pages_pinned; - size_t n; - nr_pages_to_copy = min(nr_pages - nr_pages_copied, - max_pages_per_loop); + while (!rc && nr_pages && iov_iter_count(iter)) { + int pages = min(nr_pages, max_pages_per_loop); + size_t bytes; /* Get the pages we're interested in */ down_read(&mm->mmap_sem); - pages_pinned = get_user_pages(task, mm, pa, - nr_pages_to_copy, - vm_write, 0, process_pages, NULL); + pages = get_user_pages(task, mm, pa, pages, + vm_write, 0, process_pages, NULL); up_read(&mm->mmap_sem); - if (pages_pinned <= 0) + if (pages <= 0) return -EFAULT; - n = pages_pinned * PAGE_SIZE - start_offset; - if (n > len) - n = len; + bytes = pages * PAGE_SIZE - start_offset; + if (bytes > len) + bytes = len; rc = process_vm_rw_pages(process_pages, - start_offset, n, iter, + start_offset, bytes, iter, vm_write); - len -= n; + len -= bytes; start_offset = 0; - nr_pages_copied += pages_pinned; - pa += pages_pinned * PAGE_SIZE; - while (pages_pinned) - put_page(process_pages[--pages_pinned]); - if (rc < 0) - break; + nr_pages -= pages; + pa += pages * PAGE_SIZE; + while (pages) + put_page(process_pages[--pages]); } return rc; @@ -156,8 +136,7 @@ static int process_vm_rw_single_vec(unsigned long addr, /** * process_vm_rw_core - core of reading/writing pages from task specified * @pid: PID of process to read/write from/to - * @lvec: iovec array specifying where to copy to/from locally - * @liovcnt: size of lvec array + * @iter: where to copy to/from locally * @rvec: iovec array specifying where to copy to/from in the other process * @riovcnt: size of rvec array * @flags: currently unused -- cgit v1.2.3