All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v2] mm/slub: Remove repeated action in calculate_order()
@ 2022-04-16  7:40 Wonhyuk Yang
  2022-04-17  1:43 ` Hyeonggon Yoo
  0 siblings, 1 reply; 5+ messages in thread
From: Wonhyuk Yang @ 2022-04-16  7:40 UTC (permalink / raw)
  To: Christoph Lameter, Pekka Enberg, David Rientjes, Joonsoo Kim,
	Andrew Morton, Vlastimil Babka, Roman Gushchin
  Cc: Ohhoon Kwon, JaeSang Yoo, Wonhyuk Yang, Jiyoup Kim,
	Donghyeok Kim, linux-mm, linux-kernel

To calculate order, calc_slab_order() is called repeatly changing the
fract_leftover. Thus, the branch which is not dependent on
fract_leftover is executed repeatly. So make it run only once.

Plus, when min_object reached to 0, we set fract_leftover to 1. In
this case, we can calculate order by max(slub_min_order,
get_order(size)) instead of calling calc_slab_order().

No functional impact expected.

Signed-off-by: Wonhyuk Yang <vvghjk1234@gmail.com>
---
V1 -> V2: Fix typo miss in a commit message

 mm/slub.c | 18 +++++++-----------
 1 file changed, 7 insertions(+), 11 deletions(-)

diff --git a/mm/slub.c b/mm/slub.c
index ed5c2c03a47a..e7a394d7b75a 100644
--- a/mm/slub.c
+++ b/mm/slub.c
@@ -3795,9 +3795,6 @@ static inline unsigned int calc_slab_order(unsigned int size,
 	unsigned int min_order = slub_min_order;
 	unsigned int order;
 
-	if (order_objects(min_order, size) > MAX_OBJS_PER_PAGE)
-		return get_order(size * MAX_OBJS_PER_PAGE) - 1;
-
 	for (order = max(min_order, (unsigned int)get_order(min_objects * size));
 			order <= max_order; order++) {
 
@@ -3820,6 +3817,11 @@ static inline int calculate_order(unsigned int size)
 	unsigned int max_objects;
 	unsigned int nr_cpus;
 
+	if (unlikely(order_objects(slub_min_order, size) > MAX_OBJS_PER_PAGE)) {
+		order = get_order(size * MAX_OBJS_PER_PAGE) - 1;
+		goto out;
+	}
+
 	/*
 	 * Attempt to find best configuration for a slab. This
 	 * works by first attempting to generate a layout with
@@ -3865,14 +3867,8 @@ static inline int calculate_order(unsigned int size)
 	 * We were unable to place multiple objects in a slab. Now
 	 * lets see if we can place a single object there.
 	 */
-	order = calc_slab_order(size, 1, slub_max_order, 1);
-	if (order <= slub_max_order)
-		return order;
-
-	/*
-	 * Doh this slab cannot be placed using slub_max_order.
-	 */
-	order = calc_slab_order(size, 1, MAX_ORDER, 1);
+	order = max_t(unsigned int, slub_min_order, (unsigned int)get_order(size));
+out:
 	if (order < MAX_ORDER)
 		return order;
 	return -ENOSYS;
-- 
2.30.2


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

* Re: [PATCH v2] mm/slub: Remove repeated action in calculate_order()
  2022-04-16  7:40 [PATCH v2] mm/slub: Remove repeated action in calculate_order() Wonhyuk Yang
@ 2022-04-17  1:43 ` Hyeonggon Yoo
  2022-04-17  2:07   ` Hyeonggon Yoo
  2022-04-17  6:17   ` Wonhyuk Yang
  0 siblings, 2 replies; 5+ messages in thread
From: Hyeonggon Yoo @ 2022-04-17  1:43 UTC (permalink / raw)
  To: Wonhyuk Yang
  Cc: Christoph Lameter, Pekka Enberg, David Rientjes, Joonsoo Kim,
	Andrew Morton, Vlastimil Babka, Roman Gushchin, Ohhoon Kwon,
	JaeSang Yoo, Jiyoup Kim, Donghyeok Kim, linux-mm, linux-kernel

On Sat, Apr 16, 2022 at 04:40:59PM +0900, Wonhyuk Yang wrote:
> To calculate order, calc_slab_order() is called repeatly changing the
> fract_leftover. Thus, the branch which is not dependent on
> fract_leftover is executed repeatly. So make it run only once.
> 
> Plus, when min_object reached to 0, we set fract_leftover to 1. In

Maybe you mean when min_object reached 1.

> this case, we can calculate order by max(slub_min_order,
> get_order(size)) instead of calling calc_slab_order().
> 
> No functional impact expected.
> Signed-off-by: Wonhyuk Yang <vvghjk1234@gmail.com>
> ---
> V1 -> V2: Fix typo miss in a commit message
> 
>  mm/slub.c | 18 +++++++-----------
>  1 file changed, 7 insertions(+), 11 deletions(-)
> 
> diff --git a/mm/slub.c b/mm/slub.c
> index ed5c2c03a47a..e7a394d7b75a 100644
> --- a/mm/slub.c
> +++ b/mm/slub.c
> @@ -3795,9 +3795,6 @@ static inline unsigned int calc_slab_order(unsigned int size,
>  	unsigned int min_order = slub_min_order;
>  	unsigned int order;
>  
> -	if (order_objects(min_order, size) > MAX_OBJS_PER_PAGE)
> -		return get_order(size * MAX_OBJS_PER_PAGE) - 1;
> -
>  	for (order = max(min_order, (unsigned int)get_order(min_objects * size));
>  			order <= max_order; order++) {
>  
> @@ -3820,6 +3817,11 @@ static inline int calculate_order(unsigned int size)
>  	unsigned int max_objects;
>  	unsigned int nr_cpus;
>  
> +	if (unlikely(order_objects(slub_min_order, size) > MAX_OBJS_PER_PAGE)) {
> +		order = get_order(size * MAX_OBJS_PER_PAGE) - 1;
> +		goto out;
> +	}
> +
>  	/*
>  	 * Attempt to find best configuration for a slab. This
>  	 * works by first attempting to generate a layout with
> @@ -3865,14 +3867,8 @@ static inline int calculate_order(unsigned int size)
>  	 * We were unable to place multiple objects in a slab. Now
>  	 * lets see if we can place a single object there.
>  	 */
> -	order = calc_slab_order(size, 1, slub_max_order, 1);
> -	if (order <= slub_max_order)
> -		return order;
> -
> -	/*
> -	 * Doh this slab cannot be placed using slub_max_order.
> -	 */
> -	order = calc_slab_order(size, 1, MAX_ORDER, 1);
> +	order = max_t(unsigned int, slub_min_order, (unsigned int)get_order(size));
> +out:

You don't need to cast value of get_order(size). max_t() does cast both operands.

>  	if (order < MAX_ORDER)
>  		return order;
>  	return -ENOSYS;

For the correctness of the patch, I don't see any problem about the
code.

But to be honest I'm a bit skeptical about saving some cycles in
calculating slab order. It's done only when creating caches (usually in boot
process).

> -- 
> 2.30.2
> 
> 

-- 
Thanks,
Hyeonggon

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

* Re: [PATCH v2] mm/slub: Remove repeated action in calculate_order()
  2022-04-17  1:43 ` Hyeonggon Yoo
@ 2022-04-17  2:07   ` Hyeonggon Yoo
  2022-04-17  6:21     ` Wonhyuk Yang
  2022-04-17  6:17   ` Wonhyuk Yang
  1 sibling, 1 reply; 5+ messages in thread
From: Hyeonggon Yoo @ 2022-04-17  2:07 UTC (permalink / raw)
  To: Wonhyuk Yang
  Cc: Christoph Lameter, Pekka Enberg, David Rientjes, Joonsoo Kim,
	Andrew Morton, Vlastimil Babka, Roman Gushchin, Ohhoon Kwon,
	JaeSang Yoo, Jiyoup Kim, Donghyeok Kim, linux-mm, linux-kernel

On Sun, Apr 17, 2022 at 10:43:20AM +0900, Hyeonggon Yoo wrote:
> On Sat, Apr 16, 2022 at 04:40:59PM +0900, Wonhyuk Yang wrote:
> > To calculate order, calc_slab_order() is called repeatly changing the
> > fract_leftover. Thus, the branch which is not dependent on
> > fract_leftover is executed repeatly. So make it run only once.
> > 
> > Plus, when min_object reached to 0, we set fract_leftover to 1. In
> 
> Maybe you mean when min_object reached 1.
> 
> > this case, we can calculate order by max(slub_min_order,
> > get_order(size)) instead of calling calc_slab_order().
> > 
> > No functional impact expected.
> > Signed-off-by: Wonhyuk Yang <vvghjk1234@gmail.com>
> > ---
> > V1 -> V2: Fix typo miss in a commit message
> > 
> >  mm/slub.c | 18 +++++++-----------
> >  1 file changed, 7 insertions(+), 11 deletions(-)
> > 
> > diff --git a/mm/slub.c b/mm/slub.c
> > index ed5c2c03a47a..e7a394d7b75a 100644
> > --- a/mm/slub.c
> > +++ b/mm/slub.c
> > @@ -3795,9 +3795,6 @@ static inline unsigned int calc_slab_order(unsigned int size,
> >  	unsigned int min_order = slub_min_order;
> >  	unsigned int order;
> >  
> > -	if (order_objects(min_order, size) > MAX_OBJS_PER_PAGE)
> > -		return get_order(size * MAX_OBJS_PER_PAGE) - 1;
> > -
> >  	for (order = max(min_order, (unsigned int)get_order(min_objects * size));
> >  			order <= max_order; order++) {
> >  
> > @@ -3820,6 +3817,11 @@ static inline int calculate_order(unsigned int size)
> >  	unsigned int max_objects;
> >  	unsigned int nr_cpus;
> >  
> > +	if (unlikely(order_objects(slub_min_order, size) > MAX_OBJS_PER_PAGE)) {
> > +		order = get_order(size * MAX_OBJS_PER_PAGE) - 1;
> > +		goto out;
> > +	}
> > +
> >  	/*
> >  	 * Attempt to find best configuration for a slab. This
> >  	 * works by first attempting to generate a layout with
> > @@ -3865,14 +3867,8 @@ static inline int calculate_order(unsigned int size)
> >  	 * We were unable to place multiple objects in a slab. Now
> >  	 * lets see if we can place a single object there.
> >  	 */
> > -	order = calc_slab_order(size, 1, slub_max_order, 1);
> > -	if (order <= slub_max_order)
> > -		return order;
> > -
> > -	/*
> > -	 * Doh this slab cannot be placed using slub_max_order.
> > -	 */
> > -	order = calc_slab_order(size, 1, MAX_ORDER, 1);
> > +	order = max_t(unsigned int, slub_min_order, (unsigned int)get_order(size));
> > +out:
> 
> You don't need to cast value of get_order(size). max_t() does cast both operands.
> 
> >  	if (order < MAX_ORDER)
> >  		return order;
> >  	return -ENOSYS;
> 
> For the correctness of the patch, I don't see any problem about the
> code.
> 
> But to be honest I'm a bit skeptical about saving some cycles in
> calculating slab order. It's done only when creating caches (usually in boot
> process).

But yeah, maybe it's worth for better maintenance of code.

So after considering my comments, feel free to add:
Reviewed-by: Hyeonggon Yoo <42.hyeyoo@gmail.com>

Thanks!

> > -- 
> > 2.30.2
> > 
> > 
> 
> -- 
> Thanks,
> Hyeonggon

-- 
Thanks,
Hyeonggon

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

* Re: [PATCH v2] mm/slub: Remove repeated action in calculate_order()
  2022-04-17  1:43 ` Hyeonggon Yoo
  2022-04-17  2:07   ` Hyeonggon Yoo
@ 2022-04-17  6:17   ` Wonhyuk Yang
  1 sibling, 0 replies; 5+ messages in thread
From: Wonhyuk Yang @ 2022-04-17  6:17 UTC (permalink / raw)
  To: Hyeonggon Yoo
  Cc: Christoph Lameter, Pekka Enberg, David Rientjes, Joonsoo Kim,
	Andrew Morton, Vlastimil Babka, Roman Gushchin, Ohhoon Kwon,
	JaeSang Yoo, Jiyoup Kim, Donghyeok Kim, linux-mm, linux-kernel

On Sun, Apr 17, 2022 at 10:43 AM Hyeonggon Yoo <42.hyeyoo@gmail.com> wrote:
>
> On Sat, Apr 16, 2022 at 04:40:59PM +0900, Wonhyuk Yang wrote:
> > To calculate order, calc_slab_order() is called repeatly changing the
> > fract_leftover. Thus, the branch which is not dependent on
> > fract_leftover is executed repeatly. So make it run only once.
> >
> > Plus, when min_object reached to 0, we set fract_leftover to 1. In
>
> Maybe you mean when min_object reached 1.

Yes, That comment need to be updated...

>
> > this case, we can calculate order by max(slub_min_order,
> > get_order(size)) instead of calling calc_slab_order().
> >
> > No functional impact expected.
> > Signed-off-by: Wonhyuk Yang <vvghjk1234@gmail.com>
> > ---
> > V1 -> V2: Fix typo miss in a commit message
> >
> >  mm/slub.c | 18 +++++++-----------
> >  1 file changed, 7 insertions(+), 11 deletions(-)
> >
> > diff --git a/mm/slub.c b/mm/slub.c
> > index ed5c2c03a47a..e7a394d7b75a 100644
> > --- a/mm/slub.c
> > +++ b/mm/slub.c
> > @@ -3795,9 +3795,6 @@ static inline unsigned int calc_slab_order(unsigned int size,
> >       unsigned int min_order = slub_min_order;
> >       unsigned int order;
> >
> > -     if (order_objects(min_order, size) > MAX_OBJS_PER_PAGE)
> > -             return get_order(size * MAX_OBJS_PER_PAGE) - 1;
> > -
> >       for (order = max(min_order, (unsigned int)get_order(min_objects * size));
> >                       order <= max_order; order++) {
> >
> > @@ -3820,6 +3817,11 @@ static inline int calculate_order(unsigned int size)
> >       unsigned int max_objects;
> >       unsigned int nr_cpus;
> >
> > +     if (unlikely(order_objects(slub_min_order, size) > MAX_OBJS_PER_PAGE)) {
> > +             order = get_order(size * MAX_OBJS_PER_PAGE) - 1;
> > +             goto out;
> > +     }
> > +
> >       /*
> >        * Attempt to find best configuration for a slab. This
> >        * works by first attempting to generate a layout with
> > @@ -3865,14 +3867,8 @@ static inline int calculate_order(unsigned int size)
> >        * We were unable to place multiple objects in a slab. Now
> >        * lets see if we can place a single object there.
> >        */
> > -     order = calc_slab_order(size, 1, slub_max_order, 1);
> > -     if (order <= slub_max_order)
> > -             return order;
> > -
> > -     /*
> > -      * Doh this slab cannot be placed using slub_max_order.
> > -      */
> > -     order = calc_slab_order(size, 1, MAX_ORDER, 1);
> > +     order = max_t(unsigned int, slub_min_order, (unsigned int)get_order(size));
> > +out:
>
> You don't need to cast value of get_order(size). max_t() does cast both operands.

That's a good point, I will delete it.

>
> >       if (order < MAX_ORDER)
> >               return order;
> >       return -ENOSYS;
>
> For the correctness of the patch, I don't see any problem about the
> code.
>
> But to be honest I'm a bit skeptical about saving some cycles in
> calculating slab order. It's done only when creating caches (usually in boot
> process).
>

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

* Re: [PATCH v2] mm/slub: Remove repeated action in calculate_order()
  2022-04-17  2:07   ` Hyeonggon Yoo
@ 2022-04-17  6:21     ` Wonhyuk Yang
  0 siblings, 0 replies; 5+ messages in thread
From: Wonhyuk Yang @ 2022-04-17  6:21 UTC (permalink / raw)
  To: Hyeonggon Yoo
  Cc: Christoph Lameter, Pekka Enberg, David Rientjes, Joonsoo Kim,
	Andrew Morton, Vlastimil Babka, Roman Gushchin, Ohhoon Kwon,
	JaeSang Yoo, Jiyoup Kim, Donghyeok Kim, linux-mm, linux-kernel

On Sun, Apr 17, 2022 at 11:07 AM Hyeonggon Yoo <42.hyeyoo@gmail.com> wrote:
>
> On Sun, Apr 17, 2022 at 10:43:20AM +0900, Hyeonggon Yoo wrote:
> > On Sat, Apr 16, 2022 at 04:40:59PM +0900, Wonhyuk Yang wrote:
> > > To calculate order, calc_slab_order() is called repeatly changing the
> > > fract_leftover. Thus, the branch which is not dependent on
> > > fract_leftover is executed repeatly. So make it run only once.
> > >
> > > Plus, when min_object reached to 0, we set fract_leftover to 1. In
> >
> > Maybe you mean when min_object reached 1.
> >
> > > this case, we can calculate order by max(slub_min_order,
> > > get_order(size)) instead of calling calc_slab_order().
> > >
> > > No functional impact expected.
> > > Signed-off-by: Wonhyuk Yang <vvghjk1234@gmail.com>
> > > ---
> > > V1 -> V2: Fix typo miss in a commit message
> > >
> > >  mm/slub.c | 18 +++++++-----------
> > >  1 file changed, 7 insertions(+), 11 deletions(-)
> > >
> > > diff --git a/mm/slub.c b/mm/slub.c
> > > index ed5c2c03a47a..e7a394d7b75a 100644
> > > --- a/mm/slub.c
> > > +++ b/mm/slub.c
> > > @@ -3795,9 +3795,6 @@ static inline unsigned int calc_slab_order(unsigned int size,
> > >     unsigned int min_order = slub_min_order;
> > >     unsigned int order;
> > >
> > > -   if (order_objects(min_order, size) > MAX_OBJS_PER_PAGE)
> > > -           return get_order(size * MAX_OBJS_PER_PAGE) - 1;
> > > -
> > >     for (order = max(min_order, (unsigned int)get_order(min_objects * size));
> > >                     order <= max_order; order++) {
> > >
> > > @@ -3820,6 +3817,11 @@ static inline int calculate_order(unsigned int size)
> > >     unsigned int max_objects;
> > >     unsigned int nr_cpus;
> > >
> > > +   if (unlikely(order_objects(slub_min_order, size) > MAX_OBJS_PER_PAGE)) {
> > > +           order = get_order(size * MAX_OBJS_PER_PAGE) - 1;
> > > +           goto out;
> > > +   }
> > > +
> > >     /*
> > >      * Attempt to find best configuration for a slab. This
> > >      * works by first attempting to generate a layout with
> > > @@ -3865,14 +3867,8 @@ static inline int calculate_order(unsigned int size)
> > >      * We were unable to place multiple objects in a slab. Now
> > >      * lets see if we can place a single object there.
> > >      */
> > > -   order = calc_slab_order(size, 1, slub_max_order, 1);
> > > -   if (order <= slub_max_order)
> > > -           return order;
> > > -
> > > -   /*
> > > -    * Doh this slab cannot be placed using slub_max_order.
> > > -    */
> > > -   order = calc_slab_order(size, 1, MAX_ORDER, 1);
> > > +   order = max_t(unsigned int, slub_min_order, (unsigned int)get_order(size));
> > > +out:
> >
> > You don't need to cast value of get_order(size). max_t() does cast both operands.
> >
> > >     if (order < MAX_ORDER)
> > >             return order;
> > >     return -ENOSYS;
> >
> > For the correctness of the patch, I don't see any problem about the
> > code.
> >
> > But to be honest I'm a bit skeptical about saving some cycles in
> > calculating slab order. It's done only when creating caches (usually in boot
> > process).
>
> But yeah, maybe it's worth for better maintenance of code.
>
> So after considering my comments, feel free to add:
> Reviewed-by: Hyeonggon Yoo <42.hyeyoo@gmail.com>
>
> Thanks!
>

Thanks for taking the time to review Hyeonggon!

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

end of thread, other threads:[~2022-04-17  6:21 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-04-16  7:40 [PATCH v2] mm/slub: Remove repeated action in calculate_order() Wonhyuk Yang
2022-04-17  1:43 ` Hyeonggon Yoo
2022-04-17  2:07   ` Hyeonggon Yoo
2022-04-17  6:21     ` Wonhyuk Yang
2022-04-17  6:17   ` Wonhyuk Yang

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.