Skip to contentSkip to conversation

net/ipv6/addrconf.c: Use prandom_u32_max for rfc3315 backoff time computation

RFC PATCH Maciej Żenczykowski 2 participants 2 messages Mar 28, 2020 1 message not in the archive

Subsystems (2)
lore
Share & tools
lore
#1 lore raw
In reply to a message not in the archive

Review

Quoted 6 lines
        /* multiply 'initial retransmission time' by 0.9 .. 1.1 */
(quoted diff) +2 -3
-       u64 tmp = (900000 + prandom_u32() % 200001) * (u64)irt;
-       do_div(tmp, 1000000);
-       return (s32)tmp;
+       s32 range = irt / 5;
+       return irt - (s32)(range/2) + (s32)prandom_u32_max(range);

The cast on range/2 looks entirely spurious

Review

Quoted 6 lines
        /* multiply 'retransmission timeout' by 1.9 .. 2.1 */
(quoted diff) +2 -3
-       u64 tmp = (1900000 + prandom_u32() % 200001) * (u64)rt;
-       do_div(tmp, 1000000);
-       if ((s32)tmp > mrt) {
+       s32 range = rt / 5;
+       s32 tmp = 2*rt - (s32)(range/2) + (s32)prandom_u32_max(range);

Here as well. Honestly the cast on prandom might also not be necessary, but that at least has a reason.

Review

Quoted 6 lines
(quoted diff) +3 -2
+       if (tmp > mrt) {
                /* multiply 'maximum retransmission time' by 0.9 .. 1.1 */
-               tmp = (900000 + prandom_u32() % 200001) * (u64)mrt;
-               do_div(tmp, 1000000);
+               range = mrt / 5;
+               tmp = mrt - (s32)(range/2) + (s32)prandom_u32_max(range);

Ditto.

Quoted 3 lines
        }
        return (s32)tmp;

}

- Maciej

#2 lore raw
Quoted 8 lines · Maciej ?enczykowski

Review

Quoted 6 lines
        /* multiply 'initial retransmission time' by 0.9 .. 1.1 */
(quoted diff) +2 -3
-       u64 tmp = (900000 + prandom_u32() % 200001) * (u64)irt;
-       do_div(tmp, 1000000);
-       return (s32)tmp;
+       s32 range = irt / 5;
+       return irt - (s32)(range/2) + (s32)prandom_u32_max(range);

The cast on range/2 looks entirely spurious

You're absolutely right; sorry about that. I was trying to preserve the previous code's mixture of signed and unsigned types and managed to confuse myself.

(I think I got distracted researching whether the inputs could be negative.)

Quoted 9 lines

Review

Quoted 6 lines
        /* multiply 'retransmission timeout' by 1.9 .. 2.1 */
(quoted diff) +2 -3
-       u64 tmp = (1900000 + prandom_u32() % 200001) * (u64)rt;
-       do_div(tmp, 1000000);
-       if ((s32)tmp > mrt) {
+       s32 range = rt / 5;
+       s32 tmp = 2*rt - (s32)(range/2) + (s32)prandom_u32_max(range);

Here as well. Honestly the cast on prandom might also not be necessary, but that at least has a reason.

The whole thing should go. How about just doing it all in unsigned:

static inline s32 rfc3315_s14_backoff_init(s32 irt)

{
	/* multiply 'initial retransmission time' by 0.9 .. 1.1 */
	u32 range = irt / 5u;
	return irt - range/2 + prandom_u32_max(range);
}

static inline s32 rfc3315_s14_backoff_update(s32 rt, s32 mrt)

{
	/* multiply 'retransmission timeout' by 1.9 .. 2.1 */
	 u32 range = rt / 5u;
	 u32 tmp = 2u*rt - range/2 + prandom_u32_max(range);
	 if (tmp > mrt) {
		 /* multiply 'maximum retransmission time' by 0.9 .. 1.1 */
		  range = mrt / 5u;
		  tmp = mrt - range/2 + prandom_u32_max(range);
	}
	return tmp;
}

That lets "range/2" be implemented as a 1-bit shift.

An interesting question for the latter is whether "prandom_u32_max(range) - range/2" can be considered a common subexpression, or is they have to be *independent* random values.