debugfs: Check return value of debugfs_real_fops() for NULL
PATCH 1/1 debugfs: Check return value of debugfs_real_fops() for NULL
debugfs_real_fops() returns a NULL pointer when it is invoked without a prior call to debugfs_file_get(). In code paths including this call it is not strictly necessary to check the return value of debugfs_real_fops(). However clang inlines debugfs_real_fops(), detects the invalid dereferencing of the NULL pointer and drops the code path. This leads to a bunch of objtool warnings when building with clang and CONFIG_UNWINDER_ORC=y:
fs/debugfs/file.o: warning: objtool: full_proxy_llseek() falls through to next function full_proxy_read() fs/debugfs/file.o: warning: objtool: full_proxy_read() falls through to next function full_proxy_write() fs/debugfs/file.o: warning: objtool: full_proxy_write() falls through to next function full_proxy_poll() fs/debugfs/file.o: warning: objtool: full_proxy_poll() falls through to next function full_proxy_unlocked_ioctl() fs/debugfs/file.o: warning: objtool: full_proxy_unlocked_ioctl() falls through to next function fops_u8_open()
Check the pointer returned by debugfs_real_fops() in all code paths to make clang and objtool happy.
--- fs/debugfs/file.c | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-)
fs/debugfs/file.c +9 -1
@@ -183,7 +183,8 @@ static ret_type full_proxy_ ## name(proto) \if (unlikely (r)) \return r; \ real_fops =debugfs_real_fops (filp); \ - r = real_fops->name (args); \ +if (real_fops) \ + r = real_fops->name (args); \debugfs_file_put (dentry); \return r; \ }
@@ -217,7 +218,14 @@ static unsigned int full_proxy_poll(struct file *filp,return POLLHUP ; real_fops =debugfs_real_fops (filp); +if (!real_fops) { + r = -ENXIO ; + goto out; + } + r = real_fops->poll (filp, wait); + +out:debugfs_file_put (dentry);return r; }
Signature · 1 line
2.17.0.rc1.321.gba9d0f2565-goog
Greg Kroah-Hartman
maintainerQuoted 5 lines · Matthias Kaehlcke
debugfs_real_fops()returns a NULL pointer when it is invoked without a prior call todebugfs_file_get(). In code paths including this call it is not strictly necessary to check the return value ofdebugfs_real_fops(). However clang inlinesdebugfs_real_fops(), detects the invalid dereferencing of the NULL pointer and drops the code path.
Wait, what? Why would it do that, because it thinks dereferencing NULL is undefined behaviour and it can just do whatever it wants to?
That feels crazy, as for these calls we "know" it will never be NULL because the previous call to debugfs_file_get() will always ensure it will be correct.
So this is a case of the compiler trying to be smarter than it really is, and getting things totally wrong :(
Has anyone reported this to the clang developers?
Papering over compiler foolishness is not something I like to do in kernel code if at all possible...
thanks,
greg k-h
Please note that there is nothing wrong in the generated code, just that it confuses objtool.
Clang has simply omitted the statement where NULL is returned since the pointer was always dereferenced post inlining.
Note that GCC will also remove the NULL pointers if it knows that the pointer is dereferenced.
Here is an example.
void null_check(int *P) {
int deref = *P;
if (P == 0) // GCC won't check the condition.
return;
*P = 4;
}
Compiling with gcc -O2 gives:
movl $4, (%rdi)
retThanks,
Manoj
Quoted 25 lines · Greg Kroah-Hartman
Quoted 5 lines · Matthias Kaehlcke
debugfs_real_fops()returns a NULL pointer when it is invoked without a prior call todebugfs_file_get(). In code paths including this call it is not strictly necessary to check the return value ofdebugfs_real_fops(). However clang inlinesdebugfs_real_fops(), detects the invalid dereferencing of the NULL pointer and drops the code path.Wait, what? Why would it do that, because it thinks dereferencing NULL is undefined behaviour and it can just do whatever it wants to?
That feels crazy, as for these calls we "know" it will never be NULL because the previous call to
debugfs_file_get()will always ensure it will be correct.So this is a case of the compiler trying to be smarter than it really is, and getting things totally wrong :(
Has anyone reported this to the clang developers?
Papering over compiler foolishness is not something I like to do in kernel code if at all possible...
thanks,
greg k-h
Quoted 4 lines · Manoj Gupta
Please note that there is nothing wrong in the generated code, just that it confuses objtool.
Clang has simply omitted the statement where NULL is returned since the pointer was always dereferenced post inlining.
... but returning NULL would be far more sane than falling through to the next function.
Quoted 7 lines
Note that GCC will also remove the NULL pointers if it knows that the pointer is dereferenced.
Here is an example.void
null_check(int *P){
int deref = *P;
if (P == 0) // GCC won't check the condition.
Quoted 7 lines
return;*P = 4;
}Compiling with gcc -O2 gives:
movl $4, (%rdi) ret
This is why we use -fno-delete-null-pointer-checks.
Signature · 1 line
Josh
Quoted 8 lines · Josh Poimboeuf
Quoted 4 lines · Manoj Gupta
Please note that there is nothing wrong in the generated code, just that it confuses objtool.
Clang has simply omitted the statement where NULL is returned since the pointer was always dereferenced post inlining.... but returning NULL would be far more sane than falling through to the next function.
Or, as the case may be, oopsing at the point of failure.
Quoted 16 lines
Quoted 7 lines
Note that GCC will also remove the NULL pointers if it knows that the pointer is dereferenced.
Here is an example.void
null_check(int *P){
int deref = *P;
if (P == 0) // GCC won't check the condition.Quoted 7 lines
return;*P = 4;
}Compiling with gcc -O2 gives:
movl $4, (%rdi) retThis is why we use -fno-delete-null-pointer-checks.
Signature · 1 line
Josh
Greg Kroah-Hartman
maintainerA: Because it messes up the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing in e-mail?
A: No.
Q: Should I include quotations after my reply?
http://daringfireball.net/2007/07/on_top
Quoted 2 lines · Manoj Gupta
Please note that there is nothing wrong in the generated code, just that it confuses objtool.
Then fix the tool, the C code is correct :)
Quoted 2 lines
Clang has simply omitted the statement where NULL is returned since the pointer was always dereferenced post inlining.
Then tell clang not to do that, like we tell gcc not to do that as that is a foolish thing for a compiler to do when building the kernel.
thanks,
greg k-h
El Wed, Mar 28, 2018 at 08:05:56PM +0200 Greg Kroah-Hartman ha dit:
Quoted 15 lines
A: Because it messes up the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing in e-mail?A: No.
Q: Should I include quotations after my reply?http://daringfireball.net/2007/07/on_top
Quoted 2 lines · Manoj Gupta
Please note that there is nothing wrong in the generated code, just that it confuses objtool.
Quoted 7 lines
Then fix the tool, the C code is correct :)
Quoted 2 lines
Clang has simply omitted the statement where NULL is returned since the pointer was always dereferenced post inlining.
Then tell clang not to do that, like we tell gcc not to do that as that is a foolish thing for a compiler to do when building the kernel.
Thanks all for your input, we'll try to get -fno-delete-null-pointer-checks or a similar flag to be added to clang.
Greg Kroah-Hartman
maintainerQuoted 22 lines · Matthias Kaehlcke
El Wed, Mar 28, 2018 at 08:05:56PM +0200 Greg Kroah-Hartman ha dit:
Quoted 20 lines
A: Because it messes up the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing in e-mail?A: No.
Q: Should I include quotations after my reply?http://daringfireball.net/2007/07/on_top
Quoted 2 lines · Manoj Gupta
Please note that there is nothing wrong in the generated code, just that it confuses objtool.
Then fix the tool, the C code is correct :)
Quoted 2 lines
Clang has simply omitted the statement where NULL is returned since the pointer was always dereferenced post inlining.
Quoted 6 lines
Quoted 2 lines
Then tell clang not to do that, like we tell gcc not to do that as that is a foolish thing for a compiler to do when building the kernel.
Thanks all for your input, we'll try to get -fno-delete-null-pointer-checks or a similar flag to be added to clang.
Wait, clang does not have that? That's crazy, how has this not been hit yet when building the kernel?
confused,
greg k-h
Matthias Kaehlcke
Tested-by Sodagudi PrasadEl Wed, Mar 28, 2018 at 08:19:36PM +0200 Greg Kroah-Hartman ha dit:
Quoted 23 lines · Matthias Kaehlcke
Quoted 22 lines
El Wed, Mar 28, 2018 at 08:05:56PM +0200 Greg Kroah-Hartman ha dit:
Quoted 20 lines
A: Because it messes up the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing in e-mail?A: No.
Q: Should I include quotations after my reply?http://daringfireball.net/2007/07/on_top
Quoted 2 lines · Manoj Gupta
Please note that there is nothing wrong in the generated code, just that it confuses objtool.
Then fix the tool, the C code is correct :)
Quoted 2 lines
Clang has simply omitted the statement where NULL is returned since the pointer was always dereferenced post inlining.
Quoted 8 lines
Quoted 6 lines
Quoted 2 lines
Then tell clang not to do that, like we tell gcc not to do that as that is a foolish thing for a compiler to do when building the kernel.
Thanks all for your input, we'll try to get -fno-delete-null-pointer-checks or a similar flag to be added to clang.
Wait, clang does not have that?
Nope, clang doesn't currently have such a flag.
Quoted 2 lines
That's crazy, how has this not been hit yet when building the kernel?
IIRC this patch was needed to work around the lack of the flag:
commit beaec533fc2701a28a4d667f67c9f59c6e4e0d13
Author: Alexander Potapenko <[email protected]> Date: Wed Jul 19 20:27:30 2017 +0200
llist: clang: introduce member_address_is_nonnull()
Currently llist_for_each_entry() and llist_for_each_entry_safe() iterate until &pos->member != NULL. But when building the kernel with Clang, the compiler assumes &pos->member cannot be NULL if the member's offset is greater than 0 (which would be equivalent to the object being non-contiguous in memory). Therefore the loop condition is always true, and the loops become infinite.
To work around this, introduce the member_address_is_nonnull() macro, which casts object pointer to uintptr_t, thus letting the member pointer to be NULL.
Other than that I am not aware of any known issues.
Greg Kroah-Hartman
maintainerQuoted 58 lines · Matthias Kaehlcke
El Wed, Mar 28, 2018 at 08:19:36PM +0200 Greg Kroah-Hartman ha dit:
Quoted 23 lines · Matthias Kaehlcke
Quoted 22 lines
El Wed, Mar 28, 2018 at 08:05:56PM +0200 Greg Kroah-Hartman ha dit:
Quoted 20 lines
A: Because it messes up the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing in e-mail?A: No.
Q: Should I include quotations after my reply?http://daringfireball.net/2007/07/on_top
Quoted 2 lines · Manoj Gupta
Please note that there is nothing wrong in the generated code, just that it confuses objtool.
Then fix the tool, the C code is correct :)
Quoted 2 lines
Clang has simply omitted the statement where NULL is returned since the pointer was always dereferenced post inlining.
Quoted 8 lines
Quoted 6 lines
Quoted 2 lines
Then tell clang not to do that, like we tell gcc not to do that as that is a foolish thing for a compiler to do when building the kernel.
Thanks all for your input, we'll try to get -fno-delete-null-pointer-checks or a similar flag to be added to clang.
Wait, clang does not have that?
Nope, clang doesn't currently have such a flag.
Quoted 2 lines
That's crazy, how has this not been hit yet when building the kernel?
IIRC this patch was needed to work around the lack of the flag:
commit
beaec533fc2701a28a4d667f67c9f59c6e4e0d13Author: Alexander Potapenko <[email protected]> Date: Wed Jul 19 20:27:30 2017 +0200llist: clang: introduce
member_address_is_nonnull()Currently
llist_for_each_entry()andllist_for_each_entry_safe()iterate until&pos->member!= NULL. But when building the kernel with Clang, the compiler assumes&pos->membercannot be NULL if the member's offset is greater than 0 (which would be equivalent to the object being non-contiguous in memory). Therefore the loop condition is always true, and the loops become infinite.To work around this, introduce the
member_address_is_nonnull()macro, which casts object pointer touintptr_t, thus letting the member pointer to be NULL.
Quoted 6 lines
Signed-off-by Alexander Potapenko <[email protected]>Tested-by Sodagudi Prasad <[email protected]>Signed-off-by Linus Torvalds <[email protected]>Other than that I am not aware of any known issues.
I think you have gotten lucky :)
greg k-h