From patchwork Fri Aug 13 16:40:53 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: yanghui X-Patchwork-Id: 12435839 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 CE1EDC4338F for ; Fri, 13 Aug 2021 16:43:02 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 544B56103A for ; Fri, 13 Aug 2021 16:43:02 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org 544B56103A 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 B9E478D0005; Fri, 13 Aug 2021 12:43:01 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id B4E538D0001; Fri, 13 Aug 2021 12:43:01 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A3CBC8D0005; Fri, 13 Aug 2021 12:43:01 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0051.hostedemail.com [216.40.44.51]) by kanga.kvack.org (Postfix) with ESMTP id 8D45F8D0001 for ; Fri, 13 Aug 2021 12:43:01 -0400 (EDT) Received: from smtpin12.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay02.hostedemail.com (Postfix) with ESMTP id 32CE01CB15 for ; Fri, 13 Aug 2021 16:43:01 +0000 (UTC) X-FDA: 78470626962.12.533FD31 Received: from mail-pj1-f51.google.com (mail-pj1-f51.google.com [209.85.216.51]) by imf04.hostedemail.com (Postfix) with ESMTP id 67D6B5000304 for ; Fri, 13 Aug 2021 16:43:00 +0000 (UTC) Received: by mail-pj1-f51.google.com with SMTP id u21-20020a17090a8915b02901782c36f543so21447773pjn.4 for ; Fri, 13 Aug 2021 09:42:59 -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=CUATwygOPv2kyN6CftHsZjBPjlDzWbVgcpU4L2JpF+I=; b=Gu6hhtn37Nk75yTZ9Frn574tyHk12BePB84jYD/gn2PpzFs+PSR9VzcbskIJDmP6f9 Veg19j8Qorw3vmBA7VB1Sc78ZBCWwjx8h113CMXOd2W5jor2S96mTlGTxsdWucqXIFa6 Cc48NTQSufnBgY0xxcqo5n6jEx0DqgUHnTK2fsEQRVTWfol2S6aKFg+Ps1zjxutDj9ZG 2U6issmtbSY2WdeVkOr1rQ1XkcX9QXxDFrCNg/yPaMECRlfde7k5cqV09Vt0i5UrapXA AKYHZ5DIf7iH/bq3p2uEYDG1ZqmKgJ5zg4qRPhugtlB1/bc7uY7/AnL4KtD/xReF5EWr jxQw== 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=CUATwygOPv2kyN6CftHsZjBPjlDzWbVgcpU4L2JpF+I=; b=VYtIje/h8MRzmXE76VU5G1Hip1w3gqmTKUwNc/MmoPeOh6zQ7Y2ZqSEN5vkxbYbuIu 4KO37AN63DEZWPsgDq7HkrteVTsvoHRDemeTg8e6R2mxMC/Dp96XDBFox4/sxqd+kNwR nO/C/GSRGgN+lWVJrT/4ftNUX4wEp9MVckQfPs3T1kfP1i2UDwleKkpyz6KDd3NXkw92 gF8oN0u2tKqNYxE+6piXnAXqt0UzTvkyEPHF/AEv4cLkb2ck4hqyVFyXY21Aab/2wJ1A +IZlDWjS3LyQv4fre6gdc97/Oxl8727TkrT6UW7O8BX+bGTlAsK9der8DId5VN7TJl/t HfVw== X-Gm-Message-State: AOAM530i6KQhgRmGP9HwsJYXpxehILUEVU56bqFclUUt4ssLfDyT+VpR x9RXMA7gcMZQtEiJY1B2/3BZQg== X-Google-Smtp-Source: ABdhPJxgZdp5KSjLDNDunowRaiPzcdg6oOE6o+MCUMtEDa0tSxmPE2j6TmIWtS1VUaL5xnK1Pav7Fw== X-Received: by 2002:a17:902:b692:b029:12b:e0b8:3415 with SMTP id c18-20020a170902b692b029012be0b83415mr2722503pls.32.1628872979000; Fri, 13 Aug 2021 09:42:59 -0700 (PDT) Received: from localhost.localdomain ([139.177.225.228]) by smtp.gmail.com with ESMTPSA id d17sm2874043pfn.110.2021.08.13.09.42.56 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Fri, 13 Aug 2021 09:42:58 -0700 (PDT) From: yanghui To: akpm@linux-foundation.org Cc: linux-mm@kvack.org, songmuchun@bytedance.com, yanghui Subject: [PATCH] mm/mempolicy: fix a race between offset_il_node and mpol_rebind_task Date: Sat, 14 Aug 2021 00:40:53 +0800 Message-Id: <20210813164053.51481-1-yanghui.def@bytedance.com> X-Mailer: git-send-email 2.24.3 (Apple Git-128) MIME-Version: 1.0 X-Rspamd-Queue-Id: 67D6B5000304 Authentication-Results: imf04.hostedemail.com; dkim=pass header.d=bytedance-com.20150623.gappssmtp.com header.s=20150623 header.b=Gu6hhtn3; dmarc=pass (policy=none) header.from=bytedance.com; spf=pass (imf04.hostedemail.com: domain of yanghui.def@bytedance.com designates 209.85.216.51 as permitted sender) smtp.mailfrom=yanghui.def@bytedance.com X-Rspamd-Server: rspam04 X-Stat-Signature: c5tsfn56wi1w9zcbp5frn6u9c4jn1jbo X-HE-Tag: 1628872980-178430 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 Reported-by: kernel test robot Reported-by: kernel test robot --- mm/mempolicy.c | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/mm/mempolicy.c b/mm/mempolicy.c index e32360e90274..9c3c168af3e2 100644 --- a/mm/mempolicy.c +++ b/mm/mempolicy.c @@ -193,7 +193,7 @@ static int mpol_new_interleave(struct mempolicy *pol, const nodemask_t *nodes) { if (nodes_empty(*nodes)) return -EINVAL; - pol->nodes = *nodes; + WRITE_ONCE(pol->nodes, *nodes); return 0; } @@ -211,7 +211,7 @@ static int mpol_new_bind(struct mempolicy *pol, const nodemask_t *nodes) { if (nodes_empty(*nodes)) return -EINVAL; - pol->nodes = *nodes; + WRITE_ONCE(pol->nodes, *nodes); return 0; } @@ -334,7 +334,7 @@ static void mpol_rebind_nodemask(struct mempolicy *pol, const nodemask_t *nodes) if (nodes_empty(tmp)) tmp = *nodes; - pol->nodes = tmp; + WRITE_ONCE(pol->nodes, tmp); } static void mpol_rebind_preferred(struct mempolicy *pol, @@ -1965,7 +1965,8 @@ unsigned int mempolicy_slab_node(void) */ static unsigned offset_il_node(struct mempolicy *pol, unsigned long n) { - unsigned nnodes = nodes_weight(pol->nodes); + nodemask_t nodemask = READ_ONCE(pol->nodes); + unsigned nnodes = nodes_weight(nodemask); unsigned target; int i; int nid; @@ -1973,9 +1974,9 @@ static unsigned offset_il_node(struct mempolicy *pol, unsigned long n) 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; }