util.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. * strndup_user - duplicate an existing string from user space
  115. * @s: The string to duplicate
  116. * @n: Maximum number of bytes to copy, including the trailing NUL.
  117. */
  118. char *strndup_user(const char __user *s, long n)
  119. {
  120. char *p;
  121. long length;
  122. length = strnlen_user(s, n);
  123. if (!length)
  124. return ERR_PTR(-EFAULT);
  125. if (length > n)
  126. return ERR_PTR(-EINVAL);
  127. p = kmalloc(length, GFP_KERNEL);
  128. if (!p)
  129. return ERR_PTR(-ENOMEM);
  130. if (copy_from_user(p, s, length)) {
  131. kfree(p);
  132. return ERR_PTR(-EFAULT);
  133. }
  134. p[length - 1] = '\0';
  135. return p;
  136. }
  137. EXPORT_SYMBOL(strndup_user);
  138. #ifndef HAVE_ARCH_PICK_MMAP_LAYOUT
  139. void arch_pick_mmap_layout(struct mm_struct *mm)
  140. {
  141. mm->mmap_base = TASK_UNMAPPED_BASE;
  142. mm->get_unmapped_area = arch_get_unmapped_area;
  143. mm->unmap_area = arch_unmap_area;
  144. }
  145. #endif
  146. int __attribute__((weak)) get_user_pages_fast(unsigned long start,
  147. int nr_pages, int write, struct page **pages)
  148. {
  149. struct mm_struct *mm = current->mm;
  150. int ret;
  151. down_read(&mm->mmap_sem);
  152. ret = get_user_pages(current, mm, start, nr_pages,
  153. write, 0, pages, NULL);
  154. up_read(&mm->mmap_sem);
  155. return ret;
  156. }
  157. EXPORT_SYMBOL_GPL(get_user_pages_fast);