Received: by 2002:a25:31c3:0:0:0:0:0 with SMTP id x186csp5049498ybx; Sun, 10 Nov 2019 05:58:37 -0800 (PST) X-Google-Smtp-Source: APXvYqzD4l3TmLDn5R3DusxzQ9gNs215Zjg8+BeqsaXw0SNl0ovyjZDk8wwUuNCf8RNqOO01ylVq X-Received: by 2002:a17:906:c314:: with SMTP id s20mr18119641ejz.118.1573394317667; Sun, 10 Nov 2019 05:58:37 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1573394317; cv=none; d=google.com; s=arc-20160816; b=mrDE0dAmAxr1fVqLDv91QVSoI4lIQo1LVoAxqPIhRsmMBFaFHscrnN95xHvrxqWYXf B/DHIaFemkVS/4u+OXs/InscZoKykvFpfnQIlolLw2nl4C6MdPCIBOjtfMhHpFroZke4 h0J/HXXIYFnlO4MiQ7brw88L+2nGoIMTZBhwevllBwkVbhgvEjh+aRrDbHu+B/VfF5hA nTz2CoWGkiVjx2U3G50m6s9b9U9TkCfGo7DqkAaOoJ8HYrFohKrLsmrZn/pEy5aAC91+ HyM1yoAkd9SRourKa7dr26FrP5DRIl6UzdHykNq7wgAbdmpI6Om2MhwfFsU1DltzRKPk rllw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from; bh=5LAharuWmEd+xQX3ZM6Ixng63nVNTO9Vmm9UTCs1xNc=; b=GKuyeuCdjDyteXOPIVYg7WfQJQcVRvRpg8UVKTsC4eo/pkv8CRLyZWZmF/E39JT6Yb vQ2wEgnqONoQmMZVQhmIzSSmsOIKgJArz57w7iSHUDbUcDgbRKRhFBQWMgKh8Lt1RAcK xNVRAgombo01oJsjNK2X1Yg5kTd3mmks2PjRpFtaoRuhAPsIAVZku9ikFneDiFICGDrz 44NNVGydWX52XMn1isXT6ou6quhc/ZdcUpxCuloB3eZlvButd45AXqITBRg+M7p70BoU N0RPDUp5amxYVroiBple8E8QCcwzUC3kw56EFjqW1zxR/BY6rGSzrSeRjC40sg0BzKyA x5xA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-crypto-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-crypto-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 ce3si9674349edb.441.2019.11.10.05.57.57; Sun, 10 Nov 2019 05:58:37 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-crypto-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-crypto-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-crypto-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726402AbfKJN4D (ORCPT + 99 others); Sun, 10 Nov 2019 08:56:03 -0500 Received: from vps-vb.mhejs.net ([37.28.154.113]:51690 "EHLO vps-vb.mhejs.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726390AbfKJN4D (ORCPT ); Sun, 10 Nov 2019 08:56:03 -0500 Received: from MUA by vps-vb.mhejs.net with esmtps (TLSv1.2:ECDHE-RSA-AES256-GCM-SHA384:256) (Exim 4.92.3) (envelope-from ) id 1iTngz-0000Y5-23; Sun, 10 Nov 2019 14:55:49 +0100 From: "Maciej S. Szmigiero" To: "Theodore Ts'o" Cc: Herbert Xu , Arnd Bergmann , Greg Kroah-Hartman , Sebastian Andrzej Siewior , Keerthy , Stephen Boyd , linux-crypto@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH] random: Don't freeze in add_hwgenerator_randomness() if stopping kthread Date: Sun, 10 Nov 2019 14:55:42 +0100 Message-Id: <20191110135543.3476097-1-mail@maciej.szmigiero.name> X-Mailer: git-send-email 2.23.0 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-crypto-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org Since commit 59b569480dc8 ("random: Use wait_event_freezable() in add_hwgenerator_randomness()") there is a race in add_hwgenerator_randomness() between freezing and stopping the calling kthread. This commit changed wait_event_interruptible() call with kthread_freezable_should_stop() as a condition into wait_event_freezable() with just kthread_should_stop() as a condition to fix a warning that kthread_freezable_should_stop() might sleep inside the wait. wait_event_freezable() ultimately calls __refrigerator() with its check_kthr_stop argument set to false, which causes it to keep the kthread frozen even if somebody calls kthread_stop() on it. Calling wait_event_freezable() with kthread_should_stop() as a condition is racy because it doesn't take into account the situation where this condition becomes true on a kthread marked for freezing only after this condition has already been checked. Calling freezing() should avoid the issue that the commit 59b569480dc8 has fixed, as it is only a checking function, it doesn't actually do the freezing. add_hwgenerator_randomness() has two post-boot users: in khwrng the kthread will be frozen anyway by call to kthread_freezable_should_stop() in its main loop, while its second user (ath9k-hwrng) is not freezable at all. This change allows a VM with virtio-rng loaded to write s2disk image successfully. Fixes: 59b569480dc8 ("random: Use wait_event_freezable() in add_hwgenerator_randomness()") Signed-off-by: Maciej S. Szmigiero --- drivers/char/random.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/drivers/char/random.c b/drivers/char/random.c index de434feb873a..2f87910dd498 100644 --- a/drivers/char/random.c +++ b/drivers/char/random.c @@ -2500,8 +2500,8 @@ void add_hwgenerator_randomness(const char *buffer, size_t count, * We'll be woken up again once below random_write_wakeup_thresh, * or when the calling thread is about to terminate. */ - wait_event_freezable(random_write_wait, - kthread_should_stop() || + wait_event_interruptible(random_write_wait, + kthread_should_stop() || freezing(current) || ENTROPY_BITS(&input_pool) <= random_write_wakeup_bits); mix_pool_bytes(poolp, buffer, count); credit_entropy_bits(poolp, entropy);