untangling process_vm_..., part 4
[deliverable/linux.git] / mm / process_vm_access.c
CommitLineData
fcf63409
CY
1/*
2 * linux/mm/process_vm_access.c
3 *
4 * Copyright (C) 2010-2011 Christopher Yeoh <cyeoh@au1.ibm.com>, IBM Corp.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 */
11
12#include <linux/mm.h>
13#include <linux/uio.h>
14#include <linux/sched.h>
15#include <linux/highmem.h>
16#include <linux/ptrace.h>
17#include <linux/slab.h>
18#include <linux/syscalls.h>
19
20#ifdef CONFIG_COMPAT
21#include <linux/compat.h>
22#endif
23
24/**
25 * process_vm_rw_pages - read/write pages from task specified
26 * @task: task to read/write from
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
32 * @len: number of bytes to copy
33 * @lvec: iovec array specifying where to copy to/from
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
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
41 */
42static int process_vm_rw_pages(struct task_struct *task,
43 struct mm_struct *mm,
44 struct page **process_pages,
45 unsigned long pa,
46 unsigned long start_offset,
47 unsigned long len,
c61c7038 48 const struct iovec **iovp,
1291afc1 49 unsigned long *left,
fcf63409
CY
50 size_t *lvec_offset,
51 int vm_write,
52 unsigned int nr_pages_to_copy,
53 ssize_t *bytes_copied)
54{
55 int pages_pinned;
56 void *target_kaddr;
57 int pgs_copied = 0;
58 int j;
59 int ret;
60 ssize_t bytes_to_copy;
61 ssize_t rc = 0;
c61c7038 62 const struct iovec *iov = *iovp;
fcf63409
CY
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 */
79 for (pgs_copied = 0;
1291afc1 80 (pgs_copied < nr_pages_to_copy) && *left;
fcf63409
CY
81 pgs_copied++) {
82 /* Make sure we have a non zero length iovec */
1291afc1 83 while (*left && iov->iov_len == 0) {
480402e1 84 iov++;
1291afc1 85 (*left)--;
480402e1 86 }
1291afc1 87 if (!*left)
fcf63409
CY
88 break;
89
90 /*
91 * Will copy smallest of:
92 * - bytes remaining in page
93 * - bytes remaining in destination iovec
94 */
95 bytes_to_copy = min_t(ssize_t, PAGE_SIZE - start_offset,
96 len - *bytes_copied);
97 bytes_to_copy = min_t(ssize_t, bytes_to_copy,
480402e1 98 iov->iov_len
fcf63409
CY
99 - *lvec_offset);
100
101 target_kaddr = kmap(process_pages[pgs_copied]) + start_offset;
102
103 if (vm_write)
104 ret = copy_from_user(target_kaddr,
480402e1 105 iov->iov_base
fcf63409
CY
106 + *lvec_offset,
107 bytes_to_copy);
108 else
480402e1 109 ret = copy_to_user(iov->iov_base
fcf63409
CY
110 + *lvec_offset,
111 target_kaddr, bytes_to_copy);
112 kunmap(process_pages[pgs_copied]);
113 if (ret) {
114 *bytes_copied += bytes_to_copy - ret;
115 pgs_copied++;
116 rc = -EFAULT;
117 goto end;
118 }
119 *bytes_copied += bytes_to_copy;
120 *lvec_offset += bytes_to_copy;
480402e1 121 if (*lvec_offset == iov->iov_len) {
fcf63409
CY
122 /*
123 * Need to copy remaining part of page into the
124 * next iovec if there are any bytes left in page
125 */
1291afc1 126 (*left)--;
480402e1 127 iov++;
fcf63409
CY
128 *lvec_offset = 0;
129 start_offset = (start_offset + bytes_to_copy)
130 % PAGE_SIZE;
131 if (start_offset)
132 pgs_copied--;
133 } else {
134 start_offset = 0;
135 }
136 }
137
138end:
139 if (vm_write) {
140 for (j = 0; j < pages_pinned; j++) {
141 if (j < pgs_copied)
142 set_page_dirty_lock(process_pages[j]);
143 put_page(process_pages[j]);
144 }
145 } else {
146 for (j = 0; j < pages_pinned; j++)
147 put_page(process_pages[j]);
148 }
149
c61c7038 150 *iovp = iov;
fcf63409
CY
151 return rc;
152}
153
154/* Maximum number of pages kmalloc'd to hold struct page's during copy */
155#define PVM_MAX_KMALLOC_PAGES (PAGE_SIZE * 2)
156
157/**
158 * process_vm_rw_single_vec - read/write pages from task specified
159 * @addr: start memory address of target process
160 * @len: size of area to copy to/from
161 * @lvec: iovec array specifying where to copy to/from locally
162 * @lvec_cnt: number of elements in iovec array
163 * @lvec_current: index in iovec array we are up to
164 * @lvec_offset: offset in bytes from current iovec iov_base we are up to
165 * @process_pages: struct pages area that can store at least
166 * nr_pages_to_copy struct page pointers
167 * @mm: mm for task
168 * @task: task to read/write from
169 * @vm_write: 0 means copy from, 1 means copy to
170 * @bytes_copied: returns number of bytes successfully copied
171 * Returns 0 on success or on failure error code
172 */
173static int process_vm_rw_single_vec(unsigned long addr,
174 unsigned long len,
12e3004e 175 const struct iovec **iovp,
1291afc1 176 unsigned long *left,
fcf63409
CY
177 size_t *lvec_offset,
178 struct page **process_pages,
179 struct mm_struct *mm,
180 struct task_struct *task,
181 int vm_write,
182 ssize_t *bytes_copied)
183{
184 unsigned long pa = addr & PAGE_MASK;
185 unsigned long start_offset = addr - pa;
186 unsigned long nr_pages;
187 ssize_t bytes_copied_loop;
188 ssize_t rc = 0;
189 unsigned long nr_pages_copied = 0;
190 unsigned long nr_pages_to_copy;
191 unsigned long max_pages_per_loop = PVM_MAX_KMALLOC_PAGES
192 / sizeof(struct pages *);
193
194 *bytes_copied = 0;
195
196 /* Work out address and page range required */
197 if (len == 0)
198 return 0;
199 nr_pages = (addr + len - 1) / PAGE_SIZE - addr / PAGE_SIZE + 1;
200
1291afc1 201 while ((nr_pages_copied < nr_pages) && *left) {
fcf63409
CY
202 nr_pages_to_copy = min(nr_pages - nr_pages_copied,
203 max_pages_per_loop);
204
205 rc = process_vm_rw_pages(task, mm, process_pages, pa,
206 start_offset, len,
1291afc1
AV
207 iovp, left,
208 lvec_offset,
fcf63409
CY
209 vm_write, nr_pages_to_copy,
210 &bytes_copied_loop);
211 start_offset = 0;
212 *bytes_copied += bytes_copied_loop;
213
214 if (rc < 0) {
215 return rc;
216 } else {
217 len -= bytes_copied_loop;
218 nr_pages_copied += nr_pages_to_copy;
219 pa += nr_pages_to_copy * PAGE_SIZE;
220 }
221 }
222
223 return rc;
224}
225
226/* Maximum number of entries for process pages array
227 which lives on stack */
228#define PVM_MAX_PP_ARRAY_COUNT 16
229
230/**
231 * process_vm_rw_core - core of reading/writing pages from task specified
232 * @pid: PID of process to read/write from/to
233 * @lvec: iovec array specifying where to copy to/from locally
234 * @liovcnt: size of lvec array
235 * @rvec: iovec array specifying where to copy to/from in the other process
236 * @riovcnt: size of rvec array
237 * @flags: currently unused
238 * @vm_write: 0 if reading from other process, 1 if writing to other process
239 * Returns the number of bytes read/written or error code. May
240 * return less bytes than expected if an error occurs during the copying
241 * process.
242 */
243static ssize_t process_vm_rw_core(pid_t pid, const struct iovec *lvec,
244 unsigned long liovcnt,
245 const struct iovec *rvec,
246 unsigned long riovcnt,
247 unsigned long flags, int vm_write)
248{
249 struct task_struct *task;
250 struct page *pp_stack[PVM_MAX_PP_ARRAY_COUNT];
251 struct page **process_pages = pp_stack;
252 struct mm_struct *mm;
253 unsigned long i;
254 ssize_t rc = 0;
255 ssize_t bytes_copied_loop;
256 ssize_t bytes_copied = 0;
257 unsigned long nr_pages = 0;
258 unsigned long nr_pages_iov;
1291afc1 259 unsigned long left = liovcnt;
fcf63409
CY
260 size_t iov_l_curr_offset = 0;
261 ssize_t iov_len;
262
263 /*
264 * Work out how many pages of struct pages we're going to need
265 * when eventually calling get_user_pages
266 */
267 for (i = 0; i < riovcnt; i++) {
268 iov_len = rvec[i].iov_len;
269 if (iov_len > 0) {
270 nr_pages_iov = ((unsigned long)rvec[i].iov_base
271 + iov_len)
272 / PAGE_SIZE - (unsigned long)rvec[i].iov_base
273 / PAGE_SIZE + 1;
274 nr_pages = max(nr_pages, nr_pages_iov);
275 }
276 }
277
278 if (nr_pages == 0)
279 return 0;
280
281 if (nr_pages > PVM_MAX_PP_ARRAY_COUNT) {
282 /* For reliability don't try to kmalloc more than
283 2 pages worth */
284 process_pages = kmalloc(min_t(size_t, PVM_MAX_KMALLOC_PAGES,
285 sizeof(struct pages *)*nr_pages),
286 GFP_KERNEL);
287
288 if (!process_pages)
289 return -ENOMEM;
290 }
291
292 /* Get process information */
293 rcu_read_lock();
294 task = find_task_by_vpid(pid);
295 if (task)
296 get_task_struct(task);
297 rcu_read_unlock();
298 if (!task) {
299 rc = -ESRCH;
300 goto free_proc_pages;
301 }
302
8cdb878d
CY
303 mm = mm_access(task, PTRACE_MODE_ATTACH);
304 if (!mm || IS_ERR(mm)) {
305 rc = IS_ERR(mm) ? PTR_ERR(mm) : -ESRCH;
306 /*
307 * Explicitly map EACCES to EPERM as EPERM is a more a
308 * appropriate error code for process_vw_readv/writev
309 */
310 if (rc == -EACCES)
311 rc = -EPERM;
fcf63409
CY
312 goto put_task_struct;
313 }
314
1291afc1 315 for (i = 0; i < riovcnt && left; i++) {
fcf63409
CY
316 rc = process_vm_rw_single_vec(
317 (unsigned long)rvec[i].iov_base, rvec[i].iov_len,
1291afc1 318 &lvec, &left, &iov_l_curr_offset,
fcf63409
CY
319 process_pages, mm, task, vm_write, &bytes_copied_loop);
320 bytes_copied += bytes_copied_loop;
321 if (rc != 0) {
322 /* If we have managed to copy any data at all then
323 we return the number of bytes copied. Otherwise
324 we return the error code */
325 if (bytes_copied)
326 rc = bytes_copied;
327 goto put_mm;
328 }
329 }
330
331 rc = bytes_copied;
332put_mm:
333 mmput(mm);
334
335put_task_struct:
336 put_task_struct(task);
337
338free_proc_pages:
339 if (process_pages != pp_stack)
340 kfree(process_pages);
341 return rc;
342}
343
344/**
345 * process_vm_rw - check iovecs before calling core routine
346 * @pid: PID of process to read/write from/to
347 * @lvec: iovec array specifying where to copy to/from locally
348 * @liovcnt: size of lvec array
349 * @rvec: iovec array specifying where to copy to/from in the other process
350 * @riovcnt: size of rvec array
351 * @flags: currently unused
352 * @vm_write: 0 if reading from other process, 1 if writing to other process
353 * Returns the number of bytes read/written or error code. May
354 * return less bytes than expected if an error occurs during the copying
355 * process.
356 */
357static ssize_t process_vm_rw(pid_t pid,
358 const struct iovec __user *lvec,
359 unsigned long liovcnt,
360 const struct iovec __user *rvec,
361 unsigned long riovcnt,
362 unsigned long flags, int vm_write)
363{
364 struct iovec iovstack_l[UIO_FASTIOV];
365 struct iovec iovstack_r[UIO_FASTIOV];
366 struct iovec *iov_l = iovstack_l;
367 struct iovec *iov_r = iovstack_r;
368 ssize_t rc;
369
370 if (flags != 0)
371 return -EINVAL;
372
373 /* Check iovecs */
374 if (vm_write)
375 rc = rw_copy_check_uvector(WRITE, lvec, liovcnt, UIO_FASTIOV,
ac34ebb3 376 iovstack_l, &iov_l);
fcf63409
CY
377 else
378 rc = rw_copy_check_uvector(READ, lvec, liovcnt, UIO_FASTIOV,
ac34ebb3 379 iovstack_l, &iov_l);
fcf63409
CY
380 if (rc <= 0)
381 goto free_iovecs;
382
ac34ebb3
CY
383 rc = rw_copy_check_uvector(CHECK_IOVEC_ONLY, rvec, riovcnt, UIO_FASTIOV,
384 iovstack_r, &iov_r);
fcf63409
CY
385 if (rc <= 0)
386 goto free_iovecs;
387
388 rc = process_vm_rw_core(pid, iov_l, liovcnt, iov_r, riovcnt, flags,
389 vm_write);
390
391free_iovecs:
392 if (iov_r != iovstack_r)
393 kfree(iov_r);
394 if (iov_l != iovstack_l)
395 kfree(iov_l);
396
397 return rc;
398}
399
400SYSCALL_DEFINE6(process_vm_readv, pid_t, pid, const struct iovec __user *, lvec,
401 unsigned long, liovcnt, const struct iovec __user *, rvec,
402 unsigned long, riovcnt, unsigned long, flags)
403{
404 return process_vm_rw(pid, lvec, liovcnt, rvec, riovcnt, flags, 0);
405}
406
407SYSCALL_DEFINE6(process_vm_writev, pid_t, pid,
408 const struct iovec __user *, lvec,
409 unsigned long, liovcnt, const struct iovec __user *, rvec,
410 unsigned long, riovcnt, unsigned long, flags)
411{
412 return process_vm_rw(pid, lvec, liovcnt, rvec, riovcnt, flags, 1);
413}
414
415#ifdef CONFIG_COMPAT
416
417asmlinkage ssize_t
418compat_process_vm_rw(compat_pid_t pid,
419 const struct compat_iovec __user *lvec,
420 unsigned long liovcnt,
421 const struct compat_iovec __user *rvec,
422 unsigned long riovcnt,
423 unsigned long flags, int vm_write)
424{
425 struct iovec iovstack_l[UIO_FASTIOV];
426 struct iovec iovstack_r[UIO_FASTIOV];
427 struct iovec *iov_l = iovstack_l;
428 struct iovec *iov_r = iovstack_r;
429 ssize_t rc = -EFAULT;
430
431 if (flags != 0)
432 return -EINVAL;
433
fcf63409
CY
434 if (vm_write)
435 rc = compat_rw_copy_check_uvector(WRITE, lvec, liovcnt,
436 UIO_FASTIOV, iovstack_l,
ac34ebb3 437 &iov_l);
fcf63409
CY
438 else
439 rc = compat_rw_copy_check_uvector(READ, lvec, liovcnt,
440 UIO_FASTIOV, iovstack_l,
ac34ebb3 441 &iov_l);
fcf63409
CY
442 if (rc <= 0)
443 goto free_iovecs;
ac34ebb3 444 rc = compat_rw_copy_check_uvector(CHECK_IOVEC_ONLY, rvec, riovcnt,
fcf63409 445 UIO_FASTIOV, iovstack_r,
ac34ebb3 446 &iov_r);
fcf63409
CY
447 if (rc <= 0)
448 goto free_iovecs;
449
450 rc = process_vm_rw_core(pid, iov_l, liovcnt, iov_r, riovcnt, flags,
451 vm_write);
452
453free_iovecs:
454 if (iov_r != iovstack_r)
455 kfree(iov_r);
456 if (iov_l != iovstack_l)
457 kfree(iov_l);
fcf63409
CY
458 return rc;
459}
460
461asmlinkage ssize_t
462compat_sys_process_vm_readv(compat_pid_t pid,
463 const struct compat_iovec __user *lvec,
464 unsigned long liovcnt,
465 const struct compat_iovec __user *rvec,
466 unsigned long riovcnt,
467 unsigned long flags)
468{
469 return compat_process_vm_rw(pid, lvec, liovcnt, rvec,
470 riovcnt, flags, 0);
471}
472
473asmlinkage ssize_t
474compat_sys_process_vm_writev(compat_pid_t pid,
475 const struct compat_iovec __user *lvec,
476 unsigned long liovcnt,
477 const struct compat_iovec __user *rvec,
478 unsigned long riovcnt,
479 unsigned long flags)
480{
481 return compat_process_vm_rw(pid, lvec, liovcnt, rvec,
482 riovcnt, flags, 1);
483}
484
485#endif
This page took 0.1692 seconds and 5 git commands to generate.