linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 1/2] mm/mempolicy: Fix incorrect freeing of wi_kobj
@ 2025-06-02 16:23 Joshua Hahn
  2025-06-02 16:23 ` [PATCH 2/2] mm/mempolicy: Skip unnecessary synchronize_rcu() Joshua Hahn
  2025-06-02 16:53 ` [PATCH 1/2] mm/mempolicy: Fix incorrect freeing of wi_kobj Joshua Hahn
  0 siblings, 2 replies; 5+ messages in thread
From: Joshua Hahn @ 2025-06-02 16:23 UTC (permalink / raw)
  To: Andrew Morton, Gregory Price
  Cc: David Hildenbrand, Zi Yan, Matthew Brost, Rakie Kim,
	Byungchul Park, Ying Huang, Alistair Popple, linux-mm,
	linux-kernel, kernel-team, kernel test robot

We should not free wi_group->wi_kobj here. In the error path of
add_weighted_interleave_group() where this snippet is called from,
kobj_{del, put} is immediately called right after this section. Thus,
it is not only unnecessary but also incorrect to free it here. 

Signed-off-by: Joshua Hahn <joshua.hahnjy@gmail.com>
Reported-by: kernel test robot <lkp@intel.com>
Closes: https://lore.kernel.org/oe-kbuild-all/202506011545.Fduxqxqj-lkp@intel.com/
---
 mm/mempolicy.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)

diff --git a/mm/mempolicy.c b/mm/mempolicy.c
index 72fd72e156b1..3b1dfd08338b 100644
--- a/mm/mempolicy.c
+++ b/mm/mempolicy.c
@@ -3708,15 +3708,13 @@ static void wi_state_free(void)
 			lockdep_is_held(&wi_state_lock));
 	if (!old_wi_state) {
 		mutex_unlock(&wi_state_lock);
-		goto out;
+		return;
 	}
 
 	rcu_assign_pointer(wi_state, NULL);
 	mutex_unlock(&wi_state_lock);
 	synchronize_rcu();
 	kfree(old_wi_state);
-out:
-	kfree(&wi_group->wi_kobj);
 }
 
 static struct kobj_attribute wi_auto_attr =
-- 
2.47.1



^ permalink raw reply related	[flat|nested] 5+ messages in thread

* [PATCH 2/2] mm/mempolicy: Skip unnecessary synchronize_rcu()
  2025-06-02 16:23 [PATCH 1/2] mm/mempolicy: Fix incorrect freeing of wi_kobj Joshua Hahn
@ 2025-06-02 16:23 ` Joshua Hahn
  2025-06-03  2:10   ` Huang, Ying
  2025-06-02 16:53 ` [PATCH 1/2] mm/mempolicy: Fix incorrect freeing of wi_kobj Joshua Hahn
  1 sibling, 1 reply; 5+ messages in thread
From: Joshua Hahn @ 2025-06-02 16:23 UTC (permalink / raw)
  To: Andrew Morton, Gregory Price
  Cc: David Hildenbrand, Zi Yan, Matthew Brost, Rakie Kim,
	Byungchul Park, Ying Huang, Alistair Popple, linux-mm,
	linux-kernel, kernel-team

By unconditionally setting wi_state to NULL and conditionally calling
synchronize_rcu(), we can save an unncessary call when there is no
old_wi_state.

Suggested-by: David Hildenbrand <david@redhat.com>
Signed-off-by: Joshua Hahn <joshua.hahnjy@gmail.com>
---
 mm/mempolicy.c | 13 +++++--------
 1 file changed, 5 insertions(+), 8 deletions(-)

diff --git a/mm/mempolicy.c b/mm/mempolicy.c
index 3b1dfd08338b..b0619d0020c9 100644
--- a/mm/mempolicy.c
+++ b/mm/mempolicy.c
@@ -3703,18 +3703,15 @@ static void wi_state_free(void)
 	struct weighted_interleave_state *old_wi_state;
 
 	mutex_lock(&wi_state_lock);
-
 	old_wi_state = rcu_dereference_protected(wi_state,
 			lockdep_is_held(&wi_state_lock));
-	if (!old_wi_state) {
-		mutex_unlock(&wi_state_lock);
-		return;
-	}
-
 	rcu_assign_pointer(wi_state, NULL);
 	mutex_unlock(&wi_state_lock);
-	synchronize_rcu();
-	kfree(old_wi_state);
+
+	if (old_wi_state) {
+		synchronize_rcu();
+		kfree(old_wi_state);
+	}
 }
 
 static struct kobj_attribute wi_auto_attr =
-- 
2.47.1



^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH 1/2] mm/mempolicy: Fix incorrect freeing of wi_kobj
  2025-06-02 16:23 [PATCH 1/2] mm/mempolicy: Fix incorrect freeing of wi_kobj Joshua Hahn
  2025-06-02 16:23 ` [PATCH 2/2] mm/mempolicy: Skip unnecessary synchronize_rcu() Joshua Hahn
@ 2025-06-02 16:53 ` Joshua Hahn
  1 sibling, 0 replies; 5+ messages in thread
From: Joshua Hahn @ 2025-06-02 16:53 UTC (permalink / raw)
  To: Joshua Hahn
  Cc: Andrew Morton, Gregory Price, David Hildenbrand, Zi Yan,
	Matthew Brost, Rakie Kim, Byungchul Park, Ying Huang,
	Alistair Popple, linux-mm, linux-kernel, kernel-team,
	kernel test robot

On Mon,  2 Jun 2025 09:23:39 -0700 Joshua Hahn <joshua.hahnjy@gmail.com> wrote:

> We should not free wi_group->wi_kobj here. In the error path of
> add_weighted_interleave_group() where this snippet is called from,
> kobj_{del, put} is immediately called right after this section. Thus,
> it is not only unnecessary but also incorrect to free it here. 
> 
Fixes: e341f9c3c841 ("mm/mempolicy: Weighted Interleave Auto-tuning")
> Signed-off-by: Joshua Hahn <joshua.hahnjy@gmail.com>
> Reported-by: kernel test robot <lkp@intel.com>
> Closes: https://lore.kernel.org/oe-kbuild-all/202506011545.Fduxqxqj-lkp@intel.com/
> ---
>  mm/mempolicy.c | 4 +---
>  1 file changed, 1 insertion(+), 3 deletions(-)
> 
> diff --git a/mm/mempolicy.c b/mm/mempolicy.c
> index 72fd72e156b1..3b1dfd08338b 100644
> --- a/mm/mempolicy.c
> +++ b/mm/mempolicy.c
> @@ -3708,15 +3708,13 @@ static void wi_state_free(void)
>  			lockdep_is_held(&wi_state_lock));
>  	if (!old_wi_state) {
>  		mutex_unlock(&wi_state_lock);
> -		goto out;
> +		return;
>  	}
>  
>  	rcu_assign_pointer(wi_state, NULL);
>  	mutex_unlock(&wi_state_lock);
>  	synchronize_rcu();
>  	kfree(old_wi_state);
> -out:
> -	kfree(&wi_group->wi_kobj);
>  }
>  
>  static struct kobj_attribute wi_auto_attr =
> -- 
> 2.47.1


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH 2/2] mm/mempolicy: Skip unnecessary synchronize_rcu()
  2025-06-02 16:23 ` [PATCH 2/2] mm/mempolicy: Skip unnecessary synchronize_rcu() Joshua Hahn
@ 2025-06-03  2:10   ` Huang, Ying
  2025-06-03 14:31     ` Joshua Hahn
  0 siblings, 1 reply; 5+ messages in thread
From: Huang, Ying @ 2025-06-03  2:10 UTC (permalink / raw)
  To: Joshua Hahn
  Cc: Andrew Morton, Gregory Price, David Hildenbrand, Zi Yan,
	Matthew Brost, Rakie Kim, Byungchul Park, Alistair Popple,
	linux-mm, linux-kernel, kernel-team

Joshua Hahn <joshua.hahnjy@gmail.com> writes:

> By unconditionally setting wi_state to NULL and conditionally calling
> synchronize_rcu(), we can save an unncessary call when there is no
> old_wi_state.

Per my understanding, in the original code, if !old_wi_state, we will
return immediately instead of calling synchronize_rcu() too.  Or I miss
something?

The patch itself is a nice cleanup with reduced line number.  Feel free
to add my

Reviewed-by: Huang Ying <ying.huang@linux.alibaba.com>

in the future version.

> Suggested-by: David Hildenbrand <david@redhat.com>
> Signed-off-by: Joshua Hahn <joshua.hahnjy@gmail.com>
> ---
>  mm/mempolicy.c | 13 +++++--------
>  1 file changed, 5 insertions(+), 8 deletions(-)
>
> diff --git a/mm/mempolicy.c b/mm/mempolicy.c
> index 3b1dfd08338b..b0619d0020c9 100644
> --- a/mm/mempolicy.c
> +++ b/mm/mempolicy.c
> @@ -3703,18 +3703,15 @@ static void wi_state_free(void)
>  	struct weighted_interleave_state *old_wi_state;
>  
>  	mutex_lock(&wi_state_lock);
> -
>  	old_wi_state = rcu_dereference_protected(wi_state,
>  			lockdep_is_held(&wi_state_lock));
> -	if (!old_wi_state) {
> -		mutex_unlock(&wi_state_lock);
> -		return;
> -	}
> -
>  	rcu_assign_pointer(wi_state, NULL);
>  	mutex_unlock(&wi_state_lock);
> -	synchronize_rcu();
> -	kfree(old_wi_state);
> +
> +	if (old_wi_state) {
> +		synchronize_rcu();
> +		kfree(old_wi_state);
> +	}
>  }
>  
>  static struct kobj_attribute wi_auto_attr =

---
Best Regards,
Huang, Ying


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH 2/2] mm/mempolicy: Skip unnecessary synchronize_rcu()
  2025-06-03  2:10   ` Huang, Ying
@ 2025-06-03 14:31     ` Joshua Hahn
  0 siblings, 0 replies; 5+ messages in thread
From: Joshua Hahn @ 2025-06-03 14:31 UTC (permalink / raw)
  To: Huang, Ying
  Cc: Andrew Morton, Gregory Price, David Hildenbrand, Zi Yan,
	Matthew Brost, Rakie Kim, Byungchul Park, Alistair Popple,
	linux-mm, linux-kernel, kernel-team

On Tue, 03 Jun 2025 10:10:46 +0800 "Huang, Ying" <ying.huang@linux.alibaba.com> wrote:

> Joshua Hahn <joshua.hahnjy@gmail.com> writes:
> 
> > By unconditionally setting wi_state to NULL and conditionally calling
> > synchronize_rcu(), we can save an unncessary call when there is no
> > old_wi_state.
> 
> Per my understanding, in the original code, if !old_wi_state, we will
> return immediately instead of calling synchronize_rcu() too.  Or I miss
> something?
> 
> The patch itself is a nice cleanup with reduced line number.  Feel free
> to add my
> 
> Reviewed-by: Huang Ying <ying.huang@linux.alibaba.com>

Hi Ying, you are correct. I was thinking in my mind to write "save an
unnecessary goto" but my mind must have slipped. Thank you for the catch,
and thank you for reviewing always. Have a great day!
Joshua

> in the future version.
> 
> > Suggested-by: David Hildenbrand <david@redhat.com>
> > Signed-off-by: Joshua Hahn <joshua.hahnjy@gmail.com>
> > ---
> >  mm/mempolicy.c | 13 +++++--------
> >  1 file changed, 5 insertions(+), 8 deletions(-)
> >
> > diff --git a/mm/mempolicy.c b/mm/mempolicy.c
> > index 3b1dfd08338b..b0619d0020c9 100644
> > --- a/mm/mempolicy.c
> > +++ b/mm/mempolicy.c
> > @@ -3703,18 +3703,15 @@ static void wi_state_free(void)
> >  	struct weighted_interleave_state *old_wi_state;
> >  
> >  	mutex_lock(&wi_state_lock);
> > -
> >  	old_wi_state = rcu_dereference_protected(wi_state,
> >  			lockdep_is_held(&wi_state_lock));
> > -	if (!old_wi_state) {
> > -		mutex_unlock(&wi_state_lock);
> > -		return;
> > -	}
> > -
> >  	rcu_assign_pointer(wi_state, NULL);
> >  	mutex_unlock(&wi_state_lock);
> > -	synchronize_rcu();
> > -	kfree(old_wi_state);
> > +
> > +	if (old_wi_state) {
> > +		synchronize_rcu();
> > +		kfree(old_wi_state);
> > +	}
> >  }
> >  
> >  static struct kobj_attribute wi_auto_attr =
> 
> ---
> Best Regards,
> Huang, Ying


^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2025-06-03 14:31 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-06-02 16:23 [PATCH 1/2] mm/mempolicy: Fix incorrect freeing of wi_kobj Joshua Hahn
2025-06-02 16:23 ` [PATCH 2/2] mm/mempolicy: Skip unnecessary synchronize_rcu() Joshua Hahn
2025-06-03  2:10   ` Huang, Ying
2025-06-03 14:31     ` Joshua Hahn
2025-06-02 16:53 ` [PATCH 1/2] mm/mempolicy: Fix incorrect freeing of wi_kobj Joshua Hahn

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).