All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] tcp: don't use __constant_cpu_to_be32
@ 2019-03-14  6:15 Sergey Senozhatsky
  2019-03-14 12:42 ` Eric Dumazet
  0 siblings, 1 reply; 10+ messages in thread
From: Sergey Senozhatsky @ 2019-03-14  6:15 UTC (permalink / raw)
  To: Eric Dumazet; +Cc: netdev, linux-kernel, Sergey Senozhatsky

A trivial patch.

cpu_to_be32() is capable enough to detect __builtin_constant_p()
and to use an appropriate compile time ___constant_swahb32()
function.

So we can use cpu_to_be32() instead of __constant_cpu_to_be32().

Signed-off-by: Sergey Senozhatsky <sergey.senozhatsky@gmail.com>
---
 include/uapi/linux/tcp.h | 20 ++++++++++----------
 1 file changed, 10 insertions(+), 10 deletions(-)

diff --git a/include/uapi/linux/tcp.h b/include/uapi/linux/tcp.h
index 8bb6cc5f3235..7d2330df9652 100644
--- a/include/uapi/linux/tcp.h
+++ b/include/uapi/linux/tcp.h
@@ -70,16 +70,16 @@ union tcp_word_hdr {
 #define tcp_flag_word(tp) ( ((union tcp_word_hdr *)(tp))->words [3]) 
 
 enum { 
-	TCP_FLAG_CWR = __constant_cpu_to_be32(0x00800000),
-	TCP_FLAG_ECE = __constant_cpu_to_be32(0x00400000),
-	TCP_FLAG_URG = __constant_cpu_to_be32(0x00200000),
-	TCP_FLAG_ACK = __constant_cpu_to_be32(0x00100000),
-	TCP_FLAG_PSH = __constant_cpu_to_be32(0x00080000),
-	TCP_FLAG_RST = __constant_cpu_to_be32(0x00040000),
-	TCP_FLAG_SYN = __constant_cpu_to_be32(0x00020000),
-	TCP_FLAG_FIN = __constant_cpu_to_be32(0x00010000),
-	TCP_RESERVED_BITS = __constant_cpu_to_be32(0x0F000000),
-	TCP_DATA_OFFSET = __constant_cpu_to_be32(0xF0000000)
+	TCP_FLAG_CWR = cpu_to_be32(0x00800000),
+	TCP_FLAG_ECE = cpu_to_be32(0x00400000),
+	TCP_FLAG_URG = cpu_to_be32(0x00200000),
+	TCP_FLAG_ACK = cpu_to_be32(0x00100000),
+	TCP_FLAG_PSH = cpu_to_be32(0x00080000),
+	TCP_FLAG_RST = cpu_to_be32(0x00040000),
+	TCP_FLAG_SYN = cpu_to_be32(0x00020000),
+	TCP_FLAG_FIN = cpu_to_be32(0x00010000),
+	TCP_RESERVED_BITS = cpu_to_be32(0x0F000000),
+	TCP_DATA_OFFSET = cpu_to_be32(0xF0000000)
 }; 
 
 /*
-- 
2.21.0


^ permalink raw reply related	[flat|nested] 10+ messages in thread

* Re: [PATCH] tcp: don't use __constant_cpu_to_be32
  2019-03-14  6:15 [PATCH] tcp: don't use __constant_cpu_to_be32 Sergey Senozhatsky
@ 2019-03-14 12:42 ` Eric Dumazet
  2019-03-15  1:20   ` Sergey Senozhatsky
  0 siblings, 1 reply; 10+ messages in thread
From: Eric Dumazet @ 2019-03-14 12:42 UTC (permalink / raw)
  To: Sergey Senozhatsky; +Cc: netdev, LKML, Sergey Senozhatsky

On Wed, Mar 13, 2019 at 11:15 PM Sergey Senozhatsky
<sergey.senozhatsky.work@gmail.com> wrote:
>
> A trivial patch.


Not so trivial...


>
>
> cpu_to_be32() is capable enough to detect __builtin_constant_p()
> and to use an appropriate compile time ___constant_swahb32()
> function.
>
> So we can use cpu_to_be32() instead of __constant_cpu_to_be32().


I dunno, this is uapi, this might break user space for some funky compiler ?

I do not even know if cpu_to_be32() _is_ uapi.

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH] tcp: don't use __constant_cpu_to_be32
  2019-03-14 12:42 ` Eric Dumazet
@ 2019-03-15  1:20   ` Sergey Senozhatsky
  2019-03-15 17:24     ` Stanislav Fomichev
  0 siblings, 1 reply; 10+ messages in thread
From: Sergey Senozhatsky @ 2019-03-15  1:20 UTC (permalink / raw)
  To: Eric Dumazet; +Cc: Sergey Senozhatsky, netdev, LKML, Sergey Senozhatsky

On (03/14/19 05:42), Eric Dumazet wrote:
> > cpu_to_be32() is capable enough to detect __builtin_constant_p()
> > and to use an appropriate compile time ___constant_swahb32()
> > function.
> >
> > So we can use cpu_to_be32() instead of __constant_cpu_to_be32().
> 
> 
> I dunno, this is uapi, this might break user space for some funky compiler ?
> I do not even know if cpu_to_be32() _is_ uapi.

Hmm, excellent point.

Looking at

if_pppox.h:#define PTT_EOL		__cpu_to_be16(0x0000)
if_pppox.h:#define PTT_SRV_NAME	__cpu_to_be16(0x0101)
if_pppox.h:#define PTT_AC_NAME	__cpu_to_be16(0x0102)
if_pppox.h:#define PTT_HOST_UNIQ	__cpu_to_be16(0x0103)
if_pppox.h:#define PTT_AC_COOKIE	__cpu_to_be16(0x0104)
if_pppox.h:#define PTT_VENDOR 	__cpu_to_be16(0x0105)
if_pppox.h:#define PTT_RELAY_SID	__cpu_to_be16(0x0110)
if_pppox.h:#define PTT_SRV_ERR     __cpu_to_be16(0x0201)
if_pppox.h:#define PTT_SYS_ERR  	__cpu_to_be16(0x0202)
if_pppox.h:#define PTT_GEN_ERR  	__cpu_to_be16(0x0203)
if_tunnel.h:#define GRE_CSUM	__cpu_to_be16(0x8000)
if_tunnel.h:#define GRE_ROUTING	__cpu_to_be16(0x4000)
if_tunnel.h:#define GRE_KEY		__cpu_to_be16(0x2000)
if_tunnel.h:#define GRE_SEQ		__cpu_to_be16(0x1000)
if_tunnel.h:#define GRE_STRICT	__cpu_to_be16(0x0800)
if_tunnel.h:#define GRE_REC		__cpu_to_be16(0x0700)
if_tunnel.h:#define GRE_ACK		__cpu_to_be16(0x0080)
if_tunnel.h:#define GRE_FLAGS	__cpu_to_be16(0x0078)
if_tunnel.h:#define GRE_VERSION	__cpu_to_be16(0x0007)
if_tunnel.h:#define GRE_VERSION_0		__cpu_to_be16(0x0000)
if_tunnel.h:#define GRE_VERSION_1		__cpu_to_be16(0x0001)
if_tunnel.h:#define GRE_PROTO_PPP		__cpu_to_be16(0x880b)

You are right, probably would need __cpu_to_be32, which also does
the constant folding.

=-=-=-=-=

Subject: [PATCH] tcp: don't use __constant_cpu_to_be32

__cpu_to_be32() can detect __builtin_constant_p() at compile time.
So we can use __cpu_to_be32() instead of __constant_cpu_to_be32().

Signed-off-by: Sergey Senozhatsky <sergey.senozhatsky@gmail.com>
---
 include/uapi/linux/tcp.h | 20 ++++++++++----------
 1 file changed, 10 insertions(+), 10 deletions(-)

diff --git a/include/uapi/linux/tcp.h b/include/uapi/linux/tcp.h
index 8bb6cc5f3235..cf9c246a59c8 100644
--- a/include/uapi/linux/tcp.h
+++ b/include/uapi/linux/tcp.h
@@ -70,16 +70,16 @@ union tcp_word_hdr {
 #define tcp_flag_word(tp) ( ((union tcp_word_hdr *)(tp))->words [3]) 
 
 enum { 
-	TCP_FLAG_CWR = __constant_cpu_to_be32(0x00800000),
-	TCP_FLAG_ECE = __constant_cpu_to_be32(0x00400000),
-	TCP_FLAG_URG = __constant_cpu_to_be32(0x00200000),
-	TCP_FLAG_ACK = __constant_cpu_to_be32(0x00100000),
-	TCP_FLAG_PSH = __constant_cpu_to_be32(0x00080000),
-	TCP_FLAG_RST = __constant_cpu_to_be32(0x00040000),
-	TCP_FLAG_SYN = __constant_cpu_to_be32(0x00020000),
-	TCP_FLAG_FIN = __constant_cpu_to_be32(0x00010000),
-	TCP_RESERVED_BITS = __constant_cpu_to_be32(0x0F000000),
-	TCP_DATA_OFFSET = __constant_cpu_to_be32(0xF0000000)
+	TCP_FLAG_CWR = __cpu_to_be32(0x00800000),
+	TCP_FLAG_ECE = __cpu_to_be32(0x00400000),
+	TCP_FLAG_URG = __cpu_to_be32(0x00200000),
+	TCP_FLAG_ACK = __cpu_to_be32(0x00100000),
+	TCP_FLAG_PSH = __cpu_to_be32(0x00080000),
+	TCP_FLAG_RST = __cpu_to_be32(0x00040000),
+	TCP_FLAG_SYN = __cpu_to_be32(0x00020000),
+	TCP_FLAG_FIN = __cpu_to_be32(0x00010000),
+	TCP_RESERVED_BITS = __cpu_to_be32(0x0F000000),
+	TCP_DATA_OFFSET = __cpu_to_be32(0xF0000000)
 }; 
 
 /*
-- 
2.21.0


^ permalink raw reply related	[flat|nested] 10+ messages in thread

* Re: [PATCH] tcp: don't use __constant_cpu_to_be32
  2019-03-15  1:20   ` Sergey Senozhatsky
@ 2019-03-15 17:24     ` Stanislav Fomichev
  2019-03-16  5:19       ` Sergey Senozhatsky
  0 siblings, 1 reply; 10+ messages in thread
From: Stanislav Fomichev @ 2019-03-15 17:24 UTC (permalink / raw)
  To: Sergey Senozhatsky; +Cc: Eric Dumazet, netdev, LKML, Sergey Senozhatsky

On 03/15, Sergey Senozhatsky wrote:
> On (03/14/19 05:42), Eric Dumazet wrote:
> > > cpu_to_be32() is capable enough to detect __builtin_constant_p()
> > > and to use an appropriate compile time ___constant_swahb32()
> > > function.
See my recent commit a0517a0f7ef23 ("selftests/bpf: use
__bpf_constant_htons in test_prog.c") where compiler was not smart
enough to figure it out (you can google similar issues for GCC < 4.8).

> > >
> > > So we can use cpu_to_be32() instead of __constant_cpu_to_be32().
> > 
> > 
> > I dunno, this is uapi, this might break user space for some funky compiler ?
> > I do not even know if cpu_to_be32() _is_ uapi.
> 
> Hmm, excellent point.
> 
> Looking at
> 
> if_pppox.h:#define PTT_EOL		__cpu_to_be16(0x0000)
> if_pppox.h:#define PTT_SRV_NAME	__cpu_to_be16(0x0101)
> if_pppox.h:#define PTT_AC_NAME	__cpu_to_be16(0x0102)
> if_pppox.h:#define PTT_HOST_UNIQ	__cpu_to_be16(0x0103)
> if_pppox.h:#define PTT_AC_COOKIE	__cpu_to_be16(0x0104)
> if_pppox.h:#define PTT_VENDOR 	__cpu_to_be16(0x0105)
> if_pppox.h:#define PTT_RELAY_SID	__cpu_to_be16(0x0110)
> if_pppox.h:#define PTT_SRV_ERR     __cpu_to_be16(0x0201)
> if_pppox.h:#define PTT_SYS_ERR  	__cpu_to_be16(0x0202)
> if_pppox.h:#define PTT_GEN_ERR  	__cpu_to_be16(0x0203)
> if_tunnel.h:#define GRE_CSUM	__cpu_to_be16(0x8000)
> if_tunnel.h:#define GRE_ROUTING	__cpu_to_be16(0x4000)
> if_tunnel.h:#define GRE_KEY		__cpu_to_be16(0x2000)
> if_tunnel.h:#define GRE_SEQ		__cpu_to_be16(0x1000)
> if_tunnel.h:#define GRE_STRICT	__cpu_to_be16(0x0800)
> if_tunnel.h:#define GRE_REC		__cpu_to_be16(0x0700)
> if_tunnel.h:#define GRE_ACK		__cpu_to_be16(0x0080)
> if_tunnel.h:#define GRE_FLAGS	__cpu_to_be16(0x0078)
> if_tunnel.h:#define GRE_VERSION	__cpu_to_be16(0x0007)
> if_tunnel.h:#define GRE_VERSION_0		__cpu_to_be16(0x0000)
> if_tunnel.h:#define GRE_VERSION_1		__cpu_to_be16(0x0001)
> if_tunnel.h:#define GRE_PROTO_PPP		__cpu_to_be16(0x880b)
> 
> You are right, probably would need __cpu_to_be32, which also does
> the constant folding.
> 
> =-=-=-=-=
> 
> Subject: [PATCH] tcp: don't use __constant_cpu_to_be32
> 
> __cpu_to_be32() can detect __builtin_constant_p() at compile time.
> So we can use __cpu_to_be32() instead of __constant_cpu_to_be32().
> 
> Signed-off-by: Sergey Senozhatsky <sergey.senozhatsky@gmail.com>
> ---
>  include/uapi/linux/tcp.h | 20 ++++++++++----------
>  1 file changed, 10 insertions(+), 10 deletions(-)
> 
> diff --git a/include/uapi/linux/tcp.h b/include/uapi/linux/tcp.h
> index 8bb6cc5f3235..cf9c246a59c8 100644
> --- a/include/uapi/linux/tcp.h
> +++ b/include/uapi/linux/tcp.h
> @@ -70,16 +70,16 @@ union tcp_word_hdr {
>  #define tcp_flag_word(tp) ( ((union tcp_word_hdr *)(tp))->words [3]) 
>  
>  enum { 
> -	TCP_FLAG_CWR = __constant_cpu_to_be32(0x00800000),
> -	TCP_FLAG_ECE = __constant_cpu_to_be32(0x00400000),
> -	TCP_FLAG_URG = __constant_cpu_to_be32(0x00200000),
> -	TCP_FLAG_ACK = __constant_cpu_to_be32(0x00100000),
> -	TCP_FLAG_PSH = __constant_cpu_to_be32(0x00080000),
> -	TCP_FLAG_RST = __constant_cpu_to_be32(0x00040000),
> -	TCP_FLAG_SYN = __constant_cpu_to_be32(0x00020000),
> -	TCP_FLAG_FIN = __constant_cpu_to_be32(0x00010000),
> -	TCP_RESERVED_BITS = __constant_cpu_to_be32(0x0F000000),
> -	TCP_DATA_OFFSET = __constant_cpu_to_be32(0xF0000000)
> +	TCP_FLAG_CWR = __cpu_to_be32(0x00800000),
> +	TCP_FLAG_ECE = __cpu_to_be32(0x00400000),
> +	TCP_FLAG_URG = __cpu_to_be32(0x00200000),
> +	TCP_FLAG_ACK = __cpu_to_be32(0x00100000),
> +	TCP_FLAG_PSH = __cpu_to_be32(0x00080000),
> +	TCP_FLAG_RST = __cpu_to_be32(0x00040000),
> +	TCP_FLAG_SYN = __cpu_to_be32(0x00020000),
> +	TCP_FLAG_FIN = __cpu_to_be32(0x00010000),
> +	TCP_RESERVED_BITS = __cpu_to_be32(0x0F000000),
> +	TCP_DATA_OFFSET = __cpu_to_be32(0xF0000000)
>  }; 
>  
>  /*
> -- 
> 2.21.0
> 

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH] tcp: don't use __constant_cpu_to_be32
  2019-03-15 17:24     ` Stanislav Fomichev
@ 2019-03-16  5:19       ` Sergey Senozhatsky
  2019-03-16  7:18         ` Sergey Senozhatsky
                           ` (2 more replies)
  0 siblings, 3 replies; 10+ messages in thread
From: Sergey Senozhatsky @ 2019-03-16  5:19 UTC (permalink / raw)
  To: Stanislav Fomichev
  Cc: Sergey Senozhatsky, Eric Dumazet, netdev, LKML, Sergey Senozhatsky

On (03/15/19 10:24), Stanislav Fomichev wrote:
> On 03/15, Sergey Senozhatsky wrote:
> > On (03/14/19 05:42), Eric Dumazet wrote:
> > > > cpu_to_be32() is capable enough to detect __builtin_constant_p()
> > > > and to use an appropriate compile time ___constant_swahb32()
> > > > function.
> See my recent commit a0517a0f7ef23 ("selftests/bpf: use
> __bpf_constant_htons in test_prog.c") where compiler was not smart
> enough to figure it out (you can google similar issues for GCC < 4.8).

Allow me to disagree.

The error in a0517a0f7ef23 says

	error: implicit declaration of function '__builtin_bswap16'

which doesn't sound like "the compiler was not smart".

Let's look at __swab16

100 #ifdef __HAVE_BUILTIN_BSWAP16__
101 #define __swab16(x) (__u16)__builtin_bswap16((__u16)(x))
102 #else
103 #define __swab16(x)                             \
104         (__builtin_constant_p((__u16)(x)) ?     \
105         ___constant_swab16(x) :                 \
106         __fswab16(x))
107 #endif

We can call __builtin_bswap16() only when the compiler has that builtin,
which is what #ifdef __HAVE_BUILTIN_BSWAP16__ for.

But this is not what tools/testing/selftests/bpf/bpf_endian.h does

# define __bpf_ntohs(x)                 __builtin_bswap16(x)
# define __bpf_htons(x)                 __builtin_bswap16(x)

So I sort of suspect that what should have been done was that
__HAVE_BUILTIN_BSWAP16__ ifdef, just like what include/uapi/linux/swab.h
does.

	-ss

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH] tcp: don't use __constant_cpu_to_be32
  2019-03-16  5:19       ` Sergey Senozhatsky
@ 2019-03-16  7:18         ` Sergey Senozhatsky
  2019-03-16 14:26         ` Sergey Senozhatsky
  2019-03-18 16:28         ` Stanislav Fomichev
  2 siblings, 0 replies; 10+ messages in thread
From: Sergey Senozhatsky @ 2019-03-16  7:18 UTC (permalink / raw)
  To: Stanislav Fomichev
  Cc: Sergey Senozhatsky, Eric Dumazet, netdev, LKML, Sergey Senozhatsky

On (03/16/19 14:19), Sergey Senozhatsky wrote:
> > See my recent commit a0517a0f7ef23 ("selftests/bpf: use
> > __bpf_constant_htons in test_prog.c") where compiler was not smart
> > enough to figure it out (you can google similar issues for GCC < 4.8).
> 
> Allow me to disagree.
> 
> The error in a0517a0f7ef23 says
> 
> 	error: implicit declaration of function '__builtin_bswap16'
> 
> which doesn't sound like "the compiler was not smart".
> 
> Let's look at __swab16
> 
> 100 #ifdef __HAVE_BUILTIN_BSWAP16__
> 101 #define __swab16(x) (__u16)__builtin_bswap16((__u16)(x))
> 102 #else
> 103 #define __swab16(x)                             \
> 104         (__builtin_constant_p((__u16)(x)) ?     \
> 105         ___constant_swab16(x) :                 \
> 106         __fswab16(x))
> 107 #endif
> 
> We can call __builtin_bswap16() only when the compiler has that builtin,
> which is what #ifdef __HAVE_BUILTIN_BSWAP16__ for.
> 
> But this is not what tools/testing/selftests/bpf/bpf_endian.h does
> 
> # define __bpf_ntohs(x)                 __builtin_bswap16(x)
> # define __bpf_htons(x)                 __builtin_bswap16(x)
> 
> So I sort of suspect that what should have been done was that
> __HAVE_BUILTIN_BSWAP16__ ifdef, just like what include/uapi/linux/swab.h
> does.

E.g.

#define ____const(x)	printf("const %d\n", (x))
#define ____noconst(x)	printf("noconst %d\n", (x))
#define const_test(x)	(__builtin_constant_p(x) ? ____const((x)) : ____noconst((x)))

The compiler should optimize out !__builtin_constant_p (dead code elimination
phase - dce), if it can compile the code in the first place.

So if we replace ____noconst with

#define ____noconst(x)	printf2("noconst %d\n", (x))

then compiler wouldn't be able to compile the code and thus wouldn't
be able to perform dce.

__builtin_constant_p, unlike `#if 0', is not handled by the preprocessor.

Verified with gcc 8.3, gcc 4.9, gcc 4.7 at compiler explorer website
https://gcc.godbolt.org/ - GCC is smart enough to figure out
__builtin_constant_p and do dce, given that it compile the code.

	-ss

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH] tcp: don't use __constant_cpu_to_be32
  2019-03-16  5:19       ` Sergey Senozhatsky
  2019-03-16  7:18         ` Sergey Senozhatsky
@ 2019-03-16 14:26         ` Sergey Senozhatsky
  2019-03-18 16:32           ` Stanislav Fomichev
  2019-03-18 16:28         ` Stanislav Fomichev
  2 siblings, 1 reply; 10+ messages in thread
From: Sergey Senozhatsky @ 2019-03-16 14:26 UTC (permalink / raw)
  To: Stanislav Fomichev
  Cc: Sergey Senozhatsky, Eric Dumazet, netdev, LKML, Sergey Senozhatsky

On (03/16/19 14:19), Sergey Senozhatsky wrote:
> # define __bpf_ntohs(x)                 __builtin_bswap16(x)
> # define __bpf_htons(x)                 __builtin_bswap16(x)
> 
> So I sort of suspect that what should have been done was that
> __HAVE_BUILTIN_BSWAP16__ ifdef, just like what include/uapi/linux/swab.h
> does.

E.g. use uapi __swab16/__swab32 in selftests/bpf/bpf_endian.h?

-=-=-=-=-=-

 tools/testing/selftests/bpf/bpf_endian.h | 32 ++++++++------------------------
 1 file changed, 8 insertions(+), 24 deletions(-)

diff --git a/tools/testing/selftests/bpf/bpf_endian.h b/tools/testing/selftests/bpf/bpf_endian.h
index b25595ea4a78..68789b4c7ef0 100644
--- a/tools/testing/selftests/bpf/bpf_endian.h
+++ b/tools/testing/selftests/bpf/bpf_endian.h
@@ -20,38 +20,22 @@
  * use different targets.
  */
 #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
-# define __bpf_ntohs(x)			__builtin_bswap16(x)
-# define __bpf_htons(x)			__builtin_bswap16(x)
-# define __bpf_constant_ntohs(x)	___constant_swab16(x)
-# define __bpf_constant_htons(x)	___constant_swab16(x)
-# define __bpf_ntohl(x)			__builtin_bswap32(x)
-# define __bpf_htonl(x)			__builtin_bswap32(x)
-# define __bpf_constant_ntohl(x)	___constant_swab32(x)
-# define __bpf_constant_htonl(x)	___constant_swab32(x)
+# define __bpf_ntohs(x)			__swab16(x)
+# define __bpf_htons(x)			__swab16(x)
+# define __bpf_ntohl(x)			__swab32(x)
+# define __bpf_htonl(x)			__swab32(x)
 #elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
 # define __bpf_ntohs(x)			(x)
 # define __bpf_htons(x)			(x)
-# define __bpf_constant_ntohs(x)	(x)
-# define __bpf_constant_htons(x)	(x)
 # define __bpf_ntohl(x)			(x)
 # define __bpf_htonl(x)			(x)
-# define __bpf_constant_ntohl(x)	(x)
-# define __bpf_constant_htonl(x)	(x)
 #else
 # error "Fix your compiler's __BYTE_ORDER__?!"
 #endif
 
-#define bpf_htons(x)				\
-	(__builtin_constant_p(x) ?		\
-	 __bpf_constant_htons(x) : __bpf_htons(x))
-#define bpf_ntohs(x)				\
-	(__builtin_constant_p(x) ?		\
-	 __bpf_constant_ntohs(x) : __bpf_ntohs(x))
-#define bpf_htonl(x)				\
-	(__builtin_constant_p(x) ?		\
-	 __bpf_constant_htonl(x) : __bpf_htonl(x))
-#define bpf_ntohl(x)				\
-	(__builtin_constant_p(x) ?		\
-	 __bpf_constant_ntohl(x) : __bpf_ntohl(x))
+#define bpf_htons(x)	__bpf_htons((x))
+#define bpf_ntohs(x)	__bpf_ntohs((x))
+#define bpf_htonl(x)	__bpf_htonl((x))
+#define bpf_ntohl(x)	__bpf_ntohl((x))
 
 #endif /* __BPF_ENDIAN__ */

^ permalink raw reply related	[flat|nested] 10+ messages in thread

* Re: [PATCH] tcp: don't use __constant_cpu_to_be32
  2019-03-16  5:19       ` Sergey Senozhatsky
  2019-03-16  7:18         ` Sergey Senozhatsky
  2019-03-16 14:26         ` Sergey Senozhatsky
@ 2019-03-18 16:28         ` Stanislav Fomichev
  2 siblings, 0 replies; 10+ messages in thread
From: Stanislav Fomichev @ 2019-03-18 16:28 UTC (permalink / raw)
  To: Sergey Senozhatsky; +Cc: Sergey Senozhatsky, Eric Dumazet, netdev, LKML

On 03/16, Sergey Senozhatsky wrote:
> On (03/15/19 10:24), Stanislav Fomichev wrote:
> > On 03/15, Sergey Senozhatsky wrote:
> > > On (03/14/19 05:42), Eric Dumazet wrote:
> > > > > cpu_to_be32() is capable enough to detect __builtin_constant_p()
> > > > > and to use an appropriate compile time ___constant_swahb32()
> > > > > function.
> > See my recent commit a0517a0f7ef23 ("selftests/bpf: use
> > __bpf_constant_htons in test_prog.c") where compiler was not smart
> > enough to figure it out (you can google similar issues for GCC < 4.8).
> 
> Allow me to disagree.
> 
> The error in a0517a0f7ef23 says
> 
> 	error: implicit declaration of function '__builtin_bswap16'
> 
> which doesn't sound like "the compiler was not smart".
> 
> Let's look at __swab16
> 
> 100 #ifdef __HAVE_BUILTIN_BSWAP16__
> 101 #define __swab16(x) (__u16)__builtin_bswap16((__u16)(x))
> 102 #else
> 103 #define __swab16(x)                             \
> 104         (__builtin_constant_p((__u16)(x)) ?     \
> 105         ___constant_swab16(x) :                 \
> 106         __fswab16(x))
> 107 #endif
> 
> We can call __builtin_bswap16() only when the compiler has that builtin,
> which is what #ifdef __HAVE_BUILTIN_BSWAP16__ for.
> 
> But this is not what tools/testing/selftests/bpf/bpf_endian.h does
> 
> # define __bpf_ntohs(x)                 __builtin_bswap16(x)
> # define __bpf_htons(x)                 __builtin_bswap16(x)
> 
> So I sort of suspect that what should have been done was that
> __HAVE_BUILTIN_BSWAP16__ ifdef, just like what include/uapi/linux/swab.h
> does.
That sounds convincing, especially since we have the following
in include/linux/compiler-gcc.h:

#if GCC_VERSION >= 40800
#define __HAVE_BUILTIN_BSWAP16__
#endif

So it does indeed look like it's not __builtin_constant_p, but rather
missing __builtin_bswap16 for gcc < 4.8.

> 
> 	-ss

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH] tcp: don't use __constant_cpu_to_be32
  2019-03-16 14:26         ` Sergey Senozhatsky
@ 2019-03-18 16:32           ` Stanislav Fomichev
  2019-03-19  1:29             ` Sergey Senozhatsky
  0 siblings, 1 reply; 10+ messages in thread
From: Stanislav Fomichev @ 2019-03-18 16:32 UTC (permalink / raw)
  To: Sergey Senozhatsky; +Cc: Sergey Senozhatsky, Eric Dumazet, netdev, LKML

On 03/16, Sergey Senozhatsky wrote:
> On (03/16/19 14:19), Sergey Senozhatsky wrote:
> > # define __bpf_ntohs(x)                 __builtin_bswap16(x)
> > # define __bpf_htons(x)                 __builtin_bswap16(x)
> > 
> > So I sort of suspect that what should have been done was that
> > __HAVE_BUILTIN_BSWAP16__ ifdef, just like what include/uapi/linux/swab.h
> > does.
> 
> E.g. use uapi __swab16/__swab32 in selftests/bpf/bpf_endian.h?
> 
> -=-=-=-=-=-
> 
>  tools/testing/selftests/bpf/bpf_endian.h | 32 ++++++++------------------------
>  1 file changed, 8 insertions(+), 24 deletions(-)
> 
> diff --git a/tools/testing/selftests/bpf/bpf_endian.h b/tools/testing/selftests/bpf/bpf_endian.h
> index b25595ea4a78..68789b4c7ef0 100644
> --- a/tools/testing/selftests/bpf/bpf_endian.h
> +++ b/tools/testing/selftests/bpf/bpf_endian.h
> @@ -20,38 +20,22 @@
>   * use different targets.
>   */
>  #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
> -# define __bpf_ntohs(x)			__builtin_bswap16(x)
> -# define __bpf_htons(x)			__builtin_bswap16(x)
> -# define __bpf_constant_ntohs(x)	___constant_swab16(x)
> -# define __bpf_constant_htons(x)	___constant_swab16(x)
> -# define __bpf_ntohl(x)			__builtin_bswap32(x)
> -# define __bpf_htonl(x)			__builtin_bswap32(x)
> -# define __bpf_constant_ntohl(x)	___constant_swab32(x)
> -# define __bpf_constant_htonl(x)	___constant_swab32(x)
> +# define __bpf_ntohs(x)			__swab16(x)
> +# define __bpf_htons(x)			__swab16(x)
> +# define __bpf_ntohl(x)			__swab32(x)
> +# define __bpf_htonl(x)			__swab32(x)
>  #elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
>  # define __bpf_ntohs(x)			(x)
>  # define __bpf_htons(x)			(x)
> -# define __bpf_constant_ntohs(x)	(x)
> -# define __bpf_constant_htons(x)	(x)
>  # define __bpf_ntohl(x)			(x)
>  # define __bpf_htonl(x)			(x)
> -# define __bpf_constant_ntohl(x)	(x)
> -# define __bpf_constant_htonl(x)	(x)
>  #else
>  # error "Fix your compiler's __BYTE_ORDER__?!"
>  #endif
>  
> -#define bpf_htons(x)				\
> -	(__builtin_constant_p(x) ?		\
> -	 __bpf_constant_htons(x) : __bpf_htons(x))
> -#define bpf_ntohs(x)				\
> -	(__builtin_constant_p(x) ?		\
> -	 __bpf_constant_ntohs(x) : __bpf_ntohs(x))
> -#define bpf_htonl(x)				\
> -	(__builtin_constant_p(x) ?		\
> -	 __bpf_constant_htonl(x) : __bpf_htonl(x))
> -#define bpf_ntohl(x)				\
> -	(__builtin_constant_p(x) ?		\
> -	 __bpf_constant_ntohl(x) : __bpf_ntohl(x))
> +#define bpf_htons(x)	__bpf_htons((x))
> +#define bpf_ntohs(x)	__bpf_ntohs((x))
> +#define bpf_htonl(x)	__bpf_htonl((x))
> +#define bpf_ntohl(x)	__bpf_ntohl((x))
At this point we can probably drop __bpf_xxx as well?
Care to resend with proper description when bpf-next opens?

>  
>  #endif /* __BPF_ENDIAN__ */

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH] tcp: don't use __constant_cpu_to_be32
  2019-03-18 16:32           ` Stanislav Fomichev
@ 2019-03-19  1:29             ` Sergey Senozhatsky
  0 siblings, 0 replies; 10+ messages in thread
From: Sergey Senozhatsky @ 2019-03-19  1:29 UTC (permalink / raw)
  To: Stanislav Fomichev
  Cc: Sergey Senozhatsky, Sergey Senozhatsky, Eric Dumazet, netdev, LKML

On (03/18/19 09:32), Stanislav Fomichev wrote:
[..]
> > -#define bpf_htons(x)				\
> > -	(__builtin_constant_p(x) ?		\
> > -	 __bpf_constant_htons(x) : __bpf_htons(x))
> > -#define bpf_ntohs(x)				\
> > -	(__builtin_constant_p(x) ?		\
> > -	 __bpf_constant_ntohs(x) : __bpf_ntohs(x))
> > -#define bpf_htonl(x)				\
> > -	(__builtin_constant_p(x) ?		\
> > -	 __bpf_constant_htonl(x) : __bpf_htonl(x))
> > -#define bpf_ntohl(x)				\
> > -	(__builtin_constant_p(x) ?		\
> > -	 __bpf_constant_ntohl(x) : __bpf_ntohl(x))
> > +#define bpf_htons(x)	__bpf_htons((x))
> > +#define bpf_ntohs(x)	__bpf_ntohs((x))
> > +#define bpf_htonl(x)	__bpf_htonl((x))
> > +#define bpf_ntohl(x)	__bpf_ntohl((x))
> At this point we can probably drop __bpf_xxx as well?
> Care to resend with proper description when bpf-next opens?

OK.

	-ss

^ permalink raw reply	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2019-03-19  1:29 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-03-14  6:15 [PATCH] tcp: don't use __constant_cpu_to_be32 Sergey Senozhatsky
2019-03-14 12:42 ` Eric Dumazet
2019-03-15  1:20   ` Sergey Senozhatsky
2019-03-15 17:24     ` Stanislav Fomichev
2019-03-16  5:19       ` Sergey Senozhatsky
2019-03-16  7:18         ` Sergey Senozhatsky
2019-03-16 14:26         ` Sergey Senozhatsky
2019-03-18 16:32           ` Stanislav Fomichev
2019-03-19  1:29             ` Sergey Senozhatsky
2019-03-18 16:28         ` Stanislav Fomichev

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.