@@ -45,7 +45,16 @@ struct llext_test {
4545 size_t buf_len ;
4646
4747 bool kernel_only ;
48- void (* perm_setup )(struct k_thread * llext_thread );
48+
49+ /*
50+ * Optional callbacks
51+ */
52+
53+ /* Called in kernel context before each test starts */
54+ void (* test_setup )(struct llext * ext , struct k_thread * llext_thread );
55+
56+ /* Called in kernel context after each test completes */
57+ void (* test_cleanup )(struct llext * ext );
4958};
5059
5160
@@ -93,16 +102,20 @@ K_THREAD_STACK_DEFINE(my_thread_stack, MY_THREAD_STACK_SIZE);
93102EXPORT_SYMBOL (my_thread_stack );
94103
95104#ifdef CONFIG_USERSPACE
96- /* Allow the user space test thread to access global objects */
97- static void threads_objects_perm_setup (struct k_thread * llext_thread )
105+ /* Allow the test threads to access global objects.
106+ * Note: Permissions on objects used in the test by this thread are initialized
107+ * even in supervisor mode, so that user mode descendant threads can inherit
108+ * these permissions.
109+ */
110+ static void threads_objects_test_setup (struct llext * , struct k_thread * llext_thread )
98111{
99112 k_object_access_grant (& my_sem , llext_thread );
100113 k_object_access_grant (& my_thread , llext_thread );
101114 k_object_access_grant (& my_thread_stack , llext_thread );
102115}
103116#else
104117/* No need to set up permissions for supervisor mode */
105- #define threads_objects_perm_setup NULL
118+ #define threads_objects_test_setup NULL
106119#endif /* CONFIG_USERSPACE */
107120
108121void load_call_unload (const struct llext_test * test_case )
@@ -155,17 +168,17 @@ void load_call_unload(const struct llext_test *test_case)
155168
156169 k_mem_domain_add_thread (& domain , & llext_thread );
157170
158- /* Even in supervisor mode, initialize permissions on objects used in
159- * the test by this thread, so that user mode descendant threads can
160- * inherit these permissions.
161- */
162- if (test_case -> perm_setup ) {
163- test_case -> perm_setup (& llext_thread );
171+ if (test_case -> test_setup ) {
172+ test_case -> test_setup (ext , & llext_thread );
164173 }
165174
166175 k_thread_start (& llext_thread );
167176 k_thread_join (& llext_thread , K_FOREVER );
168177
178+ if (test_case -> test_cleanup ) {
179+ test_case -> test_cleanup (ext );
180+ }
181+
169182 /* Some extensions may wish to be tried from the context
170183 * of a userspace thread along with the usual supervisor context
171184 * tried above.
@@ -178,17 +191,29 @@ void load_call_unload(const struct llext_test *test_case)
178191
179192 k_mem_domain_add_thread (& domain , & llext_thread );
180193
181- if (test_case -> perm_setup ) {
182- test_case -> perm_setup ( & llext_thread );
194+ if (test_case -> test_setup ) {
195+ test_case -> test_setup ( ext , & llext_thread );
183196 }
184197
185198 k_thread_start (& llext_thread );
186199 k_thread_join (& llext_thread , K_FOREVER );
200+
201+ if (test_case -> test_cleanup ) {
202+ test_case -> test_cleanup (ext );
203+ }
187204 }
188205
189206#else /* CONFIG_USERSPACE */
207+ if (test_case -> test_setup ) {
208+ test_case -> test_setup (ext , NULL );
209+ }
210+
190211 zassert_ok (llext_call_fn (ext , "test_entry" ),
191212 "test_entry call should succeed" );
213+
214+ if (test_case -> test_cleanup ) {
215+ test_case -> test_cleanup (ext );
216+ }
192217#endif /* CONFIG_USERSPACE */
193218
194219 llext_unload (& ext );
@@ -251,7 +276,7 @@ static LLEXT_CONST uint8_t threads_kernel_objects_ext[] ELF_ALIGN = {
251276 #include "threads_kernel_objects.inc"
252277};
253278LLEXT_LOAD_UNLOAD (threads_kernel_objects ,
254- .perm_setup = threads_objects_perm_setup ,
279+ .test_setup = threads_objects_test_setup ,
255280)
256281#endif
257282
0 commit comments