1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * linux/mm/mmzone.c
4 *
5 * management codes for pgdats, zones and page flags
6 */
7
8
9 #include <linux/stddef.h>
10 #include <linux/mm.h>
11 #include <linux/mmzone.h>
12 #include <trace/hooks/mm.h>
13
first_online_pgdat(void)14 struct pglist_data *first_online_pgdat(void)
15 {
16 return NODE_DATA(first_online_node);
17 }
18
next_online_pgdat(struct pglist_data * pgdat)19 struct pglist_data *next_online_pgdat(struct pglist_data *pgdat)
20 {
21 int nid = next_online_node(pgdat->node_id);
22
23 if (nid == MAX_NUMNODES)
24 return NULL;
25 return NODE_DATA(nid);
26 }
27
28 /*
29 * next_zone - helper magic for for_each_zone()
30 */
next_zone(struct zone * zone)31 struct zone *next_zone(struct zone *zone)
32 {
33 pg_data_t *pgdat = zone->zone_pgdat;
34
35 if (zone < pgdat->node_zones + MAX_NR_ZONES - 1)
36 zone++;
37 else {
38 pgdat = next_online_pgdat(pgdat);
39 if (pgdat)
40 zone = pgdat->node_zones;
41 else
42 zone = NULL;
43 }
44 return zone;
45 }
46
zref_in_nodemask(struct zoneref * zref,nodemask_t * nodes)47 static inline int zref_in_nodemask(struct zoneref *zref, nodemask_t *nodes)
48 {
49 #ifdef CONFIG_NUMA
50 return node_isset(zonelist_node_idx(zref), *nodes);
51 #else
52 return 1;
53 #endif /* CONFIG_NUMA */
54 }
55
56 /* Returns the next zone at or below highest_zoneidx in a zonelist */
__next_zones_zonelist(struct zoneref * z,enum zone_type highest_zoneidx,nodemask_t * nodes)57 struct zoneref *__next_zones_zonelist(struct zoneref *z,
58 enum zone_type highest_zoneidx,
59 nodemask_t *nodes)
60 {
61 /*
62 * Find the next suitable zone to use for the allocation.
63 * Only filter based on nodemask if it's set
64 */
65 if (unlikely(nodes == NULL))
66 while (zonelist_zone_idx(z) > highest_zoneidx)
67 z++;
68 else
69 while (zonelist_zone_idx(z) > highest_zoneidx ||
70 (z->zone && !zref_in_nodemask(z, nodes)))
71 z++;
72
73 return z;
74 }
75
lruvec_init(struct lruvec * lruvec)76 void lruvec_init(struct lruvec *lruvec)
77 {
78 enum lru_list lru;
79
80 memset(lruvec, 0, sizeof(struct lruvec));
81 spin_lock_init(&lruvec->lru_lock);
82
83 for_each_lru(lru)
84 INIT_LIST_HEAD(&lruvec->lists[lru]);
85
86 lru_gen_init_lruvec(lruvec);
87 }
88
89 #if defined(CONFIG_NUMA_BALANCING) && !defined(LAST_CPUPID_NOT_IN_PAGE_FLAGS)
page_cpupid_xchg_last(struct page * page,int cpupid)90 int page_cpupid_xchg_last(struct page *page, int cpupid)
91 {
92 unsigned long old_flags, flags;
93 int last_cpupid;
94
95 do {
96 old_flags = flags = page->flags;
97 last_cpupid = page_cpupid_last(page);
98
99 flags &= ~(LAST_CPUPID_MASK << LAST_CPUPID_PGSHIFT);
100 flags |= (cpupid & LAST_CPUPID_MASK) << LAST_CPUPID_PGSHIFT;
101 } while (unlikely(cmpxchg(&page->flags, old_flags, flags) != old_flags));
102
103 return last_cpupid;
104 }
105 #endif
106
gfp_zone(gfp_t flags)107 enum zone_type gfp_zone(gfp_t flags)
108 {
109 enum zone_type z;
110 gfp_t local_flags = flags;
111 int bit;
112
113 trace_android_rvh_set_gfp_zone_flags(&local_flags);
114
115 bit = (__force int) ((local_flags) & GFP_ZONEMASK);
116
117 z = (GFP_ZONE_TABLE >> (bit * GFP_ZONES_SHIFT)) &
118 ((1 << GFP_ZONES_SHIFT) - 1);
119 VM_BUG_ON((GFP_ZONE_BAD >> bit) & 1);
120 return z;
121 }
122