Skip to contentSkip to conversation

x86/hw_breakpoints: Return -EINVAL in default case in hw_breakpoint_arch_parse

PATCH In review Nathan Chancellor 3 participants 4 messages Mar 7, 2019 – Mar 22, 2019 x86

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 x86/hw_breakpoints: Return -EINVAL in default case in hw_breakpoint_arch_parse

When building with -Wsometimes-uninitialized, Clang warns:

arch/x86/kernel/hw_breakpoint.c:355:2: warning: variable 'align' is used uninitialized whenever switch default is taken [-Wsometimes-uninitialized]

It's not wrong but we can see that arch_build_bp_info will only ever initialize hw->len to one of the specified switch cases. We can easily silence Clang by just returning -EINVAL in the default case so that we can never use align without initializing it first.

---
 arch/x86/kernel/hw_breakpoint.c | 1 +
 1 file changed, 1 insertion(+)
arch/x86/kernel/hw_breakpoint.c +1 -0
index ff9bfd40429e..d73083021002 100644
@@ -354,6 +354,7 @@ int hw_breakpoint_arch_parse(struct perf_event *bp,
 #endif
 	default:
 		WARN_ON_ONCE(1);
+		return -EINVAL;
 	}
 
 	/*
Signature · 1 line
2.21.0
Reviewed-by on v1 1/1
#1 lore raw
Quoted 11 lines · Nathan Chancellor

When building with -Wsometimes-uninitialized, Clang warns:

arch/x86/kernel/hw_breakpoint.c:355:2: warning: variable 'align' is used uninitialized whenever switch default is taken [-Wsometimes-uninitialized]

It's not wrong but we can see that arch_build_bp_info will only ever initialize hw->len to one of the specified switch cases. We can easily silence Clang by just returning -EINVAL in the default case so that we can never use align without initializing it first.

And the call site just bubbles up return codes anyways. Thanks for sending.

Quoted 23 lines
---
 arch/x86/kernel/hw_breakpoint.c | 1 +
 1 file changed, 1 insertion(+)
arch/x86/kernel/hw_breakpoint.c +1 -1
index ff9bfd40429e..d73083021002 100644
@@ -354,6 +354,7 @@ int hw_breakpoint_arch_parse(struct perf_event *bp,
 #endif
        default:
                WARN_ON_ONCE(1);
+               return -EINVAL;
        }
 
        /*
--

2.21.0

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

When building with -Wsometimes-uninitialized, Clang warns:

arch/x86/kernel/hw_breakpoint.c:355:2: warning: variable 'align' is used uninitialized whenever switch default is taken [-Wsometimes-uninitialized]

It's not wrong but we can see that arch_build_bp_info will only ever initialize hw->len to one of the specified switch cases. We can easily silence Clang by just returning -EINVAL in the default case so that we can never use align without initializing it first.

---
 arch/x86/kernel/hw_breakpoint.c | 1 +
 1 file changed, 1 insertion(+)
arch/x86/kernel/hw_breakpoint.c +1 -0
index ff9bfd40429e..d73083021002 100644
@@ -354,6 +354,7 @@ int hw_breakpoint_arch_parse(struct perf_event *bp,
 #endif
 	default:
 		WARN_ON_ONCE(1);
+		return -EINVAL;
 	}
 
 	/*
Signature · 1 line
2.21.0

Gentle ping (if there was a response to this, I didn't receive it). I know I sent it in the middle of a merge window so I get if it slipped through the cracks.

Thanks,
Nathan

#3 lore raw

[tip:x86/urgent] x86/hw_breakpoints: Make default case in hw_breakpoint_arch_parse() return an error

In reply to Nathan Chancellor
x86/hw_breakpoints: Make default case in hw_breakpoint_arch_parse() return an error
Commit-ID:  e898e69d6b9475bf123f99b3c5d1a67bb7cb2361
Gitweb:     https://git.kernel.org/tip/e898e69d6b9475bf123f99b3c5d1a67bb7cb2361
Author:     Nathan Chancellor <[email protected]>
AuthorDate: Thu, 7 Mar 2019 14:27:56 -0700
Committer:  Thomas Gleixner <[email protected]>
CommitDate: Fri, 22 Mar 2019 17:08:17 +0100

x86/hw_breakpoints: Make default case in hw_breakpoint_arch_parse() return an error

When building with -Wsometimes-uninitialized, Clang warns:

arch/x86/kernel/hw_breakpoint.c:355:2: warning: variable 'align' is used uninitialized whenever switch default is taken [-Wsometimes-uninitialized]

The default cannot be reached because arch_build_bp_info() initializes hw->len to one of the specified cases. Nevertheless the warning is valid and returning -EINVAL makes sure that this cannot be broken by future modifications.

---
 arch/x86/kernel/hw_breakpoint.c | 1 +
 1 file changed, 1 insertion(+)
arch/x86/kernel/hw_breakpoint.c +1 -0
index ff9bfd40429e..d73083021002 100644
@@ -354,6 +354,7 @@ int hw_breakpoint_arch_parse(struct perf_event *bp,
 #endif
 	default:
 		WARN_ON_ONCE(1);
+		return -EINVAL;
 	}
 
 	/*