]>
git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blob - mm/process_vm_access.c
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
,
48 /* Do the copy for each page */
49 while (len
&& iov_iter_count(iter
)) {
50 struct page
*page
= *pages
++;
51 size_t copy
= PAGE_SIZE
- offset
;
58 if (copy
> iov_iter_count(iter
))
59 copy
= iov_iter_count(iter
);
60 copied
= iov_iter_copy_from_user(page
, iter
,
62 iov_iter_advance(iter
, copied
);
63 set_page_dirty_lock(page
);
65 copied
= copy_page_to_iter(page
, offset
, copy
, iter
);
68 if (copied
< copy
&& iov_iter_count(iter
))
75 /* Maximum number of pages kmalloc'd to hold struct page's during copy */
76 #define PVM_MAX_KMALLOC_PAGES (PAGE_SIZE * 2)
79 * process_vm_rw_single_vec - read/write pages from task specified
80 * @addr: start memory address of target process
81 * @len: size of area to copy to/from
82 * @lvec: iovec array specifying where to copy to/from locally
83 * @lvec_cnt: number of elements in iovec array
84 * @lvec_current: index in iovec array we are up to
85 * @lvec_offset: offset in bytes from current iovec iov_base we are up to
86 * @process_pages: struct pages area that can store at least
87 * nr_pages_to_copy struct page pointers
89 * @task: task to read/write from
90 * @vm_write: 0 means copy from, 1 means copy to
91 * @bytes_copied: returns number of bytes successfully copied
92 * Returns 0 on success or on failure error code
94 static int process_vm_rw_single_vec(unsigned long addr
,
96 struct iov_iter
*iter
,
97 struct page
**process_pages
,
99 struct task_struct
*task
,
102 unsigned long pa
= addr
& PAGE_MASK
;
103 unsigned long start_offset
= addr
- pa
;
104 unsigned long nr_pages
;
106 unsigned long nr_pages_copied
= 0;
107 unsigned long max_pages_per_loop
= PVM_MAX_KMALLOC_PAGES
108 / sizeof(struct pages
*);
110 /* Work out address and page range required */
113 nr_pages
= (addr
+ len
- 1) / PAGE_SIZE
- addr
/ PAGE_SIZE
+ 1;
115 while ((nr_pages_copied
< nr_pages
) && iov_iter_count(iter
)) {
116 int nr_pages_to_copy
;
119 nr_pages_to_copy
= min(nr_pages
- nr_pages_copied
,
122 /* Get the pages we're interested in */
123 down_read(&mm
->mmap_sem
);
124 pages_pinned
= get_user_pages(task
, mm
, pa
,
126 vm_write
, 0, process_pages
, NULL
);
127 up_read(&mm
->mmap_sem
);
129 if (pages_pinned
<= 0)
132 n
= pages_pinned
* PAGE_SIZE
- start_offset
;
136 rc
= process_vm_rw_pages(process_pages
,
137 start_offset
, n
, iter
,
141 nr_pages_copied
+= pages_pinned
;
142 pa
+= pages_pinned
* PAGE_SIZE
;
144 put_page(process_pages
[--pages_pinned
]);
152 /* Maximum number of entries for process pages array
153 which lives on stack */
154 #define PVM_MAX_PP_ARRAY_COUNT 16
157 * process_vm_rw_core - core of reading/writing pages from task specified
158 * @pid: PID of process to read/write from/to
159 * @lvec: iovec array specifying where to copy to/from locally
160 * @liovcnt: size of lvec array
161 * @rvec: iovec array specifying where to copy to/from in the other process
162 * @riovcnt: size of rvec array
163 * @flags: currently unused
164 * @vm_write: 0 if reading from other process, 1 if writing to other process
165 * Returns the number of bytes read/written or error code. May
166 * return less bytes than expected if an error occurs during the copying
169 static ssize_t
process_vm_rw_core(pid_t pid
, struct iov_iter
*iter
,
170 const struct iovec
*rvec
,
171 unsigned long riovcnt
,
172 unsigned long flags
, int vm_write
)
174 struct task_struct
*task
;
175 struct page
*pp_stack
[PVM_MAX_PP_ARRAY_COUNT
];
176 struct page
**process_pages
= pp_stack
;
177 struct mm_struct
*mm
;
180 unsigned long nr_pages
= 0;
181 unsigned long nr_pages_iov
;
183 size_t total_len
= iov_iter_count(iter
);
186 * Work out how many pages of struct pages we're going to need
187 * when eventually calling get_user_pages
189 for (i
= 0; i
< riovcnt
; i
++) {
190 iov_len
= rvec
[i
].iov_len
;
192 nr_pages_iov
= ((unsigned long)rvec
[i
].iov_base
194 / PAGE_SIZE
- (unsigned long)rvec
[i
].iov_base
196 nr_pages
= max(nr_pages
, nr_pages_iov
);
203 if (nr_pages
> PVM_MAX_PP_ARRAY_COUNT
) {
204 /* For reliability don't try to kmalloc more than
206 process_pages
= kmalloc(min_t(size_t, PVM_MAX_KMALLOC_PAGES
,
207 sizeof(struct pages
*)*nr_pages
),
214 /* Get process information */
216 task
= find_task_by_vpid(pid
);
218 get_task_struct(task
);
222 goto free_proc_pages
;
225 mm
= mm_access(task
, PTRACE_MODE_ATTACH
);
226 if (!mm
|| IS_ERR(mm
)) {
227 rc
= IS_ERR(mm
) ? PTR_ERR(mm
) : -ESRCH
;
229 * Explicitly map EACCES to EPERM as EPERM is a more a
230 * appropriate error code for process_vw_readv/writev
234 goto put_task_struct
;
237 for (i
= 0; i
< riovcnt
&& iov_iter_count(iter
) && !rc
; i
++)
238 rc
= process_vm_rw_single_vec(
239 (unsigned long)rvec
[i
].iov_base
, rvec
[i
].iov_len
,
240 iter
, process_pages
, mm
, task
, vm_write
);
242 /* copied = space before - space after */
243 total_len
-= iov_iter_count(iter
);
245 /* If we have managed to copy any data at all then
246 we return the number of bytes copied. Otherwise
247 we return the error code */
254 put_task_struct(task
);
257 if (process_pages
!= pp_stack
)
258 kfree(process_pages
);
263 * process_vm_rw - check iovecs before calling core routine
264 * @pid: PID of process to read/write from/to
265 * @lvec: iovec array specifying where to copy to/from locally
266 * @liovcnt: size of lvec array
267 * @rvec: iovec array specifying where to copy to/from in the other process
268 * @riovcnt: size of rvec array
269 * @flags: currently unused
270 * @vm_write: 0 if reading from other process, 1 if writing to other process
271 * Returns the number of bytes read/written or error code. May
272 * return less bytes than expected if an error occurs during the copying
275 static ssize_t
process_vm_rw(pid_t pid
,
276 const struct iovec __user
*lvec
,
277 unsigned long liovcnt
,
278 const struct iovec __user
*rvec
,
279 unsigned long riovcnt
,
280 unsigned long flags
, int vm_write
)
282 struct iovec iovstack_l
[UIO_FASTIOV
];
283 struct iovec iovstack_r
[UIO_FASTIOV
];
284 struct iovec
*iov_l
= iovstack_l
;
285 struct iovec
*iov_r
= iovstack_r
;
286 struct iov_iter iter
;
294 rc
= rw_copy_check_uvector(WRITE
, lvec
, liovcnt
, UIO_FASTIOV
,
297 rc
= rw_copy_check_uvector(READ
, lvec
, liovcnt
, UIO_FASTIOV
,
302 iov_iter_init(&iter
, iov_l
, liovcnt
, rc
, 0);
304 rc
= rw_copy_check_uvector(CHECK_IOVEC_ONLY
, rvec
, riovcnt
, UIO_FASTIOV
,
309 rc
= process_vm_rw_core(pid
, &iter
, iov_r
, riovcnt
, flags
, vm_write
);
312 if (iov_r
!= iovstack_r
)
314 if (iov_l
!= iovstack_l
)
320 SYSCALL_DEFINE6(process_vm_readv
, pid_t
, pid
, const struct iovec __user
*, lvec
,
321 unsigned long, liovcnt
, const struct iovec __user
*, rvec
,
322 unsigned long, riovcnt
, unsigned long, flags
)
324 return process_vm_rw(pid
, lvec
, liovcnt
, rvec
, riovcnt
, flags
, 0);
327 SYSCALL_DEFINE6(process_vm_writev
, pid_t
, pid
,
328 const struct iovec __user
*, lvec
,
329 unsigned long, liovcnt
, const struct iovec __user
*, rvec
,
330 unsigned long, riovcnt
, unsigned long, flags
)
332 return process_vm_rw(pid
, lvec
, liovcnt
, rvec
, riovcnt
, flags
, 1);
338 compat_process_vm_rw(compat_pid_t pid
,
339 const struct compat_iovec __user
*lvec
,
340 unsigned long liovcnt
,
341 const struct compat_iovec __user
*rvec
,
342 unsigned long riovcnt
,
343 unsigned long flags
, int vm_write
)
345 struct iovec iovstack_l
[UIO_FASTIOV
];
346 struct iovec iovstack_r
[UIO_FASTIOV
];
347 struct iovec
*iov_l
= iovstack_l
;
348 struct iovec
*iov_r
= iovstack_r
;
349 struct iov_iter iter
;
350 ssize_t rc
= -EFAULT
;
356 rc
= compat_rw_copy_check_uvector(WRITE
, lvec
, liovcnt
,
357 UIO_FASTIOV
, iovstack_l
,
360 rc
= compat_rw_copy_check_uvector(READ
, lvec
, liovcnt
,
361 UIO_FASTIOV
, iovstack_l
,
365 iov_iter_init(&iter
, iov_l
, liovcnt
, rc
, 0);
366 rc
= compat_rw_copy_check_uvector(CHECK_IOVEC_ONLY
, rvec
, riovcnt
,
367 UIO_FASTIOV
, iovstack_r
,
372 rc
= process_vm_rw_core(pid
, &iter
, iov_r
, riovcnt
, flags
, vm_write
);
375 if (iov_r
!= iovstack_r
)
377 if (iov_l
!= iovstack_l
)
383 compat_sys_process_vm_readv(compat_pid_t pid
,
384 const struct compat_iovec __user
*lvec
,
385 unsigned long liovcnt
,
386 const struct compat_iovec __user
*rvec
,
387 unsigned long riovcnt
,
390 return compat_process_vm_rw(pid
, lvec
, liovcnt
, rvec
,
395 compat_sys_process_vm_writev(compat_pid_t pid
,
396 const struct compat_iovec __user
*lvec
,
397 unsigned long liovcnt
,
398 const struct compat_iovec __user
*rvec
,
399 unsigned long riovcnt
,
402 return compat_process_vm_rw(pid
, lvec
, liovcnt
, rvec
,