Received: by 2002:a25:4158:0:0:0:0:0 with SMTP id o85csp1643075yba; Thu, 4 Apr 2019 15:11:18 -0700 (PDT) X-Google-Smtp-Source: APXvYqw+bcjzYo4099NdQv/2tzurSFPDCeJgUEKqpaHGB40a6bjdwPCdfYLfmsHJfM5md5WAGAjK X-Received: by 2002:a63:7d0a:: with SMTP id y10mr4241804pgc.292.1554415878457; Thu, 04 Apr 2019 15:11:18 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1554415878; cv=none; d=google.com; s=arc-20160816; b=GkrXzNQJ5HlBP+h5EqRHDaXAzaPeOmr/Oi02rkGdaqdiBdLP4C1w7ju01N5ldAklZ5 CNIHhYvWjD98WgCGcPO6EAiZa1aEs4R9g39nkO7diH+1lwygXKkDoJA6OkktiawlzuxJ cWvOhVIPFfmLQO3MAnthsdmX+ehYHB/erBW65x199C6KI7TOMJ//hDmVlg9C3LX4kLZa CgkVoToLix4YsR+xSZjbtBD4cQUYeq6UOrsV/Tl3ZRakJ7s54fXySONTgMOylScZRFG6 BJ975EM9A0I069Sj+cBb2InmlOsuK9ar+r+XFqEbll/SG8tfW59/qhl339k+nXdu2iKK FSRg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:from:subject:references :mime-version:message-id:in-reply-to:date:dkim-signature; bh=ul3rcOFvM3z9vCbrBV/KzBHe4/kShk09FwvvcSJ4FEI=; b=DhTOFYvv4SMVRZpHpEX1SeV6O9Vvf0m8H8ja45Zbd/cYRvTzMh024BVC9NjjitjwEX X9lxoJ/gvr/vpANC6XvVxCvFAHc78SqJ83WLnYza/7S58goY90ceNZVzXBoRGIlkZ7qb wEpo7r4IpXxAvZo8O54ngCSYa/S7oY8yduuukCy4Q53iF8wfjvtXFeiPEznckqXmk+fO jW/5wCHUYCdJUAb8qwhGgJl4+V7HFweH0WdTDx40iFdMODB8GFCmQzhMgPXrN4G4NXC1 /VbMc5pcOXJFU51hboeyH1EM0akBtbV/bl3LTuGvoaXUyc8cvY/VTIkgPS0bYgYOwxnW fG2g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=nznCIJfF; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 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 vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id o1si12040344pld.269.2019.04.04.15.11.02; Thu, 04 Apr 2019 15:11:18 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=nznCIJfF; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 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 S1730578AbfDDWKL (ORCPT + 99 others); Thu, 4 Apr 2019 18:10:11 -0400 Received: from mail-yw1-f73.google.com ([209.85.161.73]:38433 "EHLO mail-yw1-f73.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730512AbfDDWKH (ORCPT ); Thu, 4 Apr 2019 18:10:07 -0400 Received: by mail-yw1-f73.google.com with SMTP id i203so3060328ywa.5 for ; Thu, 04 Apr 2019 15:10:07 -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=ul3rcOFvM3z9vCbrBV/KzBHe4/kShk09FwvvcSJ4FEI=; b=nznCIJfFvHdf/ACxgRRWof101NuYDX9hhwzUPACysctLE60dlGJ3Ev7csqCVwL51Up t97owmGWzsVqOqCdyHote65r39wd0vsx/AS6EecSH8PiXvtaGzoycWiz48UMk+wfzsLE aEsDmLhNo3vZoDTb9UZ0DItQgLpN6ugAF5skPwJorCcG2BTOLrhFW98+S21XEyjMdOxz BB7YUbPqPwanoz/YeXaPiIDgTiXVBtXA0dYW/e9VuVeBtj6k80d/1//uxgPg3JtLU+8x v8riFMwLA2+gu8s6AjttblIBr420inDDvscJd1kDE2X6HVzIcpbYr9ZeBxPq1GW7/Yve CuKQ== 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=ul3rcOFvM3z9vCbrBV/KzBHe4/kShk09FwvvcSJ4FEI=; b=YQwXSjw414O4RNXp+k5tPvVKKjLhg/bFQMSqjEIKVjNoukiJ6MVzhwFgjaoUZ6lEhL fMGihXbq02qdqQpROVmES56nKqV40g+6oKGc9CoP/mHAA8k0Qh7bd171aJYIEakJXeU3 OXN9bl/b4HHOH0VaO4LbKlNfIrbtZsh6R98mss/6fw6hEoBu7XE5eyjQVzZmwIWmMd8Z 8VKpY783lkngyErY3tY8RLkQVaS6mtMCxkyVzGjbzdWvabd2XMywv6Pgk7pjKQTJ5LDb kPdbnz1RnQd76hvZspS2ufwGflqwTCldL77MVjO+yOpdJheVAuPNbG7xRHq0sDCxrtjT sK8g== X-Gm-Message-State: APjAAAU62hDnXgUoEOt5bH7m4Mr0tZka63VcrUxp64QVbhA5hhP2MP9U /vZWa5kATSYz5ooMRrrqSeRwdw7wFzmuu7MnM6L8nQ== X-Received: by 2002:a25:202:: with SMTP id 2mr1868781ybc.66.1554415806784; Thu, 04 Apr 2019 15:10:06 -0700 (PDT) Date: Thu, 4 Apr 2019 15:06:37 -0700 In-Reply-To: <20190404220652.19765-1-brendanhiggins@google.com> Message-Id: <20190404220652.19765-3-brendanhiggins@google.com> Mime-Version: 1.0 References: <20190404220652.19765-1-brendanhiggins@google.com> X-Mailer: git-send-email 2.21.0.392.gf8f6787159e-goog Subject: [PATCH v1 02/17] kunit: test: add test resource management API From: Brendan Higgins To: corbet@lwn.net, frowand.list@gmail.com, keescook@google.com, kieran.bingham@ideasonboard.com, mcgrof@kernel.org, robh@kernel.org, shuah@kernel.org, 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, dan.j.williams@intel.com, daniel@ffwll.ch, gregkh@linuxfoundation.org, jdike@addtoit.com, joel@jms.id.au, julia.lawall@lip6.fr, khilman@baylibre.com, knut.omang@oracle.com, mpe@ellerman.id.au, pmladek@suse.com, richard@nod.at, 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 --- include/kunit/test.h | 109 +++++++++++++++++++++++++++++++++++++++++++ kunit/test.c | 95 +++++++++++++++++++++++++++++++++++++ 2 files changed, 204 insertions(+) diff --git a/include/kunit/test.h b/include/kunit/test.h index 23c2ebedd6dd9..819edd8db4e81 100644 --- a/include/kunit/test.h +++ b/include/kunit/test.h @@ -12,6 +12,69 @@ #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; + * else + * return NULL; + * } + */ +struct kunit_resource { + void *allocation; + kunit_resource_free_t free; + + /* private: internal use only. */ + struct list_head node; +}; + struct kunit; /** @@ -104,6 +167,7 @@ struct kunit { const char *name; /* Read only after initialization! */ spinlock_t lock; /* Gaurds all mutable test state. */ bool success; /* Protected by lock. */ + struct list_head resources; /* Protected by lock. */ void (*vprintk)(const struct kunit *test, const char *level, struct va_format *vaf); @@ -127,6 +191,51 @@ int kunit_run_tests(struct kunit_module *module); } \ late_initcall(module_kunit_init##module) +/** + * 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 5bf97e2935579..541f9adb1608c 100644 --- a/kunit/test.c +++ b/kunit/test.c @@ -66,6 +66,7 @@ static void kunit_vprintk(const struct kunit *test, int kunit_init_test(struct kunit *test, const char *name) { spin_lock_init(&test->lock); + INIT_LIST_HEAD(&test->resources); test->name = name; test->vprintk = kunit_vprintk; @@ -93,6 +94,11 @@ static void kunit_run_case_internal(struct kunit *test, test_case->run_case(test); } +static void kunit_case_internal_cleanup(struct kunit *test) +{ + kunit_cleanup(test); +} + /* * Performs post validations and cleanup after a test case was run. * XXX: Should ONLY BE CALLED AFTER kunit_run_case_internal! @@ -103,6 +109,8 @@ static void kunit_run_case_cleanup(struct kunit *test, { if (module->exit) module->exit(test); + + kunit_case_internal_cleanup(test); } /* @@ -150,6 +158,93 @@ int kunit_run_tests(struct kunit_module *module) 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; + unsigned long flags; + int ret; + + res = kzalloc(sizeof(*res), GFP_KERNEL); + if (!res) + return NULL; + + ret = init(res, context); + if (ret) + return NULL; + + res->free = free; + spin_lock_irqsave(&test->lock, flags); + list_add_tail(&res->node, &test->resources); + spin_unlock_irqrestore(&test->lock, flags); + + 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; + else + return NULL; +} + +void kunit_cleanup(struct kunit *test) +{ + struct kunit_resource *resource, *resource_safe; + unsigned long flags; + + spin_lock_irqsave(&test->lock, flags); + list_for_each_entry_safe(resource, + resource_safe, + &test->resources, + node) { + kunit_free_resource(test, resource); + } + spin_unlock_irqrestore(&test->lock, flags); +} + void kunit_printk(const char *level, const struct kunit *test, const char *fmt, ...) -- 2.21.0.392.gf8f6787159e-goog