Received: by 2002:ac0:aed5:0:0:0:0:0 with SMTP id t21csp4604716imb; Wed, 6 Mar 2019 18:15:10 -0800 (PST) X-Google-Smtp-Source: APXvYqzvAPF8qwMh11gLdeoGTlWLtx1TuJpy0K0f/1IAsaER+5cqUTju53UVpN6vd5ruRoZ3AOYa X-Received: by 2002:a65:5142:: with SMTP id g2mr9219868pgq.149.1551924910414; Wed, 06 Mar 2019 18:15:10 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1551924910; cv=none; d=google.com; s=arc-20160816; b=G+WbF0bzCSYWhqfAYne8/x7DvQdokR+/f5TzmkQD8DdCQLoz+ZT3Al5ACJJsuQ79fF WT8UKluFeRt7VdwRmjFh30qms6fK/DwWGIO6ssJNKfGPfIVi9yjB2XijEnq3ctawSxyF VFrVmumRU/6NO2ElvsWLotdFXybZue2S/H3QIF5B6aDyUQfGBMgYwwrdcOiwAaCOw0cx 5bQp6wsLr6JMvxB2G2qNPpTyTRrq9H8lNvwH8Qakpny2U7gEtK5uk95gKtHki/lPZlSC 6BRKm9HdTKg0Yt2wFpDdHGEqYqqEjgvU52rnriIiLuBKPKMclKZfhkPaKRNY+zGa0dIZ lfcw== 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 :content-language:in-reply-to:mime-version:user-agent:date :message-id:from:references:cc:to:subject; bh=dFcQUHAwUz1kBG0aYRd/AwmoFOVz0y0c3AHqYJXM+X4=; b=ORTcAacTpvzlXeUQRVwxv2LvHaE1tTgMvXjXEaEhhg7jBmpGH25ThbbogPzXK01fZF S0CQMit+jROjsf42NhEnEbvaXFW4kWrpchy0a8q/Lb/VRPzfDFhyVooZZZ2QnMgIurYg qJjrHoiSpgWrh3AnxGaEdmKN+ogneqUtX8bn08XrW/djDYBmDiDcF7DnZ4kEKjX6iVU5 ValgkbOZkDg/qoIGvbeFitvdJrg9UYMZLgwhL9XHoWc2/wrZfu5EjoUVkw+3Pa3Ooxpw 6tkPThEhO9vi1/hszVr+7SnldbYAUgsYgd4vE3mawkkF0WKzGreWH3lwlsM+hmjIZi6K WV3g== 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 u7si2752908plz.379.2019.03.06.18.14.54; Wed, 06 Mar 2019 18:15:10 -0800 (PST) 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 S1726713AbfCGCON (ORCPT + 99 others); Wed, 6 Mar 2019 21:14:13 -0500 Received: from mail-pf1-f194.google.com ([209.85.210.194]:41006 "EHLO mail-pf1-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726121AbfCGCOM (ORCPT ); Wed, 6 Mar 2019 21:14:12 -0500 Received: by mail-pf1-f194.google.com with SMTP id d25so10150525pfn.8; Wed, 06 Mar 2019 18:14:12 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:cc:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-language :content-transfer-encoding; bh=dFcQUHAwUz1kBG0aYRd/AwmoFOVz0y0c3AHqYJXM+X4=; b=qgVBu12VWieebVYquSnin26l7es/R4xi8rIM8Pjkp8ZOjLsDj3ODy5Qs1BLvN9iWAf ggGxfvQ4z65iN9ZFNcwwr2t0+fEB9M393/Mdg/xoDiUao11cXyfYww/rimWKl2BQrljc ZFp4vrYMdzp0KuEkhr9hZDiWcbd7sYE/visUxAY/7GAKbIA03DsX34fi133tnlNKcxPn eAXZSYbniFikeYaLuCuGQv4pbol4OPJr6VWXtux7rn13QehJum1/tnUDhO34J7LwpTOI 7OKuyoQDWQtUaODJezsLfYYgtfasBrEhWEEmbOchzZiW3EqX7369f6SC455tiwcVdbwB VyVA== X-Gm-Message-State: APjAAAXFDbubr55tY1E7OO73IGVA2wAM/Fj9zXk8s6ntFWZ/Tx1g9Ab5 xlADz47Q9G66TvlLfrwLkOQ= X-Received: by 2002:a63:8d42:: with SMTP id z63mr9277447pgd.414.1551924851199; Wed, 06 Mar 2019 18:14:11 -0800 (PST) Received: from asus.site ([2601:647:4000:5dd1:a41e:80b4:deb3:fb66]) by smtp.gmail.com with ESMTPSA id p5sm4663841pfn.154.2019.03.06.18.14.10 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 06 Mar 2019 18:14:10 -0800 (PST) Subject: Re: [PATCH] Avoid that check_shl_overflow() triggers a compiler warning when building with W=1 To: Jason Gunthorpe Cc: Kees Cook , "linux-kernel@vger.kernel.org" , "linux-rdma@vger.kernel.org" , Leon Romanovsky , Rasmus Villemoes References: <20190307010153.81157-1-bvanassche@acm.org> <20190307012417.GU1758@mellanox.com> From: Bart Van Assche Message-ID: <8a5bd9ae-ebfe-687c-2868-d0f2a610d1e0@acm.org> Date: Wed, 6 Mar 2019 18:14:09 -0800 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.5.1 MIME-Version: 1.0 In-Reply-To: <20190307012417.GU1758@mellanox.com> Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 3/6/19 5:24 PM, Jason Gunthorpe wrote: > On Wed, Mar 06, 2019 at 05:01:53PM -0800, Bart Van Assche wrote: >> This patch avoids that the following warning is reported when building >> the mlx5 driver with W=1: >> >> drivers/infiniband/hw/mlx5/qp.c: In function set_user_rq_size: >> ./include/linux/overflow.h:230:6: warning: comparison of unsigned expression >= 0 is always true [-Wtype-limits] >> _s >= 0 && _s < 8 * sizeof(*d) ? _s : 0; \ >> ^ >> drivers/infiniband/hw/mlx5/qp.c:5820:6: note: in expansion of macro check_shl_overflow >> if (check_shl_overflow(rwq->wqe_count, rwq->wqe_shift, &rwq->buf_size)) >> ^~~~~~~~~~~~~~~~~~ >> >> Cc: Jason Gunthorpe >> Cc: Leon Romanovsky >> Cc: Rasmus Villemoes >> Fixes: 0c66847793d1 ("overflow.h: Add arithmetic shift helper") # v4.19 >> Signed-off-by: Bart Van Assche >> include/linux/overflow.h | 22 ++++++++++++++++++++-- >> 1 file changed, 20 insertions(+), 2 deletions(-) >> >> diff --git a/include/linux/overflow.h b/include/linux/overflow.h >> index 40b48e2133cb..8afe0c0ada6f 100644 >> +++ b/include/linux/overflow.h >> @@ -202,6 +202,24 @@ >> >> #endif /* COMPILER_HAS_GENERIC_BUILTIN_OVERFLOW */ >> >> +/* >> + * Evaluate a >= 0 without triggering a compiler warning if the type of a >> + * is an unsigned type. >> + */ >> +#define is_positive(a) ({ \ >> + typeof(a) _minus_one = -1LL; \ >> + typeof((a) + 0U) _sign_mask = _minus_one > 0 ? 0 : \ > > This is probably just is_signed_type(a) Hi Jason, I don't think that gcc accepts something like is_signed_type(typeof(a)) so I'm not sure that the is_signed_type() macro is useful in this context. >> + 1ULL << (8 * sizeof(a) - 1); \ >> + \ >> + ((a) & _sign_mask) == 0; \ > > This is the same sort of obfuscation that Leon was building, do you > think the & is better than his ==, > version? > > Will gcc shortcircuit the warning if we write it as > > (is_signed_type(a) && a < 0) > > ? I have tested this patch. With this patch applied no warnings are reported while building the mlx5 driver and the tests in lib/test_overflow.c pass. Thanks, Bart.