Skip to contentSkip to conversation

mm/mempool: Add 'else' to split mutually exclusive case

PATCH In review Miaohe Lin 4 participants 4 messages Aug 24, 2020

Subsystems (2)
lore
Share & tools
lore

Miaohe Lin

author
#0 lore raw

PATCH 1/1 mm/mempool: Add 'else' to split mutually exclusive case

Add else to split mutually exclusive case and avoid some unnecessary check.

---
 mm/mempool.c | 10 ++++------
 1 file changed, 4 insertions(+), 6 deletions(-)
mm/mempool.c +4 -6
index 79bff63ecf27..73fb2e548b43 100644
@@ -60,9 +60,8 @@ static void check_element(mempool_t *pool, void *element)
 	/* Mempools backed by slab allocator */
 	if (pool->free == mempool_free_slab || pool->free == mempool_kfree)
 		__check_element(pool, element, ksize(element));
-
 	/* Mempools backed by page allocator */
-	if (pool->free == mempool_free_pages) {
+	else if (pool->free == mempool_free_pages) {
 		int order = (int)(long)pool->pool_data;
 		void *addr = kmap_atomic((struct page *)element);
 
@@ -84,9 +83,8 @@ static void poison_element(mempool_t *pool, void *element)
 	/* Mempools backed by slab allocator */
 	if (pool->alloc == mempool_alloc_slab || pool->alloc == mempool_kmalloc)
 		__poison_element(element, ksize(element));
-
 	/* Mempools backed by page allocator */
-	if (pool->alloc == mempool_alloc_pages) {
+	else if (pool->alloc == mempool_alloc_pages) {
 		int order = (int)(long)pool->pool_data;
 		void *addr = kmap_atomic((struct page *)element);
 
@@ -107,7 +105,7 @@ static __always_inline void kasan_poison_element(mempool_t *pool, void *element)
 {
 	if (pool->alloc == mempool_alloc_slab || pool->alloc == mempool_kmalloc)
 		kasan_poison_kfree(element, _RET_IP_);
-	if (pool->alloc == mempool_alloc_pages)
+	else if (pool->alloc == mempool_alloc_pages)
 		kasan_free_pages(element, (unsigned long)pool->pool_data);
 }
 
@@ -115,7 +113,7 @@ static void kasan_unpoison_element(mempool_t *pool, void *element)
 {
 	if (pool->alloc == mempool_alloc_slab || pool->alloc == mempool_kmalloc)
 		kasan_unpoison_slab(element);
-	if (pool->alloc == mempool_alloc_pages)
+	else if (pool->alloc == mempool_alloc_pages)
 		kasan_alloc_pages(element, (unsigned long)pool->pool_data);
 }
 
Signature · 1 line
2.19.1
maintainer
#1 lore raw
Quoted 5 lines · On Mon, 24 Aug 2020 07:53:54 -0400 Miaohe Lin <[email protected]> wrote:

Add else to split mutually exclusive case and avoid some unnecessary check.

mm/mempool.c static void check_element(mempool_t *pool, void *element) · line 67

Quoted 10 lines
mm/mempool.c +1 -2
@@ -60,9 +60,8 @@ static void check_element(mempool_t *pool, void *element)
 	/* Mempools backed by slab allocator */
 	if (pool->free == mempool_free_slab || pool->free == mempool_kfree)
 		__check_element(pool, element, ksize(element));
-
 	/* Mempools backed by page allocator */
-	if (pool->free == mempool_free_pages) {
+	else if (pool->free == mempool_free_pages) {
 		int order = (int)(long)pool->pool_data;
 		void *addr = kmap_atomic((struct page *)element);
 

It doesn't seem to change code generation (compiler is smart), but I think it helps readability.

#2 lore raw
Quoted 4 lines · Andrew Morton
Quoted 2 lines · On Mon, 24 Aug 2020 07:53:54 -0400 Miaohe Lin <[email protected]> wrote:

Add else to split mutually exclusive case and avoid some unnecessary check.

Quoted 16 lines

mm/mempool.c static void check_element(mempool_t *pool, void *element) · line 67

Quoted 13 lines
mm/mempool.c +1 -2
@@ -60,9 +60,8 @@ static void check_element(mempool_t *pool, void *element)
 	/* Mempools backed by slab allocator */
 	if (pool->free == mempool_free_slab || pool->free == mempool_kfree)
 		__check_element(pool, element, ksize(element));
-
 	/* Mempools backed by page allocator */
-	if (pool->free == mempool_free_pages) {
+	else if (pool->free == mempool_free_pages) {
 		int order = (int)(long)pool->pool_data;
 		void *addr = kmap_atomic((struct page *)element);
 

It doesn't seem to change code generation (compiler is smart), but I think it helps readability.

Not with the comments where they are

#3 lore raw
In reply to Andrew Morton
Quoted 4 lines · Andrew Morton
Quoted 2 lines · On Mon, 24 Aug 2020 07:53:54 -0400 Miaohe Lin <[email protected]> wrote:

Add else to split mutually exclusive case and avoid some unnecessary check.

Quoted 16 lines

mm/mempool.c static void check_element(mempool_t *pool, void *element) · line 67

Quoted 13 lines
mm/mempool.c +1 -2
@@ -60,9 +60,8 @@ static void check_element(mempool_t *pool, void *element)
 	/* Mempools backed by slab allocator */
 	if (pool->free == mempool_free_slab || pool->free == mempool_kfree)
 		__check_element(pool, element, ksize(element));
-
 	/* Mempools backed by page allocator */
-	if (pool->free == mempool_free_pages) {
+	else if (pool->free == mempool_free_pages) {
 		int order = (int)(long)pool->pool_data;
 		void *addr = kmap_atomic((struct page *)element);
 

It doesn't seem to change code generation (compiler is smart), but I think it helps readability.

style: braces should be added to the first test.

Perhaps better as:

---
 mm/mempool.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
mm/mempool.c +5 -5
index 79bff63ecf27..d0206eab86a4 100644
@@ -57,12 +57,12 @@ static void __check_element(mempool_t *pool, void *element, size_t size)
 
 static void check_element(mempool_t *pool, void *element)
 {
-	/* Mempools backed by slab allocator */
-	if (pool->free == mempool_free_slab || pool->free == mempool_kfree)
+	if (pool->free == mempool_free_slab ||
+	    pool->free == mempool_kfree) {
+		/* Mempools backed by slab allocator */
 		__check_element(pool, element, ksize(element));
-
-	/* Mempools backed by page allocator */
-	if (pool->free == mempool_free_pages) {
+	} else if (pool->free == mempool_free_pages) {
+		/* Mempools backed by page allocator */
 		int order = (int)(long)pool->pool_data;
 		void *addr = kmap_atomic((struct page *)element);