aboutsummaryrefslogtreecommitdiffstats
path: root/mm/process_vm_access.c
diff options
context:
space:
mode:
Diffstat (limited to 'mm/process_vm_access.c')
-rw-r--r--mm/process_vm_access.c250
1 files changed, 76 insertions, 174 deletions
diff --git a/mm/process_vm_access.c b/mm/process_vm_access.c
index cb79065c19e5..8505c9262b35 100644
--- a/mm/process_vm_access.c
+++ b/mm/process_vm_access.c
@@ -23,129 +23,44 @@
23 23
24/** 24/**
25 * process_vm_rw_pages - read/write pages from task specified 25 * process_vm_rw_pages - read/write pages from task specified
26 * @task: task to read/write from 26 * @pages: array of pointers to pages we want to copy
27 * @mm: mm for task
28 * @process_pages: struct pages area that can store at least
29 * nr_pages_to_copy struct page pointers
30 * @pa: address of page in task to start copying from/to
31 * @start_offset: offset in page to start copying from/to 27 * @start_offset: offset in page to start copying from/to
32 * @len: number of bytes to copy 28 * @len: number of bytes to copy
33 * @lvec: iovec array specifying where to copy to/from 29 * @iter: where to copy to/from locally
34 * @lvec_cnt: number of elements in iovec array
35 * @lvec_current: index in iovec array we are up to
36 * @lvec_offset: offset in bytes from current iovec iov_base we are up to
37 * @vm_write: 0 means copy from, 1 means copy to 30 * @vm_write: 0 means copy from, 1 means copy to
38 * @nr_pages_to_copy: number of pages to copy
39 * @bytes_copied: returns number of bytes successfully copied
40 * Returns 0 on success, error code otherwise 31 * Returns 0 on success, error code otherwise
41 */ 32 */
42static int process_vm_rw_pages(struct task_struct *task, 33static int process_vm_rw_pages(struct page **pages,
43 struct mm_struct *mm, 34 unsigned offset,
44 struct page **process_pages, 35 size_t len,
45 unsigned long pa, 36 struct iov_iter *iter,
46 unsigned long start_offset, 37 int vm_write)
47 unsigned long len,
48 const struct iovec *lvec,
49 unsigned long lvec_cnt,
50 unsigned long *lvec_current,
51 size_t *lvec_offset,
52 int vm_write,
53 unsigned int nr_pages_to_copy,
54 ssize_t *bytes_copied)
55{ 38{
56 int pages_pinned;
57 void *target_kaddr;
58 int pgs_copied = 0;
59 int j;
60 int ret;
61 ssize_t bytes_to_copy;
62 ssize_t rc = 0;
63
64 *bytes_copied = 0;
65
66 /* Get the pages we're interested in */
67 down_read(&mm->mmap_sem);
68 pages_pinned = get_user_pages(task, mm, pa,
69 nr_pages_to_copy,
70 vm_write, 0, process_pages, NULL);
71 up_read(&mm->mmap_sem);
72
73 if (pages_pinned != nr_pages_to_copy) {
74 rc = -EFAULT;
75 goto end;
76 }
77
78 /* Do the copy for each page */ 39 /* Do the copy for each page */
79 for (pgs_copied = 0; 40 while (len && iov_iter_count(iter)) {
80 (pgs_copied < nr_pages_to_copy) && (*lvec_current < lvec_cnt); 41 struct page *page = *pages++;
81 pgs_copied++) { 42 size_t copy = PAGE_SIZE - offset;
82 /* Make sure we have a non zero length iovec */ 43 size_t copied;
83 while (*lvec_current < lvec_cnt 44
84 && lvec[*lvec_current].iov_len == 0) 45 if (copy > len)
85 (*lvec_current)++; 46 copy = len;
86 if (*lvec_current == lvec_cnt) 47
87 break; 48 if (vm_write) {
88 49 if (copy > iov_iter_count(iter))
89 /* 50 copy = iov_iter_count(iter);
90 * Will copy smallest of: 51 copied = iov_iter_copy_from_user(page, iter,
91 * - bytes remaining in page 52 offset, copy);
92 * - bytes remaining in destination iovec 53 iov_iter_advance(iter, copied);
93 */ 54 set_page_dirty_lock(page);
94 bytes_to_copy = min_t(ssize_t, PAGE_SIZE - start_offset,
95 len - *bytes_copied);
96 bytes_to_copy = min_t(ssize_t, bytes_to_copy,
97 lvec[*lvec_current].iov_len
98 - *lvec_offset);
99
100 target_kaddr = kmap(process_pages[pgs_copied]) + start_offset;
101
102 if (vm_write)
103 ret = copy_from_user(target_kaddr,
104 lvec[*lvec_current].iov_base
105 + *lvec_offset,
106 bytes_to_copy);
107 else
108 ret = copy_to_user(lvec[*lvec_current].iov_base
109 + *lvec_offset,
110 target_kaddr, bytes_to_copy);
111 kunmap(process_pages[pgs_copied]);
112 if (ret) {
113 *bytes_copied += bytes_to_copy - ret;
114 pgs_copied++;
115 rc = -EFAULT;
116 goto end;
117 }
118 *bytes_copied += bytes_to_copy;
119 *lvec_offset += bytes_to_copy;
120 if (*lvec_offset == lvec[*lvec_current].iov_len) {
121 /*
122 * Need to copy remaining part of page into the
123 * next iovec if there are any bytes left in page
124 */
125 (*lvec_current)++;
126 *lvec_offset = 0;
127 start_offset = (start_offset + bytes_to_copy)
128 % PAGE_SIZE;
129 if (start_offset)
130 pgs_copied--;
131 } else { 55 } else {
132 start_offset = 0; 56 copied = copy_page_to_iter(page, offset, copy, iter);
133 }
134 }
135
136end:
137 if (vm_write) {
138 for (j = 0; j < pages_pinned; j++) {
139 if (j < pgs_copied)
140 set_page_dirty_lock(process_pages[j]);
141 put_page(process_pages[j]);
142 } 57 }
143 } else { 58 len -= copied;
144 for (j = 0; j < pages_pinned; j++) 59 if (copied < copy && iov_iter_count(iter))
145 put_page(process_pages[j]); 60 return -EFAULT;
61 offset = 0;
146 } 62 }
147 63 return 0;
148 return rc;
149} 64}
150 65
151/* Maximum number of pages kmalloc'd to hold struct page's during copy */ 66/* Maximum number of pages kmalloc'd to hold struct page's during copy */
@@ -155,67 +70,60 @@ end:
155 * process_vm_rw_single_vec - read/write pages from task specified 70 * process_vm_rw_single_vec - read/write pages from task specified
156 * @addr: start memory address of target process 71 * @addr: start memory address of target process
157 * @len: size of area to copy to/from 72 * @len: size of area to copy to/from
158 * @lvec: iovec array specifying where to copy to/from locally 73 * @iter: where to copy to/from locally
159 * @lvec_cnt: number of elements in iovec array
160 * @lvec_current: index in iovec array we are up to
161 * @lvec_offset: offset in bytes from current iovec iov_base we are up to
162 * @process_pages: struct pages area that can store at least 74 * @process_pages: struct pages area that can store at least
163 * nr_pages_to_copy struct page pointers 75 * nr_pages_to_copy struct page pointers
164 * @mm: mm for task 76 * @mm: mm for task
165 * @task: task to read/write from 77 * @task: task to read/write from
166 * @vm_write: 0 means copy from, 1 means copy to 78 * @vm_write: 0 means copy from, 1 means copy to
167 * @bytes_copied: returns number of bytes successfully copied
168 * Returns 0 on success or on failure error code 79 * Returns 0 on success or on failure error code
169 */ 80 */
170static int process_vm_rw_single_vec(unsigned long addr, 81static int process_vm_rw_single_vec(unsigned long addr,
171 unsigned long len, 82 unsigned long len,
172 const struct iovec *lvec, 83 struct iov_iter *iter,
173 unsigned long lvec_cnt,
174 unsigned long *lvec_current,
175 size_t *lvec_offset,
176 struct page **process_pages, 84 struct page **process_pages,
177 struct mm_struct *mm, 85 struct mm_struct *mm,
178 struct task_struct *task, 86 struct task_struct *task,
179 int vm_write, 87 int vm_write)
180 ssize_t *bytes_copied)
181{ 88{
182 unsigned long pa = addr & PAGE_MASK; 89 unsigned long pa = addr & PAGE_MASK;
183 unsigned long start_offset = addr - pa; 90 unsigned long start_offset = addr - pa;
184 unsigned long nr_pages; 91 unsigned long nr_pages;
185 ssize_t bytes_copied_loop;
186 ssize_t rc = 0; 92 ssize_t rc = 0;
187 unsigned long nr_pages_copied = 0;
188 unsigned long nr_pages_to_copy;
189 unsigned long max_pages_per_loop = PVM_MAX_KMALLOC_PAGES 93 unsigned long max_pages_per_loop = PVM_MAX_KMALLOC_PAGES
190 / sizeof(struct pages *); 94 / sizeof(struct pages *);
191 95
192 *bytes_copied = 0;
193
194 /* Work out address and page range required */ 96 /* Work out address and page range required */
195 if (len == 0) 97 if (len == 0)
196 return 0; 98 return 0;
197 nr_pages = (addr + len - 1) / PAGE_SIZE - addr / PAGE_SIZE + 1; 99 nr_pages = (addr + len - 1) / PAGE_SIZE - addr / PAGE_SIZE + 1;
198 100
199 while ((nr_pages_copied < nr_pages) && (*lvec_current < lvec_cnt)) { 101 while (!rc && nr_pages && iov_iter_count(iter)) {
200 nr_pages_to_copy = min(nr_pages - nr_pages_copied, 102 int pages = min(nr_pages, max_pages_per_loop);
201 max_pages_per_loop); 103 size_t bytes;
202 104
203 rc = process_vm_rw_pages(task, mm, process_pages, pa, 105 /* Get the pages we're interested in */
204 start_offset, len, 106 down_read(&mm->mmap_sem);
205 lvec, lvec_cnt, 107 pages = get_user_pages(task, mm, pa, pages,
206 lvec_current, lvec_offset, 108 vm_write, 0, process_pages, NULL);
207 vm_write, nr_pages_to_copy, 109 up_read(&mm->mmap_sem);
208 &bytes_copied_loop);
209 start_offset = 0;
210 *bytes_copied += bytes_copied_loop;
211 110
212 if (rc < 0) { 111 if (pages <= 0)
213 return rc; 112 return -EFAULT;
214 } else { 113
215 len -= bytes_copied_loop; 114 bytes = pages * PAGE_SIZE - start_offset;
216 nr_pages_copied += nr_pages_to_copy; 115 if (bytes > len)
217 pa += nr_pages_to_copy * PAGE_SIZE; 116 bytes = len;
218 } 117
118 rc = process_vm_rw_pages(process_pages,
119 start_offset, bytes, iter,
120 vm_write);
121 len -= bytes;
122 start_offset = 0;
123 nr_pages -= pages;
124 pa += pages * PAGE_SIZE;
125 while (pages)
126 put_page(process_pages[--pages]);
219 } 127 }
220 128
221 return rc; 129 return rc;
@@ -228,8 +136,7 @@ static int process_vm_rw_single_vec(unsigned long addr,
228/** 136/**
229 * process_vm_rw_core - core of reading/writing pages from task specified 137 * process_vm_rw_core - core of reading/writing pages from task specified
230 * @pid: PID of process to read/write from/to 138 * @pid: PID of process to read/write from/to
231 * @lvec: iovec array specifying where to copy to/from locally 139 * @iter: where to copy to/from locally
232 * @liovcnt: size of lvec array
233 * @rvec: iovec array specifying where to copy to/from in the other process 140 * @rvec: iovec array specifying where to copy to/from in the other process
234 * @riovcnt: size of rvec array 141 * @riovcnt: size of rvec array
235 * @flags: currently unused 142 * @flags: currently unused
@@ -238,8 +145,7 @@ static int process_vm_rw_single_vec(unsigned long addr,
238 * return less bytes than expected if an error occurs during the copying 145 * return less bytes than expected if an error occurs during the copying
239 * process. 146 * process.
240 */ 147 */
241static ssize_t process_vm_rw_core(pid_t pid, const struct iovec *lvec, 148static ssize_t process_vm_rw_core(pid_t pid, struct iov_iter *iter,
242 unsigned long liovcnt,
243 const struct iovec *rvec, 149 const struct iovec *rvec,
244 unsigned long riovcnt, 150 unsigned long riovcnt,
245 unsigned long flags, int vm_write) 151 unsigned long flags, int vm_write)
@@ -250,13 +156,10 @@ static ssize_t process_vm_rw_core(pid_t pid, const struct iovec *lvec,
250 struct mm_struct *mm; 156 struct mm_struct *mm;
251 unsigned long i; 157 unsigned long i;
252 ssize_t rc = 0; 158 ssize_t rc = 0;
253 ssize_t bytes_copied_loop;
254 ssize_t bytes_copied = 0;
255 unsigned long nr_pages = 0; 159 unsigned long nr_pages = 0;
256 unsigned long nr_pages_iov; 160 unsigned long nr_pages_iov;
257 unsigned long iov_l_curr_idx = 0;
258 size_t iov_l_curr_offset = 0;
259 ssize_t iov_len; 161 ssize_t iov_len;
162 size_t total_len = iov_iter_count(iter);
260 163
261 /* 164 /*
262 * Work out how many pages of struct pages we're going to need 165 * Work out how many pages of struct pages we're going to need
@@ -310,24 +213,20 @@ static ssize_t process_vm_rw_core(pid_t pid, const struct iovec *lvec,
310 goto put_task_struct; 213 goto put_task_struct;
311 } 214 }
312 215
313 for (i = 0; i < riovcnt && iov_l_curr_idx < liovcnt; i++) { 216 for (i = 0; i < riovcnt && iov_iter_count(iter) && !rc; i++)
314 rc = process_vm_rw_single_vec( 217 rc = process_vm_rw_single_vec(
315 (unsigned long)rvec[i].iov_base, rvec[i].iov_len, 218 (unsigned long)rvec[i].iov_base, rvec[i].iov_len,
316 lvec, liovcnt, &iov_l_curr_idx, &iov_l_curr_offset, 219 iter, process_pages, mm, task, vm_write);
317 process_pages, mm, task, vm_write, &bytes_copied_loop); 220
318 bytes_copied += bytes_copied_loop; 221 /* copied = space before - space after */
319 if (rc != 0) { 222 total_len -= iov_iter_count(iter);
320 /* If we have managed to copy any data at all then 223
321 we return the number of bytes copied. Otherwise 224 /* If we have managed to copy any data at all then
322 we return the error code */ 225 we return the number of bytes copied. Otherwise
323 if (bytes_copied) 226 we return the error code */
324 rc = bytes_copied; 227 if (total_len)
325 goto put_mm; 228 rc = total_len;
326 }
327 }
328 229
329 rc = bytes_copied;
330put_mm:
331 mmput(mm); 230 mmput(mm);
332 231
333put_task_struct: 232put_task_struct:
@@ -363,6 +262,7 @@ static ssize_t process_vm_rw(pid_t pid,
363 struct iovec iovstack_r[UIO_FASTIOV]; 262 struct iovec iovstack_r[UIO_FASTIOV];
364 struct iovec *iov_l = iovstack_l; 263 struct iovec *iov_l = iovstack_l;
365 struct iovec *iov_r = iovstack_r; 264 struct iovec *iov_r = iovstack_r;
265 struct iov_iter iter;
366 ssize_t rc; 266 ssize_t rc;
367 267
368 if (flags != 0) 268 if (flags != 0)
@@ -378,13 +278,14 @@ static ssize_t process_vm_rw(pid_t pid,
378 if (rc <= 0) 278 if (rc <= 0)
379 goto free_iovecs; 279 goto free_iovecs;
380 280
281 iov_iter_init(&iter, iov_l, liovcnt, rc, 0);
282
381 rc = rw_copy_check_uvector(CHECK_IOVEC_ONLY, rvec, riovcnt, UIO_FASTIOV, 283 rc = rw_copy_check_uvector(CHECK_IOVEC_ONLY, rvec, riovcnt, UIO_FASTIOV,
382 iovstack_r, &iov_r); 284 iovstack_r, &iov_r);
383 if (rc <= 0) 285 if (rc <= 0)
384 goto free_iovecs; 286 goto free_iovecs;
385 287
386 rc = process_vm_rw_core(pid, iov_l, liovcnt, iov_r, riovcnt, flags, 288 rc = process_vm_rw_core(pid, &iter, iov_r, riovcnt, flags, vm_write);
387 vm_write);
388 289
389free_iovecs: 290free_iovecs:
390 if (iov_r != iovstack_r) 291 if (iov_r != iovstack_r)
@@ -424,6 +325,7 @@ compat_process_vm_rw(compat_pid_t pid,
424 struct iovec iovstack_r[UIO_FASTIOV]; 325 struct iovec iovstack_r[UIO_FASTIOV];
425 struct iovec *iov_l = iovstack_l; 326 struct iovec *iov_l = iovstack_l;
426 struct iovec *iov_r = iovstack_r; 327 struct iovec *iov_r = iovstack_r;
328 struct iov_iter iter;
427 ssize_t rc = -EFAULT; 329 ssize_t rc = -EFAULT;
428 330
429 if (flags != 0) 331 if (flags != 0)
@@ -439,14 +341,14 @@ compat_process_vm_rw(compat_pid_t pid,
439 &iov_l); 341 &iov_l);
440 if (rc <= 0) 342 if (rc <= 0)
441 goto free_iovecs; 343 goto free_iovecs;
344 iov_iter_init(&iter, iov_l, liovcnt, rc, 0);
442 rc = compat_rw_copy_check_uvector(CHECK_IOVEC_ONLY, rvec, riovcnt, 345 rc = compat_rw_copy_check_uvector(CHECK_IOVEC_ONLY, rvec, riovcnt,
443 UIO_FASTIOV, iovstack_r, 346 UIO_FASTIOV, iovstack_r,
444 &iov_r); 347 &iov_r);
445 if (rc <= 0) 348 if (rc <= 0)
446 goto free_iovecs; 349 goto free_iovecs;
447 350
448 rc = process_vm_rw_core(pid, iov_l, liovcnt, iov_r, riovcnt, flags, 351 rc = process_vm_rw_core(pid, &iter, iov_r, riovcnt, flags, vm_write);
449 vm_write);
450 352
451free_iovecs: 353free_iovecs:
452 if (iov_r != iovstack_r) 354 if (iov_r != iovstack_r)