From nobody Sun May 5 10:46:20 2024 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 F3296C6FD1F for ; Sat, 25 Mar 2023 04:31:43 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232016AbjCYEbl (ORCPT ); Sat, 25 Mar 2023 00:31:41 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42248 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231985AbjCYEb0 (ORCPT ); Sat, 25 Mar 2023 00:31:26 -0400 Received: from mail-yw1-x114a.google.com (mail-yw1-x114a.google.com [IPv6:2607:f8b0:4864:20::114a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AB9CE1A977 for ; Fri, 24 Mar 2023 21:31:20 -0700 (PDT) Received: by mail-yw1-x114a.google.com with SMTP id 00721157ae682-5417f156cb9so36927187b3.8 for ; Fri, 24 Mar 2023 21:31:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; t=1679718680; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=qvsBLzafDFRdEeFlhtCKfSPH7Z58gGOcQ/NMYd5XVas=; b=Dkx+4oPW0lddcPp4uMCQo6pmYmmbn0VU/J6ph6FIZMvJHQ04WSJmMD7xcIcZLJ6PdN mzLHDd4+oVvLMi1Zcdmmen6ZKzyf+g1GWgtWOeOuWYuo4GIJOr/0XBgAc273lDPvOxyn x/DczHXGxTwgZwYMHRELVpV4Toxy+2KxrL1CRJni3/TMk9xGSF+B6Ixg0Qh2jk842ljw rlDf4yUosLnhwdKudLfaiffDtlBTk7P4a9n3NnW6nlLUy3fEX7Q/qxKKYau9bO29v/Ww aJhM3eN9NL0dDmMVQAe0W1p9zzom/gVxfE5CItxvv4NN8JpOss79ZwU5ox4G1jGQ9ykB S25A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1679718680; 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=qvsBLzafDFRdEeFlhtCKfSPH7Z58gGOcQ/NMYd5XVas=; b=PFmIaO9gYrnjB4obXdcy/YaihvBYZ5XhUxUsPMbwrp2osjXI60UBy9E8Gj98GyF1oI 2Uuz7x5AJmjMp5HdZryyQSSGJdpKtIE59AGx4404kvM9fQ6ZvUxd4EC0lc3rTD1B1mIQ KL96usreAT9qyuZTv9aA8FxmmjMHLKlG/zpQVY+6ic1W3TqjZgipx8m15LtKYks8FAWK Iii4uLZsYdmjlLMA2yDVlo15uYt91o8PgvfDWPz5Hu8tHJGjM3yC5DIhH1h6qOUEI1PS 06/ZM0vx5zvQo+Hcq47SWcHNLvRVMGuc/5YasJC6AyHcQfl252X0JQr/qU2u9rCwD3cZ t43g== X-Gm-Message-State: AAQBX9cn4EujuCaT6rixMQLxu9sMIdHUHfgIX536LxZX0+I3NNybM1LJ +HYosv6WD5pLBIe3iB0gZ9XisYQFFHduCQ== X-Google-Smtp-Source: AKy350aAmbbgvFfbZgCOgJD0jK2dcqvaJW7t4SFAmuugOJrv8Crpejpj64U5SmpXoebmhM46ApGf7S32uVGC0Q== X-Received: from slicestar.c.googlers.com ([fda3:e722:ac3:cc00:4f:4b78:c0a8:20a1]) (user=davidgow job=sendgmr) by 2002:a81:ac64:0:b0:544:b9b2:5c32 with SMTP id z36-20020a81ac64000000b00544b9b25c32mr2114854ywj.7.1679718679808; Fri, 24 Mar 2023 21:31:19 -0700 (PDT) Date: Sat, 25 Mar 2023 12:31:03 +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-2-davidgow@google.com> Subject: [RFC PATCH 1/2] kunit: resource: Add kunit_defer() functionality 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 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Many uses of the KUnit resource system are intended to simply defer calling a function until the test exits (be it due to success or failure). The existing kunit_alloc_resource() function is often used for this, but was awkward to use (requiring passing NULL init functions, etc), and returned a resource without incrementing its reference count, which -- while okay for this use-case -- could cause problems in others. Instead, introduce a simple kunit_defer() API: a simple function (returning nothing, accepting a single void* argument) can be scheduled to be called when the test exits. Deferred functions are called in the opposite order to that which they were registered. This is implemented as a resource under the hood, so the ordering between resource cleanup and deferred functions is maintained. Signed-off-by: David Gow --- include/kunit/resource.h | 87 +++++++++++++++++++++++++++++++ lib/kunit/resource.c | 110 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 197 insertions(+) diff --git a/include/kunit/resource.h b/include/kunit/resource.h index cf6fb8f2ac1b..6c4728ca9237 100644 --- a/include/kunit/resource.h +++ b/include/kunit/resource.h @@ -387,4 +387,91 @@ static inline int kunit_destroy_named_resource(struct = kunit *test, */ void kunit_remove_resource(struct kunit *test, struct kunit_resource *res); =20 +typedef void (*kunit_defer_function_t)(void *ctx); + +/** + * kunit_defer() - Defer a function call until the test ends. + * @test: Test case to associate the deferred function with. + * @func: The function to run on test exit + * @ctx: Data passed into @func + * @internal_gfp: gfp to use for internal allocations, if unsure, use GFP_= KERNEL + * + * Defer the execution of a function until the test exits, either normally= or + * due to a failure. @ctx is passed as additional context. All functions + * registered with kunit_defer() will execute in the opposite order to that + * they were registered in. + * + * This is useful for cleaning up allocated memory and resources. + * + * RETURNS: + * An opaque "cancellation token", or NULL on error. Pass this token to + * kunit_defer_cancel() in order to cancel the deferred execution of fun= c(). + */ +void *kunit_defer(struct kunit *test, kunit_defer_function_t func, + void *ctx, gfp_t internal_gfp); + +/** + * kunit_defer_cancel_token() - Cancel a deferred function call. + * @test: Test case the deferred function is associated with. + * @cancel_token: The cancellation token returned by kunit_defer() + * + * Prevent a function deferred using kunit_defer() from executing when the + * test ends. + * + * Prefer using this to kunit_defer_cancel() where possible. + */ +void kunit_defer_cancel_token(struct kunit *test, void *cancel_token); + +/** + * kunit_defer_trigger_token() - Run a deferred function call immediately. + * @test: Test case the deferred function is associated with. + * @cancel_token: The cancellation token returned by kunit_defer() + * + * Execute a deferred function call immediately, instead of waiting for the + * test to end. + * + * Prefer using this to kunit_defer_trigger() where possible. + */ +void kunit_defer_trigger_token(struct kunit *test, void *cancel_token); + +/** + * kunit_defer_cancel() - Cancel a matching deferred function call. + * @test: Test case the deferred function is associated with. + * @func: The deferred function to cancel. + * @ctx: The context passed to the deferred function to trigger. + * + * Prevent a function deferred via kunit_defer() from executing at shutdow= n. + * Unlike kunit_defer_cancel_token(), this takes the (func, ctx) pair inst= ead of + * the cancellation token. If that function/context pair was deferred mult= iple + * times, only the most recent one will be cancelled. + * + * Prefer using kunit_defer_cancel_token() to this where possible. + */ +void kunit_defer_cancel(struct kunit *test, + kunit_defer_function_t func, + void *ctx); + +/** + * kunit_defer_trigger() - Run a matching deferred function call immediate= ly. + * @test: Test case the deferred function is associated with. + * @func: The deferred function to trigger. + * @ctx: The context passed to the deferred function to trigger. + * + * Execute a function deferred via kunit_defer() immediately, rather than = when + * the test ends. + * Unlike kunit_defer_trigger_token(), this takes the (func, ctx) pair ins= tead of + * the cancellation token. If that function/context pair was deferred mult= iple + * times, it will only be executed once here. The most recent deferral will + * no longer execute when the test ends. + * + * kunit_defer_trigger(test, func, ctx); + * is equivalent to + * func(ctx); + * kunit_defer_cancel(test, func, ctx); + * + * Prefer using kunit_defer_trigger_token() to this where possible. + */ +void kunit_defer_trigger(struct kunit *test, + kunit_defer_function_t func, + void *ctx); #endif /* _KUNIT_RESOURCE_H */ diff --git a/lib/kunit/resource.c b/lib/kunit/resource.c index c414df922f34..0d0c48054d45 100644 --- a/lib/kunit/resource.c +++ b/lib/kunit/resource.c @@ -77,3 +77,113 @@ int kunit_destroy_resource(struct kunit *test, kunit_re= source_match_t match, return 0; } EXPORT_SYMBOL_GPL(kunit_destroy_resource); + +struct kunit_defer_ctx { + kunit_defer_function_t func; + void *ctx; +}; + +static void __kunit_defer_free(struct kunit_resource *res) +{ + struct kunit_defer_ctx *defer_ctx =3D (struct kunit_defer_ctx *)res->data; + + defer_ctx->func(defer_ctx->ctx); + + kfree(res->data); +} + +void *kunit_defer(struct kunit *test, kunit_defer_function_t func, + void *ctx, gfp_t internal_gfp) +{ + struct kunit_resource *res; + struct kunit_defer_ctx *defer_ctx; + + KUNIT_ASSERT_NOT_NULL_MSG(test, func, "Tried to defer a NULL function!"); + + res =3D kzalloc(sizeof(*res), internal_gfp); + if (!res) + return NULL; + + defer_ctx =3D kzalloc(sizeof(*defer_ctx), internal_gfp); + if (!defer_ctx) + goto free_res; + + defer_ctx->func =3D func; + defer_ctx->ctx =3D ctx; + + res->should_kfree =3D true; + __kunit_add_resource(test, NULL, __kunit_defer_free, res, defer_ctx); + + return (void *)res; + +free_res: + kfree(res); + return NULL; +} + +void kunit_defer_cancel_token(struct kunit *test, void *cancel_token) +{ + struct kunit_resource *res =3D (struct kunit_resource *)cancel_token; + + /* Remove the free function so we don't run the deferred function. */ + res->free =3D NULL; + + kunit_remove_resource(test, res); +} + +void kunit_defer_trigger_token(struct kunit *test, void *cancel_token) +{ + struct kunit_resource *res =3D (struct kunit_resource *)cancel_token; + + /* Removing the resource should trigger the res->free function. */ + kunit_remove_resource(test, res); +} + +static bool __kunit_defer_match(struct kunit *test, + struct kunit_resource *res, void *match_data) +{ + struct kunit_defer_ctx *match_ctx =3D (struct kunit_defer_ctx *)match_dat= a; + struct kunit_defer_ctx *res_ctx =3D (struct kunit_defer_ctx *)res->data; + + /* Make sure this is a free function. */ + if (res->free !=3D __kunit_defer_free) + return false; + + /* Both the function and context data should match. */ + return (match_ctx->func =3D=3D res_ctx->func) && (match_ctx->ctx =3D=3D r= es_ctx->ctx); +} + +void kunit_defer_cancel(struct kunit *test, + kunit_defer_function_t func, + void *ctx) +{ + struct kunit_defer_ctx defer_ctx; + struct kunit_resource *res; + + defer_ctx.func =3D func; + defer_ctx.ctx =3D ctx; + + res =3D kunit_find_resource(test, __kunit_defer_match, &defer_ctx); + if (res) { + kunit_defer_cancel_token(test, res); + kunit_put_resource(res); + } +} + +void kunit_defer_trigger(struct kunit *test, + kunit_defer_function_t func, + void *ctx) +{ + struct kunit_defer_ctx defer_ctx; + struct kunit_resource *res; + + defer_ctx.func =3D func; + defer_ctx.ctx =3D ctx; + + res =3D kunit_find_resource(test, __kunit_defer_match, &defer_ctx); + if (res) { + kunit_defer_trigger_token(test, res); + /* We have to put() this here, else free won't be called. */ + kunit_put_resource(res); + } +} --=20 2.40.0.348.gf938b09366-goog From nobody Sun May 5 10:46:20 2024 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 3779AC6FD1F for ; Sat, 25 Mar 2023 04:31:47 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232066AbjCYEbo (ORCPT ); Sat, 25 Mar 2023 00:31:44 -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-Google-Smtp-Source: AK7set/eA25yEQztgVWNsBWE4ael/iyts9um9spxPVS5KzosXah9xcxZqDHvH6pHUe+DNTg6pSiQEEMUh6sQRg== 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 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" 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 +=3D test.o \ string-stream.o \ assert.o \ try-catch.o \ + device.o \ executor.o =20 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 =3D -ENOMEM; + + kunit_dev =3D kzalloc(sizeof(struct kunit_device), GFP_KERNEL); + if (!kunit_dev) + return ERR_PTR(err); + + kunit_dev->owner =3D test; + + err =3D dev_set_name(&kunit_dev->dev, "%s.%s", test->name, name); + if (err) { + kfree(kunit_dev); + return ERR_PTR(err); + } + + kunit_dev->dev.release =3D kunit_device_release; + + err =3D 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); + --=20 2.40.0.348.gf938b09366-goog