Received: by 2002:a05:6358:701b:b0:131:369:b2a3 with SMTP id 27csp1300691rwo; Sat, 22 Jul 2023 10:17:08 -0700 (PDT) X-Google-Smtp-Source: APBJJlFQYA8snQhpl9UgIzeWOdP+gJ9Td6GMEnN8R8ubnrxt8kZN1oM+ZRH3K2YDk9KDko+BsWHW X-Received: by 2002:a17:906:53d9:b0:99b:5642:b97a with SMTP id p25-20020a17090653d900b0099b5642b97amr5495287ejo.46.1690046227875; Sat, 22 Jul 2023 10:17:07 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1690046227; cv=none; d=google.com; s=arc-20160816; b=HJU2WEl6sSfnTDzmXq6W9ndEhM2ivkz00o9Ws7dBhdN59lUYSvW+2Iba+VB/SI87Qx MCuwDAXG3FzqcUC9Nb6Tl93HOQLX03PdG7ZVTERtEbn32ZNFwosVOozEuHo/Ip4ZdKNi L8nBd5QnCdLOP4JdjQV6J7eUu7iDyjNlZ6zsVs7ix6D1gGkFnqHUXrNOCVcKfn7cDIJS 3qgiFLXKXe4zDHdb+TOSGvp+xnF/tR6zcMTa3Gr5dkHfF0/fQpw3vaQT2wD0rW/LU1rl HT2e+B+vQjzJa1T9/eK3tiEH/IkiocakKo8m6+psmTeRz/mkiar+kDzvKvXxb9GlCntN 4TeA== 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 :message-id:date:subject:cc:to:from:dkim-signature; bh=IQyaExm2W+hWmqxwBEcF/S4pavhj7DW1iV5TthHyYq0=; fh=+YGy7aQmW5gIwqQmJLeM1JssAbHrK3xCsZBG8M8E1aI=; b=CBEXjxsoqjL9+SDJphmF9292ZKpZbeUQqHh7/1flxzruuv8LdUazqkb+21pPBb74bO ZafuN2xuO560Dk+fjQBozRQ4cbb3wwI2Q9ox0Kjkz3TVdRoZumTnj5FQWNMwDYL+XNW4 xXm5cUrEvY/zVtptghuXc3Zm0nrB/tRJIIgrMyICZ98yfJkkuh5MX1n2LrrSfex/ddhQ Kkh/qD529X5UJhQnVlNSV2cKdf9fHgV6vmz/tJOzWobOQFE3Z+cDbUcbx9bKjrQQTuvX TM3JC6XCfT0SomAHmqIsGgqwal8tDUTntdZi/lf5n52FjTUieaqlaL7U5JGT/oYmb69u seNw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@wanadoo.fr header.s=t20230301 header.b=bv6jFkyP; 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 Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id h19-20020aa7de13000000b005222065ee63si157610edv.384.2023.07.22.10.16.43; Sat, 22 Jul 2023 10:17:07 -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=@wanadoo.fr header.s=t20230301 header.b=bv6jFkyP; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229832AbjGVQre (ORCPT + 99 others); Sat, 22 Jul 2023 12:47:34 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57526 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229828AbjGVQrc (ORCPT ); Sat, 22 Jul 2023 12:47:32 -0400 Received: from smtp.smtpout.orange.fr (smtp-28.smtpout.orange.fr [80.12.242.28]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 11386E7B for ; Sat, 22 Jul 2023 09:47:30 -0700 (PDT) Received: from pop-os.home ([86.243.2.178]) by smtp.orange.fr with ESMTPA id NFlNqftgEZWkDNFlOqjnql; Sat, 22 Jul 2023 18:47:29 +0200 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=wanadoo.fr; s=t20230301; t=1690044449; bh=IQyaExm2W+hWmqxwBEcF/S4pavhj7DW1iV5TthHyYq0=; h=From:To:Cc:Subject:Date; b=bv6jFkyPyNTJAMS45ucOXR0aal1IV5W56+H6O/egzxKIDCJmW9IbSNtvYllo84heK kKUxEWgD0UzOCdCp8ckbb5dtvYxgyLrPNbHN/QivomcO3XgGtKLmno54hYOQ6SXbbo w/cK/Z9ZpGlRH0uTMjoRvlTwITZaW5ReuzffE6nwszpSH3t6Em6LY8D8IDhatmJi91 r7bTnzCQnRiq4+ct1gyt5lODJcKAHXe5B3YrSeHQVUnZ8gQlkqakffCDHc5qYRO4MM +7Wk6XlzyjnH34PqzYxEgMsrG7lvf1uyLXp2cE+nDcOkDKwKSL3pPTH0wkLELackOL K7t9XNqvKmegw== X-ME-Helo: pop-os.home X-ME-Auth: Y2hyaXN0b3BoZS5qYWlsbGV0QHdhbmFkb28uZnI= X-ME-Date: Sat, 22 Jul 2023 18:47:29 +0200 X-ME-IP: 86.243.2.178 From: Christophe JAILLET To: Dennis Dalessandro , Jason Gunthorpe , Leon Romanovsky Cc: linux-kernel@vger.kernel.org, kernel-janitors@vger.kernel.org, Christophe JAILLET , linux-rdma@vger.kernel.org Subject: [PATCH v2] IB/hfi1: Use struct_size() Date: Sat, 22 Jul 2023 18:47:24 +0200 Message-Id: X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_BLOCKED, RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL,SPF_HELO_PASS,SPF_PASS, T_SCC_BODY_TEXT_LINE autolearn=unavailable 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 Use struct_size() instead of hand-writing it, when allocating a structure with a flex array. This is less verbose, more robust and more informative. Signed-off-by: Christophe JAILLET --- It will also be helpful if the __counted_by() annotation is added with a Coccinelle script such as: https://git.kernel.org/pub/scm/linux/kernel/git/kees/linux.git/commit/?h=devel/counted_by&id=adc5b3cb48a049563dc673f348eab7b6beba8a9b Change in v2: use struct_size() in another place just a few line below. --- drivers/infiniband/hw/hfi1/pio.c | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/drivers/infiniband/hw/hfi1/pio.c b/drivers/infiniband/hw/hfi1/pio.c index 62e7dc9bea7b..dfea53e0fdeb 100644 --- a/drivers/infiniband/hw/hfi1/pio.c +++ b/drivers/infiniband/hw/hfi1/pio.c @@ -1893,9 +1893,7 @@ int pio_map_init(struct hfi1_devdata *dd, u8 port, u8 num_vls, u8 *vl_scontexts) vl_scontexts[i] = sc_per_vl + (extra > 0 ? 1 : 0); } /* build new map */ - newmap = kzalloc(sizeof(*newmap) + - roundup_pow_of_two(num_vls) * - sizeof(struct pio_map_elem *), + newmap = kzalloc(struct_size(newmap, map, roundup_pow_of_two(num_vls)), GFP_KERNEL); if (!newmap) goto bail; @@ -1910,9 +1908,8 @@ int pio_map_init(struct hfi1_devdata *dd, u8 port, u8 num_vls, u8 *vl_scontexts) int sz = roundup_pow_of_two(vl_scontexts[i]); /* only allocate once */ - newmap->map[i] = kzalloc(sizeof(*newmap->map[i]) + - sz * sizeof(struct - send_context *), + newmap->map[i] = kzalloc(struct_size(newmap->map[i], + ksc, sz), GFP_KERNEL); if (!newmap->map[i]) goto bail; -- 2.34.1