Received: by 2002:a05:6358:489b:b0:bb:da1:e618 with SMTP id x27csp1421635rwn; Thu, 8 Sep 2022 20:16:20 -0700 (PDT) X-Google-Smtp-Source: AA6agR5RQK/xXBTt3/afZMgw5CP1Ug5VJWUJcPNtMkpLRmjM6c+E66P0JZBXI6Z2+eDgSjfsUb8/ X-Received: by 2002:a17:902:ebd2:b0:172:8eee:80f5 with SMTP id p18-20020a170902ebd200b001728eee80f5mr11709491plg.9.1662693380750; Thu, 08 Sep 2022 20:16:20 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1662693380; cv=none; d=google.com; s=arc-20160816; b=mBBSkpL1G5ayXsjG5iITb6Ya6FemXOmM7MSoBW43VL44+xQ17jsElDqkI5jYrFnxrv iaxi29LbKh0KxOpeOtya+lDps2n1KrskvRtO6SqYSANJM+slvrHH643a1jKa5OqyZXxI jLv9NFT6IFMJM2KbWpORM7f2oGpqXKlEl98g0nP/kpmnqealbt1g2gG/d718ySGcIl4C 7tKoi6ka4PLxD4uswaZMgJCDtJMMhaIg/LaJhXRSQ86kHnb4tkLlg0ilW+AT1IkIe7Iw N101KN+1lWHX6yVvUvw658v7QhbZnApuHbfRFcPZtCEtfqWWSdaE2Zs/twB24zUNVKrQ v2aA== 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=pQ3dslGgO+lNo5R2bZ39ETvA/6plEHW9dvw8yHfZ6Lk=; b=IV3qsbo2Rv30g+GLQ8Is81Y+6999c5QffeJkLVdGryXalmcYcYUWzehYL8vvqt2QUk 9eIxjR3Oh2xyj2l+scmfnVGzlqvUfRdYglJkgfcGJCkPQSJGcDpxheKbZWnzGSPGfmvZ U9cOZt7t4ufrQuVAL5GXexSJc5psxkxQYJXp5+gsByj58bxTNzpxDwn5hoyfQmV7St2C krKjBamRb0FXXeGpKO5FePYXFMfe7CWXMgBhe3M9mnXhVQKoFd9uXB0blvCYG6r89ckR EixqN+CkF5rNLDcOZOOi+2yp3L8S4M90V4ftJ18P+OWc5undBOjkmfyBkyQfXCNJH+Si aX4A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=GGAD34nq; 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 m10-20020a654c8a000000b004387adde4f9si555060pgt.91.2022.09.08.20.15.52; Thu, 08 Sep 2022 20:16:20 -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=GGAD34nq; 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 S230340AbiIIDF7 (ORCPT + 99 others); Thu, 8 Sep 2022 23:05:59 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40804 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230159AbiIIDFh (ORCPT ); Thu, 8 Sep 2022 23:05:37 -0400 Received: from mga12.intel.com (mga12.intel.com [192.55.52.136]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A8952237E0; Thu, 8 Sep 2022 20:05:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1662692704; x=1694228704; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=1Twck9BA6OHNULTPl1B/ow9Z+KAQ9ZqcK5vq27ZI7Z0=; b=GGAD34nqSoUKZ4+wZXv1ixCAkmdq3ahXP0Mwm+T2hhdomDylAl2Y0Kfo rwgX3IlqVvJNQPoUpUfRWQTwpaFHCCECizEpzaI7jIl3550o0RNJ+lwVz PbAdyF+o0FvRsThVmwLf+eK9sTbDq6dZ78rBzJKztAY1SPWPi4GSlm6q4 6+zIGZtlN/ZIs9My/hYeRVzzATcpOIrGo2Z6PtdSOr1hGmdM+yFn+oQVv nN3ocP/LXd8RWCOkru4PxzAFq9vKgtfKpc5qpw+4Vr5i8RZhIES0Hwo6O /jg8T3XGnBFmn9Wwor6A0C8m14Hwf/lwq31AovRAglPe8Kaz5lBpN17L/ A==; X-IronPort-AV: E=McAfee;i="6500,9779,10464"; a="277116703" X-IronPort-AV: E=Sophos;i="5.93,300,1654585200"; d="scan'208";a="277116703" Received: from orsmga004.jf.intel.com ([10.7.209.38]) by fmsmga106.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 08 Sep 2022 20:05:00 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.93,300,1654585200"; d="scan'208";a="740909141" 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:50 -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 09/15] vfio/ap: Use the new device life cycle helpers Date: Fri, 9 Sep 2022 18:22:41 +0800 Message-Id: <20220909102247.67324-10-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,SPF_HELO_PASS,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_instances inside @init/@release. Signed-off-by: Yi Liu Signed-off-by: Kevin Tian Reviewed-by: Tony Krowiak Reviewed-by: Jason Gunthorpe --- drivers/s390/crypto/vfio_ap_ops.c | 50 ++++++++++++++++++------------- 1 file changed, 29 insertions(+), 21 deletions(-) diff --git a/drivers/s390/crypto/vfio_ap_ops.c b/drivers/s390/crypto/vfio_ap_ops.c index 6c8c41fac4e1..803aadfd0876 100644 --- a/drivers/s390/crypto/vfio_ap_ops.c +++ b/drivers/s390/crypto/vfio_ap_ops.c @@ -684,42 +684,44 @@ static bool vfio_ap_mdev_filter_matrix(unsigned long *apm, unsigned long *aqm, AP_DOMAINS); } -static int vfio_ap_mdev_probe(struct mdev_device *mdev) +static int vfio_ap_mdev_init_dev(struct vfio_device *vdev) { - struct ap_matrix_mdev *matrix_mdev; - int ret; + struct ap_matrix_mdev *matrix_mdev = + container_of(vdev, struct ap_matrix_mdev, vdev); if ((atomic_dec_if_positive(&matrix_dev->available_instances) < 0)) return -EPERM; - matrix_mdev = kzalloc(sizeof(*matrix_mdev), GFP_KERNEL); - if (!matrix_mdev) { - ret = -ENOMEM; - goto err_dec_available; - } - vfio_init_group_dev(&matrix_mdev->vdev, &mdev->dev, - &vfio_ap_matrix_dev_ops); - - matrix_mdev->mdev = mdev; + matrix_mdev->mdev = to_mdev_device(vdev->dev); vfio_ap_matrix_init(&matrix_dev->info, &matrix_mdev->matrix); matrix_mdev->pqap_hook = handle_pqap; vfio_ap_matrix_init(&matrix_dev->info, &matrix_mdev->shadow_apcb); hash_init(matrix_mdev->qtable.queues); + return 0; +} + +static int vfio_ap_mdev_probe(struct mdev_device *mdev) +{ + struct ap_matrix_mdev *matrix_mdev; + int ret; + + matrix_mdev = vfio_alloc_device(ap_matrix_mdev, vdev, &mdev->dev, + &vfio_ap_matrix_dev_ops); + if (IS_ERR(matrix_mdev)) + return PTR_ERR(matrix_mdev); + ret = vfio_register_emulated_iommu_dev(&matrix_mdev->vdev); if (ret) - goto err_list; + goto err_put_vdev; dev_set_drvdata(&mdev->dev, matrix_mdev); mutex_lock(&matrix_dev->mdevs_lock); list_add(&matrix_mdev->node, &matrix_dev->mdev_list); mutex_unlock(&matrix_dev->mdevs_lock); return 0; -err_list: - vfio_uninit_group_dev(&matrix_mdev->vdev); - kfree(matrix_mdev); -err_dec_available: - atomic_inc(&matrix_dev->available_instances); +err_put_vdev: + vfio_put_device(&matrix_mdev->vdev); return ret; } @@ -766,6 +768,12 @@ static void vfio_ap_mdev_unlink_fr_queues(struct ap_matrix_mdev *matrix_mdev) } } +static void vfio_ap_mdev_release_dev(struct vfio_device *vdev) +{ + vfio_free_device(vdev); + atomic_inc(&matrix_dev->available_instances); +} + static void vfio_ap_mdev_remove(struct mdev_device *mdev) { struct ap_matrix_mdev *matrix_mdev = dev_get_drvdata(&mdev->dev); @@ -779,9 +787,7 @@ static void vfio_ap_mdev_remove(struct mdev_device *mdev) list_del(&matrix_mdev->node); mutex_unlock(&matrix_dev->mdevs_lock); mutex_unlock(&matrix_dev->guests_lock); - vfio_uninit_group_dev(&matrix_mdev->vdev); - kfree(matrix_mdev); - atomic_inc(&matrix_dev->available_instances); + vfio_put_device(&matrix_mdev->vdev); } static ssize_t name_show(struct mdev_type *mtype, @@ -1794,6 +1800,8 @@ static const struct attribute_group vfio_queue_attr_group = { }; static const struct vfio_device_ops vfio_ap_matrix_dev_ops = { + .init = vfio_ap_mdev_init_dev, + .release = vfio_ap_mdev_release_dev, .open_device = vfio_ap_mdev_open_device, .close_device = vfio_ap_mdev_close_device, .ioctl = vfio_ap_mdev_ioctl, -- 2.21.3