From patchwork Mon Sep 6 03:46:58 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: yanghui X-Patchwork-Id: 12476249 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.6 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,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 8E14FC433FE for ; Mon, 6 Sep 2021 03:47:11 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id F122E60ED8 for ; Mon, 6 Sep 2021 03:47:10 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org F122E60ED8 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 E5F546B0071; Sun, 5 Sep 2021 23:47:09 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id E0F206B0072; Sun, 5 Sep 2021 23:47:09 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id CFD7E6B0073; Sun, 5 Sep 2021 23:47:09 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0144.hostedemail.com [216.40.44.144]) by kanga.kvack.org (Postfix) with ESMTP id C25AF6B0071 for ; Sun, 5 Sep 2021 23:47:09 -0400 (EDT) Received: from smtpin29.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay04.hostedemail.com (Postfix) with ESMTP id 5065B29DE4 for ; Mon, 6 Sep 2021 03:47:09 +0000 (UTC) X-FDA: 78555762978.29.EB7E141 Received: from mail-pf1-f169.google.com (mail-pf1-f169.google.com [209.85.210.169]) by imf21.hostedemail.com (Postfix) with ESMTP id 89C18D029307 for ; Mon, 6 Sep 2021 03:47:08 +0000 (UTC) Received: by mail-pf1-f169.google.com with SMTP id g14so4592026pfm.1 for ; Sun, 05 Sep 2021 20:47:08 -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=3fftJnEfxURxmLnjNiehf0RW0huZ6+SyHBdzxTuKx/o=; b=TfA0WDbUFHaZfHvm3PYlKKYwkRLysiF/NZG0t3L7yMmQspKOl2FWoV69dajjXVgVU+ yLVcbRpwgVWcvfRhAmDfHN0VdR51oYSbFY7knC9EjmeqkMscKMHmyvQjITqfCyVmFGs9 cw6GYvzZSg6xWRTAtXUCfUmUkA0CCcqWuE6WsXjcamPHrk8l1KJG8Hy/ZiSRcKQy+r1u FjhpPxnHLZlH5lTjK+Q4c3vKZ1g+ropq2JN1oYSMBYldy5Iy+irRFfdI6qeNgkRkJYuu TjBYBjz/amTboViNMqk9c73sh9pbMeI1UE3H4KxWqDyp7reGONQ3/fmfMUnxIVssd1Ub gRRQ== 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=3fftJnEfxURxmLnjNiehf0RW0huZ6+SyHBdzxTuKx/o=; b=kDdnwv4bwiW2/dbKy2nzqBRIoQ1BaOCKUQJp4oabdK0qiTxor/1yUZ6ILWIGTfq9E8 3F1xncEfUdGFDKX/RzOSG/aq/7FxUlLaCWMtVT9mmdWgNcg0sXF8IQ4LCX+TabDbHYJv AnY1kjQGGxjKg/le7PyMBoXCQbV2tdRhJRka7N1BLXY1nnGUxrlbQiE4aPkXk3qE8NIJ 00rEEXRGvFUXxPM/7EgjVDtef8sv9TnN7vhjtcFXcewNHGS1A+eaUDDCtAUvZKvcmBSG ECNlW9nNFQxxVOyoDpK6WVmZMeFtvUXxqANZ1nbismm9NWZHyNVvLyLXaY1brPTLtg3E P3+w== X-Gm-Message-State: AOAM531XGeF3SfomFBLz7ZF1GSd6WUxja4NykS2Nr3OdThjA2ilW4KXt QcX6WN7GNb7ma5bjtsI2KrFd3Q== X-Google-Smtp-Source: ABdhPJwrsSAYh0ecu95UAEBYKRWrifyqcsR2KmtRTzK0Q3YuNI9tnSnGfYsAlFdnr1ze5jzmP/PVtw== X-Received: by 2002:a63:ec45:: with SMTP id r5mr10368048pgj.440.1630900027510; Sun, 05 Sep 2021 20:47:07 -0700 (PDT) Received: from C02D84JPML85.bytedance.net ([61.120.150.73]) by smtp.gmail.com with ESMTPSA id t22sm5566162pfg.109.2021.09.05.20.47.04 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Sun, 05 Sep 2021 20:47:07 -0700 (PDT) From: yanghui To: akpm@linux-foundation.org Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, songmuchun@bytedance.com, yanghui Subject: [RESEND PATCH v3] mm/mempolicy: fix a race between offset_il_node and mpol_rebind_task Date: Mon, 6 Sep 2021 11:46:58 +0800 Message-Id: <20210906034658.48721-1-yanghui.def@bytedance.com> X-Mailer: git-send-email 2.24.3 (Apple Git-128) MIME-Version: 1.0 Authentication-Results: imf21.hostedemail.com; dkim=pass header.d=bytedance-com.20150623.gappssmtp.com header.s=20150623 header.b=TfA0WDbU; spf=pass (imf21.hostedemail.com: domain of yanghui.def@bytedance.com designates 209.85.210.169 as permitted sender) smtp.mailfrom=yanghui.def@bytedance.com; dmarc=pass (policy=none) header.from=bytedance.com X-Rspamd-Server: rspam02 X-Rspamd-Queue-Id: 89C18D029307 X-Stat-Signature: iyuu6mkb3picnsmty1arhkn4mxrw16oi X-HE-Tag: 1630900028-980843 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 Reviewed-by: Muchun Song --- Changes in v2: 1.Fix WRITE_ONCE/READ_ONCE can't deal with more than sizeof(long) bits data. Changes in v3: 1.Modify some wrong comments. mm/mempolicy.c | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/mm/mempolicy.c b/mm/mempolicy.c index e32360e90274..54f6eaff18c5 100644 --- 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; }