util.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. #include <linux/mm.h>
  2. #include <linux/slab.h>
  3. #include <linux/string.h>
  4. #include <linux/module.h>
  5. #include <linux/err.h>
  6. #include <linux/sched.h>
  7. #include <linux/tracepoint.h>
  8. #include <asm/uaccess.h>
  9. /**
  10. * kstrdup - allocate space for and copy an existing string
  11. * @s: the string to duplicate
  12. * @gfp: the GFP mask used in the kmalloc() call when allocating memory
  13. */
  14. char *kstrdup(const char *s, gfp_t gfp)
  15. {
  16. size_t len;
  17. char *buf;
  18. if (!s)
  19. return NULL;
  20. len = strlen(s) + 1;
  21. buf = kmalloc_track_caller(len, gfp);
  22. if (buf)
  23. memcpy(buf, s, len);
  24. return buf;
  25. }
  26. EXPORT_SYMBOL(kstrdup);
  27. /**
  28. * kstrndup - allocate space for and copy an existing string
  29. * @s: the string to duplicate
  30. * @max: read at most @max chars from @s
  31. * @gfp: the GFP mask used in the kmalloc() call when allocating memory
  32. */
  33. char *kstrndup(const char *s, size_t max, gfp_t gfp)
  34. {
  35. size_t len;
  36. char *buf;
  37. if (!s)
  38. return NULL;
  39. len = strnlen(s, max);
  40. buf = kmalloc_track_caller(len+1, gfp);
  41. if (buf) {
  42. memcpy(buf, s, len);
  43. buf[len] = '\0';
  44. }
  45. return buf;
  46. }
  47. EXPORT_SYMBOL(kstrndup);
  48. /**
  49. * kmemdup - duplicate region of memory
  50. *
  51. * @src: memory region to duplicate
  52. * @len: memory region length
  53. * @gfp: GFP mask to use
  54. */
  55. void *kmemdup(const void *src, size_t len, gfp_t gfp)
  56. {
  57. void *p;
  58. p = kmalloc_track_caller(len, gfp);
  59. if (p)
  60. memcpy(p, src, len);
  61. return p;
  62. }
  63. EXPORT_SYMBOL(kmemdup);
  64. /**
  65. * memdup_user - duplicate memory region from user space
  66. *
  67. * @src: source address in user space
  68. * @len: number of bytes to copy
  69. *
  70. * Returns an ERR_PTR() on failure.
  71. */
  72. void *memdup_user(const void __user *src, size_t len)
  73. {
  74. void *p;
  75. /*
  76. * Always use GFP_KERNEL, since copy_from_user() can sleep and
  77. * cause pagefault, which makes it pointless to use GFP_NOFS
  78. * or GFP_ATOMIC.
  79. */
  80. p = kmalloc_track_caller(len, GFP_KERNEL);
  81. if (!p)
  82. return ERR_PTR(-ENOMEM);
  83. if (copy_from_user(p, src, len)) {
  84. kfree(p);
  85. return ERR_PTR(-EFAULT);
  86. }
  87. return p;
  88. }
  89. EXPORT_SYMBOL(memdup_user);
  90. /**
  91. * __krealloc - like krealloc() but don't free @p.
  92. * @p: object to reallocate memory for.
  93. * @new_size: how many bytes of memory are required.
  94. * @flags: the type of memory to allocate.
  95. *
  96. * This function is like krealloc() except it never frees the originally
  97. * allocated buffer. Use this if you don't want to free the buffer immediately
  98. * like, for example, with RCU.
  99. */
  100. void *__krealloc(const void *p, size_t new_size, gfp_t flags)
  101. {
  102. void *ret;
  103. size_t ks = 0;
  104. if (unlikely(!new_size))
  105. return ZERO_SIZE_PTR;
  106. if (p)
  107. ks = ksize(p);
  108. if (ks >= new_size)
  109. return (void *)p;
  110. ret = kmalloc_track_caller(new_size, flags);
  111. if (ret && p)
  112. memcpy(ret, p, ks);
  113. return ret;
  114. }
  115. EXPORT_SYMBOL(__krealloc);
  116. /**
  117. * krealloc - reallocate memory. The contents will remain unchanged.
  118. * @p: object to reallocate memory for.
  119. * @new_size: how many bytes of memory are required.
  120. * @flags: the type of memory to allocate.
  121. *
  122. * The contents of the object pointed to are preserved up to the
  123. * lesser of the new and old sizes. If @p is %NULL, krealloc()
  124. * behaves exactly like kmalloc(). If @size is 0 and @p is not a
  125. * %NULL pointer, the object pointed to is freed.
  126. */
  127. void *krealloc(const void *p, size_t new_size, gfp_t flags)
  128. {
  129. void *ret;
  130. if (unlikely(!new_size)) {
  131. kfree(p);
  132. return ZERO_SIZE_PTR;
  133. }
  134. ret = __krealloc(p, new_size, flags);
  135. if (ret && p != ret)
  136. kfree(p);
  137. return ret;
  138. }
  139. EXPORT_SYMBOL(krealloc);
  140. /**
  141. * kzfree - like kfree but zero memory
  142. * @p: object to free memory of
  143. *
  144. * The memory of the object @p points to is zeroed before freed.
  145. * If @p is %NULL, kzfree() does nothing.
  146. */
  147. void kzfree(const void *p)
  148. {
  149. size_t ks;
  150. void *mem = (void *)p;
  151. if (unlikely(ZERO_OR_NULL_PTR(mem)))
  152. return;
  153. ks = ksize(mem);
  154. memset(mem, 0, ks);
  155. kfree(mem);
  156. }
  157. EXPORT_SYMBOL(kzfree);
  158. /*
  159. * strndup_user - duplicate an existing string from user space
  160. * @s: The string to duplicate
  161. * @n: Maximum number of bytes to copy, including the trailing NUL.
  162. */
  163. char *strndup_user(const char __user *s, long n)
  164. {
  165. char *p;
  166. long length;
  167. length = strnlen_user(s, n);
  168. if (!length)
  169. return ERR_PTR(-EFAULT);
  170. if (length > n)
  171. return ERR_PTR(-EINVAL);
  172. p = kmalloc(length, GFP_KERNEL);
  173. if (!p)
  174. return ERR_PTR(-ENOMEM);
  175. if (copy_from_user(p, s, length)) {
  176. kfree(p);
  177. return ERR_PTR(-EFAULT);
  178. }
  179. p[length - 1] = '\0';
  180. return p;
  181. }
  182. EXPORT_SYMBOL(strndup_user);
  183. #ifndef HAVE_ARCH_PICK_MMAP_LAYOUT
  184. void arch_pick_mmap_layout(struct mm_struct *mm)
  185. {
  186. mm->mmap_base = TASK_UNMAPPED_BASE;
  187. mm->get_unmapped_area = arch_get_unmapped_area;
  188. mm->unmap_area = arch_unmap_area;
  189. }
  190. #endif
  191. int __attribute__((weak)) get_user_pages_fast(unsigned long start,
  192. int nr_pages, int write, struct page **pages)
  193. {
  194. struct mm_struct *mm = current->mm;
  195. int ret;
  196. down_read(&mm->mmap_sem);
  197. ret = get_user_pages(current, mm, start, nr_pages,
  198. write, 0, pages, NULL);
  199. up_read(&mm->mmap_sem);
  200. return ret;
  201. }
  202. EXPORT_SYMBOL_GPL(get_user_pages_fast);
  203. /* Tracepoints definitions. */
  204. DEFINE_TRACE(kmalloc);
  205. DEFINE_TRACE(kmem_cache_alloc);
  206. DEFINE_TRACE(kmalloc_node);
  207. DEFINE_TRACE(kmem_cache_alloc_node);
  208. DEFINE_TRACE(kfree);
  209. DEFINE_TRACE(kmem_cache_free);
  210. EXPORT_TRACEPOINT_SYMBOL(kmalloc);
  211. EXPORT_TRACEPOINT_SYMBOL(kmem_cache_alloc);
  212. EXPORT_TRACEPOINT_SYMBOL(kmalloc_node);
  213. EXPORT_TRACEPOINT_SYMBOL(kmem_cache_alloc_node);
  214. EXPORT_TRACEPOINT_SYMBOL(kfree);
  215. EXPORT_TRACEPOINT_SYMBOL(kmem_cache_free);