From patchwork Thu Apr 11 18:47:35 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Tom Murphy X-Patchwork-Id: 10896711 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 135F51669 for ; Thu, 11 Apr 2019 19:04:57 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id F1082287DB for ; Thu, 11 Apr 2019 19:04:56 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id E47DC28ADD; Thu, 11 Apr 2019 19:04:56 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-5.2 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,MAILING_LIST_MULTI,RCVD_IN_DNSWL_MED autolearn=unavailable version=3.3.1 Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 88CDE287DB for ; Thu, 11 Apr 2019 19:04:56 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20170209; h=Sender: Content-Transfer-Encoding:Content-Type:Cc:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=DQQjJ+tL+ZrCjZBf3DKH63uCFfgVJxwvEED1JxZCDTs=; b=rBDj2ksUSp8ymG AyM6O7X5zsI3ratThAX3kXp2b0EI1S/54XOJxr0QcGxFF93SEgFqVsutSUf6G7aZ7FYzLI6IEnWmw +UUCqavk5iKWPeZuexKqLg1eGH6yluGXFAQHyJLr0LhYPV88OWjRwhaoKz0N2IpGdkAtoykdOGFVb L050E8gj8g3yYU1LwaWhCoKfau14nFO+g8FSJFKidIZa4zgDJ857i3xxD9E3xUK/MQjwK45lMsQa0 RhSaEmnIUhT5NrXFhdaDeAeBe3DMPjji9R22Krprgnd0VXJ4zzODM9cDws7PpuX8uc44e9qWsOUoE QcrDXOg9b/6X6FvHrNQA==; Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.90_1 #2 (Red Hat Linux)) id 1hEf0K-0002wp-5v; Thu, 11 Apr 2019 19:04:56 +0000 Received: from casper.infradead.org ([2001:8b0:10b:1236::1]) by bombadil.infradead.org with esmtps (Exim 4.90_1 #2 (Red Hat Linux)) id 1hEf0I-0002p2-SF for linux-mediatek@bombadil.infradead.org; Thu, 11 Apr 2019 19:04:55 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=casper.20170209; h=Content-Transfer-Encoding:Content-Type: MIME-Version:References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From:Sender :Reply-To:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Id:List-Help: List-Unsubscribe:List-Subscribe:List-Post:List-Owner:List-Archive; bh=lX2MVOfiam4gxtJM5rz0M+xQIQGKu992FM3+lagPerc=; b=SL9fUelBOJ7qkUh+ZjHCCHCgqz KbP0L+QFqL17gBhIfhOjwl7M1XLaUZ00Sd6GuH96Zw7NuM6fMbyNK+6nirujrxit/WbN+Nsjk97CY X0BciWujsTbkkBe1gwM7Sveh1stjUkMTq+0ZrWXnqn3JbpxUyCA3uwfFlVZZZdnWUGOVngHMPrAZH bQE/gPJ8iOxKQUAti1L40MS5KwCUy8YjK8jC3b1vbIhCjoO2wZvs4HvlS8JNWz+R5sfwGHQF4gSRX dbjjHBQf0KE/KWkorkT/7RT9HtI3DLYJs0T0STNIu1YhwuXmQs01L/vDx5OC1hga7p3BoeYONYE1u IpzF6eRg==; Received: from mail-ed1-x544.google.com ([2a00:1450:4864:20::544]) by casper.infradead.org with esmtps (Exim 4.90_1 #2 (Red Hat Linux)) id 1hEel7-0006X5-HC for linux-mediatek@lists.infradead.org; Thu, 11 Apr 2019 18:49:15 +0000 Received: by mail-ed1-x544.google.com with SMTP id d55so5347984ede.0 for ; Thu, 11 Apr 2019 11:49:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=arista.com; s=googlenew; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=lX2MVOfiam4gxtJM5rz0M+xQIQGKu992FM3+lagPerc=; b=eQ4Yac+opjGEBMpNwcIhE+BWp72NvMbMpxtmojUTpE9/rOYjMILcbpoRJOSkm78WBk Aff06hpFM7720IPoJiqJyT63C+/E8qw6EX81NozIpVIX1tteropdJH6LFlD3Eh/+bnxp TqdoMoiwgIxQtWMiOGAc90uv+qOUf34JAI/T0ybJD/E7UmC1cFWzDvL9PTwU99FOa2Cq uBtmAEg9IcB2Z+rtiVJG0Mt+tgtzmiJ3TreKGg24TfCmsdd0CwQJXJinK6O+48LvF5v/ TMCo/yA7WVatb4O5GCP0DxBkXEFa1ZseoyB+vX5EoAJETdaxDHDXc+uPIuWnEbhC9q2H SvUQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=lX2MVOfiam4gxtJM5rz0M+xQIQGKu992FM3+lagPerc=; b=JdQdV/aV//pqVjSnjwkmrCDV6cCw6RHbGJ1unDYWO9sNuOW8v4SQ9zWhT6BSxRedMz KzbIrCLEq875UhGmQyncGoW3CfQAfYGCFljoX2O3rq/2DBSlqu8jGd0g9hIRN3+zd4M8 l3XsYCrbTj5/F1LY56acywVZonH0vV0nwHjOp+Jc2HcGnLBnpJgAyG2W5LYFtmZZ76tR O20yVvyN8WkeCZPyGs6NcTSNYi5t7PTPtbGLq2vcuUtFvSaxdUvdRBbYDu3S+Unna0MV +qSAqac6Od5wNdT0ZzeVKOXQsoEM9NF5a5f/n37ZpjwzJuT5Z3uyrN3Vc2GLjSUMff+f TJAA== X-Gm-Message-State: APjAAAXxO6hTAgtYOh6KV9tcidkHaOtNB5FBuW4yKi2k7vr9O+aotrDg zhKZJmFt6AaJDJkiElym8aTVrg== X-Google-Smtp-Source: APXvYqw1ZpBKkZJS1pdF6BGZGDt7JAfIqiI9pMC84S3fxuGG7Kvy3jG4mkJy+S81q/cfVwljARoScw== X-Received: by 2002:aa7:d954:: with SMTP id l20mr32634023eds.156.1555008551931; Thu, 11 Apr 2019 11:49:11 -0700 (PDT) Received: from localhost.localdomain ([80.233.32.123]) by smtp.gmail.com with ESMTPSA id b10sm1285130edi.28.2019.04.11.11.49.09 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 11 Apr 2019 11:49:11 -0700 (PDT) From: Tom Murphy To: iommu@lists.linux-foundation.org Subject: [PATCH 6/9] iommu/amd: Implement map_atomic Date: Thu, 11 Apr 2019 19:47:35 +0100 Message-Id: <20190411184741.27540-7-tmurphy@arista.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20190411184741.27540-1-tmurphy@arista.com> References: <20190411184741.27540-1-tmurphy@arista.com> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20190411_194913_625957_E0BD9EAD X-CRM114-Status: GOOD ( 16.63 ) X-BeenThere: linux-mediatek@lists.infradead.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Heiko Stuebner , jamessewart@arista.com, Will Deacon , David Brown , Marek Szyprowski , linux-samsung-soc@vger.kernel.org, dima@arista.com, Joerg Roedel , Krzysztof Kozlowski , linux-rockchip@lists.infradead.org, Kukjin Kim , Andy Gross , Marc Zyngier , linux-arm-msm@vger.kernel.org, linux-mediatek@lists.infradead.org, Matthias Brugger , Thomas Gleixner , linux-arm-kernel@lists.infradead.org, Tom Murphy , linux-kernel@vger.kernel.org, murphyt7@tcd.ie, Rob Clark , Robin Murphy Sender: "Linux-mediatek" Errors-To: linux-mediatek-bounces+patchwork-linux-mediatek=patchwork.kernel.org@lists.infradead.org X-Virus-Scanned: ClamAV using ClamSMTP 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. 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,