Received: by 2002:ab2:1689:0:b0:1f7:5705:b850 with SMTP id d9csp1344185lqa; Mon, 29 Apr 2024 06:11:12 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCUbMt8GVXgK4A7IArDWORwwegJ2GbRSTnydlpVncwCmlG/zGVzVqFm3WK55hehYy+VBIM7fTFBAcKR5mzA9eQKdXdaEblEcISDBczRaDA== X-Google-Smtp-Source: AGHT+IF8IjKIwziiTkNkb9aDd9j0zbagFTh9a46IiJqjYyvGDLx4EMw1i23267xZ6iS1FI0fTRqv X-Received: by 2002:a05:6214:2267:b0:6a0:cb69:54a2 with SMTP id gs7-20020a056214226700b006a0cb6954a2mr4313604qvb.58.1714396271872; Mon, 29 Apr 2024 06:11:11 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1714396271; cv=pass; d=google.com; s=arc-20160816; b=gPavNNQDUM5qjKfKBLpX0Eb7/Jweo/bYQRqxnkRUzi4Xkltk/+1T+4ON3iJkKM0jq5 2iIT0HO+Ae8YXS5E89h+54v2zdIUi/uceC3RUIBGR0Xk9/fM0wTxzX8gtulaCIO+qYdb 8Aj886aB564vA/USZAs4MmIF8Sup5vqg4qCu4fLW2EzILSyR47C9PP7W5AAfmh9g00Rb 2Ks6jEG8pXlMUpfaAJCOe5ZezWJUzDeHZNbX/p8q6RzHuvqk0JoLhIu+Vgu+khBjqDbb NmQV/J9naMP/qKITXZwgc5OJSg8fVUdI0p5onyNXnsBX22rd5hCL0cL1o0NBn7H0HaCE 0PDw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=k/JxFWChI7ZcNwqLmq9VOueKEkB8RSvyTEqZAYZx3O8=; fh=T/ngxZ/rchirObc+ozpAMGxUCKvTk1uHXxRS2gfk1OI=; b=K1aCZz5ja5lJDEkHOSMe4kug1vWI1T1LuRuS+335V+EwPwnghHGxB4dTcPaxuYk7eP ygvczLMLld8iUiYS4hqscqRBB4/Xkle+ab13uYCSgyrOT6PRPBhDecJlWMVrUOai7Dh1 OpzBRGC8bkfzll6t07XKUDGlGL7mnNuNqICN9i2zM2Kmxytypd1glL3rgrN/rSmI7z8G 5h9WLJTe1ls6Tz2Z6dmeteji/1bQ/7FI+F2vXbJXjvttEUfJZlDqfEWEKdU2G8ZI27NK 2HRBblzJvrs8kaL5rh20P+dwigBLyYA/BRza6NHdRu382Okdscu01nk++5oBtIqq1OmD F3dw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@digikod.net header.s=20191114 header.b=pT0JYupi; arc=pass (i=1 spf=pass spfdomain=digikod.net dkim=pass dkdomain=digikod.net); spf=pass (google.com: domain of linux-kernel+bounces-162333-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-162333-linux.lists.archive=gmail.com@vger.kernel.org" Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id q2-20020ad45ca2000000b00699216d6e56si26616942qvh.354.2024.04.29.06.11.11 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 29 Apr 2024 06:11:11 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-162333-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@digikod.net header.s=20191114 header.b=pT0JYupi; arc=pass (i=1 spf=pass spfdomain=digikod.net dkim=pass dkdomain=digikod.net); spf=pass (google.com: domain of linux-kernel+bounces-162333-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-162333-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 ny.mirrors.kernel.org (Postfix) with ESMTPS id 97C4C1C24104 for ; Mon, 29 Apr 2024 13:11:11 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 348907BAE3; Mon, 29 Apr 2024 13:09:53 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=digikod.net header.i=@digikod.net header.b="pT0JYupi" Received: from smtp-bc0c.mail.infomaniak.ch (smtp-bc0c.mail.infomaniak.ch [45.157.188.12]) (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 9F2A854907 for ; Mon, 29 Apr 2024 13:09:48 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=45.157.188.12 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714396191; cv=none; b=LmWoXzr2Dt98n1zxZugKefm1/iriMEZUEG0Lq7iZNjfyTwUWoP/5wOZdwNYuWWJP3DqyNNs35FxRKGqfPuNnWeXUAevfrMXlnOWXReajnLSZGLn3qu9A0IfVUOEHXqQkeveZYDPgB6ilWQhXnfKER3rfaCq1WYTRIqg/gEYj0SI= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714396191; c=relaxed/simple; bh=7yyZixArZc3x6/A5gkKGGEmPAuFqi+s/YoP+nOX77o0=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=BKkcxBe4YicS7htSTeI6gY5UAZg1GrROBkCriladGIB66XfwImS6wgvf9G3s4kAoHUWkOpaRi+03ornvdq4UeZQz08+z0P/GkJJw1caaEQ90Sd9avPY7m+9Bq9rW3n2PbMCL+ivEpNhs8bApYwIBA7IbkqiVBQqlx8LHVv+CRKY= 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=pT0JYupi; arc=none smtp.client-ip=45.157.188.12 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-0001.mail.infomaniak.ch (smtp-3-0001.mail.infomaniak.ch [10.4.36.108]) by smtp-3-3000.mail.infomaniak.ch (Postfix) with ESMTPS id 4VSkFK1RcwzQNF; Mon, 29 Apr 2024 15:09:41 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=digikod.net; s=20191114; t=1714396181; bh=7yyZixArZc3x6/A5gkKGGEmPAuFqi+s/YoP+nOX77o0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=pT0JYupik/0RFyv9O9CItWC4mowyFQtUHDw6rKpFkYCYBm7I2PXID40ChgG0WVoRV 1Uhj5vcqbbz2DbO6p6N44EYiKd/baLJORhg3wiuiwwCN/UDr6qVTYDjar5vdiLYrC3 MQnUhn3MSSwqu4lX01Crws9M8p4LMB6SrKSOLHYQ= Received: from unknown by smtp-3-0001.mail.infomaniak.ch (Postfix) with ESMTPA id 4VSkFJ4vFWzhgF; Mon, 29 Apr 2024 15:09:40 +0200 (CEST) From: =?UTF-8?q?Micka=C3=ABl=20Sala=C3=BCn?= To: Christian Brauner , Jakub Kicinski , Kees Cook , Mark Brown , Shengyu Li , Shuah Khan Cc: =?UTF-8?q?Micka=C3=ABl=20Sala=C3=BCn?= , "David S . Miller" , =?UTF-8?q?G=C3=BCnther=20Noack?= , Will Drewry , kernel test robot , linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, Shuah Khan Subject: [PATCH v2 3/9] selftests/harness: Fix fixture teardown Date: Mon, 29 Apr 2024 15:09:25 +0200 Message-ID: <20240429130931.2394118-4-mic@digikod.net> In-Reply-To: <20240429130931.2394118-1-mic@digikod.net> References: <20240429130931.2394118-1-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-Transfer-Encoding: 8bit X-Infomaniak-Routing: alpha Make sure fixture teardowns are run when test cases failed, including when _metadata->teardown_parent is set to true. Make sure only one fixture teardown is run per test case, handling the case where the test child forks. Cc: Jakub Kicinski Cc: Shengyu Li Cc: Shuah Khan Fixes: 72d7cb5c190b ("selftests/harness: Prevent infinite loop due to Assert in FIXTURE_TEARDOWN") Fixes: 0710a1a73fb4 ("selftests/harness: Merge TEST_F_FORK() into TEST_F()") Reviewed-by: Kees Cook Signed-off-by: Mickaël Salaün Link: https://lore.kernel.org/r/20240429130931.2394118-4-mic@digikod.net --- tools/testing/selftests/kselftest_harness.h | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/tools/testing/selftests/kselftest_harness.h b/tools/testing/selftests/kselftest_harness.h index d98702b6955d..55699a762c45 100644 --- a/tools/testing/selftests/kselftest_harness.h +++ b/tools/testing/selftests/kselftest_harness.h @@ -382,7 +382,10 @@ FIXTURE_DATA(fixture_name) self; \ pid_t child = 1; \ int status = 0; \ - bool jmp = false; \ + /* Makes sure there is only one teardown, even when child forks again. */ \ + bool *teardown = mmap(NULL, sizeof(*teardown), \ + PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0); \ + *teardown = false; \ memset(&self, 0, sizeof(FIXTURE_DATA(fixture_name))); \ if (setjmp(_metadata->env) == 0) { \ /* Use the same _metadata. */ \ @@ -399,15 +402,16 @@ _metadata->exit_code = KSFT_FAIL; \ } \ } \ - else \ - jmp = true; \ if (child == 0) { \ - if (_metadata->setup_completed && !_metadata->teardown_parent && !jmp) \ + if (_metadata->setup_completed && !_metadata->teardown_parent && \ + __sync_bool_compare_and_swap(teardown, false, true)) \ fixture_name##_teardown(_metadata, &self, variant->data); \ _exit(0); \ } \ - if (_metadata->setup_completed && _metadata->teardown_parent) \ + if (_metadata->setup_completed && _metadata->teardown_parent && \ + __sync_bool_compare_and_swap(teardown, false, true)) \ fixture_name##_teardown(_metadata, &self, variant->data); \ + munmap(teardown, sizeof(*teardown)); \ if (!WIFEXITED(status) && WIFSIGNALED(status)) \ /* Forward signal to __wait_for_test(). */ \ kill(getpid(), WTERMSIG(status)); \ -- 2.44.0