Re: Memory Management Question

Jamie Lokier (lkd@tantalophile.demon.co.uk)
Thu, 8 Oct 1998 14:25:04 +0100


On Wed, Oct 07, 1998 at 01:49:15PM -0700, Brian Kress wrote:
> I'm trying to port some kernel code to Linux, and am
> running into one problem. Some of the code has a need to lock
> some pages from a user process into memory and then map that memory
> into the kernel's address space. This way the kernel driver code
> can access those pages directly, instead of through memcpy_from_user
> and friends. Is this possible?

- If you block the user process, then you can access the user-space data
safely. Most drivers do this one way or another.

- With cooperation from the user process, you can mmap()
driver-allocated pages into user space so they're shared.
For example, the sound driver does this, so do some network
software using custom drivers. I wouldn't be surprised if the
video framebuffer and video capture drivers do this too.

- User space, as root, can call mlock() on general pages (any kind)
after which the driver can safely assume the pages are mapped and
stay in the same place in memory, _if_ the user space process
is cooperating. Then you can do asynchronous stuff.
Beware of things like the user space process dying unexpectedly
and freeing the page mapping -- you may need to keep a reference the
mm struct somehow in the device driver, to prevent it being freed
while you're using it.

Locking user pages from the device side to make the user space process
block on accessing them isn't supported at present, and won't be done
before 2.3.

Note that locking and unlocking pages often has its own overheads that
may make just copying the data faster. Look in the linux-kernel
archives for "zero-copy network" type of thing and see the points made
about TLB flushing overhead when it's done without user space cooperation.

Hope this helps,
-- Jamie

-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.rutgers.edu
Please read the FAQ at http://www.tux.org/lkml/