Received: by 2002:ac8:156:0:b0:3e0:cd10:60c8 with SMTP id f22csp274536qtg; Fri, 31 Mar 2023 01:10:48 -0700 (PDT) X-Google-Smtp-Source: AKy350aWQE9u0wKvE9fM9ZCrTv1NprUrsBEFHeKDaT3w9bs29YxCgtUxv3hscrGRLHFFNsRY4NFn X-Received: by 2002:a17:906:1c4:b0:931:5145:c51f with SMTP id 4-20020a17090601c400b009315145c51fmr372443ejj.4.1680250248460; Fri, 31 Mar 2023 01:10:48 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1680250248; cv=none; d=google.com; s=arc-20160816; b=zbtVrQCJsIvwKu77yPPA1NHMN4tbqUqTZSgs7ilggFlaHcdvz9uRYqYMjphUKZlH53 hWih/oHGQifnUS9bU0uYUCWGGS/GjHSNwq86k+wPlW7gJKa4agTeBmFghPHaXj978FOa BJl4xvWIR65594WHComXNm87H/gs1p3YSD7OstrymkIcKPCxeMrLnlGhxol6U+iwek+2 zq77DWmAvy+aqEi/E8fC5AYFYAGCHPD9A+Aqx5gzGkfdVXTsfMLIoCb0eRix0Md37E9h GDbgrRJWOgW6re0q62gDY9Y8NFvkSLQgk3fpVSg40RDWLbmmzSvH+gtYCS0igmhvsPm6 CgPQ== 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=5m1UzqN9dan9Fve/v01UII7j8R+V4PUOKPhCOFOqZlI=; b=PXxbszWEep4y5LxRSLkVGnYPAs8i+iHlOxati7yWHwcbAc15eGPngo2VNZHqGnAqa4 RngKrg9BoRsbtJNtwBP3BvQ8MJBstOPA6RQPC9SBDuSOyraF6ZNn93Syoid/rBCx6J5s v7IsqjHdZFlJOmzBpW8AOB2nfXyI7Dw0of4Xmu5k/MaFxpx3sFTnS0fX5ePE6LeeznD6 X7uor8Vce7Abtgp6D2JuCioWBPpnXaQwm2CVZHTw3p7BtYnxwMPaSZNOdbFK/Tsaefvn pj4q2PpMNH+nhYdzzRqiXkdpgsamDzPoG0PoNlt4yqsnSVnvL9mdKT2Rhb+SaRFIvWAT CZqQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=YwdzYxAs; 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 j14-20020a170906104e00b00946c2651290si1771206ejj.232.2023.03.31.01.10.23; Fri, 31 Mar 2023 01:10:48 -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=YwdzYxAs; 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 S231407AbjCaIEi (ORCPT + 99 others); Fri, 31 Mar 2023 04:04:38 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45520 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231376AbjCaIEb (ORCPT ); Fri, 31 Mar 2023 04:04:31 -0400 Received: from mail-yw1-x1149.google.com (mail-yw1-x1149.google.com [IPv6:2607:f8b0:4864:20::1149]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id EE6031B367 for ; Fri, 31 Mar 2023 01:04:28 -0700 (PDT) Received: by mail-yw1-x1149.google.com with SMTP id 00721157ae682-545dd1a1e31so161211667b3.22 for ; Fri, 31 Mar 2023 01:04:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; t=1680249868; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=5m1UzqN9dan9Fve/v01UII7j8R+V4PUOKPhCOFOqZlI=; b=YwdzYxAsk7Si/YO5GQUMvPrVDiG4J+fxLLPwbvHl57cxrSeecolFBi3d5osEAqyYPn 7wivCX1t15lDPIQS3daejsY2Ry9IJwuUWQiaMY2AY4jfuf6m2RpaVoVF/ZmTQd7s4wbn QCChXbK5L94jmXnB8vb0WSYPWo0oCpiEJoNh8d/zMg4898jgpCMV/3891mGr5N8Xrs6M IiA3jjEsJpDF3rev0Gk2mqu/m8tKPy9uIZJ3lPzbHq0wwuWo5kDlKp9FNruZerMe2j4A LilM9v+8MYXQeyHkWymFm0IIKv9vdPj4IpXF33adYpPAly+0ATXumhEGSi3dOz0moqmc 8Kpw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680249868; 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=5m1UzqN9dan9Fve/v01UII7j8R+V4PUOKPhCOFOqZlI=; b=T38zVQP9kvCSAeBKG3MHmSOZyeA3BLFHTcdeaxord/P/kuxsy+I6PYj28j6D+QQ6Nu /ux4AWjQClkc0RItoXKeXEyGdDhmFvZQ7E8LPAqraeDfemo+ZJvMnDlu5U8xBlB9jy5d h7ULlWTnsuBVxB7IKWQhZsb//F9qiK+PiJH4N+JAnMuptHUTrrmbo7esFVjHW67Pnzl+ BVKQgZoY9vyFiLCmEnDkCf56CXyESBTYmWHmEjqNzCd2XWbGtMPqJn4y2ZmaIeP+zmyL SASggvVqEAe+J31ESO8MN8KeGStFbUAFQmR3mOrONBc8z37MfmIX/pW80ZLVTdzzY2gQ H9Aw== X-Gm-Message-State: AAQBX9dfmg/6yHVyT9/kviF9OZtoBigszUDAr9XRFxKkjLHVbqlYBsD1 QF7L1MAuq3b6UWqQUh4n4bIN+KOcD61o6g== X-Received: from slicestar.c.googlers.com ([fda3:e722:ac3:cc00:4f:4b78:c0a8:20a1]) (user=davidgow job=sendgmr) by 2002:a25:48c4:0:b0:b26:884:c35e with SMTP id v187-20020a2548c4000000b00b260884c35emr4623486yba.4.1680249868180; Fri, 31 Mar 2023 01:04:28 -0700 (PDT) Date: Fri, 31 Mar 2023 16:04:09 +0800 In-Reply-To: <20230331080411.981038-1-davidgow@google.com> Mime-Version: 1.0 References: <20230331080411.981038-1-davidgow@google.com> X-Mailer: git-send-email 2.40.0.348.gf938b09366-goog Message-ID: <20230331080411.981038-2-davidgow@google.com> Subject: [RFC PATCH v2 1/3] kunit: Add kunit_add_action() to defer a call until test exit From: David Gow To: Matti Vaittinen , Maxime Ripard , Brendan Higgins , Stephen Boyd , Shuah Khan , Daniel Latypov , Rae Moar , Benjamin Berg Cc: David Gow , Greg Kroah-Hartman , "Rafael J . Wysocki" , 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 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_add_action() API: a simple function (returning nothing, accepting a single void* argument) can be scheduled to be called when the test exits. Deferred actions are called in the opposite order to that which they were registered. This mimics the devres API, devm_add_action(), and also provides kunit_remove_action(), to cancel a deferred action, and kunit_release_action() to trigger one early. 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 --- Changes since RFC v1: https://lore.kernel.org/linux-kselftest/20230325043104.3761770-2-davidgow@google.com/ - Rename functions to better match the devm_* APIs. (Thanks Maxime) - Embed the kunit_resource in struct kunit_action_ctx to avoid an extra allocation (Thanks Benjamin) - Use 'struct kunit_action_ctx' as the type for cancellation tokens (Thanks Benjamin) - Add tests. --- include/kunit/resource.h | 89 ++++++++++++++++++++++++++++ lib/kunit/kunit-test.c | 123 ++++++++++++++++++++++++++++++++++++++- lib/kunit/resource.c | 99 +++++++++++++++++++++++++++++++ 3 files changed, 310 insertions(+), 1 deletion(-) diff --git a/include/kunit/resource.h b/include/kunit/resource.h index c0d88b318e90..15efd8924666 100644 --- a/include/kunit/resource.h +++ b/include/kunit/resource.h @@ -387,4 +387,93 @@ static inline int kunit_destroy_named_resource(struct kunit *test, */ void kunit_remove_resource(struct kunit *test, struct kunit_resource *res); +typedef void (*kunit_defer_function_t)(void *ctx); + +/* An opaque token to a deferred action. */ +struct kunit_action_ctx; + +/** + * kunit_add_action() - Defer an 'action' (function call) until the test ends. + * @test: Test case to associate the action 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_add_action() 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_remove_action_token() in order to cancel the deferred execution of + * func(). + */ +struct kunit_action_ctx *kunit_add_action(struct kunit *test, kunit_defer_function_t func, + void *ctx, gfp_t internal_gfp); + +/** + * kunit_remove_action_token() - Cancel a deferred action. + * @test: Test case the action is associated with. + * @cancel_token: The cancellation token returned by kunit_add_action() + * + * Prevent an action deferred using kunit_add_action() from executing when the + * test ends. + * + * You can also use the (test, function, context) triplet to remove an action + * with kunit_remove_action(). + */ +void kunit_remove_action_token(struct kunit *test, struct kunit_action_ctx *cancel_token); + +/** + * kunit_release_action_token() - Trigger a deferred action immediately. + * @test: Test case the action is associated with. + * @cancel_token: The cancellation token returned by kunit_add_action() + * + * Execute an action immediately, instead of waiting for the test to end. + * + * You can also use the (test, function, context) triplet to trigger an action + * with kunit_release_action(). + */ +void kunit_release_action_token(struct kunit *test, struct kunit_action_ctx *cancel_token); + +/** + * kunit_remove_action() - Cancel a matching deferred action. + * @test: Test case the action is associated with. + * @func: The deferred function to cancel. + * @ctx: The context passed to the deferred function to trigger. + * + * Prevent an action deferred via kunit_add_action() from executing when the + * test terminates.. + * Unlike kunit_remove_action_token(), this takes the (func, ctx) pair instead of + * the cancellation token. If that function/context pair was deferred multiple + * times, only the most recent one will be cancelled. + */ +void kunit_remove_action(struct kunit *test, + kunit_defer_function_t func, + void *ctx); + +/** + * kunit_release_action() - Run a matching action call immediately. + * @test: Test case the action 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_add_action()) immediately, rather than + * when the test ends. + * Unlike kunit_release_action_token(), this takes the (func, ctx) pair instead of + * the cancellation token. If that function/context pair was deferred multiple + * times, it will only be executed once here. The most recent deferral will + * no longer execute when the test ends. + * + * kunit_release_action(test, func, ctx); + * is equivalent to + * func(ctx); + * kunit_remove_action(test, func, ctx); + */ +void kunit_release_action(struct kunit *test, + kunit_defer_function_t func, + void *ctx); #endif /* _KUNIT_RESOURCE_H */ diff --git a/lib/kunit/kunit-test.c b/lib/kunit/kunit-test.c index b63595d3e241..eaca1b133922 100644 --- a/lib/kunit/kunit-test.c +++ b/lib/kunit/kunit-test.c @@ -111,7 +111,7 @@ struct kunit_test_resource_context { struct kunit test; bool is_resource_initialized; int allocate_order[2]; - int free_order[2]; + int free_order[4]; }; static int fake_resource_init(struct kunit_resource *res, void *context) @@ -402,6 +402,123 @@ static void kunit_resource_test_named(struct kunit *test) KUNIT_EXPECT_TRUE(test, list_empty(&test->resources)); } +static void increment_int(void *ctx) +{ + int *i = (int *)ctx; + (*i)++; +} + +static void kunit_resource_test_action(struct kunit *test) +{ + int num_actions = 0; + + kunit_add_action(test, increment_int, &num_actions, GFP_KERNEL); + KUNIT_EXPECT_EQ(test, num_actions, 0); + kunit_cleanup(test); + KUNIT_EXPECT_EQ(test, num_actions, 1); + + /* Once we've cleaned up, the action queue is empty. */ + kunit_cleanup(test); + KUNIT_EXPECT_EQ(test, num_actions, 1); + + /* Check the same function can be deferred multiple times. */ + kunit_add_action(test, increment_int, &num_actions, GFP_KERNEL); + kunit_add_action(test, increment_int, &num_actions, GFP_KERNEL); + kunit_cleanup(test); + KUNIT_EXPECT_EQ(test, num_actions, 3); +} +static void kunit_resource_test_remove_action(struct kunit *test) +{ + int num_actions = 0; + struct kunit_action_ctx *cancel_token; + struct kunit_action_ctx *cancel_token2; + + cancel_token = kunit_add_action(test, increment_int, &num_actions, GFP_KERNEL); + KUNIT_EXPECT_EQ(test, num_actions, 0); + + kunit_remove_action_token(test, cancel_token); + kunit_cleanup(test); + KUNIT_EXPECT_EQ(test, num_actions, 0); + + /* Check calls from the same function/context pair can be cancelled independently*/ + cancel_token = kunit_add_action(test, increment_int, &num_actions, GFP_KERNEL); + cancel_token2 = kunit_add_action(test, increment_int, &num_actions, GFP_KERNEL); + kunit_remove_action_token(test, cancel_token); + kunit_cleanup(test); + KUNIT_EXPECT_EQ(test, num_actions, 1); + + /* Also check that we can cancel just one of the identical function/context pairs. */ + cancel_token = kunit_add_action(test, increment_int, &num_actions, GFP_KERNEL); + cancel_token2 = kunit_add_action(test, increment_int, &num_actions, GFP_KERNEL); + kunit_remove_action(test, increment_int, &num_actions); + kunit_cleanup(test); + KUNIT_EXPECT_EQ(test, num_actions, 2); +} +static void kunit_resource_test_release_action(struct kunit *test) +{ + int num_actions = 0; + struct kunit_action_ctx *cancel_token; + struct kunit_action_ctx *cancel_token2; + + cancel_token = kunit_add_action(test, increment_int, &num_actions, GFP_KERNEL); + KUNIT_EXPECT_EQ(test, num_actions, 0); + /* Runs immediately on trigger. */ + kunit_release_action_token(test, cancel_token); + KUNIT_EXPECT_EQ(test, num_actions, 1); + + /* Doesn't run again on test exit. */ + kunit_cleanup(test); + KUNIT_EXPECT_EQ(test, num_actions, 1); + + /* Check calls from the same function/context pair can be triggered independently*/ + cancel_token = kunit_add_action(test, increment_int, &num_actions, GFP_KERNEL); + cancel_token2 = kunit_add_action(test, increment_int, &num_actions, GFP_KERNEL); + kunit_release_action_token(test, cancel_token); + KUNIT_EXPECT_EQ(test, num_actions, 2); + kunit_cleanup(test); + KUNIT_EXPECT_EQ(test, num_actions, 3); + + /* Also check that we can trigger just one of the identical function/context pairs. */ + kunit_add_action(test, increment_int, &num_actions, GFP_KERNEL); + kunit_add_action(test, increment_int, &num_actions, GFP_KERNEL); + kunit_release_action(test, increment_int, &num_actions); + KUNIT_EXPECT_EQ(test, num_actions, 4); + kunit_cleanup(test); + KUNIT_EXPECT_EQ(test, num_actions, 5); +} +static void action_order_1(void *ctx) +{ + struct kunit_test_resource_context *res_ctx = (struct kunit_test_resource_context *)ctx; + + KUNIT_RESOURCE_TEST_MARK_ORDER(res_ctx, free_order, 1); + kunit_log(KERN_INFO, current->kunit_test, "action_order_1"); +} +static void action_order_2(void *ctx) +{ + struct kunit_test_resource_context *res_ctx = (struct kunit_test_resource_context *)ctx; + + KUNIT_RESOURCE_TEST_MARK_ORDER(res_ctx, free_order, 2); + kunit_log(KERN_INFO, current->kunit_test, "action_order_2"); +} +static void kunit_resource_test_action_ordering(struct kunit *test) +{ + struct kunit_test_resource_context *ctx = test->priv; + struct kunit_action_ctx *cancel_token; + + kunit_add_action(test, action_order_1, ctx, GFP_KERNEL); + cancel_token = kunit_add_action(test, action_order_2, ctx, GFP_KERNEL); + kunit_add_action(test, action_order_1, ctx, GFP_KERNEL); + kunit_add_action(test, action_order_2, ctx, GFP_KERNEL); + kunit_remove_action(test, action_order_1, ctx); + kunit_release_action_token(test, cancel_token); + kunit_cleanup(test); + + /* [2 is triggered] [2], [(1 is cancelled)] [1] */ + KUNIT_EXPECT_EQ(test, ctx->free_order[0], 2); + KUNIT_EXPECT_EQ(test, ctx->free_order[1], 2); + KUNIT_EXPECT_EQ(test, ctx->free_order[2], 1); +} + static int kunit_resource_test_init(struct kunit *test) { struct kunit_test_resource_context *ctx = @@ -433,6 +550,10 @@ static struct kunit_case kunit_resource_test_cases[] = { KUNIT_CASE(kunit_resource_test_proper_free_ordering), KUNIT_CASE(kunit_resource_test_static), KUNIT_CASE(kunit_resource_test_named), + KUNIT_CASE(kunit_resource_test_action), + KUNIT_CASE(kunit_resource_test_remove_action), + KUNIT_CASE(kunit_resource_test_release_action), + KUNIT_CASE(kunit_resource_test_action_ordering), {} }; diff --git a/lib/kunit/resource.c b/lib/kunit/resource.c index c414df922f34..824cf91e306d 100644 --- a/lib/kunit/resource.c +++ b/lib/kunit/resource.c @@ -77,3 +77,102 @@ int kunit_destroy_resource(struct kunit *test, kunit_resource_match_t match, return 0; } EXPORT_SYMBOL_GPL(kunit_destroy_resource); + +struct kunit_action_ctx { + struct kunit_resource res; + kunit_defer_function_t func; + void *ctx; +}; + +static void __kunit_action_free(struct kunit_resource *res) +{ + struct kunit_action_ctx *action_ctx = container_of(res, struct kunit_action_ctx, res); + + action_ctx->func(action_ctx->ctx); +} + +struct kunit_action_ctx *kunit_add_action(struct kunit *test, kunit_defer_function_t func, + void *ctx, gfp_t internal_gfp) +{ + struct kunit_action_ctx *action_ctx; + + KUNIT_ASSERT_NOT_NULL_MSG(test, func, "Tried to action a NULL function!"); + + action_ctx = kzalloc(sizeof(*action_ctx), internal_gfp); + if (!action_ctx) + return NULL; + + action_ctx->func = func; + action_ctx->ctx = ctx; + + action_ctx->res.should_kfree = true; + /* As init is NULL, this cannot fail. */ + __kunit_add_resource(test, NULL, __kunit_action_free, &action_ctx->res, action_ctx); + + return action_ctx; +} + +void kunit_remove_action_token(struct kunit *test, struct kunit_action_ctx *cancel_token) +{ + /* Remove the free function so we don't run the action. */ + cancel_token->res.free = NULL; + + kunit_remove_resource(test, &cancel_token->res); +} + +void kunit_release_action_token(struct kunit *test, struct kunit_action_ctx *cancel_token) +{ + /* Removing the resource should trigger the res->free function. */ + kunit_remove_resource(test, &cancel_token->res); +} + +static bool __kunit_action_match(struct kunit *test, + struct kunit_resource *res, void *match_data) +{ + struct kunit_action_ctx *match_ctx = (struct kunit_action_ctx *)match_data; + struct kunit_action_ctx *res_ctx = container_of(res, struct kunit_action_ctx, res); + + /* Make sure this is a free function. */ + if (res->free != __kunit_action_free) + return false; + + /* Both the function and context data should match. */ + return (match_ctx->func == res_ctx->func) && (match_ctx->ctx == res_ctx->ctx); +} + +void kunit_remove_action(struct kunit *test, + kunit_defer_function_t func, + void *ctx) +{ + struct kunit_action_ctx match_ctx, *action_ctx; + struct kunit_resource *res; + + match_ctx.func = func; + match_ctx.ctx = ctx; + + res = kunit_find_resource(test, __kunit_action_match, &match_ctx); + if (res) { + action_ctx = container_of(res, struct kunit_action_ctx, res); + kunit_remove_action_token(test, action_ctx); + kunit_put_resource(res); + } +} + +void kunit_release_action(struct kunit *test, + kunit_defer_function_t func, + void *ctx) +{ + struct kunit_action_ctx match_ctx, *action_ctx; + struct kunit_resource *res; + + match_ctx.func = func; + match_ctx.ctx = ctx; + + res = kunit_find_resource(test, __kunit_action_match, &match_ctx); + if (res) { + action_ctx = container_of(res, struct kunit_action_ctx, res); + kunit_release_action_token(test, action_ctx); + /* We have to put() this here, else free won't be called. */ + kunit_put_resource(res); + } +} -- 2.40.0.348.gf938b09366-goog