diff options
Diffstat (limited to 'mm/util.c')
| -rw-r--r-- | mm/util.c | 290 | 
1 files changed, 249 insertions, 41 deletions
diff --git a/mm/util.c b/mm/util.c index 73dac81e9f7..d5ea733c508 100644 --- a/mm/util.c +++ b/mm/util.c @@ -1,11 +1,21 @@  #include <linux/mm.h>  #include <linux/slab.h>  #include <linux/string.h> -#include <linux/module.h> +#include <linux/compiler.h> +#include <linux/export.h>  #include <linux/err.h>  #include <linux/sched.h> +#include <linux/security.h> +#include <linux/swap.h> +#include <linux/swapops.h> +#include <linux/mman.h> +#include <linux/hugetlb.h> +#include <linux/vmalloc.h> +  #include <asm/uaccess.h> +#include "internal.h" +  #define CREATE_TRACE_POINTS  #include <trace/events/kmem.h> @@ -102,6 +112,25 @@ void *memdup_user(const void __user *src, size_t len)  }  EXPORT_SYMBOL(memdup_user); +static __always_inline void *__do_krealloc(const void *p, size_t new_size, +					   gfp_t flags) +{ +	void *ret; +	size_t ks = 0; + +	if (p) +		ks = ksize(p); + +	if (ks >= new_size) +		return (void *)p; + +	ret = kmalloc_track_caller(new_size, flags); +	if (ret && p) +		memcpy(ret, p, ks); + +	return ret; +} +  /**   * __krealloc - like krealloc() but don't free @p.   * @p: object to reallocate memory for. @@ -114,23 +143,11 @@ EXPORT_SYMBOL(memdup_user);   */  void *__krealloc(const void *p, size_t new_size, gfp_t flags)  { -	void *ret; -	size_t ks = 0; -  	if (unlikely(!new_size))  		return ZERO_SIZE_PTR; -	if (p) -		ks = ksize(p); - -	if (ks >= new_size) -		return (void *)p; - -	ret = kmalloc_track_caller(new_size, flags); -	if (ret && p) -		memcpy(ret, p, ks); +	return __do_krealloc(p, new_size, flags); -	return ret;  }  EXPORT_SYMBOL(__krealloc); @@ -142,7 +159,7 @@ EXPORT_SYMBOL(__krealloc);   *   * The contents of the object pointed to are preserved up to the   * lesser of the new and old sizes.  If @p is %NULL, krealloc() - * behaves exactly like kmalloc().  If @size is 0 and @p is not a + * behaves exactly like kmalloc().  If @new_size is 0 and @p is not a   * %NULL pointer, the object pointed to is freed.   */  void *krealloc(const void *p, size_t new_size, gfp_t flags) @@ -154,7 +171,7 @@ void *krealloc(const void *p, size_t new_size, gfp_t flags)  		return ZERO_SIZE_PTR;  	} -	ret = __krealloc(p, new_size, flags); +	ret = __do_krealloc(p, new_size, flags);  	if (ret && p != ret)  		kfree(p); @@ -186,27 +203,6 @@ void kzfree(const void *p)  }  EXPORT_SYMBOL(kzfree); -int kern_ptr_validate(const void *ptr, unsigned long size) -{ -	unsigned long addr = (unsigned long)ptr; -	unsigned long min_addr = PAGE_OFFSET; -	unsigned long align_mask = sizeof(void *) - 1; - -	if (unlikely(addr < min_addr)) -		goto out; -	if (unlikely(addr > (unsigned long)high_memory - size)) -		goto out; -	if (unlikely(addr & align_mask)) -		goto out; -	if (unlikely(!kern_addr_valid(addr))) -		goto out; -	if (unlikely(!kern_addr_valid(addr + size - 1))) -		goto out; -	return 1; -out: -	return 0; -} -  /*   * strndup_user - duplicate an existing string from user space   * @s: The string to duplicate @@ -236,22 +232,84 @@ char *strndup_user(const char __user *s, long n)  }  EXPORT_SYMBOL(strndup_user); +void __vma_link_list(struct mm_struct *mm, struct vm_area_struct *vma, +		struct vm_area_struct *prev, struct rb_node *rb_parent) +{ +	struct vm_area_struct *next; + +	vma->vm_prev = prev; +	if (prev) { +		next = prev->vm_next; +		prev->vm_next = vma; +	} else { +		mm->mmap = vma; +		if (rb_parent) +			next = rb_entry(rb_parent, +					struct vm_area_struct, vm_rb); +		else +			next = NULL; +	} +	vma->vm_next = next; +	if (next) +		next->vm_prev = vma; +} + +/* Check if the vma is being used as a stack by this task */ +static int vm_is_stack_for_task(struct task_struct *t, +				struct vm_area_struct *vma) +{ +	return (vma->vm_start <= KSTK_ESP(t) && vma->vm_end >= KSTK_ESP(t)); +} + +/* + * Check if the vma is being used as a stack. + * If is_group is non-zero, check in the entire thread group or else + * just check in the current task. Returns the pid of the task that + * the vma is stack for. + */ +pid_t vm_is_stack(struct task_struct *task, +		  struct vm_area_struct *vma, int in_group) +{ +	pid_t ret = 0; + +	if (vm_is_stack_for_task(task, vma)) +		return task->pid; + +	if (in_group) { +		struct task_struct *t; +		rcu_read_lock(); +		if (!pid_alive(task)) +			goto done; + +		t = task; +		do { +			if (vm_is_stack_for_task(t, vma)) { +				ret = t->pid; +				goto done; +			} +		} while_each_thread(task, t); +done: +		rcu_read_unlock(); +	} + +	return ret; +} +  #if defined(CONFIG_MMU) && !defined(HAVE_ARCH_PICK_MMAP_LAYOUT)  void arch_pick_mmap_layout(struct mm_struct *mm)  {  	mm->mmap_base = TASK_UNMAPPED_BASE;  	mm->get_unmapped_area = arch_get_unmapped_area; -	mm->unmap_area = arch_unmap_area;  }  #endif  /*   * Like get_user_pages_fast() except its IRQ-safe in that it won't fall   * back to the regular GUP. - * If the architecture not support this fucntion, simply return with no + * If the architecture not support this function, simply return with no   * page pinned   */ -int __attribute__((weak)) __get_user_pages_fast(unsigned long start, +int __weak __get_user_pages_fast(unsigned long start,  				 int nr_pages, int write, struct page **pages)  {  	return 0; @@ -282,7 +340,7 @@ EXPORT_SYMBOL_GPL(__get_user_pages_fast);   * callers need to carefully consider what to use. On many architectures,   * get_user_pages_fast simply falls back to get_user_pages.   */ -int __attribute__((weak)) get_user_pages_fast(unsigned long start, +int __weak get_user_pages_fast(unsigned long start,  				int nr_pages, int write, struct page **pages)  {  	struct mm_struct *mm = current->mm; @@ -297,6 +355,156 @@ int __attribute__((weak)) get_user_pages_fast(unsigned long start,  }  EXPORT_SYMBOL_GPL(get_user_pages_fast); +unsigned long vm_mmap_pgoff(struct file *file, unsigned long addr, +	unsigned long len, unsigned long prot, +	unsigned long flag, unsigned long pgoff) +{ +	unsigned long ret; +	struct mm_struct *mm = current->mm; +	unsigned long populate; + +	ret = security_mmap_file(file, prot, flag); +	if (!ret) { +		down_write(&mm->mmap_sem); +		ret = do_mmap_pgoff(file, addr, len, prot, flag, pgoff, +				    &populate); +		up_write(&mm->mmap_sem); +		if (populate) +			mm_populate(ret, populate); +	} +	return ret; +} + +unsigned long vm_mmap(struct file *file, unsigned long addr, +	unsigned long len, unsigned long prot, +	unsigned long flag, unsigned long offset) +{ +	if (unlikely(offset + PAGE_ALIGN(len) < offset)) +		return -EINVAL; +	if (unlikely(offset & ~PAGE_MASK)) +		return -EINVAL; + +	return vm_mmap_pgoff(file, addr, len, prot, flag, offset >> PAGE_SHIFT); +} +EXPORT_SYMBOL(vm_mmap); + +void kvfree(const void *addr) +{ +	if (is_vmalloc_addr(addr)) +		vfree(addr); +	else +		kfree(addr); +} +EXPORT_SYMBOL(kvfree); + +struct address_space *page_mapping(struct page *page) +{ +	struct address_space *mapping = page->mapping; + +	/* This happens if someone calls flush_dcache_page on slab page */ +	if (unlikely(PageSlab(page))) +		return NULL; + +	if (unlikely(PageSwapCache(page))) { +		swp_entry_t entry; + +		entry.val = page_private(page); +		mapping = swap_address_space(entry); +	} else if ((unsigned long)mapping & PAGE_MAPPING_ANON) +		mapping = NULL; +	return mapping; +} + +int overcommit_ratio_handler(struct ctl_table *table, int write, +			     void __user *buffer, size_t *lenp, +			     loff_t *ppos) +{ +	int ret; + +	ret = proc_dointvec(table, write, buffer, lenp, ppos); +	if (ret == 0 && write) +		sysctl_overcommit_kbytes = 0; +	return ret; +} + +int overcommit_kbytes_handler(struct ctl_table *table, int write, +			     void __user *buffer, size_t *lenp, +			     loff_t *ppos) +{ +	int ret; + +	ret = proc_doulongvec_minmax(table, write, buffer, lenp, ppos); +	if (ret == 0 && write) +		sysctl_overcommit_ratio = 0; +	return ret; +} + +/* + * Committed memory limit enforced when OVERCOMMIT_NEVER policy is used + */ +unsigned long vm_commit_limit(void) +{ +	unsigned long allowed; + +	if (sysctl_overcommit_kbytes) +		allowed = sysctl_overcommit_kbytes >> (PAGE_SHIFT - 10); +	else +		allowed = ((totalram_pages - hugetlb_total_pages()) +			   * sysctl_overcommit_ratio / 100); +	allowed += total_swap_pages; + +	return allowed; +} + +/** + * get_cmdline() - copy the cmdline value to a buffer. + * @task:     the task whose cmdline value to copy. + * @buffer:   the buffer to copy to. + * @buflen:   the length of the buffer. Larger cmdline values are truncated + *            to this length. + * Returns the size of the cmdline field copied. Note that the copy does + * not guarantee an ending NULL byte. + */ +int get_cmdline(struct task_struct *task, char *buffer, int buflen) +{ +	int res = 0; +	unsigned int len; +	struct mm_struct *mm = get_task_mm(task); +	if (!mm) +		goto out; +	if (!mm->arg_end) +		goto out_mm;	/* Shh! No looking before we're done */ + +	len = mm->arg_end - mm->arg_start; + +	if (len > buflen) +		len = buflen; + +	res = access_process_vm(task, mm->arg_start, buffer, len, 0); + +	/* +	 * If the nul at the end of args has been overwritten, then +	 * assume application is using setproctitle(3). +	 */ +	if (res > 0 && buffer[res-1] != '\0' && len < buflen) { +		len = strnlen(buffer, res); +		if (len < res) { +			res = len; +		} else { +			len = mm->env_end - mm->env_start; +			if (len > buflen - res) +				len = buflen - res; +			res += access_process_vm(task, mm->env_start, +						 buffer+res, len, 0); +			res = strnlen(buffer, res); +		} +	} +out_mm: +	mmput(mm); +out: +	return res; +} +  /* Tracepoints definitions. */  EXPORT_TRACEPOINT_SYMBOL(kmalloc);  EXPORT_TRACEPOINT_SYMBOL(kmem_cache_alloc);  | 
