Memory Alloation Self Learning 1

Background

Recently I have been struggled with falco crashes and OOM issue. After seeking for community help is hopeless, I decide to check on my own. So better I have some background information in it bfore I begin.

Content

First let me briefly describe how kernel allocates memory. Cpu initiates a comamnd to ask for data. It first lands on cache. If there was a data cached before, then just read it into CPU, this is fast. Otherwise instruction will proceed to translate the virtual addr. It will check TLB if the addr has been translated before, if yes then we just query the data with the alreadu stored physical addr. If not, the MMU will perform CR3(if this is x86 arch) and page table translation to get the actual physical addr of the required data and return it with CPU.

So what is a virtual address. Usually a process will have two stack. User and Kernel. which on a32 bit system, User stack will takes up 3Gb virtual space and Kernel stack will use 1GB virtual space addressable by kernel virtual addr. Kernel stack is shared between process while user is not.

There is a parameter of kernel config_vm_split if I don’t remember it wrong when building the kernel.

Both stacks are addressable by virtual address and the translation process are managed by above process. Usually when a user(one application) is taking some address of a variable it usually returns the virually addr. kernel provides a helper function to get it translated `virt_to_phy· saddly I forget the actual function signatures.

Also there is directly map trgion that physicall memory will maps directly into virtual addr from lower region. the lowest is used for user virual address(0x0 you can think it begins at)). What is the offset? Check PAGE_OFFSET, it is usually 0x0000c000000000 on 64 bit.

There are sevaral functions to allocate a memory block inside kernel(ignore the signature, there is params into the functions).

  • alloc_pages() or alloc_pages_exact() to use BSA to allocate physical continuous mem block directly.
  • devm_kmalloc or devm_kzalloc, the later one is more used since it will zero out the memory block. These are used when you are writing a device driver.
  • Kmalloc or kzalloc or kvmalloc or kvzalloc or kcalloc and(ksize to get sizes, kfree to free). The latter two are called for allocating virtual memory block and the first are for physical ones. Both assure that the allocated memory block in the relative space are continuous. For exmaple kvzalloc assures continuous in vitural but not in physical space.

When to use which? Let me digger deeper in the next post(maybe if I won’t forget to write it)

Leave a Reply

Your email address will not be published. Required fields are marked *