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 7600EC76196 for ; Fri, 31 Mar 2023 08:04:55 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231439AbjCaIEy (ORCPT ); Fri, 31 Mar 2023 04:04:54 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45950 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231415AbjCaIEk (ORCPT ); Fri, 31 Mar 2023 04:04:40 -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 EF72D1B7C5 for ; Fri, 31 Mar 2023 01:04:37 -0700 (PDT) Received: by mail-yb1-xb49.google.com with SMTP id p12-20020a25420c000000b00b6eb3c67574so21168278yba.11 for ; Fri, 31 Mar 2023 01:04:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; t=1680249877; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=jpGavDy61WmZmhN58Mtaqc/5O+sP6wYORL/bT/HmA3s=; b=Jk+cj+bjqrt3IqsApBiA7kyT1hEOS6On/c1fpo7tIRjMDJngA1tR7nXLnotUfnUWq/ T9mKz27VMe9Rizr9kVZo/rgfAnwx8yAn7rasVFdwe9sWKMZ26kzZs5axvhoUdkRi2Yze cB+y4k0u/GmhVe91VPk2kN8oFvfZyKPH0pAHBavpOeiyQh1Qc4NRtA3iubKYqUe5Q/fR btr2lK1oD6j6/Dr+MTbru55OEucEtyU4//WfluYDnS7obc2ttuduLNi/fKqOBm/6krll QCwKUr1vRTCpTcCgk/TuBYLgNJ/rvKpph3zItKFgg4yLOotBEr3UOX/F57G3NjJpirvD Eljg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680249877; 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=jpGavDy61WmZmhN58Mtaqc/5O+sP6wYORL/bT/HmA3s=; b=Z7PdENGUBxoLO3u+rovZfAl1h5IefTDp/BUEwq/b/nF4LLdIJgH+ItIkG8IIn7/DQC NbwUDAIb1mmwUstFlguMAkREVg2b15mRZY3bFOvsPPYKUOnscZlY5u/Cln7bSW+i2M4H /pSzJXiiPruy1YmhqFcS0dlZ5MFeKgsYL2clVyPjD5sdBVEXEeUNLnFTPxeiXx6l54hS MxLSlJqmyhbn7yg0nd2c/kIgllIAh/CxkUXpx0jjZ94w3s8j+ugpMQGgnv20OHefUy2q KsU2hYR+fYhXoEYbMoNaLPzE076W9vdZnxpM1KBwY2FL9S1no9Abz9aHjU6D+q+XlZKE N/iQ== X-Gm-Message-State: AAQBX9dLTm6oon8+UfinMRnhoyZ2/plUvbAS/pQ6qPvjUMWoloh9DZ67 ZYadICIFGGiyZai/pfzAyKxrWyM9OC9Bzw== X-Google-Smtp-Source: AKy350YeFkXOzIOL84FaInn6T78DR5eXXkOwBFoQTOFE1jQOAGN+Kkru2oR7XOnak4OZulhM4Z2Ugi4l/W5cIw== X-Received: from slicestar.c.googlers.com ([fda3:e722:ac3:cc00:4f:4b78:c0a8:20a1]) (user=davidgow job=sendgmr) by 2002:a05:6902:72e:b0:b6c:f26c:e5ab with SMTP id l14-20020a056902072e00b00b6cf26ce5abmr18114164ybt.3.1680249877289; Fri, 31 Mar 2023 01:04:37 -0700 (PDT) Date: Fri, 31 Mar 2023 16:04:11 +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-4-davidgow@google.com> Subject: [RFC PATCH v2 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_defer_trigger_all() to implement kunit_kfree(). Signed-off-by: David Gow --- lib/kunit/test.c | 48 ++++++++---------------------------------------- 1 file changed, 8 insertions(+), 40 deletions(-) diff --git a/lib/kunit/test.c b/lib/kunit/test.c index e2910b261112..ec45c8863f04 100644 --- a/lib/kunit/test.c +++ b/lib/kunit/test.c @@ -712,58 +712,26 @@ 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; - - res->data =3D kmalloc_array(params->n, params->size, params->gfp); - if (!res->data) - return -ENOMEM; - - return 0; -} + void *data; =20 -static void kunit_kmalloc_array_free(struct kunit_resource *res) -{ - kfree(res->data); -} + data =3D kmalloc_array(n, size, gfp); =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 - }; + if (!data) + return NULL; =20 - return kunit_alloc_resource(test, - kunit_kmalloc_array_init, - kunit_kmalloc_array_free, - gfp, - ¶ms); + kunit_add_action(test, (kunit_defer_function_t)kfree, data, gfp); + 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, (kunit_defer_function_t)kfree, (void *)ptr); } EXPORT_SYMBOL_GPL(kunit_kfree); =20 --=20 2.40.0.348.gf938b09366-goog