util.c 4.8 KB

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