diff mbox series

[v2,22/23] counter: ti-eqep: Convert to new counter registration

Message ID 20211227094526.698714-23-u.kleine-koenig@pengutronix.de (mailing list archive)
State Handled Elsewhere
Headers show
Series [v2,01/23] counter: Use container_of instead of drvdata to track counter_device | expand

Commit Message

Uwe Kleine-König Dec. 27, 2021, 9:45 a.m. UTC
This fixes device lifetime issues where it was possible to free a live
struct device.

Fixes: f213729f6796 ("counter: new TI eQEP driver")
Signed-off-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>
---
 drivers/counter/ti-eqep.c | 29 +++++++++++++++--------------
 1 file changed, 15 insertions(+), 14 deletions(-)

Comments

Jonathan Cameron Dec. 28, 2021, 6:29 p.m. UTC | #1
On Mon, 27 Dec 2021 10:45:25 +0100
Uwe Kleine-König         <u.kleine-koenig@pengutronix.de> wrote:

> This fixes device lifetime issues where it was possible to free a live
> struct device.
> 
> Fixes: f213729f6796 ("counter: new TI eQEP driver")
> Signed-off-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>
It's nice that all these drivers are so similar...

Reviewed-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>

> ---
>  drivers/counter/ti-eqep.c | 29 +++++++++++++++--------------
>  1 file changed, 15 insertions(+), 14 deletions(-)
> 
> diff --git a/drivers/counter/ti-eqep.c b/drivers/counter/ti-eqep.c
> index abeda966e7be..3b49b492d182 100644
> --- a/drivers/counter/ti-eqep.c
> +++ b/drivers/counter/ti-eqep.c
> @@ -368,13 +368,15 @@ static const struct regmap_config ti_eqep_regmap16_config = {
>  static int ti_eqep_probe(struct platform_device *pdev)
>  {
>  	struct device *dev = &pdev->dev;
> +	struct counter_device *counter;
>  	struct ti_eqep_cnt *priv;
>  	void __iomem *base;
>  	int err;
>  
> -	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
> -	if (!priv)
> +	counter = devm_counter_alloc(dev, sizeof(*priv));
> +	if (!counter)
>  		return -ENOMEM;
> +	priv = counter_priv(counter);
>  
>  	base = devm_platform_ioremap_resource(pdev, 0);
>  	if (IS_ERR(base))
> @@ -390,16 +392,15 @@ static int ti_eqep_probe(struct platform_device *pdev)
>  	if (IS_ERR(priv->regmap16))
>  		return PTR_ERR(priv->regmap16);
>  
> -	priv->counter.name = dev_name(dev);
> -	priv->counter.parent = dev;
> -	priv->counter.ops = &ti_eqep_counter_ops;
> -	priv->counter.counts = ti_eqep_counts;
> -	priv->counter.num_counts = ARRAY_SIZE(ti_eqep_counts);
> -	priv->counter.signals = ti_eqep_signals;
> -	priv->counter.num_signals = ARRAY_SIZE(ti_eqep_signals);
> -	priv->counter.priv = priv;
> +	counter->name = dev_name(dev);
> +	counter->parent = dev;
> +	counter->ops = &ti_eqep_counter_ops;
> +	counter->counts = ti_eqep_counts;
> +	counter->num_counts = ARRAY_SIZE(ti_eqep_counts);
> +	counter->signals = ti_eqep_signals;
> +	counter->num_signals = ARRAY_SIZE(ti_eqep_signals);
>  
> -	platform_set_drvdata(pdev, priv);
> +	platform_set_drvdata(pdev, counter);
>  
>  	/*
>  	 * Need to make sure power is turned on. On AM33xx, this comes from the
> @@ -409,7 +410,7 @@ static int ti_eqep_probe(struct platform_device *pdev)
>  	pm_runtime_enable(dev);
>  	pm_runtime_get_sync(dev);
>  
> -	err = counter_register(&priv->counter);
> +	err = counter_add(counter);
>  	if (err < 0) {
>  		pm_runtime_put_sync(dev);
>  		pm_runtime_disable(dev);
> @@ -421,10 +422,10 @@ static int ti_eqep_probe(struct platform_device *pdev)
>  
>  static int ti_eqep_remove(struct platform_device *pdev)
>  {
> -	struct ti_eqep_cnt *priv = platform_get_drvdata(pdev);
> +	struct counter_device *counter = platform_get_drvdata(pdev);
>  	struct device *dev = &pdev->dev;
>  
> -	counter_unregister(&priv->counter);
> +	counter_unregister(counter);
>  	pm_runtime_put_sync(dev);
>  	pm_runtime_disable(dev);
>
William Breathitt Gray Dec. 29, 2021, 8:41 a.m. UTC | #2
On Mon, Dec 27, 2021 at 10:45:25AM +0100, Uwe Kleine-König wrote:
> This fixes device lifetime issues where it was possible to free a live
> struct device.
> 
> Fixes: f213729f6796 ("counter: new TI eQEP driver")
> Signed-off-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>

Acked-by: William Breathitt Gray <vilhelm.gray@gmail.com>

> ---
>  drivers/counter/ti-eqep.c | 29 +++++++++++++++--------------
>  1 file changed, 15 insertions(+), 14 deletions(-)
> 
> diff --git a/drivers/counter/ti-eqep.c b/drivers/counter/ti-eqep.c
> index abeda966e7be..3b49b492d182 100644
> --- a/drivers/counter/ti-eqep.c
> +++ b/drivers/counter/ti-eqep.c
> @@ -368,13 +368,15 @@ static const struct regmap_config ti_eqep_regmap16_config = {
>  static int ti_eqep_probe(struct platform_device *pdev)
>  {
>  	struct device *dev = &pdev->dev;
> +	struct counter_device *counter;
>  	struct ti_eqep_cnt *priv;
>  	void __iomem *base;
>  	int err;
>  
> -	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
> -	if (!priv)
> +	counter = devm_counter_alloc(dev, sizeof(*priv));
> +	if (!counter)
>  		return -ENOMEM;
> +	priv = counter_priv(counter);
>  
>  	base = devm_platform_ioremap_resource(pdev, 0);
>  	if (IS_ERR(base))
> @@ -390,16 +392,15 @@ static int ti_eqep_probe(struct platform_device *pdev)
>  	if (IS_ERR(priv->regmap16))
>  		return PTR_ERR(priv->regmap16);
>  
> -	priv->counter.name = dev_name(dev);
> -	priv->counter.parent = dev;
> -	priv->counter.ops = &ti_eqep_counter_ops;
> -	priv->counter.counts = ti_eqep_counts;
> -	priv->counter.num_counts = ARRAY_SIZE(ti_eqep_counts);
> -	priv->counter.signals = ti_eqep_signals;
> -	priv->counter.num_signals = ARRAY_SIZE(ti_eqep_signals);
> -	priv->counter.priv = priv;
> +	counter->name = dev_name(dev);
> +	counter->parent = dev;
> +	counter->ops = &ti_eqep_counter_ops;
> +	counter->counts = ti_eqep_counts;
> +	counter->num_counts = ARRAY_SIZE(ti_eqep_counts);
> +	counter->signals = ti_eqep_signals;
> +	counter->num_signals = ARRAY_SIZE(ti_eqep_signals);
>  
> -	platform_set_drvdata(pdev, priv);
> +	platform_set_drvdata(pdev, counter);
>  
>  	/*
>  	 * Need to make sure power is turned on. On AM33xx, this comes from the
> @@ -409,7 +410,7 @@ static int ti_eqep_probe(struct platform_device *pdev)
>  	pm_runtime_enable(dev);
>  	pm_runtime_get_sync(dev);
>  
> -	err = counter_register(&priv->counter);
> +	err = counter_add(counter);
>  	if (err < 0) {
>  		pm_runtime_put_sync(dev);
>  		pm_runtime_disable(dev);
> @@ -421,10 +422,10 @@ static int ti_eqep_probe(struct platform_device *pdev)
>  
>  static int ti_eqep_remove(struct platform_device *pdev)
>  {
> -	struct ti_eqep_cnt *priv = platform_get_drvdata(pdev);
> +	struct counter_device *counter = platform_get_drvdata(pdev);
>  	struct device *dev = &pdev->dev;
>  
> -	counter_unregister(&priv->counter);
> +	counter_unregister(counter);
>  	pm_runtime_put_sync(dev);
>  	pm_runtime_disable(dev);
>  
> -- 
> 2.33.0
>
diff mbox series

Patch

diff --git a/drivers/counter/ti-eqep.c b/drivers/counter/ti-eqep.c
index abeda966e7be..3b49b492d182 100644
--- a/drivers/counter/ti-eqep.c
+++ b/drivers/counter/ti-eqep.c
@@ -368,13 +368,15 @@  static const struct regmap_config ti_eqep_regmap16_config = {
 static int ti_eqep_probe(struct platform_device *pdev)
 {
 	struct device *dev = &pdev->dev;
+	struct counter_device *counter;
 	struct ti_eqep_cnt *priv;
 	void __iomem *base;
 	int err;
 
-	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
-	if (!priv)
+	counter = devm_counter_alloc(dev, sizeof(*priv));
+	if (!counter)
 		return -ENOMEM;
+	priv = counter_priv(counter);
 
 	base = devm_platform_ioremap_resource(pdev, 0);
 	if (IS_ERR(base))
@@ -390,16 +392,15 @@  static int ti_eqep_probe(struct platform_device *pdev)
 	if (IS_ERR(priv->regmap16))
 		return PTR_ERR(priv->regmap16);
 
-	priv->counter.name = dev_name(dev);
-	priv->counter.parent = dev;
-	priv->counter.ops = &ti_eqep_counter_ops;
-	priv->counter.counts = ti_eqep_counts;
-	priv->counter.num_counts = ARRAY_SIZE(ti_eqep_counts);
-	priv->counter.signals = ti_eqep_signals;
-	priv->counter.num_signals = ARRAY_SIZE(ti_eqep_signals);
-	priv->counter.priv = priv;
+	counter->name = dev_name(dev);
+	counter->parent = dev;
+	counter->ops = &ti_eqep_counter_ops;
+	counter->counts = ti_eqep_counts;
+	counter->num_counts = ARRAY_SIZE(ti_eqep_counts);
+	counter->signals = ti_eqep_signals;
+	counter->num_signals = ARRAY_SIZE(ti_eqep_signals);
 
-	platform_set_drvdata(pdev, priv);
+	platform_set_drvdata(pdev, counter);
 
 	/*
 	 * Need to make sure power is turned on. On AM33xx, this comes from the
@@ -409,7 +410,7 @@  static int ti_eqep_probe(struct platform_device *pdev)
 	pm_runtime_enable(dev);
 	pm_runtime_get_sync(dev);
 
-	err = counter_register(&priv->counter);
+	err = counter_add(counter);
 	if (err < 0) {
 		pm_runtime_put_sync(dev);
 		pm_runtime_disable(dev);
@@ -421,10 +422,10 @@  static int ti_eqep_probe(struct platform_device *pdev)
 
 static int ti_eqep_remove(struct platform_device *pdev)
 {
-	struct ti_eqep_cnt *priv = platform_get_drvdata(pdev);
+	struct counter_device *counter = platform_get_drvdata(pdev);
 	struct device *dev = &pdev->dev;
 
-	counter_unregister(&priv->counter);
+	counter_unregister(counter);
 	pm_runtime_put_sync(dev);
 	pm_runtime_disable(dev);