Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 10 Aug 2023 18:14:03 GMT
From:      John Baldwin <jhb@FreeBSD.org>
To:        src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org
Subject:   git: 9795f14ec40a - main - netlink: Align allocations on __max_align_t, not uint64_t.
Message-ID:  <202308101814.37AIE3gK010376@gitrepo.freebsd.org>

next in thread | raw e-mail | index | archive | help
The branch main has been updated by jhb:

URL: https://cgit.FreeBSD.org/src/commit/?id=9795f14ec40acbab93aabedf05d70e0256ce9bf5

commit 9795f14ec40acbab93aabedf05d70e0256ce9bf5
Author:     John Baldwin <jhb@FreeBSD.org>
AuthorDate: 2023-08-10 18:12:52 +0000
Commit:     John Baldwin <jhb@FreeBSD.org>
CommitDate: 2023-08-10 18:12:52 +0000

    netlink: Align allocations on __max_align_t, not uint64_t.
    
    uint64_t is not sufficient alignment for allocators on all platforms.
    On a CHERI platform pointers require 16 byte alignment, but also if a
    type contained a uint128_t or long double it would not be aligned
    correctly either.  C11 added max_align_t precisely to provide a
    portable type for allocators to use.
    
    Reviewed by:    melifaro
    Obtained from:  CheriBSD
    Sponsored by:   DARPA
    Differential Revision:  https://reviews.freebsd.org/D41301
---
 sys/netlink/netlink_message_parser.h | 4 ++--
 sys/netlink/netlink_snl.h            | 5 +++--
 2 files changed, 5 insertions(+), 4 deletions(-)

diff --git a/sys/netlink/netlink_message_parser.h b/sys/netlink/netlink_message_parser.h
index 0934057ac49f..0242177fdd26 100644
--- a/sys/netlink/netlink_message_parser.h
+++ b/sys/netlink/netlink_message_parser.h
@@ -41,12 +41,12 @@ struct linear_buffer {
 	char		*base;	/* Base allocated memory pointer */
 	uint32_t	offset;	/* Currently used offset */
 	uint32_t	size;	/* Total buffer size */
-};
+} __aligned(_Alignof(__max_align_t));
 
 static inline void *
 lb_alloc(struct linear_buffer *lb, int len)
 {
-	len = roundup2(len, sizeof(uint64_t));
+	len = roundup2(len, _Alignof(__max_align_t));
 	if (lb->offset + len > lb->size)
 		return (NULL);
 	void *data = (void *)(lb->base + lb->offset);
diff --git a/sys/netlink/netlink_snl.h b/sys/netlink/netlink_snl.h
index 0292725bd135..8bb7b076b7b7 100644
--- a/sys/netlink/netlink_snl.h
+++ b/sys/netlink/netlink_snl.h
@@ -33,6 +33,7 @@
 
 #include <assert.h>
 #include <errno.h>
+#include <stdalign.h>
 #include <stddef.h>
 #include <stdbool.h>
 #include <stdint.h>
@@ -74,7 +75,7 @@ struct linear_buffer {
 	uint32_t		offset;	/* Currently used offset */
 	uint32_t		size;	/* Total buffer size */
 	struct linear_buffer	*next;	/* Buffer chaining */
-};
+} __aligned(alignof(__max_align_t));
 
 static inline struct linear_buffer *
 lb_init(uint32_t size)
@@ -98,7 +99,7 @@ lb_free(struct linear_buffer *lb)
 static inline char *
 lb_allocz(struct linear_buffer *lb, int len)
 {
-	len = roundup2(len, sizeof(uint64_t));
+	len = roundup2(len, alignof(__max_align_t));
 	if (lb->offset + len > lb->size)
 		return (NULL);
 	void *data = (void *)(lb->base + lb->offset);



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?202308101814.37AIE3gK010376>