Re: [PATCH 00/45] C++: Convert the kernel to C++

David Howells
2024-01-10T00:29:30Z

Andrew Pinski <pinskia@gmail.com> wrote:

> Note even in GCC, we disable exceptions and RTTI while building GCC.
> This is specifically due to not wanting to use them and use other
> methods to do that.
> Note GDB on the other hand used to use setjmp/longjmp for their
> exception handling in C and I think they moved over to using C++
> exceptions which simplified things there. But as far as I know the
> Linux kernel does not use a mechanism like that (I know of copy
> from/to user using HW exceptions/error/interrupt handling but that is
> a special case only).

If we were to allow exception handling, I wonder if we would actually need to
throw anything other than a signed long integer (e.g. an error code) and just
disable RTTI. Maybe something like:

long sys_rename(...)
{
struct rwsem_lock lock_a, lock_b;
struct inode *dir_a, *dir_b;
...
try {
if (dir_a > dir_b) {
lock_a.down_write_killable(dir_a);
lock_b.down_write_killable(dir_b);
} else {
lock_b.down_write_killable(dir_b);
lock_a.down_write_killable(dir_a);
}
} catch (-EINTR) {
throw -ERESTARTSYS;
}
...
}

then have a cut-down exception unwinder that only needs to deal with long
values.

However, I think rolling out exception handling in the kernel might be too big
a task, given the huge amount of code involved - however much we might like to
avoid all those return value checks.

David