util.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  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 pid of the task that
  148. * the vma is stack for.
  149. */
  150. pid_t vm_is_stack(struct task_struct *task,
  151. struct vm_area_struct *vma, int in_group)
  152. {
  153. pid_t ret = 0;
  154. if (vm_is_stack_for_task(task, vma))
  155. return task->pid;
  156. if (in_group) {
  157. struct task_struct *t;
  158. rcu_read_lock();
  159. for_each_thread(task, t) {
  160. if (vm_is_stack_for_task(t, vma)) {
  161. ret = t->pid;
  162. goto done;
  163. }
  164. }
  165. done:
  166. rcu_read_unlock();
  167. }
  168. return ret;
  169. }
  170. #if defined(CONFIG_MMU) && !defined(HAVE_ARCH_PICK_MMAP_LAYOUT)
  171. void arch_pick_mmap_layout(struct mm_struct *mm)
  172. {
  173. mm->mmap_base = TASK_UNMAPPED_BASE;
  174. mm->get_unmapped_area = arch_get_unmapped_area;
  175. }
  176. #endif
  177. /*
  178. * Like get_user_pages_fast() except its IRQ-safe in that it won't fall
  179. * back to the regular GUP.
  180. * If the architecture not support this function, simply return with no
  181. * page pinned
  182. */
  183. int __weak __get_user_pages_fast(unsigned long start,
  184. int nr_pages, int write, struct page **pages)
  185. {
  186. return 0;
  187. }
  188. EXPORT_SYMBOL_GPL(__get_user_pages_fast);
  189. /**
  190. * get_user_pages_fast() - pin user pages in memory
  191. * @start: starting user address
  192. * @nr_pages: number of pages from start to pin
  193. * @write: whether pages will be written to
  194. * @pages: array that receives pointers to the pages pinned.
  195. * Should be at least nr_pages long.
  196. *
  197. * Returns number of pages pinned. This may be fewer than the number
  198. * requested. If nr_pages is 0 or negative, returns 0. If no pages
  199. * were pinned, returns -errno.
  200. *
  201. * get_user_pages_fast provides equivalent functionality to get_user_pages,
  202. * operating on current and current->mm, with force=0 and vma=NULL. However
  203. * unlike get_user_pages, it must be called without mmap_sem held.
  204. *
  205. * get_user_pages_fast may take mmap_sem and page table locks, so no
  206. * assumptions can be made about lack of locking. get_user_pages_fast is to be
  207. * implemented in a way that is advantageous (vs get_user_pages()) when the
  208. * user memory area is already faulted in and present in ptes. However if the
  209. * pages have to be faulted in, it may turn out to be slightly slower so
  210. * callers need to carefully consider what to use. On many architectures,
  211. * get_user_pages_fast simply falls back to get_user_pages.
  212. */
  213. int __weak get_user_pages_fast(unsigned long start,
  214. int nr_pages, int write, struct page **pages)
  215. {
  216. struct mm_struct *mm = current->mm;
  217. int ret;
  218. down_read(&mm->mmap_sem);
  219. ret = get_user_pages(current, mm, start, nr_pages,
  220. write, 0, pages, NULL);
  221. up_read(&mm->mmap_sem);
  222. return ret;
  223. }
  224. EXPORT_SYMBOL_GPL(get_user_pages_fast);
  225. unsigned long vm_mmap_pgoff(struct file *file, unsigned long addr,
  226. unsigned long len, unsigned long prot,
  227. unsigned long flag, unsigned long pgoff)
  228. {
  229. unsigned long ret;
  230. struct mm_struct *mm = current->mm;
  231. unsigned long populate;
  232. ret = security_mmap_file(file, prot, flag);
  233. if (!ret) {
  234. down_write(&mm->mmap_sem);
  235. ret = do_mmap_pgoff(file, addr, len, prot, flag, pgoff,
  236. &populate);
  237. up_write(&mm->mmap_sem);
  238. if (populate)
  239. mm_populate(ret, populate);
  240. }
  241. return ret;
  242. }
  243. unsigned long vm_mmap(struct file *file, unsigned long addr,
  244. unsigned long len, unsigned long prot,
  245. unsigned long flag, unsigned long offset)
  246. {
  247. if (unlikely(offset + PAGE_ALIGN(len) < offset))
  248. return -EINVAL;
  249. if (unlikely(offset & ~PAGE_MASK))
  250. return -EINVAL;
  251. return vm_mmap_pgoff(file, addr, len, prot, flag, offset >> PAGE_SHIFT);
  252. }
  253. EXPORT_SYMBOL(vm_mmap);
  254. void kvfree(const void *addr)
  255. {
  256. if (is_vmalloc_addr(addr))
  257. vfree(addr);
  258. else
  259. kfree(addr);
  260. }
  261. EXPORT_SYMBOL(kvfree);
  262. struct address_space *page_mapping(struct page *page)
  263. {
  264. struct address_space *mapping = page->mapping;
  265. /* This happens if someone calls flush_dcache_page on slab page */
  266. if (unlikely(PageSlab(page)))
  267. return NULL;
  268. if (unlikely(PageSwapCache(page))) {
  269. swp_entry_t entry;
  270. entry.val = page_private(page);
  271. mapping = swap_address_space(entry);
  272. } else if ((unsigned long)mapping & PAGE_MAPPING_ANON)
  273. mapping = NULL;
  274. return mapping;
  275. }
  276. int overcommit_ratio_handler(struct ctl_table *table, int write,
  277. void __user *buffer, size_t *lenp,
  278. loff_t *ppos)
  279. {
  280. int ret;
  281. ret = proc_dointvec(table, write, buffer, lenp, ppos);
  282. if (ret == 0 && write)
  283. sysctl_overcommit_kbytes = 0;
  284. return ret;
  285. }
  286. int overcommit_kbytes_handler(struct ctl_table *table, int write,
  287. void __user *buffer, size_t *lenp,
  288. loff_t *ppos)
  289. {
  290. int ret;
  291. ret = proc_doulongvec_minmax(table, write, buffer, lenp, ppos);
  292. if (ret == 0 && write)
  293. sysctl_overcommit_ratio = 0;
  294. return ret;
  295. }
  296. /*
  297. * Committed memory limit enforced when OVERCOMMIT_NEVER policy is used
  298. */
  299. unsigned long vm_commit_limit(void)
  300. {
  301. unsigned long allowed;
  302. if (sysctl_overcommit_kbytes)
  303. allowed = sysctl_overcommit_kbytes >> (PAGE_SHIFT - 10);
  304. else
  305. allowed = ((totalram_pages - hugetlb_total_pages())
  306. * sysctl_overcommit_ratio / 100);
  307. allowed += total_swap_pages;
  308. return allowed;
  309. }
  310. /**
  311. * get_cmdline() - copy the cmdline value to a buffer.
  312. * @task: the task whose cmdline value to copy.
  313. * @buffer: the buffer to copy to.
  314. * @buflen: the length of the buffer. Larger cmdline values are truncated
  315. * to this length.
  316. * Returns the size of the cmdline field copied. Note that the copy does
  317. * not guarantee an ending NULL byte.
  318. */
  319. int get_cmdline(struct task_struct *task, char *buffer, int buflen)
  320. {
  321. int res = 0;
  322. unsigned int len;
  323. struct mm_struct *mm = get_task_mm(task);
  324. if (!mm)
  325. goto out;
  326. if (!mm->arg_end)
  327. goto out_mm; /* Shh! No looking before we're done */
  328. len = mm->arg_end - mm->arg_start;
  329. if (len > buflen)
  330. len = buflen;
  331. res = access_process_vm(task, mm->arg_start, buffer, len, 0);
  332. /*
  333. * If the nul at the end of args has been overwritten, then
  334. * assume application is using setproctitle(3).
  335. */
  336. if (res > 0 && buffer[res-1] != '\0' && len < buflen) {
  337. len = strnlen(buffer, res);
  338. if (len < res) {
  339. res = len;
  340. } else {
  341. len = mm->env_end - mm->env_start;
  342. if (len > buflen - res)
  343. len = buflen - res;
  344. res += access_process_vm(task, mm->env_start,
  345. buffer+res, len, 0);
  346. res = strnlen(buffer, res);
  347. }
  348. }
  349. out_mm:
  350. mmput(mm);
  351. out:
  352. return res;
  353. }