Search code examples
linux-kernelprintk

How to use printk to print a physical address (aka phys_addr_t)?


I want to print real physical address that is stored in a variable of type phys_addr_t. Now I'm doing something like this:

phys_addr_t paddr;
...
paddr = virt_to_phys(some_virt_addr);
pr_info("%pa", &paddr);
...

As for documentation:

Physical addresses types ``phys_addr_t``
========================================

::

    %pa[p]  0x01234567 or 0x0123456789abcdef

For printing a ``phys_addr_t`` type (and its derivatives, such as
``resource_size_t``) which can vary based on build options, regardless of
the width of the CPU data path. Passed by reference.

but I'm a bit confused about Passed by reference, because it is already something like reference. So my actual questions are:

  • How to correctly print phys_addr_t using printk?
  • What does Passed by reference mean here?
  • What does the [p] postfix of %pa[p] mean?

If my example is not correct or inefficient, please show me how it should be.


Solution

  • You are doing it correctly:

    phys_addr_t paddr = 0x1234;
    printk(KERN_INFO "%pa\n", &paddr);
    // Result: 0x0000000000001234
    

    "Pass by reference" means to pass a pointer to the physical address you want to print (&paddr) instead of directly passing its value (paddr).

    The [p] suffix in the documentation means that the last p is optional. You can use %pa or %pap. The final p is not necessary and also doesn't change the output format (as you can see from the source here).