public inbox for linux-usb@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH 0/3] Some fixes for select_usb_power_delivery
@ 2023-06-22 15:04 Kyle Tso
  2023-06-22 15:04 ` [PATCH 1/3] usb: typec: Set port->pd before adding device for typec_port Kyle Tso
                   ` (2 more replies)
  0 siblings, 3 replies; 11+ messages in thread
From: Kyle Tso @ 2023-06-22 15:04 UTC (permalink / raw)
  To: heikki.krogerus, gregkh; +Cc: badhri, linux-usb, linux-kernel, Kyle Tso

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(-)

-- 
2.41.0.162.gfafddb0af9-goog


^ permalink raw reply	[flat|nested] 11+ messages in thread

* [PATCH 1/3] usb: typec: Set port->pd before adding device for typec_port
  2023-06-22 15:04 [PATCH 0/3] Some fixes for select_usb_power_delivery Kyle Tso
@ 2023-06-22 15:04 ` Kyle Tso
  2023-06-23  7:50   ` Greg KH
  2023-06-22 15:04 ` [PATCH 2/3] usb: typec: Iterate pds array when showing the pd list Kyle Tso
  2023-06-22 15:04 ` [PATCH 3/3] usb: typec: Use sysfs_emit_at when concatenating the string Kyle Tso
  2 siblings, 1 reply; 11+ messages in thread
From: Kyle Tso @ 2023-06-22 15:04 UTC (permalink / raw)
  To: heikki.krogerus, gregkh; +Cc: badhri, linux-usb, linux-kernel, Kyle Tso

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>
---
 drivers/usb/typec/class.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/drivers/usb/typec/class.c b/drivers/usb/typec/class.c
index faa184ae3dac..3b30948bf4b0 100644
--- a/drivers/usb/typec/class.c
+++ b/drivers/usb/typec/class.c
@@ -2288,6 +2288,8 @@ struct typec_port *typec_register_port(struct device *parent,
 		return ERR_PTR(ret);
 	}
 
+	port->pd = cap->pd;
+
 	ret = device_add(&port->dev);
 	if (ret) {
 		dev_err(parent, "failed to register port (%d)\n", ret);
@@ -2295,7 +2297,7 @@ struct typec_port *typec_register_port(struct device *parent,
 		return ERR_PTR(ret);
 	}
 
-	ret = typec_port_set_usb_power_delivery(port, cap->pd);
+	ret = usb_power_delivery_link_device(port->pd, &port->dev);
 	if (ret) {
 		dev_err(&port->dev, "failed to link pd\n");
 		device_unregister(&port->dev);
-- 
2.41.0.162.gfafddb0af9-goog


^ permalink raw reply related	[flat|nested] 11+ messages in thread

* [PATCH 2/3] usb: typec: Iterate pds array when showing the pd list
  2023-06-22 15:04 [PATCH 0/3] Some fixes for select_usb_power_delivery Kyle Tso
  2023-06-22 15:04 ` [PATCH 1/3] usb: typec: Set port->pd before adding device for typec_port Kyle Tso
@ 2023-06-22 15:04 ` Kyle Tso
  2023-06-22 15:04 ` [PATCH 3/3] usb: typec: Use sysfs_emit_at when concatenating the string Kyle Tso
  2 siblings, 0 replies; 11+ messages in thread
From: Kyle Tso @ 2023-06-22 15:04 UTC (permalink / raw)
  To: heikki.krogerus, gregkh; +Cc: badhri, linux-usb, linux-kernel, Kyle Tso

The pointers of each usb_power_delivery handles are stored in "pds"
array returned from the pd_get ops but not in the adjacent memory
calculated from "pd". Get the handles from "pds" array directly instead
of deriving them from "pd".

Fixes: a7cff92f0635 ("usb: typec: USB Power Delivery helpers for ports and partners")
Signed-off-by: Kyle Tso <kyletso@google.com>
---
 drivers/usb/typec/class.c | 11 +++++------
 1 file changed, 5 insertions(+), 6 deletions(-)

diff --git a/drivers/usb/typec/class.c b/drivers/usb/typec/class.c
index 3b30948bf4b0..e7312295f8c9 100644
--- a/drivers/usb/typec/class.c
+++ b/drivers/usb/typec/class.c
@@ -1277,8 +1277,7 @@ static ssize_t select_usb_power_delivery_show(struct device *dev,
 {
 	struct typec_port *port = to_typec_port(dev);
 	struct usb_power_delivery **pds;
-	struct usb_power_delivery *pd;
-	int ret = 0;
+	int i, ret = 0;
 
 	if (!port->ops || !port->ops->pd_get)
 		return -EOPNOTSUPP;
@@ -1287,11 +1286,11 @@ static ssize_t select_usb_power_delivery_show(struct device *dev,
 	if (!pds)
 		return 0;
 
-	for (pd = pds[0]; pd; pd++) {
-		if (pd == port->pd)
-			ret += sysfs_emit(buf + ret, "[%s] ", dev_name(&pd->dev));
+	for (i = 0; pds[i]; i++) {
+		if (pds[i] == port->pd)
+			ret += sysfs_emit(buf + ret, "[%s] ", dev_name(&pds[i]->dev));
 		else
-			ret += sysfs_emit(buf + ret, "%s ", dev_name(&pd->dev));
+			ret += sysfs_emit(buf + ret, "%s ", dev_name(&pds[i]->dev));
 	}
 
 	buf[ret - 1] = '\n';
-- 
2.41.0.162.gfafddb0af9-goog


^ permalink raw reply related	[flat|nested] 11+ messages in thread

* [PATCH 3/3] usb: typec: Use sysfs_emit_at when concatenating the string
  2023-06-22 15:04 [PATCH 0/3] Some fixes for select_usb_power_delivery Kyle Tso
  2023-06-22 15:04 ` [PATCH 1/3] usb: typec: Set port->pd before adding device for typec_port Kyle Tso
  2023-06-22 15:04 ` [PATCH 2/3] usb: typec: Iterate pds array when showing the pd list Kyle Tso
@ 2023-06-22 15:04 ` Kyle Tso
  2023-06-23  7:51   ` Greg KH
  2 siblings, 1 reply; 11+ messages in thread
From: Kyle Tso @ 2023-06-22 15:04 UTC (permalink / raw)
  To: heikki.krogerus, gregkh; +Cc: badhri, linux-usb, linux-kernel, Kyle Tso

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 <kyletso@google.com>
---
 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


^ permalink raw reply related	[flat|nested] 11+ messages in thread

* Re: [PATCH 1/3] usb: typec: Set port->pd before adding device for typec_port
  2023-06-22 15:04 ` [PATCH 1/3] usb: typec: Set port->pd before adding device for typec_port Kyle Tso
@ 2023-06-23  7:50   ` Greg KH
  2023-06-23 10:08     ` Kyle Tso
  0 siblings, 1 reply; 11+ messages in thread
From: Greg KH @ 2023-06-23  7:50 UTC (permalink / raw)
  To: Kyle Tso; +Cc: heikki.krogerus, badhri, linux-usb, linux-kernel

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

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH 3/3] usb: typec: Use sysfs_emit_at when concatenating the string
  2023-06-22 15:04 ` [PATCH 3/3] usb: typec: Use sysfs_emit_at when concatenating the string Kyle Tso
@ 2023-06-23  7:51   ` Greg KH
  2023-06-23 10:06     ` Kyle Tso
  0 siblings, 1 reply; 11+ messages in thread
From: Greg KH @ 2023-06-23  7:51 UTC (permalink / raw)
  To: Kyle Tso; +Cc: heikki.krogerus, badhri, linux-usb, linux-kernel

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

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH 3/3] usb: typec: Use sysfs_emit_at when concatenating the string
  2023-06-23  7:51   ` Greg KH
@ 2023-06-23 10:06     ` Kyle Tso
  2023-06-23 10:11       ` Kyle Tso
  0 siblings, 1 reply; 11+ messages in thread
From: Kyle Tso @ 2023-06-23 10:06 UTC (permalink / raw)
  To: Greg KH; +Cc: heikki.krogerus, badhri, linux-usb, linux-kernel

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

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH 1/3] usb: typec: Set port->pd before adding device for typec_port
  2023-06-23  7:50   ` Greg KH
@ 2023-06-23 10:08     ` Kyle Tso
  0 siblings, 0 replies; 11+ messages in thread
From: Kyle Tso @ 2023-06-23 10:08 UTC (permalink / raw)
  To: Greg KH; +Cc: heikki.krogerus, badhri, linux-usb, linux-kernel

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

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH 3/3] usb: typec: Use sysfs_emit_at when concatenating the string
  2023-06-23 10:06     ` Kyle Tso
@ 2023-06-23 10:11       ` Kyle Tso
  2023-06-23 11:14         ` Greg KH
  0 siblings, 1 reply; 11+ messages in thread
From: Kyle Tso @ 2023-06-23 10:11 UTC (permalink / raw)
  To: Greg KH; +Cc: heikki.krogerus, badhri, linux-usb, linux-kernel

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

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH 3/3] usb: typec: Use sysfs_emit_at when concatenating the string
  2023-06-23 10:11       ` Kyle Tso
@ 2023-06-23 11:14         ` Greg KH
  2023-06-23 14:23           ` Kyle Tso
  0 siblings, 1 reply; 11+ messages in thread
From: Greg KH @ 2023-06-23 11:14 UTC (permalink / raw)
  To: Kyle Tso; +Cc: heikki.krogerus, badhri, linux-usb, linux-kernel

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 :)

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH 3/3] usb: typec: Use sysfs_emit_at when concatenating the string
  2023-06-23 11:14         ` Greg KH
@ 2023-06-23 14:23           ` Kyle Tso
  0 siblings, 0 replies; 11+ messages in thread
From: Kyle Tso @ 2023-06-23 14:23 UTC (permalink / raw)
  To: Greg KH; +Cc: heikki.krogerus, badhri, linux-usb, linux-kernel

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

^ permalink raw reply	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2023-06-23 14:23 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-06-22 15:04 [PATCH 0/3] Some fixes for select_usb_power_delivery Kyle Tso
2023-06-22 15:04 ` [PATCH 1/3] usb: typec: Set port->pd before adding device for typec_port Kyle Tso
2023-06-23  7:50   ` Greg KH
2023-06-23 10:08     ` Kyle Tso
2023-06-22 15:04 ` [PATCH 2/3] usb: typec: Iterate pds array when showing the pd list Kyle Tso
2023-06-22 15:04 ` [PATCH 3/3] usb: typec: Use sysfs_emit_at when concatenating the string Kyle Tso
2023-06-23  7:51   ` Greg KH
2023-06-23 10:06     ` Kyle Tso
2023-06-23 10:11       ` Kyle Tso
2023-06-23 11:14         ` Greg KH
2023-06-23 14:23           ` Kyle Tso

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox