Received: by 2002:ac0:e34a:0:0:0:0:0 with SMTP id g10csp478805imn; Wed, 27 Jul 2022 11:20:05 -0700 (PDT) X-Google-Smtp-Source: AGRyM1vm2zyJBuhi8BFeYvjzKnL4T3rNG1GOJcB8O+IB9Fv8U6PJ/VanyuSuPiDwnKEPZUns/q/f X-Received: by 2002:a17:902:bf06:b0:16d:1fd7:4d3c with SMTP id bi6-20020a170902bf0600b0016d1fd74d3cmr22618773plb.68.1658946005482; Wed, 27 Jul 2022 11:20:05 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1658946005; cv=none; d=google.com; s=arc-20160816; b=c7j0DjgsWyyWXZxNO7nO48fshrcMYeeZpKl/y0N8QImyd9X7l3JFfreeOylnirCj4D O3Nc3Hos3jpF7E/0VwbT3t+ns+2RDB8LCiMDklu5ZiFBZGXQrmRIbWvJoqodAwbr6eVn VWgyBHvnEefoqmpJ8xQpwppx/CEdtZjR16UH/lHTWQ+1jOAo5eQG0+477YEnq0NSPSj/ qv7Q03fTUo9Hdc1P3Mi6kvLnnZqGNaUc9pc9sLPepo4+BCzUsJlx779UgTfJGFKweeUZ ljFDGeaC/2O/rEMIoyUO2P07Tv3ALi9Wn5XSdjcl7eU9dW0Oj0/pENOLv0AgAKTDZTnH /Nyw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=Ep3/XjSSgMzJyW6Eakmts2e/brwZd4LFB2JXndA9E8o=; b=D5ARpalVUqOJge2pjj2Uwi2AE58YqOvNpvslb/h4pgG/v/cc4OlpnU+J5P1iagT1AU hULi1T/aauE1zoJYmWNTSmBsxOFfxDi9m6ZYoPHOtof3QzrTl10cpctWLnvguNRu4eA2 eiv1btKjVVeNaLyXCVeKHaT4WBf9ViwdL9fCIzT8mk+ibjFHpZ+8lRrQen3Mpv6j9lgk zi3+85Ot4iq3g+y1ywSzlEdB6cdjlpYT4ot19BW3gNk3F3z1gfQafFP9nEUOO8h09yeq sKUbhMllJh95nVWF6xz9zdHUUvAWno2ctEmrebEKeDohd3NfnH3bn9q6pMLbfXBpM2tc 5wSQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=ZlCSIoUw; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id l34-20020a635722000000b0041a5c88e689si21007144pgb.188.2022.07.27.11.19.50; Wed, 27 Jul 2022 11:20:05 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=ZlCSIoUw; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240258AbiG0QpM (ORCPT + 99 others); Wed, 27 Jul 2022 12:45:12 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44908 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240193AbiG0QoK (ORCPT ); Wed, 27 Jul 2022 12:44:10 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id EA6E75D0FD; Wed, 27 Jul 2022 09:30:48 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id 7AC89B821BD; Wed, 27 Jul 2022 16:30:47 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id E15E5C433C1; Wed, 27 Jul 2022 16:30:45 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1658939446; bh=jmwZE7fZT0yS5jlppOJsyv5VuU2LGt3H7AUTFK/9IZg=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=ZlCSIoUwyBnPKnbBwSHOAJNmqMREvpTQFTMrFy7Ep3tFS+o4gLLcnhfXYewB2Gu6Z 3U5NYyTjkkwAPN4dgOfJmZygKOf46GaPQjklNcLFon4CG00TeRee2eYQXbayNe7hn1 uj8jFZf5YgPcg9V5/LtFZPPUeV6OZ7OMEQ0z6uoE= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, "Peter Zijlstra (Intel)" , Josh Poimboeuf , Sasha Levin Subject: [PATCH 5.4 72/87] bitfield.h: Fix "type of reg too small for mask" test Date: Wed, 27 Jul 2022 18:11:05 +0200 Message-Id: <20220727161011.987192720@linuxfoundation.org> X-Mailer: git-send-email 2.37.1 In-Reply-To: <20220727161008.993711844@linuxfoundation.org> References: <20220727161008.993711844@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-7.7 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Peter Zijlstra [ Upstream commit bff8c3848e071d387d8b0784dc91fa49cd563774 ] The test: 'mask > (typeof(_reg))~0ull' only works correctly when both sides are unsigned, consider: - 0xff000000 vs (int)~0ull - 0x000000ff vs (int)~0ull Signed-off-by: Peter Zijlstra (Intel) Reviewed-by: Josh Poimboeuf Link: https://lore.kernel.org/r/20211110101324.950210584@infradead.org Signed-off-by: Sasha Levin --- include/linux/bitfield.h | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/include/linux/bitfield.h b/include/linux/bitfield.h index 4c0224ff0a14..4f1c0f8e1bb0 100644 --- a/include/linux/bitfield.h +++ b/include/linux/bitfield.h @@ -41,6 +41,22 @@ #define __bf_shf(x) (__builtin_ffsll(x) - 1) +#define __scalar_type_to_unsigned_cases(type) \ + unsigned type: (unsigned type)0, \ + signed type: (unsigned type)0 + +#define __unsigned_scalar_typeof(x) typeof( \ + _Generic((x), \ + char: (unsigned char)0, \ + __scalar_type_to_unsigned_cases(char), \ + __scalar_type_to_unsigned_cases(short), \ + __scalar_type_to_unsigned_cases(int), \ + __scalar_type_to_unsigned_cases(long), \ + __scalar_type_to_unsigned_cases(long long), \ + default: (x))) + +#define __bf_cast_unsigned(type, x) ((__unsigned_scalar_typeof(type))(x)) + #define __BF_FIELD_CHECK(_mask, _reg, _val, _pfx) \ ({ \ BUILD_BUG_ON_MSG(!__builtin_constant_p(_mask), \ @@ -49,7 +65,8 @@ BUILD_BUG_ON_MSG(__builtin_constant_p(_val) ? \ ~((_mask) >> __bf_shf(_mask)) & (_val) : 0, \ _pfx "value too large for the field"); \ - BUILD_BUG_ON_MSG((_mask) > (typeof(_reg))~0ull, \ + BUILD_BUG_ON_MSG(__bf_cast_unsigned(_mask, _mask) > \ + __bf_cast_unsigned(_reg, ~0ull), \ _pfx "type of reg too small for mask"); \ __BUILD_BUG_ON_NOT_POWER_OF_2((_mask) + \ (1ULL << __bf_shf(_mask))); \ -- 2.35.1