2023-06-22 15:15:32

by Kyle Tso

[permalink] [raw]
Subject: [PATCH 3/3] usb: typec: Use sysfs_emit_at when concatenating the string

The buffer address used in sysfs_emit should be aligned to PAGE_SIZE.
Use sysfs_emit_at instead to offset the buffer.

Fixes: a7cff92f0635 ("usb: typec: USB Power Delivery helpers for ports and partners")
Signed-off-by: Kyle Tso <[email protected]>
---
drivers/usb/typec/class.c | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/drivers/usb/typec/class.c b/drivers/usb/typec/class.c
index e7312295f8c9..9c1dbf3c00e0 100644
--- a/drivers/usb/typec/class.c
+++ b/drivers/usb/typec/class.c
@@ -1288,9 +1288,9 @@ static ssize_t select_usb_power_delivery_show(struct device *dev,

for (i = 0; pds[i]; i++) {
if (pds[i] == port->pd)
- ret += sysfs_emit(buf + ret, "[%s] ", dev_name(&pds[i]->dev));
+ ret += sysfs_emit_at(buf, ret, "[%s] ", dev_name(&pds[i]->dev));
else
- ret += sysfs_emit(buf + ret, "%s ", dev_name(&pds[i]->dev));
+ ret += sysfs_emit_at(buf, ret, "%s ", dev_name(&pds[i]->dev));
}

buf[ret - 1] = '\n';
--
2.41.0.162.gfafddb0af9-goog



2023-06-23 08:22:45

by Greg Kroah-Hartman

[permalink] [raw]
Subject: Re: [PATCH 3/3] usb: typec: Use sysfs_emit_at when concatenating the string

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

2023-06-23 10:19:37

by Kyle Tso

[permalink] [raw]
Subject: Re: [PATCH 3/3] usb: typec: Use sysfs_emit_at when concatenating the string

On Fri, Jun 23, 2023 at 3:51 PM Greg KH <[email protected]> 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

2023-06-23 10:37:43

by Kyle Tso

[permalink] [raw]
Subject: Re: [PATCH 3/3] usb: typec: Use sysfs_emit_at when concatenating the string

On Fri, Jun 23, 2023 at 6:06 PM Kyle Tso <[email protected]> wrote:
>
> On Fri, Jun 23, 2023 at 3:51 PM Greg KH <[email protected]> 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

2023-06-23 11:33:57

by Greg Kroah-Hartman

[permalink] [raw]
Subject: Re: [PATCH 3/3] usb: typec: Use sysfs_emit_at when concatenating the string

On Fri, Jun 23, 2023 at 06:11:00PM +0800, Kyle Tso wrote:
> On Fri, Jun 23, 2023 at 6:06 PM Kyle Tso <[email protected]> wrote:
> >
> > On Fri, Jun 23, 2023 at 3:51 PM Greg KH <[email protected]> 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 :)

2023-06-23 14:37:45

by Kyle Tso

[permalink] [raw]
Subject: Re: [PATCH 3/3] usb: typec: Use sysfs_emit_at when concatenating the string

On Fri, Jun 23, 2023 at 7:14 PM Greg KH <[email protected]> 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 <[email protected]> wrote:
> > >
> > > On Fri, Jun 23, 2023 at 3:51 PM Greg KH <[email protected]> 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