2 * linux/mm/process_vm_access.c
4 * Copyright (C) 2010-2011 Christopher Yeoh <cyeoh@au1.ibm.com>, IBM Corp.
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.
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>
21 #include <linux/compat.h>
25 * process_vm_rw_pages - read/write pages from task specified
26 * @task: task to read/write from
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
42 static int process_vm_rw_pages(struct task_struct *task,
44 struct page **process_pages,
46 unsigned long start_offset,
48 const struct iovec **iovp,
49 unsigned long lvec_cnt,
50 unsigned long *lvec_current,
53 unsigned int nr_pages_to_copy,
54 ssize_t *bytes_copied)
61 ssize_t bytes_to_copy;
63 const struct iovec *iov = *iovp;
67 /* Get the pages we're interested in */
68 down_read(&mm->mmap_sem);
69 pages_pinned = get_user_pages(task, mm, pa,
71 vm_write, 0, process_pages, NULL);
72 up_read(&mm->mmap_sem);
74 if (pages_pinned != nr_pages_to_copy) {
79 /* Do the copy for each page */
81 (pgs_copied < nr_pages_to_copy) && (*lvec_current < lvec_cnt);
83 /* Make sure we have a non zero length iovec */
84 while (*lvec_current < lvec_cnt
85 && iov->iov_len == 0) {
89 if (*lvec_current == lvec_cnt)
93 * Will copy smallest of:
94 * - bytes remaining in page
95 * - bytes remaining in destination iovec
97 bytes_to_copy = min_t(ssize_t, PAGE_SIZE - start_offset,
99 bytes_to_copy = min_t(ssize_t, bytes_to_copy,
103 target_kaddr = kmap(process_pages[pgs_copied]) + start_offset;
106 ret = copy_from_user(target_kaddr,
111 ret = copy_to_user(iov->iov_base
113 target_kaddr, bytes_to_copy);
114 kunmap(process_pages[pgs_copied]);
116 *bytes_copied += bytes_to_copy - ret;
121 *bytes_copied += bytes_to_copy;
122 *lvec_offset += bytes_to_copy;
123 if (*lvec_offset == iov->iov_len) {
125 * Need to copy remaining part of page into the
126 * next iovec if there are any bytes left in page
131 start_offset = (start_offset + bytes_to_copy)
142 for (j = 0; j < pages_pinned; j++) {
144 set_page_dirty_lock(process_pages[j]);
145 put_page(process_pages[j]);
148 for (j = 0; j < pages_pinned; j++)
149 put_page(process_pages[j]);
156 /* Maximum number of pages kmalloc'd to hold struct page's during copy */
157 #define PVM_MAX_KMALLOC_PAGES (PAGE_SIZE * 2)
160 * process_vm_rw_single_vec - read/write pages from task specified
161 * @addr: start memory address of target process
162 * @len: size of area to copy to/from
163 * @lvec: iovec array specifying where to copy to/from locally
164 * @lvec_cnt: number of elements in iovec array
165 * @lvec_current: index in iovec array we are up to
166 * @lvec_offset: offset in bytes from current iovec iov_base we are up to
167 * @process_pages: struct pages area that can store at least
168 * nr_pages_to_copy struct page pointers
170 * @task: task to read/write from
171 * @vm_write: 0 means copy from, 1 means copy to
172 * @bytes_copied: returns number of bytes successfully copied
173 * Returns 0 on success or on failure error code
175 static int process_vm_rw_single_vec(unsigned long addr,
177 const struct iovec **iovp,
178 unsigned long lvec_cnt,
179 unsigned long *lvec_current,
181 struct page **process_pages,
182 struct mm_struct *mm,
183 struct task_struct *task,
185 ssize_t *bytes_copied)
187 unsigned long pa = addr & PAGE_MASK;
188 unsigned long start_offset = addr - pa;
189 unsigned long nr_pages;
190 ssize_t bytes_copied_loop;
192 unsigned long nr_pages_copied = 0;
193 unsigned long nr_pages_to_copy;
194 unsigned long max_pages_per_loop = PVM_MAX_KMALLOC_PAGES
195 / sizeof(struct pages *);
199 /* Work out address and page range required */
202 nr_pages = (addr + len - 1) / PAGE_SIZE - addr / PAGE_SIZE + 1;
204 while ((nr_pages_copied < nr_pages) && (*lvec_current < lvec_cnt)) {
205 nr_pages_to_copy = min(nr_pages - nr_pages_copied,
208 rc = process_vm_rw_pages(task, mm, process_pages, pa,
211 lvec_current, lvec_offset,
212 vm_write, nr_pages_to_copy,
215 *bytes_copied += bytes_copied_loop;
220 len -= bytes_copied_loop;
221 nr_pages_copied += nr_pages_to_copy;
222 pa += nr_pages_to_copy * PAGE_SIZE;
229 /* Maximum number of entries for process pages array
230 which lives on stack */
231 #define PVM_MAX_PP_ARRAY_COUNT 16
234 * process_vm_rw_core - core of reading/writing pages from task specified
235 * @pid: PID of process to read/write from/to
236 * @lvec: iovec array specifying where to copy to/from locally
237 * @liovcnt: size of lvec array
238 * @rvec: iovec array specifying where to copy to/from in the other process
239 * @riovcnt: size of rvec array
240 * @flags: currently unused
241 * @vm_write: 0 if reading from other process, 1 if writing to other process
242 * Returns the number of bytes read/written or error code. May
243 * return less bytes than expected if an error occurs during the copying
246 static ssize_t process_vm_rw_core(pid_t pid, const struct iovec *lvec,
247 unsigned long liovcnt,
248 const struct iovec *rvec,
249 unsigned long riovcnt,
250 unsigned long flags, int vm_write)
252 struct task_struct *task;
253 struct page *pp_stack[PVM_MAX_PP_ARRAY_COUNT];
254 struct page **process_pages = pp_stack;
255 struct mm_struct *mm;
258 ssize_t bytes_copied_loop;
259 ssize_t bytes_copied = 0;
260 unsigned long nr_pages = 0;
261 unsigned long nr_pages_iov;
262 unsigned long iov_l_curr_idx = 0;
263 size_t iov_l_curr_offset = 0;
267 * Work out how many pages of struct pages we're going to need
268 * when eventually calling get_user_pages
270 for (i = 0; i < riovcnt; i++) {
271 iov_len = rvec[i].iov_len;
273 nr_pages_iov = ((unsigned long)rvec[i].iov_base
275 / PAGE_SIZE - (unsigned long)rvec[i].iov_base
277 nr_pages = max(nr_pages, nr_pages_iov);
284 if (nr_pages > PVM_MAX_PP_ARRAY_COUNT) {
285 /* For reliability don't try to kmalloc more than
287 process_pages = kmalloc(min_t(size_t, PVM_MAX_KMALLOC_PAGES,
288 sizeof(struct pages *)*nr_pages),
295 /* Get process information */
297 task = find_task_by_vpid(pid);
299 get_task_struct(task);
303 goto free_proc_pages;
306 mm = mm_access(task, PTRACE_MODE_ATTACH);
307 if (!mm || IS_ERR(mm)) {
308 rc = IS_ERR(mm) ? PTR_ERR(mm) : -ESRCH;
310 * Explicitly map EACCES to EPERM as EPERM is a more a
311 * appropriate error code for process_vw_readv/writev
315 goto put_task_struct;
318 for (i = 0; i < riovcnt && iov_l_curr_idx < liovcnt; i++) {
319 rc = process_vm_rw_single_vec(
320 (unsigned long)rvec[i].iov_base, rvec[i].iov_len,
321 &lvec, liovcnt, &iov_l_curr_idx, &iov_l_curr_offset,
322 process_pages, mm, task, vm_write, &bytes_copied_loop);
323 bytes_copied += bytes_copied_loop;
325 /* If we have managed to copy any data at all then
326 we return the number of bytes copied. Otherwise
327 we return the error code */
339 put_task_struct(task);
342 if (process_pages != pp_stack)
343 kfree(process_pages);
348 * process_vm_rw - check iovecs before calling core routine
349 * @pid: PID of process to read/write from/to
350 * @lvec: iovec array specifying where to copy to/from locally
351 * @liovcnt: size of lvec array
352 * @rvec: iovec array specifying where to copy to/from in the other process
353 * @riovcnt: size of rvec array
354 * @flags: currently unused
355 * @vm_write: 0 if reading from other process, 1 if writing to other process
356 * Returns the number of bytes read/written or error code. May
357 * return less bytes than expected if an error occurs during the copying
360 static ssize_t process_vm_rw(pid_t pid,
361 const struct iovec __user *lvec,
362 unsigned long liovcnt,
363 const struct iovec __user *rvec,
364 unsigned long riovcnt,
365 unsigned long flags, int vm_write)
367 struct iovec iovstack_l[UIO_FASTIOV];
368 struct iovec iovstack_r[UIO_FASTIOV];
369 struct iovec *iov_l = iovstack_l;
370 struct iovec *iov_r = iovstack_r;
378 rc = rw_copy_check_uvector(WRITE, lvec, liovcnt, UIO_FASTIOV,
381 rc = rw_copy_check_uvector(READ, lvec, liovcnt, UIO_FASTIOV,
386 rc = rw_copy_check_uvector(CHECK_IOVEC_ONLY, rvec, riovcnt, UIO_FASTIOV,
391 rc = process_vm_rw_core(pid, iov_l, liovcnt, iov_r, riovcnt, flags,
395 if (iov_r != iovstack_r)
397 if (iov_l != iovstack_l)
403 SYSCALL_DEFINE6(process_vm_readv, pid_t, pid, const struct iovec __user *, lvec,
404 unsigned long, liovcnt, const struct iovec __user *, rvec,
405 unsigned long, riovcnt, unsigned long, flags)
407 return process_vm_rw(pid, lvec, liovcnt, rvec, riovcnt, flags, 0);
410 SYSCALL_DEFINE6(process_vm_writev, pid_t, pid,
411 const struct iovec __user *, lvec,
412 unsigned long, liovcnt, const struct iovec __user *, rvec,
413 unsigned long, riovcnt, unsigned long, flags)
415 return process_vm_rw(pid, lvec, liovcnt, rvec, riovcnt, flags, 1);
421 compat_process_vm_rw(compat_pid_t pid,
422 const struct compat_iovec __user *lvec,
423 unsigned long liovcnt,
424 const struct compat_iovec __user *rvec,
425 unsigned long riovcnt,
426 unsigned long flags, int vm_write)
428 struct iovec iovstack_l[UIO_FASTIOV];
429 struct iovec iovstack_r[UIO_FASTIOV];
430 struct iovec *iov_l = iovstack_l;
431 struct iovec *iov_r = iovstack_r;
432 ssize_t rc = -EFAULT;
438 rc = compat_rw_copy_check_uvector(WRITE, lvec, liovcnt,
439 UIO_FASTIOV, iovstack_l,
442 rc = compat_rw_copy_check_uvector(READ, lvec, liovcnt,
443 UIO_FASTIOV, iovstack_l,
447 rc = compat_rw_copy_check_uvector(CHECK_IOVEC_ONLY, rvec, riovcnt,
448 UIO_FASTIOV, iovstack_r,
453 rc = process_vm_rw_core(pid, iov_l, liovcnt, iov_r, riovcnt, flags,
457 if (iov_r != iovstack_r)
459 if (iov_l != iovstack_l)
465 compat_sys_process_vm_readv(compat_pid_t pid,
466 const struct compat_iovec __user *lvec,
467 unsigned long liovcnt,
468 const struct compat_iovec __user *rvec,
469 unsigned long riovcnt,
472 return compat_process_vm_rw(pid, lvec, liovcnt, rvec,
477 compat_sys_process_vm_writev(compat_pid_t pid,
478 const struct compat_iovec __user *lvec,
479 unsigned long liovcnt,
480 const struct compat_iovec __user *rvec,
481 unsigned long riovcnt,
484 return compat_process_vm_rw(pid, lvec, liovcnt, rvec,