highmem.h 5.8 KB

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