Re: Yet another set of VM questions

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



"Kirk True" <ktrue@movaris.com> writes:

> Hi all,
> 
>     1. Regarding non-contiguous memory allocation, what is the need to have
>        *virtually* contiguous but not *physically* contiguous pages?

This is the essential purpose of virtual memory management.
Say a user-space program wants to allocate a 1MB array for some
reason. In userspace, we want that memory to appear virtually
contiguous (so that int x = *(bigArray+50000) works as expected),
but we really couldn't care less where the pages are physically
located. The same is often true in the kernel - if you need to
allocate a 32K buffer for some reason, and there's no particular
reason (such as DMA requirements) to make the buffer physically
contiguous, then it's usually a lot easier to find 8 singleton
free pages than it is to find 8 contiguous free pages.

>     2. UtLVMM says that vmalloc is only used in the kernel for storing swap
>        information - yet it's used by a bunch of drivers which are 
>        considered part of the kernel; is it just semantics?

Perhaps it really meant, the VM subsystem only uses vmalloc
in this fashion? I don't know, the statement just sounds wrong
to me.

>     3. Is vmalloc called from user-mode ever?

No. Nothing in the kernel is ever called from user-mode, ever.
The only way for userspace code to invoke kernel code is by
making a user->kernel context switch (system call).

>     4. What is the difference between kmalloc, malloc, and vmalloc?

kmalloc and vmalloc are kernel facilities that are totally unrelated
to malloc, the C standard library function.

kmalloc (IIRC) allocates contiguous regions of physical pages in
kernelspace. (It is a wrapper around get_free_pages().)

vmalloc allocates arbitrary collections of physical pages, but maps
them virtually-contiguously into kernelspace.

malloc is called from userspace when a user program wants to allocate
anonymous virtual memory. It makes a system call and requests the
appropriate allocation from the kernel, which merely does the VM-area
bookkeeping necessary to *logically* associate the requested VM with
the process. The memory is not physically allocated unless the
userspace code attempts to access it (in which case the pagefault
handler serves the fault by consulting the proc's VM-areas and
physically allocating a page to the process).

>     5. Anonymous memory is memory that is *not* backed by a file, such as
>        the stack or heap space, right? And mmap is called when mapping 
>        files into memory, right? The why does mmap deal with anonymous
>        memory (sorry, I'm totally confused here)?

Anonymous memory *is* (sometimes) backed by a file: the swap
file. However, I've never really looked closely at the mmap()
imeplementation, so I don't know if this fact addresses your question
or not.

It may also have to do with shared memory management (eg the shmat()
etc. system calls). I believe these are implemented in terms of
mmap(), though I may well be wrogn (as I've never bothered to look).

HTH,

-- Joe Knapka

-- 
(let ((antichrist 'me) (anarchist 'me))) -- the sexp-pistols.
--
Kernelnewbies: Help each other learn about the Linux kernel.
Archive:       http://mail.nl.linux.org/kernelnewbies/
FAQ:           http://kernelnewbies.org/faq/


[Index of Archives]     [Newbies FAQ]     [Linux Kernel Mentors]     [Linux Kernel Development]     [IETF Annouce]     [Git]     [Networking]     [Security]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux SCSI]     [Linux ACPI]
  Powered by Linux