It seems that casting a void*
pointer (allocated by kmalloc) to unsigned long long
changes it. Printing them with %p
and %llx
gives different values. Why is it so? Can anyone explain?
Following is a simple repro for that:
#include <linux/module.h>
#include <linux/slab.h>
#include <linux/init.h>
void* kbuff;
int init_module(void)
{
kbuff = kzalloc(sizeof(char), GFP_KERNEL);
pr_info("%p %llx\n",kbuff, (unsigned long long)kbuff);
return 0;
}
void cleanup_module(void)
{
kfree(kbuff);
}
The dmesg
output comes out to be as follows
[67355.673465] 000000003aeb0247 ffff9ef657a58c00
%llx
you are passing the wrong datatype which invokes UB. This is because the format of pointers can differ much between platforms, you cannot assume, that it can be well represented by along long int
. Did you even bother to cast it? If not, casting it explicitly to(unsigned long long)
might improve your results. – Baddie