2

What is the maximum size that we can allocate using kzalloc() in a single call?

This is a very frequently asked question. Also please let me know if i can verify that value.

Sandeep
  • 18,356
  • 16
  • 68
  • 108

3 Answers3

5

The upper limit (number of bytes that can be allocated in a single kmalloc / kzalloc request), is a function of: the processor – really, the page size – and the number of buddy system freelists (MAX_ORDER).

On both x86 and ARM, with a standard page size of 4 Kb and MAX_ORDER of 11, the kmalloc upper limit on a single call is 4 MB!

Details, including explanations and code to test this, here: http://kaiwantech.wordpress.com/2011/08/17/kmalloc-and-vmalloc-linux-kernel-memory-allocation-api-limits/

kaiwan
  • 2,114
  • 1
  • 18
  • 23
  • 1
    One caveat you don't mention: over time, the available physical memory can become "fragmented". I.e. there may be 4Mb or more free, but if there's not 4Mb of _contiguous_ free memory, kmalloc() couldn't satisfy a 4Mb allocation. Historically this was a big issue. I don't know what the current recommendations are... the most recent I've read about it was a sysadmin troubleshooting kmalloc()-or-equiv failures for 64Kb on a modern server. http://utcc.utoronto.ca/~cks/space/blog/linux/2012/06/16/ – sourcejedi Aug 24 '12 at 08:57
3

No different to kmalloc(). That's the question you should ask (or search), because kzalloc is just a thin wrapper that sets GFP_ZERO.

Up to about PAGE_SIZE (at least 4k) is no problem :p. Beyond that... you're right to say lots of people people have asked, it's definitely something you have to think about. Apparently it depends on the kernel version - there used to be a hard 128k limit, but it's been increased (or maybe dropped altogether) now. That's just the hard limit though, what you can actually get depends on a given system. (And very definitely on the kernel version).

Maybe read What is the difference between vmalloc and kmalloc?

You can always "verify" the allocation by checking the return value from kzalloc(), but by then you've probably already logged an allocation failure backtrace. Other than that, no - I don't think there's a good way to check in advance.

Community
  • 1
  • 1
sourcejedi
  • 3,051
  • 2
  • 24
  • 42
  • is there any call like kzalloc_try() in linux – Sandeep Aug 24 '12 at 10:27
  • I was going to say no. It does seem there's a GFP_NOWARN flag, if you want to avoid the backtrace. But if you want to get your code into the mainline kernel... expect to be told to go away and rewrite it. It sounds like you need to read LDD3 - Chapter 8 covers basic memory allocation. http://lwn.net/Kernel/LDD3/ Old enough that it doesn't even mention kzalloc(), but I think it would really help your understanding. (There may be more recent books available for a price). – sourcejedi Aug 24 '12 at 12:43
1

However, it depends on your kernel version and config. These limits normally locate in linux/slab.h, usually descripted as below(this example is under linux 2.6.32):

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

And you can test them with code below:

#include <linux/module.h>
#include <linux/slab.h>
int init_module()
{

    printk(KERN_INFO "KMALLOC_SHILFT_LOW:%d, KMALLOC_SHILFT_HIGH:%d, KMALLOC_MIN_SIZE:%d, KMALLOC_MAX_SIZE:%lu\n",
            KMALLOC_SHIFT_LOW,  KMALLOC_SHIFT_HIGH, KMALLOC_MIN_SIZE, KMALLOC_MAX_SIZE);
    return 0;
}

void cleanup_module()
{
    return;
}

Finally, the results under linux 2.6.32 32bits are: 3, 22, 8, 4194304, it means the min size is 8 bytes, and the max size is 4MB.

PS. you can also check the actual size of memory allocated by kmalloc, just use ksize(), i.e.

void *p = kmalloc(15, GFP_KERNEL);
printk(KERN_INFO "%u\n", ksize(p)); /* this will print "16" under my kernel */