Received: by 2002:a05:6a10:2726:0:0:0:0 with SMTP id ib38csp2846931pxb; Sat, 26 Mar 2022 05:48:45 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxvXKi4d8y8oBD+vtv3X2uX2abbqkKwrLIqaRoILDL+7Imk0wTPUG2RFOeMYH+/jDBkk8JN X-Received: by 2002:a17:90b:1d03:b0:1c7:ae9f:c35e with SMTP id on3-20020a17090b1d0300b001c7ae9fc35emr18046432pjb.60.1648298925616; Sat, 26 Mar 2022 05:48:45 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1648298925; cv=none; d=google.com; s=arc-20160816; b=0zxqDx0lhLHzwkv1DLka+GxCgxYvAbohLPdxjd4m+uHNxypY83EymWMre1eUpmksWg PxFzTZ8+zy299XuqPl+zgeXsBHFg9YPDkpbpjfCv8GjCNMAcuKDcSEAq/kIFWkjIiZaq Yvny7EDPyXRvkOBBHL/ljESrMtexV07fzfhlROhS/zVW4ztOgacedJWymsdICKBeftHx g/AESnIrGYP5JHt3un1HN6+bFdOkGy9HA9sjNPCSQg2gq5JceCAXQvBUaW9vAQFCi4a8 +rWnAxr+TuG9+ayhTrxK/F0+lTiLT2LrEIyr/LxtCxmTeQIdbnTIQTMne6Hrm61bhdrk kJeA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=wXjQomr6Ua4s3PfVrkfMx69oLfa65TadzK4GjLZb12k=; b=hINQ9xBFwlgW0DMfY8K9Ret7VryMmS89PpwYvnUHcsDFsEXzJj5c93pvV0TyWzWPEQ d/8YhFYmVwAWEPqNWckY15223zCyYbNsgFtqwUtpRKMLMpOMQZq0GJT9YyuYNBVA2zDW qfJSGTCIa/5zWMsWA4tJlIMSJRizHtJMoBagdnZM9/Z6sMVXFoLEoOGdY0FgVnredIzA eeaRg4lBAzH5LN4xnlz5NMdvJDcjscsOOlOFR6SN2IzU3zNBReBHoSL/QMQzzw9t4gWe MoEHhed1378NPSeh81CMWS9ru2IjHlcVQHcDyUMxD0EkYguMyYvI69gIpiCHBhs+G5YK uQKQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=Uoan5FLW; 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 c16-20020a170902d49000b00155c9e8029esi1075789plg.403.2022.03.26.05.48.32; Sat, 26 Mar 2022 05:48:45 -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=Uoan5FLW; 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 S230307AbiCZE3h (ORCPT + 99 others); Sat, 26 Mar 2022 00:29:37 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49966 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229832AbiCZE3f (ORCPT ); Sat, 26 Mar 2022 00:29:35 -0400 Received: from mail-wm1-x32f.google.com (mail-wm1-x32f.google.com [IPv6:2a00:1450:4864:20::32f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3559F5A09A for ; Fri, 25 Mar 2022 21:27:59 -0700 (PDT) Received: by mail-wm1-x32f.google.com with SMTP id i67-20020a1c3b46000000b0038ce25c870dso1916880wma.1 for ; Fri, 25 Mar 2022 21:27:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=wXjQomr6Ua4s3PfVrkfMx69oLfa65TadzK4GjLZb12k=; b=Uoan5FLW5aJ/AKTslmBZagRi1tYDmzDv3sYMU5/xS7rsUTI7XhkwrLKETpfoO2H14w ou3v9kWudprahOUJ9UyJzbJROUgCsdwSyG7EHIYJlPycrLpREbwIZAd4jZ8MrtaXwupF zw7MxWk+AZmV6vXxZBQWNITt5rdYU7IRVm1IRyMpch2DGWG0D14sYASm+lp+l8rFjHWP Qmpbi2IvIK5/wu8yO94ERx90iwnexfL+lN7poY6I8tL8QcWHwxLZ9aUeVwk54XI33Oxm ItsI5z5qoYe/t5zLXR+8qqD/RBzMxDR6hR9Ri2sgEpmWrvaa+PACFDgDVeeLBg3+mnm8 YsSg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=wXjQomr6Ua4s3PfVrkfMx69oLfa65TadzK4GjLZb12k=; b=uAN2cEWv1Zx7V7xJPEUbbliVjW7dX3efDGyYuGKuW9uAyPTft8hmMDfnHjLe0Vx45X m6X0yuG+rPgZ1HSjct7RTfsh99P7H4e7tAtm5mpyNbs91rd2IvjidImiigDVkMe7+HQr E2Nh4Iy1fWXm/NJXcupDaoY2udocVCjPWX9fhBhE55OBd7DhrxFcC9B7SuUj3B7wu6vC MKD+zC4x2AutbEAVrfOIwSEHHRuemtR4t0Yxn6OGTAyyCQADVsbuvGvvQfW2KX880oLO BQfBDp5gQjh6MNLOw/XCKPIDk0vRMyqC2yNFylCc72DUaZmo2BGxLkKYRD5qQTB0l4rg QdOg== X-Gm-Message-State: AOAM530R78Ob/zfBeUlo0UIKabRlvPCNo5qFrAl6cuDoVs3suBOe0zrD XyZ7BruzqhIB69BLsRIVxrN7tqXE9gNToTAvi5rBFA== X-Received: by 2002:a05:600c:4f48:b0:38c:a460:cb6 with SMTP id m8-20020a05600c4f4800b0038ca4600cb6mr13577299wmq.96.1648268877617; Fri, 25 Mar 2022 21:27:57 -0700 (PDT) MIME-Version: 1.0 References: <20220326002013.483394-1-dlatypov@google.com> <20220326002013.483394-2-dlatypov@google.com> In-Reply-To: <20220326002013.483394-2-dlatypov@google.com> From: David Gow Date: Sat, 26 Mar 2022 12:27:46 +0800 Message-ID: Subject: Re: [PATCH v2 2/2] kunit: split resource API impl from test.c into new resource.c To: Daniel Latypov Cc: Brendan Higgins , Linux Kernel Mailing List , KUnit Development , "open list:KERNEL SELFTEST FRAMEWORK" , Shuah Khan Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-17.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, ENV_AND_HDR_SPF_MATCH,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE,USER_IN_DEF_DKIM_WL,USER_IN_DEF_SPF_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 On Sat, Mar 26, 2022 at 8:20 AM Daniel Latypov wrote: > > We've split out the declarations from include/kunit/test.h into > resource.h. > This patch splits out the definitions as well for consistency. > > A side effect of this is git blame won't properly track history by > default, users need to run > $ git blame -L ,1 -C13 lib/kunit/resource.c > > Signed-off-by: Daniel Latypov > --- Looks good and works fine here. I'm going to try to rebase most of the other resource system stuff I'm working on on top of these (which will likely end up moving a bunch of code _again_, but is probably the least terrible of all the available options). One nitpick (newline at end of file) below, otherwise this is good. Reviewed-by: David Gow Cheers, -- David > lib/kunit/Makefile | 1 + > lib/kunit/resource.c | 126 +++++++++++++++++++++++++++++++++++++++++++ > lib/kunit/test.c | 116 +-------------------------------------- > 3 files changed, 128 insertions(+), 115 deletions(-) > create mode 100644 lib/kunit/resource.c > > diff --git a/lib/kunit/Makefile b/lib/kunit/Makefile > index c49f4ffb6273..29aff6562b42 100644 > --- a/lib/kunit/Makefile > +++ b/lib/kunit/Makefile > @@ -1,6 +1,7 @@ > obj-$(CONFIG_KUNIT) += kunit.o > > kunit-objs += test.o \ > + resource.o \ > string-stream.o \ > assert.o \ > try-catch.o \ > diff --git a/lib/kunit/resource.c b/lib/kunit/resource.c > new file mode 100644 > index 000000000000..b8bced246217 > --- /dev/null > +++ b/lib/kunit/resource.c > @@ -0,0 +1,126 @@ > +// SPDX-License-Identifier: GPL-2.0 > +/* > + * KUnit resource API for test managed resources (allocations, etc.). > + * > + * Copyright (C) 2022, Google LLC. > + * Author: Daniel Latypov > + */ > + > +#include > +#include > +#include > + > +/* > + * 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. > + */ > +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; > + > + res->free = free; > + kref_init(&res->refcount); > + > + if (init) { > + ret = init(res, data); > + if (ret) > + return ret; > + } else { > + res->data = data; > + } > + > + spin_lock_irqsave(&test->lock, flags); > + list_add_tail(&res->node, &test->resources); > + /* refcount for list is established by kref_init() */ > + spin_unlock_irqrestore(&test->lock, flags); > + > + 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); > + > +void kunit_remove_resource(struct kunit *test, struct kunit_resource *res) > +{ > + unsigned long flags; > + > + spin_lock_irqsave(&test->lock, flags); > + list_del(&res->node); > + spin_unlock_irqrestore(&test->lock, flags); > + kunit_put_resource(res); > +} > +EXPORT_SYMBOL_GPL(kunit_remove_resource); > + > +int kunit_destroy_resource(struct kunit *test, kunit_resource_match_t match, > + void *match_data) > +{ > + struct kunit_resource *res = kunit_find_resource(test, match, > + match_data); > + > + if (!res) > + return -ENOENT; > + > + kunit_remove_resource(test, res); > + > + /* We have a reference also via _find(); drop it. */ > + kunit_put_resource(res); > + > + return 0; > +} > +EXPORT_SYMBOL_GPL(kunit_destroy_resource); > + .git/rebase-apply/patch:151: new blank line at EOF. + > diff --git a/lib/kunit/test.c b/lib/kunit/test.c > index 3bca3bf5c15b..0f66c13d126e 100644 > --- a/lib/kunit/test.c > +++ b/lib/kunit/test.c > @@ -6,10 +6,10 @@ > * Author: Brendan Higgins > */ > > +#include > #include > #include > #include > -#include > #include > #include > #include > @@ -592,120 +592,6 @@ void __kunit_test_suites_exit(struct kunit_suite **suites) > } > EXPORT_SYMBOL_GPL(__kunit_test_suites_exit); > > -/* > - * 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. > - */ > -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; > - > - res->free = free; > - kref_init(&res->refcount); > - > - if (init) { > - ret = init(res, data); > - if (ret) > - return ret; > - } else { > - res->data = data; > - } > - > - spin_lock_irqsave(&test->lock, flags); > - list_add_tail(&res->node, &test->resources); > - /* refcount for list is established by kref_init() */ > - spin_unlock_irqrestore(&test->lock, flags); > - > - 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); > - > -void kunit_remove_resource(struct kunit *test, struct kunit_resource *res) > -{ > - unsigned long flags; > - > - spin_lock_irqsave(&test->lock, flags); > - list_del(&res->node); > - spin_unlock_irqrestore(&test->lock, flags); > - kunit_put_resource(res); > -} > -EXPORT_SYMBOL_GPL(kunit_remove_resource); > - > -int kunit_destroy_resource(struct kunit *test, kunit_resource_match_t match, > - void *match_data) > -{ > - struct kunit_resource *res = kunit_find_resource(test, match, > - match_data); > - > - if (!res) > - return -ENOENT; > - > - kunit_remove_resource(test, res); > - > - /* We have a reference also via _find(); drop it. */ > - kunit_put_resource(res); > - > - return 0; > -} > -EXPORT_SYMBOL_GPL(kunit_destroy_resource); > - > struct kunit_kmalloc_array_params { > size_t n; > size_t size; > -- > 2.35.1.1021.g381101b075-goog >