在驱动程序代码中打印指针时 %llx 和 %p 之间的区别

Difference between %llx and %p while printing a pointer inside driver code

似乎将 void* 指针(由 kmalloc 分配)转换为 unsigned long long 会改变它。使用 %p%llx 打印它们会给出不同的值。为什么会这样?谁能解释一下?

以下是一个简单的重现:

#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);
}

dmesg输出结果如下

[67355.673465] 000000003aeb0247 ffff9ef657a58c00

来自 printk() 的文档(pr_info 调用):

Pointer Types

Pointers printed without a specifier extension (i.e unadorned %p) are hashed to give a unique identifier without leaking kernel addresses to user space. On 64 bit machines the first 32 bits are zeroed. If you really want the address see %px below.

所以,这是一项安全措施。使用 %px 格式说明符打印真实地址(现在应该匹配)