From nobody Wed Dec 17 08:17:58 2025 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 BF142C7618E for ; Fri, 21 Apr 2023 08:42:52 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231476AbjDUImu (ORCPT ); Fri, 21 Apr 2023 04:42:50 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35474 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231387AbjDUImp (ORCPT ); Fri, 21 Apr 2023 04:42:45 -0400 Received: from mail-pl1-x64a.google.com (mail-pl1-x64a.google.com [IPv6:2607:f8b0:4864:20::64a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id EF49F7D81 for ; Fri, 21 Apr 2023 01:42:38 -0700 (PDT) Received: by mail-pl1-x64a.google.com with SMTP id d9443c01a7336-1a677ddd14fso21540625ad.2 for ; Fri, 21 Apr 2023 01:42:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1682066558; x=1684658558; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=JSu0ZR453Rbs9YQWMYaLMaSYooduUs1pAmSEpyOKMcM=; b=C2jKpxn91b/a7wxP4k66C1ZNh5FHjgBnX8MRRKc/d/zoq/Z6Ax2iZvQK/7Yb7kfj5e JwHt8yNLl7sxHyi/8IteTfPf8ry40U8uFz/yubuVtbXVD0+fhzpEX3oqrUviAYbaMM9h AgVlkpu0GPyrA9bazff0xsb6uV/Bwgq/RBAc6I8fZlSZXvqVS/+sCQeU/Xt/+sUMrzc0 Ysr2SCeAa0Ie+yKf9YelXdbYprDHV/wFvOD2U2K1GOJyxlqF5q5m94eV+vlvj+Ckssv8 0oPZuwiutUlaplqhglhMTIKACUkYU7brgCzcCU5IKLs/NTov+VTMDTe/nm9uv7+p4DQe Jn2Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1682066558; x=1684658558; 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=JSu0ZR453Rbs9YQWMYaLMaSYooduUs1pAmSEpyOKMcM=; b=krSS77T18AicutgM//P0EPfB78G/soUsburny3PrXFi1/ng8r8mo+QrG+KnMCQHexs rjUS82hmDMuI5fs45R79avMarhzmOsmDNYPVOoiF1vhLNrg4ss1Bxr4AwrlqLH9Wg2a8 eti2koblf0PPjAMiRkZLQl/THEDb6FviusiaNVfeMO4hUthMTuyQ2xwyD3UgbI+FoAnG KMpKYS7GY9DD7uwnm4WsbomzcCFU6zfdPN3GHHQJKPsRRyAndsuc+tl3D4n9JV0q8v9W Oou++y+2GjU/D2uJqsKLfbW2OIYLiNgSak00AXWxitDMyb7CwbZD2+nDzBd/loPOQjtM UmLA== X-Gm-Message-State: AAQBX9fXufEpL4N5ZMGHiDs1yCgYtkCUB+qTZmgwC1a73UTUSUPjwjkb /ZNBUCKIyw3Ex0TYj0q0oYDZdvpa/Xmtvg== X-Google-Smtp-Source: AKy350bxsVJ6UPXu4OLLqgZ+tzMIjr75bj9Qs7vDvkDDyZt+3FlV4zqo4HJEJBtn0KnqSuuMlnPsCb80v9jVxw== X-Received: from slicestar.c.googlers.com ([fda3:e722:ac3:cc00:4f:4b78:c0a8:20a1]) (user=davidgow job=sendgmr) by 2002:a17:902:708c:b0:1a5:8a2:5062 with SMTP id z12-20020a170902708c00b001a508a25062mr1569902plk.12.1682066558467; Fri, 21 Apr 2023 01:42:38 -0700 (PDT) Date: Fri, 21 Apr 2023 16:42:24 +0800 In-Reply-To: <20230421084226.2278282-1-davidgow@google.com> Mime-Version: 1.0 References: <20230421084226.2278282-1-davidgow@google.com> X-Mailer: git-send-email 2.40.0.634.g4ca3ef3211-goog Message-ID: <20230421084226.2278282-2-davidgow@google.com> Subject: [PATCH v1 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 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_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 Reviewed-By: Benjamin Berg Reviewed-by: Maxime Ripard Tested-by: Maxime Ripard --- Note: there is a checkpatch warning: "function definition argument 'void *' should also have an identifier name" I think this is a false-positive, as we're defining a function pointer struct member, not a specific function. Changes since RFC v2: https://lore.kernel.org/linux-kselftest/20230331080411.981038-2-davidgow@go= ogle.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@g= oogle.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 | 76 ++++++++++++++++++++++++++++++++ lib/kunit/kunit-test.c | 88 ++++++++++++++++++++++++++++++++++++- lib/kunit/resource.c | 95 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 258 insertions(+), 1 deletion(-) diff --git a/include/kunit/resource.h b/include/kunit/resource.h index c0d88b318e90..6db28cd43e9b 100644 --- a/include/kunit/resource.h +++ b/include/kunit/resource.h @@ -387,4 +387,80 @@ static inline int kunit_destroy_named_resource(struct = kunit *test, */ void kunit_remove_resource(struct kunit *test, struct kunit_resource *res); =20 + +/** + * kunit_add_action() - Defer an 'action' (function call) until the test e= nds. + * @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 t= o that + * they were registered in. + * + * This is useful for cleaning up allocated memory and resources. + * + * Returns: + * 0 on success, an error if the action could not be deferred. + */ +int kunit_add_action(struct kunit *test, void (*action) (void *), void *ct= x); + +/** + * kunit_add_action_or_reset() - Defer an 'action' (function call) until t= he 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 t= o that + * they were registered in. + * + * This is useful for cleaning up allocated memory and resources. + * + * If the action cannot be created (e.g., due to the system being out of m= emory), + * then action(ctx) will be called immediately, and an error will be retur= ned. + * + * Returns: + * 0 on success, an error if the action could not be deferred. + */ +int kunit_add_action_or_reset(struct kunit *test, void (*action)(void *), + 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 t= he + * test terminates.. + * If the function/context pair was deferred multiple times, only the most + * recent one will be cancelled. + */ +void kunit_remove_action(struct kunit *test, + void (*action)(void *), + 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); + */ +void kunit_release_action(struct kunit *test, + void (*action)(void *), + 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]; }; =20 static int fake_resource_init(struct kunit_resource *res, void *context) @@ -403,6 +403,88 @@ static void kunit_resource_test_named(struct kunit *te= st) KUNIT_EXPECT_TRUE(test, list_empty(&test->resources)); } =20 +static void increment_int(void *ctx) +{ + int *i =3D (int *)ctx; + (*i)++; +} + +static void kunit_resource_test_action(struct kunit *test) +{ + int num_actions =3D 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 =3D 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 =3D 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 =3D (struct kunit_test_resour= ce_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 =3D (struct kunit_test_resour= ce_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 =3D 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 =3D @@ -434,6 +516,10 @@ static struct kunit_case kunit_resource_test_cases[] = =3D { 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), {} }; =20 diff --git a/lib/kunit/resource.c b/lib/kunit/resource.c index c414df922f34..20b08b9341c9 100644 --- a/lib/kunit/resource.c +++ b/lib/kunit/resource.c @@ -77,3 +77,98 @@ int kunit_destroy_resource(struct kunit *test, kunit_res= ource_match_t match, return 0; } EXPORT_SYMBOL_GPL(kunit_destroy_resource); + +struct kunit_action_ctx { + struct kunit_resource res; + void (*func)(void *); + void *ctx; +}; + +static void __kunit_action_free(struct kunit_resource *res) +{ + struct kunit_action_ctx *action_ctx =3D container_of(res, struct kunit_ac= tion_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 =3D kzalloc(sizeof(*action_ctx), GFP_KERNEL); + if (!action_ctx) + return -ENOMEM; + + action_ctx->func =3D action; + action_ctx->ctx =3D ctx; + + action_ctx->res.should_kfree =3D true; + /* As init is NULL, this cannot fail. */ + __kunit_add_resource(test, NULL, __kunit_action_free, &action_ctx->res, a= ction_ctx); + + return 0; +} + +int kunit_add_action_or_reset(struct kunit *test, void (*action)(void *), + void *ctx) +{ + int res =3D kunit_add_action(test, action, ctx); + + if (res) + action(ctx); + return res; +} + +static bool __kunit_action_match(struct kunit *test, + struct kunit_resource *res, void *match_data) +{ + struct kunit_action_ctx *match_ctx =3D (struct kunit_action_ctx *)match_d= ata; + struct kunit_action_ctx *res_ctx =3D container_of(res, struct kunit_actio= n_ctx, res); + + /* Make sure this is a free function. */ + if (res->free !=3D __kunit_action_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_remove_action(struct kunit *test, + void (*action)(void *), + void *ctx) +{ + struct kunit_action_ctx match_ctx; + struct kunit_resource *res; + + match_ctx.func =3D action; + match_ctx.ctx =3D ctx; + + res =3D kunit_find_resource(test, __kunit_action_match, &match_ctx); + if (res) { + /* Remove the free function so we don't run the action. */ + res->free =3D NULL; + kunit_remove_resource(test, res); + kunit_put_resource(res); + } +} + +void kunit_release_action(struct kunit *test, + void (*action)(void *), + void *ctx) +{ + struct kunit_action_ctx match_ctx; + struct kunit_resource *res; + + match_ctx.func =3D action; + match_ctx.ctx =3D ctx; + + res =3D 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); + } +} --=20 2.40.0.634.g4ca3ef3211-goog From nobody Wed Dec 17 08:17:58 2025 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 48521C7618E for ; Fri, 21 Apr 2023 08:42:59 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231668AbjDUIm4 (ORCPT ); Fri, 21 Apr 2023 04:42:56 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35342 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231355AbjDUIms (ORCPT ); Fri, 21 Apr 2023 04:42:48 -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 9B164A270 for ; Fri, 21 Apr 2023 01:42:43 -0700 (PDT) Received: by mail-yb1-xb49.google.com with SMTP id 3f1490d57ef6-b9229bffbebso2361366276.0 for ; Fri, 21 Apr 2023 01:42:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1682066563; x=1684658563; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=dnbaIJdjbrKfpq/DYVToIRMnwb1KONDpp3f8LVUSy+A=; b=lgawZwEj0TwcdS424+bM40JeQgZ+tAwxlFUx8IelpcYkDLbYBq7BuYLq0Dxyv4sans /931EgXYmrHtGigiDLJ1OD8lz0bxlBKECTwMLMRgiFnFYpypM2XQj+SgnOvY4gRjTTr5 zUC/Q3CamfI68bVthL/chGI6cByC6tUFhtSnND4St1fIAGudzrUWGGoMPmxlKkODCjhx EMMvwNaj50jxHEFGt/CeLFevsKmuvBBZ4Lck0SW8hk4AJ8c6fztJtQf1QJbMc6Wzv53v js9o2QguwKLHQI75j6MRYnTs4yO7eOk+KhXTq4+27ilkYF9emBmloDx/9vwkowp7mxEC bbew== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1682066563; x=1684658563; 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=dnbaIJdjbrKfpq/DYVToIRMnwb1KONDpp3f8LVUSy+A=; b=d7jl1u5L8nc+hw6DQtrJ4/M0KLqjQRfuW0IBrTnlePuYUiOZ7wDlLVnv3dcOzV/DKG igWuwMcAGxtuPYDnfX3UvOQpa4C+hyrl0S1qlOdsKNQO8w4iRP9oQBtABz4djxoIjjOv ms7RIA82O323p214YEOq4rZnggT6q7teIQ0ZXEr9AB1uK/hhOTMQezuT7dduGiJ2vaha UKWsahDUCtDkuytBQXFa3qAP0Q/COMOSjGeek0cThySfgFr9+m506QYJTqIkT9rnjM3U DwIIiC3JTNv2gVomIQquW9yJAzyG9gsZe+htOwT4+QvTG/CxALBIsg8guSYWLer6C2uN WuDA== X-Gm-Message-State: AAQBX9fhb25rpwovxPEEiw8UFxroAJXOj70yjRR+bdGEal+68kwb/DTe YPJYjrT8L/Y6l55az5MhTYecKrtjGZjHPw== X-Google-Smtp-Source: AKy350YRd7UKXI3kpVQ0fG/tt/lipYvObK5SJYi7IazuopvKnPM0WnvoZmL9CeaQB4VttY1QjJ/HDPxQcogJLg== X-Received: from slicestar.c.googlers.com ([fda3:e722:ac3:cc00:4f:4b78:c0a8:20a1]) (user=davidgow job=sendgmr) by 2002:a25:3187:0:b0:b8f:59ed:e9d9 with SMTP id x129-20020a253187000000b00b8f59ede9d9mr893656ybx.9.1682066562837; Fri, 21 Apr 2023 01:42:42 -0700 (PDT) Date: Fri, 21 Apr 2023 16:42:25 +0800 In-Reply-To: <20230421084226.2278282-1-davidgow@google.com> Mime-Version: 1.0 References: <20230421084226.2278282-1-davidgow@google.com> X-Mailer: git-send-email 2.40.0.634.g4ca3ef3211-goog Message-ID: <20230421084226.2278282-3-davidgow@google.com> Subject: [PATCH v1 2/3] kunit: executor_test: Use kunit_add_action() 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 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" 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. Signed-off-by: David Gow Reviewed-By: Benjamin Berg Reviewed-by: Maxime Ripard Tested-by: Maxime Ripard --- Changes since RFCv2: https://lore.kernel.org/linux-kselftest/20230331080411.981038-3-davidgow@go= ogle.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..fd3ba4d74a66 100644 --- a/lib/kunit/executor_test.c +++ b/lib/kunit/executor_test.c @@ -125,11 +125,6 @@ kunit_test_suites(&executor_test_suite); =20 /* Test helpers */ =20 -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 dat= a. */ @@ -138,8 +133,10 @@ static void kfree_at_end(struct kunit *test, const voi= d *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, + (void (*)(void *))kfree, + (void *)to_free); } =20 static struct kunit_suite *alloc_fake_suite(struct kunit *test, --=20 2.40.0.634.g4ca3ef3211-goog From nobody Wed Dec 17 08:17:58 2025 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 B4CECC77B75 for ; Fri, 21 Apr 2023 08:43:11 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231656AbjDUInJ (ORCPT ); Fri, 21 Apr 2023 04:43:09 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35388 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231348AbjDUImy (ORCPT ); Fri, 21 Apr 2023 04:42:54 -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 B783D9011 for ; Fri, 21 Apr 2023 01:42:48 -0700 (PDT) Received: by mail-yb1-xb49.google.com with SMTP id 3f1490d57ef6-b8f324b3ef8so839684276.0 for ; Fri, 21 Apr 2023 01:42:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1682066568; x=1684658568; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=MEEb4C1MXHRprmR5HHPu3CotKdbDXVaTHLjb5P98lHM=; b=yzRovhVJ66P12HUPvsEjD48dab5ICCTEQbiTUmWPwkPZveJ7/5bYyKdfQWuNxasm0k 974M6Y84tvzdkeXl91gtaNdMevz2gswJETxqftQm74ua7wsJF4El/xUmm5r/VP1mjeN4 X0kNDkyQGm9iPFrg7TicqN/Bel5ywQDtqbGEJJeSGwAH1/zuvNIu4YuP/WBYZ2e1Yeja jDwUJ7IZVhRQqVG5Zg5GsJMUbN8h/UjqNqZayme/AjtggXUXAgTm4N1TGyPN46KLXjip 8LqeO+2Fi9yZB7KydrGRZnbfLA4cChWN1KKeRyl2doM0e4uJjGh75iixJ/kOsYGR5EQR Jpkg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1682066568; x=1684658568; 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=MEEb4C1MXHRprmR5HHPu3CotKdbDXVaTHLjb5P98lHM=; b=DzZk/pxDHUVhgDL1Vt9Zi9dafAE1l24cZcTzKVv8qCxeYLgLwiWgcX7QIN4Gwi2Dk6 dHN2ER6YcaOQGQS226Buos+c7ip8o3vFAEwQbyzLJdQnAjZrTKOwpJ+RppRaNU9ZYQbd cZl7ml6fpRHvRQvo42IZEWxnjEEOoblzbdqhKuy4m6GK56EGgvRD8Gmi6MlJJ0RWDbYR FVm8UNokc7SXl83eBjCCMTIwYGZT5aBXYVQQw2tx+VsjFwejhiSJZYIi6K6EeOKr0w6y L1hnOwrQoUbFkFsF9MP+ckwTseodtPUUixJdHbKGqixoQAxdkRqD3T7hcgBMSc44ZAlC uwOQ== X-Gm-Message-State: AAQBX9dpNVDDksDbGT1N/V/0+mj+uXNYBIvbd03j5kej6ZRQ6MWT8Chq XxJULV6CK4QEw7GT/XQM1OtsilD7M/r7Uw== X-Google-Smtp-Source: AKy350aXCY521YxQpLSky5f+7Krq4YeBVK66lmw9NALxj4KPTruWIrf9Mv+7P5X5dRe2C7exCPaiWGO5pAZFQQ== X-Received: from slicestar.c.googlers.com ([fda3:e722:ac3:cc00:4f:4b78:c0a8:20a1]) (user=davidgow job=sendgmr) by 2002:a25:6e46:0:b0:b8f:610b:e6b8 with SMTP id j67-20020a256e46000000b00b8f610be6b8mr1259447ybc.9.1682066567913; Fri, 21 Apr 2023 01:42:47 -0700 (PDT) Date: Fri, 21 Apr 2023 16:42:26 +0800 In-Reply-To: <20230421084226.2278282-1-davidgow@google.com> Mime-Version: 1.0 References: <20230421084226.2278282-1-davidgow@google.com> X-Mailer: git-send-email 2.40.0.634.g4ca3ef3211-goog Message-ID: <20230421084226.2278282-4-davidgow@google.com> Subject: [PATCH v1 3/3] kunit: kmalloc_array: Use kunit_add_action() 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 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" 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_release_action() to implement kunit_kfree(). Signed-off-by: David Gow Reviewed-By: Benjamin Berg Reviewed-by: Maxime Ripard Tested-by: Maxime Ripard --- Changes since RFCv2: https://lore.kernel.org/linux-kselftest/20230331080411.981038-4-davidgow@go= ogle.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 57b309c6ca27..3e8e98d0d8b1 100644 --- a/include/kunit/test.h +++ b/include/kunit/test.h @@ -321,8 +321,11 @@ enum kunit_status kunit_suite_has_succeeded(struct kun= it_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 &str= uct - * kunit_resource for more information. + * and is automatically cleaned up after the test case concludes. See kuni= t_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); =20 @@ -333,6 +336,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 g= fp) { diff --git a/lib/kunit/test.c b/lib/kunit/test.c index e2910b261112..6aafe2138766 100644 --- a/lib/kunit/test.c +++ b/lib/kunit/test.c @@ -712,58 +712,28 @@ static struct notifier_block kunit_mod_nb =3D { }; #endif =20 -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 *cont= ext) +void *kunit_kmalloc_array(struct kunit *test, size_t n, size_t size, gfp_t= gfp) { - struct kunit_kmalloc_array_params *params =3D context; + void *data; =20 - res->data =3D kmalloc_array(params->n, params->size, params->gfp); - if (!res->data) - return -ENOMEM; + data =3D kmalloc_array(n, size, gfp); =20 - return 0; -} + if (!data) + return NULL; =20 -static void kunit_kmalloc_array_free(struct kunit_resource *res) -{ - kfree(res->data); -} + if (kunit_add_action_or_reset(test, (void (*)(void *))kfree, data) !=3D 0) + return NULL; =20 -void *kunit_kmalloc_array(struct kunit *test, size_t n, size_t size, gfp_t= gfp) -{ - struct kunit_kmalloc_array_params params =3D { - .size =3D size, - .n =3D n, - .gfp =3D gfp - }; - - return kunit_alloc_resource(test, - kunit_kmalloc_array_init, - kunit_kmalloc_array_free, - gfp, - ¶ms); + return data; } EXPORT_SYMBOL_GPL(kunit_kmalloc_array); =20 -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 =3D=3D kunit_kmalloc_array_free && res->data =3D=3D matc= h_data; -} - void kunit_kfree(struct kunit *test, const void *ptr) { if (!ptr) return; =20 - if (kunit_destroy_resource(test, kunit_kfree_match, (void *)ptr)) - KUNIT_FAIL(test, "kunit_kfree: %px already freed or not allocated by kun= it", ptr); + kunit_release_action(test, (void (*)(void *))kfree, (void *)ptr); } EXPORT_SYMBOL_GPL(kunit_kfree); =20 --=20 2.40.0.634.g4ca3ef3211-goog