internal.h 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491
  1. /* SPDX-License-Identifier: GPL-2.0-or-later */
  2. /* internal.h: mm/ internal definitions
  3. *
  4. * Copyright (C) 2004 Red Hat, Inc. All Rights Reserved.
  5. * Written by David Howells (dhowells@redhat.com)
  6. */
  7. #ifndef __MM_INTERNAL_H
  8. #define __MM_INTERNAL_H
  9. #include <linux/fs.h>
  10. #include <linux/khugepaged.h>
  11. #include <linux/mm.h>
  12. #include <linux/mm_inline.h>
  13. #include <linux/pagemap.h>
  14. #include <linux/rmap.h>
  15. #include <linux/swap.h>
  16. #include <linux/swapops.h>
  17. #include <linux/swap_cgroup.h>
  18. #include <linux/tracepoint-defs.h>
  19. /* Internal core VMA manipulation functions. */
  20. #include "vma.h"
  21. struct folio_batch;
  22. /*
  23. * The set of flags that only affect watermark checking and reclaim
  24. * behaviour. This is used by the MM to obey the caller constraints
  25. * about IO, FS and watermark checking while ignoring placement
  26. * hints such as HIGHMEM usage.
  27. */
  28. #define GFP_RECLAIM_MASK (__GFP_RECLAIM|__GFP_HIGH|__GFP_IO|__GFP_FS|\
  29. __GFP_NOWARN|__GFP_RETRY_MAYFAIL|__GFP_NOFAIL|\
  30. __GFP_NORETRY|__GFP_MEMALLOC|__GFP_NOMEMALLOC|\
  31. __GFP_NOLOCKDEP)
  32. /* The GFP flags allowed during early boot */
  33. #define GFP_BOOT_MASK (__GFP_BITS_MASK & ~(__GFP_RECLAIM|__GFP_IO|__GFP_FS))
  34. /* Control allocation cpuset and node placement constraints */
  35. #define GFP_CONSTRAINT_MASK (__GFP_HARDWALL|__GFP_THISNODE)
  36. /* Do not use these with a slab allocator */
  37. #define GFP_SLAB_BUG_MASK (__GFP_DMA32|__GFP_HIGHMEM|~__GFP_BITS_MASK)
  38. /*
  39. * Different from WARN_ON_ONCE(), no warning will be issued
  40. * when we specify __GFP_NOWARN.
  41. */
  42. #define WARN_ON_ONCE_GFP(cond, gfp) ({ \
  43. static bool __section(".data..once") __warned; \
  44. int __ret_warn_once = !!(cond); \
  45. \
  46. if (unlikely(!(gfp & __GFP_NOWARN) && __ret_warn_once && !__warned)) { \
  47. __warned = true; \
  48. WARN_ON(1); \
  49. } \
  50. unlikely(__ret_warn_once); \
  51. })
  52. void page_writeback_init(void);
  53. /*
  54. * If a 16GB hugetlb folio were mapped by PTEs of all of its 4kB pages,
  55. * its nr_pages_mapped would be 0x400000: choose the ENTIRELY_MAPPED bit
  56. * above that range, instead of 2*(PMD_SIZE/PAGE_SIZE). Hugetlb currently
  57. * leaves nr_pages_mapped at 0, but avoid surprise if it participates later.
  58. */
  59. #define ENTIRELY_MAPPED 0x800000
  60. #define FOLIO_PAGES_MAPPED (ENTIRELY_MAPPED - 1)
  61. /*
  62. * Flags passed to __show_mem() and show_free_areas() to suppress output in
  63. * various contexts.
  64. */
  65. #define SHOW_MEM_FILTER_NODES (0x0001u) /* disallowed nodes */
  66. /*
  67. * How many individual pages have an elevated _mapcount. Excludes
  68. * the folio's entire_mapcount.
  69. *
  70. * Don't use this function outside of debugging code.
  71. */
  72. static inline int folio_nr_pages_mapped(const struct folio *folio)
  73. {
  74. return atomic_read(&folio->_nr_pages_mapped) & FOLIO_PAGES_MAPPED;
  75. }
  76. /*
  77. * Retrieve the first entry of a folio based on a provided entry within the
  78. * folio. We cannot rely on folio->swap as there is no guarantee that it has
  79. * been initialized. Used for calling arch_swap_restore()
  80. */
  81. static inline swp_entry_t folio_swap(swp_entry_t entry,
  82. const struct folio *folio)
  83. {
  84. swp_entry_t swap = {
  85. .val = ALIGN_DOWN(entry.val, folio_nr_pages(folio)),
  86. };
  87. return swap;
  88. }
  89. static inline void *folio_raw_mapping(const struct folio *folio)
  90. {
  91. unsigned long mapping = (unsigned long)folio->mapping;
  92. return (void *)(mapping & ~PAGE_MAPPING_FLAGS);
  93. }
  94. /*
  95. * This is a file-backed mapping, and is about to be memory mapped - invoke its
  96. * mmap hook and safely handle error conditions. On error, VMA hooks will be
  97. * mutated.
  98. *
  99. * @file: File which backs the mapping.
  100. * @vma: VMA which we are mapping.
  101. *
  102. * Returns: 0 if success, error otherwise.
  103. */
  104. static inline int mmap_file(struct file *file, struct vm_area_struct *vma)
  105. {
  106. int err = call_mmap(file, vma);
  107. if (likely(!err))
  108. return 0;
  109. /*
  110. * OK, we tried to call the file hook for mmap(), but an error
  111. * arose. The mapping is in an inconsistent state and we most not invoke
  112. * any further hooks on it.
  113. */
  114. vma->vm_ops = &vma_dummy_vm_ops;
  115. return err;
  116. }
  117. /*
  118. * If the VMA has a close hook then close it, and since closing it might leave
  119. * it in an inconsistent state which makes the use of any hooks suspect, clear
  120. * them down by installing dummy empty hooks.
  121. */
  122. static inline void vma_close(struct vm_area_struct *vma)
  123. {
  124. if (vma->vm_ops && vma->vm_ops->close) {
  125. vma->vm_ops->close(vma);
  126. /*
  127. * The mapping is in an inconsistent state, and no further hooks
  128. * may be invoked upon it.
  129. */
  130. vma->vm_ops = &vma_dummy_vm_ops;
  131. }
  132. }
  133. #ifdef CONFIG_MMU
  134. /* Flags for folio_pte_batch(). */
  135. typedef int __bitwise fpb_t;
  136. /* Compare PTEs after pte_mkclean(), ignoring the dirty bit. */
  137. #define FPB_IGNORE_DIRTY ((__force fpb_t)BIT(0))
  138. /* Compare PTEs after pte_clear_soft_dirty(), ignoring the soft-dirty bit. */
  139. #define FPB_IGNORE_SOFT_DIRTY ((__force fpb_t)BIT(1))
  140. static inline pte_t __pte_batch_clear_ignored(pte_t pte, fpb_t flags)
  141. {
  142. if (flags & FPB_IGNORE_DIRTY)
  143. pte = pte_mkclean(pte);
  144. if (likely(flags & FPB_IGNORE_SOFT_DIRTY))
  145. pte = pte_clear_soft_dirty(pte);
  146. return pte_wrprotect(pte_mkold(pte));
  147. }
  148. /**
  149. * folio_pte_batch - detect a PTE batch for a large folio
  150. * @folio: The large folio to detect a PTE batch for.
  151. * @addr: The user virtual address the first page is mapped at.
  152. * @start_ptep: Page table pointer for the first entry.
  153. * @pte: Page table entry for the first page.
  154. * @max_nr: The maximum number of table entries to consider.
  155. * @flags: Flags to modify the PTE batch semantics.
  156. * @any_writable: Optional pointer to indicate whether any entry except the
  157. * first one is writable.
  158. * @any_young: Optional pointer to indicate whether any entry except the
  159. * first one is young.
  160. * @any_dirty: Optional pointer to indicate whether any entry except the
  161. * first one is dirty.
  162. *
  163. * Detect a PTE batch: consecutive (present) PTEs that map consecutive
  164. * pages of the same large folio.
  165. *
  166. * All PTEs inside a PTE batch have the same PTE bits set, excluding the PFN,
  167. * the accessed bit, writable bit, dirty bit (with FPB_IGNORE_DIRTY) and
  168. * soft-dirty bit (with FPB_IGNORE_SOFT_DIRTY).
  169. *
  170. * start_ptep must map any page of the folio. max_nr must be at least one and
  171. * must be limited by the caller so scanning cannot exceed a single page table.
  172. *
  173. * Return: the number of table entries in the batch.
  174. */
  175. static inline int folio_pte_batch(struct folio *folio, unsigned long addr,
  176. pte_t *start_ptep, pte_t pte, int max_nr, fpb_t flags,
  177. bool *any_writable, bool *any_young, bool *any_dirty)
  178. {
  179. unsigned long folio_end_pfn = folio_pfn(folio) + folio_nr_pages(folio);
  180. const pte_t *end_ptep = start_ptep + max_nr;
  181. pte_t expected_pte, *ptep;
  182. bool writable, young, dirty;
  183. int nr;
  184. if (any_writable)
  185. *any_writable = false;
  186. if (any_young)
  187. *any_young = false;
  188. if (any_dirty)
  189. *any_dirty = false;
  190. VM_WARN_ON_FOLIO(!pte_present(pte), folio);
  191. VM_WARN_ON_FOLIO(!folio_test_large(folio) || max_nr < 1, folio);
  192. VM_WARN_ON_FOLIO(page_folio(pfn_to_page(pte_pfn(pte))) != folio, folio);
  193. nr = pte_batch_hint(start_ptep, pte);
  194. expected_pte = __pte_batch_clear_ignored(pte_advance_pfn(pte, nr), flags);
  195. ptep = start_ptep + nr;
  196. while (ptep < end_ptep) {
  197. pte = ptep_get(ptep);
  198. if (any_writable)
  199. writable = !!pte_write(pte);
  200. if (any_young)
  201. young = !!pte_young(pte);
  202. if (any_dirty)
  203. dirty = !!pte_dirty(pte);
  204. pte = __pte_batch_clear_ignored(pte, flags);
  205. if (!pte_same(pte, expected_pte))
  206. break;
  207. /*
  208. * Stop immediately once we reached the end of the folio. In
  209. * corner cases the next PFN might fall into a different
  210. * folio.
  211. */
  212. if (pte_pfn(pte) >= folio_end_pfn)
  213. break;
  214. if (any_writable)
  215. *any_writable |= writable;
  216. if (any_young)
  217. *any_young |= young;
  218. if (any_dirty)
  219. *any_dirty |= dirty;
  220. nr = pte_batch_hint(ptep, pte);
  221. expected_pte = pte_advance_pfn(expected_pte, nr);
  222. ptep += nr;
  223. }
  224. return min(ptep - start_ptep, max_nr);
  225. }
  226. /**
  227. * pte_move_swp_offset - Move the swap entry offset field of a swap pte
  228. * forward or backward by delta
  229. * @pte: The initial pte state; is_swap_pte(pte) must be true and
  230. * non_swap_entry() must be false.
  231. * @delta: The direction and the offset we are moving; forward if delta
  232. * is positive; backward if delta is negative
  233. *
  234. * Moves the swap offset, while maintaining all other fields, including
  235. * swap type, and any swp pte bits. The resulting pte is returned.
  236. */
  237. static inline pte_t pte_move_swp_offset(pte_t pte, long delta)
  238. {
  239. swp_entry_t entry = pte_to_swp_entry(pte);
  240. pte_t new = __swp_entry_to_pte(__swp_entry(swp_type(entry),
  241. (swp_offset(entry) + delta)));
  242. if (pte_swp_soft_dirty(pte))
  243. new = pte_swp_mksoft_dirty(new);
  244. if (pte_swp_exclusive(pte))
  245. new = pte_swp_mkexclusive(new);
  246. if (pte_swp_uffd_wp(pte))
  247. new = pte_swp_mkuffd_wp(new);
  248. return new;
  249. }
  250. /**
  251. * pte_next_swp_offset - Increment the swap entry offset field of a swap pte.
  252. * @pte: The initial pte state; is_swap_pte(pte) must be true and
  253. * non_swap_entry() must be false.
  254. *
  255. * Increments the swap offset, while maintaining all other fields, including
  256. * swap type, and any swp pte bits. The resulting pte is returned.
  257. */
  258. static inline pte_t pte_next_swp_offset(pte_t pte)
  259. {
  260. return pte_move_swp_offset(pte, 1);
  261. }
  262. /**
  263. * swap_pte_batch - detect a PTE batch for a set of contiguous swap entries
  264. * @start_ptep: Page table pointer for the first entry.
  265. * @max_nr: The maximum number of table entries to consider.
  266. * @pte: Page table entry for the first entry.
  267. *
  268. * Detect a batch of contiguous swap entries: consecutive (non-present) PTEs
  269. * containing swap entries all with consecutive offsets and targeting the same
  270. * swap type, all with matching swp pte bits.
  271. *
  272. * max_nr must be at least one and must be limited by the caller so scanning
  273. * cannot exceed a single page table.
  274. *
  275. * Return: the number of table entries in the batch.
  276. */
  277. static inline int swap_pte_batch(pte_t *start_ptep, int max_nr, pte_t pte)
  278. {
  279. pte_t expected_pte = pte_next_swp_offset(pte);
  280. const pte_t *end_ptep = start_ptep + max_nr;
  281. swp_entry_t entry = pte_to_swp_entry(pte);
  282. pte_t *ptep = start_ptep + 1;
  283. unsigned short cgroup_id;
  284. VM_WARN_ON(max_nr < 1);
  285. VM_WARN_ON(!is_swap_pte(pte));
  286. VM_WARN_ON(non_swap_entry(entry));
  287. cgroup_id = lookup_swap_cgroup_id(entry);
  288. while (ptep < end_ptep) {
  289. pte = ptep_get(ptep);
  290. if (!pte_same(pte, expected_pte))
  291. break;
  292. if (lookup_swap_cgroup_id(pte_to_swp_entry(pte)) != cgroup_id)
  293. break;
  294. expected_pte = pte_next_swp_offset(expected_pte);
  295. ptep++;
  296. }
  297. return ptep - start_ptep;
  298. }
  299. #endif /* CONFIG_MMU */
  300. void __acct_reclaim_writeback(pg_data_t *pgdat, struct folio *folio,
  301. int nr_throttled);
  302. static inline void acct_reclaim_writeback(struct folio *folio)
  303. {
  304. pg_data_t *pgdat = folio_pgdat(folio);
  305. int nr_throttled = atomic_read(&pgdat->nr_writeback_throttled);
  306. if (nr_throttled)
  307. __acct_reclaim_writeback(pgdat, folio, nr_throttled);
  308. }
  309. static inline void wake_throttle_isolated(pg_data_t *pgdat)
  310. {
  311. wait_queue_head_t *wqh;
  312. wqh = &pgdat->reclaim_wait[VMSCAN_THROTTLE_ISOLATED];
  313. if (waitqueue_active(wqh))
  314. wake_up(wqh);
  315. }
  316. vm_fault_t __vmf_anon_prepare(struct vm_fault *vmf);
  317. static inline vm_fault_t vmf_anon_prepare(struct vm_fault *vmf)
  318. {
  319. vm_fault_t ret = __vmf_anon_prepare(vmf);
  320. if (unlikely(ret & VM_FAULT_RETRY))
  321. vma_end_read(vmf->vma);
  322. return ret;
  323. }
  324. vm_fault_t do_swap_page(struct vm_fault *vmf);
  325. void folio_rotate_reclaimable(struct folio *folio);
  326. bool __folio_end_writeback(struct folio *folio);
  327. void deactivate_file_folio(struct folio *folio);
  328. void folio_activate(struct folio *folio);
  329. void free_pgtables(struct mmu_gather *tlb, struct ma_state *mas,
  330. struct vm_area_struct *start_vma, unsigned long floor,
  331. unsigned long ceiling, bool mm_wr_locked);
  332. void pmd_install(struct mm_struct *mm, pmd_t *pmd, pgtable_t *pte);
  333. struct zap_details;
  334. void unmap_page_range(struct mmu_gather *tlb,
  335. struct vm_area_struct *vma,
  336. unsigned long addr, unsigned long end,
  337. struct zap_details *details);
  338. void page_cache_ra_order(struct readahead_control *, struct file_ra_state *,
  339. unsigned int order);
  340. void force_page_cache_ra(struct readahead_control *, unsigned long nr);
  341. static inline void force_page_cache_readahead(struct address_space *mapping,
  342. struct file *file, pgoff_t index, unsigned long nr_to_read)
  343. {
  344. DEFINE_READAHEAD(ractl, file, &file->f_ra, mapping, index);
  345. force_page_cache_ra(&ractl, nr_to_read);
  346. }
  347. unsigned find_lock_entries(struct address_space *mapping, pgoff_t *start,
  348. pgoff_t end, struct folio_batch *fbatch, pgoff_t *indices);
  349. unsigned find_get_entries(struct address_space *mapping, pgoff_t *start,
  350. pgoff_t end, struct folio_batch *fbatch, pgoff_t *indices);
  351. void filemap_free_folio(struct address_space *mapping, struct folio *folio);
  352. int truncate_inode_folio(struct address_space *mapping, struct folio *folio);
  353. bool truncate_inode_partial_folio(struct folio *folio, loff_t start,
  354. loff_t end);
  355. long mapping_evict_folio(struct address_space *mapping, struct folio *folio);
  356. unsigned long mapping_try_invalidate(struct address_space *mapping,
  357. pgoff_t start, pgoff_t end, unsigned long *nr_failed);
  358. /**
  359. * folio_evictable - Test whether a folio is evictable.
  360. * @folio: The folio to test.
  361. *
  362. * Test whether @folio is evictable -- i.e., should be placed on
  363. * active/inactive lists vs unevictable list.
  364. *
  365. * Reasons folio might not be evictable:
  366. * 1. folio's mapping marked unevictable
  367. * 2. One of the pages in the folio is part of an mlocked VMA
  368. */
  369. static inline bool folio_evictable(struct folio *folio)
  370. {
  371. bool ret;
  372. /* Prevent address_space of inode and swap cache from being freed */
  373. rcu_read_lock();
  374. ret = !mapping_unevictable(folio_mapping(folio)) &&
  375. !folio_test_mlocked(folio);
  376. rcu_read_unlock();
  377. return ret;
  378. }
  379. /*
  380. * Turn a non-refcounted page (->_refcount == 0) into refcounted with
  381. * a count of one.
  382. */
  383. static inline void set_page_refcounted(struct page *page)
  384. {
  385. VM_BUG_ON_PAGE(PageTail(page), page);
  386. VM_BUG_ON_PAGE(page_ref_count(page), page);
  387. set_page_count(page, 1);
  388. }
  389. /*
  390. * Return true if a folio needs ->release_folio() calling upon it.
  391. */
  392. static inline bool folio_needs_release(struct folio *folio)
  393. {
  394. struct address_space *mapping = folio_mapping(folio);
  395. return folio_has_private(folio) ||
  396. (mapping && mapping_release_always(mapping));
  397. }
  398. extern unsigned long highest_memmap_pfn;
  399. /*
  400. * Maximum number of reclaim retries without progress before the OOM
  401. * killer is consider the only way forward.
  402. */
  403. #define MAX_RECLAIM_RETRIES 16
  404. /*
  405. * in mm/vmscan.c:
  406. */
  407. bool folio_isolate_lru(struct folio *folio);
  408. void folio_putback_lru(struct folio *folio);
  409. extern void reclaim_throttle(pg_data_t *pgdat, enum vmscan_throttle_state reason);
  410. /*
  411. * in mm/rmap.c:
  412. */
  413. pmd_t *mm_find_pmd(struct mm_struct *mm, unsigned long address);
  414. /*
  415. * in mm/page_alloc.c
  416. */
  417. #define K(x) ((x) << (PAGE_SHIFT-10))
  418. extern char * const zone_names[MAX_NR_ZONES];
  419. /* perform sanity checks on struct pages being allocated or freed */
  420. DECLARE_STATIC_KEY_MAYBE(CONFIG_DEBUG_VM, check_pages_enabled);
  421. extern int min_free_kbytes;
  422. void setup_per_zone_wmarks(void);
  423. void calculate_min_free_kbytes(void);
  424. int __meminit init_per_zone_wmark_min(void);
  425. void page_alloc_sysctl_init(void);
  426. /*
  427. * Structure for holding the mostly immutable allocation parameters passed
  428. * between functions involved in allocations, including the alloc_pages*
  429. * family of functions.
  430. *
  431. * nodemask, migratetype and highest_zoneidx are initialized only once in
  432. * __alloc_pages() and then never change.
  433. *
  434. * zonelist, preferred_zone and highest_zoneidx are set first in
  435. * __alloc_pages() for the fast path, and might be later changed
  436. * in __alloc_pages_slowpath(). All other functions pass the whole structure
  437. * by a const pointer.
  438. */
  439. struct alloc_context {
  440. struct zonelist *zonelist;
  441. nodemask_t *nodemask;
  442. struct zoneref *preferred_zoneref;
  443. int migratetype;
  444. /*
  445. * highest_zoneidx represents highest usable zone index of
  446. * the allocation request. Due to the nature of the zone,
  447. * memory on lower zone than the highest_zoneidx will be
  448. * protected by lowmem_reserve[highest_zoneidx].
  449. *
  450. * highest_zoneidx is also used by reclaim/compaction to limit
  451. * the target zone since higher zone than this index cannot be
  452. * usable for this allocation request.
  453. */
  454. enum zone_type highest_zoneidx;
  455. bool spread_dirty_pages;
  456. };
  457. /*
  458. * This function returns the order of a free page in the buddy system. In
  459. * general, page_zone(page)->lock must be held by the caller to prevent the
  460. * page from being allocated in parallel and returning garbage as the order.
  461. * If a caller does not hold page_zone(page)->lock, it must guarantee that the
  462. * page cannot be allocated or merged in parallel. Alternatively, it must
  463. * handle invalid values gracefully, and use buddy_order_unsafe() below.
  464. */
  465. static inline unsigned int buddy_order(struct page *page)
  466. {
  467. /* PageBuddy() must be checked by the caller */
  468. return page_private(page);
  469. }
  470. /*
  471. * Like buddy_order(), but for callers who cannot afford to hold the zone lock.
  472. * PageBuddy() should be checked first by the caller to minimize race window,
  473. * and invalid values must be handled gracefully.
  474. *
  475. * READ_ONCE is used so that if the caller assigns the result into a local
  476. * variable and e.g. tests it for valid range before using, the compiler cannot
  477. * decide to remove the variable and inline the page_private(page) multiple
  478. * times, potentially observing different values in the tests and the actual
  479. * use of the result.
  480. */
  481. #define buddy_order_unsafe(page) READ_ONCE(page_private(page))
  482. /*
  483. * This function checks whether a page is free && is the buddy
  484. * we can coalesce a page and its buddy if
  485. * (a) the buddy is not in a hole (check before calling!) &&
  486. * (b) the buddy is in the buddy system &&
  487. * (c) a page and its buddy have the same order &&
  488. * (d) a page and its buddy are in the same zone.
  489. *
  490. * For recording whether a page is in the buddy system, we set PageBuddy.
  491. * Setting, clearing, and testing PageBuddy is serialized by zone->lock.
  492. *
  493. * For recording page's order, we use page_private(page).
  494. */
  495. static inline bool page_is_buddy(struct page *page, struct page *buddy,
  496. unsigned int order)
  497. {
  498. if (!page_is_guard(buddy) && !PageBuddy(buddy))
  499. return false;
  500. if (buddy_order(buddy) != order)
  501. return false;
  502. /*
  503. * zone check is done late to avoid uselessly calculating
  504. * zone/node ids for pages that could never merge.
  505. */
  506. if (page_zone_id(page) != page_zone_id(buddy))
  507. return false;
  508. VM_BUG_ON_PAGE(page_count(buddy) != 0, buddy);
  509. return true;
  510. }
  511. /*
  512. * Locate the struct page for both the matching buddy in our
  513. * pair (buddy1) and the combined O(n+1) page they form (page).
  514. *
  515. * 1) Any buddy B1 will have an order O twin B2 which satisfies
  516. * the following equation:
  517. * B2 = B1 ^ (1 << O)
  518. * For example, if the starting buddy (buddy2) is #8 its order
  519. * 1 buddy is #10:
  520. * B2 = 8 ^ (1 << 1) = 8 ^ 2 = 10
  521. *
  522. * 2) Any buddy B will have an order O+1 parent P which
  523. * satisfies the following equation:
  524. * P = B & ~(1 << O)
  525. *
  526. * Assumption: *_mem_map is contiguous at least up to MAX_PAGE_ORDER
  527. */
  528. static inline unsigned long
  529. __find_buddy_pfn(unsigned long page_pfn, unsigned int order)
  530. {
  531. return page_pfn ^ (1 << order);
  532. }
  533. /*
  534. * Find the buddy of @page and validate it.
  535. * @page: The input page
  536. * @pfn: The pfn of the page, it saves a call to page_to_pfn() when the
  537. * function is used in the performance-critical __free_one_page().
  538. * @order: The order of the page
  539. * @buddy_pfn: The output pointer to the buddy pfn, it also saves a call to
  540. * page_to_pfn().
  541. *
  542. * The found buddy can be a non PageBuddy, out of @page's zone, or its order is
  543. * not the same as @page. The validation is necessary before use it.
  544. *
  545. * Return: the found buddy page or NULL if not found.
  546. */
  547. static inline struct page *find_buddy_page_pfn(struct page *page,
  548. unsigned long pfn, unsigned int order, unsigned long *buddy_pfn)
  549. {
  550. unsigned long __buddy_pfn = __find_buddy_pfn(pfn, order);
  551. struct page *buddy;
  552. buddy = page + (__buddy_pfn - pfn);
  553. if (buddy_pfn)
  554. *buddy_pfn = __buddy_pfn;
  555. if (page_is_buddy(page, buddy, order))
  556. return buddy;
  557. return NULL;
  558. }
  559. extern struct page *__pageblock_pfn_to_page(unsigned long start_pfn,
  560. unsigned long end_pfn, struct zone *zone);
  561. static inline struct page *pageblock_pfn_to_page(unsigned long start_pfn,
  562. unsigned long end_pfn, struct zone *zone)
  563. {
  564. if (zone->contiguous)
  565. return pfn_to_page(start_pfn);
  566. return __pageblock_pfn_to_page(start_pfn, end_pfn, zone);
  567. }
  568. void set_zone_contiguous(struct zone *zone);
  569. static inline void clear_zone_contiguous(struct zone *zone)
  570. {
  571. zone->contiguous = false;
  572. }
  573. extern int __isolate_free_page(struct page *page, unsigned int order);
  574. extern void __putback_isolated_page(struct page *page, unsigned int order,
  575. int mt);
  576. extern void memblock_free_pages(struct page *page, unsigned long pfn,
  577. unsigned int order);
  578. extern void __free_pages_core(struct page *page, unsigned int order,
  579. enum meminit_context context);
  580. /*
  581. * This will have no effect, other than possibly generating a warning, if the
  582. * caller passes in a non-large folio.
  583. */
  584. static inline void folio_set_order(struct folio *folio, unsigned int order)
  585. {
  586. if (WARN_ON_ONCE(!order || !folio_test_large(folio)))
  587. return;
  588. folio->_flags_1 = (folio->_flags_1 & ~0xffUL) | order;
  589. #ifdef CONFIG_64BIT
  590. folio->_folio_nr_pages = 1U << order;
  591. #endif
  592. }
  593. bool __folio_unqueue_deferred_split(struct folio *folio);
  594. static inline bool folio_unqueue_deferred_split(struct folio *folio)
  595. {
  596. if (folio_order(folio) <= 1 || !folio_test_large_rmappable(folio))
  597. return false;
  598. /*
  599. * At this point, there is no one trying to add the folio to
  600. * deferred_list. If folio is not in deferred_list, it's safe
  601. * to check without acquiring the split_queue_lock.
  602. */
  603. if (data_race(list_empty(&folio->_deferred_list)))
  604. return false;
  605. return __folio_unqueue_deferred_split(folio);
  606. }
  607. static inline struct folio *page_rmappable_folio(struct page *page)
  608. {
  609. struct folio *folio = (struct folio *)page;
  610. if (folio && folio_test_large(folio))
  611. folio_set_large_rmappable(folio);
  612. return folio;
  613. }
  614. static inline void prep_compound_head(struct page *page, unsigned int order)
  615. {
  616. struct folio *folio = (struct folio *)page;
  617. folio_set_order(folio, order);
  618. atomic_set(&folio->_large_mapcount, -1);
  619. atomic_set(&folio->_entire_mapcount, -1);
  620. atomic_set(&folio->_nr_pages_mapped, 0);
  621. atomic_set(&folio->_pincount, 0);
  622. if (order > 1)
  623. INIT_LIST_HEAD(&folio->_deferred_list);
  624. }
  625. static inline void prep_compound_tail(struct page *head, int tail_idx)
  626. {
  627. struct page *p = head + tail_idx;
  628. p->mapping = TAIL_MAPPING;
  629. set_compound_head(p, head);
  630. set_page_private(p, 0);
  631. }
  632. extern void prep_compound_page(struct page *page, unsigned int order);
  633. extern void post_alloc_hook(struct page *page, unsigned int order,
  634. gfp_t gfp_flags);
  635. extern bool free_pages_prepare(struct page *page, unsigned int order);
  636. extern int user_min_free_kbytes;
  637. void free_unref_page(struct page *page, unsigned int order);
  638. void free_unref_folios(struct folio_batch *fbatch);
  639. extern void zone_pcp_reset(struct zone *zone);
  640. extern void zone_pcp_disable(struct zone *zone);
  641. extern void zone_pcp_enable(struct zone *zone);
  642. extern void zone_pcp_init(struct zone *zone);
  643. extern void *memmap_alloc(phys_addr_t size, phys_addr_t align,
  644. phys_addr_t min_addr,
  645. int nid, bool exact_nid);
  646. void memmap_init_range(unsigned long, int, unsigned long, unsigned long,
  647. unsigned long, enum meminit_context, struct vmem_altmap *, int);
  648. #if defined CONFIG_COMPACTION || defined CONFIG_CMA
  649. /*
  650. * in mm/compaction.c
  651. */
  652. /*
  653. * compact_control is used to track pages being migrated and the free pages
  654. * they are being migrated to during memory compaction. The free_pfn starts
  655. * at the end of a zone and migrate_pfn begins at the start. Movable pages
  656. * are moved to the end of a zone during a compaction run and the run
  657. * completes when free_pfn <= migrate_pfn
  658. */
  659. struct compact_control {
  660. struct list_head freepages[NR_PAGE_ORDERS]; /* List of free pages to migrate to */
  661. struct list_head migratepages; /* List of pages being migrated */
  662. unsigned int nr_freepages; /* Number of isolated free pages */
  663. unsigned int nr_migratepages; /* Number of pages to migrate */
  664. unsigned long free_pfn; /* isolate_freepages search base */
  665. /*
  666. * Acts as an in/out parameter to page isolation for migration.
  667. * isolate_migratepages uses it as a search base.
  668. * isolate_migratepages_block will update the value to the next pfn
  669. * after the last isolated one.
  670. */
  671. unsigned long migrate_pfn;
  672. unsigned long fast_start_pfn; /* a pfn to start linear scan from */
  673. struct zone *zone;
  674. unsigned long total_migrate_scanned;
  675. unsigned long total_free_scanned;
  676. unsigned short fast_search_fail;/* failures to use free list searches */
  677. short search_order; /* order to start a fast search at */
  678. const gfp_t gfp_mask; /* gfp mask of a direct compactor */
  679. int order; /* order a direct compactor needs */
  680. int migratetype; /* migratetype of direct compactor */
  681. const unsigned int alloc_flags; /* alloc flags of a direct compactor */
  682. const int highest_zoneidx; /* zone index of a direct compactor */
  683. enum migrate_mode mode; /* Async or sync migration mode */
  684. bool ignore_skip_hint; /* Scan blocks even if marked skip */
  685. bool no_set_skip_hint; /* Don't mark blocks for skipping */
  686. bool ignore_block_suitable; /* Scan blocks considered unsuitable */
  687. bool direct_compaction; /* False from kcompactd or /proc/... */
  688. bool proactive_compaction; /* kcompactd proactive compaction */
  689. bool whole_zone; /* Whole zone should/has been scanned */
  690. bool contended; /* Signal lock contention */
  691. bool finish_pageblock; /* Scan the remainder of a pageblock. Used
  692. * when there are potentially transient
  693. * isolation or migration failures to
  694. * ensure forward progress.
  695. */
  696. bool alloc_contig; /* alloc_contig_range allocation */
  697. };
  698. /*
  699. * Used in direct compaction when a page should be taken from the freelists
  700. * immediately when one is created during the free path.
  701. */
  702. struct capture_control {
  703. struct compact_control *cc;
  704. struct page *page;
  705. };
  706. unsigned long
  707. isolate_freepages_range(struct compact_control *cc,
  708. unsigned long start_pfn, unsigned long end_pfn);
  709. int
  710. isolate_migratepages_range(struct compact_control *cc,
  711. unsigned long low_pfn, unsigned long end_pfn);
  712. int __alloc_contig_migrate_range(struct compact_control *cc,
  713. unsigned long start, unsigned long end,
  714. int migratetype);
  715. /* Free whole pageblock and set its migration type to MIGRATE_CMA. */
  716. void init_cma_reserved_pageblock(struct page *page);
  717. #endif /* CONFIG_COMPACTION || CONFIG_CMA */
  718. int find_suitable_fallback(struct free_area *area, unsigned int order,
  719. int migratetype, bool only_stealable, bool *can_steal);
  720. static inline bool free_area_empty(struct free_area *area, int migratetype)
  721. {
  722. return list_empty(&area->free_list[migratetype]);
  723. }
  724. /* mm/util.c */
  725. struct anon_vma *folio_anon_vma(struct folio *folio);
  726. #ifdef CONFIG_MMU
  727. void unmap_mapping_folio(struct folio *folio);
  728. extern long populate_vma_page_range(struct vm_area_struct *vma,
  729. unsigned long start, unsigned long end, int *locked);
  730. extern long faultin_page_range(struct mm_struct *mm, unsigned long start,
  731. unsigned long end, bool write, int *locked);
  732. extern bool mlock_future_ok(struct mm_struct *mm, unsigned long flags,
  733. unsigned long bytes);
  734. /*
  735. * NOTE: This function can't tell whether the folio is "fully mapped" in the
  736. * range.
  737. * "fully mapped" means all the pages of folio is associated with the page
  738. * table of range while this function just check whether the folio range is
  739. * within the range [start, end). Function caller needs to do page table
  740. * check if it cares about the page table association.
  741. *
  742. * Typical usage (like mlock or madvise) is:
  743. * Caller knows at least 1 page of folio is associated with page table of VMA
  744. * and the range [start, end) is intersect with the VMA range. Caller wants
  745. * to know whether the folio is fully associated with the range. It calls
  746. * this function to check whether the folio is in the range first. Then checks
  747. * the page table to know whether the folio is fully mapped to the range.
  748. */
  749. static inline bool
  750. folio_within_range(struct folio *folio, struct vm_area_struct *vma,
  751. unsigned long start, unsigned long end)
  752. {
  753. pgoff_t pgoff, addr;
  754. unsigned long vma_pglen = vma_pages(vma);
  755. VM_WARN_ON_FOLIO(folio_test_ksm(folio), folio);
  756. if (start > end)
  757. return false;
  758. if (start < vma->vm_start)
  759. start = vma->vm_start;
  760. if (end > vma->vm_end)
  761. end = vma->vm_end;
  762. pgoff = folio_pgoff(folio);
  763. /* if folio start address is not in vma range */
  764. if (!in_range(pgoff, vma->vm_pgoff, vma_pglen))
  765. return false;
  766. addr = vma->vm_start + ((pgoff - vma->vm_pgoff) << PAGE_SHIFT);
  767. return !(addr < start || end - addr < folio_size(folio));
  768. }
  769. static inline bool
  770. folio_within_vma(struct folio *folio, struct vm_area_struct *vma)
  771. {
  772. return folio_within_range(folio, vma, vma->vm_start, vma->vm_end);
  773. }
  774. /*
  775. * mlock_vma_folio() and munlock_vma_folio():
  776. * should be called with vma's mmap_lock held for read or write,
  777. * under page table lock for the pte/pmd being added or removed.
  778. *
  779. * mlock is usually called at the end of folio_add_*_rmap_*(), munlock at
  780. * the end of folio_remove_rmap_*(); but new anon folios are managed by
  781. * folio_add_lru_vma() calling mlock_new_folio().
  782. */
  783. void mlock_folio(struct folio *folio);
  784. static inline void mlock_vma_folio(struct folio *folio,
  785. struct vm_area_struct *vma)
  786. {
  787. /*
  788. * The VM_SPECIAL check here serves two purposes.
  789. * 1) VM_IO check prevents migration from double-counting during mlock.
  790. * 2) Although mmap_region() and mlock_fixup() take care that VM_LOCKED
  791. * is never left set on a VM_SPECIAL vma, there is an interval while
  792. * file->f_op->mmap() is using vm_insert_page(s), when VM_LOCKED may
  793. * still be set while VM_SPECIAL bits are added: so ignore it then.
  794. */
  795. if (unlikely((vma->vm_flags & (VM_LOCKED|VM_SPECIAL)) == VM_LOCKED))
  796. mlock_folio(folio);
  797. }
  798. void munlock_folio(struct folio *folio);
  799. static inline void munlock_vma_folio(struct folio *folio,
  800. struct vm_area_struct *vma)
  801. {
  802. /*
  803. * munlock if the function is called. Ideally, we should only
  804. * do munlock if any page of folio is unmapped from VMA and
  805. * cause folio not fully mapped to VMA.
  806. *
  807. * But it's not easy to confirm that's the situation. So we
  808. * always munlock the folio and page reclaim will correct it
  809. * if it's wrong.
  810. */
  811. if (unlikely(vma->vm_flags & VM_LOCKED))
  812. munlock_folio(folio);
  813. }
  814. void mlock_new_folio(struct folio *folio);
  815. bool need_mlock_drain(int cpu);
  816. void mlock_drain_local(void);
  817. void mlock_drain_remote(int cpu);
  818. extern pmd_t maybe_pmd_mkwrite(pmd_t pmd, struct vm_area_struct *vma);
  819. /**
  820. * vma_address - Find the virtual address a page range is mapped at
  821. * @vma: The vma which maps this object.
  822. * @pgoff: The page offset within its object.
  823. * @nr_pages: The number of pages to consider.
  824. *
  825. * If any page in this range is mapped by this VMA, return the first address
  826. * where any of these pages appear. Otherwise, return -EFAULT.
  827. */
  828. static inline unsigned long vma_address(struct vm_area_struct *vma,
  829. pgoff_t pgoff, unsigned long nr_pages)
  830. {
  831. unsigned long address;
  832. if (pgoff >= vma->vm_pgoff) {
  833. address = vma->vm_start +
  834. ((pgoff - vma->vm_pgoff) << PAGE_SHIFT);
  835. /* Check for address beyond vma (or wrapped through 0?) */
  836. if (address < vma->vm_start || address >= vma->vm_end)
  837. address = -EFAULT;
  838. } else if (pgoff + nr_pages - 1 >= vma->vm_pgoff) {
  839. /* Test above avoids possibility of wrap to 0 on 32-bit */
  840. address = vma->vm_start;
  841. } else {
  842. address = -EFAULT;
  843. }
  844. return address;
  845. }
  846. /*
  847. * Then at what user virtual address will none of the range be found in vma?
  848. * Assumes that vma_address() already returned a good starting address.
  849. */
  850. static inline unsigned long vma_address_end(struct page_vma_mapped_walk *pvmw)
  851. {
  852. struct vm_area_struct *vma = pvmw->vma;
  853. pgoff_t pgoff;
  854. unsigned long address;
  855. /* Common case, plus ->pgoff is invalid for KSM */
  856. if (pvmw->nr_pages == 1)
  857. return pvmw->address + PAGE_SIZE;
  858. pgoff = pvmw->pgoff + pvmw->nr_pages;
  859. address = vma->vm_start + ((pgoff - vma->vm_pgoff) << PAGE_SHIFT);
  860. /* Check for address beyond vma (or wrapped through 0?) */
  861. if (address < vma->vm_start || address > vma->vm_end)
  862. address = vma->vm_end;
  863. return address;
  864. }
  865. static inline struct file *maybe_unlock_mmap_for_io(struct vm_fault *vmf,
  866. struct file *fpin)
  867. {
  868. int flags = vmf->flags;
  869. if (fpin)
  870. return fpin;
  871. /*
  872. * FAULT_FLAG_RETRY_NOWAIT means we don't want to wait on page locks or
  873. * anything, so we only pin the file and drop the mmap_lock if only
  874. * FAULT_FLAG_ALLOW_RETRY is set, while this is the first attempt.
  875. */
  876. if (fault_flag_allow_retry_first(flags) &&
  877. !(flags & FAULT_FLAG_RETRY_NOWAIT)) {
  878. fpin = get_file(vmf->vma->vm_file);
  879. release_fault_lock(vmf);
  880. }
  881. return fpin;
  882. }
  883. #else /* !CONFIG_MMU */
  884. static inline void unmap_mapping_folio(struct folio *folio) { }
  885. static inline void mlock_new_folio(struct folio *folio) { }
  886. static inline bool need_mlock_drain(int cpu) { return false; }
  887. static inline void mlock_drain_local(void) { }
  888. static inline void mlock_drain_remote(int cpu) { }
  889. static inline void vunmap_range_noflush(unsigned long start, unsigned long end)
  890. {
  891. }
  892. #endif /* !CONFIG_MMU */
  893. /* Memory initialisation debug and verification */
  894. #ifdef CONFIG_DEFERRED_STRUCT_PAGE_INIT
  895. DECLARE_STATIC_KEY_TRUE(deferred_pages);
  896. bool __init deferred_grow_zone(struct zone *zone, unsigned int order);
  897. #endif /* CONFIG_DEFERRED_STRUCT_PAGE_INIT */
  898. enum mminit_level {
  899. MMINIT_WARNING,
  900. MMINIT_VERIFY,
  901. MMINIT_TRACE
  902. };
  903. #ifdef CONFIG_DEBUG_MEMORY_INIT
  904. extern int mminit_loglevel;
  905. #define mminit_dprintk(level, prefix, fmt, arg...) \
  906. do { \
  907. if (level < mminit_loglevel) { \
  908. if (level <= MMINIT_WARNING) \
  909. pr_warn("mminit::" prefix " " fmt, ##arg); \
  910. else \
  911. printk(KERN_DEBUG "mminit::" prefix " " fmt, ##arg); \
  912. } \
  913. } while (0)
  914. extern void mminit_verify_pageflags_layout(void);
  915. extern void mminit_verify_zonelist(void);
  916. #else
  917. static inline void mminit_dprintk(enum mminit_level level,
  918. const char *prefix, const char *fmt, ...)
  919. {
  920. }
  921. static inline void mminit_verify_pageflags_layout(void)
  922. {
  923. }
  924. static inline void mminit_verify_zonelist(void)
  925. {
  926. }
  927. #endif /* CONFIG_DEBUG_MEMORY_INIT */
  928. #define NODE_RECLAIM_NOSCAN -2
  929. #define NODE_RECLAIM_FULL -1
  930. #define NODE_RECLAIM_SOME 0
  931. #define NODE_RECLAIM_SUCCESS 1
  932. #ifdef CONFIG_NUMA
  933. extern int node_reclaim(struct pglist_data *, gfp_t, unsigned int);
  934. extern int find_next_best_node(int node, nodemask_t *used_node_mask);
  935. #else
  936. static inline int node_reclaim(struct pglist_data *pgdat, gfp_t mask,
  937. unsigned int order)
  938. {
  939. return NODE_RECLAIM_NOSCAN;
  940. }
  941. static inline int find_next_best_node(int node, nodemask_t *used_node_mask)
  942. {
  943. return NUMA_NO_NODE;
  944. }
  945. #endif
  946. /*
  947. * mm/memory-failure.c
  948. */
  949. #ifdef CONFIG_MEMORY_FAILURE
  950. void unmap_poisoned_folio(struct folio *folio, enum ttu_flags ttu);
  951. void shake_folio(struct folio *folio);
  952. extern int hwpoison_filter(struct page *p);
  953. extern u32 hwpoison_filter_dev_major;
  954. extern u32 hwpoison_filter_dev_minor;
  955. extern u64 hwpoison_filter_flags_mask;
  956. extern u64 hwpoison_filter_flags_value;
  957. extern u64 hwpoison_filter_memcg;
  958. extern u32 hwpoison_filter_enable;
  959. #define MAGIC_HWPOISON 0x48575053U /* HWPS */
  960. void SetPageHWPoisonTakenOff(struct page *page);
  961. void ClearPageHWPoisonTakenOff(struct page *page);
  962. bool take_page_off_buddy(struct page *page);
  963. bool put_page_back_buddy(struct page *page);
  964. struct task_struct *task_early_kill(struct task_struct *tsk, int force_early);
  965. void add_to_kill_ksm(struct task_struct *tsk, struct page *p,
  966. struct vm_area_struct *vma, struct list_head *to_kill,
  967. unsigned long ksm_addr);
  968. unsigned long page_mapped_in_vma(struct page *page, struct vm_area_struct *vma);
  969. #else
  970. static inline void unmap_poisoned_folio(struct folio *folio, enum ttu_flags ttu)
  971. {
  972. }
  973. #endif
  974. extern unsigned long __must_check vm_mmap_pgoff(struct file *, unsigned long,
  975. unsigned long, unsigned long,
  976. unsigned long, unsigned long);
  977. extern void set_pageblock_order(void);
  978. struct folio *alloc_migrate_folio(struct folio *src, unsigned long private);
  979. unsigned long reclaim_pages(struct list_head *folio_list);
  980. unsigned int reclaim_clean_pages_from_list(struct zone *zone,
  981. struct list_head *folio_list);
  982. /* The ALLOC_WMARK bits are used as an index to zone->watermark */
  983. #define ALLOC_WMARK_MIN WMARK_MIN
  984. #define ALLOC_WMARK_LOW WMARK_LOW
  985. #define ALLOC_WMARK_HIGH WMARK_HIGH
  986. #define ALLOC_NO_WATERMARKS 0x04 /* don't check watermarks at all */
  987. /* Mask to get the watermark bits */
  988. #define ALLOC_WMARK_MASK (ALLOC_NO_WATERMARKS-1)
  989. /*
  990. * Only MMU archs have async oom victim reclaim - aka oom_reaper so we
  991. * cannot assume a reduced access to memory reserves is sufficient for
  992. * !MMU
  993. */
  994. #ifdef CONFIG_MMU
  995. #define ALLOC_OOM 0x08
  996. #else
  997. #define ALLOC_OOM ALLOC_NO_WATERMARKS
  998. #endif
  999. #define ALLOC_NON_BLOCK 0x10 /* Caller cannot block. Allow access
  1000. * to 25% of the min watermark or
  1001. * 62.5% if __GFP_HIGH is set.
  1002. */
  1003. #define ALLOC_MIN_RESERVE 0x20 /* __GFP_HIGH set. Allow access to 50%
  1004. * of the min watermark.
  1005. */
  1006. #define ALLOC_CPUSET 0x40 /* check for correct cpuset */
  1007. #define ALLOC_CMA 0x80 /* allow allocations from CMA areas */
  1008. #ifdef CONFIG_ZONE_DMA32
  1009. #define ALLOC_NOFRAGMENT 0x100 /* avoid mixing pageblock types */
  1010. #else
  1011. #define ALLOC_NOFRAGMENT 0x0
  1012. #endif
  1013. #define ALLOC_HIGHATOMIC 0x200 /* Allows access to MIGRATE_HIGHATOMIC */
  1014. #define ALLOC_KSWAPD 0x800 /* allow waking of kswapd, __GFP_KSWAPD_RECLAIM set */
  1015. /* Flags that allow allocations below the min watermark. */
  1016. #define ALLOC_RESERVES (ALLOC_NON_BLOCK|ALLOC_MIN_RESERVE|ALLOC_HIGHATOMIC|ALLOC_OOM)
  1017. enum ttu_flags;
  1018. struct tlbflush_unmap_batch;
  1019. /*
  1020. * only for MM internal work items which do not depend on
  1021. * any allocations or locks which might depend on allocations
  1022. */
  1023. extern struct workqueue_struct *mm_percpu_wq;
  1024. #ifdef CONFIG_ARCH_WANT_BATCHED_UNMAP_TLB_FLUSH
  1025. void try_to_unmap_flush(void);
  1026. void try_to_unmap_flush_dirty(void);
  1027. void flush_tlb_batched_pending(struct mm_struct *mm);
  1028. #else
  1029. static inline void try_to_unmap_flush(void)
  1030. {
  1031. }
  1032. static inline void try_to_unmap_flush_dirty(void)
  1033. {
  1034. }
  1035. static inline void flush_tlb_batched_pending(struct mm_struct *mm)
  1036. {
  1037. }
  1038. #endif /* CONFIG_ARCH_WANT_BATCHED_UNMAP_TLB_FLUSH */
  1039. extern const struct trace_print_flags pageflag_names[];
  1040. extern const struct trace_print_flags vmaflag_names[];
  1041. extern const struct trace_print_flags gfpflag_names[];
  1042. static inline bool is_migrate_highatomic(enum migratetype migratetype)
  1043. {
  1044. return migratetype == MIGRATE_HIGHATOMIC;
  1045. }
  1046. void setup_zone_pageset(struct zone *zone);
  1047. struct migration_target_control {
  1048. int nid; /* preferred node id */
  1049. nodemask_t *nmask;
  1050. gfp_t gfp_mask;
  1051. enum migrate_reason reason;
  1052. };
  1053. /*
  1054. * mm/filemap.c
  1055. */
  1056. size_t splice_folio_into_pipe(struct pipe_inode_info *pipe,
  1057. struct folio *folio, loff_t fpos, size_t size);
  1058. /*
  1059. * mm/vmalloc.c
  1060. */
  1061. #ifdef CONFIG_MMU
  1062. void __init vmalloc_init(void);
  1063. int __must_check vmap_pages_range_noflush(unsigned long addr, unsigned long end,
  1064. pgprot_t prot, struct page **pages, unsigned int page_shift);
  1065. #else
  1066. static inline void vmalloc_init(void)
  1067. {
  1068. }
  1069. static inline
  1070. int __must_check vmap_pages_range_noflush(unsigned long addr, unsigned long end,
  1071. pgprot_t prot, struct page **pages, unsigned int page_shift)
  1072. {
  1073. return -EINVAL;
  1074. }
  1075. #endif
  1076. int __must_check __vmap_pages_range_noflush(unsigned long addr,
  1077. unsigned long end, pgprot_t prot,
  1078. struct page **pages, unsigned int page_shift);
  1079. void vunmap_range_noflush(unsigned long start, unsigned long end);
  1080. void __vunmap_range_noflush(unsigned long start, unsigned long end);
  1081. int numa_migrate_check(struct folio *folio, struct vm_fault *vmf,
  1082. unsigned long addr, int *flags, bool writable,
  1083. int *last_cpupid);
  1084. void free_zone_device_folio(struct folio *folio);
  1085. int migrate_device_coherent_folio(struct folio *folio);
  1086. /*
  1087. * mm/gup.c
  1088. */
  1089. int __must_check try_grab_folio(struct folio *folio, int refs,
  1090. unsigned int flags);
  1091. /*
  1092. * mm/huge_memory.c
  1093. */
  1094. void touch_pud(struct vm_area_struct *vma, unsigned long addr,
  1095. pud_t *pud, bool write);
  1096. void touch_pmd(struct vm_area_struct *vma, unsigned long addr,
  1097. pmd_t *pmd, bool write);
  1098. enum {
  1099. /* mark page accessed */
  1100. FOLL_TOUCH = 1 << 16,
  1101. /* a retry, previous pass started an IO */
  1102. FOLL_TRIED = 1 << 17,
  1103. /* we are working on non-current tsk/mm */
  1104. FOLL_REMOTE = 1 << 18,
  1105. /* pages must be released via unpin_user_page */
  1106. FOLL_PIN = 1 << 19,
  1107. /* gup_fast: prevent fall-back to slow gup */
  1108. FOLL_FAST_ONLY = 1 << 20,
  1109. /* allow unlocking the mmap lock */
  1110. FOLL_UNLOCKABLE = 1 << 21,
  1111. /* VMA lookup+checks compatible with MADV_POPULATE_(READ|WRITE) */
  1112. FOLL_MADV_POPULATE = 1 << 22,
  1113. };
  1114. #define INTERNAL_GUP_FLAGS (FOLL_TOUCH | FOLL_TRIED | FOLL_REMOTE | FOLL_PIN | \
  1115. FOLL_FAST_ONLY | FOLL_UNLOCKABLE | \
  1116. FOLL_MADV_POPULATE)
  1117. /*
  1118. * Indicates for which pages that are write-protected in the page table,
  1119. * whether GUP has to trigger unsharing via FAULT_FLAG_UNSHARE such that the
  1120. * GUP pin will remain consistent with the pages mapped into the page tables
  1121. * of the MM.
  1122. *
  1123. * Temporary unmapping of PageAnonExclusive() pages or clearing of
  1124. * PageAnonExclusive() has to protect against concurrent GUP:
  1125. * * Ordinary GUP: Using the PT lock
  1126. * * GUP-fast and fork(): mm->write_protect_seq
  1127. * * GUP-fast and KSM or temporary unmapping (swap, migration): see
  1128. * folio_try_share_anon_rmap_*()
  1129. *
  1130. * Must be called with the (sub)page that's actually referenced via the
  1131. * page table entry, which might not necessarily be the head page for a
  1132. * PTE-mapped THP.
  1133. *
  1134. * If the vma is NULL, we're coming from the GUP-fast path and might have
  1135. * to fallback to the slow path just to lookup the vma.
  1136. */
  1137. static inline bool gup_must_unshare(struct vm_area_struct *vma,
  1138. unsigned int flags, struct page *page)
  1139. {
  1140. /*
  1141. * FOLL_WRITE is implicitly handled correctly as the page table entry
  1142. * has to be writable -- and if it references (part of) an anonymous
  1143. * folio, that part is required to be marked exclusive.
  1144. */
  1145. if ((flags & (FOLL_WRITE | FOLL_PIN)) != FOLL_PIN)
  1146. return false;
  1147. /*
  1148. * Note: PageAnon(page) is stable until the page is actually getting
  1149. * freed.
  1150. */
  1151. if (!PageAnon(page)) {
  1152. /*
  1153. * We only care about R/O long-term pining: R/O short-term
  1154. * pinning does not have the semantics to observe successive
  1155. * changes through the process page tables.
  1156. */
  1157. if (!(flags & FOLL_LONGTERM))
  1158. return false;
  1159. /* We really need the vma ... */
  1160. if (!vma)
  1161. return true;
  1162. /*
  1163. * ... because we only care about writable private ("COW")
  1164. * mappings where we have to break COW early.
  1165. */
  1166. return is_cow_mapping(vma->vm_flags);
  1167. }
  1168. /* Paired with a memory barrier in folio_try_share_anon_rmap_*(). */
  1169. if (IS_ENABLED(CONFIG_HAVE_GUP_FAST))
  1170. smp_rmb();
  1171. /*
  1172. * Note that PageKsm() pages cannot be exclusive, and consequently,
  1173. * cannot get pinned.
  1174. */
  1175. return !PageAnonExclusive(page);
  1176. }
  1177. extern bool mirrored_kernelcore;
  1178. extern bool memblock_has_mirror(void);
  1179. static __always_inline void vma_set_range(struct vm_area_struct *vma,
  1180. unsigned long start, unsigned long end,
  1181. pgoff_t pgoff)
  1182. {
  1183. vma->vm_start = start;
  1184. vma->vm_end = end;
  1185. vma->vm_pgoff = pgoff;
  1186. }
  1187. static inline bool vma_soft_dirty_enabled(struct vm_area_struct *vma)
  1188. {
  1189. /*
  1190. * NOTE: we must check this before VM_SOFTDIRTY on soft-dirty
  1191. * enablements, because when without soft-dirty being compiled in,
  1192. * VM_SOFTDIRTY is defined as 0x0, then !(vm_flags & VM_SOFTDIRTY)
  1193. * will be constantly true.
  1194. */
  1195. if (!IS_ENABLED(CONFIG_MEM_SOFT_DIRTY))
  1196. return false;
  1197. /*
  1198. * Soft-dirty is kind of special: its tracking is enabled when the
  1199. * vma flags not set.
  1200. */
  1201. return !(vma->vm_flags & VM_SOFTDIRTY);
  1202. }
  1203. static inline bool pmd_needs_soft_dirty_wp(struct vm_area_struct *vma, pmd_t pmd)
  1204. {
  1205. return vma_soft_dirty_enabled(vma) && !pmd_soft_dirty(pmd);
  1206. }
  1207. static inline bool pte_needs_soft_dirty_wp(struct vm_area_struct *vma, pte_t pte)
  1208. {
  1209. return vma_soft_dirty_enabled(vma) && !pte_soft_dirty(pte);
  1210. }
  1211. void __meminit __init_single_page(struct page *page, unsigned long pfn,
  1212. unsigned long zone, int nid);
  1213. /* shrinker related functions */
  1214. unsigned long shrink_slab(gfp_t gfp_mask, int nid, struct mem_cgroup *memcg,
  1215. int priority);
  1216. #ifdef CONFIG_64BIT
  1217. static inline int can_do_mseal(unsigned long flags)
  1218. {
  1219. if (flags)
  1220. return -EINVAL;
  1221. return 0;
  1222. }
  1223. #else
  1224. static inline int can_do_mseal(unsigned long flags)
  1225. {
  1226. return -EPERM;
  1227. }
  1228. #endif
  1229. #ifdef CONFIG_SHRINKER_DEBUG
  1230. static inline __printf(2, 0) int shrinker_debugfs_name_alloc(
  1231. struct shrinker *shrinker, const char *fmt, va_list ap)
  1232. {
  1233. shrinker->name = kvasprintf_const(GFP_KERNEL, fmt, ap);
  1234. return shrinker->name ? 0 : -ENOMEM;
  1235. }
  1236. static inline void shrinker_debugfs_name_free(struct shrinker *shrinker)
  1237. {
  1238. kfree_const(shrinker->name);
  1239. shrinker->name = NULL;
  1240. }
  1241. extern int shrinker_debugfs_add(struct shrinker *shrinker);
  1242. extern struct dentry *shrinker_debugfs_detach(struct shrinker *shrinker,
  1243. int *debugfs_id);
  1244. extern void shrinker_debugfs_remove(struct dentry *debugfs_entry,
  1245. int debugfs_id);
  1246. #else /* CONFIG_SHRINKER_DEBUG */
  1247. static inline int shrinker_debugfs_add(struct shrinker *shrinker)
  1248. {
  1249. return 0;
  1250. }
  1251. static inline int shrinker_debugfs_name_alloc(struct shrinker *shrinker,
  1252. const char *fmt, va_list ap)
  1253. {
  1254. return 0;
  1255. }
  1256. static inline void shrinker_debugfs_name_free(struct shrinker *shrinker)
  1257. {
  1258. }
  1259. static inline struct dentry *shrinker_debugfs_detach(struct shrinker *shrinker,
  1260. int *debugfs_id)
  1261. {
  1262. *debugfs_id = -1;
  1263. return NULL;
  1264. }
  1265. static inline void shrinker_debugfs_remove(struct dentry *debugfs_entry,
  1266. int debugfs_id)
  1267. {
  1268. }
  1269. #endif /* CONFIG_SHRINKER_DEBUG */
  1270. /* Only track the nodes of mappings with shadow entries */
  1271. void workingset_update_node(struct xa_node *node);
  1272. extern struct list_lru shadow_nodes;
  1273. /* mremap.c */
  1274. unsigned long move_page_tables(struct vm_area_struct *vma,
  1275. unsigned long old_addr, struct vm_area_struct *new_vma,
  1276. unsigned long new_addr, unsigned long len,
  1277. bool need_rmap_locks, bool for_stack);
  1278. #ifdef CONFIG_UNACCEPTED_MEMORY
  1279. void accept_page(struct page *page);
  1280. #else /* CONFIG_UNACCEPTED_MEMORY */
  1281. static inline void accept_page(struct page *page)
  1282. {
  1283. }
  1284. #endif /* CONFIG_UNACCEPTED_MEMORY */
  1285. #endif /* __MM_INTERNAL_H */