Commit cc4ef5e3 authored by Alexander Shishkin's avatar Alexander Shishkin Committed by Stefan Bader

stm class: Guard output assignment against concurrency

BugLink: https://bugs.launchpad.net/bugs/1826212

[ Upstream commit cde4ad83 ]

It is possible to concurrently assign the same output (a character
device writer or an stm_source device) to different stm devices,
which sets off a strategically placed warning in stm_output_assign().

To avoid this, use a spinlock to serialize (un)assignments between
outputs and stm devices.
Signed-off-by: default avatarAlexander Shishkin <alexander.shishkin@linux.intel.com>
Signed-off-by: default avatarGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Signed-off-by: default avatarSasha Levin <sashal@kernel.org>
Signed-off-by: default avatarStefan Bader <stefan.bader@canonical.com>
Signed-off-by: default avatarKleber Sacilotto de Souza <kleber.souza@canonical.com>
parent b62892ca
...@@ -186,6 +186,9 @@ static void stm_output_claim(struct stm_device *stm, struct stm_output *output) ...@@ -186,6 +186,9 @@ static void stm_output_claim(struct stm_device *stm, struct stm_output *output)
{ {
struct stp_master *master = stm_master(stm, output->master); struct stp_master *master = stm_master(stm, output->master);
lockdep_assert_held(&stm->mc_lock);
lockdep_assert_held(&output->lock);
if (WARN_ON_ONCE(master->nr_free < output->nr_chans)) if (WARN_ON_ONCE(master->nr_free < output->nr_chans))
return; return;
...@@ -200,6 +203,9 @@ stm_output_disclaim(struct stm_device *stm, struct stm_output *output) ...@@ -200,6 +203,9 @@ stm_output_disclaim(struct stm_device *stm, struct stm_output *output)
{ {
struct stp_master *master = stm_master(stm, output->master); struct stp_master *master = stm_master(stm, output->master);
lockdep_assert_held(&stm->mc_lock);
lockdep_assert_held(&output->lock);
bitmap_release_region(&master->chan_map[0], output->channel, bitmap_release_region(&master->chan_map[0], output->channel,
ilog2(output->nr_chans)); ilog2(output->nr_chans));
...@@ -292,6 +298,7 @@ static int stm_output_assign(struct stm_device *stm, unsigned int width, ...@@ -292,6 +298,7 @@ static int stm_output_assign(struct stm_device *stm, unsigned int width,
} }
spin_lock(&stm->mc_lock); spin_lock(&stm->mc_lock);
spin_lock(&output->lock);
/* output is already assigned -- shouldn't happen */ /* output is already assigned -- shouldn't happen */
if (WARN_ON_ONCE(output->nr_chans)) if (WARN_ON_ONCE(output->nr_chans))
goto unlock; goto unlock;
...@@ -308,6 +315,7 @@ static int stm_output_assign(struct stm_device *stm, unsigned int width, ...@@ -308,6 +315,7 @@ static int stm_output_assign(struct stm_device *stm, unsigned int width,
ret = 0; ret = 0;
unlock: unlock:
spin_unlock(&output->lock);
spin_unlock(&stm->mc_lock); spin_unlock(&stm->mc_lock);
return ret; return ret;
...@@ -316,11 +324,18 @@ static int stm_output_assign(struct stm_device *stm, unsigned int width, ...@@ -316,11 +324,18 @@ static int stm_output_assign(struct stm_device *stm, unsigned int width,
static void stm_output_free(struct stm_device *stm, struct stm_output *output) static void stm_output_free(struct stm_device *stm, struct stm_output *output)
{ {
spin_lock(&stm->mc_lock); spin_lock(&stm->mc_lock);
spin_lock(&output->lock);
if (output->nr_chans) if (output->nr_chans)
stm_output_disclaim(stm, output); stm_output_disclaim(stm, output);
spin_unlock(&output->lock);
spin_unlock(&stm->mc_lock); spin_unlock(&stm->mc_lock);
} }
static void stm_output_init(struct stm_output *output)
{
spin_lock_init(&output->lock);
}
static int major_match(struct device *dev, const void *data) static int major_match(struct device *dev, const void *data)
{ {
unsigned int major = *(unsigned int *)data; unsigned int major = *(unsigned int *)data;
...@@ -343,6 +358,7 @@ static int stm_char_open(struct inode *inode, struct file *file) ...@@ -343,6 +358,7 @@ static int stm_char_open(struct inode *inode, struct file *file)
if (!stmf) if (!stmf)
return -ENOMEM; return -ENOMEM;
stm_output_init(&stmf->output);
stmf->stm = to_stm_device(dev); stmf->stm = to_stm_device(dev);
if (!try_module_get(stmf->stm->owner)) if (!try_module_get(stmf->stm->owner))
...@@ -953,6 +969,7 @@ int stm_source_register_device(struct device *parent, ...@@ -953,6 +969,7 @@ int stm_source_register_device(struct device *parent,
if (err) if (err)
goto err; goto err;
stm_output_init(&src->output);
spin_lock_init(&src->link_lock); spin_lock_init(&src->link_lock);
INIT_LIST_HEAD(&src->link_entry); INIT_LIST_HEAD(&src->link_entry);
src->data = data; src->data = data;
......
...@@ -57,6 +57,7 @@ struct stm_device { ...@@ -57,6 +57,7 @@ struct stm_device {
container_of((_d), struct stm_device, dev) container_of((_d), struct stm_device, dev)
struct stm_output { struct stm_output {
spinlock_t lock;
unsigned int master; unsigned int master;
unsigned int channel; unsigned int channel;
unsigned int nr_chans; unsigned int nr_chans;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment