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 EXPORT_SYMBOL_GPL(__next_zones_zonelist);
76
lruvec_init(struct lruvec * lruvec)77 void lruvec_init(struct lruvec *lruvec)
78 {
79 enum lru_list lru;
80
81 memset(lruvec, 0, sizeof(struct lruvec));
82
83 for_each_lru(lru)
84 INIT_LIST_HEAD(&lruvec->lists[lru]);
85 }
86
87 #if defined(CONFIG_NUMA_BALANCING) && !defined(LAST_CPUPID_NOT_IN_PAGE_FLAGS)
page_cpupid_xchg_last(struct page * page,int cpupid)88 int page_cpupid_xchg_last(struct page *page, int cpupid)
89 {
90 unsigned long old_flags, flags;
91 int last_cpupid;
92
93 do {
94 old_flags = flags = page->flags;
95 last_cpupid = page_cpupid_last(page);
96
97 flags &= ~(LAST_CPUPID_MASK << LAST_CPUPID_PGSHIFT);
98 flags |= (cpupid & LAST_CPUPID_MASK) << LAST_CPUPID_PGSHIFT;
99 } while (unlikely(cmpxchg(&page->flags, old_flags, flags) != old_flags));
100
101 return last_cpupid;
102 }
103 #endif
104
gfp_zone(gfp_t flags)105 enum zone_type gfp_zone(gfp_t flags)
106 {
107 enum zone_type z;
108 gfp_t local_flags = flags;
109 int bit;
110
111 trace_android_rvh_set_gfp_zone_flags(&local_flags);
112
113 bit = (__force int) ((local_flags) & GFP_ZONEMASK);
114
115 z = (GFP_ZONE_TABLE >> (bit * GFP_ZONES_SHIFT)) &
116 ((1 << GFP_ZONES_SHIFT) - 1);
117 VM_BUG_ON((GFP_ZONE_BAD >> bit) & 1);
118 return z;
119 }
120 EXPORT_SYMBOL_GPL(gfp_zone);
121