internal.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. /* internal.h: mm/ internal definitions
  2. *
  3. * Copyright (C) 2004 Red Hat, Inc. All Rights Reserved.
  4. * Written by David Howells (dhowells@redhat.com)
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version
  9. * 2 of the License, or (at your option) any later version.
  10. */
  11. #ifndef __MM_INTERNAL_H
  12. #define __MM_INTERNAL_H
  13. #include <linux/mm.h>
  14. void free_pgtables(struct mmu_gather *tlb, struct vm_area_struct *start_vma,
  15. unsigned long floor, unsigned long ceiling);
  16. static inline void set_page_count(struct page *page, int v)
  17. {
  18. atomic_set(&page->_count, v);
  19. }
  20. /*
  21. * Turn a non-refcounted page (->_count == 0) into refcounted with
  22. * a count of one.
  23. */
  24. static inline void set_page_refcounted(struct page *page)
  25. {
  26. VM_BUG_ON(PageTail(page));
  27. VM_BUG_ON(atomic_read(&page->_count));
  28. set_page_count(page, 1);
  29. }
  30. static inline void __put_page(struct page *page)
  31. {
  32. atomic_dec(&page->_count);
  33. }
  34. static inline void __get_page_tail_foll(struct page *page,
  35. bool get_page_head)
  36. {
  37. /*
  38. * If we're getting a tail page, the elevated page->_count is
  39. * required only in the head page and we will elevate the head
  40. * page->_count and tail page->_mapcount.
  41. *
  42. * We elevate page_tail->_mapcount for tail pages to force
  43. * page_tail->_count to be zero at all times to avoid getting
  44. * false positives from get_page_unless_zero() with
  45. * speculative page access (like in
  46. * page_cache_get_speculative()) on tail pages.
  47. */
  48. VM_BUG_ON(atomic_read(&page->first_page->_count) <= 0);
  49. VM_BUG_ON(atomic_read(&page->_count) != 0);
  50. VM_BUG_ON(page_mapcount(page) < 0);
  51. if (get_page_head)
  52. atomic_inc(&page->first_page->_count);
  53. atomic_inc(&page->_mapcount);
  54. }
  55. /*
  56. * This is meant to be called as the FOLL_GET operation of
  57. * follow_page() and it must be called while holding the proper PT
  58. * lock while the pte (or pmd_trans_huge) is still mapping the page.
  59. */
  60. static inline void get_page_foll(struct page *page)
  61. {
  62. if (unlikely(PageTail(page)))
  63. /*
  64. * This is safe only because
  65. * __split_huge_page_refcount() can't run under
  66. * get_page_foll() because we hold the proper PT lock.
  67. */
  68. __get_page_tail_foll(page, true);
  69. else {
  70. /*
  71. * Getting a normal page or the head of a compound page
  72. * requires to already have an elevated page->_count.
  73. */
  74. VM_BUG_ON(atomic_read(&page->_count) <= 0);
  75. atomic_inc(&page->_count);
  76. }
  77. }
  78. extern unsigned long highest_memmap_pfn;
  79. /*
  80. * in mm/vmscan.c:
  81. */
  82. extern int isolate_lru_page(struct page *page);
  83. extern void putback_lru_page(struct page *page);
  84. /*
  85. * in mm/page_alloc.c
  86. */
  87. extern void __free_pages_bootmem(struct page *page, unsigned int order);
  88. extern void prep_compound_page(struct page *page, unsigned long order);
  89. #ifdef CONFIG_MEMORY_FAILURE
  90. extern bool is_free_buddy_page(struct page *page);
  91. #endif
  92. /*
  93. * function for dealing with page's order in buddy system.
  94. * zone->lock is already acquired when we use these.
  95. * So, we don't need atomic page->flags operations here.
  96. */
  97. static inline unsigned long page_order(struct page *page)
  98. {
  99. /* PageBuddy() must be checked by the caller */
  100. return page_private(page);
  101. }
  102. /* mm/util.c */
  103. void __vma_link_list(struct mm_struct *mm, struct vm_area_struct *vma,
  104. struct vm_area_struct *prev, struct rb_node *rb_parent);
  105. #ifdef CONFIG_MMU
  106. extern long mlock_vma_pages_range(struct vm_area_struct *vma,
  107. unsigned long start, unsigned long end);
  108. extern void munlock_vma_pages_range(struct vm_area_struct *vma,
  109. unsigned long start, unsigned long end);
  110. static inline void munlock_vma_pages_all(struct vm_area_struct *vma)
  111. {
  112. munlock_vma_pages_range(vma, vma->vm_start, vma->vm_end);
  113. }
  114. /*
  115. * Called only in fault path via page_evictable() for a new page
  116. * to determine if it's being mapped into a LOCKED vma.
  117. * If so, mark page as mlocked.
  118. */
  119. static inline int is_mlocked_vma(struct vm_area_struct *vma, struct page *page)
  120. {
  121. VM_BUG_ON(PageLRU(page));
  122. if (likely((vma->vm_flags & (VM_LOCKED | VM_SPECIAL)) != VM_LOCKED))
  123. return 0;
  124. if (!TestSetPageMlocked(page)) {
  125. inc_zone_page_state(page, NR_MLOCK);
  126. count_vm_event(UNEVICTABLE_PGMLOCKED);
  127. }
  128. return 1;
  129. }
  130. /*
  131. * must be called with vma's mmap_sem held for read or write, and page locked.
  132. */
  133. extern void mlock_vma_page(struct page *page);
  134. extern void munlock_vma_page(struct page *page);
  135. /*
  136. * Clear the page's PageMlocked(). This can be useful in a situation where
  137. * we want to unconditionally remove a page from the pagecache -- e.g.,
  138. * on truncation or freeing.
  139. *
  140. * It is legal to call this function for any page, mlocked or not.
  141. * If called for a page that is still mapped by mlocked vmas, all we do
  142. * is revert to lazy LRU behaviour -- semantics are not broken.
  143. */
  144. extern void __clear_page_mlock(struct page *page);
  145. static inline void clear_page_mlock(struct page *page)
  146. {
  147. if (unlikely(TestClearPageMlocked(page)))
  148. __clear_page_mlock(page);
  149. }
  150. /*
  151. * mlock_migrate_page - called only from migrate_page_copy() to
  152. * migrate the Mlocked page flag; update statistics.
  153. */
  154. static inline void mlock_migrate_page(struct page *newpage, struct page *page)
  155. {
  156. if (TestClearPageMlocked(page)) {
  157. unsigned long flags;
  158. local_irq_save(flags);
  159. __dec_zone_page_state(page, NR_MLOCK);
  160. SetPageMlocked(newpage);
  161. __inc_zone_page_state(newpage, NR_MLOCK);
  162. local_irq_restore(flags);
  163. }
  164. }
  165. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  166. extern unsigned long vma_address(struct page *page,
  167. struct vm_area_struct *vma);
  168. #endif
  169. #else /* !CONFIG_MMU */
  170. static inline int is_mlocked_vma(struct vm_area_struct *v, struct page *p)
  171. {
  172. return 0;
  173. }
  174. static inline void clear_page_mlock(struct page *page) { }
  175. static inline void mlock_vma_page(struct page *page) { }
  176. static inline void mlock_migrate_page(struct page *new, struct page *old) { }
  177. #endif /* !CONFIG_MMU */
  178. /*
  179. * Return the mem_map entry representing the 'offset' subpage within
  180. * the maximally aligned gigantic page 'base'. Handle any discontiguity
  181. * in the mem_map at MAX_ORDER_NR_PAGES boundaries.
  182. */
  183. static inline struct page *mem_map_offset(struct page *base, int offset)
  184. {
  185. if (unlikely(offset >= MAX_ORDER_NR_PAGES))
  186. return pfn_to_page(page_to_pfn(base) + offset);
  187. return base + offset;
  188. }
  189. /*
  190. * Iterator over all subpages within the maximally aligned gigantic
  191. * page 'base'. Handle any discontiguity in the mem_map.
  192. */
  193. static inline struct page *mem_map_next(struct page *iter,
  194. struct page *base, int offset)
  195. {
  196. if (unlikely((offset & (MAX_ORDER_NR_PAGES - 1)) == 0)) {
  197. unsigned long pfn = page_to_pfn(base) + offset;
  198. if (!pfn_valid(pfn))
  199. return NULL;
  200. return pfn_to_page(pfn);
  201. }
  202. return iter + 1;
  203. }
  204. /*
  205. * FLATMEM and DISCONTIGMEM configurations use alloc_bootmem_node,
  206. * so all functions starting at paging_init should be marked __init
  207. * in those cases. SPARSEMEM, however, allows for memory hotplug,
  208. * and alloc_bootmem_node is not used.
  209. */
  210. #ifdef CONFIG_SPARSEMEM
  211. #define __paginginit __meminit
  212. #else
  213. #define __paginginit __init
  214. #endif
  215. /* Memory initialisation debug and verification */
  216. enum mminit_level {
  217. MMINIT_WARNING,
  218. MMINIT_VERIFY,
  219. MMINIT_TRACE
  220. };
  221. #ifdef CONFIG_DEBUG_MEMORY_INIT
  222. extern int mminit_loglevel;
  223. #define mminit_dprintk(level, prefix, fmt, arg...) \
  224. do { \
  225. if (level < mminit_loglevel) { \
  226. printk(level <= MMINIT_WARNING ? KERN_WARNING : KERN_DEBUG); \
  227. printk(KERN_CONT "mminit::" prefix " " fmt, ##arg); \
  228. } \
  229. } while (0)
  230. extern void mminit_verify_pageflags_layout(void);
  231. extern void mminit_verify_page_links(struct page *page,
  232. enum zone_type zone, unsigned long nid, unsigned long pfn);
  233. extern void mminit_verify_zonelist(void);
  234. #else
  235. static inline void mminit_dprintk(enum mminit_level level,
  236. const char *prefix, const char *fmt, ...)
  237. {
  238. }
  239. static inline void mminit_verify_pageflags_layout(void)
  240. {
  241. }
  242. static inline void mminit_verify_page_links(struct page *page,
  243. enum zone_type zone, unsigned long nid, unsigned long pfn)
  244. {
  245. }
  246. static inline void mminit_verify_zonelist(void)
  247. {
  248. }
  249. #endif /* CONFIG_DEBUG_MEMORY_INIT */
  250. /* mminit_validate_memmodel_limits is independent of CONFIG_DEBUG_MEMORY_INIT */
  251. #if defined(CONFIG_SPARSEMEM)
  252. extern void mminit_validate_memmodel_limits(unsigned long *start_pfn,
  253. unsigned long *end_pfn);
  254. #else
  255. static inline void mminit_validate_memmodel_limits(unsigned long *start_pfn,
  256. unsigned long *end_pfn)
  257. {
  258. }
  259. #endif /* CONFIG_SPARSEMEM */
  260. #define ZONE_RECLAIM_NOSCAN -2
  261. #define ZONE_RECLAIM_FULL -1
  262. #define ZONE_RECLAIM_SOME 0
  263. #define ZONE_RECLAIM_SUCCESS 1
  264. #endif
  265. extern int hwpoison_filter(struct page *p);
  266. extern u32 hwpoison_filter_dev_major;
  267. extern u32 hwpoison_filter_dev_minor;
  268. extern u64 hwpoison_filter_flags_mask;
  269. extern u64 hwpoison_filter_flags_value;
  270. extern u64 hwpoison_filter_memcg;
  271. extern u32 hwpoison_filter_enable;