lib: crc32: constify crc32 lookup table
[linux-2.6-block.git] / mm / util.c
CommitLineData
16d69265 1#include <linux/mm.h>
30992c97
MM
2#include <linux/slab.h>
3#include <linux/string.h>
3b32123d 4#include <linux/compiler.h>
b95f1b31 5#include <linux/export.h>
96840aa0 6#include <linux/err.h>
3b8f14b4 7#include <linux/sched.h>
eb36c587 8#include <linux/security.h>
9800339b 9#include <linux/swap.h>
33806f06 10#include <linux/swapops.h>
00619bcc
JM
11#include <linux/mman.h>
12#include <linux/hugetlb.h>
39f1f78d 13#include <linux/vmalloc.h>
00619bcc 14
96840aa0 15#include <asm/uaccess.h>
30992c97 16
6038def0
NK
17#include "internal.h"
18
30992c97 19/**
30992c97 20 * kstrdup - allocate space for and copy an existing string
30992c97
MM
21 * @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;
1d2c8eea 33 buf = kmalloc_track_caller(len, gfp);
30992c97
MM
34 if (buf)
35 memcpy(buf, s, len);
36 return buf;
37}
38EXPORT_SYMBOL(kstrdup);
96840aa0 39
1e66df3e
JF
40/**
41 * 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
1a2f67b4
AD
64/**
65 * 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
1d2c8eea 75 p = kmalloc_track_caller(len, gfp);
1a2f67b4
AD
76 if (p)
77 memcpy(p, src, len);
78 return p;
79}
80EXPORT_SYMBOL(kmemdup);
81
610a77e0
LZ
82/**
83 * 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
96840aa0
DA
112/*
113 * strndup_user - duplicate an existing string from user space
96840aa0
DA
114 * @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
90d74045 130 p = memdup_user(s, length);
96840aa0 131
90d74045
JL
132 if (IS_ERR(p))
133 return p;
96840aa0
DA
134
135 p[length - 1] = '\0';
136
137 return p;
138}
139EXPORT_SYMBOL(strndup_user);
16d69265 140
6038def0
NK
141void __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
b7643757
SP
163/* 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
58cb6548
ON
173 * just check in the current task. Returns the task_struct of the task
174 * that the vma is stack for. Must be called under rcu_read_lock().
b7643757 175 */
58cb6548
ON
176struct task_struct *task_of_stack(struct task_struct *task,
177 struct vm_area_struct *vma, bool in_group)
b7643757 178{
b7643757 179 if (vm_is_stack_for_task(task, vma))
58cb6548 180 return task;
b7643757
SP
181
182 if (in_group) {
183 struct task_struct *t;
b7643757 184
4449a51a 185 for_each_thread(task, t) {
58cb6548
ON
186 if (vm_is_stack_for_task(t, vma))
187 return t;
4449a51a 188 }
b7643757
SP
189 }
190
58cb6548 191 return NULL;
b7643757
SP
192}
193
efc1a3b1 194#if defined(CONFIG_MMU) && !defined(HAVE_ARCH_PICK_MMAP_LAYOUT)
16d69265
AM
195void arch_pick_mmap_layout(struct mm_struct *mm)
196{
197 mm->mmap_base = TASK_UNMAPPED_BASE;
198 mm->get_unmapped_area = arch_get_unmapped_area;
16d69265
AM
199}
200#endif
912985dc 201
45888a0c
XG
202/*
203 * Like get_user_pages_fast() except its IRQ-safe in that it won't fall
204 * back to the regular GUP.
25985edc 205 * If the architecture not support this function, simply return with no
45888a0c
XG
206 * page pinned
207 */
3b32123d 208int __weak __get_user_pages_fast(unsigned long start,
45888a0c
XG
209 int nr_pages, int write, struct page **pages)
210{
211 return 0;
212}
213EXPORT_SYMBOL_GPL(__get_user_pages_fast);
214
9de100d0
AG
215/**
216 * get_user_pages_fast() - pin user pages in memory
217 * @start: starting user address
218 * @nr_pages: number of pages from start to pin
219 * @write: whether pages will be written to
220 * @pages: array that receives pointers to the pages pinned.
221 * Should be at least nr_pages long.
222 *
9de100d0
AG
223 * Returns number of pages pinned. This may be fewer than the number
224 * requested. If nr_pages is 0 or negative, returns 0. If no pages
225 * were pinned, returns -errno.
d2bf6be8
NP
226 *
227 * get_user_pages_fast provides equivalent functionality to get_user_pages,
228 * operating on current and current->mm, with force=0 and vma=NULL. However
229 * unlike get_user_pages, it must be called without mmap_sem held.
230 *
231 * get_user_pages_fast may take mmap_sem and page table locks, so no
232 * assumptions can be made about lack of locking. get_user_pages_fast is to be
233 * implemented in a way that is advantageous (vs get_user_pages()) when the
234 * user memory area is already faulted in and present in ptes. However if the
235 * pages have to be faulted in, it may turn out to be slightly slower so
236 * callers need to carefully consider what to use. On many architectures,
237 * get_user_pages_fast simply falls back to get_user_pages.
9de100d0 238 */
3b32123d 239int __weak get_user_pages_fast(unsigned long start,
912985dc
RR
240 int nr_pages, int write, struct page **pages)
241{
242 struct mm_struct *mm = current->mm;
a7b78075
AA
243 return get_user_pages_unlocked(current, mm, start, nr_pages,
244 write, 0, pages);
912985dc
RR
245}
246EXPORT_SYMBOL_GPL(get_user_pages_fast);
ca2b84cb 247
eb36c587
AV
248unsigned long vm_mmap_pgoff(struct file *file, unsigned long addr,
249 unsigned long len, unsigned long prot,
250 unsigned long flag, unsigned long pgoff)
251{
252 unsigned long ret;
253 struct mm_struct *mm = current->mm;
41badc15 254 unsigned long populate;
eb36c587
AV
255
256 ret = security_mmap_file(file, prot, flag);
257 if (!ret) {
258 down_write(&mm->mmap_sem);
bebeb3d6
ML
259 ret = do_mmap_pgoff(file, addr, len, prot, flag, pgoff,
260 &populate);
eb36c587 261 up_write(&mm->mmap_sem);
41badc15
ML
262 if (populate)
263 mm_populate(ret, populate);
eb36c587
AV
264 }
265 return ret;
266}
267
268unsigned long vm_mmap(struct file *file, unsigned long addr,
269 unsigned long len, unsigned long prot,
270 unsigned long flag, unsigned long offset)
271{
272 if (unlikely(offset + PAGE_ALIGN(len) < offset))
273 return -EINVAL;
274 if (unlikely(offset & ~PAGE_MASK))
275 return -EINVAL;
276
277 return vm_mmap_pgoff(file, addr, len, prot, flag, offset >> PAGE_SHIFT);
278}
279EXPORT_SYMBOL(vm_mmap);
280
39f1f78d
AV
281void kvfree(const void *addr)
282{
283 if (is_vmalloc_addr(addr))
284 vfree(addr);
285 else
286 kfree(addr);
287}
288EXPORT_SYMBOL(kvfree);
289
9800339b
SL
290struct address_space *page_mapping(struct page *page)
291{
292 struct address_space *mapping = page->mapping;
293
03e5ac2f
MP
294 /* This happens if someone calls flush_dcache_page on slab page */
295 if (unlikely(PageSlab(page)))
296 return NULL;
297
33806f06
SL
298 if (unlikely(PageSwapCache(page))) {
299 swp_entry_t entry;
300
301 entry.val = page_private(page);
302 mapping = swap_address_space(entry);
d2cf5ad6 303 } else if ((unsigned long)mapping & PAGE_MAPPING_ANON)
9800339b
SL
304 mapping = NULL;
305 return mapping;
306}
307
49f0ce5f
JM
308int overcommit_ratio_handler(struct ctl_table *table, int write,
309 void __user *buffer, size_t *lenp,
310 loff_t *ppos)
311{
312 int ret;
313
314 ret = proc_dointvec(table, write, buffer, lenp, ppos);
315 if (ret == 0 && write)
316 sysctl_overcommit_kbytes = 0;
317 return ret;
318}
319
320int overcommit_kbytes_handler(struct ctl_table *table, int write,
321 void __user *buffer, size_t *lenp,
322 loff_t *ppos)
323{
324 int ret;
325
326 ret = proc_doulongvec_minmax(table, write, buffer, lenp, ppos);
327 if (ret == 0 && write)
328 sysctl_overcommit_ratio = 0;
329 return ret;
330}
331
00619bcc
JM
332/*
333 * Committed memory limit enforced when OVERCOMMIT_NEVER policy is used
334 */
335unsigned long vm_commit_limit(void)
336{
49f0ce5f
JM
337 unsigned long allowed;
338
339 if (sysctl_overcommit_kbytes)
340 allowed = sysctl_overcommit_kbytes >> (PAGE_SHIFT - 10);
341 else
342 allowed = ((totalram_pages - hugetlb_total_pages())
343 * sysctl_overcommit_ratio / 100);
344 allowed += total_swap_pages;
345
346 return allowed;
00619bcc
JM
347}
348
a9090253
WR
349/**
350 * get_cmdline() - copy the cmdline value to a buffer.
351 * @task: the task whose cmdline value to copy.
352 * @buffer: the buffer to copy to.
353 * @buflen: the length of the buffer. Larger cmdline values are truncated
354 * to this length.
355 * Returns the size of the cmdline field copied. Note that the copy does
356 * not guarantee an ending NULL byte.
357 */
358int get_cmdline(struct task_struct *task, char *buffer, int buflen)
359{
360 int res = 0;
361 unsigned int len;
362 struct mm_struct *mm = get_task_mm(task);
363 if (!mm)
364 goto out;
365 if (!mm->arg_end)
366 goto out_mm; /* Shh! No looking before we're done */
367
368 len = mm->arg_end - mm->arg_start;
369
370 if (len > buflen)
371 len = buflen;
372
373 res = access_process_vm(task, mm->arg_start, buffer, len, 0);
374
375 /*
376 * If the nul at the end of args has been overwritten, then
377 * assume application is using setproctitle(3).
378 */
379 if (res > 0 && buffer[res-1] != '\0' && len < buflen) {
380 len = strnlen(buffer, res);
381 if (len < res) {
382 res = len;
383 } else {
384 len = mm->env_end - mm->env_start;
385 if (len > buflen - res)
386 len = buflen - res;
387 res += access_process_vm(task, mm->env_start,
388 buffer+res, len, 0);
389 res = strnlen(buffer, res);
390 }
391 }
392out_mm:
393 mmput(mm);
394out:
395 return res;
396}