Keeping kernel memory in sync in all address spaces

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
pillow

Keeping kernel memory in sync in all address spaces

Post by pillow »

Say I allocate a page of memory from the kernel heap, and map that page into kernel space. Now if I switch tasks (new page directory), it'll revert back to the old layout before I had mapped the new memory in, which is obviously undesirable.

This isn't problematic if all page directories shared the page tables describing the kernel, since changes to that page table would then take effect in all address spaces. But what if a new page table needed to be created to satisfy the allocation request? We'd have to go through every process in the system and add this new page table into their address spaces. Is this an acceptable solution, or are there better ways? How do most operating systems typically deal with -- or avoid -- this kind of situation?
proxy

Re:Keeping kernel memory in sync in all address spaces

Post by proxy »

well personally I go with pre-allocating kernel heap page tables, so they are always in all processes.

But I have plans to improve this in the future.

Basically what I plan is this. First of all, I will have a rule, that page tables once used to map a dynamic region of kernel memory, will always map that region, in other words, they are there for good. Then I will have a special case in my page fault handler where it'll check if the page fault is due to a page table in the appropriate region, if so, copy from a "master copy". Since page tables go in but never out, this should work nicely and will only do the copy on an as needed basis.

proxy
rootel77

Re:Keeping kernel memory in sync in all address spaces

Post by rootel77 »

? How do most operating systems typically deal with -- or avoid -- this kind of situation?
I think Linux has the same problem in the "vmalloc" zone. this is an extract from the "Understanding the linux kernel" book descirbing the solution adopted by linux:
Notice that the Page Tables of the current process are not touched by vmalloc_area_pages( ). Therefore, when a process in Kernel Mode accesses the noncontiguous memory area, a Page Fault occurs, since the entries in the process's Page Tables corresponding to the area are null. However, the Page Fault handler checks the faulty linear address against the master kernel Page Tables (which are init_mm.pgd Page Global Directory and its child Page Tables; see Section 2.5.5). Once the handler discovers that a master kernel Page Table includes a non-null entry for the address, it copies its value into the corresponding process's Page Table entry and resumes normal execution of the process. This mechanism is described in Section 8.4.
Extarct from Section 8.4
We have seen in Section 7.3 that the kernel is quite lazy in updating the Page Table entries corresponding to noncontiguous memory areas. In fact, the vmalloc( ) and vfree( ) functions limit themselves to update the master kernel Page Tables (i.e., the Page Global Directory init_mm.pgd and its child Page Tables).

However, once the kernel initialization phase ends, the master kernel Page Tables are not directly used by any process or kernel thread. Thus, consider the first time that a process in Kernel Mode accesses a noncontiguous memory area. When translating the linear address into a physical address, the CPU's memory management unit encounters a null Page Table entry and raises a Page Fault. However, the handler recognizes this special case because the exception occurred in Kernel Mode and the faulty linear address is greater than TASK_SIZE. Thus, the handler checks the corresponding master kernel Page Table entry:
Slasher

Re:Keeping kernel memory in sync in all address spaces

Post by Slasher »

I make global mapping of kernel related tables and pages into all the page directories when a page fault occurs due to a process trying to write to an unmapped region of memory.

This usually happens when a call is made to the kernel memory allocator.

i.e on page fault just add the table entry to all the page directories in the system.
pillow

Re:Keeping kernel memory in sync in all address spaces

Post by pillow »

Thanks!
Post Reply