From patchwork Sat Aug 14 08:36:32 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: yanghui X-Patchwork-Id: 12436573 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.5 required=3.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id F24E7C4338F for ; Sat, 14 Aug 2021 08:37:59 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 8796260F14 for ; Sat, 14 Aug 2021 08:37:59 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org 8796260F14 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=bytedance.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=kvack.org Received: by kanga.kvack.org (Postfix) id 151696B006C; Sat, 14 Aug 2021 04:37:59 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 102AB8D0001; Sat, 14 Aug 2021 04:37:59 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id F32406B0072; Sat, 14 Aug 2021 04:37:58 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0161.hostedemail.com [216.40.44.161]) by kanga.kvack.org (Postfix) with ESMTP id D7D8B6B006C for ; Sat, 14 Aug 2021 04:37:58 -0400 (EDT) Received: from smtpin20.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay02.hostedemail.com (Postfix) with ESMTP id 725931D59A for ; Sat, 14 Aug 2021 08:37:58 +0000 (UTC) X-FDA: 78473033436.20.C2B1CB1 Received: from mail-pj1-f43.google.com (mail-pj1-f43.google.com [209.85.216.43]) by imf29.hostedemail.com (Postfix) with ESMTP id 511A1900258B for ; Sat, 14 Aug 2021 08:37:57 +0000 (UTC) Received: by mail-pj1-f43.google.com with SMTP id qe12-20020a17090b4f8c00b00179321cbae7so8886925pjb.2 for ; Sat, 14 Aug 2021 01:37:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance-com.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=++j5/SmrvyY3n/NH1VGPOs8gpr5SNrtUN+XuC9Xqj2E=; b=IKETSIdXPKr50V3cD3nmv1hQxxjz4NYU8XhlRxT/EUvBFBxPZHzX9JLkQiNf1xuXDO /pdFa5L5LCDbx1Ilpw+n3qZlOnS9iua0T3xnHo+aleRKaJg0nfQYyv327HzSQkarGnEf OjjWTevMLwdNf7djwa8vB0hdyyUpCuqTzHCbkAtFpGnX7oSutxd2NNr3Gc5g3zMVSjuT 5tusEyDEwQP2YTRwuJfm8IfVZOjOAevJXnQ9L3jvzHWdkawlwYXk77ymt30Gad2Sq3Ds WxAhG5MbUl7rWZXsA5AQ00j4inw/JndfdiTMx2aMcEBJpEQofF6+0mjXd1aRfJUQ6IOm 0ODA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=++j5/SmrvyY3n/NH1VGPOs8gpr5SNrtUN+XuC9Xqj2E=; b=jW/jE2HEeAbYycJAF28g2Mn6HxLJYeY17XeZBG3NxorPdiWCssgARUe/H+F70afZnU 8cIG8YAEeUSJGa3IoRyZOlRUOZaC8kOa92TaMCbh2IHou8kGwaqU/sc67YVAlK6ZOOz8 aDWgzIgIM61HhpTDU/u4+gHwBVBmGapz8fPfPzA6I6sjy5l/g6HbYybM5lK0LpbE0yi+ WA3eKBDbKBdmWDdJQtX9ppBzc1lUH8DBb23Z/sPs63DW+6+TpVOXM3qocF1XJoRwQx5F qoCxOil3vh51ycUVNOUCKKMmhp6eP3/NcGWzHGyVFxql17jXQFci3Va6sjzRF1RJu3HU YeuQ== X-Gm-Message-State: AOAM532TQBP8Cn6JkZKfvNwZKXUlj5C05/VD/ihbzsspMgASZo4ZByul TeHAz4HBQC83w+JgoL0Gud0fzw== X-Google-Smtp-Source: ABdhPJzOADoABee26kSVM0+hm8IN6/mdOj8VWbYI6ln6s9fUpiFo8LsLbUdvYxFWv8dfqyLCNdD4Wg== X-Received: by 2002:a05:6a00:23ca:b0:3e1:2d8:33f3 with SMTP id g10-20020a056a0023ca00b003e102d833f3mr5876864pfc.42.1628930276024; Sat, 14 Aug 2021 01:37:56 -0700 (PDT) Received: from localhost.localdomain ([139.177.225.244]) by smtp.gmail.com with ESMTPSA id 65sm5736275pgi.12.2021.08.14.01.37.52 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Sat, 14 Aug 2021 01:37:55 -0700 (PDT) From: yanghui To: akpm@linux-foundation.org, songmuchun@bytedance.com, willy@infradead.org Cc: nathan@kernel.org, linux-mm@kvack.org, yanghui Subject: [PATCH v2] mm/mempolicy: fix a race between offset_il_node and mpol_rebind_task Date: Sat, 14 Aug 2021 16:36:32 +0800 Message-Id: <20210814083632.66010-1-yanghui.def@bytedance.com> X-Mailer: git-send-email 2.24.3 (Apple Git-128) MIME-Version: 1.0 X-Rspamd-Server: rspam03 X-Rspamd-Queue-Id: 511A1900258B X-Stat-Signature: fqqeqe31xxh9gbsj9bxqruhwr5668xtf Authentication-Results: imf29.hostedemail.com; dkim=pass header.d=bytedance-com.20150623.gappssmtp.com header.s=20150623 header.b=IKETSIdX; dmarc=pass (policy=none) header.from=bytedance.com; spf=pass (imf29.hostedemail.com: domain of yanghui.def@bytedance.com designates 209.85.216.43 as permitted sender) smtp.mailfrom=yanghui.def@bytedance.com X-HE-Tag: 1628930277-318404 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: 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 [264003.977696] RAX: 0000000000002c40 RBX: 0000000000100dca RCX: 0000000000000014 [264003.977872] Call Trace: [264003.977888] __alloc_pages_nodemask+0x277/0x310 [264003.977908] alloc_page_interleave+0x13/0x70 [264003.977926] handle_mm_fault+0xf99/0x1390 [264003.977951] __do_page_fault+0x288/0x500 [264003.977979] ? schedule+0x39/0xa0 [264003.977994] do_page_fault+0x30/0x110 [264003.978010] page_fault+0x3e/0x50 The reason of panic is that MAX_NUMNODES is passd in the third parameter in function __alloc_pages_nodemask(preferred_nid). So if to access zonelist->zoneref->zone_idx in __next_zones_zonelist the panic will happen. In offset_il_node(), first_node() return nid from pol->v.nodes, after this other threads may changed 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 Signed-off-by: yanghui --- mm/mempolicy.c | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/mm/mempolicy.c b/mm/mempolicy.c index e32360e90274..e03e0a3cd8d1 100644 --- a/mm/mempolicy.c +++ b/mm/mempolicy.c @@ -1965,17 +1965,28 @@ 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 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. + * Also nodemask should be read from memory atomic especially + * MAX_NUMNODES is large enough. + */ + 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; }