From nobody Sat Sep 27 20:23:25 2025 Received: from mail-pl1-f201.google.com (mail-pl1-f201.google.com [209.85.214.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 6E82130E0D6 for ; Tue, 26 Aug 2025 09:13:53 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1756199635; cv=none; b=tpP9UxxvGSuK6tuGvASfZXePsz2HcGZBZ+j0MABUrbnAYdFTuiLFVf9vJRF/fAO+vjTWiFzxvMgEK/eV+KwQdK2zKurmQrG016XTqhsl8h7v9j/L+Ur6c0WnWxZtD1jgiu/+yIh9zkomscqDk00TWiBMHqTq05kpEQXGxc0ObtA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1756199635; c=relaxed/simple; bh=3aFRb/9IcKHZ+M6T9pjKhjrVD03MzY2Xmw+S7EN4W2Q=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=miPNAaSLoB4DVbSKGTdgZQZIeWUrZaALpNSlU9ICMpfRd68fabB9N4b8rJddMk90FJ7ii4ZdMKAAPTNq2vGjQaHWGe6xzG8kJlbtRlWsbLCMgwvAh3hloX8YxPkicZbcuDZo7F8FD44gn9lAp0xAdfUxQF56Z/MkrO/NkXrEX+k= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--davidgow.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=ztZKiFdZ; arc=none smtp.client-ip=209.85.214.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--davidgow.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="ztZKiFdZ" Received: by mail-pl1-f201.google.com with SMTP id d9443c01a7336-24457f59889so56278315ad.0 for ; Tue, 26 Aug 2025 02:13:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1756199633; x=1756804433; darn=vger.kernel.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=E/DdI8QKaF0QvnOvsbrRP9RRgtoSroBxXesHs3tNY3s=; b=ztZKiFdZMJN4rUrWKMJzleotJY622iBy8j7e46ZVKKLa/Jgjd6otQYZwYMfWyvH0NM Gk7y9MRPBdUKnqDQuNNbuuIyJUskKJjS1yn//D8bCP+AyOvUr1j5Qsj/tFinKjAdDRgI eMz4poDUQ8J7JfksA9xZZ/t/NBv6lsjZFqa91gQ5z0Zn1xuK9zYArSAu3ddO6nbNdrlJ vBlzcTW5jCxkhStAL+xxy2NCJoTXRugMEq6Tt5y6lFYz2Kg1NN10Xf0yMdP2bIVZRCDx jp1U5nAMGYZloOVPxq0pxqek6bAD19QoC4I2ApzG/mZfofKXzZpixmkV9KhTr5n1AF0/ 1dUQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1756199633; x=1756804433; 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=E/DdI8QKaF0QvnOvsbrRP9RRgtoSroBxXesHs3tNY3s=; b=KCxxPm6K9hw31jSHRTVr5gqwKtlGz3opbYtIVr02y/4DCFRFjr7HZBbWtec7Wl4S8j Cl7bVvgvcrzlneYCM1F81qofIsuw80PWk3pAwht3cubC06Gnq2/kkfI2j/YXiFaMrW5y +nQqGSwzZMLpCe4GUHzF4MHxVXT/PJojw09Z5Z2WVVb5xhiBSs+CMOGTXjmsXt5hjBkX +FT8Ed0QjABahr4jxCXINn1zwsv3Sy6KUHPRbdGsL1MMhrY7g8CJUOD7L7GEDazgMjpz caibOH2tTAeUrR40MiJRZxiRMXthmn4E3hVqD3V/hMRqViVgHYX26RgQhfFYUueP/kbT 0sfg== X-Forwarded-Encrypted: i=1; AJvYcCVuii3lL/ruHzvdRFeHW/wXDSb6iYyMuKSB//rFmbeoDopnw6Hp3y8bpIIMogkvLK1bc5Y8pGYpzRpVkyo=@vger.kernel.org X-Gm-Message-State: AOJu0YzxpqEyKX2uRQz1G+KTKRpkCiWGZzW9nTgJ1pUc9cYPDvfCdBao gXWtdmM4e4LscmGzHyODN4qfVeA/nbuOwN+0hrQPkufIlMBxcKQ9G+GZmhTPbwj6wZn8WE9gXDz iPQiNJbminGO7NA== X-Google-Smtp-Source: AGHT+IFm2ccebIdeBPqgsg0k7MRJFE97IlpYmQ10sZM4xPEDskf6ORLyTRQTD8U2eP7B+NNUOmHZ2XIMml/jXw== X-Received: from pldp2.prod.google.com ([2002:a17:902:eac2:b0:237:cedc:1467]) (user=davidgow job=prod-delivery.src-stubby-dispatcher) by 2002:a17:902:ec87:b0:246:6a8b:8473 with SMTP id d9443c01a7336-2466a8b8672mr147539915ad.45.1756199632691; Tue, 26 Aug 2025 02:13:52 -0700 (PDT) Date: Tue, 26 Aug 2025 17:13:35 +0800 In-Reply-To: <20250826091341.1427123-1-davidgow@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250826091341.1427123-1-davidgow@google.com> X-Mailer: git-send-email 2.51.0.261.g7ce5a0a67e-goog Message-ID: <20250826091341.1427123-6-davidgow@google.com> Subject: [PATCH v4 5/7] kunit: Add example parameterized test with shared resource management using the Resource API From: David Gow To: Marie Zhussupova , marievictoria875@gmail.com, rmoar@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, Stephen Rothwell , David Gow Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Marie Zhussupova Add example_params_test_with_init() to illustrate how to manage shared resources across a parameterized KUnit test. This example showcases the use of the new param_init() function and its registration to a test using the KUNIT_CASE_PARAM_WITH_INIT() macro. Additionally, the test demonstrates how to directly pass a parameter array to the parameterized test context via kunit_register_params_array() and leveraging the Resource API for shared resource management. Reviewed-by: Rae Moar Reviewed-by: David Gow Signed-off-by: Marie Zhussupova Signed-off-by: David Gow --- No changes in v4: v3: https://lore.kernel.org/linux-kselftest/20250815103604.3857930-6-mariev= ic@google.com/ Changes in v3: v2: https://lore.kernel.org/all/20250811221739.2694336-6-marievic@google.co= m/ - Code comment edits. Changes in v2: v1: https://lore.kernel.org/all/20250729193647.3410634-8-marievic@google.co= m/ - kunit_array_gen_params() is now explicitly passed to KUNIT_CASE_PARAM_WITH_INIT() to be consistent with a parameterized test being defined by the existence of the generate_params() function. - The comments were edited to be more concise. - The patch header was changed to reflect that this example test's intent is more aligned with showcasing using the Resource API for shared resource management. - 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. --- lib/kunit/kunit-example-test.c | 113 +++++++++++++++++++++++++++++++++ 1 file changed, 113 insertions(+) diff --git a/lib/kunit/kunit-example-test.c b/lib/kunit/kunit-example-test.c index 3056d6bc705d..3e858367be01 100644 --- a/lib/kunit/kunit-example-test.c +++ b/lib/kunit/kunit-example-test.c @@ -277,6 +277,117 @@ static void example_slow_test(struct kunit *test) KUNIT_EXPECT_EQ(test, 1 + 1, 2); } =20 +/* + * This custom function allocates memory and sets the information we want + * stored in the kunit_resource->data field. + */ +static int example_resource_init(struct kunit_resource *res, void *context) +{ + int *info =3D kmalloc(sizeof(*info), GFP_KERNEL); + + if (!info) + return -ENOMEM; + *info =3D *(int *)context; + res->data =3D info; + return 0; +} + +/* + * This function deallocates memory for the kunit_resource->data field. + */ +static void example_resource_free(struct kunit_resource *res) +{ + kfree(res->data); +} + +/* + * This match function is invoked by kunit_find_resource() to locate + * a test resource based on certain criteria. + */ +static bool example_resource_alloc_match(struct kunit *test, + struct kunit_resource *res, + void *match_data) +{ + return res->data && res->free =3D=3D example_resource_free; +} + +/* + * This is an example of a function that provides a description for each o= f the + * parameters in a parameterized test. + */ +static void example_param_array_get_desc(struct kunit *test, const void *p= , char *desc) +{ + const struct example_param *param =3D p; + + snprintf(desc, KUNIT_PARAM_DESC_SIZE, + "example check if %d is less than or equal to 3", param->value); +} + +/* + * This function gets passed in the parameterized test context i.e. the + * struct kunit belonging to the parameterized test. You can use this func= tion + * to add resources you want shared across the whole parameterized test or + * for additional setup. + */ +static int example_param_init(struct kunit *test) +{ + int ctx =3D 3; /* Data to be stored. */ + size_t arr_size =3D ARRAY_SIZE(example_params_array); + + /* + * This allocates a struct kunit_resource, sets its data field to + * ctx, and adds it to the struct kunit's resources list. Note that + * this is parameterized test managed. So, it doesn't need to have + * a custom exit function to deallocation as it will get cleaned up at + * the end of the parameterized test. + */ + void *data =3D kunit_alloc_resource(test, example_resource_init, example_= resource_free, + GFP_KERNEL, &ctx); + + if (!data) + return -ENOMEM; + /* + * Pass the parameter array information to the parameterized test context + * struct kunit. Note that you will need to provide kunit_array_gen_param= s() + * as the generator function to KUNIT_CASE_PARAM_WITH_INIT() when registe= ring + * a parameter array this route. + */ + kunit_register_params_array(test, example_params_array, arr_size, + example_param_array_get_desc); + return 0; +} + +/* + * This is an example of a test that uses shared resources available in the + * parameterized test context. + */ +static void example_params_test_with_init(struct kunit *test) +{ + int threshold; + struct kunit_resource *res; + const struct example_param *param =3D test->param_value; + + /* By design, param pointer will not be NULL. */ + KUNIT_ASSERT_NOT_NULL(test, param); + + /* + * Here we pass test->parent to search for shared resources in the + * parameterized test context. + */ + res =3D kunit_find_resource(test->parent, example_resource_alloc_match, N= ULL); + + KUNIT_ASSERT_NOT_NULL(test, res); + + /* Since kunit_resource->data is a void pointer we need to typecast it. */ + threshold =3D *((int *)res->data); + + /* Assert that the parameter is less than or equal to a certain threshold= . */ + KUNIT_ASSERT_LE(test, param->value, threshold); + + /* This decreases the reference count after calling kunit_find_resource()= . */ + kunit_put_resource(res); +} + /* * Here we make a list of all the test cases we want to add to the test su= ite * below. @@ -296,6 +407,8 @@ static struct kunit_case example_test_cases[] =3D { KUNIT_CASE(example_static_stub_using_fn_ptr_test), KUNIT_CASE(example_priv_test), KUNIT_CASE_PARAM(example_params_test, example_gen_params), + KUNIT_CASE_PARAM_WITH_INIT(example_params_test_with_init, kunit_array_gen= _params, + example_param_init, NULL), KUNIT_CASE_SLOW(example_slow_test), {} }; --=20 2.51.0.261.g7ce5a0a67e-goog