frame_vector.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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. return -EOPNOTSUPP;
  61. if (!(vma->vm_flags & (VM_IO | VM_PFNMAP))) {
  62. vec->got_ref = true;
  63. vec->is_pfns = false;
  64. ret = get_user_pages_locked(start, nr_frames,
  65. gup_flags, (struct page **)(vec->ptrs), &locked);
  66. goto out;
  67. }
  68. vec->got_ref = false;
  69. vec->is_pfns = true;
  70. do {
  71. unsigned long *nums = frame_vector_pfns(vec);
  72. while (ret < nr_frames && start + PAGE_SIZE <= vma->vm_end) {
  73. err = follow_pfn(vma, start, &nums[ret]);
  74. if (err) {
  75. if (ret == 0)
  76. ret = err;
  77. goto out;
  78. }
  79. start += PAGE_SIZE;
  80. ret++;
  81. }
  82. /*
  83. * We stop if we have enough pages or if VMA doesn't completely
  84. * cover the tail page.
  85. */
  86. if (ret >= nr_frames || start < vma->vm_end)
  87. break;
  88. vma = find_vma_intersection(mm, start, start + 1);
  89. } while (vma && vma->vm_flags & (VM_IO | VM_PFNMAP));
  90. out:
  91. if (locked)
  92. up_read(&mm->mmap_sem);
  93. if (!ret)
  94. ret = -EFAULT;
  95. if (ret > 0)
  96. vec->nr_frames = ret;
  97. return ret;
  98. }
  99. EXPORT_SYMBOL(get_vaddr_frames);
  100. /**
  101. * put_vaddr_frames() - drop references to pages if get_vaddr_frames() acquired
  102. * them
  103. * @vec: frame vector to put
  104. *
  105. * Drop references to pages if get_vaddr_frames() acquired them. We also
  106. * invalidate the frame vector so that it is prepared for the next call into
  107. * get_vaddr_frames().
  108. */
  109. void put_vaddr_frames(struct frame_vector *vec)
  110. {
  111. int i;
  112. struct page **pages;
  113. if (!vec->got_ref)
  114. goto out;
  115. pages = frame_vector_pages(vec);
  116. /*
  117. * frame_vector_pages() might needed to do a conversion when
  118. * get_vaddr_frames() got pages but vec was later converted to pfns.
  119. * But it shouldn't really fail to convert pfns back...
  120. */
  121. if (WARN_ON(IS_ERR(pages)))
  122. goto out;
  123. for (i = 0; i < vec->nr_frames; i++)
  124. put_page(pages[i]);
  125. vec->got_ref = false;
  126. out:
  127. vec->nr_frames = 0;
  128. }
  129. EXPORT_SYMBOL(put_vaddr_frames);
  130. /**
  131. * frame_vector_to_pages - convert frame vector to contain page pointers
  132. * @vec: frame vector to convert
  133. *
  134. * Convert @vec to contain array of page pointers. If the conversion is
  135. * successful, return 0. Otherwise return an error. Note that we do not grab
  136. * page references for the page structures.
  137. */
  138. int frame_vector_to_pages(struct frame_vector *vec)
  139. {
  140. int i;
  141. unsigned long *nums;
  142. struct page **pages;
  143. if (!vec->is_pfns)
  144. return 0;
  145. nums = frame_vector_pfns(vec);
  146. for (i = 0; i < vec->nr_frames; i++)
  147. if (!pfn_valid(nums[i]))
  148. return -EINVAL;
  149. pages = (struct page **)nums;
  150. for (i = 0; i < vec->nr_frames; i++)
  151. pages[i] = pfn_to_page(nums[i]);
  152. vec->is_pfns = false;
  153. return 0;
  154. }
  155. EXPORT_SYMBOL(frame_vector_to_pages);
  156. /**
  157. * frame_vector_to_pfns - convert frame vector to contain pfns
  158. * @vec: frame vector to convert
  159. *
  160. * Convert @vec to contain array of pfns.
  161. */
  162. void frame_vector_to_pfns(struct frame_vector *vec)
  163. {
  164. int i;
  165. unsigned long *nums;
  166. struct page **pages;
  167. if (vec->is_pfns)
  168. return;
  169. pages = (struct page **)(vec->ptrs);
  170. nums = (unsigned long *)pages;
  171. for (i = 0; i < vec->nr_frames; i++)
  172. nums[i] = page_to_pfn(pages[i]);
  173. vec->is_pfns = true;
  174. }
  175. EXPORT_SYMBOL(frame_vector_to_pfns);
  176. /**
  177. * frame_vector_create() - allocate & initialize structure for pinned pfns
  178. * @nr_frames: number of pfns slots we should reserve
  179. *
  180. * Allocate and initialize struct pinned_pfns to be able to hold @nr_pfns
  181. * pfns.
  182. */
  183. struct frame_vector *frame_vector_create(unsigned int nr_frames)
  184. {
  185. struct frame_vector *vec;
  186. int size = sizeof(struct frame_vector) + sizeof(void *) * nr_frames;
  187. if (WARN_ON_ONCE(nr_frames == 0))
  188. return NULL;
  189. /*
  190. * This is absurdly high. It's here just to avoid strange effects when
  191. * arithmetics overflows.
  192. */
  193. if (WARN_ON_ONCE(nr_frames > INT_MAX / sizeof(void *) / 2))
  194. return NULL;
  195. /*
  196. * Avoid higher order allocations, use vmalloc instead. It should
  197. * be rare anyway.
  198. */
  199. if (size <= PAGE_SIZE)
  200. vec = kmalloc(size, GFP_KERNEL);
  201. else
  202. vec = vmalloc(size);
  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);