xref: /linux-6.15/include/linux/compaction.h (revision facdaa91)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _LINUX_COMPACTION_H
3 #define _LINUX_COMPACTION_H
4 
5 /*
6  * Determines how hard direct compaction should try to succeed.
7  * Lower value means higher priority, analogically to reclaim priority.
8  */
9 enum compact_priority {
10 	COMPACT_PRIO_SYNC_FULL,
11 	MIN_COMPACT_PRIORITY = COMPACT_PRIO_SYNC_FULL,
12 	COMPACT_PRIO_SYNC_LIGHT,
13 	MIN_COMPACT_COSTLY_PRIORITY = COMPACT_PRIO_SYNC_LIGHT,
14 	DEF_COMPACT_PRIORITY = COMPACT_PRIO_SYNC_LIGHT,
15 	COMPACT_PRIO_ASYNC,
16 	INIT_COMPACT_PRIORITY = COMPACT_PRIO_ASYNC
17 };
18 
19 /* Return values for compact_zone() and try_to_compact_pages() */
20 /* When adding new states, please adjust include/trace/events/compaction.h */
21 enum compact_result {
22 	/* For more detailed tracepoint output - internal to compaction */
23 	COMPACT_NOT_SUITABLE_ZONE,
24 	/*
25 	 * compaction didn't start as it was not possible or direct reclaim
26 	 * was more suitable
27 	 */
28 	COMPACT_SKIPPED,
29 	/* compaction didn't start as it was deferred due to past failures */
30 	COMPACT_DEFERRED,
31 
32 	/* compaction not active last round */
33 	COMPACT_INACTIVE = COMPACT_DEFERRED,
34 
35 	/* For more detailed tracepoint output - internal to compaction */
36 	COMPACT_NO_SUITABLE_PAGE,
37 	/* compaction should continue to another pageblock */
38 	COMPACT_CONTINUE,
39 
40 	/*
41 	 * The full zone was compacted scanned but wasn't successfull to compact
42 	 * suitable pages.
43 	 */
44 	COMPACT_COMPLETE,
45 	/*
46 	 * direct compaction has scanned part of the zone but wasn't successfull
47 	 * to compact suitable pages.
48 	 */
49 	COMPACT_PARTIAL_SKIPPED,
50 
51 	/* compaction terminated prematurely due to lock contentions */
52 	COMPACT_CONTENDED,
53 
54 	/*
55 	 * direct compaction terminated after concluding that the allocation
56 	 * should now succeed
57 	 */
58 	COMPACT_SUCCESS,
59 };
60 
61 struct alloc_context; /* in mm/internal.h */
62 
63 /*
64  * Number of free order-0 pages that should be available above given watermark
65  * to make sure compaction has reasonable chance of not running out of free
66  * pages that it needs to isolate as migration target during its work.
67  */
68 static inline unsigned long compact_gap(unsigned int order)
69 {
70 	/*
71 	 * Although all the isolations for migration are temporary, compaction
72 	 * free scanner may have up to 1 << order pages on its list and then
73 	 * try to split an (order - 1) free page. At that point, a gap of
74 	 * 1 << order might not be enough, so it's safer to require twice that
75 	 * amount. Note that the number of pages on the list is also
76 	 * effectively limited by COMPACT_CLUSTER_MAX, as that's the maximum
77 	 * that the migrate scanner can have isolated on migrate list, and free
78 	 * scanner is only invoked when the number of isolated free pages is
79 	 * lower than that. But it's not worth to complicate the formula here
80 	 * as a bigger gap for higher orders than strictly necessary can also
81 	 * improve chances of compaction success.
82 	 */
83 	return 2UL << order;
84 }
85 
86 #ifdef CONFIG_COMPACTION
87 extern int sysctl_compact_memory;
88 extern int sysctl_compaction_proactiveness;
89 extern int sysctl_compaction_handler(struct ctl_table *table, int write,
90 			void *buffer, size_t *length, loff_t *ppos);
91 extern int sysctl_extfrag_threshold;
92 extern int sysctl_compact_unevictable_allowed;
93 
94 extern int extfrag_for_order(struct zone *zone, unsigned int order);
95 extern int fragmentation_index(struct zone *zone, unsigned int order);
96 extern enum compact_result try_to_compact_pages(gfp_t gfp_mask,
97 		unsigned int order, unsigned int alloc_flags,
98 		const struct alloc_context *ac, enum compact_priority prio,
99 		struct page **page);
100 extern void reset_isolation_suitable(pg_data_t *pgdat);
101 extern enum compact_result compaction_suitable(struct zone *zone, int order,
102 		unsigned int alloc_flags, int highest_zoneidx);
103 
104 extern void defer_compaction(struct zone *zone, int order);
105 extern bool compaction_deferred(struct zone *zone, int order);
106 extern void compaction_defer_reset(struct zone *zone, int order,
107 				bool alloc_success);
108 extern bool compaction_restarting(struct zone *zone, int order);
109 
110 /* Compaction has made some progress and retrying makes sense */
111 static inline bool compaction_made_progress(enum compact_result result)
112 {
113 	/*
114 	 * Even though this might sound confusing this in fact tells us
115 	 * that the compaction successfully isolated and migrated some
116 	 * pageblocks.
117 	 */
118 	if (result == COMPACT_SUCCESS)
119 		return true;
120 
121 	return false;
122 }
123 
124 /* Compaction has failed and it doesn't make much sense to keep retrying. */
125 static inline bool compaction_failed(enum compact_result result)
126 {
127 	/* All zones were scanned completely and still not result. */
128 	if (result == COMPACT_COMPLETE)
129 		return true;
130 
131 	return false;
132 }
133 
134 /* Compaction needs reclaim to be performed first, so it can continue. */
135 static inline bool compaction_needs_reclaim(enum compact_result result)
136 {
137 	/*
138 	 * Compaction backed off due to watermark checks for order-0
139 	 * so the regular reclaim has to try harder and reclaim something.
140 	 */
141 	if (result == COMPACT_SKIPPED)
142 		return true;
143 
144 	return false;
145 }
146 
147 /*
148  * Compaction has backed off for some reason after doing some work or none
149  * at all. It might be throttling or lock contention. Retrying might be still
150  * worthwhile, but with a higher priority if allowed.
151  */
152 static inline bool compaction_withdrawn(enum compact_result result)
153 {
154 	/*
155 	 * If compaction is deferred for high-order allocations, it is
156 	 * because sync compaction recently failed. If this is the case
157 	 * and the caller requested a THP allocation, we do not want
158 	 * to heavily disrupt the system, so we fail the allocation
159 	 * instead of entering direct reclaim.
160 	 */
161 	if (result == COMPACT_DEFERRED)
162 		return true;
163 
164 	/*
165 	 * If compaction in async mode encounters contention or blocks higher
166 	 * priority task we back off early rather than cause stalls.
167 	 */
168 	if (result == COMPACT_CONTENDED)
169 		return true;
170 
171 	/*
172 	 * Page scanners have met but we haven't scanned full zones so this
173 	 * is a back off in fact.
174 	 */
175 	if (result == COMPACT_PARTIAL_SKIPPED)
176 		return true;
177 
178 	return false;
179 }
180 
181 
182 bool compaction_zonelist_suitable(struct alloc_context *ac, int order,
183 					int alloc_flags);
184 
185 extern int kcompactd_run(int nid);
186 extern void kcompactd_stop(int nid);
187 extern void wakeup_kcompactd(pg_data_t *pgdat, int order, int highest_zoneidx);
188 
189 #else
190 static inline void reset_isolation_suitable(pg_data_t *pgdat)
191 {
192 }
193 
194 static inline enum compact_result compaction_suitable(struct zone *zone, int order,
195 					int alloc_flags, int highest_zoneidx)
196 {
197 	return COMPACT_SKIPPED;
198 }
199 
200 static inline void defer_compaction(struct zone *zone, int order)
201 {
202 }
203 
204 static inline bool compaction_deferred(struct zone *zone, int order)
205 {
206 	return true;
207 }
208 
209 static inline bool compaction_made_progress(enum compact_result result)
210 {
211 	return false;
212 }
213 
214 static inline bool compaction_failed(enum compact_result result)
215 {
216 	return false;
217 }
218 
219 static inline bool compaction_needs_reclaim(enum compact_result result)
220 {
221 	return false;
222 }
223 
224 static inline bool compaction_withdrawn(enum compact_result result)
225 {
226 	return true;
227 }
228 
229 static inline int kcompactd_run(int nid)
230 {
231 	return 0;
232 }
233 static inline void kcompactd_stop(int nid)
234 {
235 }
236 
237 static inline void wakeup_kcompactd(pg_data_t *pgdat,
238 				int order, int highest_zoneidx)
239 {
240 }
241 
242 #endif /* CONFIG_COMPACTION */
243 
244 struct node;
245 #if defined(CONFIG_COMPACTION) && defined(CONFIG_SYSFS) && defined(CONFIG_NUMA)
246 extern int compaction_register_node(struct node *node);
247 extern void compaction_unregister_node(struct node *node);
248 
249 #else
250 
251 static inline int compaction_register_node(struct node *node)
252 {
253 	return 0;
254 }
255 
256 static inline void compaction_unregister_node(struct node *node)
257 {
258 }
259 #endif /* CONFIG_COMPACTION && CONFIG_SYSFS && CONFIG_NUMA */
260 
261 #endif /* _LINUX_COMPACTION_H */
262