From patchwork Thu May 18 08:38:43 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Gow X-Patchwork-Id: 95716 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp339012vqo; Thu, 18 May 2023 01:55:34 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ7OmA1pwpsz6otiiuDCM/F9ZZh0/S2MVIi3ZJBExFNgN+eehJRCn28Mgb2OSFw/TtNqy825 X-Received: by 2002:a05:6a20:4322:b0:101:a3ec:889b with SMTP id h34-20020a056a20432200b00101a3ec889bmr1510899pzk.49.1684400133791; Thu, 18 May 2023 01:55:33 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1684400133; cv=none; d=google.com; s=arc-20160816; b=L6ztz2ntt5gh6F6/zmu1jXK4+42zczsJACcA73j3ta+UrJYZo8LnzYACZUKCxksXaf D1Muf95KxrHTDudBvccApDzgO22s28xIYGesRfgpuZmMFWIEJT08R/UNrYx5Zm9Q5j7L oSElsc8rEPjHEE/kiwsFlcj+ws8skO3aJQfpqw61aZAjZbNQw7KN54+qH7eNEQXW1TTS obBHtdkNZEGRWuQKPk4BU32WGvNs9JPdEIRa1QP7NHDNz08UNIeVZ2jIUSSGPP8fcU26 G+/PRYlcDvy8xX4xRR3kwlb3vQa5xu1krYi1j8mD42gEuGKK2AEc/WTV5+TlQYkdHZ4V mUjw== 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:mime-version:date :dkim-signature; bh=XL+AMseJYRt4IcQP1yXntGR3XgHQyJrdXGUbMaoYhPg=; b=G4e0BaijL/hCPT6x3CVwlzQwEcKcTNv+iYtmbtsvDMZ++hAea0aQSi5AEuAklO4GPg drGUKF3/6nwFNnLWqyH5b04kNipq26RL9x6Co7L4x0Gea0Ho8eIwEQNvGq/VoUnFPowQ pYxvAzP5hejdnqxeFcv+dpAL1Y9jVt401kuhS3Q3ymSI/VE4dn/cTaNIBJmhx4rSmGte 4ludju5/RZfMEGrsljE2MglKOvhbSQXyYEwEqlaL4vEVJTlRGaXceZdGpfdmAseOhDmD stPvc5bNDtbRkrZz2XzPlU67HwnH7tW6bc8vyYOg5UJKw95pxdB+HHuTkmCmjikgl7Vy kYjA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20221208 header.b=xqF0ZeJY; 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: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id g9-20020a63fa49000000b0053045e0307esi898120pgk.451.2023.05.18.01.55.15; Thu, 18 May 2023 01:55:33 -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=20221208 header.b=xqF0ZeJY; 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 S230142AbjERIjE (ORCPT + 99 others); Thu, 18 May 2023 04:39:04 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33964 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230236AbjERIi6 (ORCPT ); Thu, 18 May 2023 04:38:58 -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 E5B101BEA for ; Thu, 18 May 2023 01:38:55 -0700 (PDT) Received: by mail-yw1-x1149.google.com with SMTP id 00721157ae682-559ea6b1065so32089397b3.0 for ; Thu, 18 May 2023 01:38:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1684399135; x=1686991135; h=cc:to:from:subject:message-id:mime-version:date:from:to:cc:subject :date:message-id:reply-to; bh=XL+AMseJYRt4IcQP1yXntGR3XgHQyJrdXGUbMaoYhPg=; b=xqF0ZeJYWlalSgIEmsvsLIH19g+gHioNPzSN7+aavrhIaIGsG93ffjQZtI+lUuxxPj +i2eMrEinCczDyO5YMtePv1mq6H3V+Ba3pO2/OeWWblehWMVzzmbWoOfauA8z46xWLlb HZNpjbMxCqMpWJL26K87YogVTHx5zJjdPeC8s8UYB1P9wnW889Lh5en02Zk09Ktqu1up ZVEf+4LBKi/iEf6a9rhDGj3R6CvzQCVeGMsegvjm/naonocQwvKlQEhJ0VNcE/BRArEI YVWty7A2N+bHMzAScq51l39GrSjDwATdMiwVFv8SeBG9rNU9fo+S6+TyMztib/WhO4/L rrWw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1684399135; x=1686991135; h=cc:to:from:subject:message-id:mime-version:date:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=XL+AMseJYRt4IcQP1yXntGR3XgHQyJrdXGUbMaoYhPg=; b=hx53Fy9qQ2HQVWQ7KrehLguehQ5jUAAvmQ64u5cA57a3iHf/BLrRlHTXYMJi7xNhhS l1kR2SWT1gyLuHTHhO7FT/Ulpr1CSBaoxWMMYM+tX8UB2/qrTf6LNnrFjRF4qOBiE2dD G3eUQdxtO6fuso2RhMZ2uQiWYOmGmRDPuaD7wZq/p4P4lx+w6Idgt60+a/hv2g+FYc3b 4iw5ZSbuxR4oAdCL4QTtSBQ7KoGILgooaInXPBwGkZn7VfQa7OG4VqLs7qfAlgXtt70i JjUXdMWbV9omqFB7NSBAo6F7up09mTZDxHDEULl5WTPK5YQ7tHmy9T0ncHaC8kSZnLec /cPQ== X-Gm-Message-State: AC+VfDz/NBrKnHMS4HUtN2Y2/Npcefn9auf8GRJ5YNzcvE57JHE6hKJ3 +y/0KT4ZAtx29sqT6ysNAyavDYUWdYZ6mw== X-Received: from slicestar.c.googlers.com ([fda3:e722:ac3:cc00:4f:4b78:c0a8:20a1]) (user=davidgow job=sendgmr) by 2002:a81:c80c:0:b0:561:878a:687c with SMTP id n12-20020a81c80c000000b00561878a687cmr624231ywi.4.1684399134976; Thu, 18 May 2023 01:38:54 -0700 (PDT) Date: Thu, 18 May 2023 16:38:43 +0800 Mime-Version: 1.0 X-Mailer: git-send-email 2.40.1.698.g37aff9b760-goog Message-ID: <20230518083849.2631178-1-davidgow@google.com> Subject: [PATCH v2 1/4] kunit: Add kunit_add_action() to defer a call until test exit From: David Gow To: Matti Vaittinen , Daniel Latypov , Maxime Ripard , Benjamin Berg , Brendan Higgins , Stephen Boyd , Shuah Khan , Rae Moar Cc: David Gow , Greg Kroah-Hartman , "Rafael J . Wysocki" , Heikki Krogerus , Jonathan Cameron , Sadiya Kazi , Jonathan Corbet , linux-doc@vger.kernel.org, kunit-dev@googlegroups.com, linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org, Benjamin Berg X-Spam-Status: No, score=-9.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE,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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1766221554875770972?= X-GMAIL-MSGID: =?utf-8?q?1766221554875770972?= 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. Reviewed-by: Benjamin Berg Reviewed-by: Maxime Ripard Tested-by: Maxime Ripard Signed-off-by: David Gow --- Changes since v1: https://lore.kernel.org/linux-kselftest/20230421084226.2278282-2-davidgow@google.com/ - Some small documentation updates (Thanks Daniel) - Reinstate a typedef for the action function. - This time, it's called kunit_action_t - Thanks Maxime! Changes since RFC v2: https://lore.kernel.org/linux-kselftest/20230331080411.981038-2-davidgow@google.com/ - Got rid of internal_gfp - everything uses GFP_KERNEL now - This includes kunit_kzalloc() and friends, which still allocate the returned memory with the provided GFP, but use GFP_KERNEL for internal bookkeeping data. - Thanks Maxime & Benjamin! - Got rid of cancellation tokens. - kunit_add_action() now returns 0 on success, otherwise an error - Note that this can quite easily lead to a memory leak, so look at kunit_add_action_or_reset() - Thanks Maxime & Benjamin! - Added kunit_add_action_or_reset - Matches devm_add_action_or_reset() - Returns 0 on success. - Thanks Maxime & Benjamin! - Got rid of the kunit_defer_func_t typedef. - I liked it, but it is probably pushing the boundaries of kernel style. - Use (void (*)(void *)) instead. 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 | 92 +++++++++++++++++++++++++++++++++++++ lib/kunit/kunit-test.c | 88 ++++++++++++++++++++++++++++++++++- lib/kunit/resource.c | 99 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 278 insertions(+), 1 deletion(-) diff --git a/include/kunit/resource.h b/include/kunit/resource.h index c0d88b318e90..b64eb783b1bc 100644 --- a/include/kunit/resource.h +++ b/include/kunit/resource.h @@ -387,4 +387,96 @@ static inline int kunit_destroy_named_resource(struct kunit *test, */ void kunit_remove_resource(struct kunit *test, struct kunit_resource *res); +/* A 'deferred action' function to be used with kunit_add_action. */ +typedef void (kunit_action_t)(void *); + +/** + * kunit_add_action() - Call a function when the test ends. + * @test: Test case to associate the action with. + * @func: The function to run on test exit + * @ctx: Data passed into @func + * + * 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, as these + * functions are called even if the test aborts early due to, e.g., a failed + * assertion. + * + * See also: devm_add_action() for the devres equivalent. + * + * Returns: + * 0 on success, an error if the action could not be deferred. + */ +int kunit_add_action(struct kunit *test, kunit_action_t *action, void *ctx); + +/** + * kunit_add_action_or_reset() - Call a function when the test ends. + * @test: Test case to associate the action with. + * @func: The function to run on test exit + * @ctx: Data passed into @func + * + * 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, as these + * functions are called even if the test aborts early due to, e.g., a failed + * assertion. + * + * If the action cannot be created (e.g., due to the system being out of memory), + * then action(ctx) will be called immediately, and an error will be returned. + * + * See also: devm_add_action_or_reset() for the devres equivalent. + * + * Returns: + * 0 on success, an error if the action could not be deferred. + */ +int kunit_add_action_or_reset(struct kunit *test, kunit_action_t *action, + void *ctx); + +/** + * 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. + * + * If the function/context pair was deferred multiple times, only the most + * recent one will be cancelled. + * + * See also: devm_remove_action() for the devres equivalent. + */ +void kunit_remove_action(struct kunit *test, + kunit_action_t *action, + 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. + * + * If the 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); + * + * See also: devm_release_action() for the devres equivalent. + */ +void kunit_release_action(struct kunit *test, + kunit_action_t *action, + void *ctx); #endif /* _KUNIT_RESOURCE_H */ diff --git a/lib/kunit/kunit-test.c b/lib/kunit/kunit-test.c index 42e44caa1bdd..83d8e90ca7a2 100644 --- a/lib/kunit/kunit-test.c +++ b/lib/kunit/kunit-test.c @@ -112,7 +112,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) @@ -403,6 +403,88 @@ 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); + 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); + kunit_add_action(test, increment_int, &num_actions); + kunit_cleanup(test); + KUNIT_EXPECT_EQ(test, num_actions, 3); +} +static void kunit_resource_test_remove_action(struct kunit *test) +{ + int num_actions = 0; + + kunit_add_action(test, increment_int, &num_actions); + KUNIT_EXPECT_EQ(test, num_actions, 0); + + kunit_remove_action(test, increment_int, &num_actions); + kunit_cleanup(test); + KUNIT_EXPECT_EQ(test, num_actions, 0); +} +static void kunit_resource_test_release_action(struct kunit *test) +{ + int num_actions = 0; + + kunit_add_action(test, increment_int, &num_actions); + KUNIT_EXPECT_EQ(test, num_actions, 0); + /* Runs immediately on trigger. */ + kunit_release_action(test, increment_int, &num_actions); + KUNIT_EXPECT_EQ(test, num_actions, 1); + + /* Doesn't run again on test exit. */ + kunit_cleanup(test); + KUNIT_EXPECT_EQ(test, num_actions, 1); +} +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; + + kunit_add_action(test, action_order_1, ctx); + kunit_add_action(test, action_order_2, ctx); + kunit_add_action(test, action_order_1, ctx); + kunit_add_action(test, action_order_2, ctx); + kunit_remove_action(test, action_order_1, ctx); + kunit_release_action(test, action_order_2, ctx); + 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 = @@ -434,6 +516,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..f0209252b179 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_action_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); +} + + +int kunit_add_action(struct kunit *test, void (*action)(void *), void *ctx) +{ + struct kunit_action_ctx *action_ctx; + + KUNIT_ASSERT_NOT_NULL_MSG(test, action, "Tried to action a NULL function!"); + + action_ctx = kzalloc(sizeof(*action_ctx), GFP_KERNEL); + if (!action_ctx) + return -ENOMEM; + + action_ctx->func = action; + 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 0; +} +EXPORT_SYMBOL_GPL(kunit_add_action); + +int kunit_add_action_or_reset(struct kunit *test, void (*action)(void *), + void *ctx) +{ + int res = kunit_add_action(test, action, ctx); + + if (res) + action(ctx); + return res; +} +EXPORT_SYMBOL_GPL(kunit_add_action_or_reset); + +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_action_t *action, + void *ctx) +{ + struct kunit_action_ctx match_ctx; + struct kunit_resource *res; + + match_ctx.func = action; + match_ctx.ctx = ctx; + + res = kunit_find_resource(test, __kunit_action_match, &match_ctx); + if (res) { + /* Remove the free function so we don't run the action. */ + res->free = NULL; + kunit_remove_resource(test, res); + kunit_put_resource(res); + } +} +EXPORT_SYMBOL_GPL(kunit_remove_action); + +void kunit_release_action(struct kunit *test, + kunit_action_t *action, + void *ctx) +{ + struct kunit_action_ctx match_ctx; + struct kunit_resource *res; + + match_ctx.func = action; + match_ctx.ctx = ctx; + + res = kunit_find_resource(test, __kunit_action_match, &match_ctx); + if (res) { + kunit_remove_resource(test, res); + /* We have to put() this here, else free won't be called. */ + kunit_put_resource(res); + } +} +EXPORT_SYMBOL_GPL(kunit_release_action); From patchwork Thu May 18 08:38:44 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Gow X-Patchwork-Id: 95711 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp333864vqo; Thu, 18 May 2023 01:42:20 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ6ZRjUFfFGtKgUsI+JDXV/o4NUyFhnXtuwZUyC+XLESe8zFkdR9g8uJ3dyNTRHlxiTwvlnu X-Received: by 2002:a05:6a00:1150:b0:643:98cb:ec1 with SMTP id b16-20020a056a00115000b0064398cb0ec1mr3616938pfm.0.1684399339735; Thu, 18 May 2023 01:42:19 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1684399339; cv=none; d=google.com; s=arc-20160816; b=MePgI7g2FHwmY3VfPGVh1ymQeYapqkmbihA+CWp861xz9+DgKeXIE6REHBxLLHDbgC 12BcqRoQgX8jiMs31DvAKUkZBYHLgafq6b1SOdKdbJZ4G+jHaaNc883ZH5eqq4SIrp4D iGslHJ9IfK3mIomgFQ86ICxZegfLyiJhwfvORIwGqMQz96MsT6PSGxjo5VZQudmeitlX HcGY+xPU5R06g0x6ZF5227yUv79h3ek/bBEQ3hUQVXyqpMKx+T9QN8hX46ifZylSWrLE N31IkTIDVS8zdjLWwDXuXtQ+yySoowNXAsYQUHUKTTmCqggTILsW7X96CTDSzQmr5i0w sIfQ== 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=k09SwZ0B843M7hzpK5Qk20c0ZsPOaztHq3ds70w84Ac=; b=L/n5+CJ3uDLMReT20q1Z62vqeMfVc2zNKnNx4fnzH7wFntbHUJ2WbV+onXR6J9+K94 Q4+X4NhlpzGMjgvuc4WtztdIn3PMnTEC2Y4Uv7a/GyhIu5mvXWVVcT68M+62MSvKRaUB cbipwk3kNpW2wnJGj70Shl77DJOvRp9u8LFgAZFfiEeHb4Fq4+1XjaK4wL1N0oz2UTVA 6kYENK7SwKGneTtvFg1jhuzJ1EJsaVJvZUC7yIiXt/YyfWT/lyDKd1CK2a/WYkGI4OCJ FLEZOOtKm7BZkkjdtJwIoy6IdZR/vhG0Go2VqReK+D4P5VfO4HZVDbS7CZzwvMpejUI5 3t6Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20221208 header.b=vnqcwkGY; 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: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id r30-20020aa79ede000000b00647d69652bdsi1130172pfq.264.2023.05.18.01.42.06; Thu, 18 May 2023 01:42:19 -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=20221208 header.b=vnqcwkGY; 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 S230236AbjERIjS (ORCPT + 99 others); Thu, 18 May 2023 04:39:18 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34176 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229944AbjERIjF (ORCPT ); Thu, 18 May 2023 04:39:05 -0400 Received: from mail-yb1-xb4a.google.com (mail-yb1-xb4a.google.com [IPv6:2607:f8b0:4864:20::b4a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B92431BE9 for ; Thu, 18 May 2023 01:39:00 -0700 (PDT) Received: by mail-yb1-xb4a.google.com with SMTP id 3f1490d57ef6-ba83fed51b0so1996978276.3 for ; Thu, 18 May 2023 01:39:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1684399139; x=1686991139; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=k09SwZ0B843M7hzpK5Qk20c0ZsPOaztHq3ds70w84Ac=; b=vnqcwkGYGPFsg9Zw/C4wrRWoRxTjw9RrDrLzotAhYpmOuTUO7rj38F0+YNmWSN+H0k JGx31FEd3AnjuoyVMv9ih8lsPUTZUoZCt+dLHEanU9CN1/585218J970kCLia5JDJFAY YtxP7Wk9S3bGa4YEBol4rDz+ESgEmZuEUuNXOAiwvgV/ElJam1ozyTcJ4V8S++Yy8DTl Pva13wqc3v+uSZIJ8MfJ2CEXM99InX6Be/ax6GI4pv9jVOb5TOfrtAUx/nLtVV6LQz9i WrZizdZLdQLn8jJcEtKvdgHDiA+4t7QuwCcd8avUEEewp/oWXJZAr+Z052k/hE3Gij8g Tzug== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1684399139; x=1686991139; 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=k09SwZ0B843M7hzpK5Qk20c0ZsPOaztHq3ds70w84Ac=; b=i1kjx0TJJsBg1m93DXuOLEcxG0WWqLzO88kGs+z7So+pk0U3ml7sNqtChjtttdwwnG jfxWs1CqAgZiNN7TaAIP5ZdFYzIc+V7IJpK1KtyVL36Nfd51tmMfm6tB1vVvkoriCnL6 MN+2a1M1FO3KHk+5Dy78KRSmiI5ck7Q6E3UjHXIqx9VXXz+qObnTXuuPbcTljJh/ZTdf 9wRyv7yvt2Hpgzt0wi6Hcc/NlRWaVZ4V6vIkROLzINHjAgILgA5/s+ugDLqSALTMk1XC nuuoX5A3gPumvevD0nokbchC1Gpiv9vb8+rNN8FvkU2yLozQTuPAoq2cQhpPP8hFuil1 nbag== X-Gm-Message-State: AC+VfDyh7iUjx0ZDEzZ5sz6ucvaJyvCph5dRZw0ZjRRLV7jtINvHqR19 Pjxst2Qe4p48hVGJR3qizSziECL2Ad4SDw== X-Received: from slicestar.c.googlers.com ([fda3:e722:ac3:cc00:4f:4b78:c0a8:20a1]) (user=davidgow job=sendgmr) by 2002:a25:99c4:0:b0:ba8:ab65:ef39 with SMTP id q4-20020a2599c4000000b00ba8ab65ef39mr268701ybo.4.1684399139428; Thu, 18 May 2023 01:38:59 -0700 (PDT) Date: Thu, 18 May 2023 16:38:44 +0800 In-Reply-To: <20230518083849.2631178-1-davidgow@google.com> Mime-Version: 1.0 References: <20230518083849.2631178-1-davidgow@google.com> X-Mailer: git-send-email 2.40.1.698.g37aff9b760-goog Message-ID: <20230518083849.2631178-2-davidgow@google.com> Subject: [PATCH v2 2/4] kunit: executor_test: Use kunit_add_action() From: David Gow To: Matti Vaittinen , Daniel Latypov , Maxime Ripard , Benjamin Berg , Brendan Higgins , Stephen Boyd , Shuah Khan , Rae Moar Cc: David Gow , Greg Kroah-Hartman , "Rafael J . Wysocki" , Heikki Krogerus , Jonathan Cameron , Sadiya Kazi , Jonathan Corbet , linux-doc@vger.kernel.org, kunit-dev@googlegroups.com, linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org, Benjamin Berg X-Spam-Status: No, score=-9.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE,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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1766220721914401574?= X-GMAIL-MSGID: =?utf-8?q?1766220721914401574?= Now we have the kunit_add_action() function, we can use it to implement kfree_at_end() and free_subsuite_at_end() without the need for extra helper functions. Reviewed-by: Benjamin Berg Reviewed-by: Maxime Ripard Tested-by: Maxime Ripard Signed-off-by: David Gow --- Changes since v1: https://lore.kernel.org/linux-kselftest/20230421084226.2278282-3-davidgow@google.com/ - Use the kunit_action_t typedef Changes since RFCv2: https://lore.kernel.org/linux-kselftest/20230331080411.981038-3-davidgow@google.com/ - Don't use the no-longer-extant kunit_defer_func_t typedef. - Don't pass a GFP pointer in. --- lib/kunit/executor_test.c | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/lib/kunit/executor_test.c b/lib/kunit/executor_test.c index 0cea31c27b23..ce6749af374d 100644 --- a/lib/kunit/executor_test.c +++ b/lib/kunit/executor_test.c @@ -125,11 +125,6 @@ kunit_test_suites(&executor_test_suite); /* Test helpers */ -static void kfree_res_free(struct kunit_resource *res) -{ - kfree(res->data); -} - /* Use the resource API to register a call to kfree(to_free). * Since we never actually use the resource, it's safe to use on const data. */ @@ -138,8 +133,10 @@ static void kfree_at_end(struct kunit *test, const void *to_free) /* kfree() handles NULL already, but avoid allocating a no-op cleanup. */ if (IS_ERR_OR_NULL(to_free)) return; - kunit_alloc_resource(test, NULL, kfree_res_free, GFP_KERNEL, - (void *)to_free); + + kunit_add_action(test, + (kunit_action_t *)kfree, + (void *)to_free); } static struct kunit_suite *alloc_fake_suite(struct kunit *test, From patchwork Thu May 18 08:38:45 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Gow X-Patchwork-Id: 95710 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp333860vqo; Thu, 18 May 2023 01:42:19 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ6mpEONlGfIBw057RMrQ1ZOLjoQcW3ysvZDYGWhraRknH5RG45OIUt1ValBNiUJ+vgLTPGq X-Received: by 2002:a05:6a00:1907:b0:647:7ee8:6251 with SMTP id y7-20020a056a00190700b006477ee86251mr3304273pfi.21.1684399339167; Thu, 18 May 2023 01:42:19 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1684399339; cv=none; d=google.com; s=arc-20160816; b=FNqKZ9UQSayUXH48PbdMJKGN5f3rTCCOGFuYM5vG7N6arXvTPiqEI7e4s1obw54jbO CPQaGqwkJ0WeUBV8QGdOjbK6VPLrruEVnNMgFAn7ftj6Uo5C1wTFpz5Zss1PkC9szuC+ 5Tf7zmzCG7rAzpj4jjYaQB3WnWXmDA89Cd+EyDJXIh9qvPDeMQ+T928jIxGIDBZLYN+M 4nKnZWrvWs0C2eNO4dzaVKXmSAUz3sB8JOFrzoh0izfGLMLe0o73cIYbvSUzrSs8yYp+ MUhbPNy65WhBbpqW2PkQLTlm4q466/MsHqZDDnb+uTG1tdpFkgodSihqtk4l8GSDx0WX bCbg== 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=OMMY2IhA+ayZjLnqfRnZckNf3MVLjRtK6Q5wsal9HA8=; b=wQhkMOBzkRxUZubkTneS7wJSSvLsbUkL7707QD3vDEGMr9lF1/JV5VyEz25NqWnudE ucMfg67oc/KU4nVQ7Vum1La0rQ8S4QXmJQs3PMIfrUwjWavkO617m/IKnHAQNAGZh0pr 315zpvkQm+MiLqJfGOgwClfH0UpejxmdKp5H92e/v6oT7rODsjmRCM9cYRA5DLdBFGAs 1GKCrlMjGCAzJOxjdDCxoci5tOJPdOTDvVlY691REwBb1QxXuN4AHA1wQOxez/lYE8NO BvZ7KhPFzGChvZWQ/bdyOQUsCgcAz5ceg9N2Twbd20dNniz+ZcbyuuPmz+M+bym0udVw Yh1g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20221208 header.b=3x5r0tGr; 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: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id f4-20020aa79d84000000b0063d33dc49e8si1118913pfq.363.2023.05.18.01.42.06; Thu, 18 May 2023 01:42:19 -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=20221208 header.b=3x5r0tGr; 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 S230029AbjERIjX (ORCPT + 99 others); Thu, 18 May 2023 04:39:23 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34378 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230252AbjERIjP (ORCPT ); Thu, 18 May 2023 04:39:15 -0400 Received: from mail-yb1-xb49.google.com (mail-yb1-xb49.google.com [IPv6:2607:f8b0:4864:20::b49]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A14E826B1 for ; Thu, 18 May 2023 01:39:04 -0700 (PDT) Received: by mail-yb1-xb49.google.com with SMTP id 3f1490d57ef6-ba8337ade1cso3760133276.2 for ; Thu, 18 May 2023 01:39:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1684399144; x=1686991144; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=OMMY2IhA+ayZjLnqfRnZckNf3MVLjRtK6Q5wsal9HA8=; b=3x5r0tGrLw6rFp6TVHFxeLH725gzBcx5wp2Dx0AhmvLNwoWJPanri0ykYahpqr4Bh/ 3flyLRW5GC68zojn7bGlc1MPaiwsXlURyNl8wVubmu0OMi7PsJN2xlIqllKqTqhpTmD6 xS1Gu+ANAXFWP4km77v1QHYuvmT+FC1EwWTUmB2TfTtJrwYVSms2A+trHMDHBToiOwEf tclxWcxLDJGNEVRE2h/7o7GGPBtZHT22GyMcOFSgPTuNJkhFAs/9THQtYQwaUiIQLWWe 7vdZUf107RvLfDEURZZ25TNxWeuyHyfjsW+9LIIJJUeW2N+9LjnBzV5lPDqUUsu46jTH FGjQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1684399144; x=1686991144; 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=OMMY2IhA+ayZjLnqfRnZckNf3MVLjRtK6Q5wsal9HA8=; b=I6PorWYNAnv/VzQ3FSjO8QTCLuqOIzALxwBi38Of7u8NRAiMFVqBV0PcrKzYoCZet+ mBG3tHUbQQsGCyuDoPR1/yWBi65Vf22SmuTC4mD592MK2QB/duddlmXzlV1cVgTBdP2g Rjxe8ip6Nqs3IhqhMI8oIDPwOb9fUnugCcJAHKHYDbna+7TV3M4+KLuX2F45T54GWCkV wTsOgcShwq1uCwY1oydKc9WdEKR6A8nC0i3kUCDyFTgsmTIJtVDYss/cObhbGiuTLNsc SZE573Z0I8pR9GNoaqkFA3Y/MYr+wDmmERmw6y8K8svtslu6n6aGifr8g9quU4h4K9zo TqnQ== X-Gm-Message-State: AC+VfDxRVS43JKz6m9sfUZoRrDaysS73WzFaC3CbqhFZPViNg29TdJjS 8JocDK5FoQePEO2T9kNKxmQFo4RxEvuSKA== X-Received: from slicestar.c.googlers.com ([fda3:e722:ac3:cc00:4f:4b78:c0a8:20a1]) (user=davidgow job=sendgmr) by 2002:a25:2e07:0:b0:ba8:6b34:a6ce with SMTP id u7-20020a252e07000000b00ba86b34a6cemr461552ybu.8.1684399143958; Thu, 18 May 2023 01:39:03 -0700 (PDT) Date: Thu, 18 May 2023 16:38:45 +0800 In-Reply-To: <20230518083849.2631178-1-davidgow@google.com> Mime-Version: 1.0 References: <20230518083849.2631178-1-davidgow@google.com> X-Mailer: git-send-email 2.40.1.698.g37aff9b760-goog Message-ID: <20230518083849.2631178-3-davidgow@google.com> Subject: [PATCH v2 3/4] kunit: kmalloc_array: Use kunit_add_action() From: David Gow To: Matti Vaittinen , Daniel Latypov , Maxime Ripard , Benjamin Berg , Brendan Higgins , Stephen Boyd , Shuah Khan , Rae Moar Cc: David Gow , Greg Kroah-Hartman , "Rafael J . Wysocki" , Heikki Krogerus , Jonathan Cameron , Sadiya Kazi , Jonathan Corbet , linux-doc@vger.kernel.org, kunit-dev@googlegroups.com, linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org, Benjamin Berg X-Spam-Status: No, score=-9.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE,USER_IN_DEF_DKIM_WL autolearn=ham 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1766220721765272255?= X-GMAIL-MSGID: =?utf-8?q?1766220721765272255?= The kunit_add_action() function is much simpler and cleaner to use that the full KUnit resource API for simple things like the kunit_kmalloc_array() functionality. Replacing it allows us to get rid of a number of helper functions, and leaves us with no uses of kunit_alloc_resource(), which has some usability problems and is going to have its behaviour modified in an upcoming patch. Note that we need to use kunit_defer_trigger_all() to implement kunit_kfree(). Reviewed-by: Benjamin Berg Reviewed-by: Maxime Ripard Tested-by: Maxime Ripard Signed-off-by: David Gow --- Changes since v1: https://lore.kernel.org/linux-kselftest/20230421084226.2278282-4-davidgow@google.com/ - Use the kunit_action_t typedef. - This fixes some spurious checkpatch warnings. Changes since RFCv2: https://lore.kernel.org/linux-kselftest/20230331080411.981038-4-davidgow@google.com/ - Update to match changes in the the action API. - Always allocate the action context with GFP_KERNEL. - Update documentation to note that this will cause GFP_KERNEL allocations, regardless of the gfp argument passed in. --- include/kunit/test.h | 10 +++++++-- lib/kunit/test.c | 48 +++++++++----------------------------------- 2 files changed, 17 insertions(+), 41 deletions(-) diff --git a/include/kunit/test.h b/include/kunit/test.h index 3028a1a3fcad..2f23d6efa505 100644 --- a/include/kunit/test.h +++ b/include/kunit/test.h @@ -324,8 +324,11 @@ enum kunit_status kunit_suite_has_succeeded(struct kunit_suite *suite); * @gfp: flags passed to underlying kmalloc(). * * Just like `kmalloc_array(...)`, except the allocation is managed by the test case - * and is automatically cleaned up after the test case concludes. See &struct - * kunit_resource for more information. + * and is automatically cleaned up after the test case concludes. See kunit_add_action() + * for more information. + * + * Note that some internal context data is also allocated with GFP_KERNEL, + * regardless of the gfp passed in. */ void *kunit_kmalloc_array(struct kunit *test, size_t n, size_t size, gfp_t gfp); @@ -336,6 +339,9 @@ void *kunit_kmalloc_array(struct kunit *test, size_t n, size_t size, gfp_t gfp); * @gfp: flags passed to underlying kmalloc(). * * See kmalloc() and kunit_kmalloc_array() for more information. + * + * Note that some internal context data is also allocated with GFP_KERNEL, + * regardless of the gfp passed in. */ static inline void *kunit_kmalloc(struct kunit *test, size_t size, gfp_t gfp) { diff --git a/lib/kunit/test.c b/lib/kunit/test.c index f5e4ceffd282..d3fb93a23ccc 100644 --- a/lib/kunit/test.c +++ b/lib/kunit/test.c @@ -752,58 +752,28 @@ static struct notifier_block kunit_mod_nb = { }; #endif -struct kunit_kmalloc_array_params { - size_t n; - size_t size; - gfp_t gfp; -}; - -static int kunit_kmalloc_array_init(struct kunit_resource *res, void *context) +void *kunit_kmalloc_array(struct kunit *test, size_t n, size_t size, gfp_t gfp) { - struct kunit_kmalloc_array_params *params = context; + void *data; - res->data = kmalloc_array(params->n, params->size, params->gfp); - if (!res->data) - return -ENOMEM; + data = kmalloc_array(n, size, gfp); - return 0; -} + if (!data) + return NULL; -static void kunit_kmalloc_array_free(struct kunit_resource *res) -{ - kfree(res->data); -} + if (kunit_add_action_or_reset(test, (kunit_action_t *)kfree, data) != 0) + return NULL; -void *kunit_kmalloc_array(struct kunit *test, size_t n, size_t size, gfp_t gfp) -{ - struct kunit_kmalloc_array_params params = { - .size = size, - .n = n, - .gfp = gfp - }; - - return kunit_alloc_resource(test, - kunit_kmalloc_array_init, - kunit_kmalloc_array_free, - gfp, - ¶ms); + return data; } EXPORT_SYMBOL_GPL(kunit_kmalloc_array); -static inline bool kunit_kfree_match(struct kunit *test, - struct kunit_resource *res, void *match_data) -{ - /* Only match resources allocated with kunit_kmalloc() and friends. */ - return res->free == kunit_kmalloc_array_free && res->data == match_data; -} - void kunit_kfree(struct kunit *test, const void *ptr) { if (!ptr) return; - if (kunit_destroy_resource(test, kunit_kfree_match, (void *)ptr)) - KUNIT_FAIL(test, "kunit_kfree: %px already freed or not allocated by kunit", ptr); + kunit_release_action(test, (kunit_action_t *)kfree, (void *)ptr); } EXPORT_SYMBOL_GPL(kunit_kfree); From patchwork Thu May 18 08:38:46 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Gow X-Patchwork-Id: 95714 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp337604vqo; Thu, 18 May 2023 01:51:39 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ6P6EIX3xRxAcRx+y+GhbYmYtSTAkbyRd4xtHocC2TF8ZcnU3iopPlBo4Bqv9Js+RrghoQB X-Received: by 2002:a17:90a:6844:b0:252:7a08:8c with SMTP id e4-20020a17090a684400b002527a08008cmr1526219pjm.48.1684399898876; Thu, 18 May 2023 01:51:38 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1684399898; cv=none; d=google.com; s=arc-20160816; b=hZcI9BF1vUHp3x3cBGp2EmixORFXoVDkmOiuHn66IxySh0S8SyS6T9YRd8Axei0nQT 2DSA1y7VrfX9kRfQruHyRJXy6iB2y/G22ZHUUqAowVaCTwtaFPRpVfoenXZuaY9s0c55 o37QWWWPsAdnAD9jwXY8d/klD4ZyklLjgLulxxRU1KSFLDRuIZoRfuFeA61c29RPTHVL HwfDDZ4D4rDRSConAe/BCNTK083+yyiWplpy4zCiEG2AX7oD/IpIOZ+feOCbvHtk9mbo lc7UETSBTQDGjrmqnMDTnyVopRJ/SJeADp023Y/CgI2DKc8NqcCAtBoVDKAqP73jmEaM m36g== 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=ktXlgtWlyfQZQ1d8uj3tU7I14o2usUDk+fQoHXPWmDU=; b=ApedufvnLj12ew5c80Yp9+KO4M1ZQ6hV/K/xih3YQyojiQnxSzQ2vQM+rzk3Aq9Bjl x6VitcXzLC6tVlSPa8JR52xl+bEAWMtRkTUN5QKhNQFbqMJUaERwfIv7W3Y9J+RXnI73 q4tFsGleimDyJA0H96Z+sserFiq+DdI9i8l4mhFu8K+rA9xLkeTt2QHnWxr0bgdMCscP PIU7J9LOcS+2Aub5z/gFOTPaFXwkyM688ko+qOxp9Cc0Fy+6VbeT6XFuiMYpjjgKtN1l YKlRKCxHZEpckKtwscR62aLngX2uKvWU8Mgq96agLuzf4NLm+dprfFOvYRKlrGb4I20c W6VQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20221208 header.b=VgXvSLQJ; 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: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id x20-20020a17090a8a9400b0025357108059si1930404pjn.44.2023.05.18.01.51.23; Thu, 18 May 2023 01:51:38 -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=20221208 header.b=VgXvSLQJ; 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 S230289AbjERIja (ORCPT + 99 others); Thu, 18 May 2023 04:39:30 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34356 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230187AbjERIjS (ORCPT ); Thu, 18 May 2023 04:39:18 -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 1884A2722 for ; Thu, 18 May 2023 01:39:09 -0700 (PDT) Received: by mail-yw1-x114a.google.com with SMTP id 00721157ae682-5618f3273dbso36744327b3.1 for ; Thu, 18 May 2023 01:39:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1684399148; x=1686991148; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=ktXlgtWlyfQZQ1d8uj3tU7I14o2usUDk+fQoHXPWmDU=; b=VgXvSLQJQOhsSG6UOczEvxUlPM2ur85ri/n2c3BM88UVbcvRwtybZUbqHQhPphTTJS qEWKuFqmBczy64nT6g8RK9M0ViMel6Cy7xisxqBJxPUigBtu6vz6jkTH0+jiaz9nm4b4 6TFHJYWdYV+NZpu4BoGQPTt3tN5hUpqsm7tVYb7IeAIRVtgwF04QFOM5GjQiJHVbW8Tc eTclL8ikuZG/7d29UbQkFQ12xoBavLfUfepLs9eBVPOhRNUoXKAs20vs//yM+L4MgSkl zdkQaUk8jDtbrHgUZ5Fa3ZQFkZ/vy76GsD+pudEZUIKjppI9j5mPWYRwKR0AwhyXPU7q UYaw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1684399148; x=1686991148; 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=ktXlgtWlyfQZQ1d8uj3tU7I14o2usUDk+fQoHXPWmDU=; b=dsMztLcX3KmweHRUuJ0MGFcnUkkYy3gwFE8K/kfYX8f05RYexFpNP7Cl3JvAApSh09 0xG0qW+CSG6zkO+ykIeg7fGQQdnQJCz2KN+jk29/hKnnohkrWaXGvdw5RyxF/hmlGl1y vWVvPNlklOmMVCg68IEV/6e9pBnkGLtePIlm5WDP9B6D4s/hjIkWi9DkyVpYaoKO+/LJ 7GnTW/4PnSH/NePhuMTvM37lqCO++qWjQ5E/iSSLVw6maYqD6cE01I2GeF+7avCSUlUS 0tXPtSWqC8FNv3Ykp+Pupxs3MVPJlFZSpFaOTerD36o0cZbe+p8g527lQNoOz9lWfiA1 QlMA== X-Gm-Message-State: AC+VfDw2USONpdYKaMI/PE8a4Soyjde7iVrKkzj0FHLdoAVKVAIuYZmw 1NT/VSvRL6nvni2Jdz6f0gAj2+ec05QCww== X-Received: from slicestar.c.googlers.com ([fda3:e722:ac3:cc00:4f:4b78:c0a8:20a1]) (user=davidgow job=sendgmr) by 2002:a81:c808:0:b0:54f:bb71:c7b3 with SMTP id n8-20020a81c808000000b0054fbb71c7b3mr589835ywi.9.1684399148266; Thu, 18 May 2023 01:39:08 -0700 (PDT) Date: Thu, 18 May 2023 16:38:46 +0800 In-Reply-To: <20230518083849.2631178-1-davidgow@google.com> Mime-Version: 1.0 References: <20230518083849.2631178-1-davidgow@google.com> X-Mailer: git-send-email 2.40.1.698.g37aff9b760-goog Message-ID: <20230518083849.2631178-4-davidgow@google.com> Subject: [PATCH v2 4/4] Documentation: kunit: Add usage notes for kunit_add_action() From: David Gow To: Matti Vaittinen , Daniel Latypov , Maxime Ripard , Benjamin Berg , Brendan Higgins , Stephen Boyd , Shuah Khan , Rae Moar Cc: David Gow , Greg Kroah-Hartman , "Rafael J . Wysocki" , Heikki Krogerus , Jonathan Cameron , Sadiya Kazi , Jonathan Corbet , linux-doc@vger.kernel.org, kunit-dev@googlegroups.com, linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org X-Spam-Status: No, score=-9.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE,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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1766221308619754992?= X-GMAIL-MSGID: =?utf-8?q?1766221308619754992?= Add some basic documentation for kunit_add_action() and related deferred action functions. Signed-off-by: David Gow Reviewed-by: Rae Moar --- This patch is new in v2. --- Documentation/dev-tools/kunit/usage.rst | 51 +++++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/Documentation/dev-tools/kunit/usage.rst b/Documentation/dev-tools/kunit/usage.rst index 46957d1cbcbb..c2f0ed648385 100644 --- a/Documentation/dev-tools/kunit/usage.rst +++ b/Documentation/dev-tools/kunit/usage.rst @@ -615,6 +615,57 @@ For example: KUNIT_ASSERT_STREQ(test, buffer, ""); } +Registering Cleanup Actions +--------------------------- + +If you need to perform some cleanup beyond simple use of ``kunit_kzalloc``, +you can register a cusom "deferred action", which is a cleanup function +run when the test exits (whether cleanly, or via a failed assertion). + +Actions are simple functions with no return value, and a single ``void*`` +context argument, and forfil the same role as "cleanup" functions in Python +and Go tests, "defer" statements in languages which support them, and +(in some cases) destructors in RAII languages. + +These are very useful for unregistering things from global lists, closing +files or other resources, or freeing resources. + +For example: + +.. code-block:: C + + static void cleanup_device(void *ctx) + { + struct device *dev = (struct device *)ctx; + + device_unregister(dev); + } + + void example_device_test(struct kunit *test) + { + struct my_device dev; + + device_register(&dev); + + kunit_add_action(test, &cleanup_device, &dev); + } + +Note that, for functions like device_unregister which only accept a single +pointer-sized argument, it's possible to directly cast that function to +a ``kunit_action_t`` rather than writing a wrapper function, for example: + +.. code-block:: C + + kunit_add_action(test, (kunit_action_t *)&device_unregister, &dev); + +``kunit_add_action`` can fail if, for example, the system is out of memory. +You can use ``kunit_add_action_or_reset`` instead which runs the action +immediately if it cannot be deferred. + +If you need more control over when the cleanup function is called, you +can trigger it early using ``kunit_release_action``, or cancel it entirely +with ``kunit_remove_action``. + Testing Static Functions ------------------------