Skip to contentSkip to conversation

mlx4: Fix 1<<31 expressions

PATCH In review Jason Gunthorpe 5 participants 12 messages Feb 8, 2018 – Feb 9, 2018 9 messages not in the archive InfiniBand

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 mlx4: Fix 1<<31 expressions

They produce the value INT32_MIN not 0x80000000. This is sometimes OK if it is casted appropriately but leaves a subtle trap to the user.

---
 providers/mlx4/mlx4dv.h | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
providers/mlx4/mlx4dv.h +3 -3
index d47d3cd2e1f135..5312a866b6e281 100644
@@ -288,12 +288,12 @@ enum {
 };
 
 enum {
-	MLX4_WQE_BIND_TYPE_2		= (1<<31),
+	MLX4_WQE_BIND_TYPE_2		= (1UL<<31),
 	MLX4_WQE_BIND_ZERO_BASED	= (1<<30),
 };
 
 enum {
-	MLX4_INLINE_SEG		= 1 << 31,
+	MLX4_INLINE_SEG		= 1UL << 31,
 	MLX4_INLINE_ALIGN	= 64,
 };
 
@@ -304,7 +304,7 @@ enum {
 enum {
 	MLX4_WQE_MW_REMOTE_READ   = 1 << 29,
 	MLX4_WQE_MW_REMOTE_WRITE  = 1 << 30,
-	MLX4_WQE_MW_ATOMIC        = 1 << 31
+	MLX4_WQE_MW_ATOMIC        = 1UL << 31
 };
 
 struct mlx4_wqe_local_inval_seg {
Signature · 6 lines
2.16.1

--
To unsubscribe from this list: send the line "unsubscribe linux-rdma" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html

A message that is not in the archive (sent off-list or not yet received): 20180208233829.GA16128-uk2M96/[email protected]

#2 lore raw

T24gVGh1LCAyMDE4LTAyLTA4IGF0IDE2OjM4IC0wNzAwLCBKYXNvbiBHdW50aG9ycGUgd3JvdGU6 DQo+ICBlbnVtIHsNCj4gLQlNTFg0X1dRRV9CSU5EX1RZUEVfMgkJPSAoMTw8MzEpLA0KPiArCU1M WDRfV1FFX0JJTkRfVFlQRV8yCQk9ICgxVUw8PDMxKSwNCj4gIAlNTFg0X1dRRV9CSU5EX1pFUk9f QkFTRUQJPSAoMTw8MzApLA0KPiAgfTsNCg0KSGVsbG8gSmFzb24sDQoNCkEgcXVvdGUgZnJvbSB0 aGUgQzExIHN0YW5kYXJkOg0KDQoiVGhlIGV4cHJlc3Npb24gdGhhdCBkZWZpbmVzIHRoZSB2YWx1 ZSBvZiBhbiBlbnVtZXJhdGlvbiBjb25zdGFudCBzaGFsbCBiZSBhbg0KaW50ZWdlciBjb25zdGFu dCBleHByZXNzaW9uIHRoYXQgaGFzIGEgdmFsdWUgcmVwcmVzZW50YWJsZSBhcyBhbiBpbnQuIFsg Li4uIF0NCkVhY2ggZW51bWVyYXRlZCB0eXBlIHNoYWxsIGJlIGNvbXBhdGlibGUgd2l0aCBjaGFy LCBhIHNpZ25lZCBpbnRlZ2VyIHR5cGUsIG9yDQphbiB1bnNpZ25lZCBpbnRlZ2VyIHR5cGUuIFRo ZSBjaG9pY2Ugb2YgdHlwZSBpcyBpbXBsZW1lbnRhdGlvbi1kZWZpbmVkIGJ1dA0Kc2hhbGwgYmUg Y2FwYWJsZSBvZiByZXByZXNlbnRpbmcgdGhlIHZhbHVlcyBvZiBhbGwgdGhlIG1lbWJlcnMgb2Yg dGhlDQplbnVtZXJhdGlvbi4iDQoNCkRvZXMgdGhhdCBtZWFuIHRoYXQgdGhpcyBjaGFuZ2UgcmVs aWVzIG9uIGV4dGVuc2lvbnMgdG8gdGhlIEMgc3RhbmRhcmQ/DQoNClRoYW5rcywNCg0KQmFydC4= --
To unsubscribe from this list: send the line "unsubscribe linux-rdma" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html

A message that is not in the archive (sent off-list or not yet received): [email protected]

#4 lore raw
Quoted 11 lines · Bart Van Assche

providers/mlx4/mlx4dv.h enum { · line 293

Quoted 5 lines · Jason Gunthorpe

enum {

providers/mlx4/mlx4dv.h +1 -1
-	MLX4_WQE_BIND_TYPE_2		= (1<<31),
+	MLX4_WQE_BIND_TYPE_2		= (1UL<<31),
 	MLX4_WQE_BIND_ZERO_BASED	= (1<<30),
 };

Hello Jason,

A quote from the C11 standard:

Quoted 8 lines

"The expression that defines the value of an enumeration constant shall be an integer constant expression that has a value representable as an int. [ ... ] Each enumerated type shall be compatible with char, a signed integer type, or an unsigned integer type. The choice of type is implementation-defined but shall be capable of representing the values of all the members of the enumeration."

Does that mean that this change relies on extensions to the C standard?

Yes, this relies on a common compiler extension to promote the type of enum constants beyond int. AFAIK all compilers have done this forever, kinda bonkers it isn't in C11, IMHO.

As I understand it, the current code is also non-conforming as (1<<31) is apparently undefined behavior, and at least gcc throws a warning for it in -Wpedantic mode.

I did a patch that will make our public headers mostly strictly conforming to C11 with -Wpedantic set, which I've been wondering if it is worth sending..

Jason
--
To unsubscribe from this list: send the line "unsubscribe linux-rdma" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html

A message that is not in the archive (sent off-list or not yet received): 20180209161030.GA7584-VPRAkNaXOzVWk0Htik3J/[email protected]

#6 lore raw
Quoted 35 lines

-----Original Message-----
From: [email protected] [mailto:linux-rdma- [email protected]] On Behalf Of Jason Gunthorpe
Sent: Friday, February 09, 2018 10:11 AM
To: Bart Van Assche <[email protected]>
Cc: [email protected]
Subject: Re: [PATCH rdma-core] mlx4: Fix 1<<31 expressions

Quoted 11 lines · Bart Van Assche

providers/mlx4/mlx4dv.h enum { · line 293

Quoted 5 lines · Jason Gunthorpe

enum {

providers/mlx4/mlx4dv.h +1 -1
-	MLX4_WQE_BIND_TYPE_2		= (1<<31),
+	MLX4_WQE_BIND_TYPE_2		= (1UL<<31),
 	MLX4_WQE_BIND_ZERO_BASED	= (1<<30),
 };

Hello Jason,

A quote from the C11 standard:

Quoted 8 lines

"The expression that defines the value of an enumeration constant shall be an integer constant expression that has a value representable as an int. [ ... ] Each enumerated type shall be compatible with char, a signed integer type, or an unsigned integer type. The choice of type is implementation-defined but shall be capable of representing the values of all the members of the enumeration."

Does that mean that this change relies on extensions to the C standard?

Yes, this relies on a common compiler extension to promote the type of enum constants beyond int. AFAIK all compilers have done this forever, kinda bonkers it isn't in C11, IMHO.

Quoted 7 lines

As I understand it, the current code is also non-conforming as (1<<31) is apparently undefined behavior, and at least gcc throws a warning for it in - Wpedantic mode.

I did a patch that will make our public headers mostly strictly conforming to C11 with -Wpedantic set, which I've been wondering if it is worth sending..

Any reason to use open code instead of using BIT() macro from include/linux/bitops.h which does the same thing?

--
To unsubscribe from this list: send the line "unsubscribe linux-rdma" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html

A message that is not in the archive (sent off-list or not yet received): HE1PR0502MB3004BB45FC9ADB9B49CAF283D1F20-692Kmc8YnlL9PhveBwpv4cDSnupUy6xnnBOFsp37pqbUKgpGm//BTAC/[email protected]

#8 lore raw
Quoted 5 lines · Parav Pandit
Quoted 2 lines

I did a patch that will make our public headers mostly strictly conforming to C11 with -Wpedantic set, which I've been wondering if it is worth sending..

Any reason to use open code instead of using BIT() macro from include/linux/bitops.h which does the same thing?

BIT only works in the kernel. This is a publich header from rdma-core userspace.

Jason
--
To unsubscribe from this list: send the line "unsubscribe linux-rdma" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html

A message that is not in the archive (sent off-list or not yet received): 20180209161325.GD7570-uk2M96/[email protected]

#10 lore raw
Quoted 11 lines

-----Original Message-----
From: Jason Gunthorpe [mailto:jgg-uk2M96/[email protected]]
Sent: Friday, February 09, 2018 10:13 AM
To: Parav Pandit <parav-VPRAkNaXOzVWk0Htik3J/[email protected]>
Cc: Bart Van Assche <[email protected]>; [email protected]
Subject: Re: [PATCH rdma-core] mlx4: Fix 1<<31 expressions

On Fri, Feb 09, 2018 at 04:12:24PM +0000, Parav Pandit wrote:

Quoted 9 lines
Quoted 2 lines
Quoted 2 lines

I did a patch that will make our public headers mostly strictly conforming to C11 with -Wpedantic set, which I've been wondering if it is

worth sending..

Quoted 2 lines

Any reason to use open code instead of using BIT() macro from include/linux/bitops.h which does the same thing?

BIT only works in the kernel. This is a publich header from rdma-core userspace.

o.k. Got it. Same duplicated definition exist in kernel too which is not yet fixed because its unused, I guess. Likely user space headers don't have any BIT() or support macros.
Thanks.
--
To unsubscribe from this list: send the line "unsubscribe linux-rdma" in
the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

#11 lore raw
In reply to a message not in the archive
Quoted 10 lines · Jason Gunthorpe
Quoted 5 lines · Parav Pandit
Quoted 2 lines

I did a patch that will make our public headers mostly strictly conforming to C11 with -Wpedantic set, which I've been wondering if it is worth sending..

Any reason to use open code instead of using BIT() macro from include/linux/bitops.h which does the same thing?

BIT only works in the kernel. This is a publich header from rdma-core userspace.

➜ rdma-core git:(master) git grep "define BIT" providers/mlx5/cq.c providers/mlx5/cq.c:#define BIT(i) (1UL << (i))

Thanks

Quoted 6 lines

Jason
--
To unsubscribe from this list: send the line "unsubscribe linux-rdma" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html

A message that is not in the archive (sent off-list or not yet received): 20180209164200.GO2197-U/[email protected]

#13 lore raw
Quoted 6 lines · Leon Romanovsky
Quoted 5 lines · Jason Gunthorpe
Quoted 2 lines · Parav Pandit
Quoted 2 lines

I did a patch that will make our public headers mostly strictly conforming to C11 with -Wpedantic set, which I've been wondering if it is worth sending..

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

Any reason to use open code instead of using BIT() macro from include/linux/bitops.h which does the same thing?

BIT only works in the kernel. This is a publich header from rdma-core userspace.

➜ rdma-core git:(master) git grep "define BIT" providers/mlx5/cq.c providers/mlx5/cq.c:#define BIT(i) (1UL << (i))

Still can't use it in a public header.

Jason
--
To unsubscribe from this list: send the line "unsubscribe linux-rdma" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html

A message that is not in the archive (sent off-list or not yet received): 20180209165138.GA11850-uk2M96/[email protected]

Reviewed-by
#15 lore raw
Quoted 10 lines · Jason Gunthorpe
Quoted 9 lines · Leon Romanovsky
Quoted 8 lines · Jason Gunthorpe
Quoted 5 lines · Parav Pandit
Quoted 2 lines

I did a patch that will make our public headers mostly strictly conforming to C11 with -Wpedantic set, which I've been wondering if it is worth sending..

Any reason to use open code instead of using BIT() macro from include/linux/bitops.h which does the same thing?

Quoted 7 lines
Quoted 5 lines
Quoted 2 lines

BIT only works in the kernel. This is a publich header from rdma-core userspace.

➜ rdma-core git:(master) git grep "define BIT" providers/mlx5/cq.c providers/mlx5/cq.c:#define BIT(i) (1UL << (i))

Still can't use it in a public header.

Thanks, I missed that we are talking about mlx4dv.h

#16 lore raw
In reply to a message not in the archive

T24gRnJpLCAyMDE4LTAyLTA5IGF0IDE2OjEyICswMDAwLCBQYXJhdiBQYW5kaXQgd3JvdGU6DQo+ IEFueSByZWFzb24gdG8gdXNlIG9wZW4gY29kZSBpbnN0ZWFkIG9mIHVzaW5nIEJJVCgpIG1hY3Jv IGZyb20NCj4gaW5jbHVkZS9saW51eC9iaXRvcHMuaCB3aGljaCBkb2VzIHRoZSBzYW1lIHRoaW5n Pw0KDQpQZXJzb25hbGx5IEknbSBpbiBmYXZvciBvZiByZW1vdmluZyB0aGUgQklUKCkgbWFjcm8g ZnJvbSB0aGUga2VybmVsLiBUaGF0IG1hY3JvDQppcyBub3QgdXNlZnVsIGFuZCBvbmx5IGFkZHMg Y29uZnVzaW9uLiBDb2RlIHRoYXQgZG9lcyBub3QgdXNlIHRoYXQgbWFjcm8gaXMNCmVhc2llciB0 byByZWFkIGJlY2F1c2Ugb25lIGRvZXMgbm90IGhhdmUgdG8gbG9vayB1cCB3aGV0aGVyIHRoZSBC SVQoKSBtYWNybw0KaXMgZGVmaW5lZCBhcyAxVSA8PCB4LCAxVUwgPDwgeCBvciAxVUxMIDw8IHgu DQoNCkJhcnQuDQoNCg0K
--
To unsubscribe from this list: send the line "unsubscribe linux-rdma" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html

A message that is not in the archive (sent off-list or not yet received): [email protected]

#18 lore raw
Quoted 8 lines · Bart Van Assche
Quoted 2 lines · Parav Pandit

Any reason to use open code instead of using BIT() macro from include/linux/bitops.h which does the same thing?

Personally I'm in favor of removing the BIT() macro from the kernel. That macro is not useful and only adds confusion. Code that does not use that macro is easier to read because one does not have to look up whether the BIT() macro is defined as 1U << x, 1UL << x or 1ULL << x.

Really? The rule is pretty simple:

BIT(x) for x < 32
BIT_ULL(x) for x >= 32 && x < 64

Why do you care about what specific type the macro produces?

Jason
--
To unsubscribe from this list: send the line "unsubscribe linux-rdma" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html

A message that is not in the archive (sent off-list or not yet received): 20180209162453.GD7584-VPRAkNaXOzVWk0Htik3J/[email protected]

#20 lore raw
Quoted 10 lines · Jason Gunthorpe
Quoted 8 lines · Bart Van Assche
Quoted 2 lines · Parav Pandit

Any reason to use open code instead of using BIT() macro from include/linux/bitops.h which does the same thing?

Personally I'm in favor of removing the BIT() macro from the kernel. That macro is not useful and only adds confusion. Code that does not use that macro is easier to read because one does not have to look up whether the BIT() macro is defined as 1U << x, 1UL << x or 1ULL << x.

Quoted 6 lines

Really? The rule is pretty simple:

  BIT(x) for x < 32
  BIT_ULL(x) for x >= 32 && x < 64

Why do you care about what specific type the macro produces?

That's something additional kernel developers have to memorize. If the BIT() macro is not used but the shift operation is explicit then that saves the lookup of the BIT() definition in case one would have forgotten what the BIT() definition looks like.

Bart.
--
To unsubscribe from this list: send the line "unsubscribe linux-rdma" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html