diff --git a/.vscode/settings.json b/.vscode/settings.json index 3228639..c850152 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -122,7 +122,8 @@ "scheduler_options.h": "c", "route_config_parse.h": "c", "route.h": "c", - "pool.h": "c" + "pool.h": "c", + "local_cleanup_queue.h": "c" }, "files.exclude": { "**/.git": true, diff --git a/runtime/include/local_cleanup_queue.h b/runtime/include/local_cleanup_queue.h new file mode 100644 index 0000000..9df6107 --- /dev/null +++ b/runtime/include/local_cleanup_queue.h @@ -0,0 +1,7 @@ +#pragma once + +#include "sandbox_types.h" + +void local_cleanup_queue_add(struct sandbox *sandbox); +void local_cleanup_queue_free(); +void local_cleanup_queue_initialize(); diff --git a/runtime/include/local_completion_queue.h b/runtime/include/local_completion_queue.h deleted file mode 100644 index ed86971..0000000 --- a/runtime/include/local_completion_queue.h +++ /dev/null @@ -1,8 +0,0 @@ -#pragma once - -#include "ps_list.h" -#include "sandbox_types.h" - -void local_completion_queue_add(struct sandbox *sandbox); -void local_completion_queue_free(); -void local_completion_queue_initialize(); diff --git a/runtime/include/sandbox_set_as_complete.h b/runtime/include/sandbox_set_as_complete.h index ae25ff6..3741592 100644 --- a/runtime/include/sandbox_set_as_complete.h +++ b/runtime/include/sandbox_set_as_complete.h @@ -5,7 +5,6 @@ #include "arch/getcycles.h" #include "panic.h" -#include "local_completion_queue.h" #include "sandbox_functions.h" #include "sandbox_perf_log.h" #include "sandbox_state.h" @@ -16,7 +15,6 @@ /** * Transitions a sandbox from the SANDBOX_RETURNED state to the SANDBOX_COMPLETE state. - * Adds the sandbox to the completion queue * @param sandbox * @param last_state the state the sandbox is transitioning from. This is expressed as a constant to * enable the compiler to perform constant propagation optimizations. @@ -61,7 +59,7 @@ sandbox_set_as_complete(struct sandbox *sandbox, sandbox_state_t last_state) sandbox_state_transition_from_hook(sandbox, last_state); sandbox_state_transition_to_hook(sandbox, SANDBOX_COMPLETE); - /* Does not add to completion queue until in cooperative scheduler */ + /* Does not add to cleanup queue until in cooperative scheduler */ } static inline void diff --git a/runtime/include/sandbox_set_as_error.h b/runtime/include/sandbox_set_as_error.h index 35b6be9..13b3ecf 100644 --- a/runtime/include/sandbox_set_as_error.h +++ b/runtime/include/sandbox_set_as_error.h @@ -18,7 +18,7 @@ /** * Transitions a sandbox to the SANDBOX_ERROR state. * This can occur during initialization or execution - * Unmaps linear memory, removes from the runqueue (if on it), and adds to the completion queue + * Unmaps linear memory, removes from the runqueue (if on it) * Because the stack is still in use, freeing the stack is deferred until later * * @param sandbox the sandbox erroring out @@ -72,7 +72,7 @@ sandbox_set_as_error(struct sandbox *sandbox, sandbox_state_t last_state) sandbox_state_transition_from_hook(sandbox, last_state); sandbox_state_transition_to_hook(sandbox, SANDBOX_ERROR); - /* Does not add to completion queue until in cooperative scheduler */ + /* Does not add to cleanup queue until in cooperative scheduler */ } static inline void diff --git a/runtime/include/scheduler.h b/runtime/include/scheduler.h index 33d13ed..7c3caf8 100644 --- a/runtime/include/scheduler.h +++ b/runtime/include/scheduler.h @@ -11,6 +11,7 @@ #include "local_runqueue.h" #include "local_runqueue_minheap.h" #include "local_runqueue_list.h" +#include "local_cleanup_queue.h" #include "panic.h" #include "sandbox_functions.h" #include "sandbox_types.h" @@ -319,20 +320,20 @@ scheduler_idle_loop() scheduler_cooperative_switch_to(&worker_thread_base_context, next_sandbox); } - /* Clear the completion queue */ - local_completion_queue_free(); + /* Clear the cleanup queue */ + local_cleanup_queue_free(); } } /** * @brief Used to cooperative switch sandboxes when a sandbox sleeps or exits * Because of use-after-free bugs that interfere with our loggers, when a sandbox exits and switches away never to - * return, the boolean add_to_completion_queue needs to be set to true. Otherwise, we will leak sandboxes. - * @param add_to_completion_queue - Indicates that the sandbox should be added to the completion queue before switching + * return, the boolean add_to_cleanup_queue needs to be set to true. Otherwise, we will leak sandboxes. + * @param add_to_cleanup_queue - Indicates that the sandbox should be added to the cleanup queue before switching * away */ static inline void -scheduler_cooperative_sched(bool add_to_completion_queue) +scheduler_cooperative_sched(bool add_to_cleanup_queue) { struct sandbox *exiting_sandbox = current_sandbox_get(); assert(exiting_sandbox != NULL); @@ -353,8 +354,8 @@ scheduler_cooperative_sched(bool add_to_completion_queue) /* Try to wakeup sleeping sandboxes */ scheduler_execute_epoll_loop(); - /* We have not added ourself to the completion queue, so we can free */ - local_completion_queue_free(); + /* We have not added ourself to the cleanup queue, so we can free */ + local_cleanup_queue_free(); /* Switch to a sandbox if one is ready to run */ struct sandbox *next_sandbox = scheduler_get_next(); @@ -371,7 +372,7 @@ scheduler_cooperative_sched(bool add_to_completion_queue) // Write back global at idx 0 wasm_globals_set_i64(&exiting_sandbox->globals, 0, sledge_abi__current_wasm_module_instance.abi.wasmg_0, true); - if (add_to_completion_queue) local_completion_queue_add(exiting_sandbox); + if (add_to_cleanup_queue) local_cleanup_queue_add(exiting_sandbox); /* Do not touch sandbox struct after this point! */ if (next_sandbox != NULL) { diff --git a/runtime/src/local_cleanup_queue.c b/runtime/src/local_cleanup_queue.c new file mode 100644 index 0000000..1587c5b --- /dev/null +++ b/runtime/src/local_cleanup_queue.c @@ -0,0 +1,51 @@ +#include + +#include "local_cleanup_queue.h" +#include "sandbox_functions.h" + +/* Must be the same alignment as sandbox structs because of how the ps_list macros work */ +thread_local static struct ps_list_head local_cleanup_queue PAGE_ALIGNED; + + +void +local_cleanup_queue_initialize() +{ + ps_list_head_init(&local_cleanup_queue); +} + +static inline bool +local_cleanup_queue_is_empty() +{ + return ps_list_head_empty(&local_cleanup_queue); +} + +/** + * Adds sandbox to the cleanup queue + * @param sandbox to add to cleanup queue + */ +void +local_cleanup_queue_add(struct sandbox *sandbox) +{ + assert(sandbox); + assert(ps_list_singleton_d(sandbox)); + ps_list_head_append_d(&local_cleanup_queue, sandbox); + assert(!local_cleanup_queue_is_empty()); +} + + +/** + * @brief Frees all sandboxes in the thread local cleanup queue + * @return void + */ +void +local_cleanup_queue_free() +{ + struct sandbox *sandbox_iterator = NULL; + struct sandbox *buffer = NULL; + + ps_list_foreach_del_d(&local_cleanup_queue, sandbox_iterator, buffer) + { + ps_list_rem_d(sandbox_iterator); + sandbox_free(sandbox_iterator); + } +} diff --git a/runtime/src/local_completion_queue.c b/runtime/src/local_completion_queue.c deleted file mode 100644 index 1c6df18..0000000 --- a/runtime/src/local_completion_queue.c +++ /dev/null @@ -1,51 +0,0 @@ -#include - -#include "local_completion_queue.h" -#include "sandbox_functions.h" - -/* Must be the same alignment as sandbox structs because of how the ps_list macros work */ -thread_local static struct ps_list_head local_completion_queue PAGE_ALIGNED; - - -void -local_completion_queue_initialize() -{ - ps_list_head_init(&local_completion_queue); -} - -static inline bool -local_completion_queue_is_empty() -{ - return ps_list_head_empty(&local_completion_queue); -} - -/** - * Adds sandbox to the completion queue - * @param sandbox to add to completion queue - */ -void -local_completion_queue_add(struct sandbox *sandbox) -{ - assert(sandbox); - assert(ps_list_singleton_d(sandbox)); - ps_list_head_append_d(&local_completion_queue, sandbox); - assert(!local_completion_queue_is_empty()); -} - - -/** - * @brief Frees all sandboxes in the thread local completion queue - * @return void - */ -void -local_completion_queue_free() -{ - struct sandbox *sandbox_iterator = NULL; - struct sandbox *buffer = NULL; - - ps_list_foreach_del_d(&local_completion_queue, sandbox_iterator, buffer) - { - ps_list_rem_d(sandbox_iterator); - sandbox_free(sandbox_iterator); - } -} diff --git a/runtime/src/worker_thread.c b/runtime/src/worker_thread.c index b1399cb..cfd9535 100644 --- a/runtime/src/worker_thread.c +++ b/runtime/src/worker_thread.c @@ -7,7 +7,7 @@ #include #include "current_sandbox.h" -#include "local_completion_queue.h" +#include "local_cleanup_queue.h" #include "local_runqueue.h" #include "local_runqueue_list.h" #include "local_runqueue_minheap.h" @@ -52,8 +52,8 @@ worker_thread_main(void *argument) scheduler_runqueue_initialize(); - /* Initialize Completion Queue */ - local_completion_queue_initialize(); + /* Initialize Cleanup Queue */ + local_cleanup_queue_initialize(); /* Initialize epoll */ worker_thread_epoll_file_descriptor = epoll_create1(0);