Received: by 2002:a25:6193:0:0:0:0:0 with SMTP id v141csp809938ybb; Sat, 28 Mar 2020 10:38:12 -0700 (PDT) X-Google-Smtp-Source: ADFU+vv2qE9OljLZFLlyMlWxAOOMi2MOhzhZSPgUZYsJqw80W+xS32O0Y5XAJIthu6I72HHsPTt7 X-Received: by 2002:a9d:7c94:: with SMTP id q20mr2943438otn.150.1585417092331; Sat, 28 Mar 2020 10:38:12 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1585417092; cv=none; d=google.com; s=arc-20160816; b=vg8T3w9CEu/9FzrvjdqxN3jxBsXCWtib2aVKnbFGsfNdMntcX4r6Hngw5/RDqbt2Z3 bl++Mr7wO+X131R6QjWcVAjRJirmZ20WJiHs/krWCxM2+6iBHEAFUd9GGljhDlUwF4Q9 iiDXHxYuV0Nk9OfMcOTAjaJRl33bi504ZGBEQfohlymnXid1S0zgjb6cusfQ2h34J8gO u9DCzU30HsU6jZuwL7ahWHt0KKX1feAp/0R8GXgpdkR/h5tAbUsRZNsG4oKuJwOlZ63i uiKUJfFukARvDJKH+W9aFa2dlQho4ZJol4mv0Fum5cmOk/g4kaJmUTERQ1FMDREr+gc0 gD5Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:in-reply-to:content-disposition :mime-version:references:message-id:subject:cc:to:from:date; bh=cBImYwfTGNh6UU0RBA5GJZ92uJCdHUuel6ccDnLa+OM=; b=nM7iOS/3TDM5d5I72wnSAFW0QcWOfsZfmOV48LDNb8K3F12tyXpCcVLk/3GbFnzWZ2 bJiqyxvwizUoZUjo0fjyUhB3NQ8IJ+kftdS3g1RXUF9xweF1iRw8RCFr3obT/Q64F8/m UwLOl372ALiF5cuTPmNshXjzRuKP8oaFNEbsgpE17B4A8Zrm1GKRyn7PSXRNXuTj1PPR s/YSZp6ONJeFuLiWn6ccYYWLmm3HF6gCNYOXGnB9tsaUfOXUngRgL6sEcxN97fVNBjev qjhR/54QVY9TWFIv7HCZp/esgBtaAdY320ilW2yKMC2fILWgATO/d7Abzplc3mQphvS9 e4Zw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id c8si4784309otk.67.2020.03.28.10.37.58; Sat, 28 Mar 2020 10:38:12 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726389AbgC1Rhn (ORCPT + 99 others); Sat, 28 Mar 2020 13:37:43 -0400 Received: from mx.sdf.org ([205.166.94.20]:58571 "EHLO mx.sdf.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725807AbgC1Rhm (ORCPT ); Sat, 28 Mar 2020 13:37:42 -0400 Received: from sdf.org (IDENT:lkml@sdf.lonestar.org [205.166.94.16]) by mx.sdf.org (8.15.2/8.14.5) with ESMTPS id 02SHbC9e005749 (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256 bits) verified NO); Sat, 28 Mar 2020 17:37:12 GMT Received: (from lkml@localhost) by sdf.org (8.15.2/8.12.8/Submit) id 02SHbC1h016215; Sat, 28 Mar 2020 17:37:12 GMT Date: Sat, 28 Mar 2020 17:37:12 +0000 From: George Spelvin To: Maciej Zenczykowski Cc: Kernel hackers , "David S. Miller" , Alexey Kuznetsov , Hideaki YOSHIFUJI , Linux NetDev , lkml@sdf.org Subject: Re: [RFC PATCH v1 18/50] net/ipv6/addrconf.c: Use prandom_u32_max for rfc3315 backoff time computation Message-ID: <20200328173712.GB5859@SDF.ORG> References: <202003281643.02SGhD4n009959@sdf.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Sat, Mar 28, 2020 at 09:56:58AM -0700, Maciej ?enczykowski wrote: >> /* multiply 'initial retransmission time' by 0.9 .. 1.1 */ >> - 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.) >> /* multiply 'retransmission timeout' by 1.9 .. 2.1 */ >> - 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.