frame_vector.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/kernel.h>
  3. #include <linux/errno.h>
  4. #include <linux/err.h>
  5. #include <linux/mm.h>
  6. #include <linux/slab.h>
  7. #include <linux/vmalloc.h>
  8. #include <linux/pagemap.h>
  9. #include <linux/sched.h>
  10. /**
  11. * get_vaddr_frames() - map virtual addresses to pfns
  12. * @start: starting user address
  13. * @nr_frames: number of pages / pfns from start to map
  14. * @gup_flags: flags modifying lookup behaviour
  15. * @vec: structure which receives pages / pfns of the addresses mapped.
  16. * It should have space for at least nr_frames entries.
  17. *
  18. * This function maps virtual addresses from @start and fills @vec structure
  19. * with page frame numbers or page pointers to corresponding pages (choice
  20. * depends on the type of the vma underlying the virtual address). If @start
  21. * belongs to a normal vma, the function grabs reference to each of the pages
  22. * to pin them in memory. If @start belongs to VM_IO | VM_PFNMAP vma, we don't
  23. * touch page structures and the caller must make sure pfns aren't reused for
  24. * anything else while he is using them.
  25. *
  26. * The function returns number of pages mapped which may be less than
  27. * @nr_frames. In particular we stop mapping if there are more vmas of
  28. * different type underlying the specified range of virtual addresses.
  29. * When the function isn't able to map a single page, it returns error.
  30. *
  31. * This function takes care of grabbing mmap_sem as necessary.
  32. */
  33. int get_vaddr_frames(unsigned long start, unsigned int nr_frames,
  34. unsigned int gup_flags, struct frame_vector *vec)
  35. {
  36. struct mm_struct *mm = current->mm;
  37. struct vm_area_struct *vma;
  38. int ret = 0;
  39. int err;
  40. int locked;
  41. if (nr_frames == 0)
  42. return 0;
  43. if (WARN_ON_ONCE(nr_frames > vec->nr_allocated))
  44. nr_frames = vec->nr_allocated;
  45. down_read(&mm->mmap_sem);
  46. locked = 1;
  47. vma = find_vma_intersection(mm, start, start + 1);
  48. if (!vma) {
  49. ret = -EFAULT;
  50. goto out;
  51. }
  52. /*
  53. * While get_vaddr_frames() could be used for transient (kernel
  54. * controlled lifetime) pinning of memory pages all current
  55. * users establish long term (userspace controlled lifetime)
  56. * page pinning. Treat get_vaddr_frames() like
  57. * get_user_pages_longterm() and disallow it for filesystem-dax
  58. * mappings.
  59. */
  60. if (vma_is_fsdax(vma)) {
  61. ret = -EOPNOTSUPP;
  62. goto out;
  63. }
  64. if (!(vma->vm_flags & (VM_IO | VM_PFNMAP))) {
  65. vec->got_ref = true;
  66. vec->is_pfns = false;
  67. ret = get_user_pages_locked(start, nr_frames,
  68. gup_flags, (struct page **)(vec->ptrs), &locked);
  69. goto out;
  70. }
  71. vec->got_ref = false;
  72. vec->is_pfns = true;
  73. do {
  74. unsigned long *nums = frame_vector_pfns(vec);
  75. while (ret < nr_frames && start + PAGE_SIZE <= vma->vm_end) {
  76. err = follow_pfn(vma, start, &nums[ret]);
  77. if (err) {
  78. if (ret == 0)
  79. ret = err;
  80. goto out;
  81. }
  82. start += PAGE_SIZE;
  83. ret++;
  84. }
  85. /*
  86. * We stop if we have enough pages or if VMA doesn't completely
  87. * cover the tail page.
  88. */
  89. if (ret >= nr_frames || start < vma->vm_end)
  90. break;
  91. vma = find_vma_intersection(mm, start, start + 1);
  92. } while (vma && vma->vm_flags & (VM_IO | VM_PFNMAP));
  93. out:
  94. if (locked)
  95. up_read(&mm->mmap_sem);
  96. if (!ret)
  97. ret = -EFAULT;
  98. if (ret > 0)
  99. vec->nr_frames = ret;
  100. return ret;
  101. }
  102. EXPORT_SYMBOL(get_vaddr_frames);
  103. /**
  104. * put_vaddr_frames() - drop references to pages if get_vaddr_frames() acquired
  105. * them
  106. * @vec: frame vector to put
  107. *
  108. * Drop references to pages if get_vaddr_frames() acquired them. We also
  109. * invalidate the frame vector so that it is prepared for the next call into
  110. * get_vaddr_frames().
  111. */
  112. void put_vaddr_frames(struct frame_vector *vec)
  113. {
  114. int i;
  115. struct page **pages;
  116. if (!vec->got_ref)
  117. goto out;
  118. pages = frame_vector_pages(vec);
  119. /*
  120. * frame_vector_pages() might needed to do a conversion when
  121. * get_vaddr_frames() got pages but vec was later converted to pfns.
  122. * But it shouldn't really fail to convert pfns back...
  123. */
  124. if (WARN_ON(IS_ERR(pages)))
  125. goto out;
  126. for (i = 0; i < vec->nr_frames; i++)
  127. put_page(pages[i]);
  128. vec->got_ref = false;
  129. out:
  130. vec->nr_frames = 0;
  131. }
  132. EXPORT_SYMBOL(put_vaddr_frames);
  133. /**
  134. * frame_vector_to_pages - convert frame vector to contain page pointers
  135. * @vec: frame vector to convert
  136. *
  137. * Convert @vec to contain array of page pointers. If the conversion is
  138. * successful, return 0. Otherwise return an error. Note that we do not grab
  139. * page references for the page structures.
  140. */
  141. int frame_vector_to_pages(struct frame_vector *vec)
  142. {
  143. int i;
  144. unsigned long *nums;
  145. struct page **pages;
  146. if (!vec->is_pfns)
  147. return 0;
  148. nums = frame_vector_pfns(vec);
  149. for (i = 0; i < vec->nr_frames; i++)
  150. if (!pfn_valid(nums[i]))
  151. return -EINVAL;
  152. pages = (struct page **)nums;
  153. for (i = 0; i < vec->nr_frames; i++)
  154. pages[i] = pfn_to_page(nums[i]);
  155. vec->is_pfns = false;
  156. return 0;
  157. }
  158. EXPORT_SYMBOL(frame_vector_to_pages);
  159. /**
  160. * frame_vector_to_pfns - convert frame vector to contain pfns
  161. * @vec: frame vector to convert
  162. *
  163. * Convert @vec to contain array of pfns.
  164. */
  165. void frame_vector_to_pfns(struct frame_vector *vec)
  166. {
  167. int i;
  168. unsigned long *nums;
  169. struct page **pages;
  170. if (vec->is_pfns)
  171. return;
  172. pages = (struct page **)(vec->ptrs);
  173. nums = (unsigned long *)pages;
  174. for (i = 0; i < vec->nr_frames; i++)
  175. nums[i] = page_to_pfn(pages[i]);
  176. vec->is_pfns = true;
  177. }
  178. EXPORT_SYMBOL(frame_vector_to_pfns);
  179. /**
  180. * frame_vector_create() - allocate & initialize structure for pinned pfns
  181. * @nr_frames: number of pfns slots we should reserve
  182. *
  183. * Allocate and initialize struct pinned_pfns to be able to hold @nr_pfns
  184. * pfns.
  185. */
  186. struct frame_vector *frame_vector_create(unsigned int nr_frames)
  187. {
  188. struct frame_vector *vec;
  189. int size = sizeof(struct frame_vector) + sizeof(void *) * nr_frames;
  190. if (WARN_ON_ONCE(nr_frames == 0))
  191. return NULL;
  192. /*
  193. * This is absurdly high. It's here just to avoid strange effects when
  194. * arithmetics overflows.
  195. */
  196. if (WARN_ON_ONCE(nr_frames > INT_MAX / sizeof(void *) / 2))
  197. return NULL;
  198. /*
  199. * Avoid higher order allocations, use vmalloc instead. It should
  200. * be rare anyway.
  201. */
  202. vec = kvmalloc(size, GFP_KERNEL);
  203. if (!vec)
  204. return NULL;
  205. vec->nr_allocated = nr_frames;
  206. vec->nr_frames = 0;
  207. return vec;
  208. }
  209. EXPORT_SYMBOL(frame_vector_create);
  210. /**
  211. * frame_vector_destroy() - free memory allocated to carry frame vector
  212. * @vec: Frame vector to free
  213. *
  214. * Free structure allocated by frame_vector_create() to carry frames.
  215. */
  216. void frame_vector_destroy(struct frame_vector *vec)
  217. {
  218. /* Make sure put_vaddr_frames() got called properly... */
  219. VM_BUG_ON(vec->nr_frames > 0);
  220. kvfree(vec);
  221. }
  222. EXPORT_SYMBOL(frame_vector_destroy);