stm class: Guard output assignment against concurrency
authorAlexander Shishkin <alexander.shishkin@linux.intel.com>
Mon, 15 Feb 2016 17:12:06 +0000 (19:12 +0200)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Sat, 20 Feb 2016 22:09:14 +0000 (14:09 -0800)
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: Alexander Shishkin <alexander.shishkin@linux.intel.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
drivers/hwtracing/stm/core.c
drivers/hwtracing/stm/stm.h

index 4a626d8990b22d4cc4efb0ea3ecfa1dce84e2f95..f6ade21729fab9d5a4c8ccefe2782447ac3dc97a 100644 (file)
@@ -185,6 +185,9 @@ static void stm_output_claim(struct stm_device *stm, struct stm_output *output)
 {
        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))
                return;
 
@@ -199,6 +202,9 @@ stm_output_disclaim(struct stm_device *stm, struct stm_output *output)
 {
        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,
                              ilog2(output->nr_chans));
 
@@ -288,6 +294,7 @@ static int stm_output_assign(struct stm_device *stm, unsigned int width,
        }
 
        spin_lock(&stm->mc_lock);
+       spin_lock(&output->lock);
        /* output is already assigned -- shouldn't happen */
        if (WARN_ON_ONCE(output->nr_chans))
                goto unlock;
@@ -304,6 +311,7 @@ static int stm_output_assign(struct stm_device *stm, unsigned int width,
 
        ret = 0;
 unlock:
+       spin_unlock(&output->lock);
        spin_unlock(&stm->mc_lock);
 
        return ret;
@@ -312,11 +320,18 @@ unlock:
 static void stm_output_free(struct stm_device *stm, struct stm_output *output)
 {
        spin_lock(&stm->mc_lock);
+       spin_lock(&output->lock);
        if (output->nr_chans)
                stm_output_disclaim(stm, output);
+       spin_unlock(&output->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)
 {
        unsigned int major = *(unsigned int *)data;
@@ -339,6 +354,7 @@ static int stm_char_open(struct inode *inode, struct file *file)
        if (!stmf)
                return -ENOMEM;
 
+       stm_output_init(&stmf->output);
        stmf->stm = to_stm_device(dev);
 
        if (!try_module_get(stmf->stm->owner))
@@ -952,6 +968,7 @@ int stm_source_register_device(struct device *parent,
        if (err)
                goto err;
 
+       stm_output_init(&src->output);
        spin_lock_init(&src->link_lock);
        INIT_LIST_HEAD(&src->link_entry);
        src->data = data;
index 97ee0224144067e3d6fe0a32e6a9466aad04268c..4e8c6926260f3e8eec0ec8da70de8f13f86f0cc6 100644 (file)
@@ -57,6 +57,7 @@ struct stm_device {
        container_of((_d), struct stm_device, dev)
 
 struct stm_output {
+       spinlock_t              lock;
        unsigned int            master;
        unsigned int            channel;
        unsigned int            nr_chans;