mmzone.h 63 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef _LINUX_MMZONE_H
  3. #define _LINUX_MMZONE_H
  4. #ifndef __ASSEMBLY__
  5. #ifndef __GENERATING_BOUNDS_H
  6. #include <linux/spinlock.h>
  7. #include <linux/list.h>
  8. #include <linux/list_nulls.h>
  9. #include <linux/wait.h>
  10. #include <linux/bitops.h>
  11. #include <linux/cache.h>
  12. #include <linux/threads.h>
  13. #include <linux/numa.h>
  14. #include <linux/init.h>
  15. #include <linux/seqlock.h>
  16. #include <linux/nodemask.h>
  17. #include <linux/pageblock-flags.h>
  18. #include <linux/page-flags-layout.h>
  19. #include <linux/atomic.h>
  20. #include <linux/mm_types.h>
  21. #include <linux/page-flags.h>
  22. #include <linux/local_lock.h>
  23. #include <linux/android_kabi.h>
  24. #include <asm/page.h>
  25. /* Free memory management - zoned buddy allocator. */
  26. #ifndef CONFIG_ARCH_FORCE_MAX_ORDER
  27. #define MAX_ORDER 11
  28. #else
  29. #define MAX_ORDER CONFIG_ARCH_FORCE_MAX_ORDER
  30. #endif
  31. #define MAX_ORDER_NR_PAGES (1 << (MAX_ORDER - 1))
  32. /*
  33. * PAGE_ALLOC_COSTLY_ORDER is the order at which allocations are deemed
  34. * costly to service. That is between allocation orders which should
  35. * coalesce naturally under reasonable reclaim pressure and those which
  36. * will not.
  37. */
  38. #define PAGE_ALLOC_COSTLY_ORDER 3
  39. #define MAX_KSWAPD_THREADS 16
  40. enum migratetype {
  41. MIGRATE_UNMOVABLE,
  42. MIGRATE_MOVABLE,
  43. MIGRATE_RECLAIMABLE,
  44. #ifdef CONFIG_CMA
  45. /*
  46. * MIGRATE_CMA migration type is designed to mimic the way
  47. * ZONE_MOVABLE works. Only movable pages can be allocated
  48. * from MIGRATE_CMA pageblocks and page allocator never
  49. * implicitly change migration type of MIGRATE_CMA pageblock.
  50. *
  51. * The way to use it is to change migratetype of a range of
  52. * pageblocks to MIGRATE_CMA which can be done by
  53. * __free_pageblock_cma() function.
  54. */
  55. MIGRATE_CMA,
  56. #endif
  57. MIGRATE_PCPTYPES, /* the number of types on the pcp lists */
  58. MIGRATE_HIGHATOMIC = MIGRATE_PCPTYPES,
  59. #ifdef CONFIG_MEMORY_ISOLATION
  60. MIGRATE_ISOLATE, /* can't allocate from here */
  61. #endif
  62. MIGRATE_TYPES
  63. };
  64. /* In mm/page_alloc.c; keep in sync also with show_migration_types() there */
  65. extern const char * const migratetype_names[MIGRATE_TYPES];
  66. #ifdef CONFIG_CMA
  67. # define is_migrate_cma(migratetype) unlikely((migratetype) == MIGRATE_CMA)
  68. # define is_migrate_cma_page(_page) (get_pageblock_migratetype(_page) == MIGRATE_CMA)
  69. # define get_cma_migrate_type() MIGRATE_CMA
  70. #else
  71. # define is_migrate_cma(migratetype) false
  72. # define is_migrate_cma_page(_page) false
  73. # define get_cma_migrate_type() MIGRATE_MOVABLE
  74. #endif
  75. static inline bool is_migrate_movable(int mt)
  76. {
  77. return is_migrate_cma(mt) || mt == MIGRATE_MOVABLE;
  78. }
  79. /*
  80. * Check whether a migratetype can be merged with another migratetype.
  81. *
  82. * It is only mergeable when it can fall back to other migratetypes for
  83. * allocation. See fallbacks[MIGRATE_TYPES][3] in page_alloc.c.
  84. */
  85. static inline bool migratetype_is_mergeable(int mt)
  86. {
  87. return mt <= MIGRATE_RECLAIMABLE;
  88. }
  89. #define for_each_migratetype_order(order, type) \
  90. for (order = 0; order < MAX_ORDER; order++) \
  91. for (type = 0; type < MIGRATE_TYPES; type++)
  92. extern int page_group_by_mobility_disabled;
  93. #define MIGRATETYPE_MASK ((1UL << PB_migratetype_bits) - 1)
  94. #define get_pageblock_migratetype(page) \
  95. get_pfnblock_flags_mask(page, page_to_pfn(page), MIGRATETYPE_MASK)
  96. struct free_area {
  97. struct list_head free_list[MIGRATE_TYPES];
  98. unsigned long nr_free;
  99. };
  100. static inline struct page *get_page_from_free_area(struct free_area *area,
  101. int migratetype)
  102. {
  103. return list_first_entry_or_null(&area->free_list[migratetype],
  104. struct page, lru);
  105. }
  106. static inline bool free_area_empty(struct free_area *area, int migratetype)
  107. {
  108. return list_empty(&area->free_list[migratetype]);
  109. }
  110. struct pglist_data;
  111. #ifdef CONFIG_NUMA
  112. enum numa_stat_item {
  113. NUMA_HIT, /* allocated in intended node */
  114. NUMA_MISS, /* allocated in non intended node */
  115. NUMA_FOREIGN, /* was intended here, hit elsewhere */
  116. NUMA_INTERLEAVE_HIT, /* interleaver preferred this zone */
  117. NUMA_LOCAL, /* allocation from local node */
  118. NUMA_OTHER, /* allocation from other node */
  119. NR_VM_NUMA_EVENT_ITEMS
  120. };
  121. #else
  122. #define NR_VM_NUMA_EVENT_ITEMS 0
  123. #endif
  124. enum zone_stat_item {
  125. /* First 128 byte cacheline (assuming 64 bit words) */
  126. NR_FREE_PAGES,
  127. NR_ZONE_LRU_BASE, /* Used only for compaction and reclaim retry */
  128. NR_ZONE_INACTIVE_ANON = NR_ZONE_LRU_BASE,
  129. NR_ZONE_ACTIVE_ANON,
  130. NR_ZONE_INACTIVE_FILE,
  131. NR_ZONE_ACTIVE_FILE,
  132. NR_ZONE_UNEVICTABLE,
  133. NR_ZONE_WRITE_PENDING, /* Count of dirty, writeback and unstable pages */
  134. NR_MLOCK, /* mlock()ed pages found and moved off LRU */
  135. /* Second 128 byte cacheline */
  136. NR_BOUNCE,
  137. NR_ZSPAGES, /* allocated in zsmalloc */
  138. NR_FREE_CMA_PAGES,
  139. NR_VM_ZONE_STAT_ITEMS };
  140. enum node_stat_item {
  141. NR_LRU_BASE,
  142. NR_INACTIVE_ANON = NR_LRU_BASE, /* must match order of LRU_[IN]ACTIVE */
  143. NR_ACTIVE_ANON, /* " " " " " */
  144. NR_INACTIVE_FILE, /* " " " " " */
  145. NR_ACTIVE_FILE, /* " " " " " */
  146. NR_UNEVICTABLE, /* " " " " " */
  147. NR_SLAB_RECLAIMABLE_B,
  148. NR_SLAB_UNRECLAIMABLE_B,
  149. NR_ISOLATED_ANON, /* Temporary isolated pages from anon lru */
  150. NR_ISOLATED_FILE, /* Temporary isolated pages from file lru */
  151. WORKINGSET_NODES,
  152. WORKINGSET_REFAULT_BASE,
  153. WORKINGSET_REFAULT_ANON = WORKINGSET_REFAULT_BASE,
  154. WORKINGSET_REFAULT_FILE,
  155. WORKINGSET_ACTIVATE_BASE,
  156. WORKINGSET_ACTIVATE_ANON = WORKINGSET_ACTIVATE_BASE,
  157. WORKINGSET_ACTIVATE_FILE,
  158. WORKINGSET_RESTORE_BASE,
  159. WORKINGSET_RESTORE_ANON = WORKINGSET_RESTORE_BASE,
  160. WORKINGSET_RESTORE_FILE,
  161. WORKINGSET_NODERECLAIM,
  162. NR_ANON_MAPPED, /* Mapped anonymous pages */
  163. NR_FILE_MAPPED, /* pagecache pages mapped into pagetables.
  164. only modified from process context */
  165. NR_FILE_PAGES,
  166. NR_FILE_DIRTY,
  167. NR_WRITEBACK,
  168. NR_WRITEBACK_TEMP, /* Writeback using temporary buffers */
  169. NR_SHMEM, /* shmem pages (included tmpfs/GEM pages) */
  170. NR_SHMEM_THPS,
  171. NR_SHMEM_PMDMAPPED,
  172. NR_FILE_THPS,
  173. NR_FILE_PMDMAPPED,
  174. NR_ANON_THPS,
  175. NR_VMSCAN_WRITE,
  176. NR_VMSCAN_IMMEDIATE, /* Prioritise for reclaim when writeback ends */
  177. NR_DIRTIED, /* page dirtyings since bootup */
  178. NR_WRITTEN, /* page writings since bootup */
  179. NR_THROTTLED_WRITTEN, /* NR_WRITTEN while reclaim throttled */
  180. NR_KERNEL_MISC_RECLAIMABLE, /* reclaimable non-slab kernel pages */
  181. NR_FOLL_PIN_ACQUIRED, /* via: pin_user_page(), gup flag: FOLL_PIN */
  182. NR_FOLL_PIN_RELEASED, /* pages returned via unpin_user_page() */
  183. NR_KERNEL_STACK_KB, /* measured in KiB */
  184. #if IS_ENABLED(CONFIG_SHADOW_CALL_STACK)
  185. NR_KERNEL_SCS_KB, /* measured in KiB */
  186. #endif
  187. NR_PAGETABLE, /* used for pagetables */
  188. NR_SECONDARY_PAGETABLE, /* secondary pagetables, e.g. KVM pagetables */
  189. #ifdef CONFIG_SWAP
  190. NR_SWAPCACHE,
  191. #endif
  192. #ifdef CONFIG_NUMA_BALANCING
  193. PGPROMOTE_SUCCESS, /* promote successfully */
  194. PGPROMOTE_CANDIDATE, /* candidate pages to promote */
  195. #endif
  196. NR_VM_NODE_STAT_ITEMS
  197. };
  198. /*
  199. * Returns true if the item should be printed in THPs (/proc/vmstat
  200. * currently prints number of anon, file and shmem THPs. But the item
  201. * is charged in pages).
  202. */
  203. static __always_inline bool vmstat_item_print_in_thp(enum node_stat_item item)
  204. {
  205. if (!IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE))
  206. return false;
  207. return item == NR_ANON_THPS ||
  208. item == NR_FILE_THPS ||
  209. item == NR_SHMEM_THPS ||
  210. item == NR_SHMEM_PMDMAPPED ||
  211. item == NR_FILE_PMDMAPPED;
  212. }
  213. /*
  214. * Returns true if the value is measured in bytes (most vmstat values are
  215. * measured in pages). This defines the API part, the internal representation
  216. * might be different.
  217. */
  218. static __always_inline bool vmstat_item_in_bytes(int idx)
  219. {
  220. /*
  221. * Global and per-node slab counters track slab pages.
  222. * It's expected that changes are multiples of PAGE_SIZE.
  223. * Internally values are stored in pages.
  224. *
  225. * Per-memcg and per-lruvec counters track memory, consumed
  226. * by individual slab objects. These counters are actually
  227. * byte-precise.
  228. */
  229. return (idx == NR_SLAB_RECLAIMABLE_B ||
  230. idx == NR_SLAB_UNRECLAIMABLE_B);
  231. }
  232. /*
  233. * We do arithmetic on the LRU lists in various places in the code,
  234. * so it is important to keep the active lists LRU_ACTIVE higher in
  235. * the array than the corresponding inactive lists, and to keep
  236. * the *_FILE lists LRU_FILE higher than the corresponding _ANON lists.
  237. *
  238. * This has to be kept in sync with the statistics in zone_stat_item
  239. * above and the descriptions in vmstat_text in mm/vmstat.c
  240. */
  241. #define LRU_BASE 0
  242. #define LRU_ACTIVE 1
  243. #define LRU_FILE 2
  244. enum lru_list {
  245. LRU_INACTIVE_ANON = LRU_BASE,
  246. LRU_ACTIVE_ANON = LRU_BASE + LRU_ACTIVE,
  247. LRU_INACTIVE_FILE = LRU_BASE + LRU_FILE,
  248. LRU_ACTIVE_FILE = LRU_BASE + LRU_FILE + LRU_ACTIVE,
  249. LRU_UNEVICTABLE,
  250. NR_LRU_LISTS
  251. };
  252. enum vmscan_throttle_state {
  253. VMSCAN_THROTTLE_WRITEBACK,
  254. VMSCAN_THROTTLE_ISOLATED,
  255. VMSCAN_THROTTLE_NOPROGRESS,
  256. VMSCAN_THROTTLE_CONGESTED,
  257. NR_VMSCAN_THROTTLE,
  258. };
  259. #define for_each_lru(lru) for (lru = 0; lru < NR_LRU_LISTS; lru++)
  260. #define for_each_evictable_lru(lru) for (lru = 0; lru <= LRU_ACTIVE_FILE; lru++)
  261. static inline bool is_file_lru(enum lru_list lru)
  262. {
  263. return (lru == LRU_INACTIVE_FILE || lru == LRU_ACTIVE_FILE);
  264. }
  265. static inline bool is_active_lru(enum lru_list lru)
  266. {
  267. return (lru == LRU_ACTIVE_ANON || lru == LRU_ACTIVE_FILE);
  268. }
  269. #define WORKINGSET_ANON 0
  270. #define WORKINGSET_FILE 1
  271. #define ANON_AND_FILE 2
  272. enum lruvec_flags {
  273. LRUVEC_CONGESTED, /* lruvec has many dirty pages
  274. * backed by a congested BDI
  275. */
  276. };
  277. #endif /* !__GENERATING_BOUNDS_H */
  278. /*
  279. * Evictable pages are divided into multiple generations. The youngest and the
  280. * oldest generation numbers, max_seq and min_seq, are monotonically increasing.
  281. * They form a sliding window of a variable size [MIN_NR_GENS, MAX_NR_GENS]. An
  282. * offset within MAX_NR_GENS, i.e., gen, indexes the LRU list of the
  283. * corresponding generation. The gen counter in folio->flags stores gen+1 while
  284. * a page is on one of lrugen->folios[]. Otherwise it stores 0.
  285. *
  286. * A page is added to the youngest generation on faulting. The aging needs to
  287. * check the accessed bit at least twice before handing this page over to the
  288. * eviction. The first check takes care of the accessed bit set on the initial
  289. * fault; the second check makes sure this page hasn't been used since then.
  290. * This process, AKA second chance, requires a minimum of two generations,
  291. * hence MIN_NR_GENS. And to maintain ABI compatibility with the active/inactive
  292. * LRU, e.g., /proc/vmstat, these two generations are considered active; the
  293. * rest of generations, if they exist, are considered inactive. See
  294. * lru_gen_is_active().
  295. *
  296. * PG_active is always cleared while a page is on one of lrugen->folios[] so
  297. * that the aging needs not to worry about it. And it's set again when a page
  298. * considered active is isolated for non-reclaiming purposes, e.g., migration.
  299. * See lru_gen_add_folio() and lru_gen_del_folio().
  300. *
  301. * MAX_NR_GENS is set to 4 so that the multi-gen LRU can support twice the
  302. * number of categories of the active/inactive LRU when keeping track of
  303. * accesses through page tables. This requires order_base_2(MAX_NR_GENS+1) bits
  304. * in folio->flags.
  305. */
  306. #define MIN_NR_GENS 2U
  307. #define MAX_NR_GENS 4U
  308. /*
  309. * Each generation is divided into multiple tiers. A page accessed N times
  310. * through file descriptors is in tier order_base_2(N). A page in the first tier
  311. * (N=0,1) is marked by PG_referenced unless it was faulted in through page
  312. * tables or read ahead. A page in any other tier (N>1) is marked by
  313. * PG_referenced and PG_workingset. This implies a minimum of two tiers is
  314. * supported without using additional bits in folio->flags.
  315. *
  316. * In contrast to moving across generations which requires the LRU lock, moving
  317. * across tiers only involves atomic operations on folio->flags and therefore
  318. * has a negligible cost in the buffered access path. In the eviction path,
  319. * comparisons of refaulted/(evicted+protected) from the first tier and the
  320. * rest infer whether pages accessed multiple times through file descriptors
  321. * are statistically hot and thus worth protecting.
  322. *
  323. * MAX_NR_TIERS is set to 4 so that the multi-gen LRU can support twice the
  324. * number of categories of the active/inactive LRU when keeping track of
  325. * accesses through file descriptors. This uses MAX_NR_TIERS-2 spare bits in
  326. * folio->flags.
  327. */
  328. #define MAX_NR_TIERS 4U
  329. #ifndef __GENERATING_BOUNDS_H
  330. struct lruvec;
  331. struct page_vma_mapped_walk;
  332. #define LRU_GEN_MASK ((BIT(LRU_GEN_WIDTH) - 1) << LRU_GEN_PGOFF)
  333. #define LRU_REFS_MASK ((BIT(LRU_REFS_WIDTH) - 1) << LRU_REFS_PGOFF)
  334. #ifdef CONFIG_LRU_GEN
  335. enum {
  336. LRU_GEN_ANON,
  337. LRU_GEN_FILE,
  338. };
  339. enum {
  340. LRU_GEN_CORE,
  341. LRU_GEN_MM_WALK,
  342. LRU_GEN_NONLEAF_YOUNG,
  343. NR_LRU_GEN_CAPS
  344. };
  345. #define MIN_LRU_BATCH BITS_PER_LONG
  346. #define MAX_LRU_BATCH (MIN_LRU_BATCH * 64)
  347. /* whether to keep historical stats from evicted generations */
  348. #ifdef CONFIG_LRU_GEN_STATS
  349. #define NR_HIST_GENS MAX_NR_GENS
  350. #else
  351. #define NR_HIST_GENS 1U
  352. #endif
  353. /*
  354. * The youngest generation number is stored in max_seq for both anon and file
  355. * types as they are aged on an equal footing. The oldest generation numbers are
  356. * stored in min_seq[] separately for anon and file types as clean file pages
  357. * can be evicted regardless of swap constraints.
  358. *
  359. * Normally anon and file min_seq are in sync. But if swapping is constrained,
  360. * e.g., out of swap space, file min_seq is allowed to advance and leave anon
  361. * min_seq behind.
  362. *
  363. * The number of pages in each generation is eventually consistent and therefore
  364. * can be transiently negative when reset_batch_size() is pending.
  365. */
  366. struct lru_gen_folio {
  367. /* the aging increments the youngest generation number */
  368. unsigned long max_seq;
  369. /* the eviction increments the oldest generation numbers */
  370. unsigned long min_seq[ANON_AND_FILE];
  371. /* the birth time of each generation in jiffies */
  372. unsigned long timestamps[MAX_NR_GENS];
  373. /* the multi-gen LRU lists, lazily sorted on eviction */
  374. struct list_head folios[MAX_NR_GENS][ANON_AND_FILE][MAX_NR_ZONES];
  375. /* the multi-gen LRU sizes, eventually consistent */
  376. long nr_pages[MAX_NR_GENS][ANON_AND_FILE][MAX_NR_ZONES];
  377. /* the exponential moving average of refaulted */
  378. unsigned long avg_refaulted[ANON_AND_FILE][MAX_NR_TIERS];
  379. /* the exponential moving average of evicted+protected */
  380. unsigned long avg_total[ANON_AND_FILE][MAX_NR_TIERS];
  381. /* the first tier doesn't need protection, hence the minus one */
  382. unsigned long protected[NR_HIST_GENS][ANON_AND_FILE][MAX_NR_TIERS - 1];
  383. /* can be modified without holding the LRU lock */
  384. atomic_long_t evicted[NR_HIST_GENS][ANON_AND_FILE][MAX_NR_TIERS];
  385. atomic_long_t refaulted[NR_HIST_GENS][ANON_AND_FILE][MAX_NR_TIERS];
  386. /* whether the multi-gen LRU is enabled */
  387. bool enabled;
  388. #ifdef CONFIG_MEMCG
  389. /* the memcg generation this lru_gen_folio belongs to */
  390. u8 gen;
  391. /* the list segment this lru_gen_folio belongs to */
  392. u8 seg;
  393. /* per-node lru_gen_folio list for global reclaim */
  394. struct hlist_nulls_node list;
  395. #endif
  396. ANDROID_KABI_RESERVE(1);
  397. ANDROID_KABI_RESERVE(2);
  398. };
  399. enum {
  400. MM_LEAF_TOTAL, /* total leaf entries */
  401. MM_LEAF_OLD, /* old leaf entries */
  402. MM_LEAF_YOUNG, /* young leaf entries */
  403. MM_NONLEAF_TOTAL, /* total non-leaf entries */
  404. MM_NONLEAF_FOUND, /* non-leaf entries found in Bloom filters */
  405. MM_NONLEAF_ADDED, /* non-leaf entries added to Bloom filters */
  406. NR_MM_STATS
  407. };
  408. /* double-buffering Bloom filters */
  409. #define NR_BLOOM_FILTERS 2
  410. struct lru_gen_mm_state {
  411. /* set to max_seq after each iteration */
  412. unsigned long seq;
  413. /* where the current iteration continues after */
  414. struct list_head *head;
  415. /* where the last iteration ended before */
  416. struct list_head *tail;
  417. /* Bloom filters flip after each iteration */
  418. unsigned long *filters[NR_BLOOM_FILTERS];
  419. /* the mm stats for debugging */
  420. unsigned long stats[NR_HIST_GENS][NR_MM_STATS];
  421. ANDROID_KABI_RESERVE(1);
  422. };
  423. struct lru_gen_mm_walk {
  424. /* the lruvec under reclaim */
  425. struct lruvec *lruvec;
  426. /* unstable max_seq from lru_gen_folio */
  427. unsigned long max_seq;
  428. /* the next address within an mm to scan */
  429. unsigned long next_addr;
  430. /* to batch promoted pages */
  431. int nr_pages[MAX_NR_GENS][ANON_AND_FILE][MAX_NR_ZONES];
  432. /* to batch the mm stats */
  433. int mm_stats[NR_MM_STATS];
  434. /* total batched items */
  435. int batched;
  436. bool can_swap;
  437. bool force_scan;
  438. ANDROID_KABI_RESERVE(1);
  439. ANDROID_KABI_RESERVE(2);
  440. };
  441. void lru_gen_init_lruvec(struct lruvec *lruvec);
  442. void lru_gen_look_around(struct page_vma_mapped_walk *pvmw);
  443. #ifdef CONFIG_MEMCG
  444. /*
  445. * For each node, memcgs are divided into two generations: the old and the
  446. * young. For each generation, memcgs are randomly sharded into multiple bins
  447. * to improve scalability. For each bin, the hlist_nulls is virtually divided
  448. * into three segments: the head, the tail and the default.
  449. *
  450. * An onlining memcg is added to the tail of a random bin in the old generation.
  451. * The eviction starts at the head of a random bin in the old generation. The
  452. * per-node memcg generation counter, whose reminder (mod MEMCG_NR_GENS) indexes
  453. * the old generation, is incremented when all its bins become empty.
  454. *
  455. * There are four operations:
  456. * 1. MEMCG_LRU_HEAD, which moves an memcg to the head of a random bin in its
  457. * current generation (old or young) and updates its "seg" to "head";
  458. * 2. MEMCG_LRU_TAIL, which moves an memcg to the tail of a random bin in its
  459. * current generation (old or young) and updates its "seg" to "tail";
  460. * 3. MEMCG_LRU_OLD, which moves an memcg to the head of a random bin in the old
  461. * generation, updates its "gen" to "old" and resets its "seg" to "default";
  462. * 4. MEMCG_LRU_YOUNG, which moves an memcg to the tail of a random bin in the
  463. * young generation, updates its "gen" to "young" and resets its "seg" to
  464. * "default".
  465. *
  466. * The events that trigger the above operations are:
  467. * 1. Exceeding the soft limit, which triggers MEMCG_LRU_HEAD;
  468. * 2. The first attempt to reclaim an memcg below low, which triggers
  469. * MEMCG_LRU_TAIL;
  470. * 3. The first attempt to reclaim an memcg below reclaimable size threshold,
  471. * which triggers MEMCG_LRU_TAIL;
  472. * 4. The second attempt to reclaim an memcg below reclaimable size threshold,
  473. * which triggers MEMCG_LRU_YOUNG;
  474. * 5. Attempting to reclaim an memcg below min, which triggers MEMCG_LRU_YOUNG;
  475. * 6. Finishing the aging on the eviction path, which triggers MEMCG_LRU_YOUNG;
  476. * 7. Offlining an memcg, which triggers MEMCG_LRU_OLD.
  477. *
  478. * Note that memcg LRU only applies to global reclaim, and the round-robin
  479. * incrementing of their max_seq counters ensures the eventual fairness to all
  480. * eligible memcgs. For memcg reclaim, it still relies on mem_cgroup_iter().
  481. */
  482. #define MEMCG_NR_GENS 2
  483. #define MEMCG_NR_BINS 8
  484. struct lru_gen_memcg {
  485. /* the per-node memcg generation counter */
  486. unsigned long seq;
  487. /* each memcg has one lru_gen_folio per node */
  488. unsigned long nr_memcgs[MEMCG_NR_GENS];
  489. /* per-node lru_gen_folio list for global reclaim */
  490. struct hlist_nulls_head fifo[MEMCG_NR_GENS][MEMCG_NR_BINS];
  491. /* protects the above */
  492. spinlock_t lock;
  493. ANDROID_KABI_RESERVE(1);
  494. ANDROID_KABI_RESERVE(2);
  495. };
  496. void lru_gen_init_pgdat(struct pglist_data *pgdat);
  497. void lru_gen_init_memcg(struct mem_cgroup *memcg);
  498. void lru_gen_exit_memcg(struct mem_cgroup *memcg);
  499. void lru_gen_online_memcg(struct mem_cgroup *memcg);
  500. void lru_gen_offline_memcg(struct mem_cgroup *memcg);
  501. void lru_gen_release_memcg(struct mem_cgroup *memcg);
  502. void lru_gen_soft_reclaim(struct lruvec *lruvec);
  503. #else /* !CONFIG_MEMCG */
  504. #define MEMCG_NR_GENS 1
  505. struct lru_gen_memcg {
  506. };
  507. static inline void lru_gen_init_pgdat(struct pglist_data *pgdat)
  508. {
  509. }
  510. #endif /* CONFIG_MEMCG */
  511. #else /* !CONFIG_LRU_GEN */
  512. static inline void lru_gen_init_pgdat(struct pglist_data *pgdat)
  513. {
  514. }
  515. static inline void lru_gen_init_lruvec(struct lruvec *lruvec)
  516. {
  517. }
  518. static inline void lru_gen_look_around(struct page_vma_mapped_walk *pvmw)
  519. {
  520. }
  521. #ifdef CONFIG_MEMCG
  522. static inline void lru_gen_init_memcg(struct mem_cgroup *memcg)
  523. {
  524. }
  525. static inline void lru_gen_exit_memcg(struct mem_cgroup *memcg)
  526. {
  527. }
  528. static inline void lru_gen_online_memcg(struct mem_cgroup *memcg)
  529. {
  530. }
  531. static inline void lru_gen_offline_memcg(struct mem_cgroup *memcg)
  532. {
  533. }
  534. static inline void lru_gen_release_memcg(struct mem_cgroup *memcg)
  535. {
  536. }
  537. static inline void lru_gen_soft_reclaim(struct lruvec *lruvec)
  538. {
  539. }
  540. #endif /* CONFIG_MEMCG */
  541. #endif /* CONFIG_LRU_GEN */
  542. struct lruvec {
  543. struct list_head lists[NR_LRU_LISTS];
  544. /* per lruvec lru_lock for memcg */
  545. spinlock_t lru_lock;
  546. /*
  547. * These track the cost of reclaiming one LRU - file or anon -
  548. * over the other. As the observed cost of reclaiming one LRU
  549. * increases, the reclaim scan balance tips toward the other.
  550. */
  551. unsigned long anon_cost;
  552. unsigned long file_cost;
  553. /* Non-resident age, driven by LRU movement */
  554. atomic_long_t nonresident_age;
  555. /* Refaults at the time of last reclaim cycle */
  556. unsigned long refaults[ANON_AND_FILE];
  557. /* Various lruvec state flags (enum lruvec_flags) */
  558. unsigned long flags;
  559. #ifdef CONFIG_LRU_GEN
  560. /* evictable pages divided into generations */
  561. struct lru_gen_folio lrugen;
  562. /* to concurrently iterate lru_gen_mm_list */
  563. struct lru_gen_mm_state mm_state;
  564. #endif
  565. #ifdef CONFIG_MEMCG
  566. struct pglist_data *pgdat;
  567. #endif
  568. ANDROID_VENDOR_DATA(1);
  569. ANDROID_KABI_RESERVE(1);
  570. ANDROID_KABI_RESERVE(2);
  571. };
  572. /* Isolate unmapped pages */
  573. #define ISOLATE_UNMAPPED ((__force isolate_mode_t)0x2)
  574. /* Isolate for asynchronous migration */
  575. #define ISOLATE_ASYNC_MIGRATE ((__force isolate_mode_t)0x4)
  576. /* Isolate unevictable pages */
  577. #define ISOLATE_UNEVICTABLE ((__force isolate_mode_t)0x8)
  578. /* LRU Isolation modes. */
  579. typedef unsigned __bitwise isolate_mode_t;
  580. enum zone_watermarks {
  581. WMARK_MIN,
  582. WMARK_LOW,
  583. WMARK_HIGH,
  584. WMARK_PROMO,
  585. NR_WMARK
  586. };
  587. /*
  588. * One per migratetype for each PAGE_ALLOC_COSTLY_ORDER. One additional list
  589. * for THP which will usually be GFP_MOVABLE. Even if it is another type,
  590. * it should not contribute to serious fragmentation causing THP allocation
  591. * failures.
  592. */
  593. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  594. #define NR_PCP_THP 1
  595. #else
  596. #define NR_PCP_THP 0
  597. #endif
  598. #define NR_LOWORDER_PCP_LISTS (MIGRATE_PCPTYPES * (PAGE_ALLOC_COSTLY_ORDER + 1))
  599. #define NR_PCP_LISTS (NR_LOWORDER_PCP_LISTS + NR_PCP_THP)
  600. #define min_wmark_pages(z) (z->_watermark[WMARK_MIN] + z->watermark_boost)
  601. #define low_wmark_pages(z) (z->_watermark[WMARK_LOW] + z->watermark_boost)
  602. #define high_wmark_pages(z) (z->_watermark[WMARK_HIGH] + z->watermark_boost)
  603. #define wmark_pages(z, i) (z->_watermark[i] + z->watermark_boost)
  604. /* Fields and list protected by pagesets local_lock in page_alloc.c */
  605. struct per_cpu_pages {
  606. spinlock_t lock; /* Protects lists field */
  607. int count; /* number of pages in the list */
  608. int high; /* high watermark, emptying needed */
  609. int batch; /* chunk size for buddy add/remove */
  610. short free_factor; /* batch scaling factor during free */
  611. #ifdef CONFIG_NUMA
  612. short expire; /* When 0, remote pagesets are drained */
  613. #endif
  614. /* Lists of pages, one per migrate type stored on the pcp-lists */
  615. struct list_head lists[NR_PCP_LISTS];
  616. } ____cacheline_aligned_in_smp;
  617. struct per_cpu_zonestat {
  618. #ifdef CONFIG_SMP
  619. s8 vm_stat_diff[NR_VM_ZONE_STAT_ITEMS];
  620. s8 stat_threshold;
  621. #endif
  622. #ifdef CONFIG_NUMA
  623. /*
  624. * Low priority inaccurate counters that are only folded
  625. * on demand. Use a large type to avoid the overhead of
  626. * folding during refresh_cpu_vm_stats.
  627. */
  628. unsigned long vm_numa_event[NR_VM_NUMA_EVENT_ITEMS];
  629. #endif
  630. };
  631. struct per_cpu_nodestat {
  632. s8 stat_threshold;
  633. s8 vm_node_stat_diff[NR_VM_NODE_STAT_ITEMS];
  634. };
  635. #endif /* !__GENERATING_BOUNDS.H */
  636. enum zone_type {
  637. /*
  638. * ZONE_DMA and ZONE_DMA32 are used when there are peripherals not able
  639. * to DMA to all of the addressable memory (ZONE_NORMAL).
  640. * On architectures where this area covers the whole 32 bit address
  641. * space ZONE_DMA32 is used. ZONE_DMA is left for the ones with smaller
  642. * DMA addressing constraints. This distinction is important as a 32bit
  643. * DMA mask is assumed when ZONE_DMA32 is defined. Some 64-bit
  644. * platforms may need both zones as they support peripherals with
  645. * different DMA addressing limitations.
  646. */
  647. #ifdef CONFIG_ZONE_DMA
  648. ZONE_DMA,
  649. #endif
  650. #ifdef CONFIG_ZONE_DMA32
  651. ZONE_DMA32,
  652. #endif
  653. /*
  654. * Normal addressable memory is in ZONE_NORMAL. DMA operations can be
  655. * performed on pages in ZONE_NORMAL if the DMA devices support
  656. * transfers to all addressable memory.
  657. */
  658. ZONE_NORMAL,
  659. #ifdef CONFIG_HIGHMEM
  660. /*
  661. * A memory area that is only addressable by the kernel through
  662. * mapping portions into its own address space. This is for example
  663. * used by i386 to allow the kernel to address the memory beyond
  664. * 900MB. The kernel will set up special mappings (page
  665. * table entries on i386) for each page that the kernel needs to
  666. * access.
  667. */
  668. ZONE_HIGHMEM,
  669. #endif
  670. /*
  671. * ZONE_MOVABLE is similar to ZONE_NORMAL, except that it contains
  672. * movable pages with few exceptional cases described below. Main use
  673. * cases for ZONE_MOVABLE are to make memory offlining/unplug more
  674. * likely to succeed, and to locally limit unmovable allocations - e.g.,
  675. * to increase the number of THP/huge pages. Notable special cases are:
  676. *
  677. * 1. Pinned pages: (long-term) pinning of movable pages might
  678. * essentially turn such pages unmovable. Therefore, we do not allow
  679. * pinning long-term pages in ZONE_MOVABLE. When pages are pinned and
  680. * faulted, they come from the right zone right away. However, it is
  681. * still possible that address space already has pages in
  682. * ZONE_MOVABLE at the time when pages are pinned (i.e. user has
  683. * touches that memory before pinning). In such case we migrate them
  684. * to a different zone. When migration fails - pinning fails.
  685. * 2. memblock allocations: kernelcore/movablecore setups might create
  686. * situations where ZONE_MOVABLE contains unmovable allocations
  687. * after boot. Memory offlining and allocations fail early.
  688. * 3. Memory holes: kernelcore/movablecore setups might create very rare
  689. * situations where ZONE_MOVABLE contains memory holes after boot,
  690. * for example, if we have sections that are only partially
  691. * populated. Memory offlining and allocations fail early.
  692. * 4. PG_hwpoison pages: while poisoned pages can be skipped during
  693. * memory offlining, such pages cannot be allocated.
  694. * 5. Unmovable PG_offline pages: in paravirtualized environments,
  695. * hotplugged memory blocks might only partially be managed by the
  696. * buddy (e.g., via XEN-balloon, Hyper-V balloon, virtio-mem). The
  697. * parts not manged by the buddy are unmovable PG_offline pages. In
  698. * some cases (virtio-mem), such pages can be skipped during
  699. * memory offlining, however, cannot be moved/allocated. These
  700. * techniques might use alloc_contig_range() to hide previously
  701. * exposed pages from the buddy again (e.g., to implement some sort
  702. * of memory unplug in virtio-mem).
  703. * 6. ZERO_PAGE(0), kernelcore/movablecore setups might create
  704. * situations where ZERO_PAGE(0) which is allocated differently
  705. * on different platforms may end up in a movable zone. ZERO_PAGE(0)
  706. * cannot be migrated.
  707. * 7. Memory-hotplug: when using memmap_on_memory and onlining the
  708. * memory to the MOVABLE zone, the vmemmap pages are also placed in
  709. * such zone. Such pages cannot be really moved around as they are
  710. * self-stored in the range, but they are treated as movable when
  711. * the range they describe is about to be offlined.
  712. *
  713. * In general, no unmovable allocations that degrade memory offlining
  714. * should end up in ZONE_MOVABLE. Allocators (like alloc_contig_range())
  715. * have to expect that migrating pages in ZONE_MOVABLE can fail (even
  716. * if has_unmovable_pages() states that there are no unmovable pages,
  717. * there can be false negatives).
  718. */
  719. ZONE_MOVABLE,
  720. #ifdef CONFIG_ZONE_DEVICE
  721. ZONE_DEVICE,
  722. #endif
  723. __MAX_NR_ZONES
  724. };
  725. #ifndef __GENERATING_BOUNDS_H
  726. #define ASYNC_AND_SYNC 2
  727. struct zone {
  728. /* Read-mostly fields */
  729. /* zone watermarks, access with *_wmark_pages(zone) macros */
  730. unsigned long _watermark[NR_WMARK];
  731. unsigned long watermark_boost;
  732. unsigned long nr_reserved_highatomic;
  733. /*
  734. * We don't know if the memory that we're going to allocate will be
  735. * freeable or/and it will be released eventually, so to avoid totally
  736. * wasting several GB of ram we must reserve some of the lower zone
  737. * memory (otherwise we risk to run OOM on the lower zones despite
  738. * there being tons of freeable ram on the higher zones). This array is
  739. * recalculated at runtime if the sysctl_lowmem_reserve_ratio sysctl
  740. * changes.
  741. */
  742. long lowmem_reserve[MAX_NR_ZONES];
  743. #ifdef CONFIG_NUMA
  744. int node;
  745. #endif
  746. struct pglist_data *zone_pgdat;
  747. struct per_cpu_pages __percpu *per_cpu_pageset;
  748. struct per_cpu_zonestat __percpu *per_cpu_zonestats;
  749. /*
  750. * the high and batch values are copied to individual pagesets for
  751. * faster access
  752. */
  753. int pageset_high;
  754. int pageset_batch;
  755. #ifndef CONFIG_SPARSEMEM
  756. /*
  757. * Flags for a pageblock_nr_pages block. See pageblock-flags.h.
  758. * In SPARSEMEM, this map is stored in struct mem_section
  759. */
  760. unsigned long *pageblock_flags;
  761. #endif /* CONFIG_SPARSEMEM */
  762. /* zone_start_pfn == zone_start_paddr >> PAGE_SHIFT */
  763. unsigned long zone_start_pfn;
  764. /*
  765. * spanned_pages is the total pages spanned by the zone, including
  766. * holes, which is calculated as:
  767. * spanned_pages = zone_end_pfn - zone_start_pfn;
  768. *
  769. * present_pages is physical pages existing within the zone, which
  770. * is calculated as:
  771. * present_pages = spanned_pages - absent_pages(pages in holes);
  772. *
  773. * present_early_pages is present pages existing within the zone
  774. * located on memory available since early boot, excluding hotplugged
  775. * memory.
  776. *
  777. * managed_pages is present pages managed by the buddy system, which
  778. * is calculated as (reserved_pages includes pages allocated by the
  779. * bootmem allocator):
  780. * managed_pages = present_pages - reserved_pages;
  781. *
  782. * cma pages is present pages that are assigned for CMA use
  783. * (MIGRATE_CMA).
  784. *
  785. * So present_pages may be used by memory hotplug or memory power
  786. * management logic to figure out unmanaged pages by checking
  787. * (present_pages - managed_pages). And managed_pages should be used
  788. * by page allocator and vm scanner to calculate all kinds of watermarks
  789. * and thresholds.
  790. *
  791. * Locking rules:
  792. *
  793. * zone_start_pfn and spanned_pages are protected by span_seqlock.
  794. * It is a seqlock because it has to be read outside of zone->lock,
  795. * and it is done in the main allocator path. But, it is written
  796. * quite infrequently.
  797. *
  798. * The span_seq lock is declared along with zone->lock because it is
  799. * frequently read in proximity to zone->lock. It's good to
  800. * give them a chance of being in the same cacheline.
  801. *
  802. * Write access to present_pages at runtime should be protected by
  803. * mem_hotplug_begin/done(). Any reader who can't tolerant drift of
  804. * present_pages should use get_online_mems() to get a stable value.
  805. */
  806. atomic_long_t managed_pages;
  807. unsigned long spanned_pages;
  808. unsigned long present_pages;
  809. #if defined(CONFIG_MEMORY_HOTPLUG)
  810. unsigned long present_early_pages;
  811. #endif
  812. #ifdef CONFIG_CMA
  813. unsigned long cma_pages;
  814. #endif
  815. const char *name;
  816. #ifdef CONFIG_MEMORY_ISOLATION
  817. /*
  818. * Number of isolated pageblock. It is used to solve incorrect
  819. * freepage counting problem due to racy retrieving migratetype
  820. * of pageblock. Protected by zone->lock.
  821. */
  822. unsigned long nr_isolate_pageblock;
  823. #endif
  824. #ifdef CONFIG_MEMORY_HOTPLUG
  825. /* see spanned/present_pages for more description */
  826. seqlock_t span_seqlock;
  827. #endif
  828. int initialized;
  829. /* Write-intensive fields used from the page allocator */
  830. CACHELINE_PADDING(_pad1_);
  831. /* free areas of different sizes */
  832. struct free_area free_area[MAX_ORDER];
  833. /* zone flags, see below */
  834. unsigned long flags;
  835. /* Primarily protects free_area */
  836. spinlock_t lock;
  837. /* Write-intensive fields used by compaction and vmstats. */
  838. CACHELINE_PADDING(_pad2_);
  839. /*
  840. * When free pages are below this point, additional steps are taken
  841. * when reading the number of free pages to avoid per-cpu counter
  842. * drift allowing watermarks to be breached
  843. */
  844. unsigned long percpu_drift_mark;
  845. #if defined CONFIG_COMPACTION || defined CONFIG_CMA
  846. /* pfn where compaction free scanner should start */
  847. unsigned long compact_cached_free_pfn;
  848. /* pfn where compaction migration scanner should start */
  849. unsigned long compact_cached_migrate_pfn[ASYNC_AND_SYNC];
  850. unsigned long compact_init_migrate_pfn;
  851. unsigned long compact_init_free_pfn;
  852. #endif
  853. #ifdef CONFIG_COMPACTION
  854. /*
  855. * On compaction failure, 1<<compact_defer_shift compactions
  856. * are skipped before trying again. The number attempted since
  857. * last failure is tracked with compact_considered.
  858. * compact_order_failed is the minimum compaction failed order.
  859. */
  860. unsigned int compact_considered;
  861. unsigned int compact_defer_shift;
  862. int compact_order_failed;
  863. #endif
  864. #if defined CONFIG_COMPACTION || defined CONFIG_CMA
  865. /* Set to true when the PG_migrate_skip bits should be cleared */
  866. bool compact_blockskip_flush;
  867. #endif
  868. bool contiguous;
  869. CACHELINE_PADDING(_pad3_);
  870. /* Zone statistics */
  871. atomic_long_t vm_stat[NR_VM_ZONE_STAT_ITEMS];
  872. atomic_long_t vm_numa_event[NR_VM_NUMA_EVENT_ITEMS];
  873. ANDROID_KABI_RESERVE(1);
  874. ANDROID_KABI_RESERVE(2);
  875. ANDROID_KABI_RESERVE(3);
  876. ANDROID_KABI_RESERVE(4);
  877. } ____cacheline_internodealigned_in_smp;
  878. enum pgdat_flags {
  879. PGDAT_DIRTY, /* reclaim scanning has recently found
  880. * many dirty file pages at the tail
  881. * of the LRU.
  882. */
  883. PGDAT_WRITEBACK, /* reclaim scanning has recently found
  884. * many pages under writeback
  885. */
  886. PGDAT_RECLAIM_LOCKED, /* prevents concurrent reclaim */
  887. };
  888. enum zone_flags {
  889. ZONE_BOOSTED_WATERMARK, /* zone recently boosted watermarks.
  890. * Cleared when kswapd is woken.
  891. */
  892. ZONE_RECLAIM_ACTIVE, /* kswapd may be scanning the zone. */
  893. };
  894. static inline unsigned long zone_managed_pages(struct zone *zone)
  895. {
  896. return (unsigned long)atomic_long_read(&zone->managed_pages);
  897. }
  898. static inline unsigned long zone_cma_pages(struct zone *zone)
  899. {
  900. #ifdef CONFIG_CMA
  901. return zone->cma_pages;
  902. #else
  903. return 0;
  904. #endif
  905. }
  906. static inline unsigned long zone_end_pfn(const struct zone *zone)
  907. {
  908. return zone->zone_start_pfn + zone->spanned_pages;
  909. }
  910. static inline bool zone_spans_pfn(const struct zone *zone, unsigned long pfn)
  911. {
  912. return zone->zone_start_pfn <= pfn && pfn < zone_end_pfn(zone);
  913. }
  914. static inline bool zone_is_initialized(struct zone *zone)
  915. {
  916. return zone->initialized;
  917. }
  918. static inline bool zone_is_empty(struct zone *zone)
  919. {
  920. return zone->spanned_pages == 0;
  921. }
  922. #ifndef BUILD_VDSO32_64
  923. /*
  924. * The zone field is never updated after free_area_init_core()
  925. * sets it, so none of the operations on it need to be atomic.
  926. */
  927. /* Page flags: | [SECTION] | [NODE] | ZONE | [LAST_CPUPID] | ... | FLAGS | */
  928. #define SECTIONS_PGOFF ((sizeof(unsigned long)*8) - SECTIONS_WIDTH)
  929. #define NODES_PGOFF (SECTIONS_PGOFF - NODES_WIDTH)
  930. #define ZONES_PGOFF (NODES_PGOFF - ZONES_WIDTH)
  931. #define LAST_CPUPID_PGOFF (ZONES_PGOFF - LAST_CPUPID_WIDTH)
  932. #define KASAN_TAG_PGOFF (LAST_CPUPID_PGOFF - KASAN_TAG_WIDTH)
  933. #define LRU_GEN_PGOFF (KASAN_TAG_PGOFF - LRU_GEN_WIDTH)
  934. #define LRU_REFS_PGOFF (LRU_GEN_PGOFF - LRU_REFS_WIDTH)
  935. /*
  936. * Define the bit shifts to access each section. For non-existent
  937. * sections we define the shift as 0; that plus a 0 mask ensures
  938. * the compiler will optimise away reference to them.
  939. */
  940. #define SECTIONS_PGSHIFT (SECTIONS_PGOFF * (SECTIONS_WIDTH != 0))
  941. #define NODES_PGSHIFT (NODES_PGOFF * (NODES_WIDTH != 0))
  942. #define ZONES_PGSHIFT (ZONES_PGOFF * (ZONES_WIDTH != 0))
  943. #define LAST_CPUPID_PGSHIFT (LAST_CPUPID_PGOFF * (LAST_CPUPID_WIDTH != 0))
  944. #define KASAN_TAG_PGSHIFT (KASAN_TAG_PGOFF * (KASAN_TAG_WIDTH != 0))
  945. /* NODE:ZONE or SECTION:ZONE is used to ID a zone for the buddy allocator */
  946. #ifdef NODE_NOT_IN_PAGE_FLAGS
  947. #define ZONEID_SHIFT (SECTIONS_SHIFT + ZONES_SHIFT)
  948. #define ZONEID_PGOFF ((SECTIONS_PGOFF < ZONES_PGOFF) ? \
  949. SECTIONS_PGOFF : ZONES_PGOFF)
  950. #else
  951. #define ZONEID_SHIFT (NODES_SHIFT + ZONES_SHIFT)
  952. #define ZONEID_PGOFF ((NODES_PGOFF < ZONES_PGOFF) ? \
  953. NODES_PGOFF : ZONES_PGOFF)
  954. #endif
  955. #define ZONEID_PGSHIFT (ZONEID_PGOFF * (ZONEID_SHIFT != 0))
  956. #define ZONES_MASK ((1UL << ZONES_WIDTH) - 1)
  957. #define NODES_MASK ((1UL << NODES_WIDTH) - 1)
  958. #define SECTIONS_MASK ((1UL << SECTIONS_WIDTH) - 1)
  959. #define LAST_CPUPID_MASK ((1UL << LAST_CPUPID_SHIFT) - 1)
  960. #define KASAN_TAG_MASK ((1UL << KASAN_TAG_WIDTH) - 1)
  961. #define ZONEID_MASK ((1UL << ZONEID_SHIFT) - 1)
  962. static inline enum zone_type page_zonenum(const struct page *page)
  963. {
  964. ASSERT_EXCLUSIVE_BITS(page->flags, ZONES_MASK << ZONES_PGSHIFT);
  965. return (page->flags >> ZONES_PGSHIFT) & ZONES_MASK;
  966. }
  967. static inline enum zone_type folio_zonenum(const struct folio *folio)
  968. {
  969. return page_zonenum(&folio->page);
  970. }
  971. #ifdef CONFIG_ZONE_DEVICE
  972. static inline bool is_zone_device_page(const struct page *page)
  973. {
  974. return page_zonenum(page) == ZONE_DEVICE;
  975. }
  976. extern void memmap_init_zone_device(struct zone *, unsigned long,
  977. unsigned long, struct dev_pagemap *);
  978. #else
  979. static inline bool is_zone_device_page(const struct page *page)
  980. {
  981. return false;
  982. }
  983. #endif
  984. static inline bool folio_is_zone_device(const struct folio *folio)
  985. {
  986. return is_zone_device_page(&folio->page);
  987. }
  988. static inline bool is_zone_movable_page(const struct page *page)
  989. {
  990. return page_zonenum(page) == ZONE_MOVABLE;
  991. }
  992. #endif
  993. /*
  994. * Return true if [start_pfn, start_pfn + nr_pages) range has a non-empty
  995. * intersection with the given zone
  996. */
  997. static inline bool zone_intersects(struct zone *zone,
  998. unsigned long start_pfn, unsigned long nr_pages)
  999. {
  1000. if (zone_is_empty(zone))
  1001. return false;
  1002. if (start_pfn >= zone_end_pfn(zone) ||
  1003. start_pfn + nr_pages <= zone->zone_start_pfn)
  1004. return false;
  1005. return true;
  1006. }
  1007. /*
  1008. * The "priority" of VM scanning is how much of the queues we will scan in one
  1009. * go. A value of 12 for DEF_PRIORITY implies that we will scan 1/4096th of the
  1010. * queues ("queue_length >> 12") during an aging round.
  1011. */
  1012. #define DEF_PRIORITY 12
  1013. /* Maximum number of zones on a zonelist */
  1014. #define MAX_ZONES_PER_ZONELIST (MAX_NUMNODES * MAX_NR_ZONES)
  1015. enum {
  1016. ZONELIST_FALLBACK, /* zonelist with fallback */
  1017. #ifdef CONFIG_NUMA
  1018. /*
  1019. * The NUMA zonelists are doubled because we need zonelists that
  1020. * restrict the allocations to a single node for __GFP_THISNODE.
  1021. */
  1022. ZONELIST_NOFALLBACK, /* zonelist without fallback (__GFP_THISNODE) */
  1023. #endif
  1024. MAX_ZONELISTS
  1025. };
  1026. /*
  1027. * This struct contains information about a zone in a zonelist. It is stored
  1028. * here to avoid dereferences into large structures and lookups of tables
  1029. */
  1030. struct zoneref {
  1031. struct zone *zone; /* Pointer to actual zone */
  1032. int zone_idx; /* zone_idx(zoneref->zone) */
  1033. };
  1034. /*
  1035. * One allocation request operates on a zonelist. A zonelist
  1036. * is a list of zones, the first one is the 'goal' of the
  1037. * allocation, the other zones are fallback zones, in decreasing
  1038. * priority.
  1039. *
  1040. * To speed the reading of the zonelist, the zonerefs contain the zone index
  1041. * of the entry being read. Helper functions to access information given
  1042. * a struct zoneref are
  1043. *
  1044. * zonelist_zone() - Return the struct zone * for an entry in _zonerefs
  1045. * zonelist_zone_idx() - Return the index of the zone for an entry
  1046. * zonelist_node_idx() - Return the index of the node for an entry
  1047. */
  1048. struct zonelist {
  1049. struct zoneref _zonerefs[MAX_ZONES_PER_ZONELIST + 1];
  1050. };
  1051. /*
  1052. * The array of struct pages for flatmem.
  1053. * It must be declared for SPARSEMEM as well because there are configurations
  1054. * that rely on that.
  1055. */
  1056. extern struct page *mem_map;
  1057. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  1058. struct deferred_split {
  1059. spinlock_t split_queue_lock;
  1060. struct list_head split_queue;
  1061. unsigned long split_queue_len;
  1062. };
  1063. #endif
  1064. /*
  1065. * On NUMA machines, each NUMA node would have a pg_data_t to describe
  1066. * it's memory layout. On UMA machines there is a single pglist_data which
  1067. * describes the whole memory.
  1068. *
  1069. * Memory statistics and page replacement data structures are maintained on a
  1070. * per-zone basis.
  1071. */
  1072. typedef struct pglist_data {
  1073. /*
  1074. * node_zones contains just the zones for THIS node. Not all of the
  1075. * zones may be populated, but it is the full list. It is referenced by
  1076. * this node's node_zonelists as well as other node's node_zonelists.
  1077. */
  1078. struct zone node_zones[MAX_NR_ZONES];
  1079. /*
  1080. * node_zonelists contains references to all zones in all nodes.
  1081. * Generally the first zones will be references to this node's
  1082. * node_zones.
  1083. */
  1084. struct zonelist node_zonelists[MAX_ZONELISTS];
  1085. int nr_zones; /* number of populated zones in this node */
  1086. #ifdef CONFIG_FLATMEM /* means !SPARSEMEM */
  1087. struct page *node_mem_map;
  1088. #ifdef CONFIG_PAGE_EXTENSION
  1089. struct page_ext *node_page_ext;
  1090. #endif
  1091. #endif
  1092. #if defined(CONFIG_MEMORY_HOTPLUG) || defined(CONFIG_DEFERRED_STRUCT_PAGE_INIT)
  1093. /*
  1094. * Must be held any time you expect node_start_pfn,
  1095. * node_present_pages, node_spanned_pages or nr_zones to stay constant.
  1096. * Also synchronizes pgdat->first_deferred_pfn during deferred page
  1097. * init.
  1098. *
  1099. * pgdat_resize_lock() and pgdat_resize_unlock() are provided to
  1100. * manipulate node_size_lock without checking for CONFIG_MEMORY_HOTPLUG
  1101. * or CONFIG_DEFERRED_STRUCT_PAGE_INIT.
  1102. *
  1103. * Nests above zone->lock and zone->span_seqlock
  1104. */
  1105. spinlock_t node_size_lock;
  1106. #endif
  1107. unsigned long node_start_pfn;
  1108. unsigned long node_present_pages; /* total number of physical pages */
  1109. unsigned long node_spanned_pages; /* total size of physical page
  1110. range, including holes */
  1111. int node_id;
  1112. wait_queue_head_t kswapd_wait;
  1113. wait_queue_head_t pfmemalloc_wait;
  1114. /* workqueues for throttling reclaim for different reasons. */
  1115. wait_queue_head_t reclaim_wait[NR_VMSCAN_THROTTLE];
  1116. atomic_t nr_writeback_throttled;/* nr of writeback-throttled tasks */
  1117. unsigned long nr_reclaim_start; /* nr pages written while throttled
  1118. * when throttling started. */
  1119. #ifdef CONFIG_MEMORY_HOTPLUG
  1120. struct mutex kswapd_lock;
  1121. #endif
  1122. struct task_struct *kswapd; /* Protected by kswapd_lock */
  1123. struct task_struct *mkswapd[MAX_KSWAPD_THREADS];
  1124. int kswapd_order;
  1125. enum zone_type kswapd_highest_zoneidx;
  1126. int kswapd_failures; /* Number of 'reclaimed == 0' runs */
  1127. ANDROID_OEM_DATA(1);
  1128. #ifdef CONFIG_COMPACTION
  1129. int kcompactd_max_order;
  1130. enum zone_type kcompactd_highest_zoneidx;
  1131. wait_queue_head_t kcompactd_wait;
  1132. struct task_struct *kcompactd;
  1133. bool proactive_compact_trigger;
  1134. #endif
  1135. /*
  1136. * This is a per-node reserve of pages that are not available
  1137. * to userspace allocations.
  1138. */
  1139. unsigned long totalreserve_pages;
  1140. #ifdef CONFIG_NUMA
  1141. /*
  1142. * node reclaim becomes active if more unmapped pages exist.
  1143. */
  1144. unsigned long min_unmapped_pages;
  1145. unsigned long min_slab_pages;
  1146. #endif /* CONFIG_NUMA */
  1147. /* Write-intensive fields used by page reclaim */
  1148. CACHELINE_PADDING(_pad1_);
  1149. #ifdef CONFIG_DEFERRED_STRUCT_PAGE_INIT
  1150. /*
  1151. * If memory initialisation on large machines is deferred then this
  1152. * is the first PFN that needs to be initialised.
  1153. */
  1154. unsigned long first_deferred_pfn;
  1155. #endif /* CONFIG_DEFERRED_STRUCT_PAGE_INIT */
  1156. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  1157. struct deferred_split deferred_split_queue;
  1158. #endif
  1159. #ifdef CONFIG_NUMA_BALANCING
  1160. /* start time in ms of current promote rate limit period */
  1161. unsigned int nbp_rl_start;
  1162. /* number of promote candidate pages at start time of current rate limit period */
  1163. unsigned long nbp_rl_nr_cand;
  1164. /* promote threshold in ms */
  1165. unsigned int nbp_threshold;
  1166. /* start time in ms of current promote threshold adjustment period */
  1167. unsigned int nbp_th_start;
  1168. /*
  1169. * number of promote candidate pages at stat time of current promote
  1170. * threshold adjustment period
  1171. */
  1172. unsigned long nbp_th_nr_cand;
  1173. #endif
  1174. /* Fields commonly accessed by the page reclaim scanner */
  1175. /*
  1176. * NOTE: THIS IS UNUSED IF MEMCG IS ENABLED.
  1177. *
  1178. * Use mem_cgroup_lruvec() to look up lruvecs.
  1179. */
  1180. struct lruvec __lruvec;
  1181. unsigned long flags;
  1182. #ifdef CONFIG_LRU_GEN
  1183. /* kswap mm walk data */
  1184. struct lru_gen_mm_walk mm_walk;
  1185. /* lru_gen_folio list */
  1186. struct lru_gen_memcg memcg_lru;
  1187. #endif
  1188. CACHELINE_PADDING(_pad2_);
  1189. /* Per-node vmstats */
  1190. struct per_cpu_nodestat __percpu *per_cpu_nodestats;
  1191. atomic_long_t vm_stat[NR_VM_NODE_STAT_ITEMS];
  1192. #ifdef CONFIG_NUMA
  1193. struct memory_tier __rcu *memtier;
  1194. #endif
  1195. } pg_data_t;
  1196. #define node_present_pages(nid) (NODE_DATA(nid)->node_present_pages)
  1197. #define node_spanned_pages(nid) (NODE_DATA(nid)->node_spanned_pages)
  1198. #define node_start_pfn(nid) (NODE_DATA(nid)->node_start_pfn)
  1199. #define node_end_pfn(nid) pgdat_end_pfn(NODE_DATA(nid))
  1200. static inline unsigned long pgdat_end_pfn(pg_data_t *pgdat)
  1201. {
  1202. return pgdat->node_start_pfn + pgdat->node_spanned_pages;
  1203. }
  1204. #include <linux/memory_hotplug.h>
  1205. void build_all_zonelists(pg_data_t *pgdat);
  1206. void wakeup_kswapd(struct zone *zone, gfp_t gfp_mask, int order,
  1207. enum zone_type highest_zoneidx);
  1208. bool __zone_watermark_ok(struct zone *z, unsigned int order, unsigned long mark,
  1209. int highest_zoneidx, unsigned int alloc_flags,
  1210. long free_pages);
  1211. bool zone_watermark_ok(struct zone *z, unsigned int order,
  1212. unsigned long mark, int highest_zoneidx,
  1213. unsigned int alloc_flags);
  1214. bool zone_watermark_ok_safe(struct zone *z, unsigned int order,
  1215. unsigned long mark, int highest_zoneidx);
  1216. /*
  1217. * Memory initialization context, use to differentiate memory added by
  1218. * the platform statically or via memory hotplug interface.
  1219. */
  1220. enum meminit_context {
  1221. MEMINIT_EARLY,
  1222. MEMINIT_HOTPLUG,
  1223. };
  1224. extern void init_currently_empty_zone(struct zone *zone, unsigned long start_pfn,
  1225. unsigned long size);
  1226. extern void lruvec_init(struct lruvec *lruvec);
  1227. static inline struct pglist_data *lruvec_pgdat(struct lruvec *lruvec)
  1228. {
  1229. #ifdef CONFIG_MEMCG
  1230. return lruvec->pgdat;
  1231. #else
  1232. return container_of(lruvec, struct pglist_data, __lruvec);
  1233. #endif
  1234. }
  1235. #ifdef CONFIG_HAVE_MEMORYLESS_NODES
  1236. int local_memory_node(int node_id);
  1237. #else
  1238. static inline int local_memory_node(int node_id) { return node_id; };
  1239. #endif
  1240. /*
  1241. * zone_idx() returns 0 for the ZONE_DMA zone, 1 for the ZONE_NORMAL zone, etc.
  1242. */
  1243. #define zone_idx(zone) ((zone) - (zone)->zone_pgdat->node_zones)
  1244. #ifdef CONFIG_ZONE_DEVICE
  1245. static inline bool zone_is_zone_device(struct zone *zone)
  1246. {
  1247. return zone_idx(zone) == ZONE_DEVICE;
  1248. }
  1249. #else
  1250. static inline bool zone_is_zone_device(struct zone *zone)
  1251. {
  1252. return false;
  1253. }
  1254. #endif
  1255. /*
  1256. * Returns true if a zone has pages managed by the buddy allocator.
  1257. * All the reclaim decisions have to use this function rather than
  1258. * populated_zone(). If the whole zone is reserved then we can easily
  1259. * end up with populated_zone() && !managed_zone().
  1260. */
  1261. static inline bool managed_zone(struct zone *zone)
  1262. {
  1263. return zone_managed_pages(zone);
  1264. }
  1265. /* Returns true if a zone has memory */
  1266. static inline bool populated_zone(struct zone *zone)
  1267. {
  1268. return zone->present_pages;
  1269. }
  1270. #ifdef CONFIG_NUMA
  1271. static inline int zone_to_nid(struct zone *zone)
  1272. {
  1273. return zone->node;
  1274. }
  1275. static inline void zone_set_nid(struct zone *zone, int nid)
  1276. {
  1277. zone->node = nid;
  1278. }
  1279. #else
  1280. static inline int zone_to_nid(struct zone *zone)
  1281. {
  1282. return 0;
  1283. }
  1284. static inline void zone_set_nid(struct zone *zone, int nid) {}
  1285. #endif
  1286. extern int movable_zone;
  1287. static inline int is_highmem_idx(enum zone_type idx)
  1288. {
  1289. #ifdef CONFIG_HIGHMEM
  1290. return (idx == ZONE_HIGHMEM ||
  1291. (idx == ZONE_MOVABLE && movable_zone == ZONE_HIGHMEM));
  1292. #else
  1293. return 0;
  1294. #endif
  1295. }
  1296. /**
  1297. * is_highmem - helper function to quickly check if a struct zone is a
  1298. * highmem zone or not. This is an attempt to keep references
  1299. * to ZONE_{DMA/NORMAL/HIGHMEM/etc} in general code to a minimum.
  1300. * @zone: pointer to struct zone variable
  1301. * Return: 1 for a highmem zone, 0 otherwise
  1302. */
  1303. static inline int is_highmem(struct zone *zone)
  1304. {
  1305. return is_highmem_idx(zone_idx(zone));
  1306. }
  1307. #ifdef CONFIG_ZONE_DMA
  1308. bool has_managed_dma(void);
  1309. #else
  1310. static inline bool has_managed_dma(void)
  1311. {
  1312. return false;
  1313. }
  1314. #endif
  1315. /* These two functions are used to setup the per zone pages min values */
  1316. struct ctl_table;
  1317. int min_free_kbytes_sysctl_handler(struct ctl_table *, int, void *, size_t *,
  1318. loff_t *);
  1319. int watermark_scale_factor_sysctl_handler(struct ctl_table *, int, void *,
  1320. size_t *, loff_t *);
  1321. extern int sysctl_lowmem_reserve_ratio[MAX_NR_ZONES];
  1322. int lowmem_reserve_ratio_sysctl_handler(struct ctl_table *, int, void *,
  1323. size_t *, loff_t *);
  1324. int percpu_pagelist_high_fraction_sysctl_handler(struct ctl_table *, int,
  1325. void *, size_t *, loff_t *);
  1326. int sysctl_min_unmapped_ratio_sysctl_handler(struct ctl_table *, int,
  1327. void *, size_t *, loff_t *);
  1328. int sysctl_min_slab_ratio_sysctl_handler(struct ctl_table *, int,
  1329. void *, size_t *, loff_t *);
  1330. int numa_zonelist_order_handler(struct ctl_table *, int,
  1331. void *, size_t *, loff_t *);
  1332. extern int percpu_pagelist_high_fraction;
  1333. extern char numa_zonelist_order[];
  1334. #define NUMA_ZONELIST_ORDER_LEN 16
  1335. #ifndef CONFIG_NUMA
  1336. extern struct pglist_data contig_page_data;
  1337. static inline struct pglist_data *NODE_DATA(int nid)
  1338. {
  1339. return &contig_page_data;
  1340. }
  1341. #else /* CONFIG_NUMA */
  1342. #include <asm/mmzone.h>
  1343. #endif /* !CONFIG_NUMA */
  1344. extern struct pglist_data *first_online_pgdat(void);
  1345. extern struct pglist_data *next_online_pgdat(struct pglist_data *pgdat);
  1346. extern struct zone *next_zone(struct zone *zone);
  1347. extern int isolate_anon_lru_page(struct page *page);
  1348. /**
  1349. * for_each_online_pgdat - helper macro to iterate over all online nodes
  1350. * @pgdat: pointer to a pg_data_t variable
  1351. */
  1352. #define for_each_online_pgdat(pgdat) \
  1353. for (pgdat = first_online_pgdat(); \
  1354. pgdat; \
  1355. pgdat = next_online_pgdat(pgdat))
  1356. /**
  1357. * for_each_zone - helper macro to iterate over all memory zones
  1358. * @zone: pointer to struct zone variable
  1359. *
  1360. * The user only needs to declare the zone variable, for_each_zone
  1361. * fills it in.
  1362. */
  1363. #define for_each_zone(zone) \
  1364. for (zone = (first_online_pgdat())->node_zones; \
  1365. zone; \
  1366. zone = next_zone(zone))
  1367. #define for_each_populated_zone(zone) \
  1368. for (zone = (first_online_pgdat())->node_zones; \
  1369. zone; \
  1370. zone = next_zone(zone)) \
  1371. if (!populated_zone(zone)) \
  1372. ; /* do nothing */ \
  1373. else
  1374. static inline struct zone *zonelist_zone(struct zoneref *zoneref)
  1375. {
  1376. return zoneref->zone;
  1377. }
  1378. static inline int zonelist_zone_idx(struct zoneref *zoneref)
  1379. {
  1380. return zoneref->zone_idx;
  1381. }
  1382. static inline int zonelist_node_idx(struct zoneref *zoneref)
  1383. {
  1384. return zone_to_nid(zoneref->zone);
  1385. }
  1386. struct zoneref *__next_zones_zonelist(struct zoneref *z,
  1387. enum zone_type highest_zoneidx,
  1388. nodemask_t *nodes);
  1389. /**
  1390. * next_zones_zonelist - Returns the next zone at or below highest_zoneidx within the allowed nodemask using a cursor within a zonelist as a starting point
  1391. * @z: The cursor used as a starting point for the search
  1392. * @highest_zoneidx: The zone index of the highest zone to return
  1393. * @nodes: An optional nodemask to filter the zonelist with
  1394. *
  1395. * This function returns the next zone at or below a given zone index that is
  1396. * within the allowed nodemask using a cursor as the starting point for the
  1397. * search. The zoneref returned is a cursor that represents the current zone
  1398. * being examined. It should be advanced by one before calling
  1399. * next_zones_zonelist again.
  1400. *
  1401. * Return: the next zone at or below highest_zoneidx within the allowed
  1402. * nodemask using a cursor within a zonelist as a starting point
  1403. */
  1404. static __always_inline struct zoneref *next_zones_zonelist(struct zoneref *z,
  1405. enum zone_type highest_zoneidx,
  1406. nodemask_t *nodes)
  1407. {
  1408. if (likely(!nodes && zonelist_zone_idx(z) <= highest_zoneidx))
  1409. return z;
  1410. return __next_zones_zonelist(z, highest_zoneidx, nodes);
  1411. }
  1412. /**
  1413. * first_zones_zonelist - Returns the first zone at or below highest_zoneidx within the allowed nodemask in a zonelist
  1414. * @zonelist: The zonelist to search for a suitable zone
  1415. * @highest_zoneidx: The zone index of the highest zone to return
  1416. * @nodes: An optional nodemask to filter the zonelist with
  1417. *
  1418. * This function returns the first zone at or below a given zone index that is
  1419. * within the allowed nodemask. The zoneref returned is a cursor that can be
  1420. * used to iterate the zonelist with next_zones_zonelist by advancing it by
  1421. * one before calling.
  1422. *
  1423. * When no eligible zone is found, zoneref->zone is NULL (zoneref itself is
  1424. * never NULL). This may happen either genuinely, or due to concurrent nodemask
  1425. * update due to cpuset modification.
  1426. *
  1427. * Return: Zoneref pointer for the first suitable zone found
  1428. */
  1429. static inline struct zoneref *first_zones_zonelist(struct zonelist *zonelist,
  1430. enum zone_type highest_zoneidx,
  1431. nodemask_t *nodes)
  1432. {
  1433. return next_zones_zonelist(zonelist->_zonerefs,
  1434. highest_zoneidx, nodes);
  1435. }
  1436. /**
  1437. * for_each_zone_zonelist_nodemask - helper macro to iterate over valid zones in a zonelist at or below a given zone index and within a nodemask
  1438. * @zone: The current zone in the iterator
  1439. * @z: The current pointer within zonelist->_zonerefs being iterated
  1440. * @zlist: The zonelist being iterated
  1441. * @highidx: The zone index of the highest zone to return
  1442. * @nodemask: Nodemask allowed by the allocator
  1443. *
  1444. * This iterator iterates though all zones at or below a given zone index and
  1445. * within a given nodemask
  1446. */
  1447. #define for_each_zone_zonelist_nodemask(zone, z, zlist, highidx, nodemask) \
  1448. for (z = first_zones_zonelist(zlist, highidx, nodemask), zone = zonelist_zone(z); \
  1449. zone; \
  1450. z = next_zones_zonelist(++z, highidx, nodemask), \
  1451. zone = zonelist_zone(z))
  1452. #define for_next_zone_zonelist_nodemask(zone, z, highidx, nodemask) \
  1453. for (zone = z->zone; \
  1454. zone; \
  1455. z = next_zones_zonelist(++z, highidx, nodemask), \
  1456. zone = zonelist_zone(z))
  1457. /**
  1458. * for_each_zone_zonelist - helper macro to iterate over valid zones in a zonelist at or below a given zone index
  1459. * @zone: The current zone in the iterator
  1460. * @z: The current pointer within zonelist->zones being iterated
  1461. * @zlist: The zonelist being iterated
  1462. * @highidx: The zone index of the highest zone to return
  1463. *
  1464. * This iterator iterates though all zones at or below a given zone index.
  1465. */
  1466. #define for_each_zone_zonelist(zone, z, zlist, highidx) \
  1467. for_each_zone_zonelist_nodemask(zone, z, zlist, highidx, NULL)
  1468. /* Whether the 'nodes' are all movable nodes */
  1469. static inline bool movable_only_nodes(nodemask_t *nodes)
  1470. {
  1471. struct zonelist *zonelist;
  1472. struct zoneref *z;
  1473. int nid;
  1474. if (nodes_empty(*nodes))
  1475. return false;
  1476. /*
  1477. * We can chose arbitrary node from the nodemask to get a
  1478. * zonelist as they are interlinked. We just need to find
  1479. * at least one zone that can satisfy kernel allocations.
  1480. */
  1481. nid = first_node(*nodes);
  1482. zonelist = &NODE_DATA(nid)->node_zonelists[ZONELIST_FALLBACK];
  1483. z = first_zones_zonelist(zonelist, ZONE_NORMAL, nodes);
  1484. return (!z->zone) ? true : false;
  1485. }
  1486. #ifdef CONFIG_SPARSEMEM
  1487. #include <asm/sparsemem.h>
  1488. #endif
  1489. #ifdef CONFIG_FLATMEM
  1490. #define pfn_to_nid(pfn) (0)
  1491. #endif
  1492. #ifdef CONFIG_SPARSEMEM
  1493. /*
  1494. * PA_SECTION_SHIFT physical address to/from section number
  1495. * PFN_SECTION_SHIFT pfn to/from section number
  1496. */
  1497. #define PA_SECTION_SHIFT (SECTION_SIZE_BITS)
  1498. #define PFN_SECTION_SHIFT (SECTION_SIZE_BITS - PAGE_SHIFT)
  1499. #define NR_MEM_SECTIONS (1UL << SECTIONS_SHIFT)
  1500. #define PAGES_PER_SECTION (1UL << PFN_SECTION_SHIFT)
  1501. #define PAGE_SECTION_MASK (~(PAGES_PER_SECTION-1))
  1502. #define SECTION_BLOCKFLAGS_BITS \
  1503. ((1UL << (PFN_SECTION_SHIFT - pageblock_order)) * NR_PAGEBLOCK_BITS)
  1504. #if (MAX_ORDER - 1 + PAGE_SHIFT) > SECTION_SIZE_BITS
  1505. #error Allocator MAX_ORDER exceeds SECTION_SIZE
  1506. #endif
  1507. static inline unsigned long pfn_to_section_nr(unsigned long pfn)
  1508. {
  1509. return pfn >> PFN_SECTION_SHIFT;
  1510. }
  1511. static inline unsigned long section_nr_to_pfn(unsigned long sec)
  1512. {
  1513. return sec << PFN_SECTION_SHIFT;
  1514. }
  1515. #define SECTION_ALIGN_UP(pfn) (((pfn) + PAGES_PER_SECTION - 1) & PAGE_SECTION_MASK)
  1516. #define SECTION_ALIGN_DOWN(pfn) ((pfn) & PAGE_SECTION_MASK)
  1517. #define SUBSECTION_SHIFT 21
  1518. #define SUBSECTION_SIZE (1UL << SUBSECTION_SHIFT)
  1519. #define PFN_SUBSECTION_SHIFT (SUBSECTION_SHIFT - PAGE_SHIFT)
  1520. #define PAGES_PER_SUBSECTION (1UL << PFN_SUBSECTION_SHIFT)
  1521. #define PAGE_SUBSECTION_MASK (~(PAGES_PER_SUBSECTION-1))
  1522. #if SUBSECTION_SHIFT > SECTION_SIZE_BITS
  1523. #error Subsection size exceeds section size
  1524. #else
  1525. #define SUBSECTIONS_PER_SECTION (1UL << (SECTION_SIZE_BITS - SUBSECTION_SHIFT))
  1526. #endif
  1527. #define SUBSECTION_ALIGN_UP(pfn) ALIGN((pfn), PAGES_PER_SUBSECTION)
  1528. #define SUBSECTION_ALIGN_DOWN(pfn) ((pfn) & PAGE_SUBSECTION_MASK)
  1529. struct mem_section_usage {
  1530. #ifdef CONFIG_SPARSEMEM_VMEMMAP
  1531. DECLARE_BITMAP(subsection_map, SUBSECTIONS_PER_SECTION);
  1532. #endif
  1533. /* See declaration of similar field in struct zone */
  1534. unsigned long pageblock_flags[0];
  1535. };
  1536. void subsection_map_init(unsigned long pfn, unsigned long nr_pages);
  1537. struct page;
  1538. struct page_ext;
  1539. struct mem_section {
  1540. /*
  1541. * This is, logically, a pointer to an array of struct
  1542. * pages. However, it is stored with some other magic.
  1543. * (see sparse.c::sparse_init_one_section())
  1544. *
  1545. * Additionally during early boot we encode node id of
  1546. * the location of the section here to guide allocation.
  1547. * (see sparse.c::memory_present())
  1548. *
  1549. * Making it a UL at least makes someone do a cast
  1550. * before using it wrong.
  1551. */
  1552. unsigned long section_mem_map;
  1553. struct mem_section_usage *usage;
  1554. #ifdef CONFIG_PAGE_EXTENSION
  1555. /*
  1556. * If SPARSEMEM, pgdat doesn't have page_ext pointer. We use
  1557. * section. (see page_ext.h about this.)
  1558. */
  1559. struct page_ext *page_ext;
  1560. unsigned long pad;
  1561. #endif
  1562. /*
  1563. * WARNING: mem_section must be a power-of-2 in size for the
  1564. * calculation and use of SECTION_ROOT_MASK to make sense.
  1565. */
  1566. };
  1567. #ifdef CONFIG_SPARSEMEM_EXTREME
  1568. #define SECTIONS_PER_ROOT (PAGE_SIZE / sizeof (struct mem_section))
  1569. #else
  1570. #define SECTIONS_PER_ROOT 1
  1571. #endif
  1572. #define SECTION_NR_TO_ROOT(sec) ((sec) / SECTIONS_PER_ROOT)
  1573. #define NR_SECTION_ROOTS DIV_ROUND_UP(NR_MEM_SECTIONS, SECTIONS_PER_ROOT)
  1574. #define SECTION_ROOT_MASK (SECTIONS_PER_ROOT - 1)
  1575. #ifdef CONFIG_SPARSEMEM_EXTREME
  1576. extern struct mem_section **mem_section;
  1577. #else
  1578. extern struct mem_section mem_section[NR_SECTION_ROOTS][SECTIONS_PER_ROOT];
  1579. #endif
  1580. static inline unsigned long *section_to_usemap(struct mem_section *ms)
  1581. {
  1582. return ms->usage->pageblock_flags;
  1583. }
  1584. static inline struct mem_section *__nr_to_section(unsigned long nr)
  1585. {
  1586. unsigned long root = SECTION_NR_TO_ROOT(nr);
  1587. if (unlikely(root >= NR_SECTION_ROOTS))
  1588. return NULL;
  1589. #ifdef CONFIG_SPARSEMEM_EXTREME
  1590. if (!mem_section || !mem_section[root])
  1591. return NULL;
  1592. #endif
  1593. return &mem_section[root][nr & SECTION_ROOT_MASK];
  1594. }
  1595. extern size_t mem_section_usage_size(void);
  1596. /*
  1597. * We use the lower bits of the mem_map pointer to store
  1598. * a little bit of information. The pointer is calculated
  1599. * as mem_map - section_nr_to_pfn(pnum). The result is
  1600. * aligned to the minimum alignment of the two values:
  1601. * 1. All mem_map arrays are page-aligned.
  1602. * 2. section_nr_to_pfn() always clears PFN_SECTION_SHIFT
  1603. * lowest bits. PFN_SECTION_SHIFT is arch-specific
  1604. * (equal SECTION_SIZE_BITS - PAGE_SHIFT), and the
  1605. * worst combination is powerpc with 256k pages,
  1606. * which results in PFN_SECTION_SHIFT equal 6.
  1607. * To sum it up, at least 6 bits are available on all architectures.
  1608. * However, we can exceed 6 bits on some other architectures except
  1609. * powerpc (e.g. 15 bits are available on x86_64, 13 bits are available
  1610. * with the worst case of 64K pages on arm64) if we make sure the
  1611. * exceeded bit is not applicable to powerpc.
  1612. */
  1613. enum {
  1614. SECTION_MARKED_PRESENT_BIT,
  1615. SECTION_HAS_MEM_MAP_BIT,
  1616. SECTION_IS_ONLINE_BIT,
  1617. SECTION_IS_EARLY_BIT,
  1618. #ifdef CONFIG_ZONE_DEVICE
  1619. SECTION_TAINT_ZONE_DEVICE_BIT,
  1620. #endif
  1621. SECTION_MAP_LAST_BIT,
  1622. };
  1623. #define SECTION_MARKED_PRESENT BIT(SECTION_MARKED_PRESENT_BIT)
  1624. #define SECTION_HAS_MEM_MAP BIT(SECTION_HAS_MEM_MAP_BIT)
  1625. #define SECTION_IS_ONLINE BIT(SECTION_IS_ONLINE_BIT)
  1626. #define SECTION_IS_EARLY BIT(SECTION_IS_EARLY_BIT)
  1627. #ifdef CONFIG_ZONE_DEVICE
  1628. #define SECTION_TAINT_ZONE_DEVICE BIT(SECTION_TAINT_ZONE_DEVICE_BIT)
  1629. #endif
  1630. #define SECTION_MAP_MASK (~(BIT(SECTION_MAP_LAST_BIT) - 1))
  1631. #define SECTION_NID_SHIFT SECTION_MAP_LAST_BIT
  1632. static inline struct page *__section_mem_map_addr(struct mem_section *section)
  1633. {
  1634. unsigned long map = section->section_mem_map;
  1635. map &= SECTION_MAP_MASK;
  1636. return (struct page *)map;
  1637. }
  1638. static inline int present_section(struct mem_section *section)
  1639. {
  1640. return (section && (section->section_mem_map & SECTION_MARKED_PRESENT));
  1641. }
  1642. static inline int present_section_nr(unsigned long nr)
  1643. {
  1644. return present_section(__nr_to_section(nr));
  1645. }
  1646. static inline int valid_section(struct mem_section *section)
  1647. {
  1648. return (section && (section->section_mem_map & SECTION_HAS_MEM_MAP));
  1649. }
  1650. static inline int early_section(struct mem_section *section)
  1651. {
  1652. return (section && (section->section_mem_map & SECTION_IS_EARLY));
  1653. }
  1654. static inline int valid_section_nr(unsigned long nr)
  1655. {
  1656. return valid_section(__nr_to_section(nr));
  1657. }
  1658. static inline int online_section(struct mem_section *section)
  1659. {
  1660. return (section && (section->section_mem_map & SECTION_IS_ONLINE));
  1661. }
  1662. #ifdef CONFIG_ZONE_DEVICE
  1663. static inline int online_device_section(struct mem_section *section)
  1664. {
  1665. unsigned long flags = SECTION_IS_ONLINE | SECTION_TAINT_ZONE_DEVICE;
  1666. return section && ((section->section_mem_map & flags) == flags);
  1667. }
  1668. #else
  1669. static inline int online_device_section(struct mem_section *section)
  1670. {
  1671. return 0;
  1672. }
  1673. #endif
  1674. static inline int online_section_nr(unsigned long nr)
  1675. {
  1676. return online_section(__nr_to_section(nr));
  1677. }
  1678. #ifdef CONFIG_MEMORY_HOTPLUG
  1679. void online_mem_sections(unsigned long start_pfn, unsigned long end_pfn);
  1680. void offline_mem_sections(unsigned long start_pfn, unsigned long end_pfn);
  1681. #endif
  1682. static inline struct mem_section *__pfn_to_section(unsigned long pfn)
  1683. {
  1684. return __nr_to_section(pfn_to_section_nr(pfn));
  1685. }
  1686. extern unsigned long __highest_present_section_nr;
  1687. static inline int subsection_map_index(unsigned long pfn)
  1688. {
  1689. return (pfn & ~(PAGE_SECTION_MASK)) / PAGES_PER_SUBSECTION;
  1690. }
  1691. #ifdef CONFIG_SPARSEMEM_VMEMMAP
  1692. static inline int pfn_section_valid(struct mem_section *ms, unsigned long pfn)
  1693. {
  1694. int idx = subsection_map_index(pfn);
  1695. return test_bit(idx, ms->usage->subsection_map);
  1696. }
  1697. #else
  1698. static inline int pfn_section_valid(struct mem_section *ms, unsigned long pfn)
  1699. {
  1700. return 1;
  1701. }
  1702. #endif
  1703. #ifndef CONFIG_HAVE_ARCH_PFN_VALID
  1704. /**
  1705. * pfn_valid - check if there is a valid memory map entry for a PFN
  1706. * @pfn: the page frame number to check
  1707. *
  1708. * Check if there is a valid memory map entry aka struct page for the @pfn.
  1709. * Note, that availability of the memory map entry does not imply that
  1710. * there is actual usable memory at that @pfn. The struct page may
  1711. * represent a hole or an unusable page frame.
  1712. *
  1713. * Return: 1 for PFNs that have memory map entries and 0 otherwise
  1714. */
  1715. static inline int pfn_valid(unsigned long pfn)
  1716. {
  1717. struct mem_section *ms;
  1718. /*
  1719. * Ensure the upper PAGE_SHIFT bits are clear in the
  1720. * pfn. Else it might lead to false positives when
  1721. * some of the upper bits are set, but the lower bits
  1722. * match a valid pfn.
  1723. */
  1724. if (PHYS_PFN(PFN_PHYS(pfn)) != pfn)
  1725. return 0;
  1726. if (pfn_to_section_nr(pfn) >= NR_MEM_SECTIONS)
  1727. return 0;
  1728. ms = __pfn_to_section(pfn);
  1729. if (!valid_section(ms))
  1730. return 0;
  1731. /*
  1732. * Traditionally early sections always returned pfn_valid() for
  1733. * the entire section-sized span.
  1734. */
  1735. return early_section(ms) || pfn_section_valid(ms, pfn);
  1736. }
  1737. #endif
  1738. static inline int pfn_in_present_section(unsigned long pfn)
  1739. {
  1740. if (pfn_to_section_nr(pfn) >= NR_MEM_SECTIONS)
  1741. return 0;
  1742. return present_section(__pfn_to_section(pfn));
  1743. }
  1744. static inline unsigned long next_present_section_nr(unsigned long section_nr)
  1745. {
  1746. while (++section_nr <= __highest_present_section_nr) {
  1747. if (present_section_nr(section_nr))
  1748. return section_nr;
  1749. }
  1750. return -1;
  1751. }
  1752. /*
  1753. * These are _only_ used during initialisation, therefore they
  1754. * can use __initdata ... They could have names to indicate
  1755. * this restriction.
  1756. */
  1757. #ifdef CONFIG_NUMA
  1758. #define pfn_to_nid(pfn) \
  1759. ({ \
  1760. unsigned long __pfn_to_nid_pfn = (pfn); \
  1761. page_to_nid(pfn_to_page(__pfn_to_nid_pfn)); \
  1762. })
  1763. #else
  1764. #define pfn_to_nid(pfn) (0)
  1765. #endif
  1766. void sparse_init(void);
  1767. #else
  1768. #define sparse_init() do {} while (0)
  1769. #define sparse_index_init(_sec, _nid) do {} while (0)
  1770. #define pfn_in_present_section pfn_valid
  1771. #define subsection_map_init(_pfn, _nr_pages) do {} while (0)
  1772. #endif /* CONFIG_SPARSEMEM */
  1773. #endif /* !__GENERATING_BOUNDS.H */
  1774. #endif /* !__ASSEMBLY__ */
  1775. #endif /* _LINUX_MMZONE_H */