Received: by 2002:a25:6193:0:0:0:0:0 with SMTP id v141csp2656003ybb; Mon, 30 Mar 2020 10:17:50 -0700 (PDT) X-Google-Smtp-Source: ADFU+vvMRbaNzl0t+QJiWxSyHKsAU88sMUn72NWXNagfCvPzTzixJpnXApPsVhOwhwOQRq141mys X-Received: by 2002:a9d:618d:: with SMTP id g13mr154634otk.72.1585588670725; Mon, 30 Mar 2020 10:17:50 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1585588670; cv=none; d=google.com; s=arc-20160816; b=Dquzjwq5DiN2Vz4hbvpubz/Ggguqy8qL5NgDD4t0U89XPODsu/9aD3hUgc2PFYNaX1 hAHsG6Epm7dKalbbfSuq6deS9K8ipLaQMVSG443intREsP6biEAWUA1DGT4qiFiFiBY9 HvaPUI2ciuUp/ztXpKSYV699Eb7Cip/wKwLeQ5PjjVvLy4tOCI9J5aqmyg0Ht91Lz+zk k+Ifrf7MzCF1Ei9qVKp9MWWVo+tiP4iLD7b4hSa3tlbaom9gXKENa4N8ZijcF8gnNllr bfMtaWofCv4faigkkmS5YlS/9j3hzNwAIwTnOzWwizQ7VtaZ/4LncPYxI9FeMXXRhugw c5Fw== 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=A6NhD6KVz/ZLBW2ih3fbhf6Dk+dqBrP7derRS172xsE=; b=j+5OYoNx8YY1NUGOUc4Ht6fgY7bcVtDGH9xgJjxNpGBwzQu/upDEAbOxeHltPWTNd6 3fsdF5yJF7Y5n85HfO+dj7nJ2wuzgVEPdpxFszDaAeRfv5ddOOZp+4xtXnhvDi0QFZcF 1PNQ/i7ayF+zqBBQA93z+OL/ZFW2QBPXzGI6jDAgPFuUK1A6HgcQYquvWLwg6iCXLiBS HXKDUDgrpIYzY9bgjjfzi0hbOSxq1m8AU4zbMHBaLR1DKU8dmzT9LmGl9PINvaaoiUZd 4HTVfIIBouez4+QLq4s+2TVl9/dCzL29ix5KTADYVNeCgINetMXiY+14MGLEkOuOlYnY kaBQ== 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 r5si6396783otp.241.2020.03.30.10.17.37; Mon, 30 Mar 2020 10:17:50 -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 S1729240AbgC3Qeq (ORCPT + 99 others); Mon, 30 Mar 2020 12:34:46 -0400 Received: from mx.sdf.org ([205.166.94.20]:62779 "EHLO mx.sdf.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727191AbgC3Qeq (ORCPT ); Mon, 30 Mar 2020 12:34:46 -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 02UGYDT4006227 (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256 bits) verified NO); Mon, 30 Mar 2020 16:34:16 GMT Received: (from lkml@localhost) by sdf.org (8.15.2/8.12.8/Submit) id 02UGYCSx027097; Mon, 30 Mar 2020 16:34:12 GMT Date: Mon, 30 Mar 2020 16:34:12 +0000 From: George Spelvin To: Joseph Qi Cc: linux-kernel@vger.kernel.org, Mark Fasheh , Joel Becker , ocfs2-devel@oss.oracle.com, lkml@sdf.org Subject: Re: [RFC PATCH v1 29/50] fs/ocfs2/journal: Use prandom_u32() and not /dev/random for timeout Message-ID: <20200330163412.GA2459@SDF.ORG> References: <202003281643.02SGhIOY022599@sdf.org> <016c2bdc-68eb-245f-2292-d00d0d8e45a5@linux.alibaba.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <016c2bdc-68eb-245f-2292-d00d0d8e45a5@linux.alibaba.com> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Mon, Mar 30, 2020 at 08:09:33PM +0800, Joseph Qi wrote: > Sorry for the late reply since I might miss this mail. You're hardly late; I expect replies to dribble in for a week. > On 2019/3/21 11:07, George Spelvin wrote: >> diff --git a/fs/ocfs2/journal.c b/fs/ocfs2/journal.c >> index 68ba354cf3610..939a12e57fa8b 100644 >> --- a/fs/ocfs2/journal.c >> +++ b/fs/ocfs2/journal.c >> @@ -1884,11 +1884,8 @@ int ocfs2_mark_dead_nodes(struct ocfs2_super *osb) >> */ >> static inline unsigned long ocfs2_orphan_scan_timeout(void) >> { >> - unsigned long time; >> - >> - get_random_bytes(&time, sizeof(time)); >> - time = ORPHAN_SCAN_SCHEDULE_TIMEOUT + (time % 5000); >> - return msecs_to_jiffies(time); >> + return msecs_to_jiffies(ORPHAN_SCAN_SCHEDULE_TIMEOUT) + >> + prandom_u32_max(5 * HZ); > > Seems better include the prandom_u32_max() into msecs_to_jiffies()? What I'm trying to take advantage of here is constant propagation. msecs_to_jiffies is zero cost (it's evaluated entirely at compile time) if its argument is a compile-time constant. It's a function call and a few instructions if its argument is variable. msecs_to_jiffies(ORPHAN_SCAN_SCHEDULE_TIMEOUT + prandom_u32_max(5000)) would be forced to use the expensive version. The compiler does't know, but *I* know, that msecs_to_jiffies() is a linear function, and prandom_u32_max() is a sort-of linear function. (It's a linear function for a given PRNG starting state, so each individual call is linear, but multiple calls mess things up.) Modulo a bit of rounding, we have: msecs_to_jiffies(a + b) = msecs_to_jiffies(a) + msecs_to_jiffies(b) msecs_to_jiffies(a) * b = msecs_to_jiffies(a * b) prandom_u32_max(a) * b = prandom_u32_max(a * b) prandom_u32_max(msecs_to_jiffies(a)) = msecs_to_jiffies(prandom_u32_max(a)) By doing the addition in jiffies rather than milliseconds, we get the cheap code. It's not a huge big deal, but it's definitely smaller and faster. Admittedly, I happen to be using HZ = 300, which requires a multiply to convert, and makes the resultant random numbers slightly non-uniform. The default HZ = 250 makes it just a divide by 4, which is pretty simple. (When HZ = 300, you get 1..3 ms -> 1 jiffy, 4..6 ms -> 2 jiffies, and 7..10 ms -> 3 jiffies. Multiples of 3 are 33% more likely to be chosen.) It just seems silly and wasteful to pick a random number between 0 and 4999 (plus 30000), only to convert it to a random number between 0 and 1249 (plus 7500). And if HZ = 2000 ever happens, the timeout won't be artificially limited to integer milliseconds.