Skip to contentSkip to conversation

ACPI / sysfs: Restructure get_status

PATCH In review Nathan Chancellor 3 participants 5 messages Mar 7, 2019 – Mar 12, 2019 ACPI

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ACPI / sysfs: Restructure get_status

When building with -Wsometimes-uninitialized, Clang warns:

drivers/acpi/sysfs.c:667:13: warning: variable 'result' is used uninitialized whenever 'if' condition is false [-Wsometimes-uninitialized]

Clang can't determine that all cases are covered by the two separate if statements. We could combine then to look like this:

    int result;

    if (...) {
        ...
    } else if {
        ...
    } else {
        result -EINVAL;
    }

    return result;

However, at that point, we can further simplify this function by only using result when absolutely needed and just direct returning the value of the function.

---
 drivers/acpi/sysfs.c | 13 ++++---------
 1 file changed, 4 insertions(+), 9 deletions(-)
drivers/acpi/sysfs.c +4 -9
index 41324f0b1bee..6ed785cadad9 100644
@@ -651,23 +651,18 @@ static void acpi_global_event_handler(u32 event_type, acpi_handle device,
 static int get_status(u32 index, acpi_event_status *status,
 		      acpi_handle *handle)
 {
-	int result;
-
-	if (index >= num_gpes + ACPI_NUM_FIXED_EVENTS)
-		return -EINVAL;
-
 	if (index < num_gpes) {
-		result = acpi_get_gpe_device(index, handle);
+		int result = acpi_get_gpe_device(index, handle);
 		if (result) {
 			ACPI_EXCEPTION((AE_INFO, AE_NOT_FOUND,
 					"Invalid GPE 0x%x", index));
 			return result;
 		}
-		result = acpi_get_gpe_status(*handle, index, status);
+		return acpi_get_gpe_status(*handle, index, status);
 	} else if (index < (num_gpes + ACPI_NUM_FIXED_EVENTS))
-		result = acpi_get_event_status(index - num_gpes, status);
+		return acpi_get_event_status(index - num_gpes, status);
 
-	return result;
+	return -EINVAL;
 }
 
 static ssize_t counter_show(struct kobject *kobj,
Signature · 1 line
2.21.0
Reviewed-by on v1 1/1
#1 lore raw
Quoted 37 lines · Nathan Chancellor

When building with -Wsometimes-uninitialized, Clang warns:

drivers/acpi/sysfs.c:667:13: warning: variable 'result' is used uninitialized whenever 'if' condition is false [-Wsometimes-uninitialized]

Clang can't determine that all cases are covered by the two separate if statements. We could combine then to look like this:

    int result;

    if (...) {
        ...
    } else if {
        ...
    } else {
        result -EINVAL;
    }

    return result;

However, at that point, we can further simplify this function by only using result when absolutely needed and just direct returning the value of the function.

---
 drivers/acpi/sysfs.c | 13 ++++---------
 1 file changed, 4 insertions(+), 9 deletions(-)
drivers/acpi/sysfs.c +0 -0
index 41324f0b1bee..6ed785cadad9 100644
@@ -651,23 +651,18 @@ static void acpi_global_event_handler(u32 event_type, acpi_handle device,
 static int get_status(u32 index, acpi_event_status *status,

drivers/acpi/sysfs.c static void acpi_global_event_handler(u32 event_type, acpi_handle device, · line 655

Quoted 10 lines
drivers/acpi/sysfs.c +1 -6
                      acpi_handle *handle)
 {
-       int result;
-
-       if (index >= num_gpes + ACPI_NUM_FIXED_EVENTS)
-               return -EINVAL;
-
        if (index < num_gpes) {
-               result = acpi_get_gpe_device(index, handle);
+               int result = acpi_get_gpe_device(index, handle);

I'm surprised this does not trip -Wdeclaration-after-statement.

Quoted 4 lines
drivers/acpi/sysfs.c +0 -0
                if (result) {
                        ACPI_EXCEPTION((AE_INFO, AE_NOT_FOUND,
                                        "Invalid GPE 0x%x", index));
                        return result;

drivers/acpi/sysfs.c static void acpi_global_event_handler(u32 event_type, acpi_handle device, · line 666

Quoted 10 lines
drivers/acpi/sysfs.c +3 -3
                }
-               result = acpi_get_gpe_status(*handle, index, status);
+               return acpi_get_gpe_status(*handle, index, status);
        } else if (index < (num_gpes + ACPI_NUM_FIXED_EVENTS))
-               result = acpi_get_event_status(index - num_gpes, status);
+               return acpi_get_event_status(index - num_gpes, status);
 
-       return result;
+       return -EINVAL;
 }

LGTM, thanks Nathan!

Signature · 2 lines
Thanks,
~Nick Desaulniers
#2 lore raw
In reply to Nathan Chancellor
Quoted 44 lines · CET Nathan Chancellor

When building with -Wsometimes-uninitialized, Clang warns:

drivers/acpi/sysfs.c:667:13: warning: variable 'result' is used uninitialized whenever 'if' condition is false [-Wsometimes-uninitialized]

Clang can't determine that all cases are covered by the two separate if statements. We could combine then to look like this:

    int result;

    if (...) {
        ...
    } else if {
        ...
    } else {
        result -EINVAL;
    }

    return result;

However, at that point, we can further simplify this function by only using result when absolutely needed and just direct returning the value of the function.

---
 drivers/acpi/sysfs.c | 13 ++++---------
 1 file changed, 4 insertions(+), 9 deletions(-)
drivers/acpi/sysfs.c +0 -5
index 41324f0b1bee..6ed785cadad9 100644
@@ -651,23 +651,18 @@ static void acpi_global_event_handler(u32 event_type, acpi_handle device,
 static int get_status(u32 index, acpi_event_status *status,
 		      acpi_handle *handle)
 {
-	int result;
-
-	if (index >= num_gpes + ACPI_NUM_FIXED_EVENTS)
-		return -EINVAL;
-
 	if (index < num_gpes) {

drivers/acpi/sysfs.c static void acpi_global_event_handler(u32 event_type, acpi_handle device, · line 662

Quoted 10 lines
drivers/acpi/sysfs.c +2 -2
-		result = acpi_get_gpe_device(index, handle);
+		int result = acpi_get_gpe_device(index, handle);
 		if (result) {
 			ACPI_EXCEPTION((AE_INFO, AE_NOT_FOUND,
 					"Invalid GPE 0x%x", index));
 			return result;
 		}
-		result = acpi_get_gpe_status(*handle, index, status);
+		return acpi_get_gpe_status(*handle, index, status);
 	} else if (index < (num_gpes + ACPI_NUM_FIXED_EVENTS))

In principle, it would suffice to replace the "else if (...)" with "else" to fix the Clang warning AFAICS, but that's not the only issue with this function.

drivers/acpi/sysfs.c static void acpi_global_event_handler(u32 event_type, acpi_handle device, · line 668

Quoted 8 lines
drivers/acpi/sysfs.c +2 -2
-		result = acpi_get_event_status(index - num_gpes, status);
+		return acpi_get_event_status(index - num_gpes, status);
 
-	return result;
+	return -EINVAL;
 }
 
 static ssize_t counter_show(struct kobject *kobj,

Namely, it confuses errno with acpi_status and may cause the latter to be returned to user space in certain situations which should never be done.

So, I'd prefer to apply something like the (untested so far) patch below.

---
 drivers/acpi/sysfs.c |   27 ++++++++++++++-------------
 1 file changed, 14 insertions(+), 13 deletions(-)
drivers/acpi/sysfs.c +14 -13
Index: linux-pm/drivers/acpi/sysfs.c
===================================================================
@@ -648,26 +648,27 @@ static void acpi_global_event_handler(u3
 	}
 }
 
-static int get_status(u32 index, acpi_event_status *status,
+static int get_status(u32 index, acpi_event_status *ret,
 		      acpi_handle *handle)
 {
-	int result;
+	acpi_status status;
 
 	if (index >= num_gpes + ACPI_NUM_FIXED_EVENTS)
 		return -EINVAL;
 
-	if (index < num_gpes) {
-		result = acpi_get_gpe_device(index, handle);
-		if (result) {
-			ACPI_EXCEPTION((AE_INFO, AE_NOT_FOUND,
-					"Invalid GPE 0x%x", index));
-			return result;
-		}
-		result = acpi_get_gpe_status(*handle, index, status);
-	} else if (index < (num_gpes + ACPI_NUM_FIXED_EVENTS))
-		result = acpi_get_event_status(index - num_gpes, status);
+	if (index >= num_gpes)
+		return acpi_get_event_status(index - num_gpes, ret);
 
-	return result;
+	status = acpi_get_gpe_device(index, handle);
+	if (ACPI_FAILURE(status)) {
+		ACPI_EXCEPTION((AE_INFO, AE_NOT_FOUND, "Invalid GPE 0x%x", index));
+		return -ENXIO;
+	}
+	status = acpi_get_gpe_status(*handle, index, ret);
+	if (ACPI_FAILURE(status))
+		return -EIO;
+
+	return 0;
 }
 
 static ssize_t counter_show(struct kobject *kobj,
#3 lore raw
Quoted 67 lines · CET Rafael J. Wysocki
Quoted 44 lines · CET Nathan Chancellor

When building with -Wsometimes-uninitialized, Clang warns:

drivers/acpi/sysfs.c:667:13: warning: variable 'result' is used uninitialized whenever 'if' condition is false [-Wsometimes-uninitialized]

Clang can't determine that all cases are covered by the two separate if statements. We could combine then to look like this:

    int result;

    if (...) {
        ...
    } else if {
        ...
    } else {
        result -EINVAL;
    }

    return result;

However, at that point, we can further simplify this function by only using result when absolutely needed and just direct returning the value of the function.

---
 drivers/acpi/sysfs.c | 13 ++++---------
 1 file changed, 4 insertions(+), 9 deletions(-)
drivers/acpi/sysfs.c +0 -5
index 41324f0b1bee..6ed785cadad9 100644
@@ -651,23 +651,18 @@ static void acpi_global_event_handler(u32 event_type, acpi_handle device,
 static int get_status(u32 index, acpi_event_status *status,
 		      acpi_handle *handle)
 {
-	int result;
-
-	if (index >= num_gpes + ACPI_NUM_FIXED_EVENTS)
-		return -EINVAL;
-
 	if (index < num_gpes) {

drivers/acpi/sysfs.c static void acpi_global_event_handler(u32 event_type, acpi_handle device, · line 662

Quoted 10 lines
drivers/acpi/sysfs.c +2 -2
-		result = acpi_get_gpe_device(index, handle);
+		int result = acpi_get_gpe_device(index, handle);
 		if (result) {
 			ACPI_EXCEPTION((AE_INFO, AE_NOT_FOUND,
 					"Invalid GPE 0x%x", index));
 			return result;
 		}
-		result = acpi_get_gpe_status(*handle, index, status);
+		return acpi_get_gpe_status(*handle, index, status);
 	} else if (index < (num_gpes + ACPI_NUM_FIXED_EVENTS))

In principle, it would suffice to replace the "else if (...)" with "else" to fix the Clang warning AFAICS, but that's not the only issue with this function.

Quoted 7 lines
drivers/acpi/sysfs.c +2 -2
-		result = acpi_get_event_status(index - num_gpes, status);
+		return acpi_get_event_status(index - num_gpes, status);
 
-	return result;
+	return -EINVAL;
 }
 
Quoted 6 lines

drivers/acpi/sysfs.c static void acpi_global_event_handler(u32 event_type, acpi_handle device, · line 668

Quoted 1 line
drivers/acpi/sysfs.c +0 -0
 static ssize_t counter_show(struct kobject *kobj,

Namely, it confuses errno with acpi_status and may cause the latter to be returned to user space in certain situations which should never be done.

So, I'd prefer to apply something like the (untested so far) patch below.

Actually, acpi_get_event_status() returns acpi_status too, so something like this rather:

---
 drivers/acpi/sysfs.c |   21 ++++++++++++---------
 1 file changed, 12 insertions(+), 9 deletions(-)
drivers/acpi/sysfs.c +12 -9
Index: linux-pm/drivers/acpi/sysfs.c
===================================================================
@@ -648,26 +648,29 @@ static void acpi_global_event_handler(u3
 	}
 }
 
-static int get_status(u32 index, acpi_event_status *status,
+static int get_status(u32 index, acpi_event_status *ret,
 		      acpi_handle *handle)
 {
-	int result;
+	acpi_status status;
 
 	if (index >= num_gpes + ACPI_NUM_FIXED_EVENTS)
 		return -EINVAL;
 
 	if (index < num_gpes) {
-		result = acpi_get_gpe_device(index, handle);
-		if (result) {
+		status = acpi_get_gpe_device(index, handle);
+		if (ACPI_FAILURE(status)) {
 			ACPI_EXCEPTION((AE_INFO, AE_NOT_FOUND,
 					"Invalid GPE 0x%x", index));
-			return result;
+			return -ENXIO;
 		}
-		result = acpi_get_gpe_status(*handle, index, status);
-	} else if (index < (num_gpes + ACPI_NUM_FIXED_EVENTS))
-		result = acpi_get_event_status(index - num_gpes, status);
+		status = acpi_get_gpe_status(*handle, index, ret);
+	} else {
+		status = acpi_get_event_status(index - num_gpes, ret);
+	}
+	if (ACPI_FAILURE(status))
+		return -EIO;
 
-	return result;
+	return 0;
 }
 
 static ssize_t counter_show(struct kobject *kobj,
author Reviewed-by
#4 lore raw
Quoted 53 lines · Rafael J. Wysocki
Quoted 52 lines · CET Rafael J. Wysocki
Quoted 51 lines · CET Nathan Chancellor

When building with -Wsometimes-uninitialized, Clang warns:

drivers/acpi/sysfs.c:667:13: warning: variable 'result' is used uninitialized whenever 'if' condition is false [-Wsometimes-uninitialized]

Clang can't determine that all cases are covered by the two separate if statements. We could combine then to look like this:

    int result;

    if (...) {
        ...
    } else if {
        ...
    } else {
        result -EINVAL;
    }

    return result;

However, at that point, we can further simplify this function by only using result when absolutely needed and just direct returning the value of the function.

---
 drivers/acpi/sysfs.c | 13 ++++---------
 1 file changed, 4 insertions(+), 9 deletions(-)
drivers/acpi/sysfs.c +1 -6
index 41324f0b1bee..6ed785cadad9 100644
@@ -651,23 +651,18 @@ static void acpi_global_event_handler(u32 event_type, acpi_handle device,
 static int get_status(u32 index, acpi_event_status *status,
 		      acpi_handle *handle)
 {
-	int result;
-
-	if (index >= num_gpes + ACPI_NUM_FIXED_EVENTS)
-		return -EINVAL;
-
 	if (index < num_gpes) {
-		result = acpi_get_gpe_device(index, handle);
+		int result = acpi_get_gpe_device(index, handle);
 		if (result) {
 			ACPI_EXCEPTION((AE_INFO, AE_NOT_FOUND,
 					"Invalid GPE 0x%x", index));
 			return result;
 		}
Quoted 8 lines
Quoted 8 lines

drivers/acpi/sysfs.c static void acpi_global_event_handler(u32 event_type, acpi_handle device, · line 662

Quoted 3 lines
drivers/acpi/sysfs.c +1 -1
-		result = acpi_get_gpe_status(*handle, index, status);
+		return acpi_get_gpe_status(*handle, index, status);
 	} else if (index < (num_gpes + ACPI_NUM_FIXED_EVENTS))

In principle, it would suffice to replace the "else if (...)" with "else" to fix the Clang warning AFAICS, but that's not the only issue with this function.

Correct, that would be the simplest fix for the warning.

Quoted 55 lines
Quoted 7 lines
Quoted 7 lines
drivers/acpi/sysfs.c +2 -2
-		result = acpi_get_event_status(index - num_gpes, status);
+		return acpi_get_event_status(index - num_gpes, status);
 
-	return result;
+	return -EINVAL;
 }
 
Quoted 6 lines

drivers/acpi/sysfs.c static void acpi_global_event_handler(u3 · line 676

Quoted 1 line
drivers/acpi/sysfs.c +0 -0
 static ssize_t counter_show(struct kobject *kobj,

Namely, it confuses errno with acpi_status and may cause the latter to be returned to user space in certain situations which should never be done.

So, I'd prefer to apply something like the (untested so far) patch below.

Actually, acpi_get_event_status() returns acpi_status too, so something like this rather:

---
 drivers/acpi/sysfs.c |   21 ++++++++++++---------
 1 file changed, 12 insertions(+), 9 deletions(-)

Index: linux-pm/drivers/acpi/sysfs.c
===================================================================

drivers/acpi/sysfs.c +7 -8
@@ -648,26 +648,29 @@ static void acpi_global_event_handler(u3
 	}
 }
 
-static int get_status(u32 index, acpi_event_status *status,
+static int get_status(u32 index, acpi_event_status *ret,
 		      acpi_handle *handle)
 {
-	int result;
+	acpi_status status;
 
 	if (index >= num_gpes + ACPI_NUM_FIXED_EVENTS)
 		return -EINVAL;
 
 	if (index < num_gpes) {
-		result = acpi_get_gpe_device(index, handle);
-		if (result) {
+		status = acpi_get_gpe_device(index, handle);
+		if (ACPI_FAILURE(status)) {
 			ACPI_EXCEPTION((AE_INFO, AE_NOT_FOUND,
 					"Invalid GPE 0x%x", index));
-			return result;
+			return -ENXIO;
 		}
-		result = acpi_get_gpe_status(*handle, index, status);
-	} else if (index < (num_gpes + ACPI_NUM_FIXED_EVENTS))
-		result = acpi_get_event_status(index - num_gpes, status);
+		status = acpi_get_gpe_status(*handle, index, ret);
+	} else {

drivers/acpi/sysfs.c static void acpi_global_event_handler(u3 · line 676

Quoted 10 lines
drivers/acpi/sysfs.c +5 -1
+		status = acpi_get_event_status(index - num_gpes, ret);
+	}
+	if (ACPI_FAILURE(status))
+		return -EIO;
 
-	return result;
+	return 0;
 }
 
 static ssize_t counter_show(struct kobject *kobj,

Seems reasonable. There are no warnings from this.

Thank you for the reply and patch!