From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-17.5 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 96812C64E7A for ; Tue, 1 Dec 2020 12:24:52 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 29D0220679 for ; Tue, 1 Dec 2020 12:24:52 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=kernel.org header.i=@kernel.org header.b="oamBLPTG" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730882AbgLAMYv (ORCPT ); Tue, 1 Dec 2020 07:24:51 -0500 Received: from mail.kernel.org ([198.145.29.99]:48190 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727374AbgLAMYu (ORCPT ); Tue, 1 Dec 2020 07:24:50 -0500 Received: from mail-lj1-f171.google.com (mail-lj1-f171.google.com [209.85.208.171]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id F1DE220870 for ; Tue, 1 Dec 2020 12:24:09 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1606825450; bh=bBnrt/qXPq+H01MHvNMWCrKJ7NdoTh2PlKA4hnqr0vw=; h=References:In-Reply-To:From:Date:Subject:To:Cc:From; b=oamBLPTGYBVKgHcGxo4swpudDywxg915icZq6iIxy7vZGrGgXeJxtEL0gNh5xMjbw c4t44STSXttX0GTkGGvXoAl1DisbmFfGlIOY5Qo71JYr0lISDhQGjEZ2Aba5fgcVCU 6JQytp1lZEmg+gZgffbPzKwTE1XlGthk2sAIKE3Q= Received: by mail-lj1-f171.google.com with SMTP id y10so2548434ljc.7 for ; Tue, 01 Dec 2020 04:24:09 -0800 (PST) X-Gm-Message-State: AOAM533lj+/ktVckjpjKQvRH3edIqgyy+X5qRuxfJal8zvLiqyqxOkuj XzR5gZR91A2EyMHcn+g3O3aDv5LzPa7dMUnkCws= X-Google-Smtp-Source: ABdhPJwFK5+905CbgqECOO1cwvLh6rCSUwEu4cRIahJ2fhT+HsmIJLhDNt1D6iZInyBu8GXHMh1ZstaQibTviZg8st0= X-Received: by 2002:a05:651c:1213:: with SMTP id i19mr1110517lja.407.1606825448068; Tue, 01 Dec 2020 04:24:08 -0800 (PST) MIME-Version: 1.0 References: <20201105152944.16953-1-ardb@kernel.org> In-Reply-To: From: Ard Biesheuvel Date: Tue, 1 Dec 2020 13:23:56 +0100 X-Gmail-Original-Message-ID: Message-ID: Subject: Re: [PATCH] random: avoid arch_get_random_seed_long() when collecting IRQ randomness To: Eric Biggers , "Jason A. Donenfeld" Cc: "Theodore Y. Ts'o" , Linux Kernel Mailing List , Linux ARM , Marc Zyngier , Mark Rutland , Mark Brown , Andre Przywara Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org (+ Jason) On Fri, 20 Nov 2020 at 05:11, Eric Biggers wrote: > > On Wed, Nov 11, 2020 at 09:19:37AM +0100, Ard Biesheuvel wrote: > > (+ Eric) > > > > On Thu, 5 Nov 2020 at 16:29, Ard Biesheuvel wrote: > > > > > > When reseeding the CRNG periodically, arch_get_random_seed_long() is > > > called to obtain entropy from an architecture specific source if one > > > is implemented. In most cases, these are special instructions, but in > > > some cases, such as on ARM, we may want to back this using firmware > > > calls, which are considerably more expensive. > > > > > > Another call to arch_get_random_seed_long() exists in the CRNG driver, > > > in add_interrupt_randomness(), which collects entropy by capturing > > > inter-interrupt timing and relying on interrupt jitter to provide > > > random bits. This is done by keeping a per-CPU state, and mixing in > > > the IRQ number, the cycle counter and the return address every time an > > > interrupt is taken, and mixing this per-CPU state into the entropy pool > > > every 64 invocations, or at least once per second. The entropy that is > > > gathered this way is credited as 1 bit of entropy. Every time this > > > happens, arch_get_random_seed_long() is invoked, and the result is > > > mixed in as well, and also credited with 1 bit of entropy. > > > > > > This means that arch_get_random_seed_long() is called at least once > > > per second on every CPU, which seems excessive, and doesn't really > > > scale, especially in a virtualization scenario where CPUs may be > > > oversubscribed: in cases where arch_get_random_seed_long() is backed > > > by an instruction that actually goes back to a shared hardware entropy > > > source (such as RNDRRS on ARM), we will end up hitting it hundreds of > > > times per second. > > > > > > So let's drop the call to arch_get_random_seed_long() from > > > add_interrupt_randomness(), and instead, rely on crng_reseed() to call > > > the arch hook to get random seed material from the platform. > > > > > > Signed-off-by: Ard Biesheuvel > > > --- > > > drivers/char/random.c | 15 +-------------- > > > 1 file changed, 1 insertion(+), 14 deletions(-) > > > > > > diff --git a/drivers/char/random.c b/drivers/char/random.c > > > index 2a41b21623ae..a9c393c1466d 100644 > > > --- a/drivers/char/random.c > > > +++ b/drivers/char/random.c > > > @@ -1261,8 +1261,6 @@ void add_interrupt_randomness(int irq, int irq_flags) > > > cycles_t cycles = random_get_entropy(); > > > __u32 c_high, j_high; > > > __u64 ip; > > > - unsigned long seed; > > > - int credit = 0; > > > > > > if (cycles == 0) > > > cycles = get_reg(fast_pool, regs); > > > @@ -1298,23 +1296,12 @@ void add_interrupt_randomness(int irq, int irq_flags) > > > > > > fast_pool->last = now; > > > __mix_pool_bytes(r, &fast_pool->pool, sizeof(fast_pool->pool)); > > > - > > > - /* > > > - * If we have architectural seed generator, produce a seed and > > > - * add it to the pool. For the sake of paranoia don't let the > > > - * architectural seed generator dominate the input from the > > > - * interrupt noise. > > > - */ > > > - if (arch_get_random_seed_long(&seed)) { > > > - __mix_pool_bytes(r, &seed, sizeof(seed)); > > > - credit = 1; > > > - } > > > spin_unlock(&r->lock); > > > > > > fast_pool->count = 0; > > > > > > /* award one bit for the contents of the fast pool */ > > > - credit_entropy_bits(r, credit + 1); > > > + credit_entropy_bits(r, 1); > > > } > > > EXPORT_SYMBOL_GPL(add_interrupt_randomness); > > Looks reasonable to me. The CRNG state already gets XOR'ed with the output of > arch_get_random_seed_long() each time the CRNG is reseeded. Calling > arch_get_random_seed_long() here too isn't necessary, and it's not really > appropriate to repeatedly call it during interrupt handling, as you point out. > > Reviewed-by: Eric Biggers > > - Eric From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.5 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 2F0D1C64E7B for ; Tue, 1 Dec 2020 12:25:24 +0000 (UTC) Received: from merlin.infradead.org (merlin.infradead.org [205.233.59.134]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 9F8ED20679 for ; Tue, 1 Dec 2020 12:25:23 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=lists.infradead.org header.i=@lists.infradead.org header.b="Ax94D464"; dkim=fail reason="signature verification failed" (1024-bit key) header.d=kernel.org header.i=@kernel.org header.b="oamBLPTG" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 9F8ED20679 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=kernel.org Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=merlin.20170209; h=Sender:Content-Transfer-Encoding: Content-Type:Cc:List-Subscribe:List-Help:List-Post:List-Archive: List-Unsubscribe:List-Id:To:Subject:Message-ID:Date:From:In-Reply-To: References:MIME-Version:Reply-To:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=XdR0au4EXPA05HZEMisMJLI7fb7YT9677oxtoTIT/Tw=; b=Ax94D46456b6LmpRYn6xP2u7V 3pi1R6XWteyL2g77+xL4rU9yR83pDIk4r87cN53wbJs/D5IE+hdc7l4fMCmYz21NZihidHoanWRzq AVYUBBjUwbhym7irNGKwOOfCNCsTlIPlr61zvLlkFStRRKsGGLsIgk2ygLap0FAuHlHgNBgrYdkFK j4v484Z5hIXhPUKgX0fDQZV/PkoziZSyiirHAMy0TPOOvHhwXzbJSHHIgfzdw3s5vH0PZhOtuOh48 NcNdSAPVfmtKdwJW82oIWlQS7JYW/8vfbwc10csT0FT4fIkAuAguUGeiaM8Bb5LTQif47QKhuX6pO HllzpS6hA==; Received: from localhost ([::1] helo=merlin.infradead.org) by merlin.infradead.org with esmtp (Exim 4.92.3 #3 (Red Hat Linux)) id 1kk4ha-0000SU-MK; Tue, 01 Dec 2020 12:24:14 +0000 Received: from mail.kernel.org ([198.145.29.99]) by merlin.infradead.org with esmtps (Exim 4.92.3 #3 (Red Hat Linux)) id 1kk4hX-0000Rx-Et for linux-arm-kernel@lists.infradead.org; Tue, 01 Dec 2020 12:24:12 +0000 Received: from mail-lj1-f171.google.com (mail-lj1-f171.google.com [209.85.208.171]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id EF5F62086A for ; Tue, 1 Dec 2020 12:24:09 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1606825450; bh=bBnrt/qXPq+H01MHvNMWCrKJ7NdoTh2PlKA4hnqr0vw=; h=References:In-Reply-To:From:Date:Subject:To:Cc:From; b=oamBLPTGYBVKgHcGxo4swpudDywxg915icZq6iIxy7vZGrGgXeJxtEL0gNh5xMjbw c4t44STSXttX0GTkGGvXoAl1DisbmFfGlIOY5Qo71JYr0lISDhQGjEZ2Aba5fgcVCU 6JQytp1lZEmg+gZgffbPzKwTE1XlGthk2sAIKE3Q= Received: by mail-lj1-f171.google.com with SMTP id r18so2578392ljc.2 for ; Tue, 01 Dec 2020 04:24:09 -0800 (PST) X-Gm-Message-State: AOAM532rCjGQSOHWQGcJnb25fflZCEsis1n1Q8rFiVprvU2y2NFJg2Fg p8tnhlJb9UcVua4tuBmecVpSXkKljq2YqbEW0Fg= X-Google-Smtp-Source: ABdhPJwFK5+905CbgqECOO1cwvLh6rCSUwEu4cRIahJ2fhT+HsmIJLhDNt1D6iZInyBu8GXHMh1ZstaQibTviZg8st0= X-Received: by 2002:a05:651c:1213:: with SMTP id i19mr1110517lja.407.1606825448068; Tue, 01 Dec 2020 04:24:08 -0800 (PST) MIME-Version: 1.0 References: <20201105152944.16953-1-ardb@kernel.org> In-Reply-To: From: Ard Biesheuvel Date: Tue, 1 Dec 2020 13:23:56 +0100 X-Gmail-Original-Message-ID: Message-ID: Subject: Re: [PATCH] random: avoid arch_get_random_seed_long() when collecting IRQ randomness To: Eric Biggers , "Jason A. Donenfeld" X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20201201_072411_692328_F6CA3661 X-CRM114-Status: GOOD ( 38.56 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Mark Rutland , "Theodore Y. Ts'o" , Marc Zyngier , Linux Kernel Mailing List , Mark Brown , Andre Przywara , Linux ARM Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org (+ Jason) On Fri, 20 Nov 2020 at 05:11, Eric Biggers wrote: > > On Wed, Nov 11, 2020 at 09:19:37AM +0100, Ard Biesheuvel wrote: > > (+ Eric) > > > > On Thu, 5 Nov 2020 at 16:29, Ard Biesheuvel wrote: > > > > > > When reseeding the CRNG periodically, arch_get_random_seed_long() is > > > called to obtain entropy from an architecture specific source if one > > > is implemented. In most cases, these are special instructions, but in > > > some cases, such as on ARM, we may want to back this using firmware > > > calls, which are considerably more expensive. > > > > > > Another call to arch_get_random_seed_long() exists in the CRNG driver, > > > in add_interrupt_randomness(), which collects entropy by capturing > > > inter-interrupt timing and relying on interrupt jitter to provide > > > random bits. This is done by keeping a per-CPU state, and mixing in > > > the IRQ number, the cycle counter and the return address every time an > > > interrupt is taken, and mixing this per-CPU state into the entropy pool > > > every 64 invocations, or at least once per second. The entropy that is > > > gathered this way is credited as 1 bit of entropy. Every time this > > > happens, arch_get_random_seed_long() is invoked, and the result is > > > mixed in as well, and also credited with 1 bit of entropy. > > > > > > This means that arch_get_random_seed_long() is called at least once > > > per second on every CPU, which seems excessive, and doesn't really > > > scale, especially in a virtualization scenario where CPUs may be > > > oversubscribed: in cases where arch_get_random_seed_long() is backed > > > by an instruction that actually goes back to a shared hardware entropy > > > source (such as RNDRRS on ARM), we will end up hitting it hundreds of > > > times per second. > > > > > > So let's drop the call to arch_get_random_seed_long() from > > > add_interrupt_randomness(), and instead, rely on crng_reseed() to call > > > the arch hook to get random seed material from the platform. > > > > > > Signed-off-by: Ard Biesheuvel > > > --- > > > drivers/char/random.c | 15 +-------------- > > > 1 file changed, 1 insertion(+), 14 deletions(-) > > > > > > diff --git a/drivers/char/random.c b/drivers/char/random.c > > > index 2a41b21623ae..a9c393c1466d 100644 > > > --- a/drivers/char/random.c > > > +++ b/drivers/char/random.c > > > @@ -1261,8 +1261,6 @@ void add_interrupt_randomness(int irq, int irq_flags) > > > cycles_t cycles = random_get_entropy(); > > > __u32 c_high, j_high; > > > __u64 ip; > > > - unsigned long seed; > > > - int credit = 0; > > > > > > if (cycles == 0) > > > cycles = get_reg(fast_pool, regs); > > > @@ -1298,23 +1296,12 @@ void add_interrupt_randomness(int irq, int irq_flags) > > > > > > fast_pool->last = now; > > > __mix_pool_bytes(r, &fast_pool->pool, sizeof(fast_pool->pool)); > > > - > > > - /* > > > - * If we have architectural seed generator, produce a seed and > > > - * add it to the pool. For the sake of paranoia don't let the > > > - * architectural seed generator dominate the input from the > > > - * interrupt noise. > > > - */ > > > - if (arch_get_random_seed_long(&seed)) { > > > - __mix_pool_bytes(r, &seed, sizeof(seed)); > > > - credit = 1; > > > - } > > > spin_unlock(&r->lock); > > > > > > fast_pool->count = 0; > > > > > > /* award one bit for the contents of the fast pool */ > > > - credit_entropy_bits(r, credit + 1); > > > + credit_entropy_bits(r, 1); > > > } > > > EXPORT_SYMBOL_GPL(add_interrupt_randomness); > > Looks reasonable to me. The CRNG state already gets XOR'ed with the output of > arch_get_random_seed_long() each time the CRNG is reseeded. Calling > arch_get_random_seed_long() here too isn't necessary, and it's not really > appropriate to repeatedly call it during interrupt handling, as you point out. > > Reviewed-by: Eric Biggers > > - Eric _______________________________________________ linux-arm-kernel mailing list linux-arm-kernel@lists.infradead.org http://lists.infradead.org/mailman/listinfo/linux-arm-kernel