Received: by 2002:ac0:950c:0:0:0:0:0 with SMTP id f12csp2279523imc; Tue, 12 Mar 2019 10:28:15 -0700 (PDT) X-Google-Smtp-Source: APXvYqyF0jqQrWCxbfan3PcAUEEk08WpoCfRbhOO8/v/X/KuF2HBhIztSDF8PDYafpR27Q7ysbb7 X-Received: by 2002:a17:902:2865:: with SMTP id e92mr35188575plb.312.1552411695133; Tue, 12 Mar 2019 10:28:15 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1552411695; cv=none; d=google.com; s=arc-20160816; b=vSbf0KVS+oE9O8An9ndoCJOJ8a/RXcjqwTE0gp1xtC66cAJlKyc08GNQW1oFsOL1Hm Zz5/PMCbEeDDHDY6Cxzw6S4qLVowket3utlGYwLLu8QdhY2aGkys6en26sSYJjSYT+8+ 7OnkRkjlRRDOQjJ3fOciiXrO6MXX59uweSHdf+9yCpDXSZRYF9fQTPg+OWSlvSCd6rEa HSWYFLANneFAIowY1i6Ueo0EUpYPEGjo+8kVr3Te/Shwb4Zrp+gyTF+o/av4vDOSpamt 0LcjVa2lFFewBfuKP4Ap2bjEAhvk0JYR91h5zQcoD4/p3luGtc+dsQRRfGLgBin88Yy6 qiAQ== 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=aZ3jqTCYaTQnG4M3h2oz6GP/R8u9EZvc9++zbPTW6cE=; b=SnaR4ccRx9ALGPiCr78J7b7Zmu5H4J64i9l+0SJhlpPINJxntZ/Mri4GYICc4Yn5Yw kfU3JdgRbzC505xhdSbcLg3qg7C9CSVvWKyn2sZwoNw99Mee1detvS6zryWk4PrFHs/i ZMFsCTuMt0WWm/LSuZbWjjM21JRvroKgwYdirN3A3wUKlaD/dIB1PaIKJFXL/MjvRTyY qwhk/K35+GqkxL7ThNQN6/Qx8bfiZiSbDFxJCh8yuMvid2IZM0A4r1U6/oOc4JF++oTA 4UProA6JnAXCJxX949Rn9PqXU2Q+wTBTG0I5Huq+LsbFPoD9e8NL2X/meFAWIHuu9542 fQMQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=KldZs1zL; 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 l7si7920935pgp.513.2019.03.12.10.27.59; Tue, 12 Mar 2019 10:28:15 -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=KldZs1zL; 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 S1730030AbfCLRZh (ORCPT + 99 others); Tue, 12 Mar 2019 13:25:37 -0400 Received: from mail.kernel.org ([198.145.29.99]:34290 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729759AbfCLRRt (ORCPT ); Tue, 12 Mar 2019 13:17:49 -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 5298B2087C; Tue, 12 Mar 2019 17:17:48 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1552411068; bh=r4e9QC4PLi2VjwmK56vRItYF0sGGtQKRnKFijds7W6s=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=KldZs1zL4Yngk4dEW4yYV/GWoTHZy+g5E2NhKOSaXoxY5Q3uhoTKDJ2rdzP7wS4vF ypOCkYyiJAbeCRrwqiZyRe/rfJo/VYgnd+hB2sDxxqe+DELQXzKLUHr95HmGxWu1Hi mqrg8tjr+XXP/5+7Qwb0npjIVLu+xMABpUETrr5c= 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.9 41/96] iommu/amd: Fix IOMMU page flush when detach device from a domain Date: Tue, 12 Mar 2019 10:09:59 -0700 Message-Id: <20190312171037.626976211@linuxfoundation.org> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190312171034.530434962@linuxfoundation.org> References: <20190312171034.530434962@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.9-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 b182839d36cb..ca22483d253f 100644 --- a/drivers/iommu/amd_iommu.c +++ b/drivers/iommu/amd_iommu.c @@ -1896,6 +1896,7 @@ 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; @@ -1911,10 +1912,6 @@ static void do_detach(struct iommu_dev_data *dev_data) 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); @@ -1924,6 +1921,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