From: Tzung-Bi Shih <tzungbi@kernel.org>
To: Radu Vele <raduvele@google.com>
Cc: Benson Leung <bleung@chromium.org>,
Abhishek Pandit-Subedi <abhishekpandit@chromium.org>,
Jameson Thies <jthies@google.com>,
Andrei Kuchynski <akuchynski@chromium.org>,
chrome-platform@lists.linux.dev, linux-kernel@vger.kernel.org
Subject: Re: [PATCH v2 2/2] platform/chrome: cros_ec_typec: Add lock per-port
Date: Thu, 17 Jul 2025 07:46:06 +0000 [thread overview]
Message-ID: <aHiqPuBm8Znmph61@google.com> (raw)
In-Reply-To: <CACKy9TJvkx0Bi69pO187dxs8EUt3foc0seNYjn=vK4WCuTHYxQ@mail.gmail.com>
On Wed, Jul 16, 2025 at 01:39:53PM +0200, Radu Vele wrote:
> On Tue, Jul 15, 2025 at 8:07 AM Tzung-Bi Shih <tzungbi@kernel.org> wrote:
> >
> > On Mon, Jul 14, 2025 at 10:32:03AM +0200, Radu Vele wrote:
> > > On Fri, Jul 11, 2025 at 6:12 AM Tzung-Bi Shih <tzungbi@kernel.org> wrote:
> > > >
> > > > On Fri, Jul 11, 2025 at 12:35:02AM +0000, Radu Vele wrote:
> > > > > Add a lock associated to each port to protect port data against
> > > > > concurrent access. Concurrency may result from sysfs commands
> > > > > and ec events.
> > > >
> > > > I realized the critical sections are way too large. What exactly data the
> > > > lock tries to protect? Is the race possibility introduced by any previous
> > > > commits? Please provide more context.
> > >
> > > With the implementation of the role swap operations from the previous
> > > commit (and also enter usb mode from another recent commit) we
> > > introduce the possibility of concurrent access to the cros_ec_typec port
> > > data from the userspace (e.g. trigger a power role swap from sysfs) vs
> > > from EC events (e.g. partner triggered a role swap that we accept).
> > > This is the main reason to propose a per-port lock. This way we ensure
> > > we protect the state of each port in the cros_ec_typec driver.
> >
> > To make sure I understand, did you mean the lock tries to prevent from
> > sending multiple commands to EC at a time? If yes, does it still need
> > if the underlying ec_dev is guranteed that [1]?
>
> Not really, as I noticed that both the ec and Type-C connector class
> use their own mutexes.
>
> My intention with the mutexes is to avoid race conditions in the case
> when a role swap is in progress but at the same time there is a
> `cros_port_update` that modifies the state of the port. Another
> example I have in mind is when the port is being unregistered and
> a role swap is issued.
The critical section is too large to understand which fields in the data
structure it tries to protect. Please review again whether the lock is
needed or not and shrink the critical section if possible.
If taking the changes in cros_typec_perform_role_swap() as another example:
static int cros_typec_perform_role_swap(...)
{
...
struct ec_params_usb_pd_control req;
guard(mutex)(&port->lock);
req...
cros_ec_cmd(...)
req...
cros_ec_cmd(...)
switch (...) {
typec_set_data_role...
typec_set_pwr_role...
}
}
The `req` and `cros_ec_cmd` obviously don't need to protect. Does
typec_set_data_role() and typec_set_pwr_role() need to protect from
concurrent calling?
next prev parent reply other threads:[~2025-07-17 7:46 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-07-11 0:35 [PATCH v2 1/2] platform/chrome: cros_ec_typec: Add role swap ops Radu Vele
2025-07-11 0:35 ` [PATCH v2 2/2] platform/chrome: cros_ec_typec: Add lock per-port Radu Vele
2025-07-11 4:11 ` Tzung-Bi Shih
2025-07-14 8:32 ` Radu Vele
2025-07-15 6:07 ` Tzung-Bi Shih
2025-07-16 11:39 ` Radu Vele
2025-07-17 7:46 ` Tzung-Bi Shih [this message]
2025-07-11 4:11 ` (subset) [PATCH v2 1/2] platform/chrome: cros_ec_typec: Add role swap ops Tzung-Bi Shih
2025-07-14 9:41 ` Radu Vele
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=aHiqPuBm8Znmph61@google.com \
--to=tzungbi@kernel.org \
--cc=abhishekpandit@chromium.org \
--cc=akuchynski@chromium.org \
--cc=bleung@chromium.org \
--cc=chrome-platform@lists.linux.dev \
--cc=jthies@google.com \
--cc=linux-kernel@vger.kernel.org \
--cc=raduvele@google.com \
/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).