Received: by 2002:a05:7412:251c:b0:e2:908c:2ebd with SMTP id w28csp2410800rda; Wed, 25 Oct 2023 01:43:36 -0700 (PDT) X-Google-Smtp-Source: AGHT+IG14mW/EbhfrZ8ig247/guO3aB3g+uPBzD5MENqcCwxbI25C0OtctTUHxfS08Z4/NUwemTy X-Received: by 2002:a0d:d448:0:b0:5a7:d997:cc7b with SMTP id w69-20020a0dd448000000b005a7d997cc7bmr15441027ywd.23.1698223415696; Wed, 25 Oct 2023 01:43:35 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1698223415; cv=none; d=google.com; s=arc-20160816; b=Mk7cGIn5y2aS6+IHqkJhz5qpicOHk/VqezOqLopw+xvDYwmCFsP3EglfFQOmX/oVIa K5IdGTFnR6ydMewiOY/9Gt7ih2zh8xj0ynXFEoizspsMA5ffSLLdpmG4MBdtZ8GeLXEq twe7q7VaDGIM7H8V45qcyI/yxuogqLTdbKLrirud6/9wCMITlsO+Nnvghw10CjaDYF7s mz7HaEMfgZN6HJvdhHGz6dBDzW/vizYwGBrB5krXGJwBrprGlzBHEzFZ1Sw08OreMNqN Je5RIOFYFltArCPMvOW/MDI54dHAZHzKW3pB1s+5JoeF5REMTONY+51DvFinKA+P+PLB 8LLg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=p2BcG8XiV1v17ipx/YQFQMsQMvyoqL7Z3G/jbC08Je0=; fh=WcnaHuFRrOM3a1xNAwbDkYG6X69JWktIEB/yAmGSm+I=; b=qNa4nWUxxqrvV4UNP/+icnYhH7+I9WwuTn5nPv/nwLNDB4dHN+Fn7qBEfakq4iZtQ1 0mmumkpsvWMXne7ab8COsRR59ZgFsn6WctBxwbOnClUkTvWtMfEqVZHUU3woOBFPvp// atWMP46NsuRIlJmehQz9fu786uiimCN/U6DgiKF7nfE1k1E9W0ReobzLny5FJDIhefTy J9uT4AYL9cWSMaMXhniCQh3sBldaL9jLlHEuu0duyqLGxv2f0Sr08zJHhmstGtfylAQL nbqji7H44I6LxpTvS3Xalg7BtDZvjlzJj4PjwlzXgt/VoAGyIEhI7TWe6FMOKPUX42dM qjKA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=bGX6ec8k; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from lipwig.vger.email (lipwig.vger.email. [23.128.96.33]) by mx.google.com with ESMTPS id u71-20020a81844a000000b0059c21d53f92si9863451ywf.427.2023.10.25.01.43.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 25 Oct 2023 01:43:35 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 as permitted sender) client-ip=23.128.96.33; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=bGX6ec8k; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 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 (depot.vger.email [IPv6:2620:137:e000::3:0]) by lipwig.vger.email (Postfix) with ESMTP id 6DA2480A13A2; Wed, 25 Oct 2023 01:43:31 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at lipwig.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233930AbjJYInW (ORCPT + 99 others); Wed, 25 Oct 2023 04:43:22 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35848 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232807AbjJYInV (ORCPT ); Wed, 25 Oct 2023 04:43:21 -0400 Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 972FF9C; Wed, 25 Oct 2023 01:43:19 -0700 (PDT) Received: by smtp.kernel.org (Postfix) with ESMTPSA id E0E48C433C7; Wed, 25 Oct 2023 08:43:18 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1698223399; bh=0zZhtkrYhPXwaPPhoU0Ao0T3LN1o1SJTsWAiRLH+OtY=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=bGX6ec8k8faIE72JDouiLWwMaguSXqR0yLi1tItQC4S4F4CekXA1hPt7C79hE9a6F PGbTOEGvkD3efKyOw4DroIi4UMl/D9PEFyWGgtPJTTOJt9Ujb4qL+pge+uAWvmmPCf 4PCNB3Vsc3OHFCdRiLc9t0gQna0xczLbN1LNdnJfoSQr/ALqiL7ClNmCn2ykSZWH8H t3XVEym8yUOz7lmr6NLHYX7BbG3OEoojzIWBUpek3DyCFamqBDn1cGJaD1g7G+YEMU zi5ETf/Na9fv6+UWt25ZuKxKwUJEHrmnAC9+rhXe2er4nGF6SFErv1TVlLYvN1pay5 sCbXdZYdLp7Bw== Date: Wed, 25 Oct 2023 10:43:16 +0200 From: Maxime Ripard To: Marco Pagani Cc: Maarten Lankhorst , Thomas Zimmermann , David Airlie , Daniel Vetter , Sumit Semwal , Christian Koenig , linux-kernel@vger.kernel.org, dri-devel@lists.freedesktop.org, linux-media@vger.kernel.org, linaro-mm-sig@lists.linaro.org Subject: Re: [RFC PATCH] drm/test: add a test suite for GEM objects backed by shmem Message-ID: References: <20231023164541.92913-1-marpagan@redhat.com> <789aaf2b-4d68-4128-b8ff-c1ba4849e141@redhat.com> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha512; protocol="application/pgp-signature"; boundary="tcuh3gisjtvmugdz" Content-Disposition: inline In-Reply-To: <789aaf2b-4d68-4128-b8ff-c1ba4849e141@redhat.com> X-Spam-Status: No, score=-1.2 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lipwig.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (lipwig.vger.email [0.0.0.0]); Wed, 25 Oct 2023 01:43:31 -0700 (PDT) --tcuh3gisjtvmugdz Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable Hi, On Tue, Oct 24, 2023 at 07:14:25PM +0200, Marco Pagani wrote: > >> +static void drm_gem_shmem_test_obj_create_private(struct kunit *test) > >> +{ > >> + struct fake_dev *fdev =3D test->priv; > >> + struct drm_gem_shmem_object *shmem; > >> + struct drm_gem_object *gem_obj; > >> + struct dma_buf buf_mock; > >> + struct dma_buf_attachment attach_mock; > >> + struct sg_table *sgt; > >> + char *buf; > >> + int ret; > >> + > >> + /* Create a mock scatter/gather table */ > >> + buf =3D kunit_kzalloc(test, TEST_SIZE, GFP_KERNEL); > >> + KUNIT_ASSERT_NOT_NULL(test, buf); > >> + > >> + sgt =3D kzalloc(sizeof(*sgt), GFP_KERNEL); > >> + KUNIT_ASSERT_NOT_NULL(test, sgt); > >> + > >> + ret =3D sg_alloc_table(sgt, 1, GFP_KERNEL); > >> + KUNIT_ASSERT_EQ(test, ret, 0); > >> + sg_init_one(sgt->sgl, buf, TEST_SIZE); > >> + > >> + /* Init a mock DMA-BUF */ > >> + buf_mock.size =3D TEST_SIZE; > >> + attach_mock.dmabuf =3D &buf_mock; > >> + > >> + gem_obj =3D drm_gem_shmem_prime_import_sg_table(&fdev->drm_dev, &att= ach_mock, sgt); > >> + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, gem_obj); > >> + KUNIT_ASSERT_EQ(test, gem_obj->size, TEST_SIZE); > >> + KUNIT_ASSERT_NULL(test, gem_obj->filp); > >> + KUNIT_ASSERT_NOT_NULL(test, gem_obj->funcs); > >> + > >> + shmem =3D to_drm_gem_shmem_obj(gem_obj); > >> + KUNIT_ASSERT_PTR_EQ(test, shmem->sgt, sgt); > >> + > >> + /* The scatter/gather table is freed by drm_gem_shmem_free */ > >> + drm_gem_shmem_free(shmem); > >> +} > >=20 > > KUNIT_ASSERT_* will stop the execution of the test on failure, you > > should probably use a bit more of KUNIT_EXPECT_* calls otherwise you'll > > leak resources. > >=20 > > You also probably want to use a kunit_action to clean up and avoid that > > whole discussion > > >=20 > You are right. I slightly prefer using KUnit expectations (unless actions > are strictly necessary) since I feel using actions makes test cases a bit > less straightforward to understand. Is this okay for you? I disagree. Actions make it easier to reason about, even when comparing assertion vs expectation Like, for the call to sg_alloc_table and drm_gem_shmem_prime_import_sg_table(), the reasonable use of assert vs expect would be something like: sgt =3D kzalloc(sizeof(*sgt), GFP_KERNEL); KUNIT_ASSERT_NOT_NULL(test, sgt); ret =3D sg_alloc_table(sgt, 1, GFP_KERNEL); KUNIT_ASSERT_EQ(test, ret, 0); /* * Here, it's already not super clear whether you want to expect vs * assert. expect will make you handle the failure case later, assert will * force you to call kfree on sgt. Both kind of suck in their own ways. */ sg_init_one(sgt->sgl, buf, TEST_SIZE); gem_obj =3D drm_gem_shmem_prime_import_sg_table(&fdev->drm_dev, &attach_moc= k, sgt); KUNIT_ASSERT_NOT_ERR_OR_NULL(test, gem_obj); /* * If the assert fails, we forgot to call sg_free_table(sgt) and kfree(sgt). */ KUNIT_EXPECT_EQ(test, gem_obj->size, TEST_SIZE); KUNIT_EXPECT_NULL(test, gem_obj->filp); KUNIT_EXPECT_NOT_NULL(test, gem_obj->funcs); /* * And here we have to handle the case where the expectation was wrong, * but the test still continued. */ But if you're not using an action, you still have to call kfree(sgt), which means that you might still shmem =3D to_drm_gem_shmem_obj(gem_obj); KUNIT_ASSERT_PTR_EQ(test, shmem->sgt, sgt); /* * If the assertion fails, we now have to call drm_gem_shmem_free(shmem) */ /* The scatter/gather table is freed by drm_gem_shmem_free */ drm_gem_shmem_free(shmem); /* everything's fine now */ The semantics around drm_gem_shmem_free make it a bit convoluted, but doing it using goto/labels, plus handling the assertions and error reporting would be difficult. Using actions, we have: sgt =3D kzalloc(sizeof(*sgt), GFP_KERNEL); KUNIT_ASSERT_NOT_NULL(test, sgt); ret =3D kunit_add_action_or_reset(test, kfree_wrapper, sgt); KUNIT_ASSERT_EQ(test, ret, 0); ret =3D sg_alloc_table(sgt, 1, GFP_KERNEL); KUNIT_ASSERT_EQ(test, ret, 0); ret =3D kunit_add_action_or_reset(test, sg_free_table_wrapper, sgt); KUNIT_ASSERT_EQ(test, ret, 0); sg_init_one(sgt->sgl, buf, TEST_SIZE); gem_obj =3D drm_gem_shmem_prime_import_sg_table(&fdev->drm_dev, &attach_moc= k, sgt); KUNIT_ASSERT_NOT_ERR_OR_NULL(test, gem_obj); KUNIT_EXPECT_EQ(test, gem_obj->size, TEST_SIZE); KUNIT_EXPECT_NULL(test, gem_obj->filp); KUNIT_EXPECT_NOT_NULL(test, gem_obj->funcs); /* drm_gem_shmem_free will free the struct sg_table itself */ kunit_remove_action(test, sg_free_table_wrapper, sgt); kunit_remove_action(test, kfree_wrapper, sgt); shmem =3D to_drm_gem_shmem_obj(gem_obj); KUNIT_ASSERT_PTR_EQ(test, shmem->sgt, sgt); ret =3D kunit_add_action_or_reset(test, drm_gem_shmem_free_wrapper, shmem); KUNIT_ASSERT_EQ(test, ret, 0); The last one is arguable, but for the previous ones it makes error handling much more convenient and easy to reason about. The wrappers are also a bit inconvenient to use, but it's mostly there to avoid a compiler warning at the moment. This patch will help hopefully: https://lore.kernel.org/linux-kselftest/20230915050125.3609689-1-davidgow@g= oogle.com/ Maxime --tcuh3gisjtvmugdz Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iHUEABYKAB0WIQRcEzekXsqa64kGDp7j7w1vZxhRxQUCZTjVJAAKCRDj7w1vZxhR xYdGAQDkxKhWaor5tcnx6YjSfUKy3qxOyvKwHBkTrnr3ZiZJhgD/ciezUk1mtw77 8KroW1oLeeULoY/vcRzY1uB96jxR5Ac= =fbyv -----END PGP SIGNATURE----- --tcuh3gisjtvmugdz--