Received: by 2002:a25:c593:0:0:0:0:0 with SMTP id v141csp5764993ybe; Tue, 10 Sep 2019 08:29:45 -0700 (PDT) X-Google-Smtp-Source: APXvYqxIT2YLPr81nTnjryv2efOwyII6DhOMko7yHxJE56Lyv17zlu50a13HQa7LhYud2IBTnS0L X-Received: by 2002:a17:906:52d7:: with SMTP id w23mr25153350ejn.228.1568129385422; Tue, 10 Sep 2019 08:29:45 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1568129385; cv=none; d=google.com; s=arc-20160816; b=OyG9tl0ppcCwjpd7tbQ3qETmoWW54Yoi+dcmgFEQrghabNW3LPQ3RQ806AWDoTPTF0 gyctdM3Ub7W5avC/cbrVJ8U6MngE9sfgiDWtTs0VZGu9ToNDbnkTaXfyW0UWSWXM+oBt HfQTyas3DMBtFl65ZFTZUXj2wW5F5hBCoKcPh0EqsiW7F64taTwIn4SGEE4SsC72zvMH I/zG46waMu8CnizhvF8Diiv8NLqsLahRFs6ZTJR5X9WKUCsPJZE9wo0fc2Vf3ro9pVgJ z1Bk5xf2EIKOIkk0SrqRjXoB9NVd3POADc8/jrGOmDBwNFKItgv76PnXgRLaB7CXfeKW +q3g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:dkim-signature; bh=szCNIUW6BgbokX0FPeRCVgqZ5ZdcFTN5rqoeuNdt524=; b=HAtPn19YKfqEMx1KOVzIdaqnP1Dz3KOcBt4UrlFYsKLv9VKvShgk8a5ogNkVu4ma9o FdaMEu2vOSLq/rQE2+w75N1S+XIz28AfkhkT6Zy/YaR9ZwMZjxu0bZeZpgkNN57tReo6 NZ1VaWxPTVh8v8aExfSpGZo8CQk0JSHdFKCOSc7BV7idO0kLxn1kNI4arCA3PjDJk2SD TiinhCkYmPvJgNlVgoXCZZG7bFiDR7TX8v3Q8ah5njyn4BZEZ1DJex2Ln9L44gD5vejY WILn6lOZzr58UST7WgkY3XNlRQBRLpns3V95L0+s8Zx5BLiYbPm9MJibc50WebpmTc8c BSWQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=caR2EHx2; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id t58si11350103edd.220.2019.09.10.08.29.21; Tue, 10 Sep 2019 08:29:45 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=caR2EHx2; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1731756AbfIJP0o (ORCPT + 99 others); Tue, 10 Sep 2019 11:26:44 -0400 Received: from mail-pf1-f194.google.com ([209.85.210.194]:42056 "EHLO mail-pf1-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730821AbfIJP0o (ORCPT ); Tue, 10 Sep 2019 11:26:44 -0400 Received: by mail-pf1-f194.google.com with SMTP id w22so11719572pfi.9; Tue, 10 Sep 2019 08:26:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=szCNIUW6BgbokX0FPeRCVgqZ5ZdcFTN5rqoeuNdt524=; b=caR2EHx2Qt5HEr4rR7I7x4dW2X6VHrZZOd5QrFiwQrJcRuuNMXoYgnzTkWyFAo2FLI hIuVDkZMSQqupEAwSEVivqHBmD+UeeHLzBycMfwnzYXxLpNvOQnQjmpVF0AeIhJiGaEX C9BiYSXNaiFNjeXt5ERdvlkklW0kUyxOdN1pPlxEKI2FxI0WyoAzMVcmgMehTclWwZUS k4iPIKC58a+zGDjb0brPpC9UNBfDxITrvoCS6El+GKqRPLIkgHjzbTpEe14u9Le+86l/ nd+6LahlcilZwQyO+zDB6DQKre3yKJB4kqglYJK5ji3DbC72wx35fRtE2asafjN68Et/ Tymg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=szCNIUW6BgbokX0FPeRCVgqZ5ZdcFTN5rqoeuNdt524=; b=ucU0zRb9cbCBe9CFHh6DW4s16NhNmHFdnPRuNoZoLxJty+ET/KHdLlFAV3HQxAs1pD SP02zumpsvW+Rsp7p3D0xeM/Nc1Q7Fkbsps0RMNDZEZN4n6Kdq9Ua0/E248r0OyEbUj1 2jkzdHTQ6NqgUpFhglYZlRBow5OkGv1CVcT0v1F9rzmAyklEo1HcCjVpJCtbp7u2ejQl aHMtTuMXbFesBreca+GXqrix/hREVZ5POQjpYRjVrd1G0fMNSUZiZS3Tti14pTUW7V8K kd5a4pUFe99+oJoKqRG+Q/rkNLd9kpa+v/Ao50FguyGfWTtRO+MV2cOyaQ2a8op69sJB Xcjg== X-Gm-Message-State: APjAAAVFppRVYqzJ5AuLiT2vRRd1Ici6sZnhXCLggsL2ML2FKWm3Smhn qH8IIGBrq/sFtyEvrJZLL3nZNEd9HnE6polHPkI= X-Received: by 2002:a17:90a:b313:: with SMTP id d19mr56369pjr.132.1568129203532; Tue, 10 Sep 2019 08:26:43 -0700 (PDT) MIME-Version: 1.0 References: <20190830214655.6625-1-linux@rasmusvillemoes.dk> <20190909203826.22263-1-linux@rasmusvillemoes.dk> In-Reply-To: <20190909203826.22263-1-linux@rasmusvillemoes.dk> From: Andy Shevchenko Date: Tue, 10 Sep 2019 18:26:32 +0300 Message-ID: Subject: Re: [PATCH v2] printf: add support for printing symbolic error codes To: Rasmus Villemoes Cc: Andrew Morton , Jonathan Corbet , Joe Perches , Petr Mladek , Sergey Senozhatsky , Jani Nikula , Linux Kernel Mailing List , =?UTF-8?Q?Uwe_Kleine=2DK=C3=B6nig?= , Linux Documentation List Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Mon, Sep 9, 2019 at 11:39 PM Rasmus Villemoes 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, 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. > +#define E(err) [err + BUILD_BUG_ON_ZERO(err <= 0 || err > 300)] = #err > +#define E(err) [err - 512 + BUILD_BUG_ON_ZERO(err < 512 || err > 550)] = #err From long term prospective 300 and 550 hard coded here may be forgotten. > +const char *errcode(int err) We got long, why not to use long type for it? > +{ > + /* 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]; It won't work if one of the #ifdef:s in the array fails. Would it? > + 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"; > + return NULL; > +} > + long err = PTR_ERR(ptr); > + const char *sym = errcode(-err); Do we need additional sign change if we already have such check inside errcode()? -- With Best Regards, Andy Shevchenko