Received: by 2002:a25:4158:0:0:0:0:0 with SMTP id o85csp3226620yba; Tue, 16 Apr 2019 07:14:35 -0700 (PDT) X-Google-Smtp-Source: APXvYqyS8LljdrepLVB+d3jkjkfMWofKQh15B4Bk5W8amr186xo3ZqRHecRrv5LL6KWA1fPTs7ux X-Received: by 2002:a17:902:aa91:: with SMTP id d17mr83709328plr.43.1555424075626; Tue, 16 Apr 2019 07:14:35 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1555424075; cv=none; d=google.com; s=arc-20160816; b=RViv4+UQPjuV6Yon5endQka1O1RKh6qZB78zxxdBvotTO33bf8JLCCeGeo0SbKZnsZ a129RBwaSjnwwa27NeoVnrXSgS0AFhkvnYd6ZsEvVEkDye8a/5mlEshvYPMjE/RZorFm nnT9qZhWkB1bhaAQO0Me8pvEg0qbf/1uvRU7QtGj/Snsah0yvditnAHK4xmbClb117/Q Rdgaugb+hxh7uORaDlD1BTC9bv7hwLx0fmKRP5ABHauhwLeHEUkDClhitVM+kDDi/bKw uP8LZGYaaiHiDEkzLoLKsxuD9K4DKWGSh5RTUlgTbLiyEyn2gNtxMFdMtkG6xq7oDWxI RCBQ== 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 :content-language:in-reply-to:mime-version:user-agent:date :message-id:from:references:cc:to:subject; bh=jeUnQU2f21aaXvwBBv5b9M4Q5F+NTfN3tLj6s6H0Nao=; b=e8Jee4GP6F6cACJQiJzNRTKE/ikZlawvLUWYjTrbcDOsgUMua/BE+XFXgxaiH0lvGJ FFghWwdO8Zta9BrHdKSv4sfjSUBP62Qgs8MbHkvMn3noto1+zVfD0nsCzgRXDdtabVNc kYKWj1BP4kBEFdRFirp5KCycVQifdyoIJcbf6X6Cv0SD95aRSPyG/sjK2oExcR6HBNEw lOrflE0iaOaZz2oadENWb6eMnJGpQNHXDVRE+f2hQpEfN/GFyDv2GXG/hdrM0C5EaG+N 2BQGQQhht6kkElfaTa14Ryw73dwBYDg+3y23EYMB/MvcNhlzQ2S5O0hazQAaaH9TT4mU RbTA== 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id x186si35510435pgx.399.2019.04.16.07.14.18; Tue, 16 Apr 2019 07:14:35 -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; 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 S1729476AbfDPONY (ORCPT + 99 others); Tue, 16 Apr 2019 10:13:24 -0400 Received: from foss.arm.com ([217.140.101.70]:55996 "EHLO foss.arm.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725861AbfDPONX (ORCPT ); Tue, 16 Apr 2019 10:13:23 -0400 Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.72.51.249]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 06BE3EBD; Tue, 16 Apr 2019 07:13:23 -0700 (PDT) Received: from [10.1.196.75] (e110467-lin.cambridge.arm.com [10.1.196.75]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 5C8FF3F59C; Tue, 16 Apr 2019 07:13:18 -0700 (PDT) Subject: Re: [PATCH 6/9] iommu/amd: Implement map_atomic To: Tom Murphy , iommu@lists.linux-foundation.org Cc: dima@arista.com, jamessewart@arista.com, murphyt7@tcd.ie, Joerg Roedel , Will Deacon , Marek Szyprowski , Kukjin Kim , Krzysztof Kozlowski , Matthias Brugger , Andy Gross , David Brown , Rob Clark , Heiko Stuebner , Marc Zyngier , Thomas Gleixner , linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-samsung-soc@vger.kernel.org, linux-mediatek@lists.infradead.org, linux-arm-msm@vger.kernel.org, linux-rockchip@lists.infradead.org References: <20190411184741.27540-1-tmurphy@arista.com> <20190411184741.27540-7-tmurphy@arista.com> From: Robin Murphy Message-ID: <78f2114b-0dcb-2dcf-c3b3-411e064b079f@arm.com> Date: Tue, 16 Apr 2019 15:13:16 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.6.1 MIME-Version: 1.0 In-Reply-To: <20190411184741.27540-7-tmurphy@arista.com> Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-GB Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 11/04/2019 19:47, Tom Murphy wrote: > Instead of using a spin lock I removed the mutex lock from both the > amd_iommu_map and amd_iommu_unmap path as well. iommu_map doesn’t lock > while mapping and so if iommu_map is called by two different threads on > the same iova region it results in a race condition even with the locks. > So the locking in amd_iommu_map and amd_iommu_unmap doesn't add any real > protection. The solution to this is for whatever manages the allocated > iova’s externally to make sure iommu_map isn’t called twice on the same > region at the same time. Note that that assumption is not necessarily sufficient - even with correct address space management you can have cases like two threads mapping adjacent pages, where even thought they are targeting different PTEs they can race to install/modify intermediate levels of the pagetable. I believe AMD is actually OK in that regard, but some drivers *are* relying on locking for correctness so it can't just be unequivocally removed everywhere. Robin. > Signed-off-by: Tom Murphy > --- > drivers/iommu/amd_iommu.c | 25 ++++++++++++++++++------- > 1 file changed, 18 insertions(+), 7 deletions(-) > > diff --git a/drivers/iommu/amd_iommu.c b/drivers/iommu/amd_iommu.c > index 2d4ee10626b4..b45e0e033adc 100644 > --- a/drivers/iommu/amd_iommu.c > +++ b/drivers/iommu/amd_iommu.c > @@ -3089,12 +3089,12 @@ static int amd_iommu_attach_device(struct iommu_domain *dom, > return ret; > } > > -static int amd_iommu_map(struct iommu_domain *dom, unsigned long iova, > - phys_addr_t paddr, size_t page_size, int iommu_prot) > +static int __amd_iommu_map(struct iommu_domain *dom, unsigned long iova, > + phys_addr_t paddr, size_t page_size, int iommu_prot, > + gfp_t gfp) > { > struct protection_domain *domain = to_pdomain(dom); > int prot = 0; > - int ret; > > if (domain->mode == PAGE_MODE_NONE) > return -EINVAL; > @@ -3104,11 +3104,21 @@ static int amd_iommu_map(struct iommu_domain *dom, unsigned long iova, > if (iommu_prot & IOMMU_WRITE) > prot |= IOMMU_PROT_IW; > > - mutex_lock(&domain->api_lock); > - ret = iommu_map_page(domain, iova, paddr, page_size, prot, GFP_KERNEL); > - mutex_unlock(&domain->api_lock); > + return iommu_map_page(domain, iova, paddr, page_size, prot, gfp); > +} > > - return ret; > +static int amd_iommu_map(struct iommu_domain *dom, unsigned long iova, > + phys_addr_t paddr, size_t page_size, int iommu_prot) > +{ > + return __amd_iommu_map(dom, iova, paddr, page_size, iommu_prot, > + GFP_KERNEL); > +} > + > +static int amd_iommu_map_atomic(struct iommu_domain *dom, unsigned long iova, > + phys_addr_t paddr, size_t page_size, int iommu_prot) > +{ > + return __amd_iommu_map(dom, iova, paddr, page_size, iommu_prot, > + GFP_ATOMIC); > } > > static size_t amd_iommu_unmap(struct iommu_domain *dom, unsigned long iova, > @@ -3262,6 +3272,7 @@ const struct iommu_ops amd_iommu_ops = { > .attach_dev = amd_iommu_attach_device, > .detach_dev = amd_iommu_detach_device, > .map = amd_iommu_map, > + .map_atomic = amd_iommu_map_atomic, > .unmap = amd_iommu_unmap, > .iova_to_phys = amd_iommu_iova_to_phys, > .add_device = amd_iommu_add_device, >