From patchwork Tue Jul 9 06:30:07 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Brendan Higgins X-Patchwork-Id: 11036193 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id F103013BD for ; Tue, 9 Jul 2019 06:33:04 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id E0EEA28608 for ; Tue, 9 Jul 2019 06:33:04 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id D448F28741; Tue, 9 Jul 2019 06:33:04 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.5 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,MAILING_LIST_MULTI,RCVD_IN_DNSWL_HI, USER_IN_DEF_DKIM_WL autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 045F828712 for ; Tue, 9 Jul 2019 06:33:04 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726324AbfGIGdD (ORCPT ); Tue, 9 Jul 2019 02:33:03 -0400 Received: from mail-pf1-f202.google.com ([209.85.210.202]:37754 "EHLO mail-pf1-f202.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726318AbfGIGdD (ORCPT ); Tue, 9 Jul 2019 02:33:03 -0400 Received: by mail-pf1-f202.google.com with SMTP id x18so11829635pfj.4 for ; Mon, 08 Jul 2019 23:33:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=ic3smNwcHiaf2+e332OcS9f4hkMErHF1/7OHIGEcmHI=; b=fy9pkOZwj7UVsBTuEV3e1Y8jTWD/Ef50zM3WIjs57wHrH0SAslFTmQzqZaMWRk3J0d OtUWHFjtLHJxBsWbSmq+tk1UVxu0Vf2bA8GTrnh2SUor/KsUvIm07YaBmFMhkQF5s3+o lcO8EfaFp5WGzVrKamWXj+LwxFZbiqoQhhzOn9s/DnFS8TZot1U7bGr87Q5R/SExqY3b qIVNIw5Z4ZDfPg6ejrNS8unyGxe6mQbQ+At54/o+Fv48j+E+9/bZtGwq3EjIJhdrNtlO X1Q0Uy8y0qaRAwPQbbw/2DgVdwUiV0pNyIkZauq4CuZUECDAr3sDRgZI2RFEjLhB53my q6Mg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=ic3smNwcHiaf2+e332OcS9f4hkMErHF1/7OHIGEcmHI=; b=jjimulk+wuhAB8Wo/FfFANPjI4OHYalPn14P9zzcLQzubryjEXu7r9/Z0sFhIg59e8 AqcqUvDdA35DDwGft8UTBGU7d9+BEDbcVVvR4qOVkIwOiHWExE0PgDyU7JV9is+15unG d/a/RoVsEjcXgvJ3rgnez3F20G1KJggHCnQWsWYf1uXpTUpDUur5Nm1qLpgqr1A7RVtw QUsvvPd5AyEnjhLaWG5VtElHk2nPwqp/8jsqqZHTj40oVAa1e+RjyMK4RubSk4UKR5GN 4wu2a9iY0EdoYCIIwFoIaQgXMtCBIKLMwnXcEdsxqDDh2npZTRYsjaQBglonHTvhYcGX NpBw== X-Gm-Message-State: APjAAAXYowp4u106UrJFqkt2Wp0Y4CNsIUapUxrcEQcBu4fA5MiBBUJ5 iUGRjp0CYFexDjLjRy/O2Mkl8FykVcnQSpFLKk67eQ== X-Google-Smtp-Source: APXvYqySg7uvIFF3a1nVlJf8kZvvQTv4WJjYnjMp3jaZer3I39OEkk56iyIVupnXV0sHczMI23H94jI1fh5SWUz35wHjkg== X-Received: by 2002:a63:6fc9:: with SMTP id k192mr28317812pgc.20.1562653982165; Mon, 08 Jul 2019 23:33:02 -0700 (PDT) Date: Mon, 8 Jul 2019 23:30:07 -0700 In-Reply-To: <20190709063023.251446-1-brendanhiggins@google.com> Message-Id: <20190709063023.251446-3-brendanhiggins@google.com> Mime-Version: 1.0 References: <20190709063023.251446-1-brendanhiggins@google.com> X-Mailer: git-send-email 2.22.0.410.gd8fdbe21b5-goog Subject: [PATCH v7 02/18] kunit: test: add test resource management API From: Brendan Higgins To: frowand.list@gmail.com, gregkh@linuxfoundation.org, jpoimboe@redhat.com, keescook@google.com, kieran.bingham@ideasonboard.com, mcgrof@kernel.org, peterz@infradead.org, robh@kernel.org, sboyd@kernel.org, shuah@kernel.org, tytso@mit.edu, yamada.masahiro@socionext.com Cc: devicetree@vger.kernel.org, dri-devel@lists.freedesktop.org, kunit-dev@googlegroups.com, linux-doc@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-kbuild@vger.kernel.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-nvdimm@lists.01.org, linux-um@lists.infradead.org, Alexander.Levin@microsoft.com, Tim.Bird@sony.com, amir73il@gmail.com, dan.carpenter@oracle.com, daniel@ffwll.ch, jdike@addtoit.com, joel@jms.id.au, julia.lawall@lip6.fr, khilman@baylibre.com, knut.omang@oracle.com, logang@deltatee.com, mpe@ellerman.id.au, pmladek@suse.com, rdunlap@infradead.org, richard@nod.at, rientjes@google.com, rostedt@goodmis.org, wfg@linux.intel.com, Brendan Higgins Sender: linux-kselftest-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Create a common API for test managed resources like memory and test objects. A lot of times a test will want to set up infrastructure to be used in test cases; this could be anything from just wanting to allocate some memory to setting up a driver stack; this defines facilities for creating "test resources" which are managed by the test infrastructure and are automatically cleaned up at the conclusion of the test. Signed-off-by: Brendan Higgins Reviewed-by: Greg Kroah-Hartman Reviewed-by: Logan Gunthorpe --- include/kunit/test.h | 116 +++++++++++++++++++++++++++++++++++++++++++ kunit/test.c | 94 +++++++++++++++++++++++++++++++++++ 2 files changed, 210 insertions(+) diff --git a/include/kunit/test.h b/include/kunit/test.h index e0b34acb9ee4e..bdf41d31c343c 100644 --- a/include/kunit/test.h +++ b/include/kunit/test.h @@ -10,6 +10,70 @@ #define _KUNIT_TEST_H #include +#include + +struct kunit_resource; + +typedef int (*kunit_resource_init_t)(struct kunit_resource *, void *); +typedef void (*kunit_resource_free_t)(struct kunit_resource *); + +/** + * struct kunit_resource - represents a *test managed resource* + * @allocation: for the user to store arbitrary data. + * @free: a user supplied function to free the resource. Populated by + * kunit_alloc_resource(). + * + * Represents a *test managed resource*, a resource which will automatically be + * cleaned up at the end of a test case. + * + * Example: + * + * .. code-block:: c + * + * struct kunit_kmalloc_params { + * size_t size; + * gfp_t gfp; + * }; + * + * static int kunit_kmalloc_init(struct kunit_resource *res, void *context) + * { + * struct kunit_kmalloc_params *params = context; + * res->allocation = kmalloc(params->size, params->gfp); + * + * if (!res->allocation) + * return -ENOMEM; + * + * return 0; + * } + * + * static void kunit_kmalloc_free(struct kunit_resource *res) + * { + * kfree(res->allocation); + * } + * + * void *kunit_kmalloc(struct kunit *test, size_t size, gfp_t gfp) + * { + * struct kunit_kmalloc_params params; + * struct kunit_resource *res; + * + * params.size = size; + * params.gfp = gfp; + * + * res = kunit_alloc_resource(test, kunit_kmalloc_init, + * kunit_kmalloc_free, ¶ms); + * if (res) + * return res->allocation; + * + * return NULL; + * } + */ +struct kunit_resource { + void *allocation; + kunit_resource_free_t free; + + /* private: internal use only. */ + struct list_head node; +}; struct kunit; @@ -109,6 +173,13 @@ struct kunit { * have terminated. */ bool success; /* Read only after test_case finishes! */ + struct mutex lock; /* Gaurds all mutable test state. */ + /* + * Because resources is a list that may be updated multiple times (with + * new resources) from any thread associated with a test case, we must + * protect it with some type of lock. + */ + struct list_head resources; /* Protected by lock. */ }; void kunit_init_test(struct kunit *test, const char *name); @@ -141,6 +212,51 @@ int kunit_run_tests(struct kunit_suite *suite); } \ late_initcall(kunit_suite_init##suite) +/** + * kunit_alloc_resource() - Allocates a *test managed resource*. + * @test: The test context object. + * @init: a user supplied function to initialize the resource. + * @free: a user supplied function to free the resource. + * @context: for the user to pass in arbitrary data to the init function. + * + * Allocates a *test managed resource*, a resource which will automatically be + * cleaned up at the end of a test case. See &struct kunit_resource for an + * example. + */ +struct kunit_resource *kunit_alloc_resource(struct kunit *test, + kunit_resource_init_t init, + kunit_resource_free_t free, + void *context); + +void kunit_free_resource(struct kunit *test, struct kunit_resource *res); + +/** + * kunit_kmalloc() - Like kmalloc() except the allocation is *test managed*. + * @test: The test context object. + * @size: The size in bytes of the desired memory. + * @gfp: flags passed to underlying kmalloc(). + * + * Just like `kmalloc(...)`, except the allocation is managed by the test case + * and is automatically cleaned up after the test case concludes. See &struct + * kunit_resource for more information. + */ +void *kunit_kmalloc(struct kunit *test, size_t size, gfp_t gfp); + +/** + * kunit_kzalloc() - Just like kunit_kmalloc(), but zeroes the allocation. + * @test: The test context object. + * @size: The size in bytes of the desired memory. + * @gfp: flags passed to underlying kmalloc(). + * + * See kzalloc() and kunit_kmalloc() for more information. + */ +static inline void *kunit_kzalloc(struct kunit *test, size_t size, gfp_t gfp) +{ + return kunit_kmalloc(test, size, gfp | __GFP_ZERO); +} + +void kunit_cleanup(struct kunit *test); + void __printf(3, 4) kunit_printk(const char *level, const struct kunit *test, const char *fmt, ...); diff --git a/kunit/test.c b/kunit/test.c index 571e4c65deb5c..f165c9d8e10b0 100644 --- a/kunit/test.c +++ b/kunit/test.c @@ -122,6 +122,8 @@ static void kunit_print_test_case_ok_not_ok(struct kunit_case *test_case, void kunit_init_test(struct kunit *test, const char *name) { + mutex_init(&test->lock); + INIT_LIST_HEAD(&test->resources); test->name = name; test->success = true; } @@ -151,6 +153,8 @@ static void kunit_run_case(struct kunit_suite *suite, if (suite->exit) suite->exit(&test); + kunit_cleanup(&test); + test_case->success = test.success; } @@ -171,6 +175,96 @@ int kunit_run_tests(struct kunit_suite *suite) return 0; } +struct kunit_resource *kunit_alloc_resource(struct kunit *test, + kunit_resource_init_t init, + kunit_resource_free_t free, + void *context) +{ + struct kunit_resource *res; + int ret; + + res = kzalloc(sizeof(*res), GFP_KERNEL); + if (!res) + return NULL; + + ret = init(res, context); + if (ret) + return NULL; + + res->free = free; + mutex_lock(&test->lock); + list_add_tail(&res->node, &test->resources); + mutex_unlock(&test->lock); + + return res; +} + +void kunit_free_resource(struct kunit *test, struct kunit_resource *res) +{ + res->free(res); + list_del(&res->node); + kfree(res); +} + +struct kunit_kmalloc_params { + size_t size; + gfp_t gfp; +}; + +static int kunit_kmalloc_init(struct kunit_resource *res, void *context) +{ + struct kunit_kmalloc_params *params = context; + + res->allocation = kmalloc(params->size, params->gfp); + if (!res->allocation) + return -ENOMEM; + + return 0; +} + +static void kunit_kmalloc_free(struct kunit_resource *res) +{ + kfree(res->allocation); +} + +void *kunit_kmalloc(struct kunit *test, size_t size, gfp_t gfp) +{ + struct kunit_kmalloc_params params; + struct kunit_resource *res; + + params.size = size; + params.gfp = gfp; + + res = kunit_alloc_resource(test, + kunit_kmalloc_init, + kunit_kmalloc_free, + ¶ms); + + if (res) + return res->allocation; + + return NULL; +} + +void kunit_cleanup(struct kunit *test) +{ + struct kunit_resource *resource, *resource_safe; + + mutex_lock(&test->lock); + /* + * test->resources is a stack - each allocation must be freed in the + * reverse order from which it was added since one resource may depend + * on another for its entire lifetime. + */ + list_for_each_entry_safe_reverse(resource, + resource_safe, + &test->resources, + node) { + kunit_free_resource(test, resource); + } + mutex_unlock(&test->lock); +} + void kunit_printk(const char *level, const struct kunit *test, const char *fmt, ...)