Skip to contentSkip to conversation

eeepc-laptop: remove possible use of uninitialized value

PATCH In review Frans Klaver 5 participants 28 messages Sep 3, 2014 – Sep 17, 2014 2 messages not in the archive

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 eeepc-laptop: remove possible use of uninitialized value

In store_sys_acpi, if count equals zero, or parse_arg()s sscanf call fails, 'value' remains possibly uninitialized. In that case 'value' shouldn't be used to produce the store_sys_acpi()s return value.

Only test the return value of set_acpi() if we can actually call it. Return rv otherwise.

---
 drivers/platform/x86/eeepc-laptop.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
drivers/platform/x86/eeepc-laptop.c +4 -4
index bd533c2..41f12ba 100644
@@ -279,10 +279,10 @@ static ssize_t store_sys_acpi(struct device *dev, int cm,
 	int rv, value;
 
 	rv = parse_arg(buf, count, &value);
-	if (rv > 0)
-		value = set_acpi(eeepc, cm, value);
-	if (value < 0)
-		return -EIO;
+	if (rv > 0) {
+		if (set_acpi(eeepc, cm, value) < 0)
+			return -EIO;
+	}
 	return rv;
 }
 
Signature · 1 line
2.1.0
#1 lore raw
Quoted 17 lines · Frans Klaver

In store_sys_acpi, if count equals zero, or parse_arg()s sscanf call fails, 'value' remains possibly uninitialized. In that case 'value' shouldn't be used to produce the store_sys_acpi()s return value.

Only test the return value of set_acpi() if we can actually call it. Return rv otherwise.

---
 drivers/platform/x86/eeepc-laptop.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
drivers/platform/x86/eeepc-laptop.c +0 -0
index bd533c2..41f12ba 100644

drivers/platform/x86/eeepc-laptop.c static ssize_t store_sys_acpi(struct device *dev, int cm, · line 283

Quoted 5 lines
drivers/platform/x86/eeepc-laptop.c +0 -2
@@ -279,10 +279,10 @@ static ssize_t store_sys_acpi(struct device *dev, int cm,
 	int rv, value;
 
 	rv = parse_arg(buf, count, &value);
-	if (rv > 0)
-		value = set_acpi(eeepc, cm, value);

That was rather horrible wasn't it? :-)

drivers/platform/x86/eeepc-laptop.c static ssize_t store_sys_acpi(struct device *dev, int cm, · line 284

Quoted 5 lines
drivers/platform/x86/eeepc-laptop.c +3 -2
-	if (value < 0)
-		return -EIO;
+	if (rv > 0) {
+		if (set_acpi(eeepc, cm, value) < 0)
+			return -EIO;

Is there a compelling reason not to propogate the return code of set_acpi? (ENODEV specifically). I see -EIO in Documentation/filesystems/sysfs.txt, but it's used by default if the show() pointer is NULL (for example), but otherwise propogates the error.

Specifically it states:

- show() or store() can always return errors. If a bad value comes through, be sure to return an error.

Greg, does this need to be -EIO? or is returning someting like ENODEV preferable if it more accurately reflects the error?

Signature · 2 lines
Darren Hart
Intel Open Source Technology Center
#2 lore raw
Quoted 37 lines · Darren Hart
Quoted 17 lines · Frans Klaver

In store_sys_acpi, if count equals zero, or parse_arg()s sscanf call fails, 'value' remains possibly uninitialized. In that case 'value' shouldn't be used to produce the store_sys_acpi()s return value.

Only test the return value of set_acpi() if we can actually call it. Return rv otherwise.

---
 drivers/platform/x86/eeepc-laptop.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
drivers/platform/x86/eeepc-laptop.c +0 -0
index bd533c2..41f12ba 100644

drivers/platform/x86/eeepc-laptop.c static ssize_t store_sys_acpi(struct device *dev, int cm, · line 283

Quoted 5 lines
drivers/platform/x86/eeepc-laptop.c +0 -2
@@ -279,10 +279,10 @@ static ssize_t store_sys_acpi(struct device *dev, int cm,
 	int rv, value;
 
 	rv = parse_arg(buf, count, &value);
-	if (rv > 0)
-		value = set_acpi(eeepc, cm, value);

That was rather horrible wasn't it? :-)

drivers/platform/x86/eeepc-laptop.c static ssize_t store_sys_acpi(struct device *dev, int cm, · line 284

Quoted 5 lines
drivers/platform/x86/eeepc-laptop.c +3 -2
-	if (value < 0)
-		return -EIO;
+	if (rv > 0) {
+		if (set_acpi(eeepc, cm, value) < 0)
+			return -EIO;

Is there a compelling reason not to propogate the return code of set_acpi? (ENODEV specifically). I see -EIO in Documentation/filesystems/sysfs.txt, but it's used by default if the show() pointer is NULL (for example), but otherwise propogates the error.

Quoted 7 lines

Specifically it states:

- show() or store() can always return errors. If a bad value comes through, be sure to return an error.

Greg, does this need to be -EIO? or is returning someting like ENODEV preferable if it more accurately reflects the error?

Just return the value of set_acpi() and you should be fine.

thanks,

greg k-h

#3 lore raw
Quoted 5 lines · Greg Kroah-Hartman
Quoted 4 lines · Darren Hart
Quoted 3 lines · Frans Klaver

In store_sys_acpi, if count equals zero, or parse_arg()s sscanf call fails, 'value' remains possibly uninitialized. In that case 'value' shouldn't be used to produce the store_sys_acpi()s return value.

Here I should probably remove either 'the' or the 's' after store_sys_acpi().

Quoted 34 lines
Quoted 34 lines
Quoted 13 lines

Only test the return value of set_acpi() if we can actually call it. Return rv otherwise.

---
 drivers/platform/x86/eeepc-laptop.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
drivers/platform/x86/eeepc-laptop.c +0 -0
index bd533c2..41f12ba 100644

drivers/platform/x86/eeepc-laptop.c static ssize_t store_sys_acpi(struct device *dev, int cm, · line 283

Quoted 5 lines
drivers/platform/x86/eeepc-laptop.c +0 -2
@@ -279,10 +279,10 @@ static ssize_t store_sys_acpi(struct device *dev, int cm,
    int rv, value;
 
    rv = parse_arg(buf, count, &value);
-   if (rv > 0)
-           value = set_acpi(eeepc, cm, value);

That was rather horrible wasn't it? :-)

drivers/platform/x86/eeepc-laptop.c static ssize_t store_sys_acpi(struct device *dev, int cm, · line 284

Quoted 5 lines
drivers/platform/x86/eeepc-laptop.c +3 -2
-   if (value < 0)
-           return -EIO;
+   if (rv > 0) {
+           if (set_acpi(eeepc, cm, value) < 0)
+                   return -EIO;

Is there a compelling reason not to propogate the return code of set_acpi? (ENODEV specifically). I see -EIO in Documentation/filesystems/sysfs.txt, but it's used by default if the show() pointer is NULL (for example), but otherwise propogates the error.

Specifically it states:

Quoted 7 lines
Quoted 5 lines

- show() or store() can always return errors. If a bad value comes through, be sure to return an error.

Greg, does this need to be -EIO? or is returning someting like ENODEV preferable if it more accurately reflects the error?

Just return the value of set_acpi() and you should be fine.

According to 6dff29b63a5bf2eaf3313cb8a84f0b7520c43401 "eeepc-laptop:
disp attribute should be write-only" it should be -EIO. -ENODEV would be misleading.

Thanks,
Frans

#4 lore raw
Quoted 48 lines · Frans Klaver
Quoted 5 lines · Greg Kroah-Hartman
Quoted 4 lines · Darren Hart
Quoted 3 lines · Frans Klaver

In store_sys_acpi, if count equals zero, or parse_arg()s sscanf call fails, 'value' remains possibly uninitialized. In that case 'value' shouldn't be used to produce the store_sys_acpi()s return value.

Here I should probably remove either 'the' or the 's' after store_sys_acpi().

Quoted 37 lines
Quoted 37 lines
Quoted 13 lines

Only test the return value of set_acpi() if we can actually call it. Return rv otherwise.

---
 drivers/platform/x86/eeepc-laptop.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
drivers/platform/x86/eeepc-laptop.c +0 -0
index bd533c2..41f12ba 100644

drivers/platform/x86/eeepc-laptop.c static ssize_t store_sys_acpi(struct device *dev, int cm, · line 283

Quoted 5 lines
drivers/platform/x86/eeepc-laptop.c +0 -2
@@ -279,10 +279,10 @@ static ssize_t store_sys_acpi(struct device *dev, int cm,
    int rv, value;
 
    rv = parse_arg(buf, count, &value);
-   if (rv > 0)
-           value = set_acpi(eeepc, cm, value);

That was rather horrible wasn't it? :-)

drivers/platform/x86/eeepc-laptop.c static ssize_t store_sys_acpi(struct device *dev, int cm, · line 284

Quoted 5 lines
drivers/platform/x86/eeepc-laptop.c +3 -2
-   if (value < 0)
-           return -EIO;
+   if (rv > 0) {
+           if (set_acpi(eeepc, cm, value) < 0)
+                   return -EIO;

Is there a compelling reason not to propogate the return code of set_acpi? (ENODEV specifically). I see -EIO in Documentation/filesystems/sysfs.txt, but it's used by default if the show() pointer is NULL (for example), but otherwise propogates the error.

Specifically it states:

- show() or store() can always return errors. If a bad value comes through, be sure to return an error.

Quoted 8 lines
Quoted 4 lines
Quoted 2 lines

Greg, does this need to be -EIO? or is returning someting like ENODEV preferable if it more accurately reflects the error?

Just return the value of set_acpi() and you should be fine.

According to 6dff29b63a5bf2eaf3313cb8a84f0b7520c43401 "eeepc-laptop:
disp attribute should be write-only" it should be -EIO. -ENODEV would be misleading.

If something is "write only" then there should not be a store function for it at all, the file should not be marked as writable, to prevent anything from ever being written to it at the higher-level filesystem layer, and never get down to the driver layer...

thanks,

greg k-h

#5 lore raw
Quoted 54 lines · Greg Kroah-Hartman
Quoted 53 lines · Frans Klaver
Quoted 5 lines · Greg Kroah-Hartman
Quoted 4 lines · Darren Hart
Quoted 3 lines · Frans Klaver

In store_sys_acpi, if count equals zero, or parse_arg()s sscanf call fails, 'value' remains possibly uninitialized. In that case 'value' shouldn't be used to produce the store_sys_acpi()s return value.

Here I should probably remove either 'the' or the 's' after store_sys_acpi().

Quoted 41 lines
Quoted 32 lines
Quoted 13 lines

Only test the return value of set_acpi() if we can actually call it. Return rv otherwise.

---
 drivers/platform/x86/eeepc-laptop.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
drivers/platform/x86/eeepc-laptop.c +0 -0
index bd533c2..41f12ba 100644

drivers/platform/x86/eeepc-laptop.c static ssize_t store_sys_acpi(struct device *dev, int cm, · line 283

Quoted 5 lines
drivers/platform/x86/eeepc-laptop.c +0 -2
@@ -279,10 +279,10 @@ static ssize_t store_sys_acpi(struct device *dev, int cm,
    int rv, value;
 
    rv = parse_arg(buf, count, &value);
-   if (rv > 0)
-           value = set_acpi(eeepc, cm, value);

That was rather horrible wasn't it? :-)

drivers/platform/x86/eeepc-laptop.c static ssize_t store_sys_acpi(struct device *dev, int cm, · line 284

Quoted 5 lines
drivers/platform/x86/eeepc-laptop.c +3 -2
-   if (value < 0)
-           return -EIO;
+   if (rv > 0) {
+           if (set_acpi(eeepc, cm, value) < 0)
+                   return -EIO;

Is there a compelling reason not to propogate the return code of set_acpi? (ENODEV specifically). I see -EIO in Documentation/filesystems/sysfs.txt, but it's used by default if the show() pointer is NULL (for example), but otherwise propogates the error.

Quoted 7 lines

Specifically it states:

- show() or store() can always return errors. If a bad value comes through, be sure to return an error.

Greg, does this need to be -EIO? or is returning someting like ENODEV preferable if it more accurately reflects the error?

Just return the value of set_acpi() and you should be fine.

Quoted 8 lines
Quoted 3 lines

According to 6dff29b63a5bf2eaf3313cb8a84f0b7520c43401 "eeepc-laptop:
disp attribute should be write-only" it should be -EIO. -ENODEV would be misleading.

If something is "write only" then there should not be a store function for it at all, the file should not be marked as writable, to prevent anything from ever being written to it at the higher-level filesystem layer, and never get down to the driver layer...

If something is "write only" it should not have a show function. Removing the show function for the disp attribute would probably also remove the necessity for the file permissions.

I'll I fire up my eeepc and see what I can figure out. Should I take Pauls patch and see how it fits into this?

Thanks,
Frans

#6 lore raw
Quoted 2 lines · Frans Klaver

I'll I fire up my eeepc and see what I can figure out. Should I take Pauls patch and see how it fits into this?

I haven't followed the conversation you're having with Greg, but I would be grateful if you'd test _just_ my patch. Unless someone jumps in and points to flaws in my patch, that is.

I suppose testing would mean writing some values to the related files under /sys. I have no idea whatsoever what those files actually do, so I'm afraid I can't tell you what values to write and what effects to expect, sorry. Perhaps someone else can help you there.

Paul Bolle

#7 lore raw
In reply to Frans Klaver
Quoted 21 lines · Frans Klaver

In store_sys_acpi, if count equals zero, or parse_arg()s sscanf call fails, 'value' remains possibly uninitialized. In that case 'value' shouldn't be used to produce the store_sys_acpi()s return value.

Only test the return value of set_acpi() if we can actually call it. Return rv otherwise.

---
 drivers/platform/x86/eeepc-laptop.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
drivers/platform/x86/eeepc-laptop.c +0 -1
index bd533c2..41f12ba 100644
@@ -279,10 +279,10 @@ static ssize_t store_sys_acpi(struct device *dev, int cm,
 	int rv, value;
 
 	rv = parse_arg(buf, count, &value);
-	if (rv > 0)

drivers/platform/x86/eeepc-laptop.c static ssize_t store_sys_acpi(struct device *dev, int cm, · line 288

Quoted 10 lines
drivers/platform/x86/eeepc-laptop.c +4 -3
-		value = set_acpi(eeepc, cm, value);
-	if (value < 0)
-		return -EIO;
+	if (rv > 0) {
+		if (set_acpi(eeepc, cm, value) < 0)
+			return -EIO;
+	}
 	return rv;
 }
 

The warning that this code (currently) generated triggered me to submit
https://lkml.org/lkml/2014/7/1/150 , which uses a different approach to get rid of it. I received no reactions so far. Here's that patch again:

------------>8------------
From: Paul Bolle <[email protected]>
Subject: [PATCH] eeepc-laptop: simplify parse_arg()

parse_arg() has three possible return values:
-EINVAL if sscanf(), in short, fails;
zero if "count" is zero; and
"count" in all other cases

But "count" will never be zero. See, parse_arg() is called by the various store functions. And the callchain of these functions starts with sysfs_kf_write(). And that function checks for a zero "count". So we can stop checking for a zero "count", drop the "count" argument entirely, and transform parse_arg() into a function that returns zero on success or a negative error. That, in turn, allows to make those store functions just return "count" on success. The net effect is that the code becomes a bit easier to understand.

A nice side effect is that this GCC warning is silenced too:
drivers/platform/x86/eeepc-laptop.c: In function ‘store_sys_acpi’:
drivers/platform/x86/eeepc-laptop.c:279:10: warning: ‘value’ may be used uninitialized in this function [-Wmaybe-uninitialized]
int rv, value;

Which is, of course, the reason to have a look at parse_arg().

---
 drivers/platform/x86/eeepc-laptop.c | 34 +++++++++++++++++-----------------
 1 file changed, 17 insertions(+), 17 deletions(-)
drivers/platform/x86/eeepc-laptop.c +17 -17
index bd533c22be57..78515b850165 100644
@@ -263,13 +263,11 @@ static int acpi_setter_handle(struct eeepc_laptop *eeepc, int cm,
 /*
  * Sys helpers
  */
-static int parse_arg(const char *buf, unsigned long count, int *val)
+static int parse_arg(const char *buf, int *val)
 {
-	if (!count)
-		return 0;
 	if (sscanf(buf, "%i", val) != 1)
 		return -EINVAL;
-	return count;
+	return 0;
 }
 
 static ssize_t store_sys_acpi(struct device *dev, int cm,
@@ -278,12 +276,13 @@ static ssize_t store_sys_acpi(struct device *dev, int cm,
 	struct eeepc_laptop *eeepc = dev_get_drvdata(dev);
 	int rv, value;
 
-	rv = parse_arg(buf, count, &value);
-	if (rv > 0)
-		value = set_acpi(eeepc, cm, value);
+	rv = parse_arg(buf, &value);
+	if (rv < 0)
+		return rv;
+	value = set_acpi(eeepc, cm, value);
 	if (value < 0)
 		return -EIO;
-	return rv;
+	return count;
 }
 
 static ssize_t show_sys_acpi(struct device *dev, int cm, char *buf)
@@ -377,13 +376,13 @@ static ssize_t store_cpufv(struct device *dev,
 		return -EPERM;
 	if (get_cpufv(eeepc, &c))
 		return -ENODEV;
-	rv = parse_arg(buf, count, &value);
+	rv = parse_arg(buf, &value);
 	if (rv < 0)
 		return rv;
-	if (!rv || value < 0 || value >= c.num)
+	if (value < 0 || value >= c.num)
 		return -EINVAL;
 	set_acpi(eeepc, CM_ASL_CPUFV, value);
-	return rv;
+	return count;
 }
 
 static ssize_t show_cpufv_disabled(struct device *dev,
@@ -402,7 +401,7 @@ static ssize_t store_cpufv_disabled(struct device *dev,
 	struct eeepc_laptop *eeepc = dev_get_drvdata(dev);
 	int rv, value;
 
-	rv = parse_arg(buf, count, &value);
+	rv = parse_arg(buf, &value);
 	if (rv < 0)
 		return rv;
 
@@ -412,7 +411,7 @@ static ssize_t store_cpufv_disabled(struct device *dev,
 			pr_warn("cpufv enabled (not officially supported "
 				"on this model)\n");
 		eeepc->cpufv_disabled = false;
-		return rv;
+		return count;
 	case 1:
 		return -EPERM;
 	default:
@@ -1042,10 +1041,11 @@ static ssize_t store_sys_hwmon(void (*set)(int), const char *buf, size_t count)
 {
 	int rv, value;
 
-	rv = parse_arg(buf, count, &value);
-	if (rv > 0)
-		set(value);
-	return rv;
+	rv = parse_arg(buf, &value);
+	if (rv < 0)
+		return rv;
+	set(value);
+	return count;
 }
 
 static ssize_t show_sys_hwmon(int (*get)(void), char *buf)
Signature · 1 line
Paul Bolle
#8 lore raw
Quoted 55 lines · Paul Bolle
Quoted 21 lines · Frans Klaver

In store_sys_acpi, if count equals zero, or parse_arg()s sscanf call fails, 'value' remains possibly uninitialized. In that case 'value' shouldn't be used to produce the store_sys_acpi()s return value.

Only test the return value of set_acpi() if we can actually call it. Return rv otherwise.

---
 drivers/platform/x86/eeepc-laptop.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
drivers/platform/x86/eeepc-laptop.c +0 -1
index bd533c2..41f12ba 100644
@@ -279,10 +279,10 @@ static ssize_t store_sys_acpi(struct device *dev, int cm,
      int rv, value;
 
      rv = parse_arg(buf, count, &value);
-     if (rv > 0)

drivers/platform/x86/eeepc-laptop.c static ssize_t store_sys_acpi(struct device *dev, int cm, · line 288

Quoted 10 lines
drivers/platform/x86/eeepc-laptop.c +4 -3
-             value = set_acpi(eeepc, cm, value);
-     if (value < 0)
-             return -EIO;
+     if (rv > 0) {
+             if (set_acpi(eeepc, cm, value) < 0)
+                     return -EIO;
+     }
      return rv;
 }
 

The warning that this code (currently) generated triggered me to submit
https://lkml.org/lkml/2014/7/1/150 , which uses a different approach to get rid of it. I received no reactions so far. Here's that patch again:

------------>8------------
From: Paul Bolle <[email protected]>
Subject: [PATCH] eeepc-laptop: simplify parse_arg()

parse_arg() has three possible return values:
-EINVAL if sscanf(), in short, fails;
zero if "count" is zero; and
"count" in all other cases

But "count" will never be zero. See, parse_arg() is called by the various store functions. And the callchain of these functions starts with sysfs_kf_write(). And that function checks for a zero "count". So we can stop checking for a zero "count", drop the "count" argument entirely, and transform parse_arg() into a function that returns zero on success or a negative error. That, in turn, allows to make those store functions just return "count" on success. The net effect is that the code becomes a bit easier to understand.

Quoted 8 lines

A nice side effect is that this GCC warning is silenced too:
drivers/platform/x86/eeepc-laptop.c: In function ‘store_sys_acpi’:
drivers/platform/x86/eeepc-laptop.c:279:10: warning: ‘value’ may be used uninitialized in this function [-Wmaybe-uninitialized]
int rv, value;

Which is, of course, the reason to have a look at parse_arg().

Curious. I hadn't found this one when searching for earlier patches.

Thanks,
Frans

Quoted 98 lines
---
 drivers/platform/x86/eeepc-laptop.c | 34 +++++++++++++++++-----------------
 1 file changed, 17 insertions(+), 17 deletions(-)
drivers/platform/x86/eeepc-laptop.c +17 -18
index bd533c22be57..78515b850165 100644
@@ -263,13 +263,11 @@ static int acpi_setter_handle(struct eeepc_laptop *eeepc, int cm,
 /*
  * Sys helpers
  */
-static int parse_arg(const char *buf, unsigned long count, int *val)
+static int parse_arg(const char *buf, int *val)
 {
-       if (!count)
-               return 0;
        if (sscanf(buf, "%i", val) != 1)
                return -EINVAL;
-       return count;
+       return 0;
 }
 
 static ssize_t store_sys_acpi(struct device *dev, int cm,
@@ -278,12 +276,13 @@ static ssize_t store_sys_acpi(struct device *dev, int cm,
        struct eeepc_laptop *eeepc = dev_get_drvdata(dev);
        int rv, value;
 
-       rv = parse_arg(buf, count, &value);
-       if (rv > 0)
-               value = set_acpi(eeepc, cm, value);
+       rv = parse_arg(buf, &value);
+       if (rv < 0)
+               return rv;
+       value = set_acpi(eeepc, cm, value);
        if (value < 0)
                return -EIO;
-       return rv;
+       return count;
 }
 
 static ssize_t show_sys_acpi(struct device *dev, int cm, char *buf)
@@ -377,13 +376,13 @@ static ssize_t store_cpufv(struct device *dev,
                return -EPERM;
        if (get_cpufv(eeepc, &c))
                return -ENODEV;
-       rv = parse_arg(buf, count, &value);
+       rv = parse_arg(buf, &value);
        if (rv < 0)
                return rv;
-       if (!rv || value < 0 || value >= c.num)
+       if (value < 0 || value >= c.num)
                return -EINVAL;
        set_acpi(eeepc, CM_ASL_CPUFV, value);
-       return rv;
+       return count;
 }
 
 static ssize_t show_cpufv_disabled(struct device *dev,
@@ -402,7 +401,7 @@ static ssize_t store_cpufv_disabled(struct device *dev,
        struct eeepc_laptop *eeepc = dev_get_drvdata(dev);
        int rv, value;
 
-       rv = parse_arg(buf, count, &value);
+       rv = parse_arg(buf, &value);
        if (rv < 0)
                return rv;
 
@@ -412,7 +411,7 @@ static ssize_t store_cpufv_disabled(struct device *dev,
                        pr_warn("cpufv enabled (not officially supported "
                                "on this model)\n");
                eeepc->cpufv_disabled = false;
-               return rv;
+               return count;
        case 1:
                return -EPERM;
        default:
@@ -1042,10 +1041,11 @@ static ssize_t store_sys_hwmon(void (*set)(int), const char *buf, size_t count)
 {
        int rv, value;
 
-       rv = parse_arg(buf, count, &value);
-       if (rv > 0)
-               set(value);
-       return rv;
+       rv = parse_arg(buf, &value);
+       if (rv < 0)
+               return rv;
+       set(value);
+       return count;
 }
 
 static ssize_t show_sys_hwmon(int (*get)(void), char *buf)
--

Paul Bolle

#9 lore raw
In reply to Paul Bolle
Quoted 20 lines · Paul Bolle
Quoted 19 lines · Frans Klaver

In store_sys_acpi, if count equals zero, or parse_arg()s sscanf call fails, 'value' remains possibly uninitialized. In that case 'value' shouldn't be used to produce the store_sys_acpi()s return value.

Only test the return value of set_acpi() if we can actually call it. Return rv otherwise.

---
 drivers/platform/x86/eeepc-laptop.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
drivers/platform/x86/eeepc-laptop.c +0 -0
index bd533c2..41f12ba 100644
@@ -279,10 +279,10 @@ static ssize_t store_sys_acpi(struct device *dev, int cm,
 	int rv, value;
 
Quoted 16 lines

drivers/platform/x86/eeepc-laptop.c static ssize_t store_sys_acpi(struct device *dev, int cm, · line 288

Quoted 12 lines
drivers/platform/x86/eeepc-laptop.c +4 -4
 	rv = parse_arg(buf, count, &value);
-	if (rv > 0)
-		value = set_acpi(eeepc, cm, value);
-	if (value < 0)
-		return -EIO;
+	if (rv > 0) {
+		if (set_acpi(eeepc, cm, value) < 0)
+			return -EIO;
+	}
 	return rv;
 }
 

The warning that this code (currently) generated triggered me to submit
https://lkml.org/lkml/2014/7/1/150 , which uses a different approach to get rid of it. I received no reactions so far. Here's that patch again:

Thanks for resending.

Quoted 10 lines

------------>8------------
From: Paul Bolle <[email protected]>
Subject: [PATCH] eeepc-laptop: simplify parse_arg()

parse_arg() has three possible return values:
-EINVAL if sscanf(), in short, fails;
zero if "count" is zero; and
"count" in all other cases

Quoted 9 lines

But "count" will never be zero. See, parse_arg() is called by the various store functions. And the callchain of these functions starts with sysfs_kf_write(). And that function checks for a zero "count". So we can stop checking for a zero "count", drop the "count" argument entirely, and transform parse_arg() into a function that returns zero on success or a negative error. That, in turn, allows to make those store functions just return "count" on success. The net effect is that the code becomes a bit easier to understand.

Seems reasonable.

Quoted 34 lines

A nice side effect is that this GCC warning is silenced too:
drivers/platform/x86/eeepc-laptop.c: In function ‘store_sys_acpi’:
drivers/platform/x86/eeepc-laptop.c:279:10: warning: ‘value’ may be used uninitialized in this function [-Wmaybe-uninitialized]
int rv, value;

Which is, of course, the reason to have a look at parse_arg().

---
 drivers/platform/x86/eeepc-laptop.c | 34 +++++++++++++++++-----------------
 1 file changed, 17 insertions(+), 17 deletions(-)
drivers/platform/x86/eeepc-laptop.c +2 -4
index bd533c22be57..78515b850165 100644
@@ -263,13 +263,11 @@ static int acpi_setter_handle(struct eeepc_laptop *eeepc, int cm,
 /*
  * Sys helpers
  */
-static int parse_arg(const char *buf, unsigned long count, int *val)
+static int parse_arg(const char *buf, int *val)
 {
-	if (!count)
-		return 0;
 	if (sscanf(buf, "%i", val) != 1)
 		return -EINVAL;
-	return count;
+	return 0;
 }
 
 static ssize_t store_sys_acpi(struct device *dev, int cm,
@@ -278,12 +276,13 @@ static ssize_t store_sys_acpi(struct device *dev, int cm,
 	struct eeepc_laptop *eeepc = dev_get_drvdata(dev);

drivers/platform/x86/eeepc-laptop.c static ssize_t store_sys_acpi(struct device *dev, int cm, · line 283

Quoted 10 lines
drivers/platform/x86/eeepc-laptop.c +4 -3
 	int rv, value;
 
-	rv = parse_arg(buf, count, &value);
-	if (rv > 0)
-		value = set_acpi(eeepc, cm, value);
+	rv = parse_arg(buf, &value);
+	if (rv < 0)
+		return rv;
+	value = set_acpi(eeepc, cm, value);
 	if (value < 0)

I suppose it's harmless, but it would be more explicit to reuse rv here instead of value.

drivers/platform/x86/eeepc-laptop.c static ssize_t store_sys_acpi(struct device *dev, int cm, · line 284

Quoted 1 line
drivers/platform/x86/eeepc-laptop.c +0 -0
 		return -EIO;

And as with Frans' version, I suggest propogating the error. We're talking about a missing/invalid ACPI control method name here, ENODEV seems approprirate.

Rafael, do you have a strong preference about what to return in such an event?

Signature · 2 lines
Darren Hart
Intel Open Source Technology Center
#10 lore raw
Quoted 110 lines · Darren Hart
Quoted 20 lines · Paul Bolle
Quoted 19 lines · Frans Klaver

In store_sys_acpi, if count equals zero, or parse_arg()s sscanf call fails, 'value' remains possibly uninitialized. In that case 'value' shouldn't be used to produce the store_sys_acpi()s return value.

Only test the return value of set_acpi() if we can actually call it. Return rv otherwise.

---
 drivers/platform/x86/eeepc-laptop.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
drivers/platform/x86/eeepc-laptop.c +0 -0
index bd533c2..41f12ba 100644
@@ -279,10 +279,10 @@ static ssize_t store_sys_acpi(struct device *dev, int cm,
 	int rv, value;
 
Quoted 16 lines

drivers/platform/x86/eeepc-laptop.c static ssize_t store_sys_acpi(struct device *dev, int cm, · line 288

Quoted 12 lines
drivers/platform/x86/eeepc-laptop.c +4 -4
 	rv = parse_arg(buf, count, &value);
-	if (rv > 0)
-		value = set_acpi(eeepc, cm, value);
-	if (value < 0)
-		return -EIO;
+	if (rv > 0) {
+		if (set_acpi(eeepc, cm, value) < 0)
+			return -EIO;
+	}
 	return rv;
 }
 

The warning that this code (currently) generated triggered me to submit
https://lkml.org/lkml/2014/7/1/150 , which uses a different approach to get rid of it. I received no reactions so far. Here's that patch again:

Thanks for resending.

Quoted 10 lines

------------>8------------
From: Paul Bolle <[email protected]>
Subject: [PATCH] eeepc-laptop: simplify parse_arg()

parse_arg() has three possible return values:
-EINVAL if sscanf(), in short, fails;
zero if "count" is zero; and
"count" in all other cases

Quoted 9 lines

But "count" will never be zero. See, parse_arg() is called by the various store functions. And the callchain of these functions starts with sysfs_kf_write(). And that function checks for a zero "count". So we can stop checking for a zero "count", drop the "count" argument entirely, and transform parse_arg() into a function that returns zero on success or a negative error. That, in turn, allows to make those store functions just return "count" on success. The net effect is that the code becomes a bit easier to understand.

Seems reasonable.

Quoted 34 lines

A nice side effect is that this GCC warning is silenced too:
drivers/platform/x86/eeepc-laptop.c: In function ‘store_sys_acpi’:
drivers/platform/x86/eeepc-laptop.c:279:10: warning: ‘value’ may be used uninitialized in this function [-Wmaybe-uninitialized]
int rv, value;

Which is, of course, the reason to have a look at parse_arg().

---
 drivers/platform/x86/eeepc-laptop.c | 34 +++++++++++++++++-----------------
 1 file changed, 17 insertions(+), 17 deletions(-)
drivers/platform/x86/eeepc-laptop.c +2 -4
index bd533c22be57..78515b850165 100644
@@ -263,13 +263,11 @@ static int acpi_setter_handle(struct eeepc_laptop *eeepc, int cm,
 /*
  * Sys helpers
  */
-static int parse_arg(const char *buf, unsigned long count, int *val)
+static int parse_arg(const char *buf, int *val)
 {
-	if (!count)
-		return 0;
 	if (sscanf(buf, "%i", val) != 1)
 		return -EINVAL;
-	return count;
+	return 0;
 }
 
 static ssize_t store_sys_acpi(struct device *dev, int cm,
@@ -278,12 +276,13 @@ static ssize_t store_sys_acpi(struct device *dev, int cm,
 	struct eeepc_laptop *eeepc = dev_get_drvdata(dev);

drivers/platform/x86/eeepc-laptop.c static ssize_t store_sys_acpi(struct device *dev, int cm, · line 283

Quoted 10 lines
drivers/platform/x86/eeepc-laptop.c +4 -3
 	int rv, value;
 
-	rv = parse_arg(buf, count, &value);
-	if (rv > 0)
-		value = set_acpi(eeepc, cm, value);
+	rv = parse_arg(buf, &value);
+	if (rv < 0)
+		return rv;
+	value = set_acpi(eeepc, cm, value);
 	if (value < 0)

I suppose it's harmless, but it would be more explicit to reuse rv here instead of value.

Quoted 6 lines

drivers/platform/x86/eeepc-laptop.c static ssize_t store_sys_acpi(struct device *dev, int cm, · line 284

Quoted 1 line
drivers/platform/x86/eeepc-laptop.c +0 -0
 		return -EIO;

And as with Frans' version, I suggest propogating the error. We're talking about a missing/invalid ACPI control method name here, ENODEV seems approprirate.

Rafael, do you have a strong preference about what to return in such an event?

No, I don't, although -ENXIO could be used here too.

Rafael

#11 lore raw

PATCH 1/1 eeepc-laptop: remove disp attribute show function

The disp attribute is write-only. This is enforced by mimicking sysfs behavior store_sys_acpi() and show_sys_acpi(), but this is not ideal. Behaving like sysfs is better left to sysfs.

Remove the show function from the disp attribute. This ensures userspace can only write to disp at all times. While at it, propagate any errors produced in store_sys_acpi() and show_sys_acpi(), instead of overriding them with -EIO.

---

This patch applies on top of Paul's "eeepc-laptop: simplify parse_arg()". Tested both patches separately, as well as combined. As far as I can see the system behaves exactly like before.

 drivers/platform/x86/eeepc-laptop.c | 19 ++++++++++++++++---
 1 file changed, 16 insertions(+), 3 deletions(-)
drivers/platform/x86/eeepc-laptop.c +16 -3
index 78515b8..03bcbed 100644
@@ -281,7 +281,7 @@ static ssize_t store_sys_acpi(struct device *dev, int cm,
 		return rv;
 	value = set_acpi(eeepc, cm, value);
 	if (value < 0)
-		return -EIO;
+		return value;
 	return count;
 }
 
@@ -291,7 +291,7 @@ static ssize_t show_sys_acpi(struct device *dev, int cm, char *buf)
 	int value = get_acpi(eeepc, cm);
 
 	if (value < 0)
-		return -EIO;
+		return value;
 	return sprintf(buf, "%d\n", value);
 }
 
@@ -318,7 +318,20 @@ static ssize_t show_sys_acpi(struct device *dev, int cm, char *buf)
 
 EEEPC_CREATE_DEVICE_ATTR(camera, 0644, CM_ASL_CAMERA);
 EEEPC_CREATE_DEVICE_ATTR(cardr, 0644, CM_ASL_CARDREADER);
-EEEPC_CREATE_DEVICE_ATTR(disp, 0200, CM_ASL_DISPLAYSWITCH);
+
+static ssize_t store_disp(struct device *dev,
+			  struct device_attribute *attr,
+			  const char *buf, size_t count)
+{
+	return store_sys_acpi(dev, CM_ASL_DISPLAYSWITCH, buf, count);
+}
+
+static struct device_attribute dev_attr_disp = {
+	.attr = {
+		.name = "disp",
+		.mode = 0200 },
+	.store  = store_disp,
+};
 
 struct eeepc_cpufv {
 	int num;
Signature · 1 line
2.1.0
#12 lore raw

Re: [PATCH] eeepc-laptop: remove disp attribute show function

Quoted 50 lines · Frans Klaver

The disp attribute is write-only. This is enforced by mimicking sysfs behavior store_sys_acpi() and show_sys_acpi(), but this is not ideal. Behaving like sysfs is better left to sysfs.

Remove the show function from the disp attribute. This ensures userspace can only write to disp at all times. While at it, propagate any errors produced in store_sys_acpi() and show_sys_acpi(), instead of overriding them with -EIO.

---

This patch applies on top of Paul's "eeepc-laptop: simplify parse_arg()". Tested both patches separately, as well as combined. As far as I can see the system behaves exactly like before.

 drivers/platform/x86/eeepc-laptop.c | 19 ++++++++++++++++---
 1 file changed, 16 insertions(+), 3 deletions(-)
drivers/platform/x86/eeepc-laptop.c +6 -3
index 78515b8..03bcbed 100644
@@ -281,7 +281,7 @@ static ssize_t store_sys_acpi(struct device *dev, int cm,
 		return rv;
 	value = set_acpi(eeepc, cm, value);
 	if (value < 0)
-		return -EIO;
+		return value;
 	return count;
 }
 
@@ -291,7 +291,7 @@ static ssize_t show_sys_acpi(struct device *dev, int cm, char *buf)
 	int value = get_acpi(eeepc, cm);
 
 	if (value < 0)
-		return -EIO;
+		return value;
 	return sprintf(buf, "%d\n", value);
 }
 
@@ -318,7 +318,20 @@ static ssize_t show_sys_acpi(struct device *dev, int cm, char *buf)
 
 EEEPC_CREATE_DEVICE_ATTR(camera, 0644, CM_ASL_CAMERA);
 EEEPC_CREATE_DEVICE_ATTR(cardr, 0644, CM_ASL_CARDREADER);
-EEEPC_CREATE_DEVICE_ATTR(disp, 0200, CM_ASL_DISPLAYSWITCH);
+
+static ssize_t store_disp(struct device *dev,
+			  struct device_attribute *attr,
+			  const char *buf, size_t count)

drivers/platform/x86/eeepc-laptop.c static ssize_t show_sys_acpi(struct device *dev, int cm, char *buf) · line 334

Quoted 10 lines
drivers/platform/x86/eeepc-laptop.c +10 -0
+{
+	return store_sys_acpi(dev, CM_ASL_DISPLAYSWITCH, buf, count);
+}
+
+static struct device_attribute dev_attr_disp = {
+	.attr = {
+		.name = "disp",
+		.mode = 0200 },
+	.store  = store_disp,
+};

DEVICE_ATTR()?

A message that is not in the archive (sent off-list or not yet received): [email protected]

A message that is not in the archive (sent off-list or not yet received): [email protected]

#15 lore raw

Re: [PATCH] eeepc-laptop: remove disp attribute show function

Quoted 24 lines · Frans Klaver
Quoted 23 lines · Frans Klaver
Quoted 21 lines · Greg Kroah-Hartman
Quoted 8 lines · Frans Klaver

EEEPC_CREATE_DEVICE_ATTR(camera, 0644, CM_ASL_CAMERA); EEEPC_CREATE_DEVICE_ATTR(cardr, 0644, CM_ASL_CARDREADER);

drivers/platform/x86/eeepc-laptop.c +4 -1
-EEEPC_CREATE_DEVICE_ATTR(disp, 0200, CM_ASL_DISPLAYSWITCH);
+
+static ssize_t store_disp(struct device *dev,
+			  struct device_attribute *attr,
+			  const char *buf, size_t count)

drivers/platform/x86/eeepc-laptop.c static ssize_t show_sys_acpi(struct device *dev, int cm, char *buf) · line 334

Quoted 10 lines
drivers/platform/x86/eeepc-laptop.c +10 -0
+{
+	return store_sys_acpi(dev, CM_ASL_DISPLAYSWITCH, buf, count);
+}
+
+static struct device_attribute dev_attr_disp = {
+	.attr = {
+		.name = "disp",
+		.mode = 0200 },
+	.store  = store_disp,
+};

DEVICE_ATTR()?

Quoted 6 lines
Quoted 2 lines

That would make sense, wouldn't it? The only defense I have is that it isn't used in EEEPC_CREATE_DEVICE_ATTR() either.

I might as well fix that as well, and the fact that the octal permissions are used instead of S_IRUGO and friends. Would you prefer that in a separate patch?

I'm not taking these patches, so I can't answer that...

#16 lore raw

Re: [PATCH] eeepc-laptop: remove disp attribute show function

Quoted 25 lines · Greg Kroah-Hartman
Quoted 24 lines · Frans Klaver
Quoted 23 lines · Frans Klaver
Quoted 21 lines · Greg Kroah-Hartman
Quoted 8 lines · Frans Klaver

EEEPC_CREATE_DEVICE_ATTR(camera, 0644, CM_ASL_CAMERA); EEEPC_CREATE_DEVICE_ATTR(cardr, 0644, CM_ASL_CARDREADER);

drivers/platform/x86/eeepc-laptop.c +4 -1
-EEEPC_CREATE_DEVICE_ATTR(disp, 0200, CM_ASL_DISPLAYSWITCH);
+
+static ssize_t store_disp(struct device *dev,
+			  struct device_attribute *attr,
+			  const char *buf, size_t count)

drivers/platform/x86/eeepc-laptop.c static ssize_t show_sys_acpi(struct device *dev, int cm, char *buf) · line 334

Quoted 10 lines
drivers/platform/x86/eeepc-laptop.c +10 -0
+{
+	return store_sys_acpi(dev, CM_ASL_DISPLAYSWITCH, buf, count);
+}
+
+static struct device_attribute dev_attr_disp = {
+	.attr = {
+		.name = "disp",
+		.mode = 0200 },
+	.store  = store_disp,
+};

DEVICE_ATTR()?

Quoted 6 lines
Quoted 6 lines
Quoted 2 lines

That would make sense, wouldn't it? The only defense I have is that it isn't used in EEEPC_CREATE_DEVICE_ATTR() either.

I might as well fix that as well, and the fact that the octal permissions are used instead of S_IRUGO and friends. Would you prefer that in a separate patch?

If you're offering to fix it (thanks!), please do any necessary cleanup first (like switching to S_IRUGO and friends), and then follow up with a DEVICE_ATTR solution.

Thanks,

Signature · 2 lines
Darren Hart
Intel Open Source Technology Center
#17 lore raw
In reply to Rafael J. Wysocki

Hi Darren,

Quoted 8 lines · Rafael J. Wysocki
Quoted 7 lines · Darren Hart

On Thu, Sep 04, 2014 at 09:08:08AM +0200, Paul Bolle wrote:
[...]

Quoted 5 lines

static ssize_t store_sys_acpi(struct device *dev, int cm,

drivers/platform/x86/eeepc-laptop.c +0 -0
@@ -278,12 +276,13 @@ static ssize_t store_sys_acpi(struct device *dev, int cm,
 	struct eeepc_laptop *eeepc = dev_get_drvdata(dev);
 	int rv, value;
 
Quoted 11 lines
Quoted 11 lines

drivers/platform/x86/eeepc-laptop.c static ssize_t store_sys_acpi(struct device *dev, int cm, · line 283

Quoted 8 lines
drivers/platform/x86/eeepc-laptop.c +4 -3
-	rv = parse_arg(buf, count, &value);
-	if (rv > 0)
-		value = set_acpi(eeepc, cm, value);
+	rv = parse_arg(buf, &value);
+	if (rv < 0)
+		return rv;
+	value = set_acpi(eeepc, cm, value);
 	if (value < 0)

I suppose it's harmless, but it would be more explicit to reuse rv here instead of value.

Fine with me.

Quoted 8 lines
Quoted 6 lines

drivers/platform/x86/eeepc-laptop.c static ssize_t store_sys_acpi(struct device *dev, int cm, · line 284

Quoted 1 line
drivers/platform/x86/eeepc-laptop.c +0 -0
 		return -EIO;

And as with Frans' version, I suggest propogating the error. We're talking about a missing/invalid ACPI control method name here, ENODEV seems approprirate.

Rafael, do you have a strong preference about what to return in such an event?

No, I don't, although -ENXIO could be used here too.

If you could say what value you'd like best I'll resend using that value. (I don't know what the effect is of using a specific error here, so I guess I'll have to bluff about it in the commit explanation.)

Thanks,

Paul Bolle

#18 lore raw
Quoted 30 lines · Paul Bolle

Hi Darren,

Quoted 8 lines · Rafael J. Wysocki
Quoted 7 lines · Darren Hart

On Thu, Sep 04, 2014 at 09:08:08AM +0200, Paul Bolle wrote:
[...]

Quoted 5 lines

static ssize_t store_sys_acpi(struct device *dev, int cm,

drivers/platform/x86/eeepc-laptop.c +0 -0
@@ -278,12 +276,13 @@ static ssize_t store_sys_acpi(struct device *dev, int cm,
 	struct eeepc_laptop *eeepc = dev_get_drvdata(dev);
 	int rv, value;
 
Quoted 11 lines
Quoted 11 lines

drivers/platform/x86/eeepc-laptop.c static ssize_t store_sys_acpi(struct device *dev, int cm, · line 283

Quoted 8 lines
drivers/platform/x86/eeepc-laptop.c +4 -3
-	rv = parse_arg(buf, count, &value);
-	if (rv > 0)
-		value = set_acpi(eeepc, cm, value);
+	rv = parse_arg(buf, &value);
+	if (rv < 0)
+		return rv;
+	value = set_acpi(eeepc, cm, value);
 	if (value < 0)

I suppose it's harmless, but it would be more explicit to reuse rv here instead of value.

Fine with me.

Quoted 5 lines
Quoted 5 lines

drivers/platform/x86/eeepc-laptop.c static ssize_t store_sys_acpi(struct device *dev, int cm, · line 284

Quoted 1 line
drivers/platform/x86/eeepc-laptop.c +0 -0
 		return -EIO;

And as with Frans' version, I suggest propogating the error. We're talking about a missing/invalid ACPI control method name here, ENODEV seems approprirate.

Quoted 7 lines
Quoted 3 lines
Quoted 1 line

Rafael, do you have a strong preference about what to return in such an event?

No, I don't, although -ENXIO could be used here too.

If you could say what value you'd like best I'll resend using that value. (I don't know what the effect is of using a specific error here, so I guess I'll have to bluff about it in the commit explanation.)

First, I would prefer we propogate the error code rather than remap it.

We could consider changing what the callee returns...

#define EIO              5      /* I/O error */
#define ENXIO            6      /* No such device or address */
#define ENODEV          19      /* No such device */

Of those, ENXIO seems like the most appropriate in this case.

Signature · 2 lines
Darren Hart
Intel Open Source Technology Center
#19 lore raw
Quoted 41 lines · Darren Hart
Quoted 30 lines · Paul Bolle

Hi Darren,

Quoted 8 lines · Rafael J. Wysocki
Quoted 7 lines · Darren Hart

On Thu, Sep 04, 2014 at 09:08:08AM +0200, Paul Bolle wrote:
[...]

Quoted 5 lines

static ssize_t store_sys_acpi(struct device *dev, int cm,

drivers/platform/x86/eeepc-laptop.c +0 -0
@@ -278,12 +276,13 @@ static ssize_t store_sys_acpi(struct device *dev, int cm,
        struct eeepc_laptop *eeepc = dev_get_drvdata(dev);
        int rv, value;
 
Quoted 11 lines
Quoted 11 lines

drivers/platform/x86/eeepc-laptop.c static ssize_t store_sys_acpi(struct device *dev, int cm, · line 283

Quoted 8 lines
drivers/platform/x86/eeepc-laptop.c +4 -3
-       rv = parse_arg(buf, count, &value);
-       if (rv > 0)
-               value = set_acpi(eeepc, cm, value);
+       rv = parse_arg(buf, &value);
+       if (rv < 0)
+               return rv;
+       value = set_acpi(eeepc, cm, value);
        if (value < 0)

I suppose it's harmless, but it would be more explicit to reuse rv here instead of value.

Fine with me.

Quoted 5 lines
Quoted 5 lines

drivers/platform/x86/eeepc-laptop.c static ssize_t store_sys_acpi(struct device *dev, int cm, · line 284

Quoted 1 line
drivers/platform/x86/eeepc-laptop.c +0 -0
                return -EIO;

And as with Frans' version, I suggest propogating the error. We're talking about a missing/invalid ACPI control method name here, ENODEV seems approprirate.

Quoted 7 lines
Quoted 3 lines
Quoted 1 line

Rafael, do you have a strong preference about what to return in such an event?

No, I don't, although -ENXIO could be used here too.

If you could say what value you'd like best I'll resend using that value. (I don't know what the effect is of using a specific error here, so I guess I'll have to bluff about it in the commit explanation.)

First, I would prefer we propogate the error code rather than remap it.

Quoted 7 lines

We could consider changing what the callee returns...

#define EIO              5      /* I/O error */
#define ENXIO            6      /* No such device or address */
#define ENODEV          19      /* No such device */

Of those, ENXIO seems like the most appropriate in this case.

Would it be fair to say that for consistency we should then also change the return values of acpi_setter_handle()? It has the same basic layout and checks as set_acpi() and get_acpi() have.

#20 lore raw
Quoted 48 lines · Frans Klaver
Quoted 47 lines · Darren Hart
Quoted 30 lines · Paul Bolle

Hi Darren,

Quoted 8 lines · Rafael J. Wysocki
Quoted 7 lines · Darren Hart

On Thu, Sep 04, 2014 at 09:08:08AM +0200, Paul Bolle wrote:
[...]

Quoted 5 lines

static ssize_t store_sys_acpi(struct device *dev, int cm,

drivers/platform/x86/eeepc-laptop.c +0 -0
@@ -278,12 +276,13 @@ static ssize_t store_sys_acpi(struct device *dev, int cm,
        struct eeepc_laptop *eeepc = dev_get_drvdata(dev);
        int rv, value;
 
Quoted 11 lines
Quoted 11 lines

drivers/platform/x86/eeepc-laptop.c static ssize_t store_sys_acpi(struct device *dev, int cm, · line 283

Quoted 8 lines
drivers/platform/x86/eeepc-laptop.c +4 -3
-       rv = parse_arg(buf, count, &value);
-       if (rv > 0)
-               value = set_acpi(eeepc, cm, value);
+       rv = parse_arg(buf, &value);
+       if (rv < 0)
+               return rv;
+       value = set_acpi(eeepc, cm, value);
        if (value < 0)

I suppose it's harmless, but it would be more explicit to reuse rv here instead of value.

Fine with me.

Quoted 5 lines
Quoted 5 lines

drivers/platform/x86/eeepc-laptop.c static ssize_t store_sys_acpi(struct device *dev, int cm, · line 284

Quoted 1 line
drivers/platform/x86/eeepc-laptop.c +0 -0
                return -EIO;

And as with Frans' version, I suggest propogating the error. We're talking about a missing/invalid ACPI control method name here, ENODEV seems approprirate.

Quoted 7 lines
Quoted 3 lines
Quoted 1 line

Rafael, do you have a strong preference about what to return in such an event?

No, I don't, although -ENXIO could be used here too.

If you could say what value you'd like best I'll resend using that value. (I don't know what the effect is of using a specific error here, so I guess I'll have to bluff about it in the commit explanation.)

First, I would prefer we propogate the error code rather than remap it.

We could consider changing what the callee returns...

#define EIO              5      /* I/O error */
#define ENXIO            6      /* No such device or address */
#define ENODEV          19      /* No such device */
Quoted 5 lines
Quoted 1 line

Of those, ENXIO seems like the most appropriate in this case.

Would it be fair to say that for consistency we should then also change the return values of acpi_setter_handle()? It has the same basic layout and checks as set_acpi() and get_acpi() have.

Yes, that would be appropriate as well.

Signature · 2 lines
Darren Hart
Intel Open Source Technology Center
#21 lore raw

PATCH v2 1/1 eeepc-laptop: simplify parse_arg()

parse_arg() has three possible return values:
-EINVAL if sscanf(), in short, fails;
zero if "count" is zero; and
"count" in all other cases

But "count" will never be zero. See, parse_arg() is called by the various store functions. And the callchain of these functions starts with sysfs_kf_write(). And that function checks for a zero "count". So we can stop checking for a zero "count", drop the "count" argument entirely, and transform parse_arg() into a function that returns zero on success or a negative error. That, in turn, allows to make those store functions just return "count" on success. The net effect is that the code becomes a bit easier to understand.

While we're at it, let store_sys_acpi() return whatever error set_acpi() returns instead of remapping it to EIO.

A nice side effect is that this GCC warning is silenced too:
drivers/platform/x86/eeepc-laptop.c: In function ‘store_sys_acpi’:
drivers/platform/x86/eeepc-laptop.c:279:10: warning: ‘value’ may be used uninitialized in this function [-Wmaybe-uninitialized]
int rv, value;

Which is, of course, the reason to have a look at parse_arg().

---
v2: let store_sys_acpi() return whatever error set_acpi() returns instead of remapping it to EIO. The new line about that in the commit explanation is silly, but I couldn't come up with a better explanation.

Still build tested only on top of v3.17-rc4 (I think Frans actually tested v1, which is almost identical).

There was a lot of discussion on the errors returned by this driver. I think using new error codes is actually out of scope for this patch and should be done in follow up patches.

 drivers/platform/x86/eeepc-laptop.c | 38 ++++++++++++++++++-------------------
 1 file changed, 19 insertions(+), 19 deletions(-)
drivers/platform/x86/eeepc-laptop.c +19 -19
index bd533c22be57..90be993b140e 100644
@@ -263,13 +263,11 @@ static int acpi_setter_handle(struct eeepc_laptop *eeepc, int cm,
 /*
  * Sys helpers
  */
-static int parse_arg(const char *buf, unsigned long count, int *val)
+static int parse_arg(const char *buf, int *val)
 {
-	if (!count)
-		return 0;
 	if (sscanf(buf, "%i", val) != 1)
 		return -EINVAL;
-	return count;
+	return 0;
 }
 
 static ssize_t store_sys_acpi(struct device *dev, int cm,
@@ -278,12 +276,13 @@ static ssize_t store_sys_acpi(struct device *dev, int cm,
 	struct eeepc_laptop *eeepc = dev_get_drvdata(dev);
 	int rv, value;
 
-	rv = parse_arg(buf, count, &value);
-	if (rv > 0)
-		value = set_acpi(eeepc, cm, value);
-	if (value < 0)
-		return -EIO;
-	return rv;
+	rv = parse_arg(buf, &value);
+	if (rv < 0)
+		return rv;
+	rv = set_acpi(eeepc, cm, value);
+	if (rv < 0)
+		return rv;
+	return count;
 }
 
 static ssize_t show_sys_acpi(struct device *dev, int cm, char *buf)
@@ -377,13 +376,13 @@ static ssize_t store_cpufv(struct device *dev,
 		return -EPERM;
 	if (get_cpufv(eeepc, &c))
 		return -ENODEV;
-	rv = parse_arg(buf, count, &value);
+	rv = parse_arg(buf, &value);
 	if (rv < 0)
 		return rv;
-	if (!rv || value < 0 || value >= c.num)
+	if (value < 0 || value >= c.num)
 		return -EINVAL;
 	set_acpi(eeepc, CM_ASL_CPUFV, value);
-	return rv;
+	return count;
 }
 
 static ssize_t show_cpufv_disabled(struct device *dev,
@@ -402,7 +401,7 @@ static ssize_t store_cpufv_disabled(struct device *dev,
 	struct eeepc_laptop *eeepc = dev_get_drvdata(dev);
 	int rv, value;
 
-	rv = parse_arg(buf, count, &value);
+	rv = parse_arg(buf, &value);
 	if (rv < 0)
 		return rv;
 
@@ -412,7 +411,7 @@ static ssize_t store_cpufv_disabled(struct device *dev,
 			pr_warn("cpufv enabled (not officially supported "
 				"on this model)\n");
 		eeepc->cpufv_disabled = false;
-		return rv;
+		return count;
 	case 1:
 		return -EPERM;
 	default:
@@ -1042,10 +1041,11 @@ static ssize_t store_sys_hwmon(void (*set)(int), const char *buf, size_t count)
 {
 	int rv, value;
 
-	rv = parse_arg(buf, count, &value);
-	if (rv > 0)
-		set(value);
-	return rv;
+	rv = parse_arg(buf, &value);
+	if (rv < 0)
+		return rv;
+	set(value);
+	return count;
 }
 
 static ssize_t show_sys_hwmon(int (*get)(void), char *buf)
Signature · 1 line
1.9.3
Applied
#22 lore raw

Re: [PATCH v2] eeepc-laptop: simplify parse_arg()

Quoted 17 lines · Paul Bolle

parse_arg() has three possible return values:
-EINVAL if sscanf(), in short, fails;
zero if "count" is zero; and
"count" in all other cases

But "count" will never be zero. See, parse_arg() is called by the various store functions. And the callchain of these functions starts with sysfs_kf_write(). And that function checks for a zero "count". So we can stop checking for a zero "count", drop the "count" argument entirely, and transform parse_arg() into a function that returns zero on success or a negative error. That, in turn, allows to make those store functions just return "count" on success. The net effect is that the code becomes a bit easier to understand.

While we're at it, let store_sys_acpi() return whatever error set_acpi() returns instead of remapping it to EIO.

Quoted 8 lines

A nice side effect is that this GCC warning is silenced too:
drivers/platform/x86/eeepc-laptop.c: In function ‘store_sys_acpi’:
drivers/platform/x86/eeepc-laptop.c:279:10: warning: ‘value’ may be used uninitialized in this function [-Wmaybe-uninitialized]
int rv, value;

Which is, of course, the reason to have a look at parse_arg().

Queued, thanks Paul.

Signature · 2 lines
Darren Hart
Intel Open Source Technology Center
#23 lore raw

Re: [PATCH v2] eeepc-laptop: simplify parse_arg()

Quoted 23 lines · Darren Hart
Quoted 22 lines · Paul Bolle

parse_arg() has three possible return values:
-EINVAL if sscanf(), in short, fails;
zero if "count" is zero; and
"count" in all other cases

But "count" will never be zero. See, parse_arg() is called by the various store functions. And the callchain of these functions starts with sysfs_kf_write(). And that function checks for a zero "count". So we can stop checking for a zero "count", drop the "count" argument entirely, and transform parse_arg() into a function that returns zero on success or a negative error. That, in turn, allows to make those store functions just return "count" on success. The net effect is that the code becomes a bit easier to understand.

While we're at it, let store_sys_acpi() return whatever error set_acpi() returns instead of remapping it to EIO.

A nice side effect is that this GCC warning is silenced too:
drivers/platform/x86/eeepc-laptop.c: In function ‘store_sys_acpi’:
drivers/platform/x86/eeepc-laptop.c:279:10: warning: ‘value’ may be used uninitialized in this function [-Wmaybe-uninitialized]
int rv, value;

Quoted 5 lines
Quoted 3 lines

Which is, of course, the reason to have a look at parse_arg().

Queued, thanks Paul.

After discussion with Linus, I have had to drop this patch from the queue. We need to restore the -EIO error code mapping to store_sys_acpi().

Apologies for the run around here, we had to define some policy around this.

Signature · 2 lines
Darren Hart
Intel Open Source Technology Center
#24 lore raw

PATCH v3 1/1 eeepc-laptop: simplify parse_arg()

parse_arg() has three possible return values:
-EINVAL if sscanf(), in short, fails;
zero if "count" is zero; and
"count" in all other cases

But "count" will never be zero. See, parse_arg() is called by the various store functions. And the callchain of these functions starts with sysfs_kf_write(). And that function checks for a zero "count". So we can stop checking for a zero "count", drop the "count" argument entirely, and transform parse_arg() into a function that returns zero on success or a negative error. That, in turn, allows to make those store functions just return "count" on success. The net effect is that the code becomes a bit easier to understand.

A nice side effect is that this GCC warning is silenced too:
drivers/platform/x86/eeepc-laptop.c: In function ‘store_sys_acpi’:
drivers/platform/x86/eeepc-laptop.c:279:10: warning: ‘value’ may be used uninitialized in this function [-Wmaybe-uninitialized]
int rv, value;

Which is, of course, the reason to have a look at parse_arg().

---
Still build tested only, but now on top of v3.17-rc5. Has Frans tested writing zero length values to these sysfs files?

v3: store_sys_acpi() again returns -EIO if set_acpi() fails.

v2: let store_sys_acpi() return whatever error set_acpi() returns instead of remapping it to EIO. The new line about that in the commit explanation is silly, but I couldn't come up with a better explanation.

 drivers/platform/x86/eeepc-laptop.c | 36 ++++++++++++++++++------------------
 1 file changed, 18 insertions(+), 18 deletions(-)
drivers/platform/x86/eeepc-laptop.c +18 -18
index bd533c22be57..3095d386c7f4 100644
@@ -263,13 +263,11 @@ static int acpi_setter_handle(struct eeepc_laptop *eeepc, int cm,
 /*
  * Sys helpers
  */
-static int parse_arg(const char *buf, unsigned long count, int *val)
+static int parse_arg(const char *buf, int *val)
 {
-	if (!count)
-		return 0;
 	if (sscanf(buf, "%i", val) != 1)
 		return -EINVAL;
-	return count;
+	return 0;
 }
 
 static ssize_t store_sys_acpi(struct device *dev, int cm,
@@ -278,12 +276,13 @@ static ssize_t store_sys_acpi(struct device *dev, int cm,
 	struct eeepc_laptop *eeepc = dev_get_drvdata(dev);
 	int rv, value;
 
-	rv = parse_arg(buf, count, &value);
-	if (rv > 0)
-		value = set_acpi(eeepc, cm, value);
-	if (value < 0)
+	rv = parse_arg(buf, &value);
+	if (rv < 0)
+		return rv;
+	rv = set_acpi(eeepc, cm, value);
+	if (rv < 0)
 		return -EIO;
-	return rv;
+	return count;
 }
 
 static ssize_t show_sys_acpi(struct device *dev, int cm, char *buf)
@@ -377,13 +376,13 @@ static ssize_t store_cpufv(struct device *dev,
 		return -EPERM;
 	if (get_cpufv(eeepc, &c))
 		return -ENODEV;
-	rv = parse_arg(buf, count, &value);
+	rv = parse_arg(buf, &value);
 	if (rv < 0)
 		return rv;
-	if (!rv || value < 0 || value >= c.num)
+	if (value < 0 || value >= c.num)
 		return -EINVAL;
 	set_acpi(eeepc, CM_ASL_CPUFV, value);
-	return rv;
+	return count;
 }
 
 static ssize_t show_cpufv_disabled(struct device *dev,
@@ -402,7 +401,7 @@ static ssize_t store_cpufv_disabled(struct device *dev,
 	struct eeepc_laptop *eeepc = dev_get_drvdata(dev);
 	int rv, value;
 
-	rv = parse_arg(buf, count, &value);
+	rv = parse_arg(buf, &value);
 	if (rv < 0)
 		return rv;
 
@@ -412,7 +411,7 @@ static ssize_t store_cpufv_disabled(struct device *dev,
 			pr_warn("cpufv enabled (not officially supported "
 				"on this model)\n");
 		eeepc->cpufv_disabled = false;
-		return rv;
+		return count;
 	case 1:
 		return -EPERM;
 	default:
@@ -1042,10 +1041,11 @@ static ssize_t store_sys_hwmon(void (*set)(int), const char *buf, size_t count)
 {
 	int rv, value;
 
-	rv = parse_arg(buf, count, &value);
-	if (rv > 0)
-		set(value);
-	return rv;
+	rv = parse_arg(buf, &value);
+	if (rv < 0)
+		return rv;
+	set(value);
+	return count;
 }
 
 static ssize_t show_sys_hwmon(int (*get)(void), char *buf)
Signature · 1 line
1.9.3
Applied
#25 lore raw

Re: [PATCH v3] eeepc-laptop: simplify parse_arg()

Quoted 26 lines · Paul Bolle

parse_arg() has three possible return values:
-EINVAL if sscanf(), in short, fails;
zero if "count" is zero; and
"count" in all other cases

But "count" will never be zero. See, parse_arg() is called by the various store functions. And the callchain of these functions starts with sysfs_kf_write(). And that function checks for a zero "count". So we can stop checking for a zero "count", drop the "count" argument entirely, and transform parse_arg() into a function that returns zero on success or a negative error. That, in turn, allows to make those store functions just return "count" on success. The net effect is that the code becomes a bit easier to understand.

A nice side effect is that this GCC warning is silenced too:
drivers/platform/x86/eeepc-laptop.c: In function ‘store_sys_acpi’:
drivers/platform/x86/eeepc-laptop.c:279:10: warning: ‘value’ may be used uninitialized in this function [-Wmaybe-uninitialized]
int rv, value;

Which is, of course, the reason to have a look at parse_arg().

---
Still build tested only, but now on top of v3.17-rc5. Has Frans tested writing zero length values to these sysfs files?

Quoted 5 lines

v3: store_sys_acpi() again returns -EIO if set_acpi() fails.

v2: let store_sys_acpi() return whatever error set_acpi() returns instead of remapping it to EIO. The new line about that in the commit explanation is silly, but I couldn't come up with a better explanation.

Queued, thanks.

Signature · 2 lines
Darren Hart
Intel Open Source Technology Center
#26 lore raw

Re: [PATCH v3] eeepc-laptop: simplify parse_arg()

In reply to Paul Bolle
Quoted 19 lines · Paul Bolle

parse_arg() has three possible return values:
-EINVAL if sscanf(), in short, fails;
zero if "count" is zero; and
"count" in all other cases

But "count" will never be zero. See, parse_arg() is called by the various store functions. And the callchain of these functions starts with sysfs_kf_write(). And that function checks for a zero "count". So we can stop checking for a zero "count", drop the "count" argument entirely, and transform parse_arg() into a function that returns zero on success or a negative error. That, in turn, allows to make those store functions just return "count" on success. The net effect is that the code becomes a bit easier to understand.

A nice side effect is that this GCC warning is silenced too:
drivers/platform/x86/eeepc-laptop.c: In function ‘store_sys_acpi’:
drivers/platform/x86/eeepc-laptop.c:279:10: warning: ‘value’ may be used uninitialized in this function [-Wmaybe-uninitialized]
int rv, value;

Quoted 6 lines

Which is, of course, the reason to have a look at parse_arg().

---
Still build tested only, but now on top of v3.17-rc5. Has Frans tested writing zero length values to these sysfs files?

Frans, can you confirm testing when you get a chance please?

Signature · 2 lines
Darren Hart
Intel Open Source Technology Center
#27 lore raw

Re: [PATCH v3] eeepc-laptop: simplify parse_arg()

Quoted 20 lines · Darren Hart
Quoted 19 lines · Paul Bolle

parse_arg() has three possible return values:
-EINVAL if sscanf(), in short, fails;
zero if "count" is zero; and
"count" in all other cases

But "count" will never be zero. See, parse_arg() is called by the various store functions. And the callchain of these functions starts with sysfs_kf_write(). And that function checks for a zero "count". So we can stop checking for a zero "count", drop the "count" argument entirely, and transform parse_arg() into a function that returns zero on success or a negative error. That, in turn, allows to make those store functions just return "count" on success. The net effect is that the code becomes a bit easier to understand.

A nice side effect is that this GCC warning is silenced too:
drivers/platform/x86/eeepc-laptop.c: In function ‘store_sys_acpi’:
drivers/platform/x86/eeepc-laptop.c:279:10: warning: ‘value’ may be used uninitialized in this function [-Wmaybe-uninitialized]
int rv, value;

Quoted 8 lines
Quoted 6 lines

Which is, of course, the reason to have a look at parse_arg().

---
Still build tested only, but now on top of v3.17-rc5. Has Frans tested writing zero length values to these sysfs files?

Frans, can you confirm testing when you get a chance please?

Will do.

Frans

#28 lore raw

Re: [PATCH v3] eeepc-laptop: simplify parse_arg()

In reply to Darren Hart
Quoted 20 lines · Darren Hart
Quoted 19 lines · Paul Bolle

parse_arg() has three possible return values:
-EINVAL if sscanf(), in short, fails;
zero if "count" is zero; and
"count" in all other cases

But "count" will never be zero. See, parse_arg() is called by the various store functions. And the callchain of these functions starts with sysfs_kf_write(). And that function checks for a zero "count". So we can stop checking for a zero "count", drop the "count" argument entirely, and transform parse_arg() into a function that returns zero on success or a negative error. That, in turn, allows to make those store functions just return "count" on success. The net effect is that the code becomes a bit easier to understand.

A nice side effect is that this GCC warning is silenced too:
drivers/platform/x86/eeepc-laptop.c: In function ‘store_sys_acpi’:
drivers/platform/x86/eeepc-laptop.c:279:10: warning: ‘value’ may be used uninitialized in this function [-Wmaybe-uninitialized]
int rv, value;

Quoted 8 lines
Quoted 6 lines

Which is, of course, the reason to have a look at parse_arg().

---
Still build tested only, but now on top of v3.17-rc5. Has Frans tested writing zero length values to these sysfs files?

Frans, can you confirm testing when you get a chance please?

Tested. Looks OK to me.

Frans

#29 lore raw
In reply to Frans Klaver
Quoted 8 lines · Frans Klaver

In store_sys_acpi, if count equals zero, or parse_arg()s sscanf call fails, 'value' remains possibly uninitialized. In that case 'value' shouldn't be used to produce the store_sys_acpi()s return value.

Only test the return value of set_acpi() if we can actually call it. Return rv otherwise.

FYI, based on recent discussion with Paul, Greg, and Frans, I am not picking this up, but waiting for a next rev from Frans, possibly including Paul's original patch.

Signature · 2 lines
Darren Hart
Intel Open Source Technology Center