highmem.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. #ifndef _LINUX_HIGHMEM_H
  2. #define _LINUX_HIGHMEM_H
  3. #include <linux/fs.h>
  4. #include <linux/kernel.h>
  5. #include <linux/bug.h>
  6. #include <linux/mm.h>
  7. #include <linux/uaccess.h>
  8. #include <linux/hardirq.h>
  9. #include <asm/cacheflush.h>
  10. #ifndef ARCH_HAS_FLUSH_ANON_PAGE
  11. static inline void flush_anon_page(struct vm_area_struct *vma, struct page *page, unsigned long vmaddr)
  12. {
  13. }
  14. #endif
  15. #ifndef ARCH_HAS_FLUSH_KERNEL_DCACHE_PAGE
  16. static inline void flush_kernel_dcache_page(struct page *page)
  17. {
  18. }
  19. static inline void flush_kernel_vmap_range(void *vaddr, int size)
  20. {
  21. }
  22. static inline void invalidate_kernel_vmap_range(void *vaddr, int size)
  23. {
  24. }
  25. #endif
  26. #include <asm/kmap_types.h>
  27. #ifdef CONFIG_HIGHMEM
  28. #include <asm/highmem.h>
  29. /* declarations for linux/mm/highmem.c */
  30. unsigned int nr_free_highpages(void);
  31. extern unsigned long totalhigh_pages;
  32. void kmap_flush_unused(void);
  33. #ifdef CONFIG_ARCH_WANT_KMAP_ATOMIC_FLUSH
  34. void kmap_atomic_flush_unused(void);
  35. #else
  36. static inline void kmap_atomic_flush_unused(void) { }
  37. #endif
  38. struct page *kmap_to_page(void *addr);
  39. #else /* CONFIG_HIGHMEM */
  40. static inline unsigned int nr_free_highpages(void) { return 0; }
  41. static inline struct page *kmap_to_page(void *addr)
  42. {
  43. return virt_to_page(addr);
  44. }
  45. #define totalhigh_pages 0UL
  46. #ifndef ARCH_HAS_KMAP
  47. static inline void *kmap(struct page *page)
  48. {
  49. might_sleep();
  50. return page_address(page);
  51. }
  52. static inline void kunmap(struct page *page)
  53. {
  54. }
  55. static inline void *kmap_atomic(struct page *page)
  56. {
  57. pagefault_disable();
  58. return page_address(page);
  59. }
  60. #define kmap_atomic_prot(page, prot) kmap_atomic(page)
  61. static inline void __kunmap_atomic(void *addr)
  62. {
  63. pagefault_enable();
  64. }
  65. #define kmap_atomic_pfn(pfn) kmap_atomic(pfn_to_page(pfn))
  66. #define kmap_atomic_to_page(ptr) virt_to_page(ptr)
  67. #define kmap_flush_unused() do {} while(0)
  68. #define kmap_atomic_flush_unused() do {} while (0)
  69. #endif
  70. #endif /* CONFIG_HIGHMEM */
  71. #if defined(CONFIG_HIGHMEM) || defined(CONFIG_X86_32)
  72. DECLARE_PER_CPU(int, __kmap_atomic_idx);
  73. static inline int kmap_atomic_idx_push(void)
  74. {
  75. int idx = __this_cpu_inc_return(__kmap_atomic_idx) - 1;
  76. #ifdef CONFIG_DEBUG_HIGHMEM
  77. WARN_ON_ONCE(in_irq() && !irqs_disabled());
  78. BUG_ON(idx > KM_TYPE_NR);
  79. #endif
  80. return idx;
  81. }
  82. static inline int kmap_atomic_idx(void)
  83. {
  84. return __this_cpu_read(__kmap_atomic_idx) - 1;
  85. }
  86. static inline void kmap_atomic_idx_pop(void)
  87. {
  88. #ifdef CONFIG_DEBUG_HIGHMEM
  89. int idx = __this_cpu_dec_return(__kmap_atomic_idx);
  90. BUG_ON(idx < 0);
  91. #else
  92. __this_cpu_dec(__kmap_atomic_idx);
  93. #endif
  94. }
  95. #endif
  96. /*
  97. * NOTE:
  98. * kmap_atomic() and kunmap_atomic() with two arguments are deprecated.
  99. * We only keep them for backward compatibility, any usage of them
  100. * are now warned.
  101. */
  102. #define PASTE(a, b) a ## b
  103. #define PASTE2(a, b) PASTE(a, b)
  104. #define NARG_(_2, _1, n, ...) n
  105. #define NARG(...) NARG_(__VA_ARGS__, 2, 1, :)
  106. static inline void __deprecated *kmap_atomic_deprecated(struct page *page,
  107. enum km_type km)
  108. {
  109. return kmap_atomic(page);
  110. }
  111. #define kmap_atomic1(...) kmap_atomic(__VA_ARGS__)
  112. #define kmap_atomic2(...) kmap_atomic_deprecated(__VA_ARGS__)
  113. #define kmap_atomic(...) PASTE2(kmap_atomic, NARG(__VA_ARGS__)(__VA_ARGS__))
  114. static inline void __deprecated __kunmap_atomic_deprecated(void *addr,
  115. enum km_type km)
  116. {
  117. __kunmap_atomic(addr);
  118. }
  119. /*
  120. * Prevent people trying to call kunmap_atomic() as if it were kunmap()
  121. * kunmap_atomic() should get the return value of kmap_atomic, not the page.
  122. */
  123. #define kunmap_atomic_deprecated(addr, km) \
  124. do { \
  125. BUILD_BUG_ON(__same_type((addr), struct page *)); \
  126. __kunmap_atomic_deprecated(addr, km); \
  127. } while (0)
  128. #define kunmap_atomic_withcheck(addr) \
  129. do { \
  130. BUILD_BUG_ON(__same_type((addr), struct page *)); \
  131. __kunmap_atomic(addr); \
  132. } while (0)
  133. #define kunmap_atomic1(...) kunmap_atomic_withcheck(__VA_ARGS__)
  134. #define kunmap_atomic2(...) kunmap_atomic_deprecated(__VA_ARGS__)
  135. #define kunmap_atomic(...) PASTE2(kunmap_atomic, NARG(__VA_ARGS__)(__VA_ARGS__))
  136. /**** End of C pre-processor tricks for deprecated macros ****/
  137. /* when CONFIG_HIGHMEM is not set these will be plain clear/copy_page */
  138. #ifndef clear_user_highpage
  139. static inline void clear_user_highpage(struct page *page, unsigned long vaddr)
  140. {
  141. void *addr = kmap_atomic(page);
  142. clear_user_page(addr, vaddr, page);
  143. kunmap_atomic(addr);
  144. }
  145. #endif
  146. #ifndef __HAVE_ARCH_ALLOC_ZEROED_USER_HIGHPAGE
  147. /**
  148. * __alloc_zeroed_user_highpage - Allocate a zeroed HIGHMEM page for a VMA with caller-specified movable GFP flags
  149. * @movableflags: The GFP flags related to the pages future ability to move like __GFP_MOVABLE
  150. * @vma: The VMA the page is to be allocated for
  151. * @vaddr: The virtual address the page will be inserted into
  152. *
  153. * This function will allocate a page for a VMA but the caller is expected
  154. * to specify via movableflags whether the page will be movable in the
  155. * future or not
  156. *
  157. * An architecture may override this function by defining
  158. * __HAVE_ARCH_ALLOC_ZEROED_USER_HIGHPAGE and providing their own
  159. * implementation.
  160. */
  161. static inline struct page *
  162. __alloc_zeroed_user_highpage(gfp_t movableflags,
  163. struct vm_area_struct *vma,
  164. unsigned long vaddr)
  165. {
  166. struct page *page = alloc_page_vma(GFP_HIGHUSER | movableflags,
  167. vma, vaddr);
  168. if (page)
  169. clear_user_highpage(page, vaddr);
  170. return page;
  171. }
  172. #endif
  173. /**
  174. * alloc_zeroed_user_highpage_movable - Allocate a zeroed HIGHMEM page for a VMA that the caller knows can move
  175. * @vma: The VMA the page is to be allocated for
  176. * @vaddr: The virtual address the page will be inserted into
  177. *
  178. * This function will allocate a page for a VMA that the caller knows will
  179. * be able to migrate in the future using move_pages() or reclaimed
  180. */
  181. static inline struct page *
  182. alloc_zeroed_user_highpage_movable(struct vm_area_struct *vma,
  183. unsigned long vaddr)
  184. {
  185. #ifndef CONFIG_CMA
  186. return __alloc_zeroed_user_highpage(__GFP_MOVABLE, vma, vaddr);
  187. #else
  188. return __alloc_zeroed_user_highpage(__GFP_MOVABLE|__GFP_CMA, vma,
  189. vaddr);
  190. #endif
  191. }
  192. #ifdef CONFIG_CMA
  193. static inline struct page *
  194. alloc_zeroed_user_highpage_movable_cma(struct vm_area_struct *vma,
  195. unsigned long vaddr)
  196. {
  197. return __alloc_zeroed_user_highpage(__GFP_MOVABLE|__GFP_CMA, vma,
  198. vaddr);
  199. }
  200. #endif
  201. static inline void clear_highpage(struct page *page)
  202. {
  203. void *kaddr = kmap_atomic(page);
  204. clear_page(kaddr);
  205. kunmap_atomic(kaddr);
  206. }
  207. static inline void zero_user_segments(struct page *page,
  208. unsigned start1, unsigned end1,
  209. unsigned start2, unsigned end2)
  210. {
  211. void *kaddr = kmap_atomic(page);
  212. BUG_ON(end1 > PAGE_SIZE || end2 > PAGE_SIZE);
  213. if (end1 > start1)
  214. memset(kaddr + start1, 0, end1 - start1);
  215. if (end2 > start2)
  216. memset(kaddr + start2, 0, end2 - start2);
  217. kunmap_atomic(kaddr);
  218. flush_dcache_page(page);
  219. }
  220. static inline void zero_user_segment(struct page *page,
  221. unsigned start, unsigned end)
  222. {
  223. zero_user_segments(page, start, end, 0, 0);
  224. }
  225. static inline void zero_user(struct page *page,
  226. unsigned start, unsigned size)
  227. {
  228. zero_user_segments(page, start, start + size, 0, 0);
  229. }
  230. static inline void __deprecated memclear_highpage_flush(struct page *page,
  231. unsigned int offset, unsigned int size)
  232. {
  233. zero_user(page, offset, size);
  234. }
  235. #ifndef __HAVE_ARCH_COPY_USER_HIGHPAGE
  236. static inline void copy_user_highpage(struct page *to, struct page *from,
  237. unsigned long vaddr, struct vm_area_struct *vma)
  238. {
  239. char *vfrom, *vto;
  240. vfrom = kmap_atomic(from);
  241. vto = kmap_atomic(to);
  242. copy_user_page(vto, vfrom, vaddr, to);
  243. kunmap_atomic(vto);
  244. kunmap_atomic(vfrom);
  245. }
  246. #endif
  247. static inline void copy_highpage(struct page *to, struct page *from)
  248. {
  249. char *vfrom, *vto;
  250. vfrom = kmap_atomic(from);
  251. vto = kmap_atomic(to);
  252. copy_page(vto, vfrom);
  253. kunmap_atomic(vto);
  254. kunmap_atomic(vfrom);
  255. }
  256. #endif /* _LINUX_HIGHMEM_H */