From mboxrd@z Thu Jan 1 00:00:00 1970 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 6D681C19F2A for ; Thu, 11 Aug 2022 09:09:55 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 04E778E0001; Thu, 11 Aug 2022 05:09:55 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id F19476B0074; Thu, 11 Aug 2022 05:09:54 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id DB9BA8E0001; Thu, 11 Aug 2022 05:09:54 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id C92DE6B0073 for ; Thu, 11 Aug 2022 05:09:54 -0400 (EDT) Received: from smtpin30.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 9A402160748 for ; Thu, 11 Aug 2022 09:09:54 +0000 (UTC) X-FDA: 79786739508.30.A3D0555 Received: from smtp-out2.suse.de (smtp-out2.suse.de [195.135.220.29]) by imf09.hostedemail.com (Postfix) with ESMTP id D969D140190 for ; Thu, 11 Aug 2022 09:09:53 +0000 (UTC) Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by smtp-out2.suse.de (Postfix) with ESMTPS id 92D6A20463; Thu, 11 Aug 2022 09:09:52 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1660208992; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=ogHa+KiM+lP+RnrQji6o/A/7B+pSIENIDsiGRYFdQV4=; b=f59bscZHABasVaivpCXZ8mqOezMBwxY7rljoxhkVBaGDH8c5PdZ58buL8oKRvkTtnQ3xvk wwLRlH9NXa/k3jKEdxzcQyUNJc4loDtLvc4KPuct77ytp9CMUGzMpeUVBXBu22ouGVfa+q 3Kp+WjdKDxgD7O1+foB4h1ZrhBCmk4M= Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by imap2.suse-dmz.suse.de (Postfix) with ESMTPS id 797351342A; Thu, 11 Aug 2022 09:09:52 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id HSjGGmDH9GLYZQAAMHmgww (envelope-from ); Thu, 11 Aug 2022 09:09:52 +0000 Date: Thu, 11 Aug 2022 11:09:51 +0200 From: Michal Hocko To: Abel Wu Cc: Andrew Morton , Vlastimil Babka , Mel Gorman , Muchun Song , linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH] mm/mempolicy: fix lock contention on mems_allowed Message-ID: References: <20220809104927.44366-1-wuyun.abel@bytedance.com> <7ece0714-2646-4f1a-60b6-aaafc1135b1e@bytedance.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <7ece0714-2646-4f1a-60b6-aaafc1135b1e@bytedance.com> ARC-Authentication-Results: i=1; imf09.hostedemail.com; dkim=pass header.d=suse.com header.s=susede1 header.b=f59bscZH; dmarc=pass (policy=quarantine) header.from=suse.com; spf=pass (imf09.hostedemail.com: domain of mhocko@suse.com designates 195.135.220.29 as permitted sender) smtp.mailfrom=mhocko@suse.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1660208994; a=rsa-sha256; cv=none; b=oWwCQEKb6c+3+nub5omo6jQCJleHwW5Bnd1ZLzQVfVOKKZi9V6pgUbDcQOEB3BKIUeq6f4 kjBqRXwXXjaI/GmVlvahnAwYvYDGw/fo6Y4g1QMV+5P1BBWC3Mdz057JEFOC34HKBsZXH4 g/L4jUyqgXGjqUXgRONK4As7bWxy02o= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1660208994; 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-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=ogHa+KiM+lP+RnrQji6o/A/7B+pSIENIDsiGRYFdQV4=; b=45PGLM0JKgwZA9qk9VRboG8RommVrlHyfRs/t4DecBumONjhjHg+DfPaEG2ofZFm2ZUSDg sintClQT1lF2ZWM03EyeR1n1+vfTcgwxhU2dGdicKUANdNDw0FpGxnG4BTdoFuJSzpUlvj jGHx+UZF/r69AFPHq7ZvZqqiZT7rm6M= X-Rspam-User: Authentication-Results: imf09.hostedemail.com; dkim=pass header.d=suse.com header.s=susede1 header.b=f59bscZH; dmarc=pass (policy=quarantine) header.from=suse.com; spf=pass (imf09.hostedemail.com: domain of mhocko@suse.com designates 195.135.220.29 as permitted sender) smtp.mailfrom=mhocko@suse.com X-Stat-Signature: s67bas4hbiycsyj5osmpreeehe9ycyyj X-Rspamd-Server: rspam11 X-Rspamd-Queue-Id: D969D140190 X-HE-Tag: 1660208993-986997 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000004, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: On Thu 11-08-22 16:43:28, Abel Wu wrote: > On 8/9/22 8:11 PM, Michal Hocko Wrote: > > On Tue 09-08-22 18:49:27, 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. > > > > It would be useful to describe the racing scenario and the effect it > > would have. 78b132e9bae9 hasn't really explained thinking behind and why > > it was considered safe to drop the lock. I assume it was based on the > > fact that the operation happens on the current task but this is hard to > > tell. > > > > Sorry for my poor description. 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) { > task_lock(t); // t could be A > mpol_set_nodemask(pol) { > 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) > > cpuset_being_rebound = cpusetA; > update_tasks_nodemask(cpusetA) { > foreach t in cpusetA { > cpuset_change_task_nodemask(t) { > task_lock(t); // t could be A > pol = mpol_new(); > mmap_write_lock(A->mm); > mpol_set_nodemask(pol) { > mask = f(A->mems_allowed); > update t->mems_allowed; > pol.create(pol, mask); > } > task_unlock(t); > } > foreach v in A->mm { > if (current_cpuset_is_being_rebound()) > 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). Please add this to the patch changelog. Thanks! -- Michal Hocko SUSE Labs