Skip to contentSkip to conversation

slab.h: Avoid using & for logical and of booleans

PATCH In review Bart Van Assche 9 participants 29 messages Nov 5, 2018 – Nov 21, 2018

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 slab.h: Avoid using & for logical and of booleans

This patch suppresses the following sparse warning:

./include/linux/slab.h:332:43: warning: dubious: x & !y

Fixes 1291523f2c1d ("mm, slab/slub: introduce kmalloc-reclaimable caches")
Cc Vlastimil Babka <[email protected]>
Cc Mel Gorman <[email protected]>
Cc Christoph Lameter <[email protected]>
Cc Roman Gushchin <[email protected]>
---
 include/linux/slab.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
include/linux/slab.h +1 -1
index 918f374e7156..97d0599ddb7b 100644
@@ -329,7 +329,7 @@ static __always_inline enum kmalloc_cache_type kmalloc_type(gfp_t flags)
 	 * If an allocation is both __GFP_DMA and __GFP_RECLAIMABLE, return
 	 * KMALLOC_DMA and effectively ignore __GFP_RECLAIMABLE
 	 */
-	return type_dma + (is_reclaimable & !is_dma) * KMALLOC_RECLAIM;
+	return type_dma + is_reclaimable * !is_dma * KMALLOC_RECLAIM;
 }
 
 /*
Signature · 1 line
2.19.1.930.g4563a0d9d0-goog
maintainer
#1 lore raw
Quoted 9 lines · On Mon, 5 Nov 2018 12:40:00 -0800 Bart Van Assche <[email protected]> wrote:

This patch suppresses the following sparse warning:

./include/linux/slab.h:332:43: warning: dubious: x & !y

...

include/linux/slab.h static __always_inline enum kmalloc_cache_type kmalloc_type(gfp_t flags) · line 335

Quoted 8 lines
include/linux/slab.h +1 -1
@@ -329,7 +329,7 @@ static __always_inline enum kmalloc_cache_type kmalloc_type(gfp_t flags)
 	 * If an allocation is both __GFP_DMA and __GFP_RECLAIMABLE, return
 	 * KMALLOC_DMA and effectively ignore __GFP_RECLAIMABLE
 	 */
-	return type_dma + (is_reclaimable & !is_dma) * KMALLOC_RECLAIM;
+	return type_dma + is_reclaimable * !is_dma * KMALLOC_RECLAIM;
 }
 
 /*

I suppose so.

That function seems too clever for its own good :(. I wonder if these branch-avoiding tricks are really worthwhile.

#2 lore raw
Quoted 18 lines · Andrew Morton
Quoted 13 lines · On Mon, 5 Nov 2018 12:40:00 -0800 Bart Van Assche <[email protected]>wrote:

This patch suppresses the following sparse warning:

./include/linux/slab.h:332:43: warning: dubious: x &!y

...

--- a/include/linux/slab.h
+-+-+- b/include/linux/slab.h
@@-329,7 +-329,7 @@static __always_inline enum kmalloc_cache_type kmalloc_type(gfp_t flags)
*If an allocation is both __GFP_DMA and __GFP_RECLAIMABLE, return *KMALLOC_DMA and effectively ignore __GFP_RECLAIMABLE
*/

(quoted diff) +0 -1
-	return type_dma +- (is_reclaimable &!is_dma) *KMALLOC_RECLAIM;>>+-	return type_dma +- is_reclaimable *!is_dma *KMALLOC_RECLAIM;>> }>> 
Quoted 1 line

/*>

I suppose so.

Quoted 4 lines

That function seems too clever for its own good :(. I wonder if these branch-avoiding tricks are really worthwhile.

From what I have seen in gcc disassembly it seems to me like gcc uses the

cmov instruction to implement e.g. the ternary operator (?:). So I think none of the cleverness in kmalloc_type() is really necessary to avoid conditional branches. I think this function would become much more readable when using a switch statement or when rewriting it as follows (untested):

static __always_inline enum kmalloc_cache_type kmalloc_type(gfp_t flags)
{- int is_dma =0;- int type_dma =0;- int is_reclaimable;-
-#ifdef CONFIG_ZONE_DMA
- is_dma =!!flags &__GFP_DMA);- type_dma =is_dma *KMALLOC_DMA;-#endif
-
- is_reclaimable =!!flags &__GFP_RECLAIMABLE);-
/* *If an allocation is both __GFP_DMA and __GFP_RECLAIMABLE, return
*KMALLOC_DMA and effectively ignore __GFP_RECLAIMABLE */
- return type_dma +- (is_reclaimable &!is_dma) *KMALLOC_RECLAIM;+- static const enum kmalloc_cache_type flags_to_type[2][2]={+- {0, KMALLOC_RECLAIM }

+-		{KMALLOC_DMA,	KMALLOC_DMA }
+-	};+-#ifdef CONFIG_ZONE_DMA
+-	bool is_dma =!!flags &__GFP_DMA);+-#endif
+-	bool is_reclaimable =!!flags &__GFP_RECLAIMABLE);+-
+-	return flags_to_type[is_dma][is_reclaimable]; }
#3 lore raw
Quoted 52 lines · Bart Van Assche
Quoted 18 lines · Andrew Morton
Quoted 16 lines · On Mon, 5 Nov 2018 12:40:00 -0800 Bart Van Assche <[email protected]> wrote:

This patch suppresses the following sparse warning:

./include/linux/slab.h:332:43: warning: dubious: x & !y

...

include/linux/slab.h +1 -1
@@ -329,7 +329,7 @@ static __always_inline enum kmalloc_cache_type kmalloc_type(gfp_t flags)
 	 * If an allocation is both __GFP_DMA and __GFP_RECLAIMABLE, return
 	 * KMALLOC_DMA and effectively ignore __GFP_RECLAIMABLE
 	 */
-	return type_dma + (is_reclaimable & !is_dma) * KMALLOC_RECLAIM;
+	return type_dma + is_reclaimable * !is_dma * KMALLOC_RECLAIM;
 }
 
Quoted 6 lines

include/linux/slab.h static __always_inline enum kmalloc_cache_type kmalloc_type(gfp_t flags) · line 335

Quoted 1 line
include/linux/slab.h +0 -0
 /*

I suppose so.

That function seems too clever for its own good :(. I wonder if these branch-avoiding tricks are really worthwhile.

From what I have seen in gcc disassembly it seems to me like gcc uses the cmov instruction to implement e.g. the ternary operator (?:). So I think none of the cleverness in kmalloc_type() is really necessary to avoid conditional branches. I think this function would become much more readable when using a switch statement or when rewriting it as follows (untested):

static __always_inline enum kmalloc_cache_type kmalloc_type(gfp_t flags) {

(quoted diff) +1 -12
-	int is_dma = 0;
-	int type_dma = 0;
-	int is_reclaimable;
-
-#ifdef CONFIG_ZONE_DMA
-	is_dma = !!(flags & __GFP_DMA);
-	type_dma = is_dma * KMALLOC_DMA;
-#endif
-
-	is_reclaimable = !!(flags & __GFP_RECLAIMABLE);
-
 	/*
 	 * If an allocation is both __GFP_DMA and __GFP_RECLAIMABLE, return
 	 * KMALLOC_DMA and effectively ignore __GFP_RECLAIMABLE
 	 */
-	return type_dma + (is_reclaimable & !is_dma) * KMALLOC_RECLAIM;
+	static const enum kmalloc_cache_type flags_to_type[2][2] = {

Review

Quoted 10 lines
(quoted diff) +9 -0
+		{ 0,		KMALLOC_RECLAIM },
+		{ KMALLOC_DMA,	KMALLOC_DMA },
+	};
+#ifdef CONFIG_ZONE_DMA
+	bool is_dma = !!(flags & __GFP_DMA);
+#endif
+	bool is_reclaimable = !!(flags & __GFP_RECLAIMABLE);
+
+	return flags_to_type[is_dma][is_reclaimable];
 }

Won't that pessimize the cases where gfp is a constant to actually do the table lookup, and add 16 bytes to every translation unit?

Another option is to add a fake KMALLOC_DMA_RECLAIM so the kmalloc_caches[] array has size 4, then assign the same dma kmalloc_cache pointer to [2][i] and [3][i] (so that costs perhaps a dozen pointers in .data), and then just compute kmalloc_type() as

((flags & __GFP_RECLAIMABLE) >> someshift) | ((flags & __GFP_DMA) >> someothershift).

Perhaps one could even shuffle the GFP flags so the two shifts are the same.

Rasmus

#4 lore raw
Quoted 11 lines · Rasmus Villemoes

Won't that pessimize the cases where gfp is a constant to actually do the table lookup, and add 16 bytes to every translation unit?

Another option is to add a fake KMALLOC_DMA_RECLAIM so the kmalloc_caches[]array has size 4, then assign the same dma kmalloc_cache pointer to [2][i]and [3][i](so that costs perhaps a dozen pointers in .data), and then just compute kmalloc_type() as

((flags &__GFP_RECLAIMABLE) >>someshift) |((flags &__GFP_DMA) >>>someothershift).

Perhaps one could even shuffle the GFP flags so the two shifts are the same.

How about this version, still untested? My compiler is able to evaluate the switch expression if the argument is constant.

static __always_inline enum kmalloc_cache_type kmalloc_type(gfp_t flags)
{- int is_dma =0;- int type_dma =0;- int is_reclaimable;+- unsigned int dr =!!flags &__GFP_RECLAIMABLE);
#ifdef CONFIG_ZONE_DMA
- is_dma =!!flags &__GFP_DMA);- type_dma =is_dma *KMALLOC_DMA;+- dr |=!!flags &__GFP_DMA) <<1; #endif

- is_reclaimable =!!flags &__GFP_RECLAIMABLE);-
/* *If an allocation is both __GFP_DMA and __GFP_RECLAIMABLE, return
*KMALLOC_DMA and effectively ignore __GFP_RECLAIMABLE */
- return type_dma +- (is_reclaimable &!is_dma) *KMALLOC_RECLAIM;+- switch (dr) {+- default:

+-	case 0:
+-		return 0;+-	case 1:
+-		return KMALLOC_RECLAIM;+-	case 2:
+-	case 3:
+-		return KMALLOC_DMA;+-	} } 

Bart.

#5 lore raw
Quoted 50 lines · Bart Van Assche
Quoted 8 lines · Rasmus Villemoes

Won't that pessimize the cases where gfp is a constant to actually do the table lookup, and add 16 bytes to every translation unit?

Another option is to add a fake KMALLOC_DMA_RECLAIM so the kmalloc_caches[] array has size 4, then assign the same dma kmalloc_cache pointer to [2][i] and [3][i] (so that costs perhaps a dozen pointers in .data), and then just compute kmalloc_type() as

Quoted 4 lines

((flags & __GFP_RECLAIMABLE) >> someshift) | ((flags & __GFP_DMA) >> someothershift).

Perhaps one could even shuffle the GFP flags so the two shifts are the same.

How about this version, still untested? My compiler is able to evaluate the switch expression if the argument is constant.

static __always_inline enum kmalloc_cache_type kmalloc_type(gfp_t flags) {

(quoted diff) +12 -8
-       int is_dma = 0;
-       int type_dma = 0;
-       int is_reclaimable;
+       unsigned int dr = !!(flags & __GFP_RECLAIMABLE);
 
 #ifdef CONFIG_ZONE_DMA
-       is_dma = !!(flags & __GFP_DMA);
-       type_dma = is_dma * KMALLOC_DMA;
+       dr |= !!(flags & __GFP_DMA) << 1;
 #endif
 
-       is_reclaimable = !!(flags & __GFP_RECLAIMABLE);
-
        /*
         * If an allocation is both __GFP_DMA and __GFP_RECLAIMABLE, return
         * KMALLOC_DMA and effectively ignore __GFP_RECLAIMABLE
         */
-       return type_dma + (is_reclaimable & !is_dma) * KMALLOC_RECLAIM;
+       switch (dr) {
+       default:
+       case 0:
+               return 0;
+       case 1:
+               return KMALLOC_RECLAIM;
+       case 2:
+       case 3:
+               return KMALLOC_DMA;
+       }
 }
Quoted 1 line

Bart.

Doesn't this defeat the whole point of the code which I thought was to avoid conditional jumps and branches? Also why would you bother with the "dr" value when you could just mask the flags value and switch on that directly?

#6 lore raw
Quoted 19 lines · Alexander Duyck
Quoted 8 lines · Bart Van Assche

How about this version, still untested? My compiler is able to evaluate the switch expression if the argument is constant.

static __always_inline enum kmalloc_cache_type kmalloc_type(gfp_t flags)
{>>- int is_dma =0;>>- int type_dma =0;>>- int is_reclaimable;>>+- unsigned int dr =!!flags &__GFP_RECLAIMABLE);>>
#ifdef CONFIG_ZONE_DMA

(quoted diff) +0 -1
-       is_dma =!!flags &__GFP_DMA);>>-       type_dma =is_dma *KMALLOC_DMA;>>+-       dr |=!!flags &__GFP_DMA) <<1;>> #endif
 

Review

Quoted 10 lines
(quoted diff) +5 -2
-       is_reclaimable =!!flags &__GFP_RECLAIMABLE);>>-
       /*>>         *If an allocation is both __GFP_DMA and __GFP_RECLAIMABLE, return
        *KMALLOC_DMA and effectively ignore __GFP_RECLAIMABLE
        */
-       return type_dma +- (is_reclaimable &!is_dma) *KMALLOC_RECLAIM;>>+-       switch (dr) {>>+-       default:
+-       case 0:
+-               return 0;>>+-       case 1:
+-               return KMALLOC_RECLAIM;>>+-       case 2:
+-       case 3:
+-               return KMALLOC_DMA;>>+-       }>> }>>Doesn't this defeat the whole point of the code which I thought was to
Quoted 3 lines

avoid conditional jumps and branches? Also why would you bother with the "dr"value when you could just mask the flags value and switch on that directly?

Storing the relevant bits of 'flags' in the 'dr' variable avoids that the bit selection expressions have to be repeated and allows to use a switch statement instead of multiple if / else statements.

Most kmalloc() calls pass a constant to the gfp argument. That allows the compiler to evaluate kmalloc_type() at compile time. So the conditional jumps and branches only appear when the gfp argument is not a constant. What makes you think it is important to optimize for that case?

Bart.

#7 lore raw
Quoted 38 lines · Bart Van Assche
Quoted 36 lines · Alexander Duyck
Quoted 34 lines · Bart Van Assche

How about this version, still untested? My compiler is able to evaluate the switch expression if the argument is constant.

static __always_inline enum kmalloc_cache_type kmalloc_type(gfp_t flags) {

(quoted diff) +12 -8
-       int is_dma = 0;
-       int type_dma = 0;
-       int is_reclaimable;
+       unsigned int dr = !!(flags & __GFP_RECLAIMABLE);
 
 #ifdef CONFIG_ZONE_DMA
-       is_dma = !!(flags & __GFP_DMA);
-       type_dma = is_dma * KMALLOC_DMA;
+       dr |= !!(flags & __GFP_DMA) << 1;
 #endif
 
-       is_reclaimable = !!(flags & __GFP_RECLAIMABLE);
-
        /*
         * If an allocation is both __GFP_DMA and __GFP_RECLAIMABLE, return
         * KMALLOC_DMA and effectively ignore __GFP_RECLAIMABLE
         */
-       return type_dma + (is_reclaimable & !is_dma) * KMALLOC_RECLAIM;
+       switch (dr) {
+       default:
+       case 0:
+               return 0;
+       case 1:
+               return KMALLOC_RECLAIM;
+       case 2:
+       case 3:
+               return KMALLOC_DMA;
+       }
 }
Quoted 8 lines
Quoted 4 lines

Doesn't this defeat the whole point of the code which I thought was to avoid conditional jumps and branches? Also why would you bother with the "dr" value when you could just mask the flags value and switch on that directly?

Storing the relevant bits of 'flags' in the 'dr' variable avoids that the bit selection expressions have to be repeated and allows to use a switch statement instead of multiple if / else statements.

Really they shouldn't have to be repeated. You essentially have just 3 cases. 0, __GFP_RECLAIMABLE, and the default case.

Quoted 6 lines

Most kmalloc() calls pass a constant to the gfp argument. That allows the compiler to evaluate kmalloc_type() at compile time. So the conditional jumps and branches only appear when the gfp argument is not a constant. What makes you think it is important to optimize for that case?

Bart.

I didn't really think it was all that important to optimize, but I thought that was what you were trying to maintain with the earlier patch since it was converting things to a table lookup.

If we really don't care then why even bother with the switch statement anyway? It seems like you could just do one ternary operator and be done with it. Basically all you need is:
return (defined(CONFIG_ZONE_DMA) && (flags & __GFP_DMA)) ? KMALLOC_DMA :

        (flags & __GFP_RECLAIMABLE) ? KMALLOC_RECLAIM : 0;

Why bother with all the extra complexity of the switch statement?

Thanks.

- Alex

#8 lore raw
Quoted 6 lines · Alexander Duyck

If we really don't care then why even bother with the switch statement anyway? It seems like you could just do one ternary operator and be done with it. Basically all you need is:
return (defined(CONFIG_ZONE_DMA) &&(flags &__GFP_DMA)) ? KMALLOC_DMA :
(flags &__GFP_RECLAIMABLE) ? KMALLOC_RECLAIM : 0;>
Why bother with all the extra complexity of the switch statement?

I don't think that defined() can be used in a C expression. Hence the IS_ENABLED() macro. If you fix that, leave out four superfluous parentheses, test your patch, post that patch and cc me then I will add my Reviewed-by.

Bart.

#9 lore raw
Quoted 8 lines · Bart Van Assche
Quoted 6 lines · Alexander Duyck

If we really don't care then why even bother with the switch statement anyway? It seems like you could just do one ternary operator and be done with it. Basically all you need is:
return (defined(CONFIG_ZONE_DMA) && (flags & __GFP_DMA)) ? KMALLOC_DMA :

        (flags & __GFP_RECLAIMABLE) ? KMALLOC_RECLAIM : 0;
Quoted 7 lines
Quoted 1 line

Why bother with all the extra complexity of the switch statement?

I don't think that defined() can be used in a C expression. Hence the IS_ENABLED() macro. If you fix that, leave out four superfluous parentheses, test your patch, post that patch and cc me then I will add my Reviewed-by.

Bart.

Actually the defined macro is used multiple spots in if statements throughout the kernel.

The reason for IS_ENABLED is to address the fact that we can be dealing with macros that indicate if they are built in or a module since those end up being two different defines depending on if you select 'y' or 'm'.

Thanks.

- Alex

#10 lore raw
Quoted 10 lines · Alexander Duyck
Quoted 9 lines · Bart Van Assche
Quoted 6 lines · Alexander Duyck

If we really don't care then why even bother with the switch statement anyway? It seems like you could just do one ternary operator and be done with it. Basically all you need is:
return (defined(CONFIG_ZONE_DMA) &&(flags &__GFP_DMA)) ? KMALLOC_DMA :

       (flags &__GFP_RECLAIMABLE) ? KMALLOC_RECLAIM : 0;>>>

Why bother with all the extra complexity of the switch statement?

Quoted 6 lines
Quoted 3 lines

I don't think that defined() can be used in a C expression. Hence the IS_ENABLED() macro. If you fix that, leave out four superfluous parentheses, test your patch, post that patch and cc me then I will add my Reviewed-by.

Actually the defined macro is used multiple spots in if statements throughout the kernel.

The only 'if (defined(' matches I found in the kernel tree that are not preprocessor statements occur in Perl code. Maybe I overlooked something?

Quoted 6 lines

The reason for IS_ENABLED is to address the fact that we can be dealing with macros that indicate if they are built in or a module since those end up being two different defines depending on if you select 'y' or 'm'.

From Documentation/process/coding-style.rst:

Within code, where possible, use the IS_ENABLED macro to convert a Kconfig symbol into a C boolean expression, and use it in a normal C conditional:

.. code-block:: c

	if (IS_ENABLED(CONFIG_SOMETHING)) {		...
	}

Bart.

#11 lore raw
Quoted 17 lines · Bart Van Assche
Quoted 15 lines · Alexander Duyck
Quoted 13 lines · Bart Van Assche
Quoted 7 lines · Alexander Duyck

If we really don't care then why even bother with the switch statement anyway? It seems like you could just do one ternary operator and be done with it. Basically all you need is:
return (defined(CONFIG_ZONE_DMA) && (flags & __GFP_DMA)) ? KMALLOC_DMA :

        (flags & __GFP_RECLAIMABLE) ? KMALLOC_RECLAIM : 0;

Why bother with all the extra complexity of the switch statement?

I don't think that defined() can be used in a C expression. Hence the IS_ENABLED() macro. If you fix that, leave out four superfluous parentheses, test your patch, post that patch and cc me then I will add my Reviewed-by.

Quoted 5 lines
Quoted 2 lines

Actually the defined macro is used multiple spots in if statements throughout the kernel.

The only 'if (defined(' matches I found in the kernel tree that are not preprocessor statements occur in Perl code. Maybe I overlooked something?

You may be right. I think I was thinking of "__is_defined", not "defined".

Quoted 10 lines
Quoted 4 lines

The reason for IS_ENABLED is to address the fact that we can be dealing with macros that indicate if they are built in or a module since those end up being two different defines depending on if you select 'y' or 'm'.

From Documentation/process/coding-style.rst:

Within code, where possible, use the IS_ENABLED macro to convert a Kconfig symbol into a C boolean expression, and use it in a normal C conditional:

Quoted 7 lines

.. code-block:: c

        if (IS_ENABLED(CONFIG_SOMETHING)) {
                ...
        }

Bart.

Right. Part of the reason for suggesting that is that depending on how you define "CONFIG_SOMETHING" it can actually be defined as "CONFIG_SOMETHING" or "CONFIG_SOMETHING_MODULE". I was operating under the assumption that CONFIG_ZONE_DMA wasn't ever going to be built as a module.

Thanks.

- Alex

#12 lore raw
In reply to Andrew Morton
Quoted 9 lines · Andrew Morton
Quoted 6 lines · On Mon, 5 Nov 2018 12:40:00 -0800 Bart Van Assche <[email protected]> wrote:
include/linux/slab.h +1 -1
-    return type_dma + (is_reclaimable & !is_dma) * KMALLOC_RECLAIM;
+    return type_dma + is_reclaimable * !is_dma * KMALLOC_RECLAIM;

}

Quoted 6 lines
Quoted 1 line

/*

I suppose so.

That function seems too clever for its own good :(. I wonder if these branch-avoiding tricks are really worthwhile.

At the very least I'd like to see some comments added as to why that approach was taken for the sake of future maintainers.

William Kucharski

Acked-by on v1 1/1
#13 lore raw
In reply to Bart Van Assche
Quoted 10 lines · Bart Van Assche

This patch suppresses the following sparse warning:

./include/linux/slab.h:332:43: warning: dubious: x & !y

Fixes 1291523f2c1d ("mm, slab/slub: introduce kmalloc-reclaimable caches")
Cc Vlastimil Babka <[email protected]>
Cc Mel Gorman <[email protected]>
Cc Christoph Lameter <[email protected]>
Cc Roman Gushchin <[email protected]>

Thanks.

Quoted 18 lines
---
 include/linux/slab.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
include/linux/slab.h +1 -1
index 918f374e7156..97d0599ddb7b 100644
@@ -329,7 +329,7 @@ static __always_inline enum kmalloc_cache_type kmalloc_type(gfp_t flags)
 	 * If an allocation is both __GFP_DMA and __GFP_RECLAIMABLE, return
 	 * KMALLOC_DMA and effectively ignore __GFP_RECLAIMABLE
 	 */
-	return type_dma + (is_reclaimable & !is_dma) * KMALLOC_RECLAIM;
+	return type_dma + is_reclaimable * !is_dma * KMALLOC_RECLAIM;
 }
 
 /*
#14 lore raw
In reply to Bart Van Assche

From: Bart Van Assche

Quoted 21 lines

Sent: 05 November 2018 20:40

This patch suppresses the following sparse warning:

./include/linux/slab.h:332:43: warning: dubious: x & !y

Fixes 1291523f2c1d ("mm, slab/slub: introduce kmalloc-reclaimable caches")
Cc Vlastimil Babka <[email protected]>
Cc Mel Gorman <[email protected]>
Cc Christoph Lameter <[email protected]>
Cc Roman Gushchin <[email protected]>
---
 include/linux/slab.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
include/linux/slab.h +0 -0
index 918f374e7156..97d0599ddb7b 100644

include/linux/slab.h static __always_inline enum kmalloc_cache_type kmalloc_type(gfp_t flags) · line 332

Quoted 5 lines
include/linux/slab.h +1 -1
@@ -329,7 +329,7 @@ static __always_inline enum kmalloc_cache_type kmalloc_type(gfp_t flags)
 	 * If an allocation is both __GFP_DMA and __GFP_RECLAIMABLE, return
 	 * KMALLOC_DMA and effectively ignore __GFP_RECLAIMABLE
 	 */
-	return type_dma + (is_reclaimable & !is_dma) * KMALLOC_RECLAIM;
+	return type_dma + is_reclaimable * !is_dma * KMALLOC_RECLAIM;

ISTM that changing is_dma and is_reclaimable from int to bool will stop the bleating.

It is also strange that this code is trying so hard here to avoid conditional instructions and then uses several to generate the boolean values in the first place.

OTOH I'd probably write:

	int gfp_dma = 0;
#ifdef CONFIG_ZONE_DMA
	gfp_dma = __GFP_DMA;

#endif

	return flags & gfp_dma ? KMALLOC_DMA : flags & __GFP_RECLAIMABLE ? KMALLOC_RECLAIM : 0;

That might generate cmovs, but is may be better to put unlikely() around both conditional expressions. Or redo as:

	return !unlikely(flags & (dfp_dma | __GFP_RECLAIMABLE)) ? 0 : flags & gfp_dma ? KMALLOC_DMA : KMALLOC_RECLAIM;

	David

-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK Registration No: 1397386 (Wales)

#15 lore raw
Quoted 6 lines · David Laight

From: Bart Van Assche

Quoted 5 lines

Sent: 05 November 2018 20:40

This patch suppresses the following sparse warning:

./include/linux/slab.h:332:43: warning: dubious: x & !y

BTW, I wonder why the warnings appeared only now, after maybe months in linux-next. Don't the various automated testing bots run sparse also on linux-next?

Quoted 24 lines
Quoted 16 lines
Fixes 1291523f2c1d ("mm, slab/slub: introduce kmalloc-reclaimable caches")
Cc Vlastimil Babka <[email protected]>
Cc Mel Gorman <[email protected]>
Cc Christoph Lameter <[email protected]>
Cc Roman Gushchin <[email protected]>
---
 include/linux/slab.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
include/linux/slab.h +0 -0
index 918f374e7156..97d0599ddb7b 100644

include/linux/slab.h static __always_inline enum kmalloc_cache_type kmalloc_type(gfp_t flags) · line 332

Quoted 5 lines
include/linux/slab.h +1 -1
@@ -329,7 +329,7 @@ static __always_inline enum kmalloc_cache_type kmalloc_type(gfp_t flags)
 	 * If an allocation is both __GFP_DMA and __GFP_RECLAIMABLE, return
 	 * KMALLOC_DMA and effectively ignore __GFP_RECLAIMABLE
 	 */
-	return type_dma + (is_reclaimable & !is_dma) * KMALLOC_RECLAIM;
+	return type_dma + is_reclaimable * !is_dma * KMALLOC_RECLAIM;

ISTM that changing is_dma and is_reclaimable from int to bool will stop the bleating.

Quoted 1 line

It is also strange that this code is trying so hard here to avoid conditional instructions

I primarily wanted to avoid branches in a hot path, not cmovs. Note those are also not "free" (latency-wise) if the result of cmov is immediately used for further computation.

Quoted 1 line

and then uses several to generate the boolean values in the first place.

I'm not sure where exactly?

Quoted 8 lines

OTOH I'd probably write:

	int gfp_dma = 0;
#ifdef CONFIG_ZONE_DMA
	gfp_dma = __GFP_DMA;

#endif

	return flags & gfp_dma ? KMALLOC_DMA : flags & __GFP_RECLAIMABLE ? KMALLOC_RECLAIM : 0;

I'm not opposed to this. Christoph might :)

Quoted 5 lines

That might generate cmovs, but is may be better to put unlikely() around both conditional expressions. Or redo as:

	return !unlikely(flags & (dfp_dma | __GFP_RECLAIMABLE)) ? 0 : flags & gfp_dma ? KMALLOC_DMA : KMALLOC_RECLAIM;

I guess it should be structured so that the fast path is for gfp without both __GFP_DMA and __GFP_RECLAIMABLE, with a single test+branch. IIRC that's what Christoph originally requested.

Quoted 6 lines

David

-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK Registration No: 1397386 (Wales)

#16 lore raw

From: Vlastimil Babka [mailto:[email protected]]

Quoted 1 line

Sent: 06 November 2018 10:22

...

Quoted 6 lines
Quoted 6 lines

include/linux/slab.h static __always_inline enum kmalloc_cache_type kmalloc_type(gfp_t flags) · line 332

Quoted 2 lines
include/linux/slab.h +1 -1
-	return type_dma + (is_reclaimable & !is_dma) * KMALLOC_RECLAIM;
+	return type_dma + is_reclaimable * !is_dma * KMALLOC_RECLAIM;

ISTM that changing is_dma and is_reclaimable from int to bool will stop the bleating.

It is also strange that this code is trying so hard here to avoid conditional instructions

I've done some experiments, compiled with gcc 4.7.3 and -O2 The constants match those from the kernel headers.

It is noticable that there isn't a cmov in sight.
The code would also be better if the KMALLOC constants matched the GFP ones.

#define __GFP_DMA 1u
#define __GFP_RECLAIM 0x10u

#define KMALLOC_DMA 2
#define KMALLOC_RECLAIM 1

unsigned int f(unsigned int flags)

{
        return flags & __GFP_DMA ? KMALLOC_DMA : flags & __GFP_RECLAIM ? KMALLOC_RECLAIM : 0;
}

unsigned int f1(unsigned int flags)

{
        return !__builtin_expect(flags & (__GFP_DMA | __GFP_RECLAIM), 0) ? 0 : flags & __GFP_DMA ? KMALLOC_DMA : KMALLOC_RECLAIM;
}

unsigned int f2(unsigned int flags)

{
        int is_dma, type_dma, is_rec;

        is_dma = !!(flags & __GFP_DMA);
        type_dma = is_dma * KMALLOC_DMA;
        is_rec = !!(flags & __GFP_RECLAIM);

        return type_dma + (is_rec & !is_dma) * KMALLOC_RECLAIM;
}

unsigned int f3(unsigned int flags)

{
        int is_dma, type_dma, is_rec;

        is_dma = !!(flags & __GFP_DMA);
        type_dma = is_dma * KMALLOC_DMA;
        is_rec = !!(flags & __GFP_RECLAIM);

        return type_dma + (is_rec * !is_dma) * KMALLOC_RECLAIM;
}

Disassembly of section .text:

0000000000000000 <f>:

   0:   40 f6 c7 01             test   $0x1,%dil
   4:   b8 02 00 00 00          mov    $0x2,%eax
   9:   74 05                   je     10 <f+0x10>
   b:   f3 c3                   repz retq
   d:   0f 1f 00                nopl   (%rax)
  10:   89 f8                   mov    %edi,%eax
  12:   c1 e8 04                shr    $0x4,%eax
  15:   83 e0 01                and    $0x1,%eax
  18:   c3                      retq

This one has a misprediced branch in the common path.

0000000000000020 <f1>:

  20:   40 f6 c7 11             test   $0x11,%dil
  24:   75 03                   jne    29 <f1+0x9>
  26:   31 c0                   xor    %eax,%eax
  28:   c3                      retq
  29:   83 e7 01                and    $0x1,%edi
  2c:   83 ff 01                cmp    $0x1,%edi
  2f:   19 c0                   sbb    %eax,%eax
  31:   83 c0 02                add    $0x2,%eax
  34:   c3                      retq

The jne will be predicted not taken and the retq predicted. So this might only be 1 clock in the normal case.

0000000000000040 <f2>:

  40:   89 f8                   mov    %edi,%eax
  42:   c1 ef 04                shr    $0x4,%edi
  45:   83 e0 01                and    $0x1,%eax
  48:   89 c2                   mov    %eax,%edx
  4a:   83 f2 01                xor    $0x1,%edx
  4d:   21 d7                   and    %edx,%edi
  4f:   8d 04 47                lea    (%rdi,%rax,2),%eax
  52:   c3                      retq

No conditionals, but a 4 deep dependency chain.

0000000000000060 <f3>:

  60:   89 fa                   mov    %edi,%edx
  62:   c1 ef 04                shr    $0x4,%edi
  65:   83 e2 01                and    $0x1,%edx
  68:   83 e7 01                and    $0x1,%edi
  6b:   89 d0                   mov    %edx,%eax
  6d:   83 f0 01                xor    $0x1,%eax
  70:   0f af c7                imul   %edi,%eax
  73:   8d 04 50                lea    (%rax,%rdx,2),%eax
  76:   c3                      retq

You really don't want the multiply.

David

-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK Registration No: 1397386 (Wales)

#17 lore raw
Quoted 8 lines · David Laight

From: Vlastimil Babka [mailto:[email protected]]

Quoted 1 line

Sent: 06 November 2018 10:22

...

Quoted 5 lines
Quoted 5 lines

include/linux/slab.h static __always_inline enum kmalloc_cache_type kmalloc_type(gfp_t flags) · line 332

Quoted 2 lines
include/linux/slab.h +1 -1
-	return type_dma + (is_reclaimable & !is_dma) * KMALLOC_RECLAIM;
+	return type_dma + is_reclaimable * !is_dma * KMALLOC_RECLAIM;

ISTM that changing is_dma and is_reclaimable from int to bool will stop the bleating.

Quoted 6 lines
Quoted 1 line
Quoted 1 line

It is also strange that this code is trying so hard here to avoid conditional instructions

I've done some experiments, compiled with gcc 4.7.3 and -O2 The constants match those from the kernel headers.

It is noticable that there isn't a cmov in sight.

There is with newer gcc: https://godbolt.org/z/qFdByQ

But even that didn't remove the imul in f3() so that's indeed a bust.

Quoted 1 line

The code would also be better if the KMALLOC constants matched the GFP ones.

That would be hard, as __GFP flags have also other constraints (especially __GFP_DMA relative to other zone restricting __GFP flags) and KMALLOC_* are used as array index.

Quoted 5 lines

unsigned int f1(unsigned int flags)

{
        return !__builtin_expect(flags & (__GFP_DMA | __GFP_RECLAIM), 0) ? 0 : flags & __GFP_DMA ? KMALLOC_DMA : KMALLOC_RECLAIM;
}

...

Quoted 11 lines

0000000000000020 <f1>:

  20:   40 f6 c7 11             test   $0x11,%dil
  24:   75 03                   jne    29 <f1+0x9>
  26:   31 c0                   xor    %eax,%eax
  28:   c3                      retq
  29:   83 e7 01                and    $0x1,%edi
  2c:   83 ff 01                cmp    $0x1,%edi
  2f:   19 c0                   sbb    %eax,%eax
  31:   83 c0 02                add    $0x2,%eax
  34:   c3                      retq
Quoted 2 lines

The jne will be predicted not taken and the retq predicted. So this might only be 1 clock in the normal case.

I think this is the winner. It's also a single branch and not two, because the compiler could figure out some of the "clever arithmetics" itself. Care to send a full patch?

#18 lore raw

From: Vlastimil Babka

Quoted 8 lines

Sent: 06 November 2018 12:51

Quoted 5 lines · David Laight

From: Vlastimil Babka [mailto:[email protected]]

Quoted 1 line

Sent: 06 November 2018 10:22

...

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
include/linux/slab.h +1 -1
-	return type_dma + (is_reclaimable & !is_dma) * KMALLOC_RECLAIM;
+	return type_dma + is_reclaimable * !is_dma * KMALLOC_RECLAIM;

...

Quoted 7 lines
Quoted 4 lines

I've done some experiments, compiled with gcc 4.7.3 and -O2 The constants match those from the kernel headers.

It is noticable that there isn't a cmov in sight.

There is with newer gcc: https://godbolt.org/z/qFdByQ

Quoted 7 lines

But even that didn't remove the imul in f3() so that's indeed a bust.

Quoted 1 line

The code would also be better if the KMALLOC constants matched the GFP ones.

That would be hard, as __GFP flags have also other constraints (especially __GFP_DMA relative to other zone restricting __GFP flags) and KMALLOC_* are used as array index.

Hmmm...
With only 2 or three values conditionals are probably better than table lookups - especially if they are function pointers.

Quoted 21 lines
Quoted 4 lines

unsigned int f1(unsigned int flags)

{
        return !__builtin_expect(flags & (__GFP_DMA | __GFP_RECLAIM), 0) ? 0 : flags & __GFP_DMA ?

KMALLOC_DMA : KMALLOC_RECLAIM;

Quoted 2 lines

}

...

Quoted 11 lines

0000000000000020 <f1>:

  20:   40 f6 c7 11             test   $0x11,%dil
  24:   75 03                   jne    29 <f1+0x9>
  26:   31 c0                   xor    %eax,%eax
  28:   c3                      retq
  29:   83 e7 01                and    $0x1,%edi
  2c:   83 ff 01                cmp    $0x1,%edi
  2f:   19 c0                   sbb    %eax,%eax
  31:   83 c0 02                add    $0x2,%eax
  34:   c3                      retq
Quoted 6 lines
Quoted 2 lines

The jne will be predicted not taken and the retq predicted. So this might only be 1 clock in the normal case.

I think this is the winner. It's also a single branch and not two, because the compiler could figure out some of the "clever arithmetics" itself. Care to send a full patch?

I've not got a suitable source tree lurking.
So someone else would need to do it.
I'll waive any copyright that could plausibly be assigned to the above!

David

-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK Registration No: 1397386 (Wales)

#19 lore raw
Quoted 19 lines · David Laight

From: Vlastimil Babka

Quoted 18 lines

Sent: 06 November 2018 12:51

Quoted 14 lines · David Laight

From: Vlastimil Babka [mailto:[email protected]]
0000000000000020 <f1>:

  20:   40 f6 c7 11             test   $0x11,%dil
  24:   75 03                   jne    29 <f1+0x9>
  26:   31 c0                   xor    %eax,%eax
  28:   c3                      retq
  29:   83 e7 01                and    $0x1,%edi
  2c:   83 ff 01                cmp    $0x1,%edi
  2f:   19 c0                   sbb    %eax,%eax
  31:   83 c0 02                add    $0x2,%eax
  34:   c3                      retq

The jne will be predicted not taken and the retq predicted. So this might only be 1 clock in the normal case.

Quoted 7 lines
Quoted 3 lines

I think this is the winner. It's also a single branch and not two, because the compiler could figure out some of the "clever arithmetics" itself. Care to send a full patch?

I've not got a suitable source tree lurking.
So someone else would need to do it.
I'll waive any copyright that could plausibly be assigned to the above!

There we go. This is to replace the current fix by Bart (sorry) which seems to add an extra IMUL. Apparently current mainline is spamming anyone running sparse with lots of warning, so it should be merged soon.

----8<----
From ddd2fc6fcba425733f8320413a1451410687c9c3 Mon Sep 17 00:00:00 2001
From: Vlastimil Babka <[email protected]>
Date: Fri, 9 Nov 2018 08:47:12 +0100
Subject: [PATCH] mm, slab: fix sparse warning in kmalloc_type()

Multiple people have reported the following sparse warning:

./include/linux/slab.h:332:43: warning: dubious: x & !y

The minimal fix would be to change the logical & to boolean &&, which emits the same code, but Andrew has suggested that the branch-avoiding tricks are maybe not worthwile. David Laight provided a nice comparison of disassembly of multiple variants, which shows that the current version produces a 4 deep dependency chain, and fixing the sparse warning by changing logical and to multiplication emits an IMUL, making it even more expensive.

The code as rewritten by this patch yielded the best disassembly, with a single predictable branch for the most common case, and a ternary operator for the rest, which gcc seems to compile without a branch or cmov by itself.

The result should be more readable, without a sparse warning and probably also faster for the common case.

Fixes 1291523f2c1d ("mm, slab/slub: introduce kmalloc-reclaimable caches")
---
 include/linux/slab.h | 24 ++++++++++++------------
 1 file changed, 12 insertions(+), 12 deletions(-)
include/linux/slab.h +12 -12
index 918f374e7156..18c6920c2803 100644
@@ -304,6 +304,8 @@ enum kmalloc_cache_type {
 	KMALLOC_RECLAIM,
 #ifdef CONFIG_ZONE_DMA
 	KMALLOC_DMA,
+#else
+	KMALLOC_DMA = KMALLOC_NORMAL,
 #endif
 	NR_KMALLOC_TYPES
 };
@@ -314,22 +316,20 @@ kmalloc_caches[NR_KMALLOC_TYPES][KMALLOC_SHIFT_HIGH + 1];
 
 static __always_inline enum kmalloc_cache_type kmalloc_type(gfp_t flags)
 {
-	int is_dma = 0;
-	int type_dma = 0;
-	int is_reclaimable;
-
-#ifdef CONFIG_ZONE_DMA
-	is_dma = !!(flags & __GFP_DMA);
-	type_dma = is_dma * KMALLOC_DMA;
-#endif
+	int gfp_dma = IS_ENABLED(CONFIG_ZONE_DMA) ? __GFP_DMA : 0;
 
-	is_reclaimable = !!(flags & __GFP_RECLAIMABLE);
+	/*
+	 * The most common case is KMALLOC_NORMAL, so test for it
+	 * with a single branch for both flags.
+	 */
+	if (likely((flags & (gfp_dma | __GFP_RECLAIMABLE)) == 0))
+		return KMALLOC_NORMAL;
 
 	/*
-	 * If an allocation is both __GFP_DMA and __GFP_RECLAIMABLE, return
-	 * KMALLOC_DMA and effectively ignore __GFP_RECLAIMABLE
+	 * At least one of the flags has to be set. If both are, __GFP_DMA
+	 * is more important.
 	 */
-	return type_dma + (is_reclaimable & !is_dma) * KMALLOC_RECLAIM;
+	return flags & gfp_dma ? KMALLOC_DMA : KMALLOC_RECLAIM;
 }
 
 /*
Signature · 1 line
2.19.1
maintainer
#20 lore raw
Quoted 33 lines · On Fri, 9 Nov 2018 09:12:09 +0100 Vlastimil Babka <[email protected]> wrote:

Multiple people have reported the following sparse warning:

./include/linux/slab.h:332:43: warning: dubious: x & !y

The minimal fix would be to change the logical & to boolean &&, which emits the same code, but Andrew has suggested that the branch-avoiding tricks are maybe not worthwile. David Laight provided a nice comparison of disassembly of multiple variants, which shows that the current version produces a 4 deep dependency chain, and fixing the sparse warning by changing logical and to multiplication emits an IMUL, making it even more expensive.

The code as rewritten by this patch yielded the best disassembly, with a single predictable branch for the most common case, and a ternary operator for the rest, which gcc seems to compile without a branch or cmov by itself.

The result should be more readable, without a sparse warning and probably also faster for the common case.

Fixes 1291523f2c1d ("mm, slab/slub: introduce kmalloc-reclaimable caches")
---
 include/linux/slab.h | 24 ++++++++++++------------
 1 file changed, 12 insertions(+), 12 deletions(-)
include/linux/slab.h +0 -0
index 918f374e7156..18c6920c2803 100644

include/linux/slab.h enum kmalloc_cache_type { · line 311

Quoted 8 lines
include/linux/slab.h +2 -0
@@ -304,6 +304,8 @@ enum kmalloc_cache_type {
 	KMALLOC_RECLAIM,
 #ifdef CONFIG_ZONE_DMA
 	KMALLOC_DMA,
+#else
+	KMALLOC_DMA = KMALLOC_NORMAL,
 #endif
 	NR_KMALLOC_TYPES
 };

I don't think this works correctly. Resetting KMALLOC_DMA to 0 will cause NR_KMALLOC_TYPES to have value 1.

enum foo {
	a = 0,
	b,
	c = 0,
	d
}

main()

{
	printf("%d %d %d %d\n", a, b, c, d);
}

akpm3> ./a.out
0 1 0 1

#21 lore raw
Quoted 30 lines · Andrew Morton
Quoted 28 lines · On Fri, 9 Nov 2018 09:12:09 +0100 Vlastimil Babka <[email protected]> wrote:

Multiple people have reported the following sparse warning:

./include/linux/slab.h:332:43: warning: dubious: x & !y

The minimal fix would be to change the logical & to boolean &&, which emits the same code, but Andrew has suggested that the branch-avoiding tricks are maybe not worthwile. David Laight provided a nice comparison of disassembly of multiple variants, which shows that the current version produces a 4 deep dependency chain, and fixing the sparse warning by changing logical and to multiplication emits an IMUL, making it even more expensive.

The code as rewritten by this patch yielded the best disassembly, with a single predictable branch for the most common case, and a ternary operator for the rest, which gcc seems to compile without a branch or cmov by itself.

The result should be more readable, without a sparse warning and probably also faster for the common case.

Fixes 1291523f2c1d ("mm, slab/slub: introduce kmalloc-reclaimable caches")
---
 include/linux/slab.h | 24 ++++++++++++------------
 1 file changed, 12 insertions(+), 12 deletions(-)
Quoted 16 lines

include/linux/slab.h enum kmalloc_cache_type { · line 311

Quoted 13 lines
include/linux/slab.h +2 -0
index 918f374e7156..18c6920c2803 100644
@@ -304,6 +304,8 @@ enum kmalloc_cache_type {
 	KMALLOC_RECLAIM,
 #ifdef CONFIG_ZONE_DMA
 	KMALLOC_DMA,
+#else
+	KMALLOC_DMA = KMALLOC_NORMAL,
 #endif
 	NR_KMALLOC_TYPES
 };

I don't think this works correctly. Resetting KMALLOC_DMA to 0 will cause NR_KMALLOC_TYPES to have value 1.

Doh, right! Thanks for catching this.

This? Not terribly elegant, but I don't see a nicer way right now...

----8<----
From 40b84707e1b5aeccff11bd5f0563bb938e2c22d6 Mon Sep 17 00:00:00 2001
From: Vlastimil Babka <[email protected]>
Date: Fri, 9 Nov 2018 08:47:12 +0100
Subject: [PATCH] mm, slab: fix sparse warning in kmalloc_type()

Multiple people have reported the following sparse warning:

./include/linux/slab.h:332:43: warning: dubious: x & !y

The minimal fix would be to change the logical & to boolean &&, which emits the same code, but Andrew has suggested that the branch-avoiding tricks are maybe not worthwile. David Laight provided a nice comparison of disassembly of multiple variants, which shows that the current version produces a 4 deep dependency chain, and fixing the sparse warning by changing logical and to multiplication emits an IMUL, making it even more expensive.

The code as rewritten by this patch yielded the best disassembly, with a single predictable branch for the most common case, and a ternary operator for the rest, which gcc seems to compile without a branch or cmov by itself.

The result should be more readable, without a sparse warning and probably also faster for the common case.

Fixes 1291523f2c1d ("mm, slab/slub: introduce kmalloc-reclaimable caches")
---
 include/linux/slab.h | 26 ++++++++++++++------------
 1 file changed, 14 insertions(+), 12 deletions(-)
include/linux/slab.h +14 -12
index 918f374e7156..ca113d4ecc6f 100644
@@ -314,22 +314,24 @@ kmalloc_caches[NR_KMALLOC_TYPES][KMALLOC_SHIFT_HIGH + 1];
 
 static __always_inline enum kmalloc_cache_type kmalloc_type(gfp_t flags)
 {
-	int is_dma = 0;
-	int type_dma = 0;
-	int is_reclaimable;
+	int gfp_dma = IS_ENABLED(CONFIG_ZONE_DMA) ? __GFP_DMA : 0;
 
-#ifdef CONFIG_ZONE_DMA
-	is_dma = !!(flags & __GFP_DMA);
-	type_dma = is_dma * KMALLOC_DMA;
-#endif
-
-	is_reclaimable = !!(flags & __GFP_RECLAIMABLE);
+	/*
+	 * The most common case is KMALLOC_NORMAL, so test for it
+	 * with a single branch for both flags.
+	 */
+	if (likely((flags & (gfp_dma | __GFP_RECLAIMABLE)) == 0))
+		return KMALLOC_NORMAL;
 
 	/*
-	 * If an allocation is both __GFP_DMA and __GFP_RECLAIMABLE, return
-	 * KMALLOC_DMA and effectively ignore __GFP_RECLAIMABLE
+	 * At least one of the flags has to be set. If both are, __GFP_DMA
+	 * is more important.
 	 */
-	return type_dma + (is_reclaimable & !is_dma) * KMALLOC_RECLAIM;
+#ifdef CONFIG_ZONE_DMA
+	return flags & gfp_dma ? KMALLOC_DMA : KMALLOC_RECLAIM;
+#else
+	return KMALLOC_RECLAIM;
+#endif
 }
 
 /*
Signature · 1 line
2.19.1
#22 lore raw
Quoted 48 lines · Vlastimil Babka
Quoted 30 lines · Andrew Morton
Quoted 28 lines · On Fri, 9 Nov 2018 09:12:09 +0100 Vlastimil Babka <[email protected]> wrote:

Multiple people have reported the following sparse warning:

./include/linux/slab.h:332:43: warning: dubious: x & !y

The minimal fix would be to change the logical & to boolean &&, which emits the same code, but Andrew has suggested that the branch-avoiding tricks are maybe not worthwile. David Laight provided a nice comparison of disassembly of multiple variants, which shows that the current version produces a 4 deep dependency chain, and fixing the sparse warning by changing logical and to multiplication emits an IMUL, making it even more expensive.

The code as rewritten by this patch yielded the best disassembly, with a single predictable branch for the most common case, and a ternary operator for the rest, which gcc seems to compile without a branch or cmov by itself.

The result should be more readable, without a sparse warning and probably also faster for the common case.

Fixes 1291523f2c1d ("mm, slab/slub: introduce kmalloc-reclaimable caches")
---
 include/linux/slab.h | 24 ++++++++++++------------
 1 file changed, 12 insertions(+), 12 deletions(-)
Quoted 16 lines

include/linux/slab.h enum kmalloc_cache_type { · line 311

Quoted 13 lines
include/linux/slab.h +2 -0
index 918f374e7156..18c6920c2803 100644
@@ -304,6 +304,8 @@ enum kmalloc_cache_type {
 	KMALLOC_RECLAIM,
 #ifdef CONFIG_ZONE_DMA
 	KMALLOC_DMA,
+#else
+	KMALLOC_DMA = KMALLOC_NORMAL,
 #endif
 	NR_KMALLOC_TYPES
 };

I don't think this works correctly. Resetting KMALLOC_DMA to 0 will cause NR_KMALLOC_TYPES to have value 1.

Quoted 4 lines

Doh, right! Thanks for catching this.

This? Not terribly elegant, but I don't see a nicer way right now...

How about the solution I proposed yesterday?

https://lkml.org/lkml/2018/11/9/750

It doesn't involve any tricks.

As it is, this sparse warning is begging for a trick. Let's not oblidge it to much.

#23 lore raw
Quoted 51 lines · Darryl T. Agostinelli
Quoted 50 lines · Vlastimil Babka
Quoted 30 lines · Andrew Morton
Quoted 28 lines · On Fri, 9 Nov 2018 09:12:09 +0100 Vlastimil Babka <[email protected]> wrote:

Multiple people have reported the following sparse warning:

./include/linux/slab.h:332:43: warning: dubious: x & !y

The minimal fix would be to change the logical & to boolean &&, which emits the same code, but Andrew has suggested that the branch-avoiding tricks are maybe not worthwile. David Laight provided a nice comparison of disassembly of multiple variants, which shows that the current version produces a 4 deep dependency chain, and fixing the sparse warning by changing logical and to multiplication emits an IMUL, making it even more expensive.

The code as rewritten by this patch yielded the best disassembly, with a single predictable branch for the most common case, and a ternary operator for the rest, which gcc seems to compile without a branch or cmov by itself.

The result should be more readable, without a sparse warning and probably also faster for the common case.

Fixes 1291523f2c1d ("mm, slab/slub: introduce kmalloc-reclaimable caches")
---
 include/linux/slab.h | 24 ++++++++++++------------
 1 file changed, 12 insertions(+), 12 deletions(-)
Quoted 16 lines

include/linux/slab.h enum kmalloc_cache_type { · line 311

Quoted 13 lines
include/linux/slab.h +2 -0
index 918f374e7156..18c6920c2803 100644
@@ -304,6 +304,8 @@ enum kmalloc_cache_type {
 	KMALLOC_RECLAIM,
 #ifdef CONFIG_ZONE_DMA
 	KMALLOC_DMA,
+#else
+	KMALLOC_DMA = KMALLOC_NORMAL,
 #endif
 	NR_KMALLOC_TYPES
 };

I don't think this works correctly. Resetting KMALLOC_DMA to 0 will cause NR_KMALLOC_TYPES to have value 1.

Doh, right! Thanks for catching this.

Quoted 8 lines
Quoted 2 lines

This? Not terribly elegant, but I don't see a nicer way right now...

How about the solution I proposed yesterday?

https://lkml.org/lkml/2018/11/9/750

It doesn't involve any tricks.

It doesn't remove the "trick" that calculates return value as a sum of booleans multiplying constants. The patch converts one part of the expression of those booleans to a ternary operator. I think the result is even harder to follow and meanwhile Andrew's suggestion was to remove all the tricks.

Quoted 2 lines

As it is, this sparse warning is begging for a trick. Let's not oblidge it to much.

The sparse warning could be silenced just by changing '&' to '&&' which would emit the same code. But we decided to untrick the code.

#24 lore raw
In reply to Vlastimil Babka

From: Vlastimil Babka [mailto:[email protected]]

Quoted 1 line

Sent: 09 November 2018 19:16

...

Quoted 1 line

This? Not terribly elegant, but I don't see a nicer way right now...

Maybe just have two copies of the function body?

static __always_inline enum kmalloc_cache_type kmalloc_type(gfp_t flags)
{

#ifndef CONFIG_ZONE_DMA
	return flags & __GFP_RECLAIMABLE ? KMALLOC_RECLAIM : KMALLOC_NORMAL;
#else
	if (likely((flags & (__GFP_DMA | __GFP_RECLAIMABLE)) == 0))
		return KMALLOC_NORMAL;
	return flags & __GFP_DMA ? KMALLOC_DMA : KMALLOC_RECLAIM;

#endif
}

David

-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK Registration No: 1397386 (Wales)

#25 lore raw
Quoted 9 lines · David Laight

From: Vlastimil Babka [mailto:[email protected]]

Quoted 1 line

Sent: 09 November 2018 19:16

...

Quoted 1 line

This? Not terribly elegant, but I don't see a nicer way right now...

Maybe just have two copies of the function body?

static __always_inline enum kmalloc_cache_type kmalloc_type(gfp_t flags)
{

Quoted 8 lines
#ifndef CONFIG_ZONE_DMA
	return flags & __GFP_RECLAIMABLE ? KMALLOC_RECLAIM : KMALLOC_NORMAL;
#else
	if (likely((flags & (__GFP_DMA | __GFP_RECLAIMABLE)) == 0))
		return KMALLOC_NORMAL;
	return flags & __GFP_DMA ? KMALLOC_DMA : KMALLOC_RECLAIM;

#endif
}

OK that's probably the most straightforward to follow, thanks.
Note that for CONFIG_ZONE_DMA=n the result is identical to original code and all other attempts. flags & __GFP_DMA is converted to 1/0 index without branches or cmovs or whatnot.

----8<----
From 40735b637b28c3e5798bc7e90f72f349050c2045 Mon Sep 17 00:00:00 2001
From: Vlastimil Babka <[email protected]>
Date: Fri, 9 Nov 2018 08:47:12 +0100
Subject: [PATCH] mm, slab: fix sparse warning in kmalloc_type()

Multiple people have reported the following sparse warning:

./include/linux/slab.h:332:43: warning: dubious: x & !y

The minimal fix would be to change the logical & to boolean &&, which emits the same code, but Andrew has suggested that the branch-avoiding tricks are maybe not worthwile. David Laight provided a nice comparison of disassembly of multiple variants, which shows that the current version produces a 4 deep dependency chain, and fixing the sparse warning by changing logical and to multiplication emits an IMUL, making it even more expensive.

The code as rewritten by this patch yielded the best disassembly, with a single predictable branch for the most common case, and a ternary operator for the rest, which gcc seems to compile without a branch or cmov by itself.

The result should be more readable, without a sparse warning and probably also faster for the common case.

Fixes 1291523f2c1d ("mm, slab/slub: introduce kmalloc-reclaimable caches")
---
 include/linux/slab.h | 24 ++++++++++++------------
 1 file changed, 12 insertions(+), 12 deletions(-)
include/linux/slab.h +12 -12
index 918f374e7156..6d5009f29ce5 100644
@@ -314,22 +314,22 @@ kmalloc_caches[NR_KMALLOC_TYPES][KMALLOC_SHIFT_HIGH + 1];
 
 static __always_inline enum kmalloc_cache_type kmalloc_type(gfp_t flags)
 {
-	int is_dma = 0;
-	int type_dma = 0;
-	int is_reclaimable;
-
 #ifdef CONFIG_ZONE_DMA
-	is_dma = !!(flags & __GFP_DMA);
-	type_dma = is_dma * KMALLOC_DMA;
-#endif
-
-	is_reclaimable = !!(flags & __GFP_RECLAIMABLE);
+	/*
+	 * The most common case is KMALLOC_NORMAL, so test for it
+	 * with a single branch for both flags.
+	 */
+	if (likely((flags & (__GFP_DMA | __GFP_RECLAIMABLE)) == 0))
+		return KMALLOC_NORMAL;
 
 	/*
-	 * If an allocation is both __GFP_DMA and __GFP_RECLAIMABLE, return
-	 * KMALLOC_DMA and effectively ignore __GFP_RECLAIMABLE
+	 * At least one of the flags has to be set. If both are, __GFP_DMA
+	 * is more important.
 	 */
-	return type_dma + (is_reclaimable & !is_dma) * KMALLOC_RECLAIM;
+	return flags & __GFP_DMA ? KMALLOC_DMA : KMALLOC_RECLAIM;
+#else
+	return flags & __GFP_RECLAIMABLE ? KMALLOC_RECLAIM : KMALLOC_NORMAL;
+#endif
 }
 
 /*
Signature · 1 line
2.19.1
#26 lore raw
Quoted 19 lines · Vlastimil Babka
Quoted 9 lines · David Laight

From: Vlastimil Babka [mailto:[email protected]]

Quoted 1 line

Sent: 09 November 2018 19:16

...

Quoted 1 line

This? Not terribly elegant, but I don't see a nicer way right now...

Maybe just have two copies of the function body?

static __always_inline enum kmalloc_cache_type kmalloc_type(gfp_t flags)
{

Quoted 8 lines
#ifndef CONFIG_ZONE_DMA
	return flags & __GFP_RECLAIMABLE ? KMALLOC_RECLAIM : KMALLOC_NORMAL;
#else
	if (likely((flags & (__GFP_DMA | __GFP_RECLAIMABLE)) == 0))
		return KMALLOC_NORMAL;
	return flags & __GFP_DMA ? KMALLOC_DMA : KMALLOC_RECLAIM;

#endif
}

Quoted 4 lines

OK that's probably the most straightforward to follow, thanks.
Note that for CONFIG_ZONE_DMA=n the result is identical to original code and all other attempts. flags & __GFP_DMA is converted to 1/0 index without branches or cmovs or whatnot.

Ping? Seems like people will report duplicates until the sparse warning is gone in mainline...

Also CC linux-mm which was somehow lost.

----8<----

#27 lore raw
In reply to Bart Van Assche
Quoted 19 lines · Bart Van Assche

This patch suppresses the following sparse warning:

./include/linux/slab.h:332:43: warning: dubious: x & !y

Fixes 1291523f2c1d ("mm, slab/slub: introduce kmalloc-reclaimable caches")
Cc Vlastimil Babka <[email protected]>
Cc Mel Gorman <[email protected]>
Cc Christoph Lameter <[email protected]>
Cc Roman Gushchin <[email protected]>
---
 include/linux/slab.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
include/linux/slab.h +0 -0
index 918f374e7156..97d0599ddb7b 100644

include/linux/slab.h static __always_inline enum kmalloc_cache_type kmalloc_type(gfp_t flags) · line 334

Quoted 7 lines
include/linux/slab.h +1 -1
@@ -329,7 +329,7 @@ static __always_inline enum kmalloc_cache_type kmalloc_type(gfp_t flags)
 	 * If an allocation is both __GFP_DMA and __GFP_RECLAIMABLE, return
 	 * KMALLOC_DMA and effectively ignore __GFP_RECLAIMABLE
 	 */
-	return type_dma + (is_reclaimable & !is_dma) * KMALLOC_RECLAIM;
+	return type_dma + is_reclaimable * !is_dma * KMALLOC_RECLAIM;
 }
 

What is wrong with && ? If logical and is better done by multiply, that's compiler job, and compiler should be fixed to do it...

Pavel

Signature · 2 lines
(english) http://www.livejournal.com/~pavelmachek
(cesky, pictures) http://atrey.karlin.mff.cuni.cz/~pavel/picture/horses/blog.html
#28 lore raw
Quoted 15 lines · Pavel Machek
Quoted 14 lines · Bart Van Assche

This patch suppresses the following sparse warning:

./include/linux/slab.h:332:43: warning: dubious: x & !y

Fixes 1291523f2c1d ("mm, slab/slub: introduce kmalloc-reclaimable caches")
Cc Vlastimil Babka <[email protected]>
Cc Mel Gorman <[email protected]>
Cc Christoph Lameter <[email protected]>
Cc Roman Gushchin <[email protected]>
---
 include/linux/slab.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 14 lines

include/linux/slab.h static __always_inline enum kmalloc_cache_type kmalloc_type(gfp_t flags) · line 334

Quoted 12 lines
include/linux/slab.h +1 -1
index 918f374e7156..97d0599ddb7b 100644
@@ -329,7 +329,7 @@ static __always_inline enum kmalloc_cache_type kmalloc_type(gfp_t flags)
 	 * If an allocation is both __GFP_DMA and __GFP_RECLAIMABLE, return
 	 * KMALLOC_DMA and effectively ignore __GFP_RECLAIMABLE
 	 */
-	return type_dma + (is_reclaimable & !is_dma) * KMALLOC_RECLAIM;
+	return type_dma + is_reclaimable * !is_dma * KMALLOC_RECLAIM;
 }
 

What is wrong with && ?

Nothing, it would work and generate the same assembly as '&'. But Andrew noted that this code is probably too clever for its own good, and he has a point. The single predictable branch is also likely faster than the chain of arithmetic calculations anyway. Nobody has actually measured it, so I'd go with the easier-to-read variant.

Quoted 2 lines

If logical and is better done by multiply,
that's compiler job, and compiler should be fixed to do it...

Multiply was just another way (equivalent to '&&' semantically) to shut up sparse warning. But gcc actually emits IMUL in that case, which is wasteful, so yeah there's a bug report now:
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=87954

Quoted 3 lines

Pavel