Skip to contentSkip to conversation

C exceptions in kernel

RFC PATCH Pete Zaitcev 6 participants 10 messages Feb 23, 2002 – Feb 24, 2002 1 message not in the archive

lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): [email protected]

#1 lore raw
Quoted 3 lines
Quoted 1 line

The attached patch implements C exceptions in the kernel,

Bad idea ...

Why is it bad? I can see a couple of reasons, but this does not help me undestand your, unspecified, reasons.

Quoted 3 lines
Quoted 1 line

which *don't* depend on special support from the compiler.

... which can be implemented in simple ANSI-C. See below.

Right, but the question is, do we support setjump/longjump in kernel? The patch as I saw it does reimplement a similar thing (check out its assembler fragments).

-- Pete

#2 lore raw
Quoted 1 line · Pete Zaitcev
Quoted 1 line
Quoted 1 line

The attached patch implements C exceptions in the kernel,

Kernel code already has exception tables to handle invalid addresses, invalid opcodes etc. See copy_to_user and wrmsr_eio for examples. Apart from that, the kernel code assumes that it knows what it is doing and does not need exceptions, any unexpected exceptions quite correctly fall into the oops handler.

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.

Quoted 3 lines

do we support setjump/longjump
in kernel? The patch as I saw it does reimplement a similar thing (check out its assembler fragments).

Standard kernel code does not support setjmp/longjmp. AFAIK the only code that does is the kdb patch where I need the extra protection, when kdb is entered it is a fair bet that something has already gone wrong.

#3 lore raw
In reply to Pete Zaitcev
Quoted 5 lines

From: Keith Owens <[email protected]>
Date: Sat, 23 Feb 2002 23:26:52 +1100

Kernel code already has exception tables to handle invalid addresses, invalid opcodes etc. See copy_to_user and wrmsr_eio for examples.

I think you confuse hardware interrupts or exceptions with language exceptions (which are the topic of current discussion). Language exceptions constitute a fancy equivalent of return code checking. Every time you see the following code, an exception is handled:

int foo () {
int rc;
bar_t *x;

   if ((x = do_bar()) == NULL) { rc = -ENOMEM; goto out_nobar; }
   if ((rc = quux()) != 0) goto out_noquux;
   more_stuff();
   return 0;

out_noquux;
undo_bar(x);
out_nobar:
return rc;
}

Quoted 3 lines

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.

If you understand that we are not talking about oopses here, you will see that we emulate quite a bit of stuff with gotos. The problem with current practice is that it takes a fair bit of discipline to prevent it from growing into spaghetti [*].

Personally, I have no problem handling current practices. But I may see the point of the guy with the try/catch patch. Do not make me to defend him though. I am trying to learn is those exceptions are actually helpful. BTW, we all know where they come from (all of Cutler's NT is written that way), but let it not cloud our judgement.

[*] List of subtlietes in the example, that a number of driver monkeys get wrong:
1. rc must always be set right. Sometimes it's extracted from ERR_PTR, sometimes other ways.
2. You must have the Russian Doll commit-uncommit order. If you cannot fall into this rigid scheme, you must use more functions.
3. Names for labels correspond to what fails, not what has to be undone (or else you cannot move stuff around).

-- Pete

#4 lore raw
In reply to Pete Zaitcev
Quoted 6 lines · Pete Zaitcev

Personally, I have no problem handling current practices. But I may see the point of the guy with the try/catch patch. Do not make me to defend him though. I am trying to learn is those exceptions are actually helpful. BTW, we all know where they come from (all of Cutler's NT is written that way), but let it not cloud our judgement.

The problem here is that when using exceptions, you stop thinking about what is going on underneath at the low level which is really not what one wants in the kernel.

After all, C is just and advanced assembly interface, which is exactly why it's such a great language ;-)

Jes

#5 lore raw
Quoted 9 lines · Jes Sorensen
Quoted 6 lines · Pete Zaitcev

Personally, I have no problem handling current practices. But I may see the point of the guy with the try/catch patch. Do not make me to defend him though. I am trying to learn is those exceptions are actually helpful. BTW, we all know where they come from (all of Cutler's NT is written that way), but let it not cloud our judgement.

Quoted 6 lines

The problem here is that when using exceptions, you stop thinking about what is going on underneath at the low level which is really not what one wants in the kernel.

After all, C is just and advanced assembly interface, which is exactly why it's such a great language ;-)

What is worse is that the exceptions patch has to use assembler to walk the stack frames. Exceptions are being touted as a replacement for goto in new driver code but the sample patch only works for i386. No arch independent code can use exceptions until you have arch specific code that does the equivalent of longjmp for _all_ architectures.

Doing longjmp in the kernel is _hard_, I know because I had to do it for kdb on i386 and ia64. The kernel does things differently from user space and sometimes the arch maintainers decide to change the internal register usage. They are allowed to do this because it only affects the kernel, but any change to kernel register usage will probably require a corresponding change to setjmp/longjmp.

So you have arch dependent code which has to be done for all architectures before any driver can use it and the code has to be kept up to date by each arch maintainer. Tell me again why the existing mechanisms are not working and why we need exceptions? IOW, what existing problem justifies all the extra arch work and maintenance?

#6 lore raw
Quoted 5 lines · Keith Owens

So you have arch dependent code which has to be done for all architectures before any driver can use it and the code has to be kept up to date by each arch maintainer. Tell me again why the existing mechanisms are not working and why we need exceptions? IOW, what existing problem justifies all the extra arch work and maintenance?

Sorry, can't tell you why as I agree wholeheartedly with you. My point was that even if it was possible to implement exceptions 'for free' on all architectures, then it's still not what we want in the kernel. It's just too gross and makes people think about the code the wrong way.

Cheers,
Jes

#7 lore raw
Quoted 8 lines · Jes Sorensen
Quoted 5 lines · Keith Owens

So you have arch dependent code which has to be done for all architectures before any driver can use it and the code has to be kept up to date by each arch maintainer. Tell me again why the existing mechanisms are not working and why we need exceptions? IOW, what existing problem justifies all the extra arch work and maintenance?

Quoted 5 lines

Sorry, can't tell you why as I agree wholeheartedly with you. My point was that even if it was possible to implement exceptions 'for free' on all architectures, then it's still not what we want in the kernel. It's just too gross and makes people think about the code the wrong way.

This seems worthy of a new FAQ entry: http://www.tux.org/lkml/#s15-5
And while I was at it, I moved a bunch of these religious questions into their own section. Section 1 is a bit of a hodge-podge.

				Regards,

					Richard....
Permanent: [email protected]
Current:   [email protected]
#8 lore raw
In reply to Pete Zaitcev
Quoted 6 lines · Pete Zaitcev

Personally, I have no problem handling current practices. But I may see the point of the guy with the try/catch patch. Do not make me to defend him though. I am trying to learn is those exceptions are actually helpful. BTW, we all know where they come from (all of Cutler's NT is written that way), but let it not cloud our judgement.

Uh, that's probably not right. If I've been told/remember correctly, it's a technique that certain old school mainframe OSes use to implement sophisticate fault recovery of various sorts. As you know, one basically rewinds to the original point before the block is called so that you can recover/continue from it.

It's not clear if an OS like Linux could really benefit from it since everything that is so inheritently hotwired in the kernel, nor is it clear how something like exceptions would conceptual map onto that kind of system. Maybe DB/FS stuff would be a good of that stuff if you have a condition that prevents a write to a disk (etc.l.) and because they are data structure intensive systems.

But what about the TCP/IP stack ? or things in the bottom half ?

Those things are a bit more sticky and seem less compatible with exceptions it seems.

bill

#9 lore raw
Quoted 8 lines · Bill Huey
Quoted 6 lines · Pete Zaitcev

Personally, I have no problem handling current practices. But I may see the point of the guy with the try/catch patch. Do not make me to defend him though. I am trying to learn is those exceptions are actually helpful. BTW, we all know where they come from (all of Cutler's NT is written that way), but let it not cloud our judgement.

Quoted 5 lines

Uh, that's probably not right. If I've been told/remember correctly, it's a technique that certain old school mainframe OSes use to implement sophisticate fault recovery of various sorts. As you know, one basically rewinds to the original point before the block is called so that you can recover/continue from it.

You can't do that w/out an integrated resource allocation/deallocation system. This because real code ends up by allocating resources ( or doing whatever operation that needs an undo ) during its path and if you do not have an automatic resource deallocation you're going to leak resources more than Harleys engine oil. So w/out such system you've to catch exceptions at every level where you actually own resources with the code that is likely->surely to be way worse than the kernel gotos. Where you're going to save is in cases where your code does not allocate any resource ( book's code ) and here you save the cost of multiple unwinding 'return's against a single catch link. So, in case that an exception happen ( very low probability compared to the common path ) and in case your code underneath the catch point does not own resources, you're going to have a 'little' advantage. What is the cost ? You're going to push onto the common path the exception code by slowing down the CPU's fast path.

- Davide

#10 lore raw
In reply to Bill Huey
Quoted 7 lines · Davide Libenzi

You can't do that w/out an integrated resource allocation/deallocation system. This because real code ends up by allocating resources ( or doing whatever operation that needs an undo ) during its path and if you do not have an automatic resource deallocation you're going to leak resources more than Harleys engine oil. So w/out such system you've to catch exceptions at every level where you actually own resources with the code that is likely->surely to be way worse than the kernel gotos. Where you're

It's a different attitude to doing recoverable systems. I can't fault nor praise it because I've never used a system like that.

Quoted 3 lines

going to save is in cases where your code does not allocate any resource ( book's code ) and here you save the cost of multiple unwinding 'return's against a single catch link. So, in case that an exception happen ( very

Right, not many systems inside a kernel are the kind that you need a sophisticated resource allocation/deallocation system except for, maybe, FSes and possibly long chains of IO operations. I'm not sure what else would fit in this category, but it's generally counter to the coding attitude in Unix.

The things that exception would work well for seem to typically be modular and can function independently from other subsystems in the kernel. Unix, on the other hand, is pretty tightly interconnected so each subsystem's state is some what dependent on the others, which can be unwieldly or impossible to unwind properly.

It's almost the same kind of design style issue as the classic dead lock problem. Some OSs provide a facility to detect those circular allocation, but others just bail on that heavy weight idea and make it intrinsic to the system that dead locks shouldn't exist in the first place. Both solutions have their advantages, but Unix prefers the latter and it seems to lead to a much snappier system from my intuition of it.

Quoted 4 lines

low probability compared to the common path ) and in case your code underneath the catch point does not own resources, you're going to have a 'little' advantage. What is the cost ? You're going to push onto the common path the exception code by slowing down the CPU's fast path.

Not sure, memory allocators are typically pretty primitive relative to modern GCs. I guess you could apply generational techniques to memory allocation (stack allocation of objects typical) and have it run pretty fast, but certainly not as fast as the typical hotwired C path.

It's not the only resource that might need to be unwound. Like what do you do about IO system commits ? I'm not sure here.

Just bored and babbling here. ;-)

bill