Skip to contentSkip to conversation

Some fixes for select_usb_power_delivery

PATCH Superseded Kyle Tso 2 participants 11 messages Jun 22, 2023 – Jun 23, 2023

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Kyle Tso

author
#0 lore raw

cover Some fixes for select_usb_power_delivery

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(-)
Signature · 1 line
2.41.0.162.gfafddb0af9-goog
author
#1 lore raw

PATCH 1/3 usb: typec: Set port->pd before adding device for typec_port

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")
---
 drivers/usb/typec/class.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/usb/typec/class.c +3 -1
index faa184ae3dac..3b30948bf4b0 100644
@@ -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);
Signature · 1 line
2.41.0.162.gfafddb0af9-goog
maintainer
#2 lore raw

Re: [PATCH 1/3] usb: typec: Set port->pd before adding device for typec_port

Quoted 10 lines · 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")

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

thanks,

greg k-h

author
#3 lore raw

Re: [PATCH 1/3] usb: typec: Set port->pd before adding device for typec_port

Quoted 10 lines · Greg KH
Quoted 8 lines · 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.

Quoted 8 lines
Quoted 2 lines
Fixes a7cff92f0635 ("usb: typec: USB Power Delivery helpers for ports and partners")

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

thanks,

greg k-h

Will do in v2

Kyle

author
#4 lore raw

PATCH 2/3 usb: typec: Iterate pds array when showing the pd list

In reply to 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")
---
 drivers/usb/typec/class.c | 11 +++++------
 1 file changed, 5 insertions(+), 6 deletions(-)
drivers/usb/typec/class.c +5 -6
index 3b30948bf4b0..e7312295f8c9 100644
@@ -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';
Signature · 1 line
2.41.0.162.gfafddb0af9-goog
author
#5 lore raw

PATCH 3/3 usb: typec: Use sysfs_emit_at when concatenating the string

In reply to 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")
---
 drivers/usb/typec/class.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/usb/typec/class.c +2 -2
index e7312295f8c9..9c1dbf3c00e0 100644
@@ -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';
Signature · 1 line
2.41.0.162.gfafddb0af9-goog
maintainer
#6 lore raw

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

Quoted 1 line · Kyle Tso

The buffer address used in sysfs_emit should be aligned to PAGE_SIZE.

Why? Are you getting warnings about this?

thanks,

greg k-h

author
#7 lore raw

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

Quoted 9 lines · Greg KH
Quoted 1 line · Kyle Tso

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

author
#8 lore raw

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

Quoted 33 lines · Kyle Tso
Quoted 9 lines · Greg KH
Quoted 1 line · Kyle Tso

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
Quoted 7 lines

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

maintainer
#9 lore raw

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

Quoted 37 lines · Kyle Tso
Quoted 36 lines · Kyle Tso
Quoted 9 lines · Greg KH
Quoted 1 line · Kyle Tso

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

Quoted 6 lines
Quoted 4 lines

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

author
#10 lore raw

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

Quoted 39 lines · Greg KH
Quoted 37 lines · Kyle Tso
Quoted 36 lines · Kyle Tso
Quoted 9 lines · Greg KH
Quoted 1 line · Kyle Tso

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

Quoted 8 lines
Quoted 6 lines
Quoted 4 lines

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