Received: by 2002:a05:6358:489b:b0:bb:da1:e618 with SMTP id x27csp1446689rwn; Thu, 8 Sep 2022 20:55:18 -0700 (PDT) X-Google-Smtp-Source: AA6agR7qdWOO47nPq7D2znBF7OWzqiVa1ZEUJnK4N+u0DbKwIrXSdBS/oDYU4Hnu2jdhNfeAyzOT X-Received: by 2002:a63:89c2:0:b0:435:c7d:1532 with SMTP id v185-20020a6389c2000000b004350c7d1532mr7954191pgd.39.1662695718024; Thu, 08 Sep 2022 20:55:18 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1662695718; cv=none; d=google.com; s=arc-20160816; b=MiNcVNuZuTnaI24WnodF+/rWUguvVYXRFQBDQGsPh7lybWOOnOBLFVPMss+20g5XpX okQDrBPZsPYqLW1I4ngHo1/IzVWj4R7kUkD9tO5TvWUxK6B3DaVK2lk0pvUImMwRahZK QP4S0bJ9qaOt2jr4ZXqKLo9fzNHguA/fLCaudzc89TOD3fPIDXGsgVwGup+sdxn3gaWz daqWHzwDgJpOW15nZB0gwU5dqwLi0age5Hmy+LE27LdPHgkMveOoPq3Gpbm3Qqd7MzCt ScwE8MofW+BO6nv216U99DW/1uuwnI6bzPuH3g34Dccagb6Z1eFxiEFyUuCQ5Npeof+v T3nw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=0fOfpwcWedyKSHzSu7ZCZYnnxX0KWHRONfiiaDTs8EE=; b=gDSFdY/d+yLUSBI6iYw/TDN2W/vLc2pMLrNnL375FO524LOuDoFNnvcd0IyD0z4DbN Au51A5i/D40e6NZZot5tgLEtYtMhPWjyPaJQb4H9LTSb+/nsuKI3o4v1MmdysEJOz0ew 0KqOm1lMPrMQUy/a5V6pEWu30iOz/jRqZeIUV37C22ObW1tMNHDI14edw+0vgbQ/T2jH ov21TOeFcBVdIPi3bKq2rZCiPS6Q3n9PoCO4aw7k5IHc1mw3nO0+iywqCgJ00UojAuqH wwK+/5PYu2JFmqGDK9sy6pnRSCMsufYa23SZ1tbN3Xq/e0w57O9yv3ZkaNasCmda2VkN npkA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=NJeSnAfk; 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=intel.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id g3-20020a170902e38300b00174708399desi635802ple.556.2022.09.08.20.55.06; Thu, 08 Sep 2022 20:55:18 -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=@intel.com header.s=Intel header.b=NJeSnAfk; 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=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230182AbiIIDE5 (ORCPT + 99 others); Thu, 8 Sep 2022 23:04:57 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39334 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230070AbiIIDEj (ORCPT ); Thu, 8 Sep 2022 23:04:39 -0400 Received: from mga09.intel.com (mga09.intel.com [134.134.136.24]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 77D3513CEE; Thu, 8 Sep 2022 20:04:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1662692678; x=1694228678; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=eaCLetd3P35bUmOh4svxRrc/KVF//VLSz2q4OGmo054=; b=NJeSnAfk4gjMEWHWrG8ePQpw0zt5qBaZlSnqsC+ZvUApXv4fR4y2RyL9 p/9GuToc8RSKOGi832tHnbKMs+oBqpd5GjRoDgociKh930PwAwk6wNB68 MR15/B65bICcGoSFovi5NcWuzJdHZU5nHK6VGr0c6RJvMIaYJn1vVeVff eCndTNKkDYfEqPzbkN5PplUOtsEG0HOhsOSztsXBWn6fxCdJUhU66RFKs tLVSu476pF00Dr6ef742UwQKsGVVDndO6ZOMcobW3QiZLWtSM12CyKaEl 6FVcMB8miSCie1YJqRKuPueGC+BizkXlsT+W0fAHDsA7IsQDPEabROo75 Q==; X-IronPort-AV: E=McAfee;i="6500,9779,10464"; a="298186077" X-IronPort-AV: E=Sophos;i="5.93,300,1654585200"; d="scan'208";a="298186077" Received: from orsmga004.jf.intel.com ([10.7.209.38]) by orsmga102.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 08 Sep 2022 20:04:31 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.93,300,1654585200"; d="scan'208";a="740908911" Received: from sqa-gate.sh.intel.com (HELO michael.clx.dev.tsp.org) ([10.239.48.212]) by orsmga004.jf.intel.com with ESMTP; 08 Sep 2022 20:04:22 -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 , Christoph Hellwig , 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 v3 06/15] vfio/mtty: Use the new device life cycle helpers Date: Fri, 9 Sep 2022 18:22:38 +0800 Message-Id: <20220909102247.67324-7-kevin.tian@intel.com> X-Mailer: git-send-email 2.21.3 In-Reply-To: <20220909102247.67324-1-kevin.tian@intel.com> References: <20220909102247.67324-1-kevin.tian@intel.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.5 required=5.0 tests=BAYES_00,DATE_IN_FUTURE_06_12, DKIMWL_WL_HIGH,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, RCVD_IN_DNSWL_MED,RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE, SPF_NONE,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: X-Mailing-List: linux-kernel@vger.kernel.org From: Yi Liu and manage available ports inside @init/@release. Signed-off-by: Yi Liu Signed-off-by: Kevin Tian Reviewed-by: Jason Gunthorpe --- samples/vfio-mdev/mtty.c | 67 +++++++++++++++++++++++----------------- 1 file changed, 39 insertions(+), 28 deletions(-) diff --git a/samples/vfio-mdev/mtty.c b/samples/vfio-mdev/mtty.c index f42a59ed2e3f..41301d50b247 100644 --- a/samples/vfio-mdev/mtty.c +++ b/samples/vfio-mdev/mtty.c @@ -703,9 +703,11 @@ static ssize_t mdev_access(struct mdev_state *mdev_state, u8 *buf, size_t count, return ret; } -static int mtty_probe(struct mdev_device *mdev) +static int mtty_init_dev(struct vfio_device *vdev) { - struct mdev_state *mdev_state; + struct mdev_state *mdev_state = + container_of(vdev, struct mdev_state, vdev); + struct mdev_device *mdev = to_mdev_device(vdev->dev); int nr_ports = mdev_get_type_group_id(mdev) + 1; int avail_ports = atomic_read(&mdev_avail_ports); int ret; @@ -716,58 +718,65 @@ static int mtty_probe(struct mdev_device *mdev) } while (!atomic_try_cmpxchg(&mdev_avail_ports, &avail_ports, avail_ports - nr_ports)); - mdev_state = kzalloc(sizeof(struct mdev_state), GFP_KERNEL); - if (mdev_state == NULL) { - ret = -ENOMEM; - goto err_nr_ports; - } - - vfio_init_group_dev(&mdev_state->vdev, &mdev->dev, &mtty_dev_ops); - mdev_state->nr_ports = nr_ports; mdev_state->irq_index = -1; mdev_state->s[0].max_fifo_size = MAX_FIFO_SIZE; mdev_state->s[1].max_fifo_size = MAX_FIFO_SIZE; mutex_init(&mdev_state->rxtx_lock); - mdev_state->vconfig = kzalloc(MTTY_CONFIG_SPACE_SIZE, GFP_KERNEL); - if (mdev_state->vconfig == NULL) { + mdev_state->vconfig = kzalloc(MTTY_CONFIG_SPACE_SIZE, GFP_KERNEL); + if (!mdev_state->vconfig) { ret = -ENOMEM; - goto err_state; + goto err_nr_ports; } mutex_init(&mdev_state->ops_lock); mdev_state->mdev = mdev; - mtty_create_config_space(mdev_state); + return 0; + +err_nr_ports: + atomic_add(nr_ports, &mdev_avail_ports); + return ret; +} + +static int mtty_probe(struct mdev_device *mdev) +{ + struct mdev_state *mdev_state; + int ret; + + mdev_state = vfio_alloc_device(mdev_state, vdev, &mdev->dev, + &mtty_dev_ops); + if (IS_ERR(mdev_state)) + return PTR_ERR(mdev_state); ret = vfio_register_emulated_iommu_dev(&mdev_state->vdev); if (ret) - goto err_vconfig; + goto err_put_vdev; dev_set_drvdata(&mdev->dev, mdev_state); return 0; -err_vconfig: - kfree(mdev_state->vconfig); -err_state: - vfio_uninit_group_dev(&mdev_state->vdev); - kfree(mdev_state); -err_nr_ports: - atomic_add(nr_ports, &mdev_avail_ports); +err_put_vdev: + vfio_put_device(&mdev_state->vdev); return ret; } +static void mtty_release_dev(struct vfio_device *vdev) +{ + struct mdev_state *mdev_state = + container_of(vdev, struct mdev_state, vdev); + + kfree(mdev_state->vconfig); + vfio_free_device(vdev); + atomic_add(mdev_state->nr_ports, &mdev_avail_ports); +} + static void mtty_remove(struct mdev_device *mdev) { struct mdev_state *mdev_state = dev_get_drvdata(&mdev->dev); - int nr_ports = mdev_state->nr_ports; vfio_unregister_group_dev(&mdev_state->vdev); - - kfree(mdev_state->vconfig); - vfio_uninit_group_dev(&mdev_state->vdev); - kfree(mdev_state); - atomic_add(nr_ports, &mdev_avail_ports); + vfio_put_device(&mdev_state->vdev); } static int mtty_reset(struct mdev_state *mdev_state) @@ -1287,6 +1296,8 @@ static struct attribute_group *mdev_type_groups[] = { static const struct vfio_device_ops mtty_dev_ops = { .name = "vfio-mtty", + .init = mtty_init_dev, + .release = mtty_release_dev, .read = mtty_read, .write = mtty_write, .ioctl = mtty_ioctl, -- 2.21.3