util.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. * __krealloc - like krealloc() but don't free @p.
  65. * @p: object to reallocate memory for.
  66. * @new_size: how many bytes of memory are required.
  67. * @flags: the type of memory to allocate.
  68. *
  69. * This function is like krealloc() except it never frees the originally
  70. * allocated buffer. Use this if you don't want to free the buffer immediately
  71. * like, for example, with RCU.
  72. */
  73. void *__krealloc(const void *p, size_t new_size, gfp_t flags)
  74. {
  75. void *ret;
  76. size_t ks = 0;
  77. if (unlikely(!new_size))
  78. return ZERO_SIZE_PTR;
  79. if (p)
  80. ks = ksize(p);
  81. if (ks >= new_size)
  82. return (void *)p;
  83. ret = kmalloc_track_caller(new_size, flags);
  84. if (ret && p)
  85. memcpy(ret, p, ks);
  86. return ret;
  87. }
  88. EXPORT_SYMBOL(__krealloc);
  89. /**
  90. * krealloc - reallocate memory. The contents will remain unchanged.
  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. * The contents of the object pointed to are preserved up to the
  96. * lesser of the new and old sizes. If @p is %NULL, krealloc()
  97. * behaves exactly like kmalloc(). If @size is 0 and @p is not a
  98. * %NULL pointer, the object pointed to is freed.
  99. */
  100. void *krealloc(const void *p, size_t new_size, gfp_t flags)
  101. {
  102. void *ret;
  103. if (unlikely(!new_size)) {
  104. kfree(p);
  105. return ZERO_SIZE_PTR;
  106. }
  107. ret = __krealloc(p, new_size, flags);
  108. if (ret && p != ret)
  109. kfree(p);
  110. return ret;
  111. }
  112. EXPORT_SYMBOL(krealloc);
  113. /**
  114. * kzfree - like kfree but zero memory
  115. * @p: object to free memory of
  116. *
  117. * The memory of the object @p points to is zeroed before freed.
  118. * If @p is %NULL, kzfree() does nothing.
  119. */
  120. void kzfree(const void *p)
  121. {
  122. size_t ks;
  123. void *mem = (void *)p;
  124. if (unlikely(ZERO_OR_NULL_PTR(mem)))
  125. return;
  126. ks = ksize(mem);
  127. memset(mem, 0, ks);
  128. kfree(mem);
  129. }
  130. EXPORT_SYMBOL(kzfree);
  131. /*
  132. * strndup_user - duplicate an existing string from user space
  133. * @s: The string to duplicate
  134. * @n: Maximum number of bytes to copy, including the trailing NUL.
  135. */
  136. char *strndup_user(const char __user *s, long n)
  137. {
  138. char *p;
  139. long length;
  140. length = strnlen_user(s, n);
  141. if (!length)
  142. return ERR_PTR(-EFAULT);
  143. if (length > n)
  144. return ERR_PTR(-EINVAL);
  145. p = kmalloc(length, GFP_KERNEL);
  146. if (!p)
  147. return ERR_PTR(-ENOMEM);
  148. if (copy_from_user(p, s, length)) {
  149. kfree(p);
  150. return ERR_PTR(-EFAULT);
  151. }
  152. p[length - 1] = '\0';
  153. return p;
  154. }
  155. EXPORT_SYMBOL(strndup_user);
  156. #ifndef HAVE_ARCH_PICK_MMAP_LAYOUT
  157. void arch_pick_mmap_layout(struct mm_struct *mm)
  158. {
  159. mm->mmap_base = TASK_UNMAPPED_BASE;
  160. mm->get_unmapped_area = arch_get_unmapped_area;
  161. mm->unmap_area = arch_unmap_area;
  162. }
  163. #endif
  164. int __attribute__((weak)) get_user_pages_fast(unsigned long start,
  165. int nr_pages, int write, struct page **pages)
  166. {
  167. struct mm_struct *mm = current->mm;
  168. int ret;
  169. down_read(&mm->mmap_sem);
  170. ret = get_user_pages(current, mm, start, nr_pages,
  171. write, 0, pages, NULL);
  172. up_read(&mm->mmap_sem);
  173. return ret;
  174. }
  175. EXPORT_SYMBOL_GPL(get_user_pages_fast);