• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 				(zonelist_zone(z) && !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 	zswap_lruvec_state_init(lruvec);
83 
84 	for_each_lru(lru)
85 		INIT_LIST_HEAD(&lruvec->lists[lru]);
86 	/*
87 	 * The "Unevictable LRU" is imaginary: though its size is maintained,
88 	 * it is never scanned, and unevictable pages are not threaded on it
89 	 * (so that their lru fields can be reused to hold mlock_count).
90 	 * Poison its list head, so that any operations on it would crash.
91 	 */
92 	list_del(&lruvec->lists[LRU_UNEVICTABLE]);
93 
94 	lru_gen_init_lruvec(lruvec);
95 }
96 
97 #if defined(CONFIG_NUMA_BALANCING) && !defined(LAST_CPUPID_NOT_IN_PAGE_FLAGS)
folio_xchg_last_cpupid(struct folio * folio,int cpupid)98 int folio_xchg_last_cpupid(struct folio *folio, int cpupid)
99 {
100 	unsigned long old_flags, flags;
101 	int last_cpupid;
102 
103 	old_flags = READ_ONCE(folio->flags);
104 	do {
105 		flags = old_flags;
106 		last_cpupid = (flags >> LAST_CPUPID_PGSHIFT) & LAST_CPUPID_MASK;
107 
108 		flags &= ~(LAST_CPUPID_MASK << LAST_CPUPID_PGSHIFT);
109 		flags |= (cpupid & LAST_CPUPID_MASK) << LAST_CPUPID_PGSHIFT;
110 	} while (unlikely(!try_cmpxchg(&folio->flags, &old_flags, flags)));
111 
112 	return last_cpupid;
113 }
114 #endif
115 
gfp_zone(gfp_t flags)116 enum zone_type gfp_zone(gfp_t flags)
117 {
118 	trace_android_rvh_set_gfp_zone_flags(&flags);
119 
120 	return __gfp_zone(flags);
121 }
122