Received: by 2002:a25:868d:0:0:0:0:0 with SMTP id z13csp2974846ybk; Mon, 18 May 2020 12:32:25 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxHCdUG5jClNTwIPBotASv1cj/ZxWaYba29xS45JvgnqSeossKwAu9CStA5iVaEFFLonIw4 X-Received: by 2002:a17:907:447f:: with SMTP id oo23mr15645941ejb.274.1589830345065; Mon, 18 May 2020 12:32:25 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1589830345; cv=none; d=google.com; s=arc-20160816; b=HISDyXexHZmJWQQE+hvUDw5kaHwdGNSnZP8wNsc5bOtQVBWlLEuv0/6Bpx2g+GBCiS uw4xJlb1eNGI3vA5CtjA/PpzjXfA4M9EnyJAcMU3bgQgH3KLN3AhGGTmLY6SSN3g53RM du1OLzOXjrnF+ShHjk8AJ7km2KQizei2AZzI7+UIM+E9pKa2f/2lbsbyuExCbvC1UPjX IAtnPwtFp0C9w8kGzvUXGNDP4//WaE9M+s/nMsIeNtERjlYGNqZPoMNvudNrWcmNF3mX VMj+WP5nE6S5mjoOvfAWbdwVLQZbLvsSx4sUfDf1ew6d/iGoWpN54wyyNm9fBWf3Cvvo 91iA== 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=Trq8hd+NQ4pJXeSIFz9723SxrDVjYY2paN0JQW2jBo4=; b=viI66u7hZT8XWgLNtIJ0As+s8s81BHAQSEFS+vzB0QdYcQPPDTBPGF3uyQDrUEGZnB 3bJT1vTXDNZ4aT/nvoKpP04zD22ilA+zWqTWuve/SO0xQVK9M/KLoSmZgu/h0QR1ATgx nm/LjBXUPm+sCYgGHJx2vVSPuacYRDaXubJL/SLgmx5b9YX3XlxFJN4gu8h5yXW9Jkc7 0SrB8X4Y7ArKqjsDrp2o9xSKf+bKjidvhLFxYtdA7Q5C87anSCdmI1Gep7H8PLWAB2DH ZqMpk9s+6X2RBc+QCvx3QIpqvtVSiozido/ZOYpZO3A1lDo/ox8KfvNil8ieZHPxZeyh 3ucg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=dBAPjxQ3; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id k10si6410173eds.461.2020.05.18.12.32.00; Mon, 18 May 2020 12:32:25 -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=@kernel.org header.s=default header.b=dBAPjxQ3; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1732270AbgERSAs (ORCPT + 99 others); Mon, 18 May 2020 14:00:48 -0400 Received: from mail.kernel.org ([198.145.29.99]:42480 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1731464AbgERSAo (ORCPT ); Mon, 18 May 2020 14:00:44 -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 0076220715; Mon, 18 May 2020 18:00:43 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1589824844; bh=bilQRGimaC9QS9pokQ0sfLWFm5nTVThenmiGeoSWa9o=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=dBAPjxQ3xQdO4WbwFZvXV6bEeq5TZy+6uZDsAc/MTeFPwaDuHnw806OMwUl1HLvkT H8/HHCifNcrj/Ddh7ZRUinW5r4ijF6qdHJ6r/dgbBfRzBpJW/m3mB9wc1FMfauUkV6 ZRIVf0G7nHYunLqaARQY/6Zdam8or/px6EvwuoiM= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Qian Cai , Joerg Roedel , Sasha Levin Subject: [PATCH 5.6 026/194] iommu/amd: Update Device Table in increase_address_space() Date: Mon, 18 May 2020 19:35:16 +0200 Message-Id: <20200518173533.708889920@linuxfoundation.org> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20200518173531.455604187@linuxfoundation.org> References: <20200518173531.455604187@linuxfoundation.org> User-Agent: quilt/0.66 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 From: Joerg Roedel [ Upstream commit 19c6978fba68a2cdedee7d55fb8c3063d47982d9 ] The Device Table needs to be updated before the new page-table root can be published in domain->pt_root. Otherwise a concurrent call to fetch_pte might fetch a PTE which is not reachable through the Device Table Entry. Fixes: 92d420ec028d ("iommu/amd: Relax locking in dma_ops path") Reported-by: Qian Cai Signed-off-by: Joerg Roedel Tested-by: Qian Cai Link: https://lore.kernel.org/r/20200504125413.16798-5-joro@8bytes.org Signed-off-by: Joerg Roedel Signed-off-by: Sasha Levin --- drivers/iommu/amd_iommu.c | 49 ++++++++++++++++++++++++++++----------- 1 file changed, 36 insertions(+), 13 deletions(-) diff --git a/drivers/iommu/amd_iommu.c b/drivers/iommu/amd_iommu.c index 28229a38af4d2..500d0a8c966fc 100644 --- a/drivers/iommu/amd_iommu.c +++ b/drivers/iommu/amd_iommu.c @@ -101,6 +101,8 @@ struct kmem_cache *amd_iommu_irq_cache; static void update_domain(struct protection_domain *domain); static int protection_domain_init(struct protection_domain *domain); static void detach_device(struct device *dev); +static void update_and_flush_device_table(struct protection_domain *domain, + struct domain_pgtable *pgtable); /**************************************************************************** * @@ -1461,8 +1463,16 @@ static bool increase_address_space(struct protection_domain *domain, *pte = PM_LEVEL_PDE(pgtable.mode, iommu_virt_to_phys(pgtable.root)); - root = amd_iommu_domain_encode_pgtable(pte, pgtable.mode + 1); + pgtable.root = pte; + pgtable.mode += 1; + update_and_flush_device_table(domain, &pgtable); + domain_flush_complete(domain); + /* + * Device Table needs to be updated and flushed before the new root can + * be published. + */ + root = amd_iommu_domain_encode_pgtable(pte, pgtable.mode); atomic64_set(&domain->pt_root, root); ret = true; @@ -1882,19 +1892,17 @@ static bool dma_ops_domain(struct protection_domain *domain) } static void set_dte_entry(u16 devid, struct protection_domain *domain, + struct domain_pgtable *pgtable, bool ats, bool ppr) { - struct domain_pgtable pgtable; u64 pte_root = 0; u64 flags = 0; u32 old_domid; - amd_iommu_domain_get_pgtable(domain, &pgtable); + if (pgtable->mode != PAGE_MODE_NONE) + pte_root = iommu_virt_to_phys(pgtable->root); - if (pgtable.mode != PAGE_MODE_NONE) - pte_root = iommu_virt_to_phys(pgtable.root); - - pte_root |= (pgtable.mode & DEV_ENTRY_MODE_MASK) + pte_root |= (pgtable->mode & DEV_ENTRY_MODE_MASK) << DEV_ENTRY_MODE_SHIFT; pte_root |= DTE_FLAG_IR | DTE_FLAG_IW | DTE_FLAG_V | DTE_FLAG_TV; @@ -1967,6 +1975,7 @@ static void clear_dte_entry(u16 devid) static void do_attach(struct iommu_dev_data *dev_data, struct protection_domain *domain) { + struct domain_pgtable pgtable; struct amd_iommu *iommu; bool ats; @@ -1982,7 +1991,9 @@ static void do_attach(struct iommu_dev_data *dev_data, domain->dev_cnt += 1; /* Update device table */ - set_dte_entry(dev_data->devid, domain, ats, dev_data->iommu_v2); + amd_iommu_domain_get_pgtable(domain, &pgtable); + set_dte_entry(dev_data->devid, domain, &pgtable, + ats, dev_data->iommu_v2); clone_aliases(dev_data->pdev); device_flush_dte(dev_data); @@ -2293,22 +2304,34 @@ static int amd_iommu_domain_get_attr(struct iommu_domain *domain, * *****************************************************************************/ -static void update_device_table(struct protection_domain *domain) +static void update_device_table(struct protection_domain *domain, + struct domain_pgtable *pgtable) { struct iommu_dev_data *dev_data; list_for_each_entry(dev_data, &domain->dev_list, list) { - set_dte_entry(dev_data->devid, domain, dev_data->ats.enabled, - dev_data->iommu_v2); + set_dte_entry(dev_data->devid, domain, pgtable, + dev_data->ats.enabled, dev_data->iommu_v2); clone_aliases(dev_data->pdev); } } +static void update_and_flush_device_table(struct protection_domain *domain, + struct domain_pgtable *pgtable) +{ + update_device_table(domain, pgtable); + domain_flush_devices(domain); +} + static void update_domain(struct protection_domain *domain) { - update_device_table(domain); + struct domain_pgtable pgtable; - domain_flush_devices(domain); + /* Update device table */ + amd_iommu_domain_get_pgtable(domain, &pgtable); + update_and_flush_device_table(domain, &pgtable); + + /* Flush domain TLB(s) and wait for completion */ domain_flush_tlb_pde(domain); } -- 2.20.1