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: 10896671 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 DF4421515 for ; Thu, 11 Apr 2019 18:49:42 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id CA075288D3 for ; Thu, 11 Apr 2019 18:49:42 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id BD09728B00; Thu, 11 Apr 2019 18:49:42 +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=ham 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 5F0BE288D3 for ; Thu, 11 Apr 2019 18:49:42 +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=TO/jnw07p+qKc8okvGosoo8yTiN+E0gKUnOQ7bMjbzw=; b=cI7hf7C5j9ntFU 0cHETPrLzaDAQuE7/v8me+qHWfEAUFhnwkDpvTl78AHdUa+fOu7+8bwUVqMamuKScvZXNmbkbbZvE /rRtal6lGM3FbPRukiclQ2bSdmcsbqMu+Wi3A1CDnDTq0hiKJd2/YKRA/LweqY6fo/6Dr1ru8xEQL ZmMVM9sNIlmXbgZj9JYHJnTUy4FBsUWqEy9zPrUZW93ZIo5umbqoUUaVht43bhjFxdSSzDy7L4/FV MiHMxln90eD7gHOeeToEZe5W9v3jdfDJh7mgDkWqut6yrFCt+T0ST1O4yJ6SkrBF8P2m5yQd5X9nL YIKYusjKVfU/2UBvNbJA==; 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 1hEelW-00032g-G2; Thu, 11 Apr 2019 18:49:38 +0000 Received: from merlin.infradead.org ([2001:8b0:10b:1231::1]) by bombadil.infradead.org with esmtps (Exim 4.90_1 #2 (Red Hat Linux)) id 1hEelE-0002dM-Hv for linux-rockchip@bombadil.infradead.org; Thu, 11 Apr 2019 18:49:20 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=merlin.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=Eh8THpFtB5t9uAY0M2im+nOB6T ecqVaKkWHb9PD4kEfTKzX1kS85wed8kYbRHXhlM6z7arzUeka4uHbfskFb+rgogIvultOV3hmwpgg UEw/rTfSOSD9sj8pM6X0/q9oq03mDresPpou18RTMnsqDQe8O2Djj8i3TRF5HtQIQ1m93JiMG34TW 2GORvKoOZBw2jmy5TEz6oKMKJTQy0zF1xmtzbB/kFMxpt6S3aNQmFM9RmDuv5JvgAe1bqQDB/eQ/L qmGzO4CluCUZpdI2yQPoJNJ3fUx2jnpwqgXkMNtAMIq+YETUWbhQD5OuvxQFQ8sIx9EkTLnzyszji PCn1gf5g==; Received: from mail-ed1-x542.google.com ([2a00:1450:4864:20::542]) by merlin.infradead.org with esmtps (Exim 4.90_1 #2 (Red Hat Linux)) id 1hEelA-0003bV-LC for linux-rockchip@lists.infradead.org; Thu, 11 Apr 2019 18:49:18 +0000 Received: by mail-ed1-x542.google.com with SMTP id g6so2175889edc.8 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=j1nyvMD7XAcGj7hrC81p7+y68eCi0dc03ACIQTY5O5JKbKhsGftfleYc85Z8NFBasf rGAM8y0sa3edpdHBNnC99TS1wNZiFZqJII7pCUEArUc0/k9udMWRmD6KecEkcoLyq6tS 69EbgpS2+XGz4lWzrxr+ll/89uEO+B5cF4lLVBepdxEsXvIgJFVt+cEobSWKjY9LDvLH NXg3MRYYuv7lsxB2cJmF9WIs97cs7qYNd95OYI4k7tPLSIpu+0j8qbIeNXk22Dj4SF+X c2Uqlo6/aEIvL741ryvV7ur9/MSI6cU1JH7IHy0wdxBm/4JXjRIKEogMPc6wZPcVKlzp IWUQ== X-Gm-Message-State: APjAAAV2Agio3gQFcksDYx3v1ybKaR4pXX3ueqQnh6WYIQ5M2cgjeErJ ROljhwoZ8KyPmusjVHettEXD0Q== 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_144916_761649_855991AC X-CRM114-Status: GOOD ( 15.36 ) X-BeenThere: linux-rockchip@lists.infradead.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: Upstream kernel work for Rockchip platforms 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-rockchip" Errors-To: linux-rockchip-bounces+patchwork-linux-rockchip=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,