Received: by 2002:ab2:60d1:0:b0:1f7:5705:b850 with SMTP id i17csp1499803lqm; Thu, 2 May 2024 18:04:10 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCWO/3REAxFUZF/gAlATG8nAVW9QvvbRGmQsJgqNTnIM13J0WeYRmVdMN63EfjlwoxZEBqOy62I9kcok2gZmky1BvPHeH+sLR/KDtkxyLg== X-Google-Smtp-Source: AGHT+IFj2gfnYKWB/vfqI70yDan/zMnHAI+gvMLvZ4Si3ETSV9eyxzdLh1swYnz067hnvYLysPbI X-Received: by 2002:a17:906:bc52:b0:a58:eb9a:420d with SMTP id s18-20020a170906bc5200b00a58eb9a420dmr2956461ejv.17.1714698250747; Thu, 02 May 2024 18:04:10 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1714698250; cv=pass; d=google.com; s=arc-20160816; b=d57WIIs1Vc6OMOQKBNv68xY24l4+YW5ELMESdjxcKMPJaZv4woxfwfqWIlATGnc/ck Ph8fOSdU8jxCCdhml8s+OSGEZbWCxjohKM0EsV0NB7RlBAYmfFx3UruWV/V0rz+T1/2b doZwVmZAkt+Fc4geOR6ntbvktdYZWRwMbuwF7CbO5ffEZJIP1HQu4965NOtF6X/OyYsJ /AKFaVqqzRTFVTCRKCdbrButuqNuJ6TgDX2sZ0nESKtH2ti94azrdVAdmhifamxskiTs z3KIrR9Zd4O1Oej8RERUUC+l7HFXdlCZYLAxXGaHRWe9S0r7jyAo87tUtpQU6Q9X1rgU DDNg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=user-agent:date:to:cc:from:subject:references:in-reply-to :content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:message-id:dkim-signature; bh=GnBmooeKftmmhN3Q2OrkR4e38e3ntj2jzcsQWP8RQsI=; fh=1vk4sFoWXWxeguEKo6GgxbSvu/rmcmO+e5+bIFHQOE4=; b=ouUFwvPWnOs2Ay0DGcI5wRKhqiLOw3vZVRqvlCtkk1m/J7G7Du99FroCNM3DLcPA1e BNUvWU1IYBX12CpAOo9WW6mPPiqBj7sLOKC/QeKFm3uLEkUzMu7qQSwAEfFUJNKXbnZn SoKl8TUP7qVaqxLvrVOGYJjV5RC3YsHNCKcJO39RL95Q8ZvY1ubHJzJnzef0pdxIn77u S2LXhQ7vNO2tGWFqwlNDWHXTe3YXCko7swWSPXRarwsgCRltFriYTMd8DxUuBtZj2lDl DX6rdK5RXD5IVzw0QVmhe6hI5Erpp5MAXJbC5bz6ZaQ1XD5xAuPKl7nKHAGiq8WMI3zD tXzg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=AnSEQHyT; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-167129-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-167129-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id gb40-20020a170907962800b00a5993bb31ffsi4013ejc.14.2024.05.02.18.04.10 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 02 May 2024 18:04:10 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-167129-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) client-ip=147.75.80.249; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=AnSEQHyT; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-167129-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-167129-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id 419A01F22C0D for ; Fri, 3 May 2024 01:04:10 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 60F44DF5B; Fri, 3 May 2024 01:04:01 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="AnSEQHyT" Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 182818F47; Fri, 3 May 2024 01:03:59 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714698240; cv=none; b=NUoob3SoqVvLw88VJbcBtpocxJbnmtUri7yGD/QM/M/dNOsH9QktF4vi+4OmITXPU5xSOpAov56C9W278NYHenXGK3y0TeOwpGm4uNPbBTyT8QdnnVlecmq03L/o41CaWg0SjvfH3sATKXiG9NdWShpzL9cQ/CKqL+iv2d8mWZk= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714698240; c=relaxed/simple; bh=FGn9Nc/maQsdZyOrXW+2uXst/XbmpaxL7uMg4iUh4zU=; h=Message-ID:Content-Type:MIME-Version:In-Reply-To:References: Subject:From:Cc:To:Date; b=Vfqf3ZUMA1kbax+oSQVWz3C9J42pDEuXb8grHz0pyC+mb+zYTtr6GY80ZNXeA0TGgpKC/8PJs4ijIUHen+3XwnQ6wbke+9PNFB2LB+yCGnh9PXqOGPuDl6SzjtPogs6DyGEwuPBaW/H7PLJWJxOMyPo3A3N7vnx4Hir2bEouH3Q= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=AnSEQHyT; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 71828C113CC; Fri, 3 May 2024 01:03:59 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1714698239; bh=FGn9Nc/maQsdZyOrXW+2uXst/XbmpaxL7uMg4iUh4zU=; h=In-Reply-To:References:Subject:From:Cc:To:Date:From; b=AnSEQHyTTCGHh7q/8NojpB8iXu+pmFD3q8+Qt5iDoSxU44CaD9uP7HES+rqAfHtOj D+FGPt62oX9GyAGpLUoEwvJcndLS2sn8wqGwAT5A8OOxhsiWCPn5E82SWqQee/z7te 15Fqb7S6RL5Kwyrths1IyL4Vkkmn6ku7y9sRQBf3rEj8AM/jUSOuNgwV8w7iwTS8l8 /wMtvPmyBk0nc3ZGf2onYV3qeNKpRw/0uxJlLZC7N/2JhwAo56BKf/lRnC2TuLyfQY FDKjH+/W4XsZIPvpdsd/wS3Z9eU4QeifoPxi0bX4KsEtkuTttJFEGvL0SmvlnaeY5Y OxZQySt3ibrpw== Message-ID: <431171223433496db0a85072be5c83ba.sboyd@kernel.org> Content-Type: text/plain; charset="utf-8" Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable In-Reply-To: References: <20240422232404.213174-1-sboyd@kernel.org> <20240422232404.213174-6-sboyd@kernel.org> Subject: Re: [PATCH v4 05/10] platform: Add test managed platform_device/driver APIs From: Stephen Boyd Cc: Michael Turquette , linux-kernel@vger.kernel.org, linux-clk@vger.kernel.org, patches@lists.linux.dev, kunit-dev@googlegroups.com, linux-kselftest@vger.kernel.org, devicetree@vger.kernel.org, Brendan Higgins , Rae Moar , Greg Kroah-Hartman , Rafael J . Wysocki , Rob Herring , Saravana Kannan , Daniel Latypov , Christian Marangi , Krzysztof Kozlowski , Conor Dooley , Maxime Ripard To: David Gow Date: Thu, 02 May 2024 18:03:57 -0700 User-Agent: alot/0.10 Quoting David Gow (2024-05-01 00:55:46) > On Tue, 23 Apr 2024 at 07:24, Stephen Boyd wrote: > > diff --git a/Documentation/dev-tools/kunit/api/platformdevice.rst b/Doc= umentation/dev-tools/kunit/api/platformdevice.rst > > new file mode 100644 > > index 000000000000..b228fb6558c2 > > --- /dev/null > > +++ b/Documentation/dev-tools/kunit/api/platformdevice.rst > > @@ -0,0 +1,10 @@ > > +.. SPDX-License-Identifier: GPL-2.0 > > + > > +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D > > +Platform Device API > > +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D > > + > > +The KUnit platform device API is used to test platform devices. > > + > > +.. kernel-doc:: drivers/base/test/platform_kunit.c > > + :export: > > diff --git a/drivers/base/test/Makefile b/drivers/base/test/Makefile > > index e321dfc7e922..740aef267fbe 100644 > > --- a/drivers/base/test/Makefile > > +++ b/drivers/base/test/Makefile > > @@ -1,8 +1,11 @@ > > # SPDX-License-Identifier: GPL-2.0 > > obj-$(CONFIG_TEST_ASYNC_DRIVER_PROBE) +=3D test_async_driver_probe.o > > > > +obj-$(CONFIG_KUNIT) +=3D platform_kunit.o > > + >=20 > Do we want this to be part of the kunit.ko module (and hence, > probably, under lib/kunit), or to keep this as a separate module. > I'm tempted, personally, to treat this as a part of KUnit, and have it > be part of the same module. There are a couple of reasons for this: > - It's nice to have CONFIG_KUNIT produce only one module. If we want > this to be separate, I'd be tempted to put it behind its own kconfig > entry. > - The name platform_kunit.ko suggests (to me, at least) that this is > the test for platform devices, not the implementation of the helper. I was following *_kunit as "helpers" and *_test as the test. Only loosely based on the documentation that mentions to use _test or _kunit for test files. Maybe it should have _kunit_helpers postfix? Following the single module design should I merge the tests for this code into kunit-test.c? And do the same sort of thing for clk helpers? That sounds like it won't scale very well if everything is in one module. Shouldn't the wrapper code for subsystems live in those subsystems like drm_kunit_helpers.c does? Maybe the struct device kunit wrappers should be moved out to drivers/base/? lib/kunit can stay focused on providing pure kunit code then. >=20 > I probably can be persuaded otherwise if you've got a strong > preference for it to stay as-is, though. >=20 > > diff --git a/drivers/base/test/platform_kunit.c b/drivers/base/test/pla= tform_kunit.c > > new file mode 100644 > > index 000000000000..54af6db2a6d8 > > --- /dev/null > > +++ b/drivers/base/test/platform_kunit.c > > @@ -0,0 +1,174 @@ > > +// SPDX-License-Identifier: GPL-2.0 > > +/* > > + * Test managed platform driver > > + */ > > + > > +#include > > +#include > > + > > +#include > > +#include > > + > > +/** > > + * platform_device_alloc_kunit() - Allocate a KUnit test managed platf= orm device > > + * @test: test context > > + * @name: device name of platform device to alloc > > + * @id: identifier of platform device to alloc. > > + * > > + * Allocate a test managed platform device. The device is put when the= test completes. > > + * > > + * Return: Allocated platform device on success, NULL on failure. > > + */ > > +struct platform_device * > > +platform_device_alloc_kunit(struct kunit *test, const char *name, int = id) >=20 > I'd prefer, personally, this be named something like > kunit_platform_device_alloc(), to match the existing > kunit_device_register() functions. >=20 >=20 > > +{ > > + struct platform_device *pdev; > > + > > + pdev =3D platform_device_alloc(name, id); > > + if (!pdev) > > + return NULL; > > + > > + if (kunit_add_action_or_reset(test, (kunit_action_t *)&platform= _device_put, pdev)) >=20 > Alas, casting function pointers to kunit_action_t* breaks CFI. It's > worth using a wrapper, which can be created with the > KUNIT_DEFINE_ACTION_WRAPPER() macro, e.g. >=20 > KUNIT_DEFINE_ACTION_WRAPPER(platform_device_put_wrapper, > platform_device_put, struct platform_device *); Thanks. I missed that. >=20 > > + return NULL; > > + > > + return pdev; > > +} > > +EXPORT_SYMBOL_GPL(platform_device_alloc_kunit); > > + > > +static void platform_device_add_kunit_exit(struct kunit_resource *res) > > +{ > > + struct platform_device *pdev =3D res->data; > > + > > + platform_device_unregister(pdev); > > +} > > + > > +static bool > > +platform_device_alloc_kunit_match(struct kunit *test, > > + struct kunit_resource *res, void *mat= ch_data) > > +{ > > + struct platform_device *pdev =3D match_data; > > + > > + return res->data =3D=3D pdev; > > +} > > + > > +/** > > + * platform_device_add_kunit() - Register a KUnit test managed platfor= m device > > + * @test: test context > > + * @pdev: platform device to add > > + * > > + * Register a test managed platform device. The device is unregistered= when the > > + * test completes. > > + * > > + * Return: 0 on success, negative errno on failure. > > + */ > > +int platform_device_add_kunit(struct kunit *test, struct platform_devi= ce *pdev) >=20 > As above, I'd lean towards naming this kunit_platform_device_add() for > consistency with the other KUnit device helpers. >=20 > > +{ > > + struct kunit_resource *res; > > + int ret; > > + > > + ret =3D platform_device_add(pdev); > > + if (ret) > > + return ret; > > + > > + res =3D kunit_find_resource(test, platform_device_alloc_kunit_m= atch, pdev); > > + if (res) { > > + /* > > + * Transfer the reference count of the platform device = if it was > > + * allocated with platform_device_alloc_kunit(). In tha= t case, > > + * calling platform_device_put() leads to reference cou= nt > > + * underflow because platform_device_unregister() does = it for > > + * us and we call platform_device_unregister() from > > + * platform_device_add_kunit_exit(). > > + * > > + * Usually callers transfer the refcount from > > + * platform_device_alloc() to platform_device_add() and= simply > > + * call platform_device_unregister() when done, but wit= h kunit > > + * we have to keep this straight by redirecting the free > > + * routine for the resource. > > + */ > > + res->free =3D platform_device_add_kunit_exit; > > + kunit_put_resource(res); > > + } else if (kunit_add_action_or_reset(test, > > + (kunit_action_t *)&platfor= m_device_unregister, > > + pdev)) { >=20 > Nit: We don't want to cast directly to kunit_action_t *, as that > breaks CFI. Can we use KUNIT_DEFINE_ACTION_WRAPPER()? >=20 > > + return -ENOMEM; >=20 > Nit: This is fine, as kunit_add_action_or_reset() only returns 0 or > -ENOMEM at the moment, but it could cause problems down the line if we > ever want to return a different error. I don't think that's > particularly likely, but it might be nicer to properly propagate the > error. I will propagate the return value. >=20 > > + } > > + > > + return 0; > > +} > > +EXPORT_SYMBOL_GPL(platform_device_add_kunit); > > + > > +/** > > + * platform_driver_register_kunit() - Register a KUnit test managed pl= atform driver > > + * @test: test context > > + * @drv: platform driver to register > > + * > > + * Register a test managed platform driver. This allows callers to emb= ed the > > + * @drv in a container structure and use container_of() in the probe f= unction > > + * to pass information to KUnit tests. It can be assumed that the driv= er has > > + * probed when this function returns. > > + * > > + * Example > > + * > > + * .. code-block:: c > > + * > > + * struct kunit_test_context { > > + * struct platform_driver pdrv; > > + * const char *data; > > + * }; > > + * > > + * static inline struct kunit_test_context * > > + * to_test_context(struct platform_device *pdev) > > + * { > > + * return container_of(to_platform_driver(pdev->dev.driver= ), > > + * struct kunit_test_context, > > + * pdrv); > > + * } > > + * > > + * static int kunit_platform_driver_probe(struct platform_device *= pdev) > > + * { > > + * struct kunit_test_context *ctx; > > + * > > + * ctx =3D to_test_context(pdev); > > + * ctx->data =3D "test data"; > > + * > > + * return 0; > > + * } > > + * > > + * static void kunit_platform_driver_test(struct kunit *test) > > + * { > > + * struct kunit_test_context *ctx; > > + * > > + * ctx =3D kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL); > > + * KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx); > > + * > > + * ctx->pdrv.probe =3D kunit_platform_driver_probe; > > + * ctx->pdrv.driver.name =3D "kunit-platform"; > > + * ctx->pdrv.driver.owner =3D THIS_MODULE; > > + * > > + * KUNIT_EXPECT_EQ(test, 0, platform_driver_register_kunit= (test, &ctx->pdrv)); > > + * KUNIT_EXPECT_STREQ(test, ctx->data, "test data"); > > + * } > > + * > > + * Return: 0 on success, negative errno on failure. > > + */ > > +int platform_driver_register_kunit(struct kunit *test, > > + struct platform_driver *drv) >=20 > As above, I'd prefer kunit_platform_driver_register() >=20 > > +{ > > + int ret; > > + > > + ret =3D platform_driver_register(drv); > > + if (ret) > > + return ret; > > + > > + /* > > + * Wait for the driver to probe (or at least flush out of the d= eferred > > + * workqueue) > > + */ > > + wait_for_device_probe(); >=20 > Personally, I don't mind if this wrapper waits here (even if it makes > it less of a 'pure' wrapper), so long as we document it. Can you think > of any cases where we explicitly want _not_ to wait in a test? >=20 I don't like it because it's not deterministic. The function doesn't take any struct device to wait for. I've already written the code to use a completion, and it works well enough so I'll just do that. Then we don't have to worry if this API goes away, or that it doesn't actually determine if the driver has probed the device.