OSDN Git Service

ice: Replace one-element array with flexible-array member
authorGustavo A. R. Silva <gustavoars@kernel.org>
Tue, 29 Sep 2020 19:01:56 +0000 (14:01 -0500)
committerTony Nguyen <anthony.l.nguyen@intel.com>
Fri, 5 Feb 2021 19:44:32 +0000 (11:44 -0800)
There is a regular need in the kernel to provide a way to declare having
a dynamically sized set of trailing elements in a structure. Kernel code
should always use “flexible array members”[1] for these cases. The older
style of one-element or zero-length arrays should no longer be used[2].

Refactor the code according to the use of a flexible-array member in
struct ice_res_tracker, instead of a one-element array and use the
struct_size() helper to calculate the size for the allocations.

Also, notice that the code below suggests that, currently, two too many
bytes are being allocated with devm_kzalloc(), as the total number of
entries (pf->irq_tracker->num_entries) for pf->irq_tracker->list[] is
_vectors_ and sizeof(*pf->irq_tracker) also includes the size of the
one-element array _list_ in struct ice_res_tracker.

drivers/net/ethernet/intel/ice/ice_main.c:3511:
3511         /* populate SW interrupts pool with number of OS granted IRQs. */
3512         pf->num_avail_sw_msix = (u16)vectors;
3513         pf->irq_tracker->num_entries = (u16)vectors;
3514         pf->irq_tracker->end = pf->irq_tracker->num_entries;

With this change, the right amount of dynamic memory is now allocated
because, contrary to one-element arrays which occupy at least as much
space as a single object of the type, flexible-array members don't
occupy such space in the containing structure.

[1] https://en.wikipedia.org/wiki/Flexible_array_member
[2] https://www.kernel.org/doc/html/v5.9-rc1/process/deprecated.html#zero-length-and-one-element-arrays

Built-tested-by: kernel test robot <lkp@intel.com>
Signed-off-by: Gustavo A. R. Silva <gustavoars@kernel.org>
Tested-by: Tony Brelinski <tonyx.brelinski@intel.com>
Signed-off-by: Tony Nguyen <anthony.l.nguyen@intel.com>
drivers/net/ethernet/intel/ice/ice.h
drivers/net/ethernet/intel/ice/ice_main.c

index fa1e128..fca428c 100644 (file)
@@ -166,7 +166,7 @@ struct ice_tc_cfg {
 struct ice_res_tracker {
        u16 num_entries;
        u16 end;
-       u16 list[1];
+       u16 list[];
 };
 
 struct ice_qs_cfg {
index 643fbc8..f617759 100644 (file)
@@ -3495,9 +3495,9 @@ static int ice_init_interrupt_scheme(struct ice_pf *pf)
                return vectors;
 
        /* set up vector assignment tracking */
-       pf->irq_tracker =
-               devm_kzalloc(ice_pf_to_dev(pf), sizeof(*pf->irq_tracker) +
-                            (sizeof(u16) * vectors), GFP_KERNEL);
+       pf->irq_tracker = devm_kzalloc(ice_pf_to_dev(pf),
+                                      struct_size(pf->irq_tracker, list, vectors),
+                                      GFP_KERNEL);
        if (!pf->irq_tracker) {
                ice_dis_msix(pf);
                return -ENOMEM;