Repository navigation
JIT: Fix state left by killed processes #25842
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: PHP-8.4
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2848,19 +2848,42 @@ static int zend_jit(const zend_op_array *op_array, zend_ssa *ssa, const zend_op | |
| return FAILURE; | ||
| } | ||
|
|
||
| static void zend_jit_reset_func_info(zend_func_info *func_info) | ||
| { | ||
| func_info->num = 0; | ||
| func_info->flags &= ZEND_FUNC_JIT_ON_FIRST_EXEC | ||
| | ZEND_FUNC_JIT_ON_PROF_REQUEST | ||
| | ZEND_FUNC_JIT_ON_HOT_COUNTERS | ||
| | ZEND_FUNC_JIT_ON_HOT_TRACE | ||
| | ZEND_FUNC_JITED; | ||
| memset(&func_info->ssa, 0, sizeof(zend_func_info) - offsetof(zend_func_info, ssa)); | ||
| } | ||
|
|
||
| static void zend_jit_collect_calls(zend_op_array *op_array, zend_script *script) | ||
| { | ||
| zend_func_info *func_info; | ||
| zend_call_info *call_info; | ||
|
|
||
| if (JIT_G(trigger) == ZEND_JIT_ON_FIRST_EXEC || | ||
| JIT_G(trigger) == ZEND_JIT_ON_PROF_REQUEST || | ||
| JIT_G(trigger) == ZEND_JIT_ON_HOT_COUNTERS) { | ||
| func_info = ZEND_FUNC_INFO(op_array); | ||
| zend_analyze_calls(&CG(arena), script, ZEND_CALL_TREE, op_array, func_info); | ||
| /* A killed compilation of a callee may have left its call graph, which the inference and the cleanup read */ | ||
| for (call_info = func_info->callee_info; call_info; call_info = call_info->next_callee) { | ||
| if (call_info->callee_func->type == ZEND_USER_FUNCTION) { | ||
| zend_func_info *callee_info = ZEND_FUNC_INFO(&call_info->callee_func->op_array); | ||
|
|
||
| if (callee_info && callee_info != func_info) { | ||
| zend_jit_reset_func_info(callee_info); | ||
| } | ||
| } | ||
| } | ||
| } else { | ||
| func_info = zend_arena_calloc(&CG(arena), 1, sizeof(zend_func_info)); | ||
| ZEND_SET_FUNC_INFO(op_array, func_info); | ||
| zend_analyze_calls(&CG(arena), script, ZEND_CALL_TREE, op_array, func_info); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should add a comment here stating why cleanup is not needed, and a |
||
| } | ||
| zend_analyze_calls(&CG(arena), script, ZEND_CALL_TREE, op_array, func_info); | ||
| } | ||
|
|
||
| static void zend_jit_cleanup_func_info(zend_op_array *op_array) | ||
|
|
@@ -2875,12 +2898,7 @@ static void zend_jit_cleanup_func_info(zend_op_array *op_array) | |
| if (JIT_G(trigger) == ZEND_JIT_ON_FIRST_EXEC || | ||
| JIT_G(trigger) == ZEND_JIT_ON_PROF_REQUEST || | ||
| JIT_G(trigger) == ZEND_JIT_ON_HOT_COUNTERS) { | ||
| func_info->num = 0; | ||
| func_info->flags &= ZEND_FUNC_JIT_ON_FIRST_EXEC | ||
| | ZEND_FUNC_JIT_ON_PROF_REQUEST | ||
| | ZEND_FUNC_JIT_ON_HOT_COUNTERS | ||
| | ZEND_FUNC_JIT_ON_HOT_TRACE; | ||
| memset(&func_info->ssa, 0, sizeof(zend_func_info) - offsetof(zend_func_info, ssa)); | ||
| zend_jit_reset_func_info(func_info); | ||
| } else { | ||
| ZEND_SET_FUNC_INFO(op_array, NULL); | ||
| } | ||
|
|
@@ -2930,6 +2948,16 @@ static int zend_real_jit_func(zend_op_array *op_array, zend_script *script, cons | |
| } | ||
| } | ||
|
|
||
| if (trigger == ZEND_JIT_ON_FIRST_EXEC || | ||
| trigger == ZEND_JIT_ON_PROF_REQUEST || | ||
| trigger == ZEND_JIT_ON_HOT_COUNTERS) { | ||
| func_info = ZEND_FUNC_INFO(op_array); | ||
| /* A process killed while compiling this function may have left its call graph here */ | ||
| if (func_info) { | ||
| zend_jit_reset_func_info(func_info); | ||
| } | ||
| } | ||
|
|
||
| if (zend_jit_op_array_analyze1(op_array, script, &ssa) != SUCCESS) { | ||
| goto jit_failure; | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -19,6 +19,13 @@ | |
| static zend_op_array dummy_op_array; | ||
| static zend_jit_trace_info *zend_jit_traces = NULL; | ||
| static const void **zend_jit_exit_groups = NULL; | ||
| /* Shared func_infos holding the process-local SSA of the trace being compiled */ | ||
| typedef struct _zend_jit_trace_func_infos { | ||
| uint32_t count; | ||
| zend_func_info *func_info[ZEND_JIT_TRACE_MAX_FUNCS]; | ||
| } zend_jit_trace_func_infos; | ||
|
|
||
| static zend_jit_trace_func_infos *zend_jit_func_infos = NULL; | ||
|
Comment on lines
+26
to
+28
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I suggest to rename these to |
||
|
|
||
| #define ZEND_JIT_COUNTER_NUM zend_jit_traces[0].root | ||
| #define ZEND_JIT_TRACE_NUM zend_jit_traces[0].id | ||
|
|
@@ -57,12 +64,18 @@ static void zend_jit_trace_startup(bool reattached) | |
| if (!zend_jit_exit_groups) { | ||
| zend_accel_error_noreturn(ACCEL_LOG_FATAL, "Could not allocate JIT exit groups buffer!"); | ||
| } | ||
| zend_jit_func_infos = (zend_jit_trace_func_infos*)zend_shared_alloc(sizeof(zend_jit_trace_func_infos)); | ||
| if (!zend_jit_func_infos) { | ||
| zend_accel_error_noreturn(ACCEL_LOG_FATAL, "Could not allocate JIT func info buffer!"); | ||
| } | ||
| zend_jit_func_infos->count = 0; | ||
| ZEND_JIT_TRACE_NUM = 1; | ||
| ZEND_JIT_COUNTER_NUM = 0; | ||
| ZEND_JIT_EXIT_NUM = 0; | ||
| ZEND_JIT_EXIT_COUNTERS = 0; | ||
| ZCSG(jit_traces) = zend_jit_traces; | ||
| ZCSG(jit_exit_groups) = zend_jit_exit_groups; | ||
| ZCSG(jit_func_infos) = zend_jit_func_infos; | ||
| ZCSG(jit_counters_stopped) = false; | ||
| } else { | ||
| zend_jit_traces = ZCSG(jit_traces); | ||
|
|
@@ -73,6 +86,10 @@ static void zend_jit_trace_startup(bool reattached) | |
| if (!zend_jit_exit_groups) { | ||
| zend_accel_error_noreturn(ACCEL_LOG_FATAL, "Could not obtain JIT exit groups buffer!"); | ||
| } | ||
| zend_jit_func_infos = ZCSG(jit_func_infos); | ||
| if (!zend_jit_func_infos) { | ||
| zend_accel_error_noreturn(ACCEL_LOG_FATAL, "Could not obtain JIT func info buffer!"); | ||
| } | ||
| } | ||
|
|
||
| memset(&dummy_op_array, 0, sizeof(dummy_op_array)); | ||
|
|
@@ -531,13 +548,42 @@ static bool zend_jit_needs_arg_dtor(const zend_function *func, uint32_t arg_num, | |
| return 1; | ||
| } | ||
|
|
||
| static void zend_jit_trace_record_func_info(zend_func_info *func_info) | ||
| { | ||
| uint32_t count = zend_jit_func_infos->count; | ||
|
|
||
| ZEND_ASSERT(count < ZEND_JIT_TRACE_MAX_FUNCS); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should turn this into a release assertion (e.g. if (!count < ZEND_JIT_TRACE_MAX_FUNCS) { abort(); }`), just in case |
||
| zend_jit_func_infos->func_info[count] = func_info; | ||
| ZEND_ATOMIC_FENCE_RELEASE(); | ||
| zend_jit_func_infos->count = count + 1; | ||
| /* Record before writing, so the next compilation can reset what a killed process left */ | ||
| ZEND_ATOMIC_FENCE_RELEASE(); | ||
| } | ||
|
|
||
| static void zend_jit_trace_reset_func_infos(void) | ||
| { | ||
| for (uint32_t i = 0; i < zend_jit_func_infos->count; i++) { | ||
| zend_func_info *func_info = zend_jit_func_infos->func_info[i]; | ||
|
|
||
| func_info->num = 0; | ||
| func_info->flags &= ZEND_FUNC_JIT_ON_FIRST_EXEC | ||
| | ZEND_FUNC_JIT_ON_PROF_REQUEST | ||
| | ZEND_FUNC_JIT_ON_HOT_COUNTERS | ||
| | ZEND_FUNC_JIT_ON_HOT_TRACE; | ||
| memset(&func_info->ssa, 0, sizeof(zend_func_info) - offsetof(zend_func_info, ssa)); | ||
|
Comment on lines
+569
to
+573
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we reuse zend_jit_reset_func_info() here? |
||
| } | ||
| ZEND_ATOMIC_FENCE_RELEASE(); | ||
| zend_jit_func_infos->count = 0; | ||
| } | ||
|
|
||
| static zend_ssa *zend_jit_trace_build_ssa(const zend_op_array *op_array, zend_script *script) | ||
| { | ||
| zend_jit_op_array_trace_extension *jit_extension; | ||
| zend_ssa *ssa; | ||
|
|
||
| jit_extension = | ||
| (zend_jit_op_array_trace_extension*)ZEND_FUNC_INFO(op_array); | ||
| zend_jit_trace_record_func_info(&jit_extension->func_info); | ||
| jit_extension->func_info.num = 0; | ||
| jit_extension->func_info.flags &= ZEND_FUNC_JIT_ON_FIRST_EXEC | ||
| | ZEND_FUNC_JIT_ON_PROF_REQUEST | ||
|
|
@@ -4132,6 +4178,9 @@ static const void *zend_jit_trace(zend_jit_trace_rec *trace_buffer, uint32_t par | |
|
|
||
| JIT_G(current_trace) = trace_buffer; | ||
|
|
||
| /* A process killed while compiling a trace may have left its SSA in shared func_infos */ | ||
| zend_jit_trace_reset_func_infos(); | ||
|
|
||
| checkpoint = zend_arena_checkpoint(CG(arena)); | ||
|
|
||
| zend_try { | ||
|
|
@@ -7345,18 +7394,7 @@ jit_cleanup:; | |
| } zend_end_try(); | ||
|
|
||
| /* Clean up used op_arrays */ | ||
| while (num_op_arrays > 0) { | ||
| op_array = op_arrays[--num_op_arrays]; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looks like op_arrays is unused after this change, so we could remove it in master. |
||
| jit_extension = | ||
| (zend_jit_op_array_trace_extension*)ZEND_FUNC_INFO(op_array); | ||
|
|
||
| jit_extension->func_info.num = 0; | ||
| jit_extension->func_info.flags &= ZEND_FUNC_JIT_ON_FIRST_EXEC | ||
| | ZEND_FUNC_JIT_ON_PROF_REQUEST | ||
| | ZEND_FUNC_JIT_ON_HOT_COUNTERS | ||
| | ZEND_FUNC_JIT_ON_HOT_TRACE; | ||
| memset(&jit_extension->func_info.ssa, 0, sizeof(zend_func_info) - offsetof(zend_func_info, ssa)); | ||
| } | ||
| zend_jit_trace_reset_func_infos(); | ||
|
|
||
| zend_arena_release(&CG(arena), checkpoint); | ||
|
|
||
|
|
@@ -7672,7 +7710,6 @@ static void zend_jit_stop_counter_handlers(void) | |
| /* mprotect has an extreme overhead, avoid calls to it for every function. */ | ||
| SHM_UNPROTECT(); | ||
| if (!ZCSG(jit_counters_stopped)) { | ||
| ZCSG(jit_counters_stopped) = true; | ||
| for (uint32_t i = 0; i < ZCSG(hash).max_num_entries; i++) { | ||
| zend_accel_hash_entry *cache_entry; | ||
| for (cache_entry = ZCSG(hash).hash_table[i]; cache_entry; cache_entry = cache_entry->next) { | ||
|
|
@@ -7682,6 +7719,9 @@ static void zend_jit_stop_counter_handlers(void) | |
| zend_jit_stop_persistent_script(script); | ||
| } | ||
| } | ||
| /* Set last, so the next call redoes a loop that a killed process left half done */ | ||
| ZEND_ATOMIC_FENCE_RELEASE(); | ||
| ZCSG(jit_counters_stopped) = true; | ||
| } | ||
| SHM_PROTECT(); | ||
| zend_shared_alloc_unlock(); | ||
|
|
@@ -9006,6 +9046,8 @@ static void zend_jit_trace_restart(void) | |
| ZEND_JIT_EXIT_NUM = 0; | ||
| ZEND_JIT_EXIT_COUNTERS = 0; | ||
| ZCSG(jit_counters_stopped) = false; | ||
| /* Preloaded functions survive the restart, freed ones are not reused yet */ | ||
| zend_jit_trace_reset_func_infos(); | ||
|
|
||
| zend_jit_trace_init_caches(); | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We could use
zend_jit_func_infos/zend_jit_trace_reset_func_infos()for the function JIT too. This would reduce complexity IMO. I believe that the function JIT only dirties one func info at a time so the MAX_FUNCS limit should be fine. If we ever need more we could make a linked list of dirty func infos.This would also fix a potential case where a func info is dirtied in one JIT mode (e.g. function JIT) and reused in another mode later without being cleaned since it's not in the list. Using the same dirty list in all modes would avoid this.