All of lore.kernel.org
 help / color / mirror / Atom feed
From: Mao Jinlong <quic_jinlmao@quicinc.com>
To: Alexander Shishkin <alexander.shishkin@linux.intel.com>,
	Maxime Coquelin <mcoquelin.stm32@gmail.com>,
	Alexandre Torgue <alexandre.torgue@foss.st.com>
Cc: Mao Jinlong <quic_jinlmao@quicinc.com>,
	<linux-stm32@st-md-mailman.stormreply.com>,
	<linux-arm-msm@vger.kernel.org>,
	<linux-arm-kernel@lists.infradead.org>,
	<linux-kernel@vger.kernel.org>,
	Tingwei Zhang <quic_tingweiz@quicinc.com>,
	Yuanfang Zhang <quic_yuanfang@quicinc.com>,
	Tao Zhang <quic_taozha@quicinc.com>,
	Hao Zhang <quic_hazha@quicinc.com>
Subject: [PATCH] stm class: Fix double add issue when store source_link
Date: Mon, 18 Apr 2022 19:46:58 +0800	[thread overview]
Message-ID: <20220418114658.6491-1-quic_jinlmao@quicinc.com> (raw)

If two threads store the same stm device to stm_source_link
at the same time when stm->link_list is empty, it is possible
that stm_source_link_add will be called for both of these two
threads. Then double add issue below will happen. Add mutex
lock for stm_source_link drop and stm_source_link add to avoid
this race condition.

[ 12.386579][ T1024] list_add double add: new=ffffff87b73ebd90,
prev=ffffff87b73ebd90, next=ffffffc012737700.
[ 12.386657][ T1024] -----------[ cut here ]-----------
[ 12.386671][ T1024] kernel BUG at lib/list_debug.c:31!
[ 12.388845][ T1024] CPU: 2 PID: 1024 Comm: sh
[ 12.389162][ T1024] Call trace:
[ 12.389174][ T1024] __list_add_valid+0x68/0x98
[ 12.389199][ T1024] stm_source_link_store+0xcc/0x314 [stm_core]
[ 12.389213][ T1024] dev_attr_store+0x38/0x8c
[ 12.389228][ T1024] sysfs_kf_write+0xa0/0x100
[ 12.389239][ T1024] kernfs_fop_write_iter+0x1b0/0x2f8
[ 12.389253][ T1024] vfs_write+0x300/0x37c
[ 12.389264][ T1024] ksys_write+0x84/0x12c

Signed-off-by: Yuanfang Zhang <quic_yuanfang@quicinc.com>
Signed-off-by: Mao Jinlong <quic_jinlmao@quicinc.com>
---
 drivers/hwtracing/stm/core.c | 7 ++++++-
 drivers/hwtracing/stm/stm.h  | 1 +
 2 files changed, 7 insertions(+), 1 deletion(-)

diff --git a/drivers/hwtracing/stm/core.c b/drivers/hwtracing/stm/core.c
index 2712e699ba08..e73ac961acb2 100644
--- a/drivers/hwtracing/stm/core.c
+++ b/drivers/hwtracing/stm/core.c
@@ -1171,11 +1171,14 @@ static ssize_t stm_source_link_store(struct device *dev,
 	struct stm_device *link;
 	int err;
 
+	mutex_lock(&src->link_mutex);
 	stm_source_link_drop(src);
 
 	link = stm_find_device(buf);
-	if (!link)
+	if (!link) {
+		mutex_lock(&src->link_mutex);
 		return -EINVAL;
+	}
 
 	pm_runtime_get(&link->dev);
 
@@ -1185,6 +1188,7 @@ static ssize_t stm_source_link_store(struct device *dev,
 		/* matches the stm_find_device() above */
 		stm_put_device(link);
 	}
+	mutex_unlock(&src->link_mutex);
 
 	return err ? : count;
 }
@@ -1251,6 +1255,7 @@ int stm_source_register_device(struct device *parent,
 
 	stm_output_init(&src->output);
 	spin_lock_init(&src->link_lock);
+	mutex_init(&src->link_mutex);
 	INIT_LIST_HEAD(&src->link_entry);
 	src->data = data;
 	data->src = src;
diff --git a/drivers/hwtracing/stm/stm.h b/drivers/hwtracing/stm/stm.h
index a9be49fc7a6b..60b814cc00e0 100644
--- a/drivers/hwtracing/stm/stm.h
+++ b/drivers/hwtracing/stm/stm.h
@@ -79,6 +79,7 @@ void stm_put_device(struct stm_device *stm);
 struct stm_source_device {
 	struct device		dev;
 	struct stm_source_data	*data;
+	struct mutex		link_mutex;
 	spinlock_t		link_lock;
 	struct stm_device __rcu	*link;
 	struct list_head	link_entry;
-- 
2.17.1


WARNING: multiple messages have this Message-ID (diff)
From: Mao Jinlong <quic_jinlmao@quicinc.com>
To: Alexander Shishkin <alexander.shishkin@linux.intel.com>,
	Maxime Coquelin <mcoquelin.stm32@gmail.com>,
	Alexandre Torgue <alexandre.torgue@foss.st.com>
Cc: Mao Jinlong <quic_jinlmao@quicinc.com>,
	<linux-stm32@st-md-mailman.stormreply.com>,
	<linux-arm-msm@vger.kernel.org>,
	<linux-arm-kernel@lists.infradead.org>,
	<linux-kernel@vger.kernel.org>,
	Tingwei Zhang <quic_tingweiz@quicinc.com>,
	Yuanfang Zhang <quic_yuanfang@quicinc.com>,
	Tao Zhang <quic_taozha@quicinc.com>,
	Hao Zhang <quic_hazha@quicinc.com>
Subject: [PATCH] stm class: Fix double add issue when store source_link
Date: Mon, 18 Apr 2022 19:46:58 +0800	[thread overview]
Message-ID: <20220418114658.6491-1-quic_jinlmao@quicinc.com> (raw)

If two threads store the same stm device to stm_source_link
at the same time when stm->link_list is empty, it is possible
that stm_source_link_add will be called for both of these two
threads. Then double add issue below will happen. Add mutex
lock for stm_source_link drop and stm_source_link add to avoid
this race condition.

[ 12.386579][ T1024] list_add double add: new=ffffff87b73ebd90,
prev=ffffff87b73ebd90, next=ffffffc012737700.
[ 12.386657][ T1024] -----------[ cut here ]-----------
[ 12.386671][ T1024] kernel BUG at lib/list_debug.c:31!
[ 12.388845][ T1024] CPU: 2 PID: 1024 Comm: sh
[ 12.389162][ T1024] Call trace:
[ 12.389174][ T1024] __list_add_valid+0x68/0x98
[ 12.389199][ T1024] stm_source_link_store+0xcc/0x314 [stm_core]
[ 12.389213][ T1024] dev_attr_store+0x38/0x8c
[ 12.389228][ T1024] sysfs_kf_write+0xa0/0x100
[ 12.389239][ T1024] kernfs_fop_write_iter+0x1b0/0x2f8
[ 12.389253][ T1024] vfs_write+0x300/0x37c
[ 12.389264][ T1024] ksys_write+0x84/0x12c

Signed-off-by: Yuanfang Zhang <quic_yuanfang@quicinc.com>
Signed-off-by: Mao Jinlong <quic_jinlmao@quicinc.com>
---
 drivers/hwtracing/stm/core.c | 7 ++++++-
 drivers/hwtracing/stm/stm.h  | 1 +
 2 files changed, 7 insertions(+), 1 deletion(-)

diff --git a/drivers/hwtracing/stm/core.c b/drivers/hwtracing/stm/core.c
index 2712e699ba08..e73ac961acb2 100644
--- a/drivers/hwtracing/stm/core.c
+++ b/drivers/hwtracing/stm/core.c
@@ -1171,11 +1171,14 @@ static ssize_t stm_source_link_store(struct device *dev,
 	struct stm_device *link;
 	int err;
 
+	mutex_lock(&src->link_mutex);
 	stm_source_link_drop(src);
 
 	link = stm_find_device(buf);
-	if (!link)
+	if (!link) {
+		mutex_lock(&src->link_mutex);
 		return -EINVAL;
+	}
 
 	pm_runtime_get(&link->dev);
 
@@ -1185,6 +1188,7 @@ static ssize_t stm_source_link_store(struct device *dev,
 		/* matches the stm_find_device() above */
 		stm_put_device(link);
 	}
+	mutex_unlock(&src->link_mutex);
 
 	return err ? : count;
 }
@@ -1251,6 +1255,7 @@ int stm_source_register_device(struct device *parent,
 
 	stm_output_init(&src->output);
 	spin_lock_init(&src->link_lock);
+	mutex_init(&src->link_mutex);
 	INIT_LIST_HEAD(&src->link_entry);
 	src->data = data;
 	data->src = src;
diff --git a/drivers/hwtracing/stm/stm.h b/drivers/hwtracing/stm/stm.h
index a9be49fc7a6b..60b814cc00e0 100644
--- a/drivers/hwtracing/stm/stm.h
+++ b/drivers/hwtracing/stm/stm.h
@@ -79,6 +79,7 @@ void stm_put_device(struct stm_device *stm);
 struct stm_source_device {
 	struct device		dev;
 	struct stm_source_data	*data;
+	struct mutex		link_mutex;
 	spinlock_t		link_lock;
 	struct stm_device __rcu	*link;
 	struct list_head	link_entry;
-- 
2.17.1


_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

             reply	other threads:[~2022-04-18 11:47 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-04-18 11:46 Mao Jinlong [this message]
2022-04-18 11:46 ` [PATCH] stm class: Fix double add issue when store source_link Mao Jinlong
2022-05-16  7:14 ` Jinlong Mao
2022-05-16  7:14   ` Jinlong Mao
2022-11-02  9:34   ` Jinlong Mao
2022-11-02  9:34     ` Jinlong Mao

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=20220418114658.6491-1-quic_jinlmao@quicinc.com \
    --to=quic_jinlmao@quicinc.com \
    --cc=alexander.shishkin@linux.intel.com \
    --cc=alexandre.torgue@foss.st.com \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-arm-msm@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-stm32@st-md-mailman.stormreply.com \
    --cc=mcoquelin.stm32@gmail.com \
    --cc=quic_hazha@quicinc.com \
    --cc=quic_taozha@quicinc.com \
    --cc=quic_tingweiz@quicinc.com \
    --cc=quic_yuanfang@quicinc.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.