From patchwork Tue Aug 1 11:19:22 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Przemek Kitszel X-Patchwork-Id: 13336522 X-Patchwork-Delegate: kuba@kernel.org Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net [23.128.96.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 9BDE81ED3C for ; Tue, 1 Aug 2023 11:19:53 +0000 (UTC) Received: from mgamail.intel.com (unknown [192.55.52.43]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A9505B0 for ; Tue, 1 Aug 2023 04:19:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1690888792; x=1722424792; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=WtT6XgSWlnrDQ68ZvjZ28IvPo6s0OiqVJdElDVPQXhc=; b=NpMFhqeKuGG3DqZcOViGYB/8v8nslc5RWwj0Esa6r+pvPE2/0PoC+/AX 9W7ReYrApxbrYLAZ8Ty7OZxwWa0CkkR6b652Ta2nXELwg9ZjMiF4nXwso pB7KXNjD1W7+MLnaUNuLB+UNS0p6DXDfxnfm1M/CVTPp1UuksdHJtG5b9 CtdquZRbTr/T3JRZS3nmd+FrhPYnYtr4G8QbqkNwMfE/Recck/3t4POwA Zgnr0NaOfV3KxlAGZZTjSg5BZgqlhC87Kq7wCzdP2NpD4J+w5QBV+Snzw 2zCi+fKGofMEmk4tEJlAWR67DgKLkMQFmM65Ma7PHngnHCSHM/q7ZgAec g==; X-IronPort-AV: E=McAfee;i="6600,9927,10788"; a="455644610" X-IronPort-AV: E=Sophos;i="6.01,246,1684825200"; d="scan'208";a="455644610" Received: from orsmga004.jf.intel.com ([10.7.209.38]) by fmsmga105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 01 Aug 2023 04:19:52 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10788"; a="852415995" X-IronPort-AV: E=Sophos;i="6.01,246,1684825200"; d="scan'208";a="852415995" Received: from irvmail002.ir.intel.com ([10.43.11.120]) by orsmga004.jf.intel.com with ESMTP; 01 Aug 2023 04:19:50 -0700 Received: from pkitszel-desk.tendawifi.com (unknown [10.255.193.236]) by irvmail002.ir.intel.com (Postfix) with ESMTP id 813B233BFE; Tue, 1 Aug 2023 12:19:48 +0100 (IST) From: Przemek Kitszel To: Kees Cook Cc: Jacob Keller , intel-wired-lan@lists.osuosl.org, netdev@vger.kernel.org, Alexander Lobakin , Przemek Kitszel Subject: [RFC net-next 1/2] overflow: add DECLARE_FLEX() for on-stack allocs Date: Tue, 1 Aug 2023 13:19:22 +0200 Message-Id: <20230801111923.118268-2-przemyslaw.kitszel@intel.com> X-Mailer: git-send-email 2.38.1 In-Reply-To: <20230801111923.118268-1-przemyslaw.kitszel@intel.com> References: <20230801111923.118268-1-przemyslaw.kitszel@intel.com> Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_NONE,T_SCC_BODY_TEXT_LINE,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net X-Patchwork-Delegate: kuba@kernel.org X-Patchwork-State: RFC Add DECLARE_FLEX() macro for on-stack allocations of structs with flexible array member. Using underlying array for on-stack storage lets us to declare known on compile-time structures without kzalloc(). Actual usage for ice driver is in next patch of the series. Note that "struct" kw and "*" char is moved to the caller, to both: have shorter macro name, and have more natural type specification in the driver code (IOW not hiding an actual type of var). Signed-off-by: Przemek Kitszel --- include/linux/overflow.h | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/include/linux/overflow.h b/include/linux/overflow.h index f9b60313eaea..403b7ec120a2 100644 --- a/include/linux/overflow.h +++ b/include/linux/overflow.h @@ -309,4 +309,18 @@ static inline size_t __must_check size_sub(size_t minuend, size_t subtrahend) #define struct_size_t(type, member, count) \ struct_size((type *)NULL, member, count) +/** + * DECLARE_FLEX() - Declare an on-stack instance of structure with trailing + * flexible array. + * @type: Pointer to structure type, including "struct" keyword and "*" char. + * @name: Name for a (pointer) variable to create. + * @member: Name of the array member. + * @count: Number of elements in the array; must be compile-time const. + * + * Declare an instance of structure *@type with trailing flexible array. + */ +#define DECLARE_FLEX(type, name, member, count) \ + u8 name##_buf[struct_size((type)NULL, member, count)] __aligned(8) = {};\ + type name = (type)&name##_buf + #endif /* __LINUX_OVERFLOW_H */ From patchwork Tue Aug 1 11:19:23 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Przemek Kitszel X-Patchwork-Id: 13336523 X-Patchwork-Delegate: kuba@kernel.org Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net [23.128.96.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 5BFEB1ED3C for ; Tue, 1 Aug 2023 11:19:55 +0000 (UTC) Received: from mgamail.intel.com (unknown [192.55.52.43]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3E7A7B0 for ; Tue, 1 Aug 2023 04:19:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1690888794; x=1722424794; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=p1jYw57KC9hDihhUz9gnl4OCqMqKWft5XvD3oOaXpVA=; b=XgqIZy9kLFPaCgQy1OP0/2KpnQGUA1L7h2UKvSKN44JeRtrfUPeuOpvs s4ekaXnHCNv8NdRGw/wGuN0sOnC9q+BI35oBKuEiCyWRgJ+vZ7CETqg+d AeAB/KNpmwB4rZk4Z3PzWysqYve/o+DTK0kX/5lSHfVEntsY8cwff/o+3 TGbUqXMZiGlJDvZF8DcNk5scoRYXSkJeKnw5stT2E/9oehlKyHc/VQD0s u5q6O7mJsDUx5UC22T++tOHJeoQ0XM1QBG3BNnarNwQOXz4aB2ZYyQBGE 2BhhB4UM1cMeyJpoPyRgDK/fgy3v/aR92zKyQtBDCRrPDl6N24VdDm2H5 w==; X-IronPort-AV: E=McAfee;i="6600,9927,10788"; a="455644613" X-IronPort-AV: E=Sophos;i="6.01,246,1684825200"; d="scan'208";a="455644613" Received: from orsmga004.jf.intel.com ([10.7.209.38]) by fmsmga105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 01 Aug 2023 04:19:54 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10788"; a="852416004" X-IronPort-AV: E=Sophos;i="6.01,246,1684825200"; d="scan'208";a="852416004" Received: from irvmail002.ir.intel.com ([10.43.11.120]) by orsmga004.jf.intel.com with ESMTP; 01 Aug 2023 04:19:51 -0700 Received: from pkitszel-desk.tendawifi.com (unknown [10.255.193.236]) by irvmail002.ir.intel.com (Postfix) with ESMTP id 4774B33E8F; Tue, 1 Aug 2023 12:19:50 +0100 (IST) From: Przemek Kitszel To: Kees Cook Cc: Jacob Keller , intel-wired-lan@lists.osuosl.org, netdev@vger.kernel.org, Alexander Lobakin , Przemek Kitszel Subject: [RFC net-next 2/2] ice: make use of DECLARE_FLEX() in ice_switch.c Date: Tue, 1 Aug 2023 13:19:23 +0200 Message-Id: <20230801111923.118268-3-przemyslaw.kitszel@intel.com> X-Mailer: git-send-email 2.38.1 In-Reply-To: <20230801111923.118268-1-przemyslaw.kitszel@intel.com> References: <20230801111923.118268-1-przemyslaw.kitszel@intel.com> Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_NONE,T_SCC_BODY_TEXT_LINE,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net X-Patchwork-Delegate: kuba@kernel.org X-Patchwork-State: RFC Use DECLARE_FLEX() macro for 1-elem flex array members of ice_switch.c Signed-off-by: Przemek Kitszel --- drivers/net/ethernet/intel/ice/ice_switch.c | 53 +++++---------------- 1 file changed, 12 insertions(+), 41 deletions(-) diff --git a/drivers/net/ethernet/intel/ice/ice_switch.c b/drivers/net/ethernet/intel/ice/ice_switch.c index a7afb612fe32..41679cb6b548 100644 --- a/drivers/net/ethernet/intel/ice/ice_switch.c +++ b/drivers/net/ethernet/intel/ice/ice_switch.c @@ -1812,15 +1812,11 @@ ice_aq_alloc_free_vsi_list(struct ice_hw *hw, u16 *vsi_list_id, enum ice_sw_lkup_type lkup_type, enum ice_adminq_opc opc) { - struct ice_aqc_alloc_free_res_elem *sw_buf; + DECLARE_FLEX(struct ice_aqc_alloc_free_res_elem *, sw_buf, elem, 1); + u16 buf_len = struct_size(sw_buf, elem, 1); struct ice_aqc_res_elem *vsi_ele; - u16 buf_len; int status; - buf_len = struct_size(sw_buf, elem, 1); - sw_buf = devm_kzalloc(ice_hw_to_dev(hw), buf_len, GFP_KERNEL); - if (!sw_buf) - return -ENOMEM; sw_buf->num_elems = cpu_to_le16(1); if (lkup_type == ICE_SW_LKUP_MAC || @@ -1840,8 +1836,7 @@ ice_aq_alloc_free_vsi_list(struct ice_hw *hw, u16 *vsi_list_id, sw_buf->res_type = cpu_to_le16(ICE_AQC_RES_TYPE_VSI_LIST_PRUNE); } else { - status = -EINVAL; - goto ice_aq_alloc_free_vsi_list_exit; + return -EINVAL; } if (opc == ice_aqc_opc_free_res) @@ -1849,16 +1844,14 @@ ice_aq_alloc_free_vsi_list(struct ice_hw *hw, u16 *vsi_list_id, status = ice_aq_alloc_free_res(hw, 1, sw_buf, buf_len, opc, NULL); if (status) - goto ice_aq_alloc_free_vsi_list_exit; + return status; if (opc == ice_aqc_opc_alloc_res) { vsi_ele = &sw_buf->elem[0]; *vsi_list_id = le16_to_cpu(vsi_ele->e.sw_resp); } -ice_aq_alloc_free_vsi_list_exit: - devm_kfree(ice_hw_to_dev(hw), sw_buf); - return status; + return 0; } /** @@ -2088,15 +2081,10 @@ ice_aq_get_recipe_to_profile(struct ice_hw *hw, u32 profile_id, u8 *r_bitmap, */ int ice_alloc_recipe(struct ice_hw *hw, u16 *rid) { - struct ice_aqc_alloc_free_res_elem *sw_buf; - u16 buf_len; + DECLARE_FLEX(struct ice_aqc_alloc_free_res_elem *, sw_buf, elem, 1); + u16 buf_len = struct_size(sw_buf, elem, 1); int status; - buf_len = struct_size(sw_buf, elem, 1); - sw_buf = kzalloc(buf_len, GFP_KERNEL); - if (!sw_buf) - return -ENOMEM; - sw_buf->num_elems = cpu_to_le16(1); sw_buf->res_type = cpu_to_le16((ICE_AQC_RES_TYPE_RECIPE << ICE_AQC_RES_TYPE_S) | @@ -2105,7 +2093,6 @@ int ice_alloc_recipe(struct ice_hw *hw, u16 *rid) ice_aqc_opc_alloc_res, NULL); if (!status) *rid = le16_to_cpu(sw_buf->elem[0].e.sw_resp); - kfree(sw_buf); return status; } @@ -4482,16 +4469,10 @@ int ice_alloc_res_cntr(struct ice_hw *hw, u8 type, u8 alloc_shared, u16 num_items, u16 *counter_id) { - struct ice_aqc_alloc_free_res_elem *buf; - u16 buf_len; + DECLARE_FLEX(struct ice_aqc_alloc_free_res_elem *, buf, elem, 1); + u16 buf_len = struct_size(buf, elem, 1); int status; - /* Allocate resource */ - buf_len = struct_size(buf, elem, 1); - buf = kzalloc(buf_len, GFP_KERNEL); - if (!buf) - return -ENOMEM; - buf->num_elems = cpu_to_le16(num_items); buf->res_type = cpu_to_le16(((type << ICE_AQC_RES_TYPE_S) & ICE_AQC_RES_TYPE_M) | alloc_shared); @@ -4499,12 +4480,9 @@ ice_alloc_res_cntr(struct ice_hw *hw, u8 type, u8 alloc_shared, u16 num_items, status = ice_aq_alloc_free_res(hw, 1, buf, buf_len, ice_aqc_opc_alloc_res, NULL); if (status) - goto exit; + return status; *counter_id = le16_to_cpu(buf->elem[0].e.sw_resp); - -exit: - kfree(buf); return status; } @@ -4520,16 +4498,10 @@ int ice_free_res_cntr(struct ice_hw *hw, u8 type, u8 alloc_shared, u16 num_items, u16 counter_id) { - struct ice_aqc_alloc_free_res_elem *buf; - u16 buf_len; + DECLARE_FLEX(struct ice_aqc_alloc_free_res_elem *, buf, elem, 1); + u16 buf_len = struct_size(buf, elem, 1); int status; - /* Free resource */ - buf_len = struct_size(buf, elem, 1); - buf = kzalloc(buf_len, GFP_KERNEL); - if (!buf) - return -ENOMEM; - buf->num_elems = cpu_to_le16(num_items); buf->res_type = cpu_to_le16(((type << ICE_AQC_RES_TYPE_S) & ICE_AQC_RES_TYPE_M) | alloc_shared); @@ -4540,7 +4512,6 @@ ice_free_res_cntr(struct ice_hw *hw, u8 type, u8 alloc_shared, u16 num_items, if (status) ice_debug(hw, ICE_DBG_SW, "counter resource could not be freed\n"); - kfree(buf); return status; }