frame_vector.c 6.1 KB

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