Skip to content

Commit dffcac2

Browse files
shakeelbtorvalds
authored andcommitted
mm/vmscan.c: prevent useless kswapd loops
In production we have noticed hard lockups on large machines running large jobs due to kswaps hoarding lru lock within isolate_lru_pages when sc->reclaim_idx is 0 which is a small zone. The lru was couple hundred GiBs and the condition (page_zonenum(page) > sc->reclaim_idx) in isolate_lru_pages() was basically skipping GiBs of pages while holding the LRU spinlock with interrupt disabled. On further inspection, it seems like there are two issues: (1) If kswapd on the return from balance_pgdat() could not sleep (i.e. node is still unbalanced), the classzone_idx is unintentionally set to 0 and the whole reclaim cycle of kswapd will try to reclaim only the lowest and smallest zone while traversing the whole memory. (2) Fundamentally isolate_lru_pages() is really bad when the allocation has woken kswapd for a smaller zone on a very large machine running very large jobs. It can hoard the LRU spinlock while skipping over 100s of GiBs of pages. This patch only fixes (1). (2) needs a more fundamental solution. To fix (1), in the kswapd context, if pgdat->kswapd_classzone_idx is invalid use the classzone_idx of the previous kswapd loop otherwise use the one the waker has requested. Link: http://lkml.kernel.org/r/20190701201847.251028-1-shakeelb@google.com Fixes: e716f2e ("mm, vmscan: prevent kswapd sleeping prematurely due to mismatched classzone_idx") Signed-off-by: Shakeel Butt <shakeelb@google.com> Reviewed-by: Yang Shi <yang.shi@linux.alibaba.com> Acked-by: Mel Gorman <mgorman@techsingularity.net> Cc: Johannes Weiner <hannes@cmpxchg.org> Cc: Michal Hocko <mhocko@suse.com> Cc: Vlastimil Babka <vbabka@suse.cz> Cc: Hillf Danton <hdanton@sina.com> Cc: Roman Gushchin <guro@fb.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent cbcfa13 commit dffcac2

1 file changed

Lines changed: 15 additions & 12 deletions

File tree

‎mm/vmscan.c‎

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3644,19 +3644,18 @@ static int balance_pgdat(pg_data_t *pgdat, int order, int classzone_idx)
36443644
}
36453645

36463646
/*
3647-
* pgdat->kswapd_classzone_idx is the highest zone index that a recent
3648-
* allocation request woke kswapd for. When kswapd has not woken recently,
3649-
* the value is MAX_NR_ZONES which is not a valid index. This compares a
3650-
* given classzone and returns it or the highest classzone index kswapd
3651-
* was recently woke for.
3647+
* The pgdat->kswapd_classzone_idx is used to pass the highest zone index to be
3648+
* reclaimed by kswapd from the waker. If the value is MAX_NR_ZONES which is not
3649+
* a valid index then either kswapd runs for first time or kswapd couldn't sleep
3650+
* after previous reclaim attempt (node is still unbalanced). In that case
3651+
* return the zone index of the previous kswapd reclaim cycle.
36523652
*/
36533653
static enum zone_type kswapd_classzone_idx(pg_data_t *pgdat,
3654-
enum zone_type classzone_idx)
3654+
enum zone_type prev_classzone_idx)
36553655
{
36563656
if (pgdat->kswapd_classzone_idx == MAX_NR_ZONES)
3657-
return classzone_idx;
3658-
3659-
return max(pgdat->kswapd_classzone_idx, classzone_idx);
3657+
return prev_classzone_idx;
3658+
return pgdat->kswapd_classzone_idx;
36603659
}
36613660

36623661
static void kswapd_try_to_sleep(pg_data_t *pgdat, int alloc_order, int reclaim_order,
@@ -3797,7 +3796,7 @@ static int kswapd(void *p)
37973796

37983797
/* Read the new order and classzone_idx */
37993798
alloc_order = reclaim_order = pgdat->kswapd_order;
3800-
classzone_idx = kswapd_classzone_idx(pgdat, 0);
3799+
classzone_idx = kswapd_classzone_idx(pgdat, classzone_idx);
38013800
pgdat->kswapd_order = 0;
38023801
pgdat->kswapd_classzone_idx = MAX_NR_ZONES;
38033802

@@ -3851,8 +3850,12 @@ void wakeup_kswapd(struct zone *zone, gfp_t gfp_flags, int order,
38513850
if (!cpuset_zone_allowed(zone, gfp_flags))
38523851
return;
38533852
pgdat = zone->zone_pgdat;
3854-
pgdat->kswapd_classzone_idx = kswapd_classzone_idx(pgdat,
3855-
classzone_idx);
3853+
3854+
if (pgdat->kswapd_classzone_idx == MAX_NR_ZONES)
3855+
pgdat->kswapd_classzone_idx = classzone_idx;
3856+
else
3857+
pgdat->kswapd_classzone_idx = max(pgdat->kswapd_classzone_idx,
3858+
classzone_idx);
38563859
pgdat->kswapd_order = max(pgdat->kswapd_order, order);
38573860
if (!waitqueue_active(&pgdat->kswapd_wait))
38583861
return;

0 commit comments

Comments
 (0)