Received: by 2002:a25:7ec1:0:0:0:0:0 with SMTP id z184csp1097630ybc; Sat, 16 Nov 2019 15:02:21 -0800 (PST) X-Google-Smtp-Source: APXvYqz2bWLFOuL4q8d9jDj4KCwNsO0kuRURVxZZpBb/XpNH7Trm2UUf+c5CbEiPsS81payGX+4A X-Received: by 2002:a17:906:4e99:: with SMTP id v25mr13742207eju.106.1573945341857; Sat, 16 Nov 2019 15:02:21 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1573945341; cv=none; d=google.com; s=arc-20160816; b=kAf7/Qm3pJIFguLAxjqjCLeJrTuvcNQpid3yr+T0OVZhLGEhVKHLIFrDzAkxnPrdyP oXnxExvRcZ4cFotK95exorGZYBUEbU07Ytq0AvlxFFt3javOVY4eE7TaplalYUuCKWHc PYtEi+dsrMf/HCQEfpCHMvpFQVtsuDsCR5ADtRrqa8jVpUr81VAOVmn6127w/Rh7Nsee ysnfJM8ZZHxQ+LlGdd5Q77O91I681U/SgnU+iUR2eU9Dl8r/ikapD1gQxu+A1dIDY73i jsQLRl4+MHgajDxpk5EGwWLJRAvtCiH7OWwISxgcaf8DWI5OWBvz3K4/6t4PqOe6qZ/6 l23Q== 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 :content-language:in-reply-to:mime-version:user-agent:date :message-id:autocrypt:from:references:cc:to:subject; bh=Ae8Bgvw5m9MfrbWkuy3BPdbJIz8jJwa0SfQ/Prm3MU0=; b=IET5SIqi1zeDO1UqnXe2p6xWsRWMTo84P9a89oWpc+HSflvoF0JG9y7kb/xpa+mhKO yL9dQmp/coc2AtHM9SIAB8Shpx9oBlBqECUb+ij2qGCGc7A71yWs5413fZ3UEj8JbFQN lRQ5HdC8BSSIh6dDELISesj7jG0ad8HDe+3l45pGvLrMRQLxO275iKT27INhWwYxoD6U 5C38fvC35b2thP6CXOQoasZwMfULry31y8766jPoPd2mYTDqfOmbo6jnvId4weCMJ71j A95yhhuELFongvck9OmTfKqfkppEP8Vgv/t4qVCeMHgcL8Lu0C90ffEOKKuGagSr3SNc D4hA== 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 j21si8375978ejd.265.2019.11.16.15.01.37; Sat, 16 Nov 2019 15:02:21 -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 S1727629AbfKPXBd (ORCPT + 99 others); Sat, 16 Nov 2019 18:01:33 -0500 Received: from vps-vb.mhejs.net ([37.28.154.113]:33162 "EHLO vps-vb.mhejs.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727273AbfKPXBc (ORCPT ); Sat, 16 Nov 2019 18:01:32 -0500 Received: from MUA by vps-vb.mhejs.net with esmtps (TLSv1.2:ECDHE-RSA-AES128-GCM-SHA256:128) (Exim 4.92.3) (envelope-from ) id 1iW74I-00075J-CS; Sun, 17 Nov 2019 00:01:26 +0100 Subject: Re: [PATCH] random: Don't freeze in add_hwgenerator_randomness() if stopping kthread To: Stephen Boyd Cc: Theodore Ts'o , Herbert Xu , Arnd Bergmann , Greg Kroah-Hartman , Sebastian Andrzej Siewior , Keerthy , linux-crypto@vger.kernel.org, linux-kernel@vger.kernel.org References: <20191110135543.3476097-1-mail@maciej.szmigiero.name> <5dcee409.1c69fb81.f5027.48ad@mx.google.com> From: "Maciej S. Szmigiero" Autocrypt: addr=mail@maciej.szmigiero.name; prefer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IVWvaQG9whxZq5MdJm5dknfTcCG+MtmbP/DnpQpjAlgVmDgMgYTBW1W9etU 36YW0pTqEYuv6cmRgSAKEDaYHhFLTR1+lLJkp5fFo3Sjm7XqmXzfSv9JGJGMKzoFOMBoLYv+ VFnMoLX5UJAs0JyFqFY2YxGyLd4J103NI/ocqQeU0TVvOZGVkENPSxIESnbxPghsEC0MWEbG svqA8FwvU7XfGhZPYzTRf7CndDnezEA69EhwpZXKs4CvxbXo5PDTv0OWzVaAWqq8s8aTMJWW AhvobFozJ63zafYHkuEjMo0Xps3o3uvKg7coooH521nNsv4ci+KeBq3mgMCRAy0g/Ef+Ql7m t900RCBHu4tktOhPc3J1ep/e2WAJ4ngUqJhilzyCJnzVJ4cT79VK/uPtlfUCZdUz+jTC88Tm P1p5wlucS31kThy/CV4cqDFB8yzEujTSiRzd7neG3sH0vcxBd69uvSxLZPLGID840k0v5sft PA== Message-ID: <415922ac-3c87-081c-6fdf-73fc97d0f397@maciej.szmigiero.name> Date: Sun, 17 Nov 2019 00:01:20 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.2.2 MIME-Version: 1.0 In-Reply-To: <5dcee409.1c69fb81.f5027.48ad@mx.google.com> Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 8bit Sender: linux-crypto-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org On 15.11.2019 18:44, Stephen Boyd wrote: > Quoting Maciej S. Szmigiero (2019-11-10 05:55:42) >> 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); > > Is it a problem that this wakes up, sees that it should freeze but then > calls mix_pool_bytes() and credit_entropy_bits()? It looks like > credit_entropy_bits() will try to wakeup a reader task that is already > frozen (see the wake_up_interruptible(&random_read_wait) call). If a reader (user space) task is frozen then it is no longer waiting on this waitqueue - at least if I understand correctly how the freezer works for user space tasks, that is by interrupting waits via a fake signal. > At one> point I was checking to see if the task was freezing and avoided calling > those functions so we could get back to kthread_freezable_should_stop() > in the kthread and actually freeze. Yes, but I think mixing some extra valid data into random buffer in this very rare situation shouldn't hurt. >> mix_pool_bytes(poolp, buffer, count); >> credit_entropy_bits(poolp, entropy); > > It's almost like we need a wait_event_freezable_stoppable() API that > will freeze if freezing() and break out if the kthread is stopped and > otherwise wait for a wakeup to test the condition. Basically the same > sort of API that we have for wait_event_freezable() but we pass true for > the check_kthr_stop flag. Then we can have something like this: > > wait_event_freezable_stoppable( > ENTROPY_BITS(&input_pool) <= random_write_wakeup_bits); > if (!kthread_should_stop()) { > mix_pool_bytes(...); > credit_entropy_bits(...); > } > This API could be added but will there be other users for it? I mean I think it might not be worth adding a new core kernel API for such a specific, and workaroundable, issue. But, on the other hand, maybe this would be a cleaner solution, even though it would be more complicated. By the way, the same goes for something like set_freezable_only() function for the second khwrng freezing issue that I have mentioned in my Nov 10 UTC message. Maciej