From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753395AbcBJPQ4 (ORCPT ); Wed, 10 Feb 2016 10:16:56 -0500 Received: from bh-25.webhostbox.net ([208.91.199.152]:32864 "EHLO bh-25.webhostbox.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751633AbcBJPQw (ORCPT ); Wed, 10 Feb 2016 10:16:52 -0500 Date: Wed, 10 Feb 2016 07:16:47 -0800 From: Guenter Roeck To: Rasmus Villemoes Cc: Andrzej Hajda , Andrew Morton , Bartlomiej Zolnierkiewicz , Marek Szyprowski , open list Subject: Re: [PATCH v3] err.h: allow IS_ERR_VALUE to handle properly more types Message-ID: <20160210151647.GA23928@roeck-us.net> References: <20160202163350.f7d42f4b97f48756f3900e9a@linux-foundation.org> <1454505328-26019-1-git-send-email-a.hajda@samsung.com> <877fiknj37.fsf@rasmusvillemoes.dk> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <877fiknj37.fsf@rasmusvillemoes.dk> User-Agent: Mutt/1.5.23 (2014-03-12) X-Authenticated_sender: guenter@roeck-us.net X-OutGoing-Spam-Status: No, score=-1.0 X-AntiAbuse: This header was added to track abuse, please include it with any abuse report X-AntiAbuse: Primary Hostname - bh-25.webhostbox.net X-AntiAbuse: Original Domain - vger.kernel.org X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12] X-AntiAbuse: Sender Address Domain - roeck-us.net X-Get-Message-Sender-Via: bh-25.webhostbox.net: authenticated_id: guenter@roeck-us.net X-Authenticated-Sender: bh-25.webhostbox.net: guenter@roeck-us.net X-Source: X-Source-Args: X-Source-Dir: Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, Feb 05, 2016 at 12:37:00AM +0100, Rasmus Villemoes wrote: > On Wed, Feb 03 2016, Andrzej Hajda wrote: > > > Current implementation of IS_ERR_VALUE works correctly only with > > following types: > > - unsigned long, > > - short, int, long. > > Other types are handled incorrectly either on 32-bit either on 64-bit > > either on both architectures. > > The patch fixes it by comparing argument with MAX_ERRNO casted > > to argument's type for unsigned types and comparing with zero for signed > > types. As a result all integer types bigger than char are handled properly. > > > > > > Signed-off-by: Andrzej Hajda > > --- > > v3: > > - use '<= -1' instead of '< 0' to silence verbose warnings for gcc > > older than 4.8, > > v2: > > - use '<= 0' instead of '< 0' to silence gcc verbose warnings, > > - expand commit message. > > --- > > include/linux/err.h | 4 +++- > > 1 file changed, 3 insertions(+), 1 deletion(-) > > > > diff --git a/include/linux/err.h b/include/linux/err.h > > index 56762ab..b7d4a9f 100644 > > --- a/include/linux/err.h > > +++ b/include/linux/err.h > > @@ -18,7 +18,9 @@ > > > > #ifndef __ASSEMBLY__ > > > > -#define IS_ERR_VALUE(x) unlikely((x) >= (unsigned long)-MAX_ERRNO) > > +#define IS_ERR_VALUE(x) ((typeof(x))(-1) <= 0 \ > > + ? unlikely((x) <= -1) \ > > + : unlikely((x) >= (typeof(x))-MAX_ERRNO)) > > > > I'm a bit worried that you consider any negative value an error when x > is signed - at least that's a change which deserves some comment why > that's ok. For example, I could imagine someone using e.g. INT_MIN as a > sentinel return value meaning 'not an error, but something special > still'. > Theoretically maybe, but I think that is quite unlikely in the real world. It turns out that if (-22 >= (unsigned long)-MAX_ERRNO) printf("This is odd\n"); actually does print "This is odd" (because -22 is promoted to unsigned long). Instead of relying on such behavior, I think it would be better to convert uses of IS_ERR_VALUE() on integer values to direct comparisons. A coccinelle script to do that conversion that is already available for pm functions (scripts/coccinelle/api/pm_runtime.cocci). Such a conversion would make the code easier to read, and reduce code size instead of (at least potentially) increasing it. Guenter