From patchwork Tue Aug 9 10:49:27 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Abel Wu X-Patchwork-Id: 12939515 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id D0408C19F2D for ; Tue, 9 Aug 2022 10:49:54 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 257BA8E0001; Tue, 9 Aug 2022 06:49:54 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 207206B0072; Tue, 9 Aug 2022 06:49:54 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 0FA508E0001; Tue, 9 Aug 2022 06:49:54 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id F1E8C6B0071 for ; Tue, 9 Aug 2022 06:49:53 -0400 (EDT) Received: from smtpin22.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id CE2F11C670F for ; Tue, 9 Aug 2022 10:49:53 +0000 (UTC) X-FDA: 79779733866.22.5FC0206 Received: from mail-oi1-f178.google.com (mail-oi1-f178.google.com [209.85.167.178]) by imf31.hostedemail.com (Postfix) with ESMTP id 73A992015E for ; Tue, 9 Aug 2022 10:49:52 +0000 (UTC) Received: by mail-oi1-f178.google.com with SMTP id h125so13330505oif.8 for ; Tue, 09 Aug 2022 03:49:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance-com.20210112.gappssmtp.com; s=20210112; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=wc4pc4XjzEcCEnj/rsl5k2xYOk9EdSwkRXBZHUsSZMk=; b=cNv9ey+nvgOoNPDu3fYAV530sBBW3pHAx4VkhkoMEqb4HXlxJJhwrYW2fhuoGEabOq GwEcSrUH8Ssfa8En8W/sqL4jkAziGYFUMu3Yw2lI2ohYSQLlk3XHeKcFDHqsmLfTOUT/ MH+Xe1J4clCsuVxQnnKV9K1h8Tkqo9fsIuazgNSZWzSg2dgNzDpGt92vAOxeuWDRKwnA wUyMfb0rBy59VQx2S519n5z1HVIA+2+XlJyLAsM69t+G4iqI5j6o+SEM91l9SNcJEBcG 6R70Z2EaXlFGVWFJrhsEKuHagQ8lmBHYM7LgfmSictkfHCyM3CGVM67k7qTcgRDM3qEe eP+A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=wc4pc4XjzEcCEnj/rsl5k2xYOk9EdSwkRXBZHUsSZMk=; b=f/klTZWco+53EvZzf8c133d/foxngQIToxMusKRhLbMoGMo4H8PuCPydR5lk2wE2vY K4AHnj6vhZIMFgZGGBlkIV44+j0TZqgOx/6RR1YPP2H5Fzb3TfmPGBlSw8qzwFdFnzKp qicLP0djvlfdRcSlhB2pLn2lZKRH2KSqG0PDn8JLj/XpncCe1Fwsld0IcJfNF45TefvQ SeBetnlhOwEkPniNbhsCjxpfaeTnsOimMRaqQ9mBeMx2cjk0pBjJJkoBhLgrhkWfhJNk nynTWHjKXTR2NG1DFnsNj2teP18Z1tKAbBNsj/MDIoVHWQCMtt+560gtcAZTDZOLvFzS XdbA== X-Gm-Message-State: ACgBeo1dJMjn8Cl60y+4VhXeQwsxiIavWeD9ARr5A44fEsLasmCpRMcC qnATe8Lb3NouMhFtsQKAy1Z4xVoc469G2f56 X-Google-Smtp-Source: AA6agR4PYo3SWTvvtd2qU1lQNNRTyI1XAdbmd19eqOVySGmVsFyA0jsDNS8hDoQ6wKTbK/2nUV0IEA== X-Received: by 2002:a17:90b:4b4d:b0:1f5:164f:f7c4 with SMTP id mi13-20020a17090b4b4d00b001f5164ff7c4mr34144617pjb.131.1660042181117; Tue, 09 Aug 2022 03:49:41 -0700 (PDT) Received: from C02DV8HUMD6R.bytedance.net ([139.177.225.254]) by smtp.gmail.com with ESMTPSA id b11-20020a62a10b000000b0052def2e20dasm10270377pff.167.2022.08.09.03.49.37 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Tue, 09 Aug 2022 03:49:40 -0700 (PDT) From: Abel Wu To: Andrew Morton , Vlastimil Babka , Michal Hocko , Mel Gorman , Muchun Song Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, Abel Wu Subject: [PATCH] mm/mempolicy: fix lock contention on mems_allowed Date: Tue, 9 Aug 2022 18:49:27 +0800 Message-Id: <20220809104927.44366-1-wuyun.abel@bytedance.com> X-Mailer: git-send-email 2.31.1 MIME-Version: 1.0 ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1660042193; a=rsa-sha256; cv=none; b=4/51mycHr47oWSI9Glu3sZm0BBRP2qQN9DuGr6wS4ChQOF7AQksYF9DejWRnPXtfpWRGZy HdPyUMv7rCiiwWK79XLG9Fbgz+QSoBsWnf4TAQ+6o0VRz1Po3wm/gpppCiIbMH/WlK1BVj IndhJsiOj5IuJ/wLooplhOixTKbX+vo= ARC-Authentication-Results: i=1; imf31.hostedemail.com; dkim=pass header.d=bytedance-com.20210112.gappssmtp.com header.s=20210112 header.b=cNv9ey+n; spf=pass (imf31.hostedemail.com: domain of wuyun.abel@bytedance.com designates 209.85.167.178 as permitted sender) smtp.mailfrom=wuyun.abel@bytedance.com; dmarc=pass (policy=none) header.from=bytedance.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1660042193; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:references:dkim-signature; bh=wc4pc4XjzEcCEnj/rsl5k2xYOk9EdSwkRXBZHUsSZMk=; b=EwyaP73Sh9/q+JDFQrv4JH8BeRCbMDPIxo7qjuQfPI1YlEB9X9t0zHbyIsJv/xvHEW5e9s GUtZvx0pDF6EQPWHDighlgzvUiXoowDkqGW9Gwg3vOCxReAhoF+F2/zeZPiUayJL90DQ1g sq7nyTz1cZ4SEwj4ghIVgKixkkLx2yM= X-Rspamd-Server: rspam06 X-Rspam-User: X-Stat-Signature: h7b3s9iuq9bwhxqdtbehywxxqpf1bitq X-Rspamd-Queue-Id: 73A992015E Authentication-Results: imf31.hostedemail.com; dkim=pass header.d=bytedance-com.20210112.gappssmtp.com header.s=20210112 header.b=cNv9ey+n; spf=pass (imf31.hostedemail.com: domain of wuyun.abel@bytedance.com designates 209.85.167.178 as permitted sender) smtp.mailfrom=wuyun.abel@bytedance.com; dmarc=pass (policy=none) header.from=bytedance.com X-HE-Tag: 1660042192-969549 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: The mems_allowed field can be modified by other tasks, so it isn't safe to access it with alloc_lock unlocked even in the current process context. Fixes: 78b132e9bae9 ("mm/mempolicy: remove or narrow the lock on current") Signed-off-by: Abel Wu --- mm/mempolicy.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/mm/mempolicy.c b/mm/mempolicy.c index d39b01fd52fe..ae422e44affb 100644 --- a/mm/mempolicy.c +++ b/mm/mempolicy.c @@ -855,12 +855,14 @@ static long do_set_mempolicy(unsigned short mode, unsigned short flags, goto out; } + task_lock(current); ret = mpol_set_nodemask(new, nodes, scratch); if (ret) { + task_unlock(current); mpol_put(new); goto out; } - task_lock(current); + old = current->mempolicy; current->mempolicy = new; if (new && new->mode == MPOL_INTERLEAVE) @@ -1295,7 +1297,9 @@ static long do_mbind(unsigned long start, unsigned long len, NODEMASK_SCRATCH(scratch); if (scratch) { mmap_write_lock(mm); + task_lock(current); err = mpol_set_nodemask(new, nmask, scratch); + task_unlock(current); if (err) mmap_write_unlock(mm); } else