Received: by 2002:ab2:710b:0:b0:1ef:a325:1205 with SMTP id z11csp1016077lql; Tue, 12 Mar 2024 05:15:59 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCXeBWSfHKKJ+8SBNGIiIb/2/IGB3Sz7qYSjf1pH+HZi6X+Yxd/xg27IxaOq7KWF7WYc5TKB/uJVreqV/v/AOzaDH6D4dVqHeOicglg0vg== X-Google-Smtp-Source: AGHT+IFcFQW+nOWbrRekl7e/1kpP73f1v19hmcEFxmVVEBYQzw7bD0XKyi0Fxth3bR68jUNDWhVy X-Received: by 2002:a50:8dc8:0:b0:567:6c79:c6ef with SMTP id s8-20020a508dc8000000b005676c79c6efmr1632525edh.29.1710245759112; Tue, 12 Mar 2024 05:15:59 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1710245758; cv=pass; d=google.com; s=arc-20160816; b=F2JM5ZYryW3xcjEH5iAtGVlHV5T2ejxca3ccgGMhTKliuS0OODKaPevwyM+WSCmj6E SQGngeFtW1Kvsc1CyxS2TpxMq122DQNWo1B8Zn0VFdysnl//asGnSh+Ggo4zTkIdDVKe tSzNQQRvT7F8+pDMjgeu0uJtU8TLXtLCHvH68luHRvuK/4Ftdq7JASqAlWrMBIy7OsJm Y3iFNYdX7HNGXvz4IFR8Ojq8P4oo9mbXJKWteASjuHYFHsFQYFKZsOFmZMAH0RbGGzKB stutiKZhDWCHNVAMTkBvBE0VlcHAw5c4g13rdYqLL2ZX7xh846bOZvKTgqBIAjnzg0+g TO4A== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=in-reply-to:content-transfer-encoding:content-disposition :mime-version:list-unsubscribe:list-subscribe:list-id:precedence :references:message-id:subject:cc:to:from:date:dkim-signature; bh=/yovNcwLZ0pshdV5h7Kvjwx/WNWs8xQrfi7iTnPZS4Q=; fh=GcunwHSvmwzL08I/aB6owuc33HGVKaig4YUjQUZ0T8c=; b=yaBYmqOQA04UX9xmWoty7TwDhg1Gp7i5aosr/8HUmRdHMB33YEuJT2CFRkICBc6rYo AwPe68R0eCrGZxHG6JTwdOuf3EIONRKBrsy+sqeKoU04jmn/a0JoQ9RL/tkk3fJyKAcF wOl28KwFttxwdvWcI1k0RZLksF5m5cLqP49dpFd/W90jxXi/3H2RDiw2CZVoYLlySu9U U9Xa1HUJ8SDoaPwRepkG5av5NrZUiMZsN40UkS0Y8eNAomBu0NT5AHDnKs3XC/nT4HP2 njSUrx4NkxoTFiYQAC9qhjOnA+hM8qRFexbo8p1nzKtztl+q1mwq8i4PXkXwTJLxmVNm 8loQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@digikod.net header.s=20191114 header.b=AAQtm6hv; arc=pass (i=1 spf=pass spfdomain=digikod.net dkim=pass dkdomain=digikod.net); spf=pass (google.com: domain of linux-kernel+bounces-100244-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-100244-linux.lists.archive=gmail.com@vger.kernel.org" Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id n4-20020a056402434400b005685d8423a0si1905650edc.164.2024.03.12.05.15.58 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 12 Mar 2024 05:15:58 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-100244-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) client-ip=147.75.80.249; Authentication-Results: mx.google.com; dkim=pass header.i=@digikod.net header.s=20191114 header.b=AAQtm6hv; arc=pass (i=1 spf=pass spfdomain=digikod.net dkim=pass dkdomain=digikod.net); spf=pass (google.com: domain of linux-kernel+bounces-100244-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-100244-linux.lists.archive=gmail.com@vger.kernel.org" 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 am.mirrors.kernel.org (Postfix) with ESMTPS id 7FFB21F23B74 for ; Tue, 12 Mar 2024 12:15:58 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id CFD137A143; Tue, 12 Mar 2024 12:15:26 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=digikod.net header.i=@digikod.net header.b="AAQtm6hv" Received: from smtp-bc0b.mail.infomaniak.ch (smtp-bc0b.mail.infomaniak.ch [45.157.188.11]) (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 D253A79B9D for ; Tue, 12 Mar 2024 12:15:21 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=45.157.188.11 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710245725; cv=none; b=rRamf/fmW1ClDW5NFyHQm88R2JCiQWy4lQx0PySnBJ0NkJV2zI0oHA/jR/riS8G9tldhwrIrfinxaeOO/4SBUH4WPc+1RSsXJT4CDJXCwb+biO797TZMdDBa5lAEqmYNGw11tZ1lBuFxAm3i2U4WN4Azg0XFuQCQPIM3ezN4rzA= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710245725; c=relaxed/simple; bh=UIPlBlQ2gSp7viPBKsB16/eqMI1pgjsYrWQKabWN1/s=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=sRjqCQMrlgpAgX2AoIfHwZWttP+SJm/AtEgO4ousLBtY86Ulq+I/k/f023AfFBh14Ww2i2JcobxcMSyTKa5oqjdETPEqnfY5tq9b1PL4EM78bZVcHreLrV/nlUFYGYUBwgmxsI6iHCpM1VK2rc0pM9ZJFGoSEuwvm+jN1Bk2x74= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=digikod.net; spf=pass smtp.mailfrom=digikod.net; dkim=pass (1024-bit key) header.d=digikod.net header.i=@digikod.net header.b=AAQtm6hv; arc=none smtp.client-ip=45.157.188.11 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=digikod.net Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=digikod.net Received: from smtp-3-0000.mail.infomaniak.ch (smtp-3-0000.mail.infomaniak.ch [10.4.36.107]) by smtp-4-3000.mail.infomaniak.ch (Postfix) with ESMTPS id 4TvCJf11Fxz3my; Tue, 12 Mar 2024 13:15:14 +0100 (CET) Received: from unknown by smtp-3-0000.mail.infomaniak.ch (Postfix) with ESMTPA id 4TvCJc1dKzzsW; Tue, 12 Mar 2024 13:15:12 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=digikod.net; s=20191114; t=1710245714; bh=UIPlBlQ2gSp7viPBKsB16/eqMI1pgjsYrWQKabWN1/s=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=AAQtm6hvd8D/0qDhpfGiSpJYAJ7c4ZRRdmzxuwHAMDZ5D0rNCT9rpMwm0n6kBYZLn MIieECao+whDew4238DUdSRRjeO6cebzxSywIl7f+OXZViuaJ/cEfbfTEn2VgfYZI/ vd+XiTlMZIXWiNScOcBSP9ExwIwlT9QI5LQs6vEU= Date: Tue, 12 Mar 2024 13:15:01 +0100 From: =?utf-8?Q?Micka=C3=ABl_Sala=C3=BCn?= To: Rae Moar Cc: Brendan Higgins , David Gow , Kees Cook , Shuah Khan , Alan Maguire , Borislav Petkov , Dave Hansen , "H . Peter Anvin" , Ingo Molnar , James Morris , Luis Chamberlain , "Madhavan T . Venkataraman" , Marco Pagani , Paolo Bonzini , Sean Christopherson , Stephen Boyd , Thara Gopinath , Thomas Gleixner , Vitaly Kuznetsov , Wanpeng Li , Zahra Tarkhani , kvm@vger.kernel.org, linux-hardening@vger.kernel.org, linux-hyperv@vger.kernel.org, linux-kernel@vger.kernel.org, linux-security-module@vger.kernel.org, linux-um@lists.infradead.org, x86@kernel.org Subject: Re: [PATCH v2 4/7] kunit: Handle test faults Message-ID: <20240312.iuVoThuud2oi@digikod.net> References: <20240301194037.532117-1-mic@digikod.net> <20240301194037.532117-5-mic@digikod.net> 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 Content-Transfer-Encoding: 8bit In-Reply-To: X-Infomaniak-Routing: alpha On Mon, Mar 11, 2024 at 05:21:11PM -0400, Rae Moar wrote: > On Fri, Mar 1, 2024 at 2:40 PM Mickaël Salaün wrote: > > > > Previously, when a kernel test thread crashed (e.g. NULL pointer > > dereference, general protection fault), the KUnit test hanged for 30 > > seconds and exited with a timeout error. > > > > Fix this issue by waiting on task_struct->vfork_done instead of the > > custom kunit_try_catch.try_completion, and track the execution state by > > initially setting try_result with -EFAULT and only setting it to 0 if > > Hello! > > Thanks for your patch! This has been tested and seems pretty good to > me but I just have a few questions. First, do you mean here "setting > try_result with -EINTR" instead? Good catch, I indeed meant -EINTR. > > But happy to add the tested-by. > > Tested-by: Rae Moar > > Thanks! > -Rae > > > the test passed. > > > > Fix kunit_generic_run_threadfn_adapter() signature by returning 0 > > instead of calling kthread_complete_and_exit(). Because thread's exit > > code is never checked, always set it to 0 to make it clear. > > > > Fix the -EINTR error message, which couldn't be reached until now. > > > > This is tested with a following patch. > > > > Cc: Brendan Higgins > > Cc: David Gow > > Cc: Rae Moar > > Cc: Shuah Khan > > Reviewed-by: Kees Cook > > Signed-off-by: Mickaël Salaün > > Link: https://lore.kernel.org/r/20240301194037.532117-5-mic@digikod.net > > --- > > > > Changes since v1: > > * Added Kees's Reviewed-by. > > --- > > include/kunit/try-catch.h | 3 --- > > lib/kunit/try-catch.c | 14 +++++++------- > > 2 files changed, 7 insertions(+), 10 deletions(-) > > > > diff --git a/include/kunit/try-catch.h b/include/kunit/try-catch.h > > index c507dd43119d..7c966a1adbd3 100644 > > --- a/include/kunit/try-catch.h > > +++ b/include/kunit/try-catch.h > > @@ -14,13 +14,11 @@ > > > > typedef void (*kunit_try_catch_func_t)(void *); > > > > -struct completion; > > struct kunit; > > > > /** > > * struct kunit_try_catch - provides a generic way to run code which might fail. > > * @test: The test case that is currently being executed. > > - * @try_completion: Completion that the control thread waits on while test runs. > > * @try_result: Contains any errno obtained while running test case. > > * @try: The function, the test case, to attempt to run. > > * @catch: The function called if @try bails out. > > @@ -46,7 +44,6 @@ struct kunit; > > struct kunit_try_catch { > > /* private: internal use only. */ > > struct kunit *test; > > - struct completion *try_completion; > > int try_result; > > kunit_try_catch_func_t try; > > kunit_try_catch_func_t catch; > > diff --git a/lib/kunit/try-catch.c b/lib/kunit/try-catch.c > > index cab8b24b5d5a..c6ee4db0b3bd 100644 > > --- a/lib/kunit/try-catch.c > > +++ b/lib/kunit/try-catch.c > > @@ -18,7 +18,7 @@ > > void __noreturn kunit_try_catch_throw(struct kunit_try_catch *try_catch) > > { > > try_catch->try_result = -EFAULT; > > - kthread_complete_and_exit(try_catch->try_completion, -EFAULT); > > + kthread_exit(0); > > } > > EXPORT_SYMBOL_GPL(kunit_try_catch_throw); > > > > @@ -26,9 +26,12 @@ static int kunit_generic_run_threadfn_adapter(void *data) > > { > > struct kunit_try_catch *try_catch = data; > > > > + try_catch->try_result = -EINTR; > > try_catch->try(try_catch->context); > > + if (try_catch->try_result == -EINTR) > > + try_catch->try_result = 0; > > > > - kthread_complete_and_exit(try_catch->try_completion, 0); > > + return 0; > > Really my only question is why we do not need to still do a > kthread_exit(0) here? I realize we are not checking the thread's exit > code but isn't it safer to call kthread_exit(). I'm new to kthread so > I am not too sure. This function is the body of the thread, and as we can see in the signature it should return an integer that will then be passed to kthread_exit() (by kthread-specific code). It is then useless to directly call kthread_exit() here, and it is cleaner to follow common thread function signature. > > > } > > > > static unsigned long kunit_test_timeout(void) > > @@ -58,13 +61,11 @@ static unsigned long kunit_test_timeout(void) > > > > void kunit_try_catch_run(struct kunit_try_catch *try_catch, void *context) > > { > > - DECLARE_COMPLETION_ONSTACK(try_completion); > > struct kunit *test = try_catch->test; > > struct task_struct *task_struct; > > int exit_code, time_remaining; > > > > try_catch->context = context; > > - try_catch->try_completion = &try_completion; > > try_catch->try_result = 0; > > task_struct = kthread_create(kunit_generic_run_threadfn_adapter, > > try_catch, "kunit_try_catch_thread"); > > @@ -75,8 +76,7 @@ void kunit_try_catch_run(struct kunit_try_catch *try_catch, void *context) > > } > > get_task_struct(task_struct); > > wake_up_process(task_struct); > > - > > - time_remaining = wait_for_completion_timeout(&try_completion, > > + time_remaining = wait_for_completion_timeout(task_struct->vfork_done, > > kunit_test_timeout()); > > if (time_remaining == 0) { > > try_catch->try_result = -ETIMEDOUT; > > @@ -92,7 +92,7 @@ void kunit_try_catch_run(struct kunit_try_catch *try_catch, void *context) > > if (exit_code == -EFAULT) > > try_catch->try_result = 0; > > else if (exit_code == -EINTR) > > - kunit_err(test, "wake_up_process() was never called\n"); > > + kunit_err(test, "try faulted\n"); > > else if (exit_code == -ETIMEDOUT) > > kunit_err(test, "try timed out\n"); > > else if (exit_code) > > -- > > 2.44.0 > > >