All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v3 00/23] counter: cleanups and device lifetime fixes
@ 2021-12-29 15:44 ` Uwe Kleine-König
  0 siblings, 0 replies; 48+ messages in thread
From: Uwe Kleine-König @ 2021-12-29 15:44 UTC (permalink / raw)
  To: William Breathitt Gray
  Cc: linux-iio, kernel, Jonathan Cameron, Greg Kroah-Hartman,
	linux-kernel, Patrick Havelange, Kamel Bouhara,
	Syed Nayyar Waris, Oleksij Rempel, Ahmad Fatoum, Jarkko Nikula,
	David Lechner, Fabrice Gasnier, Maxime Coquelin,
	Alexandre Torgue, linux-stm32, linux-arm-kernel, Jonathan Corbet

Hello,

this is v3 of my series to fix device lifetime issues in the counter
framework. This hopefully addresses all things pointed out for v2.

Note this depends on 60f07e74f86b (which is in next) now. Full diffstat
below.

Things that could be further improved:

 - 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.

 - counter_sysfs_add() allocates several small structures using
   devm_kcalloc for counter->dev. Not sure this could be optimized.
   Another ugly aspect here is, that counter->dev has no name, so the
   trace generated by the devres_log event contains several "(null)":

     sh-124     [000] d..1.   439.921733: devres_log: 40000000.timer:counter ADD f0ecfb01 devm_action_release (8 bytes)
     sh-124     [000] d..1.   439.921741: devres_log: (null) ADD 44be06af devm_kzalloc_release (64 bytes)
     sh-124     [000] d..1.   439.921750: devres_log: (null) ADD 16ad25f5 devm_kzalloc_release (8 bytes)
     sh-124     [000] d..1.   439.921755: devres_log: (null) ADD 7d22db15 devm_kzalloc_release (52 bytes)
     sh-124     [000] d..1.   439.921760: devres_log: (null) ADD 34aefeea devm_kzalloc_release (52 bytes)
     sh-124     [000] d..1.   439.921764: devres_log: (null) ADD f347e9c0 devm_kzalloc_release (8 bytes)
     sh-124     [000] d..1.   439.921768: devres_log: (null) ADD 960e132d devm_kzalloc_release (52 bytes)
     sh-124     [000] d..1.   439.921772: devres_log: (null) ADD 7b4c8adc devm_kzalloc_release (52 bytes)
     sh-124     [000] d..1.   439.921776: devres_log: (null) ADD 0931ec34 devm_kzalloc_release (7 bytes)
     sh-124     [000] d..1.   439.921782: devres_log: (null) ADD c8c6c1c3 devm_kzalloc_release (15 bytes)
     sh-124     [000] d..1.   439.921788: devres_log: (null) ADD d1bb5baf devm_kzalloc_release (52 bytes)
     sh-124     [000] d..1.   439.921791: devres_log: (null) ADD 94029520 devm_kzalloc_release (52 bytes)
     sh-124     [000] d..1.   439.921796: devres_log: (null) ADD 1dcc6eeb devm_kzalloc_release (25 bytes)
     sh-124     [000] d..1.   439.921801: devres_log: (null) ADD d37b87ec devm_kzalloc_release (52 bytes)
     sh-124     [000] d..1.   439.921805: devres_log: (null) ADD 660be81a devm_kzalloc_release (28 bytes)
     sh-124     [000] d..1.   439.921810: devres_log: (null) ADD e6691ecf devm_kzalloc_release (15 bytes)
     sh-124     [000] d..1.   439.921815: devres_log: (null) ADD 0b8e2064 devm_kzalloc_release (52 bytes)
     sh-124     [000] d..1.   439.921818: devres_log: (null) ADD b4dd5e7f devm_kzalloc_release (52 bytes)
     sh-124     [000] d..1.   439.921827: devres_log: (null) ADD 3eaaec2a devm_kzalloc_release (25 bytes)
     sh-124     [000] d..1.   439.921834: devres_log: (null) ADD e519abf5 devm_kzalloc_release (52 bytes)
     sh-124     [000] d..1.   439.921841: devres_log: (null) ADD 92663a7b devm_kzalloc_release (28 bytes)
     sh-124     [000] d..1.   439.921845: devres_log: (null) ADD 7835b138 devm_kzalloc_release (52 bytes)
     sh-124     [000] d..1.   439.921849: devres_log: (null) ADD 2dfa4f94 devm_kzalloc_release (52 bytes)
     sh-124     [000] d..1.   439.921853: devres_log: (null) ADD 561fde6a devm_kzalloc_release (52 bytes)
     sh-124     [000] d..1.   439.921856: devres_log: (null) ADD 6f8b5fa6 devm_kzalloc_release (52 bytes)
     sh-124     [000] d..1.   439.921860: devres_log: (null) ADD 39e5a4e8 devm_kzalloc_release (19 bytes)
     sh-124     [000] d..1.   439.921865: devres_log: (null) ADD f9156a41 devm_kzalloc_release (52 bytes)
     sh-124     [000] d..1.   439.921868: devres_log: (null) ADD d2d5ce75 devm_kzalloc_release (52 bytes)
     sh-124     [000] d..1.   439.921872: devres_log: (null) ADD 36d78a89 devm_kzalloc_release (23 bytes)
     sh-124     [000] d..1.   439.921877: devres_log: (null) ADD 2f5c179f devm_kzalloc_release (52 bytes)
     sh-124     [000] d..1.   439.921880: devres_log: (null) ADD 72cf7389 devm_kzalloc_release (52 bytes)
     sh-124     [000] d..1.   439.921885: devres_log: (null) ADD f9badaa6 devm_kzalloc_release (20 bytes)
     sh-124     [000] d..1.   439.921889: devres_log: (null) ADD 0bac4bae devm_kzalloc_release (52 bytes)
     sh-124     [000] d..1.   439.921892: devres_log: (null) ADD e25a23bb devm_kzalloc_release (52 bytes)
     sh-124     [000] d..1.   439.921896: devres_log: (null) ADD 77c03a96 devm_kzalloc_release (21 bytes)
     sh-124     [000] d..1.   439.921901: devres_log: (null) ADD fbcf25d4 devm_kzalloc_release (52 bytes)
     sh-124     [000] d..1.   439.921905: devres_log: (null) ADD 430046b3 devm_kzalloc_release (52 bytes)
     sh-124     [000] d..1.   439.921909: devres_log: (null) ADD cb0566d5 devm_kzalloc_release (52 bytes)
     sh-124     [000] d..1.   439.921912: devres_log: (null) ADD 5ec7bdc6 devm_kzalloc_release (52 bytes)
     sh-124     [000] d..1.   439.921916: devres_log: (null) ADD a8d30cb3 devm_kzalloc_release (20 bytes)
     sh-124     [000] d..1.   439.921926: devres_log: (null) ADD 655cd641 devm_kzalloc_release (80 bytes)
     sh-124     [000] d..1.   439.921931: devres_log: (null) ADD 7211e558 devm_kzalloc_release (12 bytes)
     sh-124     [000] d..1.   439.921934: devres_log: (null) ADD 1402f677 devm_kzalloc_release (12 bytes)
     sh-124     [000] d..1.   439.921937: devres_log: (null) ADD edc5c5b1 devm_kzalloc_release (68 bytes)
     sh-124     [000] d..1.   439.921941: devres_log: (null) ADD 5c7d0a3d devm_kzalloc_release (20 bytes)
     sh-124     [000] d..1.   439.922509: devres_log: 40000000.timer:counter ADD fb446f1c devm_action_release (8 bytes)

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                 |  93 +++++-----
 drivers/counter/counter-core.c               | 186 ++++++++++++++-----
 drivers/counter/ftm-quaddec.c                |  36 ++--
 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                    |  31 ++--
 include/linux/counter.h                      |  15 +-
 11 files changed, 356 insertions(+), 242 deletions(-)

Range-diff against v2:
 1:  0f73d029e55f !  1:  164f6fc76500 counter: Use container_of instead of drvdata to track counter_device
    @@ Commit message
         There are no other users of drvdata, so the call to dev_set_drvdata can
         go away, too.
     
    +    Reviewed-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
    +    Acked-by: William Breathitt Gray <vilhelm.gray@gmail.com>
         Signed-off-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>
     
      ## drivers/counter/counter-core.c ##
 2:  b2c4b1d30034 !  2:  3808b34176ed counter: ftm-quaddec: Drop unused platform_set_drvdata()
    @@ Commit message
         The driver doesn't ever use platform_get_drvdata, so drop this unused
         call.
     
    +    Reviewed-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
    +    Acked-by: William Breathitt Gray <vilhelm.gray@gmail.com>
         Signed-off-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>
     
      ## drivers/counter/ftm-quaddec.c ##
 3:  ac333fe0f34e !  3:  54a846344f2f counter: microchip-tcb-capture: Drop unused platform_set_drvdata()
    @@ Commit message
         The driver doesn't ever use platform_get_drvdata, so drop this unused
         call.
     
    +    Reviewed-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
    +    Acked-by: William Breathitt Gray <vilhelm.gray@gmail.com>
         Signed-off-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>
     
      ## drivers/counter/microchip-tcb-capture.c ##
 4:  adef4157e314 !  4:  c57b20dbdb7d counter: Provide a wrapper to access device private data
    @@ Commit message
         individually makes fixing device lifetime issues result in easier to
         review patches.
     
    +    Reviewed-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
    +    Acked-by: William Breathitt Gray <vilhelm.gray@gmail.com>
         Signed-off-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>
     
      ## drivers/counter/counter-core.c ##
 5:  c428105f13b1 !  5:  a81b257613f1 counter: 104-quad-8: Convert to counter_priv() wrapper
    @@ Commit message
     
         This is a straight forward conversion to the new counter_priv() wrapper.
     
    +    Acked-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
    +    Acked-by: William Breathitt Gray <vilhelm.gray@gmail.com>
         Signed-off-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>
     
      ## drivers/counter/104-quad-8.c ##
 6:  c72800db0f05 !  6:  a57ac51c8499 counter: interrupt-cnt: Convert to counter_priv() wrapper
    @@ Commit message
     
         This is a straight forward conversion to the new counter_priv() wrapper.
     
    +    Acked-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
    +    Acked-by: William Breathitt Gray <vilhelm.gray@gmail.com>
         Signed-off-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>
     
      ## drivers/counter/interrupt-cnt.c ##
 7:  d9fb86250b87 !  7:  2f227060747f counter: microchip-tcb-capture: Convert to counter_priv() wrapper
    @@ Commit message
     
         This is a straight forward conversion to the new counter_priv() wrapper.
     
    +    Acked-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
    +    Acked-by: William Breathitt Gray <vilhelm.gray@gmail.com>
         Signed-off-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>
     
      ## drivers/counter/microchip-tcb-capture.c ##
 8:  0d1e999aa274 !  8:  0d917f687039 counter: intel-qep: Convert to counter_priv() wrapper
    @@ Commit message
     
         This is a straight forward conversion to the new counter_priv() wrapper.
     
    +    Acked-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
    +    Acked-by: William Breathitt Gray <vilhelm.gray@gmail.com>
         Signed-off-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>
     
      ## drivers/counter/intel-qep.c ##
 9:  a2ffdf7dbac1 !  9:  82ad8f3060e2 counter: ftm-quaddec: Convert to counter_priv() wrapper
    @@ Commit message
     
         This is a straight forward conversion to the new counter_priv() wrapper.
     
    +    Acked-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
    +    Acked-by: William Breathitt Gray <vilhelm.gray@gmail.com>
         Signed-off-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>
     
      ## drivers/counter/ftm-quaddec.c ##
10:  95c643c47795 <  -:  ------------ counter: ti-eqep: Convert to counter_priv() wrapper
 -:  ------------ > 10:  0ed4c223ae8e counter: ti-eqep: Convert to counter_priv() wrapper
11:  6960d9cf21d2 ! 11:  caff57fb76ad counter: stm32-lptimer-cnt: Convert to counter_priv() wrapper
    @@ Commit message
     
         This is a straight forward conversion to the new counter_priv() wrapper.
     
    +    Acked-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
    +    Acked-by: William Breathitt Gray <vilhelm.gray@gmail.com>
         Signed-off-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>
     
      ## drivers/counter/stm32-lptimer-cnt.c ##
12:  deeca3a39ff5 ! 12:  c45c494ed207 counter: stm32-timer-cnt: Convert to counter_priv() wrapper
    @@ Commit message
     
         This is a straight forward conversion to the new counter_priv() wrapper.
     
    +    Acked-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
    +    Acked-by: William Breathitt Gray <vilhelm.gray@gmail.com>
         Signed-off-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>
     
      ## drivers/counter/stm32-timer-cnt.c ##
13:  a9ccb6fac99f ! 13:  8427cd271336 counter: Provide alternative counter registration functions
    @@ drivers/counter/counter-core.c
      
     +struct counter_device_allochelper {
     +	struct counter_device counter;
    -+	unsigned long privdata[];
    ++
    ++	/*
    ++	 * This is cache line aligned to ensure private data behaves like if it
    ++	 * were kmalloced separately.
    ++	 */
    ++	unsigned long privdata[] ____cacheline_aligned;
     +};
     +
      static void counter_device_release(struct device *dev)
    @@ drivers/counter/counter-core.c: int counter_register(struct counter_device *cons
     +	struct counter_device_allochelper *ch;
     +	struct counter_device *counter;
     +	struct device *dev;
    -+	int id, err;
    ++	int err;
     +
     +	ch = kzalloc(sizeof(*ch) + sizeof_priv, GFP_KERNEL);
     +	if (!ch) {
    @@ drivers/counter/counter-core.c: int counter_register(struct counter_device *cons
     +
     +	/* Acquire unique ID */
     +	err = ida_alloc(&counter_ida, GFP_KERNEL);
    -+	if (err < 0) {
    ++	if (err < 0)
     +		goto err_ida_alloc;
    -+	}
     +	dev->id = err;
     +
    ++	mutex_init(&counter->ops_exist_lock);
    ++	dev->type = &counter_device_type;
    ++	dev->bus = &counter_bus_type;
    ++	dev->devt = MKDEV(MAJOR(counter_devt), dev->id);
    ++
     +	err = counter_chrdev_add(counter);
     +	if (err < 0)
     +		goto err_chrdev_add;
     +
     +	device_initialize(dev);
    -+	/* Configure device structure for Counter */
    -+	dev->type = &counter_device_type;
    -+	dev->bus = &counter_bus_type;
    -+	dev->devt = MKDEV(MAJOR(counter_devt), id);
    -+
    -+	mutex_init(&counter->ops_exist_lock);
     +
     +	return counter;
     +
    @@ drivers/counter/counter-core.c: int counter_register(struct counter_device *cons
     +{
     +	put_device(&counter->dev);
     +}
    ++EXPORT_SYMBOL_GPL(counter_put);
     +
     +/**
     + * counter_add - complete registration of a counter
    @@ drivers/counter/counter-core.c: int counter_register(struct counter_device *cons
     +	int err;
     +	struct device *dev = &counter->dev;
     +
    -+	get_device(&counter->dev);
    -+
     +	if (counter->parent) {
     +		dev->parent = counter->parent;
     +		dev->of_node = counter->parent->of_node;
    @@ drivers/counter/counter-core.c: int counter_register(struct counter_device *cons
     +		return err;
     +
     +	/* implies device_add(dev) */
    -+	err = cdev_device_add(&counter->chrdev, dev);
    -+
    -+	return err;
    ++	return cdev_device_add(&counter->chrdev, dev);
     +}
     +EXPORT_SYMBOL_GPL(counter_add);
     +
      /**
       * counter_unregister - unregister Counter from the system
       * @counter:	pointer to Counter to unregister
    +@@ drivers/counter/counter-core.c: void counter_unregister(struct counter_device *const counter)
    + 
    + 	mutex_unlock(&counter->ops_exist_lock);
    + 
    +-	put_device(&counter->dev);
    ++	if (counter->legacy_device)
    ++		put_device(&counter->dev);
    + }
    + EXPORT_SYMBOL_GPL(counter_unregister);
    + 
     @@ drivers/counter/counter-core.c: int devm_counter_register(struct device *dev,
      }
      EXPORT_SYMBOL_GPL(devm_counter_register);
    @@ drivers/counter/counter-core.c: int devm_counter_register(struct device *dev,
     +	counter_put(counter);
     +}
     +
    ++/**
    ++ * devm_counter_alloc - allocate a counter_device
    ++ * @dev: the device to register the release callback for
    ++ * @sizeof_priv: size of the driver private data
    ++ *
    ++ * This is the device managed version of counter_add(). It registers a cleanup
    ++ * callback to care for calling counter_put().
    ++ */
     +struct counter_device *devm_counter_alloc(struct device *dev, size_t sizeof_priv)
     +{
     +	struct counter_device *counter;
    @@ drivers/counter/counter-core.c: int devm_counter_register(struct device *dev,
     +}
     +EXPORT_SYMBOL_GPL(devm_counter_alloc);
     +
    ++/**
    ++ * devm_counter_add - complete registration of a counter
    ++ * @dev: the device to register the release callback for
    ++ * @counter: the counter to add
    ++ *
    ++ * This is the device managed version of counter_add(). It registers a cleanup
    ++ * callback to care for calling counter_unregister().
    ++ */
     +int devm_counter_add(struct device *dev,
     +		     struct counter_device *const counter)
     +{
14:  7ae52f5b75f4 ! 14:  f841ae4f8909 counter: Update documentation for new counter registration functions
    @@ Commit message
         In order to replace the counter registration API also update the
         documentation to the new way.
     
    +    Reviewed-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
    +    Acked-by: William Breathitt Gray <vilhelm.gray@gmail.com>
         Signed-off-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>
     
      ## Documentation/driver-api/generic-counter.rst ##
    @@ Documentation/driver-api/generic-counter.rst: order to communicate with the devi
     -it to the counter_unregister function. Similarly, the
     -devm_counter_register function may be used if device memory-managed
     -registration is desired.
    -+A counter_device structure is supposed to be allocated using counter_alloc()
    -+and may be registered to the system by passing it to the counter_add()
    -+function, and unregistered by passing it to the counter_unregister function.
    -+There are device managed variants of these functions: devm_counter_alloc() and
    ++A counter_device structure is allocated using counter_alloc() and then
    ++registered to the system by passing it to the counter_add() function, and
    ++unregistered by passing it to the counter_unregister function. There are
    ++device managed variants of these functions: devm_counter_alloc() and
     +devm_counter_add().
      
      The struct counter_comp structure is used to define counter extensions
15:  02e54fed5b4e ! 15:  db635ea62ba4 counter: 104-quad-8: Convert to new counter registration
    @@ Commit message
         struct device.
     
         Fixes: f1d8a071d45b ("counter: 104-quad-8: Add Generic Counter interface support")
    +    Reviewed-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
    +    Acked-by: William Breathitt Gray <vilhelm.gray@gmail.com>
         Signed-off-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>
     
      ## drivers/counter/104-quad-8.c ##
    @@ drivers/counter/104-quad-8.c: MODULE_PARM_DESC(irq, "ACCES 104-QUAD-8 interrupt
      	unsigned int fck_prescaler[QUAD8_NUM_COUNTERS];
      	unsigned int preset[QUAD8_NUM_COUNTERS];
      	unsigned int count_mode[QUAD8_NUM_COUNTERS];
    +@@ drivers/counter/104-quad-8.c: 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;
    +@@ drivers/counter/104-quad-8.c: 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 */
     @@ drivers/counter/104-quad-8.c: static irqreturn_t quad8_irq_handler(int irq, void *private)
      
      static int quad8_probe(struct device *dev, unsigned int id)
    @@ drivers/counter/104-quad-8.c: static int quad8_probe(struct device *dev, unsigne
      
      	err = devm_request_irq(dev, irq[id], quad8_irq_handler, IRQF_SHARED,
     -			       priv->counter.name, priv);
    -+			       counter->name, priv);
    ++			       counter->name, counter);
      	if (err)
      		return err;
      
16:  728494402580 ! 16:  3ef149d41223 counter: interrupt-cnt: Convert to new counter registration
    @@ Commit message
         struct device.
     
         Fixes: a55ebd47f21f ("counter: add IRQ or GPIO based counter")
    +    Reviewed-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
    +    Acked-by: William Breathitt Gray <vilhelm.gray@gmail.com>
         Signed-off-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>
     
      ## drivers/counter/interrupt-cnt.c ##
17:  fe5266b17ded ! 17:  ad4a85dc9f99 counter: intel-qep: Convert to new counter registration
    @@ Commit message
         struct device.
     
         Fixes: b711f687a1c1 ("counter: Add support for Intel Quadrature Encoder Peripheral")
    +    Reviewed-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
    +    Acked-by: William Breathitt Gray <vilhelm.gray@gmail.com>
         Signed-off-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>
     
      ## drivers/counter/intel-qep.c ##
18:  e2610d673a23 ! 18:  192c5bba00e4 counter: ftm-quaddec: Convert to new counter registration
    @@ Commit message
         struct device.
     
         Fixes: a3b9a99980d9 ("counter: add FlexTimer Module Quadrature decoder counter driver")
    +    Reviewed-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
    +    Acked-by: William Breathitt Gray <vilhelm.gray@gmail.com>
         Signed-off-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>
     
    - ## drivers/counter/104-quad-8.c ##
    -@@ drivers/counter/104-quad-8.c: 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;
    -@@ drivers/counter/104-quad-8.c: 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 */
    -@@ drivers/counter/104-quad-8.c: 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;
    - 
    -
      ## drivers/counter/ftm-quaddec.c ##
     @@
      	})
    @@ drivers/counter/ftm-quaddec.c: static struct counter_count ftm_quaddec_counts =
      {
     +	struct counter_device *counter;
      	struct ftm_quaddec *ftm;
    --
    + 
      	struct device_node *node = pdev->dev.of_node;
      	struct resource *io;
      	int ret;
19:  fa10a9c1f369 ! 19:  8df134fb3723 counter: microchip-tcb-capture: Convert to new counter registration
    @@ Commit message
         struct device.
     
         Fixes: 106b104137fd ("counter: Add microchip TCB capture counter")
    +    Reviewed-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
    +    Acked-by: William Breathitt Gray <vilhelm.gray@gmail.com>
         Signed-off-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>
     
      ## drivers/counter/microchip-tcb-capture.c ##
20:  f697f9a17e6d ! 20:  ec4c0c5e183f counter: stm32-timer-cnt: Convert to new counter registration
    @@ Commit message
         struct device.
     
         Fixes: ad29937e206f ("counter: Add STM32 Timer quadrature encoder")
    +    Reviewed-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
    +    Acked-by: William Breathitt Gray <vilhelm.gray@gmail.com>
         Signed-off-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>
     
      ## drivers/counter/stm32-timer-cnt.c ##
21:  4368748521cc ! 21:  2b800479151d counter: stm32-lptimer-cnt: Convert to new counter registration
    @@ Commit message
         struct device.
     
         Fixes: 597f55e3f36c ("counter: stm32-lptimer: add counter device")
    +    Reviewed-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
    +    Acked-by: William Breathitt Gray <vilhelm.gray@gmail.com>
         Signed-off-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>
     
      ## drivers/counter/stm32-lptimer-cnt.c ##
22:  47485ea4bac8 ! 22:  97a2e4aac286 counter: ti-eqep: Convert to new counter registration
    @@ Commit message
         struct device.
     
         Fixes: f213729f6796 ("counter: new TI eQEP driver")
    +    Reviewed-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
    +    Acked-by: William Breathitt Gray <vilhelm.gray@gmail.com>
         Signed-off-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>
     
      ## drivers/counter/ti-eqep.c ##
23:  eb26d7efeede ! 23:  db76bf498f3f counter: remove old and now unused registration API
    @@ Commit message
         Usage of counter_register() yields issues in device lifetime tracking. All
         drivers were converted to the new API, so the old one can go away.
     
    +    Reviewed-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
    +    Acked-by: William Breathitt Gray <vilhelm.gray@gmail.com>
         Signed-off-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>
     
      ## drivers/counter/counter-core.c ##
    @@ drivers/counter/counter-core.c: static dev_t counter_devt;
      /**
       * counter_alloc - allocate a counter_device
       * @sizeof_priv: size of the driver private data
    +@@ drivers/counter/counter-core.c: void counter_unregister(struct counter_device *const counter)
    + 	wake_up(&counter->events_wait);
    + 
    + 	mutex_unlock(&counter->ops_exist_lock);
    +-
    +-	if (counter->legacy_device)
    +-		put_device(&counter->dev);
    + }
    + EXPORT_SYMBOL_GPL(counter_unregister);
    + 
     @@ drivers/counter/counter-core.c: static void devm_counter_release(void *counter)
      	counter_unregister(counter);
      }

base-commit: a7904a538933c525096ca2ccde1e60d0ee62c08e
prerequisite-patch-id: 9459ad8bc78190558df9123f8bebe28ca1c396ea
-- 
2.34.1


^ permalink raw reply	[flat|nested] 48+ messages in thread

* [PATCH v3 00/23] counter: cleanups and device lifetime fixes
@ 2021-12-29 15:44 ` Uwe Kleine-König
  0 siblings, 0 replies; 48+ messages in thread
From: Uwe Kleine-König @ 2021-12-29 15:44 UTC (permalink / raw)
  To: William Breathitt Gray
  Cc: linux-iio, kernel, Jonathan Cameron, Greg Kroah-Hartman,
	linux-kernel, Patrick Havelange, Kamel Bouhara,
	Syed Nayyar Waris, Oleksij Rempel, Ahmad Fatoum, Jarkko Nikula,
	David Lechner, Fabrice Gasnier, Maxime Coquelin,
	Alexandre Torgue, linux-stm32, linux-arm-kernel, Jonathan Corbet

Hello,

this is v3 of my series to fix device lifetime issues in the counter
framework. This hopefully addresses all things pointed out for v2.

Note this depends on 60f07e74f86b (which is in next) now. Full diffstat
below.

Things that could be further improved:

 - 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.

 - counter_sysfs_add() allocates several small structures using
   devm_kcalloc for counter->dev. Not sure this could be optimized.
   Another ugly aspect here is, that counter->dev has no name, so the
   trace generated by the devres_log event contains several "(null)":

     sh-124     [000] d..1.   439.921733: devres_log: 40000000.timer:counter ADD f0ecfb01 devm_action_release (8 bytes)
     sh-124     [000] d..1.   439.921741: devres_log: (null) ADD 44be06af devm_kzalloc_release (64 bytes)
     sh-124     [000] d..1.   439.921750: devres_log: (null) ADD 16ad25f5 devm_kzalloc_release (8 bytes)
     sh-124     [000] d..1.   439.921755: devres_log: (null) ADD 7d22db15 devm_kzalloc_release (52 bytes)
     sh-124     [000] d..1.   439.921760: devres_log: (null) ADD 34aefeea devm_kzalloc_release (52 bytes)
     sh-124     [000] d..1.   439.921764: devres_log: (null) ADD f347e9c0 devm_kzalloc_release (8 bytes)
     sh-124     [000] d..1.   439.921768: devres_log: (null) ADD 960e132d devm_kzalloc_release (52 bytes)
     sh-124     [000] d..1.   439.921772: devres_log: (null) ADD 7b4c8adc devm_kzalloc_release (52 bytes)
     sh-124     [000] d..1.   439.921776: devres_log: (null) ADD 0931ec34 devm_kzalloc_release (7 bytes)
     sh-124     [000] d..1.   439.921782: devres_log: (null) ADD c8c6c1c3 devm_kzalloc_release (15 bytes)
     sh-124     [000] d..1.   439.921788: devres_log: (null) ADD d1bb5baf devm_kzalloc_release (52 bytes)
     sh-124     [000] d..1.   439.921791: devres_log: (null) ADD 94029520 devm_kzalloc_release (52 bytes)
     sh-124     [000] d..1.   439.921796: devres_log: (null) ADD 1dcc6eeb devm_kzalloc_release (25 bytes)
     sh-124     [000] d..1.   439.921801: devres_log: (null) ADD d37b87ec devm_kzalloc_release (52 bytes)
     sh-124     [000] d..1.   439.921805: devres_log: (null) ADD 660be81a devm_kzalloc_release (28 bytes)
     sh-124     [000] d..1.   439.921810: devres_log: (null) ADD e6691ecf devm_kzalloc_release (15 bytes)
     sh-124     [000] d..1.   439.921815: devres_log: (null) ADD 0b8e2064 devm_kzalloc_release (52 bytes)
     sh-124     [000] d..1.   439.921818: devres_log: (null) ADD b4dd5e7f devm_kzalloc_release (52 bytes)
     sh-124     [000] d..1.   439.921827: devres_log: (null) ADD 3eaaec2a devm_kzalloc_release (25 bytes)
     sh-124     [000] d..1.   439.921834: devres_log: (null) ADD e519abf5 devm_kzalloc_release (52 bytes)
     sh-124     [000] d..1.   439.921841: devres_log: (null) ADD 92663a7b devm_kzalloc_release (28 bytes)
     sh-124     [000] d..1.   439.921845: devres_log: (null) ADD 7835b138 devm_kzalloc_release (52 bytes)
     sh-124     [000] d..1.   439.921849: devres_log: (null) ADD 2dfa4f94 devm_kzalloc_release (52 bytes)
     sh-124     [000] d..1.   439.921853: devres_log: (null) ADD 561fde6a devm_kzalloc_release (52 bytes)
     sh-124     [000] d..1.   439.921856: devres_log: (null) ADD 6f8b5fa6 devm_kzalloc_release (52 bytes)
     sh-124     [000] d..1.   439.921860: devres_log: (null) ADD 39e5a4e8 devm_kzalloc_release (19 bytes)
     sh-124     [000] d..1.   439.921865: devres_log: (null) ADD f9156a41 devm_kzalloc_release (52 bytes)
     sh-124     [000] d..1.   439.921868: devres_log: (null) ADD d2d5ce75 devm_kzalloc_release (52 bytes)
     sh-124     [000] d..1.   439.921872: devres_log: (null) ADD 36d78a89 devm_kzalloc_release (23 bytes)
     sh-124     [000] d..1.   439.921877: devres_log: (null) ADD 2f5c179f devm_kzalloc_release (52 bytes)
     sh-124     [000] d..1.   439.921880: devres_log: (null) ADD 72cf7389 devm_kzalloc_release (52 bytes)
     sh-124     [000] d..1.   439.921885: devres_log: (null) ADD f9badaa6 devm_kzalloc_release (20 bytes)
     sh-124     [000] d..1.   439.921889: devres_log: (null) ADD 0bac4bae devm_kzalloc_release (52 bytes)
     sh-124     [000] d..1.   439.921892: devres_log: (null) ADD e25a23bb devm_kzalloc_release (52 bytes)
     sh-124     [000] d..1.   439.921896: devres_log: (null) ADD 77c03a96 devm_kzalloc_release (21 bytes)
     sh-124     [000] d..1.   439.921901: devres_log: (null) ADD fbcf25d4 devm_kzalloc_release (52 bytes)
     sh-124     [000] d..1.   439.921905: devres_log: (null) ADD 430046b3 devm_kzalloc_release (52 bytes)
     sh-124     [000] d..1.   439.921909: devres_log: (null) ADD cb0566d5 devm_kzalloc_release (52 bytes)
     sh-124     [000] d..1.   439.921912: devres_log: (null) ADD 5ec7bdc6 devm_kzalloc_release (52 bytes)
     sh-124     [000] d..1.   439.921916: devres_log: (null) ADD a8d30cb3 devm_kzalloc_release (20 bytes)
     sh-124     [000] d..1.   439.921926: devres_log: (null) ADD 655cd641 devm_kzalloc_release (80 bytes)
     sh-124     [000] d..1.   439.921931: devres_log: (null) ADD 7211e558 devm_kzalloc_release (12 bytes)
     sh-124     [000] d..1.   439.921934: devres_log: (null) ADD 1402f677 devm_kzalloc_release (12 bytes)
     sh-124     [000] d..1.   439.921937: devres_log: (null) ADD edc5c5b1 devm_kzalloc_release (68 bytes)
     sh-124     [000] d..1.   439.921941: devres_log: (null) ADD 5c7d0a3d devm_kzalloc_release (20 bytes)
     sh-124     [000] d..1.   439.922509: devres_log: 40000000.timer:counter ADD fb446f1c devm_action_release (8 bytes)

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                 |  93 +++++-----
 drivers/counter/counter-core.c               | 186 ++++++++++++++-----
 drivers/counter/ftm-quaddec.c                |  36 ++--
 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                    |  31 ++--
 include/linux/counter.h                      |  15 +-
 11 files changed, 356 insertions(+), 242 deletions(-)

Range-diff against v2:
 1:  0f73d029e55f !  1:  164f6fc76500 counter: Use container_of instead of drvdata to track counter_device
    @@ Commit message
         There are no other users of drvdata, so the call to dev_set_drvdata can
         go away, too.
     
    +    Reviewed-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
    +    Acked-by: William Breathitt Gray <vilhelm.gray@gmail.com>
         Signed-off-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>
     
      ## drivers/counter/counter-core.c ##
 2:  b2c4b1d30034 !  2:  3808b34176ed counter: ftm-quaddec: Drop unused platform_set_drvdata()
    @@ Commit message
         The driver doesn't ever use platform_get_drvdata, so drop this unused
         call.
     
    +    Reviewed-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
    +    Acked-by: William Breathitt Gray <vilhelm.gray@gmail.com>
         Signed-off-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>
     
      ## drivers/counter/ftm-quaddec.c ##
 3:  ac333fe0f34e !  3:  54a846344f2f counter: microchip-tcb-capture: Drop unused platform_set_drvdata()
    @@ Commit message
         The driver doesn't ever use platform_get_drvdata, so drop this unused
         call.
     
    +    Reviewed-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
    +    Acked-by: William Breathitt Gray <vilhelm.gray@gmail.com>
         Signed-off-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>
     
      ## drivers/counter/microchip-tcb-capture.c ##
 4:  adef4157e314 !  4:  c57b20dbdb7d counter: Provide a wrapper to access device private data
    @@ Commit message
         individually makes fixing device lifetime issues result in easier to
         review patches.
     
    +    Reviewed-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
    +    Acked-by: William Breathitt Gray <vilhelm.gray@gmail.com>
         Signed-off-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>
     
      ## drivers/counter/counter-core.c ##
 5:  c428105f13b1 !  5:  a81b257613f1 counter: 104-quad-8: Convert to counter_priv() wrapper
    @@ Commit message
     
         This is a straight forward conversion to the new counter_priv() wrapper.
     
    +    Acked-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
    +    Acked-by: William Breathitt Gray <vilhelm.gray@gmail.com>
         Signed-off-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>
     
      ## drivers/counter/104-quad-8.c ##
 6:  c72800db0f05 !  6:  a57ac51c8499 counter: interrupt-cnt: Convert to counter_priv() wrapper
    @@ Commit message
     
         This is a straight forward conversion to the new counter_priv() wrapper.
     
    +    Acked-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
    +    Acked-by: William Breathitt Gray <vilhelm.gray@gmail.com>
         Signed-off-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>
     
      ## drivers/counter/interrupt-cnt.c ##
 7:  d9fb86250b87 !  7:  2f227060747f counter: microchip-tcb-capture: Convert to counter_priv() wrapper
    @@ Commit message
     
         This is a straight forward conversion to the new counter_priv() wrapper.
     
    +    Acked-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
    +    Acked-by: William Breathitt Gray <vilhelm.gray@gmail.com>
         Signed-off-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>
     
      ## drivers/counter/microchip-tcb-capture.c ##
 8:  0d1e999aa274 !  8:  0d917f687039 counter: intel-qep: Convert to counter_priv() wrapper
    @@ Commit message
     
         This is a straight forward conversion to the new counter_priv() wrapper.
     
    +    Acked-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
    +    Acked-by: William Breathitt Gray <vilhelm.gray@gmail.com>
         Signed-off-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>
     
      ## drivers/counter/intel-qep.c ##
 9:  a2ffdf7dbac1 !  9:  82ad8f3060e2 counter: ftm-quaddec: Convert to counter_priv() wrapper
    @@ Commit message
     
         This is a straight forward conversion to the new counter_priv() wrapper.
     
    +    Acked-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
    +    Acked-by: William Breathitt Gray <vilhelm.gray@gmail.com>
         Signed-off-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>
     
      ## drivers/counter/ftm-quaddec.c ##
10:  95c643c47795 <  -:  ------------ counter: ti-eqep: Convert to counter_priv() wrapper
 -:  ------------ > 10:  0ed4c223ae8e counter: ti-eqep: Convert to counter_priv() wrapper
11:  6960d9cf21d2 ! 11:  caff57fb76ad counter: stm32-lptimer-cnt: Convert to counter_priv() wrapper
    @@ Commit message
     
         This is a straight forward conversion to the new counter_priv() wrapper.
     
    +    Acked-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
    +    Acked-by: William Breathitt Gray <vilhelm.gray@gmail.com>
         Signed-off-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>
     
      ## drivers/counter/stm32-lptimer-cnt.c ##
12:  deeca3a39ff5 ! 12:  c45c494ed207 counter: stm32-timer-cnt: Convert to counter_priv() wrapper
    @@ Commit message
     
         This is a straight forward conversion to the new counter_priv() wrapper.
     
    +    Acked-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
    +    Acked-by: William Breathitt Gray <vilhelm.gray@gmail.com>
         Signed-off-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>
     
      ## drivers/counter/stm32-timer-cnt.c ##
13:  a9ccb6fac99f ! 13:  8427cd271336 counter: Provide alternative counter registration functions
    @@ drivers/counter/counter-core.c
      
     +struct counter_device_allochelper {
     +	struct counter_device counter;
    -+	unsigned long privdata[];
    ++
    ++	/*
    ++	 * This is cache line aligned to ensure private data behaves like if it
    ++	 * were kmalloced separately.
    ++	 */
    ++	unsigned long privdata[] ____cacheline_aligned;
     +};
     +
      static void counter_device_release(struct device *dev)
    @@ drivers/counter/counter-core.c: int counter_register(struct counter_device *cons
     +	struct counter_device_allochelper *ch;
     +	struct counter_device *counter;
     +	struct device *dev;
    -+	int id, err;
    ++	int err;
     +
     +	ch = kzalloc(sizeof(*ch) + sizeof_priv, GFP_KERNEL);
     +	if (!ch) {
    @@ drivers/counter/counter-core.c: int counter_register(struct counter_device *cons
     +
     +	/* Acquire unique ID */
     +	err = ida_alloc(&counter_ida, GFP_KERNEL);
    -+	if (err < 0) {
    ++	if (err < 0)
     +		goto err_ida_alloc;
    -+	}
     +	dev->id = err;
     +
    ++	mutex_init(&counter->ops_exist_lock);
    ++	dev->type = &counter_device_type;
    ++	dev->bus = &counter_bus_type;
    ++	dev->devt = MKDEV(MAJOR(counter_devt), dev->id);
    ++
     +	err = counter_chrdev_add(counter);
     +	if (err < 0)
     +		goto err_chrdev_add;
     +
     +	device_initialize(dev);
    -+	/* Configure device structure for Counter */
    -+	dev->type = &counter_device_type;
    -+	dev->bus = &counter_bus_type;
    -+	dev->devt = MKDEV(MAJOR(counter_devt), id);
    -+
    -+	mutex_init(&counter->ops_exist_lock);
     +
     +	return counter;
     +
    @@ drivers/counter/counter-core.c: int counter_register(struct counter_device *cons
     +{
     +	put_device(&counter->dev);
     +}
    ++EXPORT_SYMBOL_GPL(counter_put);
     +
     +/**
     + * counter_add - complete registration of a counter
    @@ drivers/counter/counter-core.c: int counter_register(struct counter_device *cons
     +	int err;
     +	struct device *dev = &counter->dev;
     +
    -+	get_device(&counter->dev);
    -+
     +	if (counter->parent) {
     +		dev->parent = counter->parent;
     +		dev->of_node = counter->parent->of_node;
    @@ drivers/counter/counter-core.c: int counter_register(struct counter_device *cons
     +		return err;
     +
     +	/* implies device_add(dev) */
    -+	err = cdev_device_add(&counter->chrdev, dev);
    -+
    -+	return err;
    ++	return cdev_device_add(&counter->chrdev, dev);
     +}
     +EXPORT_SYMBOL_GPL(counter_add);
     +
      /**
       * counter_unregister - unregister Counter from the system
       * @counter:	pointer to Counter to unregister
    +@@ drivers/counter/counter-core.c: void counter_unregister(struct counter_device *const counter)
    + 
    + 	mutex_unlock(&counter->ops_exist_lock);
    + 
    +-	put_device(&counter->dev);
    ++	if (counter->legacy_device)
    ++		put_device(&counter->dev);
    + }
    + EXPORT_SYMBOL_GPL(counter_unregister);
    + 
     @@ drivers/counter/counter-core.c: int devm_counter_register(struct device *dev,
      }
      EXPORT_SYMBOL_GPL(devm_counter_register);
    @@ drivers/counter/counter-core.c: int devm_counter_register(struct device *dev,
     +	counter_put(counter);
     +}
     +
    ++/**
    ++ * devm_counter_alloc - allocate a counter_device
    ++ * @dev: the device to register the release callback for
    ++ * @sizeof_priv: size of the driver private data
    ++ *
    ++ * This is the device managed version of counter_add(). It registers a cleanup
    ++ * callback to care for calling counter_put().
    ++ */
     +struct counter_device *devm_counter_alloc(struct device *dev, size_t sizeof_priv)
     +{
     +	struct counter_device *counter;
    @@ drivers/counter/counter-core.c: int devm_counter_register(struct device *dev,
     +}
     +EXPORT_SYMBOL_GPL(devm_counter_alloc);
     +
    ++/**
    ++ * devm_counter_add - complete registration of a counter
    ++ * @dev: the device to register the release callback for
    ++ * @counter: the counter to add
    ++ *
    ++ * This is the device managed version of counter_add(). It registers a cleanup
    ++ * callback to care for calling counter_unregister().
    ++ */
     +int devm_counter_add(struct device *dev,
     +		     struct counter_device *const counter)
     +{
14:  7ae52f5b75f4 ! 14:  f841ae4f8909 counter: Update documentation for new counter registration functions
    @@ Commit message
         In order to replace the counter registration API also update the
         documentation to the new way.
     
    +    Reviewed-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
    +    Acked-by: William Breathitt Gray <vilhelm.gray@gmail.com>
         Signed-off-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>
     
      ## Documentation/driver-api/generic-counter.rst ##
    @@ Documentation/driver-api/generic-counter.rst: order to communicate with the devi
     -it to the counter_unregister function. Similarly, the
     -devm_counter_register function may be used if device memory-managed
     -registration is desired.
    -+A counter_device structure is supposed to be allocated using counter_alloc()
    -+and may be registered to the system by passing it to the counter_add()
    -+function, and unregistered by passing it to the counter_unregister function.
    -+There are device managed variants of these functions: devm_counter_alloc() and
    ++A counter_device structure is allocated using counter_alloc() and then
    ++registered to the system by passing it to the counter_add() function, and
    ++unregistered by passing it to the counter_unregister function. There are
    ++device managed variants of these functions: devm_counter_alloc() and
     +devm_counter_add().
      
      The struct counter_comp structure is used to define counter extensions
15:  02e54fed5b4e ! 15:  db635ea62ba4 counter: 104-quad-8: Convert to new counter registration
    @@ Commit message
         struct device.
     
         Fixes: f1d8a071d45b ("counter: 104-quad-8: Add Generic Counter interface support")
    +    Reviewed-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
    +    Acked-by: William Breathitt Gray <vilhelm.gray@gmail.com>
         Signed-off-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>
     
      ## drivers/counter/104-quad-8.c ##
    @@ drivers/counter/104-quad-8.c: MODULE_PARM_DESC(irq, "ACCES 104-QUAD-8 interrupt
      	unsigned int fck_prescaler[QUAD8_NUM_COUNTERS];
      	unsigned int preset[QUAD8_NUM_COUNTERS];
      	unsigned int count_mode[QUAD8_NUM_COUNTERS];
    +@@ drivers/counter/104-quad-8.c: 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;
    +@@ drivers/counter/104-quad-8.c: 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 */
     @@ drivers/counter/104-quad-8.c: static irqreturn_t quad8_irq_handler(int irq, void *private)
      
      static int quad8_probe(struct device *dev, unsigned int id)
    @@ drivers/counter/104-quad-8.c: static int quad8_probe(struct device *dev, unsigne
      
      	err = devm_request_irq(dev, irq[id], quad8_irq_handler, IRQF_SHARED,
     -			       priv->counter.name, priv);
    -+			       counter->name, priv);
    ++			       counter->name, counter);
      	if (err)
      		return err;
      
16:  728494402580 ! 16:  3ef149d41223 counter: interrupt-cnt: Convert to new counter registration
    @@ Commit message
         struct device.
     
         Fixes: a55ebd47f21f ("counter: add IRQ or GPIO based counter")
    +    Reviewed-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
    +    Acked-by: William Breathitt Gray <vilhelm.gray@gmail.com>
         Signed-off-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>
     
      ## drivers/counter/interrupt-cnt.c ##
17:  fe5266b17ded ! 17:  ad4a85dc9f99 counter: intel-qep: Convert to new counter registration
    @@ Commit message
         struct device.
     
         Fixes: b711f687a1c1 ("counter: Add support for Intel Quadrature Encoder Peripheral")
    +    Reviewed-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
    +    Acked-by: William Breathitt Gray <vilhelm.gray@gmail.com>
         Signed-off-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>
     
      ## drivers/counter/intel-qep.c ##
18:  e2610d673a23 ! 18:  192c5bba00e4 counter: ftm-quaddec: Convert to new counter registration
    @@ Commit message
         struct device.
     
         Fixes: a3b9a99980d9 ("counter: add FlexTimer Module Quadrature decoder counter driver")
    +    Reviewed-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
    +    Acked-by: William Breathitt Gray <vilhelm.gray@gmail.com>
         Signed-off-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>
     
    - ## drivers/counter/104-quad-8.c ##
    -@@ drivers/counter/104-quad-8.c: 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;
    -@@ drivers/counter/104-quad-8.c: 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 */
    -@@ drivers/counter/104-quad-8.c: 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;
    - 
    -
      ## drivers/counter/ftm-quaddec.c ##
     @@
      	})
    @@ drivers/counter/ftm-quaddec.c: static struct counter_count ftm_quaddec_counts =
      {
     +	struct counter_device *counter;
      	struct ftm_quaddec *ftm;
    --
    + 
      	struct device_node *node = pdev->dev.of_node;
      	struct resource *io;
      	int ret;
19:  fa10a9c1f369 ! 19:  8df134fb3723 counter: microchip-tcb-capture: Convert to new counter registration
    @@ Commit message
         struct device.
     
         Fixes: 106b104137fd ("counter: Add microchip TCB capture counter")
    +    Reviewed-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
    +    Acked-by: William Breathitt Gray <vilhelm.gray@gmail.com>
         Signed-off-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>
     
      ## drivers/counter/microchip-tcb-capture.c ##
20:  f697f9a17e6d ! 20:  ec4c0c5e183f counter: stm32-timer-cnt: Convert to new counter registration
    @@ Commit message
         struct device.
     
         Fixes: ad29937e206f ("counter: Add STM32 Timer quadrature encoder")
    +    Reviewed-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
    +    Acked-by: William Breathitt Gray <vilhelm.gray@gmail.com>
         Signed-off-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>
     
      ## drivers/counter/stm32-timer-cnt.c ##
21:  4368748521cc ! 21:  2b800479151d counter: stm32-lptimer-cnt: Convert to new counter registration
    @@ Commit message
         struct device.
     
         Fixes: 597f55e3f36c ("counter: stm32-lptimer: add counter device")
    +    Reviewed-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
    +    Acked-by: William Breathitt Gray <vilhelm.gray@gmail.com>
         Signed-off-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>
     
      ## drivers/counter/stm32-lptimer-cnt.c ##
22:  47485ea4bac8 ! 22:  97a2e4aac286 counter: ti-eqep: Convert to new counter registration
    @@ Commit message
         struct device.
     
         Fixes: f213729f6796 ("counter: new TI eQEP driver")
    +    Reviewed-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
    +    Acked-by: William Breathitt Gray <vilhelm.gray@gmail.com>
         Signed-off-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>
     
      ## drivers/counter/ti-eqep.c ##
23:  eb26d7efeede ! 23:  db76bf498f3f counter: remove old and now unused registration API
    @@ Commit message
         Usage of counter_register() yields issues in device lifetime tracking. All
         drivers were converted to the new API, so the old one can go away.
     
    +    Reviewed-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
    +    Acked-by: William Breathitt Gray <vilhelm.gray@gmail.com>
         Signed-off-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>
     
      ## drivers/counter/counter-core.c ##
    @@ drivers/counter/counter-core.c: static dev_t counter_devt;
      /**
       * counter_alloc - allocate a counter_device
       * @sizeof_priv: size of the driver private data
    +@@ drivers/counter/counter-core.c: void counter_unregister(struct counter_device *const counter)
    + 	wake_up(&counter->events_wait);
    + 
    + 	mutex_unlock(&counter->ops_exist_lock);
    +-
    +-	if (counter->legacy_device)
    +-		put_device(&counter->dev);
    + }
    + EXPORT_SYMBOL_GPL(counter_unregister);
    + 
     @@ drivers/counter/counter-core.c: static void devm_counter_release(void *counter)
      	counter_unregister(counter);
      }

base-commit: a7904a538933c525096ca2ccde1e60d0ee62c08e
prerequisite-patch-id: 9459ad8bc78190558df9123f8bebe28ca1c396ea
-- 
2.34.1


_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

^ permalink raw reply	[flat|nested] 48+ messages in thread

* [PATCH v3 01/23] counter: Use container_of instead of drvdata to track counter_device
  2021-12-29 15:44 ` Uwe Kleine-König
  (?)
@ 2021-12-29 15:44 ` Uwe Kleine-König
  -1 siblings, 0 replies; 48+ messages in thread
From: Uwe Kleine-König @ 2021-12-29 15:44 UTC (permalink / raw)
  To: William Breathitt Gray
  Cc: linux-iio, kernel, Jonathan Cameron, Greg Kroah-Hartman, linux-kernel

The counter core uses drvdata to find a struct counter_device from a
struct device. However as the device is a member of struct counter_device,
the lookup can be done faster (and a bit type safe) using container_of.

There are no other users of drvdata, so the call to dev_set_drvdata can
go away, too.

Reviewed-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
Acked-by: William Breathitt Gray <vilhelm.gray@gmail.com>
Signed-off-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>
---
 drivers/counter/counter-core.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/drivers/counter/counter-core.c b/drivers/counter/counter-core.c
index 5acc54539623..f053a43c6c04 100644
--- a/drivers/counter/counter-core.c
+++ b/drivers/counter/counter-core.c
@@ -26,7 +26,8 @@ static DEFINE_IDA(counter_ida);
 
 static void counter_device_release(struct device *dev)
 {
-	struct counter_device *const counter = dev_get_drvdata(dev);
+	struct counter_device *const counter =
+		container_of(dev, struct counter_device, dev);
 
 	counter_chrdev_remove(counter);
 	ida_free(&counter_ida, dev->id);
@@ -78,7 +79,6 @@ int counter_register(struct counter_device *const counter)
 		dev->of_node = counter->parent->of_node;
 	}
 	device_initialize(dev);
-	dev_set_drvdata(dev, counter);
 
 	err = counter_sysfs_add(counter);
 	if (err < 0)
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 48+ messages in thread

* [PATCH v3 02/23] counter: ftm-quaddec: Drop unused platform_set_drvdata()
  2021-12-29 15:44 ` Uwe Kleine-König
  (?)
  (?)
@ 2021-12-29 15:44 ` Uwe Kleine-König
  -1 siblings, 0 replies; 48+ messages in thread
From: Uwe Kleine-König @ 2021-12-29 15:44 UTC (permalink / raw)
  To: William Breathitt Gray
  Cc: linux-iio, kernel, Jonathan Cameron, Greg Kroah-Hartman,
	linux-kernel, Patrick Havelange

The driver doesn't ever use platform_get_drvdata, so drop this unused
call.

Reviewed-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
Acked-by: William Breathitt Gray <vilhelm.gray@gmail.com>
Signed-off-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>
---
 drivers/counter/ftm-quaddec.c | 2 --
 1 file changed, 2 deletions(-)

diff --git a/drivers/counter/ftm-quaddec.c b/drivers/counter/ftm-quaddec.c
index 5ef0478709cd..9272f7b58beb 100644
--- a/drivers/counter/ftm-quaddec.c
+++ b/drivers/counter/ftm-quaddec.c
@@ -269,8 +269,6 @@ static int ftm_quaddec_probe(struct platform_device *pdev)
 	if (!ftm)
 		return -ENOMEM;
 
-	platform_set_drvdata(pdev, ftm);
-
 	io = platform_get_resource(pdev, IORESOURCE_MEM, 0);
 	if (!io) {
 		dev_err(&pdev->dev, "Failed to get memory region\n");
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 48+ messages in thread

* [PATCH v3 03/23] counter: microchip-tcb-capture: Drop unused platform_set_drvdata()
  2021-12-29 15:44 ` Uwe Kleine-König
                   ` (2 preceding siblings ...)
  (?)
@ 2021-12-29 15:44 ` Uwe Kleine-König
  -1 siblings, 0 replies; 48+ messages in thread
From: Uwe Kleine-König @ 2021-12-29 15:44 UTC (permalink / raw)
  To: William Breathitt Gray
  Cc: linux-iio, kernel, Jonathan Cameron, Greg Kroah-Hartman,
	linux-kernel, Kamel Bouhara

The driver doesn't ever use platform_get_drvdata, so drop this unused
call.

Reviewed-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
Acked-by: William Breathitt Gray <vilhelm.gray@gmail.com>
Signed-off-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>
---
 drivers/counter/microchip-tcb-capture.c | 2 --
 1 file changed, 2 deletions(-)

diff --git a/drivers/counter/microchip-tcb-capture.c b/drivers/counter/microchip-tcb-capture.c
index 0ab1b2716784..bb69f2e0ba93 100644
--- a/drivers/counter/microchip-tcb-capture.c
+++ b/drivers/counter/microchip-tcb-capture.c
@@ -307,8 +307,6 @@ static int mchp_tc_probe(struct platform_device *pdev)
 	if (!priv)
 		return -ENOMEM;
 
-	platform_set_drvdata(pdev, priv);
-
 	match = of_match_node(atmel_tc_of_match, np->parent);
 	tcb_config = match->data;
 	if (!tcb_config) {
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 48+ messages in thread

* [PATCH v3 04/23] counter: Provide a wrapper to access device private data
  2021-12-29 15:44 ` Uwe Kleine-König
                   ` (3 preceding siblings ...)
  (?)
@ 2021-12-29 15:44 ` Uwe Kleine-König
  -1 siblings, 0 replies; 48+ messages in thread
From: Uwe Kleine-König @ 2021-12-29 15:44 UTC (permalink / raw)
  To: William Breathitt Gray
  Cc: linux-iio, kernel, Jonathan Cameron, Greg Kroah-Hartman, linux-kernel

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.

Reviewed-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
Acked-by: William Breathitt Gray <vilhelm.gray@gmail.com>
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);
+
 /**
  * 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.34.1


^ permalink raw reply related	[flat|nested] 48+ messages in thread

* [PATCH v3 05/23] counter: 104-quad-8: Convert to counter_priv() wrapper
  2021-12-29 15:44 ` Uwe Kleine-König
                   ` (4 preceding siblings ...)
  (?)
@ 2021-12-29 15:44 ` Uwe Kleine-König
  2021-12-30 13:19   ` Greg Kroah-Hartman
  -1 siblings, 1 reply; 48+ messages in thread
From: Uwe Kleine-König @ 2021-12-29 15:44 UTC (permalink / raw)
  To: William Breathitt Gray
  Cc: linux-iio, kernel, Jonathan Cameron, Greg Kroah-Hartman,
	linux-kernel, Syed Nayyar Waris

This is a straight forward conversion to the new counter_priv() wrapper.

Acked-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
Acked-by: William Breathitt Gray <vilhelm.gray@gmail.com>
Signed-off-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>
---
 drivers/counter/104-quad-8.c | 58 ++++++++++++++++++------------------
 1 file changed, 29 insertions(+), 29 deletions(-)

diff --git a/drivers/counter/104-quad-8.c b/drivers/counter/104-quad-8.c
index 1cbd60aaed69..6e5286cd1d4e 100644
--- a/drivers/counter/104-quad-8.c
+++ b/drivers/counter/104-quad-8.c
@@ -113,7 +113,7 @@ static int quad8_signal_read(struct counter_device *counter,
 			     struct counter_signal *signal,
 			     enum counter_signal_level *level)
 {
-	const struct quad8 *const priv = counter->priv;
+	const struct quad8 *const priv = counter_priv(counter);
 	unsigned int state;
 
 	/* Only Index signal levels can be read */
@@ -131,7 +131,7 @@ static int quad8_signal_read(struct counter_device *counter,
 static int quad8_count_read(struct counter_device *counter,
 			    struct counter_count *count, u64 *val)
 {
-	struct quad8 *const priv = counter->priv;
+	struct quad8 *const priv = counter_priv(counter);
 	const int base_offset = priv->base + 2 * count->id;
 	unsigned int flags;
 	unsigned int borrow;
@@ -163,7 +163,7 @@ static int quad8_count_read(struct counter_device *counter,
 static int quad8_count_write(struct counter_device *counter,
 			     struct counter_count *count, u64 val)
 {
-	struct quad8 *const priv = counter->priv;
+	struct quad8 *const priv = counter_priv(counter);
 	const int base_offset = priv->base + 2 * count->id;
 	unsigned long irqflags;
 	int i;
@@ -213,7 +213,7 @@ static int quad8_function_read(struct counter_device *counter,
 			       struct counter_count *count,
 			       enum counter_function *function)
 {
-	struct quad8 *const priv = counter->priv;
+	struct quad8 *const priv = counter_priv(counter);
 	const int id = count->id;
 	unsigned long irqflags;
 
@@ -243,7 +243,7 @@ static int quad8_function_write(struct counter_device *counter,
 				struct counter_count *count,
 				enum counter_function function)
 {
-	struct quad8 *const priv = counter->priv;
+	struct quad8 *const priv = counter_priv(counter);
 	const int id = count->id;
 	unsigned int *const quadrature_mode = priv->quadrature_mode + id;
 	unsigned int *const scale = priv->quadrature_scale + id;
@@ -305,7 +305,7 @@ static int quad8_direction_read(struct counter_device *counter,
 				struct counter_count *count,
 				enum counter_count_direction *direction)
 {
-	const struct quad8 *const priv = counter->priv;
+	const struct quad8 *const priv = counter_priv(counter);
 	unsigned int ud_flag;
 	const unsigned int flag_addr = priv->base + 2 * count->id + 1;
 
@@ -335,7 +335,7 @@ static int quad8_action_read(struct counter_device *counter,
 			     struct counter_synapse *synapse,
 			     enum counter_synapse_action *action)
 {
-	struct quad8 *const priv = counter->priv;
+	struct quad8 *const priv = counter_priv(counter);
 	int err;
 	enum counter_function function;
 	const size_t signal_a_id = count->synapses[0].signal->id;
@@ -399,7 +399,7 @@ enum {
 
 static int quad8_events_configure(struct counter_device *counter)
 {
-	struct quad8 *const priv = counter->priv;
+	struct quad8 *const priv = counter_priv(counter);
 	unsigned long irq_enabled = 0;
 	unsigned long irqflags;
 	size_t channel;
@@ -442,7 +442,7 @@ static int quad8_events_configure(struct counter_device *counter)
 static int quad8_watch_validate(struct counter_device *counter,
 				const struct counter_watch *watch)
 {
-	struct quad8 *const priv = counter->priv;
+	struct quad8 *const priv = counter_priv(counter);
 
 	if (watch->channel > QUAD8_NUM_COUNTERS - 1)
 		return -EINVAL;
@@ -497,7 +497,7 @@ static int quad8_index_polarity_get(struct counter_device *counter,
 				    struct counter_signal *signal,
 				    u32 *index_polarity)
 {
-	const struct quad8 *const priv = counter->priv;
+	const struct quad8 *const priv = counter_priv(counter);
 	const size_t channel_id = signal->id - 16;
 
 	*index_polarity = priv->index_polarity[channel_id];
@@ -509,7 +509,7 @@ static int quad8_index_polarity_set(struct counter_device *counter,
 				    struct counter_signal *signal,
 				    u32 index_polarity)
 {
-	struct quad8 *const priv = counter->priv;
+	struct quad8 *const priv = counter_priv(counter);
 	const size_t channel_id = signal->id - 16;
 	const int base_offset = priv->base + 2 * channel_id + 1;
 	unsigned long irqflags;
@@ -538,7 +538,7 @@ static int quad8_synchronous_mode_get(struct counter_device *counter,
 				      struct counter_signal *signal,
 				      u32 *synchronous_mode)
 {
-	const struct quad8 *const priv = counter->priv;
+	const struct quad8 *const priv = counter_priv(counter);
 	const size_t channel_id = signal->id - 16;
 
 	*synchronous_mode = priv->synchronous_mode[channel_id];
@@ -550,7 +550,7 @@ static int quad8_synchronous_mode_set(struct counter_device *counter,
 				      struct counter_signal *signal,
 				      u32 synchronous_mode)
 {
-	struct quad8 *const priv = counter->priv;
+	struct quad8 *const priv = counter_priv(counter);
 	const size_t channel_id = signal->id - 16;
 	const int base_offset = priv->base + 2 * channel_id + 1;
 	unsigned long irqflags;
@@ -589,7 +589,7 @@ static int quad8_count_mode_read(struct counter_device *counter,
 				 struct counter_count *count,
 				 enum counter_count_mode *cnt_mode)
 {
-	const struct quad8 *const priv = counter->priv;
+	const struct quad8 *const priv = counter_priv(counter);
 
 	/* Map 104-QUAD-8 count mode to Generic Counter count mode */
 	switch (priv->count_mode[count->id]) {
@@ -614,7 +614,7 @@ static int quad8_count_mode_write(struct counter_device *counter,
 				  struct counter_count *count,
 				  enum counter_count_mode cnt_mode)
 {
-	struct quad8 *const priv = counter->priv;
+	struct quad8 *const priv = counter_priv(counter);
 	unsigned int count_mode;
 	unsigned int mode_cfg;
 	const int base_offset = priv->base + 2 * count->id + 1;
@@ -661,7 +661,7 @@ static int quad8_count_mode_write(struct counter_device *counter,
 static int quad8_count_enable_read(struct counter_device *counter,
 				   struct counter_count *count, u8 *enable)
 {
-	const struct quad8 *const priv = counter->priv;
+	const struct quad8 *const priv = counter_priv(counter);
 
 	*enable = priv->ab_enable[count->id];
 
@@ -671,7 +671,7 @@ static int quad8_count_enable_read(struct counter_device *counter,
 static int quad8_count_enable_write(struct counter_device *counter,
 				    struct counter_count *count, u8 enable)
 {
-	struct quad8 *const priv = counter->priv;
+	struct quad8 *const priv = counter_priv(counter);
 	const int base_offset = priv->base + 2 * count->id;
 	unsigned long irqflags;
 	unsigned int ior_cfg;
@@ -699,7 +699,7 @@ static const char *const quad8_noise_error_states[] = {
 static int quad8_error_noise_get(struct counter_device *counter,
 				 struct counter_count *count, u32 *noise_error)
 {
-	const struct quad8 *const priv = counter->priv;
+	const struct quad8 *const priv = counter_priv(counter);
 	const int base_offset = priv->base + 2 * count->id + 1;
 
 	*noise_error = !!(inb(base_offset) & QUAD8_FLAG_E);
@@ -710,7 +710,7 @@ static int quad8_error_noise_get(struct counter_device *counter,
 static int quad8_count_preset_read(struct counter_device *counter,
 				   struct counter_count *count, u64 *preset)
 {
-	const struct quad8 *const priv = counter->priv;
+	const struct quad8 *const priv = counter_priv(counter);
 
 	*preset = priv->preset[count->id];
 
@@ -736,7 +736,7 @@ static void quad8_preset_register_set(struct quad8 *const priv, const int id,
 static int quad8_count_preset_write(struct counter_device *counter,
 				    struct counter_count *count, u64 preset)
 {
-	struct quad8 *const priv = counter->priv;
+	struct quad8 *const priv = counter_priv(counter);
 	unsigned long irqflags;
 
 	/* Only 24-bit values are supported */
@@ -755,7 +755,7 @@ static int quad8_count_preset_write(struct counter_device *counter,
 static int quad8_count_ceiling_read(struct counter_device *counter,
 				    struct counter_count *count, u64 *ceiling)
 {
-	struct quad8 *const priv = counter->priv;
+	struct quad8 *const priv = counter_priv(counter);
 	unsigned long irqflags;
 
 	spin_lock_irqsave(&priv->lock, irqflags);
@@ -780,7 +780,7 @@ static int quad8_count_ceiling_read(struct counter_device *counter,
 static int quad8_count_ceiling_write(struct counter_device *counter,
 				     struct counter_count *count, u64 ceiling)
 {
-	struct quad8 *const priv = counter->priv;
+	struct quad8 *const priv = counter_priv(counter);
 	unsigned long irqflags;
 
 	/* Only 24-bit values are supported */
@@ -807,7 +807,7 @@ static int quad8_count_preset_enable_read(struct counter_device *counter,
 					  struct counter_count *count,
 					  u8 *preset_enable)
 {
-	const struct quad8 *const priv = counter->priv;
+	const struct quad8 *const priv = counter_priv(counter);
 
 	*preset_enable = !priv->preset_enable[count->id];
 
@@ -818,7 +818,7 @@ static int quad8_count_preset_enable_write(struct counter_device *counter,
 					   struct counter_count *count,
 					   u8 preset_enable)
 {
-	struct quad8 *const priv = counter->priv;
+	struct quad8 *const priv = counter_priv(counter);
 	const int base_offset = priv->base + 2 * count->id + 1;
 	unsigned long irqflags;
 	unsigned int ior_cfg;
@@ -845,7 +845,7 @@ static int quad8_signal_cable_fault_read(struct counter_device *counter,
 					 struct counter_signal *signal,
 					 u8 *cable_fault)
 {
-	struct quad8 *const priv = counter->priv;
+	struct quad8 *const priv = counter_priv(counter);
 	const size_t channel_id = signal->id / 2;
 	unsigned long irqflags;
 	bool disabled;
@@ -875,7 +875,7 @@ static int quad8_signal_cable_fault_enable_read(struct counter_device *counter,
 						struct counter_signal *signal,
 						u8 *enable)
 {
-	const struct quad8 *const priv = counter->priv;
+	const struct quad8 *const priv = counter_priv(counter);
 	const size_t channel_id = signal->id / 2;
 
 	*enable = !!(priv->cable_fault_enable & BIT(channel_id));
@@ -887,7 +887,7 @@ static int quad8_signal_cable_fault_enable_write(struct counter_device *counter,
 						 struct counter_signal *signal,
 						 u8 enable)
 {
-	struct quad8 *const priv = counter->priv;
+	struct quad8 *const priv = counter_priv(counter);
 	const size_t channel_id = signal->id / 2;
 	unsigned long irqflags;
 	unsigned int cable_fault_enable;
@@ -913,7 +913,7 @@ static int quad8_signal_fck_prescaler_read(struct counter_device *counter,
 					   struct counter_signal *signal,
 					   u8 *prescaler)
 {
-	const struct quad8 *const priv = counter->priv;
+	const struct quad8 *const priv = counter_priv(counter);
 
 	*prescaler = priv->fck_prescaler[signal->id / 2];
 
@@ -924,7 +924,7 @@ static int quad8_signal_fck_prescaler_write(struct counter_device *counter,
 					    struct counter_signal *signal,
 					    u8 prescaler)
 {
-	struct quad8 *const priv = counter->priv;
+	struct quad8 *const priv = counter_priv(counter);
 	const size_t channel_id = signal->id / 2;
 	const int base_offset = priv->base + 2 * channel_id;
 	unsigned long irqflags;
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 48+ messages in thread

* [PATCH v3 06/23] counter: interrupt-cnt: Convert to counter_priv() wrapper
  2021-12-29 15:44 ` Uwe Kleine-König
                   ` (5 preceding siblings ...)
  (?)
@ 2021-12-29 15:44 ` Uwe Kleine-König
  -1 siblings, 0 replies; 48+ messages in thread
From: Uwe Kleine-König @ 2021-12-29 15:44 UTC (permalink / raw)
  To: William Breathitt Gray
  Cc: linux-iio, kernel, Jonathan Cameron, Greg Kroah-Hartman,
	linux-kernel, Oleksij Rempel, Ahmad Fatoum

This is a straight forward conversion to the new counter_priv() wrapper.

Acked-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
Acked-by: William Breathitt Gray <vilhelm.gray@gmail.com>
Signed-off-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>
---
 drivers/counter/interrupt-cnt.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)

diff --git a/drivers/counter/interrupt-cnt.c b/drivers/counter/interrupt-cnt.c
index 8514a87fcbee..4bf706ef46e2 100644
--- a/drivers/counter/interrupt-cnt.c
+++ b/drivers/counter/interrupt-cnt.c
@@ -37,7 +37,7 @@ static irqreturn_t interrupt_cnt_isr(int irq, void *dev_id)
 static int interrupt_cnt_enable_read(struct counter_device *counter,
 				     struct counter_count *count, u8 *enable)
 {
-	struct interrupt_cnt_priv *priv = counter->priv;
+	struct interrupt_cnt_priv *priv = counter_priv(counter);
 
 	*enable = priv->enabled;
 
@@ -47,7 +47,7 @@ static int interrupt_cnt_enable_read(struct counter_device *counter,
 static int interrupt_cnt_enable_write(struct counter_device *counter,
 				      struct counter_count *count, u8 enable)
 {
-	struct interrupt_cnt_priv *priv = counter->priv;
+	struct interrupt_cnt_priv *priv = counter_priv(counter);
 
 	if (priv->enabled == enable)
 		return 0;
@@ -85,7 +85,7 @@ static int interrupt_cnt_action_read(struct counter_device *counter,
 static int interrupt_cnt_read(struct counter_device *counter,
 			      struct counter_count *count, u64 *val)
 {
-	struct interrupt_cnt_priv *priv = counter->priv;
+	struct interrupt_cnt_priv *priv = counter_priv(counter);
 
 	*val = atomic_read(&priv->count);
 
@@ -95,7 +95,7 @@ static int interrupt_cnt_read(struct counter_device *counter,
 static int interrupt_cnt_write(struct counter_device *counter,
 			       struct counter_count *count, const u64 val)
 {
-	struct interrupt_cnt_priv *priv = counter->priv;
+	struct interrupt_cnt_priv *priv = counter_priv(counter);
 
 	if (val != (typeof(priv->count.counter))val)
 		return -ERANGE;
@@ -122,7 +122,7 @@ static int interrupt_cnt_signal_read(struct counter_device *counter,
 				     struct counter_signal *signal,
 				     enum counter_signal_level *level)
 {
-	struct interrupt_cnt_priv *priv = counter->priv;
+	struct interrupt_cnt_priv *priv = counter_priv(counter);
 	int ret;
 
 	if (!priv->gpio)
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 48+ messages in thread

* [PATCH v3 07/23] counter: microchip-tcb-capture: Convert to counter_priv() wrapper
  2021-12-29 15:44 ` Uwe Kleine-König
                   ` (6 preceding siblings ...)
  (?)
@ 2021-12-29 15:44 ` Uwe Kleine-König
  -1 siblings, 0 replies; 48+ messages in thread
From: Uwe Kleine-König @ 2021-12-29 15:44 UTC (permalink / raw)
  To: William Breathitt Gray
  Cc: linux-iio, kernel, Jonathan Cameron, Greg Kroah-Hartman,
	linux-kernel, Kamel Bouhara

This is a straight forward conversion to the new counter_priv() wrapper.

Acked-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
Acked-by: William Breathitt Gray <vilhelm.gray@gmail.com>
Signed-off-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>
---
 drivers/counter/microchip-tcb-capture.c | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)

diff --git a/drivers/counter/microchip-tcb-capture.c b/drivers/counter/microchip-tcb-capture.c
index bb69f2e0ba93..1b56b7444668 100644
--- a/drivers/counter/microchip-tcb-capture.c
+++ b/drivers/counter/microchip-tcb-capture.c
@@ -72,7 +72,7 @@ static int mchp_tc_count_function_read(struct counter_device *counter,
 				       struct counter_count *count,
 				       enum counter_function *function)
 {
-	struct mchp_tc_data *const priv = counter->priv;
+	struct mchp_tc_data *const priv = counter_priv(counter);
 
 	if (priv->qdec_mode)
 		*function = COUNTER_FUNCTION_QUADRATURE_X4;
@@ -86,7 +86,7 @@ static int mchp_tc_count_function_write(struct counter_device *counter,
 					struct counter_count *count,
 					enum counter_function function)
 {
-	struct mchp_tc_data *const priv = counter->priv;
+	struct mchp_tc_data *const priv = counter_priv(counter);
 	u32 bmr, cmr;
 
 	regmap_read(priv->regmap, ATMEL_TC_BMR, &bmr);
@@ -148,7 +148,7 @@ static int mchp_tc_count_signal_read(struct counter_device *counter,
 				     struct counter_signal *signal,
 				     enum counter_signal_level *lvl)
 {
-	struct mchp_tc_data *const priv = counter->priv;
+	struct mchp_tc_data *const priv = counter_priv(counter);
 	bool sigstatus;
 	u32 sr;
 
@@ -169,7 +169,7 @@ static int mchp_tc_count_action_read(struct counter_device *counter,
 				     struct counter_synapse *synapse,
 				     enum counter_synapse_action *action)
 {
-	struct mchp_tc_data *const priv = counter->priv;
+	struct mchp_tc_data *const priv = counter_priv(counter);
 	u32 cmr;
 
 	regmap_read(priv->regmap, ATMEL_TC_REG(priv->channel[0], CMR), &cmr);
@@ -197,7 +197,7 @@ static int mchp_tc_count_action_write(struct counter_device *counter,
 				      struct counter_synapse *synapse,
 				      enum counter_synapse_action action)
 {
-	struct mchp_tc_data *const priv = counter->priv;
+	struct mchp_tc_data *const priv = counter_priv(counter);
 	u32 edge = ATMEL_TC_ETRGEDG_NONE;
 
 	/* QDEC mode is rising edge only */
@@ -230,7 +230,7 @@ static int mchp_tc_count_action_write(struct counter_device *counter,
 static int mchp_tc_count_read(struct counter_device *counter,
 			      struct counter_count *count, u64 *val)
 {
-	struct mchp_tc_data *const priv = counter->priv;
+	struct mchp_tc_data *const priv = counter_priv(counter);
 	u32 cnt;
 
 	regmap_read(priv->regmap, ATMEL_TC_REG(priv->channel[0], CV), &cnt);
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 48+ messages in thread

* [PATCH v3 08/23] counter: intel-qep: Convert to counter_priv() wrapper
  2021-12-29 15:44 ` Uwe Kleine-König
                   ` (7 preceding siblings ...)
  (?)
@ 2021-12-29 15:44 ` Uwe Kleine-König
  2021-12-30 10:26   ` Jarkko Nikula
  -1 siblings, 1 reply; 48+ messages in thread
From: Uwe Kleine-König @ 2021-12-29 15:44 UTC (permalink / raw)
  To: William Breathitt Gray
  Cc: linux-iio, kernel, Jonathan Cameron, Greg Kroah-Hartman,
	linux-kernel, Jarkko Nikula

This is a straight forward conversion to the new counter_priv() wrapper.

Acked-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
Acked-by: William Breathitt Gray <vilhelm.gray@gmail.com>
Signed-off-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>
---
 drivers/counter/intel-qep.c | 18 +++++++++---------
 1 file changed, 9 insertions(+), 9 deletions(-)

diff --git a/drivers/counter/intel-qep.c b/drivers/counter/intel-qep.c
index 0924d16de6e2..8f84a48508ac 100644
--- a/drivers/counter/intel-qep.c
+++ b/drivers/counter/intel-qep.c
@@ -109,7 +109,7 @@ static void intel_qep_init(struct intel_qep *qep)
 static int intel_qep_count_read(struct counter_device *counter,
 				struct counter_count *count, u64 *val)
 {
-	struct intel_qep *const qep = counter->priv;
+	struct intel_qep *const qep = counter_priv(counter);
 
 	pm_runtime_get_sync(qep->dev);
 	*val = intel_qep_readl(qep, INTEL_QEPCOUNT);
@@ -176,7 +176,7 @@ static struct counter_synapse intel_qep_count_synapses[] = {
 static int intel_qep_ceiling_read(struct counter_device *counter,
 				  struct counter_count *count, u64 *ceiling)
 {
-	struct intel_qep *qep = counter->priv;
+	struct intel_qep *qep = counter_priv(counter);
 
 	pm_runtime_get_sync(qep->dev);
 	*ceiling = intel_qep_readl(qep, INTEL_QEPMAX);
@@ -188,7 +188,7 @@ static int intel_qep_ceiling_read(struct counter_device *counter,
 static int intel_qep_ceiling_write(struct counter_device *counter,
 				   struct counter_count *count, u64 max)
 {
-	struct intel_qep *qep = counter->priv;
+	struct intel_qep *qep = counter_priv(counter);
 	int ret = 0;
 
 	/* Intel QEP ceiling configuration only supports 32-bit values */
@@ -213,7 +213,7 @@ static int intel_qep_ceiling_write(struct counter_device *counter,
 static int intel_qep_enable_read(struct counter_device *counter,
 				 struct counter_count *count, u8 *enable)
 {
-	struct intel_qep *qep = counter->priv;
+	struct intel_qep *qep = counter_priv(counter);
 
 	*enable = qep->enabled;
 
@@ -223,7 +223,7 @@ static int intel_qep_enable_read(struct counter_device *counter,
 static int intel_qep_enable_write(struct counter_device *counter,
 				  struct counter_count *count, u8 val)
 {
-	struct intel_qep *qep = counter->priv;
+	struct intel_qep *qep = counter_priv(counter);
 	u32 reg;
 	bool changed;
 
@@ -256,7 +256,7 @@ static int intel_qep_spike_filter_ns_read(struct counter_device *counter,
 					  struct counter_count *count,
 					  u64 *length)
 {
-	struct intel_qep *qep = counter->priv;
+	struct intel_qep *qep = counter_priv(counter);
 	u32 reg;
 
 	pm_runtime_get_sync(qep->dev);
@@ -277,7 +277,7 @@ static int intel_qep_spike_filter_ns_write(struct counter_device *counter,
 					   struct counter_count *count,
 					   u64 length)
 {
-	struct intel_qep *qep = counter->priv;
+	struct intel_qep *qep = counter_priv(counter);
 	u32 reg;
 	bool enable;
 	int ret = 0;
@@ -326,7 +326,7 @@ static int intel_qep_preset_enable_read(struct counter_device *counter,
 					struct counter_count *count,
 					u8 *preset_enable)
 {
-	struct intel_qep *qep = counter->priv;
+	struct intel_qep *qep = counter_priv(counter);
 	u32 reg;
 
 	pm_runtime_get_sync(qep->dev);
@@ -341,7 +341,7 @@ static int intel_qep_preset_enable_read(struct counter_device *counter,
 static int intel_qep_preset_enable_write(struct counter_device *counter,
 					 struct counter_count *count, u8 val)
 {
-	struct intel_qep *qep = counter->priv;
+	struct intel_qep *qep = counter_priv(counter);
 	u32 reg;
 	int ret = 0;
 
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 48+ messages in thread

* [PATCH v3 09/23] counter: ftm-quaddec: Convert to counter_priv() wrapper
  2021-12-29 15:44 ` Uwe Kleine-König
                   ` (8 preceding siblings ...)
  (?)
@ 2021-12-29 15:44 ` Uwe Kleine-König
  -1 siblings, 0 replies; 48+ messages in thread
From: Uwe Kleine-König @ 2021-12-29 15:44 UTC (permalink / raw)
  To: William Breathitt Gray
  Cc: linux-iio, kernel, Jonathan Cameron, Greg Kroah-Hartman,
	linux-kernel, Patrick Havelange

This is a straight forward conversion to the new counter_priv() wrapper.

Acked-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
Acked-by: William Breathitt Gray <vilhelm.gray@gmail.com>
Signed-off-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>
---
 drivers/counter/ftm-quaddec.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/drivers/counter/ftm-quaddec.c b/drivers/counter/ftm-quaddec.c
index 9272f7b58beb..f5d92df6a611 100644
--- a/drivers/counter/ftm-quaddec.c
+++ b/drivers/counter/ftm-quaddec.c
@@ -118,7 +118,7 @@ static void ftm_quaddec_disable(void *ftm)
 static int ftm_quaddec_get_prescaler(struct counter_device *counter,
 				     struct counter_count *count, u32 *cnt_mode)
 {
-	struct ftm_quaddec *ftm = counter->priv;
+	struct ftm_quaddec *ftm = counter_priv(counter);
 	uint32_t scflags;
 
 	ftm_read(ftm, FTM_SC, &scflags);
@@ -131,7 +131,7 @@ static int ftm_quaddec_get_prescaler(struct counter_device *counter,
 static int ftm_quaddec_set_prescaler(struct counter_device *counter,
 				     struct counter_count *count, u32 cnt_mode)
 {
-	struct ftm_quaddec *ftm = counter->priv;
+	struct ftm_quaddec *ftm = counter_priv(counter);
 
 	mutex_lock(&ftm->ftm_quaddec_mutex);
 
@@ -162,7 +162,7 @@ static int ftm_quaddec_count_read(struct counter_device *counter,
 				  struct counter_count *count,
 				  u64 *val)
 {
-	struct ftm_quaddec *const ftm = counter->priv;
+	struct ftm_quaddec *const ftm = counter_priv(counter);
 	uint32_t cntval;
 
 	ftm_read(ftm, FTM_CNT, &cntval);
@@ -176,7 +176,7 @@ static int ftm_quaddec_count_write(struct counter_device *counter,
 				   struct counter_count *count,
 				   const u64 val)
 {
-	struct ftm_quaddec *const ftm = counter->priv;
+	struct ftm_quaddec *const ftm = counter_priv(counter);
 
 	if (val != 0) {
 		dev_warn(&ftm->pdev->dev, "Can only accept '0' as new counter value\n");
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 48+ messages in thread

* [PATCH v3 10/23] counter: ti-eqep: Convert to counter_priv() wrapper
  2021-12-29 15:44 ` Uwe Kleine-König
                   ` (9 preceding siblings ...)
  (?)
@ 2021-12-29 15:44 ` Uwe Kleine-König
  2021-12-29 22:42   ` William Breathitt Gray
  2021-12-29 22:56   ` David Lechner
  -1 siblings, 2 replies; 48+ messages in thread
From: Uwe Kleine-König @ 2021-12-29 15:44 UTC (permalink / raw)
  To: William Breathitt Gray
  Cc: linux-iio, kernel, Jonathan Cameron, Greg Kroah-Hartman,
	linux-kernel, David Lechner

This is a straight forward conversion to the new counter_priv() wrapper.

Signed-off-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>
---
 drivers/counter/ti-eqep.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/counter/ti-eqep.c b/drivers/counter/ti-eqep.c
index 9e0e46bca4c2..2c70b900a6cf 100644
--- a/drivers/counter/ti-eqep.c
+++ b/drivers/counter/ti-eqep.c
@@ -89,7 +89,7 @@ struct ti_eqep_cnt {
 
 static struct ti_eqep_cnt *ti_eqep_count_from_counter(struct counter_device *counter)
 {
-	return container_of(counter, struct ti_eqep_cnt, counter);
+	return counter_priv(counter);
 }
 
 static int ti_eqep_count_read(struct counter_device *counter,
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 48+ messages in thread

* [PATCH v3 11/23] counter: stm32-lptimer-cnt: Convert to counter_priv() wrapper
  2021-12-29 15:44 ` Uwe Kleine-König
@ 2021-12-29 15:44   ` Uwe Kleine-König
  -1 siblings, 0 replies; 48+ messages in thread
From: Uwe Kleine-König @ 2021-12-29 15:44 UTC (permalink / raw)
  To: William Breathitt Gray
  Cc: linux-iio, kernel, Jonathan Cameron, Greg Kroah-Hartman,
	linux-kernel, Fabrice Gasnier, Maxime Coquelin, Alexandre Torgue,
	linux-stm32, linux-arm-kernel

This is a straight forward conversion to the new counter_priv() wrapper.

Acked-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
Acked-by: William Breathitt Gray <vilhelm.gray@gmail.com>
Signed-off-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>
---
 drivers/counter/stm32-lptimer-cnt.c | 18 +++++++++---------
 1 file changed, 9 insertions(+), 9 deletions(-)

diff --git a/drivers/counter/stm32-lptimer-cnt.c b/drivers/counter/stm32-lptimer-cnt.c
index 5168833b1fdf..9cf00e929cc0 100644
--- a/drivers/counter/stm32-lptimer-cnt.c
+++ b/drivers/counter/stm32-lptimer-cnt.c
@@ -141,7 +141,7 @@ static const enum counter_synapse_action stm32_lptim_cnt_synapse_actions[] = {
 static int stm32_lptim_cnt_read(struct counter_device *counter,
 				struct counter_count *count, u64 *val)
 {
-	struct stm32_lptim_cnt *const priv = counter->priv;
+	struct stm32_lptim_cnt *const priv = counter_priv(counter);
 	u32 cnt;
 	int ret;
 
@@ -158,7 +158,7 @@ static int stm32_lptim_cnt_function_read(struct counter_device *counter,
 					 struct counter_count *count,
 					 enum counter_function *function)
 {
-	struct stm32_lptim_cnt *const priv = counter->priv;
+	struct stm32_lptim_cnt *const priv = counter_priv(counter);
 
 	if (!priv->quadrature_mode) {
 		*function = COUNTER_FUNCTION_INCREASE;
@@ -177,7 +177,7 @@ static int stm32_lptim_cnt_function_write(struct counter_device *counter,
 					  struct counter_count *count,
 					  enum counter_function function)
 {
-	struct stm32_lptim_cnt *const priv = counter->priv;
+	struct stm32_lptim_cnt *const priv = counter_priv(counter);
 
 	if (stm32_lptim_is_enabled(priv))
 		return -EBUSY;
@@ -200,7 +200,7 @@ static int stm32_lptim_cnt_enable_read(struct counter_device *counter,
 				       struct counter_count *count,
 				       u8 *enable)
 {
-	struct stm32_lptim_cnt *const priv = counter->priv;
+	struct stm32_lptim_cnt *const priv = counter_priv(counter);
 	int ret;
 
 	ret = stm32_lptim_is_enabled(priv);
@@ -216,7 +216,7 @@ static int stm32_lptim_cnt_enable_write(struct counter_device *counter,
 					struct counter_count *count,
 					u8 enable)
 {
-	struct stm32_lptim_cnt *const priv = counter->priv;
+	struct stm32_lptim_cnt *const priv = counter_priv(counter);
 	int ret;
 
 	/* Check nobody uses the timer, or already disabled/enabled */
@@ -241,7 +241,7 @@ static int stm32_lptim_cnt_ceiling_read(struct counter_device *counter,
 					struct counter_count *count,
 					u64 *ceiling)
 {
-	struct stm32_lptim_cnt *const priv = counter->priv;
+	struct stm32_lptim_cnt *const priv = counter_priv(counter);
 
 	*ceiling = priv->ceiling;
 
@@ -252,7 +252,7 @@ static int stm32_lptim_cnt_ceiling_write(struct counter_device *counter,
 					 struct counter_count *count,
 					 u64 ceiling)
 {
-	struct stm32_lptim_cnt *const priv = counter->priv;
+	struct stm32_lptim_cnt *const priv = counter_priv(counter);
 
 	if (stm32_lptim_is_enabled(priv))
 		return -EBUSY;
@@ -277,7 +277,7 @@ static int stm32_lptim_cnt_action_read(struct counter_device *counter,
 				       struct counter_synapse *synapse,
 				       enum counter_synapse_action *action)
 {
-	struct stm32_lptim_cnt *const priv = counter->priv;
+	struct stm32_lptim_cnt *const priv = counter_priv(counter);
 	enum counter_function function;
 	int err;
 
@@ -321,7 +321,7 @@ static int stm32_lptim_cnt_action_write(struct counter_device *counter,
 					struct counter_synapse *synapse,
 					enum counter_synapse_action action)
 {
-	struct stm32_lptim_cnt *const priv = counter->priv;
+	struct stm32_lptim_cnt *const priv = counter_priv(counter);
 	enum counter_function function;
 	int err;
 
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 48+ messages in thread

* [PATCH v3 11/23] counter: stm32-lptimer-cnt: Convert to counter_priv() wrapper
@ 2021-12-29 15:44   ` Uwe Kleine-König
  0 siblings, 0 replies; 48+ messages in thread
From: Uwe Kleine-König @ 2021-12-29 15:44 UTC (permalink / raw)
  To: William Breathitt Gray
  Cc: linux-iio, kernel, Jonathan Cameron, Greg Kroah-Hartman,
	linux-kernel, Fabrice Gasnier, Maxime Coquelin, Alexandre Torgue,
	linux-stm32, linux-arm-kernel

This is a straight forward conversion to the new counter_priv() wrapper.

Acked-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
Acked-by: William Breathitt Gray <vilhelm.gray@gmail.com>
Signed-off-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>
---
 drivers/counter/stm32-lptimer-cnt.c | 18 +++++++++---------
 1 file changed, 9 insertions(+), 9 deletions(-)

diff --git a/drivers/counter/stm32-lptimer-cnt.c b/drivers/counter/stm32-lptimer-cnt.c
index 5168833b1fdf..9cf00e929cc0 100644
--- a/drivers/counter/stm32-lptimer-cnt.c
+++ b/drivers/counter/stm32-lptimer-cnt.c
@@ -141,7 +141,7 @@ static const enum counter_synapse_action stm32_lptim_cnt_synapse_actions[] = {
 static int stm32_lptim_cnt_read(struct counter_device *counter,
 				struct counter_count *count, u64 *val)
 {
-	struct stm32_lptim_cnt *const priv = counter->priv;
+	struct stm32_lptim_cnt *const priv = counter_priv(counter);
 	u32 cnt;
 	int ret;
 
@@ -158,7 +158,7 @@ static int stm32_lptim_cnt_function_read(struct counter_device *counter,
 					 struct counter_count *count,
 					 enum counter_function *function)
 {
-	struct stm32_lptim_cnt *const priv = counter->priv;
+	struct stm32_lptim_cnt *const priv = counter_priv(counter);
 
 	if (!priv->quadrature_mode) {
 		*function = COUNTER_FUNCTION_INCREASE;
@@ -177,7 +177,7 @@ static int stm32_lptim_cnt_function_write(struct counter_device *counter,
 					  struct counter_count *count,
 					  enum counter_function function)
 {
-	struct stm32_lptim_cnt *const priv = counter->priv;
+	struct stm32_lptim_cnt *const priv = counter_priv(counter);
 
 	if (stm32_lptim_is_enabled(priv))
 		return -EBUSY;
@@ -200,7 +200,7 @@ static int stm32_lptim_cnt_enable_read(struct counter_device *counter,
 				       struct counter_count *count,
 				       u8 *enable)
 {
-	struct stm32_lptim_cnt *const priv = counter->priv;
+	struct stm32_lptim_cnt *const priv = counter_priv(counter);
 	int ret;
 
 	ret = stm32_lptim_is_enabled(priv);
@@ -216,7 +216,7 @@ static int stm32_lptim_cnt_enable_write(struct counter_device *counter,
 					struct counter_count *count,
 					u8 enable)
 {
-	struct stm32_lptim_cnt *const priv = counter->priv;
+	struct stm32_lptim_cnt *const priv = counter_priv(counter);
 	int ret;
 
 	/* Check nobody uses the timer, or already disabled/enabled */
@@ -241,7 +241,7 @@ static int stm32_lptim_cnt_ceiling_read(struct counter_device *counter,
 					struct counter_count *count,
 					u64 *ceiling)
 {
-	struct stm32_lptim_cnt *const priv = counter->priv;
+	struct stm32_lptim_cnt *const priv = counter_priv(counter);
 
 	*ceiling = priv->ceiling;
 
@@ -252,7 +252,7 @@ static int stm32_lptim_cnt_ceiling_write(struct counter_device *counter,
 					 struct counter_count *count,
 					 u64 ceiling)
 {
-	struct stm32_lptim_cnt *const priv = counter->priv;
+	struct stm32_lptim_cnt *const priv = counter_priv(counter);
 
 	if (stm32_lptim_is_enabled(priv))
 		return -EBUSY;
@@ -277,7 +277,7 @@ static int stm32_lptim_cnt_action_read(struct counter_device *counter,
 				       struct counter_synapse *synapse,
 				       enum counter_synapse_action *action)
 {
-	struct stm32_lptim_cnt *const priv = counter->priv;
+	struct stm32_lptim_cnt *const priv = counter_priv(counter);
 	enum counter_function function;
 	int err;
 
@@ -321,7 +321,7 @@ static int stm32_lptim_cnt_action_write(struct counter_device *counter,
 					struct counter_synapse *synapse,
 					enum counter_synapse_action action)
 {
-	struct stm32_lptim_cnt *const priv = counter->priv;
+	struct stm32_lptim_cnt *const priv = counter_priv(counter);
 	enum counter_function function;
 	int err;
 
-- 
2.34.1


_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

^ permalink raw reply related	[flat|nested] 48+ messages in thread

* [PATCH v3 12/23] counter: stm32-timer-cnt: Convert to counter_priv() wrapper
  2021-12-29 15:44 ` Uwe Kleine-König
@ 2021-12-29 15:44   ` Uwe Kleine-König
  -1 siblings, 0 replies; 48+ messages in thread
From: Uwe Kleine-König @ 2021-12-29 15:44 UTC (permalink / raw)
  To: William Breathitt Gray
  Cc: linux-iio, kernel, Jonathan Cameron, Greg Kroah-Hartman,
	linux-kernel, Fabrice Gasnier, Maxime Coquelin, Alexandre Torgue,
	linux-stm32, linux-arm-kernel

This is a straight forward conversion to the new counter_priv() wrapper.

Acked-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
Acked-by: William Breathitt Gray <vilhelm.gray@gmail.com>
Signed-off-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>
---
 drivers/counter/stm32-timer-cnt.c | 18 +++++++++---------
 1 file changed, 9 insertions(+), 9 deletions(-)

diff --git a/drivers/counter/stm32-timer-cnt.c b/drivers/counter/stm32-timer-cnt.c
index 0546e932db0c..4b05b198a8d8 100644
--- a/drivers/counter/stm32-timer-cnt.c
+++ b/drivers/counter/stm32-timer-cnt.c
@@ -47,7 +47,7 @@ static const enum counter_function stm32_count_functions[] = {
 static int stm32_count_read(struct counter_device *counter,
 			    struct counter_count *count, u64 *val)
 {
-	struct stm32_timer_cnt *const priv = counter->priv;
+	struct stm32_timer_cnt *const priv = counter_priv(counter);
 	u32 cnt;
 
 	regmap_read(priv->regmap, TIM_CNT, &cnt);
@@ -59,7 +59,7 @@ static int stm32_count_read(struct counter_device *counter,
 static int stm32_count_write(struct counter_device *counter,
 			     struct counter_count *count, const u64 val)
 {
-	struct stm32_timer_cnt *const priv = counter->priv;
+	struct stm32_timer_cnt *const priv = counter_priv(counter);
 	u32 ceiling;
 
 	regmap_read(priv->regmap, TIM_ARR, &ceiling);
@@ -73,7 +73,7 @@ static int stm32_count_function_read(struct counter_device *counter,
 				     struct counter_count *count,
 				     enum counter_function *function)
 {
-	struct stm32_timer_cnt *const priv = counter->priv;
+	struct stm32_timer_cnt *const priv = counter_priv(counter);
 	u32 smcr;
 
 	regmap_read(priv->regmap, TIM_SMCR, &smcr);
@@ -100,7 +100,7 @@ static int stm32_count_function_write(struct counter_device *counter,
 				      struct counter_count *count,
 				      enum counter_function function)
 {
-	struct stm32_timer_cnt *const priv = counter->priv;
+	struct stm32_timer_cnt *const priv = counter_priv(counter);
 	u32 cr1, sms;
 
 	switch (function) {
@@ -140,7 +140,7 @@ static int stm32_count_direction_read(struct counter_device *counter,
 				      struct counter_count *count,
 				      enum counter_count_direction *direction)
 {
-	struct stm32_timer_cnt *const priv = counter->priv;
+	struct stm32_timer_cnt *const priv = counter_priv(counter);
 	u32 cr1;
 
 	regmap_read(priv->regmap, TIM_CR1, &cr1);
@@ -153,7 +153,7 @@ static int stm32_count_direction_read(struct counter_device *counter,
 static int stm32_count_ceiling_read(struct counter_device *counter,
 				    struct counter_count *count, u64 *ceiling)
 {
-	struct stm32_timer_cnt *const priv = counter->priv;
+	struct stm32_timer_cnt *const priv = counter_priv(counter);
 	u32 arr;
 
 	regmap_read(priv->regmap, TIM_ARR, &arr);
@@ -166,7 +166,7 @@ static int stm32_count_ceiling_read(struct counter_device *counter,
 static int stm32_count_ceiling_write(struct counter_device *counter,
 				     struct counter_count *count, u64 ceiling)
 {
-	struct stm32_timer_cnt *const priv = counter->priv;
+	struct stm32_timer_cnt *const priv = counter_priv(counter);
 
 	if (ceiling > priv->max_arr)
 		return -ERANGE;
@@ -181,7 +181,7 @@ static int stm32_count_ceiling_write(struct counter_device *counter,
 static int stm32_count_enable_read(struct counter_device *counter,
 				   struct counter_count *count, u8 *enable)
 {
-	struct stm32_timer_cnt *const priv = counter->priv;
+	struct stm32_timer_cnt *const priv = counter_priv(counter);
 	u32 cr1;
 
 	regmap_read(priv->regmap, TIM_CR1, &cr1);
@@ -194,7 +194,7 @@ static int stm32_count_enable_read(struct counter_device *counter,
 static int stm32_count_enable_write(struct counter_device *counter,
 				    struct counter_count *count, u8 enable)
 {
-	struct stm32_timer_cnt *const priv = counter->priv;
+	struct stm32_timer_cnt *const priv = counter_priv(counter);
 	u32 cr1;
 
 	if (enable) {
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 48+ messages in thread

* [PATCH v3 12/23] counter: stm32-timer-cnt: Convert to counter_priv() wrapper
@ 2021-12-29 15:44   ` Uwe Kleine-König
  0 siblings, 0 replies; 48+ messages in thread
From: Uwe Kleine-König @ 2021-12-29 15:44 UTC (permalink / raw)
  To: William Breathitt Gray
  Cc: linux-iio, kernel, Jonathan Cameron, Greg Kroah-Hartman,
	linux-kernel, Fabrice Gasnier, Maxime Coquelin, Alexandre Torgue,
	linux-stm32, linux-arm-kernel

This is a straight forward conversion to the new counter_priv() wrapper.

Acked-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
Acked-by: William Breathitt Gray <vilhelm.gray@gmail.com>
Signed-off-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>
---
 drivers/counter/stm32-timer-cnt.c | 18 +++++++++---------
 1 file changed, 9 insertions(+), 9 deletions(-)

diff --git a/drivers/counter/stm32-timer-cnt.c b/drivers/counter/stm32-timer-cnt.c
index 0546e932db0c..4b05b198a8d8 100644
--- a/drivers/counter/stm32-timer-cnt.c
+++ b/drivers/counter/stm32-timer-cnt.c
@@ -47,7 +47,7 @@ static const enum counter_function stm32_count_functions[] = {
 static int stm32_count_read(struct counter_device *counter,
 			    struct counter_count *count, u64 *val)
 {
-	struct stm32_timer_cnt *const priv = counter->priv;
+	struct stm32_timer_cnt *const priv = counter_priv(counter);
 	u32 cnt;
 
 	regmap_read(priv->regmap, TIM_CNT, &cnt);
@@ -59,7 +59,7 @@ static int stm32_count_read(struct counter_device *counter,
 static int stm32_count_write(struct counter_device *counter,
 			     struct counter_count *count, const u64 val)
 {
-	struct stm32_timer_cnt *const priv = counter->priv;
+	struct stm32_timer_cnt *const priv = counter_priv(counter);
 	u32 ceiling;
 
 	regmap_read(priv->regmap, TIM_ARR, &ceiling);
@@ -73,7 +73,7 @@ static int stm32_count_function_read(struct counter_device *counter,
 				     struct counter_count *count,
 				     enum counter_function *function)
 {
-	struct stm32_timer_cnt *const priv = counter->priv;
+	struct stm32_timer_cnt *const priv = counter_priv(counter);
 	u32 smcr;
 
 	regmap_read(priv->regmap, TIM_SMCR, &smcr);
@@ -100,7 +100,7 @@ static int stm32_count_function_write(struct counter_device *counter,
 				      struct counter_count *count,
 				      enum counter_function function)
 {
-	struct stm32_timer_cnt *const priv = counter->priv;
+	struct stm32_timer_cnt *const priv = counter_priv(counter);
 	u32 cr1, sms;
 
 	switch (function) {
@@ -140,7 +140,7 @@ static int stm32_count_direction_read(struct counter_device *counter,
 				      struct counter_count *count,
 				      enum counter_count_direction *direction)
 {
-	struct stm32_timer_cnt *const priv = counter->priv;
+	struct stm32_timer_cnt *const priv = counter_priv(counter);
 	u32 cr1;
 
 	regmap_read(priv->regmap, TIM_CR1, &cr1);
@@ -153,7 +153,7 @@ static int stm32_count_direction_read(struct counter_device *counter,
 static int stm32_count_ceiling_read(struct counter_device *counter,
 				    struct counter_count *count, u64 *ceiling)
 {
-	struct stm32_timer_cnt *const priv = counter->priv;
+	struct stm32_timer_cnt *const priv = counter_priv(counter);
 	u32 arr;
 
 	regmap_read(priv->regmap, TIM_ARR, &arr);
@@ -166,7 +166,7 @@ static int stm32_count_ceiling_read(struct counter_device *counter,
 static int stm32_count_ceiling_write(struct counter_device *counter,
 				     struct counter_count *count, u64 ceiling)
 {
-	struct stm32_timer_cnt *const priv = counter->priv;
+	struct stm32_timer_cnt *const priv = counter_priv(counter);
 
 	if (ceiling > priv->max_arr)
 		return -ERANGE;
@@ -181,7 +181,7 @@ static int stm32_count_ceiling_write(struct counter_device *counter,
 static int stm32_count_enable_read(struct counter_device *counter,
 				   struct counter_count *count, u8 *enable)
 {
-	struct stm32_timer_cnt *const priv = counter->priv;
+	struct stm32_timer_cnt *const priv = counter_priv(counter);
 	u32 cr1;
 
 	regmap_read(priv->regmap, TIM_CR1, &cr1);
@@ -194,7 +194,7 @@ static int stm32_count_enable_read(struct counter_device *counter,
 static int stm32_count_enable_write(struct counter_device *counter,
 				    struct counter_count *count, u8 enable)
 {
-	struct stm32_timer_cnt *const priv = counter->priv;
+	struct stm32_timer_cnt *const priv = counter_priv(counter);
 	u32 cr1;
 
 	if (enable) {
-- 
2.34.1


_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

^ permalink raw reply related	[flat|nested] 48+ messages in thread

* [PATCH v3 13/23] counter: Provide alternative counter registration functions
  2021-12-29 15:44 ` Uwe Kleine-König
                   ` (12 preceding siblings ...)
  (?)
@ 2021-12-29 15:44 ` Uwe Kleine-König
  2021-12-29 17:06   ` Jonathan Cameron
  -1 siblings, 1 reply; 48+ messages in thread
From: Uwe Kleine-König @ 2021-12-29 15:44 UTC (permalink / raw)
  To: William Breathitt Gray
  Cc: linux-iio, kernel, Jonathan Cameron, Greg Kroah-Hartman, linux-kernel

The current implementation gets device lifetime tracking wrong. The
problem is that allocation of struct counter_device is controlled by the
individual drivers but this structure contains a struct device that
might have to live longer than a driver is bound. As a result a command
sequence like:

	{ sleep 5; echo bang; } > /dev/counter0 &
	sleep 1;
	echo 40000000.timer:counter > /sys/bus/platform/drivers/stm32-timer-counter/unbind

can keep a reference to the struct device and unbinding results in
freeing the memory occupied by this device resulting in an oops.

This commit provides two new functions (plus some helpers):
 - counter_alloc() to allocate a struct counter_device that is
   automatically freed once the embedded struct device is released
 - counter_add() to register such a device.

Note that this commit doesn't fix any issues, all drivers have to be
converted to these new functions to correct the lifetime problems.

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

diff --git a/drivers/counter/counter-core.c b/drivers/counter/counter-core.c
index 00c41f28c101..b3fa15bbcbdb 100644
--- a/drivers/counter/counter-core.c
+++ b/drivers/counter/counter-core.c
@@ -15,6 +15,7 @@
 #include <linux/kdev_t.h>
 #include <linux/module.h>
 #include <linux/mutex.h>
+#include <linux/slab.h>
 #include <linux/types.h>
 #include <linux/wait.h>
 
@@ -24,6 +25,16 @@
 /* Provides a unique ID for each counter device */
 static DEFINE_IDA(counter_ida);
 
+struct counter_device_allochelper {
+	struct counter_device counter;
+
+	/*
+	 * This is cache line aligned to ensure private data behaves like if it
+	 * were kmalloced separately.
+	 */
+	unsigned long privdata[] ____cacheline_aligned;
+};
+
 static void counter_device_release(struct device *dev)
 {
 	struct counter_device *const counter =
@@ -31,6 +42,9 @@ static void counter_device_release(struct device *dev)
 
 	counter_chrdev_remove(counter);
 	ida_free(&counter_ida, dev->id);
+
+	if (!counter->legacy_device)
+		kfree(container_of(counter, struct counter_device_allochelper, counter));
 }
 
 static struct device_type counter_device_type = {
@@ -53,7 +67,14 @@ static dev_t counter_devt;
  */
 void *counter_priv(const struct counter_device *const counter)
 {
-	return counter->priv;
+	if (counter->legacy_device) {
+		return counter->priv;
+	} else {
+		struct counter_device_allochelper *ch =
+			container_of(counter, struct counter_device_allochelper, counter);
+
+		return &ch->privdata;
+	}
 }
 EXPORT_SYMBOL_GPL(counter_priv);
 
@@ -74,6 +95,8 @@ int counter_register(struct counter_device *const counter)
 	int id;
 	int err;
 
+	counter->legacy_device = true;
+
 	/* Acquire unique ID */
 	id = ida_alloc(&counter_ida, GFP_KERNEL);
 	if (id < 0)
@@ -114,6 +137,95 @@ int counter_register(struct counter_device *const counter)
 }
 EXPORT_SYMBOL_GPL(counter_register);
 
+/**
+ * counter_alloc - allocate a counter_device
+ * @sizeof_priv: size of the driver private data
+ *
+ * This is part one of counter registration. The structure is allocated
+ * dynamically to ensure the right lifetime for the embedded struct device.
+ *
+ * If this succeeds, call counter_put() to get rid of the counter_device again.
+ */
+struct counter_device *counter_alloc(size_t sizeof_priv)
+{
+	struct counter_device_allochelper *ch;
+	struct counter_device *counter;
+	struct device *dev;
+	int err;
+
+	ch = kzalloc(sizeof(*ch) + sizeof_priv, GFP_KERNEL);
+	if (!ch) {
+		err = -ENOMEM;
+		goto err_alloc_ch;
+	}
+
+	counter = &ch->counter;
+	dev = &counter->dev;
+
+	/* Acquire unique ID */
+	err = ida_alloc(&counter_ida, GFP_KERNEL);
+	if (err < 0)
+		goto err_ida_alloc;
+	dev->id = err;
+
+	mutex_init(&counter->ops_exist_lock);
+	dev->type = &counter_device_type;
+	dev->bus = &counter_bus_type;
+	dev->devt = MKDEV(MAJOR(counter_devt), dev->id);
+
+	err = counter_chrdev_add(counter);
+	if (err < 0)
+		goto err_chrdev_add;
+
+	device_initialize(dev);
+
+	return counter;
+
+err_chrdev_add:
+
+	ida_free(&counter_ida, dev->id);
+err_ida_alloc:
+
+	kfree(ch);
+err_alloc_ch:
+
+	return ERR_PTR(err);
+}
+EXPORT_SYMBOL_GPL(counter_alloc);
+
+void counter_put(struct counter_device *counter)
+{
+	put_device(&counter->dev);
+}
+EXPORT_SYMBOL_GPL(counter_put);
+
+/**
+ * counter_add - complete registration of a counter
+ * @counter: the counter to add
+ *
+ * This is part two of counter registration.
+ *
+ * If this succeeds, call counter_unregister() to get rid of the counter_device again.
+ */
+int counter_add(struct counter_device *counter)
+{
+	int err;
+	struct device *dev = &counter->dev;
+
+	if (counter->parent) {
+		dev->parent = counter->parent;
+		dev->of_node = counter->parent->of_node;
+	}
+
+	err = counter_sysfs_add(counter);
+	if (err < 0)
+		return err;
+
+	/* implies device_add(dev) */
+	return cdev_device_add(&counter->chrdev, dev);
+}
+EXPORT_SYMBOL_GPL(counter_add);
+
 /**
  * counter_unregister - unregister Counter from the system
  * @counter:	pointer to Counter to unregister
@@ -134,7 +246,8 @@ void counter_unregister(struct counter_device *const counter)
 
 	mutex_unlock(&counter->ops_exist_lock);
 
-	put_device(&counter->dev);
+	if (counter->legacy_device)
+		put_device(&counter->dev);
 }
 EXPORT_SYMBOL_GPL(counter_unregister);
 
@@ -168,6 +281,57 @@ int devm_counter_register(struct device *dev,
 }
 EXPORT_SYMBOL_GPL(devm_counter_register);
 
+static void devm_counter_put(void *counter)
+{
+	counter_put(counter);
+}
+
+/**
+ * devm_counter_alloc - allocate a counter_device
+ * @dev: the device to register the release callback for
+ * @sizeof_priv: size of the driver private data
+ *
+ * This is the device managed version of counter_add(). It registers a cleanup
+ * callback to care for calling counter_put().
+ */
+struct counter_device *devm_counter_alloc(struct device *dev, size_t sizeof_priv)
+{
+	struct counter_device *counter;
+	int err;
+
+	counter = counter_alloc(sizeof_priv);
+	if (IS_ERR(counter))
+		return counter;
+
+	err = devm_add_action_or_reset(dev, devm_counter_put, counter);
+	if (err < 0)
+		return ERR_PTR(err);
+
+	return counter;
+}
+EXPORT_SYMBOL_GPL(devm_counter_alloc);
+
+/**
+ * devm_counter_add - complete registration of a counter
+ * @dev: the device to register the release callback for
+ * @counter: the counter to add
+ *
+ * This is the device managed version of counter_add(). It registers a cleanup
+ * callback to care for calling counter_unregister().
+ */
+int devm_counter_add(struct device *dev,
+		     struct counter_device *const counter)
+{
+	int err;
+
+	err = counter_add(counter);
+	if (err < 0)
+		return err;
+
+	return devm_add_action_or_reset(dev, devm_counter_release, counter);
+}
+EXPORT_SYMBOL_GPL(devm_counter_add);
+
 #define COUNTER_DEV_MAX 256
 
 static int __init counter_init(void)
diff --git a/include/linux/counter.h b/include/linux/counter.h
index 8daaa38c71d8..f1350a43cd48 100644
--- a/include/linux/counter.h
+++ b/include/linux/counter.h
@@ -327,14 +327,29 @@ struct counter_device {
 	spinlock_t events_in_lock;
 	struct mutex events_out_lock;
 	struct mutex ops_exist_lock;
+
+	/*
+	 * This can go away once all drivers are converted to
+	 * counter_alloc()/counter_add().
+	 */
+	bool legacy_device;
 };
 
 void *counter_priv(const struct counter_device *const counter);
 
 int counter_register(struct counter_device *const counter);
+
+struct counter_device *counter_alloc(size_t sizeof_priv);
+void counter_put(struct counter_device *const counter);
+int counter_add(struct counter_device *const counter);
+
 void counter_unregister(struct counter_device *const counter);
 int devm_counter_register(struct device *dev,
 			  struct counter_device *const counter);
+struct counter_device *devm_counter_alloc(struct device *dev,
+					  size_t sizeof_priv);
+int devm_counter_add(struct device *dev,
+		     struct counter_device *const counter);
 void counter_push_event(struct counter_device *const counter, const u8 event,
 			const u8 channel);
 
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 48+ messages in thread

* [PATCH v3 14/23] counter: Update documentation for new counter registration functions
  2021-12-29 15:44 ` Uwe Kleine-König
                   ` (13 preceding siblings ...)
  (?)
@ 2021-12-29 15:44 ` Uwe Kleine-König
  -1 siblings, 0 replies; 48+ messages in thread
From: Uwe Kleine-König @ 2021-12-29 15:44 UTC (permalink / raw)
  To: William Breathitt Gray
  Cc: linux-iio, kernel, Jonathan Cameron, Greg Kroah-Hartman,
	linux-kernel, Jonathan Corbet

In order to replace the counter registration API also update the
documentation to the new way.

Reviewed-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
Acked-by: William Breathitt Gray <vilhelm.gray@gmail.com>
Signed-off-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>
---
 Documentation/driver-api/generic-counter.rst | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)

diff --git a/Documentation/driver-api/generic-counter.rst b/Documentation/driver-api/generic-counter.rst
index 1b487a331467..71ccc30e586b 100644
--- a/Documentation/driver-api/generic-counter.rst
+++ b/Documentation/driver-api/generic-counter.rst
@@ -262,11 +262,11 @@ order to communicate with the device: to read and write various Signals
 and Counts, and to set and get the "action mode" and "function mode" for
 various Synapses and Counts respectively.
 
-A defined counter_device structure may be registered to the system by
-passing it to the counter_register function, and unregistered by passing
-it to the counter_unregister function. Similarly, the
-devm_counter_register function may be used if device memory-managed
-registration is desired.
+A counter_device structure is allocated using counter_alloc() and then
+registered to the system by passing it to the counter_add() function, and
+unregistered by passing it to the counter_unregister function. There are
+device managed variants of these functions: devm_counter_alloc() and
+devm_counter_add().
 
 The struct counter_comp structure is used to define counter extensions
 for Signals, Synapses, and Counts.
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 48+ messages in thread

* [PATCH v3 15/23] counter: 104-quad-8: Convert to new counter registration
  2021-12-29 15:44 ` Uwe Kleine-König
                   ` (14 preceding siblings ...)
  (?)
@ 2021-12-29 15:44 ` Uwe Kleine-König
  -1 siblings, 0 replies; 48+ messages in thread
From: Uwe Kleine-König @ 2021-12-29 15:44 UTC (permalink / raw)
  To: William Breathitt Gray
  Cc: linux-iio, kernel, Jonathan Cameron, Greg Kroah-Hartman,
	linux-kernel, Syed Nayyar Waris

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

Fixes: f1d8a071d45b ("counter: 104-quad-8: Add Generic Counter interface support")
Reviewed-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
Acked-by: William Breathitt Gray <vilhelm.gray@gmail.com>
Signed-off-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>
---
 drivers/counter/104-quad-8.c | 35 ++++++++++++++++++++---------------
 1 file changed, 20 insertions(+), 15 deletions(-)

diff --git a/drivers/counter/104-quad-8.c b/drivers/counter/104-quad-8.c
index 6e5286cd1d4e..680c7ba943a4 100644
--- a/drivers/counter/104-quad-8.c
+++ b/drivers/counter/104-quad-8.c
@@ -52,7 +52,6 @@ MODULE_PARM_DESC(irq, "ACCES 104-QUAD-8 interrupt line numbers");
  */
 struct quad8 {
 	spinlock_t lock;
-	struct counter_device counter;
 	unsigned int fck_prescaler[QUAD8_NUM_COUNTERS];
 	unsigned int preset[QUAD8_NUM_COUNTERS];
 	unsigned int count_mode[QUAD8_NUM_COUNTERS];
@@ -1085,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;
@@ -1116,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 */
@@ -1127,6 +1127,7 @@ static irqreturn_t quad8_irq_handler(int irq, void *private)
 
 static int quad8_probe(struct device *dev, unsigned int id)
 {
+	struct counter_device *counter;
 	struct quad8 *priv;
 	int i, j;
 	unsigned int base_offset;
@@ -1138,19 +1139,19 @@ static int quad8_probe(struct device *dev, unsigned int id)
 		return -EBUSY;
 	}
 
-	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);
 
 	/* Initialize Counter device and driver data */
-	priv->counter.name = dev_name(dev);
-	priv->counter.parent = dev;
-	priv->counter.ops = &quad8_ops;
-	priv->counter.counts = quad8_counts;
-	priv->counter.num_counts = ARRAY_SIZE(quad8_counts);
-	priv->counter.signals = quad8_signals;
-	priv->counter.num_signals = ARRAY_SIZE(quad8_signals);
-	priv->counter.priv = priv;
+	counter->name = dev_name(dev);
+	counter->parent = dev;
+	counter->ops = &quad8_ops;
+	counter->counts = quad8_counts;
+	counter->num_counts = ARRAY_SIZE(quad8_counts);
+	counter->signals = quad8_signals;
+	counter->num_signals = ARRAY_SIZE(quad8_signals);
 	priv->base = base[id];
 
 	spin_lock_init(&priv->lock);
@@ -1192,11 +1193,15 @@ 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,
-			       priv->counter.name, priv);
+			       counter->name, counter);
 	if (err)
 		return err;
 
-	return devm_counter_register(dev, &priv->counter);
+	err = devm_counter_add(dev, counter);
+	if (err < 0)
+		return dev_err_probe(dev, err, "Failed to add counter\n");
+
+	return 0;
 }
 
 static struct isa_driver quad8_driver = {
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 48+ messages in thread

* [PATCH v3 16/23] counter: interrupt-cnt: Convert to new counter registration
  2021-12-29 15:44 ` Uwe Kleine-König
                   ` (15 preceding siblings ...)
  (?)
@ 2021-12-29 15:44 ` Uwe Kleine-König
  -1 siblings, 0 replies; 48+ messages in thread
From: Uwe Kleine-König @ 2021-12-29 15:44 UTC (permalink / raw)
  To: William Breathitt Gray
  Cc: linux-iio, kernel, Jonathan Cameron, Greg Kroah-Hartman,
	linux-kernel, Oleksij Rempel, Ahmad Fatoum

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

Fixes: a55ebd47f21f ("counter: add IRQ or GPIO based counter")
Reviewed-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
Acked-by: William Breathitt Gray <vilhelm.gray@gmail.com>
Signed-off-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>
---
 drivers/counter/interrupt-cnt.c | 28 ++++++++++++++++------------
 1 file changed, 16 insertions(+), 12 deletions(-)

diff --git a/drivers/counter/interrupt-cnt.c b/drivers/counter/interrupt-cnt.c
index 4bf706ef46e2..9e99702470c2 100644
--- a/drivers/counter/interrupt-cnt.c
+++ b/drivers/counter/interrupt-cnt.c
@@ -16,7 +16,6 @@
 
 struct interrupt_cnt_priv {
 	atomic_t count;
-	struct counter_device counter;
 	struct gpio_desc *gpio;
 	int irq;
 	bool enabled;
@@ -148,12 +147,14 @@ static const struct counter_ops interrupt_cnt_ops = {
 static int interrupt_cnt_probe(struct platform_device *pdev)
 {
 	struct device *dev = &pdev->dev;
+	struct counter_device *counter;
 	struct interrupt_cnt_priv *priv;
 	int ret;
 
-	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);
 
 	priv->irq = platform_get_irq_optional(pdev,  0);
 	if (priv->irq == -ENXIO)
@@ -184,8 +185,8 @@ static int interrupt_cnt_probe(struct platform_device *pdev)
 	if (!priv->signals.name)
 		return -ENOMEM;
 
-	priv->counter.signals = &priv->signals;
-	priv->counter.num_signals = 1;
+	counter->signals = &priv->signals;
+	counter->num_signals = 1;
 
 	priv->synapses.actions_list = interrupt_cnt_synapse_actions;
 	priv->synapses.num_actions = ARRAY_SIZE(interrupt_cnt_synapse_actions);
@@ -199,12 +200,11 @@ static int interrupt_cnt_probe(struct platform_device *pdev)
 	priv->cnts.ext = interrupt_cnt_ext;
 	priv->cnts.num_ext = ARRAY_SIZE(interrupt_cnt_ext);
 
-	priv->counter.priv = priv;
-	priv->counter.name = dev_name(dev);
-	priv->counter.parent = dev;
-	priv->counter.ops = &interrupt_cnt_ops;
-	priv->counter.counts = &priv->cnts;
-	priv->counter.num_counts = 1;
+	counter->name = dev_name(dev);
+	counter->parent = dev;
+	counter->ops = &interrupt_cnt_ops;
+	counter->counts = &priv->cnts;
+	counter->num_counts = 1;
 
 	irq_set_status_flags(priv->irq, IRQ_NOAUTOEN);
 	ret = devm_request_irq(dev, priv->irq, interrupt_cnt_isr,
@@ -213,7 +213,11 @@ static int interrupt_cnt_probe(struct platform_device *pdev)
 	if (ret)
 		return ret;
 
-	return devm_counter_register(dev, &priv->counter);
+	ret = devm_counter_add(dev, counter);
+	if (ret < 0)
+		return dev_err_probe(dev, ret, "Failed to add counter\n");
+
+	return 0;
 }
 
 static const struct of_device_id interrupt_cnt_of_match[] = {
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 48+ messages in thread

* [PATCH v3 17/23] counter: intel-qep: Convert to new counter registration
  2021-12-29 15:44 ` Uwe Kleine-König
                   ` (16 preceding siblings ...)
  (?)
@ 2021-12-29 15:44 ` Uwe Kleine-König
  2021-12-30 10:26   ` Jarkko Nikula
  -1 siblings, 1 reply; 48+ messages in thread
From: Uwe Kleine-König @ 2021-12-29 15:44 UTC (permalink / raw)
  To: William Breathitt Gray
  Cc: linux-iio, kernel, Jonathan Cameron, Greg Kroah-Hartman,
	linux-kernel, Jarkko Nikula

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

Fixes: b711f687a1c1 ("counter: Add support for Intel Quadrature Encoder Peripheral")
Reviewed-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
Acked-by: William Breathitt Gray <vilhelm.gray@gmail.com>
Signed-off-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>
---
 drivers/counter/intel-qep.c | 28 ++++++++++++++++------------
 1 file changed, 16 insertions(+), 12 deletions(-)

diff --git a/drivers/counter/intel-qep.c b/drivers/counter/intel-qep.c
index 8f84a48508ac..47a6a9dfc9e8 100644
--- a/drivers/counter/intel-qep.c
+++ b/drivers/counter/intel-qep.c
@@ -63,7 +63,6 @@
 #define INTEL_QEP_CLK_PERIOD_NS		10
 
 struct intel_qep {
-	struct counter_device counter;
 	struct mutex lock;
 	struct device *dev;
 	void __iomem *regs;
@@ -392,14 +391,16 @@ static struct counter_count intel_qep_counter_count[] = {
 
 static int intel_qep_probe(struct pci_dev *pci, const struct pci_device_id *id)
 {
+	struct counter_device *counter;
 	struct intel_qep *qep;
 	struct device *dev = &pci->dev;
 	void __iomem *regs;
 	int ret;
 
-	qep = devm_kzalloc(dev, sizeof(*qep), GFP_KERNEL);
-	if (!qep)
+	counter = devm_counter_alloc(dev, sizeof(*qep));
+	if (!counter)
 		return -ENOMEM;
+	qep = counter_priv(counter);
 
 	ret = pcim_enable_device(pci);
 	if (ret)
@@ -422,20 +423,23 @@ static int intel_qep_probe(struct pci_dev *pci, const struct pci_device_id *id)
 	intel_qep_init(qep);
 	pci_set_drvdata(pci, qep);
 
-	qep->counter.name = pci_name(pci);
-	qep->counter.parent = dev;
-	qep->counter.ops = &intel_qep_counter_ops;
-	qep->counter.counts = intel_qep_counter_count;
-	qep->counter.num_counts = ARRAY_SIZE(intel_qep_counter_count);
-	qep->counter.signals = intel_qep_signals;
-	qep->counter.num_signals = ARRAY_SIZE(intel_qep_signals);
-	qep->counter.priv = qep;
+	counter->name = pci_name(pci);
+	counter->parent = dev;
+	counter->ops = &intel_qep_counter_ops;
+	counter->counts = intel_qep_counter_count;
+	counter->num_counts = ARRAY_SIZE(intel_qep_counter_count);
+	counter->signals = intel_qep_signals;
+	counter->num_signals = ARRAY_SIZE(intel_qep_signals);
 	qep->enabled = false;
 
 	pm_runtime_put(dev);
 	pm_runtime_allow(dev);
 
-	return devm_counter_register(&pci->dev, &qep->counter);
+	ret = devm_counter_add(&pci->dev, counter);
+	if (ret < 0)
+		return dev_err_probe(&pci->dev, ret, "Failed to add counter\n");
+
+	return 0;
 }
 
 static void intel_qep_remove(struct pci_dev *pci)
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 48+ messages in thread

* [PATCH v3 18/23] counter: ftm-quaddec: Convert to new counter registration
  2021-12-29 15:44 ` Uwe Kleine-König
                   ` (17 preceding siblings ...)
  (?)
@ 2021-12-29 15:44 ` Uwe Kleine-König
  -1 siblings, 0 replies; 48+ messages in thread
From: Uwe Kleine-König @ 2021-12-29 15:44 UTC (permalink / raw)
  To: William Breathitt Gray
  Cc: linux-iio, kernel, Jonathan Cameron, Greg Kroah-Hartman,
	linux-kernel, Patrick Havelange

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

Fixes: a3b9a99980d9 ("counter: add FlexTimer Module Quadrature decoder counter driver")
Reviewed-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
Acked-by: William Breathitt Gray <vilhelm.gray@gmail.com>
Signed-off-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>
---
 drivers/counter/ftm-quaddec.c | 26 +++++++++++++-------------
 1 file changed, 13 insertions(+), 13 deletions(-)

diff --git a/drivers/counter/ftm-quaddec.c b/drivers/counter/ftm-quaddec.c
index f5d92df6a611..2a58582a9df4 100644
--- a/drivers/counter/ftm-quaddec.c
+++ b/drivers/counter/ftm-quaddec.c
@@ -26,7 +26,6 @@
 	})
 
 struct ftm_quaddec {
-	struct counter_device counter;
 	struct platform_device *pdev;
 	void __iomem *ftm_base;
 	bool big_endian;
@@ -259,15 +258,17 @@ static struct counter_count ftm_quaddec_counts = {
 
 static int ftm_quaddec_probe(struct platform_device *pdev)
 {
+	struct counter_device *counter;
 	struct ftm_quaddec *ftm;
 
 	struct device_node *node = pdev->dev.of_node;
 	struct resource *io;
 	int ret;
 
-	ftm = devm_kzalloc(&pdev->dev, sizeof(*ftm), GFP_KERNEL);
-	if (!ftm)
+	counter = devm_counter_alloc(&pdev->dev, sizeof(*ftm));
+	if (!counter)
 		return -ENOMEM;
+	ftm = counter_priv(counter);
 
 	io = platform_get_resource(pdev, IORESOURCE_MEM, 0);
 	if (!io) {
@@ -283,14 +284,13 @@ static int ftm_quaddec_probe(struct platform_device *pdev)
 		dev_err(&pdev->dev, "Failed to map memory region\n");
 		return -EINVAL;
 	}
-	ftm->counter.name = dev_name(&pdev->dev);
-	ftm->counter.parent = &pdev->dev;
-	ftm->counter.ops = &ftm_quaddec_cnt_ops;
-	ftm->counter.counts = &ftm_quaddec_counts;
-	ftm->counter.num_counts = 1;
-	ftm->counter.signals = ftm_quaddec_signals;
-	ftm->counter.num_signals = ARRAY_SIZE(ftm_quaddec_signals);
-	ftm->counter.priv = ftm;
+	counter->name = dev_name(&pdev->dev);
+	counter->parent = &pdev->dev;
+	counter->ops = &ftm_quaddec_cnt_ops;
+	counter->counts = &ftm_quaddec_counts;
+	counter->num_counts = 1;
+	counter->signals = ftm_quaddec_signals;
+	counter->num_signals = ARRAY_SIZE(ftm_quaddec_signals);
 
 	mutex_init(&ftm->ftm_quaddec_mutex);
 
@@ -300,9 +300,9 @@ static int ftm_quaddec_probe(struct platform_device *pdev)
 	if (ret)
 		return ret;
 
-	ret = devm_counter_register(&pdev->dev, &ftm->counter);
+	ret = devm_counter_add(&pdev->dev, counter);
 	if (ret)
-		return ret;
+		return dev_err_probe(&pdev->dev, ret, "Failed to add counter\n");
 
 	return 0;
 }
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 48+ messages in thread

* [PATCH v3 19/23] counter: microchip-tcb-capture: Convert to new counter registration
  2021-12-29 15:44 ` Uwe Kleine-König
                   ` (18 preceding siblings ...)
  (?)
@ 2021-12-29 15:44 ` Uwe Kleine-König
  -1 siblings, 0 replies; 48+ messages in thread
From: Uwe Kleine-König @ 2021-12-29 15:44 UTC (permalink / raw)
  To: William Breathitt Gray
  Cc: linux-iio, kernel, Jonathan Cameron, Greg Kroah-Hartman,
	linux-kernel, Kamel Bouhara

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

Fixes: 106b104137fd ("counter: Add microchip TCB capture counter")
Reviewed-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
Acked-by: William Breathitt Gray <vilhelm.gray@gmail.com>
Signed-off-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>
---
 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.34.1


^ permalink raw reply related	[flat|nested] 48+ messages in thread

* [PATCH v3 20/23] counter: stm32-timer-cnt: Convert to new counter registration
  2021-12-29 15:44 ` Uwe Kleine-König
@ 2021-12-29 15:44   ` Uwe Kleine-König
  -1 siblings, 0 replies; 48+ messages in thread
From: Uwe Kleine-König @ 2021-12-29 15:44 UTC (permalink / raw)
  To: William Breathitt Gray
  Cc: linux-iio, kernel, Jonathan Cameron, Greg Kroah-Hartman,
	linux-kernel, Fabrice Gasnier, Maxime Coquelin, Alexandre Torgue,
	linux-stm32, linux-arm-kernel

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

Fixes: ad29937e206f ("counter: Add STM32 Timer quadrature encoder")
Reviewed-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
Acked-by: William Breathitt Gray <vilhelm.gray@gmail.com>
Signed-off-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>
---
 drivers/counter/stm32-timer-cnt.c | 30 ++++++++++++++++++------------
 1 file changed, 18 insertions(+), 12 deletions(-)

diff --git a/drivers/counter/stm32-timer-cnt.c b/drivers/counter/stm32-timer-cnt.c
index 4b05b198a8d8..5779ae7c73cf 100644
--- a/drivers/counter/stm32-timer-cnt.c
+++ b/drivers/counter/stm32-timer-cnt.c
@@ -29,7 +29,6 @@ struct stm32_timer_regs {
 };
 
 struct stm32_timer_cnt {
-	struct counter_device counter;
 	struct regmap *regmap;
 	struct clk *clk;
 	u32 max_arr;
@@ -317,31 +316,38 @@ static int stm32_timer_cnt_probe(struct platform_device *pdev)
 	struct stm32_timers *ddata = dev_get_drvdata(pdev->dev.parent);
 	struct device *dev = &pdev->dev;
 	struct stm32_timer_cnt *priv;
+	struct counter_device *counter;
+	int ret;
 
 	if (IS_ERR_OR_NULL(ddata))
 		return -EINVAL;
 
-	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);
+
 	priv->regmap = ddata->regmap;
 	priv->clk = ddata->clk;
 	priv->max_arr = ddata->max_arr;
 
-	priv->counter.name = dev_name(dev);
-	priv->counter.parent = dev;
-	priv->counter.ops = &stm32_timer_cnt_ops;
-	priv->counter.counts = &stm32_counts;
-	priv->counter.num_counts = 1;
-	priv->counter.signals = stm32_signals;
-	priv->counter.num_signals = ARRAY_SIZE(stm32_signals);
-	priv->counter.priv = priv;
+	counter->name = dev_name(dev);
+	counter->parent = dev;
+	counter->ops = &stm32_timer_cnt_ops;
+	counter->counts = &stm32_counts;
+	counter->num_counts = 1;
+	counter->signals = stm32_signals;
+	counter->num_signals = ARRAY_SIZE(stm32_signals);
 
 	platform_set_drvdata(pdev, priv);
 
 	/* Register Counter device */
-	return devm_counter_register(dev, &priv->counter);
+	ret = devm_counter_add(dev, counter);
+	if (ret < 0)
+		dev_err_probe(dev, ret, "Failed to add counter\n");
+
+	return ret;
 }
 
 static int __maybe_unused stm32_timer_cnt_suspend(struct device *dev)
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 48+ messages in thread

* [PATCH v3 20/23] counter: stm32-timer-cnt: Convert to new counter registration
@ 2021-12-29 15:44   ` Uwe Kleine-König
  0 siblings, 0 replies; 48+ messages in thread
From: Uwe Kleine-König @ 2021-12-29 15:44 UTC (permalink / raw)
  To: William Breathitt Gray
  Cc: linux-iio, kernel, Jonathan Cameron, Greg Kroah-Hartman,
	linux-kernel, Fabrice Gasnier, Maxime Coquelin, Alexandre Torgue,
	linux-stm32, linux-arm-kernel

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

Fixes: ad29937e206f ("counter: Add STM32 Timer quadrature encoder")
Reviewed-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
Acked-by: William Breathitt Gray <vilhelm.gray@gmail.com>
Signed-off-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>
---
 drivers/counter/stm32-timer-cnt.c | 30 ++++++++++++++++++------------
 1 file changed, 18 insertions(+), 12 deletions(-)

diff --git a/drivers/counter/stm32-timer-cnt.c b/drivers/counter/stm32-timer-cnt.c
index 4b05b198a8d8..5779ae7c73cf 100644
--- a/drivers/counter/stm32-timer-cnt.c
+++ b/drivers/counter/stm32-timer-cnt.c
@@ -29,7 +29,6 @@ struct stm32_timer_regs {
 };
 
 struct stm32_timer_cnt {
-	struct counter_device counter;
 	struct regmap *regmap;
 	struct clk *clk;
 	u32 max_arr;
@@ -317,31 +316,38 @@ static int stm32_timer_cnt_probe(struct platform_device *pdev)
 	struct stm32_timers *ddata = dev_get_drvdata(pdev->dev.parent);
 	struct device *dev = &pdev->dev;
 	struct stm32_timer_cnt *priv;
+	struct counter_device *counter;
+	int ret;
 
 	if (IS_ERR_OR_NULL(ddata))
 		return -EINVAL;
 
-	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);
+
 	priv->regmap = ddata->regmap;
 	priv->clk = ddata->clk;
 	priv->max_arr = ddata->max_arr;
 
-	priv->counter.name = dev_name(dev);
-	priv->counter.parent = dev;
-	priv->counter.ops = &stm32_timer_cnt_ops;
-	priv->counter.counts = &stm32_counts;
-	priv->counter.num_counts = 1;
-	priv->counter.signals = stm32_signals;
-	priv->counter.num_signals = ARRAY_SIZE(stm32_signals);
-	priv->counter.priv = priv;
+	counter->name = dev_name(dev);
+	counter->parent = dev;
+	counter->ops = &stm32_timer_cnt_ops;
+	counter->counts = &stm32_counts;
+	counter->num_counts = 1;
+	counter->signals = stm32_signals;
+	counter->num_signals = ARRAY_SIZE(stm32_signals);
 
 	platform_set_drvdata(pdev, priv);
 
 	/* Register Counter device */
-	return devm_counter_register(dev, &priv->counter);
+	ret = devm_counter_add(dev, counter);
+	if (ret < 0)
+		dev_err_probe(dev, ret, "Failed to add counter\n");
+
+	return ret;
 }
 
 static int __maybe_unused stm32_timer_cnt_suspend(struct device *dev)
-- 
2.34.1


_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

^ permalink raw reply related	[flat|nested] 48+ messages in thread

* [PATCH v3 21/23] counter: stm32-lptimer-cnt: Convert to new counter registration
  2021-12-29 15:44 ` Uwe Kleine-König
@ 2021-12-29 15:44   ` Uwe Kleine-König
  -1 siblings, 0 replies; 48+ messages in thread
From: Uwe Kleine-König @ 2021-12-29 15:44 UTC (permalink / raw)
  To: William Breathitt Gray
  Cc: linux-iio, kernel, Jonathan Cameron, Greg Kroah-Hartman,
	linux-kernel, Fabrice Gasnier, Maxime Coquelin, Alexandre Torgue,
	linux-stm32, linux-arm-kernel

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

Fixes: 597f55e3f36c ("counter: stm32-lptimer: add counter device")
Reviewed-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
Acked-by: William Breathitt Gray <vilhelm.gray@gmail.com>
Signed-off-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>
---
 drivers/counter/stm32-lptimer-cnt.c | 33 +++++++++++++++++------------
 1 file changed, 19 insertions(+), 14 deletions(-)

diff --git a/drivers/counter/stm32-lptimer-cnt.c b/drivers/counter/stm32-lptimer-cnt.c
index 9cf00e929cc0..68031d93ce89 100644
--- a/drivers/counter/stm32-lptimer-cnt.c
+++ b/drivers/counter/stm32-lptimer-cnt.c
@@ -20,7 +20,6 @@
 #include <linux/types.h>
 
 struct stm32_lptim_cnt {
-	struct counter_device counter;
 	struct device *dev;
 	struct regmap *regmap;
 	struct clk *clk;
@@ -411,14 +410,17 @@ static struct counter_count stm32_lptim_in1_counts = {
 static int stm32_lptim_cnt_probe(struct platform_device *pdev)
 {
 	struct stm32_lptimer *ddata = dev_get_drvdata(pdev->dev.parent);
+	struct counter_device *counter;
 	struct stm32_lptim_cnt *priv;
+	int ret;
 
 	if (IS_ERR_OR_NULL(ddata))
 		return -EINVAL;
 
-	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);
 
 	priv->dev = &pdev->dev;
 	priv->regmap = ddata->regmap;
@@ -426,23 +428,26 @@ static int stm32_lptim_cnt_probe(struct platform_device *pdev)
 	priv->ceiling = STM32_LPTIM_MAX_ARR;
 
 	/* Initialize Counter device */
-	priv->counter.name = dev_name(&pdev->dev);
-	priv->counter.parent = &pdev->dev;
-	priv->counter.ops = &stm32_lptim_cnt_ops;
+	counter->name = dev_name(&pdev->dev);
+	counter->parent = &pdev->dev;
+	counter->ops = &stm32_lptim_cnt_ops;
 	if (ddata->has_encoder) {
-		priv->counter.counts = &stm32_lptim_enc_counts;
-		priv->counter.num_signals = ARRAY_SIZE(stm32_lptim_cnt_signals);
+		counter->counts = &stm32_lptim_enc_counts;
+		counter->num_signals = ARRAY_SIZE(stm32_lptim_cnt_signals);
 	} else {
-		priv->counter.counts = &stm32_lptim_in1_counts;
-		priv->counter.num_signals = 1;
+		counter->counts = &stm32_lptim_in1_counts;
+		counter->num_signals = 1;
 	}
-	priv->counter.num_counts = 1;
-	priv->counter.signals = stm32_lptim_cnt_signals;
-	priv->counter.priv = priv;
+	counter->num_counts = 1;
+	counter->signals = stm32_lptim_cnt_signals;
 
 	platform_set_drvdata(pdev, priv);
 
-	return devm_counter_register(&pdev->dev, &priv->counter);
+	ret = devm_counter_add(&pdev->dev, counter);
+	if (ret < 0)
+		return dev_err_probe(&pdev->dev, ret, "Failed to add counter\n");
+
+	return 0;
 }
 
 #ifdef CONFIG_PM_SLEEP
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 48+ messages in thread

* [PATCH v3 21/23] counter: stm32-lptimer-cnt: Convert to new counter registration
@ 2021-12-29 15:44   ` Uwe Kleine-König
  0 siblings, 0 replies; 48+ messages in thread
From: Uwe Kleine-König @ 2021-12-29 15:44 UTC (permalink / raw)
  To: William Breathitt Gray
  Cc: linux-iio, kernel, Jonathan Cameron, Greg Kroah-Hartman,
	linux-kernel, Fabrice Gasnier, Maxime Coquelin, Alexandre Torgue,
	linux-stm32, linux-arm-kernel

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

Fixes: 597f55e3f36c ("counter: stm32-lptimer: add counter device")
Reviewed-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
Acked-by: William Breathitt Gray <vilhelm.gray@gmail.com>
Signed-off-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>
---
 drivers/counter/stm32-lptimer-cnt.c | 33 +++++++++++++++++------------
 1 file changed, 19 insertions(+), 14 deletions(-)

diff --git a/drivers/counter/stm32-lptimer-cnt.c b/drivers/counter/stm32-lptimer-cnt.c
index 9cf00e929cc0..68031d93ce89 100644
--- a/drivers/counter/stm32-lptimer-cnt.c
+++ b/drivers/counter/stm32-lptimer-cnt.c
@@ -20,7 +20,6 @@
 #include <linux/types.h>
 
 struct stm32_lptim_cnt {
-	struct counter_device counter;
 	struct device *dev;
 	struct regmap *regmap;
 	struct clk *clk;
@@ -411,14 +410,17 @@ static struct counter_count stm32_lptim_in1_counts = {
 static int stm32_lptim_cnt_probe(struct platform_device *pdev)
 {
 	struct stm32_lptimer *ddata = dev_get_drvdata(pdev->dev.parent);
+	struct counter_device *counter;
 	struct stm32_lptim_cnt *priv;
+	int ret;
 
 	if (IS_ERR_OR_NULL(ddata))
 		return -EINVAL;
 
-	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);
 
 	priv->dev = &pdev->dev;
 	priv->regmap = ddata->regmap;
@@ -426,23 +428,26 @@ static int stm32_lptim_cnt_probe(struct platform_device *pdev)
 	priv->ceiling = STM32_LPTIM_MAX_ARR;
 
 	/* Initialize Counter device */
-	priv->counter.name = dev_name(&pdev->dev);
-	priv->counter.parent = &pdev->dev;
-	priv->counter.ops = &stm32_lptim_cnt_ops;
+	counter->name = dev_name(&pdev->dev);
+	counter->parent = &pdev->dev;
+	counter->ops = &stm32_lptim_cnt_ops;
 	if (ddata->has_encoder) {
-		priv->counter.counts = &stm32_lptim_enc_counts;
-		priv->counter.num_signals = ARRAY_SIZE(stm32_lptim_cnt_signals);
+		counter->counts = &stm32_lptim_enc_counts;
+		counter->num_signals = ARRAY_SIZE(stm32_lptim_cnt_signals);
 	} else {
-		priv->counter.counts = &stm32_lptim_in1_counts;
-		priv->counter.num_signals = 1;
+		counter->counts = &stm32_lptim_in1_counts;
+		counter->num_signals = 1;
 	}
-	priv->counter.num_counts = 1;
-	priv->counter.signals = stm32_lptim_cnt_signals;
-	priv->counter.priv = priv;
+	counter->num_counts = 1;
+	counter->signals = stm32_lptim_cnt_signals;
 
 	platform_set_drvdata(pdev, priv);
 
-	return devm_counter_register(&pdev->dev, &priv->counter);
+	ret = devm_counter_add(&pdev->dev, counter);
+	if (ret < 0)
+		return dev_err_probe(&pdev->dev, ret, "Failed to add counter\n");
+
+	return 0;
 }
 
 #ifdef CONFIG_PM_SLEEP
-- 
2.34.1


_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

^ permalink raw reply related	[flat|nested] 48+ messages in thread

* [PATCH v3 22/23] counter: ti-eqep: Convert to new counter registration
  2021-12-29 15:44 ` Uwe Kleine-König
                   ` (21 preceding siblings ...)
  (?)
@ 2021-12-29 15:44 ` Uwe Kleine-König
  2021-12-29 22:56   ` David Lechner
  -1 siblings, 1 reply; 48+ messages in thread
From: Uwe Kleine-König @ 2021-12-29 15:44 UTC (permalink / raw)
  To: William Breathitt Gray
  Cc: linux-iio, kernel, Jonathan Cameron, Greg Kroah-Hartman,
	linux-kernel, David Lechner

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

Fixes: f213729f6796 ("counter: new TI eQEP driver")
Reviewed-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
Acked-by: William Breathitt Gray <vilhelm.gray@gmail.com>
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(-)

diff --git a/drivers/counter/ti-eqep.c b/drivers/counter/ti-eqep.c
index 2c70b900a6cf..0489d26eb47c 100644
--- a/drivers/counter/ti-eqep.c
+++ b/drivers/counter/ti-eqep.c
@@ -373,13 +373,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))
@@ -395,16 +397,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
@@ -414,7 +415,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);
@@ -426,10 +427,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.34.1


^ permalink raw reply related	[flat|nested] 48+ messages in thread

* [PATCH v3 23/23] counter: remove old and now unused registration API
  2021-12-29 15:44 ` Uwe Kleine-König
                   ` (22 preceding siblings ...)
  (?)
@ 2021-12-29 15:44 ` Uwe Kleine-König
  -1 siblings, 0 replies; 48+ messages in thread
From: Uwe Kleine-König @ 2021-12-29 15:44 UTC (permalink / raw)
  To: William Breathitt Gray
  Cc: linux-iio, kernel, Jonathan Cameron, Greg Kroah-Hartman, linux-kernel

Usage of counter_register() yields issues in device lifetime tracking. All
drivers were converted to the new API, so the old one can go away.

Reviewed-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
Acked-by: William Breathitt Gray <vilhelm.gray@gmail.com>
Signed-off-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>
---
 drivers/counter/counter-core.c | 100 ++-------------------------------
 include/linux/counter.h        |  12 ----
 2 files changed, 4 insertions(+), 108 deletions(-)

diff --git a/drivers/counter/counter-core.c b/drivers/counter/counter-core.c
index b3fa15bbcbdb..7e0957eea094 100644
--- a/drivers/counter/counter-core.c
+++ b/drivers/counter/counter-core.c
@@ -43,8 +43,7 @@ static void counter_device_release(struct device *dev)
 	counter_chrdev_remove(counter);
 	ida_free(&counter_ida, dev->id);
 
-	if (!counter->legacy_device)
-		kfree(container_of(counter, struct counter_device_allochelper, counter));
+	kfree(container_of(counter, struct counter_device_allochelper, counter));
 }
 
 static struct device_type counter_device_type = {
@@ -67,76 +66,13 @@ static dev_t counter_devt;
  */
 void *counter_priv(const struct counter_device *const counter)
 {
-	if (counter->legacy_device) {
-		return counter->priv;
-	} else {
-		struct counter_device_allochelper *ch =
-			container_of(counter, struct counter_device_allochelper, counter);
+	struct counter_device_allochelper *ch =
+		container_of(counter, struct counter_device_allochelper, counter);
 
-		return &ch->privdata;
-	}
+	return &ch->privdata;
 }
 EXPORT_SYMBOL_GPL(counter_priv);
 
-/**
- * counter_register - register Counter to the system
- * @counter:	pointer to Counter to register
- *
- * This function registers a Counter to the system. A sysfs "counter" directory
- * will be created and populated with sysfs attributes correlating with the
- * Counter Signals, Synapses, and Counts respectively.
- *
- * RETURNS:
- * 0 on success, negative error number on failure.
- */
-int counter_register(struct counter_device *const counter)
-{
-	struct device *const dev = &counter->dev;
-	int id;
-	int err;
-
-	counter->legacy_device = true;
-
-	/* Acquire unique ID */
-	id = ida_alloc(&counter_ida, GFP_KERNEL);
-	if (id < 0)
-		return id;
-
-	mutex_init(&counter->ops_exist_lock);
-
-	/* Configure device structure for Counter */
-	dev->id = id;
-	dev->type = &counter_device_type;
-	dev->bus = &counter_bus_type;
-	dev->devt = MKDEV(MAJOR(counter_devt), id);
-	if (counter->parent) {
-		dev->parent = counter->parent;
-		dev->of_node = counter->parent->of_node;
-	}
-	device_initialize(dev);
-
-	err = counter_sysfs_add(counter);
-	if (err < 0)
-		goto err_free_id;
-
-	err = counter_chrdev_add(counter);
-	if (err < 0)
-		goto err_free_id;
-
-	err = cdev_device_add(&counter->chrdev, dev);
-	if (err < 0)
-		goto err_remove_chrdev;
-
-	return 0;
-
-err_remove_chrdev:
-	counter_chrdev_remove(counter);
-err_free_id:
-	put_device(dev);
-	return err;
-}
-EXPORT_SYMBOL_GPL(counter_register);
-
 /**
  * counter_alloc - allocate a counter_device
  * @sizeof_priv: size of the driver private data
@@ -245,9 +181,6 @@ void counter_unregister(struct counter_device *const counter)
 	wake_up(&counter->events_wait);
 
 	mutex_unlock(&counter->ops_exist_lock);
-
-	if (counter->legacy_device)
-		put_device(&counter->dev);
 }
 EXPORT_SYMBOL_GPL(counter_unregister);
 
@@ -256,31 +189,6 @@ static void devm_counter_release(void *counter)
 	counter_unregister(counter);
 }
 
-/**
- * devm_counter_register - Resource-managed counter_register
- * @dev:	device to allocate counter_device for
- * @counter:	pointer to Counter to register
- *
- * Managed counter_register. The Counter registered with this function is
- * automatically unregistered on driver detach. This function calls
- * counter_register internally. Refer to that function for more information.
- *
- * RETURNS:
- * 0 on success, negative error number on failure.
- */
-int devm_counter_register(struct device *dev,
-			  struct counter_device *const counter)
-{
-	int err;
-
-	err = counter_register(counter);
-	if (err < 0)
-		return err;
-
-	return devm_add_action_or_reset(dev, devm_counter_release, counter);
-}
-EXPORT_SYMBOL_GPL(devm_counter_register);
-
 static void devm_counter_put(void *counter)
 {
 	counter_put(counter);
diff --git a/include/linux/counter.h b/include/linux/counter.h
index f1350a43cd48..9afa0f1e9cea 100644
--- a/include/linux/counter.h
+++ b/include/linux/counter.h
@@ -314,8 +314,6 @@ struct counter_device {
 	struct counter_comp *ext;
 	size_t num_ext;
 
-	void *priv;
-
 	struct device dev;
 	struct cdev chrdev;
 	struct list_head events_list;
@@ -327,25 +325,15 @@ struct counter_device {
 	spinlock_t events_in_lock;
 	struct mutex events_out_lock;
 	struct mutex ops_exist_lock;
-
-	/*
-	 * This can go away once all drivers are converted to
-	 * counter_alloc()/counter_add().
-	 */
-	bool legacy_device;
 };
 
 void *counter_priv(const struct counter_device *const counter);
 
-int counter_register(struct counter_device *const counter);
-
 struct counter_device *counter_alloc(size_t sizeof_priv);
 void counter_put(struct counter_device *const counter);
 int counter_add(struct counter_device *const counter);
 
 void counter_unregister(struct counter_device *const counter);
-int devm_counter_register(struct device *dev,
-			  struct counter_device *const counter);
 struct counter_device *devm_counter_alloc(struct device *dev,
 					  size_t sizeof_priv);
 int devm_counter_add(struct device *dev,
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 48+ messages in thread

* Re: [PATCH v3 13/23] counter: Provide alternative counter registration functions
  2021-12-29 15:44 ` [PATCH v3 13/23] counter: Provide alternative counter registration functions Uwe Kleine-König
@ 2021-12-29 17:06   ` Jonathan Cameron
  2021-12-30  8:38     ` Uwe Kleine-König
  0 siblings, 1 reply; 48+ messages in thread
From: Jonathan Cameron @ 2021-12-29 17:06 UTC (permalink / raw)
  To: Uwe Kleine-König
  Cc: William Breathitt Gray, linux-iio, kernel, Jonathan Cameron,
	Greg Kroah-Hartman, linux-kernel

On Wed, 29 Dec 2021 16:44:31 +0100
Uwe Kleine-König         <u.kleine-koenig@pengutronix.de> wrote:

> The current implementation gets device lifetime tracking wrong. The
> problem is that allocation of struct counter_device is controlled by the
> individual drivers but this structure contains a struct device that
> might have to live longer than a driver is bound. As a result a command
> sequence like:
> 
> 	{ sleep 5; echo bang; } > /dev/counter0 &
> 	sleep 1;
> 	echo 40000000.timer:counter > /sys/bus/platform/drivers/stm32-timer-counter/unbind
> 
> can keep a reference to the struct device and unbinding results in
> freeing the memory occupied by this device resulting in an oops.
> 
> This commit provides two new functions (plus some helpers):
>  - counter_alloc() to allocate a struct counter_device that is
>    automatically freed once the embedded struct device is released
>  - counter_add() to register such a device.
> 
> Note that this commit doesn't fix any issues, all drivers have to be
> converted to these new functions to correct the lifetime problems.
> 
> Signed-off-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>
Basically fine - a few trivial comments inline that I'm not that fussed
about whether you take notice of or not. As such

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

> ---

I'd have liked to have seen a change log here. Quite a few comments on this
one and not all had 'obvious' resolutions.

>  drivers/counter/counter-core.c | 168 ++++++++++++++++++++++++++++++++-
>  include/linux/counter.h        |  15 +++
>  2 files changed, 181 insertions(+), 2 deletions(-)
> 
> diff --git a/drivers/counter/counter-core.c b/drivers/counter/counter-core.c
> index 00c41f28c101..b3fa15bbcbdb 100644
> --- a/drivers/counter/counter-core.c
> +++ b/drivers/counter/counter-core.c
> @@ -15,6 +15,7 @@
>  #include <linux/kdev_t.h>
>  #include <linux/module.h>
>  #include <linux/mutex.h>
> +#include <linux/slab.h>
>  #include <linux/types.h>
>  #include <linux/wait.h>
>  
> @@ -24,6 +25,16 @@
>  /* Provides a unique ID for each counter device */
>  static DEFINE_IDA(counter_ida);
>  
> +struct counter_device_allochelper {
> +	struct counter_device counter;
> +
> +	/*
> +	 * This is cache line aligned to ensure private data behaves like if it
> +	 * were kmalloced separately.
> +	 */
> +	unsigned long privdata[] ____cacheline_aligned;

Change log for the patch would have made it easier to see you decided
to make this change after the discussion in v2.

> +};
> +

...

>  
> +/**
> + * counter_alloc - allocate a counter_device
> + * @sizeof_priv: size of the driver private data
> + *
> + * This is part one of counter registration. The structure is allocated
> + * dynamically to ensure the right lifetime for the embedded struct device.
> + *
> + * If this succeeds, call counter_put() to get rid of the counter_device again.
> + */
> +struct counter_device *counter_alloc(size_t sizeof_priv)
> +{
> +	struct counter_device_allochelper *ch;
> +	struct counter_device *counter;
> +	struct device *dev;
> +	int err;
> +
> +	ch = kzalloc(sizeof(*ch) + sizeof_priv, GFP_KERNEL);
> +	if (!ch) {
> +		err = -ENOMEM;
> +		goto err_alloc_ch;

Slight preference for a direct return here even though it means
replicating the ERR_PTR() statement.  Makes for one less error
path where a reviewer has to go see what is being done.

> +	}
> +
> +	counter = &ch->counter;
> +	dev = &counter->dev;
> +
> +	/* Acquire unique ID */
> +	err = ida_alloc(&counter_ida, GFP_KERNEL);
> +	if (err < 0)
> +		goto err_ida_alloc;
> +	dev->id = err;
> +
> +	mutex_init(&counter->ops_exist_lock);
> +	dev->type = &counter_device_type;
> +	dev->bus = &counter_bus_type;
> +	dev->devt = MKDEV(MAJOR(counter_devt), dev->id);
> +
> +	err = counter_chrdev_add(counter);
> +	if (err < 0)
> +		goto err_chrdev_add;
> +
> +	device_initialize(dev);
> +
> +	return counter;
> +
> +err_chrdev_add:
> +
Nitpick: Unusual spacing (to my eye anyway). I wouldn't expect to see a blank line after a label
as the label indentation makes a visual separation anyway.

> +	ida_free(&counter_ida, dev->id);
> +err_ida_alloc:
> +
> +	kfree(ch);
> +err_alloc_ch:
> +
> +	return ERR_PTR(err);
> +}
> +EXPORT_SYMBOL_GPL(counter_alloc);


^ permalink raw reply	[flat|nested] 48+ messages in thread

* Re: [PATCH v3 10/23] counter: ti-eqep: Convert to counter_priv() wrapper
  2021-12-29 15:44 ` [PATCH v3 10/23] counter: ti-eqep: " Uwe Kleine-König
@ 2021-12-29 22:42   ` William Breathitt Gray
  2021-12-29 22:56   ` David Lechner
  1 sibling, 0 replies; 48+ messages in thread
From: William Breathitt Gray @ 2021-12-29 22:42 UTC (permalink / raw)
  To: Uwe Kleine-König
  Cc: linux-iio, kernel, Jonathan Cameron, Greg Kroah-Hartman,
	linux-kernel, David Lechner

[-- Attachment #1: Type: text/plain, Size: 901 bytes --]

On Wed, Dec 29, 2021 at 04:44:28PM +0100, Uwe Kleine-König wrote:
> This is a straight forward conversion to the new counter_priv() wrapper.
> 
> 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 | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/drivers/counter/ti-eqep.c b/drivers/counter/ti-eqep.c
> index 9e0e46bca4c2..2c70b900a6cf 100644
> --- a/drivers/counter/ti-eqep.c
> +++ b/drivers/counter/ti-eqep.c
> @@ -89,7 +89,7 @@ struct ti_eqep_cnt {
>  
>  static struct ti_eqep_cnt *ti_eqep_count_from_counter(struct counter_device *counter)
>  {
> -	return container_of(counter, struct ti_eqep_cnt, counter);
> +	return counter_priv(counter);
>  }
>  
>  static int ti_eqep_count_read(struct counter_device *counter,
> -- 
> 2.34.1
> 

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

^ permalink raw reply	[flat|nested] 48+ messages in thread

* Re: [PATCH v3 22/23] counter: ti-eqep: Convert to new counter registration
  2021-12-29 15:44 ` [PATCH v3 22/23] counter: ti-eqep: " Uwe Kleine-König
@ 2021-12-29 22:56   ` David Lechner
  0 siblings, 0 replies; 48+ messages in thread
From: David Lechner @ 2021-12-29 22:56 UTC (permalink / raw)
  To: Uwe Kleine-König, William Breathitt Gray
  Cc: linux-iio, kernel, Jonathan Cameron, Greg Kroah-Hartman, linux-kernel

On 12/29/21 9:44 AM, 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")
> Reviewed-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
> Acked-by: William Breathitt Gray <vilhelm.gray@gmail.com>
> Signed-off-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>
> ---

Acked-by: David Lechner <david@lechnology.com>


^ permalink raw reply	[flat|nested] 48+ messages in thread

* Re: [PATCH v3 10/23] counter: ti-eqep: Convert to counter_priv() wrapper
  2021-12-29 15:44 ` [PATCH v3 10/23] counter: ti-eqep: " Uwe Kleine-König
  2021-12-29 22:42   ` William Breathitt Gray
@ 2021-12-29 22:56   ` David Lechner
  1 sibling, 0 replies; 48+ messages in thread
From: David Lechner @ 2021-12-29 22:56 UTC (permalink / raw)
  To: Uwe Kleine-König, William Breathitt Gray
  Cc: linux-iio, kernel, Jonathan Cameron, Greg Kroah-Hartman, linux-kernel

On 12/29/21 9:44 AM, Uwe Kleine-König wrote:
> This is a straight forward conversion to the new counter_priv() wrapper.
> 
> Signed-off-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>
> ---
Acked-by: David Lechner <david@lechnology.com>

^ permalink raw reply	[flat|nested] 48+ messages in thread

* Re: [PATCH v3 13/23] counter: Provide alternative counter registration functions
  2021-12-29 17:06   ` Jonathan Cameron
@ 2021-12-30  8:38     ` Uwe Kleine-König
  0 siblings, 0 replies; 48+ messages in thread
From: Uwe Kleine-König @ 2021-12-30  8:38 UTC (permalink / raw)
  To: Jonathan Cameron
  Cc: linux-iio, Greg Kroah-Hartman, linux-kernel,
	William Breathitt Gray, kernel, Jonathan Cameron

[-- Attachment #1: Type: text/plain, Size: 5949 bytes --]

Hello Jonathan,

On Wed, Dec 29, 2021 at 05:06:12PM +0000, Jonathan Cameron wrote:
> On Wed, 29 Dec 2021 16:44:31 +0100
> Uwe Kleine-König         <u.kleine-koenig@pengutronix.de> wrote:
> 
> > The current implementation gets device lifetime tracking wrong. The
> > problem is that allocation of struct counter_device is controlled by the
> > individual drivers but this structure contains a struct device that
> > might have to live longer than a driver is bound. As a result a command
> > sequence like:
> > 
> > 	{ sleep 5; echo bang; } > /dev/counter0 &
> > 	sleep 1;
> > 	echo 40000000.timer:counter > /sys/bus/platform/drivers/stm32-timer-counter/unbind
> > 
> > can keep a reference to the struct device and unbinding results in
> > freeing the memory occupied by this device resulting in an oops.
> > 
> > This commit provides two new functions (plus some helpers):
> >  - counter_alloc() to allocate a struct counter_device that is
> >    automatically freed once the embedded struct device is released
> >  - counter_add() to register such a device.
> > 
> > Note that this commit doesn't fix any issues, all drivers have to be
> > converted to these new functions to correct the lifetime problems.
> > 
> > Signed-off-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>
> Basically fine - a few trivial comments inline that I'm not that fussed
> about whether you take notice of or not. As such
> 
> Reviewed-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>

Thanks.

> I'd have liked to have seen a change log here. Quite a few comments on this
> one and not all had 'obvious' resolutions.

yeah, should have been a bit less lazy and comment a bit more than the
range diff in the cover letter.

To catch up:

 - privdata is now cache line aligned
 - consistent bracing for oneline if blocks
 - fixed the warning by the 0day bot which explained that only one
   device can be bound
 - reordered the body of counter_add() to better match
   counter_register()
 - Add an EXPORT_SYMBOL_GPL(counter_put);
 - Drop one pair of get_device/put_device
 - kernel doc for devm_counter_a{lloc,dd}

> >  drivers/counter/counter-core.c | 168 ++++++++++++++++++++++++++++++++-
> >  include/linux/counter.h        |  15 +++
> >  2 files changed, 181 insertions(+), 2 deletions(-)
> > 
> > diff --git a/drivers/counter/counter-core.c b/drivers/counter/counter-core.c
> > index 00c41f28c101..b3fa15bbcbdb 100644
> > --- a/drivers/counter/counter-core.c
> > +++ b/drivers/counter/counter-core.c
> > @@ -15,6 +15,7 @@
> >  #include <linux/kdev_t.h>
> >  #include <linux/module.h>
> >  #include <linux/mutex.h>
> > +#include <linux/slab.h>
> >  #include <linux/types.h>
> >  #include <linux/wait.h>
> >  
> > @@ -24,6 +25,16 @@
> >  /* Provides a unique ID for each counter device */
> >  static DEFINE_IDA(counter_ida);
> >  
> > +struct counter_device_allochelper {
> > +	struct counter_device counter;
> > +
> > +	/*
> > +	 * This is cache line aligned to ensure private data behaves like if it
> > +	 * were kmalloced separately.
> > +	 */
> > +	unsigned long privdata[] ____cacheline_aligned;
> 
> Change log for the patch would have made it easier to see you decided
> to make this change after the discussion in v2.

Yeah, this was a wim of the moment after I saw that this usually only
results in a 32 byte alignment.

> > +};
> > +
> 
> ...
> 
> >  
> > +/**
> > + * counter_alloc - allocate a counter_device
> > + * @sizeof_priv: size of the driver private data
> > + *
> > + * This is part one of counter registration. The structure is allocated
> > + * dynamically to ensure the right lifetime for the embedded struct device.
> > + *
> > + * If this succeeds, call counter_put() to get rid of the counter_device again.
> > + */
> > +struct counter_device *counter_alloc(size_t sizeof_priv)
> > +{
> > +	struct counter_device_allochelper *ch;
> > +	struct counter_device *counter;
> > +	struct device *dev;
> > +	int err;
> > +
> > +	ch = kzalloc(sizeof(*ch) + sizeof_priv, GFP_KERNEL);
> > +	if (!ch) {
> > +		err = -ENOMEM;
> > +		goto err_alloc_ch;
> 
> Slight preference for a direct return here even though it means
> replicating the ERR_PTR() statement.  Makes for one less error
> path where a reviewer has to go see what is being done.

I'll consider it if it comes to a v4.

> > +	}
> > +
> > +	counter = &ch->counter;
> > +	dev = &counter->dev;
> > +
> > +	/* Acquire unique ID */
> > +	err = ida_alloc(&counter_ida, GFP_KERNEL);
> > +	if (err < 0)
> > +		goto err_ida_alloc;
> > +	dev->id = err;
> > +
> > +	mutex_init(&counter->ops_exist_lock);
> > +	dev->type = &counter_device_type;
> > +	dev->bus = &counter_bus_type;
> > +	dev->devt = MKDEV(MAJOR(counter_devt), dev->id);
> > +
> > +	err = counter_chrdev_add(counter);
> > +	if (err < 0)
> > +		goto err_chrdev_add;
> > +
> > +	device_initialize(dev);
> > +
> > +	return counter;
> > +
> > +err_chrdev_add:
>
> Nitpick: Unusual spacing (to my eye anyway). I wouldn't expect to see a blank line after a label
> as the label indentation makes a visual separation anyway.

I know this is unusual, but I like this approach. The error label is
named after what failed (instead of the more usual what has to be undone
first) and then it's grouped to the matching undo-function.

See
https://lore.kernel.org/linux-pwm/20201106093435.4mlr6ujivvkzkd5z@pengutronix.de
for a more verbose reasoning (which however failed to convince my fellow
pwm maintainers :-\).

> > +   ida_free(&counter_ida, dev->id);
> > +err_ida_alloc:
> > +
> > +   kfree(ch);
> > +err_alloc_ch:
> > +
> > +   return ERR_PTR(err);
> > +}

Best regards and thanks for your feedback,
Uwe

-- 
Pengutronix e.K.                           | Uwe Kleine-König            |
Industrial Linux Solutions                 | https://www.pengutronix.de/ |

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 488 bytes --]

^ permalink raw reply	[flat|nested] 48+ messages in thread

* Re: [PATCH v3 00/23] counter: cleanups and device lifetime fixes
  2021-12-29 15:44 ` Uwe Kleine-König
@ 2021-12-30  8:53   ` Uwe Kleine-König
  -1 siblings, 0 replies; 48+ messages in thread
From: Uwe Kleine-König @ 2021-12-30  8:53 UTC (permalink / raw)
  To: William Breathitt Gray, Greg Kroah-Hartman, Jonathan Cameron
  Cc: Kamel Bouhara, Ahmad Fatoum, David Lechner, Jonathan Corbet,
	linux-iio, linux-kernel, Oleksij Rempel, Alexandre Torgue,
	Fabrice Gasnier, Jarkko Nikula, linux-arm-kernel, kernel,
	Patrick Havelange, Syed Nayyar Waris, linux-stm32,
	Maxime Coquelin

[-- Attachment #1: Type: text/plain, Size: 3391 bytes --]

Hello,

On Wed, Dec 29, 2021 at 04:44:18PM +0100, Uwe Kleine-König wrote:
> this is v3 of my series to fix device lifetime issues in the counter
> framework. This hopefully addresses all things pointed out for v2.
> 
> Note this depends on 60f07e74f86b (which is in next) now. Full diffstat
> below.
> 
> Things that could be further improved:
> 
> [...]
> 
> 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                 |  93 +++++-----
>  drivers/counter/counter-core.c               | 186 ++++++++++++++-----
>  drivers/counter/ftm-quaddec.c                |  36 ++--
>  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                    |  31 ++--
>  include/linux/counter.h                      |  15 +-
>  11 files changed, 356 insertions(+), 242 deletions(-)
> 
> Range-diff against v2:
> [...]
> 
> base-commit: a7904a538933c525096ca2ccde1e60d0ee62c08e
> prerequisite-patch-id: 9459ad8bc78190558df9123f8bebe28ca1c396ea

All patches have a blessing by at least one of William and Jonathan.
The prerequisite commit (60f07e74f86b) is in Greg's char-misc-next branch.

Assuming noone still finds a problem in this series that requires me to
respin I wonder who will pick it up? Greg?

Given that it fixes a possible use-after-free in all counter drivers,
I'd like to see it hit before v5.17-rc1. For 5.16 it's probably too
late.

Best regards
Uwe

-- 
Pengutronix e.K.                           | Uwe Kleine-König            |
Industrial Linux Solutions                 | https://www.pengutronix.de/ |

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 488 bytes --]

^ permalink raw reply	[flat|nested] 48+ messages in thread

* Re: [PATCH v3 00/23] counter: cleanups and device lifetime fixes
@ 2021-12-30  8:53   ` Uwe Kleine-König
  0 siblings, 0 replies; 48+ messages in thread
From: Uwe Kleine-König @ 2021-12-30  8:53 UTC (permalink / raw)
  To: William Breathitt Gray, Greg Kroah-Hartman, Jonathan Cameron
  Cc: Kamel Bouhara, Ahmad Fatoum, David Lechner, Jonathan Corbet,
	linux-iio, linux-kernel, Oleksij Rempel, Alexandre Torgue,
	Fabrice Gasnier, Jarkko Nikula, linux-arm-kernel, kernel,
	Patrick Havelange, Syed Nayyar Waris, linux-stm32,
	Maxime Coquelin


[-- Attachment #1.1: Type: text/plain, Size: 3391 bytes --]

Hello,

On Wed, Dec 29, 2021 at 04:44:18PM +0100, Uwe Kleine-König wrote:
> this is v3 of my series to fix device lifetime issues in the counter
> framework. This hopefully addresses all things pointed out for v2.
> 
> Note this depends on 60f07e74f86b (which is in next) now. Full diffstat
> below.
> 
> Things that could be further improved:
> 
> [...]
> 
> 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                 |  93 +++++-----
>  drivers/counter/counter-core.c               | 186 ++++++++++++++-----
>  drivers/counter/ftm-quaddec.c                |  36 ++--
>  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                    |  31 ++--
>  include/linux/counter.h                      |  15 +-
>  11 files changed, 356 insertions(+), 242 deletions(-)
> 
> Range-diff against v2:
> [...]
> 
> base-commit: a7904a538933c525096ca2ccde1e60d0ee62c08e
> prerequisite-patch-id: 9459ad8bc78190558df9123f8bebe28ca1c396ea

All patches have a blessing by at least one of William and Jonathan.
The prerequisite commit (60f07e74f86b) is in Greg's char-misc-next branch.

Assuming noone still finds a problem in this series that requires me to
respin I wonder who will pick it up? Greg?

Given that it fixes a possible use-after-free in all counter drivers,
I'd like to see it hit before v5.17-rc1. For 5.16 it's probably too
late.

Best regards
Uwe

-- 
Pengutronix e.K.                           | Uwe Kleine-König            |
Industrial Linux Solutions                 | https://www.pengutronix.de/ |

[-- Attachment #1.2: signature.asc --]
[-- Type: application/pgp-signature, Size: 488 bytes --]

[-- Attachment #2: Type: text/plain, Size: 176 bytes --]

_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

^ permalink raw reply	[flat|nested] 48+ messages in thread

* Re: [PATCH v3 08/23] counter: intel-qep: Convert to counter_priv() wrapper
  2021-12-29 15:44 ` [PATCH v3 08/23] counter: intel-qep: " Uwe Kleine-König
@ 2021-12-30 10:26   ` Jarkko Nikula
  0 siblings, 0 replies; 48+ messages in thread
From: Jarkko Nikula @ 2021-12-30 10:26 UTC (permalink / raw)
  To: Uwe Kleine-König, William Breathitt Gray
  Cc: linux-iio, kernel, Jonathan Cameron, Greg Kroah-Hartman, linux-kernel

On 12/29/21 17:44, Uwe Kleine-König wrote:
> This is a straight forward conversion to the new counter_priv() wrapper.
> 
> Acked-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
> Acked-by: William Breathitt Gray <vilhelm.gray@gmail.com>
> Signed-off-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>
> ---
>   drivers/counter/intel-qep.c | 18 +++++++++---------
>   1 file changed, 9 insertions(+), 9 deletions(-)
> 
Acked-by: Jarkko Nikula <jarkko.nikula@linux.intel.com>

^ permalink raw reply	[flat|nested] 48+ messages in thread

* Re: [PATCH v3 17/23] counter: intel-qep: Convert to new counter registration
  2021-12-29 15:44 ` [PATCH v3 17/23] counter: intel-qep: " Uwe Kleine-König
@ 2021-12-30 10:26   ` Jarkko Nikula
  0 siblings, 0 replies; 48+ messages in thread
From: Jarkko Nikula @ 2021-12-30 10:26 UTC (permalink / raw)
  To: Uwe Kleine-König, William Breathitt Gray
  Cc: linux-iio, kernel, Jonathan Cameron, Greg Kroah-Hartman, linux-kernel

On 12/29/21 17:44, Uwe Kleine-König wrote:
> This fixes device lifetime issues where it was possible to free a live
> struct device.
> 
> Fixes: b711f687a1c1 ("counter: Add support for Intel Quadrature Encoder Peripheral")
> Reviewed-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
> Acked-by: William Breathitt Gray <vilhelm.gray@gmail.com>
> Signed-off-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>
> ---
>   drivers/counter/intel-qep.c | 28 ++++++++++++++++------------
>   1 file changed, 16 insertions(+), 12 deletions(-)
> 
Acked-by: Jarkko Nikula <jarkko.nikula@linux.intel.com>
Tested-by: Jarkko Nikula <jarkko.nikula@linux.intel.com>

Following test will cause an oops usually after a few iterations before 
this patch:

while :; do { sleep 5; echo bang; } > /dev/counter0 & sleep 1; echo 
0000:00:18.4 >/sys/bus/pci/drivers/intel-qep/unbind; sleep 8; echo 
0000:00:18.4 >/sys/bus/pci/drivers/intel-qep/bind; done

^ permalink raw reply	[flat|nested] 48+ messages in thread

* Re: [PATCH v3 00/23] counter: cleanups and device lifetime fixes
  2021-12-30  8:53   ` Uwe Kleine-König
@ 2021-12-30 12:31     ` Greg Kroah-Hartman
  -1 siblings, 0 replies; 48+ messages in thread
From: Greg Kroah-Hartman @ 2021-12-30 12:31 UTC (permalink / raw)
  To: Uwe Kleine-König
  Cc: William Breathitt Gray, Jonathan Cameron, Kamel Bouhara,
	Ahmad Fatoum, David Lechner, Jonathan Corbet, linux-iio,
	linux-kernel, Oleksij Rempel, Alexandre Torgue, Fabrice Gasnier,
	Jarkko Nikula, linux-arm-kernel, kernel, Patrick Havelange,
	Syed Nayyar Waris, linux-stm32, Maxime Coquelin

On Thu, Dec 30, 2021 at 09:53:51AM +0100, Uwe Kleine-König wrote:
> Hello,
> 
> On Wed, Dec 29, 2021 at 04:44:18PM +0100, Uwe Kleine-König wrote:
> > this is v3 of my series to fix device lifetime issues in the counter
> > framework. This hopefully addresses all things pointed out for v2.
> > 
> > Note this depends on 60f07e74f86b (which is in next) now. Full diffstat
> > below.
> > 
> > Things that could be further improved:
> > 
> > [...]
> > 
> > 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                 |  93 +++++-----
> >  drivers/counter/counter-core.c               | 186 ++++++++++++++-----
> >  drivers/counter/ftm-quaddec.c                |  36 ++--
> >  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                    |  31 ++--
> >  include/linux/counter.h                      |  15 +-
> >  11 files changed, 356 insertions(+), 242 deletions(-)
> > 
> > Range-diff against v2:
> > [...]
> > 
> > base-commit: a7904a538933c525096ca2ccde1e60d0ee62c08e
> > prerequisite-patch-id: 9459ad8bc78190558df9123f8bebe28ca1c396ea
> 
> All patches have a blessing by at least one of William and Jonathan.
> The prerequisite commit (60f07e74f86b) is in Greg's char-misc-next branch.
> 
> Assuming noone still finds a problem in this series that requires me to
> respin I wonder who will pick it up? Greg?
> 
> Given that it fixes a possible use-after-free in all counter drivers,
> I'd like to see it hit before v5.17-rc1. For 5.16 it's probably too
> late.

Of course it is too later for 5.16, sorry.

I'll queue this up to my tree now, for 5.17-rc1, thanks.

greg k-h

^ permalink raw reply	[flat|nested] 48+ messages in thread

* Re: [PATCH v3 00/23] counter: cleanups and device lifetime fixes
@ 2021-12-30 12:31     ` Greg Kroah-Hartman
  0 siblings, 0 replies; 48+ messages in thread
From: Greg Kroah-Hartman @ 2021-12-30 12:31 UTC (permalink / raw)
  To: Uwe Kleine-König
  Cc: William Breathitt Gray, Jonathan Cameron, Kamel Bouhara,
	Ahmad Fatoum, David Lechner, Jonathan Corbet, linux-iio,
	linux-kernel, Oleksij Rempel, Alexandre Torgue, Fabrice Gasnier,
	Jarkko Nikula, linux-arm-kernel, kernel, Patrick Havelange,
	Syed Nayyar Waris, linux-stm32, Maxime Coquelin

On Thu, Dec 30, 2021 at 09:53:51AM +0100, Uwe Kleine-König wrote:
> Hello,
> 
> On Wed, Dec 29, 2021 at 04:44:18PM +0100, Uwe Kleine-König wrote:
> > this is v3 of my series to fix device lifetime issues in the counter
> > framework. This hopefully addresses all things pointed out for v2.
> > 
> > Note this depends on 60f07e74f86b (which is in next) now. Full diffstat
> > below.
> > 
> > Things that could be further improved:
> > 
> > [...]
> > 
> > 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                 |  93 +++++-----
> >  drivers/counter/counter-core.c               | 186 ++++++++++++++-----
> >  drivers/counter/ftm-quaddec.c                |  36 ++--
> >  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                    |  31 ++--
> >  include/linux/counter.h                      |  15 +-
> >  11 files changed, 356 insertions(+), 242 deletions(-)
> > 
> > Range-diff against v2:
> > [...]
> > 
> > base-commit: a7904a538933c525096ca2ccde1e60d0ee62c08e
> > prerequisite-patch-id: 9459ad8bc78190558df9123f8bebe28ca1c396ea
> 
> All patches have a blessing by at least one of William and Jonathan.
> The prerequisite commit (60f07e74f86b) is in Greg's char-misc-next branch.
> 
> Assuming noone still finds a problem in this series that requires me to
> respin I wonder who will pick it up? Greg?
> 
> Given that it fixes a possible use-after-free in all counter drivers,
> I'd like to see it hit before v5.17-rc1. For 5.16 it's probably too
> late.

Of course it is too later for 5.16, sorry.

I'll queue this up to my tree now, for 5.17-rc1, thanks.

greg k-h

_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

^ permalink raw reply	[flat|nested] 48+ messages in thread

* Re: [PATCH v3 05/23] counter: 104-quad-8: Convert to counter_priv() wrapper
  2021-12-29 15:44 ` [PATCH v3 05/23] counter: 104-quad-8: Convert to counter_priv() wrapper Uwe Kleine-König
@ 2021-12-30 13:19   ` Greg Kroah-Hartman
  0 siblings, 0 replies; 48+ messages in thread
From: Greg Kroah-Hartman @ 2021-12-30 13:19 UTC (permalink / raw)
  To: Uwe Kleine-König
  Cc: William Breathitt Gray, linux-iio, kernel, Jonathan Cameron,
	linux-kernel, Syed Nayyar Waris

On Wed, Dec 29, 2021 at 04:44:23PM +0100, Uwe Kleine-König wrote:
> This is a straight forward conversion to the new counter_priv() wrapper.
> 
> Acked-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
> Acked-by: William Breathitt Gray <vilhelm.gray@gmail.com>
> Signed-off-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>
> ---
>  drivers/counter/104-quad-8.c | 58 ++++++++++++++++++------------------
>  1 file changed, 29 insertions(+), 29 deletions(-)

This patch fails to apply to my tree:

checking file drivers/counter/104-quad-8.c
Hunk #1 succeeded at 112 (offset -1 lines).
Hunk #2 succeeded at 130 (offset -1 lines).
Hunk #3 succeeded at 162 (offset -1 lines).
Hunk #4 succeeded at 212 (offset -1 lines).
Hunk #5 succeeded at 242 (offset -1 lines).
Hunk #6 succeeded at 304 (offset -1 lines).
Hunk #7 succeeded at 334 (offset -1 lines).
Hunk #8 succeeded at 397 with fuzz 1 (offset -2 lines).
Hunk #9 FAILED at 442.
Hunk #10 succeeded at 495 (offset -2 lines).
Hunk #11 succeeded at 507 (offset -2 lines).
Hunk #12 succeeded at 536 (offset -2 lines).
Hunk #13 succeeded at 548 (offset -2 lines).
Hunk #14 succeeded at 587 (offset -2 lines).
Hunk #15 succeeded at 612 (offset -2 lines).
Hunk #16 succeeded at 659 (offset -2 lines).
Hunk #17 succeeded at 669 (offset -2 lines).
Hunk #18 succeeded at 697 (offset -2 lines).
Hunk #19 succeeded at 708 (offset -2 lines).
Hunk #20 succeeded at 734 (offset -2 lines).
Hunk #21 succeeded at 753 (offset -2 lines).
Hunk #22 succeeded at 778 (offset -2 lines).
Hunk #23 succeeded at 805 (offset -2 lines).
Hunk #24 succeeded at 816 (offset -2 lines).
Hunk #25 succeeded at 843 (offset -2 lines).
Hunk #26 succeeded at 873 (offset -2 lines).
Hunk #27 succeeded at 885 (offset -2 lines).
Hunk #28 succeeded at 911 (offset -2 lines).
Hunk #29 succeeded at 922 (offset -2 lines).
1 out of 29 hunks FAILED

Please rebase and resend the series against my char-misc-next branch.

thanks,

greg k-h

^ permalink raw reply	[flat|nested] 48+ messages in thread

* Re: [PATCH v3 00/23] counter: cleanups and device lifetime fixes
  2021-12-30  8:53   ` Uwe Kleine-König
@ 2021-12-30 14:58     ` Jonathan Cameron
  -1 siblings, 0 replies; 48+ messages in thread
From: Jonathan Cameron @ 2021-12-30 14:58 UTC (permalink / raw)
  To: Uwe Kleine-König
  Cc: William Breathitt Gray, Greg Kroah-Hartman, Jonathan Cameron,
	Kamel Bouhara, Ahmad Fatoum, David Lechner, Jonathan Corbet,
	linux-iio, linux-kernel, Oleksij Rempel, Alexandre Torgue,
	Fabrice Gasnier, Jarkko Nikula, linux-arm-kernel, kernel,
	Patrick Havelange, Syed Nayyar Waris, linux-stm32,
	Maxime Coquelin

On Thu, 30 Dec 2021 09:53:51 +0100
Uwe Kleine-König <u.kleine-koenig@pengutronix.de> wrote:

> Hello,
> 
> On Wed, Dec 29, 2021 at 04:44:18PM +0100, Uwe Kleine-König wrote:
> > this is v3 of my series to fix device lifetime issues in the counter
> > framework. This hopefully addresses all things pointed out for v2.
> > 
> > Note this depends on 60f07e74f86b (which is in next) now. Full diffstat
> > below.
> > 
> > Things that could be further improved:
> > 
> > [...]
> > 
> > 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                 |  93 +++++-----
> >  drivers/counter/counter-core.c               | 186 ++++++++++++++-----
> >  drivers/counter/ftm-quaddec.c                |  36 ++--
> >  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                    |  31 ++--
> >  include/linux/counter.h                      |  15 +-
> >  11 files changed, 356 insertions(+), 242 deletions(-)
> > 
> > Range-diff against v2:
> > [...]
> > 
> > base-commit: a7904a538933c525096ca2ccde1e60d0ee62c08e
> > prerequisite-patch-id: 9459ad8bc78190558df9123f8bebe28ca1c396ea  
> 
> All patches have a blessing by at least one of William and Jonathan.

For future reference (may be fine this time) William has final say on counter
stuff as the maintainer so treat my input as just another set of eyes.

Anyhow, plenty of time for any necessary fixes during the RCs so shouldn't
be a problem.

Jonathan


> The prerequisite commit (60f07e74f86b) is in Greg's char-misc-next branch.
> 
> Assuming noone still finds a problem in this series that requires me to
> respin I wonder who will pick it up? Greg?
> 
> Given that it fixes a possible use-after-free in all counter drivers,
> I'd like to see it hit before v5.17-rc1. For 5.16 it's probably too
> late.
> 
> Best regards
> Uwe
> 


^ permalink raw reply	[flat|nested] 48+ messages in thread

* Re: [PATCH v3 00/23] counter: cleanups and device lifetime fixes
@ 2021-12-30 14:58     ` Jonathan Cameron
  0 siblings, 0 replies; 48+ messages in thread
From: Jonathan Cameron @ 2021-12-30 14:58 UTC (permalink / raw)
  To: Uwe Kleine-König
  Cc: William Breathitt Gray, Greg Kroah-Hartman, Jonathan Cameron,
	Kamel Bouhara, Ahmad Fatoum, David Lechner, Jonathan Corbet,
	linux-iio, linux-kernel, Oleksij Rempel, Alexandre Torgue,
	Fabrice Gasnier, Jarkko Nikula, linux-arm-kernel, kernel,
	Patrick Havelange, Syed Nayyar Waris, linux-stm32,
	Maxime Coquelin

On Thu, 30 Dec 2021 09:53:51 +0100
Uwe Kleine-König <u.kleine-koenig@pengutronix.de> wrote:

> Hello,
> 
> On Wed, Dec 29, 2021 at 04:44:18PM +0100, Uwe Kleine-König wrote:
> > this is v3 of my series to fix device lifetime issues in the counter
> > framework. This hopefully addresses all things pointed out for v2.
> > 
> > Note this depends on 60f07e74f86b (which is in next) now. Full diffstat
> > below.
> > 
> > Things that could be further improved:
> > 
> > [...]
> > 
> > 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                 |  93 +++++-----
> >  drivers/counter/counter-core.c               | 186 ++++++++++++++-----
> >  drivers/counter/ftm-quaddec.c                |  36 ++--
> >  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                    |  31 ++--
> >  include/linux/counter.h                      |  15 +-
> >  11 files changed, 356 insertions(+), 242 deletions(-)
> > 
> > Range-diff against v2:
> > [...]
> > 
> > base-commit: a7904a538933c525096ca2ccde1e60d0ee62c08e
> > prerequisite-patch-id: 9459ad8bc78190558df9123f8bebe28ca1c396ea  
> 
> All patches have a blessing by at least one of William and Jonathan.

For future reference (may be fine this time) William has final say on counter
stuff as the maintainer so treat my input as just another set of eyes.

Anyhow, plenty of time for any necessary fixes during the RCs so shouldn't
be a problem.

Jonathan


> The prerequisite commit (60f07e74f86b) is in Greg's char-misc-next branch.
> 
> Assuming noone still finds a problem in this series that requires me to
> respin I wonder who will pick it up? Greg?
> 
> Given that it fixes a possible use-after-free in all counter drivers,
> I'd like to see it hit before v5.17-rc1. For 5.16 it's probably too
> late.
> 
> Best regards
> Uwe
> 


_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

^ permalink raw reply	[flat|nested] 48+ messages in thread

* Re: [PATCH v3 00/23] counter: cleanups and device lifetime fixes
  2021-12-30 14:58     ` Jonathan Cameron
@ 2021-12-30 15:08       ` Uwe Kleine-König
  -1 siblings, 0 replies; 48+ messages in thread
From: Uwe Kleine-König @ 2021-12-30 15:08 UTC (permalink / raw)
  To: Jonathan Cameron
  Cc: Kamel Bouhara, Ahmad Fatoum, David Lechner, Jonathan Corbet,
	linux-iio, Greg Kroah-Hartman, William Breathitt Gray,
	Maxime Coquelin, linux-kernel, Alexandre Torgue, Fabrice Gasnier,
	Jarkko Nikula, kernel, Jonathan Cameron, Syed Nayyar Waris,
	Oleksij Rempel, Patrick Havelange, linux-stm32, linux-arm-kernel

[-- Attachment #1: Type: text/plain, Size: 4105 bytes --]

On Thu, Dec 30, 2021 at 02:58:26PM +0000, Jonathan Cameron wrote:
> On Thu, 30 Dec 2021 09:53:51 +0100
> Uwe Kleine-König <u.kleine-koenig@pengutronix.de> wrote:
> 
> > Hello,
> > 
> > On Wed, Dec 29, 2021 at 04:44:18PM +0100, Uwe Kleine-König wrote:
> > > this is v3 of my series to fix device lifetime issues in the counter
> > > framework. This hopefully addresses all things pointed out for v2.
> > > 
> > > Note this depends on 60f07e74f86b (which is in next) now. Full diffstat
> > > below.
> > > 
> > > Things that could be further improved:
> > > 
> > > [...]
> > > 
> > > 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                 |  93 +++++-----
> > >  drivers/counter/counter-core.c               | 186 ++++++++++++++-----
> > >  drivers/counter/ftm-quaddec.c                |  36 ++--
> > >  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                    |  31 ++--
> > >  include/linux/counter.h                      |  15 +-
> > >  11 files changed, 356 insertions(+), 242 deletions(-)
> > > 
> > > Range-diff against v2:
> > > [...]
> > > 
> > > base-commit: a7904a538933c525096ca2ccde1e60d0ee62c08e
> > > prerequisite-patch-id: 9459ad8bc78190558df9123f8bebe28ca1c396ea  
> > 
> > All patches have a blessing by at least one of William and Jonathan.
> 
> For future reference (may be fine this time) William has final say on counter
> stuff as the maintainer so treat my input as just another set of eyes.

Yeah, right. William only didn't ack patch 13 but wrote in reply it in v2:

	I agree with the approach taken in this patch, and I don't have much to
	add after the suggestions Lars-Peter and Jonathan have already given. So
	assuming those are addressed in the next version I expect to Ack this
	patch as well.

So I assume it's just that William didn't have the time yet to look into
v3 (or v4 that I just sent out) yet.

Best regards and thanks to all who gave feedback to improve this patch
set,
Uwe

-- 
Pengutronix e.K.                           | Uwe Kleine-König            |
Industrial Linux Solutions                 | https://www.pengutronix.de/ |

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 488 bytes --]

^ permalink raw reply	[flat|nested] 48+ messages in thread

* Re: [PATCH v3 00/23] counter: cleanups and device lifetime fixes
@ 2021-12-30 15:08       ` Uwe Kleine-König
  0 siblings, 0 replies; 48+ messages in thread
From: Uwe Kleine-König @ 2021-12-30 15:08 UTC (permalink / raw)
  To: Jonathan Cameron
  Cc: Kamel Bouhara, Ahmad Fatoum, David Lechner, Jonathan Corbet,
	linux-iio, Greg Kroah-Hartman, William Breathitt Gray,
	Maxime Coquelin, linux-kernel, Alexandre Torgue, Fabrice Gasnier,
	Jarkko Nikula, kernel, Jonathan Cameron, Syed Nayyar Waris,
	Oleksij Rempel, Patrick Havelange, linux-stm32, linux-arm-kernel


[-- Attachment #1.1: Type: text/plain, Size: 4105 bytes --]

On Thu, Dec 30, 2021 at 02:58:26PM +0000, Jonathan Cameron wrote:
> On Thu, 30 Dec 2021 09:53:51 +0100
> Uwe Kleine-König <u.kleine-koenig@pengutronix.de> wrote:
> 
> > Hello,
> > 
> > On Wed, Dec 29, 2021 at 04:44:18PM +0100, Uwe Kleine-König wrote:
> > > this is v3 of my series to fix device lifetime issues in the counter
> > > framework. This hopefully addresses all things pointed out for v2.
> > > 
> > > Note this depends on 60f07e74f86b (which is in next) now. Full diffstat
> > > below.
> > > 
> > > Things that could be further improved:
> > > 
> > > [...]
> > > 
> > > 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                 |  93 +++++-----
> > >  drivers/counter/counter-core.c               | 186 ++++++++++++++-----
> > >  drivers/counter/ftm-quaddec.c                |  36 ++--
> > >  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                    |  31 ++--
> > >  include/linux/counter.h                      |  15 +-
> > >  11 files changed, 356 insertions(+), 242 deletions(-)
> > > 
> > > Range-diff against v2:
> > > [...]
> > > 
> > > base-commit: a7904a538933c525096ca2ccde1e60d0ee62c08e
> > > prerequisite-patch-id: 9459ad8bc78190558df9123f8bebe28ca1c396ea  
> > 
> > All patches have a blessing by at least one of William and Jonathan.
> 
> For future reference (may be fine this time) William has final say on counter
> stuff as the maintainer so treat my input as just another set of eyes.

Yeah, right. William only didn't ack patch 13 but wrote in reply it in v2:

	I agree with the approach taken in this patch, and I don't have much to
	add after the suggestions Lars-Peter and Jonathan have already given. So
	assuming those are addressed in the next version I expect to Ack this
	patch as well.

So I assume it's just that William didn't have the time yet to look into
v3 (or v4 that I just sent out) yet.

Best regards and thanks to all who gave feedback to improve this patch
set,
Uwe

-- 
Pengutronix e.K.                           | Uwe Kleine-König            |
Industrial Linux Solutions                 | https://www.pengutronix.de/ |

[-- Attachment #1.2: signature.asc --]
[-- Type: application/pgp-signature, Size: 488 bytes --]

[-- Attachment #2: Type: text/plain, Size: 176 bytes --]

_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

^ permalink raw reply	[flat|nested] 48+ messages in thread

* Re: [PATCH v3 00/23] counter: cleanups and device lifetime fixes
  2021-12-29 15:44 ` Uwe Kleine-König
                   ` (24 preceding siblings ...)
  (?)
@ 2022-01-05 12:26 ` William Breathitt Gray
  2022-01-06 15:13   ` Uwe Kleine-König
  -1 siblings, 1 reply; 48+ messages in thread
From: William Breathitt Gray @ 2022-01-05 12:26 UTC (permalink / raw)
  To: Uwe Kleine-König; +Cc: linux-iio, kernel, linux-kernel, Jarkko Nikula

[-- Attachment #1: Type: text/plain, Size: 482 bytes --]

On Wed, Dec 29, 2021 at 04:44:18PM +0100, Uwe Kleine-König wrote:
>  - I think intel-qep.c makes the counter unfunctional in
>    intel_qep_remove before the counter is unregistered.

Hello Uwe,

Would you elaborate some more on this? I think intel_qep_remove() is
only called after the counter is unregistered because the struct
counter_device parent is set to &pci->dev in intel_qep_probe(). Am I
misunderstanding the removal path?

Thanks,

William Breathitt Gray

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

^ permalink raw reply	[flat|nested] 48+ messages in thread

* Re: [PATCH v3 00/23] counter: cleanups and device lifetime fixes
  2022-01-05 12:26 ` William Breathitt Gray
@ 2022-01-06 15:13   ` Uwe Kleine-König
  2022-01-11  9:56     ` Jarkko Nikula
  0 siblings, 1 reply; 48+ messages in thread
From: Uwe Kleine-König @ 2022-01-06 15:13 UTC (permalink / raw)
  To: William Breathitt Gray; +Cc: linux-iio, Jarkko Nikula, linux-kernel, kernel

[-- Attachment #1: Type: text/plain, Size: 1526 bytes --]

On Wed, Jan 05, 2022 at 09:26:58PM +0900, William Breathitt Gray wrote:
> On Wed, Dec 29, 2021 at 04:44:18PM +0100, Uwe Kleine-König wrote:
> >  - I think intel-qep.c makes the counter unfunctional in
> >    intel_qep_remove before the counter is unregistered.
> 
> Hello Uwe,
> 
> Would you elaborate some more on this? I think intel_qep_remove() is
> only called after the counter is unregistered because the struct
> counter_device parent is set to &pci->dev in intel_qep_probe(). Am I
> misunderstanding the removal path?

If the counter device is unbound (e.g. via sysfs), the following calls
are made:

	intel_qep_remove() (stopping the hardware?)
	devm_counter_release (devm callback of devm_counter_register or ..._add)
	then the release callbacks of the earlier devm functions

My concern is, that in the timeslot between intel_qep_remove() and
devm_counter_release() the device looks like a functional device and
might be queried/reconfigured/... while the hardware is already dead.

It's probably not a big issue (unless for example reading the counter
this race window makes the hardware hang?), but it's at least ugly.
Maybe the worst effect is that a counter value is missed (which is OK at
unregister time). Still it would be nicer to first take down the counter
device and only then stop the hardware.

Best regards
Uwe

-- 
Pengutronix e.K.                           | Uwe Kleine-König            |
Industrial Linux Solutions                 | https://www.pengutronix.de/ |

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 488 bytes --]

^ permalink raw reply	[flat|nested] 48+ messages in thread

* Re: [PATCH v3 00/23] counter: cleanups and device lifetime fixes
  2022-01-06 15:13   ` Uwe Kleine-König
@ 2022-01-11  9:56     ` Jarkko Nikula
  0 siblings, 0 replies; 48+ messages in thread
From: Jarkko Nikula @ 2022-01-11  9:56 UTC (permalink / raw)
  To: Uwe Kleine-König, William Breathitt Gray
  Cc: linux-iio, linux-kernel, kernel

Hi

On 1/6/22 17:13, Uwe Kleine-König wrote:
> On Wed, Jan 05, 2022 at 09:26:58PM +0900, William Breathitt Gray wrote:
>> On Wed, Dec 29, 2021 at 04:44:18PM +0100, Uwe Kleine-König wrote:
>>>   - I think intel-qep.c makes the counter unfunctional in
>>>     intel_qep_remove before the counter is unregistered.
>>
>> Hello Uwe,
>>
>> Would you elaborate some more on this? I think intel_qep_remove() is
>> only called after the counter is unregistered because the struct
>> counter_device parent is set to &pci->dev in intel_qep_probe(). Am I
>> misunderstanding the removal path?
> 
> If the counter device is unbound (e.g. via sysfs), the following calls
> are made:
> 
> 	intel_qep_remove() (stopping the hardware?)
> 	devm_counter_release (devm callback of devm_counter_register or ..._add)
> 	then the release callbacks of the earlier devm functions
> 
> My concern is, that in the timeslot between intel_qep_remove() and
> devm_counter_release() the device looks like a functional device and
> might be queried/reconfigured/... while the hardware is already dead.
> 
> It's probably not a big issue (unless for example reading the counter
> this race window makes the hardware hang?), but it's at least ugly.
> Maybe the worst effect is that a counter value is missed (which is OK at
> unregister time). Still it would be nicer to first take down the counter
> device and only then stop the hardware.
> 
In HW point of view it should be safe. We do disable the HW in 
intel_qep_remove() but that doesn't render the HW unusable and registers 
are accessible.

Perhaps that line can go since I think it was put there just to stop the 
HW just in case after remove.

Jarkko

^ permalink raw reply	[flat|nested] 48+ messages in thread

end of thread, other threads:[~2022-01-11  9:56 UTC | newest]

Thread overview: 48+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-12-29 15:44 [PATCH v3 00/23] counter: cleanups and device lifetime fixes Uwe Kleine-König
2021-12-29 15:44 ` Uwe Kleine-König
2021-12-29 15:44 ` [PATCH v3 01/23] counter: Use container_of instead of drvdata to track counter_device Uwe Kleine-König
2021-12-29 15:44 ` [PATCH v3 02/23] counter: ftm-quaddec: Drop unused platform_set_drvdata() Uwe Kleine-König
2021-12-29 15:44 ` [PATCH v3 03/23] counter: microchip-tcb-capture: " Uwe Kleine-König
2021-12-29 15:44 ` [PATCH v3 04/23] counter: Provide a wrapper to access device private data Uwe Kleine-König
2021-12-29 15:44 ` [PATCH v3 05/23] counter: 104-quad-8: Convert to counter_priv() wrapper Uwe Kleine-König
2021-12-30 13:19   ` Greg Kroah-Hartman
2021-12-29 15:44 ` [PATCH v3 06/23] counter: interrupt-cnt: " Uwe Kleine-König
2021-12-29 15:44 ` [PATCH v3 07/23] counter: microchip-tcb-capture: " Uwe Kleine-König
2021-12-29 15:44 ` [PATCH v3 08/23] counter: intel-qep: " Uwe Kleine-König
2021-12-30 10:26   ` Jarkko Nikula
2021-12-29 15:44 ` [PATCH v3 09/23] counter: ftm-quaddec: " Uwe Kleine-König
2021-12-29 15:44 ` [PATCH v3 10/23] counter: ti-eqep: " Uwe Kleine-König
2021-12-29 22:42   ` William Breathitt Gray
2021-12-29 22:56   ` David Lechner
2021-12-29 15:44 ` [PATCH v3 11/23] counter: stm32-lptimer-cnt: " Uwe Kleine-König
2021-12-29 15:44   ` Uwe Kleine-König
2021-12-29 15:44 ` [PATCH v3 12/23] counter: stm32-timer-cnt: " Uwe Kleine-König
2021-12-29 15:44   ` Uwe Kleine-König
2021-12-29 15:44 ` [PATCH v3 13/23] counter: Provide alternative counter registration functions Uwe Kleine-König
2021-12-29 17:06   ` Jonathan Cameron
2021-12-30  8:38     ` Uwe Kleine-König
2021-12-29 15:44 ` [PATCH v3 14/23] counter: Update documentation for new " Uwe Kleine-König
2021-12-29 15:44 ` [PATCH v3 15/23] counter: 104-quad-8: Convert to new counter registration Uwe Kleine-König
2021-12-29 15:44 ` [PATCH v3 16/23] counter: interrupt-cnt: " Uwe Kleine-König
2021-12-29 15:44 ` [PATCH v3 17/23] counter: intel-qep: " Uwe Kleine-König
2021-12-30 10:26   ` Jarkko Nikula
2021-12-29 15:44 ` [PATCH v3 18/23] counter: ftm-quaddec: " Uwe Kleine-König
2021-12-29 15:44 ` [PATCH v3 19/23] counter: microchip-tcb-capture: " Uwe Kleine-König
2021-12-29 15:44 ` [PATCH v3 20/23] counter: stm32-timer-cnt: " Uwe Kleine-König
2021-12-29 15:44   ` Uwe Kleine-König
2021-12-29 15:44 ` [PATCH v3 21/23] counter: stm32-lptimer-cnt: " Uwe Kleine-König
2021-12-29 15:44   ` Uwe Kleine-König
2021-12-29 15:44 ` [PATCH v3 22/23] counter: ti-eqep: " Uwe Kleine-König
2021-12-29 22:56   ` David Lechner
2021-12-29 15:44 ` [PATCH v3 23/23] counter: remove old and now unused registration API Uwe Kleine-König
2021-12-30  8:53 ` [PATCH v3 00/23] counter: cleanups and device lifetime fixes Uwe Kleine-König
2021-12-30  8:53   ` Uwe Kleine-König
2021-12-30 12:31   ` Greg Kroah-Hartman
2021-12-30 12:31     ` Greg Kroah-Hartman
2021-12-30 14:58   ` Jonathan Cameron
2021-12-30 14:58     ` Jonathan Cameron
2021-12-30 15:08     ` Uwe Kleine-König
2021-12-30 15:08       ` Uwe Kleine-König
2022-01-05 12:26 ` William Breathitt Gray
2022-01-06 15:13   ` Uwe Kleine-König
2022-01-11  9:56     ` Jarkko Nikula

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.