linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v2] opp: use list iterator only inside the loop
@ 2022-03-31  2:36 Xiaomeng Tong
  2022-03-31  2:49 ` Viresh Kumar
  0 siblings, 1 reply; 6+ messages in thread
From: Xiaomeng Tong @ 2022-03-31  2:36 UTC (permalink / raw)
  To: vireshk, nm, sboyd; +Cc: linux-pm, linux-kernel, Xiaomeng Tong

The list iterator 'new_dev' will point to a bogus position containing
HEAD if any one of these conditions is possible: the list is empty or
no element is found, thus can potentially lead to an invalid memory
access in 'dev = new_dev->dev;'.

As discussed before, we should avoid to use a list iterator variable
outside the loop which is considered harmful[1].

In this case, use a new variable 'iter' as the list iterator, while
use the old variable 'new_dev' as a dedicated pointer to point to the
found entry. And BUG_ON(!new_dev);.

[1]:  https://lkml.org/lkml/2022/2/17/1032

Signed-off-by: Xiaomeng Tong <xiam0nd.tong@gmail.com>
---

changes since v1:
 - use BUG_ON(!new_dev); instead of return; (Viresh Kumar)

v1: https://lore.kernel.org/lkml/20220331015818.28045-1-xiam0nd.tong@gmail.com/

---
 drivers/opp/debugfs.c | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)

diff --git a/drivers/opp/debugfs.c b/drivers/opp/debugfs.c
index 596c185b5dda..81b2bc4b5f43 100644
--- a/drivers/opp/debugfs.c
+++ b/drivers/opp/debugfs.c
@@ -187,14 +187,18 @@ void opp_debug_register(struct opp_device *opp_dev, struct opp_table *opp_table)
 static void opp_migrate_dentry(struct opp_device *opp_dev,
 			       struct opp_table *opp_table)
 {
-	struct opp_device *new_dev;
+	struct opp_device *new_dev = NULL, *iter;
 	const struct device *dev;
 	struct dentry *dentry;
 
 	/* Look for next opp-dev */
-	list_for_each_entry(new_dev, &opp_table->dev_list, node)
-		if (new_dev != opp_dev)
+	list_for_each_entry(iter, &opp_table->dev_list, node)
+		if (iter != opp_dev) {
+			new_dev = iter;
 			break;
+		}
+
+	BUG_ON(!new_dev);
 
 	/* new_dev is guaranteed to be valid here */
 	dev = new_dev->dev;
-- 
2.17.1


^ permalink raw reply related	[flat|nested] 6+ messages in thread
* [PATCH v2] opp: use list iterator only inside the loop
@ 2022-03-31  8:30 Xiaomeng Tong
  2022-03-31  8:44 ` Viresh Kumar
  2022-04-11  3:02 ` Viresh Kumar
  0 siblings, 2 replies; 6+ messages in thread
From: Xiaomeng Tong @ 2022-03-31  8:30 UTC (permalink / raw)
  To: vireshk, nm, sboyd; +Cc: linux-pm, linux-kernel, Xiaomeng Tong

To move the list iterator variable into the list_for_each_entry_*()
macro in the future it should be avoided to use the list iterator
variable after the loop body.

To *never* use the list iterator variable after the loop it was
concluded to use a separate dedicated pointer variable [1].

In this case, use a new variable 'iter' as the list iterator, while
use the old variable 'new_dev' as a dedicated pointer to point to the
found entry. And BUG_ON(!new_dev);.

[1]: https://lore.kernel.org/all/CAHk-=wgRr_D8CB-D9Kg-c=EHreAsk5SqXPwr9Y7k9sA6cWXJ6w@mail.gmail.com/

Signed-off-by: Xiaomeng Tong <xiam0nd.tong@gmail.com>
---

changes since v1:
 - use BUG_ON(!new_dev); instead of return; (Viresh Kumar)

v1: https://lore.kernel.org/lkml/20220331015818.28045-1-xiam0nd.tong@gmail.com/

---
 drivers/opp/debugfs.c | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)

diff --git a/drivers/opp/debugfs.c b/drivers/opp/debugfs.c
index 596c185b5dda..81b2bc4b5f43 100644
--- a/drivers/opp/debugfs.c
+++ b/drivers/opp/debugfs.c
@@ -187,14 +187,18 @@ void opp_debug_register(struct opp_device *opp_dev, struct opp_table *opp_table)
 static void opp_migrate_dentry(struct opp_device *opp_dev,
 			       struct opp_table *opp_table)
 {
-	struct opp_device *new_dev;
+	struct opp_device *new_dev = NULL, *iter;
 	const struct device *dev;
 	struct dentry *dentry;
 
 	/* Look for next opp-dev */
-	list_for_each_entry(new_dev, &opp_table->dev_list, node)
-		if (new_dev != opp_dev)
+	list_for_each_entry(iter, &opp_table->dev_list, node)
+		if (iter != opp_dev) {
+			new_dev = iter;
 			break;
+		}
+
+	BUG_ON(!new_dev);
 
 	/* new_dev is guaranteed to be valid here */
 	dev = new_dev->dev;
-- 
2.17.1


^ permalink raw reply related	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2022-04-11  3:02 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-03-31  2:36 [PATCH v2] opp: use list iterator only inside the loop Xiaomeng Tong
2022-03-31  2:49 ` Viresh Kumar
2022-03-31  8:30 Xiaomeng Tong
2022-03-31  8:44 ` Viresh Kumar
2022-03-31  8:51   ` Xiaomeng Tong
2022-04-11  3:02 ` Viresh Kumar

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).