Re: Doubt regarding memory

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

 



Hello Tzahi

You really give me tough Q :)
Let's say we have 1GB ram in the machine.
The kernel uses virtual addresses 3gb-4gb to address using identity mapping to 1gb. Where does user space gets it's memory in physical ram? Obviously it will have to intermingle with other kernel allocations. Is there a rule for that or some kind of ordering in physical memory?

In general, this is what happens if user space asks for memory allocation
1. user space asks for memory allocation, i.e via malloc(), mmap, sbrk() or else.

2. certain VMA is extended, or new VMA is created, but no page is really allocated (deferred allocation).

3. When the application hits this "blank" area, page fault is generated.

4. kernel intercept this fault and check that this fault is indeed happen inside valid VMA (actually there are many path possibilities, but I am assuming the valid one)

5. The page is allocated (I forgot the function's name, maybe get_pages()). They are allocated after searching pages that is marked as free in the selected memory zone. Sometimes, IIRC, it doesn't need to check the zone pool, but simply checking from the slab.

6. Before page are returned to user space, the related page tables that points to these pages are manipulated so that they are using user space virtual address space range (the clue is by looking where the fault did happen)

Note those pages now could have two mapping (if they lies inside normal and DMA zone), one is via identity mapping, one is that process's own mapping


Let's say we have 2gb ram.
If the kernel already allocated it's 3-4gb (1gb) but it wants more, it will have to resort to taking from virtual user space addresses, right? but they won't be protected so what gives?
If the kernel wants more, it can do so via temporary mapping (kmap()). By doing it, the highmem pages will be temporarily mapped in the area slightly beyond the end of kernel identity mapping (AFAIK, this is the one we know as the upper 128 MB area, use for vmalloc, fixed linear mapping and temporary mapping. But since it is just temporary, the mapping *must be* released as soon as it is not used, because you don't do so, there will be lesser space for the mapping and if all are occupied, kmap() can no longer map highmem pages.

For bigger memory size, IIRC kernel have the ability to use PAE (in x86 32 bit), but it still doesn't eliminate the need of temporary mapping since your kernel address space is still 1GB (again, in 3:1 VM split)

In other word, if you have big RAM, let's say 2GB or more, your best choice is by switching to 64 bit platform. There, all RAM pages are directly addressable (and actually, there is no more highmem zone).


I wish my explanation really clears the confusion, because once I struggled a lot with these things too and slowly I got the idea.

regards,

Mulyadi


--
To unsubscribe from this list: send an email with
"unsubscribe kernelnewbies" to ecartis@xxxxxxxxxxxx
Please read the FAQ at 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