Re: [PATCH V3] mm: Standardize printing for pgtable entries
From: Andy Shevchenko
Date: Thu Jul 09 2026 - 06:30:35 EST
On Thu, Jul 09, 2026 at 11:15:27AM +0200, Petr Mladek wrote:
> On Thu 2026-07-09 10:40:43, Andy Shevchenko wrote:
> > On Thu, Jul 09, 2026 at 10:13:34AM +0530, Anshuman Khandual wrote:
...
> > > +static void ptval_bytes_to_hex_str(char *buf, size_t buf_size, const void *entry, size_t entry_size)
> > > +{
> > > + if (WARN_ON_ONCE(buf_size < entry_size * 2 + 1)) {
> > > + snprintf(buf, buf_size, "overflow");
> > > + return;
> > > + }
> > > +
> > > + switch (entry_size) {
> > > + case sizeof(u32):
> > > + snprintf(buf, buf_size, "%08x", *(const u32 *)entry);
> > > + break;
> > > + case sizeof(u64):
> > > + snprintf(buf, buf_size, "%016llx", *(const u64 *)entry);
> > > + break;
> > > +#if defined(__SIZEOF_INT128__)
> > > + case sizeof(u128):
> > > + snprintf(buf, buf_size, "%016llx%016llx",
> > > + (unsigned long long)(*(const u128 *)entry >> 64),
> > > + (unsigned long long)*(const u128 *)entry);
> > > + break;
> > > +#endif
> > > + default:
> > > + snprintf(buf, buf_size, "unsupported");
> > > + break;
> > > + }
> > > +}
> > > +
> > > +#define ptval_to_str(buf, val) \
> > > + do { \
> > > + auto __val = (val); \
> > > + \
> > > + ptval_bytes_to_hex_str((buf), sizeof(buf), &__val, sizeof(__val)); \
> > > + } while (0)
> > > +
> > > +#if defined(__SIZEOF_INT128__)
> > > +#define PTVAL_STR_MAX (32 + 1) /* Max 128-bit value in hex + NUL */
> > > +#else
> > > +#define PTVAL_STR_MAX (16 + 1) /* Max 64-bit value in hex + NUL */
> > > +#endif
> >
> > The above is quite duplicative with what we have in lib/vsprintf.c. Have you considered
> > using something from there instead? (Yes, it might require some functions to be wrapped
> > or dropped from static.)
>
> Honestly, I can't see any reasonable way to deduplicate the code.
>
> Most of the magic in the above macros are in the value/buffer size
> detection. I do not see anything similar in vsprintf.c. The sizes
> are handled another way there.
>
> Also the internal API used in vsprintf.c is a bit tricky because
> the buffer size is passed via *end pointer and it counts the printed
> characters even behind the *end pointer.
>
> But maybe you had something particular in mind.
Yes, I was thinking of a wrapper on top of special_hex_number(). It takes size
as an argument and hence will work even for 128-bit cases.
--
With Best Regards,
Andy Shevchenko