From mboxrd@z Thu Jan 1 00:00:00 1970 Message-ID: X-Archive-Original-Message-ID: <199610170526.BAA10266@churchy.gnu.ai.mit.edu> Date: Thu, 17 Oct 1996 09:06:04 +0300 (EET DST) From: Linus Torvalds Subject: Re: Linux-2.1.4 In-Reply-To: On Wed, 16 Oct 1996, Ingo Molnar wrote: > > On Wed, 16 Oct 1996, Theodore Y. Ts'o wrote: > > > So will it be faster now to copy everything from userspace into a kernel > > "bounce buffer" first, instead of fetching things from user space one > > byte at a time? That seems counter-intuitive, since traditionally the > > way you get speedups is to *reduce* the number of memory copies while > > going through a network or tty layer.... > > i think you can do something like this: > > if (exception()) { > printk("this is a test message in the tty exception handler"); > return -EFAULT; > } > > byte_by_byte fetching and tty logic; > > end_exception(); Not any more. This _was_ the original exception interface I used, and the theory was exactly as you state: to move the exception handling to outside any loops and thus make the overhead of exceptions negligible. However, reality intruded on my dreams. There were two problems with the above approach (they were obvious pretty quickly: by the time I released 2.1.3 I already knew I had to change the interface): - it worked on a C language level, and that meant that the exception code had to do a lot of state saving in order to restore the state at the time of the exception. Saving 4-5 registers etc.. (remember: I'm actually counting instructions here, so saving 5 registers is a _minimum_ of 5 instructions. Too much overhead ;) - it was _extremely_ tricky to be 100% sure that the above kind of thing worked correctly in the face of compiler optimizations. Essentially, the compiler needs to know about that kind of "longjmp" behaviour, and while it is possible to tell gcc about longjmp that also has a rather detrimental effect on code generation and on error reporting. Also, the problem was that the above kind of interface put the burden of being tricky on the people who wrote basic kernel code, and that's bad. Because there is a LOT of basic kernel code, so having a complex interface is a bad thing because that makes the kernel complex. It _also_ had the problem that it still needed the explicit "verify_area()" calls due to various architecture-specific "features" (like the lack of reliable write protection reporting on 386 chips and problems on ARM etc). So, in 2.1.4 I changed the rules. Instead of having a complex interface, I changed it to use a very simple interface that has a hairy and ugly architecture-specific implementation. But that _implementation_ can be hidden in header files and a few small architecture-specific files, so now the complexity is in the right place (concentrated in one place that you can go over with a microscope to make sure it works), rather than scattered all over the kernel. Also, the 2.1.4 interface is totally abstracted, so I moved the exception handling to the assembler level: that allowed me to do much less state saving (in fact, I do _no_ state saving, I just put the information on where to resume after a exception into a register). It also made the worry about what the compiler does go away. (It also makes it easier to support nested exceptions if I ever decide that it's a good idea - the original code really couldn't handle nested exceptions very cleanly at all, and certainly not efficiently). So the 2.1.4 interface (and I'm pretty certain this is the "final" interface) doesn't actually export the notion of exceptions to the kernel proper at all. Nobody sees the exceptions themselves, because the magic is all hidden in architecture-specific files. That also means that if there is an architecture that cannot reliably generate exceptions we can just do it all "by hand", and the kernel proper never even knows that exceptions aren't used. Now, this all means that you can't move the exception handling to outside any loops: you simply cannot separate the exception handling and the actual user access any more. On the other hand, the new code is a _lot_ simpler to use than the original exception handling, and it's also actually faster for the case where you can't move it out of the loop. Quite noticeably less overhead, actually. Just as an example: the optimal code right now for getting something from user space looks like this (x86 assembly) movl current_set,%ebx /* get the "current" pointer */ movl $1f,%edx /* tell the exception code where to abort */ incl 984(%ebx) /* increment exception count */ movl xxxx,%eax /* get the value */ decl 984(%ebx) /* decrement exception count */ xorl %edx,%edx /* no error if we got here */ 1: That's just 6 instructions to get a word from user space. The full sequence is a bit more, because it needs to verify the address (check that it isn't a kernel address unless it's allowed to be) but it's still not very much. It's roughly 10-12 instructions (and _no_ loop - just one test) all told, depending a bit on how gcc organizes it all. And eventually, when the exception code has matured, I can actually remove the "sanity check" code, and then the exception count tests are not needed any more, so the above shrinks down to just movl $1f,%edx /* tell the exception code where to abort */ movl xxxx,%eax /* get the value */ xorl %edx,%edx /* no error if we got here */ 1: which is just about optimal (remember: the above three instructions essentially make the whole "verify_area()" rigamole totally unnecessary, and of the above three instructions one is obviously required even if you don't check the address: the one that actually gets the value from user space ;) Now, the above is the reason why I am so certain that the new exception code is the "right thing". I can actually _count_ the instructions needed to do the full test for validity, and go through them one by one and make sure that none of them are useless. With _three_ instructions I can do what used to need a loop through the virtual memory management structures. Now, because the checking cannot be moved outside the loop, some loops have to be moved inside the checking if we want to have the best possible performance. That's why I want to add the new functions "clear_user()" and "strncpy_from_user()" : those are things where we're currently looping outside the exception code for no really good reason (as a side note: those are also things that generally need architecture-specific functions for the best performance anyway). All the other loops I have looked through that do checking inside the loop are not really a problem. They do some non-trivial operation on the data inside the loop (translating a byte with opost processing in the case of the tty layer, for example), and as such the very small overhead caused by the exception handling isn't really an issue. It's the RISC philosophy: make the basic operations simple, and do them extremely quickly. Complex operations might be slower, but complex operations are relatively rare. Linus -- To unsubscribe: mail -s unsubscribe axp-list-request@redhat.com < /dev/null