Received: by 2002:a05:6a10:2726:0:0:0:0 with SMTP id ib38csp1815656pxb; Sat, 2 Apr 2022 04:46:07 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwVgHvs8sN0997Or6OLS3r/Iuz7MpdA/wBqPC3eobcHQCvE/FuRrE8HpgzEHW+xcmSzUgXO X-Received: by 2002:a17:902:e20a:b0:154:bfb8:7141 with SMTP id u10-20020a170902e20a00b00154bfb87141mr14808222plb.164.1648899967645; Sat, 02 Apr 2022 04:46:07 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1648899967; cv=none; d=google.com; s=arc-20160816; b=E/nWuHM67a5SZtaFzsg3Gy0Wz37h39mzqkQHaji9kGCL+Za0TORxzvkfcsaePRCIh7 7Ea+FTHbQv8fuYHCGHVKCb0j4gWVGu2xQC+NB0Gt9woBE07qeu0gnzgCDSgt4V12asl0 CVjj/LdK7rxEXWh3O5UtvirTvnBznx4upNNHCBXMnHaEh8lYyoTNnxQ5ywPwHqvRHaOY DAXv3P2TUiuIAiHYxfkuBKYHwujkrcI/p5vxf92lsUkX9arxUGYAGv02MUqMwDGva58J xfYxaS2mFu0VFA/AYblzUoyPVfsh1F7ZEwTcYdfZhZMQwLB6jzOSNxZY0IPxvxjL6BBZ ZVag== 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:references:mime-version :message-id:in-reply-to:date:dkim-signature; bh=cZc0GmrzkAfI+cudbavM/icDpyA+Hphtk/mRobHHbxA=; b=aDaBkPdBRAvXDEJLP6NNZcHZTJ8z61K+oGszP/N0hrKwSjBcnF/t3aT9kCpIi1uOAx dHqBBBi89/k4FtFxJx7y7jLgcExhMxQ/GqZ70Zae5Jn0l5u/YS423rFr9kT+sc6eRkPE p4dsw47dKW4t+wIdJ0X2xYLnUpRHPvCOQLK+Xp0htQTG/OJ1pxCjMUm1m8P9CXM2tiVF EDv2Pxan7403taU7dhnFM9j9qYs59dE3J95Yvbc+QSYf4ZiyYnk1HbhSSEcragSgFG+7 6/BYX7tjX8/cMt3VCf/XGy5L3PFUwSfos+FRxI5WNsT5AaIBTjp4r3BXhpkixRENhpzH oEvA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=eJ1+d5Ow; 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 x13-20020a056a00188d00b004fa3a8e0096si5331510pfh.333.2022.04.02.04.45.54; Sat, 02 Apr 2022 04:46:07 -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=eJ1+d5Ow; 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 S245426AbiDBEhp (ORCPT + 99 others); Sat, 2 Apr 2022 00:37:45 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53922 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239350AbiDBEhm (ORCPT ); Sat, 2 Apr 2022 00:37:42 -0400 Received: from mail-pg1-x54a.google.com (mail-pg1-x54a.google.com [IPv6:2607:f8b0:4864:20::54a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 256A2DEAB for ; Fri, 1 Apr 2022 21:35:51 -0700 (PDT) Received: by mail-pg1-x54a.google.com with SMTP id t24-20020a632258000000b003988eed18b2so2492083pgm.22 for ; Fri, 01 Apr 2022 21:35:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=cZc0GmrzkAfI+cudbavM/icDpyA+Hphtk/mRobHHbxA=; b=eJ1+d5OwPj70RjCWQfMFoPQzg7fN44o1GQGVW0qt7KbxVqUVHPBcWXEBNa7aUtWbpz 1zqTtF19zQcTPpzG31w7ZSz7qVpEZeD5insNCJZRDAGk/jT2Vue3rHkNoeH/OK4JhUaF 3P3B4Of1iRJU5WQETD8QmUFfy6ZKqpGwVLXw0EetMpjz5Fd34hKtM5DqrgtCbE/bSFKe l0R/CYxT+8GaHVuQoUlk+ku8NakR8GcKfKvr0tKyBexAVWYBZwg9qri+bAVkCHqWuF7f DAdaAkCQ8e/JEZ+V5MEO0AUd+ochdbfefGVDgCzfEBD/MDfv1SsaTfNQqKeFpRpEE3rF W/mg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=cZc0GmrzkAfI+cudbavM/icDpyA+Hphtk/mRobHHbxA=; b=nBM/1eZA4eGX/kzjV2dyzrKjXN6cq/Y4MKgKakoAJhkEz+W1KrziBQzMv6AaRci6VF am9N7aUrnhxUJxjE0gbADW0wrFsjgRnX8BXSgBD5Q6S/f3k0xKQmyIh1SdEx7Yvm97ZQ YlWPwzfpsVhQcTChUBvM0Y5lssd6wENBVNXnSluyW6Xg/zYarN6M0+xS9EnKirgvgded dcxRtmNXjdcWUpqwet/cRyzDO7so5WJ3sEw+iLRAK+vuGkbG7A4+0CHjNdIVHRAPJIoA 1pc6AcT09oPWIRL7nitJFuYG6ChjQINUxaWvvHfha21NWi9Vq9xeGirSWNGYf2cuclr5 +qBg== X-Gm-Message-State: AOAM530WE6c/dVFJqSndGGn1NiHjKxmYdrsKvAXitGs3mSawEzSTyqiP 45idkH7lDXbXkCYymEgOQ4c/bpnpaK+54A== X-Received: from slicestar.c.googlers.com ([fda3:e722:ac3:cc00:4f:4b78:c0a8:20a1]) (user=davidgow job=sendgmr) by 2002:a17:90b:e81:b0:1c6:5a9c:5afa with SMTP id fv1-20020a17090b0e8100b001c65a9c5afamr751637pjb.1.1648874150361; Fri, 01 Apr 2022 21:35:50 -0700 (PDT) Date: Sat, 2 Apr 2022 12:35:30 +0800 In-Reply-To: <20220402043530.923747-1-davidgow@google.com> Message-Id: <20220402043530.923747-2-davidgow@google.com> Mime-Version: 1.0 References: <20220402043530.923747-1-davidgow@google.com> X-Mailer: git-send-email 2.35.1.1094.g7c7d902a7c-goog Subject: [PATCH v2 2/2] kunit: Rework kunit_resource allocation policy From: David Gow To: Daniel Latypov , Brendan Higgins , Shuah Khan Cc: David Gow , kunit-dev@googlegroups.com, linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-9.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE,USER_IN_DEF_DKIM_WL autolearn=ham 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 KUnit's test-managed resources can be created in two ways: - Using the kunit_add_resource() family of functions, which accept a struct kunit_resource pointer, typically allocated statically or on the stack during the test. - Using the kunit_alloc_resource() family of functions, which allocate a struct kunit_resource using kzalloc() behind the scenes. Both of these families of functions accept a 'free' function to be called when the resource is finally disposed of. At present, KUnit will kfree() the resource if this 'free' function is specified, and will not if it is NULL. However, this can lead kunit_alloc_resource() to leak memory (if no 'free' function is passed in), or kunit_add_resource() to incorrectly kfree() memory which was allocated by some other means (on the stack, as part of a larger allocation, etc), if a 'free' function is provided. Instead, always kfree() if the resource was allocated with kunit_alloc_resource(), and never kfree() if it was passed into kunit_add_resource() by the user. (If the user of kunit_add_resource() wishes the resource be kfree()ed, they can call kfree() on the resource from within the 'free' function. This is implemented by adding a 'should_free' member to struct kunit_resource and setting it appropriately. To facilitate this, the various resource add/alloc functions have been refactored somewhat, making them all call a __kunit_add_resource() helper after setting the 'should_free' member appropriately. In the process, all other functions have been made static inline functions. Signed-off-by: David Gow Tested-by: Daniel Latypov --- Changes since v1: https://lore.kernel.org/linux-kselftest/20220319055600.3471875-1-davidgow@google.com/ - Rebased on top of Daniel's split of the resource system into resource.{c,h} - https://lore.kernel.org/linux-kselftest/20220328174143.857262-1-dlatypov@google.com/ - https://lore.kernel.org/linux-kselftest/20220328174143.857262-2-dlatypov@google.com/ - Renamed should_free to should_kfree - Fixed an issue where should_kfree was overwritten. - Tidied up some comments slightly. include/kunit/resource.h | 142 +++++++++++++++++++++++++++++++-------- lib/kunit/resource.c | 64 ++---------------- 2 files changed, 122 insertions(+), 84 deletions(-) diff --git a/include/kunit/resource.h b/include/kunit/resource.h index 7ab1fd83972b..09c2b34d1c61 100644 --- a/include/kunit/resource.h +++ b/include/kunit/resource.h @@ -25,11 +25,13 @@ typedef void (*kunit_resource_free_t)(struct kunit_resource *); * struct kunit_resource - represents a *test managed resource* * @data: for the user to store arbitrary data. * @name: optional name - * @free: a user supplied function to free the resource. Populated by - * kunit_resource_alloc(). + * @free: a user supplied function to free the resource. * * Represents a *test managed resource*, a resource which will automatically be - * cleaned up at the end of a test case. + * cleaned up at the end of a test case. This cleanup is performed by the 'free' + * function. The struct kunit_resource itself is freed automatically with + * kfree() if it was allocated by KUnit (e.g., by kunit_alloc_resource()), but + * must be freed by the user otherwise. * * Resources are reference counted so if a resource is retrieved via * kunit_alloc_and_get_resource() or kunit_find_resource(), we need @@ -86,18 +88,9 @@ struct kunit_resource { /* private: internal use only. */ struct kref refcount; struct list_head node; + bool should_kfree; }; -/* - * 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_get_resource() - Hold resource for use. Should not need to be used * by most users as we automatically get resources @@ -118,11 +111,14 @@ static inline void kunit_release_resource(struct kref *kref) struct kunit_resource *res = container_of(kref, struct kunit_resource, refcount); - /* If free function is defined, resource was dynamically allocated. */ - if (res->free) { + if (res->free) res->free(res); + + /* 'res' is valid here, as if should_kfree is set, res->free may not free + * 'res' itself, just res->data + */ + if (res->should_kfree) kfree(res); - } } /** @@ -142,6 +138,24 @@ static inline void kunit_put_resource(struct kunit_resource *res) kref_put(&res->refcount, kunit_release_resource); } +/** + * __kunit_add_resource() - Internal helper to add a resource. + * + * res->should_kfree is not initialised. + * @test: The test context object. + * @init: a user-supplied function to initialize the result (if needed). If + * none is supplied, the resource data value is simply set to @data. + * If an init function is supplied, @data is passed to it instead. + * @free: a user-supplied function to free the resource (if needed). + * @res: The resource. + * @data: value to pass to init function or set in resource data field. + */ +int __kunit_add_resource(struct kunit *test, + kunit_resource_init_t init, + kunit_resource_free_t free, + struct kunit_resource *res, + void *data); + /** * kunit_add_resource() - Add a *test managed resource*. * @test: The test context object. @@ -152,11 +166,18 @@ static inline void kunit_put_resource(struct kunit_resource *res) * @res: The resource. * @data: value to pass to init function or set in resource data field. */ -int kunit_add_resource(struct kunit *test, - kunit_resource_init_t init, - kunit_resource_free_t free, - struct kunit_resource *res, - void *data); +static inline int kunit_add_resource(struct kunit *test, + kunit_resource_init_t init, + kunit_resource_free_t free, + struct kunit_resource *res, + void *data) +{ + res->should_kfree = false; + return __kunit_add_resource(test, init, free, res, data); +} + +static inline struct kunit_resource * +kunit_find_named_resource(struct kunit *test, const char *name); /** * kunit_add_named_resource() - Add a named *test managed resource*. @@ -167,18 +188,84 @@ int kunit_add_resource(struct kunit *test, * @name: name to be set for resource. * @data: value to pass to init function or set in resource data field. */ -int kunit_add_named_resource(struct kunit *test, +static inline int kunit_add_named_resource(struct kunit *test, + kunit_resource_init_t init, + kunit_resource_free_t free, + struct kunit_resource *res, + const char *name, + void *data) +{ + struct kunit_resource *existing; + + if (!name) + return -EINVAL; + + existing = kunit_find_named_resource(test, name); + if (existing) { + kunit_put_resource(existing); + return -EEXIST; + } + + res->name = name; + res->should_kfree = false; + + return __kunit_add_resource(test, init, free, res, data); +} + +/** + * kunit_alloc_and_get_resource() - Allocates and returns 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 (if needed). + * @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. + * + * This is effectively identical to kunit_alloc_resource, but returns the + * struct kunit_resource pointer, not just the 'data' pointer. It therefore + * also increments the resource's refcount, so kunit_put_resource() should be + * called when you've finished with it. + * + * Note: KUnit needs to allocate memory for a kunit_resource object. You must + * specify an @internal_gfp that is compatible with the use context of your + * resource. + */ +static inline struct kunit_resource * +kunit_alloc_and_get_resource(struct kunit *test, kunit_resource_init_t init, kunit_resource_free_t free, - struct kunit_resource *res, - const char *name, - void *data); + gfp_t internal_gfp, + void *context) +{ + struct kunit_resource *res; + int ret; + + res = kzalloc(sizeof(*res), internal_gfp); + if (!res) + return NULL; + + res->should_kfree = true; + + ret = __kunit_add_resource(test, init, free, res, context); + if (!ret) { + /* + * bump refcount for get; kunit_resource_put() should be called + * when done. + */ + kunit_get_resource(res); + return res; + } + return NULL; +} /** * 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. + * @free: a user supplied function to free the resource (if needed). * @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. * @@ -202,7 +289,8 @@ static inline void *kunit_alloc_resource(struct kunit *test, if (!res) return NULL; - if (!kunit_add_resource(test, init, free, res, context)) + res->should_kfree = true; + if (!__kunit_add_resource(test, init, free, res, context)) return res->data; return NULL; diff --git a/lib/kunit/resource.c b/lib/kunit/resource.c index 09ec392d2323..9acef295d519 100644 --- a/lib/kunit/resource.c +++ b/lib/kunit/resource.c @@ -14,13 +14,13 @@ * Used for static resources and when a kunit_resource * has been created by * kunit_alloc_resource(). When an init function is supplied, @data is passed * into the init function; otherwise, we simply set the resource data field to - * the data value passed in. + * the data value passed in. Doesn't initialize res->should_kfree. */ -int kunit_add_resource(struct kunit *test, - kunit_resource_init_t init, - kunit_resource_free_t free, - struct kunit_resource *res, - void *data) +int __kunit_add_resource(struct kunit *test, + kunit_resource_init_t init, + kunit_resource_free_t free, + struct kunit_resource *res, + void *data) { int ret = 0; unsigned long flags; @@ -43,57 +43,7 @@ int kunit_add_resource(struct kunit *test, return ret; } -EXPORT_SYMBOL_GPL(kunit_add_resource); - -int kunit_add_named_resource(struct kunit *test, - kunit_resource_init_t init, - kunit_resource_free_t free, - struct kunit_resource *res, - const char *name, - void *data) -{ - struct kunit_resource *existing; - - if (!name) - return -EINVAL; - - existing = kunit_find_named_resource(test, name); - if (existing) { - kunit_put_resource(existing); - return -EEXIST; - } - - res->name = name; - - return kunit_add_resource(test, init, free, res, data); -} -EXPORT_SYMBOL_GPL(kunit_add_named_resource); - -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 *data) -{ - struct kunit_resource *res; - int ret; - - res = kzalloc(sizeof(*res), internal_gfp); - if (!res) - return NULL; - - ret = kunit_add_resource(test, init, free, res, data); - if (!ret) { - /* - * bump refcount for get; kunit_resource_put() should be called - * when done. - */ - kunit_get_resource(res); - return res; - } - return NULL; -} -EXPORT_SYMBOL_GPL(kunit_alloc_and_get_resource); +EXPORT_SYMBOL_GPL(__kunit_add_resource); void kunit_remove_resource(struct kunit *test, struct kunit_resource *res) { -- 2.35.1.1094.g7c7d902a7c-goog