5

In almost all books and articles I have read about about HIGHMEM in Linux kernel, they say while using 3:1 split, not all of the 1GB is available to the kernel for mapping. And normally its 896MB or so, with the rest being used for kernel data structures, memory maps, page tables and such.

My question is, what exactly are these data structures? Page tables are normally accessed via a page table address register, right? And the base address of page table is normally stored as a physical address. Now why do one need to reserve a virtual address space for the entire table?

Similarly, I read about kernel code itself occupying space. What does that have to do with virtual address space? Is it not the physical memory that would be consumed for storing the code?

And finally, these data structures why do they have to reserve the 128MB space? Why can't they be used out of the entire 1GB address space, as required, like any other normal data structure in kernel would do?

I have gone through LDD3, Professional Linux Kernel Architecture and several posts here at stack-overflow (like: Why Linux Kernel ZONE_NORMAL is limited to 896 MB?) and an older LWN article, but found no specific information on the same.

2 Answers 2

4

With regards to page tables, it's true that the MMU wouldn't care if the page tables themselves weren't mapped in the virtual address space - for the purposes of address translations, that would be OK. But when the kernel needs to modify the page tables, they do need to be mapped in the virtual address space - and the kernel can't just map them in "just in time", because it needs to modify the page tables themselves to do that. It's a chicken-and-egg problem, which means that the page tables need to remain mapped at all times.

A similar problem exists with the kernel code. For the code to execute, it must be mapped in the virtual address space - and if the code that does the page table modification were itself not present, we'd have a similar chicken-and-egg problem. Given this, it's easier to leave the entireity of the kernel code mapped all the time, along with the kernel-mode stacks and any kernel data structures access by code where you wouldn't want to potentially take a page fault. One large example of such data structures is the array of struct page structures, representing each physical memory page.

1

The 128MB reserve is not for a specific data structure, that always use it.
It's virtual memory, reserved for various users, which might use it. Normally, it's not all used.

About physical and virtual memory: every allocation needs three things - a physical page, a virutal page, and mapping connecting the two. Linux almost never uses physical addresses directly, it always passes virtual address translation.
For most kernel memory allocation (called lowmem), this translation is quite simple - subtract some constant from the virtual address to get the physical. But still, a virtual address is used.

Linux's memory management was written when the virtual memory space (4GB) was much larger than the physical memory, even on the largest machines. In such cases, wasting virtual addresses is not a problem. Today, when physical memory is large, this leads to inefficiencies and problems.

The vmalloc virtual address range is used by any caller of vmalloc. For example:
1. Loading kernel drivers (using modprobe or insmod).
2. Kernel modules often allocate with vmalloc. The alternative function kmalloc used to be limited to 128K, and it rounds the size up to a power of 2, so vmalloc is often preferred for large allocations.

5
  • If 128MB is not reserved for a specific data structure, why is it that we can't use whole 1G kernel-virtual-space to be mapped to some virtual address? That way if a system has exactly 1GB physical memory, one does not need to use HIGHMEM at all. In the 896MB based setting, one needs HIGHMEM to utilize any physical memory > 896MB in size. Again, when we reserve 128MB for something (or even not use it), that much virtual address range becomes unavailable for vmalloc. And as you said everything is accessed through virtual address. They why waste any virtual addresses for nothing? Jul 19, 2012 at 14:33
  • @codetwiddler, it's quite complicated... It is bad to waste virtual addresses, but it isn't for nothing. The basic point is that there are two methods to connect virtual addresses to physical ones - direct mapping (lowmem) and arbitrary dynamic mapping (vmalloc). Each has to use a separate virtual address range, so they won't collide.
    – ugoren
    Jul 19, 2012 at 14:37
  • Agreeing on everything, I know there are two methods and its uses. Now, specifically, if I have 1GB physical memory, I need to use HIGHMEM to access more than 896MB because of that 1GB-896MB chunk being reserved. What's the purpose of that, how ever complicated it is? That reservation should have some struct or something associated with that space, or at least a pointer to it somewhere, and I'd assume it should be quite important an entity if that reservation is made statically. Jul 19, 2012 at 16:40
  • Also, in your previous answer you said "Normally, it's not all used.", and here you say "but it isn't for nothing". Unfortunately, that's sort of confusing for me. Jul 19, 2012 at 16:43
  • Explaining lowmem/vmalloc here in detail is too much for me. Sorry. About your confusion - emergency exits are normally not used, yet they're not there for nothing. 128M of vmalloc space are also normally not used. Yet reserving it is important for all kinds of unexpected configurations.
    – ugoren
    Jul 22, 2012 at 11:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.