Received: by 2002:a25:6193:0:0:0:0:0 with SMTP id v141csp787824ybb; Sat, 28 Mar 2020 10:05:26 -0700 (PDT) X-Google-Smtp-Source: ADFU+vv3POA9JXrOGt3h4VntMnu8L4x66TMlsAWbltEgrM24K+sYicDxRS3f84GbayPW7A74M05T X-Received: by 2002:a05:6820:221:: with SMTP id j1mr4005962oob.12.1585415126837; Sat, 28 Mar 2020 10:05:26 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1585415126; cv=none; d=google.com; s=arc-20160816; b=Slh8W7JuWnDxlauPkdap3L61yg2saiDhYnToOf2PyUoIqJ6bSpOBKEilb0NMmRcE2t I04dXAm7LnRvpihrq8WZNIdRl3Smp52tfuXMVsopiHQsLFZ9X86+MaA07Eaa6L2JqK1O hjGSCA7KRUtQ0fC5wJOjndNr77drDwPu6JMP8U34Ky8rgRIqzdZU27uabqUvT2ir0udf hLWPmwKL0OStwzKSPi5EYCWHm9I817wqqbiI6Y0VlPufZBjjNKuERJWjVrlIAwAF/Xdc Dn8nlqADriez6IpSwZw4ou7EMna8kr0kR9fHJBdcAENK5SU3MFKrY9XofSOmY1wdl4FJ lfYA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:content-transfer-encoding :mime-version:subject:date:from:message-id; bh=+JklNqEU6fqZZ5DngUQfgn5f1BEP+uS9GctLmF0hYGw=; b=o/Ew+jg424qBqRORw+MO+WTnrVyUad4kn8q85g0cys/6YhQWvuSprub/UgtVMoMqj9 spVhpRjmZi7aWw1cW+uWEsrpJKqnt412HdpdTJJPwBKSs2ofLhrdQsTb0T0Ts8z0Pi6u UBNzR2luuSgMDkxzzQO4pB9evzmF51AmdfBaKVY77EpqGr5MKHqTRr+vl3bjhkSk+8H6 gwkdJjKMLlmGTDhiuQmbrtrqyo3MZQapTb506zA0npH8R5UrQtddDkB0VwGn2EXhF8vs jUi1NBSfshmgoFYAtsdyx1hlPEyA2yIeIzqnyBFS83qFuR14MqbiBnEGIIGjzE7+FAnS fwtA== 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 j206si4040299oia.158.2020.03.28.10.05.13; Sat, 28 Mar 2020 10:05:26 -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 S1727067AbgC1REl (ORCPT + 99 others); Sat, 28 Mar 2020 13:04:41 -0400 Received: from mx.sdf.org ([205.166.94.20]:63281 "EHLO mx.sdf.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725807AbgC1REk (ORCPT ); Sat, 28 Mar 2020 13:04:40 -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 02SH47cb023288 (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256 bits) verified NO); Sat, 28 Mar 2020 17:04:08 GMT Received: (from lkml@localhost) by sdf.org (8.15.2/8.12.8/Submit) id 02SH4767016334; Sat, 28 Mar 2020 17:04:07 GMT Message-Id: <202003281704.02SH4767016334@sdf.org> From: George Spelvin Date: Wed, 21 Aug 2019 20:30:18 -0400 Subject: [RFC PATCH v1 18/50] net/ipv6/addrconf.c: Use prandom_u32_max for rfc3315 backoff time computation MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit To: linux-kernel@vger.kernel.org, lkml@sdf.org Cc: Maciej Zenczykowski , "David S. Miller" , Alexey Kuznetsov , Hideaki YOSHIFUJI , netdev@vger.kernel.org Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org There's no need for 64-bit intermediate values and do_div. (Actually, the algorithm isn't changing much, except that the old code used a scaling factor of 1 million. prandom_u32_max uses a factor of 2^32, making the final division more efficient.) One thing that concerns me a bit is that the data types are all signed. The old code cast the inputs to unsigned and produced strange overflowed results if they were negative, so presumably that never happens in practice. The new code works the same for positive inputs, but produces different strange overflowed results if fed negative inputs. Signed-off-by: George Spelvin Cc: Maciej Żenczykowski Cc: "David S. Miller" Cc: Alexey Kuznetsov Cc: Hideaki YOSHIFUJI Cc: netdev@vger.kernel.org --- net/ipv6/addrconf.c | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/net/ipv6/addrconf.c b/net/ipv6/addrconf.c index ec3f472bc5a8f..5172f1f874363 100644 --- a/net/ipv6/addrconf.c +++ b/net/ipv6/addrconf.c @@ -103,20 +103,19 @@ static inline u32 cstamp_delta(unsigned long cstamp) static inline s32 rfc3315_s14_backoff_init(s32 irt) { /* 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); } static inline s32 rfc3315_s14_backoff_update(s32 rt, s32 mrt) { /* 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)(range/2) + (s32)prandom_u32_max(range); + 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); } return (s32)tmp; } -- 2.26.0