From patchwork Wed Mar 29 17:23:09 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Benjamin Berg X-Patchwork-Id: 13193057 X-Patchwork-Delegate: brendanhiggins@google.com 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 27F7BC6FD18 for ; Wed, 29 Mar 2023 17:23:30 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230179AbjC2RX3 (ORCPT ); Wed, 29 Mar 2023 13:23:29 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49662 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229456AbjC2RX2 (ORCPT ); Wed, 29 Mar 2023 13:23:28 -0400 Received: from sipsolutions.net (s3.sipsolutions.net [IPv6:2a01:4f8:191:4433::2]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6EF88422C for ; Wed, 29 Mar 2023 10:23:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=sipsolutions.net; s=mail; h=Content-Transfer-Encoding:MIME-Version: Message-Id:Date:Subject:Cc:To:From:Content-Type:Sender:Reply-To:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-To:Resent-Cc: Resent-Message-ID:In-Reply-To:References; bh=qCUo0G46cYQXreTtfAzXKfSeX+oSqchDN4KPUhh50yI=; t=1680110607; x=1681320207; b=seRoA4omQ/rpHaxdDJzhcVjYFPWhYkjBbFxdCpAcCQ7YJaL3hcTzOpa1iHhgZnxB+/sZkTOo0Bc wvJClGTb4Ayu35Id8wV7CcKmqLIgDW/tbtU1ldQO9fdPuUTKRAd4V0ZZzp4amztG2ALVpsWOPzGUB riir3k1RhdShYp8eEc6NyiCs7vkhSznAt12WpH7l3z6SRZeOZwUMF5SZVKzpo4OMZK71qw/wjJzFA 9HAp9aPHCuUEQ56cSfBokiwVzFezug4ercZsmMrw0YY+nJ+MQuqb9Ke5B/fqljYq6HbKdYwbzFv8X 1H0IJMWVuReiXn+uXxh4Q6Obc94S9KgELqBQ==; Received: by sipsolutions.net with esmtpsa (TLS1.3:ECDHE_X25519__RSA_PSS_RSAE_SHA256__AES_256_GCM:256) (Exim 4.96) (envelope-from ) id 1phZW8-000AZQ-10; Wed, 29 Mar 2023 19:23:24 +0200 From: benjamin@sipsolutions.net To: linux-kselftest@vger.kernel.org, kunit-dev@googlegroups.com Cc: Johannes Berg , Benjamin Berg Subject: [PATCH 1/3] kunit: add parameter generation macro using description from array Date: Wed, 29 Mar 2023 19:23:09 +0200 Message-Id: <20230329172311.71861-1-benjamin@sipsolutions.net> X-Mailer: git-send-email 2.39.2 MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org From: Benjamin Berg The existing KUNIT_ARRAY_PARAM macro requires a separate function to get the description. However, in a lot of cases the description can just be copied directly from the array. Add a second macro that avoids having to write a static function just for a single strscpy. Signed-off-by: Benjamin Berg --- include/kunit/test.h | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/include/kunit/test.h b/include/kunit/test.h index 08d3559dd703..519b90261c72 100644 --- a/include/kunit/test.h +++ b/include/kunit/test.h @@ -1414,6 +1414,25 @@ do { \ return NULL; \ } +/** + * KUNIT_ARRAY_PARAM_DESC() - Define test parameter generator from an array. + * @name: prefix for the test parameter generator function. + * @array: array of test parameters. + * @desc_member: structure member from array element to use as description + * + * Define function @name_gen_params which uses @array to generate parameters. + */ +#define KUNIT_ARRAY_PARAM_DESC(name, array, desc_member) \ + static const void *name##_gen_params(const void *prev, char *desc) \ + { \ + typeof((array)[0]) *__next = prev ? ((typeof(__next)) prev) + 1 : (array); \ + if (__next - (array) < ARRAY_SIZE((array))) { \ + strscpy(desc, __next->desc_member, KUNIT_PARAM_DESC_SIZE); \ + return __next; \ + } \ + return NULL; \ + } + // TODO(dlatypov@google.com): consider eventually migrating users to explicitly // include resource.h themselves if they need it. #include From patchwork Wed Mar 29 17:23:10 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Benjamin Berg X-Patchwork-Id: 13193059 X-Patchwork-Delegate: brendanhiggins@google.com 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 16E32C761AF for ; Wed, 29 Mar 2023 17:23:31 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230051AbjC2RXa (ORCPT ); Wed, 29 Mar 2023 13:23:30 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49670 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230103AbjC2RX3 (ORCPT ); Wed, 29 Mar 2023 13:23:29 -0400 Received: from sipsolutions.net (s3.sipsolutions.net [IPv6:2a01:4f8:191:4433::2]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BD8D53C05 for ; Wed, 29 Mar 2023 10:23:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=sipsolutions.net; s=mail; h=Content-Transfer-Encoding:MIME-Version: References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From:Content-Type:Sender :Reply-To:Content-ID:Content-Description:Resent-Date:Resent-From:Resent-To: Resent-Cc:Resent-Message-ID; bh=8SRscMN5b9HTgCA/7xjbHkj7Ve/A0/I00v8SnxaFrfs=; t=1680110607; x=1681320207; b=vdR4QNrXfElKgCRhyi92tiGObndNpZfg0QhFyoytyF/IonH K+L0fFs/rYNFaCvl1TmPOCc1pShAHPYWWgP1oU2ezQCHJ+QRWAgfkVe24eyNVBMPY8+nGojhD1Vli 0VOPGB+n5Hz1eeZj5FY3FdTKDbUfpx7dvRWBUbMY0w/WzhBnccgGjWT6SKnV/enKQMzEgfXT+z7Vj G21+XPE8wNZ0sqSLNsEfnPTOj0lUxNIIQmAmNcMGY5r+FKXOH3PDKimF6dh2i1TJzbtfedyOYA6xE h8E5HDeTewJ0VvNOxuZ1TTTeezP4l1S2AM+BDzSyjACqVeI9dq8IfAKSwixBzDhQ==; Received: by sipsolutions.net with esmtpsa (TLS1.3:ECDHE_X25519__RSA_PSS_RSAE_SHA256__AES_256_GCM:256) (Exim 4.96) (envelope-from ) id 1phZW8-000AZQ-1g; Wed, 29 Mar 2023 19:23:24 +0200 From: benjamin@sipsolutions.net To: linux-kselftest@vger.kernel.org, kunit-dev@googlegroups.com Cc: Johannes Berg , Benjamin Berg Subject: [PATCH 2/3] kunit: add ability to register a simple cleanup function Date: Wed, 29 Mar 2023 19:23:10 +0200 Message-Id: <20230329172311.71861-2-benjamin@sipsolutions.net> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230329172311.71861-1-benjamin@sipsolutions.net> References: <20230329172311.71861-1-benjamin@sipsolutions.net> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org From: Benjamin Berg This is useful to e.g. automatically free resources at the end of the test, without having to deal with kunit resource objects directly. The whole point of doing this is that the callback is guaranteed to happen in case the test aborts (due to an assertion). As such, use assertions internally rather than requiring further error checking by the API user. Signed-off-by: Benjamin Berg --- include/kunit/test.h | 20 ++++++++++++++++++++ lib/kunit/test.c | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+) diff --git a/include/kunit/test.h b/include/kunit/test.h index 519b90261c72..ab1dacf1c9f4 100644 --- a/include/kunit/test.h +++ b/include/kunit/test.h @@ -373,6 +373,26 @@ static inline void *kunit_kcalloc(struct kunit *test, size_t n, size_t size, gfp return kunit_kmalloc_array(test, n, size, gfp | __GFP_ZERO); } +typedef void (*kunit_cleanup_t)(const void *); + +/** + * kunit_add_cleanup() - Add post-test cleanup action. + * @test: The test case to which the resource belongs. + * @cleanup_func: function to call at end of test. + * @data: data to pass to @free_func. + * @internal_gfp: gfp to use for internal allocations, if unsure, use GFP_KERNEL + * + * This adds a cleanup action to be executed after the test completes. + * Internally this is handled using a *test managed resource*. + * + * This function will abort the test on failure. + * + * Note: KUnit needs to allocate memory for a kunit_resource object. You must + * specify an @internal_gfp that is compatible with the current context. + */ +void kunit_add_cleanup(struct kunit *test, kunit_cleanup_t cleanup_func, + const void *data, gfp_t internal_gfp); + void kunit_cleanup(struct kunit *test); void __printf(2, 3) kunit_log_append(char *log, const char *fmt, ...); diff --git a/lib/kunit/test.c b/lib/kunit/test.c index c9e15bb60058..72d956dfc324 100644 --- a/lib/kunit/test.c +++ b/lib/kunit/test.c @@ -719,6 +719,43 @@ void *kunit_kmalloc_array(struct kunit *test, size_t n, size_t size, gfp_t gfp) } EXPORT_SYMBOL_GPL(kunit_kmalloc_array); +struct kunit_auto_cleanup { + struct kunit_resource resource; + kunit_cleanup_t cleanup_func; +}; + +static void kunit_auto_cleanup_free(struct kunit_resource *res) +{ + struct kunit_auto_cleanup *cleanup; + + cleanup = container_of(res, struct kunit_auto_cleanup, resource); + + cleanup->cleanup_func(cleanup->resource.data); +} + +void kunit_add_cleanup(struct kunit *test, kunit_cleanup_t cleanup_func, + const void *data, gfp_t internal_gfp) +{ + struct kunit_auto_cleanup *res; + + KUNIT_ASSERT_NOT_NULL_MSG(test, cleanup_func, + "Cleanup function must not be NULL"); + + res = kzalloc(sizeof(*res), internal_gfp); + if (!res) { + cleanup_func(data); + KUNIT_ASSERT_FAILURE(test, "Could not allocate resource for cleanup"); + } + + res->cleanup_func = cleanup_func; + res->resource.should_kfree = true; + + /* Cannot fail as init is NULL */ + __kunit_add_resource(test, NULL, kunit_auto_cleanup_free, + &res->resource, (void *)data); +} +EXPORT_SYMBOL_GPL(kunit_add_cleanup); + static inline bool kunit_kfree_match(struct kunit *test, struct kunit_resource *res, void *match_data) { From patchwork Wed Mar 29 17:23:11 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Benjamin Berg X-Patchwork-Id: 13193058 X-Patchwork-Delegate: brendanhiggins@google.com 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 976BFC74A5B for ; Wed, 29 Mar 2023 17:23:30 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229456AbjC2RXa (ORCPT ); Wed, 29 Mar 2023 13:23:30 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49666 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230051AbjC2RX2 (ORCPT ); Wed, 29 Mar 2023 13:23:28 -0400 Received: from sipsolutions.net (s3.sipsolutions.net [IPv6:2a01:4f8:191:4433::2]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B78AB10D3 for ; Wed, 29 Mar 2023 10:23:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=sipsolutions.net; s=mail; h=Content-Transfer-Encoding:MIME-Version: References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From:Content-Type:Sender :Reply-To:Content-ID:Content-Description:Resent-Date:Resent-From:Resent-To: Resent-Cc:Resent-Message-ID; bh=fWWMWsWDiMkkqHyM1AnILBzBWIyFWrb7Hh7aBaAQW40=; t=1680110607; x=1681320207; b=auuBdJjWuTJ06rGBBX7bWLF7ZnS1ZCi6sRd8RYiMTMd7j2Z I+6mviCluz1OKdwcKhTsa0dt6JiGJbAuZgkNQOQ72YsQAqbQ8fbKf8rNTX5KoABb03HJLoNVmAhAW APc3EN9I+ReIskAuBn4YMRqhl64nrd4sKrSYDgiDWFQS6L1NkCEwgg81KUU9EvKPxL7CcOYrCN2uo GIuqnyWrdojn02gMQ51VF2DCvugsCFFxqa0fRinRCPjQKWo9mUKoolD55v55UEt0CrO1prcKnUH6P 5eNsIFiUJHBcybqUMwnGENX5P6LxVRB3z+uumEhnzs4/Bj+6qQ794q73m3K61ZOg==; Received: by sipsolutions.net with esmtpsa (TLS1.3:ECDHE_X25519__RSA_PSS_RSAE_SHA256__AES_256_GCM:256) (Exim 4.96) (envelope-from ) id 1phZW8-000AZQ-2S; Wed, 29 Mar 2023 19:23:24 +0200 From: benjamin@sipsolutions.net To: linux-kselftest@vger.kernel.org, kunit-dev@googlegroups.com Cc: Johannes Berg , Benjamin Berg Subject: [PATCH 3/3] kunit: add a convenience allocation wrapper for SKBs Date: Wed, 29 Mar 2023 19:23:11 +0200 Message-Id: <20230329172311.71861-3-benjamin@sipsolutions.net> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230329172311.71861-1-benjamin@sipsolutions.net> References: <20230329172311.71861-1-benjamin@sipsolutions.net> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org From: Benjamin Berg This simplifies the use of SKBs in tests by avoiding the need for error checking. Signed-off-by: Benjamin Berg --- include/kunit/skbuff.h | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 include/kunit/skbuff.h diff --git a/include/kunit/skbuff.h b/include/kunit/skbuff.h new file mode 100644 index 000000000000..46dcb00af655 --- /dev/null +++ b/include/kunit/skbuff.h @@ -0,0 +1,38 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* + * KUnit struct sk_buff helpers. + * + * Copyright (C) 2023 Intel Corporation + */ + +#ifndef _KUNIT_SKBUFF_H +#define _KUNIT_SKBUFF_H + +#include +#include + +/** + * kunit_zalloc_skb() - Allocate and initialize a resource managed skb. + * @test: The test case to which the skb belongs + * @len: size to allocate + * @gfp: allocation mask + * + * Allocate a new struct sk_buff, zero fill the give length and add it as a + * resource to the kunit test for automatic cleanup. + * + * The function will not return in case of an allocation error. + */ +static inline struct sk_buff *kunit_zalloc_skb(struct kunit *test, int len, + gfp_t gfp) +{ + struct sk_buff *res = alloc_skb(len, gfp); + + KUNIT_ASSERT_NOT_NULL(test, res); + KUNIT_ASSERT_EQ(test, skb_pad(res, len), 0); + + kunit_add_cleanup(test, (kunit_cleanup_t) kfree_skb, res, gfp); + + return res; +} + +#endif /* _KUNIT_SKBUFF_H */