highmem.h 5.9 KB

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