0

Apart from clean way to write code, is there any other benefit (specially from performance point of view) to use kzalloc() / kmem_cache_zalloc() as compare to kmalloc() / kmem_cache_alloc() + memset()?

I tried to measure their performance, but results are almost same.

Kernel version: 4.18.0-372.19.1.el8_6.x86_64

Below code is creating 2 million slab objects each of 4096 bytes:

#define DATA_NUMBER    2000000
char *data[DATA_NUMBER];

/* kthread */
static int zero_slab(void *arg)
{
        unsigned int            counter, i;
        struct kmem_cache       *mp_cache;
        unsigned long           before, after;

        mp_cache = kmem_cache_create("MP_CACHE", PAGE_SIZE, 0, 0, NULL);
        if (!mp_cache) {
                printk(KERN_INFO "Failed to create slab.\n");
                return -ENOMEM;
        }
        printk(KERN_INFO "Slab MP_CACHE is created.\n");

        before = ktime_get();
        for (counter = 0; counter < DATA_NUMBER; counter++) {
                // data[counter] = kmem_cache_zalloc(mp_cache, GFP_KERNEL);
                data[counter] = kmem_cache_alloc(mp_cache, GFP_KERNEL);
                if (!data[counter])
                        goto err_out;
                memset(data[counter], 0, PAGE_SIZE);
        }
        after = ktime_get() - before;
        printk(KERN_INFO "Time taken in ns (%lu)\n", after);

err_out:
        printk(KERN_INFO "Total objects : %u\n", counter);
        for (i = 0; i < counter; ++i)
                kmem_cache_free(mp_cache, data[i]);
        kmem_cache_destroy(mp_cache);
        return 0;
}
0andriy
  • 4,183
  • 1
  • 24
  • 37
MankPan
  • 79
  • 6

1 Answers1

0

Apart from clean way to write code, is there any other benefit

Nope. That's it. kzalloc is simply a wrapper around kmalloc that passes __GFP_ZERO. That flag tells kmalloc to do memset for you before returning. There is no performance difference between kmalloc(x, GFP_KERNEL) + memset() and kmalloc(x, GFP_KERNEL|__GFP_ZERO).

Marco Bonelli
  • 63,369
  • 21
  • 118
  • 128
  • 1
    I would add that if zeroed memory is needed it's better to use **z** variant of allocation to avoid open coding existing APIs. – 0andriy May 21 '23 at 14:22