From: Sasha Levin <sashal@kernel.org>
To: linux-kernel@vger.kernel.org, stable@vger.kernel.org
Cc: Nicolin Chen <nicoleotsuka@gmail.com>,
Dmitry Osipenko <digetx@gmail.com>,
Thierry Reding <treding@nvidia.com>,
Will Deacon <will@kernel.org>, Sasha Levin <sashal@kernel.org>,
linux-tegra@vger.kernel.org, iommu@lists.linux-foundation.org
Subject: [PATCH AUTOSEL 4.9 31/48] iommu/tegra-smmu: Expand mutex protection range
Date: Tue, 22 Dec 2020 21:23:59 -0500 [thread overview]
Message-ID: <20201223022417.2794032-31-sashal@kernel.org> (raw)
In-Reply-To: <20201223022417.2794032-1-sashal@kernel.org>
From: Nicolin Chen <nicoleotsuka@gmail.com>
[ 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 <nicoleotsuka@gmail.com>
Tested-by: Dmitry Osipenko <digetx@gmail.com>
Reviewed-by: Dmitry Osipenko <digetx@gmail.com>
Acked-by: Thierry Reding <treding@nvidia.com>
Link: https://lore.kernel.org/r/20201125101013.14953-3-nicoleotsuka@gmail.com
Signed-off-by: Will Deacon <will@kernel.org>
Signed-off-by: Sasha Levin <sashal@kernel.org>
---
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 04cec050e42bf..ae796342717a6 100644
--- a/drivers/iommu/tegra-smmu.c
+++ b/drivers/iommu/tegra-smmu.c
@@ -241,26 +241,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)
@@ -395,17 +388,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)) {
@@ -428,24 +425,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
next prev parent reply other threads:[~2020-12-23 2:34 UTC|newest]
Thread overview: 48+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-12-23 2:23 [PATCH AUTOSEL 4.9 01/48] locks: Fix UBSAN undefined behaviour in flock64_to_posix_lock Sasha Levin
2020-12-23 2:23 ` [PATCH AUTOSEL 4.9 02/48] tomoyo: fix clang pointer arithmetic warning Sasha Levin
2020-12-23 2:23 ` [PATCH AUTOSEL 4.9 03/48] crypto: omap-aes - fix the reference count leak of omap device Sasha Levin
2020-12-23 2:23 ` [PATCH AUTOSEL 4.9 04/48] staging: wimax: depends on NET Sasha Levin
2020-12-23 2:23 ` [PATCH AUTOSEL 4.9 05/48] scsi: pm80xx: Avoid busywait in FW ready check Sasha Levin
2020-12-23 2:23 ` [PATCH AUTOSEL 4.9 06/48] scsi: pm80xx: Fix pm8001_mpi_get_nvmd_resp() race condition Sasha Levin
2020-12-23 2:23 ` [PATCH AUTOSEL 4.9 07/48] staging: rtl8192u: fix wrong judgement in rtl8192_rx_isr Sasha Levin
2020-12-23 2:23 ` [PATCH AUTOSEL 4.9 08/48] mips: ar7: add missing iounmap() on error in ar7_gpio_init Sasha Levin
2020-12-23 2:23 ` [PATCH AUTOSEL 4.9 09/48] locktorture: Prevent hangs for invalid arguments Sasha Levin
2020-12-23 2:23 ` [PATCH AUTOSEL 4.9 10/48] rcutorture: " Sasha Levin
2020-12-23 2:23 ` [PATCH AUTOSEL 4.9 11/48] drm: panel: simple: add missing platform_driver_unregister() in panel_simple_init Sasha Levin
2020-12-23 2:23 ` [PATCH AUTOSEL 4.9 12/48] Bluetooth: hidp: use correct wait queue when removing ctrl_wait Sasha Levin
2020-12-23 2:23 ` [PATCH AUTOSEL 4.9 13/48] net: skb_vlan_untag(): don't reset transport offset if set by GRO layer Sasha Levin
2020-12-23 2:23 ` [PATCH AUTOSEL 4.9 14/48] MIPS: BMC47xx: fix kconfig dependency bug for BCM47XX_SSB Sasha Levin
2020-12-23 2:23 ` [PATCH AUTOSEL 4.9 15/48] jfs: Fix memleak in dbAdjCtl Sasha Levin
2020-12-23 2:23 ` [PATCH AUTOSEL 4.9 16/48] media: zr364xx: propagate errors from zr364xx_start_readpipe() Sasha Levin
2020-12-23 2:23 ` [PATCH AUTOSEL 4.9 17/48] media: cec-core: first mark device unregistered, then wake up fhs Sasha Levin
2020-12-23 2:23 ` [PATCH AUTOSEL 4.9 18/48] media: isif: reset global state Sasha Levin
2020-12-23 2:23 ` [PATCH AUTOSEL 4.9 19/48] media: dvbdev: Fix memleak in dvb_register_device Sasha Levin
2020-12-23 2:23 ` [PATCH AUTOSEL 4.9 20/48] mmc: tmio: do not print real IOMEM pointer Sasha Levin
2020-12-23 2:23 ` [PATCH AUTOSEL 4.9 21/48] ARM: OMAP2+: Fix memleak in omap2xxx_clkt_vps_init Sasha Levin
2020-12-23 2:23 ` [PATCH AUTOSEL 4.9 22/48] ALSA: usb-audio: Don't call usb_set_interface() at trigger callback Sasha Levin
2020-12-23 2:23 ` [PATCH AUTOSEL 4.9 23/48] rxrpc: Don't leak the service-side session key to userspace Sasha Levin
2020-12-23 2:23 ` [PATCH AUTOSEL 4.9 24/48] scsi: atari_scsi: Fix race condition between .queuecommand and EH Sasha Levin
2020-12-23 2:23 ` [PATCH AUTOSEL 4.9 25/48] ARM: dts: hisilicon: fix errors detected by snps-dw-apb-uart.yaml Sasha Levin
2020-12-23 2:23 ` [PATCH AUTOSEL 4.9 26/48] ARM: dts: hisilicon: fix errors detected by usb yaml Sasha Levin
2020-12-23 2:23 ` [PATCH AUTOSEL 4.9 27/48] ARM: dts: hisilicon: fix errors detected by simple-bus.yaml Sasha Levin
2020-12-23 2:23 ` [PATCH AUTOSEL 4.9 28/48] ARM: dts: hisilicon: fix errors detected by spi-pl022.yaml Sasha Levin
2020-12-23 2:23 ` [PATCH AUTOSEL 4.9 29/48] selftests/x86/fsgsbase: Fix GS == 1, 2, and 3 tests Sasha Levin
2020-12-23 2:23 ` [PATCH AUTOSEL 4.9 30/48] brcmsmac: ampdu: Check BA window size before checking block ack Sasha Levin
2020-12-23 2:23 ` Sasha Levin [this message]
2020-12-23 2:24 ` [PATCH AUTOSEL 4.9 32/48] crypto: qce - Fix SHA result buffer corruption issues Sasha Levin
2020-12-23 2:24 ` [PATCH AUTOSEL 4.9 33/48] media: gp8psk: initialize stats at power control logic Sasha Levin
2020-12-23 2:24 ` [PATCH AUTOSEL 4.9 34/48] net/lapb: fix t1 timer handling for LAPB_STATE_0 Sasha Levin
2020-12-23 2:24 ` [PATCH AUTOSEL 4.9 35/48] x86/mce: Panic for LMCE only if mca_cfg.tolerant < 3 Sasha Levin
2020-12-23 2:24 ` [PATCH AUTOSEL 4.9 36/48] MIPS: KASLR: Avoid endless loop in sync_icache if synci_step is zero Sasha Levin
2020-12-23 2:24 ` [PATCH AUTOSEL 4.9 37/48] mwifiex: Fix possible buffer overflows in mwifiex_cmd_802_11_ad_hoc_start Sasha Levin
2020-12-23 2:24 ` [PATCH AUTOSEL 4.9 38/48] misc: vmw_vmci: fix kernel info-leak by initializing dbells in vmci_ctx_get_chkpt_doorbells() Sasha Levin
2020-12-23 2:24 ` [PATCH AUTOSEL 4.9 39/48] iwlwifi: trans: consider firmware dead after errors Sasha Levin
2020-12-23 2:24 ` [PATCH AUTOSEL 4.9 40/48] iwlwifi: add an extra firmware state in the transport Sasha Levin
2020-12-23 2:24 ` [PATCH AUTOSEL 4.9 41/48] nl80211: always accept scan request with the duration set Sasha Levin
2020-12-23 2:24 ` [PATCH AUTOSEL 4.9 42/48] cfg80211: Save the regulatory domain when setting custom regulatory Sasha Levin
2020-12-23 2:24 ` [PATCH AUTOSEL 4.9 43/48] mac80211: disallow band-switch during CSA Sasha Levin
2020-12-23 2:24 ` [PATCH AUTOSEL 4.9 44/48] mac80211: Fix calculation of minimal channel width Sasha Levin
2020-12-23 2:24 ` [PATCH AUTOSEL 4.9 45/48] mac80211: don't filter out beacons once we start CSA Sasha Levin
2020-12-23 2:24 ` [PATCH AUTOSEL 4.9 46/48] mac80211: Update rate control on channel change Sasha Levin
2020-12-23 2:24 ` [PATCH AUTOSEL 4.9 47/48] ALSA: hda/hdmi: packet buffer index must be set before reading value Sasha Levin
2020-12-23 2:24 ` [PATCH AUTOSEL 4.9 48/48] PCI: Add function 1 DMA alias quirk for Marvell 9215 SATA controller Sasha Levin
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20201223022417.2794032-31-sashal@kernel.org \
--to=sashal@kernel.org \
--cc=digetx@gmail.com \
--cc=iommu@lists.linux-foundation.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-tegra@vger.kernel.org \
--cc=nicoleotsuka@gmail.com \
--cc=stable@vger.kernel.org \
--cc=treding@nvidia.com \
--cc=will@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
Powered by JetHome