Skip to content

Commit f6d7f05

Browse files
andy-shevbroonie
authored andcommitted
spi: Don't use flexible array in struct spi_message definition
The struct spi_message can be embedded into another structures. With that the flexible array might be problematic as sparse complains about it, although there is no real issue in the code because when the message is embedded it doesn't use flexible array member. That memeber is a private to spi_message_alloc() API, so move it to that API in a form of an inherited data type. Reported-by: Marc Kleine-Budde <mkl@pengutronix.de> Fixes: 75e308f ("spi: Use struct_size() helper")) Closes: https://lore.kernel.org/r/20231009-onshore-underage-c58415adfd92-mkl@pengutronix.de Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com> Reviewed-by: Marc Kleine-Budde <mkl@pengutronix.de> Link: https://lore.kernel.org/r/20231010163100.89734-1-andriy.shevchenko@linux.intel.com Signed-off-by: Mark Brown <broonie@kernel.org>
1 parent 8097dbd commit f6d7f05

File tree

1 file changed

+13
-14
lines changed

1 file changed

+13
-14
lines changed

include/linux/spi/spi.h

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1086,8 +1086,6 @@ struct spi_transfer {
10861086
* @state: for use by whichever driver currently owns the message
10871087
* @resources: for resource management when the SPI message is processed
10881088
* @prepared: spi_prepare_message was called for the this message
1089-
* @t: for use with spi_message_alloc() when message and transfers have
1090-
* been allocated together
10911089
*
10921090
* A @spi_message is used to execute an atomic sequence of data transfers,
10931091
* each represented by a struct spi_transfer. The sequence is "atomic"
@@ -1142,9 +1140,6 @@ struct spi_message {
11421140

11431141
/* List of spi_res resources when the SPI message is processed */
11441142
struct list_head resources;
1145-
1146-
/* For embedding transfers into the memory of the message */
1147-
struct spi_transfer t[];
11481143
};
11491144

11501145
static inline void spi_message_init_no_memset(struct spi_message *m)
@@ -1203,17 +1198,21 @@ struct spi_transfer *xfers, unsigned int num_xfers)
12031198
*/
12041199
static inline struct spi_message *spi_message_alloc(unsigned ntrans, gfp_t flags)
12051200
{
1206-
struct spi_message *m;
1201+
struct spi_message_with_transfers {
1202+
struct spi_message m;
1203+
struct spi_transfer t[];
1204+
} *mwt;
1205+
unsigned i;
1206+
1207+
mwt = kzalloc(struct_size(mwt, t, ntrans), flags);
1208+
if (!mwt)
1209+
return NULL;
12071210

1208-
m = kzalloc(struct_size(m, t, ntrans), flags);
1209-
if (m) {
1210-
unsigned i;
1211+
spi_message_init_no_memset(&mwt->m);
1212+
for (i = 0; i < ntrans; i++)
1213+
spi_message_add_tail(&mwt->t[i], &mwt->m);
12111214

1212-
spi_message_init_no_memset(m);
1213-
for (i = 0; i < ntrans; i++)
1214-
spi_message_add_tail(&m->t[i], m);
1215-
}
1216-
return m;
1215+
return &mwt->m;
12171216
}
12181217

12191218
static inline void spi_message_free(struct spi_message *m)

0 commit comments

Comments
 (0)