blob: 093c973f1697dd39545c6c93aeac8e9c3d60cf52 [file] [log] [blame]
Andrew Morton16d69262008-07-25 19:44:36 -07001#include <linux/mm.h>
Matt Mackall30992c92006-01-08 01:01:43 -08002#include <linux/slab.h>
3#include <linux/string.h>
Gideon Israel Dsouza3b321232014-04-07 15:37:26 -07004#include <linux/compiler.h>
Paul Gortmakerb95f1b312011-10-16 02:01:52 -04005#include <linux/export.h>
Davi Arnaut96840aa2006-03-24 03:18:42 -08006#include <linux/err.h>
Adrian Bunk3b8f14b2008-07-26 15:22:28 -07007#include <linux/sched.h>
Al Viroeb36c582012-05-30 20:17:35 -04008#include <linux/security.h>
Shaohua Li98003392013-02-22 16:34:35 -08009#include <linux/swap.h>
Shaohua Li33806f02013-02-22 16:34:37 -080010#include <linux/swapops.h>
Jerome Marchand00619bc2013-11-12 15:08:31 -080011#include <linux/mman.h>
12#include <linux/hugetlb.h>
Al Viro39f1f782014-05-06 14:02:53 -040013#include <linux/vmalloc.h>
Jerome Marchand00619bc2013-11-12 15:08:31 -080014
Davi Arnaut96840aa2006-03-24 03:18:42 -080015#include <asm/uaccess.h>
Matt Mackall30992c92006-01-08 01:01:43 -080016
Namhyung Kim6038def2011-05-24 17:11:22 -070017#include "internal.h"
18
Matt Mackall30992c92006-01-08 01:01:43 -080019/**
Matt Mackall30992c92006-01-08 01:01:43 -080020 * kstrdup - allocate space for and copy an existing string
Matt Mackall30992c92006-01-08 01:01:43 -080021 * @s: the string to duplicate
22 * @gfp: the GFP mask used in the kmalloc() call when allocating memory
23 */
24char *kstrdup(const char *s, gfp_t gfp)
25{
26 size_t len;
27 char *buf;
28
29 if (!s)
30 return NULL;
31
32 len = strlen(s) + 1;
Christoph Hellwig1d2c8ee2006-10-04 02:15:25 -070033 buf = kmalloc_track_caller(len, gfp);
Matt Mackall30992c92006-01-08 01:01:43 -080034 if (buf)
35 memcpy(buf, s, len);
36 return buf;
37}
38EXPORT_SYMBOL(kstrdup);
Davi Arnaut96840aa2006-03-24 03:18:42 -080039
Alexey Dobriyan1a2f67b2006-09-30 23:27:20 -070040/**
Jeremy Fitzhardinge1e66df32007-07-17 18:37:02 -070041 * kstrndup - allocate space for and copy an existing string
42 * @s: the string to duplicate
43 * @max: read at most @max chars from @s
44 * @gfp: the GFP mask used in the kmalloc() call when allocating memory
45 */
46char *kstrndup(const char *s, size_t max, gfp_t gfp)
47{
48 size_t len;
49 char *buf;
50
51 if (!s)
52 return NULL;
53
54 len = strnlen(s, max);
55 buf = kmalloc_track_caller(len+1, gfp);
56 if (buf) {
57 memcpy(buf, s, len);
58 buf[len] = '\0';
59 }
60 return buf;
61}
62EXPORT_SYMBOL(kstrndup);
63
64/**
Alexey Dobriyan1a2f67b2006-09-30 23:27:20 -070065 * kmemdup - duplicate region of memory
66 *
67 * @src: memory region to duplicate
68 * @len: memory region length
69 * @gfp: GFP mask to use
70 */
71void *kmemdup(const void *src, size_t len, gfp_t gfp)
72{
73 void *p;
74
Christoph Hellwig1d2c8ee2006-10-04 02:15:25 -070075 p = kmalloc_track_caller(len, gfp);
Alexey Dobriyan1a2f67b2006-09-30 23:27:20 -070076 if (p)
77 memcpy(p, src, len);
78 return p;
79}
80EXPORT_SYMBOL(kmemdup);
81
Christoph Lameteref2ad802007-07-17 04:03:21 -070082/**
Li Zefan610a77e2009-03-31 15:23:16 -070083 * memdup_user - duplicate memory region from user space
84 *
85 * @src: source address in user space
86 * @len: number of bytes to copy
87 *
88 * Returns an ERR_PTR() on failure.
89 */
90void *memdup_user(const void __user *src, size_t len)
91{
92 void *p;
93
94 /*
95 * Always use GFP_KERNEL, since copy_from_user() can sleep and
96 * cause pagefault, which makes it pointless to use GFP_NOFS
97 * or GFP_ATOMIC.
98 */
99 p = kmalloc_track_caller(len, GFP_KERNEL);
100 if (!p)
101 return ERR_PTR(-ENOMEM);
102
103 if (copy_from_user(p, src, len)) {
104 kfree(p);
105 return ERR_PTR(-EFAULT);
106 }
107
108 return p;
109}
110EXPORT_SYMBOL(memdup_user);
111
Davi Arnaut96840aa2006-03-24 03:18:42 -0800112/*
113 * strndup_user - duplicate an existing string from user space
Davi Arnaut96840aa2006-03-24 03:18:42 -0800114 * @s: The string to duplicate
115 * @n: Maximum number of bytes to copy, including the trailing NUL.
116 */
117char *strndup_user(const char __user *s, long n)
118{
119 char *p;
120 long length;
121
122 length = strnlen_user(s, n);
123
124 if (!length)
125 return ERR_PTR(-EFAULT);
126
127 if (length > n)
128 return ERR_PTR(-EINVAL);
129
Julia Lawall90d74042010-08-09 17:18:26 -0700130 p = memdup_user(s, length);
Davi Arnaut96840aa2006-03-24 03:18:42 -0800131
Julia Lawall90d74042010-08-09 17:18:26 -0700132 if (IS_ERR(p))
133 return p;
Davi Arnaut96840aa2006-03-24 03:18:42 -0800134
135 p[length - 1] = '\0';
136
137 return p;
138}
139EXPORT_SYMBOL(strndup_user);
Andrew Morton16d69262008-07-25 19:44:36 -0700140
Namhyung Kim6038def2011-05-24 17:11:22 -0700141void __vma_link_list(struct mm_struct *mm, struct vm_area_struct *vma,
142 struct vm_area_struct *prev, struct rb_node *rb_parent)
143{
144 struct vm_area_struct *next;
145
146 vma->vm_prev = prev;
147 if (prev) {
148 next = prev->vm_next;
149 prev->vm_next = vma;
150 } else {
151 mm->mmap = vma;
152 if (rb_parent)
153 next = rb_entry(rb_parent,
154 struct vm_area_struct, vm_rb);
155 else
156 next = NULL;
157 }
158 vma->vm_next = next;
159 if (next)
160 next->vm_prev = vma;
161}
162
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -0700163/* Check if the vma is being used as a stack by this task */
164static int vm_is_stack_for_task(struct task_struct *t,
165 struct vm_area_struct *vma)
166{
167 return (vma->vm_start <= KSTK_ESP(t) && vma->vm_end >= KSTK_ESP(t));
168}
169
170/*
171 * Check if the vma is being used as a stack.
172 * If is_group is non-zero, check in the entire thread group or else
173 * just check in the current task. Returns the pid of the task that
174 * the vma is stack for.
175 */
176pid_t vm_is_stack(struct task_struct *task,
177 struct vm_area_struct *vma, int in_group)
178{
179 pid_t ret = 0;
180
181 if (vm_is_stack_for_task(task, vma))
182 return task->pid;
183
184 if (in_group) {
185 struct task_struct *t;
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -0700186
Oleg Nesterov4449a512014-08-08 14:19:17 -0700187 rcu_read_lock();
188 for_each_thread(task, t) {
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -0700189 if (vm_is_stack_for_task(t, vma)) {
190 ret = t->pid;
191 goto done;
192 }
Oleg Nesterov4449a512014-08-08 14:19:17 -0700193 }
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -0700194done:
195 rcu_read_unlock();
196 }
197
198 return ret;
199}
200
David Howellsefc1a3b2010-01-15 17:01:35 -0800201#if defined(CONFIG_MMU) && !defined(HAVE_ARCH_PICK_MMAP_LAYOUT)
Andrew Morton16d69262008-07-25 19:44:36 -0700202void arch_pick_mmap_layout(struct mm_struct *mm)
203{
204 mm->mmap_base = TASK_UNMAPPED_BASE;
205 mm->get_unmapped_area = arch_get_unmapped_area;
Andrew Morton16d69262008-07-25 19:44:36 -0700206}
207#endif
Rusty Russell912985d2008-08-12 17:52:52 -0500208
Xiao Guangrong45888a02010-08-22 19:08:57 +0800209/*
210 * Like get_user_pages_fast() except its IRQ-safe in that it won't fall
211 * back to the regular GUP.
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300212 * If the architecture not support this function, simply return with no
Xiao Guangrong45888a02010-08-22 19:08:57 +0800213 * page pinned
214 */
Gideon Israel Dsouza3b321232014-04-07 15:37:26 -0700215int __weak __get_user_pages_fast(unsigned long start,
Xiao Guangrong45888a02010-08-22 19:08:57 +0800216 int nr_pages, int write, struct page **pages)
217{
218 return 0;
219}
220EXPORT_SYMBOL_GPL(__get_user_pages_fast);
221
Andy Grover9de100d2009-04-13 14:40:05 -0700222/**
223 * get_user_pages_fast() - pin user pages in memory
224 * @start: starting user address
225 * @nr_pages: number of pages from start to pin
226 * @write: whether pages will be written to
227 * @pages: array that receives pointers to the pages pinned.
228 * Should be at least nr_pages long.
229 *
Andy Grover9de100d2009-04-13 14:40:05 -0700230 * Returns number of pages pinned. This may be fewer than the number
231 * requested. If nr_pages is 0 or negative, returns 0. If no pages
232 * were pinned, returns -errno.
Nick Piggind2bf6be2009-06-16 15:31:39 -0700233 *
234 * get_user_pages_fast provides equivalent functionality to get_user_pages,
235 * operating on current and current->mm, with force=0 and vma=NULL. However
236 * unlike get_user_pages, it must be called without mmap_sem held.
237 *
238 * get_user_pages_fast may take mmap_sem and page table locks, so no
239 * assumptions can be made about lack of locking. get_user_pages_fast is to be
240 * implemented in a way that is advantageous (vs get_user_pages()) when the
241 * user memory area is already faulted in and present in ptes. However if the
242 * pages have to be faulted in, it may turn out to be slightly slower so
243 * callers need to carefully consider what to use. On many architectures,
244 * get_user_pages_fast simply falls back to get_user_pages.
Andy Grover9de100d2009-04-13 14:40:05 -0700245 */
Gideon Israel Dsouza3b321232014-04-07 15:37:26 -0700246int __weak get_user_pages_fast(unsigned long start,
Rusty Russell912985d2008-08-12 17:52:52 -0500247 int nr_pages, int write, struct page **pages)
248{
249 struct mm_struct *mm = current->mm;
250 int ret;
251
252 down_read(&mm->mmap_sem);
253 ret = get_user_pages(current, mm, start, nr_pages,
254 write, 0, pages, NULL);
255 up_read(&mm->mmap_sem);
256
257 return ret;
258}
259EXPORT_SYMBOL_GPL(get_user_pages_fast);
Eduard - Gabriel Munteanuca2b84cb2009-03-23 15:12:24 +0200260
Al Viroeb36c582012-05-30 20:17:35 -0400261unsigned long vm_mmap_pgoff(struct file *file, unsigned long addr,
262 unsigned long len, unsigned long prot,
263 unsigned long flag, unsigned long pgoff)
264{
265 unsigned long ret;
266 struct mm_struct *mm = current->mm;
Michel Lespinasse41badc12013-02-22 16:32:47 -0800267 unsigned long populate;
Al Viroeb36c582012-05-30 20:17:35 -0400268
269 ret = security_mmap_file(file, prot, flag);
270 if (!ret) {
271 down_write(&mm->mmap_sem);
Michel Lespinassebebeb3d2013-02-22 16:32:37 -0800272 ret = do_mmap_pgoff(file, addr, len, prot, flag, pgoff,
273 &populate);
Al Viroeb36c582012-05-30 20:17:35 -0400274 up_write(&mm->mmap_sem);
Michel Lespinasse41badc12013-02-22 16:32:47 -0800275 if (populate)
276 mm_populate(ret, populate);
Al Viroeb36c582012-05-30 20:17:35 -0400277 }
278 return ret;
279}
280
281unsigned long vm_mmap(struct file *file, unsigned long addr,
282 unsigned long len, unsigned long prot,
283 unsigned long flag, unsigned long offset)
284{
285 if (unlikely(offset + PAGE_ALIGN(len) < offset))
286 return -EINVAL;
287 if (unlikely(offset & ~PAGE_MASK))
288 return -EINVAL;
289
290 return vm_mmap_pgoff(file, addr, len, prot, flag, offset >> PAGE_SHIFT);
291}
292EXPORT_SYMBOL(vm_mmap);
293
Al Viro39f1f782014-05-06 14:02:53 -0400294void kvfree(const void *addr)
295{
296 if (is_vmalloc_addr(addr))
297 vfree(addr);
298 else
299 kfree(addr);
300}
301EXPORT_SYMBOL(kvfree);
302
Shaohua Li98003392013-02-22 16:34:35 -0800303struct address_space *page_mapping(struct page *page)
304{
305 struct address_space *mapping = page->mapping;
306
Mikulas Patocka03e5ac22014-01-14 17:56:40 -0800307 /* This happens if someone calls flush_dcache_page on slab page */
308 if (unlikely(PageSlab(page)))
309 return NULL;
310
Shaohua Li33806f02013-02-22 16:34:37 -0800311 if (unlikely(PageSwapCache(page))) {
312 swp_entry_t entry;
313
314 entry.val = page_private(page);
315 mapping = swap_address_space(entry);
Joonsoo Kimd2cf5ad2013-09-11 14:21:29 -0700316 } else if ((unsigned long)mapping & PAGE_MAPPING_ANON)
Shaohua Li98003392013-02-22 16:34:35 -0800317 mapping = NULL;
318 return mapping;
319}
320
Jerome Marchand49f0ce52014-01-21 15:49:14 -0800321int overcommit_ratio_handler(struct ctl_table *table, int write,
322 void __user *buffer, size_t *lenp,
323 loff_t *ppos)
324{
325 int ret;
326
327 ret = proc_dointvec(table, write, buffer, lenp, ppos);
328 if (ret == 0 && write)
329 sysctl_overcommit_kbytes = 0;
330 return ret;
331}
332
333int overcommit_kbytes_handler(struct ctl_table *table, int write,
334 void __user *buffer, size_t *lenp,
335 loff_t *ppos)
336{
337 int ret;
338
339 ret = proc_doulongvec_minmax(table, write, buffer, lenp, ppos);
340 if (ret == 0 && write)
341 sysctl_overcommit_ratio = 0;
342 return ret;
343}
344
Jerome Marchand00619bc2013-11-12 15:08:31 -0800345/*
346 * Committed memory limit enforced when OVERCOMMIT_NEVER policy is used
347 */
348unsigned long vm_commit_limit(void)
349{
Jerome Marchand49f0ce52014-01-21 15:49:14 -0800350 unsigned long allowed;
351
352 if (sysctl_overcommit_kbytes)
353 allowed = sysctl_overcommit_kbytes >> (PAGE_SHIFT - 10);
354 else
355 allowed = ((totalram_pages - hugetlb_total_pages())
356 * sysctl_overcommit_ratio / 100);
357 allowed += total_swap_pages;
358
359 return allowed;
Jerome Marchand00619bc2013-11-12 15:08:31 -0800360}
361
William Robertsa9090252014-02-11 10:11:59 -0800362/**
363 * get_cmdline() - copy the cmdline value to a buffer.
364 * @task: the task whose cmdline value to copy.
365 * @buffer: the buffer to copy to.
366 * @buflen: the length of the buffer. Larger cmdline values are truncated
367 * to this length.
368 * Returns the size of the cmdline field copied. Note that the copy does
369 * not guarantee an ending NULL byte.
370 */
371int get_cmdline(struct task_struct *task, char *buffer, int buflen)
372{
373 int res = 0;
374 unsigned int len;
375 struct mm_struct *mm = get_task_mm(task);
376 if (!mm)
377 goto out;
378 if (!mm->arg_end)
379 goto out_mm; /* Shh! No looking before we're done */
380
381 len = mm->arg_end - mm->arg_start;
382
383 if (len > buflen)
384 len = buflen;
385
386 res = access_process_vm(task, mm->arg_start, buffer, len, 0);
387
388 /*
389 * If the nul at the end of args has been overwritten, then
390 * assume application is using setproctitle(3).
391 */
392 if (res > 0 && buffer[res-1] != '\0' && len < buflen) {
393 len = strnlen(buffer, res);
394 if (len < res) {
395 res = len;
396 } else {
397 len = mm->env_end - mm->env_start;
398 if (len > buflen - res)
399 len = buflen - res;
400 res += access_process_vm(task, mm->env_start,
401 buffer+res, len, 0);
402 res = strnlen(buffer, res);
403 }
404 }
405out_mm:
406 mmput(mm);
407out:
408 return res;
409}