Re: [PATCH] printf: add support for printing symbolic error codes

From: Uwe Kleine-KÃnig
Date: Wed Sep 04 2019 - 12:29:08 EST


On 9/4/19 6:19 PM, Andy Shevchenko wrote:
> On Sat, Aug 31, 2019 at 12:48 AM Rasmus Villemoes
> <linux@xxxxxxxxxxxxxxxxxx> wrote:
>>
>> It has been suggested several times to extend vsnprintf() to be able
>> to convert the numeric value of ENOSPC to print "ENOSPC". This is yet
>> another attempt. Rather than adding another %p extension, simply teach
>> plain %p to convert ERR_PTRs. While the primary use case is
>>
>> if (IS_ERR(foo)) {
>> pr_err("Sorry, can't do that: %p\n", foo);
>> return PTR_ERR(foo);
>> }
>>
>> it is also more helpful to get a symbolic error code (or, worst case,
>> a decimal number) in case an ERR_PTR is accidentally passed to some
>> %p<something>, rather than the (efault) that check_pointer() would
>> result in.
>>
>> With my embedded hat on, I've made it possible to remove this.
>>
>> I've tested that the #ifdeffery in errcode.c is sufficient to make
>> this compile on arm, arm64, mips, powerpc, s390, x86 - I'm sure the
>> 0day bot will tell me which ones I've missed.
>>
>> The symbols to include have been found by massaging the output of
>>
>> find arch include -iname 'errno*.h' | xargs grep -E 'define\s*E'
>>
>> In the cases where some common aliasing exists
>> (e.g. EAGAIN=EWOULDBLOCK on all platforms, EDEADLOCK=EDEADLK on most),
>> I've moved the more popular one (in terms of 'git grep -w Efoo | wc)
>> to the bottom so that one takes precedence.
>
>> +/*
>> + * Ensure these tables to not accidentally become gigantic if some
>> + * huge errno makes it in. On most architectures, the first table will
>> + * only have about 140 entries, but mips and parisc have more sparsely
>> + * allocated errnos (with EHWPOISON = 257 on parisc, and EDQUOT = 1133
>> + * on mips), so this wastes a bit of space on those - though we
>> + * special case the EDQUOT case.
>> + */
>> +#define E(err) [err + BUILD_BUG_ON_ZERO(err <= 0 || err > 300)] = #err
>
> Hmm... Perhaps better to define the upper boundary with something like
>
> #define __E_POSIX_UPPER_BOUNDARY 300 // name sucks, I know
>
>> +#define E(err) [err - 512 + BUILD_BUG_ON_ZERO(err < 512 || err > 550)] = #err
>
> Similar to 550?

I'd not add "POSIX" in the name. Given that the arrays are called
codes_0 and codes_512 I don't think using plain numbers hurts much and
choosing a good name is hard, so I suggest to keep the explicit numbers.

>> +const char *errcode(int err)
>> +{
>> + /* Might as well accept both -EIO and EIO. */
>> + if (err < 0)
>> + err = -err;
>> + if (err <= 0) /* INT_MIN or 0 */
>> + return NULL;
>> + if (err < ARRAY_SIZE(codes_0))
>> + return codes_0[err];
>> + if (err >= 512 && err - 512 < ARRAY_SIZE(codes_512))
>> + return codes_512[err - 512];
>> + /* But why? */
>> + if (IS_ENABLED(CONFIG_MIPS) && err == EDQUOT) /* 1133 */
>> + return "EDQUOT";
>
> Another possibility is to initialize the errors at run time with radix tree.

The idea was to save space. But when using a radix tree this has
overhead compared to the lists here, and you still need a map for
error-code -> error-name to initialize the radix tree. Also a lookup is
slower than with the idea implemented here. So it's bigger, slower and
more complicated ... I don't think we should do that.

>
>> + return NULL;
>> +}
>
>> @@ -2111,6 +2112,31 @@ static noinline_for_stack
>> char *pointer(const char *fmt, char *buf, char *end, void *ptr,
>> struct printf_spec spec)
>> {
>> + /* %px means the user explicitly wanted the pointer formatted as a hex value. */
>> + if (*fmt == 'x')
>> + return pointer_string(buf, end, ptr, spec);
>
> But instead of breaking switch case apart can we use...
>
>> +
>> + /* If it's an ERR_PTR, try to print its symbolic representation. */
>> + if (IS_ERR(ptr)) {
>
> ... if (IS_ERR() && *fmt != 'x') {
> here?

I don't feel strong here, works either way for me.

Best regards
Uwe

Attachment: signature.asc
Description: OpenPGP digital signature