From nobody Sat Sep 27 18:14:15 2025 Received: from mail-qk1-f201.google.com (mail-qk1-f201.google.com [209.85.222.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 972EC1F428C for ; Mon, 11 Aug 2025 22:18:00 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.222.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1754950682; cv=none; b=AInjvVcouhv63cbzoBTbZS8WMSR8TUr8tDHV6EtqCc3+j1zdK28LQq8EpMhtJsON6xnLPzQdVBYebpIpfqY0ObFzep7pz42Mt/flB24j/uT/GPpsBigwKSj2a97u8Az7KQ4nl7OFRPXpVHuPgbc7Ogvn++sNsHdL33osgpW2GI8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1754950682; c=relaxed/simple; bh=XHm/+SmUNM7zQQQ4kDPepNO4LjLMWubYjIG0fZthsfc=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=IypWF+Jgul0FfPzDnOopNEcfXyORr/fdghJt/rL+talNprnGxVySvhocY1DGau1arYtVr0DOP1/ZlY4ZEVQuhPQUa/vOAmOoKcpFwffhWbXixbV+JatV18J9MtRPb1jVz9C9eVPD/Lw5T1KmL7JRWAuSgrRQi/zuZQov4vYmKtY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--marievic.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=3ed4wifC; arc=none smtp.client-ip=209.85.222.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--marievic.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="3ed4wifC" Received: by mail-qk1-f201.google.com with SMTP id af79cd13be357-7e8046e3706so1500493085a.0 for ; Mon, 11 Aug 2025 15:18:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1754950679; x=1755555479; 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=kZCyJ8fS+0ViRKQvRY7E+bm8qVK2uq6SSWVZQUVTZM4=; b=3ed4wifCaE9rpCPeb7kupu6Dd1TorjO9oXaKXPz+BX2QDQgLvpAeKpLFAw8rSJ/jV0 MSEFXnV9jPHo6u4AMRNSNIfRfVECoFPtad5wZkEP0UxhWiNsscLDfswjYCAKcASFZGGt zYmVUZJBM+9KM3hzu/1V1q98CfCr7PlsCs6BHyVD+F32MPq9ML6doF/jpmAnBGkKHd+q 9vlqk+9qNFSfP08DRatin9AtL4RhUJQveNtpqpJ82dKErwRvhzICUBY/U34cx7YjvGPr JqLhGqbp9x7iQ51vNpUV3Y20pO3tv3dqaoEujpnARsOTl9Y16gJFak8IlLvVI4x6dzue nGrg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1754950679; x=1755555479; 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=kZCyJ8fS+0ViRKQvRY7E+bm8qVK2uq6SSWVZQUVTZM4=; b=CG0h+zWNkleM28duPlIj/azgF1Yha2unLxbJMpPoiz6JNW89Outb6fn4YT5TmmYB5O Gd9Op//TXhHZsjMtptbPmwpRkb+spldhAjT3Hoc22i8hfY/vsZ5znOaCD6TeqTtl5XIL JgJtEXjhj8ZFwf89IHHQPA8Z99BFddfWGehiu3+cYt8kYHAdCRebDw8Ei6DdXj4nR7jk Wlzm3mUVlofIXq3AvebjSpO/u7HhszERcMJVeRGy0SHDrigr/ze/QGa+4umPn/ZIdtWn G8E+pfA6jeP4SOY/Cz21cPcyliNas1/YYhC5XbYd/vGceCAf5K+LHVAm0lY9NtFZnwmx +hKA== X-Forwarded-Encrypted: i=1; AJvYcCXE0Abt5I7+qxTcH7H+YnjTq3P1kn01Zk2sxBRnO24TeYShDiGc2E5swuUNr9rFUvQonX0MI7E3NdiKjoU=@vger.kernel.org X-Gm-Message-State: AOJu0YzYPK5SMLcKSdmh4rYw8AhuWAIsVIkGCKSqt6XpibAb5rJ1NuOI N+awP9NWX3LJUckoibcBHtSEyhNJkqReYleCASU7L/6I9T1xY04ihXYAN/YS13cP0dZ8xDvK0iJ 8Zv140IVmKuUYFg== X-Google-Smtp-Source: AGHT+IGhtZ5xU+kV0HYNIrp9JCR5xLG8HbXSQzbL/irNzdF3bGZbSnIMXJ4PReI3rQSi5cdwkjBavHO6zcrEZg== X-Received: from qktt14.prod.google.com ([2002:a05:620a:4e:b0:7e6:9d66:9ee3]) (user=marievic job=prod-delivery.src-stubby-dispatcher) by 2002:a05:620a:e0a:b0:7e6:2610:f2e0 with SMTP id af79cd13be357-7e8588faba8mr194499385a.39.1754950679395; Mon, 11 Aug 2025 15:17:59 -0700 (PDT) Date: Mon, 11 Aug 2025 22:17:37 +0000 In-Reply-To: <20250811221739.2694336-1-marievic@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250811221739.2694336-1-marievic@google.com> X-Mailer: git-send-email 2.51.0.rc0.205.g4a044479a3-goog Message-ID: <20250811221739.2694336-6-marievic@google.com> Subject: [PATCH v2 5/7] kunit: Add example parameterized test with shared resource management using the Resource API 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-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" 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. Signed-off-by: Marie Zhussupova Reviewed-by: David Gow Reviewed-by: Rae Moar --- Changes in v2: - 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 | 118 +++++++++++++++++++++++++++++++++ 1 file changed, 118 insertions(+) diff --git a/lib/kunit/kunit-example-test.c b/lib/kunit/kunit-example-test.c index 3056d6bc705d..f2819ee58965 100644 --- a/lib/kunit/kunit-example-test.c +++ b/lib/kunit/kunit-example-test.c @@ -277,6 +277,122 @@ 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. + * + * Alternatively, since this is a static array we can also use + * KUNIT_CASE_PARAM_ARRAY(,DESC) to create a `*_gen_params()` function + * and pass that to KUNIT_CASE_PARAM_WITH_INIT() instead of registering + * the parameter array here. + */ + 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 +412,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.rc0.205.g4a044479a3-goog