Skip to contentSkip to conversation

usb: typec: tcpm: IS_ERR_OR_NULL check for port->partner

PATCH Superseded Jimmy Hu 3 participants 3 messages Aug 4, 2023

Subsystems (3)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Jimmy Hu

author
#0 lore raw

PATCH v2 1/1 usb: typec: tcpm: IS_ERR_OR_NULL check for port->partner

port->partner may be an error or NULL, so we must check it with IS_ERR_OR_NULL() before dereferencing it.

Move the check to the beginning of the tcpm_handle_vdm_request function.

Fixes 5e1d4c49fbc8 ("usb: typec: tcpm: Determine common SVDM Version")
---
 drivers/usb/typec/tcpm/tcpm.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)
drivers/usb/typec/tcpm/tcpm.c +6 -4
index 829d75ebab42..2c6a0af155ab 100644
@@ -1683,10 +1683,6 @@ static int tcpm_pd_svdm(struct tcpm_port *port, struct typec_altmode *adev,
 			      (VDO_SVDM_VERS(typec_get_negotiated_svdm_version(typec)));
 		break;
 	case CMDT_RSP_ACK:
-		/* silently drop message if we are not connected */
-		if (IS_ERR_OR_NULL(port->partner))
-			break;
-
 		tcpm_ams_finish(port);
 
 		switch (cmd) {
@@ -1792,6 +1788,12 @@ static void tcpm_handle_vdm_request(struct tcpm_port *port,
 	u32 response[8] = { };
 	int i, rlen = 0;
 
+	/* silently drop message if we are not connected */
+	if (IS_ERR_OR_NULL(port->partner)) {
+		dev_warn(port->dev, "port partner is an error or NULL\n");
+		return;
+	}
+
 	for (i = 0; i < cnt; i++)
 		p[i] = le32_to_cpu(payload[i]);
 
Signature · 1 line
2.41.0.585.gd2178a4bd4-goog
#1 lore raw

Hi Jimmy,

Quoted 27 lines · Jimmy Hu

port->partner may be an error or NULL, so we must check it with IS_ERR_OR_NULL() before dereferencing it.

Move the check to the beginning of the tcpm_handle_vdm_request function.

Fixes 5e1d4c49fbc8 ("usb: typec: tcpm: Determine common SVDM Version")
---
 drivers/usb/typec/tcpm/tcpm.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)
drivers/usb/typec/tcpm/tcpm.c +0 -4
index 829d75ebab42..2c6a0af155ab 100644
@@ -1683,10 +1683,6 @@ static int tcpm_pd_svdm(struct tcpm_port *port, struct typec_altmode *adev,
 			      (VDO_SVDM_VERS(typec_get_negotiated_svdm_version(typec)));
 		break;
 	case CMDT_RSP_ACK:
-		/* silently drop message if we are not connected */
-		if (IS_ERR_OR_NULL(port->partner))
-			break;
-
 		tcpm_ams_finish(port);
 
 		switch (cmd) {

drivers/usb/typec/tcpm/tcpm.c static void tcpm_handle_vdm_request(struct tcpm_port *port, · line 1791

Quoted 4 lines
drivers/usb/typec/tcpm/tcpm.c +1 -0
@@ -1792,6 +1788,12 @@ static void tcpm_handle_vdm_request(struct tcpm_port *port,
 	u32 response[8] = { };
 	int i, rlen = 0;
 
+	/* silently drop message if we are not connected */

...comment moved.

drivers/usb/typec/tcpm/tcpm.c static void tcpm_handle_vdm_request(struct tcpm_port *port, · line 1793

Quoted 2 lines
drivers/usb/typec/tcpm/tcpm.c +2 -0
+	if (IS_ERR_OR_NULL(port->partner)) {
+		dev_warn(port->dev, "port partner is an error or NULL\n");

But code is actually not silent. Also, does the verbsity make sense? And if it does, is knowing what error port->partner is containing usefull?

Quoted 8 lines
drivers/usb/typec/tcpm/tcpm.c +3 -0
+		return;
+	}
+
 	for (i = 0; i < cnt; i++)
 		p[i] = le32_to_cpu(payload[i]);
 
Signature · 1 line
2.41.0.585.gd2178a4bd4-goog
#2 lore raw
Quoted 37 lines · Ladislav Michl

Hi Jimmy,

Quoted 27 lines · Jimmy Hu

port->partner may be an error or NULL, so we must check it with IS_ERR_OR_NULL() before dereferencing it.

Move the check to the beginning of the tcpm_handle_vdm_request function.

Fixes 5e1d4c49fbc8 ("usb: typec: tcpm: Determine common SVDM Version")
---
  drivers/usb/typec/tcpm/tcpm.c | 10 ++++++----
  1 file changed, 6 insertions(+), 4 deletions(-)
drivers/usb/typec/tcpm/tcpm.c +0 -4
index 829d75ebab42..2c6a0af155ab 100644
@@ -1683,10 +1683,6 @@ static int tcpm_pd_svdm(struct tcpm_port *port, struct typec_altmode *adev,
  			      (VDO_SVDM_VERS(typec_get_negotiated_svdm_version(typec)));
  		break;
  	case CMDT_RSP_ACK:
-		/* silently drop message if we are not connected */
-		if (IS_ERR_OR_NULL(port->partner))
-			break;
-
  		tcpm_ams_finish(port);
 
  		switch (cmd) {

drivers/usb/typec/tcpm/tcpm.c static void tcpm_handle_vdm_request(struct tcpm_port *port, · line 1791

Quoted 4 lines
drivers/usb/typec/tcpm/tcpm.c +1 -0
@@ -1792,6 +1788,12 @@ static void tcpm_handle_vdm_request(struct tcpm_port *port,
  	u32 response[8] = { };
  	int i, rlen = 0;
 
+	/* silently drop message if we are not connected */

...comment moved.

Quoted 6 lines

drivers/usb/typec/tcpm/tcpm.c static void tcpm_handle_vdm_request(struct tcpm_port *port, · line 1793

Quoted 2 lines
drivers/usb/typec/tcpm/tcpm.c +2 -0
+	if (IS_ERR_OR_NULL(port->partner)) {
+		dev_warn(port->dev, "port partner is an error or NULL\n");

But code is actually not silent. Also, does the verbsity make sense? And if it does, is knowing what error port->partner is containing usefull?

Not only that, it also changes behavior radically from ignoring certain messages while not connected or after partner registration failed to rejecting all VDM messages, even those not requiring any partner data. It now also ignores all VDM messages if the state machine is not in READY state. This is a significant functional change which needs _much_ more explanation than provided in the patch description.

Guenter