xref: /linux-6.15/include/linux/compaction.h (revision 25cd480e)
1 #ifndef _LINUX_COMPACTION_H
2 #define _LINUX_COMPACTION_H
3 
4 /* Return values for compact_zone() and try_to_compact_pages() */
5 /* compaction didn't start as it was deferred due to past failures */
6 #define COMPACT_DEFERRED	0
7 /* compaction didn't start as it was not possible or direct reclaim was more suitable */
8 #define COMPACT_SKIPPED		1
9 /* compaction should continue to another pageblock */
10 #define COMPACT_CONTINUE	2
11 /* direct compaction partially compacted a zone and there are suitable pages */
12 #define COMPACT_PARTIAL		3
13 /* The full zone was compacted */
14 #define COMPACT_COMPLETE	4
15 
16 /* Used to signal whether compaction detected need_sched() or lock contention */
17 /* No contention detected */
18 #define COMPACT_CONTENDED_NONE	0
19 /* Either need_sched() was true or fatal signal pending */
20 #define COMPACT_CONTENDED_SCHED	1
21 /* Zone lock or lru_lock was contended in async compaction */
22 #define COMPACT_CONTENDED_LOCK	2
23 
24 #ifdef CONFIG_COMPACTION
25 extern int sysctl_compact_memory;
26 extern int sysctl_compaction_handler(struct ctl_table *table, int write,
27 			void __user *buffer, size_t *length, loff_t *ppos);
28 extern int sysctl_extfrag_threshold;
29 extern int sysctl_extfrag_handler(struct ctl_table *table, int write,
30 			void __user *buffer, size_t *length, loff_t *ppos);
31 
32 extern int fragmentation_index(struct zone *zone, unsigned int order);
33 extern unsigned long try_to_compact_pages(struct zonelist *zonelist,
34 			int order, gfp_t gfp_mask, nodemask_t *mask,
35 			enum migrate_mode mode, int *contended,
36 			int alloc_flags, int classzone_idx);
37 extern void compact_pgdat(pg_data_t *pgdat, int order);
38 extern void reset_isolation_suitable(pg_data_t *pgdat);
39 extern unsigned long compaction_suitable(struct zone *zone, int order,
40 					int alloc_flags, int classzone_idx);
41 
42 /* Do not skip compaction more than 64 times */
43 #define COMPACT_MAX_DEFER_SHIFT 6
44 
45 /*
46  * Compaction is deferred when compaction fails to result in a page
47  * allocation success. 1 << compact_defer_limit compactions are skipped up
48  * to a limit of 1 << COMPACT_MAX_DEFER_SHIFT
49  */
50 static inline void defer_compaction(struct zone *zone, int order)
51 {
52 	zone->compact_considered = 0;
53 	zone->compact_defer_shift++;
54 
55 	if (order < zone->compact_order_failed)
56 		zone->compact_order_failed = order;
57 
58 	if (zone->compact_defer_shift > COMPACT_MAX_DEFER_SHIFT)
59 		zone->compact_defer_shift = COMPACT_MAX_DEFER_SHIFT;
60 }
61 
62 /* Returns true if compaction should be skipped this time */
63 static inline bool compaction_deferred(struct zone *zone, int order)
64 {
65 	unsigned long defer_limit = 1UL << zone->compact_defer_shift;
66 
67 	if (order < zone->compact_order_failed)
68 		return false;
69 
70 	/* Avoid possible overflow */
71 	if (++zone->compact_considered > defer_limit)
72 		zone->compact_considered = defer_limit;
73 
74 	return zone->compact_considered < defer_limit;
75 }
76 
77 /*
78  * Update defer tracking counters after successful compaction of given order,
79  * which means an allocation either succeeded (alloc_success == true) or is
80  * expected to succeed.
81  */
82 static inline void compaction_defer_reset(struct zone *zone, int order,
83 		bool alloc_success)
84 {
85 	if (alloc_success) {
86 		zone->compact_considered = 0;
87 		zone->compact_defer_shift = 0;
88 	}
89 	if (order >= zone->compact_order_failed)
90 		zone->compact_order_failed = order + 1;
91 }
92 
93 /* Returns true if restarting compaction after many failures */
94 static inline bool compaction_restarting(struct zone *zone, int order)
95 {
96 	if (order < zone->compact_order_failed)
97 		return false;
98 
99 	return zone->compact_defer_shift == COMPACT_MAX_DEFER_SHIFT &&
100 		zone->compact_considered >= 1UL << zone->compact_defer_shift;
101 }
102 
103 #else
104 static inline unsigned long try_to_compact_pages(struct zonelist *zonelist,
105 			int order, gfp_t gfp_mask, nodemask_t *nodemask,
106 			enum migrate_mode mode, int *contended,
107 			int alloc_flags, int classzone_idx)
108 {
109 	return COMPACT_CONTINUE;
110 }
111 
112 static inline void compact_pgdat(pg_data_t *pgdat, int order)
113 {
114 }
115 
116 static inline void reset_isolation_suitable(pg_data_t *pgdat)
117 {
118 }
119 
120 static inline unsigned long compaction_suitable(struct zone *zone, int order,
121 					int alloc_flags, int classzone_idx)
122 {
123 	return COMPACT_SKIPPED;
124 }
125 
126 static inline void defer_compaction(struct zone *zone, int order)
127 {
128 }
129 
130 static inline bool compaction_deferred(struct zone *zone, int order)
131 {
132 	return true;
133 }
134 
135 #endif /* CONFIG_COMPACTION */
136 
137 #if defined(CONFIG_COMPACTION) && defined(CONFIG_SYSFS) && defined(CONFIG_NUMA)
138 extern int compaction_register_node(struct node *node);
139 extern void compaction_unregister_node(struct node *node);
140 
141 #else
142 
143 static inline int compaction_register_node(struct node *node)
144 {
145 	return 0;
146 }
147 
148 static inline void compaction_unregister_node(struct node *node)
149 {
150 }
151 #endif /* CONFIG_COMPACTION && CONFIG_SYSFS && CONFIG_NUMA */
152 
153 #endif /* _LINUX_COMPACTION_H */
154