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_lock 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. start = untagged_addr(start);
  46. mmap_read_lock(mm);
  47. locked = 1;
  48. vma = find_vma_intersection(mm, start, start + 1);
  49. if (!vma) {
  50. ret = -EFAULT;
  51. goto out;
  52. }
  53. /*
  54. * While get_vaddr_frames() could be used for transient (kernel
  55. * controlled lifetime) pinning of memory pages all current
  56. * users establish long term (userspace controlled lifetime)
  57. * page pinning. Treat get_vaddr_frames() like
  58. * get_user_pages_longterm() and disallow it for filesystem-dax
  59. * mappings.
  60. */
  61. if (vma_is_fsdax(vma)) {
  62. ret = -EOPNOTSUPP;
  63. goto out;
  64. }
  65. if (!(vma->vm_flags & (VM_IO | VM_PFNMAP))) {
  66. vec->got_ref = true;
  67. vec->is_pfns = false;
  68. ret = pin_user_pages_locked(start, nr_frames,
  69. gup_flags, (struct page **)(vec->ptrs), &locked);
  70. goto out;
  71. }
  72. vec->got_ref = false;
  73. vec->is_pfns = true;
  74. do {
  75. unsigned long *nums = frame_vector_pfns(vec);
  76. while (ret < nr_frames && start + PAGE_SIZE <= vma->vm_end) {
  77. err = follow_pfn(vma, start, &nums[ret]);
  78. if (err) {
  79. if (ret == 0)
  80. ret = err;
  81. goto out;
  82. }
  83. start += PAGE_SIZE;
  84. ret++;
  85. }
  86. /*
  87. * We stop if we have enough pages or if VMA doesn't completely
  88. * cover the tail page.
  89. */
  90. if (ret >= nr_frames || start < vma->vm_end)
  91. break;
  92. vma = find_vma_intersection(mm, start, start + 1);
  93. } while (vma && vma->vm_flags & (VM_IO | VM_PFNMAP));
  94. out:
  95. if (locked)
  96. mmap_read_unlock(mm);
  97. if (!ret)
  98. ret = -EFAULT;
  99. if (ret > 0)
  100. vec->nr_frames = ret;
  101. return ret;
  102. }
  103. EXPORT_SYMBOL(get_vaddr_frames);
  104. /**
  105. * put_vaddr_frames() - drop references to pages if get_vaddr_frames() acquired
  106. * them
  107. * @vec: frame vector to put
  108. *
  109. * Drop references to pages if get_vaddr_frames() acquired them. We also
  110. * invalidate the frame vector so that it is prepared for the next call into
  111. * get_vaddr_frames().
  112. */
  113. void put_vaddr_frames(struct frame_vector *vec)
  114. {
  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. unpin_user_pages(pages, vec->nr_frames);
  127. vec->got_ref = false;
  128. out:
  129. vec->nr_frames = 0;
  130. }
  131. EXPORT_SYMBOL(put_vaddr_frames);
  132. /**
  133. * frame_vector_to_pages - convert frame vector to contain page pointers
  134. * @vec: frame vector to convert
  135. *
  136. * Convert @vec to contain array of page pointers. If the conversion is
  137. * successful, return 0. Otherwise return an error. Note that we do not grab
  138. * page references for the page structures.
  139. */
  140. int frame_vector_to_pages(struct frame_vector *vec)
  141. {
  142. int i;
  143. unsigned long *nums;
  144. struct page **pages;
  145. if (!vec->is_pfns)
  146. return 0;
  147. nums = frame_vector_pfns(vec);
  148. for (i = 0; i < vec->nr_frames; i++)
  149. if (!pfn_valid(nums[i]))
  150. return -EINVAL;
  151. pages = (struct page **)nums;
  152. for (i = 0; i < vec->nr_frames; i++)
  153. pages[i] = pfn_to_page(nums[i]);
  154. vec->is_pfns = false;
  155. return 0;
  156. }
  157. EXPORT_SYMBOL(frame_vector_to_pages);
  158. /**
  159. * frame_vector_to_pfns - convert frame vector to contain pfns
  160. * @vec: frame vector to convert
  161. *
  162. * Convert @vec to contain array of pfns.
  163. */
  164. void frame_vector_to_pfns(struct frame_vector *vec)
  165. {
  166. int i;
  167. unsigned long *nums;
  168. struct page **pages;
  169. if (vec->is_pfns)
  170. return;
  171. pages = (struct page **)(vec->ptrs);
  172. nums = (unsigned long *)pages;
  173. for (i = 0; i < vec->nr_frames; i++)
  174. nums[i] = page_to_pfn(pages[i]);
  175. vec->is_pfns = true;
  176. }
  177. EXPORT_SYMBOL(frame_vector_to_pfns);
  178. /**
  179. * frame_vector_create() - allocate & initialize structure for pinned pfns
  180. * @nr_frames: number of pfns slots we should reserve
  181. *
  182. * Allocate and initialize struct pinned_pfns to be able to hold @nr_pfns
  183. * pfns.
  184. */
  185. struct frame_vector *frame_vector_create(unsigned int nr_frames)
  186. {
  187. struct frame_vector *vec;
  188. int size = sizeof(struct frame_vector) + sizeof(void *) * nr_frames;
  189. if (WARN_ON_ONCE(nr_frames == 0))
  190. return NULL;
  191. /*
  192. * This is absurdly high. It's here just to avoid strange effects when
  193. * arithmetics overflows.
  194. */
  195. if (WARN_ON_ONCE(nr_frames > INT_MAX / sizeof(void *) / 2))
  196. return NULL;
  197. /*
  198. * Avoid higher order allocations, use vmalloc instead. It should
  199. * be rare anyway.
  200. */
  201. vec = kvmalloc(size, GFP_KERNEL);
  202. if (!vec)
  203. return NULL;
  204. vec->nr_allocated = nr_frames;
  205. vec->nr_frames = 0;
  206. return vec;
  207. }
  208. EXPORT_SYMBOL(frame_vector_create);
  209. /**
  210. * frame_vector_destroy() - free memory allocated to carry frame vector
  211. * @vec: Frame vector to free
  212. *
  213. * Free structure allocated by frame_vector_create() to carry frames.
  214. */
  215. void frame_vector_destroy(struct frame_vector *vec)
  216. {
  217. /* Make sure put_vaddr_frames() got called properly... */
  218. VM_BUG_ON(vec->nr_frames > 0);
  219. kvfree(vec);
  220. }
  221. EXPORT_SYMBOL(frame_vector_destroy);