diff mbox series

[v2,04/23] counter: Provide a wrapper to access device private data

Message ID 20211227094526.698714-5-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
For now this just wraps accessing struct counter_device::priv. However
this is about to change and converting drivers to this helper
individually makes fixing device lifetime issues result in easier to
review patches.

Signed-off-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>
---
 drivers/counter/counter-core.c | 12 ++++++++++++
 include/linux/counter.h        |  2 ++
 2 files changed, 14 insertions(+)

Comments

Greg KH Dec. 27, 2021, 11:02 a.m. UTC | #1
On Mon, Dec 27, 2021 at 10:45:07AM +0100, Uwe Kleine-König wrote:
> For now this just wraps accessing struct counter_device::priv. However
> this is about to change and converting drivers to this helper
> individually makes fixing device lifetime issues result in easier to
> review patches.
> 
> Signed-off-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>
> ---
>  drivers/counter/counter-core.c | 12 ++++++++++++
>  include/linux/counter.h        |  2 ++
>  2 files changed, 14 insertions(+)
> 
> diff --git a/drivers/counter/counter-core.c b/drivers/counter/counter-core.c
> index f053a43c6c04..00c41f28c101 100644
> --- a/drivers/counter/counter-core.c
> +++ b/drivers/counter/counter-core.c
> @@ -45,6 +45,18 @@ static struct bus_type counter_bus_type = {
>  
>  static dev_t counter_devt;
>  
> +/**
> + * counter_priv - access counter device private data
> + * @counter: counter device
> + *
> + * Get the counter device private data
> + */
> +void *counter_priv(const struct counter_device *const counter)
> +{
> +	return counter->priv;
> +}
> +EXPORT_SYMBOL_GPL(counter_priv);

Shouldn't this be usin gdev_get_drvdata() and using the private data
pointer that is already on the struct device structure itself?  The void
*priv; should be dropped from struct counter_device entirely.

Oh ick, I just now looked at 'struct counter_device', there are other
reference counting issues in there (hint, struct cdev has a reference
count...)  But that's independent of this patch series...

thanks,

greg k-h
Lars-Peter Clausen Dec. 27, 2021, 11:34 a.m. UTC | #2
On 12/27/21 12:02 PM, Greg Kroah-Hartman wrote:
> On Mon, Dec 27, 2021 at 10:45:07AM +0100, Uwe Kleine-König wrote:
>> For now this just wraps accessing struct counter_device::priv. However
>> this is about to change and converting drivers to this helper
>> individually makes fixing device lifetime issues result in easier to
>> review patches.
>>
>> Signed-off-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>
>> ---
>>   drivers/counter/counter-core.c | 12 ++++++++++++
>>   include/linux/counter.h        |  2 ++
>>   2 files changed, 14 insertions(+)
>>
>> diff --git a/drivers/counter/counter-core.c b/drivers/counter/counter-core.c
>> index f053a43c6c04..00c41f28c101 100644
>> --- a/drivers/counter/counter-core.c
>> +++ b/drivers/counter/counter-core.c
>> @@ -45,6 +45,18 @@ static struct bus_type counter_bus_type = {
>>   
>>   static dev_t counter_devt;
>>   
>> +/**
>> + * counter_priv - access counter device private data
>> + * @counter: counter device
>> + *
>> + * Get the counter device private data
>> + */
>> +void *counter_priv(const struct counter_device *const counter)
>> +{
>> +	return counter->priv;
>> +}
>> +EXPORT_SYMBOL_GPL(counter_priv);
> Shouldn't this be usin gdev_get_drvdata() and using the private data
> pointer that is already on the struct device structure itself?  The void
> *priv; should be dropped from struct counter_device entirely.
>
> Oh ick, I just now looked at 'struct counter_device', there are other
> reference counting issues in there (hint, struct cdev has a reference
> count...)  But that's independent of this patch series...
This is not a problem. The struct cdev holds a reference to the struct 
dev. This allows them to use the same allocation. As long as there is a 
reference to the cdev there will be a reference to the dev and the 
memory will be kept alive.
Greg KH Dec. 27, 2021, 11:52 a.m. UTC | #3
On Mon, Dec 27, 2021 at 12:34:28PM +0100, Lars-Peter Clausen wrote:
> On 12/27/21 12:02 PM, Greg Kroah-Hartman wrote:
> > On Mon, Dec 27, 2021 at 10:45:07AM +0100, Uwe Kleine-König wrote:
> > > For now this just wraps accessing struct counter_device::priv. However
> > > this is about to change and converting drivers to this helper
> > > individually makes fixing device lifetime issues result in easier to
> > > review patches.
> > > 
> > > Signed-off-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>
> > > ---
> > >   drivers/counter/counter-core.c | 12 ++++++++++++
> > >   include/linux/counter.h        |  2 ++
> > >   2 files changed, 14 insertions(+)
> > > 
> > > diff --git a/drivers/counter/counter-core.c b/drivers/counter/counter-core.c
> > > index f053a43c6c04..00c41f28c101 100644
> > > --- a/drivers/counter/counter-core.c
> > > +++ b/drivers/counter/counter-core.c
> > > @@ -45,6 +45,18 @@ static struct bus_type counter_bus_type = {
> > >   static dev_t counter_devt;
> > > +/**
> > > + * counter_priv - access counter device private data
> > > + * @counter: counter device
> > > + *
> > > + * Get the counter device private data
> > > + */
> > > +void *counter_priv(const struct counter_device *const counter)
> > > +{
> > > +	return counter->priv;
> > > +}
> > > +EXPORT_SYMBOL_GPL(counter_priv);
> > Shouldn't this be usin gdev_get_drvdata() and using the private data
> > pointer that is already on the struct device structure itself?  The void
> > *priv; should be dropped from struct counter_device entirely.
> > 
> > Oh ick, I just now looked at 'struct counter_device', there are other
> > reference counting issues in there (hint, struct cdev has a reference
> > count...)  But that's independent of this patch series...
> This is not a problem. The struct cdev holds a reference to the struct dev.
> This allows them to use the same allocation. As long as there is a reference
> to the cdev there will be a reference to the dev and the memory will be kept
> alive.

Ick, a cdev shouldn't be doing stuff like that, but I see how people
like to use it that way :(

Ok, it's fine for now, but yet-another-reaason why the cdev api is a
mess in places...

thanks,

greg k-h
Lars-Peter Clausen Dec. 27, 2021, 12:28 p.m. UTC | #4
On 12/27/21 12:52 PM, Greg Kroah-Hartman wrote:
> On Mon, Dec 27, 2021 at 12:34:28PM +0100, Lars-Peter Clausen wrote:
>> On 12/27/21 12:02 PM, Greg Kroah-Hartman wrote:
>>> On Mon, Dec 27, 2021 at 10:45:07AM +0100, Uwe Kleine-König wrote:
>>>> For now this just wraps accessing struct counter_device::priv. However
>>>> this is about to change and converting drivers to this helper
>>>> individually makes fixing device lifetime issues result in easier to
>>>> review patches.
>>>>
>>>> Signed-off-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>
>>>> ---
>>>>    drivers/counter/counter-core.c | 12 ++++++++++++
>>>>    include/linux/counter.h        |  2 ++
>>>>    2 files changed, 14 insertions(+)
>>>>
>>>> diff --git a/drivers/counter/counter-core.c b/drivers/counter/counter-core.c
>>>> index f053a43c6c04..00c41f28c101 100644
>>>> --- a/drivers/counter/counter-core.c
>>>> +++ b/drivers/counter/counter-core.c
>>>> @@ -45,6 +45,18 @@ static struct bus_type counter_bus_type = {
>>>>    static dev_t counter_devt;
>>>> +/**
>>>> + * counter_priv - access counter device private data
>>>> + * @counter: counter device
>>>> + *
>>>> + * Get the counter device private data
>>>> + */
>>>> +void *counter_priv(const struct counter_device *const counter)
>>>> +{
>>>> +	return counter->priv;
>>>> +}
>>>> +EXPORT_SYMBOL_GPL(counter_priv);
>>> Shouldn't this be usin gdev_get_drvdata() and using the private data
>>> pointer that is already on the struct device structure itself?  The void
>>> *priv; should be dropped from struct counter_device entirely.
>>>
>>> Oh ick, I just now looked at 'struct counter_device', there are other
>>> reference counting issues in there (hint, struct cdev has a reference
>>> count...)  But that's independent of this patch series...
>> This is not a problem. The struct cdev holds a reference to the struct dev.
>> This allows them to use the same allocation. As long as there is a reference
>> to the cdev there will be a reference to the dev and the memory will be kept
>> alive.
> Ick, a cdev shouldn't be doing stuff like that, but I see how people
> like to use it that way :(
>
Can you explain why cdev shouldn't be doing that? This commit has some 
backstory why this is done 
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=233ed09d7fdacf592ee91e6c97ce5f4364fbe7c0
Jonathan Cameron Dec. 28, 2021, 6:01 p.m. UTC | #5
On Mon, 27 Dec 2021 10:45:07 +0100
Uwe Kleine-König         <u.kleine-koenig@pengutronix.de> wrote:

> For now this just wraps accessing struct counter_device::priv. However
> this is about to change and converting drivers to this helper
> individually makes fixing device lifetime issues result in easier to
> review patches.
> 
> Signed-off-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>

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

> ---
>  drivers/counter/counter-core.c | 12 ++++++++++++
>  include/linux/counter.h        |  2 ++
>  2 files changed, 14 insertions(+)
> 
> diff --git a/drivers/counter/counter-core.c b/drivers/counter/counter-core.c
> index f053a43c6c04..00c41f28c101 100644
> --- a/drivers/counter/counter-core.c
> +++ b/drivers/counter/counter-core.c
> @@ -45,6 +45,18 @@ static struct bus_type counter_bus_type = {
>  
>  static dev_t counter_devt;
>  
> +/**
> + * counter_priv - access counter device private data
> + * @counter: counter device
> + *
> + * Get the counter device private data
> + */
> +void *counter_priv(const struct counter_device *const counter)
> +{
> +	return counter->priv;
> +}
> +EXPORT_SYMBOL_GPL(counter_priv);
> +
>  /**
>   * counter_register - register Counter to the system
>   * @counter:	pointer to Counter to register
> diff --git a/include/linux/counter.h b/include/linux/counter.h
> index b7d0a00a61cf..8daaa38c71d8 100644
> --- a/include/linux/counter.h
> +++ b/include/linux/counter.h
> @@ -329,6 +329,8 @@ struct counter_device {
>  	struct mutex ops_exist_lock;
>  };
>  
> +void *counter_priv(const struct counter_device *const counter);
> +
>  int counter_register(struct counter_device *const counter);
>  void counter_unregister(struct counter_device *const counter);
>  int devm_counter_register(struct device *dev,
William Breathitt Gray Dec. 29, 2021, 6:47 a.m. UTC | #6
On Mon, Dec 27, 2021 at 10:45:07AM +0100, Uwe Kleine-König wrote:
> For now this just wraps accessing struct counter_device::priv. However
> this is about to change and converting drivers to this helper
> individually makes fixing device lifetime issues result in easier to
> review patches.
> 
> Signed-off-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>

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

> ---
>  drivers/counter/counter-core.c | 12 ++++++++++++
>  include/linux/counter.h        |  2 ++
>  2 files changed, 14 insertions(+)
> 
> diff --git a/drivers/counter/counter-core.c b/drivers/counter/counter-core.c
> index f053a43c6c04..00c41f28c101 100644
> --- a/drivers/counter/counter-core.c
> +++ b/drivers/counter/counter-core.c
> @@ -45,6 +45,18 @@ static struct bus_type counter_bus_type = {
>  
>  static dev_t counter_devt;
>  
> +/**
> + * counter_priv - access counter device private data
> + * @counter: counter device
> + *
> + * Get the counter device private data
> + */
> +void *counter_priv(const struct counter_device *const counter)
> +{
> +	return counter->priv;
> +}
> +EXPORT_SYMBOL_GPL(counter_priv);
> +
>  /**
>   * counter_register - register Counter to the system
>   * @counter:	pointer to Counter to register
> diff --git a/include/linux/counter.h b/include/linux/counter.h
> index b7d0a00a61cf..8daaa38c71d8 100644
> --- a/include/linux/counter.h
> +++ b/include/linux/counter.h
> @@ -329,6 +329,8 @@ struct counter_device {
>  	struct mutex ops_exist_lock;
>  };
>  
> +void *counter_priv(const struct counter_device *const counter);
> +
>  int counter_register(struct counter_device *const counter);
>  void counter_unregister(struct counter_device *const counter);
>  int devm_counter_register(struct device *dev,
> -- 
> 2.33.0
>
diff mbox series

Patch

diff --git a/drivers/counter/counter-core.c b/drivers/counter/counter-core.c
index f053a43c6c04..00c41f28c101 100644
--- a/drivers/counter/counter-core.c
+++ b/drivers/counter/counter-core.c
@@ -45,6 +45,18 @@  static struct bus_type counter_bus_type = {
 
 static dev_t counter_devt;
 
+/**
+ * counter_priv - access counter device private data
+ * @counter: counter device
+ *
+ * Get the counter device private data
+ */
+void *counter_priv(const struct counter_device *const counter)
+{
+	return counter->priv;
+}
+EXPORT_SYMBOL_GPL(counter_priv);
+
 /**
  * counter_register - register Counter to the system
  * @counter:	pointer to Counter to register
diff --git a/include/linux/counter.h b/include/linux/counter.h
index b7d0a00a61cf..8daaa38c71d8 100644
--- a/include/linux/counter.h
+++ b/include/linux/counter.h
@@ -329,6 +329,8 @@  struct counter_device {
 	struct mutex ops_exist_lock;
 };
 
+void *counter_priv(const struct counter_device *const counter);
+
 int counter_register(struct counter_device *const counter);
 void counter_unregister(struct counter_device *const counter);
 int devm_counter_register(struct device *dev,