linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
From: David Hildenbrand <david@redhat.com>
To: Zhen Ni <zhen.ni@easystack.cn>, akpm@linux-foundation.org
Cc: linux-mm@kvack.org
Subject: Re: [PATCH] mm/mempolicy: Fix redundant check and refine lock protection scope in init_nodemask_of_mempolicy
Date: Thu, 21 Nov 2024 14:40:44 +0100	[thread overview]
Message-ID: <7f9c4c98-86bf-40a0-817a-858767946cb0@redhat.com> (raw)
In-Reply-To: <20241121122409.277927-1-zhen.ni@easystack.cn>

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.
> 
> Signed-off-by: Zhen Ni <zhen.ni@easystack.cn>
> ---
>   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 ...


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.


So, regarding your (1): which redundant "check" ? There is a single "not 
NULL" chech. Reagrding your (2), I think you are introducing a BUG and 
regrding your (3) please share performance numbers, or realize that youa 
re making something up ;)


The only improvement we could make here is converting:
	if (!(mask && current->mempolicy))
into a more readable
	if (!mask || !current->mapping)

And maybe adding a comment that while we can race with someone changing 
current->mapping, we cannot race with someone changing it to NULL.

-- 
Cheers,

David / dhildenb



  reply	other threads:[~2024-11-21 13:40 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-11-21 12:24 Zhen Ni
2024-11-21 13:40 ` David Hildenbrand [this message]
2024-11-21 14:41   ` Gregory Price
2024-11-22  3:54     ` zhen.ni

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=7f9c4c98-86bf-40a0-817a-858767946cb0@redhat.com \
    --to=david@redhat.com \
    --cc=akpm@linux-foundation.org \
    --cc=linux-mm@kvack.org \
    --cc=zhen.ni@easystack.cn \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox