Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp4611813pxj; Wed, 12 May 2021 09:13:27 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxJQ4JFQrA106tQrrdN2KQqcZ7SFBu+CJRQWOs/H18xUAIJeHx/wq1IDDaSWmjuLMXd/lhz X-Received: by 2002:a17:906:46d0:: with SMTP id k16mr38896594ejs.105.1620836006933; Wed, 12 May 2021 09:13:26 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1620836006; cv=none; d=google.com; s=arc-20160816; b=FB7abFpj9lVk10q2mf/J2UerXD78xu5zxln5Zg1R0cXRRwz5fSwO2rOvejGERUnJLW diDIwbXHP3QKeNULnEa6oxQna/aw+mRMo/SFPmXF0iozUs/0V0T6qz6o1bLLh9ZDu7hE p164oBVh9c1ONZVDoatu3CjPLqP6mrWJYOA8bXI0spyAA7FJIURb1mdGSRcv99yO2S7g trbXSzwF4IK5BvPkpfpCfoCGIpQV7IcJ2n3KVGTpYVTlwW5LmOzH+I7RhGRbzxV3KJLy mdU+L2dNgbKFmbYK0jo/8r12xRQS1j5F/EnqR+4/G4fVvlnvM2N7VcYPo4U+3gVvfDbk Ks+w== 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 :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=AZLgNyQOMTP9bgAyw/CAO1sQH4XGSIe6A2JgoDUu0qQ=; b=kiBdCmQvpcGlqPJfqvp/dvMUh39K8YHcqMk5WNHL1Dh7Tsd2bbWGZvPXfyJP2tFN9S zzb3SZxfX4Ot4jEnziLTATtqpgRGETUOHpweeUF0QlO6O+X3Ljc8ZFgYf3OvcvBhKz37 s+q10SRBXnp0Gda7crc6vnsJDGk5J3ntukNn2g6EqGT17XMB6bUSnLp7Xu09UHVqQmll hXHpdGrsWXmAH8VYw/BZ+XckLcd9vFk8qQRXgRCc4btZcIMCcKXGYoDjLMrTGTSxrETR nIh2JpFWHe0kTPsZqAeam7kTMXxZ6fr1hCxHWMMJLPK0ZCGC+qQOR9Uri4NVI8ryukX2 YkVg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=0nvtyBDK; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id b6si451911ejb.398.2021.05.12.09.13.03; Wed, 12 May 2021 09:13:26 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=0nvtyBDK; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234296AbhELQK5 (ORCPT + 99 others); Wed, 12 May 2021 12:10:57 -0400 Received: from mail.kernel.org ([198.145.29.99]:40742 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234886AbhELP3u (ORCPT ); Wed, 12 May 2021 11:29:50 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 3563D61945; Wed, 12 May 2021 15:15:32 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1620832532; bh=IctrLuusrI9vVJ9a28zso74mmzrptNV6lJ3aTrhRA5Q=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=0nvtyBDKnrnkVChlsHyxLOlwCRIuZYIJMtXGLgpj5Lbcn6AvAfveQjH5B6/Dy6ayj cS19ke7rKY/mN4oZqa+f8FPSJhvu4X+fESoqJAfB79DKPctIG5jyftN/CN/jkTJIOv 2WUk/CWG+rzBZoREj+RFndsn0gIuotnXcXfl+2NI= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Christoph Hellwig , Max Gurtovoy , Kevin Tian , Cornelia Huck , Eric Auger , Jason Gunthorpe , Alex Williamson , Sasha Levin Subject: [PATCH 5.10 318/530] vfio/pci: Re-order vfio_pci_probe() Date: Wed, 12 May 2021 16:47:08 +0200 Message-Id: <20210512144830.263162448@linuxfoundation.org> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210512144819.664462530@linuxfoundation.org> References: <20210512144819.664462530@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Jason Gunthorpe [ Upstream commit 4aeec3984ddc853f7c65903bde472ffdef738bae ] vfio_add_group_dev() must be called only after all of the private data in vdev is fully setup and ready, otherwise there could be races with user space instantiating a device file descriptor and starting to call ops. For instance vfio_pci_reflck_attach() sets vdev->reflck and vfio_pci_open(), called by fops open, unconditionally derefs it, which will crash if things get out of order. Fixes: cc20d7999000 ("vfio/pci: Introduce VF token") Fixes: e309df5b0c9e ("vfio/pci: Parallelize device open and release") Fixes: 6eb7018705de ("vfio-pci: Move idle devices to D3hot power state") Fixes: ecaa1f6a0154 ("vfio-pci: Add VGA arbiter client") Reviewed-by: Christoph Hellwig Reviewed-by: Max Gurtovoy Reviewed-by: Kevin Tian Reviewed-by: Cornelia Huck Reviewed-by: Eric Auger Signed-off-by: Jason Gunthorpe Message-Id: <8-v3-225de1400dfc+4e074-vfio1_jgg@nvidia.com> Signed-off-by: Alex Williamson Signed-off-by: Sasha Levin --- drivers/vfio/pci/vfio_pci.c | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/drivers/vfio/pci/vfio_pci.c b/drivers/vfio/pci/vfio_pci.c index f31aa25f361c..48b048edf1ee 100644 --- a/drivers/vfio/pci/vfio_pci.c +++ b/drivers/vfio/pci/vfio_pci.c @@ -2034,13 +2034,9 @@ static int vfio_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id) INIT_LIST_HEAD(&vdev->vma_list); init_rwsem(&vdev->memory_lock); - ret = vfio_add_group_dev(&pdev->dev, &vfio_pci_ops, vdev); - if (ret) - goto out_free; - ret = vfio_pci_reflck_attach(vdev); if (ret) - goto out_del_group_dev; + goto out_free; ret = vfio_pci_vf_init(vdev); if (ret) goto out_reflck; @@ -2064,15 +2060,20 @@ static int vfio_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id) vfio_pci_set_power_state(vdev, PCI_D3hot); } - return ret; + ret = vfio_add_group_dev(&pdev->dev, &vfio_pci_ops, vdev); + if (ret) + goto out_power; + return 0; +out_power: + if (!disable_idle_d3) + vfio_pci_set_power_state(vdev, PCI_D0); out_vf: vfio_pci_vf_uninit(vdev); out_reflck: vfio_pci_reflck_put(vdev->reflck); -out_del_group_dev: - vfio_del_group_dev(&pdev->dev); out_free: + kfree(vdev->pm_save); kfree(vdev); out_group_put: vfio_iommu_group_put(group, &pdev->dev); -- 2.30.2