frame_vector.c 6.2 KB

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