From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-oi1-x22a.google.com (mail-oi1-x22a.google.com [IPv6:2607:f8b0:4864:20::22a]) by sourceware.org (Postfix) with ESMTPS id CC1333858C50 for ; Tue, 29 Mar 2022 20:44:19 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org CC1333858C50 Received: by mail-oi1-x22a.google.com with SMTP id t21so15342742oie.11 for ; Tue, 29 Mar 2022 13:44:19 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:message-id:date:mime-version:user-agent:subject :content-language:to:cc:references:from:in-reply-to :content-transfer-encoding; bh=Fc2W7ePKVuy86zSKHLPktt8hf1Q09VES3G0XC4UB29M=; b=JqVSCRzxUCsQde3kh8YkffDUiRaFpEAI0s8ISR649iHc/Kjgm8ryM72MIpppHqotsO COItEiL7ROuCjx8rUefOL/iaFiLW2YxHVUKF5jxKREkwLXok66AyYx9S0RLnJMQHub1q RGjgUpOQzBHW4iIpd7RAk6joA1LG6MBnyzrjuA6/hRUMNwnwnlDDlcY/D4TyUTbFVrqx X26DPZXw5GFfvNtOVquCa4mG0N+OJM2bDdHdZFOX7dKHR3jObLBVcrNaZiuvDrb8PDYo +1yb76LmfHhU9//mq8Hl/T5xOxXhT4rK8sMhFOdzU4JEItTXLTi1oPtilCxIlpYxt7V8 fxFQ== X-Gm-Message-State: AOAM531RBmVNH8KN3QPt1+odK3OuHiK+lIykt9y3o2lEILsgImXTTFMd A43r9AtdEg12o0PovApe3v7Asw== X-Google-Smtp-Source: ABdhPJxeCto0YsVWft6M/XSB91IAsFkiv6jtwFO4DHqcDU2+e44vbGEf4aClXc0rlnLGCoNLVaZB/Q== X-Received: by 2002:a54:4197:0:b0:2d9:be61:f577 with SMTP id 23-20020a544197000000b002d9be61f577mr480354oiy.271.1648586658836; Tue, 29 Mar 2022 13:44:18 -0700 (PDT) Received: from ?IPV6:2804:431:c7cb:a6c0:f1e1:dcf6:8c18:df3b? ([2804:431:c7cb:a6c0:f1e1:dcf6:8c18:df3b]) by smtp.gmail.com with ESMTPSA id r10-20020a056830448a00b005c9344dac06sm9715964otv.19.2022.03.29.13.44.17 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 29 Mar 2022 13:44:18 -0700 (PDT) Message-ID: <1802122d-9160-8596-b22c-4c909df9a68d@linaro.org> Date: Tue, 29 Mar 2022 17:44:15 -0300 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.7.0 Subject: Re: [PATCH v1 1/2] random-bits: Factor out entropy generating function Content-Language: en-US To: "H.J. Lu" , Noah Goldstein Cc: GNU C Library References: <20220328220936.2724834-1-goldstein.w.n@gmail.com> From: Adhemerval Zanella In-Reply-To: Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-12.2 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, NICE_REPLY_A, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 29 Mar 2022 20:44:21 -0000 On 29/03/2022 17:14, H.J. Lu wrote: > On Tue, Mar 29, 2022 at 12:56 PM Noah Goldstein via Libc-alpha > wrote: >> >> On Tue, Mar 29, 2022 at 2:51 PM Adhemerval Zanella >> wrote: >>> >>> >>> >>> On 28/03/2022 19:09, Noah Goldstein via Libc-alpha wrote: >>>> On some architectures `clock_gettime` is undesirable as >>>> it may use a syscall or there may be a faster alternative. >>>> Future architecture specific functions can be added in >>>> sysdeps//random-bits-entropy.h to provide a version of >>>> 'random_bits_entropy' that doesn't use 'clock_gettime'. >>>> --- >>>> include/random-bits.h | 16 ++++++-------- >>>> sysdeps/generic/random-bits-entropy.h | 31 +++++++++++++++++++++++++++ >>>> 2 files changed, 37 insertions(+), 10 deletions(-) >>>> create mode 100644 sysdeps/generic/random-bits-entropy.h >>>> >>>> diff --git a/include/random-bits.h b/include/random-bits.h >>>> index 17665b479a..016b87576c 100644 >>>> --- a/include/random-bits.h >>>> +++ b/include/random-bits.h >>>> @@ -19,21 +19,17 @@ >>>> #ifndef _RANDOM_BITS_H >>>> # define _RANDOM_BITS_H >>>> >>>> -#include >>>> -#include >>>> +# include >>>> +# include >>>> >>>> -/* Provides fast pseudo-random bits through clock_gettime. It has unspecified >>>> - starting time, nano-second accuracy, its randomness is significantly better >>>> - than gettimeofday, and for mostly architectures it is implemented through >>>> - vDSO instead of a syscall. Since the source is a system clock, the upper >>>> - bits will have less entropy. */ >>>> +/* Provides fast pseudo-random bits through architecture specific >>>> + random_bits_entropy. Expectation is source is some timing function so >>>> + the upper bits have less entropy. */ >>>> static inline uint32_t >>>> random_bits (void) >>>> { >>>> - struct __timespec64 tv; >>>> - __clock_gettime64 (CLOCK_MONOTONIC, &tv); >>>> + uint32_t ret = random_bits_entropy (); >>>> /* Shuffle the lower bits to minimize the clock bias. */ >>>> - uint32_t ret = tv.tv_nsec ^ tv.tv_sec; >>>> ret ^= (ret << 24) | (ret >> 8); >>>> return ret; >>>> } >>> >>> We already provide hp-timing.h, which uses rdtsc on x86 and clock_gettime on >>> generic interface (and other high precision timing on other architectures). >>> So I think a better way would be to: >> >> For x86/generic that works but other architectures also have hp-timing >> implementations that might not be suitable for this (i.e there might be >> an entropy regression). > > The default hp-timing.h has > > # define HP_TIMING_NOW(var) \ > ({ \ > struct __timespec64 tv; \ > __clock_gettime64 (CLOCK_MONOTONIC, &tv); \ > (var) = (tv.tv_nsec + UINT64_C(1000000000) * tv.tv_sec); \ > }) > > It isn't the same as the current include/random-bits.h. Maybe refactor hp-timing.h to add a routine to get the system clock without any adjustments? I don't have a strong preference here, but I take that you are not aiming to use RDRAND or similar instruction, since you are optimizing to latency. So I see that using hp-timing.h seems the best approach, since it should work similar on different architectures (and each one might disable if the entropy is not large enough).