diff mbox series

include/linux/slab.h: use for() and left shift to calculate

Message ID 20210302033908.51706-1-yejune.deng@gmail.com (mailing list archive)
State New, archived
Headers show
Series include/linux/slab.h: use for() and left shift to calculate | expand

Commit Message

Yejune Deng March 2, 2021, 3:39 a.m. UTC
use for() and left shift to calculate the value that compared with size.

Signed-off-by: Yejune Deng <yejune.deng@gmail.com>
---
 include/linux/slab.h | 32 ++++++++------------------------
 1 file changed, 8 insertions(+), 24 deletions(-)

Comments

Matthew Wilcox (Oracle) March 2, 2021, 3:52 a.m. UTC | #1
On Tue, Mar 02, 2021 at 11:39:08AM +0800, Yejune Deng wrote:
> use for() and left shift to calculate the value that compared with size.

https://lore.kernel.org/linux-mm/339dbb54-b4bc-78e2-e3f0-986814e86d0e@suse.cz/
Christoph Lameter March 2, 2021, 9:09 a.m. UTC | #2
On Tue, 2 Mar 2021, Yejune Deng wrote:

> use for() and left shift to calculate the value that compared with size.

There is a reason for the madness...

The current code was written so compilers can do proper constant folding
and eliminate the whole function entirely.

If you want this change then please verify that all compilers currently in
use with this code do proper constant folding and never generate code for
the for loop.
diff mbox series

Patch

diff --git a/include/linux/slab.h b/include/linux/slab.h
index 7ae604076767..0411f57930fb 100644
--- a/include/linux/slab.h
+++ b/include/linux/slab.h
@@ -347,6 +347,8 @@  static __always_inline enum kmalloc_cache_type kmalloc_type(gfp_t flags)
  */
 static __always_inline unsigned int kmalloc_index(size_t size)
 {
+	int i;
+
 	if (!size)
 		return 0;
 
@@ -357,30 +359,12 @@  static __always_inline unsigned int kmalloc_index(size_t size)
 		return 1;
 	if (KMALLOC_MIN_SIZE <= 64 && size > 128 && size <= 192)
 		return 2;
-	if (size <=          8) return 3;
-	if (size <=         16) return 4;
-	if (size <=         32) return 5;
-	if (size <=         64) return 6;
-	if (size <=        128) return 7;
-	if (size <=        256) return 8;
-	if (size <=        512) return 9;
-	if (size <=       1024) return 10;
-	if (size <=   2 * 1024) return 11;
-	if (size <=   4 * 1024) return 12;
-	if (size <=   8 * 1024) return 13;
-	if (size <=  16 * 1024) return 14;
-	if (size <=  32 * 1024) return 15;
-	if (size <=  64 * 1024) return 16;
-	if (size <= 128 * 1024) return 17;
-	if (size <= 256 * 1024) return 18;
-	if (size <= 512 * 1024) return 19;
-	if (size <= 1024 * 1024) return 20;
-	if (size <=  2 * 1024 * 1024) return 21;
-	if (size <=  4 * 1024 * 1024) return 22;
-	if (size <=  8 * 1024 * 1024) return 23;
-	if (size <=  16 * 1024 * 1024) return 24;
-	if (size <=  32 * 1024 * 1024) return 25;
-	if (size <=  64 * 1024 * 1024) return 26;
+
+	/* The maximum of size is 2^26 = 64 * 1024 * 1024 */
+	for (i = 3; i <= 26; i++) {
+		if (size <= (1 << i))
+			return i;
+	}
 	BUG();
 
 	/* Will never be reached. Needed because the compiler may complain */