Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

overflow: Add struct_size_t() helper

While struct_size() is normally used in situations where the structure
type already has a pointer instance, there are places where no variable
is available. In the past, this has been worked around by using a typed
NULL first argument, but this is a bit ugly. Add a helper to do this,
and replace the handful of instances of the code pattern with it.

Instances were found with this Coccinelle script:

@struct_size_t@
identifier STRUCT, MEMBER;
expression COUNT;
@@

- struct_size((struct STRUCT *)\(0\|NULL\),
+ struct_size_t(struct STRUCT,
MEMBER, COUNT)

Suggested-by: Christoph Hellwig <hch@infradead.org>
Cc: Jesse Brandeburg <jesse.brandeburg@intel.com>
Cc: Tony Nguyen <anthony.l.nguyen@intel.com>
Cc: "David S. Miller" <davem@davemloft.net>
Cc: Eric Dumazet <edumazet@google.com>
Cc: Paolo Abeni <pabeni@redhat.com>
Cc: James Smart <james.smart@broadcom.com>
Cc: Keith Busch <kbusch@kernel.org>
Cc: Jens Axboe <axboe@kernel.dk>
Cc: Sagi Grimberg <sagi@grimberg.me>
Cc: HighPoint Linux Team <linux@highpoint-tech.com>
Cc: "James E.J. Bottomley" <jejb@linux.ibm.com>
Cc: "Martin K. Petersen" <martin.petersen@oracle.com>
Cc: Kashyap Desai <kashyap.desai@broadcom.com>
Cc: Sumit Saxena <sumit.saxena@broadcom.com>
Cc: Shivasharan S <shivasharan.srikanteshwara@broadcom.com>
Cc: Don Brace <don.brace@microchip.com>
Cc: "Darrick J. Wong" <djwong@kernel.org>
Cc: Dave Chinner <dchinner@redhat.com>
Cc: Guo Xuenan <guoxuenan@huawei.com>
Cc: Gwan-gyeong Mun <gwan-gyeong.mun@intel.com>
Cc: Nick Desaulniers <ndesaulniers@google.com>
Cc: Daniel Latypov <dlatypov@google.com>
Cc: kernel test robot <lkp@intel.com>
Cc: intel-wired-lan@lists.osuosl.org
Cc: netdev@vger.kernel.org
Cc: linux-nvme@lists.infradead.org
Cc: linux-scsi@vger.kernel.org
Cc: megaraidlinux.pdl@broadcom.com
Cc: storagedev@microchip.com
Cc: linux-xfs@vger.kernel.org
Cc: linux-hardening@vger.kernel.org
Signed-off-by: Kees Cook <keescook@chromium.org>
Acked-by: Martin K. Petersen <martin.petersen@oracle.com>
Reviewed-by: Darrick J. Wong <djwong@kernel.org>
Reviewed-by: Gustavo A. R. Silva <gustavoars@kernel.org>
Reviewed-by: Christoph Hellwig <hch@lst.de>
Acked-by: Jakub Kicinski <kuba@kernel.org>
Reviewed-by: Alexander Lobakin <aleksander.lobakin@intel.com>
Link: https://lore.kernel.org/r/20230522211810.never.421-kees@kernel.org

+40 -25
+4 -5
drivers/net/ethernet/intel/ice/ice_ddp.h
··· 185 185 186 186 #define ICE_MAX_ENTRIES_IN_BUF(hd_sz, ent_sz) \ 187 187 ((ICE_PKG_BUF_SIZE - \ 188 - struct_size((struct ice_buf_hdr *)0, section_entry, 1) - (hd_sz)) / \ 188 + struct_size_t(struct ice_buf_hdr, section_entry, 1) - (hd_sz)) / \ 189 189 (ent_sz)) 190 190 191 191 /* ice package section IDs */ ··· 297 297 }; 298 298 299 299 #define ICE_MAX_LABELS_IN_BUF \ 300 - ICE_MAX_ENTRIES_IN_BUF(struct_size((struct ice_label_section *)0, \ 300 + ICE_MAX_ENTRIES_IN_BUF(struct_size_t(struct ice_label_section, \ 301 301 label, 1) - \ 302 302 sizeof(struct ice_label), \ 303 303 sizeof(struct ice_label)) ··· 352 352 }; 353 353 354 354 #define ICE_MAX_BST_TCAMS_IN_BUF \ 355 - ICE_MAX_ENTRIES_IN_BUF(struct_size((struct ice_boost_tcam_section *)0, \ 355 + ICE_MAX_ENTRIES_IN_BUF(struct_size_t(struct ice_boost_tcam_section, \ 356 356 tcam, 1) - \ 357 357 sizeof(struct ice_boost_tcam_entry), \ 358 358 sizeof(struct ice_boost_tcam_entry)) ··· 372 372 }; 373 373 374 374 #define ICE_MAX_MARKER_PTYPE_TCAMS_IN_BUF \ 375 - ICE_MAX_ENTRIES_IN_BUF( \ 376 - struct_size((struct ice_marker_ptype_tcam_section *)0, tcam, \ 375 + ICE_MAX_ENTRIES_IN_BUF(struct_size_t(struct ice_marker_ptype_tcam_section, tcam, \ 377 376 1) - \ 378 377 sizeof(struct ice_marker_ptype_tcam_entry), \ 379 378 sizeof(struct ice_marker_ptype_tcam_entry))
+4 -4
drivers/nvme/host/fc.c
··· 2917 2917 2918 2918 ret = nvme_alloc_io_tag_set(&ctrl->ctrl, &ctrl->tag_set, 2919 2919 &nvme_fc_mq_ops, 1, 2920 - struct_size((struct nvme_fcp_op_w_sgl *)NULL, priv, 2921 - ctrl->lport->ops->fcprqst_priv_sz)); 2920 + struct_size_t(struct nvme_fcp_op_w_sgl, priv, 2921 + ctrl->lport->ops->fcprqst_priv_sz)); 2922 2922 if (ret) 2923 2923 return ret; 2924 2924 ··· 3536 3536 3537 3537 ret = nvme_alloc_admin_tag_set(&ctrl->ctrl, &ctrl->admin_tag_set, 3538 3538 &nvme_fc_admin_mq_ops, 3539 - struct_size((struct nvme_fcp_op_w_sgl *)NULL, priv, 3540 - ctrl->lport->ops->fcprqst_priv_sz)); 3539 + struct_size_t(struct nvme_fcp_op_w_sgl, priv, 3540 + ctrl->lport->ops->fcprqst_priv_sz)); 3541 3541 if (ret) 3542 3542 goto fail_ctrl; 3543 3543
+2 -2
drivers/scsi/hptiop.c
··· 1394 1394 host->cmd_per_lun = le32_to_cpu(iop_config.max_requests); 1395 1395 host->max_cmd_len = 16; 1396 1396 1397 - req_size = struct_size((struct hpt_iop_request_scsi_command *)0, 1398 - sg_list, hba->max_sg_descriptors); 1397 + req_size = struct_size_t(struct hpt_iop_request_scsi_command, 1398 + sg_list, hba->max_sg_descriptors); 1399 1399 if ((req_size & 0x1f) != 0) 1400 1400 req_size = (req_size + 0x1f) & ~0x1f; 1401 1401
+6 -6
drivers/scsi/megaraid/megaraid_sas_base.c
··· 5153 5153 fusion->max_map_sz = ventura_map_sz; 5154 5154 } else { 5155 5155 fusion->old_map_sz = 5156 - struct_size((struct MR_FW_RAID_MAP *)0, ldSpanMap, 5157 - instance->fw_supported_vd_count); 5156 + struct_size_t(struct MR_FW_RAID_MAP, ldSpanMap, 5157 + instance->fw_supported_vd_count); 5158 5158 fusion->new_map_sz = sizeof(struct MR_FW_RAID_MAP_EXT); 5159 5159 5160 5160 fusion->max_map_sz = ··· 5789 5789 struct fusion_context *fusion = instance->ctrl_context; 5790 5790 size_t pd_seq_map_sz; 5791 5791 5792 - pd_seq_map_sz = struct_size((struct MR_PD_CFG_SEQ_NUM_SYNC *)0, seq, 5793 - MAX_PHYSICAL_DEVICES); 5792 + pd_seq_map_sz = struct_size_t(struct MR_PD_CFG_SEQ_NUM_SYNC, seq, 5793 + MAX_PHYSICAL_DEVICES); 5794 5794 5795 5795 instance->use_seqnum_jbod_fp = 5796 5796 instance->support_seqnum_jbod_fp; ··· 8033 8033 if (instance->adapter_type != MFI_SERIES) { 8034 8034 megasas_release_fusion(instance); 8035 8035 pd_seq_map_sz = 8036 - struct_size((struct MR_PD_CFG_SEQ_NUM_SYNC *)0, 8037 - seq, MAX_PHYSICAL_DEVICES); 8036 + struct_size_t(struct MR_PD_CFG_SEQ_NUM_SYNC, 8037 + seq, MAX_PHYSICAL_DEVICES); 8038 8038 for (i = 0; i < 2 ; i++) { 8039 8039 if (fusion->ld_map[i]) 8040 8040 dma_free_coherent(&instance->pdev->dev,
+3 -3
drivers/scsi/megaraid/megaraid_sas_fp.c
··· 326 326 else if (instance->supportmax256vd) 327 327 expected_size = sizeof(struct MR_FW_RAID_MAP_EXT); 328 328 else 329 - expected_size = struct_size((struct MR_FW_RAID_MAP *)0, 330 - ldSpanMap, 331 - le16_to_cpu(pDrvRaidMap->ldCount)); 329 + expected_size = struct_size_t(struct MR_FW_RAID_MAP, 330 + ldSpanMap, 331 + le16_to_cpu(pDrvRaidMap->ldCount)); 332 332 333 333 if (le32_to_cpu(pDrvRaidMap->totalSize) != expected_size) { 334 334 dev_dbg(&instance->pdev->dev, "megasas: map info structure size 0x%x",
+1 -1
drivers/scsi/smartpqi/smartpqi_init.c
··· 5015 5015 } 5016 5016 5017 5017 #define PQI_REPORT_EVENT_CONFIG_BUFFER_LENGTH \ 5018 - struct_size((struct pqi_event_config *)0, descriptors, PQI_MAX_EVENT_DESCRIPTORS) 5018 + struct_size_t(struct pqi_event_config, descriptors, PQI_MAX_EVENT_DESCRIPTORS) 5019 5019 5020 5020 static int pqi_configure_events(struct pqi_ctrl_info *ctrl_info, 5021 5021 bool enable_events)
+1 -1
fs/xfs/libxfs/xfs_btree.h
··· 301 301 static inline size_t 302 302 xfs_btree_cur_sizeof(unsigned int nlevels) 303 303 { 304 - return struct_size((struct xfs_btree_cur *)NULL, bc_levels, nlevels); 304 + return struct_size_t(struct xfs_btree_cur, bc_levels, nlevels); 305 305 } 306 306 307 307 /* cursor flags */
+1 -1
fs/xfs/scrub/btree.h
··· 60 60 static inline size_t 61 61 xchk_btree_sizeof(unsigned int nlevels) 62 62 { 63 - return struct_size((struct xchk_btree *)NULL, lastkey, nlevels - 1); 63 + return struct_size_t(struct xchk_btree, lastkey, nlevels - 1); 64 64 } 65 65 66 66 int xchk_btree(struct xfs_scrub *sc, struct xfs_btree_cur *cur,
+17 -1
include/linux/overflow.h
··· 283 283 * @member: Name of the array member. 284 284 * @count: Number of elements in the array. 285 285 * 286 - * Calculates size of memory needed for structure @p followed by an 286 + * Calculates size of memory needed for structure of @p followed by an 287 287 * array of @count number of @member elements. 288 288 * 289 289 * Return: number of bytes needed or SIZE_MAX on overflow. ··· 292 292 __builtin_choose_expr(__is_constexpr(count), \ 293 293 sizeof(*(p)) + flex_array_size(p, member, count), \ 294 294 size_add(sizeof(*(p)), flex_array_size(p, member, count))) 295 + 296 + /** 297 + * struct_size_t() - Calculate size of structure with trailing flexible array 298 + * @type: structure type name. 299 + * @member: Name of the array member. 300 + * @count: Number of elements in the array. 301 + * 302 + * Calculates size of memory needed for structure @type followed by an 303 + * array of @count number of @member elements. Prefer using struct_size() 304 + * when possible instead, to keep calculations associated with a specific 305 + * instance variable of type @type. 306 + * 307 + * Return: number of bytes needed or SIZE_MAX on overflow. 308 + */ 309 + #define struct_size_t(type, member, count) \ 310 + struct_size((type *)NULL, member, count) 295 311 296 312 #endif /* __LINUX_OVERFLOW_H */
+1 -1
lib/overflow_kunit.c
··· 649 649 static void overflow_size_helpers_test(struct kunit *test) 650 650 { 651 651 /* Make sure struct_size() can be used in a constant expression. */ 652 - u8 ce_array[struct_size((struct __test_flex_array *)0, data, 55)]; 652 + u8 ce_array[struct_size_t(struct __test_flex_array, data, 55)]; 653 653 struct __test_flex_array *obj; 654 654 int count = 0; 655 655 int var;