Red Hat axp-list: Linux on Alpha (1995-2022)
 help / color / mirror / Atom feed
From: Linus Torvalds <torvalds@cs.Helsinki.FI>
Subject: Re: Linux-2.1.4
Date: Thu, 17 Oct 1996 09:06:04 +0300 (EET DST)	[thread overview]
Message-ID: <ac6248efa545a4eea58974737a2d194b.axp-list@archive.invalid> (raw)
In-Reply-To: <Pine.LNX.3.93.961016194825.19685A-100000@pc5829.hil.siemens.at>

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

       reply	other threads:[~1996-10-17  6:06 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <Pine.LNX.3.93.961016194825.19685A-100000@pc5829.hil.siemens.at>
1996-10-17  6:06 ` Linus Torvalds [this message]
     [not found] <Pine.LNX.3.91.961018075100.23966E-100000@linux.cs.Helsinki.FI>
1996-10-18  7:34 ` Linux-2.1.4 Richard Henderson
     [not found] <Pine.LNX.3.95.961017232056.6666A-100000@odin.asgard.hr>
1996-10-17 22:03 ` Linux-2.1.4 Richard Henderson
1996-10-18  5:16   ` Linux-2.1.4 Linus Torvalds
     [not found] <Pine.LNX.3.93.961017124005.6940C-100000@pc5829.hil.siemens.at>
1996-10-17 20:26 ` Linux-2.1.4 Richard Henderson
     [not found] <Pine.LNX.3.91.961017130736.25546C-100000@linux.cs.Helsinki.FI>
1996-10-17 11:52 ` Linux-2.1.4 Ingo Molnar
1996-10-17 14:00 ` Linux-2.1.4 Linus Torvalds
1996-10-17 10:31 Linux-2.1.4 David A Rusling
     [not found] <Pine.LNX.3.93.961017104701.8482A-100000@pc5829.hil.siemens.at>
1996-10-17 10:18 ` Linux-2.1.4 Linus Torvalds
  -- strict thread matches above, loose matches on Subject: below --
1996-10-17 10:03 Linux-2.1.4 David A Rusling
     [not found] <Pine.LNX.3.91.961017082647.20618H-100000@linux.cs.Helsinki.FI>
1996-10-17 10:00 ` Linux-2.1.4 Ingo Molnar
     [not found] <199610171003.KAA00502@linux.reo.dec.com>
1996-10-17  9:03 ` Linux-2.1.4 Linus Torvalds
     [not found] <Pine.LNX.3.91.961016101122.19371J-100000@linux.cs.Helsinki.FI>
1996-10-16 17:20 ` Linux-2.1.4 Theodore Y. Ts'o
1996-10-16 18:58   ` Linux-2.1.4 Ingo Molnar
1996-10-17  4:51   ` Linux-2.1.4 Linus Torvalds
1996-10-17  3:42 ` Linux-2.1.4 Marc Singer
1996-10-17 12:39   ` Linux-2.1.4 Jim Nance
     [not found] <9610160531.AA04330@feral.com>
1996-10-16  7:18 ` Linux-2.1.4 Linus Torvalds
     [not found] <Pine.LNX.3.95.961015224149.4897A-100000@hlermen.ffm.fgan.de>
1996-10-15 21:56 ` Linux-2.1.4 Hans Lermen
1996-10-15 18:41 Linux-2.1.4 Michael Stiller
1996-10-15 21:15 ` Linux-2.1.4 Hans Lermen
     [not found] <Pine.LNX.3.91.961010180237.1289A-100000@linux.cs.Helsinki.FI>
1996-10-15 17:14 ` Linux-2.1.4 Linus Torvalds
read-only archive

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox