Received: by 2002:a6b:fb09:0:0:0:0:0 with SMTP id h9csp6048320iog; Thu, 23 Jun 2022 10:14:17 -0700 (PDT) X-Google-Smtp-Source: AGRyM1soo6K4AbGwb69B8mmOLrWeJBhUC6ZHSWnaTDwrgDC5G2zCFNVO83fD+cN7sxjn0HGaepid X-Received: by 2002:a17:906:3c07:b0:718:e1a7:b834 with SMTP id h7-20020a1709063c0700b00718e1a7b834mr9112692ejg.635.1656004456972; Thu, 23 Jun 2022 10:14:16 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1656004456; cv=none; d=google.com; s=arc-20160816; b=L6cFiLtTGqWKO7GOaym4GlAZ/UrFEC7mpInkayRHWePGUD3CJ4Hg0LVAA5Oc08KSIY DHg4NcKZ1Izn1O9egDkbXVPTfgy4ojHV40MY1f2v9w9ZExjCMjPaLdnFS9DJeaHW4/T/ BRyjXt5CMX/IsKnNr4EsFzIHAs3MEFOK00RHmAdqlMjZPwiiYt1oHHT7eQWuNMf0rL3f KNKSxbonKkPHfcHDOtwyAFouqoT7ZmpjKq3OPsR18oKQVKCODY/kE06V1cKoL4fE8+dQ LHV+uGknrqgplA30Tpac+I0sq/D5Q4bA/iCcSaixLabeA4iCp4njcE6rFAYzRffpFo4Q PpVw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=fwd+KGvz4MnrbuPN9EnHs0TeteUl7zzpx2Fmkkdbxv4=; b=a7b4jfn7u9gplAPLXQA5bUvoP8/Qdp5WIEHdZhrfADI9NhmBGJyqRJDu2JsMSTjTDq y6+bHj5X/tUuMCI/ZIYfww+Hm2rs68OHsap02dCjA4i1N/F3EB5SGhf53enviJwfyD7i M7oFP1bYNzUvgUlkFCUUypxIFG9wLJ8NP9rXjnl9eQ8L9UfuTao24cqMKxpV752cxoGG gAHtjGXwsNjNOjlubTdClskMwHDt1CjbNkB1BJRL3oaobfkN6PSmRfgdQy3eR5Pln9pK V4osuWx9eA73BtYA/dHZZ6d9Dkc24BInfrthtJ/uVadqtupWQrvJQHOMuDGhtL4Mu1qT IiWA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=1NrRNgKQ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id bm6-20020a0564020b0600b0043561e0c9aesi102787edb.425.2022.06.23.10.13.51; Thu, 23 Jun 2022 10:14:16 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=1NrRNgKQ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230265AbiFWRBp (ORCPT + 99 others); Thu, 23 Jun 2022 13:01:45 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59778 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233121AbiFWQ5c (ORCPT ); Thu, 23 Jun 2022 12:57:32 -0400 Received: from sin.source.kernel.org (sin.source.kernel.org [IPv6:2604:1380:40e1:4800::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A95BA4D260; Thu, 23 Jun 2022 09:53:18 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sin.source.kernel.org (Postfix) with ESMTPS id A4A17CE25E6; Thu, 23 Jun 2022 16:53:11 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 76CF7C3411B; Thu, 23 Jun 2022 16:53:09 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1656003189; bh=1WHGREk6VpvGi11di3QakbO+TNb1GMaPkag3KyNvD1A=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=1NrRNgKQsIFeLXihNVtWHIuCSAQQK1Z+jwmF82RJZ9UiOpY/MvKsN7WbMDrqnVCQg 5MM9AShyl2mq+dIkH+sQO0v5galUspTFwf2HH3rfdN0/WyU1jOdHw9xroRQ3P5Fz6N v1rqupBS9WrCztzZiPFHIa/AjTKElWXwDXQuCJ60= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Sultan Alsawaf , Thomas Gleixner , Peter Zijlstra , Eric Biggers , Theodore Tso , Sebastian Andrzej Siewior , Dominik Brodowski , "Jason A. Donenfeld" Subject: [PATCH 4.9 158/264] random: do crng pre-init loading in worker rather than irq Date: Thu, 23 Jun 2022 18:42:31 +0200 Message-Id: <20220623164348.532986624@linuxfoundation.org> X-Mailer: git-send-email 2.36.1 In-Reply-To: <20220623164344.053938039@linuxfoundation.org> References: <20220623164344.053938039@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-7.7 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: "Jason A. Donenfeld" commit c2a7de4feb6e09f23af7accc0f882a8fa92e7ae5 upstream. Taking spinlocks from IRQ context is generally problematic for PREEMPT_RT. That is, in part, why we take trylocks instead. However, a spin_try_lock() is also problematic since another spin_lock() invocation can potentially PI-boost the wrong task, as the spin_try_lock() is invoked from an IRQ-context, so the task on CPU (random task or idle) is not the actual owner. Additionally, by deferring the crng pre-init loading to the worker, we can use the cryptographic hash function rather than xor, which is perhaps a meaningful difference when considering this data has only been through the relatively weak fast_mix() function. The biggest downside of this approach is that the pre-init loading is now deferred until later, which means things that need random numbers after interrupts are enabled, but before workqueues are running -- or before this particular worker manages to run -- are going to get into trouble. Hopefully in the real world, this window is rather small, especially since this code won't run until 64 interrupts had occurred. Cc: Sultan Alsawaf Cc: Thomas Gleixner Cc: Peter Zijlstra Cc: Eric Biggers Cc: Theodore Ts'o Acked-by: Sebastian Andrzej Siewior Reviewed-by: Dominik Brodowski Signed-off-by: Jason A. Donenfeld Signed-off-by: Greg Kroah-Hartman --- drivers/char/random.c | 65 ++++++++++++++------------------------------------ 1 file changed, 19 insertions(+), 46 deletions(-) --- a/drivers/char/random.c +++ b/drivers/char/random.c @@ -442,10 +442,6 @@ static void crng_make_state(u32 chacha_s * boot time when it's better to have something there rather than * nothing. * - * There are two paths, a slow one and a fast one. The slow one - * hashes the input along with the current key. The fast one simply - * xors it in, and should only be used from interrupt context. - * * If account is set, then the crng_init_cnt counter is incremented. * This shouldn't be set by functions like add_device_randomness(), * where we can't trust the buffer passed to it is guaranteed to be @@ -454,19 +450,15 @@ static void crng_make_state(u32 chacha_s * Returns the number of bytes processed from input, which is bounded * by CRNG_INIT_CNT_THRESH if account is true. */ -static size_t crng_pre_init_inject(const void *input, size_t len, - bool fast, bool account) +static size_t crng_pre_init_inject(const void *input, size_t len, bool account) { static int crng_init_cnt = 0; + struct blake2s_state hash; unsigned long flags; - if (fast) { - if (!spin_trylock_irqsave(&base_crng.lock, flags)) - return 0; - } else { - spin_lock_irqsave(&base_crng.lock, flags); - } + blake2s_init(&hash, sizeof(base_crng.key)); + spin_lock_irqsave(&base_crng.lock, flags); if (crng_init != 0) { spin_unlock_irqrestore(&base_crng.lock, flags); return 0; @@ -475,21 +467,9 @@ static size_t crng_pre_init_inject(const if (account) len = min_t(size_t, len, CRNG_INIT_CNT_THRESH - crng_init_cnt); - if (fast) { - const u8 *src = input; - size_t i; - - for (i = 0; i < len; ++i) - base_crng.key[(crng_init_cnt + i) % - sizeof(base_crng.key)] ^= src[i]; - } else { - struct blake2s_state hash; - - blake2s_init(&hash, sizeof(base_crng.key)); - blake2s_update(&hash, base_crng.key, sizeof(base_crng.key)); - blake2s_update(&hash, input, len); - blake2s_final(&hash, base_crng.key); - } + blake2s_update(&hash, base_crng.key, sizeof(base_crng.key)); + blake2s_update(&hash, input, len); + blake2s_final(&hash, base_crng.key); if (account) { crng_init_cnt += len; @@ -1030,7 +1010,7 @@ void add_device_randomness(const void *b unsigned long flags, now = jiffies; if (crng_init == 0 && size) - crng_pre_init_inject(buf, size, false, false); + crng_pre_init_inject(buf, size, false); spin_lock_irqsave(&input_pool.lock, flags); _mix_pool_bytes(&cycles, sizeof(cycles)); @@ -1151,7 +1131,7 @@ void add_hwgenerator_randomness(const vo size_t entropy) { if (unlikely(crng_init == 0)) { - size_t ret = crng_pre_init_inject(buffer, count, false, true); + size_t ret = crng_pre_init_inject(buffer, count, true); mix_pool_bytes(buffer, ret); count -= ret; buffer += ret; @@ -1291,8 +1271,14 @@ static void mix_interrupt_randomness(str fast_pool->last = jiffies; local_irq_enable(); - mix_pool_bytes(pool, sizeof(pool)); - credit_entropy_bits(1); + if (unlikely(crng_init == 0)) { + crng_pre_init_inject(pool, sizeof(pool), true); + mix_pool_bytes(pool, sizeof(pool)); + } else { + mix_pool_bytes(pool, sizeof(pool)); + credit_entropy_bits(1); + } + memzero_explicit(pool, sizeof(pool)); } @@ -1325,24 +1311,11 @@ void add_interrupt_randomness(int irq) fast_mix(fast_pool->pool32); new_count = ++fast_pool->count; - if (unlikely(crng_init == 0)) { - if (new_count >= 64 && - crng_pre_init_inject(fast_pool->pool32, sizeof(fast_pool->pool32), - true, true) > 0) { - fast_pool->count = 0; - fast_pool->last = now; - if (spin_trylock(&input_pool.lock)) { - _mix_pool_bytes(&fast_pool->pool32, sizeof(fast_pool->pool32)); - spin_unlock(&input_pool.lock); - } - } - return; - } - if (new_count & MIX_INFLIGHT) return; - if (new_count < 64 && !time_after(now, fast_pool->last + HZ)) + if (new_count < 64 && (!time_after(now, fast_pool->last + HZ) || + unlikely(crng_init == 0))) return; if (unlikely(!fast_pool->mix.func))