Skip to content

Commit a8161d1

Browse files
tehcastertorvalds
authored andcommitted
mm, page_alloc: restructure direct compaction handling in slowpath
The retry loop in __alloc_pages_slowpath is supposed to keep trying reclaim and compaction (and OOM), until either the allocation succeeds, or returns with failure. Success here is more probable when reclaim precedes compaction, as certain watermarks have to be met for compaction to even try, and more free pages increase the probability of compaction success. On the other hand, starting with light async compaction (if the watermarks allow it), can be more efficient, especially for smaller orders, if there's enough free memory which is just fragmented. Thus, the current code starts with compaction before reclaim, and to make sure that the last reclaim is always followed by a final compaction, there's another direct compaction call at the end of the loop. This makes the code hard to follow and adds some duplicated handling of migration_mode decisions. It's also somewhat inefficient that even if reclaim or compaction decides not to retry, the final compaction is still attempted. Some gfp flags combination also shortcut these retry decisions by "goto noretry;", making it even harder to follow. This patch attempts to restructure the code with only minimal functional changes. The call to the first compaction and THP-specific checks are now placed above the retry loop, and the "noretry" direct compaction is removed. The initial compaction is additionally restricted only to costly orders, as we can expect smaller orders to be held back by watermarks, and only larger orders to suffer primarily from fragmentation. This better matches the checks in reclaim's shrink_zones(). There are two other smaller functional changes. One is that the upgrade from async migration to light sync migration will always occur after the initial compaction. This is how it has been until recent patch "mm, oom: protect !costly allocations some more", which introduced upgrading the mode based on COMPACT_COMPLETE result, but kept the final compaction always upgraded, which made it even more special. It's better to return to the simpler handling for now, as migration modes will be further modified later in the series. The second change is that once both reclaim and compaction declare it's not worth to retry the reclaim/compact loop, there is no final compaction attempt. As argued above, this is intentional. If that final compaction were to succeed, it would be due to a wrong retry decision, or simply a race with somebody else freeing memory for us. The main outcome of this patch should be simpler code. Logically, the initial compaction without reclaim is the exceptional case to the reclaim/compaction scheme, but prior to the patch, it was the last loop iteration that was exceptional. Now the code matches the logic better. The change also enable the following patches. Link: http://lkml.kernel.org/r/[email protected] Signed-off-by: Vlastimil Babka <[email protected]> Acked-by: Michal Hocko <[email protected]> Acked-by: Mel Gorman <[email protected]> Signed-off-by: Andrew Morton <[email protected]> Signed-off-by: Linus Torvalds <[email protected]>
1 parent 2377123 commit a8161d1

File tree

1 file changed

+57
-52
lines changed

1 file changed

+57
-52
lines changed

mm/page_alloc.c

Lines changed: 57 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -3479,7 +3479,7 @@ __alloc_pages_slowpath(gfp_t gfp_mask, unsigned int order,
34793479
struct page *page = NULL;
34803480
unsigned int alloc_flags;
34813481
unsigned long did_some_progress;
3482-
enum migrate_mode migration_mode = MIGRATE_ASYNC;
3482+
enum migrate_mode migration_mode = MIGRATE_SYNC_LIGHT;
34833483
enum compact_result compact_result;
34843484
int compaction_retries = 0;
34853485
int no_progress_loops = 0;
@@ -3521,6 +3521,52 @@ __alloc_pages_slowpath(gfp_t gfp_mask, unsigned int order,
35213521
if (page)
35223522
goto got_pg;
35233523

3524+
/*
3525+
* For costly allocations, try direct compaction first, as it's likely
3526+
* that we have enough base pages and don't need to reclaim. Don't try
3527+
* that for allocations that are allowed to ignore watermarks, as the
3528+
* ALLOC_NO_WATERMARKS attempt didn't yet happen.
3529+
*/
3530+
if (can_direct_reclaim && order > PAGE_ALLOC_COSTLY_ORDER &&
3531+
!gfp_pfmemalloc_allowed(gfp_mask)) {
3532+
page = __alloc_pages_direct_compact(gfp_mask, order,
3533+
alloc_flags, ac,
3534+
MIGRATE_ASYNC,
3535+
&compact_result);
3536+
if (page)
3537+
goto got_pg;
3538+
3539+
/* Checks for THP-specific high-order allocations */
3540+
if (is_thp_gfp_mask(gfp_mask)) {
3541+
/*
3542+
* If compaction is deferred for high-order allocations,
3543+
* it is because sync compaction recently failed. If
3544+
* this is the case and the caller requested a THP
3545+
* allocation, we do not want to heavily disrupt the
3546+
* system, so we fail the allocation instead of entering
3547+
* direct reclaim.
3548+
*/
3549+
if (compact_result == COMPACT_DEFERRED)
3550+
goto nopage;
3551+
3552+
/*
3553+
* Compaction is contended so rather back off than cause
3554+
* excessive stalls.
3555+
*/
3556+
if (compact_result == COMPACT_CONTENDED)
3557+
goto nopage;
3558+
3559+
/*
3560+
* It can become very expensive to allocate transparent
3561+
* hugepages at fault, so use asynchronous memory
3562+
* compaction for THP unless it is khugepaged trying to
3563+
* collapse. All other requests should tolerate at
3564+
* least light sync migration.
3565+
*/
3566+
if (!(current->flags & PF_KTHREAD))
3567+
migration_mode = MIGRATE_ASYNC;
3568+
}
3569+
}
35243570

35253571
retry:
35263572
/* Ensure kswapd doesn't accidentally go to sleep as long as we loop */
@@ -3575,55 +3621,33 @@ __alloc_pages_slowpath(gfp_t gfp_mask, unsigned int order,
35753621
if (test_thread_flag(TIF_MEMDIE) && !(gfp_mask & __GFP_NOFAIL))
35763622
goto nopage;
35773623

3578-
/*
3579-
* Try direct compaction. The first pass is asynchronous. Subsequent
3580-
* attempts after direct reclaim are synchronous
3581-
*/
3624+
3625+
/* Try direct reclaim and then allocating */
3626+
page = __alloc_pages_direct_reclaim(gfp_mask, order, alloc_flags, ac,
3627+
&did_some_progress);
3628+
if (page)
3629+
goto got_pg;
3630+
3631+
/* Try direct compaction and then allocating */
35823632
page = __alloc_pages_direct_compact(gfp_mask, order, alloc_flags, ac,
35833633
migration_mode,
35843634
&compact_result);
35853635
if (page)
35863636
goto got_pg;
35873637

3588-
/* Checks for THP-specific high-order allocations */
3589-
if (is_thp_gfp_mask(gfp_mask)) {
3590-
/*
3591-
* If compaction is deferred for high-order allocations, it is
3592-
* because sync compaction recently failed. If this is the case
3593-
* and the caller requested a THP allocation, we do not want
3594-
* to heavily disrupt the system, so we fail the allocation
3595-
* instead of entering direct reclaim.
3596-
*/
3597-
if (compact_result == COMPACT_DEFERRED)
3598-
goto nopage;
3599-
3600-
/*
3601-
* Compaction is contended so rather back off than cause
3602-
* excessive stalls.
3603-
*/
3604-
if(compact_result == COMPACT_CONTENDED)
3605-
goto nopage;
3606-
}
3607-
36083638
if (order && compaction_made_progress(compact_result))
36093639
compaction_retries++;
36103640

3611-
/* Try direct reclaim and then allocating */
3612-
page = __alloc_pages_direct_reclaim(gfp_mask, order, alloc_flags, ac,
3613-
&did_some_progress);
3614-
if (page)
3615-
goto got_pg;
3616-
36173641
/* Do not loop if specifically requested */
36183642
if (gfp_mask & __GFP_NORETRY)
3619-
goto noretry;
3643+
goto nopage;
36203644

36213645
/*
36223646
* Do not retry costly high order allocations unless they are
36233647
* __GFP_REPEAT
36243648
*/
36253649
if (order > PAGE_ALLOC_COSTLY_ORDER && !(gfp_mask & __GFP_REPEAT))
3626-
goto noretry;
3650+
goto nopage;
36273651

36283652
/*
36293653
* Costly allocations might have made a progress but this doesn't mean
@@ -3662,25 +3686,6 @@ __alloc_pages_slowpath(gfp_t gfp_mask, unsigned int order,
36623686
goto retry;
36633687
}
36643688

3665-
noretry:
3666-
/*
3667-
* High-order allocations do not necessarily loop after direct reclaim
3668-
* and reclaim/compaction depends on compaction being called after
3669-
* reclaim so call directly if necessary.
3670-
* It can become very expensive to allocate transparent hugepages at
3671-
* fault, so use asynchronous memory compaction for THP unless it is
3672-
* khugepaged trying to collapse. All other requests should tolerate
3673-
* at least light sync migration.
3674-
*/
3675-
if (is_thp_gfp_mask(gfp_mask) && !(current->flags & PF_KTHREAD))
3676-
migration_mode = MIGRATE_ASYNC;
3677-
else
3678-
migration_mode = MIGRATE_SYNC_LIGHT;
3679-
page = __alloc_pages_direct_compact(gfp_mask, order, alloc_flags,
3680-
ac, migration_mode,
3681-
&compact_result);
3682-
if (page)
3683-
goto got_pg;
36843689
nopage:
36853690
warn_alloc_failed(gfp_mask, order, NULL);
36863691
got_pg:

0 commit comments

Comments
 (0)