mbox series

[v1,00/23] counter: cleanups and device lifetime fixes

Message ID 20211225161056.682797-1-u.kleine-koenig@pengutronix.de (mailing list archive)
Headers show
Series counter: cleanups and device lifetime fixes | expand

Message

Uwe Kleine-König Dec. 25, 2021, 4:10 p.m. UTC
Hello,

this serie's goal is to fix struct device lifetime issues as pointed out
in patch #13. The patches up to patch #12 are only prepatory and cleanup
patches. Patch #13 provides the needed functions to fix the issues in
all drivers (patches #15 to #22). The last patch removes the then unused
API calls.

Best regards
Uwe

Uwe Kleine-König (23):
  counter: Use container_of instead of drvdata to track counter_device
  counter: ftm-quaddec: Drop unused platform_set_drvdata()
  counter: microchip-tcb-capture: Drop unused platform_set_drvdata()
  counter: Provide a wrapper to access device private data
  counter: 104-quad-8: Convert to counter_priv() wrapper
  counter: interrupt-cnt: Convert to counter_priv() wrapper
  counter: microchip-tcb-capture: Convert to counter_priv() wrapper
  counter: intel-qep: Convert to counter_priv() wrapper
  counter: ftm-quaddec: Convert to counter_priv() wrapper
  counter: ti-eqep: Convert to counter_priv() wrapper
  counter: stm32-lptimer-cnt: Convert to counter_priv() wrapper
  counter: stm32-timer-cnt: Convert to counter_priv() wrapper
  counter: Provide alternative counter registration functions
  counter: Update documentation for new counter registration functions
  counter: 104-quad-8: Convert to new counter registration
  counter: interrupt-cnt: Convert to new counter registration
  counter: intel-qep: Convert to new counter registration
  counter: ftm-quaddec: Convert to new counter registration
  counter: microchip-tcb-capture: Convert to new counter registration
  counter: stm32-timer-cnt: Convert to new counter registration
  counter: stm32-lptimer-cnt: Convert to new counter registration
  counter: ti-eqep: Convert to new counter registration
  counter: remove old and now unused registration API

 Documentation/driver-api/generic-counter.rst |  10 +-
 drivers/counter/104-quad-8.c                 |  88 +++++-----
 drivers/counter/counter-core.c               | 168 +++++++++++++------
 drivers/counter/ftm-quaddec.c                |  37 ++--
 drivers/counter/intel-qep.c                  |  46 ++---
 drivers/counter/interrupt-cnt.c              |  38 +++--
 drivers/counter/microchip-tcb-capture.c      |  44 ++---
 drivers/counter/stm32-lptimer-cnt.c          |  51 +++---
 drivers/counter/stm32-timer-cnt.c            |  48 +++---
 drivers/counter/ti-eqep.c                    |  47 +++---
 include/linux/counter.h                      |  15 +-
 11 files changed, 345 insertions(+), 247 deletions(-)


base-commit: a7904a538933c525096ca2ccde1e60d0ee62c08e

Comments

Uwe Kleine-König Dec. 25, 2021, 7:20 p.m. UTC | #1
Hello,

some sidestory to this series: While working on fixing the struct dev
lifetime thing, I saw a few ugly things that I don't intend to fix
myself. Here is just a mail to let you know this with a list of issues.
Feel free to address or ignore:

 - 104_QUAD_8 depends on X86, but compiles fine on ARCH=arm. Maybe
   adding support for COMPILE_TEST would be a good idea.

 - 104-quad-8.c uses devm_request_irq() and (now) devm_counter_add(). On
   unbind an irq might be pending which results in quad8_irq_handler()
   calling counter_push_event() for a counter that is already
   unregistered. (The issue exists also without my changes.)

 - I think intel-qep.c makes the counter unfunctional in
   intel_qep_remove before the counter is unregistered.

 - I wonder why counter is a bus and not a class device type. There is
   no driver that would ever bind a counter device, is there? So
   /sys/bus/counter/driver is always empty.

Best regards
Uwe
Uwe Kleine-König Dec. 27, 2021, 8:22 a.m. UTC | #2
Hello,

I just noticed I had a few dirty changes in my working copy that need to
be squashed into these commits:

diff --git a/drivers/counter/104-quad-8.c b/drivers/counter/104-quad-8.c
index 4315b14f239e..680c7ba943a4 100644
--- a/drivers/counter/104-quad-8.c
+++ b/drivers/counter/104-quad-8.c
@@ -1084,7 +1084,8 @@ static struct counter_count quad8_counts[] = {
 
 static irqreturn_t quad8_irq_handler(int irq, void *private)
 {
-	struct quad8 *const priv = private;
+	struct counter_device *counter = private;
+	struct quad8 *const priv = counter_priv(counter);
 	const unsigned long base = priv->base;
 	unsigned long irq_status;
 	unsigned long channel;
@@ -1115,7 +1116,7 @@ static irqreturn_t quad8_irq_handler(int irq, void *private)
 			continue;
 		}
 
-		counter_push_event(&priv->counter, event, channel);
+		counter_push_event(counter, event, channel);
 	}
 
 	/* Clear pending interrupts on device */
@@ -1192,7 +1193,7 @@ static int quad8_probe(struct device *dev, unsigned int id)
 	outb(QUAD8_CHAN_OP_ENABLE_INTERRUPT_FUNC, base[id] + QUAD8_REG_CHAN_OP);
 
 	err = devm_request_irq(dev, irq[id], quad8_irq_handler, IRQF_SHARED,
-			       counter->name, priv);
+			       counter->name, counter);
 	if (err)
 		return err;
 
diff --git a/drivers/counter/ftm-quaddec.c b/drivers/counter/ftm-quaddec.c
index d73506436e58..c636183b1337 100644
--- a/drivers/counter/ftm-quaddec.c
+++ b/drivers/counter/ftm-quaddec.c
@@ -301,7 +301,7 @@ static int ftm_quaddec_probe(struct platform_device *pdev)
 
 	ret = devm_counter_add(&pdev->dev, counter);
 	if (ret)
-		return dev_err_probe(&pdev->dev, "Failed to add counter\n");
+		return dev_err_probe(&pdev->dev, ret, "Failed to add counter\n");
 
 	return 0;
 }
diff --git a/drivers/counter/interrupt-cnt.c b/drivers/counter/interrupt-cnt.c
index 3f816454220e..9e99702470c2 100644
--- a/drivers/counter/interrupt-cnt.c
+++ b/drivers/counter/interrupt-cnt.c
@@ -215,7 +215,7 @@ static int interrupt_cnt_probe(struct platform_device *pdev)
 
 	ret = devm_counter_add(dev, counter);
 	if (ret < 0)
-		return dev_err_probe(dev, err, "Failed to add counter\n");
+		return dev_err_probe(dev, ret, "Failed to add counter\n");
 
 	return 0;
 }

Will fix these in a v2. 

Best regards
Uwe
David Lechner Dec. 27, 2021, 3:24 p.m. UTC | #3
On 12/25/21 1:20 PM, Uwe Kleine-König wrote:
> Hello,
> 
> some sidestory to this series: While working on fixing the struct dev
> lifetime thing, I saw a few ugly things that I don't intend to fix
> myself. Here is just a mail to let you know this with a list of issues.
> Feel free to address or ignore:
> 
>   - 104_QUAD_8 depends on X86, but compiles fine on ARCH=arm. Maybe
>     adding support for COMPILE_TEST would be a good idea.
> 
>   - 104-quad-8.c uses devm_request_irq() and (now) devm_counter_add(). On
>     unbind an irq might be pending which results in quad8_irq_handler()
>     calling counter_push_event() for a counter that is already
>     unregistered. (The issue exists also without my changes.)
> 
>   - I think intel-qep.c makes the counter unfunctional in
>     intel_qep_remove before the counter is unregistered.
> 
>   - I wonder why counter is a bus and not a class device type. There is
>     no driver that would ever bind a counter device, is there? So
>     /sys/bus/counter/driver is always empty.

This last item has been brought up before. The conclusion was yes it
should have been a class but it is too late to change it now since it
would break userspace.

(I think bus was cargo-culted from the iio subsystem.)