Skip to contentSkip to conversation

IPV6 : Change a divide into a right shift in tcp_v6_send_ack()

TCP Eric Dumazet 5 participants 12 messages Dec 21, 2007 1 message not in the archive

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

Because tot_len is signed in tcp_v6_send_ack(), tot_len/4 forces compiler to emit an integer divide, while we can help it to use a right shift, less expensive.

net/ipv6/tcp_ipv6.c +1 -1
index 0268e11..92f0fda 100644
@@ -1124,7 +1124,7 @@ static void tcp_v6_send_ack(struct tcp_timewait_sock *tw,
 	memset(t1, 0, sizeof(*t1));
 	t1->dest = th->source;
 	t1->source = th->dest;
-	t1->doff = tot_len/4;
+	t1->doff = tot_len >> 2;
 	t1->seq = htonl(seq);
 	t1->ack_seq = htonl(ack);
 	t1->ack = 1;
#1 lore raw

In article <[email protected]> (at Fri, 21 Dec 2007 07:03:58 +0100), Eric Dumazet <[email protected]> says:

Quoted 3 lines

Because tot_len is signed in tcp_v6_send_ack(), tot_len/4 forces compiler to emit an integer divide, while we can help it to use a right shift, less expensive.

Are you really sure?
At least, gcc-4.1.2-20061115 (debian) does not make any difference.

And, IMHO, because shift for signed variable is fragile, so we should avoid using it.

--yoshfuji

#2 lore raw
Quoted 6 lines · YOSHIFUJI Hideaki / 吉藤英明

In article <[email protected]> (at Fri, 21 Dec 2007 07:03:58 +0100), Eric Dumazet <[email protected]> says:

Quoted 3 lines

Because tot_len is signed in tcp_v6_send_ack(), tot_len/4 forces compiler to emit an integer divide, while we can help it to use a right shift, less expensive.

Quoted 6 lines

Are you really sure?
At least, gcc-4.1.2-20061115 (debian) does not make any difference.

And, IMHO, because shift for signed variable is fragile, so we should avoid using it.

Yes I am sure, but maybe you are on x86_64 ?

gcc-4.2.2 on x86

# objdump --disassemble net/ipv6/tcp_ipv6.o|grep -6 idiv
      b2:       66 8b 42 02             mov    0x2(%edx),%ax
      b6:       ba 04 00 00 00          mov    $0x4,%edx
      bb:       89 d7                   mov    %edx,%edi
      bd:       66 89 45 00             mov    %ax,0x0(%ebp)
      c1:       89 d8                   mov    %ebx,%eax
      c3:       99                      cltd
      c4:       f7 ff                   idiv   %edi
      c6:       88 c2                   mov    %al,%dl
      c8:       8a 45 0c                mov    0xc(%ebp),%al
      cb:       c1 e2 04                shl    $0x4,%edx
      ce:       83 e0 0f                and    $0xf,%eax
      d1:       09 d0                   or     %edx,%eax
      d3:       88 45 0c                mov    %al,0xc(%ebp)

If you think tot_len can be negative, I understand you can be against this patch. But I am sure it's allways > 0, even if I am a total ipv6 newbie :)

Thank you

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

#4 lore raw
Quoted 18 lines · YOSHIFUJI Hideaki / 吉藤英明

In article <[email protected]> (at Fri, 21 Dec 2007 08:06:32 +0100), Eric Dumazet <[email protected]> says:

Quoted 9 lines
Quoted 8 lines · YOSHIFUJI Hideaki / 吉藤英明

In article <[email protected]> (at Fri, 21 Dec 2007 07:03:58 +0100), Eric Dumazet <[email protected]> says:

Quoted 3 lines

Because tot_len is signed in tcp_v6_send_ack(), tot_len/4 forces compiler to emit an integer divide, while we can help it to use a right shift, less expensive.

Are you really sure?
At least, gcc-4.1.2-20061115 (debian) does not make any difference.

Quoted 6 lines
Quoted 3 lines

And, IMHO, because shift for signed variable is fragile, so we should avoid using it.

Yes I am sure, but maybe you are on x86_64 ?

gcc-4.2.2 on x86

Quoted 7 lines

I'm on gcc-4.1.2 20061115 (prerelease) (Debian 4.1.1-21), on x86 (i686). Maybe compiler difference?!

Quoted 1 line

If you think tot_len can be negative, I understand you can be against this patch. But I am sure it's allways > 0, even if I am a total ipv6 newbie :)

Okay, anyway, I'll convert them to unsigned int, which is more appropriate.

I didnt chose this path, because David was against changing some fields from 'int' to 'unsigned'. If you look in other parts of networking, we have many >> 1 or >> 2 already there.

#5 lore raw

In article <[email protected]> (at Fri, 21 Dec 2007 08:39:24 +0100), Eric Dumazet <[email protected]> says:

Quoted 6 lines
Quoted 2 lines

Okay, anyway, I'll convert them to unsigned int, which is more appropriate.

I didnt chose this path, because David was against changing some fields from 'int' to 'unsigned'. If you look in other parts of networking, we have many >> 1 or >> 2 already there.

I do think it is safe to convert them here.

--yoshfuji

#6 lore raw
Quoted 8 lines · YOSHIFUJI Hideaki / 吉藤英明

In article <[email protected]> (at Fri, 21 Dec 2007 08:39:24 +0100), Eric Dumazet <[email protected]> says:

Quoted 3 lines
Quoted 2 lines

Okay, anyway, I'll convert them to unsigned int, which is more appropriate.

I didnt chose this path, because David was against changing some fields from 'int' to 'unsigned'. If you look in other parts of networking, we have many >> 1 or >> 2 already there.

I do think it is safe to convert them here.

Sure :)

maintainer
#7 lore raw
In reply to Eric Dumazet

From: Eric Dumazet <[email protected]>
Date: Fri, 21 Dec 2007 08:39:24 +0100

Quoted 3 lines

I didnt chose this path, because David was against changing some fields from 'int' to 'unsigned'. If you look in other parts of networking, we have many >> 1 or >> 2 already there.

I don't remember making this statement, where did I say this? I'm genuinely curious :-)

But I am indeed against it in cases where the variable is compared against signed quantities.

I think the shifts are more pretty and more closely match what the programmer wants to come out of the compiler. Getting all of these divides is an awful surprise for me.

I've learned over the years to never explicitly code divides or multiplies by powers of two and to always use shifts. As a result I am never surprised. In fact I've been burnt every time I mistakedly didn't use a shift.

Nevertheless, this tcplen arg is always assigned to and used with unsigned quantities so I'll apply Yoshifuji's version of the fix.

#8 lore raw
In reply to a message not in the archive
Quoted 14 lines · YOSHIFUJI Hideaki / 吉藤英明

In article <[email protected]> (at Fri, 21 Dec 2007 08:06:32 +0100), Eric Dumazet <[email protected]> says:

Quoted 12 lines
Quoted 11 lines · YOSHIFUJI Hideaki / 吉藤英明

In article <[email protected]> (at Fri, 21 Dec 2007 07:03:58 +0100), Eric Dumazet <[email protected]> says:

Quoted 3 lines

Because tot_len is signed in tcp_v6_send_ack(), tot_len/4 forces compiler to emit an integer divide, while we can help it to use a right shift, less expensive.

Are you really sure?
At least, gcc-4.1.2-20061115 (debian) does not make any difference.

And, IMHO, because shift for signed variable is fragile, so we should avoid using it.

Quoted 6 lines
Quoted 3 lines

Yes I am sure, but maybe you are on x86_64 ?

gcc-4.2.2 on x86

I'm on gcc-4.1.2 20061115 (prerelease) (Debian 4.1.1-21), on x86 (i686). Maybe compiler difference?!

hum, more probably a .config difference. Try with

CONFIG_CC_OPTIMIZE_FOR_SIZE=Y

(default config)

#9 lore raw
In reply to YOSHIFUJI Hideaki / 吉藤英明
Quoted 8 lines · YOSHIFUJI Hideaki / 吉藤英明

In article <[email protected]> (at Fri, 21 Dec 2007 07:03:58 +0100), Eric Dumazet <[email protected]> says:

Quoted 3 lines

Because tot_len is signed in tcp_v6_send_ack(), tot_len/4 forces compiler to emit an integer divide, while we can help it to use a right shift, less expensive.

Are you really sure?
At least, gcc-4.1.2-20061115 (debian) does not make any difference.

Quite true -- thus it is a matter of taste to the programmer. Constant folding inside the compiler ensures that "foo / 4" asm output is just as optimal as a shift.

Quoted 2 lines

And, IMHO, because shift for signed variable is fragile, so we should avoid using it.

I respectfully disagree, but this is an unrelated matter. As you say, "/4" is fine as-is.

Jeff

maintainer
#10 lore raw

From: Jeff Garzik <[email protected]>
Date: Fri, 21 Dec 2007 06:26:48 -0500

Quoted 11 lines
Quoted 8 lines · YOSHIFUJI Hideaki / 吉藤英明

In article <[email protected]> (at Fri, 21 Dec 2007 07:03:58 +0100), Eric Dumazet <[email protected]> says:

Quoted 3 lines

Because tot_len is signed in tcp_v6_send_ack(), tot_len/4 forces compiler to emit an integer divide, while we can help it to use a right shift, less expensive.

Are you really sure?
At least, gcc-4.1.2-20061115 (debian) does not make any difference.

Quite true -- thus it is a matter of taste to the programmer.

Not true, the code output does change, check your optimize-for-size kernel config setting.

This was discussed and explained later in this thread, and I also explained it to you on IRC Jeff ;-)

#11 lore raw
Quoted 11 lines · David Miller

From: Jeff Garzik <[email protected]>
Date: Fri, 21 Dec 2007 06:26:48 -0500

Quoted 8 lines
Quoted 7 lines · YOSHIFUJI Hideaki / 吉藤英明

In article <[email protected]> (at Fri, 21 Dec 2007 07:03:58 +0100), Eric Dumazet <[email protected]> says:

Quoted 3 lines

Because tot_len is signed in tcp_v6_send_ack(), tot_len/4 forces compiler to emit an integer divide, while we can help it to use a right shift, less expensive.

Are you really sure?
At least, gcc-4.1.2-20061115 (debian) does not make any difference.

Quoted 7 lines
Quoted 1 line

Quite true -- thus it is a matter of taste to the programmer.

Not true, the code output does change, check your optimize-for-size kernel config setting.

This was discussed and explained later in this thread, and I also explained it to you on IRC Jeff ;-)

[looks]

For signed integers, this is true.

For unsigned integers, the code output is the same, regardless of optimization setting.

Jeff

#12 lore raw
In reply to Eric Dumazet
Quoted 3 lines · Eric Dumazet

Because tot_len is signed in tcp_v6_send_ack(), tot_len/4 forces compiler to emit an integer divide, while we can help it to use a right shift, less expensive.

Can't you just change tot_len to unsigned here? It's just sizeof and some positive constants added...

Signature · 1 line
i.
net/ipv6/tcp_ipv6.c +1 -1
index 0268e11..92f0fda 100644
@@ -1124,7 +1124,7 @@ static void tcp_v6_send_ack(struct tcp_timewait_sock *tw,
 	memset(t1, 0, sizeof(*t1));
 	t1->dest = th->source;
 	t1->source = th->dest;
-	t1->doff = tot_len/4;
+	t1->doff = tot_len >> 2;
 	t1->seq = htonl(seq);
 	t1->ack_seq = htonl(ack);
 	t1->ack = 1;