Received: by 2002:a25:ad19:0:0:0:0:0 with SMTP id y25csp6703416ybi; Wed, 31 Jul 2019 19:53:16 -0700 (PDT) X-Google-Smtp-Source: APXvYqzntlPfvEAC9ShrH/MX5frQ3cTeHDnuWoZ6eFf/aHtkV5/atad9P5AS8QvWtxwT2qv1x8aW X-Received: by 2002:a63:fb14:: with SMTP id o20mr104161304pgh.136.1564627995841; Wed, 31 Jul 2019 19:53:15 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1564627995; cv=none; d=google.com; s=arc-20160816; b=hq1v8x2uvXzBHlUEPcAJWiREc3vap+4hFa8lmtndumJKk5bv9HXQBWNvPGCuID/YxP kBUxdm72teEYMbPpSmgdp9P7duyUm/a7bTZoB7Sk1PKl/fT9Qiyq3TdW+o6TQQ5mSVb/ 67bPluq/uS9oG4sPmHdYEtSXFxPcD4mdQXjx4BY8N56Ap+xKINf7ESdBCv+iJKoSaUt4 p5IKSwwMK1HngsFFbFHTNwWyH3LH+e5D38pzAnGJgqfjFJeo9Hc+HK1vKi9RF8N1Tpb3 D0XoLRauiIwoqrLz4NWF7SMdMhZ9VkrbLZvw2YDL+/P16KcfcdIyJfD7hhpxqNuV1FXc 5/lA== 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:dkim-filter; bh=6TJM9VDYF9EtQNeGWDC4vgS6nHe+nk5JsaYmJiVXyBI=; b=VZQbL0Rz4NT2yaoTNDzOwZefCZ5JophFZ/ZSjAPVSZDPcwe7FhjEarUQFxQDjooNVZ NWMKt13FvrKnUKiDcF3xSJxGCYwnkhjz2rXVfW/aLbZx827z9zmSH6goPIQVOFG5xP7i k/T13V7WXFY7200kAKLjvsu6R3ZsHqRC1r2vhz5CG/CZMjl64bGit+NQhuEno5iO1f/F +5q8LKJjaqI90dOwOzUIAhg4hjqc7aoAy1HdNScCqp8e74JKKlyrSkX0rKUwkn0IczBB lBllGpX5xq4PySJLtvWhkiyEDbb29XkiXVqasZhh/GPyPCV3DVZL7dDac/aW2GkT+hWK DMDg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@nifty.com header.s=dec2015msa header.b="Ip3T/FmO"; 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 t13si34024739pfh.245.2019.07.31.19.53.00; Wed, 31 Jul 2019 19:53:15 -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=@nifty.com header.s=dec2015msa header.b="Ip3T/FmO"; 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 S1727918AbfHACvW (ORCPT + 99 others); Wed, 31 Jul 2019 22:51:22 -0400 Received: from conssluserg-04.nifty.com ([210.131.2.83]:48880 "EHLO conssluserg-04.nifty.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726125AbfHACvV (ORCPT ); Wed, 31 Jul 2019 22:51:21 -0400 Received: from mail-vs1-f47.google.com (mail-vs1-f47.google.com [209.85.217.47]) (authenticated) by conssluserg-04.nifty.com with ESMTP id x712pDvX024730 for ; Thu, 1 Aug 2019 11:51:14 +0900 DKIM-Filter: OpenDKIM Filter v2.10.3 conssluserg-04.nifty.com x712pDvX024730 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=nifty.com; s=dec2015msa; t=1564627874; bh=6TJM9VDYF9EtQNeGWDC4vgS6nHe+nk5JsaYmJiVXyBI=; h=References:In-Reply-To:From:Date:Subject:To:Cc:From; b=Ip3T/FmOcMM0Xe1G9kF0kVoTwbrk8BewSMJnPxRispVgtY50AhB8uFI3rOG3M9/QM XLjmSx3URsrphsgwvgQdUY57MfeEMzr8IKvwp2zJljTdnoCYQ/FTmf/AqqA90Mq5CE Rc/shGko8O6Sozs4Pnx9H0Wd/zCHvt4HvA4Lav7j6IoMWkHTd9w80ZWJqStvYT8nhj TpiwvOOmJy8UalIPDjzobDQqg6bSLmAnMSbMQLF0SY7nAtoXj2AWy0NJXLOGP3jbaU mzSot8oistN5lDod77IzhazhuL2o27wHMpat6HsD6Xb9wHOHYk8H5pZHZ9mZIM+kHS qyumrdtP1ZZDQ== X-Nifty-SrcIP: [209.85.217.47] Received: by mail-vs1-f47.google.com with SMTP id 2so47771454vso.8 for ; Wed, 31 Jul 2019 19:51:13 -0700 (PDT) X-Gm-Message-State: APjAAAXBrmW+T8baILD1sT6AzUDhnorG+VJ4Nr3P9hgvLfqgsobVGKnj C/U9KdjE58CRiRYEHhr8BsydGg9YSrmVXhPcyAY= X-Received: by 2002:a67:d46:: with SMTP id 67mr81172763vsn.181.1564627872707; Wed, 31 Jul 2019 19:51:12 -0700 (PDT) MIME-Version: 1.0 References: <0306bec0ec270b01b09441da3200252396abed27.camel@perches.com> <20190731190309.19909-1-rikard.falkeborn@gmail.com> In-Reply-To: <20190731190309.19909-1-rikard.falkeborn@gmail.com> From: Masahiro Yamada Date: Thu, 1 Aug 2019 11:50:36 +0900 X-Gmail-Original-Message-ID: Message-ID: Subject: Re: [PATCH] linux/bits.h: Add compile time sanity check of GENMASK inputs To: Rikard Falkeborn Cc: Joe Perches , Andrew Morton , Johannes Berg , Linux Kernel Mailing 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 Thu, Aug 1, 2019 at 4:04 AM Rikard Falkeborn wrote: > > GENMASK() and GENMASK_ULL() are supposed to be called with the high bit > as the first argument and the low bit as the second argument. Mixing > them will return a mask with zero bits set. > > Recent commits show getting this wrong is not uncommon, see e.g. > commit aa4c0c9091b0 ("net: stmmac: Fix misuses of GENMASK macro") and > commit 9bdd7bb3a844 ("clocksource/drivers/npcm: Fix misuse of GENMASK > macro"). > > To prevent such mistakes from appearing again, add compile time sanity > checking to the arguments of GENMASK() and GENMASK_ULL(). If both the > arguments are known at compile time, and the low bit is higher than the > high bit, break the build to detect the mistake immediately. > > Since GENMASK() is used in declarations, BUILD_BUG_OR_ZERO() must be > used instead of BUILD_BUG_ON(), and __is_constexpr() must be used instead > of __builtin_constant_p(). > > Commit 95b980d62d52 ("linux/bits.h: make BIT(), GENMASK(), and friends > available in assembly") made the macros in linux/bits.h available in > assembly. Since neither BUILD_BUG_OR_ZERO() or __is_constexpr() are asm > compatible, disable the checks if the file is included in an asm file. > > Signed-off-by: Rikard Falkeborn > --- > Joe Perches sent a series to fix the existing misuses of GENMASK() that > needs to be merged before this to avoid build failures. Currently, 7 of > the patches were not in Linus tree, and 2 were not in linux-next. > > Also, there's currently no asm users of bits.h, but since it was made > asm-compatible just two weeks ago it would be a shame to break it right > away... > > include/linux/bits.h | 14 ++++++++++++-- > 1 file changed, 12 insertions(+), 2 deletions(-) > > diff --git a/include/linux/bits.h b/include/linux/bits.h > index 669d69441a62..73489579eef9 100644 > --- a/include/linux/bits.h > +++ b/include/linux/bits.h > @@ -18,12 +18,22 @@ > * position @h. For example > * GENMASK_ULL(39, 21) gives us the 64bit vector 0x000000ffffe00000. > */ > +#ifndef __ASSEMBLY__ > +#include > +#define GENMASK_INPUT_CHECK(h, l) BUILD_BUG_ON_ZERO(__builtin_choose_expr( \ > + __is_constexpr(h) && __is_constexpr(l), (l) > (h), 0)) > +#else > +#define GENMASK_INPUT_CHECK(h, l) 0 > +#endif > + > #define GENMASK(h, l) \ > + (GENMASK_INPUT_CHECK(h, l) + \ > (((~UL(0)) - (UL(1) << (l)) + 1) & \ > - (~UL(0) >> (BITS_PER_LONG - 1 - (h)))) > + (~UL(0) >> (BITS_PER_LONG - 1 - (h))))) > > #define GENMASK_ULL(h, l) \ > + (GENMASK_INPUT_CHECK(h, l) + \ > (((~ULL(0)) - (ULL(1) << (l)) + 1) & \ > - (~ULL(0) >> (BITS_PER_LONG_LONG - 1 - (h)))) > + (~ULL(0) >> (BITS_PER_LONG_LONG - 1 - (h))))) This is getting cluttered with so many parentheses. One way of clean-up is to rename the current macros as follows: GENMASK() -> __GENMASK() GENMASK_UL() -> __GENMASK_ULL() Then, #define GENMASK(h, l) (GENMASK_INPUT_CHECK(h, l) + __GENMASK(h, l)) #define GENMASK_ULL(h, l) (GENMASK_INPUT_CHECK(h, l) + __GENMASK_ULL(h, l)) -- Best Regards Masahiro Yamada