Received: by 2002:a05:7412:251c:b0:e2:908c:2ebd with SMTP id w28csp701493rda; Sun, 22 Oct 2023 06:18:35 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFLJApdLmIRR7jJA/1BdNCL0cue3zDApEVI6pKoBCgn/SgvQC8Gw8gwldrkC8nBh6DOZUjn X-Received: by 2002:a05:6358:7e07:b0:166:f338:78e3 with SMTP id o7-20020a0563587e0700b00166f33878e3mr10491481rwm.21.1697980715259; Sun, 22 Oct 2023 06:18:35 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1697980715; cv=none; d=google.com; s=arc-20160816; b=xWHIjj6fTIVTkYv8991igmUKldbDUpW+i8iEXDtYrP5XvN5o8irLNB4dndgz0IqIpb +zMPZLnftcbBCyWoKrIxcwJQE3I0drtIXNxdZtm6GJrixs89DnqU4i+f3iXClpOskOgO rtmuG86yKRkieW7U9Nk/I2HrRwGMYgrm9nhrcVaoN/JkR8sNqfmdikv2q28vmdHEDDfi Q1QKsNyWSM1Dz1JJrOOHaCElwd55+OCk6AT4lXy3NCD84NajUHnKwDMGdvf4QtHHrkMd edHuOsPVgVMLCfAQCjwdKTwlqDOBSU0k8jr/P2Y1EkM6bABwFMLrr/VLrVaLKQfEFuqi 3qzQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=FozGdIRQmW2EWY82p0+85v8JSoc6DFTbluojPbCfYpI=; fh=NwlrkWCPwZOB2YShzRPtx4gmBvCsOiQEdsZCcACWVkg=; b=HCmkAe22gEpUISoGe+LZduC5Um8o9bRGwDfD/lqw77L4xmar2+DO777PlrSAR8wFyq Fivn2w2oyTOIrMhsgW3Zflw0Sj0050j9Ojh36qbMEQWhi/XGwNEc1TRfdCEBaAv5zNIo aTq8vM0L0Vp98FXaCNdilmpG0++91Zic7Aw+0UOEfL0ezzxxdLD289iqvvTVLJTASP9K bG5zulF2BlTb5LRKdGKh4368Y4zFTh+9/DmSSJv8OY+S0BSEQovXWyqS4VxA7bswjRvr Kzja/mQwlDAl+Gj7hrq5P/ev5ZD6wM05dBg+nf5sChkfV6m78ybox+7i816BTdwi/808 Yu9w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@ibm.com header.s=pp1 header.b="CvijnA/X"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:4 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=NONE dis=NONE) header.from=ibm.com Return-Path: Received: from howler.vger.email (howler.vger.email. [2620:137:e000::3:4]) by mx.google.com with ESMTPS id a21-20020a63e855000000b005897bfc2ed1si4941124pgk.215.2023.10.22.06.18.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 22 Oct 2023 06:18:35 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:4 as permitted sender) client-ip=2620:137:e000::3:4; Authentication-Results: mx.google.com; dkim=pass header.i=@ibm.com header.s=pp1 header.b="CvijnA/X"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:4 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=NONE dis=NONE) header.from=ibm.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by howler.vger.email (Postfix) with ESMTP id DAA98805BCBB; Sun, 22 Oct 2023 06:18:32 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at howler.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231626AbjJVNSL (ORCPT + 99 others); Sun, 22 Oct 2023 09:18:11 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60484 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229500AbjJVNSK (ORCPT ); Sun, 22 Oct 2023 09:18:10 -0400 Received: from mx0b-001b2d01.pphosted.com (mx0b-001b2d01.pphosted.com [148.163.158.5]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 284C4A1; Sun, 22 Oct 2023 06:18:05 -0700 (PDT) Received: from pps.filterd (m0353722.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 39MDA9B1024218; Sun, 22 Oct 2023 13:18:01 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ibm.com; h=date : from : to : cc : subject : message-id : references : mime-version : content-type : in-reply-to; s=pp1; bh=FozGdIRQmW2EWY82p0+85v8JSoc6DFTbluojPbCfYpI=; b=CvijnA/XVqP6m/hUU7yVVZk2vlh+AwWiQ4Frvgko18WP4fiaYphbOF8Jys0Cy1UjwfI3 rKyEKl1NyDdkM3fZuQqXyUiWOe0qfFGO7p76VYpRgTbKT3CkC1qiThJ1BFxaA/6/38oS ZGeAmbzkUVfTlGNFgYMOwo6cpBRdeNjOtNrqdsR4jP4P43+8vLAIO9au/VCoI4uO+TMt CdHPbBPhjV3kNx5EDn701pUrJSFGLKar7P7DbmVvBzSY4kVogYwdAYHSaebQOmjh9b6S o044hPfJCG4MaG/aEFxWOKxp6gk7EmlYmuLD4SuhT4ddqJ/vBMAgFjvHDcokIPCY+FbW YA== Received: from pps.reinject (localhost [127.0.0.1]) by mx0a-001b2d01.pphosted.com (PPS) with ESMTPS id 3tw4cjr4g5-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Sun, 22 Oct 2023 13:18:01 +0000 Received: from m0353722.ppops.net (m0353722.ppops.net [127.0.0.1]) by pps.reinject (8.17.1.5/8.17.1.5) with ESMTP id 39MDBBml027279; Sun, 22 Oct 2023 13:18:00 GMT Received: from ppma11.dal12v.mail.ibm.com (db.9e.1632.ip4.static.sl-reverse.com [50.22.158.219]) by mx0a-001b2d01.pphosted.com (PPS) with ESMTPS id 3tw4cjr4fy-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Sun, 22 Oct 2023 13:18:00 +0000 Received: from pps.filterd (ppma11.dal12v.mail.ibm.com [127.0.0.1]) by ppma11.dal12v.mail.ibm.com (8.17.1.19/8.17.1.19) with ESMTP id 39MAkWr8012399; Sun, 22 Oct 2023 13:18:00 GMT Received: from smtprelay04.fra02v.mail.ibm.com ([9.218.2.228]) by ppma11.dal12v.mail.ibm.com (PPS) with ESMTPS id 3tvup1aaxb-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Sun, 22 Oct 2023 13:17:59 +0000 Received: from smtpav07.fra02v.mail.ibm.com (smtpav07.fra02v.mail.ibm.com [10.20.54.106]) by smtprelay04.fra02v.mail.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id 39MDHvbj45154598 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Sun, 22 Oct 2023 13:17:57 GMT Received: from smtpav07.fra02v.mail.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 111A720043; Sun, 22 Oct 2023 13:17:57 +0000 (GMT) Received: from smtpav07.fra02v.mail.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 715C020040; Sun, 22 Oct 2023 13:17:56 +0000 (GMT) Received: from localhost (unknown [9.171.9.54]) by smtpav07.fra02v.mail.ibm.com (Postfix) with ESMTPS; Sun, 22 Oct 2023 13:17:56 +0000 (GMT) Date: Sun, 22 Oct 2023 15:17:54 +0200 From: Vasily Gorbik To: Linus Torvalds Cc: Andy Shevchenko , Dmitry Torokhov , Kees Cook , Heiko Carstens , Alexander Gordeev , linux-kernel@vger.kernel.org, linux-s390@vger.kernel.org Subject: Re: [GIT PULL] s390 fixes for 6.6-rc7 Message-ID: References: MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: X-TM-AS-GCONF: 00 X-Proofpoint-ORIG-GUID: aPVmjF00rwTxrNjB_9hQ_Bj55XRM0iMD X-Proofpoint-GUID: amtIQaC2dMxAfJ0XIJ_PzqcYIFVYY9uv X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.272,Aquarius:18.0.980,Hydra:6.0.619,FMLib:17.11.176.26 definitions=2023-10-22_10,2023-10-19_01,2023-05-22_02 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 suspectscore=0 mlxlogscore=999 priorityscore=1501 mlxscore=0 phishscore=0 malwarescore=0 adultscore=0 clxscore=1011 bulkscore=0 lowpriorityscore=0 impostorscore=0 spamscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2310170001 definitions=main-2310220120 X-Spam-Status: No, score=-0.8 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,SPF_HELO_NONE, SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on howler.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (howler.vger.email [0.0.0.0]); Sun, 22 Oct 2023 06:18:33 -0700 (PDT) On Sat, Oct 21, 2023 at 11:08:31AM -0700, Linus Torvalds wrote: > in case you or somebody has a better idea for BITS_TO_LONG handling > than just "you need to check for zero before and after". > > On Sat, 21 Oct 2023 at 10:56, Linus Torvalds > wrote: > > > > If you *do* want to add proper overflow handling, you'd need to either > > fix BITS_TO_LONGS() some way (which is actually non-trivial since it > > needs to be able to stay a constant and only use the argument once), > > or you do something like > > > > if (!bits) > > return ZERO_SIZE_PTR; > > longs = BITS_TO_LONG(bits); > > if (!longs) > > return NULL; > > return vzalloc(longs * sizeof(long)); This might work. BITS_TO_(bits) utilizes __KERNEL_DIV_ROUND_UP, which may potentially result in an overflow condition when bits > ULONG_MAX - sizeof() * 8 + 1. To resolve this issue, avoid using the overflow-prone __KERNEL_DIV_ROUND_UP. To meet the requirements of BITS_TO(bits) for remaining constant and preventing side effects from multiple argument uses, employ __is_constexpr to differentiate between constant and non-constant cases, employing a helper function in the latter. In the constant case, this ensures compatibility with constructs like DECLARE_BITMAP. While in the non-constant case, the __bits_to_elem_count function could be optimized for potentially improved code generation by compilers, though this might come at the expense of readability and visual consistency between the constant and non-constant cases. I could further investigate if this approach, in general, appears acceptable. --- include/linux/bitops.h | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/include/linux/bitops.h b/include/linux/bitops.h index 2ba557e067fe..72be25d4b95d 100644 --- a/include/linux/bitops.h +++ b/include/linux/bitops.h @@ -15,11 +15,21 @@ # define aligned_byte_mask(n) (~0xffUL << (BITS_PER_LONG - 8 - 8*(n))) #endif +static inline unsigned long __bits_to_elem_count(size_t nr, size_t sz) +{ + return nr / sz + (nr % sz ? 1 : 0); +} + +#define BITS_TO_ELEM_COUNT(nr, sz) \ + __builtin_choose_expr(__is_constexpr(nr), \ + (nr) / sz + ((nr) % sz ? 1 : 0), \ + __bits_to_elem_count((nr), sz)) + #define BITS_PER_TYPE(type) (sizeof(type) * BITS_PER_BYTE) -#define BITS_TO_LONGS(nr) __KERNEL_DIV_ROUND_UP(nr, BITS_PER_TYPE(long)) -#define BITS_TO_U64(nr) __KERNEL_DIV_ROUND_UP(nr, BITS_PER_TYPE(u64)) -#define BITS_TO_U32(nr) __KERNEL_DIV_ROUND_UP(nr, BITS_PER_TYPE(u32)) -#define BITS_TO_BYTES(nr) __KERNEL_DIV_ROUND_UP(nr, BITS_PER_TYPE(char)) +#define BITS_TO_LONGS(nr) BITS_TO_ELEM_COUNT(nr, BITS_PER_TYPE(long)) +#define BITS_TO_U64(nr) BITS_TO_ELEM_COUNT(nr, BITS_PER_TYPE(u64)) +#define BITS_TO_U32(nr) BITS_TO_ELEM_COUNT(nr, BITS_PER_TYPE(u32)) +#define BITS_TO_BYTES(nr) BITS_TO_ELEM_COUNT(nr, BITS_PER_TYPE(char)) extern unsigned int __sw_hweight8(unsigned int w); extern unsigned int __sw_hweight16(unsigned int w); -- 2.39.2