platform/chrome: cros_ec_proto: Fix check_features ret val
Subsystems (2)
PATCH 1/1 platform/chrome: cros_ec_proto: Fix check_features ret val
The kerneldoc for cros_ec_check_features() states that it returns 1 or 0 depedending on whether a feature is supported or not, but it instead returns a negative error number in one case, and a non-1 bitmask in other cases.
Since all call-sites only check for a 1 or 0 return value, update the function to return boolean values.
--- drivers/platform/chrome/cros_ec_proto.c | 12 +++++++----- include/linux/platform_data/cros_ec_proto.h | 2 +- 2 files changed, 8 insertions(+), 6 deletions(-)
drivers/platform/chrome/cros_ec_proto.c +7 -5
@@ -808,9 +808,9 @@ EXPORT_SYMBOL(cros_ec_get_host_event);* * Call this function to test whether the ChromeOS EC supports a feature. * -* Return: 1 if supported, 0 if not +* Return: true if supported, false if not (or if an error was encountered). */ -int cros_ec_check_features (struct cros_ec_dev *ec,int feature) +bool cros_ec_check_features (struct cros_ec_dev *ec,int feature) {struct cros_ec_command *msg;int ret;
@@ -818,8 +818,10 @@ int cros_ec_check_features(struct cros_ec_dev *ec, int feature)if (ec->features[0 ] ==-1U && ec->features[1 ] ==-1U ) {/* features bitmap not read yet */ msg =kzalloc (sizeof (*msg) +sizeof (ec->features),GFP_KERNEL ); -if (!msg) -return -ENOMEM ; +if (!msg) { +dev_err (ec->dev,"failed to allocate memory to get EC features\n" ); +return false; + } msg->command =EC_CMD_GET_FEATURES + ec->cmd_offset; msg->insize =sizeof (ec->features);
@@ -839,7 +841,7 @@ int cros_ec_check_features(struct cros_ec_dev *ec, int feature)kfree (msg); } -return ec->features[feature /32 ] &EC_FEATURE_MASK_0 (feature); +return !!(ec->features[feature /32 ] &EC_FEATURE_MASK_0 (feature)); }EXPORT_SYMBOL_GPL (cros_ec_check_features);
include/linux/platform_data/cros_ec_proto.h +1 -1
@@ -227,7 +227,7 @@ int cros_ec_get_next_event(struct cros_ec_device *ec_dev,u32 cros_ec_get_host_event (struct cros_ec_device *ec_dev); -int cros_ec_check_features (struct cros_ec_dev *ec,int feature); +bool cros_ec_check_features (struct cros_ec_dev *ec,int feature);int cros_ec_get_sensor_count (struct cros_ec_dev *ec);
Signature · 1 line
2.33.0.309.g3052b89438-goog
Quoted 10 lines · Prashant Malani
The kerneldoc for
cros_ec_check_features()states that it returns 1 or 0 depedending on whether a feature is supported or not, but it instead returns a negative error number in one case, and a non-1 bitmask in other cases.Since all call-sites only check for a 1 or 0 return value, update the function to return boolean values.
Signed-off-by Prashant Malani <[email protected]>
Quoted 59 lines
--- drivers/platform/chrome/cros_ec_proto.c | 12 +++++++----- include/linux/platform_data/cros_ec_proto.h | 2 +- 2 files changed, 8 insertions(+), 6 deletions(-)drivers/platform/chrome/cros_ec_proto.c +7
-5@@ -808,9 +808,9 @@ EXPORT_SYMBOL(cros_ec_get_host_event);* * Call this function to test whether the ChromeOS EC supports a feature. * -* Return: 1 if supported, 0 if not +* Return: true if supported, false if not (or if an error was encountered). */ -int cros_ec_check_features (struct cros_ec_dev *ec,int feature) +bool cros_ec_check_features (struct cros_ec_dev *ec,int feature) {struct cros_ec_command *msg;int ret;@@ -818,8 +818,10 @@ int cros_ec_check_features(struct cros_ec_dev *ec, int feature)if (ec->features[0 ] ==-1U && ec->features[1 ] ==-1U ) {/* features bitmap not read yet */ msg =kzalloc (sizeof (*msg) +sizeof (ec->features),GFP_KERNEL ); -if (!msg) -return -ENOMEM ; +if (!msg) { +dev_err (ec->dev,"failed to allocate memory to get EC features\n" ); +return false; + } msg->command =EC_CMD_GET_FEATURES + ec->cmd_offset; msg->insize =sizeof (ec->features);@@ -839,7 +841,7 @@ int cros_ec_check_features(struct cros_ec_dev *ec, int feature)kfree (msg); } -return ec->features[feature /32 ] &EC_FEATURE_MASK_0 (feature); +return !!(ec->features[feature /32 ] &EC_FEATURE_MASK_0 (feature)); }EXPORT_SYMBOL_GPL (cros_ec_check_features);include/linux/platform_data/cros_ec_proto.h +1
-2@@ -227,7 +227,7 @@ int cros_ec_get_next_event(struct cros_ec_device *ec_dev,u32 cros_ec_get_host_event (struct cros_ec_device *ec_dev); -int cros_ec_check_features (struct cros_ec_dev *ec,int feature); +bool cros_ec_check_features (struct cros_ec_dev *ec,int feature);int cros_ec_get_sensor_count (struct cros_ec_dev *ec); --2.33.0.309.g3052b89438-goog
Hi Prashant,
Thank you for the patch. Just one comment below ...
Quoted 31 lines · Prashant Malani
The kerneldoc for
cros_ec_check_features()states that it returns 1 or 0 depedending on whether a feature is supported or not, but it instead returns a negative error number in one case, and a non-1 bitmask in other cases.Since all call-sites only check for a 1 or 0 return value, update the function to return boolean values.
Signed-off-by Prashant Malani <[email protected]>--- drivers/platform/chrome/cros_ec_proto.c | 12 +++++++----- include/linux/platform_data/cros_ec_proto.h | 2 +- 2 files changed, 8 insertions(+), 6 deletions(-)drivers/platform/chrome/cros_ec_proto.c +2
-2@@ -808,9 +808,9 @@ EXPORT_SYMBOL(cros_ec_get_host_event);* * Call this function to test whether the ChromeOS EC supports a feature. * -* Return: 1 if supported, 0 if not +* Return: true if supported, false if not (or if an error was encountered). */ -int cros_ec_check_features (struct cros_ec_dev *ec,int feature) +bool cros_ec_check_features (struct cros_ec_dev *ec,int feature) {struct cros_ec_command *msg;int ret;
drivers/platform/chrome/cros_ec_proto.c int cros_ec_check_features(struct cros_ec_dev *ec, int feature) · line 822
Quoted 7 lines
drivers/platform/chrome/cros_ec_proto.c +2
-2@@ -818,8 +818,10 @@ int cros_ec_check_features(struct cros_ec_dev *ec, int feature)if (ec->features[0 ] ==-1U && ec->features[1 ] ==-1U ) {/* features bitmap not read yet */ msg =kzalloc (sizeof (*msg) +sizeof (ec->features),GFP_KERNEL ); -if (!msg) -return -ENOMEM ; +if (!msg) { +dev_err (ec->dev,"failed to allocate memory to get EC features\n" );
In case of failure you will be noticed by the allocator, prints after [k|v][m|z|c]alloc() functions are not needed, so I think you can just return false here.
Other than that the patch looks good to me.
Thanks,
Enric
Quoted 28 lines
drivers/platform/chrome/cros_ec_proto.c +3
-1+return false; + } msg->command =EC_CMD_GET_FEATURES + ec->cmd_offset; msg->insize =sizeof (ec->features);@@ -839,7 +841,7 @@ int cros_ec_check_features(struct cros_ec_dev *ec, int feature)kfree (msg); } -return ec->features[feature /32 ] &EC_FEATURE_MASK_0 (feature); +return !!(ec->features[feature /32 ] &EC_FEATURE_MASK_0 (feature)); }EXPORT_SYMBOL_GPL (cros_ec_check_features);include/linux/platform_data/cros_ec_proto.h +1
-1@@ -227,7 +227,7 @@ int cros_ec_get_next_event(struct cros_ec_device *ec_dev,u32 cros_ec_get_host_event (struct cros_ec_device *ec_dev); -int cros_ec_check_features (struct cros_ec_dev *ec,int feature); +bool cros_ec_check_features (struct cros_ec_dev *ec,int feature);int cros_ec_get_sensor_count (struct cros_ec_dev *ec);
Hi Enric,
Thanks for reviewing the patch.
Quoted 39 lines · Enric Balletbo i Serra
Hi Prashant,
Thank you for the patch. Just one comment below ...
Quoted 34 lines · Prashant Malani
The kerneldoc for
cros_ec_check_features()states that it returns 1 or 0 depedending on whether a feature is supported or not, but it instead returns a negative error number in one case, and a non-1 bitmask in other cases.Since all call-sites only check for a 1 or 0 return value, update the function to return boolean values.
Signed-off-by Prashant Malani <[email protected]>--- drivers/platform/chrome/cros_ec_proto.c | 12 +++++++----- include/linux/platform_data/cros_ec_proto.h | 2 +- 2 files changed, 8 insertions(+), 6 deletions(-)drivers/platform/chrome/cros_ec_proto.c +2
-2@@ -808,9 +808,9 @@ EXPORT_SYMBOL(cros_ec_get_host_event);* * Call this function to test whether the ChromeOS EC supports a feature. * -* Return: 1 if supported, 0 if not +* Return: true if supported, false if not (or if an error was encountered). */ -int cros_ec_check_features (struct cros_ec_dev *ec,int feature) +bool cros_ec_check_features (struct cros_ec_dev *ec,int feature) {struct cros_ec_command *msg;int ret;@@ -818,8 +818,10 @@ int cros_ec_check_features(struct cros_ec_dev *ec, int feature)if (ec->features[0 ] ==-1U && ec->features[1 ] ==-1U ) {/* features bitmap not read yet */ msg =kzalloc (sizeof (*msg) +sizeof (ec->features),GFP_KERNEL );
Quoted 9 lines
drivers/platform/chrome/cros_ec_proto.c int cros_ec_check_features(struct cros_ec_dev *ec, int feature) · line 822
Quoted 4 lines
drivers/platform/chrome/cros_ec_proto.c +2
-2-if (!msg) -return -ENOMEM ; +if (!msg) { +dev_err (ec->dev,"failed to allocate memory to get EC features\n" );In case of failure you will be noticed by the allocator, prints after [k|v][m|z|c]alloc() functions are not needed, so I think you can just return false here.
Quoted 1 line
Makes sense; I can make the change, but I had one question:
If we solely return false, how will we tell from the logs that the
allocation error message was associated with this driver? Only returning
false means the driver probe (e.g cros-ec-typec) will continue (just assuming a certain feature is not present). Wouldn't having this error message make this clear?
Best regards,
-Prashant
Hi Prashant,
Quoted 54 lines · Prashant Malani
Hi Enric,
Thanks for reviewing the patch.
Quoted 39 lines · Enric Balletbo i Serra
Hi Prashant,
Thank you for the patch. Just one comment below ...
Quoted 34 lines · Prashant Malani
The kerneldoc for
cros_ec_check_features()states that it returns 1 or 0 depedending on whether a feature is supported or not, but it instead returns a negative error number in one case, and a non-1 bitmask in other cases.Since all call-sites only check for a 1 or 0 return value, update the function to return boolean values.
Signed-off-by Prashant Malani <[email protected]>--- drivers/platform/chrome/cros_ec_proto.c | 12 +++++++----- include/linux/platform_data/cros_ec_proto.h | 2 +- 2 files changed, 8 insertions(+), 6 deletions(-)drivers/platform/chrome/cros_ec_proto.c +2
-2@@ -808,9 +808,9 @@ EXPORT_SYMBOL(cros_ec_get_host_event);* * Call this function to test whether the ChromeOS EC supports a feature. * -* Return: 1 if supported, 0 if not +* Return: true if supported, false if not (or if an error was encountered). */ -int cros_ec_check_features (struct cros_ec_dev *ec,int feature) +bool cros_ec_check_features (struct cros_ec_dev *ec,int feature) {struct cros_ec_command *msg;int ret;@@ -818,8 +818,10 @@ int cros_ec_check_features(struct cros_ec_dev *ec, int feature)if (ec->features[0 ] ==-1U && ec->features[1 ] ==-1U ) {/* features bitmap not read yet */ msg =kzalloc (sizeof (*msg) +sizeof (ec->features),GFP_KERNEL );Quoted 9 lines
drivers/platform/chrome/cros_ec_proto.c int cros_ec_check_features(struct cros_ec_dev *ec, int feature) · line 822
Quoted 4 lines
drivers/platform/chrome/cros_ec_proto.c +2
-2-if (!msg) -return -ENOMEM ; +if (!msg) { +dev_err (ec->dev,"failed to allocate memory to get EC features\n" );In case of failure you will be noticed by the allocator, prints after [k|v][m|z|c]alloc() functions are not needed, so I think you can just return false here.
Quoted 1 line
Quoted 7 lines
Makes sense; I can make the change, but I had one question:
If we solely return false, how will we tell from the logs that the
allocation error message was associated with this driver? Only returning
false means the driver probe (e.g cros-ec-typec) will continue (just assuming a certain feature is not present). Wouldn't having this error message make this clear?
So I tried to find some doc about this without luck. But I think it has been an unwritten rule that GFP_KERNEL allocations for small allocations will never fail. If you system fails to allocate that small amount of memory you probably have bigger problems to solve and the above message is not really useful, even confusing, as the focus, likely, shouldn't be in this driver to solve the problem.
Thinking a bit more about this change, and after your question, I don't really like functions not returning an error in the unlikely case that fails. On the other hand, I like this function return a bool as is a bit more clear IMO, so I'm wondering if wouldn't be better don't use dynamic memory here (I know that this is not really related to your patch)
And another thing that I detected, now that you're returning a bool is that in drivers/platform/chrome/cros_ec_typec.c there is:
typec->typec_cmd_supported = !!cros_ec_check_features(ec_dev, typec->needs_mux_ack = !!cros_ec_check_features(ec_dev,
I think that you can remove the !! now. That could be in another patch.
Thanks,
Enric
Quoted 4 lines
Best regards,
-Prashant
Hi Enric,
Quoted 66 lines · Enric Balletbo i Serra
Hi Prashant,
Quoted 54 lines · Prashant Malani
Hi Enric,
Thanks for reviewing the patch.
Quoted 39 lines · Enric Balletbo i Serra
Hi Prashant,
Thank you for the patch. Just one comment below ...
Quoted 34 lines · Prashant Malani
The kerneldoc for
cros_ec_check_features()states that it returns 1 or 0 depedending on whether a feature is supported or not, but it instead returns a negative error number in one case, and a non-1 bitmask in other cases.Since all call-sites only check for a 1 or 0 return value, update the function to return boolean values.
Signed-off-by Prashant Malani <[email protected]>--- drivers/platform/chrome/cros_ec_proto.c | 12 +++++++----- include/linux/platform_data/cros_ec_proto.h | 2 +- 2 files changed, 8 insertions(+), 6 deletions(-)drivers/platform/chrome/cros_ec_proto.c +2
-2@@ -808,9 +808,9 @@ EXPORT_SYMBOL(cros_ec_get_host_event);* * Call this function to test whether the ChromeOS EC supports a feature. * -* Return: 1 if supported, 0 if not +* Return: true if supported, false if not (or if an error was encountered). */ -int cros_ec_check_features (struct cros_ec_dev *ec,int feature) +bool cros_ec_check_features (struct cros_ec_dev *ec,int feature) {struct cros_ec_command *msg;int ret;@@ -818,8 +818,10 @@ int cros_ec_check_features(struct cros_ec_dev *ec, int feature)if (ec->features[0 ] ==-1U && ec->features[1 ] ==-1U ) {/* features bitmap not read yet */ msg =kzalloc (sizeof (*msg) +sizeof (ec->features),GFP_KERNEL );Quoted 9 lines
drivers/platform/chrome/cros_ec_proto.c int cros_ec_check_features(struct cros_ec_dev *ec, int feature) · line 822
Quoted 4 lines
drivers/platform/chrome/cros_ec_proto.c +2
-2-if (!msg) -return -ENOMEM ; +if (!msg) { +dev_err (ec->dev,"failed to allocate memory to get EC features\n" );In case of failure you will be noticed by the allocator, prints after [k|v][m|z|c]alloc() functions are not needed, so I think you can just return false here.
Quoted 1 line
Quoted 7 lines
Makes sense; I can make the change, but I had one question:
If we solely return false, how will we tell from the logs that the
allocation error message was associated with this driver? Only returning
false means the driver probe (e.g cros-ec-typec) will continue (just assuming a certain feature is not present). Wouldn't having this error message make this clear?
Quoted 3 lines
So I tried to find some doc about this without luck. But I think it has been an unwritten rule that
GFP_KERNELallocations for small allocations will never fail.
That might be the case, but kzalloc() still returns the error value, so even if it is very unlikely, we
still need to handle that error.
If you system fails to allocate that small amount of memory you probably
Quoted 2 lines
have bigger problems to solve and the above message is not really useful, even confusing, as the focus, likely, shouldn't be in this driver to solve the problem.
I don't know if I necessarily concur with that rationale ("if it fails, there are likely bigger issues").
There could be situations (hypothetical) where a series of allocations might lead to a failure (or this might be a transient allocation failure),
and it might be useful to know which driver is contributing the alloc that finally precipitates the failure.
Also, although it is very unlikely, returning true without an error can mean the typec driver silently continues to function wrongly assuming a feature flag to be set a certain way. That is something we need to flag through the logs.
I certainly can't see the log message as confusing the reader of a log any further.
Quoted 6 lines
Thinking a bit more about this change, and after your question, I don't really like functions not returning an error in the unlikely case that fails. On the other hand, I like this function return a bool as is a bit more clear IMO, so I'm wondering if wouldn't be better don't use dynamic memory here (I know that this is not really related to your patch)
That sounds right to me. Can I make that change as a follow up patch? I'd like to contain the scope
of this patch to just fix the return value.
Quoted 8 lines
And another thing that I detected, now that you're returning a bool is that in
drivers/platform/chrome/cros_ec_typec.cthere is:typec->typec_cmd_supported = !!cros_ec_check_features(ec_dev, typec->needs_mux_ack = !!cros_ec_check_features(ec_dev,I think that you can remove the !! now. That could be in another patch.
Yes, I was planning on doing that in a later patch.
Best regards,
Hi Prashant,
Quoted 70 lines · Prashant Malani
Hi Enric,
Quoted 66 lines · Enric Balletbo i Serra
Hi Prashant,
Quoted 61 lines · Prashant Malani
Hi Enric,
Thanks for reviewing the patch.
Quoted 39 lines · Enric Balletbo i Serra
Hi Prashant,
Thank you for the patch. Just one comment below ...
Quoted 34 lines · Prashant Malani
The kerneldoc for
cros_ec_check_features()states that it returns 1 or 0 depedending on whether a feature is supported or not, but it instead returns a negative error number in one case, and a non-1 bitmask in other cases.Since all call-sites only check for a 1 or 0 return value, update the function to return boolean values.
Signed-off-by Prashant Malani <[email protected]>--- drivers/platform/chrome/cros_ec_proto.c | 12 +++++++----- include/linux/platform_data/cros_ec_proto.h | 2 +- 2 files changed, 8 insertions(+), 6 deletions(-)drivers/platform/chrome/cros_ec_proto.c +2
-2@@ -808,9 +808,9 @@ EXPORT_SYMBOL(cros_ec_get_host_event);* * Call this function to test whether the ChromeOS EC supports a feature. * -* Return: 1 if supported, 0 if not +* Return: true if supported, false if not (or if an error was encountered). */ -int cros_ec_check_features (struct cros_ec_dev *ec,int feature) +bool cros_ec_check_features (struct cros_ec_dev *ec,int feature) {struct cros_ec_command *msg;int ret;@@ -818,8 +818,10 @@ int cros_ec_check_features(struct cros_ec_dev *ec, int feature)if (ec->features[0 ] ==-1U && ec->features[1 ] ==-1U ) {/* features bitmap not read yet */ msg =kzalloc (sizeof (*msg) +sizeof (ec->features),GFP_KERNEL );Quoted 9 lines
drivers/platform/chrome/cros_ec_proto.c int cros_ec_check_features(struct cros_ec_dev *ec, int feature) · line 822
Quoted 4 lines
drivers/platform/chrome/cros_ec_proto.c +2
-2-if (!msg) -return -ENOMEM ; +if (!msg) { +dev_err (ec->dev,"failed to allocate memory to get EC features\n" );In case of failure you will be noticed by the allocator, prints after [k|v][m|z|c]alloc() functions are not needed, so I think you can just return false here.
Quoted 1 line
Makes sense; I can make the change, but I had one question:
If we solely return false, how will we tell from the logs that the
allocation error message was associated with this driver? Only returning
false means the driver probe (e.g cros-ec-typec) will continue (just assuming a certain feature is not present). Wouldn't having this error message make this clear?
Quoted 8 lines
Quoted 3 lines
So I tried to find some doc about this without luck. But I think it has been an unwritten rule that
GFP_KERNELallocations for small allocations will never fail.That might be the case, but
kzalloc()still returns the error value, so even if it is very unlikely, we
still need to handle that error.
I'm not saying that we don't need to handle that error, we *must* handle the allocation errors, there is no doubts about this, but in this specific function we're ignoring all the errors and not noticing to the users. The discussion, or my nitpick (it's just a nit), is about if logging that the allocation failed in this driver is useful or not.
Quoted 4 lines
Quoted 3 lines
If you system fails to allocate that small amount of memory you probably have bigger problems to solve and the above message is not really useful, even confusing, as the focus, likely, shouldn't be in this driver to solve the problem.
Quoted 8 lines
I don't know if I necessarily concur with that rationale ("if it fails, there are likely bigger issues").
There could be situations (hypothetical) where a series of allocations might lead to a failure (or this might be a transient allocation failure),
and it might be useful to know which driver is contributing the alloc that finally precipitates the failure.
If the kernel is not able to give you a small amount of memory it is just a coincidence that this happened in your driver. Dynamic allocations and deallocations happens often, the print doesn't gives you more information as it could be any other driver because your system runs out of memory, and everybody can be affected.
Quoted 1 line
Also, although it is very unlikely, returning true without an error
I'm not saying this, I'm saying returning false but don't log the error to the console.
Quoted 1 line
can mean the typec driver silently continues to function
I think the consensus on the interface is, and was clear:
1/True if the feature is supported, 0/false if is not supported or there is an error. At some point we decided that the callers don't need to differentiate about if the feature is not supported or there is and unlikely error.
Quoted 2 lines
wrongly assuming a feature flag to be set a certain way. That is something we need to flag through the logs.
If you run out of memory there will be a lot of logs, trust me ;-)
Quoted 3 lines
I certainly can't see the log message as confusing the reader of a log any further.
As I said there is an unwritten rule (didn't find written doc about it tbh, although I know other maintainers follow) that prints after [k|v][m|z|c]alloc() functions are not needed.
And as I said, this is a nit, I won't strongly oppose if you think that this message is really needed.
Cheers,
Enric
Quoted 24 lines
Quoted 6 lines
Thinking a bit more about this change, and after your question, I don't really like functions not returning an error in the unlikely case that fails. On the other hand, I like this function return a bool as is a bit more clear IMO, so I'm wondering if wouldn't be better don't use dynamic memory here (I know that this is not really related to your patch)
That sounds right to me. Can I make that change as a follow up patch? I'd like to contain the scope
of this patch to just fix the return value.Quoted 8 lines
And another thing that I detected, now that you're returning a bool is that in
drivers/platform/chrome/cros_ec_typec.cthere is:typec->typec_cmd_supported = !!cros_ec_check_features(ec_dev, typec->needs_mux_ack = !!cros_ec_check_features(ec_dev,I think that you can remove the !! now. That could be in another patch.
Yes, I was planning on doing that in a later patch.
Best regards,
Hi Enric,
Quoted 114 lines · Enric Balletbo i Serra
Hi Prashant,
Quoted 70 lines · Prashant Malani
Hi Enric,
Quoted 66 lines · Enric Balletbo i Serra
Hi Prashant,
Quoted 61 lines · Prashant Malani
Hi Enric,
Thanks for reviewing the patch.
Quoted 39 lines · Enric Balletbo i Serra
Hi Prashant,
Thank you for the patch. Just one comment below ...
Quoted 34 lines · Prashant Malani
The kerneldoc for
cros_ec_check_features()states that it returns 1 or 0 depedending on whether a feature is supported or not, but it instead returns a negative error number in one case, and a non-1 bitmask in other cases.Since all call-sites only check for a 1 or 0 return value, update the function to return boolean values.
Signed-off-by Prashant Malani <[email protected]>--- drivers/platform/chrome/cros_ec_proto.c | 12 +++++++----- include/linux/platform_data/cros_ec_proto.h | 2 +- 2 files changed, 8 insertions(+), 6 deletions(-)drivers/platform/chrome/cros_ec_proto.c +2
-2@@ -808,9 +808,9 @@ EXPORT_SYMBOL(cros_ec_get_host_event);* * Call this function to test whether the ChromeOS EC supports a feature. * -* Return: 1 if supported, 0 if not +* Return: true if supported, false if not (or if an error was encountered). */ -int cros_ec_check_features (struct cros_ec_dev *ec,int feature) +bool cros_ec_check_features (struct cros_ec_dev *ec,int feature) {struct cros_ec_command *msg;int ret;@@ -818,8 +818,10 @@ int cros_ec_check_features(struct cros_ec_dev *ec, int feature)if (ec->features[0 ] ==-1U && ec->features[1 ] ==-1U ) {/* features bitmap not read yet */ msg =kzalloc (sizeof (*msg) +sizeof (ec->features),GFP_KERNEL );Quoted 9 lines
drivers/platform/chrome/cros_ec_proto.c int cros_ec_check_features(struct cros_ec_dev *ec, int feature) · line 822
Quoted 4 lines
drivers/platform/chrome/cros_ec_proto.c +2
-2-if (!msg) -return -ENOMEM ; +if (!msg) { +dev_err (ec->dev,"failed to allocate memory to get EC features\n" );In case of failure you will be noticed by the allocator, prints after [k|v][m|z|c]alloc() functions are not needed, so I think you can just return false here.
Quoted 1 line
Makes sense; I can make the change, but I had one question:
If we solely return false, how will we tell from the logs that the
allocation error message was associated with this driver? Only returning
false means the driver probe (e.g cros-ec-typec) will continue (just assuming a certain feature is not present). Wouldn't having this error message make this clear?Quoted 8 lines
Quoted 3 lines
So I tried to find some doc about this without luck. But I think it has been an unwritten rule that
GFP_KERNELallocations for small allocations will never fail.That might be the case, but
kzalloc()still returns the error value, so even if it is very unlikely, we
still need to handle that error.I'm not saying that we don't need to handle that error, we *must* handle the allocation errors, there is no doubts about this, but in this specific function we're ignoring all the errors and not noticing to the users. The discussion, or my nitpick (it's just a nit), is about if logging that the allocation failed in this driver is useful or not.
Quoted 4 lines
Quoted 3 lines
If you system fails to allocate that small amount of memory you probably have bigger problems to solve and the above message is not really useful, even confusing, as the focus, likely, shouldn't be in this driver to solve the problem.
Quoted 8 lines
I don't know if I necessarily concur with that rationale ("if it fails, there are likely bigger issues").
There could be situations (hypothetical) where a series of allocations might lead to a failure (or this might be a transient allocation failure),
and it might be useful to know which driver is contributing the alloc that finally precipitates the failure.If the kernel is not able to give you a small amount of memory it is just a coincidence that this happened in your driver. Dynamic allocations and deallocations happens often, the print doesn't gives you more information as it could be any other driver because your system runs out of memory, and everybody can be affected.
Quoted 1 line
Also, although it is very unlikely, returning true without an error
I'm not saying this, I'm saying returning false but don't log the error to the console.
Quoted 8 lines
Quoted 1 line
can mean the typec driver silently continues to function
I think the consensus on the interface is, and was clear:
1/True if the feature is supported, 0/false if is not supported or there is an error. At some point we decided that the callers don't need to differentiate about if the feature is not supported or there is and unlikely error.
I wasn't present when this was agreed on, but I don't think that's the right approach; moreover the current
implementation doesn't adhere to it.
In general I prefer either returning the appropriate error code, or parsing the error and logging it.
Quoted 5 lines
Quoted 2 lines
wrongly assuming a feature flag to be set a certain way. That is something we need to flag through the logs.
If you run out of memory there will be a lot of logs, trust me ;-)
Quoted 7 lines
Quoted 3 lines
I certainly can't see the log message as confusing the reader of a log any further.
As I said there is an unwritten rule (didn't find written doc about it tbh, although I know other maintainers follow) that prints after [k|v][m|z|c]alloc() functions are not needed.
Right, but in most cases they return -ENOMEM; here the boolean return type prevents that.
Quoted 3 lines
And as I said, this is a nit, I won't strongly oppose if you think that this message is really needed.
I'd prefer sticking with this if it's possible; in any case we'll have a follow up patch soon which gets rid of
the kzalloc entirely (as you suggested) so this discussion will become moot.
Best regards,
Enric Balletbo i Serra
AppliedQuoted 7 lines · Prashant Malani
The kerneldoc for
cros_ec_check_features()states that it returns 1 or 0 depedending on whether a feature is supported or not, but it instead returns a negative error number in one case, and a non-1 bitmask in other cases.Since all call-sites only check for a 1 or 0 return value, update the function to return boolean values.
Applied, thanks!
[1/1] platform/chrome: cros_ec_proto: Fix check_features ret val
commit: d50497c4a05e73e76874fd0d4942036375a7ec0f
Best regards,
Signature · 1 line
Enric Balletbo i Serra <[email protected]>