From nobody Tue Dec 16 16:41:22 2025 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 94878C4167B for ; Fri, 8 Dec 2023 10:10:08 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1573552AbjLHKJ5 (ORCPT ); Fri, 8 Dec 2023 05:09:57 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37376 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1573553AbjLHKJu (ORCPT ); Fri, 8 Dec 2023 05:09:50 -0500 Received: from mail-yw1-x1149.google.com (mail-yw1-x1149.google.com [IPv6:2607:f8b0:4864:20::1149]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id EFDF2171F for ; Fri, 8 Dec 2023 02:09:52 -0800 (PST) Received: by mail-yw1-x1149.google.com with SMTP id 00721157ae682-5de8e375768so6491647b3.3 for ; Fri, 08 Dec 2023 02:09:52 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1702030192; x=1702634992; 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=p9BDbU6BiK/bb+b6d2mvvKMnn7b29FDLvRbbG2au7Cs=; b=EqYGlbBuyMaVVhzcmY3IlpzQkxU5kGU31+UGlT5uHRgwl4kT0NfSocv/1l0aQ/Q0Ne 9OvMcdMsFwV8wsz0PXkdIslwfHWf4HdprB2DKOekPriSsVXAUdvFf26FNdmXYEKdYfWL NlhLM4VlFyI2ZCbx6vIv7M0Q0QyWX6KQkGQe7NhZFmkSfEnZPbsHlimFLxriw2gRmDtw sL2MJQVUp6F3gXCRDJGwPTGYcwUgow8+AAXAZBN6/BJLO/CgBl0HTA6gsmKE9B6c4BNF S+FGymw5wx+u9ZlR52Co9793pgt+IQFiBXJzNr5fXG7nzK5H6P3xlRJ4aL1fB/W/6Uq1 reQg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702030192; x=1702634992; 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=p9BDbU6BiK/bb+b6d2mvvKMnn7b29FDLvRbbG2au7Cs=; b=GRqUdQdoWClheMJhEjhlMyIxz+bd2h8lqDXnkQ76AmSI2m7BNzIMUkzUb+EOGnccjl JhhdUGhiQCZmAZ0RRNylRE/NKc5DKLR8G0tA/7wkkORBu/OIPjIhwRwcWoX7wNycvJQm /JahDXtyCYlvtsEoD7qsSvz+pMduJ2aWn3F7ZkoffoczQnDagSPmIDY87EEi8BU/mpWp No3Ax4ThIcR/pR48aQMHzZCuDCWPtvtE1TUzVMCa2v1tbE6GfsO1PMnTy4bikZMu++h2 Nc67w3r5RW6OGriOz4fpAbaM+IW5ygiYSBcURhQE22jhOuSKbT38MVwPPFTVHh/LW3dx LnaA== X-Gm-Message-State: AOJu0YwXAS/d/rA6xL9MIobKwsr4exPYQJpCkMOBReKq3rVZbQNupJNR RJh0Tq1HON+tS7MPxTzdqvJm8LVm0TR0sw== X-Google-Smtp-Source: AGHT+IHNleCdgyatNThdB1BLyotO7f3vCuP6pLPPEha6KdVcY3KjVBTGQb3eHydW81vewFCX+TfdBv5sYTJ5dw== X-Received: from slicestar.c.googlers.com ([fda3:e722:ac3:cc00:4f:4b78:c0a8:20a1]) (user=davidgow job=sendgmr) by 2002:a05:6902:389:b0:dbc:4e4b:88e1 with SMTP id f9-20020a056902038900b00dbc4e4b88e1mr8837ybs.1.1702030192088; Fri, 08 Dec 2023 02:09:52 -0800 (PST) Date: Fri, 08 Dec 2023 18:09:29 +0800 In-Reply-To: <20231208-kunit_bus-v2-0-e95905d9b325@google.com> Mime-Version: 1.0 References: <20231208-kunit_bus-v2-0-e95905d9b325@google.com> X-Mailer: b4 0.13-dev-099c9 Message-ID: <20231208-kunit_bus-v2-1-e95905d9b325@google.com> Subject: [PATCH v2 1/4] kunit: Add APIs for managing devices From: davidgow@google.com To: Rae Moar , Brendan Higgins , Greg Kroah-Hartman , Matti Vaittinen , Stephen Boyd , Shuah Khan , Jonathan Corbet , Kees Cook , Liam Girdwood , Mark Brown , Jaroslav Kysela , Takashi Iwai , Maxime Ripard Cc: linux-kselftest@vger.kernel.org, kunit-dev@googlegroups.com, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-hardening@vger.kernel.org, linux-sound@vger.kernel.org, David Gow Content-Type: text/plain; charset="utf-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Tests for drivers often require a struct device to pass to other functions. While it's possible to create these with root_device_register(), or to use something like a platform device, this is both a misuse of those APIs, and can be difficult to clean up after, for example, a failed assertion. Add some KUnit-specific functions for registering and unregistering a struct device: - kunit_device_register() - kunit_device_register_with_driver() - kunit_device_unregister() These helpers allocate a on a 'kunit' bus which will either probe the driver passed in (kunit_device_register_with_driver), or will create a stub driver (kunit_device_register) which is cleaned up on test shutdown. Devices are automatically unregistered on test shutdown, but can be manually unregistered earlier with kunit_device_unregister() in order to, for example, test device release code. Reviewed-by: Matti Vaittinen Signed-off-by: David Gow Reviewed-by: Maxime Ripard --- Documentation/dev-tools/kunit/api/resource.rst | 9 ++ Documentation/dev-tools/kunit/usage.rst | 50 +++++++ include/kunit/device.h | 80 +++++++++++ lib/kunit/Makefile | 3 +- lib/kunit/device.c | 181 +++++++++++++++++++++= ++++ lib/kunit/kunit-test.c | 134 +++++++++++++++++- lib/kunit/test.c | 3 + 7 files changed, 458 insertions(+), 2 deletions(-) diff --git a/Documentation/dev-tools/kunit/api/resource.rst b/Documentation= /dev-tools/kunit/api/resource.rst index 0a94f831259e..ec6002a6b0db 100644 --- a/Documentation/dev-tools/kunit/api/resource.rst +++ b/Documentation/dev-tools/kunit/api/resource.rst @@ -11,3 +11,12 @@ state on a per-test basis, register custom cleanup actio= ns, and more. =20 .. kernel-doc:: include/kunit/resource.h :internal: + +Managed Devices +--------------- + +Functions for using KUnit-managed struct device and struct device_driver. +Include ``kunit/device.h`` to use these. + +.. kernel-doc:: include/kunit/device.h + :internal: diff --git a/Documentation/dev-tools/kunit/usage.rst b/Documentation/dev-to= ols/kunit/usage.rst index 9db12e91668e..53c6f7dc8a42 100644 --- a/Documentation/dev-tools/kunit/usage.rst +++ b/Documentation/dev-tools/kunit/usage.rst @@ -797,3 +797,53 @@ structures as shown below: KUnit is not enabled, or if no test is running in the current task, it wil= l do nothing. This compiles down to either a no-op or a static key check, so wi= ll have a negligible performance impact when no test is running. + +Managing Fake Devices and Drivers +--------------------------------- + +When testing drivers or code which interacts with drivers, many functions = will +require a ``struct device`` or ``struct device_driver``. In many cases, se= tting +up a real device is not required to test any given function, so a fake dev= ice +can be used instead. + +KUnit provides helper functions to create and manage these fake devices, w= hich +are internally of type ``struct kunit_device``, and are attached to a spec= ial +``kunit_bus``. These devices support managed device resources (devres), as +described in Documentation/driver-api/driver-model/devres.rst + +To create a KUnit-managed ``struct device_driver``, use ``kunit_driver_cre= ate()``, +which will create a driver with the given name, on the ``kunit_bus``. This= driver +will automatically be destroyed when the corresponding test finishes, but = can also +be manually destroyed with ``driver_unregister()``. + +To create a fake device, use the ``kunit_device_register()``, which will c= reate +and register a device, using a new KUnit-managed driver created with ``kun= it_driver_create()``. +To provide a specific, non-KUnit-managed driver, use ``kunit_device_regist= er_with_driver()`` +instead. Like with managed drivers, KUnit-managed fake devices are automat= ically +cleaned up when the test finishes, but can be manually cleaned up early wi= th +``kunit_device_unregister()``. + +The KUnit devices should be used in preference to ``root_device_register()= ``, and +instead of ``platform_device_register()`` in cases where the device is not= otherwise +a platform device. + +For example: + +.. code-block:: c + + #include + + static void test_my_device(struct kunit *test) + { + struct device *fake_device; + const char *dev_managed_string; + + // Create a fake device. + fake_device =3D kunit_device_register(test, "my_device"); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, fake_device) + + // Pass it to functions which need a device. + dev_managed_string =3D devm_kstrdup(fake_device, "Hello, World!"); + + // Everything is cleaned up automatically when the test ends. + } \ No newline at end of file diff --git a/include/kunit/device.h b/include/kunit/device.h new file mode 100644 index 000000000000..2450110ad64e --- /dev/null +++ b/include/kunit/device.h @@ -0,0 +1,80 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* + * KUnit basic device implementation + * + * Helpers for creating and managing fake devices for KUnit tests. + * + * Copyright (C) 2023, Google LLC. + * Author: David Gow + */ + +#ifndef _KUNIT_DEVICE_H +#define _KUNIT_DEVICE_H + +#if IS_ENABLED(CONFIG_KUNIT) + +#include + +struct device; +struct device_driver; + +/** + * kunit_driver_create() - Create a struct device_driver attached to the k= unit_bus + * @test: The test context object. + * @name: The name to give the created driver. + * + * Creates a struct device_driver attached to the kunit_bus, with the name= @name. + * This driver will automatically be cleaned up on test exit. + * + * Return: a stub struct device_driver, managed by KUnit, with the name @n= ame. + */ +struct device_driver *kunit_driver_create(struct kunit *test, const char *= name); + +/** + * kunit_device_register() - Create a struct device for use in KUnit tests + * @test: The test context object. + * @name: The name to give the created device. + * + * Creates a struct kunit_device (which is a struct device) with the given= name, + * and a corresponding driver. The device and driver will be cleaned up on= test + * exit, or when kunit_device_unregister is called. See also + * kunit_device_register_with_driver, if you wish to provide your own + * struct device_driver. + * + * Return: a pointer to a struct device which will be cleaned up when the = test + * exits, or an error pointer if the device could not be allocated or regi= stered. + */ +struct device *kunit_device_register(struct kunit *test, const char *name); + +/** + * kunit_device_register_with_driver() - Create a struct device for use in= KUnit tests + * @test: The test context object. + * @name: The name to give the created device. + * @drv: The struct device_driver to associate with the device. + * + * Creates a struct kunit_device (which is a struct device) with the given + * name, and driver. The device will be cleaned up on test exit, or when + * kunit_device_unregister is called. See also kunit_device_register, if y= ou + * wish KUnit to create and manage a driver for you. + * + * Return: a pointer to a struct device which will be cleaned up when the = test + * exits, or an error pointer if the device could not be allocated or regi= stered. + */ +struct device *kunit_device_register_with_driver(struct kunit *test, + const char *name, + const struct device_driver *drv); + +/** + * kunit_device_unregister() - Unregister a KUnit-managed device + * @test: The test context object which created the device + * @dev: The device. + * + * Unregisters and destroys a struct device which was created with + * kunit_device_register or kunit_device_register_with_driver. If KUnit cr= eated + * a driver, cleans it up as well. + */ +void kunit_device_unregister(struct kunit *test, struct device *dev); + +#endif + +#endif diff --git a/lib/kunit/Makefile b/lib/kunit/Makefile index 46f75f23dfe4..309659a32a78 100644 --- a/lib/kunit/Makefile +++ b/lib/kunit/Makefile @@ -7,7 +7,8 @@ kunit-objs +=3D test.o \ assert.o \ try-catch.o \ executor.o \ - attributes.o + attributes.o \ + device.o =20 ifeq ($(CONFIG_KUNIT_DEBUGFS),y) kunit-objs +=3D debugfs.o diff --git a/lib/kunit/device.c b/lib/kunit/device.c new file mode 100644 index 000000000000..1db4305b615a --- /dev/null +++ b/lib/kunit/device.c @@ -0,0 +1,181 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * KUnit-managed device implementation + * + * Implementation of struct kunit_device helpers for fake devices whose + * lifecycle is managed by KUnit. + * + * Copyright (C) 2023, Google LLC. + * Author: David Gow + */ + +#include + +#include +#include +#include + +#include "device-impl.h" + +/* Wrappers for use with kunit_add_action() */ +KUNIT_DEFINE_ACTION_WRAPPER(device_unregister_wrapper, device_unregister, = struct device *); +KUNIT_DEFINE_ACTION_WRAPPER(driver_unregister_wrapper, driver_unregister, = struct device_driver *); + +/* The root device for the KUnit bus, parent of all kunit_devices. */ +static struct device *kunit_bus_device; + +/* A device owned by a KUnit test. */ +struct kunit_device { + struct device dev; + /* The KUnit test which owns this device. */ + struct kunit *owner; + /* If the driver is managed by KUnit and unique to this device. */ + const struct device_driver *driver; +}; + +#define to_kunit_device(d) container_of_const(d, struct kunit_device, dev) + +static struct bus_type kunit_bus_type =3D { + .name =3D "kunit", +}; + +/* Register the 'kunit_bus' used for fake devices. */ +int kunit_bus_init(void) +{ + int error; + + kunit_bus_device =3D root_device_register("kunit"); + if (!kunit_bus_device) + return -ENOMEM; + + error =3D bus_register(&kunit_bus_type); + if (error) + bus_unregister(&kunit_bus_type); + return error; +} + +/* Release a 'fake' KUnit device. */ +static void kunit_device_release(struct device *d) +{ + kfree(to_kunit_device(d)); +} + +/** + * Create and register a KUnit-managed struct device_driver on the kunit_b= us. + * Returns an error pointer on failure. + */ +struct device_driver *kunit_driver_create(struct kunit *test, const char *= name) +{ + struct device_driver *driver; + int err =3D -ENOMEM; + + driver =3D kunit_kzalloc(test, sizeof(*driver), GFP_KERNEL); + + if (!driver) + return ERR_PTR(err); + + driver->name =3D name; + driver->bus =3D &kunit_bus_type; + driver->owner =3D THIS_MODULE; + + err =3D driver_register(driver); + if (err) { + kunit_kfree(test, driver); + return ERR_PTR(err); + } + + kunit_add_action(test, driver_unregister_wrapper, driver); + return driver; +} +EXPORT_SYMBOL_GPL(kunit_driver_create); + +/* Helper which creates a kunit_device, attaches it to the kunit_bus*/ +static struct kunit_device *kunit_device_register_internal(struct kunit *t= est, + const char *name, + const struct device_driver *drv) +{ + struct kunit_device *kunit_dev; + int err =3D -ENOMEM; + + kunit_dev =3D kzalloc(sizeof(*kunit_dev), GFP_KERNEL); + if (!kunit_dev) + return ERR_PTR(err); + + kunit_dev->owner =3D test; + + err =3D dev_set_name(&kunit_dev->dev, "%s.%s", test->name, name); + if (err) { + kfree(kunit_dev); + return ERR_PTR(err); + } + + kunit_dev->dev.release =3D kunit_device_release; + kunit_dev->dev.bus =3D &kunit_bus_type; + kunit_dev->dev.parent =3D kunit_bus_device; + + err =3D device_register(&kunit_dev->dev); + if (err) { + put_device(&kunit_dev->dev); + return ERR_PTR(err); + } + + kunit_add_action(test, device_unregister_wrapper, &kunit_dev->dev); + + return kunit_dev; +} + +/** + * Create and register a new KUnit-managed device, using the user-supplied= device_driver. + * On failure, returns an error pointer. + */ +struct device *kunit_device_register_with_driver(struct kunit *test, + const char *name, + const struct device_driver *drv) +{ + struct kunit_device *kunit_dev =3D kunit_device_register_internal(test, n= ame, drv); + + if (IS_ERR_OR_NULL(kunit_dev)) + return ERR_CAST(kunit_dev); + + return &kunit_dev->dev; +} +EXPORT_SYMBOL_GPL(kunit_device_register_with_driver); + +/** + * Create and register a new KUnit-managed device, including a matching de= vice_driver. + * On failure, returns an error pointer. + */ +struct device *kunit_device_register(struct kunit *test, const char *name) +{ + struct device_driver *drv; + struct kunit_device *dev; + + drv =3D kunit_driver_create(test, name); + if (IS_ERR(drv)) + return ERR_CAST(drv); + + dev =3D kunit_device_register_internal(test, name, drv); + if (IS_ERR(dev)) { + kunit_release_action(test, driver_unregister_wrapper, (void *)drv); + return ERR_CAST(dev); + } + + /* Request the driver be freed. */ + dev->driver =3D drv; + + + return &dev->dev; +} +EXPORT_SYMBOL_GPL(kunit_device_register); + +/* Unregisters a KUnit-managed device early (including the driver, if auto= matically created). */ +void kunit_device_unregister(struct kunit *test, struct device *dev) +{ + const struct device_driver *driver =3D to_kunit_device(dev)->driver; + + kunit_release_action(test, device_unregister_wrapper, dev); + if (driver) + kunit_release_action(test, driver_unregister_wrapper, (void *)driver); +} +EXPORT_SYMBOL_GPL(kunit_device_unregister); + diff --git a/lib/kunit/kunit-test.c b/lib/kunit/kunit-test.c index 3e9c5192d095..6c666fbdd6ad 100644 --- a/lib/kunit/kunit-test.c +++ b/lib/kunit/kunit-test.c @@ -8,6 +8,9 @@ #include #include =20 +#include +#include + #include "string-stream.h" #include "try-catch-impl.h" =20 @@ -687,6 +690,135 @@ static struct kunit_case kunit_current_test_cases[] = =3D { {} }; =20 +static void test_dev_action(void *priv) +{ + *(void **)priv =3D (void *)1; +} + +static void kunit_device_test(struct kunit *test) +{ + struct device *test_device; + long action_was_run =3D 0; + + test_device =3D kunit_device_register(test, "my_device"); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, test_device); + + // Add an action to verify cleanup. + devm_add_action(test_device, test_dev_action, &action_was_run); + + KUNIT_EXPECT_EQ(test, action_was_run, 0); + + kunit_device_unregister(test, test_device); + + KUNIT_EXPECT_EQ(test, action_was_run, 1); +} + +static void kunit_device_cleanup_test(struct kunit *test) +{ + struct device *test_device; + long action_was_run =3D 0; + + test_device =3D kunit_device_register(test, "my_device"); + KUNIT_ASSERT_NOT_NULL(test, test_device); + + /* Add an action to verify cleanup. */ + devm_add_action(test_device, test_dev_action, &action_was_run); + + KUNIT_EXPECT_EQ(test, action_was_run, 0); + + /* Force KUnit to run cleanup early. */ + kunit_cleanup(test); + + KUNIT_EXPECT_EQ(test, action_was_run, 1); +} + +struct driver_test_state { + bool driver_device_probed; + bool driver_device_removed; + long action_was_run; +}; + +static int driver_probe_hook(struct device *dev) +{ + struct kunit *test =3D kunit_get_current_test(); + struct driver_test_state *state =3D (struct driver_test_state *)test->pri= v; + + state->driver_device_probed =3D true; + return 0; +} + +static int driver_remove_hook(struct device *dev) +{ + struct kunit *test =3D kunit_get_current_test(); + struct driver_test_state *state =3D (struct driver_test_state *)test->pri= v; + + state->driver_device_removed =3D true; + return 0; +} + + +static void kunit_device_driver_test(struct kunit *test) +{ + struct device_driver *test_driver; + struct device *test_device; + struct driver_test_state test_state =3D {}; + + test->priv =3D &test_state; + test_driver =3D kunit_driver_create(test, "my_driver"); + + // This can fail with an error pointer. + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, test_driver); + + test_driver->probe =3D driver_probe_hook; + test_driver->remove =3D driver_remove_hook; + + test_device =3D kunit_device_register_with_driver(test, "my_device", test= _driver); + + // This can fail with an error pointer. + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, test_device); + + // Make sure the probe function was called. + KUNIT_ASSERT_TRUE(test, test_state.driver_device_probed); + + // Add an action to verify cleanup. + devm_add_action(test_device, test_dev_action, &test_state.action_was_run); + + KUNIT_EXPECT_EQ(test, test_state.action_was_run, 0); + + kunit_device_unregister(test, test_device); + test_device =3D NULL; + + // Make sure the remove hook was called. + KUNIT_ASSERT_TRUE(test, test_state.driver_device_removed); + + // We're going to test this again. + test_state.driver_device_probed =3D false; + + // The driver should not automatically be destroyed by + // kunit_device_unregister, so we can re-use it. + test_device =3D kunit_device_register_with_driver(test, "my_device", test= _driver); + + // This can fail with an error pointer. + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, test_device); + + // Probe was called again. + KUNIT_ASSERT_TRUE(test, test_state.driver_device_probed); + + // Everything is automatically freed here. +} + +static struct kunit_case kunit_device_test_cases[] =3D { + KUNIT_CASE(kunit_device_test), + KUNIT_CASE(kunit_device_cleanup_test), + KUNIT_CASE(kunit_device_driver_test), + {} +}; + +static struct kunit_suite kunit_device_test_suite =3D { + .name =3D "kunit_device", + .test_cases =3D kunit_device_test_cases, +}; + static struct kunit_suite kunit_current_test_suite =3D { .name =3D "kunit_current", .test_cases =3D kunit_current_test_cases, @@ -694,6 +826,6 @@ static struct kunit_suite kunit_current_test_suite =3D { =20 kunit_test_suites(&kunit_try_catch_test_suite, &kunit_resource_test_suite, &kunit_log_test_suite, &kunit_status_test_suite, - &kunit_current_test_suite); + &kunit_current_test_suite, &kunit_device_test_suite); =20 MODULE_LICENSE("GPL v2"); diff --git a/lib/kunit/test.c b/lib/kunit/test.c index 0308865194bb..c457593e4913 100644 --- a/lib/kunit/test.c +++ b/lib/kunit/test.c @@ -18,6 +18,7 @@ #include =20 #include "debugfs.h" +#include "device-impl.h" #include "hooks-impl.h" #include "string-stream.h" #include "try-catch-impl.h" @@ -840,6 +841,8 @@ static int __init kunit_init(void) kunit_install_hooks(); =20 kunit_debugfs_init(); + + kunit_bus_init(); #ifdef CONFIG_MODULES return register_module_notifier(&kunit_mod_nb); #else --=20 2.43.0.472.g3155946c3a-goog From nobody Tue Dec 16 16:41:22 2025 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id BA9EDC10DC1 for ; Fri, 8 Dec 2023 10:10:14 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1573577AbjLHKKF (ORCPT ); Fri, 8 Dec 2023 05:10:05 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54782 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1573561AbjLHKJw (ORCPT ); Fri, 8 Dec 2023 05:09:52 -0500 Received: from mail-yb1-xb49.google.com (mail-yb1-xb49.google.com [IPv6:2607:f8b0:4864:20::b49]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B1B9E1738 for ; Fri, 8 Dec 2023 02:09:57 -0800 (PST) Received: by mail-yb1-xb49.google.com with SMTP id 3f1490d57ef6-dbc507311d7so739649276.1 for ; Fri, 08 Dec 2023 02:09:57 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1702030197; x=1702634997; 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=vJ3XYd4VAh0rMwZdPZnf0/R4/jXxDQ1RiH7pfcHiYc8=; b=VDr9tcj4StnA6acjRdrtszhpigA+LyEHfIwbdwp5vEDSLyQ2c06X0f1qayIhI8+EAM r6FhkDaaKJDAFG2WPAqaLSFgFJYgJTQmwSinohvw6fX9JU05gn1piF4HSMp1QyjuD+hc LlgfLm9xm/qiGoZ+JYDrryT6EFNdLOCNWFYWUNJJIkjz86006ehBfhgbvosPV1O4WBPU ABaqxj2OzKfF8pO28M7kbucpENFfkZVMQ+R/GvWK0j/oeVPrTz99IIeQ5nRoaxIhWa7G qQ5dED3iTrJbm5hEMRwp3R0L563dY3XUizJ0DaECRNx5LvnFnzhjznIrwqajOL4Tp+9H x8eA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702030197; x=1702634997; 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=vJ3XYd4VAh0rMwZdPZnf0/R4/jXxDQ1RiH7pfcHiYc8=; b=Yuzryeau+HcREo/Ar33fi+xZGq6o06clKp0ZUiQoJh7HadbvGsLJ1z6sOl3gWu5Q3E WYUzusAO7OdQ2P5Ba655labAV/itMCKPeLKu7tY5+ze0JsTOmXC/Itd26SClQE2MtVs8 pSpz7dtX5jcC167Ut5AvnXg6t30lGGrp9FKGOE26c2gSBVwPN649yMDY8Dv4ZsxJT5sE mxeCD/bJz2pZf2BJ0OKnWU7+7CXq0Wk0gR++ENEkBICDwlsHlVwaRE7uFxnTmZwOZUM9 UgyTa+ZLJ3dx3lg8a/xfs9OheXxepVh/gl+M2MX8/ffxftnPkiM5OfaPt11dGCszaj1N BAvQ== X-Gm-Message-State: AOJu0YxIu693MvYRGLWmmOt9GYU/5d9db4WA7KxL9MAsg/pvDrQbhZew DCUHEGHkbtd0tA6bEBdMAAlnEry8GoD7hg== X-Google-Smtp-Source: AGHT+IEoLvJsRYtYBtpiMYYBRwRZCWLln+T0Te5bpJ5ZhKJL6iGz1n9WIFhdhIvRsrqNCPNn+H/9E0PqoIsEoA== X-Received: from slicestar.c.googlers.com ([fda3:e722:ac3:cc00:4f:4b78:c0a8:20a1]) (user=davidgow job=sendgmr) by 2002:a25:fc12:0:b0:dbc:1b65:fd8c with SMTP id v18-20020a25fc12000000b00dbc1b65fd8cmr43469ybd.5.1702030196750; Fri, 08 Dec 2023 02:09:56 -0800 (PST) Date: Fri, 08 Dec 2023 18:09:30 +0800 In-Reply-To: <20231208-kunit_bus-v2-0-e95905d9b325@google.com> Mime-Version: 1.0 References: <20231208-kunit_bus-v2-0-e95905d9b325@google.com> X-Mailer: b4 0.13-dev-099c9 Message-ID: <20231208-kunit_bus-v2-2-e95905d9b325@google.com> Subject: [PATCH v2 2/4] fortify: test: Use kunit_device From: davidgow@google.com To: Rae Moar , Brendan Higgins , Greg Kroah-Hartman , Matti Vaittinen , Stephen Boyd , Shuah Khan , Jonathan Corbet , Kees Cook , Liam Girdwood , Mark Brown , Jaroslav Kysela , Takashi Iwai , Maxime Ripard Cc: linux-kselftest@vger.kernel.org, kunit-dev@googlegroups.com, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-hardening@vger.kernel.org, linux-sound@vger.kernel.org, David Gow Content-Type: text/plain; charset="utf-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Using struct root_device to create fake devices for tests is something of a hack. The new struct kunit_device is meant for this purpose, so use it instead. Reviewed-by: Matti Vaittinen Acked-by: Kees Cook Signed-off-by: David Gow --- lib/fortify_kunit.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/fortify_kunit.c b/lib/fortify_kunit.c index c8c33cbaae9e..2e4fedc81621 100644 --- a/lib/fortify_kunit.c +++ b/lib/fortify_kunit.c @@ -15,6 +15,7 @@ */ #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt =20 +#include #include #include #include @@ -269,7 +270,7 @@ DEFINE_ALLOC_SIZE_TEST_PAIR(kvmalloc) size_t len; \ \ /* Create dummy device for devm_kmalloc()-family tests. */ \ - dev =3D root_device_register(dev_name); \ + dev =3D kunit_device_register(test, dev_name); \ KUNIT_ASSERT_FALSE_MSG(test, IS_ERR(dev), \ "Cannot register test device\n"); \ \ @@ -303,7 +304,7 @@ DEFINE_ALLOC_SIZE_TEST_PAIR(kvmalloc) checker(len, devm_kmemdup(dev, "Ohai", len, gfp), \ devm_kfree(dev, p)); \ \ - device_unregister(dev); \ + kunit_device_unregister(test, dev); \ } while (0) DEFINE_ALLOC_SIZE_TEST_PAIR(devm_kmalloc) =20 --=20 2.43.0.472.g3155946c3a-goog From nobody Tue Dec 16 16:41:22 2025 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 4882EC10DCE for ; Fri, 8 Dec 2023 10:10:19 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1573583AbjLHKKK (ORCPT ); Fri, 8 Dec 2023 05:10:10 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54846 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1573558AbjLHKJ4 (ORCPT ); Fri, 8 Dec 2023 05:09:56 -0500 Received: from mail-yw1-x1149.google.com (mail-yw1-x1149.google.com [IPv6:2607:f8b0:4864:20::1149]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3D62D1720 for ; Fri, 8 Dec 2023 02:10:02 -0800 (PST) Received: by mail-yw1-x1149.google.com with SMTP id 00721157ae682-5ca26c07848so24805347b3.0 for ; Fri, 08 Dec 2023 02:10:02 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1702030201; x=1702635001; 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=NkRyaEIclzm+ShIMyVUbqREgKKajNjynsQ0mobEpXzk=; b=Ia4PYyztigFmTktxZsK+SONpo6VUAt6QnxvhqOpLXF33toW7VSD3bVOBWvnjBHTItz 1BiqSNITOd2BrYnyFCj+/YrVNw2lxIHD3FphQaJB2jhaf0Fa18PtGV7e70TwLAwuho+e 9y6HwVXMdCuCn+zRNlHmqWpEuJEDComnl23WQvbPM/Zu5x7Dyhue5mPGDd3xfa9MG4/V 40wNVnsHkktXzmXNVCaLBWL5IN3/3YcN69mbdi93FklJ52rnrxTijOmZJc2q7RFXEir3 kexo8LGk8Ub+r3m7fg8+n861bs3PUZH8aSiVLqBhgd9lCIpX1uu4vbM/owGhXb8AjKIM KsqQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702030201; x=1702635001; 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=NkRyaEIclzm+ShIMyVUbqREgKKajNjynsQ0mobEpXzk=; b=CxNhpK/r215V7ExOh6ljO/y84bnD3I6ww9manfjxFl56YSc/xIKB6JHwq08rROGYA2 DBIB2Rnz9EyLE3dKWANOmj/rQIBjFGrebH9QMMR0jrU4ru4MqmYPsE+9UXyxPUs51nkZ EemfY3lpspUirFIkNbilTqYpsAb1L25TZ+rRQPcaBzKusbCZbhsBtNwhctqk6+ViMQRa F2forS7yMgPiBJ8Ujyz5PyMiKgHF0fLKquUZQcbkgrmr6lzRhWW11VJGLcVuelAsQ1o6 PBm0+6cPaa4Rybw5CW9yA82qRRdQjDehsV6BSl3wx6jGAQXiE7e1MlIxaQaalMmTttCt yMVQ== X-Gm-Message-State: AOJu0YxlkdHbnEgbwZucMqrd9/1YmEjl1hvm7feGTKB5y+gnyNgR7MxX mP+NCBETyzXgXTxA+B2Ykx9Ny7GBm4ECvg== X-Google-Smtp-Source: AGHT+IHGF3yZf9WJj2lWCzBA+Zp1ww/Jd0TaRJ+ZbfD0tlPqmjYoZLAo7qIUoj8IifBSVJ/CH0LnfRQUmCKMLQ== X-Received: from slicestar.c.googlers.com ([fda3:e722:ac3:cc00:4f:4b78:c0a8:20a1]) (user=davidgow job=sendgmr) by 2002:a05:690c:3146:b0:5d7:4619:82dd with SMTP id fc6-20020a05690c314600b005d7461982ddmr53175ywb.6.1702030201444; Fri, 08 Dec 2023 02:10:01 -0800 (PST) Date: Fri, 08 Dec 2023 18:09:31 +0800 In-Reply-To: <20231208-kunit_bus-v2-0-e95905d9b325@google.com> Mime-Version: 1.0 References: <20231208-kunit_bus-v2-0-e95905d9b325@google.com> X-Mailer: b4 0.13-dev-099c9 Message-ID: <20231208-kunit_bus-v2-3-e95905d9b325@google.com> Subject: [PATCH v2 3/4] overflow: Replace fake root_device with kunit_device From: davidgow@google.com To: Rae Moar , Brendan Higgins , Greg Kroah-Hartman , Matti Vaittinen , Stephen Boyd , Shuah Khan , Jonathan Corbet , Kees Cook , Liam Girdwood , Mark Brown , Jaroslav Kysela , Takashi Iwai , Maxime Ripard Cc: linux-kselftest@vger.kernel.org, kunit-dev@googlegroups.com, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-hardening@vger.kernel.org, linux-sound@vger.kernel.org, David Gow Content-Type: text/plain; charset="utf-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Using struct root_device to create fake devices for tests is something of a hack. The new struct kunit_device is meant for this purpose, so use it instead. Reviewed-by: Matti Vaittinen Signed-off-by: David Gow --- lib/overflow_kunit.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/lib/overflow_kunit.c b/lib/overflow_kunit.c index 34db0b3aa502..c527f6b75789 100644 --- a/lib/overflow_kunit.c +++ b/lib/overflow_kunit.c @@ -6,6 +6,7 @@ */ #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt =20 +#include #include #include #include @@ -618,7 +619,7 @@ static void overflow_allocation_test(struct kunit *test) } while (0) =20 /* Create dummy device for devm_kmalloc()-family tests. */ - dev =3D root_device_register(device_name); + dev =3D kunit_device_register(test, device_name); KUNIT_ASSERT_FALSE_MSG(test, IS_ERR(dev), "Cannot register test device\n"); =20 @@ -634,8 +635,6 @@ static void overflow_allocation_test(struct kunit *test) check_allocation_overflow(devm_kmalloc); check_allocation_overflow(devm_kzalloc); =20 - device_unregister(dev); - kunit_info(test, "%d allocation overflow tests finished\n", count); #undef check_allocation_overflow } --=20 2.43.0.472.g3155946c3a-goog From nobody Tue Dec 16 16:41:22 2025 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 86025C4167B for ; Fri, 8 Dec 2023 10:10:24 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1573602AbjLHKKP (ORCPT ); Fri, 8 Dec 2023 05:10:15 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41928 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1573570AbjLHKKC (ORCPT ); Fri, 8 Dec 2023 05:10:02 -0500 Received: from mail-yw1-x114a.google.com (mail-yw1-x114a.google.com [IPv6:2607:f8b0:4864:20::114a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9C5631727 for ; Fri, 8 Dec 2023 02:10:07 -0800 (PST) Received: by mail-yw1-x114a.google.com with SMTP id 00721157ae682-5d1ed4b268dso23673137b3.0 for ; Fri, 08 Dec 2023 02:10:07 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1702030206; x=1702635006; 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=d4MXXH5cbPxzFQMx+eKCBMLxcs0KvqjK2kPkW0nLFkA=; b=N3QOUT7B/2U9w0TIVO9Yaxwm3671HaMgZ5oIpgtryVcoN3WkcNoYKTtnOI/8Yxc/qS U47kE6h/WP1McWitCqImdtG/mllGttk+w1GHfaqTqVTI/VC8E7GNlYbhFkGSYX0/lwDR 3GA8FyMeONKItTIF73tIWVsYTL2bNQYW4bf5NNbvypkq5vjMbT9N1OYZWQzRi2EJTUGl RDW3qwuYb/D3PuCaWMpsXbAO78OkAFZVk80ygFkvRLUnRKgoaCJnv6o4e1rRIT8ylUDw 3A5mR86qCWGY9JMuRW0jWkdnGp+nO0j78t0YjclCV3zIZSilVfCEEaKQoffj4qlmBWC7 J/lw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702030206; x=1702635006; 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=d4MXXH5cbPxzFQMx+eKCBMLxcs0KvqjK2kPkW0nLFkA=; b=oAgmFVhiahg3exvp62EtGlHBHFFL2beLVJdXlcUEG/DZbx6Io6UP9F0+FREscFwL0V ObnDJd27HxIzs0qYRgW6lbJa69YZg9+ENC/HybiO2BY28UUzVSTLZMomZ7EhXWA74eAY x08t2xkO3QRXjPBVmtevNWyVLVh8SV2mZcLxUqMpMyX6h216rYLpBJd7udcNt2EhtGde P5wKSGWGG/WZ9teqf63jw2sINwf2707uAe9CQEEQUdoH7Ew1zGX38AZw1Z7gi7t3OOWp m1tqraf4Rig2k7F9g0ETdVxg3+pFafyIPXohUXCaWm0KPmO6s1v8bS67ivRCJtlna1mO ZCsQ== X-Gm-Message-State: AOJu0YwuC+9f8gey9uc3NrWheMtSmqvhUp57C6MzXyzqWxBGGx8trX0n Fg/OHVA2tFQN34zfe6havz7T9g6gVj0D8A== X-Google-Smtp-Source: AGHT+IGRCKchlwDieJP65LzkIospb19LMxwfhEMGz5PddR5fY3t/b8MjzABJjvWV9MENqxn3YYZ6m3IfVjrfog== X-Received: from slicestar.c.googlers.com ([fda3:e722:ac3:cc00:4f:4b78:c0a8:20a1]) (user=davidgow job=sendgmr) by 2002:a81:be18:0:b0:5d4:27ab:83c with SMTP id i24-20020a81be18000000b005d427ab083cmr58978ywn.1.1702030206337; Fri, 08 Dec 2023 02:10:06 -0800 (PST) Date: Fri, 08 Dec 2023 18:09:32 +0800 In-Reply-To: <20231208-kunit_bus-v2-0-e95905d9b325@google.com> Mime-Version: 1.0 References: <20231208-kunit_bus-v2-0-e95905d9b325@google.com> X-Mailer: b4 0.13-dev-099c9 Message-ID: <20231208-kunit_bus-v2-4-e95905d9b325@google.com> Subject: [PATCH v2 4/4] ASoC: topology: Replace fake root_device with kunit_device in tests From: davidgow@google.com To: Rae Moar , Brendan Higgins , Greg Kroah-Hartman , Matti Vaittinen , Stephen Boyd , Shuah Khan , Jonathan Corbet , Kees Cook , Liam Girdwood , Mark Brown , Jaroslav Kysela , Takashi Iwai , Maxime Ripard Cc: linux-kselftest@vger.kernel.org, kunit-dev@googlegroups.com, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-hardening@vger.kernel.org, linux-sound@vger.kernel.org, David Gow Content-Type: text/plain; charset="utf-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Using struct root_device to create fake devices for tests is something of a hack. The new struct kunit_device is meant for this purpose, so use it instead. Acked-by: Mark Brown Signed-off-by: David Gow --- sound/soc/soc-topology-test.c | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/sound/soc/soc-topology-test.c b/sound/soc/soc-topology-test.c index 2cd3540cec04..70cbccc42a42 100644 --- a/sound/soc/soc-topology-test.c +++ b/sound/soc/soc-topology-test.c @@ -9,6 +9,7 @@ #include #include #include +#include #include =20 /* =3D=3D=3D=3D=3D HELPER FUNCTIONS =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D */ @@ -21,26 +22,19 @@ */ static struct device *test_dev; =20 -static struct device_driver test_drv =3D { - .name =3D "sound-soc-topology-test-driver", -}; - static int snd_soc_tplg_test_init(struct kunit *test) { - test_dev =3D root_device_register("sound-soc-topology-test"); + test_dev =3D kunit_device_register(test, "sound-soc-topology-test"); test_dev =3D get_device(test_dev); if (!test_dev) return -ENODEV; =20 - test_dev->driver =3D &test_drv; - return 0; } =20 static void snd_soc_tplg_test_exit(struct kunit *test) { put_device(test_dev); - root_device_unregister(test_dev); } =20 /* --=20 2.43.0.472.g3155946c3a-goog