Received: by 2002:a05:6a10:f347:0:0:0:0 with SMTP id d7csp5456686pxu; Tue, 22 Dec 2020 18:36:33 -0800 (PST) X-Google-Smtp-Source: ABdhPJyQInbkJqvtxOc4WA3ReldciROaRWdEPAoy68IWv8cncdI+AQUYqkoFB0WI6SeyUcfTKuPT X-Received: by 2002:a17:906:1f07:: with SMTP id w7mr173533ejj.519.1608690993662; Tue, 22 Dec 2020 18:36:33 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1608690993; cv=none; d=google.com; s=arc-20160816; b=xdEJ1xomSmwyXj3OZfptbC4zgSc3YstWqEkKsrTQv25HZIvHBPNMmvt11TdEWPABiI T7t8EtvgMhYWP4wp/kUQ69rKO/vIxyBUW5H7vfDu7PgJtqAbAciSrdc19VvpEVwYsAqB l19xkW34iqHacba0KN4IvzYQeT+yVWxLvEHnqE4BfbQTZpGNRUj9HyWo5E+LxONUnEE2 Eigj+dkfzmF+fb923v3LYqTpOrWGQqFyLfv0ECEU2BMcZcNqSfQKVHeOlMylGFR/K69d FlGHE8dAYFd1h1BemjyamS+himrE2o/hgEENTMjbTdHJtrnwmw7R/Oawd/7/yS+3P/E0 C8ww== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=KGwN/tvG/XPWSKYVUJ/OlWzUALJ4nTFQGFqZKfAxilA=; b=SqR5TpavkKUaKD8pRSF9W2WB8h0mnrqktf+mexRahYlSVcttPWuZqPVz5Ir5ABTRVT 9UCqdUyqmWxU6HaZdhyq1ld5TdY1SGQAaqNgNuHmuBYF+8VNeO/yfpULuy5r7USsU8Uy b/xwYD76H+YOmNoPRKe50bqg9qoY7T0+ggYPvkbmyvaq2Vvk/jlUH6e56aSdSK7pmL4w aCgt7U/ZVBpW/oiSLNVCr/PHmueNQ5CNP6kzsh3OliKhcIQvZROZcMk71uSru9pF5eDV qMmcErlGpQvPkclhfaT7l2vaJsSjtdCzaIj2rOx1U/ByGopGshvJ+Lheuo2xSCN+N/rF FMIA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=ZDqVbmR4; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id l23si13047313edr.325.2020.12.22.18.36.11; Tue, 22 Dec 2020 18:36:33 -0800 (PST) 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=k20201202 header.b=ZDqVbmR4; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729299AbgLWCYd (ORCPT + 99 others); Tue, 22 Dec 2020 21:24:33 -0500 Received: from mail.kernel.org ([198.145.29.99]:54288 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729252AbgLWCYZ (ORCPT ); Tue, 22 Dec 2020 21:24:25 -0500 Received: by mail.kernel.org (Postfix) with ESMTPSA id 1D0512313F; Wed, 23 Dec 2020 02:23:44 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1608690225; bh=yU2Q8s/JIOzM5oFF2LA7kz+WsHoXm2IKUFG2CyuZP3Y=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=ZDqVbmR4yobf7nd67c2oVlEX3Hx26/7lLkfol6dIcPeCe27RspabvGwV7MKK0RIdw Eygr+ZLe0bga9zZm1ZzxlcA5Zh7qJUL1i/o5mxdU0rrtFRQQEL99t/Hlmd3JEWk0Ap tElxIeuPM+Y2kW6hJr3GiKPgDc9HEJ6x4xxZvaaFJRRJuCJ/yw7XxPPd66FSMcT2+r zrujZ8z3ES016Ri0e1/ZoWKM31CA5X1A5O6YqXkaaDDLINRLZSRXJRd/625heAVSpb a+dY5N0DdrCf1LMtQYLl2UxRMxX/1xZDfIoS1odOpaBJUP5Es7eay+KPJ6zh8ISgcD cME8F+04Lvokw== From: Sasha Levin To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Nicolin Chen , Dmitry Osipenko , Thierry Reding , Will Deacon , Sasha Levin , linux-tegra@vger.kernel.org, iommu@lists.linux-foundation.org Subject: [PATCH AUTOSEL 4.14 41/66] iommu/tegra-smmu: Expand mutex protection range Date: Tue, 22 Dec 2020 21:22:27 -0500 Message-Id: <20201223022253.2793452-41-sashal@kernel.org> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20201223022253.2793452-1-sashal@kernel.org> References: <20201223022253.2793452-1-sashal@kernel.org> MIME-Version: 1.0 X-stable: review X-Patchwork-Hint: Ignore Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Nicolin Chen [ Upstream commit d5f583bf8654c231b781096bc1a186065cda72b3 ] This is used to protect potential race condition at use_count. since probes of client drivers, calling attach_dev(), may run concurrently. Signed-off-by: Nicolin Chen Tested-by: Dmitry Osipenko Reviewed-by: Dmitry Osipenko Acked-by: Thierry Reding Link: https://lore.kernel.org/r/20201125101013.14953-3-nicoleotsuka@gmail.com Signed-off-by: Will Deacon Signed-off-by: Sasha Levin --- drivers/iommu/tegra-smmu.c | 34 +++++++++++++++++++++------------- 1 file changed, 21 insertions(+), 13 deletions(-) diff --git a/drivers/iommu/tegra-smmu.c b/drivers/iommu/tegra-smmu.c index 848dac3e4580f..30f0c72d685fe 100644 --- a/drivers/iommu/tegra-smmu.c +++ b/drivers/iommu/tegra-smmu.c @@ -244,26 +244,19 @@ static int tegra_smmu_alloc_asid(struct tegra_smmu *smmu, unsigned int *idp) { unsigned long id; - mutex_lock(&smmu->lock); - id = find_first_zero_bit(smmu->asids, smmu->soc->num_asids); - if (id >= smmu->soc->num_asids) { - mutex_unlock(&smmu->lock); + if (id >= smmu->soc->num_asids) return -ENOSPC; - } set_bit(id, smmu->asids); *idp = id; - mutex_unlock(&smmu->lock); return 0; } static void tegra_smmu_free_asid(struct tegra_smmu *smmu, unsigned int id) { - mutex_lock(&smmu->lock); clear_bit(id, smmu->asids); - mutex_unlock(&smmu->lock); } static bool tegra_smmu_capable(enum iommu_cap cap) @@ -398,17 +391,21 @@ static int tegra_smmu_as_prepare(struct tegra_smmu *smmu, struct tegra_smmu_as *as) { u32 value; - int err; + int err = 0; + + mutex_lock(&smmu->lock); if (as->use_count > 0) { as->use_count++; - return 0; + goto unlock; } as->pd_dma = dma_map_page(smmu->dev, as->pd, 0, SMMU_SIZE_PD, DMA_TO_DEVICE); - if (dma_mapping_error(smmu->dev, as->pd_dma)) - return -ENOMEM; + if (dma_mapping_error(smmu->dev, as->pd_dma)) { + err = -ENOMEM; + goto unlock; + } /* We can't handle 64-bit DMA addresses */ if (!smmu_dma_addr_valid(smmu, as->pd_dma)) { @@ -431,24 +428,35 @@ static int tegra_smmu_as_prepare(struct tegra_smmu *smmu, as->smmu = smmu; as->use_count++; + mutex_unlock(&smmu->lock); + return 0; err_unmap: dma_unmap_page(smmu->dev, as->pd_dma, SMMU_SIZE_PD, DMA_TO_DEVICE); +unlock: + mutex_unlock(&smmu->lock); + return err; } static void tegra_smmu_as_unprepare(struct tegra_smmu *smmu, struct tegra_smmu_as *as) { - if (--as->use_count > 0) + mutex_lock(&smmu->lock); + + if (--as->use_count > 0) { + mutex_unlock(&smmu->lock); return; + } tegra_smmu_free_asid(smmu, as->id); dma_unmap_page(smmu->dev, as->pd_dma, SMMU_SIZE_PD, DMA_TO_DEVICE); as->smmu = NULL; + + mutex_unlock(&smmu->lock); } static int tegra_smmu_attach_dev(struct iommu_domain *domain, -- 2.27.0