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 556EFD7876C for ; Thu, 21 Nov 2024 14:41:43 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id E5ECE6B008C; Thu, 21 Nov 2024 09:41:42 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id E0E656B0092; Thu, 21 Nov 2024 09:41:42 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id CD6866B0093; Thu, 21 Nov 2024 09:41:42 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id AB51E6B008C for ; Thu, 21 Nov 2024 09:41:42 -0500 (EST) Received: from smtpin03.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 5526381384 for ; Thu, 21 Nov 2024 14:41:42 +0000 (UTC) X-FDA: 82810363356.03.68310C1 Received: from mail-io1-f50.google.com (mail-io1-f50.google.com [209.85.166.50]) by imf10.hostedemail.com (Postfix) with ESMTP id 13B9BC001B for ; Thu, 21 Nov 2024 14:41:16 +0000 (UTC) Authentication-Results: imf10.hostedemail.com; dkim=pass header.d=gourry.net header.s=google header.b=vFkGsBpg; spf=pass (imf10.hostedemail.com: domain of gourry@gourry.net designates 209.85.166.50 as permitted sender) smtp.mailfrom=gourry@gourry.net; dmarc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1732200038; 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=D58g9m6r1L8TsqMzIkGhPirzJHY6FPvniZrb5lUL9SQ=; b=nilUkp44dHMxbq9pz7BHuVBvsF3eCwf+JNBu0Jvw5Knh51ML5mVlN4kELIZl0l83/a5vsL 8JxtPfpYQNgHxcpgxugpitBue2PBelA+pRZxKxfLKY6K/O9Sv5vx9fbj4lqwerIC53GVuk bOCRRWA1yjjdQfhrPToBqviOJQrMZwg= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1732200038; a=rsa-sha256; cv=none; b=ORxeEBlDJpTwtwjA0x9JREzxN6oCiZvtsVYUYp0+sfI4euqrVzZm4JmxRmBy1c+fA1JPmP dXqamUwNxZyMqM0HSBir4sHcdwB7isKn6wmpcyQ6Cen5wM0HNXtdBgMS1s/3v1xM7i8dUH CCjFO1lKR8j3yC4K1vrjpdBXDovku/s= ARC-Authentication-Results: i=1; imf10.hostedemail.com; dkim=pass header.d=gourry.net header.s=google header.b=vFkGsBpg; spf=pass (imf10.hostedemail.com: domain of gourry@gourry.net designates 209.85.166.50 as permitted sender) smtp.mailfrom=gourry@gourry.net; dmarc=none Received: by mail-io1-f50.google.com with SMTP id ca18e2360f4ac-83a9be2c028so32802439f.1 for ; Thu, 21 Nov 2024 06:41:40 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gourry.net; s=google; t=1732200099; x=1732804899; darn=kvack.org; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=D58g9m6r1L8TsqMzIkGhPirzJHY6FPvniZrb5lUL9SQ=; b=vFkGsBpgk3jQP8wIM0VKXYqOb01/2PxW7sarbr0hfRco2pMCuz4sMOiAJj8EiAdLSd CHOTNfWHobtahqdjA4LKckRWkgro5XBXVDNEJBCU3dpVb8TeTirn2oScEH4OLfU06LQR H0kQ0woAlvsSVtqLSFMfwWZS0t0qtdkh+7RniIC0kDEFkfVkFJWJcxPcoHQO0AQKYK8+ CH90NiSNenKdjKD/4rMGT+B7MrhQyHM8HlRrEwjMZwRhxgkDcdXk0v6zjlQtyTCr/9bk EASoasfeZdX3oOvTaqY+DuQ2DvzKPjA4R0GOZmp7vffWVtSn/k0FNKQNqMAF4wFr/GpN kedw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1732200099; x=1732804899; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=D58g9m6r1L8TsqMzIkGhPirzJHY6FPvniZrb5lUL9SQ=; b=ULm/h9ChwzR5kt4THIK4wUaJ5Ok74P6qC9+LgIFPMwZsoP2+W5VHyzbunWs99es0x9 yots9GW0VxfDz6PuHXqSPdRuJwpJmxbqaqmVxiigBTdc7AucEM//s1Bm9AA10TlxGqI1 HFe3z2E03iEEpaKMZUVWeNO6igA2e9u65xDJnyYas5/VhUwkIeLRmEmXVrIMnqY9L9aq Ilmfy8iPHVx6+H+yhgFiGpET98TAi6rPTZ0o7i83hR+vNyfyuvaJv+KE7CDTTpIlg14x XiAf+hHJlpWBy5B9x1J+RregDUUnc2lYpOqnG1koti4ZyIVFmHD5kzCPVC2wZatVOn+o EFPQ== X-Forwarded-Encrypted: i=1; AJvYcCUUtFHJ5NiB1FhGixiBVO+rhnjfyAhSOAAX7ua9hiH5QVs+alZ0h5U7zCCiMOFZY+kI1GxcPtvcnA==@kvack.org X-Gm-Message-State: AOJu0YwOkKt2UFDtoJlLWLZDdPJQ3zaLtcCcYQq3JtV+N4Z/pFZ8coSG aTaEbHdj8DJCoz2BI85KLUh8ODtkoiZjYbiJkNIuBJEw2426+d1mPaoJ2SOo/vjgjk6cYEuq+/T L X-Gm-Gg: ASbGncvJWrWb7b06rUN8kBw91gjoNHvGnJqcY8yhMFGoBDxrpvymZ0y+lkb8uBaJT7d cKAdyTGwHdklTJhYyFCwLySC6ceiRN+SBF1bJc70DAuQy+8vGBoNyGrUCSbLdpHxPEINzuYJbw+ 924bWi8eRpAuaWU+nMNV51UuStDeqx+EQq699rF1w3bvX3EiA5EntHKFMWXTt1QxvT52v1z1srV Dv8sNTj8o59aG4yDqFBjVzRSJ1U4A8cPP4efJcVHeavY/gXsiHDr7H6/xVM+UeWL996Gq5NOpmh 3IvhjnDMpv+7/MU0LWtoBbMLHyUWJ5+UmvI= X-Google-Smtp-Source: AGHT+IFnTZb+L/D1s/7SlsN2UdZI16lkDu610mjW+7+9l+WO2ykppSRG6BbuAdjmoJp2bOfJwyBjow== X-Received: by 2002:a05:622a:30b:b0:463:788e:790a with SMTP id d75a77b69052e-46477dbc8d2mr85508841cf.13.1732200088600; Thu, 21 Nov 2024 06:41:28 -0800 (PST) Received: from PC2K9PVX.TheFacebook.com (pool-173-79-56-208.washdc.fios.verizon.net. [173.79.56.208]) by smtp.gmail.com with ESMTPSA id d75a77b69052e-4653923af9bsm3686051cf.80.2024.11.21.06.41.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 21 Nov 2024 06:41:28 -0800 (PST) Date: Thu, 21 Nov 2024 09:41:21 -0500 From: Gregory Price To: David Hildenbrand Cc: Zhen Ni , akpm@linux-foundation.org, linux-mm@kvack.org Subject: Re: [PATCH] mm/mempolicy: Fix redundant check and refine lock protection scope in init_nodemask_of_mempolicy Message-ID: References: <20241121122409.277927-1-zhen.ni@easystack.cn> <7f9c4c98-86bf-40a0-817a-858767946cb0@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <7f9c4c98-86bf-40a0-817a-858767946cb0@redhat.com> X-Stat-Signature: dfkmbkm7bhk9w544h71t3pwkwrmxeegc X-Rspam-User: X-Rspamd-Queue-Id: 13B9BC001B X-Rspamd-Server: rspam02 X-HE-Tag: 1732200076-3417 X-HE-Meta: U2FsdGVkX1+ZUQ3wwp5TtraF8l149vFptcF2BJfAE3UidiErarEa1Pctu20Z6j9YXTww5y3UWa/dzpdaDrLsGMcmJc0KrSspE6B7b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9LSS+GUc+KK4TzlREoMF4Z9GnZpqj10PUUfX/BVyoDsfHejFFLccMgRNVNyiE63+ZbF1iYP06TvnTpyP+S1hLBSZcGhE/fABQ+c7wHgAy/9HLC5n6urqQWi1j2UV+q2D8HYlKGDhE6dht2uyarF58EiQiBI51Q== X-Bogosity: Ham, tests=bogofilter, spamicity=0.002062, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: On Thu, Nov 21, 2024 at 02:40:44PM +0100, David Hildenbrand wrote: > On 21.11.24 13:24, Zhen Ni wrote: > > 1.Removing redundant checks for current->mempolicy, with a more concise > > check order. > > > > 2.Using READ_ONCE(current->mempolicy) for safe, single access to > > current->mempolicy to prevent potential race conditions. > > > > 3.Optimizing the scope of task_lock(current). The lock now only protects > > the critical section where mempolicy is accessed, reducing the duration > > the lock is held. This enhances performance by limiting the scope of the > > lock to only what is necessary. > > While optimizing task lock scopes is admirable, we're talking about a scale of maybe a microsecond length access on a lock that's typically held quite a bit longer - if it's ever really held given the context of this function. What you'd actually want to do is the opposite: task_lock(current); mempolicy = get_mempolicy(); task_unlock(current); /* do work */ put_mempolicy(); but even this is only optimizing a small amount of work. And if you look at what init_nodemask_of_mempolicy is actually used for, there is no need for this optimization - the task is almost certainly short lived and intended to just adjust a system control value. > > Signed-off-by: Zhen Ni > > --- > > mm/mempolicy.c | 7 +++++-- > > 1 file changed, 5 insertions(+), 2 deletions(-) > > > > diff --git a/mm/mempolicy.c b/mm/mempolicy.c > > index b646fab3e45e..8bff8830b7e6 100644 > > --- a/mm/mempolicy.c > > +++ b/mm/mempolicy.c > > @@ -2132,11 +2132,14 @@ bool init_nodemask_of_mempolicy(nodemask_t *mask) > > { > > struct mempolicy *mempolicy; > > - if (!(mask && current->mempolicy)) > > + if (!mask)> + return false; > > + > > + mempolicy = READ_ONCE(current->mempolicy); > > + if (!mempolicy) > > return false; > > task_lock(current); > > - mempolicy = current->mempolicy; > > switch (mempolicy->mode) { > > case MPOL_PREFERRED: > > case MPOL_PREFERRED_MANY: > > (a) current->mempolicy can only change under the task_lock(), see > do_set_mempolicy(). > > (b) current->mempolicy cannot usually NULL once it was none-NULL. There are > two exceptions: > > copy_process() might set it to NULL when creating the process, before the > task gets scheduled. > > do_exit() might set it to NULL via mpol_put_task_policy(). > > I don't think init_nodemask_of_mempolicy() can be called while our task is > exiting ... > Unless someone put an access to nr_hugepages_mempolicy in the exit path, I doubt it (also that would probably deadlock :]) > > Now, if you read current->mempolicy outside of task_lock() to the > *dereference* it when it might already have changed+was freed, that's a BUG > you're introducing. > 100% a bug, although since the task mempolicy can't currently change outside the context of the task - I can't think of a a way poke this particular bug. ~Gregory