Received: by 2002:a05:6358:4e97:b0:b3:742d:4702 with SMTP id ce23csp123775rwb; Thu, 18 Aug 2022 00:22:37 -0700 (PDT) X-Google-Smtp-Source: AA6agR6di6jCOB95A7P1XBIcbCGxkpNHPcrtB+ItGh6vxx7FIsUrotcYmg+smRT9fJ+CL/GdKgHO X-Received: by 2002:a17:902:f650:b0:172:8ee1:7f40 with SMTP id m16-20020a170902f65000b001728ee17f40mr1805577plg.101.1660807357117; Thu, 18 Aug 2022 00:22:37 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1660807357; cv=none; d=google.com; s=arc-20160816; b=dyNh2NPShBTZq7DTcQ0hrwqgCUexFkgJDDIUvcm8y2hbNPUOZyNxr1nypYEU9jSnNz cNHldEjsSBWI9zurN2oJsfaDunyAqECDVY5kBD+2D4/kLMWIxfGxGmpxQ5eAWyICDr4C Z/fnzixJkRXumKOVEi95CK/6I7kI7apITdBpsVbSrzAY7/yiHuYj+WbjJf19K4yH3vR3 fVXym9H6/8nlCRhLya/j43LZELOCB9Xo0lTLq9fpZQiIMV5fFcXmwwaOM9hqohuvayzZ 22iGpk/7Ngh/mhwuVwYY3mUpA71pc9DVPGIT5IA6OUptp7h/RU5e5rySnCKrGmhgBbkD VM8g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:to:references:message-id :content-transfer-encoding:cc:date:in-reply-to:from:subject :mime-version:dkim-signature; bh=f9JWaZ+Pf+0lJTRMwBclTF/cLsLmXbQ/pjz4rtVLgyA=; b=bUHJeyqoF5o7DWxN/jslf6rPUg+K+X6z3omWdED+omjHdm4QoPGwXCuhh32DDP3bw2 vx3rF5l8eoF3A8EYFckrPDmssyuKDv5TRpmQYJe09Zuzo5XATa9JHq81v/nDFt/VqjBs ZrJXY9RPJL/ifz5RytNUVmOgYiGNwEZg0Huzr1E3JEmVZ7T1vyGypiUosZgbyGzHcPBy vfswi8lty4NAK0loeZLpHJT9BoSenA1xrbR7ktUC4hJDSGRg4Vs1Id7HTw5LKcPC6r21 DGiByjum4oHpbSTciJdLFw9nn5aKQB89W1XnEdenD+4PSBuWautUF+9zqRwvekRrf4CO 8Y2A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linux.dev header.s=key1 header.b=vmswnGUG; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.dev Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id y186-20020a638ac3000000b00421904fd399si784912pgd.347.2022.08.18.00.22.25; Thu, 18 Aug 2022 00:22:37 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@linux.dev header.s=key1 header.b=vmswnGUG; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.dev Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S243557AbiHRG53 (ORCPT + 99 others); Thu, 18 Aug 2022 02:57:29 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35340 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239022AbiHRG51 (ORCPT ); Thu, 18 Aug 2022 02:57:27 -0400 Received: from out0.migadu.com (out0.migadu.com [94.23.1.103]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 694976DF94 for ; Wed, 17 Aug 2022 23:57:26 -0700 (PDT) Content-Type: text/plain; charset=us-ascii DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1660805844; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=f9JWaZ+Pf+0lJTRMwBclTF/cLsLmXbQ/pjz4rtVLgyA=; b=vmswnGUGMWUucEFmUSB5GO9rLMbcxg45sdkXMf86ZY8aYc+Rqm6/g/qGftkBc3BPiK5tGB Jspoz9YhNhWN8BUeUpTS4914fD9jYzQX5Bxc3uZ/U9QL20QdtZsod26edTtCnq4aalb5RT t5kNsEQCegx7oVR6AOtOSW8xzdXhHSo= MIME-Version: 1.0 Subject: Re: [PATCH v2] mm/mempolicy: fix lock contention on mems_allowed X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. From: Muchun Song In-Reply-To: <20220811124157.74888-1-wuyun.abel@bytedance.com> Date: Thu, 18 Aug 2022 14:56:40 +0800 Cc: Andrew Morton , Vlastimil Babka , Michal Hocko , Mel Gorman , Muchun Song , Linux MM , linux-kernel@vger.kernel.org Content-Transfer-Encoding: 7bit Message-Id: References: <20220811124157.74888-1-wuyun.abel@bytedance.com> To: Abel Wu X-Migadu-Flow: FLOW_OUT X-Migadu-Auth-User: linux.dev X-Spam-Status: No, score=-2.8 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_LOW,SPF_HELO_PASS, SPF_PASS,T_SCC_BODY_TEXT_LINE,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org > On Aug 11, 2022, at 20:41, Abel Wu wrote: > > 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. > > Say there are two tasks: A from cpusetA is performing set_mempolicy(2), > and B is changing cpusetA's cpuset.mems: > > A (set_mempolicy) B (echo xx > cpuset.mems) > ------------------------------------------------------- > pol = mpol_new(); > update_tasks_nodemask(cpusetA) { > foreach t in cpusetA { > cpuset_change_task_nodemask(t) { > mpol_set_nodemask(pol) { > task_lock(t); // t could be A > new = f(A->mems_allowed); > update t->mems_allowed; > pol.create(pol, new); > task_unlock(t); > } > } > } > } > task_lock(A); > A->mempolicy = pol; > task_unlock(A); > > In this case A's pol->nodes is computed by old mems_allowed, and could > be inconsistent with A's new mems_allowed. > > While it is different when replacing vmas' policy: the pol->nodes is > gone wild only when current_cpuset_is_being_rebound(): > > A (mbind) B (echo xx > cpuset.mems) > ------------------------------------------------------- > pol = mpol_new(); > mmap_write_lock(A->mm); > cpuset_being_rebound = cpusetA; > update_tasks_nodemask(cpusetA) { > foreach t in cpusetA { > cpuset_change_task_nodemask(t) { > mpol_set_nodemask(pol) { > task_lock(t); // t could be A > mask = f(A->mems_allowed); > update t->mems_allowed; > pol.create(pol, mask); > task_unlock(t); > } > } > foreach v in A->mm { > if (cpuset_being_rebound == cpusetA) > pol.rebind(pol, cpuset.mems); > v->vma_policy = pol; > } > mmap_write_unlock(A->mm); > mmap_write_lock(t->mm); > mpol_rebind_mm(t->mm); > mmap_write_unlock(t->mm); > } > } > cpuset_being_rebound = NULL; > > In this case, the cpuset.mems, which has already done updating, is > finally used for calculating pol->nodes, rather than A->mems_allowed. > So it is OK to call mpol_set_nodemask() with alloc_lock unlocked when > doing mbind(2). > > Fixes: 78b132e9bae9 ("mm/mempolicy: remove or narrow the lock on current") > Signed-off-by: Abel Wu Reviewed-by: Muchun Song Thanks.