From patchwork Sat Jul 22 16:47:24 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christophe JAILLET X-Patchwork-Id: 124346 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:9010:0:b0:3e4:2afc:c1 with SMTP id l16csp910377vqg; Sat, 22 Jul 2023 10:26:30 -0700 (PDT) X-Google-Smtp-Source: APBJJlE6Kdab5aU0puPMUIKy+je9x7vKznpE+yaxy+rITt3b/CMQ6AacwH1Oukvg/NOybDMcF/TI X-Received: by 2002:a05:6402:1203:b0:51d:d4c3:6858 with SMTP id c3-20020a056402120300b0051dd4c36858mr4537838edw.12.1690046790665; Sat, 22 Jul 2023 10:26:30 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1690046790; cv=none; d=google.com; s=arc-20160816; b=SN+OdSBcUwyKtC2eIZB+bHBEwDYOyYGSOrsWARkFRMLojoyDnLP5AfD1PM10m9FiVe 5RVSz/nfNWn079d83zYJmv4Yj1CjVQ2+xHD5Kf9yW4hYvYPO8Cyn5CXxnqUPQ9HLIkeD T3sG5rfiR+8PCdI+q4OuTgqWLkjV26r7Y4ngOQOGrVCYyvihXzKsv421JkOKS9PARwTq bdLoeOaXORI2/tcEZU0M2LW8UdtFGG3RC1Kfezehtl/GoxtjQn5zKay8/gzrfiAlI0+q Yk22acBsJQRuJjWJuF5Th/sXYxgdzhg8WexmIhA9awIpQYqskMmdZPXxaBz1OtDHvR94 b9KQ== 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 Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id ca9-20020aa7cd69000000b0051a52a48ed4si4161417edb.156.2023.07.22.10.26.05; Sat, 22 Jul 2023 10:26:30 -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 S229835AbjGVQre (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 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 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1772142503053079602 X-GMAIL-MSGID: 1772142503053079602 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;