util.c 11 KB

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