All of lore.kernel.org
 help / color / mirror / Atom feed
From: William Breathitt Gray <william.gray@linaro.org>
To: Julien Panis <jpanis@baylibre.com>
Cc: robh+dt@kernel.org, krzysztof.kozlowski+dt@linaro.org,
	linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org,
	devicetree@vger.kernel.org, mranostay@ti.com
Subject: Re: [PATCH v8 3/4] counter: ti-ecap-capture: capture driver support for ECAP
Date: Fri, 23 Sep 2022 09:09:32 -0400	[thread overview]
Message-ID: <Yy2wDPITCZ/J01Xr@fedora> (raw)
In-Reply-To: <8a60b6a0-9289-0d89-c543-02386a721bca@baylibre.com>

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

On Fri, Sep 23, 2022 at 02:32:26PM +0200, Julien Panis wrote:
> 
> 
> On 23/09/2022 14:17, Julien Panis wrote:
> > 
> > 
> > On 23/09/2022 13:35, William Breathitt Gray wrote:
> > > On Fri, Sep 23, 2022 at 09:23:26AM +0200, Julien Panis wrote:
> > > > 
> > > > On 23/09/2022 03:08, William Breathitt Gray wrote:
> > > > > On Thu, Sep 22, 2022 at 07:04:01PM +0200, Julien Panis wrote:
> > > > > > ECAP hardware on TI AM62x SoC supports capture feature.
> > > > > > It can be used
> > > > > > to timestamp events (falling/rising edges) detected on input signal.
> > > > > > 
> > > > > > This commit adds capture driver support for ECAP
> > > > > > hardware on AM62x SoC.
> > > > > > 
> > > > > > In the ECAP hardware, capture pin can also be configured to be in
> > > > > > PWM mode. Current implementation only supports capture
> > > > > > operating mode.
> > > > > > Hardware also supports timebase sync between multiple instances, but
> > > > > > this driver supports simple independent capture functionality.
> > > > > > 
> > > > > > Signed-off-by: Julien Panis <jpanis@baylibre.com>
> > > > > Hello Julien,
> > > > > 
> > > > > Comments follow inline below.
> > > > > 
> > > > > > +/**
> > > > > > + * struct ecap_cnt_dev - device private data structure
> > > > > > + * @enabled: device state
> > > > > > + * @clk:     device clock
> > > > > > + * @regmap:  device register map
> > > > > > + * @nb_ovf:  number of overflows since capture start
> > > > > > + * @pm_ctx:  device context for PM operations
> > > > > > + */
> > > > > > +struct ecap_cnt_dev {
> > > > > > +    bool enabled;
> > > > > > +    struct clk *clk;
> > > > > > +    struct regmap *regmap;
> > > > > > +    atomic_t nb_ovf;
> > > > > > +    struct {
> > > > > > +        u8 ev_mode;
> > > > > > +        u32 time_cntr;
> > > > > > +    } pm_ctx;
> > > > > > +};
> > > > > Provide documentation for the ev_mode and time_cntr members. You
> > > > > probably need a lock as well to protect access to this structure or
> > > > > you'll end up with race problems.
> > > > Hi William,
> > > > 
> > > > How can I end up with race problems ? pm_ctx members are only
> > > > accessed at
> > > > suspend (after capture/IRQ are disabled) and resume (before
> > > > capture/IRQ are
> > > > re-enabled).
> > > > Is there any risk I did not identify ?
> > > > 
> > > > Julien
> > > I was thinking of the ecap_cnt_dev enabled member. The Counter callbacks
> > > may execute in concurrent threads, so races can appear when you access
> > > members of the ecap_cnt_dev structure in these callbacks.
> > > 
> > > Take for example this section of ecap_cnt_enable_write():
> > > 
> > >          if (enable == ecap_dev->enabled)
> > >                  return 0;
> > >          if (enable)
> > >                  ecap_cnt_capture_enable(counter);
> > >          else
> > >                  ecap_cnt_capture_disable(counter);
> > >          ecap_dev->enabled = enable
> > > 
> > > Suppose two threads try to enable the count capture. A race condition is
> > > present where the two threads could see ecap_dev->enabled as false and
> > > both proceed to call ecap_cnt_capture_enable(). This results in
> > > pm_runtime_get_sync() bumping the usage count twice and we're left with
> > > a mismatch the next time ecap_cnt_capture_disable() is called.
> > > 
> > > William Breathitt Gray
> > 
> > OK, If I understand well there's the same problem with IO access with
> > regmap ?
> > Julien
> 
> [ERRATUM] It seems that some spinlock is already used by regmap API.
> So, only the 'enabled' member needs a lock.

Keep in mind that although individual regmap API calls can be considered
atomic, the order of the calls made could be a point of race if there
are concurrent threads. However, in your particular driver it looks
you're fine as long as access to 'enabled' is protected.

William Breathitt Gray

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

  reply	other threads:[~2022-09-23 14:12 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-09-22 17:03 [PATCH v8 0/4] ECAP support on TI AM62x SoC Julien Panis
2022-09-22 17:03 ` [PATCH v8 1/4] dt-bindings: counter: add ti,am62-ecap-capture.yaml Julien Panis
2022-09-22 17:04 ` [PATCH v8 2/4] Documentation: ABI: sysfs-bus-counter: add frequency & num_overflows items Julien Panis
2022-09-22 17:04 ` [PATCH v8 3/4] counter: ti-ecap-capture: capture driver support for ECAP Julien Panis
2022-09-23  1:08   ` William Breathitt Gray
2022-09-23  7:23     ` Julien Panis
2022-09-23 11:35       ` William Breathitt Gray
2022-09-23 12:17         ` Julien Panis
2022-09-23 12:32           ` Julien Panis
2022-09-23 13:09             ` William Breathitt Gray [this message]
2022-09-22 17:04 ` [PATCH v8 4/4] MAINTAINERS: add TI ECAP driver info Julien Panis

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=Yy2wDPITCZ/J01Xr@fedora \
    --to=william.gray@linaro.org \
    --cc=devicetree@vger.kernel.org \
    --cc=jpanis@baylibre.com \
    --cc=krzysztof.kozlowski+dt@linaro.org \
    --cc=linux-iio@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mranostay@ti.com \
    --cc=robh+dt@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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.