diff mbox series

[v2] mm/slub: Simplify get_partial_node()

Message ID 20240312140532.64124-1-sxwjean@me.com (mailing list archive)
State New
Headers show
Series [v2] mm/slub: Simplify get_partial_node() | expand

Commit Message

Xiongwei Song March 12, 2024, 2:05 p.m. UTC
From: Xiongwei Song <xiongwei.song@windriver.com>

Remove the check of !kmem_cache_has_cpu_partial() because it is always
false, we've known this by calling kmem_cache_debug() before calling
remove_partial(), so we can remove the check.

Meanwhile, redo filling cpu partial and add comment to improve the
readability.

Signed-off-by: Xiongwei Song <xiongwei.song@windriver.com>
---
Changes in v2:
 - Use "#if IS_ENABLED(CONFIG_SLUB_CPU_PARTIAL)" to instead 
   "if (IS_ENABLED(CONFIG_SLUB_CPU_PARTIAL))" to fix build error.
   (Thanks Chengming Zhou)
 - Add __maybe_unused for partial_slabs to prevent compiler warning.

v1: 
 https://lore.kernel.org/linux-kernel/20240311132720.37741-1-sxwjean@me.com/T/
---
 mm/slub.c | 24 +++++++++++++-----------
 1 file changed, 13 insertions(+), 11 deletions(-)

Comments

Chengming Zhou March 12, 2024, 3:22 p.m. UTC | #1
On 2024/3/12 22:05, sxwjean@me.com wrote:
> From: Xiongwei Song <xiongwei.song@windriver.com>
> 
> Remove the check of !kmem_cache_has_cpu_partial() because it is always
> false, we've known this by calling kmem_cache_debug() before calling
> remove_partial(), so we can remove the check.
> 
> Meanwhile, redo filling cpu partial and add comment to improve the
> readability.
> 
> Signed-off-by: Xiongwei Song <xiongwei.song@windriver.com>
> ---
> Changes in v2:
>  - Use "#if IS_ENABLED(CONFIG_SLUB_CPU_PARTIAL)" to instead 
>    "if (IS_ENABLED(CONFIG_SLUB_CPU_PARTIAL))" to fix build error.
>    (Thanks Chengming Zhou)
>  - Add __maybe_unused for partial_slabs to prevent compiler warning.
> 
> v1: 
>  https://lore.kernel.org/linux-kernel/20240311132720.37741-1-sxwjean@me.com/T/
> ---
>  mm/slub.c | 24 +++++++++++++-----------
>  1 file changed, 13 insertions(+), 11 deletions(-)
> 
> diff --git a/mm/slub.c b/mm/slub.c
> index a3ab096c38c0..ab526960ee5b 100644
> --- a/mm/slub.c
> +++ b/mm/slub.c
> @@ -2588,7 +2588,7 @@ static struct slab *get_partial_node(struct kmem_cache *s,
>  {
>  	struct slab *slab, *slab2, *partial = NULL;
>  	unsigned long flags;
> -	unsigned int partial_slabs = 0;
> +	unsigned int __maybe_unused partial_slabs = 0;
>  
>  	/*
>  	 * Racy check. If we mistakenly see no partial slabs then we
> @@ -2620,19 +2620,21 @@ static struct slab *get_partial_node(struct kmem_cache *s,
>  		if (!partial) {
>  			partial = slab;
>  			stat(s, ALLOC_FROM_PARTIAL);
> -		} else {
> -			put_cpu_partial(s, slab, 0);
> -			stat(s, CPU_PARTIAL_NODE);
> -			partial_slabs++;
> +
> +			/* Fill cpu partial if needed from next iteration, or break */
> +			if (IS_ENABLED(CONFIG_SLUB_CPU_PARTIAL))
> +				continue;
> +			else
> +				break;
>  		}
> -#ifdef CONFIG_SLUB_CPU_PARTIAL
> -		if (!kmem_cache_has_cpu_partial(s)
> -			|| partial_slabs > s->cpu_partial_slabs / 2)
> +
> +#if IS_ENABLED(CONFIG_SLUB_CPU_PARTIAL)

Hmm, these two CONFIG_SLUB_CPU_PARTIAL look verbose to me :(

How about using just one, maybe like this?

diff --git a/mm/slub.c b/mm/slub.c
index 2ef88bbf56a3..a018c715b648 100644
--- a/mm/slub.c
+++ b/mm/slub.c
@@ -2620,19 +2620,16 @@ static struct slab *get_partial_node(struct kmem_cache *s,
 		if (!partial) {
 			partial = slab;
 			stat(s, ALLOC_FROM_PARTIAL);
+#ifdef CONFIG_SLUB_CPU_PARTIAL
 		} else {
 			put_cpu_partial(s, slab, 0);
 			stat(s, CPU_PARTIAL_NODE);
-			partial_slabs++;
-		}
-#ifdef CONFIG_SLUB_CPU_PARTIAL
-		if (!kmem_cache_has_cpu_partial(s)
-			|| partial_slabs > s->cpu_partial_slabs / 2)
-			break;
+			if (++partial_slabs > s->cpu_partial_slabs / 2)
+				break;
 #else
-		break;
+			break;
 #endif
-
+		}
 	}
 	spin_unlock_irqrestore(&n->list_lock, flags);
 	return partial;


> +		put_cpu_partial(s, slab, 0);
> +		stat(s, CPU_PARTIAL_NODE);
> +
> +		if (++partial_slabs > s->cpu_partial_slabs/2)
>  			break;
> -#else
> -		break;
>  #endif
> -
>  	}
>  	spin_unlock_irqrestore(&n->list_lock, flags);
>  	return partial;
Song, Xiongwei March 13, 2024, 6:15 a.m. UTC | #2
> On 2024/3/12 22:05, sxwjean@me.com wrote:
> > From: Xiongwei Song <xiongwei.song@windriver.com>
> >
> > Remove the check of !kmem_cache_has_cpu_partial() because it is always
> > false, we've known this by calling kmem_cache_debug() before calling
> > remove_partial(), so we can remove the check.
> >
> > Meanwhile, redo filling cpu partial and add comment to improve the
> > readability.
> >
> > Signed-off-by: Xiongwei Song <xiongwei.song@windriver.com>
> > ---
> > Changes in v2:
> >  - Use "#if IS_ENABLED(CONFIG_SLUB_CPU_PARTIAL)" to instead
> >    "if (IS_ENABLED(CONFIG_SLUB_CPU_PARTIAL))" to fix build error.
> >    (Thanks Chengming Zhou)
> >  - Add __maybe_unused for partial_slabs to prevent compiler warning.
> >
> > v1:
> >  https://lore.kernel.org/linux-kernel/20240311132720.37741-1-sxwjean@me.com/T/
> > ---
> >  mm/slub.c | 24 +++++++++++++-----------
> >  1 file changed, 13 insertions(+), 11 deletions(-)
> >
> > diff --git a/mm/slub.c b/mm/slub.c
> > index a3ab096c38c0..ab526960ee5b 100644
> > --- a/mm/slub.c
> > +++ b/mm/slub.c
> > @@ -2588,7 +2588,7 @@ static struct slab *get_partial_node(struct kmem_cache *s,
> >  {
> >       struct slab *slab, *slab2, *partial = NULL;
> >       unsigned long flags;
> > -     unsigned int partial_slabs = 0;
> > +     unsigned int __maybe_unused partial_slabs = 0;
> >
> >       /*
> >        * Racy check. If we mistakenly see no partial slabs then we
> > @@ -2620,19 +2620,21 @@ static struct slab *get_partial_node(struct kmem_cache *s,
> >               if (!partial) {
> >                       partial = slab;
> >                       stat(s, ALLOC_FROM_PARTIAL);
> > -             } else {
> > -                     put_cpu_partial(s, slab, 0);
> > -                     stat(s, CPU_PARTIAL_NODE);
> > -                     partial_slabs++;
> > +
> > +                     /* Fill cpu partial if needed from next iteration, or break */
> > +                     if (IS_ENABLED(CONFIG_SLUB_CPU_PARTIAL))
> > +                             continue;
> > +                     else
> > +                             break;
> >               }
> > -#ifdef CONFIG_SLUB_CPU_PARTIAL
> > -             if (!kmem_cache_has_cpu_partial(s)
> > -                     || partial_slabs > s->cpu_partial_slabs / 2)
> > +
> > +#if IS_ENABLED(CONFIG_SLUB_CPU_PARTIAL)
> 
> Hmm, these two CONFIG_SLUB_CPU_PARTIAL look verbose to me :(
> 
> How about using just one, maybe like this?
> 
> diff --git a/mm/slub.c b/mm/slub.c
> index 2ef88bbf56a3..a018c715b648 100644
> --- a/mm/slub.c
> +++ b/mm/slub.c
> @@ -2620,19 +2620,16 @@ static struct slab *get_partial_node(struct kmem_cache *s,
>                 if (!partial) {
>                         partial = slab;
>                         stat(s, ALLOC_FROM_PARTIAL);
> +#ifdef CONFIG_SLUB_CPU_PARTIAL
>                 } else {

I don't like splitting  "if...else..." up with preprocessor directives personally. It's messy for me.
David Rientjes March 13, 2024, 11:36 p.m. UTC | #3
On Wed, 13 Mar 2024, Song, Xiongwei wrote:

> > On 2024/3/12 22:05, sxwjean@me.com wrote:
> > > From: Xiongwei Song <xiongwei.song@windriver.com>
> > >
> > > Remove the check of !kmem_cache_has_cpu_partial() because it is always
> > > false, we've known this by calling kmem_cache_debug() before calling
> > > remove_partial(), so we can remove the check.
> > >
> > > Meanwhile, redo filling cpu partial and add comment to improve the
> > > readability.
> > >
> > > Signed-off-by: Xiongwei Song <xiongwei.song@windriver.com>
> > > ---
> > > Changes in v2:
> > >  - Use "#if IS_ENABLED(CONFIG_SLUB_CPU_PARTIAL)" to instead
> > >    "if (IS_ENABLED(CONFIG_SLUB_CPU_PARTIAL))" to fix build error.
> > >    (Thanks Chengming Zhou)
> > >  - Add __maybe_unused for partial_slabs to prevent compiler warning.
> > >
> > > v1:
> > >  https://lore.kernel.org/linux-kernel/20240311132720.37741-1-sxwjean@me.com/T/
> > > ---
> > >  mm/slub.c | 24 +++++++++++++-----------
> > >  1 file changed, 13 insertions(+), 11 deletions(-)
> > >
> > > diff --git a/mm/slub.c b/mm/slub.c
> > > index a3ab096c38c0..ab526960ee5b 100644
> > > --- a/mm/slub.c
> > > +++ b/mm/slub.c
> > > @@ -2588,7 +2588,7 @@ static struct slab *get_partial_node(struct kmem_cache *s,
> > >  {
> > >       struct slab *slab, *slab2, *partial = NULL;
> > >       unsigned long flags;
> > > -     unsigned int partial_slabs = 0;
> > > +     unsigned int __maybe_unused partial_slabs = 0;
> > >
> > >       /*
> > >        * Racy check. If we mistakenly see no partial slabs then we
> > > @@ -2620,19 +2620,21 @@ static struct slab *get_partial_node(struct kmem_cache *s,
> > >               if (!partial) {
> > >                       partial = slab;
> > >                       stat(s, ALLOC_FROM_PARTIAL);
> > > -             } else {
> > > -                     put_cpu_partial(s, slab, 0);
> > > -                     stat(s, CPU_PARTIAL_NODE);
> > > -                     partial_slabs++;
> > > +
> > > +                     /* Fill cpu partial if needed from next iteration, or break */
> > > +                     if (IS_ENABLED(CONFIG_SLUB_CPU_PARTIAL))
> > > +                             continue;
> > > +                     else
> > > +                             break;
> > >               }
> > > -#ifdef CONFIG_SLUB_CPU_PARTIAL
> > > -             if (!kmem_cache_has_cpu_partial(s)
> > > -                     || partial_slabs > s->cpu_partial_slabs / 2)
> > > +
> > > +#if IS_ENABLED(CONFIG_SLUB_CPU_PARTIAL)
> > 
> > Hmm, these two CONFIG_SLUB_CPU_PARTIAL look verbose to me :(
> > 
> > How about using just one, maybe like this?
> > 
> > diff --git a/mm/slub.c b/mm/slub.c
> > index 2ef88bbf56a3..a018c715b648 100644
> > --- a/mm/slub.c
> > +++ b/mm/slub.c
> > @@ -2620,19 +2620,16 @@ static struct slab *get_partial_node(struct kmem_cache *s,
> >                 if (!partial) {
> >                         partial = slab;
> >                         stat(s, ALLOC_FROM_PARTIAL);
> > +#ifdef CONFIG_SLUB_CPU_PARTIAL
> >                 } else {
> 
> I don't like splitting  "if...else..." up with preprocessor directives personally. It's messy for me.
> 

I would agree, but the flow here actually seems like it's easier to follow 
if we are checking for CONFIG_SLUB_CPU_PARTIAL in multiple places :/
diff mbox series

Patch

diff --git a/mm/slub.c b/mm/slub.c
index a3ab096c38c0..ab526960ee5b 100644
--- a/mm/slub.c
+++ b/mm/slub.c
@@ -2588,7 +2588,7 @@  static struct slab *get_partial_node(struct kmem_cache *s,
 {
 	struct slab *slab, *slab2, *partial = NULL;
 	unsigned long flags;
-	unsigned int partial_slabs = 0;
+	unsigned int __maybe_unused partial_slabs = 0;
 
 	/*
 	 * Racy check. If we mistakenly see no partial slabs then we
@@ -2620,19 +2620,21 @@  static struct slab *get_partial_node(struct kmem_cache *s,
 		if (!partial) {
 			partial = slab;
 			stat(s, ALLOC_FROM_PARTIAL);
-		} else {
-			put_cpu_partial(s, slab, 0);
-			stat(s, CPU_PARTIAL_NODE);
-			partial_slabs++;
+
+			/* Fill cpu partial if needed from next iteration, or break */
+			if (IS_ENABLED(CONFIG_SLUB_CPU_PARTIAL))
+				continue;
+			else
+				break;
 		}
-#ifdef CONFIG_SLUB_CPU_PARTIAL
-		if (!kmem_cache_has_cpu_partial(s)
-			|| partial_slabs > s->cpu_partial_slabs / 2)
+
+#if IS_ENABLED(CONFIG_SLUB_CPU_PARTIAL)
+		put_cpu_partial(s, slab, 0);
+		stat(s, CPU_PARTIAL_NODE);
+
+		if (++partial_slabs > s->cpu_partial_slabs/2)
 			break;
-#else
-		break;
 #endif
-
 	}
 	spin_unlock_irqrestore(&n->list_lock, flags);
 	return partial;