Combine allocations with a flexible array member to simplify the whole thing.
Add __counted_by for extra runtime analysis. Move assignment of counting variable to after allocation as this is already done by kzalloc_flex and will be done by its devm variant when introduced for GCC 15 and above. Signed-off-by: Rosen Penev <[email protected]> --- drivers/platform/x86/intel/vsec_tpmi.c | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/drivers/platform/x86/intel/vsec_tpmi.c b/drivers/platform/x86/intel/vsec_tpmi.c index 7fc6ff8d1040..79c0cbea2dee 100644 --- a/drivers/platform/x86/intel/vsec_tpmi.c +++ b/drivers/platform/x86/intel/vsec_tpmi.c @@ -113,13 +113,13 @@ struct intel_tpmi_pm_feature { * Stores the information for all TPMI devices enumerated from a single PCI device. */ struct intel_tpmi_info { - struct intel_tpmi_pm_feature *tpmi_features; struct intel_vsec_device *vsec_dev; int feature_count; u64 pfs_start; struct oobmsm_plat_info plat_info; void __iomem *tpmi_control_mem; struct dentry *dbgfs_dir; + struct intel_tpmi_pm_feature tpmi_features[] __counted_by(feature_count); }; /** @@ -751,20 +751,16 @@ static int intel_vsec_tpmi_init(struct auxiliary_device *auxdev) u64 pfs_start = 0; int ret, i; - tpmi_info = devm_kzalloc(&auxdev->dev, sizeof(*tpmi_info), GFP_KERNEL); + tpmi_info = devm_kzalloc(&auxdev->dev, + struct_size(tpmi_info, tpmi_features, vsec_dev->num_resources), + GFP_KERNEL); if (!tpmi_info) return -ENOMEM; - tpmi_info->vsec_dev = vsec_dev; tpmi_info->feature_count = vsec_dev->num_resources; + tpmi_info->vsec_dev = vsec_dev; tpmi_info->plat_info.bus_number = pci_dev->bus->number; - tpmi_info->tpmi_features = devm_kcalloc(&auxdev->dev, vsec_dev->num_resources, - sizeof(*tpmi_info->tpmi_features), - GFP_KERNEL); - if (!tpmi_info->tpmi_features) - return -ENOMEM; - for (i = 0; i < vsec_dev->num_resources; i++) { struct intel_tpmi_pm_feature *pfs; struct resource *res; -- 2.54.0

