Received: by 2002:ac8:156:0:b0:3e0:cd10:60c8 with SMTP id f22csp274381qtg; Fri, 31 Mar 2023 01:10:24 -0700 (PDT) X-Google-Smtp-Source: AKy350ailHGf2QLcobeQzH213S1M/iXKwzJSU1Wrft6imCXYOdMM4wko+EztWAXWiF9vj1a1BpNS X-Received: by 2002:a17:906:297:b0:932:170:e07b with SMTP id 23-20020a170906029700b009320170e07bmr3802337ejf.7.1680250224386; Fri, 31 Mar 2023 01:10:24 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1680250224; cv=none; d=google.com; s=arc-20160816; b=wvfxt0BXTkyAz892WYQSgtV3HnZe1tZCD0TPnVuU83cAPPXvF1qLiKR/pU0LmnOEDu 8B72dyZc9wfCzKYVXixm9I6F0QHYTKO3roHndZg/VInr3NYVpIfgCfL/U1pOfBUlVpgW m8NvGQP0OmT/0/HNlCEOi/LBCVESPT4YzMp91OSMUurEM3J3TT7NeACCe1BsuL1V8AR/ /U+YrKZQSIqVMSchBewYFIaPpEfzm0350Nfp8CsTFIx1hZ15yLeGRBYNzqSQuO0tp/hp IlTa3orGcwt/GKSj9x65898ysB7KVi9SKzaqSd6lnyXoazG/8WKtvRJ80aSSE3dYMSHe VMTg== 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=jpGavDy61WmZmhN58Mtaqc/5O+sP6wYORL/bT/HmA3s=; b=O3EqUp6TtJZE80xX+Wzpl3jxGnaDPMVDeNDPtZL+GpoGInrERlKsbv3Zvlca4yQhgj 2nkf05QJH02WuZA7VoX7KT7MYPGfIffXzO/fGH90LNg/LhHV6j2LtljY6YdFLxLiZ/S/ vphdknZRYy/bb9NemBNuhZAIMnvn+1mXmW+s+uag2J5lWMwEsrxgLsgOrrNCQj/BDquf rqX1JZC/YvbRu3zwkszlid4w5HvArGQNefWTRnfnRXm49JnD2mjIuJ5vEAAY+wC98GV0 RXDkkL+pKHHNyZE88hAue2K1ea2m5sbDWjWnEm2Y3JtflJlY09wMTlfjzmbuUoyp7D87 Q4dw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=Jk+cj+bj; 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 d10-20020a1709064c4a00b00946fff191cbsi1492042ejw.778.2023.03.31.01.09.59; Fri, 31 Mar 2023 01:10:24 -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=Jk+cj+bj; 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 S230332AbjCaIEx (ORCPT + 99 others); Fri, 31 Mar 2023 04:04:53 -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-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 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 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 = { }; #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; - - res->data = kmalloc_array(params->n, params->size, params->gfp); - if (!res->data) - return -ENOMEM; - - return 0; -} + void *data; -static void kunit_kmalloc_array_free(struct kunit_resource *res) -{ - kfree(res->data); -} + data = kmalloc_array(n, size, gfp); -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 - }; + if (!data) + return NULL; - 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); -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_defer_function_t)kfree, (void *)ptr); } EXPORT_SYMBOL_GPL(kunit_kfree); -- 2.40.0.348.gf938b09366-goog