Documentation: coding-style: don't encourage WARN*()
Subsystems (2)
PATCH 1/1 Documentation: coding-style: don't encourage WARN*()
Several times recently Greg KH has admonished that variants of WARN() should not be used, because when the panic_on_warn kernel option is set, their use can lead to a panic. His reasoning was that the majority of Linux instances (including Android and cloud systems) run with this option enabled. And therefore a condition leading to a warning will frequently cause an undesirable panic.
The "coding-style.rst" document says not to worry about this kernel option. Update it to provide a more nuanced explanation.
--- Documentation/process/coding-style.rst | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-)
Documentation/process/coding-style.rst +11 -10
@@ -1235,17 +1235,18 @@ example. Again: WARN*() must not be used for a condition that is expected to trigger easily, for example, by user space actions. pr_warn_once() is a possible alternative, if you need to notify the user of a problem. -Do not worry about panic_on_warn users -************************************** +The panic_on_warn kernel option +******************************** -A few more words about panic_on_warn: Remember that ``panic_on_warn`` is an -available kernel option, and that many users set this option. This is why -there is a "Do not WARN lightly" writeup, above. However, the existence of -panic_on_warn users is not a valid reason to avoid the judicious use -WARN*(). That is because, whoever enables panic_on_warn has explicitly -asked the kernel to crash if a WARN*() fires, and such users must be -prepared to deal with the consequences of a system that is somewhat more -likely to crash. +Note that ``panic_on_warn`` is an available kernel option. If it is enabled, +a WARN*() call whose condition holds leads to a kernel panic. Many users +(including Android and many cloud providers) set this option, and this is +why there is a "Do not WARN lightly" writeup, above. + +The existence of this option is not a valid reason to avoid the judicious +use of warnings. There are other options: ``dev_warn*()`` and ``pr_warn*()`` +issue warnings but do **not** cause the kernel to crash. Use these if you +want to prevent such panics. Use BUILD_BUG_ON() for compile-time assertions **********************************************
Signature · 1 line
2.40.1
Hi Alex,
Thank you for the patch.
Quoted 35 lines · Alex Elder
Several times recently Greg KH has admonished that variants of
WARN()should not be used, because when thepanic_on_warnkernel option is set, their use can lead to a panic. His reasoning was that the majority of Linux instances (including Android and cloud systems) run with this option enabled. And therefore a condition leading to a warning will frequently cause an undesirable panic.The "
coding-style.rst" document says not to worry about this kernel option. Update it to provide a more nuanced explanation.Signed-off-by Alex Elder <[email protected]>--- Documentation/process/coding-style.rst | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-)Documentation/process/coding-style.rst +2
-9@@ -1235,17 +1235,18 @@ example. Again: WARN*() must not be used for a condition that is expected to trigger easily, for example, by user space actions. pr_warn_once() is a possible alternative, if you need to notify the user of a problem. -Do not worry about panic_on_warn users -************************************** +The panic_on_warn kernel option +******************************** -A few more words about panic_on_warn: Remember that ``panic_on_warn`` is an -available kernel option, and that many users set this option. This is why -there is a "Do not WARN lightly" writeup, above. However, the existence of -panic_on_warn users is not a valid reason to avoid the judicious use -WARN*(). That is because, whoever enables panic_on_warn has explicitly -asked the kernel to crash if a WARN*() fires, and such users must be -prepared to deal with the consequences of a system that is somewhat more
Documentation/process/coding-style.rst example. Again: WARN*() must not be used for a condition that is expected · line 1249
Quoted 10 lines
Documentation/process/coding-style.rst +9
-1-likely to crash. +Note that ``panic_on_warn`` is an available kernel option. If it is enabled, +a WARN*() call whose condition holds leads to a kernel panic. Many users +(including Android and many cloud providers) set this option, and this is +why there is a "Do not WARN lightly" writeup, above. + +The existence of this option is not a valid reason to avoid the judicious +use of warnings. There are other options: ``dev_warn*()`` and ``pr_warn*()`` +issue warnings but do **not** cause the kernel to crash. Use these if you +want to prevent such panics.
Those options are not equivalent, they print a single message, which is much easier to ignore. WARN() is similar to -Werror in some sense, it pushes vendors to fix the warnings. I have used WARN() in the past to indicate usage of long-deprecated APIs that we were getting close to removing for instance. dev_warn() wouldn't have had the same effect.
Quoted 3 lines
Documentation/process/coding-style.rst +0
-0Use BUILD_BUG_ON() for compile-time assertions **********************************************
Signature · 3 lines
Regards, Laurent Pinchart
Quoted 48 lines · Laurent Pinchart
Hi Alex,
Thank you for the patch.
Quoted 43 lines · Alex Elder
Several times recently Greg KH has admonished that variants of
WARN()should not be used, because when thepanic_on_warnkernel option is set, their use can lead to a panic. His reasoning was that the majority of Linux instances (including Android and cloud systems) run with this option enabled. And therefore a condition leading to a warning will frequently cause an undesirable panic.The "
coding-style.rst" document says not to worry about this kernel option. Update it to provide a more nuanced explanation.Signed-off-by Alex Elder <[email protected]>--- Documentation/process/coding-style.rst | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-)Documentation/process/coding-style.rst +9
-10@@ -1235,17 +1235,18 @@ example. Again: WARN*() must not be used for a condition that is expected to trigger easily, for example, by user space actions. pr_warn_once() is a possible alternative, if you need to notify the user of a problem. -Do not worry about panic_on_warn users -************************************** +The panic_on_warn kernel option +******************************** -A few more words about panic_on_warn: Remember that ``panic_on_warn`` is an -available kernel option, and that many users set this option. This is why -there is a "Do not WARN lightly" writeup, above. However, the existence of -panic_on_warn users is not a valid reason to avoid the judicious use -WARN*(). That is because, whoever enables panic_on_warn has explicitly -asked the kernel to crash if a WARN*() fires, and such users must be -prepared to deal with the consequences of a system that is somewhat more -likely to crash. +Note that ``panic_on_warn`` is an available kernel option. If it is enabled, +a WARN*() call whose condition holds leads to a kernel panic. Many users +(including Android and many cloud providers) set this option, and this is +why there is a "Do not WARN lightly" writeup, above. + +The existence of this option is not a valid reason to avoid the judicious +use of warnings. There are other options: ``dev_warn*()`` and ``pr_warn*()``
Quoted 8 lines
Documentation/process/coding-style.rst example. Again: WARN*() must not be used for a condition that is expected · line 1249
Quoted 2 lines
Documentation/process/coding-style.rst +2
-0+issue warnings but do **not** cause the kernel to crash. Use these if you +want to prevent such panics.Those options are not equivalent, they print a single message, which is much easier to ignore.
WARN()is similar to -Werror in some sense, it pushes vendors to fix the warnings. I have usedWARN()in the past to indicate usage of long-deprecated APIs that we were getting close to removing for instance.dev_warn()wouldn't have had the same effect.
Honestly, I feel somewhat the same way--that WARN() has a use that differs from dev_warn(). E.g., in places where something "won't happen" (but conceivably could if someone was developing a future change and violated an assumption).
Nevertheless, if panic_on_warn is used in Android and cloud scenarios as Greg says, he's right that it affects many, many systems. Perhaps it's better to more strongly discourage the use of that option?
I saw this "don't worry about it" message and felt it at least ought to be toned down. The broader question of whether WARN() should generally not be used might need some more discussion.
-Alex
Quoted 3 lines
Quoted 2 lines
Documentation/process/coding-style.rst +0
-0Use BUILD_BUG_ON() for compile-time assertions **********************************************
Quoted 48 lines · Laurent Pinchart
Hi Alex,
Thank you for the patch.
Quoted 43 lines · Alex Elder
Several times recently Greg KH has admonished that variants of
WARN()should not be used, because when thepanic_on_warnkernel option is set, their use can lead to a panic. His reasoning was that the majority of Linux instances (including Android and cloud systems) run with this option enabled. And therefore a condition leading to a warning will frequently cause an undesirable panic.The "
coding-style.rst" document says not to worry about this kernel option. Update it to provide a more nuanced explanation.Signed-off-by Alex Elder <[email protected]>--- Documentation/process/coding-style.rst | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-)Documentation/process/coding-style.rst +9
-10@@ -1235,17 +1235,18 @@ example. Again: WARN*() must not be used for a condition that is expected to trigger easily, for example, by user space actions. pr_warn_once() is a possible alternative, if you need to notify the user of a problem. -Do not worry about panic_on_warn users -************************************** +The panic_on_warn kernel option +******************************** -A few more words about panic_on_warn: Remember that ``panic_on_warn`` is an -available kernel option, and that many users set this option. This is why -there is a "Do not WARN lightly" writeup, above. However, the existence of -panic_on_warn users is not a valid reason to avoid the judicious use -WARN*(). That is because, whoever enables panic_on_warn has explicitly -asked the kernel to crash if a WARN*() fires, and such users must be -prepared to deal with the consequences of a system that is somewhat more -likely to crash. +Note that ``panic_on_warn`` is an available kernel option. If it is enabled, +a WARN*() call whose condition holds leads to a kernel panic. Many users +(including Android and many cloud providers) set this option, and this is +why there is a "Do not WARN lightly" writeup, above. + +The existence of this option is not a valid reason to avoid the judicious +use of warnings. There are other options: ``dev_warn*()`` and ``pr_warn*()``
Quoted 8 lines
Documentation/process/coding-style.rst example. Again: WARN*() must not be used for a condition that is expected · line 1249
Quoted 2 lines
Documentation/process/coding-style.rst +2
-0+issue warnings but do **not** cause the kernel to crash. Use these if you +want to prevent such panics.Those options are not equivalent, they print a single message, which is much easier to ignore.
WARN()is similar to -Werror in some sense, it pushes vendors to fix the warnings. I have usedWARN()in the past to indicate usage of long-deprecated APIs that we were getting close to removing for instance.dev_warn()wouldn't have had the same effect.
If you want to reboot a box because someone called an "improper" api, then sure, use WARN(), but that feels like a really bad idea. Just remove the api and fix up all in-kernel users instead. Why wait?
If you want to show a traceback, then just print that out, but I've seen that totally ignored as well, removing the api is usually the only way to get people to actually notice, as then their builds break.
thanks,
greg k-h
Hi Greg,
Quoted 54 lines · Greg KH
Quoted 44 lines · Laurent Pinchart
Quoted 43 lines · Alex Elder
Several times recently Greg KH has admonished that variants of
WARN()should not be used, because when thepanic_on_warnkernel option is set, their use can lead to a panic. His reasoning was that the majority of Linux instances (including Android and cloud systems) run with this option enabled. And therefore a condition leading to a warning will frequently cause an undesirable panic.The "
coding-style.rst" document says not to worry about this kernel option. Update it to provide a more nuanced explanation.Signed-off-by Alex Elder <[email protected]>--- Documentation/process/coding-style.rst | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-)Documentation/process/coding-style.rst +9
-10@@ -1235,17 +1235,18 @@ example. Again: WARN*() must not be used for a condition that is expected to trigger easily, for example, by user space actions. pr_warn_once() is a possible alternative, if you need to notify the user of a problem. -Do not worry about panic_on_warn users -************************************** +The panic_on_warn kernel option +******************************** -A few more words about panic_on_warn: Remember that ``panic_on_warn`` is an -available kernel option, and that many users set this option. This is why -there is a "Do not WARN lightly" writeup, above. However, the existence of -panic_on_warn users is not a valid reason to avoid the judicious use -WARN*(). That is because, whoever enables panic_on_warn has explicitly -asked the kernel to crash if a WARN*() fires, and such users must be -prepared to deal with the consequences of a system that is somewhat more -likely to crash. +Note that ``panic_on_warn`` is an available kernel option. If it is enabled, +a WARN*() call whose condition holds leads to a kernel panic. Many users +(including Android and many cloud providers) set this option, and this is +why there is a "Do not WARN lightly" writeup, above. + +The existence of this option is not a valid reason to avoid the judicious +use of warnings. There are other options: ``dev_warn*()`` and ``pr_warn*()``Quoted 8 lines
Documentation/process/coding-style.rst example. Again: WARN*() must not be used for a condition that is expected · line 1249
Quoted 2 lines
Documentation/process/coding-style.rst +2
-0+issue warnings but do **not** cause the kernel to crash. Use these if you +want to prevent such panics.Those options are not equivalent, they print a single message, which is much easier to ignore.
WARN()is similar to -Werror in some sense, it pushes vendors to fix the warnings. I have usedWARN()in the past to indicate usage of long-deprecated APIs that we were getting close to removing for instance.dev_warn()wouldn't have had the same effect.
Quoted 1 line
If you want to reboot a box because someone called an "improper" api,
I don't "want" to reboot. It came as a side effect when panic_on_warn was added, and worsened when its adoption increased. I won't argued for or against panic_on_warn, but WARN() serves some use cases today that I consider valid. If we want to discourage its usage, we need another API to cover those use cases.
Quoted 2 lines
then sure, use
WARN(), but that feels like a really bad idea. Just remove the api and fix up all in-kernel users instead. Why wait?
There are multiple use cases. One of them is to make sure no new user of the old, deprecated behaviour is introduced. This is especially important when driver development spans multiple kernel releases, the development can start before the API behaviour changes, with the driver merged after the API change. This is something we've done multiple times in V4L2.
Quoted 3 lines
If you want to show a traceback, then just print that out, but I've seen that totally ignored as well, removing the api is usually the only way to get people to actually notice, as then their builds break.
Does your experience tell that tracebacks are routinely ignored during development too, not just in production ?
Signature · 3 lines
Regards, Laurent Pinchart
Quoted 75 lines · Laurent Pinchart
Hi Greg,
Quoted 54 lines · Greg KH
Quoted 44 lines · Laurent Pinchart
Quoted 43 lines · Alex Elder
Several times recently Greg KH has admonished that variants of
WARN()should not be used, because when thepanic_on_warnkernel option is set, their use can lead to a panic. His reasoning was that the majority of Linux instances (including Android and cloud systems) run with this option enabled. And therefore a condition leading to a warning will frequently cause an undesirable panic.The "
coding-style.rst" document says not to worry about this kernel option. Update it to provide a more nuanced explanation.Signed-off-by Alex Elder <[email protected]>--- Documentation/process/coding-style.rst | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-)Documentation/process/coding-style.rst +9
-10@@ -1235,17 +1235,18 @@ example. Again: WARN*() must not be used for a condition that is expected to trigger easily, for example, by user space actions. pr_warn_once() is a possible alternative, if you need to notify the user of a problem. -Do not worry about panic_on_warn users -************************************** +The panic_on_warn kernel option +******************************** -A few more words about panic_on_warn: Remember that ``panic_on_warn`` is an -available kernel option, and that many users set this option. This is why -there is a "Do not WARN lightly" writeup, above. However, the existence of -panic_on_warn users is not a valid reason to avoid the judicious use -WARN*(). That is because, whoever enables panic_on_warn has explicitly -asked the kernel to crash if a WARN*() fires, and such users must be -prepared to deal with the consequences of a system that is somewhat more -likely to crash. +Note that ``panic_on_warn`` is an available kernel option. If it is enabled, +a WARN*() call whose condition holds leads to a kernel panic. Many users +(including Android and many cloud providers) set this option, and this is +why there is a "Do not WARN lightly" writeup, above. + +The existence of this option is not a valid reason to avoid the judicious +use of warnings. There are other options: ``dev_warn*()`` and ``pr_warn*()``Quoted 8 lines
Documentation/process/coding-style.rst example. Again: WARN*() must not be used for a condition that is expected · line 1249
Quoted 2 lines
Documentation/process/coding-style.rst +2
-0+issue warnings but do **not** cause the kernel to crash. Use these if you +want to prevent such panics.Those options are not equivalent, they print a single message, which is much easier to ignore.
WARN()is similar to -Werror in some sense, it pushes vendors to fix the warnings. I have usedWARN()in the past to indicate usage of long-deprecated APIs that we were getting close to removing for instance.dev_warn()wouldn't have had the same effect.Quoted 1 line
If you want to reboot a box because someone called an "improper" api,
I don't "want" to reboot. It came as a side effect when
panic_on_warnwas added, and worsened when its adoption increased. I won't argued for or againstpanic_on_warn, butWARN()serves some use cases today that I consider valid. If we want to discourage its usage, we need another API to cover those use cases.Quoted 2 lines
then sure, use
WARN(), but that feels like a really bad idea. Just remove the api and fix up all in-kernel users instead. Why wait?There are multiple use cases. One of them is to make sure no new user of the old, deprecated behaviour is introduced. This is especially important when driver development spans multiple kernel releases, the development can start before the API behaviour changes, with the driver merged after the API change. This is something we've done multiple times in V4L2.
Quoted 6 lines
Quoted 3 lines
If you want to show a traceback, then just print that out, but I've seen that totally ignored as well, removing the api is usually the only way to get people to actually notice, as then their builds break.
Does your experience tell that tracebacks are routinely ignored during development too, not just in production ?
Yes, we have done this in the past in some driver core apis and nothing ever changed until we actually deleted the apis.
thanks,
greg k-h
Quoted 78 lines · Greg KH
Quoted 77 lines · Laurent Pinchart
Quoted 54 lines · Greg KH
Quoted 44 lines · Laurent Pinchart
Quoted 43 lines · Alex Elder
Several times recently Greg KH has admonished that variants of
WARN()should not be used, because when thepanic_on_warnkernel option is set, their use can lead to a panic. His reasoning was that the majority of Linux instances (including Android and cloud systems) run with this option enabled. And therefore a condition leading to a warning will frequently cause an undesirable panic.The "
coding-style.rst" document says not to worry about this kernel option. Update it to provide a more nuanced explanation.Signed-off-by Alex Elder <[email protected]>--- Documentation/process/coding-style.rst | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-)Documentation/process/coding-style.rst +9
-10@@ -1235,17 +1235,18 @@ example. Again: WARN*() must not be used for a condition that is expected to trigger easily, for example, by user space actions. pr_warn_once() is a possible alternative, if you need to notify the user of a problem. -Do not worry about panic_on_warn users -************************************** +The panic_on_warn kernel option +******************************** -A few more words about panic_on_warn: Remember that ``panic_on_warn`` is an -available kernel option, and that many users set this option. This is why -there is a "Do not WARN lightly" writeup, above. However, the existence of -panic_on_warn users is not a valid reason to avoid the judicious use -WARN*(). That is because, whoever enables panic_on_warn has explicitly -asked the kernel to crash if a WARN*() fires, and such users must be -prepared to deal with the consequences of a system that is somewhat more -likely to crash. +Note that ``panic_on_warn`` is an available kernel option. If it is enabled, +a WARN*() call whose condition holds leads to a kernel panic. Many users +(including Android and many cloud providers) set this option, and this is +why there is a "Do not WARN lightly" writeup, above. + +The existence of this option is not a valid reason to avoid the judicious +use of warnings. There are other options: ``dev_warn*()`` and ``pr_warn*()``Quoted 8 lines
Documentation/process/coding-style.rst example. Again: WARN*() must not be used for a condition that is expected · line 1249
Quoted 2 lines
Documentation/process/coding-style.rst +2
-0+issue warnings but do **not** cause the kernel to crash. Use these if you +want to prevent such panics.Those options are not equivalent, they print a single message, which is much easier to ignore.
WARN()is similar to -Werror in some sense, it pushes vendors to fix the warnings. I have usedWARN()in the past to indicate usage of long-deprecated APIs that we were getting close to removing for instance.dev_warn()wouldn't have had the same effect.Quoted 1 line
If you want to reboot a box because someone called an "improper" api,
I don't "want" to reboot. It came as a side effect when
panic_on_warnwas added, and worsened when its adoption increased. I won't argued for or againstpanic_on_warn, butWARN()serves some use cases today that I consider valid. If we want to discourage its usage, we need another API to cover those use cases.Quoted 2 lines
then sure, use
WARN(), but that feels like a really bad idea. Just remove the api and fix up all in-kernel users instead. Why wait?There are multiple use cases. One of them is to make sure no new user of the old, deprecated behaviour is introduced. This is especially important when driver development spans multiple kernel releases, the development can start before the API behaviour changes, with the driver merged after the API change. This is something we've done multiple times in V4L2.
Quoted 3 lines
If you want to show a traceback, then just print that out, but I've seen that totally ignored as well, removing the api is usually the only way to get people to actually notice, as then their builds break.
Quoted 5 lines
Quoted 2 lines
Does your experience tell that tracebacks are routinely ignored during development too, not just in production ?
Yes, we have done this in the past in some driver core apis and nothing ever changed until we actually deleted the apis.
Let's keep WARN() + panic_on_warn then, it should help making people notice :-)
Jokes aside, if we want to discourage new users of WARN() because of panic_on_warn, I'd like a WARN_NO_PANIC().
Signature · 3 lines
Regards, Laurent Pinchart
Quoted 11 lines · Alex Elder
Several times recently Greg KH has admonished that variants of
WARN()should not be used, because when thepanic_on_warnkernel option is set, their use can lead to a panic. His reasoning was that the majority of Linux instances (including Android and cloud systems) run with this option enabled. And therefore a condition leading to a warning will frequently cause an undesirable panic.The "
coding-style.rst" document says not to worry about this kernel option. Update it to provide a more nuanced explanation.Signed-off-by Alex Elder <[email protected]>
Thanks for writing this up:
No, this advice is wronger than wrong. If you set panic_on_warn you get to keep the pieces.
Quoted 3 lines · Christoph Hellwig
No, this advice is wronger than wrong. If you set
panic_on_warnyou get to keep the pieces.
But don't add new WARN() calls please, just properly clean up and handle the error. And any WARN() that userspace can trigger ends up triggering syzbot reports which also is a major pain, even if you don't have panic_on_warn enabled.
And I think the "do not use panic_on_warn" recommendation has been ignored, given the huge use of it by vendors who have enabled it (i.e. all Samsung phones and cloud servers).
thanks,
greg k-h
Quoted 9 lines · Greg KH
Quoted 3 lines · Christoph Hellwig
No, this advice is wronger than wrong. If you set
panic_on_warnyou get to keep the pieces.But don't add new
WARN()calls please, just properly clean up and handle the error. And anyWARN()that userspace can trigger ends up triggering syzbot reports which also is a major pain, even if you don't havepanic_on_warnenabled.
Important distinction here: WARN_ON_ONCE is for internal error checking and absolutely intentional, and does not replace error handling, that's why it passes the error value through. OF course it should not be trigger by user action.
Quoted 3 lines
And I think the "do not use
panic_on_warn" recommendation has been ignored, given the huge use of it by vendors who have enabled it (i.e. all Samsung phones and cloud servers).
Sucks for them.
Quoted 9 lines · Greg KH
Quoted 3 lines · Christoph Hellwig
No, this advice is wronger than wrong. If you set
panic_on_warnyou get to keep the pieces.But don't add new
WARN()calls please, just properly clean up and handle the error. And anyWARN()that userspace can trigger ends up triggering syzbot reports which also is a major pain, even if you don't havepanic_on_warnenabled.
Here's what was more recently written on WARN:
https://docs.kernel.org/process/deprecated.html#bug-and-bug-on
Specifically:
- never use BUG*()
- WARN*() should only be used for "expected to be unreachable" situations
This, then, maps correctly to panic_on_warn: System owners may have set the panic_on_warn sysctl, to make sure their systems do not continue running in the face of "unreachable" conditions.
As in, userspace should _never_ be able to reach a WARN(). If it can, either the logic leading to it needs to be fixed, or the WARN() needs to be changed to a pr_warn().
Signature · 1 line
Kees Cook
Quoted 20 lines · Kees Cook
Quoted 9 lines · Greg KH
Quoted 3 lines · Christoph Hellwig
No, this advice is wronger than wrong. If you set
panic_on_warnyou get to keep the pieces.But don't add new
WARN()calls please, just properly clean up and handle the error. And anyWARN()that userspace can trigger ends up triggering syzbot reports which also is a major pain, even if you don't havepanic_on_warnenabled.Here's what was more recently written on WARN:
https://docs.kernel.org/process/deprecated.html#bug-and-bug-on
Specifically:
- never use BUG*()
- WARN*() should only be used for "expected to be unreachable" situations
Quoted 7 lines
This, then, maps correctly to
panic_on_warn: System owners may have set thepanic_on_warnsysctl, to make sure their systems do not continue running in the face of "unreachable" conditions.As in, userspace should _never_ be able to reach a
WARN(). If it can, either the logic leading to it needs to be fixed, or theWARN()needs to be changed to apr_warn().
Exactly! No doubt there are mistakes, but we already document this too a few lines above where this is touching:
Do not WARN lightly
*******************
WARN*() is intended for unexpected, this-should-never-happen situations. WARN*() macros are not to be used for anything that is expected to happen during normal operation. These are not pre- or post-condition asserts, for example. Again: WARN*() must not be used for a condition that is expected to trigger easily, for example, by user space actions. pr_warn_once() is a possible alternative, if you need to notify the user of a problem.
Usages following that advice should be left alone and more should be added. Invariant checks that indicate the kernel is malfunctioning are desirable things to have!
Yes, by all means tell people to follow the above rules! But that isn't a ban on WARN and shouldn't be communicated as "don't add new WARN() calls please".
Let's all keep in mind that fuzzing reports are incredibly valuable to make the kernel more secure and robust. We actually want *more* invariants that indicate bugs for the fuzzers to trip up on!
As above, a correctly used WARN, should indicate a certain bug if it triggers.
I'd guess about 30-40% of the syzkaller found bugs I've delt with are from a correct use of WARN_ON not oops/kasn/etc. I wonder what a datamine on the whole syzkaller database would indicate.
pr_warn/etc don't trigger fuzzer faults, and don't give a debugging backtrace.
I also find it strange to want panic_on_warn to exist, and people want to use it, while also saying that the WARN() calls that actually make it do something and be valuable are forbidden :(
Jason
Quoted 21 lines · Alex Elder
Documentation/process/coding-style.rst +2
-10@@ -1235,17 +1235,18 @@ example. Again: WARN*() must not be used for a condition that is expected to trigger easily, for example, by user space actions. pr_warn_once() is a possible alternative, if you need to notify the user of a problem. -Do not worry about panic_on_warn users -************************************** +The panic_on_warn kernel option +******************************** -A few more words about panic_on_warn: Remember that ``panic_on_warn`` is an -available kernel option, and that many users set this option. This is why -there is a "Do not WARN lightly" writeup, above. However, the existence of -panic_on_warn users is not a valid reason to avoid the judicious use -WARN*(). That is because, whoever enables panic_on_warn has explicitly -asked the kernel to crash if a WARN*() fires, and such users must be -prepared to deal with the consequences of a system that is somewhat more -likely to crash.
Documentation/process/coding-style.rst example. Again: WARN*() must not be used for a condition that is expected · line 1250
Quoted 10 lines
Documentation/process/coding-style.rst +9
-0+Note that ``panic_on_warn`` is an available kernel option. If it is enabled, +a WARN*() call whose condition holds leads to a kernel panic. Many users +(including Android and many cloud providers) set this option, and this is +why there is a "Do not WARN lightly" writeup, above. + +The existence of this option is not a valid reason to avoid the judicious +use of warnings. There are other options: ``dev_warn*()`` and ``pr_warn*()`` +issue warnings but do **not** cause the kernel to crash. Use these if you +want to prevent such panics.
WARN*() are for recoverable assertions, i.e. situations where the condition being true can only happen due to a kernel bug but where they can be recovered from (unlike BUG*() which are for unrecoverable situations). The people who use panic_on_warn *want* the kernel to crash when such a situation happens so that the underlying issue can be discovered and fixed. That's the whole point.
Also, it's not true that "Android" sets this option. It might be the case that some individual Android OEMs have decided to use it for some reason (they do have the ability to customize their kernel command line, after all). It's certainly not used by default or even recommended.
- Eric
Quoted 4 lines · Eric Biggers
Also, it's not true that "Android" sets this option. It might be the case that some individual Android OEMs have decided to use it for some reason (they do have the ability to customize their kernel command line, after all). It's certainly not used by default or even recommended.
Ah yes, you are right. I had misremembered; last I looked Android uses of CONFIG_UBSAN_TRAP=y, which has a similar affect (effectively promoting warning into bug) for UBSAN checks.
Signature · 1 line
Kees Cook
Quoted 10 lines · Eric Biggers
Documentation/process/coding-style.rst example. Again: WARN*() must not be used for a condition that is expected · line 1238
Quoted 9 lines · Alex Elder
Documentation/process/coding-style.rst +0
-1@@ -1235,17 +1235,18 @@ example. Again: WARN*() must not be used for a condition that is expected to trigger easily, for example, by user space actions. pr_warn_once() is a possible alternative, if you need to notify the user of a problem. -Do not worry about panic_on_warn users
This is still[1] good advice. panic_on_warn users have made a trade-off that works for them, but that should not mean that all of the valid cases for WARN*() suddenly disappear. In fact, without the WARN*() calls, panic_on_warn is a no-op, as someone else has already pointed out.
Quoted 16 lines
Quoted 16 lines
Documentation/process/coding-style.rst +6
-9-************************************** +The panic_on_warn kernel option +******************************** -A few more words about panic_on_warn: Remember that ``panic_on_warn`` is an -available kernel option, and that many users set this option. This is why -there is a "Do not WARN lightly" writeup, above. However, the existence of -panic_on_warn users is not a valid reason to avoid the judicious use -WARN*(). That is because, whoever enables panic_on_warn has explicitly -asked the kernel to crash if a WARN*() fires, and such users must be -prepared to deal with the consequences of a system that is somewhat more -likely to crash. +Note that ``panic_on_warn`` is an available kernel option. If it is enabled, +a WARN*() call whose condition holds leads to a kernel panic. Many users +(including Android and many cloud providers) set this option, and this is +why there is a "Do not WARN lightly" writeup, above.
Quoted 8 lines
Documentation/process/coding-style.rst example. Again: WARN*() must not be used for a condition that is expected · line 1250
Quoted 6 lines
Documentation/process/coding-style.rst +5
-0+ +The existence of this option is not a valid reason to avoid the judicious +use of warnings. There are other options: ``dev_warn*()`` and ``pr_warn*()`` +issue warnings but do **not** cause the kernel to crash. Use these if you +want to prevent such panics.Nacked-by Eric Biggers <[email protected]>
Yes. I agree with this NAK.
Quoted 7 lines
WARN*() are for recoverable assertions, i.e. situations where the condition being true can only happen due to a kernel bug but where they can be recovered from (unlike BUG*() which are for unrecoverable situations). The people who use
panic_on_warn*want* the kernel to crash when such a situation happens so that the underlying issue can be discovered and fixed. That's the whole point.
Quoted 7 lines
Also, it's not true that "Android" sets this option. It might be the case that some individual Android OEMs have decided to use it for some reason (they do have the ability to customize their kernel command line, after all). It's certainly not used by default or even recommended.
- Eric
[1] https://lore.kernel.org/lkml/[email protected]/T/#md6836102150ac1e6265569aad55a692e3af75f34
thanks,
Signature · 2 lines
John Hubbard NVIDIA
Quoted 38 lines · Alex Elder
Several times recently Greg KH has admonished that variants of
WARN()should not be used, because when thepanic_on_warnkernel option is set, their use can lead to a panic. His reasoning was that the majority of Linux instances (including Android and cloud systems) run with this option enabled. And therefore a condition leading to a warning will frequently cause an undesirable panic.The "
coding-style.rst" document says not to worry about this kernel option. Update it to provide a more nuanced explanation.Signed-off-by Alex Elder <[email protected]>--- Documentation/process/coding-style.rst | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-)Documentation/process/coding-style.rst +4
-10@@ -1235,17 +1235,18 @@ example. Again: WARN*() must not be used for a condition that is expected to trigger easily, for example, by user space actions. pr_warn_once() is a possible alternative, if you need to notify the user of a problem. -Do not worry about panic_on_warn users -************************************** +The panic_on_warn kernel option +******************************** -A few more words about panic_on_warn: Remember that ``panic_on_warn`` is an -available kernel option, and that many users set this option. This is why -there is a "Do not WARN lightly" writeup, above. However, the existence of -panic_on_warn users is not a valid reason to avoid the judicious use -WARN*(). That is because, whoever enables panic_on_warn has explicitly -asked the kernel to crash if a WARN*() fires, and such users must be -prepared to deal with the consequences of a system that is somewhat more -likely to crash. +Note that ``panic_on_warn`` is an available kernel option. If it is enabled, +a WARN*() call whose condition holds leads to a kernel panic. Many users
Documentation/process/coding-style.rst example. Again: WARN*() must not be used for a condition that is expected · line 1252
Quoted 10 lines
Documentation/process/coding-style.rst +7
-0+(including Android and many cloud providers) set this option, and this is +why there is a "Do not WARN lightly" writeup, above. + +The existence of this option is not a valid reason to avoid the judicious +use of warnings. There are other options: ``dev_warn*()`` and ``pr_warn*()`` +issue warnings but do **not** cause the kernel to crash. Use these if you +want to prevent such panics. Use BUILD_BUG_ON() for compile-time assertions **********************************************
Did you even read the history about that? Likely not, otherwise I wouldn't have to learn about this patch on lwn.net.
I suggest reading:
commit 1cfd9d7e43d5a1cf739d1420b10b1e65feb02f88
Author: David Hildenbrand <[email protected]> Date: Fri Sep 23 13:34:24 2022 +0200
coding-style.rst: document BUG() and WARN() rules ("do not crash the kernel")
which includes links to relevant discussions between me and Linus. Most relevant to the discussion is [1].
All that's written in the document right now (use WARN_ON_ONCE() *lightly*) is precisely what I still think we should do. That's the case *1.5 years* after I documented that.
Clear NACK from my side: "If you set 'panic_on_warn' you get to keep both pieces when something breaks." [1]
[1] https://lore.kernel.org/all/CAHk-=wgF7K2gSSpy=m_=K3Nov4zaceUX9puQf1TjkTJLA2XC_g@mail.gmail.com/
Signature · 3 lines
Cheers, David / dhildenb