Skip to content

Commit b6dd960

Browse files
dcpleungnashif
authored andcommitted
kernel: userspace: fix dynamic kernel object alignment
Previous commit 55350a9 fixing address-of-packed-mem warnings uncovered an issue with the alignment of dynamic kernel objects. On 64-bit platforms, the alignment is 16 bytes instead of 4/8 bytes (as in pointer, void *). This changes the function of mapping between kernel object types and alignments to use the dynamic object struct as basis for alignment instead of simply using pointers. This also uncomments the assertion added in the previous commit 55350a9 so that we can keep an eye on the alignment in the future. Note that the assertion is moved after checking if the incoming kernel object is dynamically allocated. Static kernel objects are not subjected to this alignment requirement. Fixes #41062 Signed-off-by: Daniel Leung <[email protected]>
1 parent 1e32d44 commit b6dd960

File tree

1 file changed

+8
-7
lines changed

1 file changed

+8
-7
lines changed

kernel/userspace.c

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -190,11 +190,11 @@ static size_t obj_align_get(enum k_objects otype)
190190
#ifdef ARCH_DYMANIC_OBJ_K_THREAD_ALIGNMENT
191191
ret = ARCH_DYMANIC_OBJ_K_THREAD_ALIGNMENT;
192192
#else
193-
ret = sizeof(void *);
193+
ret = __alignof(struct dyn_obj);
194194
#endif
195195
break;
196196
default:
197-
ret = sizeof(void *);
197+
ret = __alignof(struct dyn_obj);
198198
break;
199199
}
200200

@@ -466,15 +466,16 @@ static void unref_check(struct z_object *ko, uintptr_t index)
466466
sys_bitfield_clear_bit((mem_addr_t)&ko->perms, index);
467467

468468
#ifdef CONFIG_DYNAMIC_OBJECTS
469+
if ((ko->flags & K_OBJ_FLAG_ALLOC) == 0U) {
470+
/* skip unref check for static kernel object */
471+
goto out;
472+
}
473+
469474
void *vko = ko;
470475

471476
struct dyn_obj *dyn = CONTAINER_OF(vko, struct dyn_obj, kobj);
472-
/* TODO: check why this assert hits */
473-
/*__ASSERT(IS_PTR_ALIGNED(dyn, struct dyn_obj), "unaligned z_object");*/
474477

475-
if ((ko->flags & K_OBJ_FLAG_ALLOC) == 0U) {
476-
goto out;
477-
}
478+
__ASSERT(IS_PTR_ALIGNED(dyn, struct dyn_obj), "unaligned z_object");
478479

479480
for (int i = 0; i < CONFIG_MAX_THREAD_BYTES; i++) {
480481
if (ko->perms[i] != 0U) {

0 commit comments

Comments
 (0)