Okay, now, let's extend our previous kernel module (ch8/slab3_maxsize) to ch8/slab4_actualsize. Here, we will perform the same loop, allocating memory with kmalloc() and freeing it as before, but this time, we will also document the actual amount of memory allocated to us in each loop iteration by the slab layer, by invoking the ksize() API:
// ch8/slab4_actualsize/slab4_actualsize.c
static int test_maxallocsz(void)
{
size_t size2alloc = 100, actual_alloced;
void *p;
pr_info("kmalloc( n) : Actual : Wastage : Waste %%\n");
while (1) {
p = kmalloc(size2alloc, GFP_KERNEL);
if (!p) {
pr_alert("kmalloc fail, size2alloc=%zu\n", size2alloc);
return -ENOMEM;
}
actual_alloced = ksize(p);
/* Print the size2alloc, the amount actually allocated,
* the delta between the two, and the percentage of waste
...