Linux KMALLOC 大小分配

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12568379/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 14:26:13  来源:igfitidea点击:

KMALLOC size allocation

clinuxmemorylinux-kernel

提问by shd

Does KMALLOC allocates only in page size memory or it can allocate less ? What are the sizes that the kmalloc can allocate ? Where can I find description of it, as everywhere I looked it doesn't really say how much memory it allocates ? What I want to know is what are the actual sizes that KMALLOC allocates. Does it allocate size of power of 2 ? Does it just find free objects from the cache that is ready ?

KMALLOC 是只分配页面大小的内存还是可以分配更少的内存?kmalloc 可以分配的大小是多少?我在哪里可以找到它的描述,因为我到处看它并没有真正说明它分配了多少内存?我想知道的是 KMALLOC 分配的实际大小是多少。它是否分配 2 的幂的大小?它是否只是从准备好的缓存中找到空闲对象?

采纳答案by Mike

My understanding is as follows: the kernel is dealing with the physical memory of the system, which is available only in page-sized chunks; thus when you call kmalloc()you are going to get only certain predefined, fixed-size byte arrays.

我的理解是这样的:内核在处理系统的物理内存,它只在页面大小的块中可用;因此,当您调用时,您kmalloc()将只获得某些预定义的、固定大小的字节数组。

The actual memory you get back is dependent on the system's architecture, but the smallest allocation that kmalloc can handle is as big as 32 or 64 bytes. You will get back from a call to kmalloc()at leastas much memory as you asked for (usually more). Typically you will not get more than 128 KB (again, architecture dependent)

您返回的实际内存取决于系统的体系结构,但 kmalloc 可以处理的最小分配高达 32 或 64 字节。您将从呼叫中恢复kmalloc()到至少与您要求的一样多的内存(通常更多)。通常你不会得到超过 128 KB(同样,依赖于架构)

To get the page size (in bytes) of your system you can execute the command:

要获取系统的页面大小(以字节为单位),您可以执行以下命令:

getconf PAGESIZE

or

或者

getconf PAGE_SIZE

This information on max page size is in /usr/src/linux/include/linux/slab.h

有关最大页面大小的信息位于 /usr/src/linux/include/linux/slab.h

And yes, the page sizes are generally powers of 2, but again, you're not going to get exactly what you ask for, but a little bit more.

是的,页面大小通常是 2 的幂,但同样,你不会得到你所要求的,但会更多一点。

You can use some code like this:

你可以使用一些这样的代码:

void * stuff;
stuff = kmalloc(1,GFP_KERNEL);
printk("I got: %zu bytes of memory\n", ksize(stuff));
kfree(stuff);

To show the actual amount of memory allocated:

显示实际分配的内存量:

[90144.702588] I got: 32 bytes of memory

回答by ninjalj

You can see some common sizes used by kmalloc()int your system with:

您可以通过以下方式kmalloc()查看系统中使用的一些常见尺寸:

cat /proc/slabinfo  | grep kmalloc

More important than the consideration of whether kmalloc can allocate less than a page is the question of whether it can allocate more than a page: if you are calling kmalloc()in atomic context (with the GFP_ATOMICflag), it can't and won't try very hard to find contiguous pages in memory, so if your memory is very fragmented, and the order of your allocation is high (allocation size → pagesize*2^(allocation order)), the allocation may fail. So, in atomic context, big allocations are likely to fail.

比考虑 kmalloc 是否可以分配少于一个页面更重要的是它是否可以分配多于一个页面的问题:如果您kmalloc()在原子上下文中调用(带有GFP_ATOMIC标志),它不能也不会尝试非常在内存中很难找到连续的页面,因此如果您的内存非常碎片化,并且您的分配顺序很高(allocation size → pagesize*2^(allocation order)),则分配可能会失败。因此,在原子上下文中,大分配可能会失败。

There is an example of a failed allocation of order 7 (512 Kb) at this other SO question about maximum AF_UNIX datagram size.

另一个关于最大 AF_UNIX 数据报大小的 SO 问题中,有一个 7 (512 Kb) 阶分配失败的示例。

回答by iGRJ

It all depends on the allocator used in your kernel. Slab, Slub or Slob. See Following kernel configuration variables:

这一切都取决于内核中使用的分配器。板条、粗条或粗条。请参阅以下内核配置变量:

CONFIG_SLAB
CONFIG_SLUB
CONFIG_SLOB

All above allocators use MAX_ORDERand PAGE_SHIFTto decide the maximum limit of kmalloc()

以上所有分配器都使用MAX_ORDERPAGE_SHIFT来决定 kmalloc() 的最大限制

The largest kmalloc size supported by the SLAB allocators is 32 megabyte (2^25) or the maximum allocatable page order if that is less than 32 MB.

SLAB 分配器支持的最大 kmalloc 大小为 32 兆字节 (2^25) 或最大可分配页面顺序(如果小于 32 MB)。

#define KMALLOC_SHIFT_HIGH      ((MAX_ORDER + PAGE_SHIFT - 1) <= 25 ? \
  (MAX_ORDER + PAGE_SHIFT - 1) : 25)
#define KMALLOC_SHIFT_MAX       KMALLOC_SHIFT_HIGH

SLUB directly allocates requests fitting in to an order-1 page (PAGE_SIZE*2). Larger requests are passed to the page allocator.

SLUB 直接分配适合订单 1 页面 ( PAGE_SIZE*2) 的请求。较大的请求被传递给页面分配器。

#define KMALLOC_SHIFT_HIGH      (PAGE_SHIFT + 1)
#define KMALLOC_SHIFT_MAX       (MAX_ORDER + PAGE_SHIFT)

SLOB passes all requests larger than one page to the page allocator. No kmalloc array is necessary since objects of different sizes can be allocated from the same page.

SLOB 将所有大于一页的请求传递给页面分配器。由于可以从同一页分配不同大小的对象,因此不需要 kmalloc 数组。

#define KMALLOC_SHIFT_HIGH      PAGE_SHIFT
#define KMALLOC_SHIFT_MAX       30

Maximum allocatable size by kmalloc() is

kmalloc() 的最大可分配大小为

#define KMALLOC_MAX_SIZE        (1UL << KMALLOC_SHIFT_MAX)

Minimum allocatable size from kmalloc() is

kmalloc() 的最小可分配大小是

#define KMALLOC_MIN_SIZE (1 << KMALLOC_SHIFT_LOW)

Default KMALLOC_SHIFT_LOWfor slab is 5, and for slub and slob it is 3.

KMALLOC_SHIFT_LOWslab 的默认值为 5,slub 和 slob 的默认值为 3。