util.c 9.4 KB

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