Received: by 2002:a05:7208:9594:b0:7e:5202:c8b4 with SMTP id gs20csp446292rbb; Sat, 24 Feb 2024 07:17:34 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCXeSZsBZrVp9fGIyf0I/m208MZdeQwakrtBmQWx4Za/DHAtZ8ZLdpYabacNsJXHjR01TJE2Ckfx7AY49FsbvVmI017JgOPbWZ5Z1DYcww== X-Google-Smtp-Source: AGHT+IFSy1HrNFNdJ2lJ5ZWRU48HUNx+ijsBcALd+4KRC9tlu5FXwVDdDyV9+qS0AvL/WSSQxxuX X-Received: by 2002:a17:902:ec8b:b0:1db:2ad9:9393 with SMTP id x11-20020a170902ec8b00b001db2ad99393mr3739945plg.48.1708787853928; Sat, 24 Feb 2024 07:17:33 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708787853; cv=pass; d=google.com; s=arc-20160816; b=jGtKGmgwDXKMZfEVQAhDSZJjjcmEg/yJBZDyABkcFzEGkSyEE1iuptM1+/C1wOFvrX 7PGSQ7YykTxF8mufim3uVx3pN0U3i99qkicVOsocNWypHIxsoAyAIObuoJdpNGeDhcgh f55xoNVqCYoMLdmP22PTAT7KcWsQYk20w2UF/TlWvkgOh5rG5mtji1ZVaBIJWcTDgpGi jg3eAfU27Sr9L23uWgxDo4joAPaTs1J2rLqfc06ll/ntguydtxjHsUaHsL5Xx01zzqP0 ep7rW/ypsA97JR8l+GpNhhFOXegW3GLUvZ6DbeG9xOMAWP/GgpwVNd9VL8VzCRKMYhRi 4ARg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=ui-outboundreport:content-transfer-encoding:mime-version :list-unsubscribe:list-subscribe:list-id:precedence:message-id:date :subject:cc:to:from:dkim-signature; bh=+HcSfPFMLChK6J/TX7gwqgPZmghiNiP7t0KIE07Fq+4=; fh=XqbhKM6+62AJKqUXDzjWbPw1sYNb9O4emxySFhF9Qzc=; b=iPPDdw46UyGIC2PGLYVBEMp1/xbkHbFIQQ6nmSZO859YPoHz3nBpEsAUpqcTeepXDs O8xUDazhlAGp4ShRmZofcN1biWaCcKcPYQuGCE8XXj2MTvR12H+mtar7UcRzMzINEz4e dw8gRLJqam3FDJyGFGY6lErujDSUHhRtW3VCnpwOq+9/TOlNOz2jx0Czr+a6LUJ8NztE +uEYAdL/wweYWsbTdNyT6NZFYf+qLjIqiT379WLIQnrDk3fnjcU7euixJAti5OubLgDo mHo6yaiGfKHR9spTRaRQjLVhvYPxhvlXc/Q4zqZraSlQmNgtjAIf/J8XQ1usZmhb36h4 u6Bw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@gmx.com header.s=s31663417 header.b=du0Gha4L; arc=pass (i=1 spf=pass spfdomain=gmx.com dkim=pass dkdomain=gmx.com dmarc=pass fromdomain=gmx.com); spf=pass (google.com: domain of linux-kernel+bounces-79722-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-79722-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=gmx.com Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id j18-20020a170902c3d200b001d724cd0d18si915128plj.323.2024.02.24.07.17.33 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 24 Feb 2024 07:17:33 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-79722-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; dkim=pass header.i=@gmx.com header.s=s31663417 header.b=du0Gha4L; arc=pass (i=1 spf=pass spfdomain=gmx.com dkim=pass dkdomain=gmx.com dmarc=pass fromdomain=gmx.com); spf=pass (google.com: domain of linux-kernel+bounces-79722-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-79722-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=gmx.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id 9AC0F282B3B for ; Sat, 24 Feb 2024 15:17:33 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 87EA146544; Sat, 24 Feb 2024 15:17:29 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmx.com header.i=erick.archer@gmx.com header.b="du0Gha4L" Received: from mout.gmx.net (mout.gmx.net [212.227.15.19]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 6290145033; Sat, 24 Feb 2024 15:17:26 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=212.227.15.19 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708787848; cv=none; b=sey7UNfVmIk/OeRFAiyqa45vuPXe7NdGhpdKsEBgbg7PWJvrLw42/1jBkcYwgBHL6hWMTthc2Q30E1/vvhnPr/S8nYMbTlsjhOGZsp+6tPYUy3v5tQ5uMLCtmaJEKarzg6wzqny0NGdOKBatHDhoRsyMv+524/8RegJI1vrGtcs= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708787848; c=relaxed/simple; bh=/Ai4/Q7V0MmW3HXmJ0iBatCL69uChHgdEWoGbQ5ItZk=; h=From:To:Cc:Subject:Date:Message-Id:MIME-Version; b=mNVBnz3RLy4v8a/rbUowr85M/DNW9WdsC7jZYNHt3LVrciDj2PlR6c9Bx1S7NS2kM+hgWi8YIUuHe1R0DciyAUIUnoZ5evu7tisfiHJYgLL701ZG5x7PqFAdfFTITm2UupZuzZMM3dxUmkVYcW0j+wYMzAYZtF7hkzKRE66C9gE= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=gmx.com; spf=pass smtp.mailfrom=gmx.com; dkim=pass (2048-bit key) header.d=gmx.com header.i=erick.archer@gmx.com header.b=du0Gha4L; arc=none smtp.client-ip=212.227.15.19 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=gmx.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmx.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gmx.com; s=s31663417; t=1708787837; x=1709392637; i=erick.archer@gmx.com; bh=/Ai4/Q7V0MmW3HXmJ0iBatCL69uChHgdEWoGbQ5ItZk=; h=X-UI-Sender-Class:From:To:Cc:Subject:Date; b=du0Gha4LrGTzIR1VjWKkV4JBqPp0HYcNFkXAKgp+S3lsY8SwP7umMaZ78i9Stqu7 qpRWmhdqGKhlG0Ep3tVnIGZ79/1OqGqOCI7q7TTcsbM0L2jO1zBEgkjz+iX73g8gS KI5IM7kAtv6MY1hChDP3fLhNfWUjh+LiBluTRmsfbwIIX8MtVicXKEutSwK5xHIVk flrMqemW8jr/1ZXdmfy4mmPQejNBfPvMImaRNVSy5fTKK+04BwtaOC6rtw+9SEOaB FxE4BUzgqR+zVXD+CsMGwTw6dTatuc5yEQDYA+Nao6BrDDSWk04nP8Y5OcIT2mFlz MPYk2qFTBaDYbYYNhA== X-UI-Sender-Class: 724b4f7f-cbec-4199-ad4e-598c01a50d3a Received: from localhost.localdomain ([79.157.194.183]) by mail.gmx.net (mrgmx005 [212.227.17.184]) with ESMTPSA (Nemesis) id 1MA7Ka-1rjyZb2BfW-00Bahh; Sat, 24 Feb 2024 16:17:17 +0100 From: Erick Archer To: Kent Overstreet , Brian Foster , "Gustavo A. R. Silva" , Kees Cook Cc: Erick Archer , linux-bcachefs@vger.kernel.org, linux-kernel@vger.kernel.org, linux-hardening@vger.kernel.org Subject: [PATCH] bcachefs: Prefer struct_size over open coded arithmetic Date: Sat, 24 Feb 2024 16:16:58 +0100 Message-Id: <20240224151658.8272-1-erick.archer@gmx.com> X-Mailer: git-send-email 2.25.1 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Provags-ID: V03:K1:GZsbmeNaBr0xi5hRenKf+OJZBVs7DEAPKRYS0/rm+w2gCsyic61 sKzfT0VaJ1bEMsEzPS3S/0HJHtkPltI5lPA6sMy5wVX9Ijd+Q4xe3KPZMGRf3ml8q8jXU/A fBM/4SBTCpwMSd0FHnXym5s/sL97mYr+tqflhcsJxu2CwINqeMfx+pQYNNNTFBVW6humkHu 8+J5Txcy1n2/uiIkPmS8w== X-Spam-Flag: NO UI-OutboundReport: notjunk:1;M01:P0:14ZWeun70VM=;TIYijmmwBT7pG+DaqiuWItPhcrR /jGH1gmGcYxnW87plh7Zp11dc3WxGVEO97WpWyDhrGNq9kxH20NvUNJqDcU68mE+z/hO+hFJP +DGt+W0bKJXjqomZtkKW57QQYbyQ4Lr+a/thOD9MR+wC5OB39LpDaD+oQ4xg6CoAf28bvcmda CvwGmkTAAy8ebfUOq1TnjaUjGU34gWKOL8+PL0s8l2+dqU+91TEMtwgYcdMLPTWkH5DEM1iC5 TBIK42NpvSk8za3ioZTjnGsozgnaxD3t4aR2WkpeMX2nBfIuaurJItNCMdr0ABqpbPB0LLzv1 61BZlCmZzjppZE6oPdv34YyVaB4tgxQGyiu35Nrx8xcXixLtnHrnT/PvARLp1zaRx6gan6jpu yaU4/Z1eoRWttKnzMqTfE0XXtdh/cZNqIcngNl/8tH1eOGttpFo5Sd4eyAconTt3R8zR4TsSv 7dZLyaxNrOVk29f7pqIRm80ruENzAFKZ4iRcJHBE7xAgUvgy35duHfkynyatpqIZRsPJLg4sy DuBhJ3EjeouU7DlyHEYB1uXToSeEFt/VAnzXDgs5VJvQ+ajtHmj9W6qH8OclwLW3WvZioPzVI 7DHr4kXWF4HhB0BAvazwMVdGxFpF2zNp2VIuDnI8Gn5ZYP7m8+oizkqQcpu5vU+nfnkz+ewHi 9mEjwfjooGigD10VYkUwBuEH61QMQyFj6/tkbVdXoJaDYQva2wiZq6C5tHpVS6F7jVTayzqCS 1x8iFqGGvuv4jHtq+HoEAKex6c2Sk7zqlIjLA4mzxiCyGm5O2kVh6aBnZ0loMJLKZaXmFWJXu MuGIoefpWN7I62/e7nQ8XoM0c1oImJ0Ut9kOrDEiBPXSk= This is an effort to get rid of all multiplications from allocation functions in order to prevent integer overflows [1][2]. As the "t" variable is a pointer to "struct journal_seq_blacklist_table" and this structure ends in a flexible array: struct journal_seq_blacklist_table { [...] struct journal_seq_blacklist_table_entry { u64 start; u64 end; bool dirty; } entries[]; }; the preferred way in the kernel is to use the struct_size() helper to do the arithmetic instead of the argument "size + size * count" in the kzalloc() function. This way, the code is more readable and safer. Link: https://www.kernel.org/doc/html/latest/process/deprecated.html#open-= coded-arithmetic-in-allocator-arguments [1] Link: https://github.com/KSPP/linux/issues/160 [2] Signed-off-by: Erick Archer =2D-- fs/bcachefs/journal_seq_blacklist.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/fs/bcachefs/journal_seq_blacklist.c b/fs/bcachefs/journal_seq= _blacklist.c index 024c9b1b323f..2c2490aa15fe 100644 =2D-- a/fs/bcachefs/journal_seq_blacklist.c +++ b/fs/bcachefs/journal_seq_blacklist.c @@ -165,8 +165,7 @@ int bch2_blacklist_table_initialize(struct bch_fs *c) if (!bl) return 0; - t =3D kzalloc(sizeof(*t) + sizeof(t->entries[0]) * nr, - GFP_KERNEL); + t =3D kzalloc(struct_size(t, entries, nr), GFP_KERNEL); if (!t) return -BCH_ERR_ENOMEM_blacklist_table_init; =2D- 2.25.1