Skip to content

Commit 66eca10

Browse files
zhijianli88akpm00
authored andcommitted
mm/page_alloc: fix pcp->count race between drain_pages_zone() vs __rmqueue_pcplist()
It's expected that no page should be left in pcp_list after calling zone_pcp_disable() in offline_pages(). Previously, it's observed that offline_pages() gets stuck [1] due to some pages remaining in pcp_list. Cause: There is a race condition between drain_pages_zone() and __rmqueue_pcplist() involving the pcp->count variable. See below scenario: CPU0 CPU1 ---------------- --------------- spin_lock(&pcp->lock); __rmqueue_pcplist() { zone_pcp_disable() { /* list is empty */ if (list_empty(list)) { /* add pages to pcp_list */ alloced = rmqueue_bulk() mutex_lock(&pcp_batch_high_lock) ... __drain_all_pages() { drain_pages_zone() { /* read pcp->count, it's 0 here */ count = READ_ONCE(pcp->count) /* 0 means nothing to drain */ /* update pcp->count */ pcp->count += alloced << order; ... ... spin_unlock(&pcp->lock); In this case, after calling zone_pcp_disable() though, there are still some pages in pcp_list. And these pages in pcp_list are neither movable nor isolated, offline_pages() gets stuck as a result. Solution: Expand the scope of the pcp->lock to also protect pcp->count in drain_pages_zone(), to ensure no pages are left in the pcp list after zone_pcp_disable() [1] https://lore.kernel.org/linux-mm/[email protected]/ Link: https://lkml.kernel.org/r/[email protected] Fixes: 4b23a68 ("mm/page_alloc: protect PCP lists with a spinlock") Signed-off-by: Li Zhijian <[email protected]> Reported-by: Yao Xingtao <[email protected]> Reviewed-by: Vlastimil Babka <[email protected]> Cc: David Hildenbrand <[email protected]> Cc: <[email protected]> Signed-off-by: Andrew Morton <[email protected]>
1 parent f59adcf commit 66eca10

File tree

1 file changed

+11
-7
lines changed

1 file changed

+11
-7
lines changed

mm/page_alloc.c

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2343,16 +2343,20 @@ void drain_zone_pages(struct zone *zone, struct per_cpu_pages *pcp)
23432343
static void drain_pages_zone(unsigned int cpu, struct zone *zone)
23442344
{
23452345
struct per_cpu_pages *pcp = per_cpu_ptr(zone->per_cpu_pageset, cpu);
2346-
int count = READ_ONCE(pcp->count);
2347-
2348-
while (count) {
2349-
int to_drain = min(count, pcp->batch << CONFIG_PCP_BATCH_SCALE_MAX);
2350-
count -= to_drain;
2346+
int count;
23512347

2348+
do {
23522349
spin_lock(&pcp->lock);
2353-
free_pcppages_bulk(zone, to_drain, pcp, 0);
2350+
count = pcp->count;
2351+
if (count) {
2352+
int to_drain = min(count,
2353+
pcp->batch << CONFIG_PCP_BATCH_SCALE_MAX);
2354+
2355+
free_pcppages_bulk(zone, to_drain, pcp, 0);
2356+
count -= to_drain;
2357+
}
23542358
spin_unlock(&pcp->lock);
2355-
}
2359+
} while (count);
23562360
}
23572361

23582362
/*

0 commit comments

Comments
 (0)