2021-12-27 09:45:53

by Uwe Kleine-König

[permalink] [raw]
Subject: [PATCH v2 19/23] counter: microchip-tcb-capture: Convert to new counter registration

This fixes device lifetime issues where it was possible to free a live
struct device.

Fixes: 106b104137fd ("counter: Add microchip TCB capture counter")
Signed-off-by: Uwe Kleine-König <[email protected]>
---
drivers/counter/microchip-tcb-capture.c | 30 ++++++++++++++-----------
1 file changed, 17 insertions(+), 13 deletions(-)

diff --git a/drivers/counter/microchip-tcb-capture.c b/drivers/counter/microchip-tcb-capture.c
index 1b56b7444668..70c1d28546be 100644
--- a/drivers/counter/microchip-tcb-capture.c
+++ b/drivers/counter/microchip-tcb-capture.c
@@ -24,7 +24,6 @@

struct mchp_tc_data {
const struct atmel_tcb_config *tc_cfg;
- struct counter_device counter;
struct regmap *regmap;
int qdec_mode;
int num_channels;
@@ -296,6 +295,7 @@ static int mchp_tc_probe(struct platform_device *pdev)
struct device_node *np = pdev->dev.of_node;
const struct atmel_tcb_config *tcb_config;
const struct of_device_id *match;
+ struct counter_device *counter;
struct mchp_tc_data *priv;
char clk_name[7];
struct regmap *regmap;
@@ -303,9 +303,10 @@ static int mchp_tc_probe(struct platform_device *pdev)
int channel;
int ret, i;

- priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
- if (!priv)
+ counter = devm_counter_alloc(&pdev->dev, sizeof(*priv));
+ if (!counter)
return -ENOMEM;
+ priv = counter_priv(counter);

match = of_match_node(atmel_tc_of_match, np->parent);
tcb_config = match->data;
@@ -360,16 +361,19 @@ static int mchp_tc_probe(struct platform_device *pdev)

priv->tc_cfg = tcb_config;
priv->regmap = regmap;
- priv->counter.name = dev_name(&pdev->dev);
- priv->counter.parent = &pdev->dev;
- priv->counter.ops = &mchp_tc_ops;
- priv->counter.num_counts = ARRAY_SIZE(mchp_tc_counts);
- priv->counter.counts = mchp_tc_counts;
- priv->counter.num_signals = ARRAY_SIZE(mchp_tc_count_signals);
- priv->counter.signals = mchp_tc_count_signals;
- priv->counter.priv = priv;
-
- return devm_counter_register(&pdev->dev, &priv->counter);
+ counter->name = dev_name(&pdev->dev);
+ counter->parent = &pdev->dev;
+ counter->ops = &mchp_tc_ops;
+ counter->num_counts = ARRAY_SIZE(mchp_tc_counts);
+ counter->counts = mchp_tc_counts;
+ counter->num_signals = ARRAY_SIZE(mchp_tc_count_signals);
+ counter->signals = mchp_tc_count_signals;
+
+ ret = devm_counter_add(&pdev->dev, counter);
+ if (ret < 0)
+ return dev_err_probe(&pdev->dev, ret, "Failed to add counter\n");
+
+ return 0;
}

static const struct of_device_id mchp_tc_dt_ids[] = {
--
2.33.0



2021-12-28 18:18:40

by Jonathan Cameron

[permalink] [raw]
Subject: Re: [PATCH v2 19/23] counter: microchip-tcb-capture: Convert to new counter registration

On Mon, 27 Dec 2021 10:45:22 +0100
Uwe Kleine-König <[email protected]> wrote:

> This fixes device lifetime issues where it was possible to free a live
> struct device.
>
> Fixes: 106b104137fd ("counter: Add microchip TCB capture counter")
> Signed-off-by: Uwe Kleine-König <[email protected]>

Reviewed-by: Jonathan Cameron <[email protected]>

> ---
> drivers/counter/microchip-tcb-capture.c | 30 ++++++++++++++-----------
> 1 file changed, 17 insertions(+), 13 deletions(-)
>
> diff --git a/drivers/counter/microchip-tcb-capture.c b/drivers/counter/microchip-tcb-capture.c
> index 1b56b7444668..70c1d28546be 100644
> --- a/drivers/counter/microchip-tcb-capture.c
> +++ b/drivers/counter/microchip-tcb-capture.c
> @@ -24,7 +24,6 @@
>
> struct mchp_tc_data {
> const struct atmel_tcb_config *tc_cfg;
> - struct counter_device counter;
> struct regmap *regmap;
> int qdec_mode;
> int num_channels;
> @@ -296,6 +295,7 @@ static int mchp_tc_probe(struct platform_device *pdev)
> struct device_node *np = pdev->dev.of_node;
> const struct atmel_tcb_config *tcb_config;
> const struct of_device_id *match;
> + struct counter_device *counter;
> struct mchp_tc_data *priv;
> char clk_name[7];
> struct regmap *regmap;
> @@ -303,9 +303,10 @@ static int mchp_tc_probe(struct platform_device *pdev)
> int channel;
> int ret, i;
>
> - priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
> - if (!priv)
> + counter = devm_counter_alloc(&pdev->dev, sizeof(*priv));
> + if (!counter)
> return -ENOMEM;
> + priv = counter_priv(counter);
>
> match = of_match_node(atmel_tc_of_match, np->parent);
> tcb_config = match->data;
> @@ -360,16 +361,19 @@ static int mchp_tc_probe(struct platform_device *pdev)
>
> priv->tc_cfg = tcb_config;
> priv->regmap = regmap;
> - priv->counter.name = dev_name(&pdev->dev);
> - priv->counter.parent = &pdev->dev;
> - priv->counter.ops = &mchp_tc_ops;
> - priv->counter.num_counts = ARRAY_SIZE(mchp_tc_counts);
> - priv->counter.counts = mchp_tc_counts;
> - priv->counter.num_signals = ARRAY_SIZE(mchp_tc_count_signals);
> - priv->counter.signals = mchp_tc_count_signals;
> - priv->counter.priv = priv;
> -
> - return devm_counter_register(&pdev->dev, &priv->counter);
> + counter->name = dev_name(&pdev->dev);
> + counter->parent = &pdev->dev;
> + counter->ops = &mchp_tc_ops;
> + counter->num_counts = ARRAY_SIZE(mchp_tc_counts);
> + counter->counts = mchp_tc_counts;
> + counter->num_signals = ARRAY_SIZE(mchp_tc_count_signals);
> + counter->signals = mchp_tc_count_signals;
> +
> + ret = devm_counter_add(&pdev->dev, counter);
> + if (ret < 0)
> + return dev_err_probe(&pdev->dev, ret, "Failed to add counter\n");
> +
> + return 0;
> }
>
> static const struct of_device_id mchp_tc_dt_ids[] = {


2021-12-29 08:36:36

by William Breathitt Gray

[permalink] [raw]
Subject: Re: [PATCH v2 19/23] counter: microchip-tcb-capture: Convert to new counter registration

On Mon, Dec 27, 2021 at 10:45:22AM +0100, Uwe Kleine-König wrote:
> This fixes device lifetime issues where it was possible to free a live
> struct device.
>
> Fixes: 106b104137fd ("counter: Add microchip TCB capture counter")
> Signed-off-by: Uwe Kleine-König <[email protected]>

Acked-by: William Breathitt Gray <[email protected]>

> ---
> drivers/counter/microchip-tcb-capture.c | 30 ++++++++++++++-----------
> 1 file changed, 17 insertions(+), 13 deletions(-)
>
> diff --git a/drivers/counter/microchip-tcb-capture.c b/drivers/counter/microchip-tcb-capture.c
> index 1b56b7444668..70c1d28546be 100644
> --- a/drivers/counter/microchip-tcb-capture.c
> +++ b/drivers/counter/microchip-tcb-capture.c
> @@ -24,7 +24,6 @@
>
> struct mchp_tc_data {
> const struct atmel_tcb_config *tc_cfg;
> - struct counter_device counter;
> struct regmap *regmap;
> int qdec_mode;
> int num_channels;
> @@ -296,6 +295,7 @@ static int mchp_tc_probe(struct platform_device *pdev)
> struct device_node *np = pdev->dev.of_node;
> const struct atmel_tcb_config *tcb_config;
> const struct of_device_id *match;
> + struct counter_device *counter;
> struct mchp_tc_data *priv;
> char clk_name[7];
> struct regmap *regmap;
> @@ -303,9 +303,10 @@ static int mchp_tc_probe(struct platform_device *pdev)
> int channel;
> int ret, i;
>
> - priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
> - if (!priv)
> + counter = devm_counter_alloc(&pdev->dev, sizeof(*priv));
> + if (!counter)
> return -ENOMEM;
> + priv = counter_priv(counter);
>
> match = of_match_node(atmel_tc_of_match, np->parent);
> tcb_config = match->data;
> @@ -360,16 +361,19 @@ static int mchp_tc_probe(struct platform_device *pdev)
>
> priv->tc_cfg = tcb_config;
> priv->regmap = regmap;
> - priv->counter.name = dev_name(&pdev->dev);
> - priv->counter.parent = &pdev->dev;
> - priv->counter.ops = &mchp_tc_ops;
> - priv->counter.num_counts = ARRAY_SIZE(mchp_tc_counts);
> - priv->counter.counts = mchp_tc_counts;
> - priv->counter.num_signals = ARRAY_SIZE(mchp_tc_count_signals);
> - priv->counter.signals = mchp_tc_count_signals;
> - priv->counter.priv = priv;
> -
> - return devm_counter_register(&pdev->dev, &priv->counter);
> + counter->name = dev_name(&pdev->dev);
> + counter->parent = &pdev->dev;
> + counter->ops = &mchp_tc_ops;
> + counter->num_counts = ARRAY_SIZE(mchp_tc_counts);
> + counter->counts = mchp_tc_counts;
> + counter->num_signals = ARRAY_SIZE(mchp_tc_count_signals);
> + counter->signals = mchp_tc_count_signals;
> +
> + ret = devm_counter_add(&pdev->dev, counter);
> + if (ret < 0)
> + return dev_err_probe(&pdev->dev, ret, "Failed to add counter\n");
> +
> + return 0;
> }
>
> static const struct of_device_id mchp_tc_dt_ids[] = {
> --
> 2.33.0
>


Attachments:
(No filename) (2.74 kB)
signature.asc (833.00 B)
Download all attachments