How does the linux kernel manage less than 1GB physical memory?

后端 未结 5 1493
伪装坚强ぢ
伪装坚强ぢ 2020-12-12 09:50

I\'m learning the linux kernel internals and while reading \"Understanding Linux Kernel\", quite a few memory related questions struck me. One of them is, how the Linux kern

5条回答
  •  無奈伤痛
    2020-12-12 10:17

    osgx has an excellent answer, but I see a comment where someone still doesn't understand.

    Every article explains only the situation, when you've installed 4 GB of memory and the kernel maps the 1 GB into kernel space and user processes uses the remaining amount of RAM.

    Here is much of the confusion. There is virtual memory and there is physical memory. Every 32bit CPU has 4GB of virtual memory. The Linux kernel's traditional split was 3G/1G for user memory and kernel memory, but newer options allow different partitioning.

    Why distinguish between the kernel and user space? - my own question

    When a task swaps, the MMU must be updated. The kernel MMU space should remain the same for all processes. The kernel must handle interrupts and fault requests at any time.

    How does virtual to physical mapping work? - my own question.

    There are many permutations of virtual memory.

    • a single private mapping to a physical RAM page.
    • a duplicate virtual mapping to a single physical page.
    • a mapping that throws a SIGBUS or other error.
    • a mapping backed by disk/swap.

    From the above list, it is easy to see why you may have more virtual address space than physical memory. In fact, the fault handler will typically inspect process memory information to see if a page is mapped (I mean allocated for the process), but not in memory. In this case the fault handler will call the I/O sub-system to read in the page. When the page has been read and the MMU tables updated to point the virtual address to a new physical address, the process that caused the fault resumes.

    If you understand the above, it becomes clear why you would like to have a larger virtual mapping than physical memory. It is how memory swapping is supported.

    There are other uses. For instance two processes may use the same code library. It is possible that they are at different virtual addresses in the process space due to linking. You may map the different virtual addresses to the same physical page in this case in order to save physical memory. This is quite common for new allocations; they all point to a physical 'zero page'. When you touch/write the memory the zero page is copied and a new physical page allocated (COW or copy on write).

    It is also sometimes useful to have the virtual pages aliased with one as cached and another as non-cached. The two pages can be examined to see what data is cached and what is not.

    Mainly virtual and physical are not the same! Easily stated, but often confusing when looking at the Linux VMM code.

提交回复
热议问题