From patchwork Tue Dec 5 07:31:33 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Gow X-Patchwork-Id: 13479514 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="XP/23V3H" 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 5C952109 for ; Mon, 4 Dec 2023 23:31:56 -0800 (PST) Received: by mail-yw1-x114a.google.com with SMTP id 00721157ae682-5d3bdbf1fb5so64678707b3.2 for ; Mon, 04 Dec 2023 23:31:56 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1701761515; x=1702366315; 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=ZVAFZpMC/pXXNd/TQY0spuN4AL3218SOHwbqzpG63Og=; b=XP/23V3H9uWZPnI1fVr7B/Z9WAqkHRaQpwzPYHC2UsQZ26b/JJDXC4uzkh6gDaoutV HfTuOgf6aIzfMsPQM1fILXVLn5V9zMtSjn8Tldz20AYcOyvgUsCj3DdkjOVxxrfZT2A0 EKlME95+nzUWPEv0/SWAd10PiyF+NZi5jnPKArQP0Yl2huuBRzoo69G+1ukAQIRlGQQk qMuSuzOcDTn2FcFMq1YfY2wCCIpaGR3XNYUbrpS1ygKMVEsKZUWwXo8d0buYfltdlOqo s5VQit0iZXGyVuK9wjfW7Jgn02ETVKw2IDmkDUnPK6BeGZLjib+nDNiySyjzxmMLUKrD XUQA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701761515; x=1702366315; 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=ZVAFZpMC/pXXNd/TQY0spuN4AL3218SOHwbqzpG63Og=; b=D6jxSZz2nE7v4tD3eUbo1Ghd67BOve6juYyq5qb/hw0QcYgc0sH4I4Qh98yFuaWw4s kA/sv0i2JoHa53M3Vi/nhd1O8d5IvbwdZq2q9XFcc91S72+gQv9KHEnoIXdERAItWzYB yqja1SCiEpkyCyTzWL+8VDB/YlWPaVA5OafGGp1y0tn/1bef1O4ekr35b4rqvlhaaibE RyLzmXb9jefjcEqSMSpc9dIowUM0tBcQBmSEK2a3qlSRQVv3QuhXdLEil7H4ZNdFdyy4 c5egmhoD73Xw0lBCxguldZpFwKrTvww41nu5vFTUMLTlRXxgMozsf0uw3LsBJuqG+yQs Jssg== X-Gm-Message-State: AOJu0YzcLojNYWOgFswP/YLzZWShX0qm571uuRn4h5GAUhs/aV5rN8tt umX//vCWZRuRTCDHPmFClhMHIR9M5rlf/A== X-Google-Smtp-Source: AGHT+IGfzqpHM2bt3eU+nbEARgOrDBsLKYb6WEtxSxhfPLTlJokPndKpUxfpVudRRQ022uRPVG12qnbYnST1BA== X-Received: from slicestar.c.googlers.com ([fda3:e722:ac3:cc00:4f:4b78:c0a8:20a1]) (user=davidgow job=sendgmr) by 2002:a81:ff08:0:b0:5cb:1bf4:ce09 with SMTP id k8-20020a81ff08000000b005cb1bf4ce09mr1108825ywn.2.1701761515615; Mon, 04 Dec 2023 23:31:55 -0800 (PST) Date: Tue, 05 Dec 2023 15:31:33 +0800 In-Reply-To: <20231205-kunit_bus-v1-0-635036d3bc13@google.com> Precedence: bulk X-Mailing-List: linux-hardening@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20231205-kunit_bus-v1-0-635036d3bc13@google.com> X-Mailer: b4 0.13-dev-099c9 Message-ID: <20231205-kunit_bus-v1-1-635036d3bc13@google.com> Subject: [PATCH 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 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. Signed-off-by: David Gow Reviewed-by: Matti Vaittinen --- Documentation/dev-tools/kunit/usage.rst | 49 +++++++++ include/kunit/device.h | 76 ++++++++++++++ lib/kunit/Makefile | 3 +- lib/kunit/device.c | 176 ++++++++++++++++++++++++++++++++ lib/kunit/kunit-test.c | 68 +++++++++++- lib/kunit/test.c | 3 + 6 files changed, 373 insertions(+), 2 deletions(-) diff --git a/Documentation/dev-tools/kunit/usage.rst b/Documentation/dev-tools/kunit/usage.rst index 9db12e91668e..a222a98edceb 100644 --- a/Documentation/dev-tools/kunit/usage.rst +++ b/Documentation/dev-tools/kunit/usage.rst @@ -797,3 +797,52 @@ structures as shown below: KUnit is not enabled, or if no test is running in the current task, it will do nothing. This compiles down to either a no-op or a static key check, so will have a negligible performance impact when no test is running. + +Managing Fake Devcices 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, setting +up a real device is not required to test any given function, so a fake device +can be used instead. + +KUnit provides helper functions to create and manage these fake devices, which +are internally of type ``struct kunit_device``, and are attached to a special +``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_create()``, +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 create +and register a device, using a new KUnit-managed driver created with ``kunit_driver_create()``. +To provide a specific, non-KUnit-managed driver, use ``kunit_device_register_with_driver()`` +instead. Like with managed drivers, KUnit-managed fake devices are automatically +cleaned up when the test finishes, but can be manually cleaned up early with +``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 = kunit_device_register(test, "my_device"); + + // Pass it to functions which need a device. + dev_managed_string = 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..fd2193bc55f1 --- /dev/null +++ b/include/kunit/device.h @@ -0,0 +1,76 @@ +/* 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 kunit_device; +struct device; +struct device_driver; + +// For internal use only -- registers the kunit_bus. +int kunit_bus_init(void); + +/** + * kunit_driver_create() - Create a struct device_driver attached to the kunit_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. + */ +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. + */ +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 you + * wish KUnit to create and manage a driver for you + */ +struct device *kunit_device_register_with_driver(struct kunit *test, + const char *name, + 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 created + * 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 += test.o \ assert.o \ try-catch.o \ executor.o \ - attributes.o + attributes.o \ + device.o ifeq ($(CONFIG_KUNIT_DEBUGFS),y) kunit-objs += debugfs.o diff --git a/lib/kunit/device.c b/lib/kunit/device.c new file mode 100644 index 000000000000..93ace1a2297d --- /dev/null +++ b/lib/kunit/device.c @@ -0,0 +1,176 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * KUnit basic device implementation + * + * Implementation of struct kunit_device helpers. + * + * Copyright (C) 2023, Google LLC. + * Author: David Gow + */ + +#include + +#include +#include +#include + + +/* 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 *); + +static struct device kunit_bus = { + .init_name = "kunit" +}; + +/* A device owned by a KUnit test. */ +struct kunit_device { + struct device dev; + struct kunit *owner; + /* Force binding to a specific driver. */ + struct device_driver *driver; + /* The driver is managed by KUnit and unique to this device. */ + bool cleanup_driver; +}; + +static inline struct kunit_device *to_kunit_device(struct device *d) +{ + return container_of(d, struct kunit_device, dev); +} + +static int kunit_bus_match(struct device *dev, struct device_driver *driver) +{ + struct kunit_device *kunit_dev = to_kunit_device(dev); + + if (kunit_dev->driver == driver) + return 1; + + return 0; +} + +static struct bus_type kunit_bus_type = { + .name = "kunit", + .match = kunit_bus_match +}; + +int kunit_bus_init(void) +{ + int error; + + error = bus_register(&kunit_bus_type); + if (!error) { + error = device_register(&kunit_bus); + if (error) + bus_unregister(&kunit_bus_type); + } + return error; +} +late_initcall(kunit_bus_init); + +static void kunit_device_release(struct device *d) +{ + kfree(to_kunit_device(d)); +} + +struct device_driver *kunit_driver_create(struct kunit *test, const char *name) +{ + struct device_driver *driver; + int err = -ENOMEM; + + driver = kunit_kzalloc(test, sizeof(*driver), GFP_KERNEL); + + if (!driver) + return ERR_PTR(err); + + driver->name = name; + driver->bus = &kunit_bus_type; + driver->owner = THIS_MODULE; + + err = 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); + +struct kunit_device *__kunit_device_register_internal(struct kunit *test, + const char *name, + struct device_driver *drv) +{ + struct kunit_device *kunit_dev; + int err = -ENOMEM; + + kunit_dev = kzalloc(sizeof(struct kunit_device), GFP_KERNEL); + if (!kunit_dev) + return ERR_PTR(err); + + kunit_dev->owner = test; + + err = dev_set_name(&kunit_dev->dev, "%s.%s", test->name, name); + if (err) { + kfree(kunit_dev); + return ERR_PTR(err); + } + + /* Set the expected driver pointer, so we match. */ + kunit_dev->driver = drv; + + kunit_dev->dev.release = kunit_device_release; + kunit_dev->dev.bus = &kunit_bus_type; + kunit_dev->dev.parent = &kunit_bus; + + err = 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; +} + +struct device *kunit_device_register_with_driver(struct kunit *test, + const char *name, + struct device_driver *drv) +{ + struct kunit_device *kunit_dev = __kunit_device_register_internal(test, name, drv); + + if (IS_ERR_OR_NULL(kunit_dev)) + return (struct device *)kunit_dev; /* This is an error or NULL, so is compatible */ + + return &kunit_dev->dev; +} +EXPORT_SYMBOL_GPL(kunit_device_register_with_driver); + +struct device *kunit_device_register(struct kunit *test, const char *name) +{ + struct device_driver *drv = kunit_driver_create(test, name); + struct kunit_device *dev; + + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, drv); + + dev = __kunit_device_register_internal(test, name, drv); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, dev); + + dev->cleanup_driver = true; + + return (struct device *)dev; +} +EXPORT_SYMBOL_GPL(kunit_device_register); + +void kunit_device_unregister(struct kunit *test, struct device *dev) +{ + bool cleanup_driver = ((struct kunit_device *)dev)->cleanup_driver; + struct device_driver *driver = ((struct kunit_device *)dev)->driver; + + kunit_release_action(test, device_unregister_wrapper, dev); + if (cleanup_driver) + kunit_release_action(test, driver_unregister_wrapper, driver); +} +EXPORT_SYMBOL_GPL(kunit_device_unregister); + diff --git a/lib/kunit/kunit-test.c b/lib/kunit/kunit-test.c index 3e9c5192d095..a4007158bf36 100644 --- a/lib/kunit/kunit-test.c +++ b/lib/kunit/kunit-test.c @@ -8,6 +8,9 @@ #include #include +#include +#include + #include "string-stream.h" #include "try-catch-impl.h" @@ -687,6 +690,69 @@ static struct kunit_case kunit_current_test_cases[] = { {} }; +static void test_dev_action(void *priv) +{ + *(void **)priv = (void *)1; +} + +static void kunit_device_test(struct kunit *test) +{ + struct device *test_device; + + test_device = 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, &test->priv); + + KUNIT_EXPECT_PTR_EQ(test, test->priv, (void *)0); + + kunit_device_unregister(test, test_device); + + KUNIT_EXPECT_PTR_EQ(test, test->priv, (void *)1); +} + +static void kunit_device_driver_test(struct kunit *test) +{ + struct device_driver *test_driver; + struct device *test_device; + + test_driver = kunit_driver_create(test, "my_driver"); + + KUNIT_ASSERT_NOT_NULL(test, test_driver); + + test_device = kunit_device_register_with_driver(test, "my_device", test_driver); + + KUNIT_ASSERT_NOT_NULL(test, test_device); + + // Add an action to verify cleanup. + devm_add_action(test_device, test_dev_action, &test->priv); + + KUNIT_EXPECT_PTR_EQ(test, test->priv, (void *)0); + + kunit_device_unregister(test, test_device); + test_device = NULL; + + // The driver should not automatically be destroyed by + // kunit_device_unregister, so we can re-use it. + test_device = kunit_device_register_with_driver(test, "my_device", test_driver); + KUNIT_ASSERT_NOT_NULL(test, test_device); + + // Everything is automatically freed here. +} + +static struct kunit_case kunit_device_test_cases[] = { + KUNIT_CASE(kunit_device_test), + KUNIT_CASE(kunit_device_driver_test), + {} +}; + +static struct kunit_suite kunit_device_test_suite = { + .name = "kunit_device", + .test_cases = kunit_device_test_cases, +}; + static struct kunit_suite kunit_current_test_suite = { .name = "kunit_current", .test_cases = kunit_current_test_cases, @@ -694,6 +760,6 @@ static struct kunit_suite kunit_current_test_suite = { 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); MODULE_LICENSE("GPL v2"); diff --git a/lib/kunit/test.c b/lib/kunit/test.c index 0308865194bb..144c8e7be197 100644 --- a/lib/kunit/test.c +++ b/lib/kunit/test.c @@ -10,6 +10,7 @@ #include #include #include +#include #include #include #include @@ -840,6 +841,8 @@ static int __init kunit_init(void) kunit_install_hooks(); kunit_debugfs_init(); + + kunit_bus_init(); #ifdef CONFIG_MODULES return register_module_notifier(&kunit_mod_nb); #else From patchwork Tue Dec 5 07:31:34 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Gow X-Patchwork-Id: 13479515 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="ODWFWSuE" 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 EBB76187 for ; Mon, 4 Dec 2023 23:32:00 -0800 (PST) Received: by mail-yw1-x114a.google.com with SMTP id 00721157ae682-5d064f9e2a1so75484327b3.1 for ; Mon, 04 Dec 2023 23:32:00 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1701761520; x=1702366320; 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=K0zdW7CBKdTcyZ6NkC3IZ70G/WJIHzJoU6Zyjp/hyXw=; b=ODWFWSuEFkV8GCsHmRatZGcony01v/uW8FqEAWHZ6v9DKNcJr7LGwv6RREzSth54Ij 0tfb8D/Yqk1dy4XES+mC+pN1hzucQVSEC9EOKMcy/fxjQWqNvBxTlrsBBdTmUqe4+BcV 23Gnvl2glpk7aWBBBKV3CcfznbCgyqtAF6kecf22J5aL9mSFLJnZxzbvnm88zivJP3Xe utUwuAIb000bpYgKsIoQTqrexGfTygbx3GIu8xqVFNjGrQ3fftlD3K+8Lh/1JjeQwZPM DpLH1qS04Or6194+Zs/o3+dYTeFTyJgJOE9Pv3GmrqU9H7CCms4burSVn3G3KME2vDuM wlVA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701761520; x=1702366320; 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=K0zdW7CBKdTcyZ6NkC3IZ70G/WJIHzJoU6Zyjp/hyXw=; b=IlM4xdnFZJkiwCcJeUkbp1/JtDeDoq++CuFTXMSlUhazYD/ZiIjofn6aB2fCKKf7q8 HYdcF7p86cLxG0I+NKGFxmtfBSdXTNzbAhxHcJMhZI8DJ44wnfkOuzevpnOMFTvzx9He mKYJR+ACWp9exdyTjGFUaD4MxOwp2tnK82Fmzl5K/8o1q/0NOByh59llUIP1+d5E1J4X 9eiGTsFMYBR4CZ7KdfHFa/VEMShTAsdn5eTl/leIfzaDxQ54bjoDrv7WZ7v4MLW5aF82 5uou+yysvAgB0+jY3X3EP+pEgm7YglZfNDu8a/DsGi4CrrMcUN0EbMS+MdshwVI6DUMK H/jw== X-Gm-Message-State: AOJu0YxJ8gEVKYeQu1u6DIDS1XvA5dNr+P4FLEU56oX9KkVV2l1V+Q7v ecNvixdQnnHEODboVhX6yZQfJILoWk16RA== X-Google-Smtp-Source: AGHT+IFoVz0OcZkIRG5/Sf50+3F5qfsDJpbbL6ah305RE6J1zf52P+Ky7oV+UR+nfZA3THFYH0yVogxgbzcb5g== X-Received: from slicestar.c.googlers.com ([fda3:e722:ac3:cc00:4f:4b78:c0a8:20a1]) (user=davidgow job=sendgmr) by 2002:a25:d04c:0:b0:db5:4b1d:c63c with SMTP id h73-20020a25d04c000000b00db54b1dc63cmr312842ybg.11.1701761520168; Mon, 04 Dec 2023 23:32:00 -0800 (PST) Date: Tue, 05 Dec 2023 15:31:34 +0800 In-Reply-To: <20231205-kunit_bus-v1-0-635036d3bc13@google.com> Precedence: bulk X-Mailing-List: linux-hardening@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20231205-kunit_bus-v1-0-635036d3bc13@google.com> X-Mailer: b4 0.13-dev-099c9 Message-ID: <20231205-kunit_bus-v1-2-635036d3bc13@google.com> Subject: [PATCH 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 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. Signed-off-by: David Gow Reviewed-by: Matti Vaittinen Acked-by: Kees Cook --- 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..f7a1fce8849b 100644 --- a/lib/fortify_kunit.c +++ b/lib/fortify_kunit.c @@ -16,6 +16,7 @@ #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt #include +#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 = root_device_register(dev_name); \ + dev = 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) From patchwork Tue Dec 5 07:31:35 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Gow X-Patchwork-Id: 13479516 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="l4Tx8j3c" Received: from mail-yb1-xb4a.google.com (mail-yb1-xb4a.google.com [IPv6:2607:f8b0:4864:20::b4a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3CA8A189 for ; Mon, 4 Dec 2023 23:32:06 -0800 (PST) Received: by mail-yb1-xb4a.google.com with SMTP id 3f1490d57ef6-db7d201be93so3242381276.2 for ; Mon, 04 Dec 2023 23:32:06 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1701761525; x=1702366325; 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=DV9smLZQ3RX5iGHV5Mh5U08N7CP5uXEtJIVwqfx/P5o=; b=l4Tx8j3cLfBVHAArTgqSGR3ub0EuGRlDa298yBD+23HvoLHHgTUAkH/8iQu8LHH+E4 Q18aw3clzqr4uyhDrj8B2/nIsZglA+Ft3E3QJ4sufn8JI10O1K/FRTIe0GDtCjYg5RzX ZoIgsQ56jAswAG36a9sJ3Fm9IAFW2lbI2/7JkOtUoL7CzD//iclCFV27gHDnaixWcSa5 WjAOesrX80WgM2JBeiY1pHvJn6UMJW9cxdCcOeCePxiHik05s6kYSjWOauBe3jrRUpah vsVxCEPpNaBTvPIIzlUoTA1lE8eE5ct0QjLKrCu38NfDqk7coOLxwKgPhWMzm0cv6nB1 gh+g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701761525; x=1702366325; 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=DV9smLZQ3RX5iGHV5Mh5U08N7CP5uXEtJIVwqfx/P5o=; b=F/08v+OYzYJylz8PgG3XAa9zWWzS+C1yHQxBrd2s/m2DygeXoAalcWousigfdavgu2 mvFbkLWnuLdzUD2Jqf7j5GKkRJIWHY/uHztzEvE/6uGaa78waV7EJcfxQwL9YG3fQlQO fM4GSg4ZNk/iqmdrU3ZZqGdnNaZkduaQX7NC+ah9FmDMt5exOD/uz/ertUYZwAPC9owX 0cN3kAt4J1X/aqSdpMux/K3kBzF8zep7zKkiIgG5YjZClcsMFQ6ECaaiBV9uvrFpeaw7 JT4tMXisL8ZLoTkooDr25pTbwuuNjAQgpCeVbeo1fPwJz99DvByhWMEuoR+bc6JxCtM0 z2+w== X-Gm-Message-State: AOJu0YzG3zsppbhHmT+wRHNv//sTuAAm1oCJs4AMhPACJOPUx6Ssmtdy G+/ZIukJr1W40uqI8Y6CtVI+UpfYrFmiNw== X-Google-Smtp-Source: AGHT+IHd6HO2jMP5HD9ZeIpo6hwIyN/w0eC95NNxakjiXl3f5OV+SEA9oRHhQ+nDPt3tJrzszUO4EACeVBNHug== X-Received: from slicestar.c.googlers.com ([fda3:e722:ac3:cc00:4f:4b78:c0a8:20a1]) (user=davidgow job=sendgmr) by 2002:a25:3d05:0:b0:d9a:ec95:9687 with SMTP id k5-20020a253d05000000b00d9aec959687mr967759yba.11.1701761525047; Mon, 04 Dec 2023 23:32:05 -0800 (PST) Date: Tue, 05 Dec 2023 15:31:35 +0800 In-Reply-To: <20231205-kunit_bus-v1-0-635036d3bc13@google.com> Precedence: bulk X-Mailing-List: linux-hardening@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20231205-kunit_bus-v1-0-635036d3bc13@google.com> X-Mailer: b4 0.13-dev-099c9 Message-ID: <20231205-kunit_bus-v1-3-635036d3bc13@google.com> Subject: [PATCH 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 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. Signed-off-by: David Gow Reviewed-by: Matti Vaittinen --- lib/overflow_kunit.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/overflow_kunit.c b/lib/overflow_kunit.c index 34db0b3aa502..91b03217c462 100644 --- a/lib/overflow_kunit.c +++ b/lib/overflow_kunit.c @@ -7,6 +7,7 @@ #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt #include +#include #include #include #include @@ -618,7 +619,7 @@ static void overflow_allocation_test(struct kunit *test) } while (0) /* Create dummy device for devm_kmalloc()-family tests. */ - dev = root_device_register(device_name); + dev = kunit_device_register(test, device_name); KUNIT_ASSERT_FALSE_MSG(test, IS_ERR(dev), "Cannot register test device\n"); @@ -634,7 +635,7 @@ static void overflow_allocation_test(struct kunit *test) check_allocation_overflow(devm_kmalloc); check_allocation_overflow(devm_kzalloc); - device_unregister(dev); + kunit_device_unregister(test, dev); kunit_info(test, "%d allocation overflow tests finished\n", count); #undef check_allocation_overflow From patchwork Tue Dec 5 07:31:36 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Gow X-Patchwork-Id: 13479517 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="gU77UWLf" Received: from mail-yb1-xb4a.google.com (mail-yb1-xb4a.google.com [IPv6:2607:f8b0:4864:20::b4a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7D6F21B9 for ; Mon, 4 Dec 2023 23:32:10 -0800 (PST) Received: by mail-yb1-xb4a.google.com with SMTP id 3f1490d57ef6-db584b8fb9cso4402145276.2 for ; Mon, 04 Dec 2023 23:32:10 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1701761529; x=1702366329; 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=FdHfmQmZDYuyXCu0NGdHRUb/90Lva6Q2oB2rwJOjRS8=; b=gU77UWLf17NGFwns1NTtH4ufmlGXSuKUVMe0SMM3Z/hkuzpNhnNVs/ZdiK6v8qArF6 1iPq9ZvY1n1XV3BuyBhS4bmkK8n6w20y/1zUlCfOFepTmp8dM/vEVUVW3ZPiyfbMRP09 hCwu1fRloMi511BNUmei169Jh4wxlce0fe+rV9OB4IrxNpS6tsjOgXiLaRfHNMwnwex2 ejrpMQ53Cn32gWbPxEVS1/Yjq8olZc8mk8my+okMtGnUhPA6d6nmXnJXs+6+EWW05mW0 ifZyaaeAlZ2xHbNk+ndalBpIbTFFUgDECLafYNtE7lWJ7IA0Dp9H6goWe+DyqAqCVbkp Y9YQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701761529; x=1702366329; 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=FdHfmQmZDYuyXCu0NGdHRUb/90Lva6Q2oB2rwJOjRS8=; b=CtjFIbOz9hTgVb05Pp1/+Qj48IB23GHugnKb/bOfovVXsO32c5eJsGBOxK/2UtzW+i 2gmO7Z9sPIQPmRqzAFUJIDzFykROKxjZ7w+3CUIYXwW8O03zKA6DQfLNu+qaIfwAKQaM xiD5dgGsxutNumFDATgc9z8xm6vfaPZEi4nT6CgoU9sWfJwCIbdtK8+a+O5QuVHVJnKO 6nQPvj3GDO6DCmoEj3JNjlDkIkjtItmWd9yybhnyoiwcT7UrIwxe3xYTp9iPF7jLVB9y xG763cw22DN8lFWlzNj3LxUwv4imYfLu2G32Ck0fLxJLabKtmnXwY7cuGNbbElb9gtuH UGrw== X-Gm-Message-State: AOJu0YxnNN4QsqWR8sgodUSPiHUl9G8E7+m7yPKk7mhe+Vpcx7GCEe+g 2sT0dkfmAwjDgPwdZr6b7b7ShbxDPEM4Pg== X-Google-Smtp-Source: AGHT+IHHF6+HTN+R7wTnVRWLqb4w18tX0Cim+QBivHuTkcMFGQFLnFtpexLAxiytHhXHivrQcbisM6rp+Y02ZQ== X-Received: from slicestar.c.googlers.com ([fda3:e722:ac3:cc00:4f:4b78:c0a8:20a1]) (user=davidgow job=sendgmr) by 2002:a25:d6d1:0:b0:db7:dce9:76d3 with SMTP id n200-20020a25d6d1000000b00db7dce976d3mr188630ybg.9.1701761529700; Mon, 04 Dec 2023 23:32:09 -0800 (PST) Date: Tue, 05 Dec 2023 15:31:36 +0800 In-Reply-To: <20231205-kunit_bus-v1-0-635036d3bc13@google.com> Precedence: bulk X-Mailing-List: linux-hardening@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20231205-kunit_bus-v1-0-635036d3bc13@google.com> X-Mailer: b4 0.13-dev-099c9 Message-ID: <20231205-kunit_bus-v1-4-635036d3bc13@google.com> Subject: [PATCH 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 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. Signed-off-by: David Gow Acked-by: Mark Brown --- sound/soc/soc-topology-test.c | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/sound/soc/soc-topology-test.c b/sound/soc/soc-topology-test.c index 2cd3540cec04..1d7696e5bffc 100644 --- a/sound/soc/soc-topology-test.c +++ b/sound/soc/soc-topology-test.c @@ -10,6 +10,7 @@ #include #include #include +#include /* ===== HELPER FUNCTIONS =================================================== */ @@ -21,26 +22,20 @@ */ static struct device *test_dev; -static struct device_driver test_drv = { - .name = "sound-soc-topology-test-driver", -}; - static int snd_soc_tplg_test_init(struct kunit *test) { - test_dev = root_device_register("sound-soc-topology-test"); + test_dev = kunit_device_register(test, "sound-soc-topology-test"); test_dev = get_device(test_dev); if (!test_dev) return -ENODEV; - test_dev->driver = &test_drv; - return 0; } static void snd_soc_tplg_test_exit(struct kunit *test) { put_device(test_dev); - root_device_unregister(test_dev); + kunit_device_unregister(test, test_dev); } /*