From patchwork Thu Dec 14 08:49:15 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Gow X-Patchwork-Id: 178575 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:3b04:b0:fb:cd0c:d3e with SMTP id c4csp8403239dys; Thu, 14 Dec 2023 00:49:50 -0800 (PST) X-Google-Smtp-Source: AGHT+IEcuc7WG4UevJoJS5G4ur94trTN1hSNOE3Xhcrulr0OrLlH50nj/TocU4/21/L9gQAbTOdK X-Received: by 2002:a05:6870:40c4:b0:203:55f0:caa2 with SMTP id l4-20020a05687040c400b0020355f0caa2mr40878oal.45.1702543790112; Thu, 14 Dec 2023 00:49:50 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702543790; cv=none; d=google.com; s=arc-20160816; b=DPrn1miSZ5wPCp+PFDUa7mxHTI+nRVTkGE4R71J5fQhA0GuwhsgjwoxEARBZbvGn+f s+qNFt30SgXN3r+mFVJ8IEnvgkFZjpQRmGhQfV0VSRkG6BvRZWACfwkNF+rp7UmgDIOF y+h46QCfRlrioyhRVnSNh1E3JN18+SfSjHBIxhyRsp9OaLJOHbFibrWbcWD1VKAD+M38 FNKdq4J/JEJLv9CRoRF7AAshOOqtsG929wkLcLAtIwuvb+p8pqDwfLW0ZMvhVdtt1Gz8 OU5WR66p++/z5RDZjOqqlhvQ92fD+DI2bHv9fF1qKQ6DdXspBINUnTFD7JeFOvhnbXAJ qHHw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:message-id:references :mime-version:in-reply-to:date:dkim-signature; bh=e9PX0PXS1f3f3pBJoG7zI47BnYzw6Hhl3FOBvIaiIQI=; fh=s2ufp5odxBiVI1DugGmR9pnJ96PDI7y2LhWfMqzKBMk=; b=x8x5c8/va9aCw9+9AnEg7J9SUfNLNpOQi8eynA1PrnY3Cg1v3QSR5JggDW01Q3SXaW Cz8gLD+I+rGFycrrwqvedQC2lwKgLDuuwjU06GyQ1bvfGJKqtvqiUTmg3BqV6UWbE4YP 99/VjzeMU1I41EWJpFc73QzOHuUtksHRh4rfVXvYk9QCQorT6/gL1S1MC9l7GNPO6WJh 2NjyCMM966A1eEACgU7CEHvubVVHLXzC4ObJvg1vxjvPPDO4eOG5crp4lPtH5XGW0w8i rHgAkz+BERCdTH7tuSwTvTsQEpB5drpkYDY4XfX2yX/Tk/GL28DbksdapPqFqilejRS6 JASQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=j07L2V9C; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from snail.vger.email (snail.vger.email. [2620:137:e000::3:7]) by mx.google.com with ESMTPS id a123-20020a636681000000b005c6032d28c2si10574758pgc.41.2023.12.14.00.49.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 14 Dec 2023 00:49:50 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) client-ip=2620:137:e000::3:7; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=j07L2V9C; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by snail.vger.email (Postfix) with ESMTP id 79E398029441; Thu, 14 Dec 2023 00:49:48 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235576AbjLNItj (ORCPT + 99 others); Thu, 14 Dec 2023 03:49:39 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41678 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235557AbjLNIth (ORCPT ); Thu, 14 Dec 2023 03:49:37 -0500 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 9220D10E for ; Thu, 14 Dec 2023 00:49:40 -0800 (PST) Received: by mail-yb1-xb4a.google.com with SMTP id 3f1490d57ef6-dbcca990ee9so2047222276.0 for ; Thu, 14 Dec 2023 00:49:40 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1702543780; x=1703148580; 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=j07L2V9CFesI/ZQNRFsyu7TuDCUGjVwzhvoXlwceuKmAQlYW4o1L1MYpn0SoMFhmfu UgLmsdxV5tYeTyi2Z+/wBCSVUB4BrRJiSG6a3oTlw4+f3lRLuHW05ijflJLLzN6UVMI4 Ke4MXlMnbIGoKfhTWVj070cUD08/NnIEFP7eBA1abXMFFm7xrlPdtkk/rLnu2Pvd/C4t 77HAJDPmB/PVWDRBOayZ9jKtH+LpfFJN+dTXxm4ucr7AgQhqXq+yq4XY31TpCaKjkpDK C/caAybYgrc8Ks8xEUdsDdeCnLeDsnCHNpL/zZkJrmOWIMN75OBJnEIZnpiLApq5OjT9 3Zow== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702543780; x=1703148580; 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=OgkcSWv8UEMn1Tbvw8fdM9Y/5kqtL/2sIvkttfZqqPGCnNl3lL4iBvWXhMLH/QoiFk yOluaF42flVCupzpwCxiXf+64ftxDOYvrNacTgB0IP7GuA51eEnIB5iPX9L4aRKWll7b upKsGtqgCkcqgH7iNWpqLB1y10925Q5YpHasmxyQm7HruflXwTpzlVH6+L1FikjkM2hA ELT42DLO1kKyZFB0UaR8dDGIM+00+c1QMT0I8sTF1Et8emq+kq8gtjf0rH8fosBtqgZc ZFnpjrLK5cHt2L2iKDPpoXQfxNCy8z//m5qZQE1FHWHWYmTxmQMh1dNG0G0yBF/96mzc Hgrg== X-Gm-Message-State: AOJu0Yy0Dmixbz7/7QFcVflPvcsHsNHY1Dn7qqjZGu4A2DxtM4DQfjqq rZtlNA9dtN0qPvwqKUXzwkGayAdy4zs7Mg== X-Received: from slicestar.c.googlers.com ([fda3:e722:ac3:cc00:4f:4b78:c0a8:20a1]) (user=davidgow job=sendgmr) by 2002:a25:5f50:0:b0:dbc:e825:2d2f with SMTP id h16-20020a255f50000000b00dbce8252d2fmr4766ybm.0.1702543779746; Thu, 14 Dec 2023 00:49:39 -0800 (PST) Date: Thu, 14 Dec 2023 16:49:15 +0800 In-Reply-To: <20231214-kunit_bus-v3-0-7e9a287d3048@google.com> Mime-Version: 1.0 References: <20231214-kunit_bus-v3-0-7e9a287d3048@google.com> X-Mailer: b4 0.13-dev-099c9 Message-ID: <20231214-kunit_bus-v3-1-7e9a287d3048@google.com> Subject: [PATCH v3 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 X-Spam-Status: No, score=-9.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE,USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (snail.vger.email [0.0.0.0]); Thu, 14 Dec 2023 00:49:48 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1785246557157804237 X-GMAIL-MSGID: 1785246557157804237 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 --- 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 actions, and more. .. 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-tools/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 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 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, 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"); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, fake_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..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 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. + * + * Return: a stub struct device_driver, managed by KUnit, with the name @name. + */ +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 registered. + */ +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. + * + * 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 registered. + */ +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 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-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 = { + .name = "kunit", +}; + +/* Register the 'kunit_bus' used for fake devices. */ +int kunit_bus_init(void) +{ + int error; + + kunit_bus_device = root_device_register("kunit"); + if (!kunit_bus_device) + return -ENOMEM; + + error = 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_bus. + * Returns an error pointer on failure. + */ +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); + +/* Helper which creates a kunit_device, attaches it to the kunit_bus*/ +static struct kunit_device *kunit_device_register_internal(struct kunit *test, + const char *name, + const struct device_driver *drv) +{ + struct kunit_device *kunit_dev; + int err = -ENOMEM; + + kunit_dev = kzalloc(sizeof(*kunit_dev), 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); + } + + kunit_dev->dev.release = kunit_device_release; + kunit_dev->dev.bus = &kunit_bus_type; + kunit_dev->dev.parent = kunit_bus_device; + + 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; +} + +/** + * 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 = kunit_device_register_internal(test, name, 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 device_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 = kunit_driver_create(test, name); + if (IS_ERR(drv)) + return ERR_CAST(drv); + + dev = 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 = drv; + + + return &dev->dev; +} +EXPORT_SYMBOL_GPL(kunit_device_register); + +/* Unregisters a KUnit-managed device early (including the driver, if automatically created). */ +void kunit_device_unregister(struct kunit *test, struct device *dev) +{ + const struct device_driver *driver = 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 +#include +#include + #include "string-stream.h" #include "try-catch-impl.h" @@ -687,6 +691,134 @@ 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; + long action_was_run = 0; + + test_device = 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 = 0; + + 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, &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 = kunit_get_current_test(); + struct driver_test_state *state = (struct driver_test_state *)test->priv; + + state->driver_device_probed = true; + return 0; +} + +static int driver_remove_hook(struct device *dev) +{ + struct kunit *test = kunit_get_current_test(); + struct driver_test_state *state = (struct driver_test_state *)test->priv; + + state->driver_device_removed = 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 = kunit_kzalloc(test, sizeof(*test_state), GFP_KERNEL); + + test->priv = test_state; + test_driver = 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 = driver_probe_hook; + test_driver->remove = driver_remove_hook; + + test_device = 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 = 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 = false; + + // 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); + + // 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[] = { + 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 = { + .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 +826,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..c457593e4913 100644 --- a/lib/kunit/test.c +++ b/lib/kunit/test.c @@ -18,6 +18,7 @@ #include #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(); kunit_debugfs_init(); + + kunit_bus_init(); #ifdef CONFIG_MODULES return register_module_notifier(&kunit_mod_nb); #else From patchwork Thu Dec 14 08:49:16 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Gow X-Patchwork-Id: 178576 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:3b04:b0:fb:cd0c:d3e with SMTP id c4csp8403268dys; Thu, 14 Dec 2023 00:49:55 -0800 (PST) X-Google-Smtp-Source: AGHT+IE8ybgfpu5qqQFUQCA4Yu/+o/HDGU9PX1cAMpgeAGMzDW8G/x24C8jgjuf8A5XV9ozJeDxS X-Received: by 2002:a17:903:248:b0:1d0:c41b:1d0c with SMTP id j8-20020a170903024800b001d0c41b1d0cmr5464797plh.75.1702543795722; Thu, 14 Dec 2023 00:49:55 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702543795; cv=none; d=google.com; s=arc-20160816; b=tB91h710RUO/QlqcNBupk2hCViYKZyM4jyIDdNslYCtOc7c1h925niWpL9OFQNval+ U/P7++j5uoxe/7pBrmeHSUt66//fFamlecDamFFfJnvpruTCK6V5WUu+IstcrGsISand ehYvTNw9fLpRnHAT6FWY6kCvBEnWw18wPQWXzQj50MZXgobl2cg0dQIsxk1EJWEsjPhI mtfBIBKBO+FcC7bmMEDyBB+MBceMCegZTe6nJ0ZucW3TjjrFbdh5izEpZJ7R+G1Xc9+n 6uNpHL46u5Q61gGjqKY2EkSYZ3gQEP0He9USh73G6gixHWlSBb+sDfU3+5gpwpJfrVqx 37Jw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:message-id:references :mime-version:in-reply-to:date:dkim-signature; bh=vJ3XYd4VAh0rMwZdPZnf0/R4/jXxDQ1RiH7pfcHiYc8=; fh=FBRasfUlr9n+nXXXlXR1Gg7/LVBbWP8wdjqI9r8Tmds=; b=qDmcA3nzb+tIyk4kpe0sAH/98ggYZZMvhMhuuPzGfRlUTeL+9Nj0uYaUACkaTQcWIz wxA7yOVkGMmwc/WpSWz7BvhOURCV9MfFteBhZdd4/RqQskIsRY/B5CSYTJk3p31i55xd ctZaA3YNi0zhBRB6cVn5KM0L83joUhn9n1Wc0Vud14YYZuGe6Ve9dC5l+YbTp4z/TPiK sKAsOzOp+F+PW28wc0Nz12TSgIjGk5cZwPKF4xdExPeadbZcP1t0PRzuFZoiOBWU5J8v 3A72QqjnReAgE94dRM0UBuCFRQ3h2oufG717GqIj3bZfJrNEwO8tKKC7iJ1fh9AKvGLI O9mA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=aRByfs6j; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from snail.vger.email (snail.vger.email. [23.128.96.37]) by mx.google.com with ESMTPS id c10-20020a170902724a00b001cfa17ec30dsi10953363pll.508.2023.12.14.00.49.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 14 Dec 2023 00:49:55 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) client-ip=23.128.96.37; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=aRByfs6j; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by snail.vger.email (Postfix) with ESMTP id 433ED80294D1; Thu, 14 Dec 2023 00:49:54 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1443435AbjLNItk (ORCPT + 99 others); Thu, 14 Dec 2023 03:49:40 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41714 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1443425AbjLNIti (ORCPT ); Thu, 14 Dec 2023 03:49:38 -0500 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 21510118 for ; Thu, 14 Dec 2023 00:49:45 -0800 (PST) Received: by mail-yb1-xb4a.google.com with SMTP id 3f1490d57ef6-dbcca4c56b9so2382325276.3 for ; Thu, 14 Dec 2023 00:49:45 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1702543784; x=1703148584; 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=aRByfs6jJv1pPl+XO5MRrqPIEKLf20hbEIpup6djP2bJKRqLu9PMErZeB6Wn/uAKpr G91nfjkpuuoXv581ZhCxU44bDu3oeBDHIkwI09alP0Ei6zXO3GqXx746/JAUu7mA0xcZ d91pIaanzP9lwQUmw+P/4oFZGlKa/+nHsmOJs04dVVrtgq+bIgsnbKqbeAE/LxiIKWVd 60NngqrtSB8o79W+YfxOKpRVq0hfRyhbEwcvaOK1cQeq1+2uYNL4S8nvfNv2HpGi31C0 o1wkxhUq292fLKjC+F2uw5swWgMqpmvbjIW4rR/a2FH33eNGai5C4mUhzFKD8p5yLD2i 3AyA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702543784; x=1703148584; 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=RGjHlPYUiZPCSg1r3qsEsSHQ/58KcPBxyNLH1w8i2IM4vi+IlHqh3zNzBERnmYSwKD quZ+miJw8flm04IwLkMURfdozu57ceo7r45oj7YI+Ah11jjLRGAe3SRyxkwY9myYFZOe hKNfwF6Fx0VwAsrkRsNxWQv3Bi+QhdY3Kaa3GutkT1CE4sYQIyo69hY7Pqg5/y5M2uu0 t/m0hmyU1u4cOE3QKJPBGwsRxH5TTqGMI/RgkmmZteRpltYsZwItKeWBkKlDG1lxw+Br H5725WI2TKlrf7Bma8U6t4clGfAv5sPVEsTwzHJ6MxkLfcjIWKg5tJxOfWlQA5ERtzc4 CHbA== X-Gm-Message-State: AOJu0Yy3P9HLEJgJ36yXkiEXCEBdJ3OLoAi/rRPA3Qq/17OkMmnEWFFw 8m/4XCyFeDJuCUiXnsw/kv+icElorpVeqg== X-Received: from slicestar.c.googlers.com ([fda3:e722:ac3:cc00:4f:4b78:c0a8:20a1]) (user=davidgow job=sendgmr) by 2002:a25:ca92:0:b0:dbc:cb73:15c8 with SMTP id a140-20020a25ca92000000b00dbccb7315c8mr38319ybg.5.1702543784333; Thu, 14 Dec 2023 00:49:44 -0800 (PST) Date: Thu, 14 Dec 2023 16:49:16 +0800 In-Reply-To: <20231214-kunit_bus-v3-0-7e9a287d3048@google.com> Mime-Version: 1.0 References: <20231214-kunit_bus-v3-0-7e9a287d3048@google.com> X-Mailer: b4 0.13-dev-099c9 Message-ID: <20231214-kunit_bus-v3-2-7e9a287d3048@google.com> Subject: [PATCH v3 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 X-Spam-Status: No, score=-9.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE,USER_IN_DEF_DKIM_WL autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (snail.vger.email [0.0.0.0]); Thu, 14 Dec 2023 00:49:54 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1785246562715157909 X-GMAIL-MSGID: 1785246562715157909 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 +#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 Thu Dec 14 08:49:17 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Gow X-Patchwork-Id: 178577 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:3b04:b0:fb:cd0c:d3e with SMTP id c4csp8403291dys; Thu, 14 Dec 2023 00:50:00 -0800 (PST) X-Google-Smtp-Source: AGHT+IEgwZJT9p/GAwqRoNxWYeyfWwBFwlTMOOiCBc4aOyedyCqHk7xJOgvloUiJUX2hNqdG1AWL X-Received: by 2002:a17:902:b207:b0:1d2:eee7:2a7e with SMTP id t7-20020a170902b20700b001d2eee72a7emr4010484plr.61.1702543800453; Thu, 14 Dec 2023 00:50:00 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702543800; cv=none; d=google.com; s=arc-20160816; b=P+QvKYxdFuimcBG/FE6w201bw3I0s1W52QaLc8dxWi9r7qSquorqTkjDsmyIMEicAV 2a00FSXxHsaLMGeHDDzXjM0+PI96+zHBgcUuglCk1c+jokd98t08mRaTSJvatSsPjPFo CMpDzGbG2QYBeLUgx4djMwVjGKI/zYsFsNROWgtiAqSX2lggcmfPWBcJX7D0ASHz05cM u92Klnww6WMhiDUzBBM9GfG2zn0G6iRppkMcTOGK3EjU1/KpG3aITn8jWFJ46IcCAwoj UFMXEJVjn9vZJiZ9AjVDfKVPo+LsiWvlVzcFSkFMvMrtQ8dcN1hNv1A73nKQaYsLcJTX OL7w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:message-id:references :mime-version:in-reply-to:date:dkim-signature; bh=NkRyaEIclzm+ShIMyVUbqREgKKajNjynsQ0mobEpXzk=; fh=FBRasfUlr9n+nXXXlXR1Gg7/LVBbWP8wdjqI9r8Tmds=; b=MKhkcJsMDKiCIoZb2b/Gq572EV7/tIz7y5MF3gIzKWCiwLzaQobGc8jveIdZevQHCi HeNtwjlLVIWP/NoTLJ863tNw9oGQNyeOC8m6N1eaZ8EKHGGDqY6fUl3B8qUofpK394O9 GnBaRaAD4DkRKHt0bMDHpUsKm+neInGyOG7OxLLr5lcjHtyIHbGOQAD9BJnm75MBcbq/ eI7Xqx7UT7pyErRE3hnhDR0jwERwvHLKo2OHD3OJIzYkSmxuqSzZPw6UAMCA+rvKG10u nE/enyYJHATQXR8CJWq8FE+iLpHmd/46fRHpNsHPVtBTUUbZNAh+IF5fnXyHZhmLWDZT GiYA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=cgvWjpRn; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:4 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from howler.vger.email (howler.vger.email. [2620:137:e000::3:4]) by mx.google.com with ESMTPS id j9-20020a17090276c900b001cfa17ec332si10822396plt.616.2023.12.14.00.50.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 14 Dec 2023 00:50:00 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:4 as permitted sender) client-ip=2620:137:e000::3:4; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=cgvWjpRn; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:4 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by howler.vger.email (Postfix) with ESMTP id 9D3DB82FA572; Thu, 14 Dec 2023 00:49:55 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at howler.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235614AbjLNItp (ORCPT + 99 others); Thu, 14 Dec 2023 03:49:45 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58502 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234535AbjLNItn (ORCPT ); Thu, 14 Dec 2023 03:49:43 -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 984BBE3 for ; Thu, 14 Dec 2023 00:49:49 -0800 (PST) Received: by mail-yb1-xb49.google.com with SMTP id 3f1490d57ef6-dbcc629d8f5so2333351276.1 for ; Thu, 14 Dec 2023 00:49:49 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1702543789; x=1703148589; 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=cgvWjpRn1Wu1bWJ1qeTxF/WH65bRi37aahZVCQy3rXjzx32KJiErk89YQefh5ZEf+C +dGWH7vRN8IOpRl07fQ+FSVoJEvUMfTLUWQVSgPkds49fzXfpZBNia34Scyg53Mu0lyz Xg83Vle2jySQXw9ahzxjC7oPbrnv/qdWmfNPqfLe0ebqBcPYLqjql5AaIqa2aEwRDxly rCIFfdsRrZtI80HrjIfaxg+kv+9g5CNd85HNocXkNODhjwPQQX6AoVNW92xwMHn9ICn5 2LPsuDlBJ2dQXuga3zKT2uOxAdfAJz7/6h6NQqXOnIXZfVL4ZdaYOnT3InSBTTjIFF+Q +DdQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702543789; x=1703148589; 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=xU8WX1HR0rsUbbWB2FQybr4FSt40yWng4dkfER3z9GcLDZGPb8OR7WKZl47WugYtOO vTmkUOKLQdEr38pkSAqEPgU/eToebxW4dfbOfyiRzx2R7TtL5CYK/R3HfDx2eSrOu2wM Xgx44FizDyRIfiG/Hp3/vClZFOF+rqY10mvZZKND/tewOp0T5rVGETlBkW3S6iAy2e76 1DhqN4NiiHWo8ULsrxe4QvS08OF5wSGqeZKLbPmXJ4sX+RlGJauRZ/ZBV6rIKcML2uyJ TRObG+xFhIymUTitmznEs0JL+IXl2elRpB7m9bH1kt3AoO4eCELS3DcC/K5A+LTNAnna TXrw== X-Gm-Message-State: AOJu0YyIi2OgjsacJEtl3g6VkgouU+QSnoFlCKiMSQjgRwQKftj/zfU0 xIgqIqhnJoNv7MWRX73m6xeZeBu7ND3j6Q== X-Received: from slicestar.c.googlers.com ([fda3:e722:ac3:cc00:4f:4b78:c0a8:20a1]) (user=davidgow job=sendgmr) by 2002:a25:6b0b:0:b0:dbc:d896:9795 with SMTP id g11-20020a256b0b000000b00dbcd8969795mr39417ybc.12.1702543788881; Thu, 14 Dec 2023 00:49:48 -0800 (PST) Date: Thu, 14 Dec 2023 16:49:17 +0800 In-Reply-To: <20231214-kunit_bus-v3-0-7e9a287d3048@google.com> Mime-Version: 1.0 References: <20231214-kunit_bus-v3-0-7e9a287d3048@google.com> X-Mailer: b4 0.13-dev-099c9 Message-ID: <20231214-kunit_bus-v3-3-7e9a287d3048@google.com> Subject: [PATCH v3 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 X-Spam-Status: No, score=-8.4 required=5.0 tests=DKIMWL_WL_MED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE, USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on howler.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (howler.vger.email [0.0.0.0]); Thu, 14 Dec 2023 00:49:55 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1785246568343794252 X-GMAIL-MSGID: 1785246568343794252 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 Acked-by: Kees Cook --- 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 +#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,8 +635,6 @@ static void overflow_allocation_test(struct kunit *test) check_allocation_overflow(devm_kmalloc); check_allocation_overflow(devm_kzalloc); - device_unregister(dev); - kunit_info(test, "%d allocation overflow tests finished\n", count); #undef check_allocation_overflow } From patchwork Thu Dec 14 08:49:18 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Gow X-Patchwork-Id: 178578 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:3b04:b0:fb:cd0c:d3e with SMTP id c4csp8403396dys; Thu, 14 Dec 2023 00:50:17 -0800 (PST) X-Google-Smtp-Source: AGHT+IHQqkBb6Qh56eDBLI0XxUeN082bGy7UV7uncEFsqQjqZrfSHHfcF0D+XMp5ig4LMosJKJw5 X-Received: by 2002:a05:6830:2086:b0:6d9:d8b1:dc4a with SMTP id y6-20020a056830208600b006d9d8b1dc4amr10259311otq.5.1702543817400; Thu, 14 Dec 2023 00:50:17 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702543817; cv=none; d=google.com; s=arc-20160816; b=qfB+vmSJ66eC6KFwPHw252E9lO5j8hpDxNZg9mzYcjltlbF8UD6ADjnbk3eJ4dvybw bwUkRUPFX9i/OSP2qgvGlgR1wClVnlHyddYQOKBphQvCnxal8qCZKUKsfC1GxRnop8py 28uy1ps3ck41ir/Qau6vgHp+W9Meq3vcao0L4OclI5qrfCe0+1ZEquEY3xIvXlhT50Y8 S1B3xhjSUrt4sk1ceu7cZbq/GIVm4qOVXp6BETL8/HIYiMtJz4p8kOB7mTnIydkviAY1 icn1ZiGHqIPWMeTUMT25RAbw6rXpNXYqulnWiyoMrXNApsanKBFzgrH5EyV3djb4cVw3 KrNA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:message-id:references :mime-version:in-reply-to:date:dkim-signature; bh=d4MXXH5cbPxzFQMx+eKCBMLxcs0KvqjK2kPkW0nLFkA=; fh=FBRasfUlr9n+nXXXlXR1Gg7/LVBbWP8wdjqI9r8Tmds=; b=HCjUlwh/iALdQAO8rmC3ANwRDZ6PWGjS4vP5Y9CiE0NIWeKxnRTX6R0cJdScRrBHcn DFJHQaAtrrweueOtC3PHEG/xo42YkGEdpu/1Nx7yEGQgusWQTab9qzu1h046BgAQGT0z gE6DbhMZ3h5IyLZU+fLKxJ9KTJfOy7DGc6TB1KOUol9GIqMSRW9zsWKqvsP7N79WamPQ p16g2nKqbewWt9bPenQn3mXb6CjSHAWtMsX2wqZbR9fQLaoJkjiHPk7e813PNzpyHcGW vOvd2G1jrTMul6/Ls6Dl8wIR92f0DZi4Zetnp9OW6HPTiYgIGPxrUv0Z0r2mDd6kFMNL ERZQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b="4ZX/fbYz"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:3 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from lipwig.vger.email (lipwig.vger.email. [2620:137:e000::3:3]) by mx.google.com with ESMTPS id s66-20020a632c45000000b005b881cc9498si11117661pgs.339.2023.12.14.00.50.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 14 Dec 2023 00:50:17 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:3 as permitted sender) client-ip=2620:137:e000::3:3; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b="4ZX/fbYz"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:3 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by lipwig.vger.email (Postfix) with ESMTP id 83FAF80EB848; Thu, 14 Dec 2023 00:50:14 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at lipwig.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1443440AbjLNItu (ORCPT + 99 others); Thu, 14 Dec 2023 03:49:50 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58662 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1443447AbjLNIts (ORCPT ); Thu, 14 Dec 2023 03:49:48 -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 5514411F for ; Thu, 14 Dec 2023 00:49:54 -0800 (PST) Received: by mail-yw1-x1149.google.com with SMTP id 00721157ae682-5e300db4a48so15803117b3.2 for ; Thu, 14 Dec 2023 00:49:54 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1702543793; x=1703148593; 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=4ZX/fbYz+jcdNJSoUwRjqLWu4YDj+ejUob64+sziDztDZ7NKc5SO32YqEIGQxIRuvs h/Xk4VO7Qrt8JDiA4c4VSOSo0rTWndzgfvsSKTPpdduuM5J2/xnFmxOmW7J+6bUCoDRJ AHkp0b6Fr4JsWt2L935vdj4FA8b5hEBgCrZ+WKwMZzIej9mVBWkmLeQixYNo4vQzYxzd ND+xt1EPCa7zTfh4iuSQ7EZjx6ft/xH/2y9MSph9j6+I9zkqjuBdb1g5bGUm0y1uSeoc UQaRzqrTKMsSSV6bF9UxmB6mB6HqLdjAH9byk505Y3u/IHvl//1O75Tv2iKe5jKHDgsE 5YNA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702543793; x=1703148593; 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=rM7L2Fx/sk5bTMIstXzudYWrwMCWLBSGw1x9IUk41D0nKAMImQraW90oJCGMN5Xbk9 N82a0E5zQV2SdqLuNouIA19Wboq9HGNAnGiREnw3onTBy7NjjQilbP92P2ITOZEk8N3y WV4mw3bryj99t1W2Dex/oFyC0MsI5EuCe+zLvv66W6pw5s9g7Nqt/vWqtHFHgaAeSL6M 0LH2Uo5e145dFRmCy3t+ZDDSfQ2E4arnMgMleSP1HIBSR4zeAmt4XBmHmPuP1xfrRnjQ BntVSy7/QxQuldk5TJ+XhKLvcIHq4+wHO+OPsWiLul8rUyKh4sA4X0O7k7eJ+9IrWE8D iCOw== X-Gm-Message-State: AOJu0YxyKTmbhTI+xsShqIiz/A4WiDGE/dAlFEnZKLdz9j8vprdh/L4x AuPjra4GT0Av9fPh327Lb+GeWzLnDiq0Qg== X-Received: from slicestar.c.googlers.com ([fda3:e722:ac3:cc00:4f:4b78:c0a8:20a1]) (user=davidgow job=sendgmr) by 2002:a05:690c:891:b0:5d7:a8b2:327 with SMTP id cd17-20020a05690c089100b005d7a8b20327mr112986ywb.7.1702543793391; Thu, 14 Dec 2023 00:49:53 -0800 (PST) Date: Thu, 14 Dec 2023 16:49:18 +0800 In-Reply-To: <20231214-kunit_bus-v3-0-7e9a287d3048@google.com> Mime-Version: 1.0 References: <20231214-kunit_bus-v3-0-7e9a287d3048@google.com> X-Mailer: b4 0.13-dev-099c9 Message-ID: <20231214-kunit_bus-v3-4-7e9a287d3048@google.com> Subject: [PATCH v3 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 X-Spam-Status: No, score=-8.4 required=5.0 tests=DKIMWL_WL_MED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE, USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lipwig.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (lipwig.vger.email [0.0.0.0]); Thu, 14 Dec 2023 00:50:14 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1785246586070108674 X-GMAIL-MSGID: 1785246586070108674 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 /* ===== HELPER FUNCTIONS =================================================== */ @@ -21,26 +22,19 @@ */ 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); } /* From patchwork Thu Dec 14 08:49:19 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Gow X-Patchwork-Id: 178579 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:3b04:b0:fb:cd0c:d3e with SMTP id c4csp8403704dys; Thu, 14 Dec 2023 00:51:13 -0800 (PST) X-Google-Smtp-Source: AGHT+IG4kgDOdHH5Q4+vuACBKWCoIAWrkCLGfJIKPViKdLockqpwunJXsLzrxhw7mog6bw7Bm1Cl X-Received: by 2002:a05:6a20:b3aa:b0:18f:e1f:32ce with SMTP id eg42-20020a056a20b3aa00b0018f0e1f32cemr9689208pzb.115.1702543873229; Thu, 14 Dec 2023 00:51:13 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702543873; cv=none; d=google.com; s=arc-20160816; b=jxYRwFWAvgkxaZbl4akKkZwIIJEXHEszR/ezPgeYS7LhMVSw3MH8kE3dUYXBlDHDDk HEfklUfcOYYuAQh4SJGu2ztL4FDMHef1SyBYBWrZmlTt1FqyO01E3rfanvPVIv9id18S vcmSw7qZlwTtcgAApUAOC6VgkcFZt9HXKnbsGMpVLOwoyAGc3hZ2id3hYCLlrXT4kpu/ fEUwGh2c/WOe0Mv8EbEYFyv01G3cPpyzhT2ENW8fyNYDx9ZxjQbsS1E8PreckbMix58d QWO97Xv1Zlydp0pRY332OYg9weKbCfZFlD0cYEZBHMHIZ+CyZIiDPIy/PDBL4HQOCio8 X4DA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:message-id:references :mime-version:in-reply-to:date:dkim-signature; bh=3OlTPqlli2uCG1E/b+wjlxbRhYFvKODMjutJLmsxab0=; fh=s2ufp5odxBiVI1DugGmR9pnJ96PDI7y2LhWfMqzKBMk=; b=rOV/GqRXLrozhRjFCpxCQCyQG1seTVRo+Xg12YOpY4EJPhEi8t1jflJ62idIHGU279 bbESZvtaQ90ktxUfkqZs9hYR5y0hXJ1TNjxttVluw7RSyI0VhsJu1qFzZ+YS/fua1r/v gxjzFjKVsIPKOcI8RozBcITlQpU4Dt6w/w2kSeBltchhz1wRT8wv+j6YPB1Buf9jWx0U Ja2sTPi2XUrmXP1RIeLisYS4yMj3Kc2k86d0a7kU7HPxSZwsTaDqewgZRBo9d3cZPlpr UKXw5lUVjLyb+B1IrJbHKpjVbqOHNy9NtNpi2O1QfDNahq/cOCegc11f1KG5sDYsJeW+ 48Pw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=bTn6W0H2; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.34 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from howler.vger.email (howler.vger.email. [23.128.96.34]) by mx.google.com with ESMTPS id c15-20020a634e0f000000b005c6075f8a31si10698705pgb.153.2023.12.14.00.51.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 14 Dec 2023 00:51:13 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.34 as permitted sender) client-ip=23.128.96.34; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=bTn6W0H2; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.34 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by howler.vger.email (Postfix) with ESMTP id 96C0C82FA5B4; Thu, 14 Dec 2023 00:50:31 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at howler.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1443436AbjLNIuE (ORCPT + 99 others); Thu, 14 Dec 2023 03:50:04 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54582 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1443452AbjLNItx (ORCPT ); Thu, 14 Dec 2023 03:49:53 -0500 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 DFB8811B for ; Thu, 14 Dec 2023 00:49:58 -0800 (PST) Received: by mail-yb1-xb4a.google.com with SMTP id 3f1490d57ef6-dbcc50d7dd3so2360499276.2 for ; Thu, 14 Dec 2023 00:49:58 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1702543798; x=1703148598; 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=3OlTPqlli2uCG1E/b+wjlxbRhYFvKODMjutJLmsxab0=; b=bTn6W0H2un/UrpbYru9M2NKox2ChTjwRO0EtM78EaaKaJm6EIOM1H/kueNK2ApH0Dn ET8BHm4hdPyim67TZ08fFqna4qZOA67ILFetdp1Qnp605MnLLg0Dz0fuTHhzOkoEFddy ktGbX53nlxc5RFUcmS2NtRsyJpa4eZXTuvDXdbU+xapYrwIXjpq1dgksborzldLqojyX /1viJoOJ2boCzdi/+B0Cdrgha/YFj8aGtmjH+5t3VrLbcnb06kYjp2J39hMKDtSYjgtK ATGfEOn1IrHjopKjhPcojcBc8NUxIWan2xkqxZKEoJBaZaG12iyScJLNDL5J6oY2K+8h cRuA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702543798; x=1703148598; 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=3OlTPqlli2uCG1E/b+wjlxbRhYFvKODMjutJLmsxab0=; b=nFEFVPXDy+NWSJ1jAQQ+cpDiqEHPRBaLAw/b6q98dJF1CmRPQKpXTh7BBFihByJ/Cq fWIXeVU3NEmgW2jdtqm+YjluwqaUYTFRrZurFy+7z/w7igfgevcCXGyBpR4G2r6yQWC0 klPoex+N0nKiqluvM/Kv9uTMBJpPHxrRY7eXbSduMKJNflCHJlrbHd0YpkYsisS4yMwO jjLuJCf89CpZb7o5m/sAnUF/uwyJiwQGhAXTAkUyiVxExApTraZj5t3OZBleHWKe8Dey DxY3tIz9C2Gc5sOe49ha9H/tNqO4ilZGMjCE60ByGUewAfVRgC4qvf0DmEN/DZhk4f+y 3c6Q== X-Gm-Message-State: AOJu0YxcaCT9oYg3ydCs05PyRcFjjHzSM6JkRXeKbnyTnSpcMLUeLyIx IdW3Q50rjg54bBdC0aiL5Ycw4ibSF+0rCg== X-Received: from slicestar.c.googlers.com ([fda3:e722:ac3:cc00:4f:4b78:c0a8:20a1]) (user=davidgow job=sendgmr) by 2002:a25:3103:0:b0:dbc:cf6d:9a50 with SMTP id x3-20020a253103000000b00dbccf6d9a50mr41005ybx.10.1702543798003; Thu, 14 Dec 2023 00:49:58 -0800 (PST) Date: Thu, 14 Dec 2023 16:49:19 +0800 In-Reply-To: <20231214-kunit_bus-v3-0-7e9a287d3048@google.com> Mime-Version: 1.0 References: <20231214-kunit_bus-v3-0-7e9a287d3048@google.com> X-Mailer: b4 0.13-dev-099c9 Message-ID: <20231214-kunit_bus-v3-5-7e9a287d3048@google.com> Subject: [PATCH v3 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 X-Spam-Status: No, score=-8.4 required=5.0 tests=DKIMWL_WL_MED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE, USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on howler.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (howler.vger.email [0.0.0.0]); Thu, 14 Dec 2023 00:50:31 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1785246644279240813 X-GMAIL-MSGID: 1785246644279240813 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 --- 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/tests/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 +#include #include #include @@ -15,28 +16,6 @@ static const struct drm_mode_config_funcs drm_mode_config_funcs = { }; -static int fake_probe(struct platform_device *pdev) -{ - return 0; -} - -static struct platform_driver fake_platform_driver = { - .probe = fake_probe, - .driver = { - .name = 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 = platform_driver_register(&fake_platform_driver); - KUNIT_ASSERT_EQ(test, ret, 0); - - ret = kunit_add_action_or_reset(test, - kunit_action_platform_driver_unregister, - &fake_platform_driver); - KUNIT_ASSERT_EQ(test, ret, 0); - - pdev = platform_device_alloc(KUNIT_DEVICE_NAME, PLATFORM_DEVID_NONE); - KUNIT_ASSERT_NOT_ERR_OR_NULL(test, pdev); - - ret = kunit_add_action_or_reset(test, - kunit_action_platform_device_put, - pdev); - KUNIT_ASSERT_EQ(test, ret, 0); - - ret = platform_device_add(pdev); - KUNIT_ASSERT_EQ(test, ret, 0); - - ret = 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); @@ -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 = 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);