highmem.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. #ifndef _LINUX_HIGHMEM_H
  2. #define _LINUX_HIGHMEM_H
  3. #include <linux/fs.h>
  4. #include <linux/kernel.h>
  5. #include <linux/mm.h>
  6. #include <linux/uaccess.h>
  7. #include <linux/hardirq.h>
  8. #include <asm/cacheflush.h>
  9. #ifndef ARCH_HAS_FLUSH_ANON_PAGE
  10. static inline void flush_anon_page(struct vm_area_struct *vma, struct page *page, unsigned long vmaddr)
  11. {
  12. }
  13. #endif
  14. #ifndef ARCH_HAS_FLUSH_KERNEL_DCACHE_PAGE
  15. static inline void flush_kernel_dcache_page(struct page *page)
  16. {
  17. }
  18. static inline void flush_kernel_vmap_range(void *vaddr, int size)
  19. {
  20. }
  21. static inline void invalidate_kernel_vmap_range(void *vaddr, int size)
  22. {
  23. }
  24. #endif
  25. #include <asm/kmap_types.h>
  26. #ifdef CONFIG_HIGHMEM
  27. #include <asm/highmem.h>
  28. /* declarations for linux/mm/highmem.c */
  29. unsigned int nr_free_highpages(void);
  30. extern unsigned long totalhigh_pages;
  31. void kmap_flush_unused(void);
  32. #else /* CONFIG_HIGHMEM */
  33. static inline unsigned int nr_free_highpages(void) { return 0; }
  34. #define totalhigh_pages 0UL
  35. #ifndef ARCH_HAS_KMAP
  36. static inline void *kmap(struct page *page)
  37. {
  38. might_sleep();
  39. return page_address(page);
  40. }
  41. static inline void kunmap(struct page *page)
  42. {
  43. }
  44. static inline void *__kmap_atomic(struct page *page)
  45. {
  46. pagefault_disable();
  47. return page_address(page);
  48. }
  49. #define kmap_atomic_prot(page, prot) __kmap_atomic(page)
  50. static inline void __kunmap_atomic(void *addr)
  51. {
  52. pagefault_enable();
  53. }
  54. #define kmap_atomic_pfn(pfn) kmap_atomic(pfn_to_page(pfn))
  55. #define kmap_atomic_to_page(ptr) virt_to_page(ptr)
  56. #define kmap_flush_unused() do {} while(0)
  57. #endif
  58. #endif /* CONFIG_HIGHMEM */
  59. #if defined(CONFIG_HIGHMEM) || defined(CONFIG_X86_32)
  60. DECLARE_PER_CPU(int, __kmap_atomic_idx);
  61. static inline int kmap_atomic_idx_push(void)
  62. {
  63. int idx = __this_cpu_inc_return(__kmap_atomic_idx) - 1;
  64. #ifdef CONFIG_DEBUG_HIGHMEM
  65. WARN_ON_ONCE(in_irq() && !irqs_disabled());
  66. BUG_ON(idx > KM_TYPE_NR);
  67. #endif
  68. return idx;
  69. }
  70. static inline int kmap_atomic_idx(void)
  71. {
  72. return __this_cpu_read(__kmap_atomic_idx) - 1;
  73. }
  74. static inline void kmap_atomic_idx_pop(void)
  75. {
  76. #ifdef CONFIG_DEBUG_HIGHMEM
  77. int idx = __this_cpu_dec_return(__kmap_atomic_idx);
  78. BUG_ON(idx < 0);
  79. #else
  80. __this_cpu_dec(__kmap_atomic_idx);
  81. #endif
  82. }
  83. #endif
  84. /*
  85. * Make both: kmap_atomic(page, idx) and kmap_atomic(page) work.
  86. */
  87. #define kmap_atomic(page, args...) __kmap_atomic(page)
  88. /*
  89. * Prevent people trying to call kunmap_atomic() as if it were kunmap()
  90. * kunmap_atomic() should get the return value of kmap_atomic, not the page.
  91. */
  92. #define kunmap_atomic(addr, args...) \
  93. do { \
  94. BUILD_BUG_ON(__same_type((addr), struct page *)); \
  95. __kunmap_atomic(addr); \
  96. } while (0)
  97. /* when CONFIG_HIGHMEM is not set these will be plain clear/copy_page */
  98. #ifndef clear_user_highpage
  99. static inline void clear_user_highpage(struct page *page, unsigned long vaddr)
  100. {
  101. void *addr = kmap_atomic(page, KM_USER0);
  102. clear_user_page(addr, vaddr, page);
  103. kunmap_atomic(addr, KM_USER0);
  104. }
  105. #endif
  106. #ifndef __HAVE_ARCH_ALLOC_ZEROED_USER_HIGHPAGE
  107. /**
  108. * __alloc_zeroed_user_highpage - Allocate a zeroed HIGHMEM page for a VMA with caller-specified movable GFP flags
  109. * @movableflags: The GFP flags related to the pages future ability to move like __GFP_MOVABLE
  110. * @vma: The VMA the page is to be allocated for
  111. * @vaddr: The virtual address the page will be inserted into
  112. *
  113. * This function will allocate a page for a VMA but the caller is expected
  114. * to specify via movableflags whether the page will be movable in the
  115. * future or not
  116. *
  117. * An architecture may override this function by defining
  118. * __HAVE_ARCH_ALLOC_ZEROED_USER_HIGHPAGE and providing their own
  119. * implementation.
  120. */
  121. static inline struct page *
  122. __alloc_zeroed_user_highpage(gfp_t movableflags,
  123. struct vm_area_struct *vma,
  124. unsigned long vaddr)
  125. {
  126. struct page *page = alloc_page_vma(GFP_HIGHUSER | movableflags,
  127. vma, vaddr);
  128. if (page)
  129. clear_user_highpage(page, vaddr);
  130. return page;
  131. }
  132. #endif
  133. /**
  134. * alloc_zeroed_user_highpage_movable - Allocate a zeroed HIGHMEM page for a VMA that the caller knows can move
  135. * @vma: The VMA the page is to be allocated for
  136. * @vaddr: The virtual address the page will be inserted into
  137. *
  138. * This function will allocate a page for a VMA that the caller knows will
  139. * be able to migrate in the future using move_pages() or reclaimed
  140. */
  141. static inline struct page *
  142. alloc_zeroed_user_highpage_movable(struct vm_area_struct *vma,
  143. unsigned long vaddr)
  144. {
  145. return __alloc_zeroed_user_highpage(__GFP_MOVABLE, vma, vaddr);
  146. }
  147. static inline void clear_highpage(struct page *page)
  148. {
  149. void *kaddr = kmap_atomic(page, KM_USER0);
  150. clear_page(kaddr);
  151. kunmap_atomic(kaddr, KM_USER0);
  152. }
  153. static inline void zero_user_segments(struct page *page,
  154. unsigned start1, unsigned end1,
  155. unsigned start2, unsigned end2)
  156. {
  157. void *kaddr = kmap_atomic(page, KM_USER0);
  158. BUG_ON(end1 > PAGE_SIZE || end2 > PAGE_SIZE);
  159. if (end1 > start1)
  160. memset(kaddr + start1, 0, end1 - start1);
  161. if (end2 > start2)
  162. memset(kaddr + start2, 0, end2 - start2);
  163. kunmap_atomic(kaddr, KM_USER0);
  164. flush_dcache_page(page);
  165. }
  166. static inline void zero_user_segment(struct page *page,
  167. unsigned start, unsigned end)
  168. {
  169. zero_user_segments(page, start, end, 0, 0);
  170. }
  171. static inline void zero_user(struct page *page,
  172. unsigned start, unsigned size)
  173. {
  174. zero_user_segments(page, start, start + size, 0, 0);
  175. }
  176. static inline void __deprecated memclear_highpage_flush(struct page *page,
  177. unsigned int offset, unsigned int size)
  178. {
  179. zero_user(page, offset, size);
  180. }
  181. #ifndef __HAVE_ARCH_COPY_USER_HIGHPAGE
  182. static inline void copy_user_highpage(struct page *to, struct page *from,
  183. unsigned long vaddr, struct vm_area_struct *vma)
  184. {
  185. char *vfrom, *vto;
  186. vfrom = kmap_atomic(from, KM_USER0);
  187. vto = kmap_atomic(to, KM_USER1);
  188. copy_user_page(vto, vfrom, vaddr, to);
  189. kunmap_atomic(vto, KM_USER1);
  190. kunmap_atomic(vfrom, KM_USER0);
  191. }
  192. #endif
  193. static inline void copy_highpage(struct page *to, struct page *from)
  194. {
  195. char *vfrom, *vto;
  196. vfrom = kmap_atomic(from, KM_USER0);
  197. vto = kmap_atomic(to, KM_USER1);
  198. copy_page(vto, vfrom);
  199. kunmap_atomic(vto, KM_USER1);
  200. kunmap_atomic(vfrom, KM_USER0);
  201. }
  202. #endif /* _LINUX_HIGHMEM_H */