From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-17.4 required=3.0 tests=DKIMWL_WL_MED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_PATCH, MAILING_LIST_MULTI,SIGNED_OFF_BY,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED, USER_AGENT_GIT,USER_IN_DEF_DKIM_WL autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 19C58C0650F for ; Wed, 14 Aug 2019 05:52:21 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id D2D9C208C2 for ; Wed, 14 Aug 2019 05:52:20 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="m4t4Q1X6" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727535AbfHNFwT (ORCPT ); Wed, 14 Aug 2019 01:52:19 -0400 Received: from mail-qk1-f202.google.com ([209.85.222.202]:41164 "EHLO mail-qk1-f202.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727474AbfHNFwQ (ORCPT ); Wed, 14 Aug 2019 01:52:16 -0400 Received: by mail-qk1-f202.google.com with SMTP id d9so98339896qko.8 for ; Tue, 13 Aug 2019 22:52:15 -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=DkLMCozEggqiah26AxqlN2wVZCZ6gbhovFpZPXrfnXM=; b=m4t4Q1X6QPpxj/zxUBj0Bfqq0D1W7RaUXFV2UJG7RPYtVOGnvPgRVr5f4O6qDylP/D /zKl0j4bshAkl/SnRaSy1svuJjgTQ3zeY0qcwIZJpkmBl8pQx52sfsDtDHg50sSUzze5 vU1peLBJ2nXQjG6T1JfQZjlJ2f2O4ouGNRQywzI7Hhv4p1QxNk4+9CExZ6UlzAK3yJ2l dxe+c62XEyjw/gbUCwdZ5+Vhees64V5zIUBOXxPn3V2Enp9yuLGM53DZWHVLRUjSAS+R qByxl70Jpi9n3prqNX5UlmZjRiEqRYsb1rBRubgevh4Od9R3HxcQBQU4aIvNAEFIoloO lqbQ== 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=DkLMCozEggqiah26AxqlN2wVZCZ6gbhovFpZPXrfnXM=; b=nswwNqEup/J4tSLPJc3iqFe7kJpoanDw9FYcTtnZMAJdfeOYb80+12Yqeo89fvl6kQ WRKc93IJS6PNk+0pC52LpKWAxQV3AZpARhrY2dPa6N1y9HEG4n1RBHh4P0Co2Kgu7bHt Pl7tXoN2/5F2eMhA2cXDNkjkNmN4chmlT+AeWViolZNGywrDbiwf/zyM4nyhLn6lrvkq p3qm7Kwm61kGl14nmwHiUiP2z9WAFVuWOHKRaDPp7mIpnHiEHqUKNQ0SIfDzM+jg+/qS s2mvtMDag4kCklYB1nSBayBlK//KnCnBsGBX1H0mpDivSTwH9/0A7w7qhno+h1jnq25x UnQw== X-Gm-Message-State: APjAAAXHN5AJJnYOGsR+aytuVJRfgNczI1yiu/mvrUIrp20mwaASEGyx BT3FTE0U8VElJZIbcOZDduaaipIN+B8PCF1g1oQOew== X-Google-Smtp-Source: APXvYqw6D6FOJmjXUgHV73WYrM9Cqo23sFK0ISXT0FY24aZM69VuFI5amqhnAA+Yktkb+GeyCovE7OLyBqGxYjrF+lwUEQ== X-Received: by 2002:ac8:4802:: with SMTP id g2mr35127747qtq.202.1565761934421; Tue, 13 Aug 2019 22:52:14 -0700 (PDT) Date: Tue, 13 Aug 2019 22:50:52 -0700 In-Reply-To: <20190814055108.214253-1-brendanhiggins@google.com> Message-Id: <20190814055108.214253-3-brendanhiggins@google.com> Mime-Version: 1.0 References: <20190814055108.214253-1-brendanhiggins@google.com> X-Mailer: git-send-email 2.23.0.rc1.153.gdeed80330f-goog Subject: [PATCH v13 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 Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 Reviewed-by: Stephen Boyd --- include/kunit/test.h | 187 +++++++++++++++++++++++++++++++++++++++++++ kunit/test.c | 163 +++++++++++++++++++++++++++++++++++++ 2 files changed, 350 insertions(+) diff --git a/include/kunit/test.h b/include/kunit/test.h index e0b34acb9ee4e..f264ffe58f008 100644 --- a/include/kunit/test.h +++ b/include/kunit/test.h @@ -9,8 +9,72 @@ #ifndef _KUNIT_TEST_H #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! */ + spinlock_t lock; /* Guards 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,122 @@ int kunit_run_tests(struct kunit_suite *suite); } \ late_initcall(kunit_suite_init##suite) +/* + * Like kunit_alloc_resource() below, but returns the struct kunit_resource + * object that contains the allocation. This is mostly for testing purposes. + */ +struct kunit_resource *kunit_alloc_and_get_resource(struct kunit *test, + kunit_resource_init_t init, + kunit_resource_free_t free, + gfp_t internal_gfp, + void *context); + +/** + * 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. + * @internal_gfp: gfp to use for internal allocations, if unsure, use GFP_KERNEL + * @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. + * + * NOTE: KUnit needs to allocate memory for each kunit_resource object. You must + * specify an @internal_gfp that is compatible with the use context of your + * resource. + */ +static inline void *kunit_alloc_resource(struct kunit *test, + kunit_resource_init_t init, + kunit_resource_free_t free, + gfp_t internal_gfp, + void *context) +{ + struct kunit_resource *res; + + res = kunit_alloc_and_get_resource(test, init, free, internal_gfp, + context); + + if (res) + return res->allocation; + + return NULL; +} + +typedef bool (*kunit_resource_match_t)(struct kunit *test, + const void *res, + void *match_data); + +/** + * kunit_resource_instance_match() - Match a resource with the same instance. + * @test: Test case to which the resource belongs. + * @res: The data stored in kunit_resource->allocation. + * @match_data: The resource pointer to match against. + * + * An instance of kunit_resource_match_t that matches a resource whose + * allocation matches @match_data. + */ +static inline bool kunit_resource_instance_match(struct kunit *test, + const void *res, + void *match_data) +{ + return res == match_data; +} + +/** + * kunit_resource_destroy() - Find a kunit_resource and destroy it. + * @test: Test case to which the resource belongs. + * @match: Match function. Returns whether a given resource matches @match_data. + * @free: Must match free on the kunit_resource to free. + * @match_data: Data passed into @match. + * + * Free the latest kunit_resource of @test for which @free matches the + * kunit_resource_free_t associated with the resource and for which @match + * returns true. + * + * RETURNS: + * 0 if kunit_resource is found and freed, -ENOENT if not found. + */ +int kunit_resource_destroy(struct kunit *test, + kunit_resource_match_t match, + kunit_resource_free_t free, + void *match_data); + +/** + * 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_kfree() - Like kfree except for allocations managed by KUnit. + * @test: The test case to which the resource belongs. + * @ptr: The memory allocation to free. + */ +void kunit_kfree(struct kunit *test, const void *ptr); + +/** + * 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 d3dda359f99b1..68b1037ab74d0 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) { + spin_lock_init(&test->lock); + INIT_LIST_HEAD(&test->resources); test->name = name; test->success = true; } @@ -153,6 +155,8 @@ static void kunit_run_case(struct kunit_suite *suite, if (suite->exit) suite->exit(&test); + kunit_cleanup(&test); + test_case->success = test.success; } @@ -173,6 +177,165 @@ int kunit_run_tests(struct kunit_suite *suite) return 0; } +struct kunit_resource *kunit_alloc_and_get_resource(struct kunit *test, + kunit_resource_init_t init, + kunit_resource_free_t free, + gfp_t internal_gfp, + void *context) +{ + struct kunit_resource *res; + int ret; + + res = kzalloc(sizeof(*res), internal_gfp); + if (!res) + return NULL; + + ret = init(res, context); + if (ret) + return NULL; + + res->free = free; + spin_lock(&test->lock); + list_add_tail(&res->node, &test->resources); + spin_unlock(&test->lock); + + return res; +} + +static void kunit_resource_free(struct kunit *test, struct kunit_resource *res) +{ + res->free(res); + kfree(res); +} + +static struct kunit_resource *kunit_resource_find(struct kunit *test, + kunit_resource_match_t match, + kunit_resource_free_t free, + void *match_data) +{ + struct kunit_resource *resource; + + lockdep_assert_held(&test->lock); + + list_for_each_entry_reverse(resource, &test->resources, node) { + if (resource->free != free) + continue; + if (match(test, resource->allocation, match_data)) + return resource; + } + + return NULL; +} + +static struct kunit_resource *kunit_resource_remove( + struct kunit *test, + kunit_resource_match_t match, + kunit_resource_free_t free, + void *match_data) +{ + struct kunit_resource *resource; + + spin_lock(&test->lock); + resource = kunit_resource_find(test, match, free, match_data); + if (resource) + list_del(&resource->node); + spin_unlock(&test->lock); + + return resource; +} + +int kunit_resource_destroy(struct kunit *test, + kunit_resource_match_t match, + kunit_resource_free_t free, + void *match_data) +{ + struct kunit_resource *resource; + + resource = kunit_resource_remove(test, match, free, match_data); + + if (!resource) + return -ENOENT; + + kunit_resource_free(test, resource); + return 0; +} + +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 = { + .size = size, + .gfp = gfp + }; + + return kunit_alloc_resource(test, + kunit_kmalloc_init, + kunit_kmalloc_free, + gfp, + ¶ms); +} + +void kunit_kfree(struct kunit *test, const void *ptr) +{ + int rc; + + rc = kunit_resource_destroy(test, + kunit_resource_instance_match, + kunit_kmalloc_free, + (void *)ptr); + + WARN_ON(rc); +} + +void kunit_cleanup(struct kunit *test) +{ + struct kunit_resource *resource; + + /* + * 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. + * Also, we cannot use the normal list_for_each constructs, even the + * safe ones because *arbitrary* nodes may be deleted when + * kunit_resource_free is called; the list_for_each_safe variants only + * protect against the current node being deleted, not the next. + */ + while (true) { + spin_lock(&test->lock); + if (list_empty(&test->resources)) { + spin_unlock(&test->lock); + break; + } + resource = list_last_entry(&test->resources, + struct kunit_resource, + node); + list_del(&resource->node); + spin_unlock(&test->lock); + + kunit_resource_free(test, resource); + } +} + void kunit_printk(const char *level, const struct kunit *test, const char *fmt, ...) -- 2.23.0.rc1.153.gdeed80330f-goog