C exceptions in kernel
RFC PATCH 1/1 C exceptions in kernel
The attached patch implements C exceptions in the kernel, which *don't* depend on special support from the compiler. This is a 'request for comments'. The patch is very initial, should not be applied.
I actually got this code to work in the kernel:
try {
printk("TEST: before throwing \n");
throw(1000);
printk("TEST: won't run\n");
}
catch(unsigned long, value) {
printk("TEST: caught: %ld\n", value);
} yrt;I know it would a *hugh* task to get all existing code in the kernel to use exceptions, but the design allows exceptions to be used locally within the local call branches in *new* code. Basically, exception handling needs to be added only to functions who call functions which already use exceptions.
Although this patch is against 2.4, it should go to 2.5 (2.5.5-dj1 currently breaks here, so I am temporarily developing it using 2.4)
This patch implements only for i386 at the moment. Theoretically can be ported to other archs. Of course, the arch dependant functions in this code are separated for the ease of porting.
I haven't written it with interrupts and SMP in mind. I wonder what are the race conditions and what should be protected there.
For unhandled exceptions, there's a possibility to add a function that printk's the information about the unhandled exception (file, line number, etc), and optionally calls panic() or BUG().
The code supports re-throwing from catches.
Last thing: I must get rid of that yrt closer macro. Suggestions?
arch/i386/kernel/Makefile +2 -1
@@ -18,7 +18,8 @@ obj-y := process.o semaphore.o signal.o entry.o traps.o irq.o vm86.o \ ptrace.o i8259.o ioport.o ldt.o setup.o time.o sys_i386.o \ - pci-dma.o i386_ksyms.o i387.o bluesmoke.o dmi_scan.o + pci-dma.o i386_ksyms.o i387.o bluesmoke.o dmi_scan.o \ + exception.o ifdef CONFIG_PCI
arch/i386/kernel/exception.S +112 -0
112 lines not on this page · raw message
include/asm-i386/exception.h +13 -0
@@ -0,0 +1,13 @@ +#ifndef _I386_EXCEPTION_H +#define _I386_EXCEPTION_H + +struct exception_arch_struct { +unsigned long eip;/* 0x0 */ +unsigned long ebp;/* 0x4 */ +unsigned long esi;/* 0x8 */ +unsigned long edi;/* 0xC */ +unsigned long ebx;/* 0x10 */ +unsigned long esp;/* 0x14 */ +}; + +#endif
include/linux/exception.h +83 -0
83 lines not on this page · raw message
include/linux/sched.h +1 -0
@@ -385,6 +385,7 @@struct sem_queue *semsleeping;/* CPU-specific state of this task */ struct thread_struct thread; +struct exception_struct *exception;/* filesystem information */ struct fs_struct *fs;/* open file information */
Quoted 31 lines · Dan Aloni
The attached patch implements C exceptions in the kernel, which *don't* depend on special support from the compiler. This is a 'request for comments'. The patch is very initial, should not be applied.
I actually got this code to work in the kernel:
try { printk("TEST: before throwing \n"); throw(1000); printk("TEST: won't run\n"); } catch(unsigned long, value) { printk("TEST: caught: %ld\n", value); } yrt;I know it would a *hugh* task to get all existing code in the kernel to use exceptions, but the design allows exceptions to be used locally within the local call branches in *new* code. Basically, exception handling needs to be added only to functions who call functions which already use exceptions.
Although this patch is against 2.4, it should go to 2.5 (2.5.5-dj1 currently breaks here, so I am temporarily developing it using 2.4)
This patch implements only for i386 at the moment. Theoretically can be ported to other archs. Of course, the arch dependant functions in this code are separated for the ease of porting.
I haven't written it with interrupts and SMP in mind. I wonder what are the race conditions and what should be protected there.
Quoted 7 lines
For unhandled exceptions, there's a possibility to add a function that printk's the information about the unhandled exception (file, line number, etc), and optionally calls
panic()orBUG().The code supports re-throwing from catches.
Last thing: I must get rid of that yrt closer macro. Suggestions?
Is today the 1st of April ? You kidding, don't you ?
- Davide
Quoted 6 lines · Dan Aloni
The attached patch implements C exceptions in the kernel, which *don't* depend on special support from the compiler. This is a 'request for comments'. The patch is very initial, should not be applied.
I actually got this code to work in the kernel:
Quoted 9 lines
try {
printk("TEST: before throwing \n");
throw(1000);
printk("TEST: won't run\n");
}
catch(unsigned long, value) {
printk("TEST: caught: %ld\n", value);
} yrt;What is this supposed to do? Are these a bunch of solutions waiting for a problem? Or is my Calendar wrong?
Cheers,
Dick Johnson
Penguin : Linux version 2.4.1 on an i686 machine (797.90 BogoMips).
111,111,111 * 111,111,111 = 12,345,678,987,654,321
Dan,
Don't let'em get to ya, they are having a time warp problem.
Finally a method of preventing oops'es.
Cheers,
Dave
Quoted 15 lines · Dan Aloni
The attached patch implements C exceptions in the kernel, which *don't* depend on special support from the compiler. This is a 'request for comments'. The patch is very initial, should not be applied.
I actually got this code to work in the kernel:
try { printk("TEST: before throwing \n"); throw(1000); printk("TEST: won't run\n"); } catch(unsigned long, value) { printk("TEST: caught: %ld\n", value); } yrt;
Quoted 3 lines · David B. Stevens
Dan,
Don't let'em get to ya, they are having a time warp problem.
Yup, if exceptions inside kernel code is the current time, my time warp is so huge that Spilberg could use me like 1st actor in Jurassic Park IV Buf i've got to tell you, life in Jurassica is pretty fine ...
- Davide
Quoted 2 lines · Dan Aloni
The attached patch implements C exceptions in the kernel,
Bad idea ...
Quoted 1 line
which *don't* depend on special support from the compiler.
... which can be implemented in simple ANSI-C. See below.
Ciao, ET.
#include <setjmp.h>
struct _catch
{
struct _catch *next;
jmp_buf buf;
};static struct _catch _catch_top[1];
struct _catch *_catch = _catch_top;
#define throw() \
do { \
_catch = _catch->next; \
longjmp(_catch->buf, 1); \
} while (0)#define try \
if (setjmp(_catch->buf) == 0) { \
struct _catch _catch_new[1]; \
_catch_new->next = _catch; \
_catch = _catch_new;#define catch \
_catch = _catch->next; \
} else/**** example below ****/
#include <stdio.h>
int a = 1;
void
foo(int x)
{
a=2;
if (x & 1)
throw();
}intmain(int argc, char **argv)
{
int i;
for (i = 0; i < 10; ++i)
{
a=i;
try
{
try foo(i); catch
{
printf("2: caught at %d (a=%d)\n", i, a);
if ((i&3)==1)
throw();
}
}
catch
{
printf("1: caught at %d (a=%d)\n", i, a);
}
printf("a=%d\n", a);
}
return 0;
}Thus spake Dan Aloni ([email protected]):
Quoted 3 lines
The attached patch implements C exceptions in the kernel, which *don't* depend on special support from the compiler. This is a 'request for comments'. The patch is very initial, should not be applied.
First of all: setjmp/longjmp is quite inefficient.
But my real problem with this is that the point about exceptions in C++ is the automatic stack unwinding. You use local variables and if an exception is thrown, they automatically self-destruct. In particular, you could implement spin locks as a class, and an exception will release the lock automatically. Since this is not there in C, this is no more elegant than using explicit goto.
Also, it makes understanding the code (and correlating assembly output with C code) less easy, because you also have to look at that exception implementation.
Felix
Quoted 6 lines · Dan Aloni
The attached patch implements C exceptions in the kernel, which *don't* depend on special support from the compiler. This is a 'request for comments'. The patch is very initial, should not be applied.
I actually got this code to work in the kernel:
Quoted 8 lines
try {
printk("TEST: before throwing \n");
throw(1000);
printk("TEST: won't run\n");
}
catch(unsigned long, value) {
printk("TEST: caught: %ld\n", value);
} yrt;Can they fall through multiple function calls? How do they jive with preemtive scheduling? How much is the stack unwinding overhead?
Potentially this is very cool but I'm again appalled at the INSTANT rejection seen here by kernel hackers, minor and major. Do NOT reject an idea before you've thought it through. Do NOT reject an idea simply because it is new.
Also, do not jump on the bandwagon BECAUSE it is new. But still - people here should get a life if they get off on rejecting new stuff because it is new.
Regards,
bert
Signature · 4 lines
http://www.PowerDNS.com Versatile DNS Software & Services http://www.tk the dot in .tk Netherlabs BV / Rent-a-Nerd.nl - Nerd Available - Linux Advanced Routing & Traffic Control: http://ds9a.nl/lartc
Quoted 16 lines · bert hubert
Quoted 6 lines · Dan Aloni
The attached patch implements C exceptions in the kernel, which *don't* depend on special support from the compiler. This is a 'request for comments'. The patch is very initial, should not be applied.
I actually got this code to work in the kernel:
Quoted 8 lines
try { printk("TEST: before throwing \n"); throw(1000); printk("TEST: won't run\n"); } catch(unsigned long, value) { printk("TEST: caught: %ld\n", value); } yrt;
Quoted 2 lines
Can they fall through multiple function calls? How do they jive with preemtive scheduling? How much is the stack unwinding overhead?
They fall through several function calls like they should.
I don't see any problem with preemtive scheduling (every kernel thread has its own seperated exception frames).
The overhead on the stack is 36 bytes for each exception frame (a context of a try block). The unwinding procedure itself is short, the throw macro calls a rather small asm function for an unwind.
Quoted 8 lines
Potentially this is very cool but I'm again appalled at the INSTANT rejection seen here by kernel hackers, minor and major. Do NOT reject an idea before you've thought it through. Do NOT reject an idea simply because it is new.
Also, do not jump on the bandwagon BECAUSE it is new. But still - people here should get a life if they get off on rejecting new stuff because it is new.
Whether it is accepted or not, I can't see it being used in the core kernel code, just because there is too much code to rewrite for it to happen. Maybe if this thing was proposed back in 1992/3 it would have been different.
But, it CAN be used in *local* driver call branches. Writing a new driver? have a lot of local nested calls? Hate goto's? You can use exceptions.
The only problem is that because C is natively not object oriented, it's hard to come up with an exception scheme for the C language that is better than ye' old goto's, like in C++ when you have automatic destruction during unwinding.
Quoted 3 lines · bert hubert
Potentially this is very cool but I'm again appalled at the INSTANT rejection seen here by kernel hackers, minor and major. Do NOT reject an idea before you've thought it through.
Quite a few of us _had_ thought it through.
Quoted 2 lines
Do NOT reject an idea simply because
it is new.
There was a new idea in this proposal? Where?
You know, there's a reason why most of letal mutations are recessive. Namely, it's not the first time when they happen and mechanisms making them recessive had been selected for.
Same story here. It's _not_ the first time this topic had been discussed on l-k. It might be new for you, but it sure as hell _not_ new for quite a few people here.
Quoted 7 lines · Alexander Viro
Quoted 3 lines · bert hubert
Potentially this is very cool but I'm again appalled at the INSTANT rejection seen here by kernel hackers, minor and major. Do NOT reject an idea before you've thought it through.
Quite a few of us _had_ thought it through.
Good.
Quoted 3 lines
You know, there's a reason why most of letal mutations are recessive. Namely, it's not the first time when they happen and mechanisms making them recessive had been selected for.
An ostrich might reason that way too. And that is basically my main gripe.
Quoted 3 lines
Same story here. It's _not_ the first time this topic had been discussed on l-k. It might be new for you, but it sure as hell _not_ new for quite a few people here.
Oh I've seen it. What I resent is the kneejerk reaction. Most people replying obviously hadn't even looked at the source. Keith Owen's reaction was wonderful:
The kernel model is "get it right the first time, so we don't need exception handlers". You have not given any reason why the existing mechanisms are failing.
Or Edgar Toernig, who also had an implementation and rejected Dan's in one line:
Bad idea ...
Richard B. Johnson:
What is this supposed to do? Are these a bunch of solutions waiting for a problem? Or is my Calendar wrong?
Davide Libenzi:
Is today the 1st of April ? You kidding, don't you ?
I mean, this is just pathetic. There is valid criticism to new ideas, but this isn't it.
Regards,
bert
Signature · 4 lines
http://www.PowerDNS.com Versatile DNS Software & Services http://www.tk the dot in .tk Netherlabs BV / Rent-a-Nerd.nl - Nerd Available - Linux Advanced Routing & Traffic Control: http://ds9a.nl/lartc
Quoted 2 lines
I mean, this is just pathetic. There is valid criticism to new ideas, but this isn't it.
There are places to learn how to do basic programming and this isn't it. There is no way that the kernel list can handle every bad idea over and over again in a kind and gentle way. Doing so requires so much time that noone would ever get any real work done. At some point, you just say no. If we didn't, we'd still be arguing about STREAMS, redoing the kernel in C++, etc.
Signature · 2 lines
--- Larry McVoy lm at bitmover.com http://www.bitmover.com/lm
Quoted 7 lines · Larry McVoy
Quoted 2 lines
I mean, this is just pathetic. There is valid criticism to new ideas, but this isn't it.
There are places to learn how to do basic programming and this isn't it. There is no way that the kernel list can handle every bad idea over and over again in a kind and gentle way. Doing so requires so much time that noone would ever get any real work done. At some point, you just say
Lots of people found the time to send derogative answers though - and how well known are these in extinguising discussion and allowing us more time to code!
Typing in the few reasons why this, or any idea, is so well rejected hardly takes more time and *does* extinguish the thread rapidly.
Regards,
bert
Signature · 4 lines
http://www.PowerDNS.com Versatile DNS Software & Services http://www.tk the dot in .tk Netherlabs BV / Rent-a-Nerd.nl - Nerd Available - Linux Advanced Routing & Traffic Control: http://ds9a.nl/lartc
Hello,
Quoted 7 lines · bert hubert
Lots of people found the time to send derogative answers though - and how well known are these in extinguising discussion and allowing us more time to code!
Typing in the few reasons why this, or any idea, is so well rejected hardly takes more time and *does* extinguish the thread rapidly.
I think you might not being understanding. It has been discussed. In some cases over and over. A search of the mailing list archives give the reasons why these (and many like it) have been rejected. The terse 'no' is most likley fustration that the poster did not do his/her homework by searching archives before posting.
Kurt
Quoted 7 lines · bert hubert
On Sat, Feb 23, 2002 at 05:49:04PM +0000, Alexander Viro wrote:
Davide Libenzi:Is today the 1st of April ? You kidding, don't you ?
I mean, this is just pathetic. There is valid criticism to new ideas, but this isn't it.
When the idea is as broken as this, then the above is what you are to expect.
Jes
Quoted 3 lines · Dan Aloni
But, it CAN be used in *local* driver call branches. Writing a new driver? have a lot of local nested calls? Hate goto's? You can use exceptions.
Is this really anything other than syntactic sugar? Maybe it's different in drivers, but I find myself doing the following in user space all the time
#define unless(x) if (!(x)) /* perl/BCPL corrupted me */
function(...)
{
char *foo = 0, *bar = 0;
int locked = 0;
int rc = -1;
if (bad args or something) {
out: if (foo) free(foo);
if (bar) free(bar);
if (locked) unlock();
return (rc);
}
unless (locked = get_the_lock()) goto out;
unless (foo = allocate_foo()) goto out;
unless (bar = allocate_bar()) goto out;
more code....
rc = 0;
goto out;
}It seems ugly at first but it has some nice attributes:
a) all the cleanup is in one place, for both the error path and the non-error path. I could put it at the bottom, I like it at the top because that's where I tend to have the list of things needed to be cleaned.
b) all the error cases are branches, the normal path is straightline.
c) it's as dense as I can make it.
So how would you do the same thing with exceptions?
Signature · 2 lines
--- Larry McVoy lm at bitmover.com http://www.bitmover.com/lm
Quoted 34 lines · Larry McVoy
Quoted 3 lines · Dan Aloni
But, it CAN be used in *local* driver call branches. Writing a new driver? have a lot of local nested calls? Hate goto's? You can use exceptions.
Is this really anything other than syntactic sugar? Maybe it's different in drivers, but I find myself doing the following in user space all the time
#define unless(x) if (!(x)) /* perl/BCPL corrupted me */ function(...) { char *foo = 0, *bar = 0; int locked = 0; int rc = -1; if (bad args or something) { out: if (foo) free(foo); if (bar) free(bar); if (locked) unlock(); return (rc); } unless (locked = get_the_lock()) goto out; unless (foo = allocate_foo()) goto out; unless (bar = allocate_bar()) goto out; more code.... rc = 0; goto out; }
Quoted 3 lines
It seems ugly at first but it has some nice attributes:
a) all the cleanup is in one place, for both the error path and
the
Quoted 2 lines
non-error path. I could put it at the bottom, I like it at the top because that's where I tend to have the list of things
needed
Quoted 3 lines
to be cleaned.
b) all the error cases are branches, the normal path is
straightline.
Quoted 4 lines
c) it's as dense as I can make it.
So how would you do the same thing with exceptions?
Like this:
function(...)
{
char *foo = 0, *bar = 0;
int locked = 0;
int rc = -1;
try {
if (bad args or something)
throw;
locked = get_the_lock();
foo = allocate_foo();
bar = allocate_bar();
more code....
rc = 0;
}
cleanup {
if (foo) free(foo);
if (bar) free(bar);
if (locked) unlock();
}
return rc;
}Looks much better, IMHO.
The cleanup() block will run after the try block even if an exception did not occur, and will run also if the exception occured, passing the exception to the next catch() or cleanup() block in stack.
Quoted 7 lines · Larry McVoy
Quoted 3 lines · Dan Aloni
But, it CAN be used in *local* driver call branches. Writing a new driver? have a lot of local nested calls? Hate goto's? You can use exceptions.
Is this really anything other than syntactic sugar? Maybe it's different in drivers, but I find myself doing the following in user space all the time
Exceptions provide implicit handling. You should see tham as an alternate return path. And because of that implicitness, it is not syntactic sugar.
However, one thing which your example does emphasize, it the risk of resource leaks. Bjarne Stroustrup, who is no mean architect himself, spends a lot of pages discussing those leaks.
Basically, an exception may fall through several functions before being caught. Any resources acquired by those intermediate functions will not be released unless this is done automatically during stack unwinding, ie, destroctors or alloca(3).
So while having an alternate return path is cool, it really needs destructors in order to be useful in a real world.
This url documents the problem, which is solved by adopting the 'Resource Acquisition is Initialization' paradigm:
http://sourceforge.net/docman/display_doc.php?docid=8673&group_id=9028
Regards,
bert
Signature · 3 lines
http://www.tk the dot in .tk Netherlabs BV / Rent-a-Nerd.nl - Nerd Available - Linux Advanced Routing & Traffic Control: http://ds9a.nl/lartc
Dan Aloni <[email protected]> :
[...]
Quoted 2 lines
cleanup {
if (foo) free(foo);^^
Quoted 1 line
if (bar) free(bar);
^^
Quoted 1 line
if (locked) unlock();
^^
Quoted 9 lines
} return rc; }Looks much better, IMHO.
The
cleanup()block will run after the try block even if an exception did not occur, and will run also if the exception occured, passing the exception to the nextcatch()orcleanup()block in stack.
Three useless "if" if no exception occured.
Signature · 1 line
Ueimor