From nobody Tue Dec 16 16:39:01 2025 Received: from mail-yw1-f201.google.com (mail-yw1-f201.google.com [209.85.128.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 4FDAE1094D for ; Fri, 15 Dec 2023 07:39:19 +0000 (UTC) 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="xSHyjyKF" Received: by mail-yw1-f201.google.com with SMTP id 00721157ae682-5e1e41fd6a6so3578927b3.1 for ; Thu, 14 Dec 2023 23:39:19 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1702625958; x=1703230758; 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=e9PX0PXS1f3f3pBJoG7zI47BnYzw6Hhl3FOBvIaiIQI=; b=xSHyjyKFOY54NFxMuh/vA1zGnWRnU+lPnXUEJueRyN7hUJNxbcZETOKMVb8t8AggCP oDH0UtCrqBTNMffGu2IvqE/AjWdQqtJVbq8Ab35lXXfE572wHY7An59XZBO0W7z/NTRf TEW+4xenQRklCySPCv6jI5P6QqGsfr+Ayo82LF3IOCBdAfxeKtBh6vJQhcVGoyt28Kvo skwpSX/IUajm7P0JSlzc+o48uX8HN0eppuUW6ATi17+GA9WRS5mTrEeRz+3d1wFZScUj RKYy2n3Tr2hpA3Pt9npItky4uk4QUrr0sTIiYQHfST7A7M6mDriogxbrx0bzLk1Y1bRz 6XuA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702625958; x=1703230758; 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=e9PX0PXS1f3f3pBJoG7zI47BnYzw6Hhl3FOBvIaiIQI=; b=isofTIO7J4Lh+ssVzEKgh561O/+xANwl/8bkZvZjgjOVi82o/UnFdV7p4hzfWpYQGb 5Y+ElXSQAIR3nKARKAWjMgo6khLFouIGFhundNbNepWYe7djBvA1ZAC0zqIFr7dQGmXu B97KM7jc2UlY1lU2irPp5UenZJEX0lyXJV0tixjTljlcSTqotc4PNco3h0eZz0SAE7IM RrdrBmucQG50PfJ/UGfiFgWznBZQqxf/u8ESKW9Tqo4nZxGLT4C/4244wwW6qMymwRYC DbwFRG1cTNjK92uDkUMuo3/LTtGtXxizmLbqzefXeljNpqvHrjbyax7rAIX0cNPsagVQ bLZg== X-Gm-Message-State: AOJu0YzvB0GxNQZOEbGJgZCME82YGfY+QAMUyJPYIuBYWoApRlM1s9KA e29TfSLfFl73RuUiwACeab3EOmh1i/Qx5Q== X-Google-Smtp-Source: AGHT+IELCGlN0uE2BwyC5++X13nPxgxRisKXnkwBl2Q3oFlqXcqDxcAfNp7jpaTPNFQ65Tbho4MH0XvAMsFyEA== X-Received: from slicestar.c.googlers.com ([fda3:e722:ac3:cc00:4f:4b78:c0a8:20a1]) (user=davidgow job=sendgmr) by 2002:a81:fe03:0:b0:5e3:51d3:3b5 with SMTP id j3-20020a81fe03000000b005e351d303b5mr39117ywn.7.1702625958321; Thu, 14 Dec 2023 23:39:18 -0800 (PST) Date: Fri, 15 Dec 2023 15:39:08 +0800 In-Reply-To: <20231215-kunit_bus-v4-0-4f5160e2f95e@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20231215-kunit_bus-v4-0-4f5160e2f95e@google.com> X-Mailer: b4 0.13-dev-099c9 Message-ID: <20231215-kunit_bus-v4-1-4f5160e2f95e@google.com> Subject: [PATCH v4 1/5] 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 , Maxime Ripard Content-Type: text/plain; charset="utf-8" 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 Reviewed-by: Maxime Ripard Signed-off-by: David Gow Reviewed-by: Greg Kroah-Hartman --- 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-impl.h | 17 +++ lib/kunit/device.c | 181 +++++++++++++++++++++= ++++ lib/kunit/kunit-test.c | 134 +++++++++++++++++- lib/kunit/test.c | 3 + 8 files changed, 475 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-impl.h b/lib/kunit/device-impl.h new file mode 100644 index 000000000000..54bd55836405 --- /dev/null +++ b/lib/kunit/device-impl.h @@ -0,0 +1,17 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* + * KUnit internal header for device helpers + * + * Header for KUnit-internal driver / bus management. + * + * Copyright (C) 2023, Google LLC. + * Author: David Gow + */ + +#ifndef _KUNIT_DEVICE_IMPL_H +#define _KUNIT_DEVICE_IMPL_H + +// For internal use only -- registers the kunit_bus. +int kunit_bus_init(void); + +#endif //_KUNIT_DEVICE_IMPL_H 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 ee6927c60979..c4259d910356 100644 --- a/lib/kunit/kunit-test.c +++ b/lib/kunit/kunit-test.c @@ -5,9 +5,13 @@ * Copyright (C) 2019, Google LLC. * Author: Brendan Higgins */ +#include "linux/gfp_types.h" #include #include =20 +#include +#include + #include "string-stream.h" #include "try-catch-impl.h" =20 @@ -687,6 +691,134 @@ 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 kunit_kzalloc(test, sizeof(*test= _state), GFP_KERNEL); + + 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:39:01 2025 Received: from mail-yw1-f201.google.com (mail-yw1-f201.google.com [209.85.128.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 13363D52D for ; Fri, 15 Dec 2023 07:39:23 +0000 (UTC) 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="4M406bEs" Received: by mail-yw1-f201.google.com with SMTP id 00721157ae682-5d1b2153ba1so2238467b3.2 for ; Thu, 14 Dec 2023 23:39:23 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1702625963; x=1703230763; 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=4M406bEsdMF6O3NRYBNV0wD3FBpWItAMUWZdgyj29cY5Bsaecy5YDs0h2B8ApR8Uz9 8XuPdszBhfvNpgEEWWDE6df7Z7zMRWgPOAZNjcd0xZeQkoxRczhnXOT5lxkSqXkP1uhY FC3n6HG6YnzcuiSyL6rxUiouLm7ywgAkZRroHB7aggZ4RdM76a/vy8tEGPn5cLToLoi2 pz6c4hcAJq6Y0soTQS5Y72IRGWW/PNlHlfhGByc5xfjrydePSep2ecgVVrMiJnI1hAbl Iq4Mi3JwVzP56tcPtYd6B+M8PPKiTsJW8EkyTUZ28Q2yE3WYsZc4S3oJXiRl7+HQZB/g CKhA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702625963; x=1703230763; 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=pHIfIY++3xPoIxl6M+HOyYeznZVvfOtYRS3xUWN1NFrzyL29nff7f9/344zOgEiC9m gBoNJx7GifpZqK/UHo0K3BAuTFIqT6Ilzco6wUHB6XycWDbGSvyeQkQItEL5y3dBW6jK PsX9RzhxiEW00ABUGjpIzSmayM+D7DFdm7A8/wNSifyfxaGl83fDOjT1lWWf6HVVx/20 nYe3S/8kRIVNM6P92vI+kPtEczRIU33J/ISPuGCcxwOaUdCu+G7cUdKHTGunMfD1m0YP VpWPk68pW1KWgTyGzK70uOM181FFr+HiK3aXZTot7RbFZ2dlLv/ol+uDXAdGUGJDAuaU WKXA== X-Gm-Message-State: AOJu0YxJMjyhoVrtNmw1J6uNbmnjLEkEBUOuGy/lhh4cpXfsAWvrykLj iPRQZzHSyIRVzqYi+/EMTpyHklf6ZdfkAw== X-Google-Smtp-Source: AGHT+IESolPUWUjcu8ehVBMVbwsHg0hbt8xH+da+fFZstOFajYilu1HiXZJcR+VkbSIMMfIUvRBPM/Nns0DS4A== X-Received: from slicestar.c.googlers.com ([fda3:e722:ac3:cc00:4f:4b78:c0a8:20a1]) (user=davidgow job=sendgmr) by 2002:a05:690c:3604:b0:5e3:d2af:8b78 with SMTP id ft4-20020a05690c360400b005e3d2af8b78mr27176ywb.3.1702625962892; Thu, 14 Dec 2023 23:39:22 -0800 (PST) Date: Fri, 15 Dec 2023 15:39:09 +0800 In-Reply-To: <20231215-kunit_bus-v4-0-4f5160e2f95e@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20231215-kunit_bus-v4-0-4f5160e2f95e@google.com> X-Mailer: b4 0.13-dev-099c9 Message-ID: <20231215-kunit_bus-v4-2-4f5160e2f95e@google.com> Subject: [PATCH v4 2/5] 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" 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:39:01 2025 Received: from mail-yw1-f202.google.com (mail-yw1-f202.google.com [209.85.128.202]) (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 AF8B912E49 for ; Fri, 15 Dec 2023 07:39:28 +0000 (UTC) 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="HCn6D14G" Received: by mail-yw1-f202.google.com with SMTP id 00721157ae682-5d10f5bf5d9so2224537b3.3 for ; Thu, 14 Dec 2023 23:39:28 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1702625967; x=1703230767; 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=3Ew0yjzotVMz+DbsDGXsO7ZBvpe+EragVhaS0DRk4H8=; b=HCn6D14GDwVWkuY/LrrVskDkkUc9yj1KPyam/75XIX93f1zZa9Lz66yZUtY001wbTA kSyIzS9A1q2LY3AibGWWaExvrEtOy2XVAc8xLm73sNe0+/R9DTPLs5ZFnKW2MtVTf76D vxKEaGxl3urJKN8A1XZwEnK0OKJ/ZMu2muSOgTM26htxcMf34Go9GgDcfk1zA6BYWjgL Yn0b40ybIiDQ6W8R8G3PE3VVTVK+7ePcj3fsxbCS9JYXVLKfBwd7nQvDVJEFMUMb6SPH kVUjginkWgoqMAyr16CkoXvuekMsuYDXqAs3BGgRuvErG4z5D3NbAlhC/Ho74OIIy7UP 61jQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702625967; x=1703230767; 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=3Ew0yjzotVMz+DbsDGXsO7ZBvpe+EragVhaS0DRk4H8=; b=Qtr3G98kzYVv/8ml6Lbdq/GCtLzwPvLq1r2ya3aqRZ/nW3jkx9w7GA+BwlnMJSaRJG Fo4qFlko91xUcUojo4lYNBMUivAcS+AGArmpiflg0tNkDenz87+HfXgtVEdYG1EbFFRa 5cy+yizrzFfNennC4iKVGJkOYkyKPTNSpAtjqSbYKl1DiPJwonN4YveM7aaHU+0lEPVE JNyv4SDgzljClUwpDUHkga3BEsSGavv0/UYl60E5HnSn4XLjOYNMAKSf4QfwdU2nZoF8 ylPTDJllueXr82dfKVcMAP3Iym9v7pn1FVPOGFIrM0HfjppKnURFQpqYz92bMVU2KesX kBDQ== X-Gm-Message-State: AOJu0YzCAl8Tm3qErmiMsJr2PBMwkOdxIbucwTAYNR/Y+67wHr7U6fch d1nnoOPIeC2pGEtI5F4tst1sQZTHvHr5rw== X-Google-Smtp-Source: AGHT+IEdNCTRzTrVPHds1vKNLGSiHnYDt78/zYUDSFVgM1wliEJevL7Fy5dUxym2mGK1jqN4jajZwpgyejAjIw== X-Received: from slicestar.c.googlers.com ([fda3:e722:ac3:cc00:4f:4b78:c0a8:20a1]) (user=davidgow job=sendgmr) by 2002:a05:690c:b91:b0:5d6:cb62:4793 with SMTP id ck17-20020a05690c0b9100b005d6cb624793mr130837ywb.0.1702625967709; Thu, 14 Dec 2023 23:39:27 -0800 (PST) Date: Fri, 15 Dec 2023 15:39:10 +0800 In-Reply-To: <20231215-kunit_bus-v4-0-4f5160e2f95e@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20231215-kunit_bus-v4-0-4f5160e2f95e@google.com> X-Mailer: b4 0.13-dev-099c9 Message-ID: <20231215-kunit_bus-v4-3-4f5160e2f95e@google.com> Subject: [PATCH v4 3/5] 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" 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/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:39:01 2025 Received: from mail-yw1-f201.google.com (mail-yw1-f201.google.com [209.85.128.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 71C3313AE5 for ; Fri, 15 Dec 2023 07:39:33 +0000 (UTC) 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="G0lx2l5I" Received: by mail-yw1-f201.google.com with SMTP id 00721157ae682-5d1b2153ba1so2238987b3.2 for ; Thu, 14 Dec 2023 23:39:33 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1702625972; x=1703230772; 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=G0lx2l5I+vav3thN3cKmD1WWSs6NF6uQsEFdCIAH7M3r0whgeoX1BdfWBCcFBIg8+q xJzQPxnh05VFerTIyBUweR48mVScGreqo6WDxM5INYStE7T2Y0SdupOba1AkSMKpTcE+ oLdp3cg0yZLyDmyRLP5J/3WCUkFE1cRbAmI040dr7WOwn0xtzqponnIzdVLIQ3a05xrZ I77xdlKl0CO4+KPcdkR33lnEEEzCZ44LQKjmcyw+Bft8K7tg1De6NOhpl4c4W+SXNxpC IKdxyaBDMivHmwaI5XgX9wfcqHFkjyptHj8Ju3kepZvEeNWzVTIT8J+Y+09DYdjiXiyX cTqQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702625972; x=1703230772; 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=qHLiIX4SmIa93uMhNHmfkQtHyNKC6/QMjqRzqzw6sZF4wbkrf05ElAnl/NyWWGBUeA 6wLhpmwYS/FkRP9N39Td7bpf0dRRqOK1l0PYim9JV4e8P/+M71M7BkBq6l1uoYQYIumI tdmz26zY7X3/iFQBna13b0+jJUYMXqw3MlWe8Q5AIgEwdoJuTKvs/1gmc5fLc8zk2wnz SCJpZwQX13umU5Imx1O/FcP2JtbJaJEnmtMXA6t7fb7k90/eQCsoNzbMpIE60yo3nEZi VH6N/HpzkRcIY1mh9MWqXNivXgl5dC54D4VW+fUg2i7IV63ufy2HrlWq7uOLiL/3Un9M nvRg== X-Gm-Message-State: AOJu0Yx7uL+T3P3DhdOLM6JSElb8Rt5eAzBzedlxk4bILcOQ6/cenaG/ UTwoirK563IQqPT0G+7VVMmbsVRtn76vJw== X-Google-Smtp-Source: AGHT+IHFhhWMR/kJRqTx6WHoW1Cw50Apf42JCOhF6MR/ULXu4wBRUQj91kJR8wij5MUTOYH+2REhYsHY7wVTEg== X-Received: from slicestar.c.googlers.com ([fda3:e722:ac3:cc00:4f:4b78:c0a8:20a1]) (user=davidgow job=sendgmr) by 2002:a25:ab8c:0:b0:dbc:c82d:7f0a with SMTP id v12-20020a25ab8c000000b00dbcc82d7f0amr53140ybi.9.1702625972472; Thu, 14 Dec 2023 23:39:32 -0800 (PST) Date: Fri, 15 Dec 2023 15:39:11 +0800 In-Reply-To: <20231215-kunit_bus-v4-0-4f5160e2f95e@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20231215-kunit_bus-v4-0-4f5160e2f95e@google.com> X-Mailer: b4 0.13-dev-099c9 Message-ID: <20231215-kunit_bus-v4-4-4f5160e2f95e@google.com> Subject: [PATCH v4 4/5] 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" 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 From nobody Tue Dec 16 16:39:01 2025 Received: from mail-yw1-f202.google.com (mail-yw1-f202.google.com [209.85.128.202]) (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 F1F2215AC1 for ; Fri, 15 Dec 2023 07:39:37 +0000 (UTC) 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="YWaT0kCC" Received: by mail-yw1-f202.google.com with SMTP id 00721157ae682-5d10f5bf5d9so2225057b3.3 for ; Thu, 14 Dec 2023 23:39:37 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1702625977; x=1703230777; 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=DEdD2ZFpDj7jGQ6MNzamPllkdSHgzBRafHvZdTTxiEM=; b=YWaT0kCCSY2z+sO8smD4w8l2PGHMn86TtY3Td6m8YY3iVeR9q6aRwoVGL3Awm+5jmM 5s0Sl/nD7k92EuuWBQhfDhzPykdqmZXQF1LsHMZwKHIRqtyqS9ucDxGwhZRDCtQTePbI qeT2aiG3fGm54Vn9w/ZIBqCRM2scNyMxNTZHWERaJq38LrbnUmjKkWYoIVM1ZeXBODoo nht4AVQZLxpZ7y2NAQicw4mQ7aPkO8SF73Js5tDTiNV7/R7XV5ATbKW8wPTsIzwSBVD3 AU7vUTDjSR7cMWzu0XL3yGKsGimpsOKcmve6n83hGIX0KOfe5vm3q7ieXYOQcBsmK2lP NT/g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702625977; x=1703230777; 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=DEdD2ZFpDj7jGQ6MNzamPllkdSHgzBRafHvZdTTxiEM=; b=ucxVRKfiktsmXQGYPLT2+tP2dfvFf1yOB+5wi4luvStfhJQijUhyheFEk0AuiPdVIn O5rBu/3aQq8wXdHf6Mx5L3N/BcWl+P34OFuoIW/iTMgoJH2JPGJmep+iTUnG+AaLNpK3 2PEJDrfFFENi5eSXv/GhEXhWnN9jkXRL4qpxW26pBNgtycFi3LPI4qOwiNpTuL8VIKui hoXYUAKT2IUjLQWBaY3cg6IhU7MYDvyGD/bkAj94DekcnO3PspB4ewdBZipyILkPHvR/ qCxoLteEx3YlCEiVgTl2E7T/Dee+9RM6JzYyXGOO+CtE5PJX0zRYme6f/JjO8xOGqrnO trcw== X-Gm-Message-State: AOJu0Yyv3mVYyuKWIWnhAFh1H0k1LKYW44GACXSjrmd6GNZHOehrlzO2 8OL2dGfMjunzTO/oPhMRV6+p7pQDbnmxug== X-Google-Smtp-Source: AGHT+IGuliGc1x7ZG0hmVGtlZI8HEIuCYqjmpfGoxBzPzTU8nz0wyUB0pw1fP4D1TpgUQwqGYAf5edyyW5sP3A== X-Received: from slicestar.c.googlers.com ([fda3:e722:ac3:cc00:4f:4b78:c0a8:20a1]) (user=davidgow job=sendgmr) by 2002:a05:690c:e0b:b0:5d3:b449:e58e with SMTP id cp11-20020a05690c0e0b00b005d3b449e58emr127478ywb.6.1702625977034; Thu, 14 Dec 2023 23:39:37 -0800 (PST) Date: Fri, 15 Dec 2023 15:39:12 +0800 In-Reply-To: <20231215-kunit_bus-v4-0-4f5160e2f95e@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20231215-kunit_bus-v4-0-4f5160e2f95e@google.com> X-Mailer: b4 0.13-dev-099c9 Message-ID: <20231215-kunit_bus-v4-5-4f5160e2f95e@google.com> Subject: [PATCH v4 5/5] drm/tests: Switch to kunit 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 , Maxime Ripard Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable From: Maxime Ripard Kunit recently gained helpers to create test managed devices. This means that we no longer have to roll our own helpers in KMS and we can reuse them. Signed-off-by: Maxime Ripard Tested-by: David Gow Signed-off-by: David Gow Reviewed-by: Rae Moar --- drivers/gpu/drm/tests/drm_kunit_helpers.c | 66 ++-------------------------= ---- 1 file changed, 3 insertions(+), 63 deletions(-) diff --git a/drivers/gpu/drm/tests/drm_kunit_helpers.c b/drivers/gpu/drm/te= sts/drm_kunit_helpers.c index c251e6b34de0..ca4f8e4c5d5d 100644 --- a/drivers/gpu/drm/tests/drm_kunit_helpers.c +++ b/drivers/gpu/drm/tests/drm_kunit_helpers.c @@ -5,6 +5,7 @@ #include #include =20 +#include #include =20 #include @@ -15,28 +16,6 @@ static const struct drm_mode_config_funcs drm_mode_config_funcs =3D { }; =20 -static int fake_probe(struct platform_device *pdev) -{ - return 0; -} - -static struct platform_driver fake_platform_driver =3D { - .probe =3D fake_probe, - .driver =3D { - .name =3D KUNIT_DEVICE_NAME, - }, -}; - -KUNIT_DEFINE_ACTION_WRAPPER(kunit_action_platform_driver_unregister, - platform_driver_unregister, - struct platform_driver *); -KUNIT_DEFINE_ACTION_WRAPPER(kunit_action_platform_device_put, - platform_device_put, - struct platform_device *); -KUNIT_DEFINE_ACTION_WRAPPER(kunit_action_platform_device_del, - platform_device_del, - struct platform_device *); - /** * drm_kunit_helper_alloc_device - Allocate a mock device for a KUnit test * @test: The test context object @@ -54,34 +33,7 @@ KUNIT_DEFINE_ACTION_WRAPPER(kunit_action_platform_device= _del, */ struct device *drm_kunit_helper_alloc_device(struct kunit *test) { - struct platform_device *pdev; - int ret; - - ret =3D platform_driver_register(&fake_platform_driver); - KUNIT_ASSERT_EQ(test, ret, 0); - - ret =3D kunit_add_action_or_reset(test, - kunit_action_platform_driver_unregister, - &fake_platform_driver); - KUNIT_ASSERT_EQ(test, ret, 0); - - pdev =3D platform_device_alloc(KUNIT_DEVICE_NAME, PLATFORM_DEVID_NONE); - KUNIT_ASSERT_NOT_ERR_OR_NULL(test, pdev); - - ret =3D kunit_add_action_or_reset(test, - kunit_action_platform_device_put, - pdev); - KUNIT_ASSERT_EQ(test, ret, 0); - - ret =3D platform_device_add(pdev); - KUNIT_ASSERT_EQ(test, ret, 0); - - ret =3D kunit_add_action_or_reset(test, - kunit_action_platform_device_del, - pdev); - KUNIT_ASSERT_EQ(test, ret, 0); - - return &pdev->dev; + return kunit_device_register(test, KUNIT_DEVICE_NAME); } EXPORT_SYMBOL_GPL(drm_kunit_helper_alloc_device); =20 @@ -94,19 +46,7 @@ EXPORT_SYMBOL_GPL(drm_kunit_helper_alloc_device); */ void drm_kunit_helper_free_device(struct kunit *test, struct device *dev) { - struct platform_device *pdev =3D to_platform_device(dev); - - kunit_release_action(test, - kunit_action_platform_device_del, - pdev); - - kunit_release_action(test, - kunit_action_platform_device_put, - pdev); - - kunit_release_action(test, - kunit_action_platform_driver_unregister, - &fake_platform_driver); + kunit_device_unregister(test, dev); } EXPORT_SYMBOL_GPL(drm_kunit_helper_free_device); =20 --=20 2.43.0.472.g3155946c3a-goog