Received: by 2002:a25:4158:0:0:0:0:0 with SMTP id o85csp3695816yba; Tue, 9 Apr 2019 02:51:49 -0700 (PDT) X-Google-Smtp-Source: APXvYqwfgPO0eBW9G55nkfQH6jnSPEJiWncmLZ7d5pbXXNyzckaa+2syG75Hkx6rZ+YlktloJ9g+ X-Received: by 2002:a63:3dc8:: with SMTP id k191mr33464186pga.286.1554803509470; Tue, 09 Apr 2019 02:51:49 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1554803509; cv=none; d=google.com; s=arc-20160816; b=j1cyJY5+EEPSe/3nS9Mb17bVP1FTIrfpq4RrmjMV2423hzg8bOK4Zp5ieKYc1H+g0n +H2vHTzS3r7FkkBS3UPn7ufnc8Mylj38FxkqAsa3tBBriWu7mBnE+DskVfTPg02kpHap 0/rKtBWPJql2TWuT8tlFmPwr8opgMagyjYiqWv6xJprI9Xsiblzb1CD5L3ejTnhourhO zhp8DlGIf/LM3GJFxc5SYk3pb5VyJt0tN3jX0OP2xbDvEC5sRaffqo8382Re1gPn9buC YM97c+toIJi2GiYAMwKjY0EP9qqY9Lc7xuagMcXMp1d/AEpK8HBHw8asxAvw63qfPbc9 KugA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-disposition:mime-version:references:message-id:subject:cc :to:from:date; bh=h2R/C5k5maPmLO4Csuy7HtgpJCbVPtSidugb+iEACSw=; b=VFrMp3r+XYRS6rgm8C+i5c0LimfeDkOMJuZ5TkU45xwt3OWnS5hVnRGNStAeY0jRvz 8mzizlRF27oluuskusdLlklg4lMxwUjri0gq9gcvMHETO/g5sLh15AtrP02oqUcGviE3 fiWEntkuznTW3Gd35jL29ZL0Pd50thA+eG8TIWK06ELIX4pJO9UX/ov3opdevxSUYcvC 6vut10CwlFycfVy9zzunSdq7R64qVAq9cSEOlGwuGez8mm3XSB3iSy2/PchgA/4zScr4 X4uUbBOrwxywtzu+Vd8yvt1RA1qhchd0sOZXjmppnnSyW6MTF0jDm5mOlhvGvhwvIFUY s7mg== ARC-Authentication-Results: i=1; mx.google.com; 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 a13si17293104pgg.365.2019.04.09.02.51.33; Tue, 09 Apr 2019 02:51:49 -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; 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 S1726513AbfDIJuw (ORCPT + 99 others); Tue, 9 Apr 2019 05:50:52 -0400 Received: from atrey.karlin.mff.cuni.cz ([195.113.26.193]:58698 "EHLO atrey.karlin.mff.cuni.cz" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726079AbfDIJuw (ORCPT ); Tue, 9 Apr 2019 05:50:52 -0400 Received: by atrey.karlin.mff.cuni.cz (Postfix, from userid 512) id 55CAF80839; Tue, 9 Apr 2019 11:50:41 +0200 (CEST) Date: Tue, 9 Apr 2019 11:50:41 +0200 From: Pavel Machek To: Andrew Morton Cc: Vadim Pasternak , jacek.anaszewski@gmail.com, linux-kernel@vger.kernel.org, linux-leds@vger.kernel.org, idosch@mellanox.com, Andrey Ryabinin Subject: Re: [PATCH v1 bitops] bitops: Fix UBSAN undefined behavior warning for rotation right Message-ID: <20190409095041.GA32344@atrey.karlin.mff.cuni.cz> References: <20190407125325.24440-1-vadimp@mellanox.com> <20190408155217.3f723554ae7fbcb34eeacb30@linux-foundation.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20190408155217.3f723554ae7fbcb34eeacb30@linux-foundation.org> User-Agent: Mutt/1.5.21 (2010-09-15) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Hi! > (resend, cc Andrey) > > On Sun, 7 Apr 2019 12:53:25 +0000 Vadim Pasternak wrote: > > > The warning is caused by call to rorXX(), if the second parameters of > > this function "shift" is zero. In such case UBSAN reports the warning > > for the next expression: (word << (XX - shift), where XX is > > 64, 32, 16, 8 for respectively ror64, ror32, ror16, ror8. > > Fix adds validation of this parameter - in case it's equal zero, no > > need to rotate, just original "word" is to be returned to caller. > > > > The UBSAN undefined behavior warning has been reported for call to > > ror32(): > > [ 11.426543] UBSAN: Undefined behaviour in ./include/linux/bitops.h:93:33 > > [ 11.434045] shift exponent 32 is too large for 32-bit type 'unsigned int' > > hm, do we care? > > > ... > > > > > --- a/include/linux/bitops.h > > +++ b/include/linux/bitops.h > > @@ -70,6 +70,9 @@ static inline __u64 rol64(__u64 word, unsigned int shift) > > */ > > static inline __u64 ror64(__u64 word, unsigned int shift) > > { > > + if (!shift) > > + return word; > > + > > return (word >> shift) | (word << (64 - shift)); > > } > > Is there any known architecture or compiler for which UL<<64 doesn't > reliably produce zero? Is there any prospect that this will become a > problem in the future? Compiler is free to assume that shift !=0 after running ror64()... and use that fact in optimalizations. so... if it is not problem today it may easily become problem tommorow. Pavel -- (english) http://www.livejournal.com/~pavelmachek (cesky, pictures) http://atrey.karlin.mff.cuni.cz/~pavel/picture/horses/blog.html