frame_vector.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. #include <linux/kernel.h>
  2. #include <linux/errno.h>
  3. #include <linux/err.h>
  4. #include <linux/mm.h>
  5. #include <linux/slab.h>
  6. #include <linux/vmalloc.h>
  7. #include <linux/pagemap.h>
  8. #include <linux/sched.h>
  9. /**
  10. * get_vaddr_frames() - map virtual addresses to pfns
  11. * @start: starting user address
  12. * @nr_frames: number of pages / pfns from start to map
  13. * @gup_flags: flags modifying lookup behaviour
  14. * @vec: structure which receives pages / pfns of the addresses mapped.
  15. * It should have space for at least nr_frames entries.
  16. *
  17. * This function maps virtual addresses from @start and fills @vec structure
  18. * with page frame numbers or page pointers to corresponding pages (choice
  19. * depends on the type of the vma underlying the virtual address). If @start
  20. * belongs to a normal vma, the function grabs reference to each of the pages
  21. * to pin them in memory. If @start belongs to VM_IO | VM_PFNMAP vma, we don't
  22. * touch page structures and the caller must make sure pfns aren't reused for
  23. * anything else while he is using them.
  24. *
  25. * The function returns number of pages mapped which may be less than
  26. * @nr_frames. In particular we stop mapping if there are more vmas of
  27. * different type underlying the specified range of virtual addresses.
  28. * When the function isn't able to map a single page, it returns error.
  29. *
  30. * This function takes care of grabbing mmap_sem as necessary.
  31. */
  32. int get_vaddr_frames(unsigned long start, unsigned int nr_frames,
  33. unsigned int gup_flags, struct frame_vector *vec)
  34. {
  35. struct mm_struct *mm = current->mm;
  36. struct vm_area_struct *vma;
  37. int ret = 0;
  38. int err;
  39. int locked;
  40. if (nr_frames == 0)
  41. return 0;
  42. if (WARN_ON_ONCE(nr_frames > vec->nr_allocated))
  43. nr_frames = vec->nr_allocated;
  44. down_read(&mm->mmap_sem);
  45. locked = 1;
  46. vma = find_vma_intersection(mm, start, start + 1);
  47. if (!vma) {
  48. ret = -EFAULT;
  49. goto out;
  50. }
  51. /*
  52. * While get_vaddr_frames() could be used for transient (kernel
  53. * controlled lifetime) pinning of memory pages all current
  54. * users establish long term (userspace controlled lifetime)
  55. * page pinning. Treat get_vaddr_frames() like
  56. * get_user_pages_longterm() and disallow it for filesystem-dax
  57. * mappings.
  58. */
  59. if (vma_is_fsdax(vma)) {
  60. ret = -EOPNOTSUPP;
  61. goto out;
  62. }
  63. if (!(vma->vm_flags & (VM_IO | VM_PFNMAP))) {
  64. vec->got_ref = true;
  65. vec->is_pfns = false;
  66. ret = get_user_pages_locked(start, nr_frames,
  67. gup_flags, (struct page **)(vec->ptrs), &locked);
  68. goto out;
  69. }
  70. vec->got_ref = false;
  71. vec->is_pfns = true;
  72. do {
  73. unsigned long *nums = frame_vector_pfns(vec);
  74. while (ret < nr_frames && start + PAGE_SIZE <= vma->vm_end) {
  75. err = follow_pfn(vma, start, &nums[ret]);
  76. if (err) {
  77. if (ret == 0)
  78. ret = err;
  79. goto out;
  80. }
  81. start += PAGE_SIZE;
  82. ret++;
  83. }
  84. /*
  85. * We stop if we have enough pages or if VMA doesn't completely
  86. * cover the tail page.
  87. */
  88. if (ret >= nr_frames || start < vma->vm_end)
  89. break;
  90. vma = find_vma_intersection(mm, start, start + 1);
  91. } while (vma && vma->vm_flags & (VM_IO | VM_PFNMAP));
  92. out:
  93. if (locked)
  94. up_read(&mm->mmap_sem);
  95. if (!ret)
  96. ret = -EFAULT;
  97. if (ret > 0)
  98. vec->nr_frames = ret;
  99. return ret;
  100. }
  101. EXPORT_SYMBOL(get_vaddr_frames);
  102. /**
  103. * put_vaddr_frames() - drop references to pages if get_vaddr_frames() acquired
  104. * them
  105. * @vec: frame vector to put
  106. *
  107. * Drop references to pages if get_vaddr_frames() acquired them. We also
  108. * invalidate the frame vector so that it is prepared for the next call into
  109. * get_vaddr_frames().
  110. */
  111. void put_vaddr_frames(struct frame_vector *vec)
  112. {
  113. int i;
  114. struct page **pages;
  115. if (!vec->got_ref)
  116. goto out;
  117. pages = frame_vector_pages(vec);
  118. /*
  119. * frame_vector_pages() might needed to do a conversion when
  120. * get_vaddr_frames() got pages but vec was later converted to pfns.
  121. * But it shouldn't really fail to convert pfns back...
  122. */
  123. if (WARN_ON(IS_ERR(pages)))
  124. goto out;
  125. for (i = 0; i < vec->nr_frames; i++)
  126. put_page(pages[i]);
  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. if (size <= PAGE_SIZE)
  202. vec = kmalloc(size, GFP_KERNEL);
  203. else
  204. vec = vmalloc(size);
  205. if (!vec)
  206. return NULL;
  207. vec->nr_allocated = nr_frames;
  208. vec->nr_frames = 0;
  209. return vec;
  210. }
  211. EXPORT_SYMBOL(frame_vector_create);
  212. /**
  213. * frame_vector_destroy() - free memory allocated to carry frame vector
  214. * @vec: Frame vector to free
  215. *
  216. * Free structure allocated by frame_vector_create() to carry frames.
  217. */
  218. void frame_vector_destroy(struct frame_vector *vec)
  219. {
  220. /* Make sure put_vaddr_frames() got called properly... */
  221. VM_BUG_ON(vec->nr_frames > 0);
  222. kvfree(vec);
  223. }
  224. EXPORT_SYMBOL(frame_vector_destroy);