HID: ft260: improve error handling of ft260_hid_feature_report_get()
Michael Zaidman
maintainerPATCH v4 1/1 HID: ft260: improve error handling of ft260_hid_feature_report_get()
The ft260_hid_feature_report_get() checks if the return size matches the requested size. But the function can also fail with at least -ENOMEM. Add the < 0 checks.
In ft260_hid_feature_report_get(), do not do the memcpy to the caller's buffer if there is an error.
---
v4 Fixed commit message
---
v3 Simplify and optimize the changes
---
v2: add unlikely()'s for error conditions
---
--- drivers/hid/hid-ft260.c | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-)
drivers/hid/hid-ft260.c +12 -12
@@ -249,7 +249,10 @@ static int ft260_hid_feature_report_get(struct hid_device *hdev, ret =hid_hw_raw_request (hdev, report_id, buf, len,HID_FEATURE_REPORT ,HID_REQ_GET_REPORT ); -memcpy (data, buf, len); +if (likely (ret == len)) +memcpy (data, buf, len); +else if (ret >=0 ) + ret = -EIO ;kfree (buf);return ret; }
@@ -298,7 +301,7 @@ static int ft260_xfer_status(struct ft260_device *dev) ret =ft260_hid_feature_report_get (hdev,FT260_I2C_STATUS , (u8 *)&report,sizeof (report)); -if (ret <0 ) { +if (unlikely (ret <0 )) {hid_err (hdev,"failed to retrieve status: %d\n" , ret);return ret; }
@@ -720,10 +723,9 @@ static int ft260_get_system_config(struct hid_device *hdev, ret =ft260_hid_feature_report_get (hdev,FT260_SYSTEM_SETTINGS , (u8 *)cfg, len); -if (ret != len) { +if (ret <0 ) {hid_err (hdev,"failed to retrieve system status\n" ); -if (ret >=0 ) -return -EIO ; +return ret; }return 0 ; }
@@ -776,8 +778,8 @@ static int ft260_byte_show(struct hid_device *hdev, int id, u8 *cfg, int len,int ret; ret =ft260_hid_feature_report_get (hdev, id, cfg, len); -if (ret != len && ret >=0 ) -return -EIO ; +if (ret <0 ) +return ret;return scnprintf (buf,PAGE_SIZE ,"%hi\n" , *field); }
@@ -788,8 +790,8 @@ static int ft260_word_show(struct hid_device *hdev, int id, u8 *cfg, int len,int ret; ret =ft260_hid_feature_report_get (hdev, id, cfg, len); -if (ret != len && ret >=0 ) -return -EIO ; +if (ret <0 ) +return ret;return scnprintf (buf,PAGE_SIZE ,"%hi\n" ,le16_to_cpu (*field)); }
@@ -940,10 +942,8 @@ static int ft260_probe(struct hid_device *hdev, const struct hid_device_id *id) ret =ft260_hid_feature_report_get (hdev,FT260_CHIP_VERSION , (u8 *)&version,sizeof (version)); -if (ret !=sizeof (version)) { +if (ret <0 ) {hid_err (hdev,"failed to retrieve chip version\n" ); -if (ret >=0 ) - ret = -EIO ; goto err_hid_close; }
Signature · 1 line
2.25.1
Generally change is fine.
a nit below.
Quoted 49 lines · Michael Zaidman
Fixes 6a82582d9fa4 ("HID: ft260: add usb hid to i2c host bridge driver")The
ft260_hid_feature_report_get()checks if the return size matches the requested size. But the function can also fail with at least-ENOMEM. Add the < 0 checks.In
ft260_hid_feature_report_get(), do not do the memcpy to the caller's buffer if there is an error.---
v4 Fixed commit message
---
v3 Simplify and optimize the changes
---
v2: add unlikely()'s for error conditions
---Signed-off-by Tom Rix <[email protected]>Signed-off-by Michael Zaidman <[email protected]>--- drivers/hid/hid-ft260.c | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-)drivers/hid/hid-ft260.c +5
-2@@ -249,7 +249,10 @@ static int ft260_hid_feature_report_get(struct hid_device *hdev, ret =hid_hw_raw_request (hdev, report_id, buf, len,HID_FEATURE_REPORT ,HID_REQ_GET_REPORT ); -memcpy (data, buf, len); +if (likely (ret == len)) +memcpy (data, buf, len); +else if (ret >=0 ) + ret = -EIO ;kfree (buf);return ret; }@@ -298,7 +301,7 @@ static int ft260_xfer_status(struct ft260_device *dev) ret =ft260_hid_feature_report_get (hdev,FT260_I2C_STATUS , (u8 *)&report,sizeof (report)); -if (ret <0 ) { +if (unlikely (ret <0 )) {hid_err (hdev,"failed to retrieve status: %d\n" , ret);return ret; }
drivers/hid/hid-ft260.c static int ft260_get_system_config(struct hid_device *hdev, · line 726
Quoted 5 lines
drivers/hid/hid-ft260.c +1
-1@@ -720,10 +723,9 @@ static int ft260_get_system_config(struct hid_device *hdev, ret =ft260_hid_feature_report_get (hdev,FT260_SYSTEM_SETTINGS , (u8 *)cfg, len); -if (ret != len) { +if (ret <0 ) {
nit: should be consistent and use unlikely(ret < 0) for this and other similar checks.
Tom
Quoted 41 lines
drivers/hid/hid-ft260.c +6
-9hid_err (hdev,"failed to retrieve system status\n" ); -if (ret >=0 ) -return -EIO ; +return ret; }return 0 ; }@@ -776,8 +778,8 @@ static int ft260_byte_show(struct hid_device *hdev, int id, u8 *cfg, int len,int ret; ret =ft260_hid_feature_report_get (hdev, id, cfg, len); -if (ret != len && ret >=0 ) -return -EIO ; +if (ret <0 ) +return ret;return scnprintf (buf,PAGE_SIZE ,"%hi\n" , *field); }@@ -788,8 +790,8 @@ static int ft260_word_show(struct hid_device *hdev, int id, u8 *cfg, int len,int ret; ret =ft260_hid_feature_report_get (hdev, id, cfg, len); -if (ret != len && ret >=0 ) -return -EIO ; +if (ret <0 ) +return ret;return scnprintf (buf,PAGE_SIZE ,"%hi\n" ,le16_to_cpu (*field)); }@@ -940,10 +942,8 @@ static int ft260_probe(struct hid_device *hdev, const struct hid_device_id *id) ret =ft260_hid_feature_report_get (hdev,FT260_CHIP_VERSION , (u8 *)&version,sizeof (version)); -if (ret !=sizeof (version)) { +if (ret <0 ) {hid_err (hdev,"failed to retrieve chip version\n" ); -if (ret >=0 ) - ret = -EIO ; goto err_hid_close; }
Michael Zaidman
maintainerQuoted 60 lines · Tom Rix
Generally change is fine.
a nit below.
Quoted 47 lines · Michael Zaidman
Fixes 6a82582d9fa4 ("HID: ft260: add usb hid to i2c host bridge driver")The
ft260_hid_feature_report_get()checks if the return size matches the requested size. But the function can also fail with at least-ENOMEM. Add the < 0 checks.In
ft260_hid_feature_report_get(), do not do the memcpy to the caller's buffer if there is an error.---
v4 Fixed commit message
---
v3 Simplify and optimize the changes
---
v2: add unlikely()'s for error conditions
---Signed-off-by Tom Rix <[email protected]>Signed-off-by Michael Zaidman <[email protected]>--- drivers/hid/hid-ft260.c | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-)drivers/hid/hid-ft260.c +5
-2@@ -249,7 +249,10 @@ static int ft260_hid_feature_report_get(struct hid_device *hdev, ret =hid_hw_raw_request (hdev, report_id, buf, len,HID_FEATURE_REPORT ,HID_REQ_GET_REPORT ); -memcpy (data, buf, len); +if (likely (ret == len)) +memcpy (data, buf, len); +else if (ret >=0 ) + ret = -EIO ;kfree (buf);return ret; }@@ -298,7 +301,7 @@ static int ft260_xfer_status(struct ft260_device *dev) ret =ft260_hid_feature_report_get (hdev,FT260_I2C_STATUS , (u8 *)&report,sizeof (report)); -if (ret <0 ) { +if (unlikely (ret <0 )) {hid_err (hdev,"failed to retrieve status: %d\n" , ret);return ret; }drivers/hid/hid-ft260.c static int ft260_get_system_config(struct hid_device *hdev, · line 725
Quoted 4 lines
drivers/hid/hid-ft260.c +1
-1@@ -720,10 +723,9 @@ static int ft260_get_system_config(struct hid_device *hdev, ret =ft260_hid_feature_report_get (hdev,FT260_SYSTEM_SETTINGS , (u8 *)cfg, len); -if (ret != len) { +if (ret <0 ) {nit: should be consistent and use unlikely(ret < 0) for this and other similar checks.
Quoted 1 line
Tom
I preserved the likely/unlikely hints in the critical path where the performance matters. And for the sake of consistency, I removed them from the rest of the places that are called rarely and are not performance-critical to be aligned to the other "if" statements in the code.
Michael
Quoted 60 lines · Michael Zaidman
Quoted 59 lines · Tom Rix
Generally change is fine.
a nit below.
Quoted 47 lines · Michael Zaidman
Fixes 6a82582d9fa4 ("HID: ft260: add usb hid to i2c host bridge driver")The
ft260_hid_feature_report_get()checks if the return size matches the requested size. But the function can also fail with at least-ENOMEM. Add the < 0 checks.In
ft260_hid_feature_report_get(), do not do the memcpy to the caller's buffer if there is an error.---
v4 Fixed commit message
---
v3 Simplify and optimize the changes
---
v2: add unlikely()'s for error conditions
---Signed-off-by Tom Rix <[email protected]>Signed-off-by Michael Zaidman <[email protected]>--- drivers/hid/hid-ft260.c | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-)drivers/hid/hid-ft260.c +5
-2@@ -249,7 +249,10 @@ static int ft260_hid_feature_report_get(struct hid_device *hdev, ret =hid_hw_raw_request (hdev, report_id, buf, len,HID_FEATURE_REPORT ,HID_REQ_GET_REPORT ); -memcpy (data, buf, len); +if (likely (ret == len)) +memcpy (data, buf, len); +else if (ret >=0 ) + ret = -EIO ;kfree (buf);return ret; }@@ -298,7 +301,7 @@ static int ft260_xfer_status(struct ft260_device *dev) ret =ft260_hid_feature_report_get (hdev,FT260_I2C_STATUS , (u8 *)&report,sizeof (report)); -if (ret <0 ) { +if (unlikely (ret <0 )) {hid_err (hdev,"failed to retrieve status: %d\n" , ret);return ret; }drivers/hid/hid-ft260.c static int ft260_get_system_config(struct hid_device *hdev, · line 725
Quoted 4 lines
drivers/hid/hid-ft260.c +1
-1@@ -720,10 +723,9 @@ static int ft260_get_system_config(struct hid_device *hdev, ret =ft260_hid_feature_report_get (hdev,FT260_SYSTEM_SETTINGS , (u8 *)cfg, len); -if (ret != len) { +if (ret <0 ) {nit: should be consistent and use unlikely(ret < 0) for this and other similar checks.
Quoted 5 lines
Quoted 1 line
Tom
I preserved the likely/unlikely hints in the critical path where the performance matters. And for the sake of consistency, I removed them from the rest of the places that are called rarely and are not performance-critical to be aligned to the other "if" statements in the code.
This is fine.
Thanks,
Tom
Quoted 3 lines
Michael
Jiri Kosina
maintainerQuoted 17 lines · Michael Zaidman
Fixes 6a82582d9fa4 ("HID: ft260: add usb hid to i2c host bridge driver")The
ft260_hid_feature_report_get()checks if the return size matches the requested size. But the function can also fail with at least-ENOMEM. Add the < 0 checks.In
ft260_hid_feature_report_get(), do not do the memcpy to the caller's buffer if there is an error.---
v4 Fixed commit message
---
v3 Simplify and optimize the changes
---
v2: add unlikely()'s for error conditions
---
Quoted 2 lines
Signed-off-by Tom Rix <[email protected]>Signed-off-by Michael Zaidman <[email protected]>
Who should be the author of the git commit?
Thanks,
Signature · 2 lines
Jiri Kosina SUSE Labs
Quoted 19 lines · Jiri Kosina
Quoted 17 lines · Michael Zaidman
Fixes 6a82582d9fa4 ("HID: ft260: add usb hid to i2c host bridge driver")The
ft260_hid_feature_report_get()checks if the return size matches the requested size. But the function can also fail with at least-ENOMEM. Add the < 0 checks.In
ft260_hid_feature_report_get(), do not do the memcpy to the caller's buffer if there is an error.---
v4 Fixed commit message
---
v3 Simplify and optimize the changes
---
v2: add unlikely()'s for error conditions
---
Quoted 3 lines
Quoted 2 lines
Signed-off-by Tom Rix <[email protected]>Signed-off-by Michael Zaidman <[email protected]>Who should be the author of the git commit?
Go with the latest patch's author, Micheal.
Tom
Quoted 3 lines
Thanks,