Received: by 2002:a25:8b12:0:0:0:0:0 with SMTP id i18csp2406710ybl; Sat, 24 Aug 2019 17:02:51 -0700 (PDT) X-Google-Smtp-Source: APXvYqydNubYst4cK55p/JYYtvhZOYsPX+axvBfxiHD1lDELsu8aE2o5rPJSpseDyrGZTqqD3QfO X-Received: by 2002:a63:e901:: with SMTP id i1mr9797522pgh.451.1566691371400; Sat, 24 Aug 2019 17:02:51 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1566691371; cv=none; d=google.com; s=arc-20160816; b=G+TMis/U29Mhb/9msoLofV5Ya6SymOfEnnDgGi3nCC4jn2pU9Dpd5j7xDYDRuQSvWK /9+jAvwdrjgp5Vjserl83PK+jXoPi42vZXG8HCJHqQum9eaPr/79xkw17+WMRoV6zWjC nictD2ld0buQxotasiJpK0C1HsGe0ZDwHZ++rRWz0qwffnbiTqgbrNCKtteZbxPYpHUF RSq6SfsfUQW+1/MWvuz1hHMefmSdf3gspYpVchfV9hOI2I61Ew2vQIaPQ+Nrqu7rmkE6 H2ngU0f7BBQYHBNoYlVN/WlHJ8XaJgQJcqYTRxIxl2fRjyFk0sXoQYZHRrWx/aSML0Zm Vbhw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:subject:cc:to:from:date :dkim-signature; bh=f7D1zXyrkjM5W/1Wo266VDkLB2v5lp38s9Je5pb+yYg=; b=UwGRexKkfVaNGt3D5NKUrLZxbgde6EyoRK/tMKOFYF+baDeZHjgME2SlP/S8wXlixp nZUwiiEJXv+Rh52M/ujnqfRoBMeWk7woz0ntYZU6xaboGiYhqRP3KAAnUuI7evSO2sfo 9YGJ8nai3EKau6/IkC2IgXuSmQJpM+H2mq33ghrNLjd8/qC0WuVE5SJ2j3SK0Aeg9url 4sAakGEPhaa8caiO29AqW+QGxkMJZaE3ZnHwBumYrrJJ8DJt/f7E7K0ZCZlLFsOFliGA 1KLy0XFRGPJnzaRu4hxcRww28gq7mkubDXlKRaMwTwYoKXOJKkDOJRmzoVsQRL8ihM1p ZcXw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=VLmmmAQY; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id e37si5336530pge.343.2019.08.24.17.01.47; Sat, 24 Aug 2019 17:02:51 -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=@kernel.org header.s=default header.b=VLmmmAQY; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728320AbfHXX6b (ORCPT + 99 others); Sat, 24 Aug 2019 19:58:31 -0400 Received: from mail.kernel.org ([198.145.29.99]:33202 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727708AbfHXX6b (ORCPT ); Sat, 24 Aug 2019 19:58:31 -0400 Received: from localhost.localdomain (c-73-231-172-41.hsd1.ca.comcast.net [73.231.172.41]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 952CB2146E; Sat, 24 Aug 2019 23:58:29 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1566691110; bh=M1/H4AHqbV1FG5SmdVNCBOq8Z72AscE8HXdy4tYQpkQ=; h=Date:From:To:Cc:Subject:In-Reply-To:References:From; b=VLmmmAQYvM86BVLQ3rx0sIRgOgK8wmNWAuTRLEvk0FKsiaGPjojwAz/OBewTTd3yU g+sz34F3MFg7Om9mm40XY7ji/6KHpCcL1CFvgw7typaRwp66ac+yKY3YA0OsvC2xDN rAXVTmMYQoTcLL71fZknTaaZVJb1irAy0cC3wGuc= Date: Sat, 24 Aug 2019 16:58:29 -0700 From: Andrew Morton To: Uwe =?ISO-8859-1?Q?Kleine-K=F6nig?= Cc: Jonathan Corbet , linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-gpio@vger.kernel.org, Linus Walleij , Bartosz Golaszewski , Petr Mladek , Sergey Senozhatsky , Steven Rostedt Subject: Re: [PATCH v1 1/2] vsprintf: introduce %dE for error constants Message-Id: <20190824165829.7d330367992c62dab87f6652@linux-foundation.org> In-Reply-To: <20190824233724.1775-1-uwe@kleine-koenig.org> References: <20190824233724.1775-1-uwe@kleine-koenig.org> X-Mailer: Sylpheed 3.5.1 (GTK+ 2.24.31; x86_64-pc-linux-gnu) Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org (cc printk maintainers). On Sun, 25 Aug 2019 01:37:23 +0200 Uwe Kleine-K=F6nig wrote: > pr_info("probing failed (%dE)\n", ret); >=20 > expands to >=20 > probing failed (EIO) >=20 > if ret holds -EIO (or EIO). This introduces an array of error codes. If > the error code is missing, %dE falls back to %d and so prints the plain > number. Huh. I'm surprised we don't already have this. Seems that this will be applicable in a lot of places? Although we shouldn't go blindly converting everything in sight - that would risk breaking userspace which parses kernel strings. Is it really necessary to handle the positive errnos? Does much kernel code actually do that (apart from kernel code which is buggy)? > Signed-off-by: Uwe Kleine-K=F6nig > --- > Hello >=20 > there are many code sites that benefit from this. Just grep for > "(%d)" ... Yup. This observation shouldn't be below the "^---$" ;) An approximate grep|wc would be interesting. > --- a/lib/vsprintf.c > +++ b/lib/vsprintf.c > @@ -533,6 +533,192 @@ char *number(char *buf, char *end, unsigned long lo= ng num, > return buf; > } > =20 > +#define ERRORCODE(x) { .str =3D #x, .err =3D x } > + > +static const struct { > + const char *str; > + int err; > +} errorcodes[] =3D { It's a bit of a hack, but an array of char*'s and a separate array of ushorts would save a bit of space. > + ERRORCODE(EPERM), > + ERRORCODE(ENOENT), > + ERRORCODE(ESRCH), > > ... > > +static noinline_for_stack Why this? I'm suspecting this will actually increase stack use? > +char *errstr(char *buf, char *end, unsigned long long num, > + struct printf_spec spec) > +{ > + char *errname =3D NULL; > + size_t errnamelen, copy; > + int i; > + > + for (i =3D 0; i < ARRAY_SIZE(errorcodes); ++i) { > + if (num =3D=3D errorcodes[i].err || num =3D=3D -errorcodes[i].err) { > + errname =3D errorcodes[i].str; > + break; > + } > + } > + > + if (!errname) { > + /* fall back to ordinary number */ > + return number(buf, end, num, spec); > + } > + > + copy =3D errnamelen =3D strlen(errname); > + if (copy > end - buf) > + copy =3D end - buf; > + buf =3D memcpy(buf, errname, copy); > + > + return buf + errnamelen; > +} OK, I guess `errstr' is an OK name for a static function and we can use this to add a new strerror() should the need arise. > > ... >