From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id BAD69CA0EE4 for ; Fri, 15 Aug 2025 10:36:16 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 7DEDA10E923; Fri, 15 Aug 2025 10:36:16 +0000 (UTC) Authentication-Results: gabe.freedesktop.org; dkim=pass (2048-bit key; unprotected) header.d=google.com header.i=@google.com header.b="PLhLwhhN"; dkim-atps=neutral Received: from mail-qt1-f201.google.com (mail-qt1-f201.google.com [209.85.160.201]) by gabe.freedesktop.org (Postfix) with ESMTPS id 9F57210E926 for ; Fri, 15 Aug 2025 10:36:14 +0000 (UTC) Received: by mail-qt1-f201.google.com with SMTP id d75a77b69052e-4b109a92dddso50593841cf.1 for ; Fri, 15 Aug 2025 03:36:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1755254174; x=1755858974; darn=lists.freedesktop.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=sAGiMmlsvvRAyIRtfIkliO7s5pzV7dahw9GOcGCzEKA=; b=PLhLwhhN3T39EXZZNJEvb/5fc9cMMEs3CM8T2QY0QuxkPVy75FrPQ6WZ82LeEHU48B vHbQajgVZV3WHca7NJ1l+MelBdAtS3bMhpZypKwfoZtMVwKpSBDJDGVhZLiYF1cRrC2X dd5Ep0QjvGC2jK6RkymrpXCaIkTfdZPiU7U3Dz60jQhpgFah61Hi6nEDcFTtT0v21ZB0 dD6kXmdU6J+FtfWGDBs2OrxAs4pffWc3woMEDs7/HlaFFgsqy0eDGCEHibYgmDTBYQDc yjqnZmLhJqFwiLbi8p4Zyt6uQ8b26OTBDX4criYigV9LUsaxtXVoULfIDkImGiCV/WUT IwZg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1755254174; x=1755858974; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=sAGiMmlsvvRAyIRtfIkliO7s5pzV7dahw9GOcGCzEKA=; b=WaM1LDVAWh94UMDjTIM5k6kyMCgF9xPu7W6YwfAWwXw93SSYLKqLiwLDS07nbLtGOG e3RWGCWxsK5x4V4LadT2mumpAjyS3KHVJBluCvKBonpjbTJm6DA5jPK2m6j4Tzzxrig/ g0RnkBj51nDICoqDlaCr9Q6C8xzQzVz50uvjhzqTK/RXw1SvHqjt+8jQyMb4jQCzb0Gk OVxRmA9kDGI8T80t20cdDbAQk1myON3XaMH4QVe9/UtXecq6SwWw47vmaRB/jq/NykDj AbX9faH3aWbInp502myana2/OfD82JP5uuay61YqnDfcI/6rSpGKS2ip234z6bj3mViL T2rw== X-Forwarded-Encrypted: i=1; AJvYcCXqNtJJlk72bEIis8gIvu720fN+MezXtVPFNy0PTQK6UZ5/I7/PkQEdJAQwxK8ezoNuHkv5HC8Xrg==@lists.freedesktop.org X-Gm-Message-State: AOJu0YxZv9oVxScvn6R6abPIC0ZAE6kKrIiOUbb8oN3ByxRys0Oeb1Hu yaKOq32SIMd+5sASIqb+UxoS1haLOOj6h5vXXBLaxjIIF2S60NmPDPee0UGKNXwJ/ncS7CO1GKV mG6kqylSe3xS0rw== X-Google-Smtp-Source: AGHT+IHSVv7xHpU+Q0K/EeYyGEApkzk4giTDGbZ8lKVWGiJHgmFp5Hi5KpmylCxkoZzIsi7d4UYcfQKbguUZug== X-Received: from qth25.prod.google.com ([2002:a05:622a:9019:b0:4af:205f:b347]) (user=marievic job=prod-delivery.src-stubby-dispatcher) by 2002:a05:622a:19a7:b0:4b0:6f65:4622 with SMTP id d75a77b69052e-4b11e100b18mr13704771cf.2.1755254173610; Fri, 15 Aug 2025 03:36:13 -0700 (PDT) Date: Fri, 15 Aug 2025 10:35:59 +0000 In-Reply-To: <20250815103604.3857930-1-marievic@google.com> Mime-Version: 1.0 References: <20250815103604.3857930-1-marievic@google.com> X-Mailer: git-send-email 2.51.0.rc1.167.g924127e9c0-goog Message-ID: <20250815103604.3857930-3-marievic@google.com> Subject: [PATCH v3 2/7] kunit: Introduce param_init/exit for parameterized test context management From: Marie Zhussupova To: rmoar@google.com, davidgow@google.com, shuah@kernel.org, brendan.higgins@linux.dev Cc: mark.rutland@arm.com, elver@google.com, dvyukov@google.com, lucas.demarchi@intel.com, thomas.hellstrom@linux.intel.com, rodrigo.vivi@intel.com, linux-kselftest@vger.kernel.org, kunit-dev@googlegroups.com, kasan-dev@googlegroups.com, intel-xe@lists.freedesktop.org, dri-devel@lists.freedesktop.org, linux-kernel@vger.kernel.org, Marie Zhussupova Content-Type: text/plain; charset="UTF-8" X-BeenThere: intel-xe@lists.freedesktop.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Intel Xe graphics driver List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: intel-xe-bounces@lists.freedesktop.org Sender: "Intel-xe" Add (*param_init) and (*param_exit) function pointers to `struct kunit_case`. Users will be able to set them via the new KUNIT_CASE_PARAM_WITH_INIT() macro. param_init/exit will be invoked by kunit_run_tests() once before and once after the parameterized test, respectively. They will receive the `struct kunit` that holds the parameterized test context; facilitating init and exit for shared state. This patch also sets param_init/exit to None in rust/kernel/kunit.rs. Reviewed-by: Rae Moar Signed-off-by: Marie Zhussupova --- Changes in v3: v2: https://lore.kernel.org/all/20250811221739.2694336-3-marievic@google.com/ - kunit_init_parent_param_test() now sets both the `struct kunit_case` and the `struct kunit` statuses as failed if the parameterized test init failed. The failure message was also changed to include the failure code, mirroring the kunit_suite init failure message. - A check for parameter init failure was added in kunit_run_tests(). So, if the init failed, the framework will skip the parameter runs and update the param_test statistics to count that failure. - Commit message formatting. Changes in v2: v1: https://lore.kernel.org/all/20250729193647.3410634-3-marievic@google.com/ - param init/exit were set to None in rust/kernel/kunit.rs to fix the Rust breakage. - The name of __kunit_init_parent_test was changed to kunit_init_parent_param_test and its call was changed to happen only if the test is parameterized. - The param_exit call was also moved inside the check for if the test is parameterized. - KUNIT_CASE_PARAM_WITH_INIT() macro logic was change to not automatically set generate_params() to KUnit's built-in generator function. Instead, the test user will be asked to provide it themselves. - The comments and the commit message were changed to reflect the parameterized testing terminology. See the patch series cover letter change log for the definitions. --- include/kunit/test.h | 25 +++++++++++++++++++++++++ lib/kunit/test.c | 27 ++++++++++++++++++++++++++- rust/kernel/kunit.rs | 4 ++++ 3 files changed, 55 insertions(+), 1 deletion(-) diff --git a/include/kunit/test.h b/include/kunit/test.h index b47b9a3102f3..d2e1b986b161 100644 --- a/include/kunit/test.h +++ b/include/kunit/test.h @@ -92,6 +92,8 @@ struct kunit_attributes { * @name: the name of the test case. * @generate_params: the generator function for parameterized tests. * @attr: the attributes associated with the test + * @param_init: The init function to run before a parameterized test. + * @param_exit: The exit function to run after a parameterized test. * * A test case is a function with the signature, * ``void (*)(struct kunit *)`` @@ -128,6 +130,8 @@ struct kunit_case { const char *name; const void* (*generate_params)(const void *prev, char *desc); struct kunit_attributes attr; + int (*param_init)(struct kunit *test); + void (*param_exit)(struct kunit *test); /* private: internal use only. */ enum kunit_status status; @@ -218,6 +222,27 @@ static inline char *kunit_status_to_ok_not_ok(enum kunit_status status) .generate_params = gen_params, \ .attr = attributes, .module_name = KBUILD_MODNAME} +/** + * KUNIT_CASE_PARAM_WITH_INIT - Define a parameterized KUnit test case with custom + * param_init() and param_exit() functions. + * @test_name: The function implementing the test case. + * @gen_params: The function to generate parameters for the test case. + * @init: A reference to the param_init() function to run before a parameterized test. + * @exit: A reference to the param_exit() function to run after a parameterized test. + * + * Provides the option to register param_init() and param_exit() functions. + * param_init/exit will be passed the parameterized test context and run once + * before and once after the parameterized test. The init function can be used + * to add resources to share between parameter runs, and any other setup logic. + * The exit function can be used to clean up resources that were not managed by + * the parameterized test, and any other teardown logic. + */ +#define KUNIT_CASE_PARAM_WITH_INIT(test_name, gen_params, init, exit) \ + { .run_case = test_name, .name = #test_name, \ + .generate_params = gen_params, \ + .param_init = init, .param_exit = exit, \ + .module_name = KBUILD_MODNAME} + /** * struct kunit_suite - describes a related collection of &struct kunit_case * diff --git a/lib/kunit/test.c b/lib/kunit/test.c index 14a8bd846939..917df2e1688d 100644 --- a/lib/kunit/test.c +++ b/lib/kunit/test.c @@ -641,6 +641,20 @@ static void kunit_accumulate_stats(struct kunit_result_stats *total, total->total += add.total; } +static void kunit_init_parent_param_test(struct kunit_case *test_case, struct kunit *test) +{ + if (test_case->param_init) { + int err = test_case->param_init(test); + + if (err) { + kunit_err(test_case, KUNIT_SUBTEST_INDENT KUNIT_SUBTEST_INDENT + "# failed to initialize parent parameter test (%d)", err); + test->status = KUNIT_FAILURE; + test_case->status = KUNIT_FAILURE; + } + } +} + int kunit_run_tests(struct kunit_suite *suite) { char param_desc[KUNIT_PARAM_DESC_SIZE]; @@ -678,6 +692,11 @@ int kunit_run_tests(struct kunit_suite *suite) kunit_run_case_catch_errors(suite, test_case, &test); kunit_update_stats(¶m_stats, test.status); } else { + kunit_init_parent_param_test(test_case, &test); + if (test_case->status == KUNIT_FAILURE) { + kunit_update_stats(¶m_stats, test.status); + goto test_case_end; + } /* Get initial param. */ param_desc[0] = '\0'; /* TODO: Make generate_params try-catch */ @@ -714,10 +733,16 @@ int kunit_run_tests(struct kunit_suite *suite) param_desc[0] = '\0'; curr_param = test_case->generate_params(curr_param, param_desc); } + /* + * TODO: Put into a try catch. Since we don't need suite->exit + * for it we can't reuse kunit_try_run_cleanup for this yet. + */ + if (test_case->param_exit) + test_case->param_exit(&test); /* TODO: Put this kunit_cleanup into a try-catch. */ kunit_cleanup(&test); } - +test_case_end: kunit_print_attr((void *)test_case, true, KUNIT_LEVEL_CASE); kunit_print_test_stats(&test, param_stats); diff --git a/rust/kernel/kunit.rs b/rust/kernel/kunit.rs index 4b8cdcb21e77..cda64574b44d 100644 --- a/rust/kernel/kunit.rs +++ b/rust/kernel/kunit.rs @@ -207,6 +207,8 @@ pub const fn kunit_case( status: kernel::bindings::kunit_status_KUNIT_SUCCESS, module_name: core::ptr::null_mut(), log: core::ptr::null_mut(), + param_init: None, + param_exit: None, } } @@ -226,6 +228,8 @@ pub const fn kunit_case_null() -> kernel::bindings::kunit_case { status: kernel::bindings::kunit_status_KUNIT_SUCCESS, module_name: core::ptr::null_mut(), log: core::ptr::null_mut(), + param_init: None, + param_exit: None, } } -- 2.51.0.rc1.167.g924127e9c0-goog