From: Michal Pecio <michal.pecio@gmail.com>
To: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
Cc: Niklas Neronin <niklas.neronin@linux.intel.com>,
mathias.nyman@linux.intel.com, linux-usb@vger.kernel.org
Subject: Re: [PATCH 2/7] usb: xhci: use '%pad' specifier for DMA address printing
Date: Mon, 15 Sep 2025 12:22:51 +0200 [thread overview]
Message-ID: <20250915122251.333b4db4.michal.pecio@gmail.com> (raw)
In-Reply-To: <aMe-QcrnE5hMHC5E@smile.fi.intel.com>
On Mon, 15 Sep 2025 10:20:33 +0300, Andy Shevchenko wrote:
> > > No, it's other way around, we should not put explicit casts in
> > > printf() in C as there are plenty of the format specifiers that
> > > allows us to be sure that the printed value is correct
> > > independently on architecture, endianess, etc.
> >
> > At least if you do it, the compiler will also do the right thing:
> > - if the cast doesn't match the format, warn (xhci needs a patch)
>
> Of course this doesn't work properly on the types that are less than
> int. So, this is fragile argument to support explicit castings.
The issue is how to print u64 and dma_addr_t, and the suggestion is
to stay with ("%08llx", (u64)addr) for both. What should go wrong?
1. 'addr' is transparently widened if necessary
2. if 'addr' type changes later, nothing happens
3. missing cast is a build error on common platforms (needs patch)
4. wrong format (%lx, %d, %pad, %p) is a build error
With %pad used for dma_addr_t:
1. different formats must be written manually
2a. u64 to dma_addr_t: manual edit
2b. dma_addr_t to u64: manual edit or it's a silent bug, invisible
to compilers, invisible on 64 bit platforms used by developers
That's for type safety. And further:
5. rvalues work without proliferation of temp variables
6. same number looks same, whether stored as u64 or dma_addr_t
7. consistency with the rest of the kernel
Seriously, *lots* of drivers and even the PCI subsystem itself print
addresses unpadded, using %llx or similar formats. The numbers have
8 digits on a PC (even 64 bit) and grow to 12 or more elsewhere.
It's first time I see somebody who appears really bothered by this.
> > Reminder: this drivers handles DMAs as u64 too, so it will *never*
> > print all DMAs as %pad. And if it tries, it will be a silent bug.
>
> Yes, and the problem here is not in the printf() specifiers, the
> problem is in the (used) data types.
And what else can be done? The driver uses dma_addr_t where applicable
for efficiency on 32 bits, the HW uses 64 bits like 'buffer' below:
struct xhci_transfer_event {
/* 64-bit buffer address, or immediate data */
__le64 buffer;
__le32 transfer_len;
/* This field is interpreted differently based on the type of TRB */
__le32 flags;
};
Same address may be logged at various stages of the flow where it
exists in variabes of different type. The number matters, not type.
Regards,
Michal
next prev parent reply other threads:[~2025-09-15 10:23 UTC|newest]
Thread overview: 37+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-09-03 17:01 [PATCH 0/7] usb: xhci: enhancements to address printing Niklas Neronin
2025-09-03 17:01 ` [PATCH 1/7] usb: xhci-dbgcap: correct DMA address handling Niklas Neronin
2025-09-09 10:13 ` Michal Pecio
2025-09-10 8:00 ` Neronin, Niklas
2025-09-10 8:15 ` Michal Pecio
2025-09-03 17:01 ` [PATCH 2/7] usb: xhci: use '%pad' specifier for DMA address printing Niklas Neronin
2025-09-09 9:59 ` Michal Pecio
2025-09-09 11:29 ` Andy Shevchenko
2025-09-09 20:44 ` Michal Pecio
2025-09-10 5:56 ` Michal Pecio
2025-09-11 7:41 ` Andy Shevchenko
2025-09-11 9:34 ` Michal Pecio
2025-09-11 20:13 ` Andy Shevchenko
2025-09-12 9:46 ` Michal Pecio
2025-09-12 18:02 ` Andy Shevchenko
2025-09-13 8:12 ` Michal Pecio
2025-09-15 7:20 ` Andy Shevchenko
2025-09-15 10:22 ` Michal Pecio [this message]
2025-09-15 12:32 ` Neronin, Niklas
2025-09-16 9:32 ` Michal Pecio
2025-09-16 9:36 ` Michal Pecio
2025-09-15 14:22 ` Andy Shevchenko
2025-09-10 9:04 ` Michal Pecio
2025-09-10 9:17 ` Michal Pecio
2025-09-03 17:01 ` [PATCH 3/7] usb: xhci: improve Stream Context register debugging Niklas Neronin
2025-09-09 9:23 ` Michal Pecio
2025-09-03 17:01 ` [PATCH 4/7] usb: xhci: improve Endpoint " Niklas Neronin
2025-09-09 9:20 ` Michal Pecio
2025-09-09 10:24 ` Michal Pecio
2025-09-15 12:45 ` Neronin, Niklas
2025-09-03 17:01 ` [PATCH 5/7] usb: xhci: improve Command Ring Control " Niklas Neronin
2025-09-09 9:17 ` Michal Pecio
2025-09-09 10:20 ` Michal Pecio
2025-09-03 17:01 ` [PATCH 6/7] usb: xhci: improve Event Ring Dequeue Pointer Register debugging Niklas Neronin
2025-09-03 17:01 ` [PATCH 7/7] usb: xhci: standardize address format Niklas Neronin
2025-09-09 9:06 ` Michal Pecio
2025-09-15 13:24 ` Neronin, Niklas
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20250915122251.333b4db4.michal.pecio@gmail.com \
--to=michal.pecio@gmail.com \
--cc=andriy.shevchenko@linux.intel.com \
--cc=linux-usb@vger.kernel.org \
--cc=mathias.nyman@linux.intel.com \
--cc=niklas.neronin@linux.intel.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox