]> git.karo-electronics.de Git - karo-tx-linux.git/blob - mm/process_vm_access.c
5ac8abc0d09bcbc25ef178e6003012454aefbf67
[karo-tx-linux.git] / mm / process_vm_access.c
1 /*
2  * linux/mm/process_vm_access.c
3  *
4  * Copyright (C) 2010-2011 Christopher Yeoh <cyeoh@au1.ibm.com>, IBM Corp.
5  *
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.
10  */
11
12 #include <linux/mm.h>
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>
19
20 #ifdef CONFIG_COMPAT
21 #include <linux/compat.h>
22 #endif
23
24 /**
25  * process_vm_rw_pages - read/write pages from task specified
26  * @task: task to read/write from
27  * @mm: mm for task
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
41  */
42 static int process_vm_rw_pages(struct task_struct *task,
43                                struct mm_struct *mm,
44                                struct page **process_pages,
45                                unsigned long pa,
46                                unsigned long start_offset,
47                                unsigned long len,
48                                const struct iovec *lvec,
49                                unsigned long lvec_cnt,
50                                unsigned long *lvec_current,
51                                size_t *lvec_offset,
52                                int vm_write,
53                                unsigned int nr_pages_to_copy,
54                                ssize_t *bytes_copied)
55 {
56         int pages_pinned;
57         void *target_kaddr;
58         int pgs_copied = 0;
59         int j;
60         int ret;
61         ssize_t bytes_to_copy;
62         ssize_t rc = 0;
63
64         *bytes_copied = 0;
65
66         /* Get the pages we're interested in */
67         down_read(&mm->mmap_sem);
68         pages_pinned = get_user_pages(task, mm, pa,
69                                       nr_pages_to_copy,
70                                       vm_write, 0, process_pages, NULL);
71         up_read(&mm->mmap_sem);
72
73         if (pages_pinned != nr_pages_to_copy) {
74                 rc = -EFAULT;
75                 goto end;
76         }
77
78         /* Do the copy for each page */
79         for (pgs_copied = 0;
80              (pgs_copied < nr_pages_to_copy) && (*lvec_current < lvec_cnt);
81              pgs_copied++) {
82                 /* Make sure we have a non zero length iovec */
83                 while (*lvec_current < lvec_cnt
84                        && lvec[*lvec_current].iov_len == 0)
85                         (*lvec_current)++;
86                 if (*lvec_current == lvec_cnt)
87                         break;
88
89                 /*
90                  * Will copy smallest of:
91                  * - bytes remaining in page
92                  * - bytes remaining in destination iovec
93                  */
94                 bytes_to_copy = min_t(ssize_t, PAGE_SIZE - start_offset,
95                                       len - *bytes_copied);
96                 bytes_to_copy = min_t(ssize_t, bytes_to_copy,
97                                       lvec[*lvec_current].iov_len
98                                       - *lvec_offset);
99
100                 target_kaddr = kmap(process_pages[pgs_copied]) + start_offset;
101
102                 if (vm_write)
103                         ret = copy_from_user(target_kaddr,
104                                              lvec[*lvec_current].iov_base
105                                              + *lvec_offset,
106                                              bytes_to_copy);
107                 else
108                         ret = copy_to_user(lvec[*lvec_current].iov_base
109                                            + *lvec_offset,
110                                            target_kaddr, bytes_to_copy);
111                 kunmap(process_pages[pgs_copied]);
112                 if (ret) {
113                         *bytes_copied += bytes_to_copy - ret;
114                         pgs_copied++;
115                         rc = -EFAULT;
116                         goto end;
117                 }
118                 *bytes_copied += bytes_to_copy;
119                 *lvec_offset += bytes_to_copy;
120                 if (*lvec_offset == lvec[*lvec_current].iov_len) {
121                         /*
122                          * Need to copy remaining part of page into the
123                          * next iovec if there are any bytes left in page
124                          */
125                         (*lvec_current)++;
126                         *lvec_offset = 0;
127                         start_offset = (start_offset + bytes_to_copy)
128                                 % PAGE_SIZE;
129                         if (start_offset)
130                                 pgs_copied--;
131                 } else {
132                         start_offset = 0;
133                 }
134         }
135
136 end:
137         if (vm_write) {
138                 for (j = 0; j < pages_pinned; j++) {
139                         if (j < pgs_copied)
140                                 set_page_dirty_lock(process_pages[j]);
141                         put_page(process_pages[j]);
142                 }
143         } else {
144                 for (j = 0; j < pages_pinned; j++)
145                         put_page(process_pages[j]);
146         }
147
148         return rc;
149 }
150
151 /* Maximum number of pages kmalloc'd to hold struct page's during copy */
152 #define PVM_MAX_KMALLOC_PAGES (PAGE_SIZE * 2)
153
154 /**
155  * process_vm_rw_single_vec - read/write pages from task specified
156  * @addr: start memory address of target process
157  * @len: size of area to copy to/from
158  * @lvec: iovec array specifying where to copy to/from locally
159  * @lvec_cnt: number of elements in iovec array
160  * @lvec_current: index in iovec array we are up to
161  * @lvec_offset: offset in bytes from current iovec iov_base we are up to
162  * @process_pages: struct pages area that can store at least
163  *  nr_pages_to_copy struct page pointers
164  * @mm: mm for task
165  * @task: task to read/write from
166  * @vm_write: 0 means copy from, 1 means copy to
167  * @bytes_copied: returns number of bytes successfully copied
168  * Returns 0 on success or on failure error code
169  */
170 static int process_vm_rw_single_vec(unsigned long addr,
171                                     unsigned long len,
172                                     const struct iovec *lvec,
173                                     unsigned long lvec_cnt,
174                                     unsigned long *lvec_current,
175                                     size_t *lvec_offset,
176                                     struct page **process_pages,
177                                     struct mm_struct *mm,
178                                     struct task_struct *task,
179                                     int vm_write,
180                                     ssize_t *bytes_copied)
181 {
182         unsigned long pa = addr & PAGE_MASK;
183         unsigned long start_offset = addr - pa;
184         unsigned long nr_pages;
185         ssize_t bytes_copied_loop;
186         ssize_t rc = 0;
187         unsigned long nr_pages_copied = 0;
188         unsigned long nr_pages_to_copy;
189         unsigned long max_pages_per_loop = PVM_MAX_KMALLOC_PAGES
190                 / sizeof(struct pages *);
191
192         *bytes_copied = 0;
193
194         /* Work out address and page range required */
195         if (len == 0)
196                 return 0;
197         nr_pages = (addr + len - 1) / PAGE_SIZE - addr / PAGE_SIZE + 1;
198
199         while ((nr_pages_copied < nr_pages) && (*lvec_current < lvec_cnt)) {
200                 nr_pages_to_copy = min(nr_pages - nr_pages_copied,
201                                        max_pages_per_loop);
202
203                 rc = process_vm_rw_pages(task, mm, process_pages, pa,
204                                          start_offset, len,
205                                          lvec, lvec_cnt,
206                                          lvec_current, lvec_offset,
207                                          vm_write, nr_pages_to_copy,
208                                          &bytes_copied_loop);
209                 start_offset = 0;
210                 *bytes_copied += bytes_copied_loop;
211
212                 if (rc < 0) {
213                         return rc;
214                 } else {
215                         len -= bytes_copied_loop;
216                         nr_pages_copied += nr_pages_to_copy;
217                         pa += nr_pages_to_copy * PAGE_SIZE;
218                 }
219         }
220
221         return rc;
222 }
223
224 /**
225  * process_vm_rw_core - core of reading/writing pages from task specified
226  * @pid: PID of process to read/write from/to
227  * @lvec: iovec array specifying where to copy to/from locally
228  * @liovcnt: size of lvec array
229  * @rvec: iovec array specifying where to copy to/from in the other process
230  * @riovcnt: size of rvec array
231  * @flags: currently unused
232  * @vm_write: 0 if reading from other process, 1 if writing to other process
233  * Returns the number of bytes read/written or error code. May
234  *  return less bytes than expected if an error occurs during the copying
235  *  process.
236  */
237 static ssize_t process_vm_rw_core(pid_t pid, const struct iovec *lvec,
238                                   unsigned long liovcnt,
239                                   const struct iovec *rvec,
240                                   unsigned long riovcnt,
241                                   unsigned long flags, int vm_write)
242 {
243         struct task_struct *task;
244         struct page **process_pages = NULL;
245         struct mm_struct *mm;
246         unsigned long i;
247         ssize_t rc = 0;
248         ssize_t bytes_copied_loop;
249         ssize_t bytes_copied = 0;
250         unsigned long nr_pages = 0;
251         unsigned long nr_pages_iov;
252         unsigned long iov_l_curr_idx = 0;
253         size_t iov_l_curr_offset = 0;
254         ssize_t iov_len;
255
256         /*
257          * Work out how many pages of struct pages we're going to need
258          * when eventually calling get_user_pages
259          */
260         for (i = 0; i < riovcnt; i++) {
261                 iov_len = rvec[i].iov_len;
262                 if (iov_len > 0) {
263                         nr_pages_iov = ((unsigned long)rvec[i].iov_base
264                                         + iov_len)
265                                 / PAGE_SIZE - (unsigned long)rvec[i].iov_base
266                                 / PAGE_SIZE + 1;
267                         nr_pages = max(nr_pages, nr_pages_iov);
268                 }
269         }
270
271         if (nr_pages == 0)
272                 return 0;
273
274         /* For reliability don't try to kmalloc more than 2 pages worth */
275         process_pages = kmalloc(min_t(size_t, PVM_MAX_KMALLOC_PAGES,
276                                       sizeof(struct pages *)*nr_pages),
277                                 GFP_KERNEL);
278
279         if (!process_pages)
280                 return -ENOMEM;
281
282         /* Get process information */
283         rcu_read_lock();
284         task = find_task_by_vpid(pid);
285         if (task)
286                 get_task_struct(task);
287         rcu_read_unlock();
288         if (!task) {
289                 rc = -ESRCH;
290                 goto free_proc_pages;
291         }
292
293         task_lock(task);
294         if (__ptrace_may_access(task, PTRACE_MODE_ATTACH)) {
295                 task_unlock(task);
296                 rc = -EPERM;
297                 goto put_task_struct;
298         }
299         mm = task->mm;
300
301         if (!mm || (task->flags & PF_KTHREAD)) {
302                 task_unlock(task);
303                 rc = -EINVAL;
304                 goto put_task_struct;
305         }
306
307         atomic_inc(&mm->mm_users);
308         task_unlock(task);
309
310         for (i = 0; i < riovcnt && iov_l_curr_idx < liovcnt; i++) {
311                 rc = process_vm_rw_single_vec(
312                         (unsigned long)rvec[i].iov_base, rvec[i].iov_len,
313                         lvec, liovcnt, &iov_l_curr_idx, &iov_l_curr_offset,
314                         process_pages, mm, task, vm_write, &bytes_copied_loop);
315                 bytes_copied += bytes_copied_loop;
316                 if (rc != 0) {
317                         /* If we have managed to copy any data at all then
318                            we return the number of bytes copied. Otherwise
319                            we return the error code */
320                         if (bytes_copied)
321                                 rc = bytes_copied;
322                         goto put_mm;
323                 }
324         }
325
326         rc = bytes_copied;
327 put_mm:
328         mmput(mm);
329
330 put_task_struct:
331         put_task_struct(task);
332
333 free_proc_pages:
334         kfree(process_pages);
335         return rc;
336 }
337
338 /**
339  * process_vm_rw - check iovecs before calling core routine
340  * @pid: PID of process to read/write from/to
341  * @lvec: iovec array specifying where to copy to/from locally
342  * @liovcnt: size of lvec array
343  * @rvec: iovec array specifying where to copy to/from in the other process
344  * @riovcnt: size of rvec array
345  * @flags: currently unused
346  * @vm_write: 0 if reading from other process, 1 if writing to other process
347  * Returns the number of bytes read/written or error code. May
348  *  return less bytes than expected if an error occurs during the copying
349  *  process.
350  */
351 static ssize_t process_vm_rw(pid_t pid,
352                              const struct iovec __user *lvec,
353                              unsigned long liovcnt,
354                              const struct iovec __user *rvec,
355                              unsigned long riovcnt,
356                              unsigned long flags, int vm_write)
357 {
358         struct iovec iovstack_l[UIO_FASTIOV];
359         struct iovec iovstack_r[UIO_FASTIOV];
360         struct iovec *iov_l = iovstack_l;
361         struct iovec *iov_r = iovstack_r;
362         ssize_t rc;
363
364         if (flags != 0)
365                 return -EINVAL;
366
367         /* Check iovecs */
368         if (vm_write)
369                 rc = rw_copy_check_uvector(WRITE, lvec, liovcnt, UIO_FASTIOV,
370                                            iovstack_l, &iov_l, 1);
371         else
372                 rc = rw_copy_check_uvector(READ, lvec, liovcnt, UIO_FASTIOV,
373                                            iovstack_l, &iov_l, 1);
374         if (rc <= 0)
375                 goto free_iovecs;
376
377         rc = rw_copy_check_uvector(READ, rvec, riovcnt, UIO_FASTIOV,
378                                    iovstack_r, &iov_r, 0);
379         if (rc <= 0)
380                 goto free_iovecs;
381
382         rc = process_vm_rw_core(pid, iov_l, liovcnt, iov_r, riovcnt, flags,
383                                 vm_write);
384
385 free_iovecs:
386         if (iov_r != iovstack_r)
387                 kfree(iov_r);
388         if (iov_l != iovstack_l)
389                 kfree(iov_l);
390
391         return rc;
392 }
393
394 SYSCALL_DEFINE6(process_vm_readv, pid_t, pid, const struct iovec __user *, lvec,
395                 unsigned long, liovcnt, const struct iovec __user *, rvec,
396                 unsigned long, riovcnt, unsigned long, flags)
397 {
398         return process_vm_rw(pid, lvec, liovcnt, rvec, riovcnt, flags, 0);
399 }
400
401 SYSCALL_DEFINE6(process_vm_writev, pid_t, pid,
402                 const struct iovec __user *, lvec,
403                 unsigned long, liovcnt, const struct iovec __user *, rvec,
404                 unsigned long, riovcnt, unsigned long, flags)
405 {
406         return process_vm_rw(pid, lvec, liovcnt, rvec, riovcnt, flags, 1);
407 }
408
409 #ifdef CONFIG_COMPAT
410
411 asmlinkage ssize_t
412 compat_process_vm_rw(compat_pid_t pid,
413                      const struct compat_iovec __user *lvec,
414                      unsigned long liovcnt,
415                      const struct compat_iovec __user *rvec,
416                      unsigned long riovcnt,
417                      unsigned long flags, int vm_write)
418 {
419         struct iovec iovstack_l[UIO_FASTIOV];
420         struct iovec iovstack_r[UIO_FASTIOV];
421         struct iovec *iov_l = iovstack_l;
422         struct iovec *iov_r = iovstack_r;
423         ssize_t rc = -EFAULT;
424
425         if (flags != 0)
426                 return -EINVAL;
427
428         if (!access_ok(VERIFY_READ, lvec, liovcnt * sizeof(*lvec)))
429                 goto out;
430
431         if (!access_ok(VERIFY_READ, rvec, riovcnt * sizeof(*rvec)))
432                 goto out;
433
434         if (vm_write)
435                 rc = compat_rw_copy_check_uvector(WRITE, lvec, liovcnt,
436                                                   UIO_FASTIOV, iovstack_l,
437                                                   &iov_l, 1);
438         else
439                 rc = compat_rw_copy_check_uvector(READ, lvec, liovcnt,
440                                                   UIO_FASTIOV, iovstack_l,
441                                                   &iov_l, 1);
442         if (rc <= 0)
443                 goto free_iovecs;
444         rc = compat_rw_copy_check_uvector(READ, rvec, riovcnt,
445                                           UIO_FASTIOV, iovstack_r,
446                                           &iov_r, 0);
447         if (rc <= 0)
448                 goto free_iovecs;
449
450         rc = process_vm_rw_core(pid, iov_l, liovcnt, iov_r, riovcnt, flags,
451                            vm_write);
452
453 free_iovecs:
454         if (iov_r != iovstack_r)
455                 kfree(iov_r);
456         if (iov_l != iovstack_l)
457                 kfree(iov_l);
458
459 out:
460         return rc;
461 }
462
463 asmlinkage ssize_t
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,
469                             unsigned long flags)
470 {
471         return compat_process_vm_rw(pid, lvec, liovcnt, rvec,
472                                     riovcnt, flags, 0);
473 }
474
475 asmlinkage ssize_t
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,
481                              unsigned long flags)
482 {
483         return compat_process_vm_rw(pid, lvec, liovcnt, rvec,
484                                     riovcnt, flags, 1);
485 }
486
487 #endif