util.c 11 KB

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