Skip to contentSkip to conversation

tty: tty_buffer: Avoid hundreds of -Wflex-array-member-not-at-end warnings

PATCH Superseded Gustavo A. R. Silva 1 participant 1 message Feb 5, 2025 TTY layer and serial drivers

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 tty: tty_buffer: Avoid hundreds of -Wflex-array-member-not-at-end warnings

-Wflex-array-member-not-at-end was introduced in GCC-14, and we are getting ready to enable it, globally.

So, in order to avoid ending up with a flexible-array member in the middle of other structs, we use the struct_group_tagged() helper to create a new tagged struct tty_buffer_hdr. This structure groups together all the members of the flexible struct tty_buffer except the flexible array.

As a result, the array is effectively separated from the rest of the members without modifying the memory layout of the flexible structure. We then change the type of the middle struct member currently causing trouble from struct tty_buffer to struct tty_buffer_hdr.

We also want to ensure that when new members need to be added to the flexible structure, they are always included within the newly created tagged struct. For this, we use static_assert(). This ensures that the memory layout for both the flexible structure and the new tagged struct is the same after any changes.

This approach avoids having to implement struct tty_buffer_hdr as a completely separate structure, thus preventing having to maintain two independent but basically identical structures, closing the door to potential bugs in the future.

We also use container_of() whenever we need to retrieve a pointer to the flexible structure, through which we can access the flexible-array member, if necessary.

So, with these changes, fix 384 of the following warnings:
include/linux/tty_buffer.h:40:27: warning: structure containing a flexible array member is not at the end of another structure [-Wflex-array-member-not-at-end]

---
Changes in v2:
- Fix a space at the beginning of the line issue, and adjust the identation of a code coment.

v1:
- Link: https://lore.kernel.org/linux-hardening/Z6L1XwE-WEzcGFwv@kspp/

 drivers/tty/tty_buffer.c   | 18 ++++++++++------
 include/linux/tty_buffer.h | 43 +++++++++++++++++++++-----------------
 2 files changed, 36 insertions(+), 25 deletions(-)
drivers/tty/tty_buffer.c +12 -6
index 79f0ff94ce00..f4e7520df7b1 100644
@@ -120,11 +120,14 @@ static void tty_buffer_reset(struct tty_buffer *p, size_t size)
 void tty_buffer_free_all(struct tty_port *port)
 {
 	struct tty_bufhead *buf = &port->buf;
+	struct tty_buffer *buf_sentinel;
 	struct tty_buffer *p, *next;
 	struct llist_node *llist;
 	unsigned int freed = 0;
 	int still_used;
 
+	buf_sentinel = container_of(&buf->sentinel, struct tty_buffer, __hdr);
+
 	while ((p = buf->head) != NULL) {
 		buf->head = p->next;
 		freed += p->size;
@@ -135,9 +138,9 @@ void tty_buffer_free_all(struct tty_port *port)
 	llist_for_each_entry_safe(p, next, llist, free)
 		kfree(p);
 
-	tty_buffer_reset(&buf->sentinel, 0);
-	buf->head = &buf->sentinel;
-	buf->tail = &buf->sentinel;
+	tty_buffer_reset(buf_sentinel, 0);
+	buf->head = buf_sentinel;
+	buf->tail = buf_sentinel;
 
 	still_used = atomic_xchg(&buf->mem_used, 0);
 	WARN(still_used != freed, "we still have not freed %d bytes!",
@@ -576,11 +579,14 @@ int tty_insert_flip_string_and_push_buffer(struct tty_port *port,
 void tty_buffer_init(struct tty_port *port)
 {
 	struct tty_bufhead *buf = &port->buf;
+	struct tty_buffer *buf_sentinel;
+
+	buf_sentinel = container_of(&buf->sentinel, struct tty_buffer, __hdr);
 
 	mutex_init(&buf->lock);
-	tty_buffer_reset(&buf->sentinel, 0);
-	buf->head = &buf->sentinel;
-	buf->tail = &buf->sentinel;
+	tty_buffer_reset(buf_sentinel, 0);
+	buf->head = buf_sentinel;
+	buf->tail = buf_sentinel;
 	init_llist_head(&buf->free);
 	atomic_set(&buf->mem_used, 0);
 	atomic_set(&buf->priority, 0);
include/linux/tty_buffer.h +24 -19

58 lines not on this page · raw message

Signature · 1 line
2.43.0