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 page **pages,
45 struct iov_iter *iter,
47 unsigned int nr_pages_to_copy,
48 ssize_t *bytes_copied)
52 /* Do the copy for each page */
53 while (iov_iter_count(iter) && nr_pages_to_copy--) {
54 struct page *page = *pages++;
55 size_t copy = min_t(ssize_t, PAGE_SIZE - offset, len);
59 if (copy > iov_iter_count(iter))
60 copy = iov_iter_count(iter);
61 copied = iov_iter_copy_from_user(page, iter,
63 iov_iter_advance(iter, copied);
64 set_page_dirty_lock(page);
66 copied = copy_page_to_iter(page, offset, copy, iter);
68 *bytes_copied += copied;
70 if (copied < copy && iov_iter_count(iter))
77 /* Maximum number of pages kmalloc'd to hold struct page's during copy */
78 #define PVM_MAX_KMALLOC_PAGES (PAGE_SIZE * 2)
81 * process_vm_rw_single_vec - read/write pages from task specified
82 * @addr: start memory address of target process
83 * @len: size of area to copy to/from
84 * @lvec: iovec array specifying where to copy to/from locally
85 * @lvec_cnt: number of elements in iovec array
86 * @lvec_current: index in iovec array we are up to
87 * @lvec_offset: offset in bytes from current iovec iov_base we are up to
88 * @process_pages: struct pages area that can store at least
89 * nr_pages_to_copy struct page pointers
91 * @task: task to read/write from
92 * @vm_write: 0 means copy from, 1 means copy to
93 * @bytes_copied: returns number of bytes successfully copied
94 * Returns 0 on success or on failure error code
96 static int process_vm_rw_single_vec(unsigned long addr,
98 struct iov_iter *iter,
99 struct page **process_pages,
100 struct mm_struct *mm,
101 struct task_struct *task,
103 ssize_t *bytes_copied)
105 unsigned long pa = addr & PAGE_MASK;
106 unsigned long start_offset = addr - pa;
107 unsigned long nr_pages;
108 ssize_t bytes_copied_loop;
110 unsigned long nr_pages_copied = 0;
111 unsigned long max_pages_per_loop = PVM_MAX_KMALLOC_PAGES
112 / sizeof(struct pages *);
116 /* Work out address and page range required */
119 nr_pages = (addr + len - 1) / PAGE_SIZE - addr / PAGE_SIZE + 1;
121 while ((nr_pages_copied < nr_pages) && iov_iter_count(iter)) {
122 int nr_pages_to_copy;
124 nr_pages_to_copy = min(nr_pages - nr_pages_copied,
127 /* Get the pages we're interested in */
128 down_read(&mm->mmap_sem);
129 pages_pinned = get_user_pages(task, mm, pa,
131 vm_write, 0, process_pages, NULL);
132 up_read(&mm->mmap_sem);
134 if (pages_pinned <= 0)
137 rc = process_vm_rw_pages(process_pages,
138 start_offset, len, iter,
139 vm_write, pages_pinned,
142 *bytes_copied += bytes_copied_loop;
143 len -= bytes_copied_loop;
144 nr_pages_copied += pages_pinned;
145 pa += pages_pinned * PAGE_SIZE;
147 put_page(process_pages[--pages_pinned]);
156 /* Maximum number of entries for process pages array
157 which lives on stack */
158 #define PVM_MAX_PP_ARRAY_COUNT 16
161 * process_vm_rw_core - core of reading/writing pages from task specified
162 * @pid: PID of process to read/write from/to
163 * @lvec: iovec array specifying where to copy to/from locally
164 * @liovcnt: size of lvec array
165 * @rvec: iovec array specifying where to copy to/from in the other process
166 * @riovcnt: size of rvec array
167 * @flags: currently unused
168 * @vm_write: 0 if reading from other process, 1 if writing to other process
169 * Returns the number of bytes read/written or error code. May
170 * return less bytes than expected if an error occurs during the copying
173 static ssize_t process_vm_rw_core(pid_t pid, struct iov_iter *iter,
174 const struct iovec *rvec,
175 unsigned long riovcnt,
176 unsigned long flags, int vm_write)
178 struct task_struct *task;
179 struct page *pp_stack[PVM_MAX_PP_ARRAY_COUNT];
180 struct page **process_pages = pp_stack;
181 struct mm_struct *mm;
184 ssize_t bytes_copied_loop;
185 ssize_t bytes_copied = 0;
186 unsigned long nr_pages = 0;
187 unsigned long nr_pages_iov;
191 * Work out how many pages of struct pages we're going to need
192 * when eventually calling get_user_pages
194 for (i = 0; i < riovcnt; i++) {
195 iov_len = rvec[i].iov_len;
197 nr_pages_iov = ((unsigned long)rvec[i].iov_base
199 / PAGE_SIZE - (unsigned long)rvec[i].iov_base
201 nr_pages = max(nr_pages, nr_pages_iov);
208 if (nr_pages > PVM_MAX_PP_ARRAY_COUNT) {
209 /* For reliability don't try to kmalloc more than
211 process_pages = kmalloc(min_t(size_t, PVM_MAX_KMALLOC_PAGES,
212 sizeof(struct pages *)*nr_pages),
219 /* Get process information */
221 task = find_task_by_vpid(pid);
223 get_task_struct(task);
227 goto free_proc_pages;
230 mm = mm_access(task, PTRACE_MODE_ATTACH);
231 if (!mm || IS_ERR(mm)) {
232 rc = IS_ERR(mm) ? PTR_ERR(mm) : -ESRCH;
234 * Explicitly map EACCES to EPERM as EPERM is a more a
235 * appropriate error code for process_vw_readv/writev
239 goto put_task_struct;
242 for (i = 0; i < riovcnt && iov_iter_count(iter); i++) {
243 rc = process_vm_rw_single_vec(
244 (unsigned long)rvec[i].iov_base, rvec[i].iov_len,
245 iter, process_pages, mm, task, vm_write,
247 bytes_copied += bytes_copied_loop;
249 /* If we have managed to copy any data at all then
250 we return the number of bytes copied. Otherwise
251 we return the error code */
263 put_task_struct(task);
266 if (process_pages != pp_stack)
267 kfree(process_pages);
272 * process_vm_rw - check iovecs before calling core routine
273 * @pid: PID of process to read/write from/to
274 * @lvec: iovec array specifying where to copy to/from locally
275 * @liovcnt: size of lvec array
276 * @rvec: iovec array specifying where to copy to/from in the other process
277 * @riovcnt: size of rvec array
278 * @flags: currently unused
279 * @vm_write: 0 if reading from other process, 1 if writing to other process
280 * Returns the number of bytes read/written or error code. May
281 * return less bytes than expected if an error occurs during the copying
284 static ssize_t process_vm_rw(pid_t pid,
285 const struct iovec __user *lvec,
286 unsigned long liovcnt,
287 const struct iovec __user *rvec,
288 unsigned long riovcnt,
289 unsigned long flags, int vm_write)
291 struct iovec iovstack_l[UIO_FASTIOV];
292 struct iovec iovstack_r[UIO_FASTIOV];
293 struct iovec *iov_l = iovstack_l;
294 struct iovec *iov_r = iovstack_r;
295 struct iov_iter iter;
303 rc = rw_copy_check_uvector(WRITE, lvec, liovcnt, UIO_FASTIOV,
306 rc = rw_copy_check_uvector(READ, lvec, liovcnt, UIO_FASTIOV,
311 iov_iter_init(&iter, iov_l, liovcnt, rc, 0);
313 rc = rw_copy_check_uvector(CHECK_IOVEC_ONLY, rvec, riovcnt, UIO_FASTIOV,
318 rc = process_vm_rw_core(pid, &iter, iov_r, riovcnt, flags, vm_write);
321 if (iov_r != iovstack_r)
323 if (iov_l != iovstack_l)
329 SYSCALL_DEFINE6(process_vm_readv, pid_t, pid, const struct iovec __user *, lvec,
330 unsigned long, liovcnt, const struct iovec __user *, rvec,
331 unsigned long, riovcnt, unsigned long, flags)
333 return process_vm_rw(pid, lvec, liovcnt, rvec, riovcnt, flags, 0);
336 SYSCALL_DEFINE6(process_vm_writev, pid_t, pid,
337 const struct iovec __user *, lvec,
338 unsigned long, liovcnt, const struct iovec __user *, rvec,
339 unsigned long, riovcnt, unsigned long, flags)
341 return process_vm_rw(pid, lvec, liovcnt, rvec, riovcnt, flags, 1);
347 compat_process_vm_rw(compat_pid_t pid,
348 const struct compat_iovec __user *lvec,
349 unsigned long liovcnt,
350 const struct compat_iovec __user *rvec,
351 unsigned long riovcnt,
352 unsigned long flags, int vm_write)
354 struct iovec iovstack_l[UIO_FASTIOV];
355 struct iovec iovstack_r[UIO_FASTIOV];
356 struct iovec *iov_l = iovstack_l;
357 struct iovec *iov_r = iovstack_r;
358 struct iov_iter iter;
359 ssize_t rc = -EFAULT;
365 rc = compat_rw_copy_check_uvector(WRITE, lvec, liovcnt,
366 UIO_FASTIOV, iovstack_l,
369 rc = compat_rw_copy_check_uvector(READ, lvec, liovcnt,
370 UIO_FASTIOV, iovstack_l,
374 iov_iter_init(&iter, iov_l, liovcnt, rc, 0);
375 rc = compat_rw_copy_check_uvector(CHECK_IOVEC_ONLY, rvec, riovcnt,
376 UIO_FASTIOV, iovstack_r,
381 rc = process_vm_rw_core(pid, &iter, iov_r, riovcnt, flags, vm_write);
384 if (iov_r != iovstack_r)
386 if (iov_l != iovstack_l)
392 compat_sys_process_vm_readv(compat_pid_t pid,
393 const struct compat_iovec __user *lvec,
394 unsigned long liovcnt,
395 const struct compat_iovec __user *rvec,
396 unsigned long riovcnt,
399 return compat_process_vm_rw(pid, lvec, liovcnt, rvec,
404 compat_sys_process_vm_writev(compat_pid_t pid,
405 const struct compat_iovec __user *lvec,
406 unsigned long liovcnt,
407 const struct compat_iovec __user *rvec,
408 unsigned long riovcnt,
411 return compat_process_vm_rw(pid, lvec, liovcnt, rvec,