From nobody Wed Dec 17 10:01:03 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 --- 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