mbox series

[0/3] Some fixes for select_usb_power_delivery

Message ID 20230622150423.3726894-1-kyletso@google.com
Headers show
Series Some fixes for select_usb_power_delivery | expand

Message

Kyle Tso June 22, 2023, 3:04 p.m. UTC
Hi, here are some fixes about the attribute "select_usb_power_delivery"
in typec/class.c

===

usb: typec: Set port->pd before adding device for typec_port

This one is about the visibility of the attribute. port->pd should be
set before device_add otherwise the visibility will be false because
port->pd is NULL.

===

usb: typec: Iterate pds array when showing the pd list

This patch fixes a problem about the incorrect fetching of the pointers
to each usb_power_delivery handle.

===

usb: typec: Use sysfs_emit_at when concatenating the string

This patch changes the use of the API from sysfs_emit to sysfs_emit_at
because the buffer address is not aligned to PAGE_SIZE.

===

Kyle Tso (3):
  usb: typec: Set port->pd before adding device for typec_port
  usb: typec: Iterate pds array when showing the pd list
  usb: typec: Use sysfs_emit_at when concatenating the string

 drivers/usb/typec/class.c | 15 ++++++++-------
 1 file changed, 8 insertions(+), 7 deletions(-)

Comments

Greg KH June 23, 2023, 7:50 a.m. UTC | #1
On Thu, Jun 22, 2023 at 11:04:21PM +0800, Kyle Tso wrote:
> When calling device_add in the registration of typec_port, it will do
> the NULL check on usb_power_delivery handle in typec_port for the
> visibility of the device attributes. It is always NULL because port->pd
> is set in typec_port_set_usb_power_delivery which is later than the
> device_add call.
> 
> Set port->pd before device_add and only link the device after that.
> 
> Fixes: a7cff92f0635 ("usb: typec: USB Power Delivery helpers for ports and partners")
> Signed-off-by: Kyle Tso <kyletso@google.com>

Shouldn't all of these also have a cc: stable@ line in them as well?

thanks,

greg k-h
Greg KH June 23, 2023, 7:51 a.m. UTC | #2
On Thu, Jun 22, 2023 at 11:04:23PM +0800, Kyle Tso wrote:
> The buffer address used in sysfs_emit should be aligned to PAGE_SIZE.

Why?  Are you getting warnings about this?

thanks,

greg k-h
Kyle Tso June 23, 2023, 10:06 a.m. UTC | #3
On Fri, Jun 23, 2023 at 3:51 PM Greg KH <gregkh@linuxfoundation.org> wrote:
>
> On Thu, Jun 22, 2023 at 11:04:23PM +0800, Kyle Tso wrote:
> > The buffer address used in sysfs_emit should be aligned to PAGE_SIZE.
>
> Why?  Are you getting warnings about this?
>
> thanks,
>
> greg k-h

Yes, here is the warning.

[  223.243123] invalid sysfs_emit: buf:00000000eda2d647
[  223.243197] WARNING: CPU: 4 PID: 8860 at fs/sysfs/file.c:735
sysfs_emit+0xb0/0xc0
[  223.244335] CPU: 4 PID: 8860 Comm: cat
[  223.244363] pstate: 62400005 (nZCv daif +PAN -UAO +TCO -DIT -SSBS BTYPE=--)
[  223.244378] pc : sysfs_emit+0xb0/0xc0
[  223.244394] lr : sysfs_emit+0xac/0xc0
...
[  223.244560] Call trace:
[  223.244568] sysfs_emit+0xb0/0xc0
[  223.244582] select_usb_power_delivery_show+0x134/0x18c
[  223.244626] dev_attr_show+0x38/0x74
[  223.244654] sysfs_kf_seq_show+0xb4/0x130
[  223.244668] kernfs_seq_show+0x44/0x54
[  223.244683] seq_read_iter+0x158/0x4ec
[  223.244727] kernfs_fop_read_iter+0x68/0x1b0
[  223.244739] vfs_read+0x1d8/0x2b0
[  223.244775] ksys_read+0x78/0xe8

The warning comes from
https://elixir.bootlin.com/linux/v6.3.9/source/fs/sysfs/file.c#L734

if (WARN(!buf || offset_in_page(buf), "invalid sysfs_emit: buf:%p\n", buf))
        return 0;

Kyle
Kyle Tso June 23, 2023, 10:08 a.m. UTC | #4
On Fri, Jun 23, 2023 at 3:50 PM Greg KH <gregkh@linuxfoundation.org> wrote:
>
> On Thu, Jun 22, 2023 at 11:04:21PM +0800, Kyle Tso wrote:
> > When calling device_add in the registration of typec_port, it will do
> > the NULL check on usb_power_delivery handle in typec_port for the
> > visibility of the device attributes. It is always NULL because port->pd
> > is set in typec_port_set_usb_power_delivery which is later than the
> > device_add call.
> >
> > Set port->pd before device_add and only link the device after that.
> >
> > Fixes: a7cff92f0635 ("usb: typec: USB Power Delivery helpers for ports and partners")
> > Signed-off-by: Kyle Tso <kyletso@google.com>
>
> Shouldn't all of these also have a cc: stable@ line in them as well?
>
> thanks,
>
> greg k-h

Will do in v2

Kyle
Kyle Tso June 23, 2023, 10:11 a.m. UTC | #5
On Fri, Jun 23, 2023 at 6:06 PM Kyle Tso <kyletso@google.com> wrote:
>
> On Fri, Jun 23, 2023 at 3:51 PM Greg KH <gregkh@linuxfoundation.org> wrote:
> >
> > On Thu, Jun 22, 2023 at 11:04:23PM +0800, Kyle Tso wrote:
> > > The buffer address used in sysfs_emit should be aligned to PAGE_SIZE.
> >
> > Why?  Are you getting warnings about this?
> >
> > thanks,
> >
> > greg k-h
>
> Yes, here is the warning.
>
> [  223.243123] invalid sysfs_emit: buf:00000000eda2d647
> [  223.243197] WARNING: CPU: 4 PID: 8860 at fs/sysfs/file.c:735
> sysfs_emit+0xb0/0xc0
> [  223.244335] CPU: 4 PID: 8860 Comm: cat
> [  223.244363] pstate: 62400005 (nZCv daif +PAN -UAO +TCO -DIT -SSBS BTYPE=--)
> [  223.244378] pc : sysfs_emit+0xb0/0xc0
> [  223.244394] lr : sysfs_emit+0xac/0xc0
> ...
> [  223.244560] Call trace:
> [  223.244568] sysfs_emit+0xb0/0xc0
> [  223.244582] select_usb_power_delivery_show+0x134/0x18c
> [  223.244626] dev_attr_show+0x38/0x74
> [  223.244654] sysfs_kf_seq_show+0xb4/0x130
> [  223.244668] kernfs_seq_show+0x44/0x54
> [  223.244683] seq_read_iter+0x158/0x4ec
> [  223.244727] kernfs_fop_read_iter+0x68/0x1b0
> [  223.244739] vfs_read+0x1d8/0x2b0
> [  223.244775] ksys_read+0x78/0xe8
>
> The warning comes from
> https://elixir.bootlin.com/linux/v6.3.9/source/fs/sysfs/file.c#L734
>
> if (WARN(!buf || offset_in_page(buf), "invalid sysfs_emit: buf:%p\n", buf))
>         return 0;
>
> Kyle

BTW, to print the buf address, it should use %pK ...

Kyle
Greg KH June 23, 2023, 11:14 a.m. UTC | #6
On Fri, Jun 23, 2023 at 06:11:00PM +0800, Kyle Tso wrote:
> On Fri, Jun 23, 2023 at 6:06 PM Kyle Tso <kyletso@google.com> wrote:
> >
> > On Fri, Jun 23, 2023 at 3:51 PM Greg KH <gregkh@linuxfoundation.org> wrote:
> > >
> > > On Thu, Jun 22, 2023 at 11:04:23PM +0800, Kyle Tso wrote:
> > > > The buffer address used in sysfs_emit should be aligned to PAGE_SIZE.
> > >
> > > Why?  Are you getting warnings about this?
> > >
> > > thanks,
> > >
> > > greg k-h
> >
> > Yes, here is the warning.
> >
> > [  223.243123] invalid sysfs_emit: buf:00000000eda2d647
> > [  223.243197] WARNING: CPU: 4 PID: 8860 at fs/sysfs/file.c:735
> > sysfs_emit+0xb0/0xc0
> > [  223.244335] CPU: 4 PID: 8860 Comm: cat
> > [  223.244363] pstate: 62400005 (nZCv daif +PAN -UAO +TCO -DIT -SSBS BTYPE=--)
> > [  223.244378] pc : sysfs_emit+0xb0/0xc0
> > [  223.244394] lr : sysfs_emit+0xac/0xc0
> > ...
> > [  223.244560] Call trace:
> > [  223.244568] sysfs_emit+0xb0/0xc0
> > [  223.244582] select_usb_power_delivery_show+0x134/0x18c
> > [  223.244626] dev_attr_show+0x38/0x74
> > [  223.244654] sysfs_kf_seq_show+0xb4/0x130
> > [  223.244668] kernfs_seq_show+0x44/0x54
> > [  223.244683] seq_read_iter+0x158/0x4ec
> > [  223.244727] kernfs_fop_read_iter+0x68/0x1b0
> > [  223.244739] vfs_read+0x1d8/0x2b0
> > [  223.244775] ksys_read+0x78/0xe8
> >
> > The warning comes from
> > https://elixir.bootlin.com/linux/v6.3.9/source/fs/sysfs/file.c#L734
> >
> > if (WARN(!buf || offset_in_page(buf), "invalid sysfs_emit: buf:%p\n", buf))
> >         return 0;
> >
> > Kyle
> 
> BTW, to print the buf address, it should use %pK ...

Patches accepted :)
Kyle Tso June 23, 2023, 2:23 p.m. UTC | #7
On Fri, Jun 23, 2023 at 7:14 PM Greg KH <gregkh@linuxfoundation.org> wrote:
>
> On Fri, Jun 23, 2023 at 06:11:00PM +0800, Kyle Tso wrote:
> > On Fri, Jun 23, 2023 at 6:06 PM Kyle Tso <kyletso@google.com> wrote:
> > >
> > > On Fri, Jun 23, 2023 at 3:51 PM Greg KH <gregkh@linuxfoundation.org> wrote:
> > > >
> > > > On Thu, Jun 22, 2023 at 11:04:23PM +0800, Kyle Tso wrote:
> > > > > The buffer address used in sysfs_emit should be aligned to PAGE_SIZE.
> > > >
> > > > Why?  Are you getting warnings about this?
> > > >
> > > > thanks,
> > > >
> > > > greg k-h
> > >
> > > Yes, here is the warning.
> > >
> > > [  223.243123] invalid sysfs_emit: buf:00000000eda2d647
> > > [  223.243197] WARNING: CPU: 4 PID: 8860 at fs/sysfs/file.c:735
> > > sysfs_emit+0xb0/0xc0
> > > [  223.244335] CPU: 4 PID: 8860 Comm: cat
> > > [  223.244363] pstate: 62400005 (nZCv daif +PAN -UAO +TCO -DIT -SSBS BTYPE=--)
> > > [  223.244378] pc : sysfs_emit+0xb0/0xc0
> > > [  223.244394] lr : sysfs_emit+0xac/0xc0
> > > ...
> > > [  223.244560] Call trace:
> > > [  223.244568] sysfs_emit+0xb0/0xc0
> > > [  223.244582] select_usb_power_delivery_show+0x134/0x18c
> > > [  223.244626] dev_attr_show+0x38/0x74
> > > [  223.244654] sysfs_kf_seq_show+0xb4/0x130
> > > [  223.244668] kernfs_seq_show+0x44/0x54
> > > [  223.244683] seq_read_iter+0x158/0x4ec
> > > [  223.244727] kernfs_fop_read_iter+0x68/0x1b0
> > > [  223.244739] vfs_read+0x1d8/0x2b0
> > > [  223.244775] ksys_read+0x78/0xe8
> > >
> > > The warning comes from
> > > https://elixir.bootlin.com/linux/v6.3.9/source/fs/sysfs/file.c#L734
> > >
> > > if (WARN(!buf || offset_in_page(buf), "invalid sysfs_emit: buf:%p\n", buf))
> > >         return 0;
> > >
> > > Kyle
> >
> > BTW, to print the buf address, it should use %pK ...
>
> Patches accepted :)

The patch (sysfs) has been sent to the mailing list.

Kyle