kernelnewbies.kernelnewbies.org archive mirror
 help / color / mirror / Atom feed
From: "Valdis Klētnieks" <valdis.kletnieks@vt.edu>
To: Ian Pilcher <arequipeno@gmail.com>
Cc: kernelnewbies@kernelnewbies.org
Subject: Re: Return value for "impossible" situations
Date: Sun, 25 Jul 2021 19:15:39 -0400	[thread overview]
Message-ID: <88167.1627254939@turing-police> (raw)
In-Reply-To: <sdk98p$tmm$1@ciao.gmane.io>


[-- Attachment #1.1: Type: text/plain, Size: 2265 bytes --]

On Sun, 25 Jul 2021 13:07:21 -0500, Ian Pilcher said:

> Is there any sort of convention around what to return in the case of an
> error in the logic of the code itself, something that will make it as
> obvious as possible that the problem is a bug.

In general, there's no good way to signal such issues back to userspace,
because there's no reserved '-EHIT_A_BUG' value.  This has been true
for decades, ever since Unix was still on the 18-bit PDP-7 in 1969.

And it's basically useless to return such a value to userspace, because there's
nothing useful that userspace can *do* in such a case. Note that pretty much
all the defined error codes refer back to things that userspace could at least
potentially do something useful - it may retry the operation after a delay, or
tell the user that an optional facility isn't available in the currently running kernel,
or try an alternate method of doing an operation (for example, trying again
with IPv4 if an IPv6 connection fails, or use a different method of file locking).

But if a userspace process hits an actual kernel bug, what is it supposed to do
to recover?  Do you add "check for -EHIT_A_BUG' to every single place you do a
syscall?  After all, 98% of userspace code is, *at best*, going to simply do an
'if (!erro)' test.  And userspace code only does a more detailed check of *which*
errno it got handed if it can do something different/useful for a specific code (such
as code that goes into a retry loop if it gets -EEXIST when trying to create a
lock file that shouldn't exist, and should be removed by another process when
it's done).

In particular, userspace has no ability to log any useful debugging
information. There's the additional issue that the actual problem may not even
be in that syscall's code - it could be some previous syscall from the current
process that mis-set something in a structure, or some other kernel thread
doing a write-after-free and corrupting memory, or code that assumed that it
wouldn't be rescheduled to another CPU, or a myriad of other ways to fail.

The *proper* thing to do is, instead of deciding to return -EHIT_A_BUG, do a
WARN(), or BUG(), so that the dmesg has something that's at least potentially
useful.  Then use that information to fix the issue.

[-- Attachment #1.2: Type: application/pgp-signature, Size: 494 bytes --]

[-- Attachment #2: Type: text/plain, Size: 170 bytes --]

_______________________________________________
Kernelnewbies mailing list
Kernelnewbies@kernelnewbies.org
https://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies

  parent reply	other threads:[~2021-07-25 23:16 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-07-25 18:07 Return value for "impossible" situations Ian Pilcher
2021-07-25 18:59 ` Bernd Petrovitsch
2021-07-25 19:13   ` Ian Pilcher
2021-07-25 23:15 ` Valdis Klētnieks [this message]
2021-07-26  4:18   ` Ian Pilcher
2021-07-26 10:42     ` Bernd Petrovitsch
2021-07-26 11:08     ` Bernd Petrovitsch

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=88167.1627254939@turing-police \
    --to=valdis.kletnieks@vt.edu \
    --cc=arequipeno@gmail.com \
    --cc=kernelnewbies@kernelnewbies.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).