Received: by 2002:a05:6358:11c7:b0:104:8066:f915 with SMTP id i7csp86215rwl; Fri, 24 Mar 2023 21:32:55 -0700 (PDT) X-Google-Smtp-Source: AKy350Z/aBOy4GKxFCBiLRJOJrmZPGYcPPKQO5FXQSLXbxUZlTN8mdWP4bTgsjPwcB4taKJSCmHE X-Received: by 2002:a17:90b:4b8e:b0:23f:580d:b252 with SMTP id lr14-20020a17090b4b8e00b0023f580db252mr4829215pjb.36.1679718774801; Fri, 24 Mar 2023 21:32:54 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1679718774; cv=none; d=google.com; s=arc-20160816; b=pLuUgebCprt42Zryjozq+q4+K9Qr/BLb81Kw+aQ6TNUw6w0HXMaYE8AZcs+K3MYWvK NmSuEiP0R9Kxa98epEcBPityzq2Fin+NiOP/ni0hRwnXACjfvi2+QDPM3DDTMml78W0N UpnNIqA/oOzGfo95D0/PAvQy3pz94zn8rEAcRTsaKpMeb8QQuihZEz6sl4gvyUzgCAKv tBjBsiWO5XsA3XVF2ufFi3pvkh+OCDig02yivtv6ywRHkfrhQKcPLfH8/9vH+GnAV+6U xKnpTV6Zk2mCm2I4AmD0NN5WPmBt1aUuIAjMcgTxR2Ds5n5MyzwbCsC1Ap+maugntqYE 2Fbw== 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=2GFTGuce/jasIOoxgBhcBDFvlf80kZtNsVdpwus1c0o=; b=HKVF14Et/dhUFj2dAG4JZ7Fkepri6W04snVdHLQeT/n/Nx2Az3/uTy106n5F2Rw2dd zBJKFVDWbvFNCVRO6sD0eKIIvTAdwGmuv+txHJ1nGT80UxloSK//cMvm8fiVBHrmGvuO exEsIp/8C2mJKRPDfssTlyVZF8duUkPV9W0urnhvZVbkKilsZZVXtTho9ksZ4yLxksH9 fa2vbW5ef+1EdG5egz9I27MP6SvMZYvg0+MRQSz6LH1RQ7SKEyYSVOkBP4arA6aT0WHu 1HfcaEVhy7/8l4V2DvUJOaYqobHVvcsmkcepc1qgVGRiyksQjTaEF4gmuwnYOkT3r/oh tGzw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=jPiK9bzw; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id ls14-20020a17090b350e00b00234ba530b89si1344926pjb.146.2023.03.24.21.32.43; Fri, 24 Mar 2023 21:32:54 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=jPiK9bzw; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232038AbjCYEbm (ORCPT + 99 others); Sat, 25 Mar 2023 00:31:42 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42904 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232034AbjCYEbh (ORCPT ); Sat, 25 Mar 2023 00:31:37 -0400 Received: from mail-pf1-x44a.google.com (mail-pf1-x44a.google.com [IPv6:2607:f8b0:4864:20::44a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E54981ABD6 for ; Fri, 24 Mar 2023 21:31:24 -0700 (PDT) Received: by mail-pf1-x44a.google.com with SMTP id x3-20020a62fb03000000b00622df3f5d0cso1911025pfm.10 for ; Fri, 24 Mar 2023 21:31:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; t=1679718684; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=2GFTGuce/jasIOoxgBhcBDFvlf80kZtNsVdpwus1c0o=; b=jPiK9bzw/LfWhk/EqotDJX72pjO1JKd1BwuwlbjMi9aYrXP1DPcn8Nt4cOwDHXs9jh ij4e2UNXRWtJoZtf2Z94vobgXgG4FvRXLAmxm91yhbdBFPkqrIsk0c2QmZjI9UWAQCSg f4cKmyY38G7Q9bQw598jxVJkV0K3c/r2Vi+GxwcNR241OJLr0VBRpsyYJ15SmiZj4UAF D3Nx5ZoSqHRxu60/MVPookyisHGGOXNKKMYjYZ9kM5A2jAaNIpmlMY1fD7eRgXTi8KSB NEeXv3z1mPoeQVEWMgiyr4eDInIdZECHHG4Dmo/ttWv/PoymoNUwLRgzIq9uGIbcfl+A oK+A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1679718684; 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=2GFTGuce/jasIOoxgBhcBDFvlf80kZtNsVdpwus1c0o=; b=bIxvDmEcJTaII/rObInM2o6fSzMFcnbvdBbidmpT3MCzSliz9MIfRAlRPH/e19Jlgl afLfDv/s1SWawsoPs4CdgwA4LFJDo/K7xLPVRcCgoO913l03/BoGSDodaSzYmDxhXFCq 0Mp6Dh0qhd8KwS0fUxBD1YKkbuuXxn32w+dgMB+UtOMBC2gGzYScWtMUk4vqOIqaT0zC ROymHE48FWtA7B76S7K/0hx2qBCWIwfWJii+UZi9wk7haYsZ4FL9nwmOupaU+B5HNv01 oY23TbofBOBY/C/SEbs2h/W0f9WF+ZFu8tbhJGHX/O17YXZ7gON1zB3KDuyeryo4K2Im zc2g== X-Gm-Message-State: AO0yUKX/yvwavPiUFN71j++38fxqE+MSTkm2MhLGO2zhPt+SG7xoDx3/ eEfj6hcrPoRm8SHZNqrXaCSl7yx5DrdScA== X-Received: from slicestar.c.googlers.com ([fda3:e722:ac3:cc00:4f:4b78:c0a8:20a1]) (user=davidgow job=sendgmr) by 2002:a05:6a00:4510:b0:626:1710:9b7d with SMTP id cw16-20020a056a00451000b0062617109b7dmr5135789pfb.0.1679718684393; Fri, 24 Mar 2023 21:31:24 -0700 (PDT) Date: Sat, 25 Mar 2023 12:31:04 +0800 In-Reply-To: <20230325043104.3761770-1-davidgow@google.com> Mime-Version: 1.0 References: <20230325043104.3761770-1-davidgow@google.com> X-Mailer: git-send-email 2.40.0.348.gf938b09366-goog Message-ID: <20230325043104.3761770-3-davidgow@google.com> Subject: [RFC PATCH 2/2] kunit: Add APIs for managing devices From: David Gow To: Greg Kroah-Hartman , Matti Vaittinen , Maxime Ripard , Brendan Higgins , Stephen Boyd , Shuah Khan Cc: David Gow , "Rafael J . Wysocki" , Andy Shevchenko , Heikki Krogerus , Jonathan Cameron , linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, kunit-dev@googlegroups.com Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-7.7 required=5.0 tests=DKIMWL_WL_MED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,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 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 two KUnit-specific functions for registering and unregistering a struct device: - kunit_device_register() - kunit_device_unregister() These behave similarly to root_device_register() and root_device_unregister() except: - They take a struct kunit pointer with a test context. - They do not create a root device directory in sysfs. - The device will automatically be unregistered when the test exits (unless it has already been unregistered using kunit_device_unregister()) - The device name is set to .. This API can be extended in the future to, for example, add these devices to a KUnit bus as tests begin to require that functionality. Signed-off-by: David Gow --- include/kunit/device.h | 25 ++++++++++++++++ lib/kunit/Makefile | 1 + lib/kunit/device.c | 68 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 94 insertions(+) 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..19a35b5e4e59 --- /dev/null +++ b/include/kunit/device.h @@ -0,0 +1,25 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* + * KUnit basic device implementation + * + * Implementation of struct kunit_device helpers. + * + * Copyright (C) 2023, Google LLC. + * Author: David Gow + */ + +#ifndef _KUNIT_DEVICE_H +#define _KUNIT_DEVICE_H + +#if IS_ENABLED(CONFIG_KUNIT) + +#include + +struct kunit_device; + +struct device *kunit_device_register(struct kunit *test, const char *name); +void kunit_device_unregister(struct kunit *test, struct device *dev); + +#endif + +#endif diff --git a/lib/kunit/Makefile b/lib/kunit/Makefile index cb417f504996..b9bd059269ed 100644 --- a/lib/kunit/Makefile +++ b/lib/kunit/Makefile @@ -6,6 +6,7 @@ kunit-objs += test.o \ string-stream.o \ assert.o \ try-catch.o \ + device.o \ executor.o ifeq ($(CONFIG_KUNIT_DEBUGFS),y) diff --git a/lib/kunit/device.c b/lib/kunit/device.c new file mode 100644 index 000000000000..ce87b7c40d9b --- /dev/null +++ b/lib/kunit/device.c @@ -0,0 +1,68 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * KUnit basic device implementation + * + * Implementation of struct kunit_device helpers. + * + * Copyright (C) 2023, Google LLC. + * Author: David Gow + */ + +#include + +#include +#include + +/* A device owned by a KUnit test. */ +struct kunit_device { + struct device dev; + struct kunit *owner; +}; + +static inline struct kunit_device *to_kunit_device(struct device *d) +{ + return container_of(d, struct kunit_device, dev); +} + +static void kunit_device_release(struct device *d) +{ + kfree(to_kunit_device(d)); +} + +struct device *kunit_device_register(struct kunit *test, const char *name) +{ + struct kunit_device *kunit_dev; + int err = -ENOMEM; + + kunit_dev = kzalloc(sizeof(struct kunit_device), GFP_KERNEL); + if (!kunit_dev) + return ERR_PTR(err); + + kunit_dev->owner = test; + + err = dev_set_name(&kunit_dev->dev, "%s.%s", test->name, name); + if (err) { + kfree(kunit_dev); + return ERR_PTR(err); + } + + kunit_dev->dev.release = kunit_device_release; + + err = device_register(&kunit_dev->dev); + if (err) { + put_device(&kunit_dev->dev); + return ERR_PTR(err); + } + + kunit_defer(test, (kunit_defer_function_t)device_unregister, &kunit_dev->dev, GFP_KERNEL); + + return &kunit_dev->dev; +} +EXPORT_SYMBOL_GPL(kunit_device_register); + +void kunit_device_unregister(struct kunit *test, struct device *dev) +{ + kunit_defer_trigger(test, (kunit_defer_function_t)device_unregister, dev); +} +EXPORT_SYMBOL_GPL(kunit_device_unregister); + -- 2.40.0.348.gf938b09366-goog