Skip to contentSkip to conversation

irqchip/imx-irqsteer: Convert to devm_pm_runtime_enable()

PATCH Superseded Fabio Estevam 3 participants 7 messages Aug 5 – Sep 13

Subsystems (2)

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Fabio Estevam

author reviewer
#0 lore raw

PATCH v3 1/2 irqchip/imx-irqsteer: Convert to devm_pm_runtime_enable()

From: Fabio Estevam <[email protected]>

imx_irqsteer_probe() enables runtime PM, but imx_irqsteer_remove() does not disable it. Consequently, runtime PM remains enabled after unbinding the device, and rebinding it triggers:

Unbalanced pm_runtime_enable!

Use devm_pm_runtime_enable() to automatically disable runtime PM when the device is removed. Set up runtime PM before creating the IRQ domain and registering chained handlers so that a failure cannot leave either resource pointing at freed driver data.

Fixes 4730d2233311 ("irqchip/imx-irqsteer: Add runtime PM support")

---
Changes since v1:
- Move devm_pm_runtime_enable() prior to irq_domain_create_linear(). (Frank)

 drivers/irqchip/irq-imx-irqsteer.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
drivers/irqchip/irq-imx-irqsteer.c +5 -3
index 87b07f517be3..653e25115083 100644
@@ -236,6 +236,11 @@ static int imx_irqsteer_probe(struct platform_device *pdev)
 	if (irqsteer_has_chanctrl(data->devtype_data))
 		writel_relaxed(BIT(data->channel), data->regs + CHANCTRL);
 
+	pm_runtime_set_active(&pdev->dev);
+	ret = devm_pm_runtime_enable(&pdev->dev);
+	if (ret)
+		goto out;
+
 	data->domain = irq_domain_create_linear(dev_fwnode(&pdev->dev), data->reg_num * 32,
 						&imx_irqsteer_domain_ops, data);
 	if (!data->domain) {
@@ -262,9 +267,6 @@ static int imx_irqsteer_probe(struct platform_device *pdev)
 
 	platform_set_drvdata(pdev, data);
 
-	pm_runtime_set_active(&pdev->dev);
-	pm_runtime_enable(&pdev->dev);
-
 	return 0;
 out:
 	clk_disable_unprepare(data->ipg_clk);
Signature · 1 line
2.43.0
author reviewer
#1 lore raw

PATCH v3 2/2 irqchip/imx-irqsteer: Validate IRQ count before creating domain

From: Fabio Estevam <[email protected]>

The IRQ count is validated after creating the IRQ domain. If it is invalid, probe returns without removing the domain, leaving its host data pointing at devm-managed memory that is freed on probe failure.

Validate the count before allocating resources to avoid the leak and dangling pointer.

Fixes 28528fca4908 ("irqchip/imx-irqsteer: Add multi output interrupts support")

---
Changes since v2:
- Newly introduced.

 drivers/irqchip/irq-imx-irqsteer.c | 7 ++-----
 1 file changed, 2 insertions(+), 5 deletions(-)
drivers/irqchip/irq-imx-irqsteer.c +2 -5
index 653e25115083..55aec60dee40 100644
@@ -217,6 +217,8 @@ static int imx_irqsteer_probe(struct platform_device *pdev)
 	 */
 	data->irq_count = DIV_ROUND_UP(irqs_num, 64);
 	data->reg_num = irqs_num / 32;
+	if (!data->irq_count || data->irq_count > CHAN_MAX_OUTPUT_INT)
+		return -EINVAL;
 
 	if (IS_ENABLED(CONFIG_PM)) {
 		data->saved_reg = devm_kzalloc(&pdev->dev,
@@ -250,11 +252,6 @@ static int imx_irqsteer_probe(struct platform_device *pdev)
 	}
 	irq_domain_set_pm_device(data->domain, &pdev->dev);
 
-	if (!data->irq_count || data->irq_count > CHAN_MAX_OUTPUT_INT) {
-		ret = -EINVAL;
-		goto out;
-	}
-
 	for (i = 0; i < data->irq_count; i++) {
 		data->irq[i] = irq_of_parse_and_map(np, i);
 		if (!data->irq[i])
Signature · 1 line
2.43.0
Reviewed-by on v3 2/2
#2 lore raw

Re: [PATCH v3 2/2] irqchip/imx-irqsteer: Validate IRQ count before creating domain

Quoted 6 lines · Fabio Estevam

From: Fabio Estevam <[email protected]>

The IRQ count is validated after creating the IRQ domain. If it is invalid, probe returns without removing the domain, leaving its host data pointing at devm-managed memory that is freed on probe failure.

Quoted 6 lines

Validate the count before allocating resources to avoid the leak and dangling pointer.

Fixes 28528fca4908 ("irqchip/imx-irqsteer: Add multi output interrupts support")

---

Quoted 35 lines

Changes since v2:
- Newly introduced.

 drivers/irqchip/irq-imx-irqsteer.c | 7 ++-----
 1 file changed, 2 insertions(+), 5 deletions(-)
drivers/irqchip/irq-imx-irqsteer.c +2 -6
index 653e25115083..55aec60dee40 100644
@@ -217,6 +217,8 @@ static int imx_irqsteer_probe(struct platform_device *pdev)
 	 */
 	data->irq_count = DIV_ROUND_UP(irqs_num, 64);
 	data->reg_num = irqs_num / 32;
+	if (!data->irq_count || data->irq_count > CHAN_MAX_OUTPUT_INT)
+		return -EINVAL;
 
 	if (IS_ENABLED(CONFIG_PM)) {
 		data->saved_reg = devm_kzalloc(&pdev->dev,
@@ -250,11 +252,6 @@ static int imx_irqsteer_probe(struct platform_device *pdev)
 	}
 	irq_domain_set_pm_device(data->domain, &pdev->dev);
 
-	if (!data->irq_count || data->irq_count > CHAN_MAX_OUTPUT_INT) {
-		ret = -EINVAL;
-		goto out;
-	}
-
 	for (i = 0; i < data->irq_count; i++) {
 		data->irq[i] = irq_of_parse_and_map(np, i);
 		if (!data->irq[i])
--

2.43.0

Reviewed-by on v3 1/2
#3 lore raw
In reply to Fabio Estevam
Quoted 13 lines · Fabio Estevam

From: Fabio Estevam <[email protected]>

imx_irqsteer_probe() enables runtime PM, but imx_irqsteer_remove() does not disable it. Consequently, runtime PM remains enabled after unbinding the device, and rebinding it triggers:

Unbalanced pm_runtime_enable!

Use devm_pm_runtime_enable() to automatically disable runtime PM when the device is removed. Set up runtime PM before creating the IRQ domain and registering chained handlers so that a failure cannot leave either resource pointing at freed driver data.

Quoted 5 lines
Fixes 4730d2233311 ("irqchip/imx-irqsteer: Add runtime PM support")

---
Changes since v1:
- Move devm_pm_runtime_enable() prior to irq_domain_create_linear(). (Frank)

Quoted 34 lines
 drivers/irqchip/irq-imx-irqsteer.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
drivers/irqchip/irq-imx-irqsteer.c +5 -4
index 87b07f517be3..653e25115083 100644
@@ -236,6 +236,11 @@ static int imx_irqsteer_probe(struct platform_device *pdev)
 	if (irqsteer_has_chanctrl(data->devtype_data))
 		writel_relaxed(BIT(data->channel), data->regs + CHANCTRL);
 
+	pm_runtime_set_active(&pdev->dev);
+	ret = devm_pm_runtime_enable(&pdev->dev);
+	if (ret)
+		goto out;
+
 	data->domain = irq_domain_create_linear(dev_fwnode(&pdev->dev), data->reg_num * 32,
 						&imx_irqsteer_domain_ops, data);
 	if (!data->domain) {
@@ -262,9 +267,6 @@ static int imx_irqsteer_probe(struct platform_device *pdev)
 
 	platform_set_drvdata(pdev, data);
 
-	pm_runtime_set_active(&pdev->dev);
-	pm_runtime_enable(&pdev->dev);
-
 	return 0;
 out:
 	clk_disable_unprepare(data->ipg_clk);
--

2.43.0

reviewer
#4 lore raw
In reply to Fabio Estevam
Quoted 45 lines · Fabio Estevam

From: Fabio Estevam <[email protected]>

imx_irqsteer_probe() enables runtime PM, but imx_irqsteer_remove() does not disable it. Consequently, runtime PM remains enabled after unbinding the device, and rebinding it triggers:

Unbalanced pm_runtime_enable!

Use devm_pm_runtime_enable() to automatically disable runtime PM when the device is removed. Set up runtime PM before creating the IRQ domain and registering chained handlers so that a failure cannot leave either resource pointing at freed driver data.

Fixes 4730d2233311 ("irqchip/imx-irqsteer: Add runtime PM support")

---
Changes since v1:
- Move devm_pm_runtime_enable() prior to irq_domain_create_linear(). (Frank)

 drivers/irqchip/irq-imx-irqsteer.c | 8 +++++---

 1 file changed, 5 insertions(+), 3 deletions(-)
drivers/irqchip/irq-imx-irqsteer.c +0 -0
index 87b07f517be3..653e25115083 100644
@@ -236,6 +236,11 @@ static int imx_irqsteer_probe(struct platform_device *pdev)
  	if (irqsteer_has_chanctrl(data->devtype_data))
  		writel_relaxed(BIT(data->channel), data->regs + CHANCTRL);

drivers/irqchip/irq-imx-irqsteer.c +5 -0
+	pm_runtime_set_active(&pdev->dev);
+	ret = devm_pm_runtime_enable(&pdev->dev);
+	if (ret)
+		goto out;
+

  data->domain = irq_domain_create_linear(dev_fwnode(&pdev->dev), data->reg_num * 32, &imx_irqsteer_domain_ops, data); if (!data->domain) {

drivers/irqchip/irq-imx-irqsteer.c +0 -0
@@ -262,9 +267,6 @@ static int imx_irqsteer_probe(struct platform_device *pdev)


  platform_set_drvdata(pdev, data);

drivers/irqchip/irq-imx-irqsteer.c +0 -3
-	pm_runtime_set_active(&pdev->dev);
-	pm_runtime_enable(&pdev->dev);
-
Quoted 3 lines

  return 0;
 out:
  clk_disable_unprepare(data->ipg_clk);

I still believe there is something off with the way pm_runtime is handled, and that the double clock disable is possible.

Since (like I said) I have very limited understanding of the runtime_pm framework, I decided to make a little experiment.

With the dummy module below, I see this:

[  548.253591] pm_dummy pm_dummy: pm_dummy_probe() executed
[  548.254209] pm_dummy pm_dummy: clock enabled; refcount: 1
[  548.255028] pm_dummy pm_dummy: pm_dummy_runtime_suspend() triggered
[  548.255845] pm_dummy pm_dummy: clock disabled; refcount: 0
[  554.261820] pm_dummy pm_dummy: pm_dummy_runtime_resume() triggered
[  554.263100] pm_dummy pm_dummy: clock enabled; refcount: 1
[  554.264006] pm_dummy pm_dummy: pm_dummy_runtime_suspend() triggered
[  554.264997] pm_dummy pm_dummy: clock disabled; refcount: 0
[  554.265819] pm_dummy pm_dummy: pm_dummy_remove() executed
[  554.266609] pm_dummy pm_dummy: clock disabled; refcount: -1
[  554.267468] pm_dummy pm_dummy: **************************************************
[  554.268554] pm_dummy pm_dummy: [BUG DETECTED] Clock disable count underflow! (-1)
[  554.269479] pm_dummy pm_dummy: **************************************************

What I find interesting is that the device is suspended immediately during probe(), then it's automatically resumed and immediately suspended again right before remove(). The latter is probably a side effect of devm_pm_runtime_enable(). But in any case, the clock *is* disabled twice, and that's even without any explicit suspend or resume, it's just by loading and unloading the module.

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/platform_device.h>
#include <linux/pm_runtime.h>

MODULE_LICENSE("GPL");
MODULE_AUTHOR("Radu Rendec <[email protected]>");
MODULE_DESCRIPTION("runtime_pm playground");

static int mock_clk_count = 0;

static int mock_clk_prepare_enable(struct device *dev)

{
	mock_clk_count++;
	dev_info(dev, "clock enabled; refcount: %d\n", mock_clk_count);
	return 0;
}

static void mock_clk_disable_unprepare(struct device *dev)

{
	mock_clk_count--;
	dev_info(dev, "clock disabled; refcount: %d\n", mock_clk_count);

	if (mock_clk_count < 0) {
		dev_err(dev, "**************************************************\n");
		dev_err(dev, "[BUG DETECTED] Clock disable count underflow! (%d)\n", mock_clk_count);
		dev_err(dev, "**************************************************\n");
	}
}

static int pm_dummy_runtime_suspend(struct device *dev)

{
	dev_info(dev, "%s() triggered\n", __func__);
	mock_clk_disable_unprepare(dev);
	return 0;
}

static int pm_dummy_runtime_resume(struct device *dev)

{
	dev_info(dev, "%s() triggered\n", __func__);
	return mock_clk_prepare_enable(dev);
}

static const struct dev_pm_ops pm_dummy_pm_ops = {
SET_RUNTIME_PM_OPS(pm_dummy_runtime_suspend, pm_dummy_runtime_resume, NULL)
};

static int pm_dummy_probe(struct platform_device *pdev)

{
	dev_info(&pdev->dev, "%s() executed\n", __func__);

	mock_clk_prepare_enable(&pdev->dev);
	pm_runtime_set_active(&pdev->dev);
	devm_pm_runtime_enable(&pdev->dev);

	return 0;
}

static void pm_dummy_remove(struct platform_device *pdev)

{
	dev_info(&pdev->dev, "%s() executed\n", __func__);

	mock_clk_disable_unprepare(&pdev->dev);
}
static struct platform_driver pm_dummy = {
	.probe = pm_dummy_probe,
	.remove = pm_dummy_remove,
	.driver = {
		.name = "pm_dummy",
		.pm = &pm_dummy_pm_ops,
	},
};

static struct platform_device *pdev;

static int __init pm_demo_init(void)

{
	int ret;

	ret = platform_driver_register(&pm_dummy);
	if (ret)
		return ret;

	pdev = platform_device_register_simple("pm_dummy", -1, NULL, 0);
	if (IS_ERR(pdev)) {
		platform_driver_unregister(&pm_dummy);
		return PTR_ERR(pdev);
	}

	return 0;
}

static void __exit pm_demo_exit(void)

{
	platform_device_unregister(pdev);
	platform_driver_unregister(&pm_dummy);
}

module_init(pm_demo_init);
module_exit(pm_demo_exit);

#5 lore raw
Quoted 53 lines · Radu Rendec
Quoted 45 lines · Fabio Estevam

From: Fabio Estevam <[email protected]>

imx_irqsteer_probe() enables runtime PM, but imx_irqsteer_remove() does not disable it. Consequently, runtime PM remains enabled after unbinding the device, and rebinding it triggers:

Unbalanced pm_runtime_enable!

Use devm_pm_runtime_enable() to automatically disable runtime PM when the device is removed. Set up runtime PM before creating the IRQ domain and registering chained handlers so that a failure cannot leave either resource pointing at freed driver data.

Fixes 4730d2233311 ("irqchip/imx-irqsteer: Add runtime PM support")

---
Changes since v1:
- Move devm_pm_runtime_enable() prior to irq_domain_create_linear(). (Frank)

 drivers/irqchip/irq-imx-irqsteer.c | 8 +++++---

 1 file changed, 5 insertions(+), 3 deletions(-)
drivers/irqchip/irq-imx-irqsteer.c +0 -0
index 87b07f517be3..653e25115083 100644
@@ -236,6 +236,11 @@ static int imx_irqsteer_probe(struct platform_device *pdev)
  	if (irqsteer_has_chanctrl(data->devtype_data))
  		writel_relaxed(BIT(data->channel), data->regs + CHANCTRL);

drivers/irqchip/irq-imx-irqsteer.c +5 -0
+	pm_runtime_set_active(&pdev->dev);
+	ret = devm_pm_runtime_enable(&pdev->dev);
+	if (ret)
+		goto out;
+

  data->domain = irq_domain_create_linear(dev_fwnode(&pdev->dev), data->reg_num * 32, &imx_irqsteer_domain_ops, data); if (!data->domain) {

drivers/irqchip/irq-imx-irqsteer.c +0 -0
@@ -262,9 +267,6 @@ static int imx_irqsteer_probe(struct platform_device *pdev)


  platform_set_drvdata(pdev, data);

drivers/irqchip/irq-imx-irqsteer.c +0 -3
-	pm_runtime_set_active(&pdev->dev);
-	pm_runtime_enable(&pdev->dev);
-
Quoted 3 lines

  return 0;
 out:
  clk_disable_unprepare(data->ipg_clk);

I still believe there is something off with the way pm_runtime is handled, and that the double clock disable is possible.

Quoted 7 lines

Since (like I said) I have very limited understanding of the runtime_pm framework, I decided to make a little experiment.

With the dummy module below, I see this:

[  548.253591] pm_dummy pm_dummy: pm_dummy_probe() executed
[  548.254209] pm_dummy pm_dummy: clock enabled; refcount: 1
[  548.255028] pm_dummy pm_dummy: pm_dummy_runtime_suspend() triggered

https://elixir.bootlin.com/linux/v7.2.2/source/drivers/base/dd.c#L827

after probe, pm_request_idle(dev), which trigger pm_dummy_runtime_suspend()

Quoted 7 lines
[  548.255845] pm_dummy pm_dummy: clock disabled; refcount: 0
[  554.261820] pm_dummy pm_dummy: pm_dummy_runtime_resume() triggered
[  554.263100] pm_dummy pm_dummy: clock enabled; refcount: 1
[  554.264006] pm_dummy pm_dummy: pm_dummy_runtime_suspend() triggered
[  554.264997] pm_dummy pm_dummy: clock disabled; refcount: 0
[  554.265819] pm_dummy pm_dummy: pm_dummy_remove() executed

You should set runtime_resume() at remove function to match probe's state. if your driver remove() did disable clock.

It is trick between runtime pm and devm clock management.

The beneafit of keep runtime active in probe, driver can work when disable CONFIG_PM, but complex at tear down.

keep inactive in probe, code will be simple, but it will not work if disable CONFIG_PM because clock have not enabled

Frank

Quoted 114 lines
[  554.266609] pm_dummy pm_dummy: clock disabled; refcount: -1
[  554.267468] pm_dummy pm_dummy: **************************************************
[  554.268554] pm_dummy pm_dummy: [BUG DETECTED] Clock disable count underflow! (-1)
[  554.269479] pm_dummy pm_dummy: **************************************************

What I find interesting is that the device is suspended immediately during probe(), then it's automatically resumed and immediately suspended again right before remove(). The latter is probably a side effect of devm_pm_runtime_enable(). But in any case, the clock *is* disabled twice, and that's even without any explicit suspend or resume, it's just by loading and unloading the module.

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/platform_device.h>
#include <linux/pm_runtime.h>

MODULE_LICENSE("GPL");
MODULE_AUTHOR("Radu Rendec <[email protected]>");
MODULE_DESCRIPTION("runtime_pm playground");

static int mock_clk_count = 0;

static int mock_clk_prepare_enable(struct device *dev)

{
	mock_clk_count++;
	dev_info(dev, "clock enabled; refcount: %d\n", mock_clk_count);
	return 0;
}

static void mock_clk_disable_unprepare(struct device *dev)

{
	mock_clk_count--;
	dev_info(dev, "clock disabled; refcount: %d\n", mock_clk_count);

	if (mock_clk_count < 0) {
		dev_err(dev, "**************************************************\n");
		dev_err(dev, "[BUG DETECTED] Clock disable count underflow! (%d)\n", mock_clk_count);
		dev_err(dev, "**************************************************\n");
	}
}

static int pm_dummy_runtime_suspend(struct device *dev)

{
	dev_info(dev, "%s() triggered\n", __func__);
	mock_clk_disable_unprepare(dev);
	return 0;
}

static int pm_dummy_runtime_resume(struct device *dev)

{
	dev_info(dev, "%s() triggered\n", __func__);
	return mock_clk_prepare_enable(dev);
}

static const struct dev_pm_ops pm_dummy_pm_ops = {
SET_RUNTIME_PM_OPS(pm_dummy_runtime_suspend, pm_dummy_runtime_resume, NULL)
};

static int pm_dummy_probe(struct platform_device *pdev)

{
	dev_info(&pdev->dev, "%s() executed\n", __func__);

	mock_clk_prepare_enable(&pdev->dev);
	pm_runtime_set_active(&pdev->dev);
	devm_pm_runtime_enable(&pdev->dev);

	return 0;
}

static void pm_dummy_remove(struct platform_device *pdev)

{
	dev_info(&pdev->dev, "%s() executed\n", __func__);

	mock_clk_disable_unprepare(&pdev->dev);
}
static struct platform_driver pm_dummy = {
	.probe = pm_dummy_probe,
	.remove = pm_dummy_remove,
	.driver = {
		.name = "pm_dummy",
		.pm = &pm_dummy_pm_ops,
	},
};

static struct platform_device *pdev;

static int __init pm_demo_init(void)

{
	int ret;

	ret = platform_driver_register(&pm_dummy);
	if (ret)
		return ret;

	pdev = platform_device_register_simple("pm_dummy", -1, NULL, 0);
	if (IS_ERR(pdev)) {
		platform_driver_unregister(&pm_dummy);
		return PTR_ERR(pdev);
	}

	return 0;
}

static void __exit pm_demo_exit(void)

{
	platform_device_unregister(pdev);
	platform_driver_unregister(&pm_dummy);
}

module_init(pm_demo_init);
module_exit(pm_demo_exit);

reviewer
#6 lore raw
Quoted 77 lines · Frank Li
Quoted 53 lines · Radu Rendec
Quoted 45 lines · Fabio Estevam

From: Fabio Estevam <[email protected]>

imx_irqsteer_probe() enables runtime PM, but imx_irqsteer_remove() does not disable it. Consequently, runtime PM remains enabled after unbinding the device, and rebinding it triggers:

Unbalanced pm_runtime_enable!

Use devm_pm_runtime_enable() to automatically disable runtime PM when the device is removed. Set up runtime PM before creating the IRQ domain and registering chained handlers so that a failure cannot leave either resource pointing at freed driver data.

Fixes 4730d2233311 ("irqchip/imx-irqsteer: Add runtime PM support")

---
Changes since v1:
- Move devm_pm_runtime_enable() prior to irq_domain_create_linear(). (Frank)

 drivers/irqchip/irq-imx-irqsteer.c | 8 +++++---

 1 file changed, 5 insertions(+), 3 deletions(-)
drivers/irqchip/irq-imx-irqsteer.c +0 -0
index 87b07f517be3..653e25115083 100644
@@ -236,6 +236,11 @@ static int imx_irqsteer_probe(struct platform_device *pdev)
  	if (irqsteer_has_chanctrl(data->devtype_data))
  		writel_relaxed(BIT(data->channel), data->regs + CHANCTRL);

drivers/irqchip/irq-imx-irqsteer.c +5 -0
+	pm_runtime_set_active(&pdev->dev);
+	ret = devm_pm_runtime_enable(&pdev->dev);
+	if (ret)
+		goto out;
+

  data->domain = irq_domain_create_linear(dev_fwnode(&pdev->dev), data->reg_num * 32, &imx_irqsteer_domain_ops, data); if (!data->domain) {

drivers/irqchip/irq-imx-irqsteer.c +0 -0
@@ -262,9 +267,6 @@ static int imx_irqsteer_probe(struct platform_device *pdev)


  platform_set_drvdata(pdev, data);

drivers/irqchip/irq-imx-irqsteer.c +0 -3
-	pm_runtime_set_active(&pdev->dev);
-	pm_runtime_enable(&pdev->dev);
-
Quoted 3 lines

  return 0;
 out:
  clk_disable_unprepare(data->ipg_clk);

I still believe there is something off with the way pm_runtime is handled, and that the double clock disable is possible.

Quoted 7 lines

Since (like I said) I have very limited understanding of the runtime_pm framework, I decided to make a little experiment.

With the dummy module below, I see this:
[  548.253591] pm_dummy pm_dummy: pm_dummy_probe() executed [  548.254209] pm_dummy pm_dummy: clock enabled; refcount: 1 [  548.255028] pm_dummy pm_dummy: pm_dummy_runtime_suspend() triggered

https://elixir.bootlin.com/linux/v7.2.2/source/drivers/base/dd.c#L827

after probe, pm_request_idle(dev), which trigger pm_dummy_runtime_suspend()

Quoted 7 lines

[  548.255845] pm_dummy pm_dummy: clock disabled; refcount: 0

[  554.261820] pm_dummy pm_dummy: pm_dummy_runtime_resume() triggered [  554.263100] pm_dummy pm_dummy: clock enabled; refcount: 1 [  554.264006] pm_dummy pm_dummy: pm_dummy_runtime_suspend() triggered [  554.264997] pm_dummy pm_dummy: clock disabled; refcount: 0 [  554.265819] pm_dummy pm_dummy: pm_dummy_remove() executed

You should set runtime_resume() at remove function to match probe's state. if your driver remove() did disable clock.

Quoted 7 lines

It is trick between runtime pm and devm clock management.

The beneafit of keep runtime active in probe, driver can work when disable CONFIG_PM, but complex at tear down.

keep inactive in probe, code will be simple, but it will not work if disable CONFIG_PM because clock have not enabled

Thanks, Frank! I appreciate you took the time to explain this in detail.

The dummy driver followed the exact same logic/sequence as the proposed patch, and the purpose was to observe the behavior of runtime_pm in isolation and prove that something wasn't quite right about the clock management.

I believe this is now handled correctly in Zhipeng's consolidated patch series against imx-irqsteer.

Quoted 114 lines
Quoted 114 lines

[  554.266609] pm_dummy pm_dummy: clock disabled; refcount: -1
[  554.267468] pm_dummy pm_dummy: ************************************************** [  554.268554] pm_dummy pm_dummy: [BUG DETECTED] Clock disable count underflow! (-1) [  554.269479] pm_dummy pm_dummy: **************************************************

What I find interesting is that the device is suspended immediately during probe(), then it's automatically resumed and immediately suspended again right before remove(). The latter is probably a side effect of devm_pm_runtime_enable(). But in any case, the clock *is* disabled twice, and that's even without any explicit suspend or resume, it's just by loading and unloading the module.

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/platform_device.h>
#include <linux/pm_runtime.h>

MODULE_LICENSE("GPL");
MODULE_AUTHOR("Radu Rendec <[email protected]>");
MODULE_DESCRIPTION("runtime_pm playground");

static int mock_clk_count = 0;

static int mock_clk_prepare_enable(struct device *dev)

{
	mock_clk_count++;
	dev_info(dev, "clock enabled; refcount: %d\n", mock_clk_count);
	return 0;
}

static void mock_clk_disable_unprepare(struct device *dev)

{
	mock_clk_count--;
	dev_info(dev, "clock disabled; refcount: %d\n", mock_clk_count);

	if (mock_clk_count < 0) {
		dev_err(dev, "**************************************************\n");
		dev_err(dev, "[BUG DETECTED] Clock disable count underflow! (%d)\n", mock_clk_count);
		dev_err(dev, "**************************************************\n");
	}
}

static int pm_dummy_runtime_suspend(struct device *dev)

{
	dev_info(dev, "%s() triggered\n", __func__);
	mock_clk_disable_unprepare(dev);
	return 0;
}

static int pm_dummy_runtime_resume(struct device *dev)

{
	dev_info(dev, "%s() triggered\n", __func__);
	return mock_clk_prepare_enable(dev);
}

static const struct dev_pm_ops pm_dummy_pm_ops = {
SET_RUNTIME_PM_OPS(pm_dummy_runtime_suspend, pm_dummy_runtime_resume, NULL)
};

static int pm_dummy_probe(struct platform_device *pdev)

{
	dev_info(&pdev->dev, "%s() executed\n", __func__);

	mock_clk_prepare_enable(&pdev->dev);
	pm_runtime_set_active(&pdev->dev);
	devm_pm_runtime_enable(&pdev->dev);

	return 0;
}

static void pm_dummy_remove(struct platform_device *pdev)

{
	dev_info(&pdev->dev, "%s() executed\n", __func__);

	mock_clk_disable_unprepare(&pdev->dev);
}
static struct platform_driver pm_dummy = {
	.probe = pm_dummy_probe,
	.remove = pm_dummy_remove,
	.driver = {
		.name = "pm_dummy",
		.pm = &pm_dummy_pm_ops,
	},
};

static struct platform_device *pdev;

static int __init pm_demo_init(void)

{
	int ret;

	ret = platform_driver_register(&pm_dummy);
	if (ret)
		return ret;

	pdev = platform_device_register_simple("pm_dummy", -1, NULL, 0);
	if (IS_ERR(pdev)) {
		platform_driver_unregister(&pm_dummy);
		return PTR_ERR(pdev);
	}

	return 0;
}

static void __exit pm_demo_exit(void)

{
	platform_device_unregister(pdev);
	platform_driver_unregister(&pm_dummy);
}

module_init(pm_demo_init);
module_exit(pm_demo_exit);

Signature · 2 lines
Best regards,
Radu