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 *lvec,
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 = lvec + *lvec_current;
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]);
155 /* Maximum number of pages kmalloc'd to hold struct page's during copy */
156 #define PVM_MAX_KMALLOC_PAGES (PAGE_SIZE * 2)
159 * process_vm_rw_single_vec - read/write pages from task specified
160 * @addr: start memory address of target process
161 * @len: size of area to copy to/from
162 * @lvec: iovec array specifying where to copy to/from locally
163 * @lvec_cnt: number of elements in iovec array
164 * @lvec_current: index in iovec array we are up to
165 * @lvec_offset: offset in bytes from current iovec iov_base we are up to
166 * @process_pages: struct pages area that can store at least
167 * nr_pages_to_copy struct page pointers
169 * @task: task to read/write from
170 * @vm_write: 0 means copy from, 1 means copy to
171 * @bytes_copied: returns number of bytes successfully copied
172 * Returns 0 on success or on failure error code
174 static int process_vm_rw_single_vec(unsigned long addr,
176 const struct iovec *lvec,
177 unsigned long lvec_cnt,
178 unsigned long *lvec_current,
180 struct page **process_pages,
181 struct mm_struct *mm,
182 struct task_struct *task,
184 ssize_t *bytes_copied)
186 unsigned long pa = addr & PAGE_MASK;
187 unsigned long start_offset = addr - pa;
188 unsigned long nr_pages;
189 ssize_t bytes_copied_loop;
191 unsigned long nr_pages_copied = 0;
192 unsigned long nr_pages_to_copy;
193 unsigned long max_pages_per_loop = PVM_MAX_KMALLOC_PAGES
194 / sizeof(struct pages *);
198 /* Work out address and page range required */
201 nr_pages = (addr + len - 1) / PAGE_SIZE - addr / PAGE_SIZE + 1;
203 while ((nr_pages_copied < nr_pages) && (*lvec_current < lvec_cnt)) {
204 nr_pages_to_copy = min(nr_pages - nr_pages_copied,
207 rc = process_vm_rw_pages(task, mm, process_pages, pa,
210 lvec_current, lvec_offset,
211 vm_write, nr_pages_to_copy,
214 *bytes_copied += bytes_copied_loop;
219 len -= bytes_copied_loop;
220 nr_pages_copied += nr_pages_to_copy;
221 pa += nr_pages_to_copy * PAGE_SIZE;
228 /* Maximum number of entries for process pages array
229 which lives on stack */
230 #define PVM_MAX_PP_ARRAY_COUNT 16
233 * process_vm_rw_core - core of reading/writing pages from task specified
234 * @pid: PID of process to read/write from/to
235 * @lvec: iovec array specifying where to copy to/from locally
236 * @liovcnt: size of lvec array
237 * @rvec: iovec array specifying where to copy to/from in the other process
238 * @riovcnt: size of rvec array
239 * @flags: currently unused
240 * @vm_write: 0 if reading from other process, 1 if writing to other process
241 * Returns the number of bytes read/written or error code. May
242 * return less bytes than expected if an error occurs during the copying
245 static ssize_t process_vm_rw_core(pid_t pid, const struct iovec *lvec,
246 unsigned long liovcnt,
247 const struct iovec *rvec,
248 unsigned long riovcnt,
249 unsigned long flags, int vm_write)
251 struct task_struct *task;
252 struct page *pp_stack[PVM_MAX_PP_ARRAY_COUNT];
253 struct page **process_pages = pp_stack;
254 struct mm_struct *mm;
257 ssize_t bytes_copied_loop;
258 ssize_t bytes_copied = 0;
259 unsigned long nr_pages = 0;
260 unsigned long nr_pages_iov;
261 unsigned long iov_l_curr_idx = 0;
262 size_t iov_l_curr_offset = 0;
266 * Work out how many pages of struct pages we're going to need
267 * when eventually calling get_user_pages
269 for (i = 0; i < riovcnt; i++) {
270 iov_len = rvec[i].iov_len;
272 nr_pages_iov = ((unsigned long)rvec[i].iov_base
274 / PAGE_SIZE - (unsigned long)rvec[i].iov_base
276 nr_pages = max(nr_pages, nr_pages_iov);
283 if (nr_pages > PVM_MAX_PP_ARRAY_COUNT) {
284 /* For reliability don't try to kmalloc more than
286 process_pages = kmalloc(min_t(size_t, PVM_MAX_KMALLOC_PAGES,
287 sizeof(struct pages *)*nr_pages),
294 /* Get process information */
296 task = find_task_by_vpid(pid);
298 get_task_struct(task);
302 goto free_proc_pages;
305 mm = mm_access(task, PTRACE_MODE_ATTACH);
306 if (!mm || IS_ERR(mm)) {
307 rc = IS_ERR(mm) ? PTR_ERR(mm) : -ESRCH;
309 * Explicitly map EACCES to EPERM as EPERM is a more a
310 * appropriate error code for process_vw_readv/writev
314 goto put_task_struct;
317 for (i = 0; i < riovcnt && iov_l_curr_idx < liovcnt; i++) {
318 rc = process_vm_rw_single_vec(
319 (unsigned long)rvec[i].iov_base, rvec[i].iov_len,
320 lvec, liovcnt, &iov_l_curr_idx, &iov_l_curr_offset,
321 process_pages, mm, task, vm_write, &bytes_copied_loop);
322 bytes_copied += bytes_copied_loop;
324 /* If we have managed to copy any data at all then
325 we return the number of bytes copied. Otherwise
326 we return the error code */
338 put_task_struct(task);
341 if (process_pages != pp_stack)
342 kfree(process_pages);
347 * process_vm_rw - check iovecs before calling core routine
348 * @pid: PID of process to read/write from/to
349 * @lvec: iovec array specifying where to copy to/from locally
350 * @liovcnt: size of lvec array
351 * @rvec: iovec array specifying where to copy to/from in the other process
352 * @riovcnt: size of rvec array
353 * @flags: currently unused
354 * @vm_write: 0 if reading from other process, 1 if writing to other process
355 * Returns the number of bytes read/written or error code. May
356 * return less bytes than expected if an error occurs during the copying
359 static ssize_t process_vm_rw(pid_t pid,
360 const struct iovec __user *lvec,
361 unsigned long liovcnt,
362 const struct iovec __user *rvec,
363 unsigned long riovcnt,
364 unsigned long flags, int vm_write)
366 struct iovec iovstack_l[UIO_FASTIOV];
367 struct iovec iovstack_r[UIO_FASTIOV];
368 struct iovec *iov_l = iovstack_l;
369 struct iovec *iov_r = iovstack_r;
377 rc = rw_copy_check_uvector(WRITE, lvec, liovcnt, UIO_FASTIOV,
380 rc = rw_copy_check_uvector(READ, lvec, liovcnt, UIO_FASTIOV,
385 rc = rw_copy_check_uvector(CHECK_IOVEC_ONLY, rvec, riovcnt, UIO_FASTIOV,
390 rc = process_vm_rw_core(pid, iov_l, liovcnt, iov_r, riovcnt, flags,
394 if (iov_r != iovstack_r)
396 if (iov_l != iovstack_l)
402 SYSCALL_DEFINE6(process_vm_readv, pid_t, pid, const struct iovec __user *, lvec,
403 unsigned long, liovcnt, const struct iovec __user *, rvec,
404 unsigned long, riovcnt, unsigned long, flags)
406 return process_vm_rw(pid, lvec, liovcnt, rvec, riovcnt, flags, 0);
409 SYSCALL_DEFINE6(process_vm_writev, pid_t, pid,
410 const struct iovec __user *, lvec,
411 unsigned long, liovcnt, const struct iovec __user *, rvec,
412 unsigned long, riovcnt, unsigned long, flags)
414 return process_vm_rw(pid, lvec, liovcnt, rvec, riovcnt, flags, 1);
420 compat_process_vm_rw(compat_pid_t pid,
421 const struct compat_iovec __user *lvec,
422 unsigned long liovcnt,
423 const struct compat_iovec __user *rvec,
424 unsigned long riovcnt,
425 unsigned long flags, int vm_write)
427 struct iovec iovstack_l[UIO_FASTIOV];
428 struct iovec iovstack_r[UIO_FASTIOV];
429 struct iovec *iov_l = iovstack_l;
430 struct iovec *iov_r = iovstack_r;
431 ssize_t rc = -EFAULT;
437 rc = compat_rw_copy_check_uvector(WRITE, lvec, liovcnt,
438 UIO_FASTIOV, iovstack_l,
441 rc = compat_rw_copy_check_uvector(READ, lvec, liovcnt,
442 UIO_FASTIOV, iovstack_l,
446 rc = compat_rw_copy_check_uvector(CHECK_IOVEC_ONLY, rvec, riovcnt,
447 UIO_FASTIOV, iovstack_r,
452 rc = process_vm_rw_core(pid, iov_l, liovcnt, iov_r, riovcnt, flags,
456 if (iov_r != iovstack_r)
458 if (iov_l != iovstack_l)
464 compat_sys_process_vm_readv(compat_pid_t pid,
465 const struct compat_iovec __user *lvec,
466 unsigned long liovcnt,
467 const struct compat_iovec __user *rvec,
468 unsigned long riovcnt,
471 return compat_process_vm_rw(pid, lvec, liovcnt, rvec,
476 compat_sys_process_vm_writev(compat_pid_t pid,
477 const struct compat_iovec __user *lvec,
478 unsigned long liovcnt,
479 const struct compat_iovec __user *rvec,
480 unsigned long riovcnt,
483 return compat_process_vm_rw(pid, lvec, liovcnt, rvec,