Skip to contentSkip to conversation

mm/slab_common: use helper function is_power_of_2()

PATCH In review Miaohe Lin 2 participants 5 messages Feb 17, 2022 – Feb 18, 2022

Subsystems (2)
lore
Share & tools
lore

Miaohe Lin

author
#0 lore raw

PATCH 1/1 mm/slab_common: use helper function is_power_of_2()

Use helper function is_power_of_2() to check if KMALLOC_MIN_SIZE is power of two. Minor readability improvement.

---
 mm/slab_common.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
mm/slab_common.c +1 -1
index 23f2ab0713b7..6ee64d6208b3 100644
@@ -807,7 +807,7 @@ void __init setup_kmalloc_cache_index_table(void)
 	unsigned int i;
 
 	BUILD_BUG_ON(KMALLOC_MIN_SIZE > 256 ||
-		(KMALLOC_MIN_SIZE & (KMALLOC_MIN_SIZE - 1)));
+		!is_power_of_2(KMALLOC_MIN_SIZE));
 
 	for (i = 8; i < KMALLOC_MIN_SIZE; i += 8) {
 		unsigned int elem = size_index_elem(i);
Signature · 1 line
2.23.0
#1 lore raw
Quoted 4 lines · Miaohe Lin

Use helper function is_power_of_2() to check if KMALLOC_MIN_SIZE is power of two. Minor readability improvement.

We can try, but in case some compiler will no longer able to perform the check at build-time (although is_power_of_2() looks sufficiently trivial too), we'll have to revert it.

Quoted 17 lines
---
 mm/slab_common.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
mm/slab_common.c +1 -1
index 23f2ab0713b7..6ee64d6208b3 100644
@@ -807,7 +807,7 @@ void __init setup_kmalloc_cache_index_table(void)
 	unsigned int i;
 
 	BUILD_BUG_ON(KMALLOC_MIN_SIZE > 256 ||
-		(KMALLOC_MIN_SIZE & (KMALLOC_MIN_SIZE - 1)));
+		!is_power_of_2(KMALLOC_MIN_SIZE));
 
 	for (i = 8; i < KMALLOC_MIN_SIZE; i += 8) {
 		unsigned int elem = size_index_elem(i);
author
#2 lore raw
Quoted 9 lines · Vlastimil Babka
Quoted 4 lines · Miaohe Lin

Use helper function is_power_of_2() to check if KMALLOC_MIN_SIZE is power of two. Minor readability improvement.

We can try, but in case some compiler will no longer able to perform the check at build-time (although is_power_of_2() looks sufficiently trivial too), we'll have to revert it.

I see. I checked the compiler in my env but there might be some compilers can't perform the check at build-time.

Many thanks for your reply.

Quoted 21 lines
Quoted 10 lines
---
 mm/slab_common.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
mm/slab_common.c +0 -0
index 23f2ab0713b7..6ee64d6208b3 100644

mm/slab_common.c void __init setup_kmalloc_cache_index_table(void) · line 813

Quoted 8 lines
mm/slab_common.c +1 -1
@@ -807,7 +807,7 @@ void __init setup_kmalloc_cache_index_table(void)
 	unsigned int i;
 
 	BUILD_BUG_ON(KMALLOC_MIN_SIZE > 256 ||
-		(KMALLOC_MIN_SIZE & (KMALLOC_MIN_SIZE - 1)));
+		!is_power_of_2(KMALLOC_MIN_SIZE));
 
 	for (i = 8; i < KMALLOC_MIN_SIZE; i += 8) {
 		unsigned int elem = size_index_elem(i);

.

#3 lore raw
Quoted 5 lines · Miaohe Lin
Quoted 4 lines · Vlastimil Babka
Quoted 3 lines · Miaohe Lin

Use helper function is_power_of_2() to check if KMALLOC_MIN_SIZE is power of two. Minor readability improvement.

Quoted 8 lines
Quoted 5 lines
Quoted 1 line

We can try, but in case some compiler will no longer able to perform the check at build-time (although is_power_of_2() looks sufficiently trivial too), we'll have to revert it.

I see. I checked the compiler in my env but there might be some compilers can't perform the check at build-time.

I'll add this to the slab tree and linux-next and we'll see if wider testing uncovers such compilers. Should be unlikely.

Quoted 24 lines

Many thanks for your reply.

Quoted 21 lines
Quoted 10 lines
---
 mm/slab_common.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
mm/slab_common.c +0 -0
index 23f2ab0713b7..6ee64d6208b3 100644

mm/slab_common.c void __init setup_kmalloc_cache_index_table(void) · line 813

Quoted 8 lines
mm/slab_common.c +1 -1
@@ -807,7 +807,7 @@ void __init setup_kmalloc_cache_index_table(void)
 	unsigned int i;
 
 	BUILD_BUG_ON(KMALLOC_MIN_SIZE > 256 ||
-		(KMALLOC_MIN_SIZE & (KMALLOC_MIN_SIZE - 1)));
+		!is_power_of_2(KMALLOC_MIN_SIZE));
 
 	for (i = 8; i < KMALLOC_MIN_SIZE; i += 8) {
 		unsigned int elem = size_index_elem(i);

.

author
#4 lore raw
Quoted 12 lines · Vlastimil Babka
Quoted 11 lines · Miaohe Lin
Quoted 9 lines · Vlastimil Babka
Quoted 4 lines · Miaohe Lin

Use helper function is_power_of_2() to check if KMALLOC_MIN_SIZE is power of two. Minor readability improvement.

We can try, but in case some compiler will no longer able to perform the check at build-time (although is_power_of_2() looks sufficiently trivial too), we'll have to revert it.

Quoted 5 lines
Quoted 2 lines

I see. I checked the compiler in my env but there might be some compilers can't perform the check at build-time.

I'll add this to the slab tree and linux-next and we'll see if wider testing uncovers such compilers. Should be unlikely.

Many thanks! :)

Quoted 28 lines
Quoted 25 lines

Many thanks for your reply.

Quoted 21 lines
Quoted 10 lines
---
 mm/slab_common.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
mm/slab_common.c +0 -0
index 23f2ab0713b7..6ee64d6208b3 100644

mm/slab_common.c void __init setup_kmalloc_cache_index_table(void) · line 813

Quoted 8 lines
mm/slab_common.c +1 -1
@@ -807,7 +807,7 @@ void __init setup_kmalloc_cache_index_table(void)
 	unsigned int i;
 
 	BUILD_BUG_ON(KMALLOC_MIN_SIZE > 256 ||
-		(KMALLOC_MIN_SIZE & (KMALLOC_MIN_SIZE - 1)));
+		!is_power_of_2(KMALLOC_MIN_SIZE));
 
 	for (i = 8; i < KMALLOC_MIN_SIZE; i += 8) {
 		unsigned int elem = size_index_elem(i);

.

.