Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp4726444pxj; Wed, 12 May 2021 11:49:06 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyiuJAYUwF8dllyzmGgEULGFce2cgihQnOB9Hn4zpAfH7kdjalezCFlPKfLPaiwM5epv8r4 X-Received: by 2002:a17:906:3a04:: with SMTP id z4mr22452022eje.221.1620845346708; Wed, 12 May 2021 11:49:06 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1620845346; cv=none; d=google.com; s=arc-20160816; b=NNNu6HToGZsGqW2AVi/b7gvcxiuoF8RyT54ojozxmTUFATD/w4P+7SLsDX6qTe5iZf +CZOVfSsycKcV2tzlNXbAztzHSUOlnenDTcOIUnz0VHLyoLkcQhnK/DGZlBmptJbdFAJ AuVdzxoFr0sLwESYWYDI0U+v+wO7mO8iX4Q9elbVEcm4OOAWM3nHRgB6n5VtYvXEeoQH gsV6L6wOg3IkBB4Zu3ySPau186gvhZPK/T0G0RGGsDqVErxYEBz8AYoSc4x1v4X72NDR FZSZz03Yhk2a99VBYiP4HZco7wpjQ/vhUW1rxrZ/jR1ZjvNZSIZv3xYRfjXJdpFpjAf6 m4dg== 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=UJaHG+sIdK6K8rEDAtv3XFZBKlB+ZQheR+RgDaQquyc=; b=UhiilZRWcEMt03g1/6765RDt923MWDg1uAF+LWFT9G/HdcH0fY/KCqFT0maSk/ngtA 1T8Do8+te5E16Z73gtL1r+PT28eUidjYofBaTWVWn+4/4Hnyf4Sa4ur/NBR0fYF3Cxh2 /6r5xNb6sk+3eObjQkNlycq4oCQ4A8roHKKoh7OVOdII8pnqWWDfxj3DQiNhT/lNiYh6 SsnLXyEKz7DPVnpQ//fLoIMs4KPYfHE5JtDNoeW5yRV7ddP8d/VZgQlGszzXA88YAYs5 wpdwe3Wu2JNbWUtP0LEebwqzkh2C4ZEehyepDCG+8iFDMccxZMxW0BEAWgJ0z523Te7R 7sGQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=yFAo3jJm; 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 dn27si359010edb.326.2021.05.12.11.48.43; Wed, 12 May 2021 11:49:06 -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=yFAo3jJm; 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 S243373AbhELSo7 (ORCPT + 99 others); Wed, 12 May 2021 14:44:59 -0400 Received: from mail.kernel.org ([198.145.29.99]:34162 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S243718AbhELQl5 (ORCPT ); Wed, 12 May 2021 12:41:57 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 0B83D61CEE; Wed, 12 May 2021 16:06:09 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1620835570; bh=+18fLh6+xzcca+ZBIC1c5w1zg5jkpXWQCzYr/8F/yV8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=yFAo3jJmbGjV7/knefWwxM+JbkBzvuZWXAEGqpIkqzJ9wqo5oMrgSqEFLJh/Kl4wC IXlV2p8k2Iq7cK0SHUzHxKkCnoYbc32SSBZfrIuNQXwzrCfYus6WKgcib+bdoYJUK6 n/ac8efGknXTwgcIM+ZDDIqsIjP46NqhC4mRxVT8= 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.12 397/677] vfio/pci: Re-order vfio_pci_probe() Date: Wed, 12 May 2021 16:47:23 +0200 Message-Id: <20210512144850.524404815@linuxfoundation.org> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210512144837.204217980@linuxfoundation.org> References: <20210512144837.204217980@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 b73144a6804a..cb7f2dc09e9d 100644 --- a/drivers/vfio/pci/vfio_pci.c +++ b/drivers/vfio/pci/vfio_pci.c @@ -2032,13 +2032,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; @@ -2062,15 +2058,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