From nobody Thu Apr 9 16:15:01 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 00B93ECAAD1 for ; Thu, 1 Sep 2022 07:19:00 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233600AbiIAHS6 (ORCPT ); Thu, 1 Sep 2022 03:18:58 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60294 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233797AbiIAHSe (ORCPT ); Thu, 1 Sep 2022 03:18:34 -0400 Received: from mga04.intel.com (mga04.intel.com [192.55.52.120]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0CBDF126913; Thu, 1 Sep 2022 00:18:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1662016709; x=1693552709; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=QCqnH5yyFKRSM/tDdALx86Zz7Ypn4WP/5pwxdmZHGPQ=; b=WNXERE8NZbCG05fbAbcrY7fbP86tsuIKezjk0gYdej1G9x1KOo+OXhtu 19VHpBlfZw9oy9XECyB6vuSbaOcnyeruD2pMRcwHaLimlctBhzdEqISRZ mUgLIYkJB8nci8ekf7gNj2puuRzqaYVypIpUun0aZSaql/GiK8rjlgT4w dhrpow74MkCoIrxIy4nM2ldToWBlPK8TFcc30tjx19nfQyDVr+G5Mlpgi 8oQ5R215YfEWSby3Ev5hykuc4E1pfeMSneRR45I5VDRl7O9x5V9MWZxFB faPep0+xoSEYuv7I/YFAMb42Sf3x0b0LV/V1FBBfZEyXf8ZyCMw3xWq2F w==; X-IronPort-AV: E=McAfee;i="6500,9779,10456"; a="294378157" X-IronPort-AV: E=Sophos;i="5.93,280,1654585200"; d="scan'208";a="294378157" Received: from fmsmga008.fm.intel.com ([10.253.24.58]) by fmsmga104.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 01 Sep 2022 00:18:22 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.93,280,1654585200"; d="scan'208";a="673720107" Received: from sqa-gate.sh.intel.com (HELO michael.clx.dev.tsp.org) ([10.239.48.212]) by fmsmga008.fm.intel.com with ESMTP; 01 Sep 2022 00:18:08 -0700 From: Kevin Tian To: Zhenyu Wang , Zhi Wang , Jani Nikula , Joonas Lahtinen , Rodrigo Vivi , Tvrtko Ursulin , David Airlie , Daniel Vetter , Eric Farman , Matthew Rosato , Halil Pasic , Vineeth Vijayan , Peter Oberparleiter , Heiko Carstens , Vasily Gorbik , Alexander Gordeev , Christian Borntraeger , Sven Schnelle , Tony Krowiak , Jason Herne , Harald Freudenberger , Diana Craciun , Alex Williamson , Cornelia Huck , Longfang Liu , Shameer Kolothum , Jason Gunthorpe , Yishai Hadas , Kevin Tian , Eric Auger , Kirti Wankhede , Leon Romanovsky , Abhishek Sahu , intel-gvt-dev@lists.freedesktop.org, intel-gfx@lists.freedesktop.org, dri-devel@lists.freedesktop.org, linux-kernel@vger.kernel.org, linux-s390@vger.kernel.org, kvm@vger.kernel.org Cc: Yi Liu Subject: [PATCH v2 05/15] vfio/mdpy: Use the new device life cycle helpers Date: Thu, 1 Sep 2022 22:37:37 +0800 Message-Id: <20220901143747.32858-6-kevin.tian@intel.com> X-Mailer: git-send-email 2.21.3 In-Reply-To: <20220901143747.32858-1-kevin.tian@intel.com> References: <20220901143747.32858-1-kevin.tian@intel.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" From: Yi Liu and manage mdpy_count inside @init/@release. Signed-off-by: Yi Liu Signed-off-by: Kevin Tian Reviewed-by: Jason Gunthorpe --- samples/vfio-mdev/mdpy.c | 81 +++++++++++++++++++++++----------------- 1 file changed, 47 insertions(+), 34 deletions(-) diff --git a/samples/vfio-mdev/mdpy.c b/samples/vfio-mdev/mdpy.c index e8c46eb2e246..a07dac16d873 100644 --- a/samples/vfio-mdev/mdpy.c +++ b/samples/vfio-mdev/mdpy.c @@ -216,61 +216,77 @@ static int mdpy_reset(struct mdev_state *mdev_state) return 0; } =20 -static int mdpy_probe(struct mdev_device *mdev) +static int mdpy_init_dev(struct vfio_device *vdev) { + struct mdev_state *mdev_state =3D + container_of(vdev, struct mdev_state, vdev); + struct mdev_device *mdev =3D to_mdev_device(vdev->dev); const struct mdpy_type *type =3D &mdpy_types[mdev_get_type_group_id(mdev)]; - struct device *dev =3D mdev_dev(mdev); - struct mdev_state *mdev_state; u32 fbsize; - int ret; + int ret =3D -ENOMEM; =20 if (mdpy_count >=3D max_devices) - return -ENOMEM; - - mdev_state =3D kzalloc(sizeof(struct mdev_state), GFP_KERNEL); - if (mdev_state =3D=3D NULL) - return -ENOMEM; - vfio_init_group_dev(&mdev_state->vdev, &mdev->dev, &mdpy_dev_ops); + return ret; =20 mdev_state->vconfig =3D kzalloc(MDPY_CONFIG_SPACE_SIZE, GFP_KERNEL); - if (mdev_state->vconfig =3D=3D NULL) { - ret =3D -ENOMEM; - goto err_state; - } + if (!mdev_state->vconfig) + return ret; =20 fbsize =3D roundup_pow_of_two(type->width * type->height * type->bytepp); =20 mdev_state->memblk =3D vmalloc_user(fbsize); - if (!mdev_state->memblk) { - ret =3D -ENOMEM; - goto err_vconfig; - } - dev_info(dev, "%s: %s (%dx%d)\n", __func__, type->name, type->width, - type->height); + if (!mdev_state->memblk) + goto out_vconfig; =20 mutex_init(&mdev_state->ops_lock); mdev_state->mdev =3D mdev; - mdev_state->type =3D type; + mdev_state->type =3D type; mdev_state->memsize =3D fbsize; mdpy_create_config_space(mdev_state); mdpy_reset(mdev_state); =20 + dev_info(vdev->dev, "%s: %s (%dx%d)\n", __func__, type->name, type->width, + type->height); + mdpy_count++; + return 0; + +out_vconfig: + kfree(mdev_state->vconfig); + return ret; +} + +static int mdpy_probe(struct mdev_device *mdev) +{ + struct mdev_state *mdev_state; + int ret; + + mdev_state =3D vfio_alloc_device(mdev_state, vdev, &mdev->dev, + &mdpy_dev_ops); + if (IS_ERR(mdev_state)) + return PTR_ERR(mdev_state); =20 ret =3D vfio_register_emulated_iommu_dev(&mdev_state->vdev); if (ret) - goto err_mem; + goto err_put_vdev; dev_set_drvdata(&mdev->dev, mdev_state); return 0; -err_mem: + +err_put_vdev: + vfio_put_device(&mdev_state->vdev); + return ret; +} + +static void mdpy_release_dev(struct vfio_device *vdev) +{ + struct mdev_state *mdev_state =3D + container_of(vdev, struct mdev_state, vdev); + vfree(mdev_state->memblk); -err_vconfig: kfree(mdev_state->vconfig); -err_state: - vfio_uninit_group_dev(&mdev_state->vdev); - kfree(mdev_state); - return ret; + vfio_free_device(vdev); + mdpy_count--; } =20 static void mdpy_remove(struct mdev_device *mdev) @@ -280,12 +296,7 @@ static void mdpy_remove(struct mdev_device *mdev) dev_info(&mdev->dev, "%s\n", __func__); =20 vfio_unregister_group_dev(&mdev_state->vdev); - vfree(mdev_state->memblk); - kfree(mdev_state->vconfig); - vfio_uninit_group_dev(&mdev_state->vdev); - kfree(mdev_state); - - mdpy_count--; + vfio_put_device(&mdev_state->vdev); } =20 static ssize_t mdpy_read(struct vfio_device *vdev, char __user *buf, @@ -708,6 +719,8 @@ static struct attribute_group *mdev_type_groups[] =3D { }; =20 static const struct vfio_device_ops mdpy_dev_ops =3D { + .init =3D mdpy_init_dev, + .release =3D mdpy_release_dev, .read =3D mdpy_read, .write =3D mdpy_write, .ioctl =3D mdpy_ioctl, --=20 2.21.3