Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0 */
0002 #ifndef _LINUX_COMPACTION_H
0003 #define _LINUX_COMPACTION_H
0004 
0005 /*
0006  * Determines how hard direct compaction should try to succeed.
0007  * Lower value means higher priority, analogically to reclaim priority.
0008  */
0009 enum compact_priority {
0010     COMPACT_PRIO_SYNC_FULL,
0011     MIN_COMPACT_PRIORITY = COMPACT_PRIO_SYNC_FULL,
0012     COMPACT_PRIO_SYNC_LIGHT,
0013     MIN_COMPACT_COSTLY_PRIORITY = COMPACT_PRIO_SYNC_LIGHT,
0014     DEF_COMPACT_PRIORITY = COMPACT_PRIO_SYNC_LIGHT,
0015     COMPACT_PRIO_ASYNC,
0016     INIT_COMPACT_PRIORITY = COMPACT_PRIO_ASYNC
0017 };
0018 
0019 /* Return values for compact_zone() and try_to_compact_pages() */
0020 /* When adding new states, please adjust include/trace/events/compaction.h */
0021 enum compact_result {
0022     /* For more detailed tracepoint output - internal to compaction */
0023     COMPACT_NOT_SUITABLE_ZONE,
0024     /*
0025      * compaction didn't start as it was not possible or direct reclaim
0026      * was more suitable
0027      */
0028     COMPACT_SKIPPED,
0029     /* compaction didn't start as it was deferred due to past failures */
0030     COMPACT_DEFERRED,
0031 
0032     /* For more detailed tracepoint output - internal to compaction */
0033     COMPACT_NO_SUITABLE_PAGE,
0034     /* compaction should continue to another pageblock */
0035     COMPACT_CONTINUE,
0036 
0037     /*
0038      * The full zone was compacted scanned but wasn't successful to compact
0039      * suitable pages.
0040      */
0041     COMPACT_COMPLETE,
0042     /*
0043      * direct compaction has scanned part of the zone but wasn't successful
0044      * to compact suitable pages.
0045      */
0046     COMPACT_PARTIAL_SKIPPED,
0047 
0048     /* compaction terminated prematurely due to lock contentions */
0049     COMPACT_CONTENDED,
0050 
0051     /*
0052      * direct compaction terminated after concluding that the allocation
0053      * should now succeed
0054      */
0055     COMPACT_SUCCESS,
0056 };
0057 
0058 struct alloc_context; /* in mm/internal.h */
0059 
0060 /*
0061  * Number of free order-0 pages that should be available above given watermark
0062  * to make sure compaction has reasonable chance of not running out of free
0063  * pages that it needs to isolate as migration target during its work.
0064  */
0065 static inline unsigned long compact_gap(unsigned int order)
0066 {
0067     /*
0068      * Although all the isolations for migration are temporary, compaction
0069      * free scanner may have up to 1 << order pages on its list and then
0070      * try to split an (order - 1) free page. At that point, a gap of
0071      * 1 << order might not be enough, so it's safer to require twice that
0072      * amount. Note that the number of pages on the list is also
0073      * effectively limited by COMPACT_CLUSTER_MAX, as that's the maximum
0074      * that the migrate scanner can have isolated on migrate list, and free
0075      * scanner is only invoked when the number of isolated free pages is
0076      * lower than that. But it's not worth to complicate the formula here
0077      * as a bigger gap for higher orders than strictly necessary can also
0078      * improve chances of compaction success.
0079      */
0080     return 2UL << order;
0081 }
0082 
0083 #ifdef CONFIG_COMPACTION
0084 extern unsigned int sysctl_compaction_proactiveness;
0085 extern int sysctl_compaction_handler(struct ctl_table *table, int write,
0086             void *buffer, size_t *length, loff_t *ppos);
0087 extern int compaction_proactiveness_sysctl_handler(struct ctl_table *table,
0088         int write, void *buffer, size_t *length, loff_t *ppos);
0089 extern int sysctl_extfrag_threshold;
0090 extern int sysctl_compact_unevictable_allowed;
0091 
0092 extern unsigned int extfrag_for_order(struct zone *zone, unsigned int order);
0093 extern int fragmentation_index(struct zone *zone, unsigned int order);
0094 extern enum compact_result try_to_compact_pages(gfp_t gfp_mask,
0095         unsigned int order, unsigned int alloc_flags,
0096         const struct alloc_context *ac, enum compact_priority prio,
0097         struct page **page);
0098 extern void reset_isolation_suitable(pg_data_t *pgdat);
0099 extern enum compact_result compaction_suitable(struct zone *zone, int order,
0100         unsigned int alloc_flags, int highest_zoneidx);
0101 
0102 extern void compaction_defer_reset(struct zone *zone, int order,
0103                 bool alloc_success);
0104 
0105 /* Compaction has made some progress and retrying makes sense */
0106 static inline bool compaction_made_progress(enum compact_result result)
0107 {
0108     /*
0109      * Even though this might sound confusing this in fact tells us
0110      * that the compaction successfully isolated and migrated some
0111      * pageblocks.
0112      */
0113     if (result == COMPACT_SUCCESS)
0114         return true;
0115 
0116     return false;
0117 }
0118 
0119 /* Compaction has failed and it doesn't make much sense to keep retrying. */
0120 static inline bool compaction_failed(enum compact_result result)
0121 {
0122     /* All zones were scanned completely and still not result. */
0123     if (result == COMPACT_COMPLETE)
0124         return true;
0125 
0126     return false;
0127 }
0128 
0129 /* Compaction needs reclaim to be performed first, so it can continue. */
0130 static inline bool compaction_needs_reclaim(enum compact_result result)
0131 {
0132     /*
0133      * Compaction backed off due to watermark checks for order-0
0134      * so the regular reclaim has to try harder and reclaim something.
0135      */
0136     if (result == COMPACT_SKIPPED)
0137         return true;
0138 
0139     return false;
0140 }
0141 
0142 /*
0143  * Compaction has backed off for some reason after doing some work or none
0144  * at all. It might be throttling or lock contention. Retrying might be still
0145  * worthwhile, but with a higher priority if allowed.
0146  */
0147 static inline bool compaction_withdrawn(enum compact_result result)
0148 {
0149     /*
0150      * If compaction is deferred for high-order allocations, it is
0151      * because sync compaction recently failed. If this is the case
0152      * and the caller requested a THP allocation, we do not want
0153      * to heavily disrupt the system, so we fail the allocation
0154      * instead of entering direct reclaim.
0155      */
0156     if (result == COMPACT_DEFERRED)
0157         return true;
0158 
0159     /*
0160      * If compaction in async mode encounters contention or blocks higher
0161      * priority task we back off early rather than cause stalls.
0162      */
0163     if (result == COMPACT_CONTENDED)
0164         return true;
0165 
0166     /*
0167      * Page scanners have met but we haven't scanned full zones so this
0168      * is a back off in fact.
0169      */
0170     if (result == COMPACT_PARTIAL_SKIPPED)
0171         return true;
0172 
0173     return false;
0174 }
0175 
0176 
0177 bool compaction_zonelist_suitable(struct alloc_context *ac, int order,
0178                     int alloc_flags);
0179 
0180 extern void kcompactd_run(int nid);
0181 extern void kcompactd_stop(int nid);
0182 extern void wakeup_kcompactd(pg_data_t *pgdat, int order, int highest_zoneidx);
0183 
0184 #else
0185 static inline void reset_isolation_suitable(pg_data_t *pgdat)
0186 {
0187 }
0188 
0189 static inline enum compact_result compaction_suitable(struct zone *zone, int order,
0190                     int alloc_flags, int highest_zoneidx)
0191 {
0192     return COMPACT_SKIPPED;
0193 }
0194 
0195 static inline bool compaction_made_progress(enum compact_result result)
0196 {
0197     return false;
0198 }
0199 
0200 static inline bool compaction_failed(enum compact_result result)
0201 {
0202     return false;
0203 }
0204 
0205 static inline bool compaction_needs_reclaim(enum compact_result result)
0206 {
0207     return false;
0208 }
0209 
0210 static inline bool compaction_withdrawn(enum compact_result result)
0211 {
0212     return true;
0213 }
0214 
0215 static inline void kcompactd_run(int nid)
0216 {
0217 }
0218 static inline void kcompactd_stop(int nid)
0219 {
0220 }
0221 
0222 static inline void wakeup_kcompactd(pg_data_t *pgdat,
0223                 int order, int highest_zoneidx)
0224 {
0225 }
0226 
0227 #endif /* CONFIG_COMPACTION */
0228 
0229 struct node;
0230 #if defined(CONFIG_COMPACTION) && defined(CONFIG_SYSFS) && defined(CONFIG_NUMA)
0231 extern int compaction_register_node(struct node *node);
0232 extern void compaction_unregister_node(struct node *node);
0233 
0234 #else
0235 
0236 static inline int compaction_register_node(struct node *node)
0237 {
0238     return 0;
0239 }
0240 
0241 static inline void compaction_unregister_node(struct node *node)
0242 {
0243 }
0244 #endif /* CONFIG_COMPACTION && CONFIG_SYSFS && CONFIG_NUMA */
0245 
0246 #endif /* _LINUX_COMPACTION_H */