Received: by 2002:a05:7412:251c:b0:e2:908c:2ebd with SMTP id w28csp850057rda; Sun, 22 Oct 2023 12:40:10 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHDk/Z7CwK5fqIVv75c+zFcfoRHI9T0IN4cLuhA2sElVST0VIE4A6AU2zkc7QrP5XARP5IH X-Received: by 2002:a05:6e02:216e:b0:34f:2756:8fd with SMTP id s14-20020a056e02216e00b0034f275608fdmr9774864ilv.9.1698003610455; Sun, 22 Oct 2023 12:40:10 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1698003610; cv=none; d=google.com; s=arc-20160816; b=tNO+0jKGFCnKk2amIS0NTlM0aytsFeYsvEX8J9lPuanD/GenkXmdTB/lQ48AOQvO6a jzaeCT+wPFI4khMtJiTr7Z3pz277Id+UKW9qHckt5oUzZFbnc2l8+RpR5zl09Qj30Aaa C3WVAH7hyjVCc89s1Zr81qqdS87RTgRO9GGvzenWzCCJFjUN7fy3nwHWdPU/aVXu6rVI EabonhDjaLHmGU3XA8NQrS9NJEKHcogYLItmnBSBOhjI2aFzgUrdgNOHbfhPAv89HJue OePViN+AF5VNZAOzgOcJ9j+kNT55tAPvarZa5iF+mHztF/3ZqU8iyBmEcgMJP6zQh0KD QgpA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-disposition:mime-version:message-id :subject:cc:to:from:date:dkim-signature; bh=RjC8Y/pab1L2l+rQYaE4Wv4U+6siS+n8WEbIoYjl9EE=; fh=IhDG1DAjhcjYLpZK7GdN6Ucoij7AHOXpyAQn0VYtyI4=; b=luqNyxkIx8TIPzzEv8Ghd5BnLQEyehoOD9bzewbPhrUfFu3xvDo8DNpUBvuUIwT3th 0THi9Z/RV2qo1vhkoKYzikZfV5mbRuMLaN1K28Gm4Yr0867T+C/qjgvucdDcJ/V1MznL B6+caZS6T2Cs6ki+R7b6loznTm4QUcYwN014nS98RQZbiudu1hFuXvjdOzxXCqm3CMt5 Ms3UAXKFt5py3lwOMeyrOL/aUi7geePk2qO3jUnkSl7OcvWbtTyjNMjOQ29ttLVnXCEz wXoPFC7JU8JvUrKdAAcFtM74rdetjtCpQ5qquih2AfZBMwsgYF1hWRQkN7ve8fCUImkF byFg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=isESi4p3; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.31 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from morse.vger.email (morse.vger.email. [23.128.96.31]) by mx.google.com with ESMTPS id h26-20020a63385a000000b005b5df8bf69csi5484365pgn.98.2023.10.22.12.40.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 22 Oct 2023 12:40:10 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.31 as permitted sender) client-ip=23.128.96.31; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=isESi4p3; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.31 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by morse.vger.email (Postfix) with ESMTP id 7B167806137B; Sun, 22 Oct 2023 12:40:06 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at morse.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232515AbjJVTkB (ORCPT + 99 others); Sun, 22 Oct 2023 15:40:01 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42076 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229452AbjJVTkA (ORCPT ); Sun, 22 Oct 2023 15:40:00 -0400 Received: from mail-ed1-x52e.google.com (mail-ed1-x52e.google.com [IPv6:2a00:1450:4864:20::52e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 67329F2; Sun, 22 Oct 2023 12:39:58 -0700 (PDT) Received: by mail-ed1-x52e.google.com with SMTP id 4fb4d7f45d1cf-54041e33845so766467a12.3; Sun, 22 Oct 2023 12:39:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1698003596; x=1698608396; darn=vger.kernel.org; h=content-disposition:mime-version:message-id:subject:cc:to:from:date :from:to:cc:subject:date:message-id:reply-to; bh=RjC8Y/pab1L2l+rQYaE4Wv4U+6siS+n8WEbIoYjl9EE=; b=isESi4p3o6WWWMEuIxMU3XECqadMVlsJPxF9AFbtkoUQX4wn05bbcGdgsURbkUTKdA LesPwpe/26vvjIcYKHBedjB/z5carbm6eYXVLhL+Vwcj8/DC+LXKf3VIPY7tQc5vSflw tJ7uP4ej5HVFomP63vNWkUY72UH0WllZnTS4/AUisBIxucxNuHYFx/9NNHCSpn8gM5dY hQRkPdNaH7C0gNijknty0qWrwiMUQ7No3ofqEKVEiDCkQ4IvUMhJKwsD+oyMr/F4i+C7 pfY35pQK3r69BfMyB+4p15BexUXACs0yLLoR/UNuUQhgRMg/Ij+cxFYDrr5JkClVBKcg BSKA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698003596; x=1698608396; h=content-disposition:mime-version:message-id:subject:cc:to:from:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=RjC8Y/pab1L2l+rQYaE4Wv4U+6siS+n8WEbIoYjl9EE=; b=dGDhvAT97cq99aqel8X3Zybx57DKmSZPy6SQejryyb3hplM+JpERERVDpk95g9o5VD YJR12SNnb8XYJN17ryMr6pf5Rxt48lZJtyPGCePBW3//j+ufNDe9Ff51Pxa5lhY3s9CO jM9HWsSXtWYacCPctHFiZe2lQ2527ZavPqFnFtfR32/hSLCU6Jx7QOK9Y3wXZpjyel12 a/7pfKXEGnQikT9SXm59CZ09I/X7E48OHlH34+lxmrwfly1qFdDiwF6Qy8Q1oUQKxm/r dWgF1eJQARh2+JGPWo6E6ouEA6dJzHZE6gQ732auUhwJg4fM2mXNdKeziZWCOcWmS+SX 7kaw== X-Gm-Message-State: AOJu0YxKjSL9aT68WQziP5aet+vpXFT1QM3ZVXzxoIws+sMWS2/kqC8p 4dUfxVoP4ml96AM57Fr+TCKYoQza+Q== X-Received: by 2002:a50:aacf:0:b0:53e:1388:cb2e with SMTP id r15-20020a50aacf000000b0053e1388cb2emr5144044edc.35.1698003596358; Sun, 22 Oct 2023 12:39:56 -0700 (PDT) Received: from p183 ([46.53.251.94]) by smtp.gmail.com with ESMTPSA id f9-20020aa7d849000000b0053e49302056sm5101480eds.64.2023.10.22.12.39.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 22 Oct 2023 12:39:55 -0700 (PDT) Date: Sun, 22 Oct 2023 22:39:54 +0300 From: Alexey Dobriyan To: linux-kernel@vger.kernel.org Cc: torvalds@vger.kernel.org Subject: PSA: BITS_TO_LONGS() most likely returns size_t, not what you think Message-ID: MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline X-Spam-Status: No, score=-0.6 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, 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 morse.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 (morse.vger.email [0.0.0.0]); Sun, 22 Oct 2023 12:40:06 -0700 (PDT) I wrote the following code today (don't ask): for (int i = 0; i <= BITS_TO_LONGS(NR_CPUS) - 2; i += 1) { } only to get near infinite loop. Note that NR_CPUS is "int". This is because BITS_TO_LONGS() and __KERNEL_DIV_ROUND_UP() macros work together to promote everything to "size_t". The loop essientially becomes: for (int i = 0; i <= (size_t)-1; i += 1) { } This is easily fixable by doing very sketchy and obvious thing. --- a/include/linux/bitops.h +++ b/include/linux/bitops.h @@ -16,7 +16,7 @@ # define aligned_byte_mask(n) (~0xffUL << (BITS_PER_LONG - 8 - 8*(n))) #endif -#define BITS_PER_TYPE(type) (sizeof(type) * BITS_PER_BYTE) +#define BITS_PER_TYPE(type) ((int)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)) Ideally __KERNEL_DIV_ROUND_UP() should return the type of the first argument but this stuff is UAPI header which complicates things.