From patchwork Mon Mar 27 11:34:02 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Matti Vaittinen X-Patchwork-Id: 13189077 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id A65B6C761A6 for ; Mon, 27 Mar 2023 11:34:14 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232334AbjC0LeN (ORCPT ); Mon, 27 Mar 2023 07:34:13 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47710 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232069AbjC0LeK (ORCPT ); Mon, 27 Mar 2023 07:34:10 -0400 Received: from mail-lf1-x12a.google.com (mail-lf1-x12a.google.com [IPv6:2a00:1450:4864:20::12a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C273B49D1; Mon, 27 Mar 2023 04:34:08 -0700 (PDT) Received: by mail-lf1-x12a.google.com with SMTP id bi9so10958555lfb.12; Mon, 27 Mar 2023 04:34:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; t=1679916847; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=aTSQxrRoE8Shcy5mtway4FQV4AF6vH1cCAm1YryA9wE=; b=iYExBj9hAxdPZi1fuhIeAn9Mq1mSWvCXJBPYUJQ32gb/LkUpqkT053vj499yRIRtZx 95T+mABLT5Kn6a529wcr00nC8WiUZ6MTP8DXETRDo3l4o13gzUT8MgEOeqgdYC6q5NCb 8rw6F/9+tH3BKaiy8/uP/M9uh8cgg/gGthZrdiILn4TEgsegonHycVqujRWzr3xXZUIv 52p/Mb3TXPXNC2+fBsKT0c+a+9WMrw66isQmtYiK5k3D4cUT+NyC5+a2JulrbG2aky3U pPgMZlks9iU9ZCIaTQmxVVSLVoZDRRxShI9qENgxGaxYC410QcMb8rGlSMGgtwWe4EYs vcHA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1679916847; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=aTSQxrRoE8Shcy5mtway4FQV4AF6vH1cCAm1YryA9wE=; b=JtsEuZPTHeAupBSbJXYUYDvcS/x6epHajhMCJiIpcNgqg8gjPUSaKwG0JCTNBBfITp newmah35r/8kPmR07rHYQUsbEaDoEmtbV85zyzUQqCHC5KxbfuBn+TOuAPvzEErmXatO ISM/9sRHGIx0YqJFxt1L8eiojySsywHrg270iIY7sN7xPTXV5wBZ5fCchI0FTHkJk0hq Ff99aWCv0Q9ndOUFv1i2QMp/mpDU1w9TbTFu92moCYaVocagRsxtU6IRICcdKeN5tlpN qaGsIwSvVtVs2bZRN7zfCSSraj2EZISpV+5btXfdtuLIn3sIpgaeMdL0NJV0q0Kz5ML+ NtOg== X-Gm-Message-State: AAQBX9cGRhzgqa73k4sztRRflp3QCRALTalxkLZSuFzCItGi3nPQ/0H7 f+CfXsvOh7iycjIWbPOE6Xo= X-Google-Smtp-Source: AKy350YonYc0d8QJUr+vn6SSI4APqCbjccljhCEBZuYvQG0GJny1lnz9OR43eJJ6QXOHUgqIE91vhw== X-Received: by 2002:a05:6512:11c3:b0:4d8:6f37:2611 with SMTP id h3-20020a05651211c300b004d86f372611mr3137200lfr.20.1679916846842; Mon, 27 Mar 2023 04:34:06 -0700 (PDT) Received: from dc75zzyyyyyyyyyyyyyyt-3.rev.dnainternet.fi (dc75zzyyyyyyyyyyyyyyt-3.rev.dnainternet.fi. [2001:14ba:16f3:4a00::1]) by smtp.gmail.com with ESMTPSA id j7-20020a19f507000000b004db3e2d3efesm4646907lfb.204.2023.03.27.04.34.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 27 Mar 2023 04:34:06 -0700 (PDT) Date: Mon, 27 Mar 2023 14:34:02 +0300 From: Matti Vaittinen To: Matti Vaittinen , Matti Vaittinen Cc: Brendan Higgins , David Gow , linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, kunit-dev@googlegroups.com, Stephen Boyd , Jonathan Cameron , Greg Kroah-Hartman , linux-iio@vger.kernel.org Subject: [PATCH v6 3/7] kunit: Add kunit wrappers for (root) device creation Message-ID: References: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: Precedence: bulk List-ID: X-Mailing-List: linux-iio@vger.kernel.org A few tests need to have a valid struct device. One such example is tests which want to be testing devm-managed interfaces. Add kunit wrapper for root_device_[un]register(), which create a root device and also add a kunit managed clean-up routine for the device destruction upon test exit. Special note: In some cases the device reference-count does not reach zero and devm-unwinding is not done if device is not sitting on a bus. The root_device_[un]register() are dealing with such devices and thus this interface may not be usable by all in its current form. More information can be found from: https://lore.kernel.org/dri-devel/20221117165311.vovrc7usy4efiytl@houat/ The use of root-devices in the kunit helpers is intended to be an intermediate solution to allow tests which do not require device to sit on a bus avoid directly abusing the root_device_[un]register() while proper kunit device solution is being worked on. Related discussion can be found from: https://lore.kernel.org/lkml/CABVgOSmx3A4Vwos2_8xO-XQrQAw5gvY0nc5zLpLmcJ7FtA-dTQ@mail.gmail.com/ Signed-off-by: Matti Vaittinen --- Change history: v5 => v6: - Kunit resource-managed root_device creation wrapper (new patch) Please note: This patch uses root-devices (as was suggested) until there is a proper dummy device creation mechanism added in kunit. The root devices are embedded in kunit wrappers to simplify replacing the root-devices with proper solution when it is available. David Gow has sent out an RFC[1] which should implement these helpers using not-yet-in-tree deferring API. This RFC aims to support kunit_device which should be _the right thing to do_. I added this implementation here because it may (or may not) take a while for the David's RFC to make it's way in-kernel. So, in order to not delay this series I added these helpers which use the existing kunit resource management for clean-up while the new deferring kunit API is not yet in place. [1] https://lore.kernel.org/linux-kselftest/20230325043104.3761770-1-davidgow@google.com/T/#mf797239a8bce11630875fdf60aab9ed627add1f0 --- include/kunit/device.h | 18 ++++++++++++++++++ lib/kunit/Makefile | 3 ++- lib/kunit/device.c | 36 ++++++++++++++++++++++++++++++++++++ 3 files changed, 56 insertions(+), 1 deletion(-) create mode 100644 include/kunit/device.h create mode 100644 lib/kunit/device.c diff --git a/include/kunit/device.h b/include/kunit/device.h new file mode 100644 index 000000000000..f02740b7583b --- /dev/null +++ b/include/kunit/device.h @@ -0,0 +1,18 @@ +/* SPDX-License-Identifier: GPL-2.0 */ + +#ifndef __KUNIT_DEVICE_H__ +#define __KUNIT_DEVICE_H__ + +#include + +struct device; + +/* Register a new device against a KUnit test. */ +struct device *kunit_device_register(struct kunit *test, const char *name); +/* + * Unregister a device created by kunit_device_register() early (i.e., + * before test cleanup). + */ +void kunit_device_unregister(struct kunit *test, struct device *dev); + +#endif diff --git a/lib/kunit/Makefile b/lib/kunit/Makefile index cb417f504996..64449549b990 100644 --- a/lib/kunit/Makefile +++ b/lib/kunit/Makefile @@ -6,7 +6,8 @@ kunit-objs += test.o \ string-stream.o \ assert.o \ try-catch.o \ - executor.o + executor.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..425f6d62ebd7 --- /dev/null +++ b/lib/kunit/device.c @@ -0,0 +1,36 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include +#include + +#include + +static void kunit_device_drop(struct kunit_resource *res) +{ + root_device_unregister(res->data); +} + +struct device *kunit_device_register(struct kunit *test, const char *name) +{ + struct device *dev; + + dev = root_device_register(name); + if (IS_ERR_OR_NULL(dev)) + return dev; + + return kunit_alloc_resource(test, NULL, kunit_device_drop, GFP_KERNEL, + dev); +} +EXPORT_SYMBOL_GPL(kunit_device_register); + +static bool kunit_device_match(struct kunit *test, struct kunit_resource *res, + void *match_data) +{ + return res->data == match_data; +} + +void kunit_device_unregister(struct kunit *test, struct device *dev) +{ + kunit_destroy_resource(test, kunit_device_match, dev); +} +EXPORT_SYMBOL_GPL(kunit_device_unregister);