Received: by 2002:ab2:6203:0:b0:1f5:f2ab:c469 with SMTP id o3csp493949lqt; Fri, 19 Apr 2024 01:56:00 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCVOHixqrLT1pdTB0qGDVOMm81dO+v6kR0eriZfW+AaBf3BCEZBD3GojcAE/vZrO5Aroi64Fh0O7JbUiZs0JtUJECO13B/abNAk1nXlesw== X-Google-Smtp-Source: AGHT+IG0iKwA2UIhWu4cnlP9bSsGYHVIdlTPrFU64Bs0OsV2w1O3NgJcPMnyXfwHKAZ3WH/+zRuG X-Received: by 2002:a05:6830:447:b0:6eb:7b6f:150d with SMTP id d7-20020a056830044700b006eb7b6f150dmr1491082otc.8.1713516960010; Fri, 19 Apr 2024 01:56:00 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1713516959; cv=pass; d=google.com; s=arc-20160816; b=wtPfgLPYYTSCRg0SwmxukFdDfVQXV/6FbvEJFWKiaHbad7L5esLQSzprDj6UfB1xSp hTRgMX3IYM3QCVF6YCrs8YIAJw0W4vC9LIyDChBkVgmRZCOh22zWWHrXMgjbg9CJH7d3 tNkY1LvjAeg6UJ7e76lnjxALF//V7NR6cNYjgL/nl+h+yZlDLhRzPZ37Mbg01OXr6kpj kGBm6JTPZ/gAw8H6bdMdqa3L/BhgMa862YU61806IFQf84uxb9pwI74iP+CA6glM6aR/ r7CWwWBLffZfZQ84c7Bo/zY5Zak5TwSNNZPDqQl+qn8tHjyNMVWWpiIYZJbn+xbLtCMa bzMA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=in-reply-to:content-disposition:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:message-id:subject:cc :to:from:date:dkim-signature; bh=E5KzsI1r985YbuWl9u53Q0Y08ML3MWhBVBwKzEgP884=; fh=k9mYeTSFf0HcQrgJuFP7xrG3Pg6aOcvLT1Pu6TDt6Bs=; b=CSUvF3Sww4bxIKbmXl4CwRESziVuprIh+wT/XmSE6Ebm9w0/QsXb4JZtJkT/vrIzR1 rMPgpzSmFz8Qc4754uassijpDkGWyi9sZbgRfZI7hiYLYGctcrz4UfPHGowxKKlvxEwf ZAbbC5ueAtuzs/FCb+o5axVhOiWT2OXwopkyxvlHYYDSbHANm8UUWmEgc9szF5eUchp+ ceK3vj/6xtiyFwbfk4/2N/TMJWsdzLC96PkgJNUOfQqwrcFVRCBngEV51TsMNTiphepe 6S8UswNDhpkrBq244cTxnc2+S1yLSiSKo1wFQosKhy84+z+bn4YkjZSPd6yMxfPrMnSX kJIA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@zx2c4.com header.s=20210105 header.b=FLQ8+lk2; arc=pass (i=1 dkim=pass dkdomain=zx2c4.com); spf=pass (google.com: domain of linux-kernel+bounces-151195-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-151195-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=zx2c4.com Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id q20-20020a63d614000000b005f06995b372si2845476pgg.286.2024.04.19.01.55.59 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 19 Apr 2024 01:55:59 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-151195-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; dkim=pass header.i=@zx2c4.com header.s=20210105 header.b=FLQ8+lk2; arc=pass (i=1 dkim=pass dkdomain=zx2c4.com); spf=pass (google.com: domain of linux-kernel+bounces-151195-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-151195-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=zx2c4.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id 7A9D3281453 for ; Fri, 19 Apr 2024 08:55:59 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 909FE71B3D; Fri, 19 Apr 2024 08:55:54 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=zx2c4.com header.i=@zx2c4.com header.b="FLQ8+lk2" Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 56A9343AA1 for ; Fri, 19 Apr 2024 08:55:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713516953; cv=none; b=nF/AK67PNxpj51nz2loulAxlsUe/bjprAMonYFSNmiTJEReHDd+ldQVh9/EjatY1PpKmDPJ0NZcVY1AzqtgELsraoyJCQ+2wPpW1B7fO+sDhkb7ctNXlJMhEBZMYfvRfpVjSejoRymp2BzKhK5G639OKAXTf039lcigKvrdQIi4= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713516953; c=relaxed/simple; bh=o/S9eg21xMbHDMdlY6+uPWEoHgmg1lCUOWMayHX3g4I=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=Aq1yUbtgiHJDzB9Zm9Z6FaDU3o94zWeKiHvcVrvKhoM8/omO7mTBR/DMz9eCadq0XIm5gJyjwvWurulF9weq/kv8NlRH2GiGt5UAJrlaWMiuL/aDsXK08YVpAsdx8gzrfLfSGqYZK2E6KdfQnklD4W899C6Ql88lmeMvIwvirLc= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=zx2c4.com header.i=@zx2c4.com header.b=FLQ8+lk2; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 6A996C2BD10; Fri, 19 Apr 2024 08:55:51 +0000 (UTC) Authentication-Results: smtp.kernel.org; dkim=pass (1024-bit key) header.d=zx2c4.com header.i=@zx2c4.com header.b="FLQ8+lk2" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=zx2c4.com; s=20210105; t=1713516948; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=E5KzsI1r985YbuWl9u53Q0Y08ML3MWhBVBwKzEgP884=; b=FLQ8+lk2CyNtCqkfxzqO0MlHs9VO/H9k6VbdjGcBtDyvPIXLHKJpbpQdABsVbiH4ohrxAG kPV3/ljiaWF8eUgMjcSRwkvNh+QjUI/NbnkOmvgla1Y8NwECBsi7bB7vfhpzV9+0HFJHEt 7qxiUXyYbi/VxZVfaBnNRNOpJJbWiJA= Received: by mail.zx2c4.com (ZX2C4 Mail Server) with ESMTPSA id c9162869 (TLSv1.3:TLS_AES_256_GCM_SHA384:256:NO); Fri, 19 Apr 2024 08:55:46 +0000 (UTC) Date: Fri, 19 Apr 2024 10:55:44 +0200 From: "Jason A. Donenfeld" To: Guoyong Wang Cc: Theodore Ts'o , Tejun Heo , Lai Jiangshan , Matthias Brugger , AngeloGioacchino Del Regno , linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-mediatek@lists.infradead.org, wsd_upstream@mediatek.com Subject: Re: [PATCH] random: Fix the issue of '_might_sleep' function running in an atomic contex Message-ID: References: <20240417120217.3814215-2-Jason@zx2c4.com> <20240419084112.4089-1-guoyong.wang@mediatek.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: <20240419084112.4089-1-guoyong.wang@mediatek.com> On Fri, Apr 19, 2024 at 04:41:12PM +0800, Guoyong Wang wrote: > On Wed, 17 Apr 2024 14:01:11 +0200, Jason A. Donenfeld wrote: > > The entropy accounting changes a static key when the RNG has > > initialized, since it only ever initializes once. Static key changes, > > however, cannot be made from atomic context, so depending on where the > > last creditable entropy comes from, the static key change might need to > > be deferred to a worker. > > > > Previously the code used the execute_in_process_context() helper > > function, which accounts for whether or not the caller is > > in_interrupt(). However, that doesn't account for the case where the > > caller is actually in process context but is holding a spinlock. > > > > This turned out to be the case with input_handle_event() in > > drivers/input/input.c contributing entropy: > > > > [] die+0xa8/0x2fc > > [] bug_handler+0x44/0xec > > [] brk_handler+0x90/0x144 > > [] do_debug_exception+0xa0/0x148 > > [] el1_dbg+0x60/0x7c > > [] el1h_64_sync_handler+0x38/0x90 > > [] el1h_64_sync+0x64/0x6c > > [] __might_resched+0x1fc/0x2e8 > > [] __might_sleep+0x44/0x7c > > [] cpus_read_lock+0x1c/0xec > > [] static_key_enable+0x14/0x38 > > [] crng_set_ready+0x14/0x28 > > [] execute_in_process_context+0xb8/0xf8 > > [] _credit_init_bits+0x118/0x1dc > > [] add_timer_randomness+0x264/0x270 > > [] add_input_randomness+0x38/0x48 > > [] input_handle_event+0x2b8/0x490 > > [] input_event+0x6c/0x98 > > > > According to Guoyong, it's not really possible to refactor the various > > drivers to never hold a spinlock there. And in_atomic() isn't reliable. > > > > So, rather than trying to be too fancy, just punt the change in the > > static key to a workqueue always. There's basically no drawback of doing > > this, as the code already needed to account for the static key not > > changing immediately, and given that it's just an optimization, there's > > not exactly a hurry to change the static key right away, so deferal is > > fine. > > > > Reported-by: Guoyong Wang > > Cc: stable@vger.kernel.org > > Fixes: f5bda35fba61 ("random: use static branch for crng_ready()") > > Signed-off-by: Jason A. Donenfeld > > --- > > Guoyong- can you test this and tell me whether it fixes the problem you > > were seeing? If so, I'll try to get this sent up for 6.9. -Jason > > > > drivers/char/random.c | 10 +++++----- > > 1 file changed, 5 insertions(+), 5 deletions(-) > > > > diff --git a/drivers/char/random.c b/drivers/char/random.c > > index 456be28ba67c..2597cb43f438 100644 > > --- a/drivers/char/random.c > > +++ b/drivers/char/random.c > > @@ -702,7 +702,7 @@ static void extract_entropy(void *buf, size_t len) > > > > static void __cold _credit_init_bits(size_t bits) > > { > > - static struct execute_work set_ready; > > + static DECLARE_WORK(set_ready, crng_set_ready); > > unsigned int new, orig, add; > > unsigned long flags; > > > > @@ -718,8 +718,8 @@ static void __cold _credit_init_bits(size_t bits) > > > > if (orig < POOL_READY_BITS && new >= POOL_READY_BITS) { > > crng_reseed(NULL); /* Sets crng_init to CRNG_READY under base_crng.lock. */ > > - if (static_key_initialized) > > - execute_in_process_context(crng_set_ready, &set_ready); > > + if (static_key_initialized && system_unbound_wq) > > + queue_work(system_unbound_wq, &set_ready); > > atomic_notifier_call_chain(&random_ready_notifier, 0, NULL); > > wake_up_interruptible(&crng_init_wait); > > kill_fasync(&fasync, SIGIO, POLL_IN); > > @@ -890,8 +890,8 @@ void __init random_init(void) > > > > /* > > * If we were initialized by the cpu or bootloader before jump labels > > - * are initialized, then we should enable the static branch here, where > > - * it's guaranteed that jump labels have been initialized. > > + * or workqueues are initialized, then we should enable the static > > + * branch here, where it's guaranteed that these have been initialized. > > */ > > if (!static_branch_likely(&crng_is_ready) && crng_init >= CRNG_READY) > > crng_set_ready(NULL); > > -- > > 2.44.0 > > Hi Jason, > > Thanks for your feedback. We concur with the proposed change and have verified that it works well > in our tests. Next, I will provide a patch v2 for the changes discussed. No need. This is already upstream and has your Reported-by. It'll get backported to stable too. https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=e871abcda3b67d0820b4182ebe93435624e9c6a4