简体   繁体   English

Linux内核中的内存分配

[英]Memory Allocation in Linux Kernel

I had an interview today and was asked this question. 今天我接受了采访,并被问到这个问题。 What Kernel Memory allocation strategy would you use, if you were asked to allocate memory of size 2KB and that allocated memory should be page aligned. 如果要求您分配大小为2KB的内存,并且所分配的内存应与页面对齐,则将使用哪种内核内存分配策略。

KMALLOC handles smaller memory allocation strategies but the lowest unit that it supports is 4KB, which is the size of the physical page. KMALLOC处理较小的内存分配策略,但支持的最低单位为4KB,即物理页面的大小。 I asked him, if he was expecting slab allocators? 我问他,他是否期望平板分配器? He didn't reply positively. 他没有积极回应。

For page aligned memory allocation use alloc_pages/alloc_page . 对于页面对齐的内存分配,请使用alloc_pages / alloc_page You can also use _ _get_free_pages/__get_free_page . 您还可以使用_ _get_free_pages / __ get_free_page __get_free_page eventually uses alloc_pages only. __get_free_page最终仅使用alloc_pages。 These functions are used to allocate page from physical memory. 这些功能用于从物理内存分配页面。 The allocator for these is physical memory allocator or buddy allocator 这些分配器是物理内存分配器伙伴分配器

Your assumption that kmalloc allocates minimum 4KB of memory is wrong. 您关于kmalloc分配最少4KB内存的假设是错误的。 The kmalloc is based on slab allocator . kmalloc基于slab分配器 Do a cat /proc/slabinfo you will know that there are several slabs already created for kmalloc. 做一个cat / proc / slabinfo,您将知道已经为kmalloc创建了几个平板。 These slabs will reduce the internal memory fragmentation for allocations using kmalloc. 这些平板将减少使用kmalloc进行分配的内部内存碎片。 So if you allocate 4 bytes then only 8 bytes will be allocated from kmalloc slab of kmalloc-8(4 bytes of internal fragmentation). 因此,如果您分配4个字节,则将从kmalloc-8的kmalloc平板中分配8个字节(内部分段为4个字节)。 If you allocate 9 bytes then 16 bytes are allocated from kmalloc-16 slab, and so on. 如果分配9个字节,则从kmalloc-16 slab分配16个字节,依此类推。

kmalloc, alloc_page/s, __get_free_page/s dont require page tables. kmalloc,alloc_page / s,__ get_free_page / s不需要页表。 The virtual memory address returned are just offsetted address . 返回的虚拟内存地址只是偏移地址

Though you have not asked, I will mention that vmalloc is another allocation technique which uses resource map allocator . 尽管您没有要求,但我会提到vmalloc是另一种使用资源映射分配器的分配技术。 The noncontiguous memory allocated via vmalloc is accessed by using kernel master page table ( swapper_pg_dir ) 使用内核主页面表swapper_pg_dir )访问通过vmalloc分配的非连续内存。

The different Linux allocators are mentioned in Mel Gorman book and Professional linux kernel architecture book. 在Mel Gorman的书和Professional linux内核体系结构的书中提到了不同的Linux分配器。 Go through these, it will help. 通过这些,将有所帮助。

I read in: http://www.makelinux.net/books/lkd2/ch11lev1sec4 我读到了: http : //www.makelinux.net/books/lkd2/ch11lev1sec4

The function returns a pointer to a region of memory that is at least size bytes in length 该函数返回一个指向至少为字节大小的内存区域的指针

I know the system Operation x86 ( 32 bits ) - the max size mapping in RAM 4GB, but it include Reference and information brute. 我知道系统运行x86(32位)-RAM 4GB中的最大大小映射,但是它包括参考和信息蛮力。

so 4GB is the all space available, convert for 'KB' = 4194304 KB, The Memory Ram iqual the Grid 4194304 spaces( index and bory information ), or the side of square( in latin Radix Quadratum ) SQRT(4194304)= all size only index the information, as well as below "int flags") 因此4GB是所有可用空间,请转换为'KB'= 4194304 KB,内存Ram等于网格4194304空格(索引和进位信息),或者正方形的边(在拉丁语Radix Quadratum中)SQRT(4194304)=所有大小仅索引信息,以及在“ int标志”下方)

void * kmalloc (size_t size, int flags) void * kmalloc (size_t大小,int标志)

you might want to you use the function, for use KB diferent 2 or 4. 您可能希望使用该功能,以使用不同的KB 2或4。

att ATT

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM