Message ID | 20230710-kms-kunit-actions-rework-v1-5-722c58d72c72@kernel.org |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel-owner@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:9f45:0:b0:3ea:f831:8777 with SMTP id v5csp4856613vqx; Mon, 10 Jul 2023 00:57:36 -0700 (PDT) X-Google-Smtp-Source: APBJJlGEwW/JGaKdLMNxRIIhlfOBiRfEGqYIXM9x/9GnvtD1tfVw6a/epLr1VcT/dF5MA6wytnU1 X-Received: by 2002:a17:907:1248:b0:992:345e:831b with SMTP id wc8-20020a170907124800b00992345e831bmr11349978ejb.50.1688975855795; Mon, 10 Jul 2023 00:57:35 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1688975855; cv=none; d=google.com; s=arc-20160816; b=rmJbzt/gu8otrMCfu7bbPs+u+4kjJZWU4MsPnyfmtQSczCupgCUdKbkpYa4fFQtQww 5133yIZQpMrYR6xeLO8fAycPb4ssmjroRbo8zfvIlst2+7jQ/SD3BvzHvBSorVVu+0Ku 4F8UWKFCMLOlucsADTGE+Cvz0QqVU24z3bWZPZCzJnEYgWw8Bf8IqzsDtOpyZsDE8PWx Hwgwb1bPBhpAs2HeCE0Sjx+8BeLnPZiy80y5LODQHINiEbnA9kPvWWnkr6a1LNR2qDpu NmoJFf+KVtK1oDu6Vr7buQQWlDv2dCgbzuWjETKbUzrmrKcuibM0xKrv9Y1Je218poyc o9tg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:in-reply-to:references:message-id :content-transfer-encoding:mime-version:subject:date:from :dkim-signature; bh=vWQjUpLtxeykdfs4vmekva1wMAjnaef+UE4NO0q7UaA=; fh=hv+BGfRnZKm/Mx7Um70Asy2r1y7Zqr2lNnN/bFBL5yk=; b=Hv+FQVRP/VU0YcOKXESFO5h7tetfbCX6fHqH77Ff0bXf3VU/N1doTvqVU4fu1BvMEV tgV7jByo458L1bYiumbhTw+cHtIq60bd9VnyHzaAwD+gXEcb9f+tNqejYoxRmo1MWJkG AVf66oiVv8VWqcZdk7PmixMwtVaY4JryLKntDNufOO+nervgGhMmUsEMP50q9ZJrgnV6 DicePzIEXFFJTAb8tUR2lqoa0cz6I0uYWI5XHHdqOkZg4uba8SUupIA17TzbmNkysMcp TmhHh6Zd0aAC+d5ln1vpI9CD7+88m0zfF7En7XRJkLMe3UIiwS6vif8lm9+19mmNyLQD IMYQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=i4P+qdzQ; 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=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id h6-20020a17090634c600b00992437c5880si8436186ejb.327.2023.07.10.00.57.12; Mon, 10 Jul 2023 00:57:35 -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=@kernel.org header.s=k20201202 header.b=i4P+qdzQ; 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=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232105AbjGJHsc (ORCPT <rfc822;ybw1215001957@gmail.com> + 99 others); Mon, 10 Jul 2023 03:48:32 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50422 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232147AbjGJHsD (ORCPT <rfc822;linux-kernel@vger.kernel.org>); Mon, 10 Jul 2023 03:48:03 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E5FFCE1 for <linux-kernel@vger.kernel.org>; Mon, 10 Jul 2023 00:47:56 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 7BDA160EB6 for <linux-kernel@vger.kernel.org>; Mon, 10 Jul 2023 07:47:56 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 91087C433C9; Mon, 10 Jul 2023 07:47:55 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1688975275; bh=92Z66pevW+elalzHRPcF5q2TXp4Zof6H0dCZ1lsPh08=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=i4P+qdzQM8FjAv5I66MjcyeKFkZA8kjn/csJpN3q4yC1nyX/nQ8sSnIyBOe995Bs7 KLcKxMOH6mpoy6XfBuoZUtjgPV9dM/A2T2fAw4QfIQ580FttabPI4Z6wttGkgDzRLU ebwdtcyC1XCj51MNv0+9HMPkXro17s6FMYl63/GapMntc9e/a77l9o81d7G3+W+G7l kqEAIkRKcRE05ZcT4+mAPjYzF7z8BoE/bX296bx+ull620ecL0Usr3M2tpmQSgfIyG wdbAPjhBeOg2dNSOhEkszPTMFTRHiEcL0VXcJocqXMtcUk8iCrHp83D7btSTXJgCl3 jq7rcNGBz+TGg== From: Maxime Ripard <mripard@kernel.org> Date: Mon, 10 Jul 2023 09:47:36 +0200 Subject: [PATCH 05/11] drm/tests: helpers: Create an helper to allocate a locking ctx MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Message-Id: <20230710-kms-kunit-actions-rework-v1-5-722c58d72c72@kernel.org> References: <20230710-kms-kunit-actions-rework-v1-0-722c58d72c72@kernel.org> In-Reply-To: <20230710-kms-kunit-actions-rework-v1-0-722c58d72c72@kernel.org> To: David Airlie <airlied@gmail.com>, Daniel Vetter <daniel@ffwll.ch>, Maarten Lankhorst <maarten.lankhorst@linux.intel.com>, Thomas Zimmermann <tzimmermann@suse.de>, Emma Anholt <emma@anholt.net> Cc: dri-devel@lists.freedesktop.org, linux-kernel@vger.kernel.org, Maxime Ripard <mripard@kernel.org> X-Mailer: b4 0.13-dev-099c9 X-Developer-Signature: v=1; a=openpgp-sha256; l=2592; i=mripard@kernel.org; h=from:subject:message-id; bh=92Z66pevW+elalzHRPcF5q2TXp4Zof6H0dCZ1lsPh08=; b=owGbwMvMwCX2+D1vfrpE4FHG02pJDCmrt894fF9O9eqDwqQD4n3qmzXunui2vKk//+CEVv+FPmLF 53M2dJSyMIhxMciKKbLECJsviTs163UnG988mDmsTCBDGLg4BWAi++UZGRojfM6uzt4wI6FEZdoRae EZlryr3ZuX6gROrd9d+eUSmzLDP9OT/Pk7LjR0XbBR8ngxNyXh9d2YQzbvk3/4fxGfcJ/hECsA X-Developer-Key: i=mripard@kernel.org; a=openpgp; fpr=BE5675C37E818C8B5764241C254BCFC56BF6CE8D X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, RCVD_IN_DNSWL_BLOCKED,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE 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: <linux-kernel.vger.kernel.org> X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1771019547073948775 X-GMAIL-MSGID: 1771019547073948775 |
Series |
drm: kunit: Switch to kunit actions
|
|
Commit Message
Maxime Ripard
July 10, 2023, 7:47 a.m. UTC
As we get more and more tests, the locking context initialisation
creates more and more boilerplate, both at creation and destruction.
Let's create a helper that will allocate, initialise a context, and
register kunit actions to clean up once the test is done.
Signed-off-by: Maxime Ripard <mripard@kernel.org>
---
drivers/gpu/drm/tests/drm_kunit_helpers.c | 41 +++++++++++++++++++++++++++++++
include/drm/drm_kunit_helpers.h | 2 ++
2 files changed, 43 insertions(+)
Comments
Hi, On 2023/7/10 15:47, Maxime Ripard wrote: > As we get more and more tests, the locking context initialisation > creates more and more boilerplate, both at creation and destruction. > > Let's create a helper that will allocate, initialise a context, and > register kunit actions to clean up once the test is done. > > Signed-off-by: Maxime Ripard <mripard@kernel.org> > --- > drivers/gpu/drm/tests/drm_kunit_helpers.c | 41 +++++++++++++++++++++++++++++++ > include/drm/drm_kunit_helpers.h | 2 ++ > 2 files changed, 43 insertions(+) > > diff --git a/drivers/gpu/drm/tests/drm_kunit_helpers.c b/drivers/gpu/drm/tests/drm_kunit_helpers.c > index 38211fea9ae6..40a27c78d692 100644 > --- a/drivers/gpu/drm/tests/drm_kunit_helpers.c > +++ b/drivers/gpu/drm/tests/drm_kunit_helpers.c > @@ -124,5 +124,46 @@ __drm_kunit_helper_alloc_drm_device_with_driver(struct kunit *test, > } > EXPORT_SYMBOL_GPL(__drm_kunit_helper_alloc_drm_device_with_driver); > > +static void action_drm_release_context(void *ptr) > +{ > + struct drm_modeset_acquire_ctx *ctx = ptr; > + > + drm_modeset_drop_locks(ctx); > + drm_modeset_acquire_fini(ctx); > +} > + > +/** > + * drm_kunit_helper_context_alloc - Allocates an acquire context > + * @test: The test context object > + * > + * Allocates and initializes a modeset acquire context. > + * > + * The context is tied to the kunit test context, so we must not call > + * drm_modeset_acquire_fini() on it, it will be done so automatically. > + * > + * Returns: > + * An ERR_PTR on error, a pointer to the newly allocated context otherwise > + */ > +struct drm_modeset_acquire_ctx * > +drm_kunit_helper_acquire_ctx_alloc(struct kunit *test) > +{ > + struct drm_modeset_acquire_ctx *ctx; > + int ret; > + > + ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL); > + KUNIT_ASSERT_NOT_NULL(test, ctx); > + > + drm_modeset_acquire_init(ctx, 0); > + > + ret = kunit_add_action_or_reset(test, > + action_drm_release_context, > + ctx); > + if (ret) > + return ERR_PTR(ret); > + > + return ctx; > +} > +EXPORT_SYMBOL_GPL(drm_kunit_helper_acquire_ctx_alloc); > + I think all of the patch inside this series are quite well. Personally, I can't find problems in it. But I still want to ask a question: Should the managed functions you introduced be prefixed with drmm_ (instead of drm_) ? As mindless programmer may still want to call drm_modeset_acquire_fini() on the pointer returned by drm_kunit_helper_acquire_ctx_alloc()? > MODULE_AUTHOR("Maxime Ripard <maxime@cerno.tech>"); > MODULE_LICENSE("GPL"); > diff --git a/include/drm/drm_kunit_helpers.h b/include/drm/drm_kunit_helpers.h > index ed013fdcc1ff..4ba5e10653c6 100644 > --- a/include/drm/drm_kunit_helpers.h > +++ b/include/drm/drm_kunit_helpers.h > @@ -87,5 +87,7 @@ __drm_kunit_helper_alloc_drm_device(struct kunit *test, > sizeof(_type), \ > offsetof(_type, _member), \ > _feat)) > +struct drm_modeset_acquire_ctx * > +drm_kunit_helper_acquire_ctx_alloc(struct kunit *test); > > #endif // DRM_KUNIT_HELPERS_H_
Hi, On 2023/7/10 15:47, Maxime Ripard wrote: > As we get more and more tests, the locking context initialisation > creates more and more boilerplate, both at creation and destruction. > > Let's create a helper that will allocate, initialise a context, and > register kunit actions to clean up once the test is done. > > Signed-off-by: Maxime Ripard <mripard@kernel.org> > --- > drivers/gpu/drm/tests/drm_kunit_helpers.c | 41 +++++++++++++++++++++++++++++++ > include/drm/drm_kunit_helpers.h | 2 ++ > 2 files changed, 43 insertions(+) > > diff --git a/drivers/gpu/drm/tests/drm_kunit_helpers.c b/drivers/gpu/drm/tests/drm_kunit_helpers.c > index 38211fea9ae6..40a27c78d692 100644 > --- a/drivers/gpu/drm/tests/drm_kunit_helpers.c > +++ b/drivers/gpu/drm/tests/drm_kunit_helpers.c > @@ -124,5 +124,46 @@ __drm_kunit_helper_alloc_drm_device_with_driver(struct kunit *test, > } > EXPORT_SYMBOL_GPL(__drm_kunit_helper_alloc_drm_device_with_driver); > > +static void action_drm_release_context(void *ptr) > +{ > + struct drm_modeset_acquire_ctx *ctx = ptr; > + > + drm_modeset_drop_locks(ctx); > + drm_modeset_acquire_fini(ctx); > +} > + > +/** > + * drm_kunit_helper_context_alloc - Allocates an acquire context > + * @test: The test context object > + * > + * Allocates and initializes a modeset acquire context. > + * > + * The context is tied to the kunit test context, so we must not call > + * drm_modeset_acquire_fini() on it, it will be done so automatically. > + * > + * Returns: > + * An ERR_PTR on error, a pointer to the newly allocated context otherwise > + */ > +struct drm_modeset_acquire_ctx * > +drm_kunit_helper_acquire_ctx_alloc(struct kunit *test) > +{ > + struct drm_modeset_acquire_ctx *ctx; > + int ret; > + > + ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL); Because kunit_kzalloc() is also managed, Is there any possibility that kfree(ctx) get called before action_drm_release_context(ctx) ? Currently, I can't find where the order is guaranteed. > + KUNIT_ASSERT_NOT_NULL(test, ctx); > + > + drm_modeset_acquire_init(ctx, 0); > + > + ret = kunit_add_action_or_reset(test, > + action_drm_release_context, > + ctx); > + if (ret) > + return ERR_PTR(ret); > + > + return ctx; > +} > +EXPORT_SYMBOL_GPL(drm_kunit_helper_acquire_ctx_alloc); > + > MODULE_AUTHOR("Maxime Ripard <maxime@cerno.tech>"); > MODULE_LICENSE("GPL"); > diff --git a/include/drm/drm_kunit_helpers.h b/include/drm/drm_kunit_helpers.h > index ed013fdcc1ff..4ba5e10653c6 100644 > --- a/include/drm/drm_kunit_helpers.h > +++ b/include/drm/drm_kunit_helpers.h > @@ -87,5 +87,7 @@ __drm_kunit_helper_alloc_drm_device(struct kunit *test, > sizeof(_type), \ > offsetof(_type, _member), \ > _feat)) > +struct drm_modeset_acquire_ctx * > +drm_kunit_helper_acquire_ctx_alloc(struct kunit *test); > > #endif // DRM_KUNIT_HELPERS_H_
Maxime Ripard <mripard@kernel.org> writes: > As we get more and more tests, the locking context initialisation > creates more and more boilerplate, both at creation and destruction. > > Let's create a helper that will allocate, initialise a context, and > register kunit actions to clean up once the test is done. > > Signed-off-by: Maxime Ripard <mripard@kernel.org> > --- Reviewed-by: Javier Martinez Canillas <javierm@redhat.com>
suijingfeng <suijingfeng@loongson.cn> writes: > Hi, > > On 2023/7/10 15:47, Maxime Ripard wrote: [...] >> + >> +/** >> + * drm_kunit_helper_context_alloc - Allocates an acquire context >> + * @test: The test context object >> + * >> + * Allocates and initializes a modeset acquire context. >> + * >> + * The context is tied to the kunit test context, so we must not call >> + * drm_modeset_acquire_fini() on it, it will be done so automatically. >> + * >> + * Returns: >> + * An ERR_PTR on error, a pointer to the newly allocated context otherwise >> + */ >> +struct drm_modeset_acquire_ctx * >> +drm_kunit_helper_acquire_ctx_alloc(struct kunit *test) >> +{ >> + struct drm_modeset_acquire_ctx *ctx; >> + int ret; >> + >> + ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL); >> + KUNIT_ASSERT_NOT_NULL(test, ctx); >> + >> + drm_modeset_acquire_init(ctx, 0); >> + >> + ret = kunit_add_action_or_reset(test, >> + action_drm_release_context, >> + ctx); >> + if (ret) >> + return ERR_PTR(ret); >> + >> + return ctx; >> +} >> +EXPORT_SYMBOL_GPL(drm_kunit_helper_acquire_ctx_alloc); >> + > > I think all of the patch inside this series are quite well. > > Personally, I can't find problems in it. > > > But I still want to ask a question: > > Should the managed functions you introduced be prefixed with drmm_ > (instead of drm_) ? > That's a good question. But personally I think that the drmm_ prefix should be reserved for drm_device managed resources and helpers. > As mindless programmer may still want to call drm_modeset_acquire_fini() > on the pointer returned by > > drm_kunit_helper_acquire_ctx_alloc()? > The function kernel-doc already mentions that there's no need to do that and that will be done automatically by kunit. So shouldn't be different of other functions helper where the programmer didn't read the documentation.
suijingfeng <suijingfeng@loongson.cn> writes: > Hi, > > On 2023/7/10 15:47, Maxime Ripard wrote: >> As we get more and more tests, the locking context initialisation [...] >> +/** >> + * drm_kunit_helper_context_alloc - Allocates an acquire context >> + * @test: The test context object >> + * >> + * Allocates and initializes a modeset acquire context. >> + * >> + * The context is tied to the kunit test context, so we must not call >> + * drm_modeset_acquire_fini() on it, it will be done so automatically. >> + * >> + * Returns: >> + * An ERR_PTR on error, a pointer to the newly allocated context otherwise >> + */ >> +struct drm_modeset_acquire_ctx * >> +drm_kunit_helper_acquire_ctx_alloc(struct kunit *test) >> +{ >> + struct drm_modeset_acquire_ctx *ctx; >> + int ret; >> + >> + ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL); > > Because kunit_kzalloc() is also managed, > > Is there any possibility that kfree(ctx) get called before > action_drm_release_context(ctx) ? > > Currently, I can't find where the order is guaranteed. > It isn't documented indeed in Documentation/dev-tools/kunit/usage.rst but the kunit_add_action() kernel-doc says: "All functions registered with kunit_add_action() will execute in the opposite order to that they were registered in". And now that kunit_kzalloc() and friends are also implemented using the cleanup actions, it will be part of that execution chain. Probably the kunit docs can make this more clear.
On Wed, Jul 19, 2023 at 09:12:14PM +0200, Javier Martinez Canillas wrote: > suijingfeng <suijingfeng@loongson.cn> writes: > > > Hi, > > > > On 2023/7/10 15:47, Maxime Ripard wrote: > > [...] > > >> + > >> +/** > >> + * drm_kunit_helper_context_alloc - Allocates an acquire context > >> + * @test: The test context object > >> + * > >> + * Allocates and initializes a modeset acquire context. > >> + * > >> + * The context is tied to the kunit test context, so we must not call > >> + * drm_modeset_acquire_fini() on it, it will be done so automatically. > >> + * > >> + * Returns: > >> + * An ERR_PTR on error, a pointer to the newly allocated context otherwise > >> + */ > >> +struct drm_modeset_acquire_ctx * > >> +drm_kunit_helper_acquire_ctx_alloc(struct kunit *test) > >> +{ > >> + struct drm_modeset_acquire_ctx *ctx; > >> + int ret; > >> + > >> + ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL); > >> + KUNIT_ASSERT_NOT_NULL(test, ctx); > >> + > >> + drm_modeset_acquire_init(ctx, 0); > >> + > >> + ret = kunit_add_action_or_reset(test, > >> + action_drm_release_context, > >> + ctx); > >> + if (ret) > >> + return ERR_PTR(ret); > >> + > >> + return ctx; > >> +} > >> +EXPORT_SYMBOL_GPL(drm_kunit_helper_acquire_ctx_alloc); > >> + > > > > I think all of the patch inside this series are quite well. > > > > Personally, I can't find problems in it. > > > > > > But I still want to ask a question: > > > > Should the managed functions you introduced be prefixed with drmm_ > > (instead of drm_) ? > > > > That's a good question. But personally I think that the drmm_ prefix > should be reserved for drm_device managed resources and helpers. Yeah, to me drmm functions are meant for resources that are tied to the DRM device lifetime. These resources are tied to the test lifetime, so they shouldn't share the same prefix. > > As mindless programmer may still want to call drm_modeset_acquire_fini() > > on the pointer returned by > > > > drm_kunit_helper_acquire_ctx_alloc()? > > > > The function kernel-doc already mentions that there's no need to do that > and that will be done automatically by kunit. So shouldn't be different of > other functions helper where the programmer didn't read the documentation. Right Maxime
Hi, On 2023/7/20 18:07, Maxime Ripard wrote: > On Wed, Jul 19, 2023 at 09:12:14PM +0200, Javier Martinez Canillas wrote: >> suijingfeng <suijingfeng@loongson.cn> writes: >> >>> Hi, >>> >>> On 2023/7/10 15:47, Maxime Ripard wrote: >> [...] >> >>>> + >>>> +/** >>>> + * drm_kunit_helper_context_alloc - Allocates an acquire context >>>> + * @test: The test context object >>>> + * >>>> + * Allocates and initializes a modeset acquire context. >>>> + * >>>> + * The context is tied to the kunit test context, so we must not call >>>> + * drm_modeset_acquire_fini() on it, it will be done so automatically. >>>> + * >>>> + * Returns: >>>> + * An ERR_PTR on error, a pointer to the newly allocated context otherwise >>>> + */ >>>> +struct drm_modeset_acquire_ctx * >>>> +drm_kunit_helper_acquire_ctx_alloc(struct kunit *test) >>>> +{ >>>> + struct drm_modeset_acquire_ctx *ctx; >>>> + int ret; >>>> + >>>> + ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL); >>>> + KUNIT_ASSERT_NOT_NULL(test, ctx); >>>> + >>>> + drm_modeset_acquire_init(ctx, 0); >>>> + >>>> + ret = kunit_add_action_or_reset(test, >>>> + action_drm_release_context, >>>> + ctx); >>>> + if (ret) >>>> + return ERR_PTR(ret); >>>> + >>>> + return ctx; >>>> +} >>>> +EXPORT_SYMBOL_GPL(drm_kunit_helper_acquire_ctx_alloc); >>>> + >>> I think all of the patch inside this series are quite well. >>> >>> Personally, I can't find problems in it. >>> >>> >>> But I still want to ask a question: >>> >>> Should the managed functions you introduced be prefixed with drmm_ >>> (instead of drm_) ? >>> >> That's a good question. But personally I think that the drmm_ prefix >> should be reserved for drm_device managed resources and helpers. > Yeah, to me drmm functions are meant for resources that are tied to the > DRM device lifetime. These resources are tied to the test lifetime, so > they shouldn't share the same prefix. Okay then, Thanks for reply. >>> As mindless programmer may still want to call drm_modeset_acquire_fini() >>> on the pointer returned by >>> >>> drm_kunit_helper_acquire_ctx_alloc()? >>> >> The function kernel-doc already mentions that there's no need to do that >> and that will be done automatically by kunit. So shouldn't be different of >> other functions helper where the programmer didn't read the documentation. > Right > > Maxime
diff --git a/drivers/gpu/drm/tests/drm_kunit_helpers.c b/drivers/gpu/drm/tests/drm_kunit_helpers.c index 38211fea9ae6..40a27c78d692 100644 --- a/drivers/gpu/drm/tests/drm_kunit_helpers.c +++ b/drivers/gpu/drm/tests/drm_kunit_helpers.c @@ -124,5 +124,46 @@ __drm_kunit_helper_alloc_drm_device_with_driver(struct kunit *test, } EXPORT_SYMBOL_GPL(__drm_kunit_helper_alloc_drm_device_with_driver); +static void action_drm_release_context(void *ptr) +{ + struct drm_modeset_acquire_ctx *ctx = ptr; + + drm_modeset_drop_locks(ctx); + drm_modeset_acquire_fini(ctx); +} + +/** + * drm_kunit_helper_context_alloc - Allocates an acquire context + * @test: The test context object + * + * Allocates and initializes a modeset acquire context. + * + * The context is tied to the kunit test context, so we must not call + * drm_modeset_acquire_fini() on it, it will be done so automatically. + * + * Returns: + * An ERR_PTR on error, a pointer to the newly allocated context otherwise + */ +struct drm_modeset_acquire_ctx * +drm_kunit_helper_acquire_ctx_alloc(struct kunit *test) +{ + struct drm_modeset_acquire_ctx *ctx; + int ret; + + ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL); + KUNIT_ASSERT_NOT_NULL(test, ctx); + + drm_modeset_acquire_init(ctx, 0); + + ret = kunit_add_action_or_reset(test, + action_drm_release_context, + ctx); + if (ret) + return ERR_PTR(ret); + + return ctx; +} +EXPORT_SYMBOL_GPL(drm_kunit_helper_acquire_ctx_alloc); + MODULE_AUTHOR("Maxime Ripard <maxime@cerno.tech>"); MODULE_LICENSE("GPL"); diff --git a/include/drm/drm_kunit_helpers.h b/include/drm/drm_kunit_helpers.h index ed013fdcc1ff..4ba5e10653c6 100644 --- a/include/drm/drm_kunit_helpers.h +++ b/include/drm/drm_kunit_helpers.h @@ -87,5 +87,7 @@ __drm_kunit_helper_alloc_drm_device(struct kunit *test, sizeof(_type), \ offsetof(_type, _member), \ _feat)) +struct drm_modeset_acquire_ctx * +drm_kunit_helper_acquire_ctx_alloc(struct kunit *test); #endif // DRM_KUNIT_HELPERS_H_