Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp1165659imu; Wed, 16 Jan 2019 14:00:42 -0800 (PST) X-Google-Smtp-Source: ALg8bN5IQ5wpO583qFNUzDeeIhmJ7P8BS+f0Ii1Bcn6R+QKswTetqMJF8DxnpKd3uSh8PwZpXiUd X-Received: by 2002:a17:902:6bc7:: with SMTP id m7mr12355078plt.106.1547676042813; Wed, 16 Jan 2019 14:00:42 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1547676042; cv=none; d=google.com; s=arc-20160816; b=SNzn+A4OQgNPGwqE8oppKhr3+ymT/o8COAHzlSXUVoa73PQtcEyS9YIewU56DEjvxg WKqqw1WowWD/Of12eBpBYiqH4uc7TDGvP5IHLHtn4uKduj/L8Edml5hrH4/tG3NhgwJO ZY6QN4g1XL9DBrX0THY7JJixFWIzcoMF2KckLOPW7FEeKJ8Fzx2qsFmcHwy6DuJ++ci4 FzvbnrV0PE01su70ydsHK7qMH1ZVPrR0cOgRUXcsqxpeLsCA98kC+77v315sgAc91YBk NRkzd8uf1FpLHnf+GaGmjVwzxwB3EAwdO6uEfkgFZeffAmF2mKA3R0VZ7gPpBP7RxqUW JA+Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-disposition:mime-version:references:message-id:subject:cc :to:from:date; bh=IXnfBDoS3DBONMN2v7uJ11URFatEQCwIGov+z63vlwE=; b=hwtgaFfQzs9lP82dyaLbMDyMoNzTPVak7tk+uxtC035687q5m6sCBk3jv/+G/VMP3x KagB/cqU2Hd2Q8GJbpNUUk8jL9s3ta+17nA2aNKOnvqyajvWtNYqhDuBfMlRsZw6yuo3 ULwnBHAebGRwRzCZ3SlOw7v51GWIaCD3rkgtOZmh68p/MlivHVE80XvFXLSEtF5fiswM ei23mNVpqaq+9fAKrMr/cr0TzjxcoayZb8W+BzxOlwxM7wIA+Ym0N/ar3FV6rVaI/Pnq 0Z71Ge4oWQPKE1bpEWu9PvdQ6FOb5ZWZK3LRmzEpXuaVzwsalRkeiw1Doofqszqp+4Jo 25Ig== ARC-Authentication-Results: i=1; mx.google.com; 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; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=8bytes.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id i17si7179247pgk.233.2019.01.16.14.00.26; Wed, 16 Jan 2019 14:00:42 -0800 (PST) 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; 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; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=8bytes.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2404312AbfAPN0v (ORCPT + 99 others); Wed, 16 Jan 2019 08:26:51 -0500 Received: from 8bytes.org ([81.169.241.247]:57966 "EHLO theia.8bytes.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1732810AbfAPN0u (ORCPT ); Wed, 16 Jan 2019 08:26:50 -0500 Received: by theia.8bytes.org (Postfix, from userid 1000) id DC342273; Wed, 16 Jan 2019 14:26:49 +0100 (CET) Date: Wed, 16 Jan 2019 14:26:48 +0100 From: "joro@8bytes.org" To: "Suthikulpanit, Suravee" Cc: "linux-kernel@vger.kernel.org" , "iommu@lists.linux-foundation.org" , Boris Ostrovsky , "Singh, Brijesh" Subject: Re: [PATCH] iommu/amd: Fix IOMMU page flush when detach all devices from a domain Message-ID: <20190116132648.i5n3hz3k7d2wxbrx@8bytes.org> References: <20190116041546.3541-1-Suravee.Suthikulpanit@amd.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20190116041546.3541-1-Suravee.Suthikulpanit@amd.com> User-Agent: NeoMutt/20170421 (1.8.2) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, Jan 16, 2019 at 04:16:25AM +0000, Suthikulpanit, Suravee wrote: > diff --git a/drivers/iommu/amd_iommu.c b/drivers/iommu/amd_iommu.c > index 525659b88ade..ab31ba75da1b 100644 > --- a/drivers/iommu/amd_iommu.c > +++ b/drivers/iommu/amd_iommu.c > @@ -1248,7 +1248,13 @@ static void __domain_flush_pages(struct protection_domain *domain, > build_inv_iommu_pages(&cmd, address, size, domain->id, pde); > > for (i = 0; i < amd_iommu_get_num_iommus(); ++i) { > - if (!domain->dev_iommu[i]) > + /* > + * The dev_cnt is zero when all devices are detached > + * from the domain. This is the case when VFIO detaches > + * all devices from the group before flushing IOMMU pages. > + * So, always issue the flush command. > + */ > + if (domain->dev_cnt && !domain->dev_iommu[i]) > continue; This doesn't look like the correct fix. We still miss the flush if we detach the last device from the domain. How about the attached diff? If I understand the problem correctly, it should fix the problem more reliably. Thanks, Joerg diff --git a/drivers/iommu/amd_iommu.c b/drivers/iommu/amd_iommu.c index 87ba23a75b38..dc1e2a8a19d7 100644 --- a/drivers/iommu/amd_iommu.c +++ b/drivers/iommu/amd_iommu.c @@ -1991,25 +1991,36 @@ 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); - clear_dte_entry(dev_data->devid); - if (alias != dev_data->devid) - clear_dte_entry(alias); + clear_dte_entry(dev_data->devid); /* Flush the DTE entry */ device_flush_dte(dev_data); + + if (alias != dev_data->devid) { + clear_dte_entry(alias); + /* Flush the Alias DTE entry */ + device_flush_dte(alias); + } + + /* 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; } /*