~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/mm/util.c

Version: ~ [ linux-5.9.1 ] ~ [ linux-5.8.16 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.72 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.152 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.202 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.240 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.240 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.19.8 ] ~ [ linux-3.18.140 ] ~ [ linux-3.17.8 ] ~ [ linux-3.16.85 ] ~ [ linux-3.15.10 ] ~ [ linux-3.14.79 ] ~ [ linux-3.13.11 ] ~ [ linux-3.12.74 ] ~ [ linux-3.11.10 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 #include <linux/mm.h>
  2 #include <linux/slab.h>
  3 #include <linux/string.h>
  4 #include <linux/compiler.h>
  5 #include <linux/export.h>
  6 #include <linux/err.h>
  7 #include <linux/sched.h>
  8 #include <linux/security.h>
  9 #include <linux/swap.h>
 10 #include <linux/swapops.h>
 11 #include <linux/mman.h>
 12 #include <linux/hugetlb.h>
 13 #include <linux/vmalloc.h>
 14 
 15 #include <asm/uaccess.h>
 16 
 17 #include "internal.h"
 18 
 19 /**
 20  * kstrdup - allocate space for and copy an existing string
 21  * @s: the string to duplicate
 22  * @gfp: the GFP mask used in the kmalloc() call when allocating memory
 23  */
 24 char *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;
 33         buf = kmalloc_track_caller(len, gfp);
 34         if (buf)
 35                 memcpy(buf, s, len);
 36         return buf;
 37 }
 38 EXPORT_SYMBOL(kstrdup);
 39 
 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  *
 46  * Note: Use kmemdup_nul() instead if the size is known exactly.
 47  */
 48 char *kstrndup(const char *s, size_t max, gfp_t gfp)
 49 {
 50         size_t len;
 51         char *buf;
 52 
 53         if (!s)
 54                 return NULL;
 55 
 56         len = strnlen(s, max);
 57         buf = kmalloc_track_caller(len+1, gfp);
 58         if (buf) {
 59                 memcpy(buf, s, len);
 60                 buf[len] = '\0';
 61         }
 62         return buf;
 63 }
 64 EXPORT_SYMBOL(kstrndup);
 65 
 66 /**
 67  * kmemdup - duplicate region of memory
 68  *
 69  * @src: memory region to duplicate
 70  * @len: memory region length
 71  * @gfp: GFP mask to use
 72  */
 73 void *kmemdup(const void *src, size_t len, gfp_t gfp)
 74 {
 75         void *p;
 76 
 77         p = kmalloc_track_caller(len, gfp);
 78         if (p)
 79                 memcpy(p, src, len);
 80         return p;
 81 }
 82 EXPORT_SYMBOL(kmemdup);
 83 
 84 /**
 85  * kmemdup_nul - Create a NUL-terminated string from unterminated data
 86  * @s: The data to stringify
 87  * @len: The size of the data
 88  * @gfp: the GFP mask used in the kmalloc() call when allocating memory
 89  */
 90 char *kmemdup_nul(const char *s, size_t len, gfp_t gfp)
 91 {
 92         char *buf;
 93 
 94         if (!s)
 95                 return NULL;
 96 
 97         buf = kmalloc_track_caller(len + 1, gfp);
 98         if (buf) {
 99                 memcpy(buf, s, len);
100                 buf[len] = '\0';
101         }
102         return buf;
103 }
104 EXPORT_SYMBOL(kmemdup_nul);
105 
106 /**
107  * memdup_user - duplicate memory region from user space
108  *
109  * @src: source address in user space
110  * @len: number of bytes to copy
111  *
112  * Returns an ERR_PTR() on failure.
113  */
114 void *memdup_user(const void __user *src, size_t len)
115 {
116         void *p;
117 
118         /*
119          * Always use GFP_KERNEL, since copy_from_user() can sleep and
120          * cause pagefault, which makes it pointless to use GFP_NOFS
121          * or GFP_ATOMIC.
122          */
123         p = kmalloc_track_caller(len, GFP_KERNEL);
124         if (!p)
125                 return ERR_PTR(-ENOMEM);
126 
127         if (copy_from_user(p, src, len)) {
128                 kfree(p);
129                 return ERR_PTR(-EFAULT);
130         }
131 
132         return p;
133 }
134 EXPORT_SYMBOL(memdup_user);
135 
136 /*
137  * strndup_user - duplicate an existing string from user space
138  * @s: The string to duplicate
139  * @n: Maximum number of bytes to copy, including the trailing NUL.
140  */
141 char *strndup_user(const char __user *s, long n)
142 {
143         char *p;
144         long length;
145 
146         length = strnlen_user(s, n);
147 
148         if (!length)
149                 return ERR_PTR(-EFAULT);
150 
151         if (length > n)
152                 return ERR_PTR(-EINVAL);
153 
154         p = memdup_user(s, length);
155 
156         if (IS_ERR(p))
157                 return p;
158 
159         p[length - 1] = '\0';
160 
161         return p;
162 }
163 EXPORT_SYMBOL(strndup_user);
164 
165 void __vma_link_list(struct mm_struct *mm, struct vm_area_struct *vma,
166                 struct vm_area_struct *prev, struct rb_node *rb_parent)
167 {
168         struct vm_area_struct *next;
169 
170         vma->vm_prev = prev;
171         if (prev) {
172                 next = prev->vm_next;
173                 prev->vm_next = vma;
174         } else {
175                 mm->mmap = vma;
176                 if (rb_parent)
177                         next = rb_entry(rb_parent,
178                                         struct vm_area_struct, vm_rb);
179                 else
180                         next = NULL;
181         }
182         vma->vm_next = next;
183         if (next)
184                 next->vm_prev = vma;
185 }
186 
187 /* Check if the vma is being used as a stack by this task */
188 int vma_is_stack_for_task(struct vm_area_struct *vma, struct task_struct *t)
189 {
190         return (vma->vm_start <= KSTK_ESP(t) && vma->vm_end >= KSTK_ESP(t));
191 }
192 
193 #if defined(CONFIG_MMU) && !defined(HAVE_ARCH_PICK_MMAP_LAYOUT)
194 void arch_pick_mmap_layout(struct mm_struct *mm)
195 {
196         mm->mmap_base = TASK_UNMAPPED_BASE;
197         mm->get_unmapped_area = arch_get_unmapped_area;
198 }
199 #endif
200 
201 /*
202  * Like get_user_pages_fast() except its IRQ-safe in that it won't fall
203  * back to the regular GUP.
204  * If the architecture not support this function, simply return with no
205  * page pinned
206  */
207 int __weak __get_user_pages_fast(unsigned long start,
208                                  int nr_pages, int write, struct page **pages)
209 {
210         return 0;
211 }
212 EXPORT_SYMBOL_GPL(__get_user_pages_fast);
213 
214 /**
215  * get_user_pages_fast() - pin user pages in memory
216  * @start:      starting user address
217  * @nr_pages:   number of pages from start to pin
218  * @write:      whether pages will be written to
219  * @pages:      array that receives pointers to the pages pinned.
220  *              Should be at least nr_pages long.
221  *
222  * Returns number of pages pinned. This may be fewer than the number
223  * requested. If nr_pages is 0 or negative, returns 0. If no pages
224  * were pinned, returns -errno.
225  *
226  * get_user_pages_fast provides equivalent functionality to get_user_pages,
227  * operating on current and current->mm, with force=0 and vma=NULL. However
228  * unlike get_user_pages, it must be called without mmap_sem held.
229  *
230  * get_user_pages_fast may take mmap_sem and page table locks, so no
231  * assumptions can be made about lack of locking. get_user_pages_fast is to be
232  * implemented in a way that is advantageous (vs get_user_pages()) when the
233  * user memory area is already faulted in and present in ptes. However if the
234  * pages have to be faulted in, it may turn out to be slightly slower so
235  * callers need to carefully consider what to use. On many architectures,
236  * get_user_pages_fast simply falls back to get_user_pages.
237  */
238 int __weak get_user_pages_fast(unsigned long start,
239                                 int nr_pages, int write, struct page **pages)
240 {
241         struct mm_struct *mm = current->mm;
242         int ret;
243 
244         down_read(&mm->mmap_sem);
245         ret = get_user_pages(current, mm, start, nr_pages,
246                                         write, 0, pages, NULL);
247         up_read(&mm->mmap_sem);
248 
249         return ret;
250 }
251 EXPORT_SYMBOL_GPL(get_user_pages_fast);
252 
253 unsigned long vm_mmap_pgoff(struct file *file, unsigned long addr,
254         unsigned long len, unsigned long prot,
255         unsigned long flag, unsigned long pgoff)
256 {
257         unsigned long ret;
258         struct mm_struct *mm = current->mm;
259         unsigned long populate;
260 
261         ret = security_mmap_file(file, prot, flag);
262         if (!ret) {
263                 down_write(&mm->mmap_sem);
264                 ret = do_mmap_pgoff(file, addr, len, prot, flag, pgoff,
265                                     &populate);
266                 up_write(&mm->mmap_sem);
267                 if (populate)
268                         mm_populate(ret, populate);
269         }
270         return ret;
271 }
272 
273 unsigned long vm_mmap(struct file *file, unsigned long addr,
274         unsigned long len, unsigned long prot,
275         unsigned long flag, unsigned long offset)
276 {
277         if (unlikely(offset + PAGE_ALIGN(len) < offset))
278                 return -EINVAL;
279         if (unlikely(offset & ~PAGE_MASK))
280                 return -EINVAL;
281 
282         return vm_mmap_pgoff(file, addr, len, prot, flag, offset >> PAGE_SHIFT);
283 }
284 EXPORT_SYMBOL(vm_mmap);
285 
286 void kvfree(const void *addr)
287 {
288         if (is_vmalloc_addr(addr))
289                 vfree(addr);
290         else
291                 kfree(addr);
292 }
293 EXPORT_SYMBOL(kvfree);
294 
295 struct address_space *page_mapping(struct page *page)
296 {
297         struct address_space *mapping = page->mapping;
298 
299         /* This happens if someone calls flush_dcache_page on slab page */
300         if (unlikely(PageSlab(page)))
301                 return NULL;
302 
303         if (unlikely(PageSwapCache(page))) {
304                 swp_entry_t entry;
305 
306                 entry.val = page_private(page);
307                 mapping = swap_address_space(entry);
308         } else if ((unsigned long)mapping & PAGE_MAPPING_ANON)
309                 mapping = NULL;
310         return mapping;
311 }
312 
313 int overcommit_ratio_handler(struct ctl_table *table, int write,
314                              void __user *buffer, size_t *lenp,
315                              loff_t *ppos)
316 {
317         int ret;
318 
319         ret = proc_dointvec(table, write, buffer, lenp, ppos);
320         if (ret == 0 && write)
321                 sysctl_overcommit_kbytes = 0;
322         return ret;
323 }
324 
325 int overcommit_kbytes_handler(struct ctl_table *table, int write,
326                              void __user *buffer, size_t *lenp,
327                              loff_t *ppos)
328 {
329         int ret;
330 
331         ret = proc_doulongvec_minmax(table, write, buffer, lenp, ppos);
332         if (ret == 0 && write)
333                 sysctl_overcommit_ratio = 0;
334         return ret;
335 }
336 
337 /*
338  * Committed memory limit enforced when OVERCOMMIT_NEVER policy is used
339  */
340 unsigned long vm_commit_limit(void)
341 {
342         unsigned long allowed;
343 
344         if (sysctl_overcommit_kbytes)
345                 allowed = sysctl_overcommit_kbytes >> (PAGE_SHIFT - 10);
346         else
347                 allowed = ((totalram_pages - hugetlb_total_pages())
348                            * sysctl_overcommit_ratio / 100);
349         allowed += total_swap_pages;
350 
351         return allowed;
352 }
353 
354 /**
355  * get_cmdline() - copy the cmdline value to a buffer.
356  * @task:     the task whose cmdline value to copy.
357  * @buffer:   the buffer to copy to.
358  * @buflen:   the length of the buffer. Larger cmdline values are truncated
359  *            to this length.
360  * Returns the size of the cmdline field copied. Note that the copy does
361  * not guarantee an ending NULL byte.
362  */
363 int get_cmdline(struct task_struct *task, char *buffer, int buflen)
364 {
365         int res = 0;
366         unsigned int len;
367         struct mm_struct *mm = get_task_mm(task);
368         if (!mm)
369                 goto out;
370         if (!mm->arg_end)
371                 goto out_mm;    /* Shh! No looking before we're done */
372 
373         len = mm->arg_end - mm->arg_start;
374 
375         if (len > buflen)
376                 len = buflen;
377 
378         res = access_process_vm(task, mm->arg_start, buffer, len, 0);
379 
380         /*
381          * If the nul at the end of args has been overwritten, then
382          * assume application is using setproctitle(3).
383          */
384         if (res > 0 && buffer[res-1] != '\0' && len < buflen) {
385                 len = strnlen(buffer, res);
386                 if (len < res) {
387                         res = len;
388                 } else {
389                         len = mm->env_end - mm->env_start;
390                         if (len > buflen - res)
391                                 len = buflen - res;
392                         res += access_process_vm(task, mm->env_start,
393                                                  buffer+res, len, 0);
394                         res = strnlen(buffer, res);
395                 }
396         }
397 out_mm:
398         mmput(mm);
399 out:
400         return res;
401 }
402 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp