Received: by 2002:ab2:6f44:0:b0:1fd:c486:4f03 with SMTP id l4csp226434lqq; Thu, 13 Jun 2024 00:50:49 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCWVMyqvkNmKzMFZow2R0dgc2uHcJF7t5IVZToMMe7ToHEjC6AtVdeWXF9iHWVcVCNDdTYUduKXMnDph1Jb5zBtpICl3s7pGzi8bbrPsSg== X-Google-Smtp-Source: AGHT+IFm4CAjLLJet7JB20ja2JqEVxMja3jwjbNnXx88yrQSgHki1L3K6QTpEEsE2oTQyl79VCdC X-Received: by 2002:a05:6102:320a:b0:48c:4280:4495 with SMTP id ada2fe7eead31-48d91cc0dc4mr3929763137.0.1718265048933; Thu, 13 Jun 2024 00:50:48 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1718265048; cv=pass; d=google.com; s=arc-20160816; b=vbJzVwp1I0HEw0Ufz3qUisu6tnBDYi49a5bsJpYd0WjYfX7G67scac7TY91Obv7fNe 1kMVf6NnZsV/NI0DkgAmWBT/RcsCXolQo+SzQQUydki3D73AtmIiA5fqahrDwLYQZ45V oE2KAzwsfg+ICPgMT5mXrIio8XF1+ZrkOt7aMJWuib3EPkMp1FysNuOdGUahTdzpIqmx fx6EUWwR6vQovCYhAIiwKlVx7isD20/m5YvWnAY9KpH6lGymO4w8M9JslXRATtJowzFF 946ComNuHC1XTM973Xa7Fgyn0V+16t0P2uTd9WBzUrZHXMt1sHsdI+8S6/1PnLSTSIMe L+jg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:list-unsubscribe:list-subscribe:list-id:precedence :dkim-signature; bh=SY7pCAGtcA0yESUUmdfWIO1Cm4VA6AlzYqUL7FMoXEY=; fh=V2D3tnzXXNRbQa9gAk428/5xNWRyANMcqbEv3a1iOr4=; b=kpvJ27WFZuMetR3LFqqec5Fap2ssH5GUVvSnMD9g2+cVvHWzN8bQ1OqpE3TmOKJpSv pFLDFWtkPy1cZS4llm0DBKgkGwxjbG7P8iEDES0RU3a2CrsjM1sLPV+KIiuNQjLlf/x2 Yo2EA7u0PDGlpzbzUODaxpd9zjoqPHwPbIg0hEE6mFuuWGDn+CSLezMKa/+Y57lgOtPM uDyrvifD5MA/BRSgXIKjgms7P+RUdBH4TxMHf5FVpo1ej6MZqGVfP1MjrhDM8H4dYJeo A0EmQhAzIDBPuXtpb0xcOSqE4H+ZY3ujUGZl96CF4R2Wmc24ICV0HPbWZcHBRIqOR8rC L7uQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=Z2JCXG4a; arc=pass (i=1 spf=pass spfdomain=google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-212749-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-212749-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id af79cd13be357-798aac9b8b0si86272485a.56.2024.06.13.00.50.48 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 13 Jun 2024 00:50:48 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-212749-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=Z2JCXG4a; arc=pass (i=1 spf=pass spfdomain=google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-212749-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-212749-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com 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 ny.mirrors.kernel.org (Postfix) with ESMTPS id 8FB2D1C20E91 for ; Thu, 13 Jun 2024 07:50:48 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 0EC4D13C904; Thu, 13 Jun 2024 07:50:35 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="Z2JCXG4a" Received: from mail-qt1-f176.google.com (mail-qt1-f176.google.com [209.85.160.176]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 6E39013C80C for ; Thu, 13 Jun 2024 07:50:30 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.160.176 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718265032; cv=none; b=RWhrXIs2LtMD4BUWr4dV5OdvzJDHfqvnz6ZzDUijq45fF68DeblToRc1rMFDCKhx90DYztuK0IfVbrUtlKVAGvGNWIBRRR40n6443DLkuxHR5ADkF2r3OS+Z2G9whBBkwRIt4Qynkbek2pjJ1QZSxF5cIegj5lafbMX5qcK6TzU= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718265032; c=relaxed/simple; bh=M7nxxW3KPmjfnsrOk0OjpXkazNisEfEFgKodUrit/+s=; h=MIME-Version:References:In-Reply-To:From:Date:Message-ID:Subject: To:Cc:Content-Type; b=WZki9QDiIzkfH+V1Mllq76tZPlnwe69EN6dQ+rWeXpTOIjg7ea6e7jGyX/4b0S6up1YddZJani26u0/VMVl6qm2jnBHY+liuODEB8Oq7z++8oMkXBRLFEw6p8MjkokUT3JPm0qr53yL/W9m1YoBtPqh5HuztL7vUuM6cBHsybSI= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=Z2JCXG4a; arc=none smtp.client-ip=209.85.160.176 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=google.com Received: by mail-qt1-f176.google.com with SMTP id d75a77b69052e-4400cc0dad1so348181cf.0 for ; Thu, 13 Jun 2024 00:50:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1718265029; x=1718869829; darn=vger.kernel.org; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=SY7pCAGtcA0yESUUmdfWIO1Cm4VA6AlzYqUL7FMoXEY=; b=Z2JCXG4ac6q/xb0AmvANVtvbZCJG7iK55h4iRbKcXZrJ5k8RWjbzSx75bVgAkOn6Fd s2A0WITgpSs435a2IOHVd65khF3mMmabjFWY06R0C2eCA28934RpzTtzCwaaGhryT8R6 hc3kObDus7CsxQo1vMzGpVqqxMFPQmCcUNB++7FCyfHqjdB9pP2pusgLVL9cGyWWLRId YVaiULxgVzOU7I1aOX5yXdy8umxIaSZBDer9CuIHrfiQfUe8a/18fTsbekRS73hJ7J6g O39ZoT82KhCM6lNHvh5e/rP1U65Hbhw8n9Lu6gWvJxMRDbsiBU23HDB0Mgdn6ZyPJDEw 5CxA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1718265029; x=1718869829; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=SY7pCAGtcA0yESUUmdfWIO1Cm4VA6AlzYqUL7FMoXEY=; b=VlYnj6Zjv+Ve2UuP0QaTS1FfozPShHKfsnKkmCX9ovAEYme1tt/cDHA4WdPmI1uqkZ VgrsryKC2Vfyq+CrxnolIqKs17VGNgaX5b9QGv/jVIdbkSAQm7EgewGfwljF5qx5rp6N nZCisIBi1Gp3Sj/pCFLQgXkWQ7hVarXNTY5ehYJoTknSbbwetO+W9kn8RpZSj/rCWFNW XZ5eJRIobhdJRG7PWmxEYcAvhQBffr0aT9GxfGSpowukr99I7Jrpo23uBy6NeklkPO0J LgmwyaXvL++QIqOqD+v3sn75sgoGoI/gMMeISdnXY2bquC3A0MgcwEYg2HhZ1BPToBcZ ygpw== X-Forwarded-Encrypted: i=1; AJvYcCUjHmWFMYCOCP+Xsg9gkCV3ZCtBfDyW2x6VvKYB5GsLQw2m3Vg67keof70S/GWp3hmPsGYM6JKP4yGlh2SXa/gDo98N/f4B4NLyfesw X-Gm-Message-State: AOJu0YxL9O0rqoF/tq9i19RMjMaEWCHdNBWWPQK3K2EUd0DW9HzUUblU yspc+ev6KDZ96wfA/GmtbUDZY/dErsSDEJt/EywW6HZErDrrSAhV2GXPzcSc+XC4zDnz0L7UWJM a2tLB4VDpXae4NCALj+HpDb0gPDKU8IWp8gJ6iLWcMfjqvNw5masdvgM= X-Received: by 2002:a05:622a:1c08:b0:440:4673:9854 with SMTP id d75a77b69052e-441eea6061bmr1292471cf.21.1718265029167; Thu, 13 Jun 2024 00:50:29 -0700 (PDT) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 References: <20240603223811.3815762-1-sboyd@kernel.org> <20240603223811.3815762-7-sboyd@kernel.org> In-Reply-To: <20240603223811.3815762-7-sboyd@kernel.org> From: David Gow Date: Thu, 13 Jun 2024 15:50:18 +0800 Message-ID: Subject: Re: [PATCH v5 06/11] platform: Add test managed platform_device/driver APIs To: 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 Content-Type: multipart/signed; protocol="application/pkcs7-signature"; micalg=sha-256; boundary="000000000000bafcbf061ac0c010" --000000000000bafcbf061ac0c010 Content-Type: text/plain; charset="UTF-8" On Tue, 4 Jun 2024 at 06:38, Stephen Boyd wrote: > > Introduce KUnit resource wrappers around platform_driver_register(), > platform_device_alloc(), and platform_device_add() so that test authors > can register platform drivers/devices from their tests and have the > drivers/devices automatically be unregistered when the test is done. > > This makes test setup code simpler when a platform driver or platform > device is needed. Add a few test cases at the same time to make sure the > APIs work as intended. > > Cc: Brendan Higgins > Cc: David Gow > Cc: Rae Moar > Cc: Greg Kroah-Hartman > Cc: "Rafael J. Wysocki" > Signed-off-by: Stephen Boyd > --- This looks great, thanks! Reviewed-by: David Gow Cheers, -- David > Documentation/dev-tools/kunit/api/index.rst | 5 + > .../dev-tools/kunit/api/platformdevice.rst | 10 + > include/kunit/platform_device.h | 20 ++ > lib/kunit/Makefile | 4 +- > lib/kunit/platform-test.c | 223 +++++++++++++ > lib/kunit/platform.c | 302 ++++++++++++++++++ > 6 files changed, 563 insertions(+), 1 deletion(-) > create mode 100644 Documentation/dev-tools/kunit/api/platformdevice.rst > create mode 100644 include/kunit/platform_device.h > create mode 100644 lib/kunit/platform-test.c > create mode 100644 lib/kunit/platform.c > > diff --git a/Documentation/dev-tools/kunit/api/index.rst b/Documentation/dev-tools/kunit/api/index.rst > index 282befa17edf..02b26f5e8750 100644 > --- a/Documentation/dev-tools/kunit/api/index.rst > +++ b/Documentation/dev-tools/kunit/api/index.rst > @@ -10,6 +10,7 @@ API Reference > resource > functionredirection > of > + platformdevice > > > This page documents the KUnit kernel testing API. It is divided into the > @@ -36,3 +37,7 @@ Driver KUnit API > Documentation/dev-tools/kunit/api/of.rst > > - Documents the KUnit device tree (OF) API > + > +Documentation/dev-tools/kunit/api/platformdevice.rst > + > + - Documents the KUnit platform device API > diff --git a/Documentation/dev-tools/kunit/api/platformdevice.rst b/Documentation/dev-tools/kunit/api/platformdevice.rst > new file mode 100644 > index 000000000000..49ddd5729003 > --- /dev/null > +++ b/Documentation/dev-tools/kunit/api/platformdevice.rst > @@ -0,0 +1,10 @@ > +.. SPDX-License-Identifier: GPL-2.0 > + > +=================== > +Platform Device API > +=================== > + > +The KUnit platform device API is used to test platform devices. > + > +.. kernel-doc:: lib/kunit/platform.c > + :export: > diff --git a/include/kunit/platform_device.h b/include/kunit/platform_device.h > new file mode 100644 > index 000000000000..0fc0999d2420 > --- /dev/null > +++ b/include/kunit/platform_device.h > @@ -0,0 +1,20 @@ > +/* SPDX-License-Identifier: GPL-2.0 */ > +#ifndef _KUNIT_PLATFORM_DRIVER_H > +#define _KUNIT_PLATFORM_DRIVER_H > + > +struct kunit; > +struct platform_device; > +struct platform_driver; > + > +struct platform_device * > +kunit_platform_device_alloc(struct kunit *test, const char *name, int id); > +int kunit_platform_device_add(struct kunit *test, struct platform_device *pdev); > + > +int kunit_platform_device_prepare_wait_for_probe(struct kunit *test, > + struct platform_device *pdev, > + struct completion *x); > + > +int kunit_platform_driver_register(struct kunit *test, > + struct platform_driver *drv); > + > +#endif > diff --git a/lib/kunit/Makefile b/lib/kunit/Makefile > index 309659a32a78..a980ae62eff6 100644 > --- a/lib/kunit/Makefile > +++ b/lib/kunit/Makefile > @@ -8,7 +8,8 @@ kunit-objs += test.o \ > try-catch.o \ > executor.o \ > attributes.o \ > - device.o > + device.o \ > + platform.o > > ifeq ($(CONFIG_KUNIT_DEBUGFS),y) > kunit-objs += debugfs.o > @@ -18,6 +19,7 @@ endif > obj-y += hooks.o > > obj-$(CONFIG_KUNIT_TEST) += kunit-test.o > +obj-$(CONFIG_KUNIT_TEST) += platform-test.o > > # string-stream-test compiles built-in only. > ifeq ($(CONFIG_KUNIT_TEST),y) > diff --git a/lib/kunit/platform-test.c b/lib/kunit/platform-test.c > new file mode 100644 > index 000000000000..b4fbedadc55e > --- /dev/null > +++ b/lib/kunit/platform-test.c > @@ -0,0 +1,223 @@ > +// SPDX-License-Identifier: GPL-2.0 > +/* > + * KUnit test for platform driver infrastructure. > + */ > + > +#include > + > +#include > +#include > + > +/* > + * Test that kunit_platform_device_alloc() creates a platform device. > + */ > +static void kunit_platform_device_alloc_test(struct kunit *test) > +{ > + KUNIT_EXPECT_NOT_ERR_OR_NULL(test, > + kunit_platform_device_alloc(test, "kunit-platform", 1)); > +} > + > +/* > + * Test that kunit_platform_device_add() registers a platform device on the > + * platform bus with the proper name and id. > + */ > +static void kunit_platform_device_add_test(struct kunit *test) > +{ > + struct platform_device *pdev; > + const char *name = "kunit-platform-add"; > + const int id = -1; > + > + pdev = kunit_platform_device_alloc(test, name, id); > + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, pdev); > + > + KUNIT_EXPECT_EQ(test, 0, kunit_platform_device_add(test, pdev)); > + KUNIT_EXPECT_TRUE(test, dev_is_platform(&pdev->dev)); > + KUNIT_EXPECT_STREQ(test, pdev->name, name); > + KUNIT_EXPECT_EQ(test, pdev->id, id); > +} > + > +/* > + * Test that kunit_platform_device_add() called twice with the same device name > + * and id fails the second time and properly cleans up. > + */ > +static void kunit_platform_device_add_twice_fails_test(struct kunit *test) > +{ > + struct platform_device *pdev; > + const char *name = "kunit-platform-add-2"; > + const int id = -1; > + > + pdev = kunit_platform_device_alloc(test, name, id); > + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, pdev); > + KUNIT_ASSERT_EQ(test, 0, kunit_platform_device_add(test, pdev)); > + > + pdev = kunit_platform_device_alloc(test, name, id); > + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, pdev); > + > + KUNIT_EXPECT_NE(test, 0, kunit_platform_device_add(test, pdev)); > +} > + > +static int kunit_platform_device_find_by_name(struct device *dev, const void *data) > +{ > + return strcmp(dev_name(dev), data) == 0; > +} > + > +/* > + * Test that kunit_platform_device_add() cleans up by removing the platform > + * device when the test finishes. */ > +static void kunit_platform_device_add_cleans_up(struct kunit *test) > +{ > + struct platform_device *pdev; > + const char *name = "kunit-platform-clean"; > + const int id = -1; > + struct kunit fake; > + struct device *dev; > + > + kunit_init_test(&fake, "kunit_platform_device_add_fake_test", NULL); > + KUNIT_ASSERT_EQ(test, fake.status, KUNIT_SUCCESS); > + > + pdev = kunit_platform_device_alloc(&fake, name, id); > + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, pdev); > + KUNIT_ASSERT_EQ(test, 0, kunit_platform_device_add(&fake, pdev)); > + dev = bus_find_device(&platform_bus_type, NULL, name, > + kunit_platform_device_find_by_name); > + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, dev); > + put_device(dev); > + > + /* Remove pdev */ > + kunit_cleanup(&fake); > + > + /* > + * Failing to migrate the kunit_resource would lead to an extra > + * put_device() call on the platform device. The best we can do here is > + * make sure the device no longer exists on the bus, but if something > + * is wrong we'll see a refcount underflow here. We can't test for a > + * refcount underflow because the kref matches the lifetime of the > + * device which should already be freed and could be used by something > + * else. > + */ > + dev = bus_find_device(&platform_bus_type, NULL, name, > + kunit_platform_device_find_by_name); > + KUNIT_EXPECT_PTR_EQ(test, NULL, dev); > + put_device(dev); > +} > + > +/* > + * Test suite for struct platform_device kunit APIs > + */ > +static struct kunit_case kunit_platform_device_test_cases[] = { > + KUNIT_CASE(kunit_platform_device_alloc_test), > + KUNIT_CASE(kunit_platform_device_add_test), > + KUNIT_CASE(kunit_platform_device_add_twice_fails_test), > + KUNIT_CASE(kunit_platform_device_add_cleans_up), > + {} > +}; > + > +static struct kunit_suite kunit_platform_device_suite = { > + .name = "kunit_platform_device", > + .test_cases = kunit_platform_device_test_cases, > +}; > + > +struct kunit_platform_driver_test_context { > + struct platform_driver pdrv; > + const char *data; > +}; > + > +static const char * const test_data = "test data"; > + > +static inline struct kunit_platform_driver_test_context * > +to_test_context(struct platform_device *pdev) > +{ > + return container_of(to_platform_driver(pdev->dev.driver), > + struct kunit_platform_driver_test_context, > + pdrv); > +} > + > +static int kunit_platform_driver_probe(struct platform_device *pdev) > +{ > + struct kunit_platform_driver_test_context *ctx; > + > + ctx = to_test_context(pdev); > + ctx->data = test_data; > + > + return 0; > +} > + > +/* Test that kunit_platform_driver_register() registers a driver that probes. */ > +static void kunit_platform_driver_register_test(struct kunit *test) > +{ > + struct platform_device *pdev; > + struct kunit_platform_driver_test_context *ctx; > + DECLARE_COMPLETION_ONSTACK(comp); > + const char *name = "kunit-platform-register"; > + > + ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL); > + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx); > + > + pdev = kunit_platform_device_alloc(test, name, -1); > + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, pdev); > + KUNIT_ASSERT_EQ(test, 0, kunit_platform_device_add(test, pdev)); > + > + ctx->pdrv.probe = kunit_platform_driver_probe; > + ctx->pdrv.driver.name = name; > + ctx->pdrv.driver.owner = THIS_MODULE; > + > + KUNIT_ASSERT_EQ(test, 0, kunit_platform_device_prepare_wait_for_probe(test, pdev, &comp)); > + > + KUNIT_EXPECT_EQ(test, 0, kunit_platform_driver_register(test, &ctx->pdrv)); > + KUNIT_EXPECT_NE(test, 0, wait_for_completion_timeout(&comp, 3 * HZ)); > + KUNIT_EXPECT_STREQ(test, ctx->data, test_data); > +} > + > +/* > + * Test that kunit_platform_device_prepare_wait_for_probe() completes the completion > + * when the device is already probed. > + */ > +static void kunit_platform_device_prepare_wait_for_probe_completes_when_already_probed(struct kunit *test) > +{ > + struct platform_device *pdev; > + struct kunit_platform_driver_test_context *ctx; > + DECLARE_COMPLETION_ONSTACK(comp); > + const char *name = "kunit-platform-wait"; > + > + ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL); > + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx); > + > + pdev = kunit_platform_device_alloc(test, name, -1); > + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, pdev); > + KUNIT_ASSERT_EQ(test, 0, kunit_platform_device_add(test, pdev)); > + > + ctx->pdrv.probe = kunit_platform_driver_probe; > + ctx->pdrv.driver.name = name; > + ctx->pdrv.driver.owner = THIS_MODULE; > + > + /* Make sure driver has actually probed */ > + KUNIT_ASSERT_EQ(test, 0, kunit_platform_device_prepare_wait_for_probe(test, pdev, &comp)); > + KUNIT_ASSERT_EQ(test, 0, kunit_platform_driver_register(test, &ctx->pdrv)); > + KUNIT_ASSERT_NE(test, 0, wait_for_completion_timeout(&comp, 3 * HZ)); > + > + reinit_completion(&comp); > + KUNIT_ASSERT_EQ(test, 0, kunit_platform_device_prepare_wait_for_probe(test, pdev, &comp)); > + > + KUNIT_EXPECT_NE(test, 0, wait_for_completion_timeout(&comp, HZ)); > +} > + > +static struct kunit_case kunit_platform_driver_test_cases[] = { > + KUNIT_CASE(kunit_platform_driver_register_test), > + KUNIT_CASE(kunit_platform_device_prepare_wait_for_probe_completes_when_already_probed), > + {} > +}; > + > +/* > + * Test suite for struct platform_driver kunit APIs > + */ > +static struct kunit_suite kunit_platform_driver_suite = { > + .name = "kunit_platform_driver", > + .test_cases = kunit_platform_driver_test_cases, > +}; > + > +kunit_test_suites( > + &kunit_platform_device_suite, > + &kunit_platform_driver_suite, > +); > + > +MODULE_LICENSE("GPL"); > diff --git a/lib/kunit/platform.c b/lib/kunit/platform.c > new file mode 100644 > index 000000000000..ba1b0006dc45 > --- /dev/null > +++ b/lib/kunit/platform.c > @@ -0,0 +1,302 @@ > +// SPDX-License-Identifier: GPL-2.0 > +/* > + * Test managed platform driver > + */ > + > +#include > +#include > +#include > +#include > + > +#include > +#include > + > +struct kunit_platform_device_alloc_params { > + const char *name; > + int id; > +}; > + > +static int kunit_platform_device_alloc_init(struct kunit_resource *res, void *context) > +{ > + struct kunit_platform_device_alloc_params *params = context; > + struct platform_device *pdev; > + > + pdev = platform_device_alloc(params->name, params->id); > + if (!pdev) > + return -ENOMEM; > + > + res->data = pdev; > + > + return 0; > +} > + > +static void kunit_platform_device_alloc_exit(struct kunit_resource *res) > +{ > + struct platform_device *pdev = res->data; > + > + platform_device_put(pdev); > +} > + > +/** > + * kunit_platform_device_alloc() - Allocate a KUnit test managed platform 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 * > +kunit_platform_device_alloc(struct kunit *test, const char *name, int id) > +{ > + struct kunit_platform_device_alloc_params params = { > + .name = name, > + .id = id, > + }; > + > + return kunit_alloc_resource(test, > + kunit_platform_device_alloc_init, > + kunit_platform_device_alloc_exit, > + GFP_KERNEL, ¶ms); > +} > +EXPORT_SYMBOL_GPL(kunit_platform_device_alloc); > + > +static void kunit_platform_device_add_exit(struct kunit_resource *res) > +{ > + struct platform_device *pdev = res->data; > + > + platform_device_unregister(pdev); > +} > + > +static bool > +kunit_platform_device_alloc_match(struct kunit *test, > + struct kunit_resource *res, void *match_data) > +{ > + struct platform_device *pdev = match_data; > + > + return res->data == pdev && res->free != kunit_platform_device_alloc_exit; > +} > + > +KUNIT_DEFINE_ACTION_WRAPPER(platform_device_unregister_wrapper, > + platform_device_unregister, struct platform_device *); > +/** > + * kunit_platform_device_add() - Register a KUnit test managed platform 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 kunit_platform_device_add(struct kunit *test, struct platform_device *pdev) > +{ > + struct kunit_resource *res; > + int ret; > + > + ret = platform_device_add(pdev); > + if (ret) > + return ret; > + > + res = kunit_find_resource(test, kunit_platform_device_alloc_match, pdev); > + if (res) { > + /* > + * Transfer the reference count of the platform device if it > + * was allocated with kunit_platform_device_alloc(). In this > + * case, calling platform_device_put() when the test exits from > + * kunit_platform_device_alloc_exit() would lead to reference > + * count underflow because platform_device_unregister_wrapper() > + * calls platform_device_unregister() which also calls > + * platform_device_put(). > + * > + * Usually callers transfer the refcount initialized in > + * platform_device_alloc() to platform_device_add() by calling > + * platform_device_unregister() when platform_device_add() > + * succeeds or platform_device_put() when it fails. KUnit has to > + * keep this straight by redirecting the free routine for the > + * resource to the right function. Luckily this only has to > + * account for the success scenario. > + */ > + res->free = kunit_platform_device_add_exit; > + kunit_put_resource(res); > + } else { > + ret = kunit_add_action_or_reset(test, platform_device_unregister_wrapper, pdev); > + if (ret) > + return ret; > + } > + > + return 0; > +} > +EXPORT_SYMBOL_GPL(kunit_platform_device_add); > + > +struct kunit_platform_device_probe_nb { > + struct completion *x; > + struct device *dev; > + struct notifier_block nb; > +}; > + > +static int kunit_platform_device_probe_notify(struct notifier_block *nb, > + unsigned long event, void *data) > +{ > + struct kunit_platform_device_probe_nb *knb; > + struct device *dev = data; > + > + knb = container_of(nb, struct kunit_platform_device_probe_nb, nb); > + if (event != BUS_NOTIFY_BOUND_DRIVER || knb->dev != dev) > + return NOTIFY_DONE; > + > + complete(knb->x); > + > + return NOTIFY_OK; > +} > + > +static void kunit_platform_device_probe_nb_remove(void *nb) > +{ > + bus_unregister_notifier(&platform_bus_type, nb); > +} > + > +/** > + * kunit_platform_device_prepare_wait_for_probe() - Prepare a completion > + * variable to wait for a platform device to probe > + * @test: test context > + * @pdev: platform device to prepare to wait for probe of > + * @x: completion variable completed when @dev has probed > + * > + * Prepare a completion variable @x to wait for @pdev to probe. Waiting on the > + * completion forces a preemption, allowing the platform driver to probe. > + * > + * Example > + * > + * .. code-block:: c > + * > + * static int kunit_platform_driver_probe(struct platform_device *pdev) > + * { > + * return 0; > + * } > + * > + * static void kunit_platform_driver_test(struct kunit *test) > + * { > + * struct platform_device *pdev; > + * struct platform_driver *pdrv; > + * DECLARE_COMPLETION_ONSTACK(comp); > + * > + * pdev = kunit_platform_device_alloc(test, "kunit-platform", -1); > + * KUNIT_ASSERT_NOT_ERR_OR_NULL(test, pdev); > + * KUNIT_ASSERT_EQ(test, 0, kunit_platform_device_add(test, pdev)); > + * > + * pdrv = kunit_kzalloc(test, sizeof(*pdrv), GFP_KERNEL); > + * KUNIT_ASSERT_NOT_ERR_OR_NULL(test, pdrv); > + * > + * pdrv->probe = kunit_platform_driver_probe; > + * pdrv->driver.name = "kunit-platform"; > + * pdrv->driver.owner = THIS_MODULE; > + * > + * KUNIT_ASSERT_EQ(test, 0, kunit_platform_device_prepare_wait_for_probe(test, pdev, &comp)); > + * KUNIT_ASSERT_EQ(test, 0, kunit_platform_driver_register(test, pdrv)); > + * > + * KUNIT_EXPECT_NE(test, 0, wait_for_completion_timeout(&comp, 3 * HZ)); > + * } > + * > + * Return: 0 on success, negative errno on failure. > + */ > +int kunit_platform_device_prepare_wait_for_probe(struct kunit *test, > + struct platform_device *pdev, > + struct completion *x) > +{ > + struct device *dev = &pdev->dev; > + struct kunit_platform_device_probe_nb *knb; > + bool bound; > + > + knb = kunit_kzalloc(test, sizeof(*knb), GFP_KERNEL); > + if (!knb) > + return -ENOMEM; > + > + knb->nb.notifier_call = kunit_platform_device_probe_notify; > + knb->dev = dev; > + knb->x = x; > + > + device_lock(dev); > + bound = device_is_bound(dev); > + if (bound) { > + device_unlock(dev); > + complete(x); > + kunit_kfree(test, knb); > + return 0; > + } > + > + bus_register_notifier(&platform_bus_type, &knb->nb); > + device_unlock(&pdev->dev); > + > + return kunit_add_action_or_reset(test, kunit_platform_device_probe_nb_remove, &knb->nb); > +} > +EXPORT_SYMBOL_GPL(kunit_platform_device_prepare_wait_for_probe); > + > +KUNIT_DEFINE_ACTION_WRAPPER(platform_driver_unregister_wrapper, > + platform_driver_unregister, struct platform_driver *); > +/** > + * kunit_platform_driver_register() - Register a KUnit test managed platform driver > + * @test: test context > + * @drv: platform driver to register > + * > + * Register a test managed platform driver. This allows callers to embed the > + * @drv in a container structure and use container_of() in the probe function > + * to pass information to KUnit tests. > + * > + * 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 = to_test_context(pdev); > + * ctx->data = "test data"; > + * > + * return 0; > + * } > + * > + * static void kunit_platform_driver_test(struct kunit *test) > + * { > + * struct kunit_test_context *ctx; > + * > + * ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL); > + * KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx); > + * > + * ctx->pdrv.probe = kunit_platform_driver_probe; > + * ctx->pdrv.driver.name = "kunit-platform"; > + * ctx->pdrv.driver.owner = THIS_MODULE; > + * > + * KUNIT_EXPECT_EQ(test, 0, kunit_platform_driver_register(test, &ctx->pdrv)); > + * <... wait for driver to probe ...> > + * KUNIT_EXPECT_STREQ(test, ctx->data, "test data"); > + * } > + * > + * Return: 0 on success, negative errno on failure. > + */ > +int kunit_platform_driver_register(struct kunit *test, > + struct platform_driver *drv) > +{ > + int ret; > + > + ret = platform_driver_register(drv); > + if (ret) > + return ret; > + > + return kunit_add_action_or_reset(test, platform_driver_unregister_wrapper, drv); > +} > +EXPORT_SYMBOL_GPL(kunit_platform_driver_register); > -- > https://git.kernel.org/pub/scm/linux/kernel/git/clk/linux.git/ > https://git.kernel.org/pub/scm/linux/kernel/git/sboyd/spmi.git > --000000000000bafcbf061ac0c010 Content-Type: application/pkcs7-signature; name="smime.p7s" Content-Transfer-Encoding: base64 Content-Disposition: attachment; filename="smime.p7s" Content-Description: S/MIME Cryptographic Signature MIIPqgYJKoZIhvcNAQcCoIIPmzCCD5cCAQExDzANBglgh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