Received: by 2002:ac0:bc90:0:0:0:0:0 with SMTP id a16csp577513img; Fri, 22 Mar 2019 04:24:40 -0700 (PDT) X-Google-Smtp-Source: APXvYqxk7SxVkHqLnnJi8Wt1uVbAbUtMnDNNpapRBNEHD6z3CXIwZTw7MlAJjpFlMAJnHr06o++0 X-Received: by 2002:a17:902:7786:: with SMTP id o6mr8921952pll.206.1553253880390; Fri, 22 Mar 2019 04:24:40 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1553253880; cv=none; d=google.com; s=arc-20160816; b=oP0jBJjslugs875411xKG3Ig/18lyHimekIOriGKZbDfFgTwviqU33UBjIjX1uxinx v4CI9u++89vmtGDX0DlT3HvmDv/ICKVH3MBlFme2w35bx52NvTBRlajPnNJktOphBOjg UVJOly9S6abudX50NrUsWo6n9a67eoXq4iaJEWEqbxMD7Efqq0A57cMCzBA3VRK5Denr 8qZU0MYiHxVhMHPwpaqyuHQDZRV2EC73mvN/soaFKtbfD0OaoAHfxl1RQHg2eNugvapN sRcN/NoJH9zfxIEUwQkRoCydCJJ4YyTV+dVI4WbpMSDBQVsoXcD76eV8hsfpQrNBnDSn Cjxg== 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=nW37crcFOFbburwc8x2DyIPgg9Y5whF90909UjkZMjo=; b=K7Z070wX35ESX+NQI4fg/6U8sJ0S1af87bBMTNe+36W4kKOnAZHVRJI54qDO6SXOw+ tZwpvPAGyQv7DCV3V180ErL7L6d70bfp/PPM2f6rE8BigSc9o8n7sjpiAD4OCjVhvBNf hwoEcn2vQd6nLWNDDfb8pyCrdhaUj79ycf7fXtreJQGA7d9uhlRE0BJwgW198qf8urzx AFND/kbzMdgywBdDOMhYAj7H+tpfDKynVKyOL1YwOVe4tKtXwQE7J63m9lwpsD3iYdjJ /tgfR+sjJIqCMkEp0JAOqgjbrdTzjWxvtboxcQyMEZykc67wUs+C0vMyVVWL/MzN3fmf jODQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=Dgxes5h7; 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 f40si7242102plb.339.2019.03.22.04.24.24; Fri, 22 Mar 2019 04:24:40 -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=Dgxes5h7; 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 S1728238AbfCVLWy (ORCPT + 99 others); Fri, 22 Mar 2019 07:22:54 -0400 Received: from mail.kernel.org ([198.145.29.99]:49614 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728401AbfCVLWt (ORCPT ); Fri, 22 Mar 2019 07:22:49 -0400 Received: from localhost (83-86-89-107.cable.dynamic.v4.ziggo.nl [83.86.89.107]) (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 409DA218A2; Fri, 22 Mar 2019 11:22:48 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1553253768; bh=zmEg1I8HcG+p5HlgBPXh83MTtzguc8QtMeFWP5vbOZ8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Dgxes5h79lRa+wYw1FyEiuXkR3fm/KwtqVdgF8MRRQgn8cdRElivS9Uxtk1jFX+6u 5dTNehlyw8ZIohpNVhHIYPRKXQ95bfMb51NRaHYjiKdiEXOSJoURgBmLpjVHR9X8ZY mxFtDyeKw1tVCNoxlqJqk3minsyrFIlXz8VVhKYw= 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 3.18 058/134] iommu/amd: Fix IOMMU page flush when detach device from a domain Date: Fri, 22 Mar 2019 12:14:31 +0100 Message-Id: <20190322111214.210520651@linuxfoundation.org> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190322111210.465931067@linuxfoundation.org> References: <20190322111210.465931067@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 3.18-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 5cf388ad1555..48a73c48876b 100644 --- a/drivers/iommu/amd_iommu.c +++ b/drivers/iommu/amd_iommu.c @@ -2132,14 +2132,11 @@ 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; iommu = amd_iommu_rlookup_table[dev_data->devid]; - /* 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); @@ -2147,6 +2144,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