[<prev] [next>] [<thread-prev] [day] [month] [year] [list]
Message-ID: <CAGZ6i=1pjxETqcJG8Czv91Hv3tuWowLmYTqseqhAn=wuFP-Y6A@mail.gmail.com>
Date: Tue, 12 Feb 2019 18:33:45 +0800
From: Kyle Tso <kyletso@...gle.com>
To: Guenter Roeck <linux@...ck-us.net>
Cc: Greg KH <gregkh@...uxfoundation.org>,
Heikki Krogerus <heikki.krogerus@...ux.intel.com>,
Badhri Jagan Sridharan <badhri@...gle.com>,
linux-usb@...r.kernel.org, linux-kernel@...r.kernel.org
Subject: Re: [PATCH] usb: typec: tcpm: Export partner Source Capabilities
On Fri, Feb 1, 2019 at 2:22 AM Guenter Roeck <linux@...ck-us.net> wrote:
>
> On Thu, Jan 31, 2019 at 08:02:38AM +0100, Greg KH wrote:
> > On Thu, Jan 31, 2019 at 11:54:11AM +0800, Kyle Tso wrote:
> > > Provide a function to get the partner Source Capabilities.
> > >
> > > Signed-off-by: Kyle Tso <kyletso@...gle.com>
> > > ---
> > > drivers/usb/typec/tcpm/tcpm.c | 23 +++++++++++++++++++++++
> > > include/linux/usb/tcpm.h | 1 +
> > > 2 files changed, 24 insertions(+)
> > >
> > > diff --git a/drivers/usb/typec/tcpm/tcpm.c b/drivers/usb/typec/tcpm/tcpm.c
> > > index f1d3e54210df..29cd84ba9960 100644
> > > --- a/drivers/usb/typec/tcpm/tcpm.c
> > > +++ b/drivers/usb/typec/tcpm/tcpm.c
> > > @@ -4494,6 +4494,29 @@ int tcpm_update_sink_capabilities(struct tcpm_port *port, const u32 *pdo,
> > > }
> > > EXPORT_SYMBOL_GPL(tcpm_update_sink_capabilities);
> > >
> > > +/*
> > > + * Don't call this function in interrupt context. Caller needs to free the
> > > + * memory itself.
> > > + */
> > > +int tcpm_get_partner_src_caps(struct tcpm_port *port, u32 **src_pdo)
> > > +{
> > > + unsigned int nr_pdo;
> > > +
> > > + if (port->nr_source_caps == 0)
> > > + return -ENODATA;
> > > +
> > > + *src_pdo = kcalloc(port->nr_source_caps, sizeof(u32), GFP_KERNEL);
> > > + if (!src_pdo)
> > > + return -ENOMEM;
> > > +
> > > + mutex_lock(&port->lock);
> > > + nr_pdo = tcpm_copy_pdos(*src_pdo, port->source_caps,
> > > + port->nr_source_caps);
> > > + mutex_unlock(&port->lock);
>
> The mutex use here suggests that the data can be updated while being
> copied. But that suggests that port->nr_source_caps can change as
> well and may no longer be current after mutex_lock().
>
Thanks for pointing out the bug. I will fix this in the next version.
> > > + return nr_pdo;
> > > +}
> > > +EXPORT_SYMBOL_GPL(tcpm_get_partner_src_caps);
> >
> > We don't add new functions that no one uses :(
> >
>
> I am also concerned about the API itself; passing a pointer to be freed
> by the caller invites memory leaks.
>
> Guenter
What is the better way to implement this? How about providing a function
(say "tcpm_put_partner_src_caps") for the caller to free the memory?
Powered by blists - more mailing lists