Skip to contentSkip to conversation

mm/slub: Remove repeated action in calculate_order()

PATCH Superseded Wonhyuk Yang 2 participants 5 messages Apr 16, 2022 – Apr 17, 2022

Subsystems (2)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 mm/slub: Remove repeated action in calculate_order()

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.

---
V1 -> V2: Fix typo miss in a commit message

 mm/slub.c | 18 +++++++-----------
 1 file changed, 7 insertions(+), 11 deletions(-)
mm/slub.c +7 -11
index ed5c2c03a47a..e7a394d7b75a 100644
@@ -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;
Signature · 1 line
2.30.2
#1 lore raw
Quoted 5 lines · Wonhyuk Yang

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.

Quoted 41 lines

this case, we can calculate order by max(slub_min_order, get_order(size)) instead of calling calc_slab_order().

No functional impact expected.

---
V1 -> V2: Fix typo miss in a commit message

 mm/slub.c | 18 +++++++-----------
 1 file changed, 7 insertions(+), 11 deletions(-)
mm/slub.c +5 -3
index ed5c2c03a47a..e7a394d7b75a 100644
@@ -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.
 	 */

mm/slub.c static inline int calculate_order(unsigned int size) · line 3871

Quoted 10 lines
mm/slub.c +2 -8
-	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.

mm/slub.c static inline int calculate_order(unsigned int size) · line 3874

Quoted 3 lines
mm/slub.c +0 -0
 	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).

Quoted 4 lines
Signature · 1 line
2.30.2
Signature · 2 lines
Thanks,
Hyeonggon
Reviewed-by
#2 lore raw
Quoted 67 lines · Hyeonggon Yoo
Quoted 5 lines · Wonhyuk Yang

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.

Quoted 41 lines

this case, we can calculate order by max(slub_min_order, get_order(size)) instead of calling calc_slab_order().

No functional impact expected.

---
V1 -> V2: Fix typo miss in a commit message

 mm/slub.c | 18 +++++++-----------
 1 file changed, 7 insertions(+), 11 deletions(-)
mm/slub.c +5 -3
index ed5c2c03a47a..e7a394d7b75a 100644
@@ -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.
 	 */

mm/slub.c static inline int calculate_order(unsigned int size) · line 3871

Quoted 10 lines
mm/slub.c +2 -8
-	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.

mm/slub.c static inline int calculate_order(unsigned int size) · line 3874

Quoted 3 lines
mm/slub.c +0 -0
 	if (order < MAX_ORDER)
 		return order;
 	return -ENOSYS;
Quoted 6 lines

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:

Thanks!

Quoted 8 lines
Quoted 4 lines
Signature · 1 line
2.30.2
Signature · 2 lines
Thanks,
Hyeonggon
Signature · 2 lines
Thanks,
Hyeonggon
#3 lore raw
Quoted 76 lines · Hyeonggon Yoo
Quoted 67 lines · Hyeonggon Yoo
Quoted 5 lines · Wonhyuk Yang

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.

Quoted 41 lines

this case, we can calculate order by max(slub_min_order, get_order(size)) instead of calling calc_slab_order().

No functional impact expected.

---
V1 -> V2: Fix typo miss in a commit message

 mm/slub.c | 18 +++++++-----------
 1 file changed, 7 insertions(+), 11 deletions(-)
mm/slub.c +5 -3
index ed5c2c03a47a..e7a394d7b75a 100644
@@ -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.
     */

mm/slub.c static inline int calculate_order(unsigned int size) · line 3871

Quoted 10 lines
mm/slub.c +2 -8
-   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.

mm/slub.c static inline int calculate_order(unsigned int size) · line 3874

Quoted 3 lines
mm/slub.c +0 -0
    if (order < MAX_ORDER)
            return order;
    return -ENOSYS;
Quoted 6 lines

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).

Quoted 7 lines

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

So after considering my comments, feel free to add:

Thanks!

Thanks for taking the time to review Hyeonggon!

#4 lore raw
In reply to Hyeonggon Yoo
Quoted 9 lines · Hyeonggon Yoo
Quoted 5 lines · Wonhyuk Yang

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...

Quoted 46 lines
Quoted 46 lines

this case, we can calculate order by max(slub_min_order, get_order(size)) instead of calling calc_slab_order().

No functional impact expected.

---
V1 -> V2: Fix typo miss in a commit message

 mm/slub.c | 18 +++++++-----------
 1 file changed, 7 insertions(+), 11 deletions(-)
mm/slub.c +5 -7
index ed5c2c03a47a..e7a394d7b75a 100644
@@ -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;
-
Quoted 8 lines

mm/slub.c static inline int calculate_order(unsigned int size) · line 3871

Quoted 6 lines
mm/slub.c +2 -4
-     /*
-      * 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.

Quoted 12 lines

mm/slub.c static inline int calculate_order(unsigned int size) · line 3874

Quoted 4 lines
mm/slub.c +0 -0
      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).