Received: by 2002:ac0:950c:0:0:0:0:0 with SMTP id f12csp2297649imc; Tue, 12 Mar 2019 10:51:34 -0700 (PDT) X-Google-Smtp-Source: APXvYqyHOdMt1w84sQt5d57hchTEIQT7qcjXzSHoXPqN5d/jjPXIDLcO2ylYKu6JH6o/WONbAQl7 X-Received: by 2002:a17:902:a7:: with SMTP id a36mr41348126pla.267.1552413093912; Tue, 12 Mar 2019 10:51:33 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1552413093; cv=none; d=google.com; s=arc-20160816; b=BrLohwSY6EWFjtKm5VBChscNgRz4MH9p96wFoPJy+nECMleVBa91hcJnZFya4n829b l3G8W+Ndj3ZCNQSUzXP+hEm6IFyll+nZkp+TX8n84A/EBytA+KfuciT6NkGO60/rPtJy ELXZ2ZwPY+2npOpKdQBMSlCWl0MjsKTkBYczzZ6AKvBPjTKhTv0Iwyuo5W13h7eH+rqX vDfeo1P7HSU+mFYfra6rge3MLfDFm+Sx5e+6+q6JqXpszxJ+CKLoGZ5UJv6mERckl7pc ufacozpmSY1qIzysk5pNF6CMnF++XoJodM0UANyi51guJIJbudToeIXE23Dk/YGyAKe6 FoBw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=U5ieWD8j6RKyQegSAN/MiSVo8GAssxTjNPKQEh5JUus=; b=TofgUXw/4QyhDQ9J4iDi045sEn2UoGKuFhypNkwe+QOu0L6JgLAy9v7+y+SS4AlCfk ysTEsN20c9UNxt01TMdUSQEqajjPvVKWxNXgUDGoUn6Wsx356ALNYDncvezM+G9bcMo8 YWETqjGHyRxqc8hIMD+TFIfc0YKYz495xa59Jnkw0nYLwLEwYyo1wwKOgfEjo4ThTZvm FiNkm1wwHIFAJiX5NbXliCIi4fpi0L2SvK13cD+TZF6YZp3e0L4Ve2hiTUuqo+mc2d6P 7S0zDm6R5UZA2Vpj4lVf3KY/mLmtxEIbvRwu68+zxnJz8d2pntmLxpS0tJSGFfwvbEnT GLlA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=MbV7LZbz; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id d1si7666771pgv.580.2019.03.12.10.51.18; Tue, 12 Mar 2019 10:51:33 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=MbV7LZbz; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727639AbfCLROn (ORCPT + 99 others); Tue, 12 Mar 2019 13:14:43 -0400 Received: from mail.kernel.org ([198.145.29.99]:51536 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728127AbfCLRNx (ORCPT ); Tue, 12 Mar 2019 13:13:53 -0400 Received: from localhost (unknown [104.133.8.98]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 2D3D3206DF; Tue, 12 Mar 2019 17:13:52 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1552410832; bh=l58QFlNPGzK2qJKjEeXcWvrvPnZiuWFWk2ZtfZ/9+44=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=MbV7LZbzPRRsJI/HoUcSasb61fh93ikjszRcF23gLzITQLrTkflnwGbzq/Gaz3k7d lpK8ILCKMr+mXOFD9ncLbpARfW9+F3irdxJQk4i/6F95Ikc1HsffXqe2Tvs5Q9B7rp S/2LbIajO3D3wDFA4z1Bk4iExI3fdds3KxOIUsuI= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Boris Ostrovsky , Joerg Roedel , Brijesh Singh , Suravee Suthikulpanit , Joerg Roedel , Sasha Levin Subject: [PATCH 4.19 012/149] iommu/amd: Fix IOMMU page flush when detach device from a domain Date: Tue, 12 Mar 2019 10:07:10 -0700 Message-Id: <20190312170350.571541927@linuxfoundation.org> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190312170349.421581206@linuxfoundation.org> References: <20190312170349.421581206@linuxfoundation.org> User-Agent: quilt/0.65 X-stable: review X-Patchwork-Hint: ignore MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 4.19-stable review patch. If anyone has any objections, please let me know. ------------------ [ Upstream commit 9825bd94e3a2baae1f4874767ae3a7d4c049720e ] When a VM is terminated, the VFIO driver detaches all pass-through devices from VFIO domain by clearing domain id and page table root pointer from each device table entry (DTE), and then invalidates the DTE. Then, the VFIO driver unmap pages and invalidate IOMMU pages. Currently, the IOMMU driver keeps track of which IOMMU and how many devices are attached to the domain. When invalidate IOMMU pages, the driver checks if the IOMMU is still attached to the domain before issuing the invalidate page command. However, since VFIO has already detached all devices from the domain, the subsequent INVALIDATE_IOMMU_PAGES commands are being skipped as there is no IOMMU attached to the domain. This results in data corruption and could cause the PCI device to end up in indeterministic state. Fix this by invalidate IOMMU pages when detach a device, and before decrementing the per-domain device reference counts. Cc: Boris Ostrovsky Suggested-by: Joerg Roedel Co-developed-by: Brijesh Singh Signed-off-by: Brijesh Singh Signed-off-by: Suravee Suthikulpanit Fixes: 6de8ad9b9ee0 ('x86/amd-iommu: Make iommu_flush_pages aware of multiple IOMMUs') Signed-off-by: Joerg Roedel Signed-off-by: Sasha Levin --- drivers/iommu/amd_iommu.c | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/drivers/iommu/amd_iommu.c b/drivers/iommu/amd_iommu.c index 2ab87e3f07e6..27500abe8ca7 100644 --- a/drivers/iommu/amd_iommu.c +++ b/drivers/iommu/amd_iommu.c @@ -1929,16 +1929,13 @@ static void do_attach(struct iommu_dev_data *dev_data, static void do_detach(struct iommu_dev_data *dev_data) { + struct protection_domain *domain = dev_data->domain; struct amd_iommu *iommu; u16 alias; iommu = amd_iommu_rlookup_table[dev_data->devid]; alias = dev_data->alias; - /* decrease reference counters */ - dev_data->domain->dev_iommu[iommu->index] -= 1; - dev_data->domain->dev_cnt -= 1; - /* Update data structures */ dev_data->domain = NULL; list_del(&dev_data->list); @@ -1948,6 +1945,16 @@ static void do_detach(struct iommu_dev_data *dev_data) /* Flush the DTE entry */ device_flush_dte(dev_data); + + /* Flush IOTLB */ + domain_flush_tlb_pde(domain); + + /* Wait for the flushes to finish */ + domain_flush_complete(domain); + + /* decrease reference counters - needs to happen after the flushes */ + domain->dev_iommu[iommu->index] -= 1; + domain->dev_cnt -= 1; } /* -- 2.19.1