Received: by 2002:a05:6a11:4021:0:0:0:0 with SMTP id ky33csp1991308pxb; Thu, 16 Sep 2021 22:41:15 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwjtMmlXXAAP6oyUM0CSFq+raPc76YMSsMWcsMeJPmkn9HvQM6/C0eZlQimqoP2+Mn3z1vr X-Received: by 2002:a50:9b17:: with SMTP id o23mr10700166edi.341.1631857275662; Thu, 16 Sep 2021 22:41:15 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1631857275; cv=none; d=google.com; s=arc-20160816; b=dytcsMcOuUyhGTpOR8r6fW5YsPbFZs9IzCgtzf3gdWzly9D1vnAa8wScR/hIDsBTKt CZUnL3v0FDIUlbYlGI6WXWBuge7veAZipmuOebqFV8PQrZEHH7mGHLbK6+hE9r2Pp4NX VM1fXAnANZF4pkYeYv6O5nmUN4jnBR2XrIMnST9IdATrdqmr7G/Kawcj7nEtlct14ikm zPeQmjxiCHDj4w4tiVkCoIB1hGSC1JqvVzsggm4+dMd1k12Tdma8k6O698F9drO2rSNA TwEJhKXSB3NoQSnNxIM93gqLNDUlJg6xPC7wSxo/LOpzVYp7mamMBsG/JIG56B4O//KE Uy8Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=ZCjZvcayEstBvJ/QHd2QoB06jy8oV1hjIGNycdUdil4=; b=cNa/vAkn/rh3whY+iUV6SKt+60HfHtt/PLsmOA98TpXMvkdoGXjfe08fTZgPFpltK/ Xi1GKX4aKyB75T1u+iRmBHbJiNyjJ23/wLrySwtnCPWpVnCnXm34vf+JYBtRAlcW4ngg /W02Z9T3bZ+M3QMnBEw4E5f2gLcrXEDvGTQWukT1bv3e87LAToW4me7wt+tEpiP+xlp7 3nmGmTr4W+gnrWwdSzEH8ZyOoO4VyN0H7MbpjkzJDMkVJf5+hdtuV9rsajpgngdyRnDK RlrrVYdSveR8a7P3Y93NYFDfd92IZxayBP5ZqghUDVc1cp11QuFv+15p7j4k3pou09kM fHGg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=PO3hTT8m; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id w5si5739898edr.146.2021.09.16.22.40.49; Thu, 16 Sep 2021 22:41:15 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=PO3hTT8m; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1357606AbhIPRwC (ORCPT + 99 others); Thu, 16 Sep 2021 13:52:02 -0400 Received: from mail.kernel.org ([198.145.29.99]:57136 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1355681AbhIPRl7 (ORCPT ); Thu, 16 Sep 2021 13:41:59 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 6460F6326A; Thu, 16 Sep 2021 16:54:01 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1631811242; bh=zOxIMmM7JqWdZCY4KIrYNtd1vD/SZ3H0XjVafAUFlXM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=PO3hTT8mgbof//V6683nlY6FKKKtMu9hoxW06yFwmr0z7G8spJkNGrJTPyYgabWsl XliufxTDXxjMzc+dvsYjKizaIBy/0geOGF7K3m8M1gLtHxde/585oMfjvkvVlC4iIP VMjgAtissi7qHM2QvM1gtbsTCg7I2waa7w20Uasc= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, yanghui , Muchun Song , Andrew Morton , Linus Torvalds Subject: [PATCH 5.14 409/432] mm/mempolicy: fix a race between offset_il_node and mpol_rebind_task Date: Thu, 16 Sep 2021 18:02:38 +0200 Message-Id: <20210916155824.711041910@linuxfoundation.org> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20210916155810.813340753@linuxfoundation.org> References: <20210916155810.813340753@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: yanghui commit 276aeee1c5fc00df700f0782060beae126600472 upstream. 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/20210906034658.48721-1-yanghui.def@bytedance.com Signed-off-by: yanghui Reviewed-by: Muchun Song Cc: Signed-off-by: Andrew Morton Signed-off-by: Linus Torvalds Signed-off-by: Greg Kroah-Hartman --- mm/mempolicy.c | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) --- a/mm/mempolicy.c +++ b/mm/mempolicy.c @@ -1965,17 +1965,26 @@ unsigned int mempolicy_slab_node(void) */ static unsigned offset_il_node(struct mempolicy *pol, unsigned long n) { - unsigned nnodes = nodes_weight(pol->nodes); - unsigned target; + nodemask_t nodemask = pol->nodes; + unsigned int target, nnodes; int i; int nid; + /* + * The barrier will stabilize the nodemask in a register or on + * the stack so that it will stop changing under the code. + * + * Between first_node() and next_node(), pol->nodes could be changed + * by other threads. So we put pol->nodes in a local stack. + */ + barrier(); + nnodes = nodes_weight(nodemask); if (!nnodes) return numa_node_id(); target = (unsigned int)n % nnodes; - nid = first_node(pol->nodes); + nid = first_node(nodemask); for (i = 0; i < target; i++) - nid = next_node(nid, pol->nodes); + nid = next_node(nid, nodemask); return nid; }