From: Pierre-Louis Bossart <pierre-louis.bossart@linux.intel.com>
To: alsa-devel@alsa-project.org
Cc: Pierre-Louis Bossart <pierre-louis.bossart@linux.intel.com>,
tiwai@suse.de, gregkh@linuxfoundation.org,
linux-kernel@vger.kernel.org,
Ranjani Sridharan <ranjani.sridharan@linux.intel.com>,
vkoul@kernel.org, broonie@kernel.org,
srinivas.kandagatla@linaro.org, jank@cadence.com,
slawomir.blauciak@intel.com,
Sanyog Kale <sanyog.r.kale@intel.com>,
Bard liao <yung-chuan.liao@linux.intel.com>,
Rander Wang <rander.wang@linux.intel.com>
Subject: [alsa-devel] [PATCH v2 02/19] soundwire: fix race between driver probe and update_status callback
Date: Wed, 6 Nov 2019 13:22:06 -0600 [thread overview]
Message-ID: <20191106192223.6003-3-pierre-louis.bossart@linux.intel.com> (raw)
In-Reply-To: <20191106192223.6003-1-pierre-louis.bossart@linux.intel.com>
The driver probe takes care of basic initialization and is invoked
when a Slave becomes attached, after a match between the Slave DevID
registers and ACPI/DT entries.
The update_status callback is invoked when a Slave state changes,
e.g. when it is assigned a non-zero Device Number and it reports with
an ATTACHED/ALERT state.
The state change detection is usually hardware-based and based on the
SoundWire frame rate (e.g. double-digit microseconds) while the probe
is a pure software operation, which may involve a kernel module
load. In corner cases, it's possible that the state changes before the
probe completes.
This patch suggests the use of wait_for_completion to avoid races on
startup, so that the update_status callback does not rely on invalid
pointers/data structures.
Signed-off-by: Pierre-Louis Bossart <pierre-louis.bossart@linux.intel.com>
---
drivers/soundwire/bus.c | 24 +++++++++++++++++++++---
drivers/soundwire/bus.h | 1 +
drivers/soundwire/bus_type.c | 5 +++++
drivers/soundwire/slave.c | 2 ++
4 files changed, 29 insertions(+), 3 deletions(-)
diff --git a/drivers/soundwire/bus.c b/drivers/soundwire/bus.c
index 4b22ee996a65..903aee258800 100644
--- a/drivers/soundwire/bus.c
+++ b/drivers/soundwire/bus.c
@@ -961,10 +961,28 @@ static int sdw_handle_slave_alerts(struct sdw_slave *slave)
static int sdw_update_slave_status(struct sdw_slave *slave,
enum sdw_slave_status status)
{
- if (slave->ops && slave->ops->update_status)
- return slave->ops->update_status(slave, status);
+ unsigned long time;
- return 0;
+ if (!slave->ops || !slave->ops->update_status)
+ return 0;
+
+ if (!slave->probed) {
+ /*
+ * the slave status update is typically handled in an
+ * interrupt thread, which can race with the driver
+ * probe, e.g. when a module needs to be loaded.
+ *
+ * make sure the probe is complete before updating
+ * status.
+ */
+ time = wait_for_completion_timeout(&slave->probe_complete,
+ msecs_to_jiffies(DEFAULT_PROBE_TIMEOUT));
+ if (!time) {
+ dev_err(&slave->dev, "Probe not complete, timed out\n");
+ return -ETIMEDOUT;
+ }
+ }
+ return slave->ops->update_status(slave, status);
}
/**
diff --git a/drivers/soundwire/bus.h b/drivers/soundwire/bus.h
index cb482da914da..acb8d11a4c84 100644
--- a/drivers/soundwire/bus.h
+++ b/drivers/soundwire/bus.h
@@ -5,6 +5,7 @@
#define __SDW_BUS_H
#define DEFAULT_BANK_SWITCH_TIMEOUT 3000
+#define DEFAULT_PROBE_TIMEOUT 2000
#if IS_ENABLED(CONFIG_ACPI)
int sdw_acpi_find_slaves(struct sdw_bus *bus);
diff --git a/drivers/soundwire/bus_type.c b/drivers/soundwire/bus_type.c
index fd234f63bf2f..ac484384bd6e 100644
--- a/drivers/soundwire/bus_type.c
+++ b/drivers/soundwire/bus_type.c
@@ -118,6 +118,11 @@ static int sdw_slave_drv_probe(struct device *dev)
slave->bus->clk_stop_timeout = max_t(u32, slave->bus->clk_stop_timeout,
slave->prop.clk_stop_timeout);
+ slave->probed = true;
+ complete(&slave->probe_complete);
+
+ dev_dbg(dev, "probe complete\n");
+
return 0;
}
diff --git a/drivers/soundwire/slave.c b/drivers/soundwire/slave.c
index c87267f12a3b..81b94cd3985e 100644
--- a/drivers/soundwire/slave.c
+++ b/drivers/soundwire/slave.c
@@ -52,6 +52,8 @@ static int sdw_slave_add(struct sdw_bus *bus,
slave->bus = bus;
slave->status = SDW_SLAVE_UNATTACHED;
slave->dev_num = 0;
+ init_completion(&slave->probe_complete);
+ slave->probed = false;
mutex_lock(&bus->bus_lock);
list_add_tail(&slave->node, &bus->slaves);
--
2.20.1
_______________________________________________
Alsa-devel mailing list
Alsa-devel@alsa-project.org
https://mailman.alsa-project.org/mailman/listinfo/alsa-devel
next prev parent reply other threads:[~2019-11-06 19:26 UTC|newest]
Thread overview: 20+ messages / expand[flat|nested] mbox.gz Atom feed top
2019-11-06 19:22 [alsa-devel] [PATCH v2 00/19] soundwire: code hardening and suspend-resume support Pierre-Louis Bossart
2019-11-06 19:22 ` [alsa-devel] [PATCH v2 01/19] soundwire: intel/cadence: fix timeouts in MSI mode Pierre-Louis Bossart
2019-11-06 19:22 ` Pierre-Louis Bossart [this message]
2019-11-06 19:22 ` [alsa-devel] [PATCH v2 03/19] soundwire: bus: add PM/no-PM versions of read/write functions Pierre-Louis Bossart
2019-11-06 19:22 ` [alsa-devel] [PATCH v2 04/19] soundwire: bus: write Slave Device Number without runtime_pm Pierre-Louis Bossart
2019-11-06 19:22 ` [alsa-devel] [PATCH v2 05/19] soundwire: intel: add helpers for link power down and shim wake Pierre-Louis Bossart
2019-11-06 19:22 ` [alsa-devel] [PATCH v2 06/19] soundwire: intel: Add basic power management support Pierre-Louis Bossart
2019-11-06 19:22 ` [alsa-devel] [PATCH v2 07/19] soundwire: intel: add pm_runtime support Pierre-Louis Bossart
2019-11-06 19:22 ` [alsa-devel] [PATCH v2 08/19] soundwire: intel: reset pm_runtime status during system resume Pierre-Louis Bossart
2019-11-06 19:22 ` [alsa-devel] [PATCH v2 09/19] soundwire: bus: add helper to reset Slave status to UNATTACHED Pierre-Louis Bossart
2019-11-06 19:22 ` [alsa-devel] [PATCH v2 10/19] soundwire: intel: call helper to reset Slave states on resume Pierre-Louis Bossart
2019-11-06 19:22 ` [alsa-devel] [PATCH v2 11/19] soundwire: bus: check first if Slaves become UNATTACHED Pierre-Louis Bossart
2019-11-06 19:22 ` [alsa-devel] [PATCH v2 12/19] soundwire: add enumeration_complete signaling Pierre-Louis Bossart
2019-11-06 19:22 ` [alsa-devel] [PATCH v2 13/19] soundwire: bus: add initialization_complete signaling Pierre-Louis Bossart
2019-11-06 19:22 ` [alsa-devel] [PATCH v2 14/19] soundwire: intel: disable pm_runtime when removing a master Pierre-Louis Bossart
2019-11-06 19:22 ` [alsa-devel] [PATCH v2 15/19] soundwire: bus: disable pm_runtime in sdw_slave_delete Pierre-Louis Bossart
2019-11-06 19:22 ` [alsa-devel] [PATCH v2 16/19] soundwire: stream: update state machine and add state checks Pierre-Louis Bossart
2019-11-06 19:22 ` [alsa-devel] [PATCH v2 17/19] soundwire: stream: only prepare stream when it is configured Pierre-Louis Bossart
2019-11-06 19:22 ` [alsa-devel] [PATCH v2 18/19] soundwire: stream: do not update parameters during DISABLED-PREPARED transition Pierre-Louis Bossart
2019-11-06 19:22 ` [alsa-devel] [PATCH v2 19/19] soundwire: intel: reinitialize IP+DSP in .prepare() Pierre-Louis Bossart
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=20191106192223.6003-3-pierre-louis.bossart@linux.intel.com \
--to=pierre-louis.bossart@linux.intel.com \
--cc=alsa-devel@alsa-project.org \
--cc=broonie@kernel.org \
--cc=gregkh@linuxfoundation.org \
--cc=jank@cadence.com \
--cc=linux-kernel@vger.kernel.org \
--cc=rander.wang@linux.intel.com \
--cc=ranjani.sridharan@linux.intel.com \
--cc=sanyog.r.kale@intel.com \
--cc=slawomir.blauciak@intel.com \
--cc=srinivas.kandagatla@linaro.org \
--cc=tiwai@suse.de \
--cc=vkoul@kernel.org \
--cc=yung-chuan.liao@linux.intel.com \
/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;
as well as URLs for NNTP newsgroup(s).