Skip to contentSkip to conversation

opp: Fixup release of clock when using set/put clkname

RFC In review Rajendra Nayak 2 participants 4 messages Apr 14, 2020 – Apr 15, 2020

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

RFC 1/1 opp: Fixup release of clock when using set/put clkname

Fixup dev_pm_opp_put_clkname() to check for a valid clock pointer before it does a clk_put, since its likely that _opp_table_kref_release() has already done a clk_put. Also fixup _opp_table_kref_release() to set the clock pointer to ERR_PTR(-EINVAL) after its done doing a clk_put so dev_pm_opp_put_clkname() can then catch it.

---
 drivers/opp/core.c | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)
drivers/opp/core.c +7 -3
index e4f01e7..6d064a8 100644
@@ -1061,8 +1061,10 @@ static void _opp_table_kref_release(struct kref *kref)
 	_of_clear_opp_table(opp_table);
 
 	/* Release clk */
-	if (!IS_ERR(opp_table->clk))
+	if (!IS_ERR(opp_table->clk)) {
 		clk_put(opp_table->clk);
+		opp_table->clk = ERR_PTR(-EINVAL);
+	}
 
 	WARN_ON(!list_empty(&opp_table->opp_list));
 
@@ -1744,8 +1746,10 @@ void dev_pm_opp_put_clkname(struct opp_table *opp_table)
 	/* Make sure there are no concurrent readers while updating opp_table */
 	WARN_ON(!list_empty(&opp_table->opp_list));
 
-	clk_put(opp_table->clk);
-	opp_table->clk = ERR_PTR(-EINVAL);
+	if (!IS_ERR(opp_table->clk)) {
+		clk_put(opp_table->clk);
+		opp_table->clk = ERR_PTR(-EINVAL);
+	}
 
 	dev_pm_opp_put_opp_table(opp_table);
 }
Signature · 2 lines
QUALCOMM INDIA, on behalf of Qualcomm Innovation Center, Inc. is a member
of Code Aurora Forum, hosted by The Linux Foundation
#1 lore raw
Quoted 3 lines · Rajendra Nayak

Fixup dev_pm_opp_put_clkname() to check for a valid clock pointer before it does a clk_put, since its likely that _opp_table_kref_release() has already done a clk_put. Also fixup

kref release is the last thing that happens on the table, it can't get called after dev_pm_opp_put_clkname().

Quoted 41 lines

_opp_table_kref_release() to set the clock pointer to ERR_PTR(-EINVAL) after its done doing a clk_put so dev_pm_opp_put_clkname() can then catch it.

---
 drivers/opp/core.c | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)
drivers/opp/core.c +7 -3
index e4f01e7..6d064a8 100644
@@ -1061,8 +1061,10 @@ static void _opp_table_kref_release(struct kref *kref)
 	_of_clear_opp_table(opp_table);
 
 	/* Release clk */
-	if (!IS_ERR(opp_table->clk))
+	if (!IS_ERR(opp_table->clk)) {
 		clk_put(opp_table->clk);
+		opp_table->clk = ERR_PTR(-EINVAL);
+	}
 
 	WARN_ON(!list_empty(&opp_table->opp_list));
 
@@ -1744,8 +1746,10 @@ void dev_pm_opp_put_clkname(struct opp_table *opp_table)
 	/* Make sure there are no concurrent readers while updating opp_table */
 	WARN_ON(!list_empty(&opp_table->opp_list));
 
-	clk_put(opp_table->clk);
-	opp_table->clk = ERR_PTR(-EINVAL);
+	if (!IS_ERR(opp_table->clk)) {
+		clk_put(opp_table->clk);
+		opp_table->clk = ERR_PTR(-EINVAL);
+	}
 
 	dev_pm_opp_put_opp_table(opp_table);
 }
Signature · 2 lines
QUALCOMM INDIA, on behalf of Qualcomm Innovation Center, Inc. is a member
of Code Aurora Forum, hosted by The Linux Foundation
Signature · 1 line
viresh
#2 lore raw
Quoted 7 lines · Viresh Kumar
Quoted 3 lines · Rajendra Nayak

Fixup dev_pm_opp_put_clkname() to check for a valid clock pointer before it does a clk_put, since its likely that _opp_table_kref_release() has already done a clk_put. Also fixup

kref release is the last thing that happens on the table, it can't get called after dev_pm_opp_put_clkname().

so in cases where I do have an OPP table for a device in DT and I do dev_pm_opp_set_clkname()
dev_pm_opp_of_add_table()

and clean it up with a
dev_pm_opp_of_remove_table()
dev_pm_opp_put_clkname()

things seem to work fine as expected.

However, given I call these unconditionally in common drivers and on some (old) platforms we really don't have an OPP table (only scale clocks) things get a little tricky. So looks like the ref counting in such cases gets messed up, and we end up with dev_pm_opp_of_remove_table() calling _opp_table_kref_release() and releasing the clock, and then the subsequent call to dev_pm_opp_put_clkname() crashes.

Quoted 42 lines
Quoted 41 lines

_opp_table_kref_release() to set the clock pointer to ERR_PTR(-EINVAL) after its done doing a clk_put so dev_pm_opp_put_clkname() can then catch it.

---
  drivers/opp/core.c | 10 +++++++---
  1 file changed, 7 insertions(+), 3 deletions(-)
drivers/opp/core.c +7 -3
index e4f01e7..6d064a8 100644
@@ -1061,8 +1061,10 @@ static void _opp_table_kref_release(struct kref *kref)
  	_of_clear_opp_table(opp_table);
 
  	/* Release clk */
-	if (!IS_ERR(opp_table->clk))
+	if (!IS_ERR(opp_table->clk)) {
  		clk_put(opp_table->clk);
+		opp_table->clk = ERR_PTR(-EINVAL);
+	}
 
  	WARN_ON(!list_empty(&opp_table->opp_list));
 
@@ -1744,8 +1746,10 @@ void dev_pm_opp_put_clkname(struct opp_table *opp_table)
  	/* Make sure there are no concurrent readers while updating opp_table */
  	WARN_ON(!list_empty(&opp_table->opp_list));
 
-	clk_put(opp_table->clk);
-	opp_table->clk = ERR_PTR(-EINVAL);
+	if (!IS_ERR(opp_table->clk)) {
+		clk_put(opp_table->clk);
+		opp_table->clk = ERR_PTR(-EINVAL);
+	}
 
  	dev_pm_opp_put_opp_table(opp_table);
  }
Signature · 2 lines
QUALCOMM INDIA, on behalf of Qualcomm Innovation Center, Inc. is a member
of Code Aurora Forum, hosted by The Linux Foundation
Signature · 2 lines
QUALCOMM INDIA, on behalf of Qualcomm Innovation Center, Inc. is a member
of Code Aurora Forum, hosted by The Linux Foundation
#3 lore raw
Quoted 6 lines · Rajendra Nayak

However, given I call these unconditionally in common drivers and on some (old) platforms we really don't have an OPP table (only scale clocks) things get a little tricky. So looks like the ref counting in such cases gets messed up, and we end up with dev_pm_opp_of_remove_table() calling _opp_table_kref_release() and releasing the clock, and then the subsequent call to dev_pm_opp_put_clkname() crashes.

We aren't supposed to call dev_pm_opp_of_remove_table() if dev_pm_opp_of_add_table() isn't called or it failed. Either we can handle this in core, which I would like to avoid, or just keep a variable to track it in the driver, which will be easy enough I guess.

Signature · 1 line
viresh