highmem.h 5.8 KB

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