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
*lvec
,
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
*);
196 const struct iovec
*iov
= lvec
+ *lvec_current
;
200 /* Work out address and page range required */
203 nr_pages
= (addr
+ len
- 1) / PAGE_SIZE
- addr
/ PAGE_SIZE
+ 1;
205 while ((nr_pages_copied
< nr_pages
) && (*lvec_current
< lvec_cnt
)) {
206 nr_pages_to_copy
= min(nr_pages
- nr_pages_copied
,
209 rc
= process_vm_rw_pages(task
, mm
, process_pages
, pa
,
212 lvec_current
, lvec_offset
,
213 vm_write
, nr_pages_to_copy
,
216 *bytes_copied
+= bytes_copied_loop
;
221 len
-= bytes_copied_loop
;
222 nr_pages_copied
+= nr_pages_to_copy
;
223 pa
+= nr_pages_to_copy
* PAGE_SIZE
;
230 /* Maximum number of entries for process pages array
231 which lives on stack */
232 #define PVM_MAX_PP_ARRAY_COUNT 16
235 * process_vm_rw_core - core of reading/writing pages from task specified
236 * @pid: PID of process to read/write from/to
237 * @lvec: iovec array specifying where to copy to/from locally
238 * @liovcnt: size of lvec array
239 * @rvec: iovec array specifying where to copy to/from in the other process
240 * @riovcnt: size of rvec array
241 * @flags: currently unused
242 * @vm_write: 0 if reading from other process, 1 if writing to other process
243 * Returns the number of bytes read/written or error code. May
244 * return less bytes than expected if an error occurs during the copying
247 static ssize_t
process_vm_rw_core(pid_t pid
, const struct iovec
*lvec
,
248 unsigned long liovcnt
,
249 const struct iovec
*rvec
,
250 unsigned long riovcnt
,
251 unsigned long flags
, int vm_write
)
253 struct task_struct
*task
;
254 struct page
*pp_stack
[PVM_MAX_PP_ARRAY_COUNT
];
255 struct page
**process_pages
= pp_stack
;
256 struct mm_struct
*mm
;
259 ssize_t bytes_copied_loop
;
260 ssize_t bytes_copied
= 0;
261 unsigned long nr_pages
= 0;
262 unsigned long nr_pages_iov
;
263 unsigned long iov_l_curr_idx
= 0;
264 size_t iov_l_curr_offset
= 0;
268 * Work out how many pages of struct pages we're going to need
269 * when eventually calling get_user_pages
271 for (i
= 0; i
< riovcnt
; i
++) {
272 iov_len
= rvec
[i
].iov_len
;
274 nr_pages_iov
= ((unsigned long)rvec
[i
].iov_base
276 / PAGE_SIZE
- (unsigned long)rvec
[i
].iov_base
278 nr_pages
= max(nr_pages
, nr_pages_iov
);
285 if (nr_pages
> PVM_MAX_PP_ARRAY_COUNT
) {
286 /* For reliability don't try to kmalloc more than
288 process_pages
= kmalloc(min_t(size_t, PVM_MAX_KMALLOC_PAGES
,
289 sizeof(struct pages
*)*nr_pages
),
296 /* Get process information */
298 task
= find_task_by_vpid(pid
);
300 get_task_struct(task
);
304 goto free_proc_pages
;
307 mm
= mm_access(task
, PTRACE_MODE_ATTACH
);
308 if (!mm
|| IS_ERR(mm
)) {
309 rc
= IS_ERR(mm
) ? PTR_ERR(mm
) : -ESRCH
;
311 * Explicitly map EACCES to EPERM as EPERM is a more a
312 * appropriate error code for process_vw_readv/writev
316 goto put_task_struct
;
319 for (i
= 0; i
< riovcnt
&& iov_l_curr_idx
< liovcnt
; i
++) {
320 rc
= process_vm_rw_single_vec(
321 (unsigned long)rvec
[i
].iov_base
, rvec
[i
].iov_len
,
322 lvec
, liovcnt
, &iov_l_curr_idx
, &iov_l_curr_offset
,
323 process_pages
, mm
, task
, vm_write
, &bytes_copied_loop
);
324 bytes_copied
+= bytes_copied_loop
;
326 /* If we have managed to copy any data at all then
327 we return the number of bytes copied. Otherwise
328 we return the error code */
340 put_task_struct(task
);
343 if (process_pages
!= pp_stack
)
344 kfree(process_pages
);
349 * process_vm_rw - check iovecs before calling core routine
350 * @pid: PID of process to read/write from/to
351 * @lvec: iovec array specifying where to copy to/from locally
352 * @liovcnt: size of lvec array
353 * @rvec: iovec array specifying where to copy to/from in the other process
354 * @riovcnt: size of rvec array
355 * @flags: currently unused
356 * @vm_write: 0 if reading from other process, 1 if writing to other process
357 * Returns the number of bytes read/written or error code. May
358 * return less bytes than expected if an error occurs during the copying
361 static ssize_t
process_vm_rw(pid_t pid
,
362 const struct iovec __user
*lvec
,
363 unsigned long liovcnt
,
364 const struct iovec __user
*rvec
,
365 unsigned long riovcnt
,
366 unsigned long flags
, int vm_write
)
368 struct iovec iovstack_l
[UIO_FASTIOV
];
369 struct iovec iovstack_r
[UIO_FASTIOV
];
370 struct iovec
*iov_l
= iovstack_l
;
371 struct iovec
*iov_r
= iovstack_r
;
379 rc
= rw_copy_check_uvector(WRITE
, lvec
, liovcnt
, UIO_FASTIOV
,
382 rc
= rw_copy_check_uvector(READ
, lvec
, liovcnt
, UIO_FASTIOV
,
387 rc
= rw_copy_check_uvector(CHECK_IOVEC_ONLY
, rvec
, riovcnt
, UIO_FASTIOV
,
392 rc
= process_vm_rw_core(pid
, iov_l
, liovcnt
, iov_r
, riovcnt
, flags
,
396 if (iov_r
!= iovstack_r
)
398 if (iov_l
!= iovstack_l
)
404 SYSCALL_DEFINE6(process_vm_readv
, pid_t
, pid
, const struct iovec __user
*, lvec
,
405 unsigned long, liovcnt
, const struct iovec __user
*, rvec
,
406 unsigned long, riovcnt
, unsigned long, flags
)
408 return process_vm_rw(pid
, lvec
, liovcnt
, rvec
, riovcnt
, flags
, 0);
411 SYSCALL_DEFINE6(process_vm_writev
, pid_t
, pid
,
412 const struct iovec __user
*, lvec
,
413 unsigned long, liovcnt
, const struct iovec __user
*, rvec
,
414 unsigned long, riovcnt
, unsigned long, flags
)
416 return process_vm_rw(pid
, lvec
, liovcnt
, rvec
, riovcnt
, flags
, 1);
422 compat_process_vm_rw(compat_pid_t pid
,
423 const struct compat_iovec __user
*lvec
,
424 unsigned long liovcnt
,
425 const struct compat_iovec __user
*rvec
,
426 unsigned long riovcnt
,
427 unsigned long flags
, int vm_write
)
429 struct iovec iovstack_l
[UIO_FASTIOV
];
430 struct iovec iovstack_r
[UIO_FASTIOV
];
431 struct iovec
*iov_l
= iovstack_l
;
432 struct iovec
*iov_r
= iovstack_r
;
433 ssize_t rc
= -EFAULT
;
439 rc
= compat_rw_copy_check_uvector(WRITE
, lvec
, liovcnt
,
440 UIO_FASTIOV
, iovstack_l
,
443 rc
= compat_rw_copy_check_uvector(READ
, lvec
, liovcnt
,
444 UIO_FASTIOV
, iovstack_l
,
448 rc
= compat_rw_copy_check_uvector(CHECK_IOVEC_ONLY
, rvec
, riovcnt
,
449 UIO_FASTIOV
, iovstack_r
,
454 rc
= process_vm_rw_core(pid
, iov_l
, liovcnt
, iov_r
, riovcnt
, flags
,
458 if (iov_r
!= iovstack_r
)
460 if (iov_l
!= iovstack_l
)
466 compat_sys_process_vm_readv(compat_pid_t pid
,
467 const struct compat_iovec __user
*lvec
,
468 unsigned long liovcnt
,
469 const struct compat_iovec __user
*rvec
,
470 unsigned long riovcnt
,
473 return compat_process_vm_rw(pid
, lvec
, liovcnt
, rvec
,
478 compat_sys_process_vm_writev(compat_pid_t pid
,
479 const struct compat_iovec __user
*lvec
,
480 unsigned long liovcnt
,
481 const struct compat_iovec __user
*rvec
,
482 unsigned long riovcnt
,
485 return compat_process_vm_rw(pid
, lvec
, liovcnt
, rvec
,