From: Simon South <simon@simonsouth.net>
To: Trent Piepho <tpiepho@gmail.com>
Cc: linux-pwm@vger.kernel.org, heiko@sntech.de,
Boris Brezillon <bbrezillon@kernel.org>,
linux-rockchip@lists.infradead.org, thierry.reding@gmail.com,
u.kleine-koenig@pengutronix.de, lee.jones@linaro.org,
linux-arm-kernel@lists.infradead.org
Subject: Re: [PATCH v2] pwm: rockchip: Keep enabled PWMs running while probing
Date: Sun, 29 Nov 2020 19:36:09 -0500 [thread overview]
Message-ID: <875z5nof46.fsf@simonsouth.net> (raw)
In-Reply-To: <2177360.ElGaqSPkdT@zen.local> (Trent Piepho's message of "Fri, 20 Nov 2020 17:09:58 -0800")
Trent Piepho <tpiepho@gmail.com> writes:
> Anyway, it seems like this solution has a race. Isn't the pwm live
> and requestable as soon as pwmchip_add() returns? Which would mean
> that disabling the clock here could race with other code requesting
> and enabling the pwm.
Yes, I think that's true. Glad you caught this.
> Seems like it would be safer to check the initial state and turn off
> the clock before calling pwmchip_add.
Yes. I tested this and it works, but on further consideration it seems
to me the code is actually doing things backwards: Instead of enabling
every PWM's clock and then turning off the clocks for PWMs that are not
themselves enabled, it should enable only the clocks for PWMs that
appear to be in use and leave the remaining clocks in their default
(presumably disabled) state. This should produce the same end result but
without the driver enabling clocks indiscriminately and without creating
a race condition.
I'll follow up with a patch for review that implements this change. I've
tested it as best I can on my own Pinebook Pro; everything works as it
did previously, with the display backlight on, no kernel hang and no
other apparent side effects.
> It seems like the issue is the driver was calling pwm_is_enabled()
> without first requesting the pwm with pwm_get().
This used to work because pwmchip_add() happened to invoke
rockchip_pwm_get_state(), which populates the PWM's pwm_state structure
from which pwm_is_enabled() reads. And I think that's why the code was
written the way it was originally: By waiting until pwmchip_add()
returned the PWM's state could be queried in a convenient manner,
without resorting to peeking at the hardware's registers.
Commit cfc4c189bc70 ("pwm: Read initial hardware state at request time")
changed this and pwmchip_add() no longer has the side effect of
populating the state structure, so the call to pwm_is_enabled() no
longer worked reliably. That's what I fixed with the patch you're
replying to, though now I wish I'd seen the larger issue.
As to why this code is needed in the first place (as I wondered recently
while working on it again), it seems to be a somewhat hacky way of
initializing the enable_count reference counter (see drivers/clk/clk.c)
of the already running clock to 1 instead of 0. This is necessary
because on SoCs like the RK3399 that use only a single clock for each
PWM, the driver treats the "APB" and "bus" clocks as referring to the
same physical device ("pc->pclk = pc->clk"), and without it functions
like rockchip_pwm_get_state() that enable the APB clock on entry and
disable it on exit would end up halting the PWM entirely.
--
Simon South
simon@simonsouth.net
_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
next prev parent reply other threads:[~2020-11-30 0:38 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-09-19 19:33 [PATCH v2] pwm: rockchip: Keep enabled PWMs running while probing Simon South
2020-09-21 8:01 ` Uwe Kleine-König
2020-09-23 10:49 ` Heiko Stübner
2020-09-23 11:41 ` Thierry Reding
2020-11-21 1:09 ` Trent Piepho
2020-11-30 0:36 ` Simon South [this message]
2020-11-30 0:44 ` [PATCH] pwm: rockchip: Eliminate potential race condition when probing Simon South
2020-12-10 17:48 ` Thierry Reding
2020-12-10 21:00 ` Trent Piepho
2020-12-11 10:44 ` Robin Murphy
2020-12-19 20:32 ` Simon South
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=875z5nof46.fsf@simonsouth.net \
--to=simon@simonsouth.net \
--cc=bbrezillon@kernel.org \
--cc=heiko@sntech.de \
--cc=lee.jones@linaro.org \
--cc=linux-arm-kernel@lists.infradead.org \
--cc=linux-pwm@vger.kernel.org \
--cc=linux-rockchip@lists.infradead.org \
--cc=thierry.reding@gmail.com \
--cc=tpiepho@gmail.com \
--cc=u.kleine-koenig@pengutronix.de \
/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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).