internal.h 54 KB

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