Skip to content

Commit 276aeee

Browse files
yanghuitorvalds
authored andcommitted
mm/mempolicy: fix a race between offset_il_node and mpol_rebind_task
Servers happened below panic: Kernel version:5.4.56 BUG: unable to handle page fault for address: 0000000000002c48 RIP: 0010:__next_zones_zonelist+0x1d/0x40 Call Trace: __alloc_pages_nodemask+0x277/0x310 alloc_page_interleave+0x13/0x70 handle_mm_fault+0xf99/0x1390 __do_page_fault+0x288/0x500 do_page_fault+0x30/0x110 page_fault+0x3e/0x50 The reason for the panic is that MAX_NUMNODES is passed in the third parameter in __alloc_pages_nodemask(preferred_nid). So access to zonelist->zoneref->zone_idx in __next_zones_zonelist will cause a panic. In offset_il_node(), first_node() returns nid from pol->v.nodes, after this other threads may chang pol->v.nodes before next_node(). This race condition will let next_node return MAX_NUMNODES. So put pol->nodes in a local variable. The race condition is between offset_il_node and cpuset_change_task_nodemask: CPU0: CPU1: alloc_pages_vma() interleave_nid(pol,) offset_il_node(pol,) first_node(pol->v.nodes) cpuset_change_task_nodemask //nodes==0xc mpol_rebind_task mpol_rebind_policy mpol_rebind_nodemask(pol,nodes) //nodes==0x3 next_node(nid, pol->v.nodes)//return MAX_NUMNODES Link: https://lkml.kernel.org/r/[email protected] Signed-off-by: yanghui <[email protected]> Reviewed-by: Muchun Song <[email protected]> Cc: <[email protected]> Signed-off-by: Andrew Morton <[email protected]> Signed-off-by: Linus Torvalds <[email protected]>
1 parent 79d3705 commit 276aeee

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

mm/mempolicy.c

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1979,17 +1979,26 @@ unsigned int mempolicy_slab_node(void)
19791979
*/
19801980
static unsigned offset_il_node(struct mempolicy *pol, unsigned long n)
19811981
{
1982-
unsigned nnodes = nodes_weight(pol->nodes);
1983-
unsigned target;
1982+
nodemask_t nodemask = pol->nodes;
1983+
unsigned int target, nnodes;
19841984
int i;
19851985
int nid;
1986+
/*
1987+
* The barrier will stabilize the nodemask in a register or on
1988+
* the stack so that it will stop changing under the code.
1989+
*
1990+
* Between first_node() and next_node(), pol->nodes could be changed
1991+
* by other threads. So we put pol->nodes in a local stack.
1992+
*/
1993+
barrier();
19861994

1995+
nnodes = nodes_weight(nodemask);
19871996
if (!nnodes)
19881997
return numa_node_id();
19891998
target = (unsigned int)n % nnodes;
1990-
nid = first_node(pol->nodes);
1999+
nid = first_node(nodemask);
19912000
for (i = 0; i < target; i++)
1992-
nid = next_node(nid, pol->nodes);
2001+
nid = next_node(nid, nodemask);
19932002
return nid;
19942003
}
19952004

0 commit comments

Comments
 (0)