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

hpilo: Replace one-element array with flexible-array member

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].

For this particular case, it is important to notice that the cachelines
change from 7 to 6 after the flexible-array conversion:

$ pahole -C 'fifo' drivers/misc/hpilo.o
struct fifo {
u64 nrents; /* 0 8 */
u64 imask; /* 8 8 */
u64 merge; /* 16 8 */
u64 reset; /* 24 8 */
u8 pad_0[96]; /* 32 96 */
/* --- cacheline 2 boundary (128 bytes) --- */
u64 head; /* 128 8 */
u8 pad_1[120]; /* 136 120 */
/* --- cacheline 4 boundary (256 bytes) --- */
u64 tail; /* 256 8 */
u8 pad_2[120]; /* 264 120 */
/* --- cacheline 6 boundary (384 bytes) --- */
u64 fifobar[1]; /* 384 8 */

/* size: 392, cachelines: 7, members: 10 */
/* last cacheline: 8 bytes */
};

$ pahole -C 'fifo' drivers/misc/hpilo.o
struct fifo {
u64 nrents; /* 0 8 */
u64 imask; /* 8 8 */
u64 merge; /* 16 8 */
u64 reset; /* 24 8 */
u8 pad_0[96]; /* 32 96 */
/* --- cacheline 2 boundary (128 bytes) --- */
u64 head; /* 128 8 */
u8 pad_1[120]; /* 136 120 */
/* --- cacheline 4 boundary (256 bytes) --- */
u64 tail; /* 256 8 */
u8 pad_2[120]; /* 264 120 */
/* --- cacheline 6 boundary (384 bytes) --- */
u64 fifobar[]; /* 384 0 */

/* size: 384, cachelines: 6, members: 10 */
};

Lastly, remove unnecessary parentheses in fifo_sz() and fix the following
checkpatch.pl warning for the whole fifo structure:

WARNING: please, no spaces at the start of a line

[1] https://en.wikipedia.org/wiki/Flexible_array_member
[2] https://github.com/KSPP/linux/issues/79

Tested-by: kernel test robot <lkp@intel.com>
Link: https://github.com/GustavoARSilva/linux-hardening/blob/master/cii/kernel-ci/hpilo-20200714.md
Signed-off-by: Gustavo A. R. Silva <gustavoars@kernel.org>
Link: https://lore.kernel.org/r/20200714154449.GA26153@embeddedor
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

authored by

Gustavo A. R. Silva and committed by
Greg Kroah-Hartman
fadbfc38 3a12c2b5

+12 -12
+1 -1
drivers/misc/hpilo.c
··· 207 207 static inline int fifo_sz(int nr_entry) 208 208 { 209 209 /* size of a fifo is determined by the number of entries it contains */ 210 - return (nr_entry * sizeof(u64)) + FIFOHANDLESIZE; 210 + return nr_entry * sizeof(u64) + FIFOHANDLESIZE; 211 211 } 212 212 213 213 static void fifo_setup(void *base_addr, int nr_entry)
+11 -11
drivers/misc/hpilo.h
··· 160 160 #define ILO_START_ALIGN 4096 161 161 #define ILO_CACHE_SZ 128 162 162 struct fifo { 163 - u64 nrents; /* user requested number of fifo entries */ 164 - u64 imask; /* mask to extract valid fifo index */ 165 - u64 merge; /* O/C bits to merge in during enqueue operation */ 166 - u64 reset; /* set to non-zero when the target device resets */ 167 - u8 pad_0[ILO_CACHE_SZ - (sizeof(u64) * 4)]; 163 + u64 nrents; /* user requested number of fifo entries */ 164 + u64 imask; /* mask to extract valid fifo index */ 165 + u64 merge; /* O/C bits to merge in during enqueue operation */ 166 + u64 reset; /* set to non-zero when the target device resets */ 167 + u8 pad_0[ILO_CACHE_SZ - (sizeof(u64) * 4)]; 168 168 169 - u64 head; 170 - u8 pad_1[ILO_CACHE_SZ - (sizeof(u64))]; 169 + u64 head; 170 + u8 pad_1[ILO_CACHE_SZ - (sizeof(u64))]; 171 171 172 - u64 tail; 173 - u8 pad_2[ILO_CACHE_SZ - (sizeof(u64))]; 172 + u64 tail; 173 + u8 pad_2[ILO_CACHE_SZ - (sizeof(u64))]; 174 174 175 - u64 fifobar[1]; 175 + u64 fifobar[]; 176 176 }; 177 177 178 178 /* convert between struct fifo, and the fifobar, which is saved in the ccb */ 179 - #define FIFOHANDLESIZE (sizeof(struct fifo) - sizeof(u64)) 179 + #define FIFOHANDLESIZE (sizeof(struct fifo)) 180 180 #define FIFOBARTOHANDLE(_fifo) \ 181 181 ((struct fifo *)(((char *)(_fifo)) - FIFOHANDLESIZE)) 182 182