mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Alexander Shishkin <alexander.shishkin@linux.intel.com>
To: Greg KH <greg@kroah.com>
Cc: Mathieu Poirier <mathieu.poirier@linaro.org>,
	Chunyan Zhang <zhang.chunyan@linaro.org>,
	laurent.fert@intel.com, yann.fouassier@intel.com,
	linux-kernel@vger.kernel.org,
	Alexander Shishkin <alexander.shishkin@linux.intel.com>
Subject: [PATCH 16/20] stm class: Guard output assignment against concurrency
Date: Mon, 15 Feb 2016 19:12:06 +0200	[thread overview]
Message-ID: <1455556330-12473-17-git-send-email-alexander.shishkin@linux.intel.com> (raw)
In-Reply-To: <1455556330-12473-1-git-send-email-alexander.shishkin@linux.intel.com>

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>
---
 drivers/hwtracing/stm/core.c | 17 +++++++++++++++++
 drivers/hwtracing/stm/stm.h  |  1 +
 2 files changed, 18 insertions(+)

diff --git a/drivers/hwtracing/stm/core.c b/drivers/hwtracing/stm/core.c
index 4a626d8990..f6ade21729 100644
--- a/drivers/hwtracing/stm/core.c
+++ b/drivers/hwtracing/stm/core.c
@@ -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;
diff --git a/drivers/hwtracing/stm/stm.h b/drivers/hwtracing/stm/stm.h
index 97ee022414..4e8c692626 100644
--- a/drivers/hwtracing/stm/stm.h
+++ b/drivers/hwtracing/stm/stm.h
@@ -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;
-- 
2.7.0

  parent reply	other threads:[~2016-02-15 17:14 UTC|newest]

Thread overview: 24+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-02-15 16:08 [git pull] stm class/intel_th: Updates for char-misc-next Alexander Shishkin
2016-02-15 16:44 ` Greg KH
2016-02-15 17:11   ` [PATCH 00/20] " Alexander Shishkin
2016-02-15 17:11     ` [PATCH 01/20] stm class: Use a signed return type for stm_find_master_chan Alexander Shishkin
2016-02-15 17:11     ` [PATCH 02/20] stm class: Fix master deallocation in device unregistering Alexander Shishkin
2016-02-15 17:11     ` [PATCH 03/20] intel_th: Depend on HAS_IOMEM Alexander Shishkin
2016-02-15 17:11     ` [PATCH 04/20] intel_th: gth: Remove commented-out code Alexander Shishkin
2016-02-15 17:11     ` [PATCH 05/20] intel_th: Update scratchpad bits according to enabled output activity Alexander Shishkin
2016-02-15 17:11     ` [PATCH 06/20] intel_th: msu: Fix offset for wrapped block Alexander Shishkin
2016-02-15 17:11     ` [PATCH 07/20] intel_th: msu: Release resources on read error Alexander Shishkin
2016-02-15 17:11     ` [PATCH 08/20] intel_th: sth: Sanitize packet callback's return values Alexander Shishkin
2016-02-15 17:11     ` [PATCH 09/20] intel_th: Set root device's drvdata early Alexander Shishkin
2016-02-15 17:12     ` [PATCH 10/20] intel_th: Use real device index in the node names Alexander Shishkin
2016-02-15 17:12     ` [PATCH 11/20] stm class: Use driver's packet callback return value Alexander Shishkin
2016-02-15 17:12     ` [PATCH 12/20] stm class: Support devices with multiple instances Alexander Shishkin
2016-02-15 17:12     ` [PATCH 13/20] stm class: dummy_stm: Create multiple devices Alexander Shishkin
2016-02-15 17:12     ` [PATCH 14/20] stm class: Add heartbeat stm source device Alexander Shishkin
2016-02-15 17:12     ` [PATCH 15/20] stm class: Fix unlocking braino in the error path Alexander Shishkin
2016-02-15 17:12     ` Alexander Shishkin [this message]
2016-02-15 17:12     ` [PATCH 17/20] stm class: Fix unbalanced module/device refcounting Alexander Shishkin
2016-02-15 17:12     ` [PATCH 18/20] stm class: Fix a race in unlinking Alexander Shishkin
2016-02-15 17:12     ` [PATCH 19/20] stm class: Plug stm device's unlink callback Alexander Shishkin
2016-02-15 17:12     ` [PATCH 20/20] stm class: dummy_stm: Add link callback for fault injection Alexander Shishkin
2016-02-16 12:29   ` [git pull] stm class/intel_th: Updates for char-misc-next Alexander Shishkin

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=1455556330-12473-17-git-send-email-alexander.shishkin@linux.intel.com \
    --to=alexander.shishkin@linux.intel.com \
    --cc=greg@kroah.com \
    --cc=laurent.fert@intel.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mathieu.poirier@linaro.org \
    --cc=yann.fouassier@intel.com \
    --cc=zhang.chunyan@linaro.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