diff --git a/ext/opcache/ZendAccelerator.c b/ext/opcache/ZendAccelerator.c index 76797291e64d..0ba030697678 100644 --- a/ext/opcache/ZendAccelerator.c +++ b/ext/opcache/ZendAccelerator.c @@ -625,13 +625,15 @@ zend_string* ZEND_FASTCALL accel_new_interned_string(zend_string *str) s = ZCSG(interned_strings).top; hash_slot = STRTAB_HASH_TO_SLOT(&ZCSG(interned_strings), h); STRTAB_COLLISION(s) = *hash_slot; - *hash_slot = STRTAB_STR_TO_POS(&ZCSG(interned_strings), s); GC_SET_REFCOUNT(s, 2); GC_TYPE_INFO(s) = GC_STRING | ((IS_STR_INTERNED | IS_STR_PERMANENT) << GC_FLAGS_SHIFT)| (ZSTR_IS_VALID_UTF8(str) ? IS_STR_VALID_UTF8 : 0); ZSTR_H(s) = h; ZSTR_LEN(s) = ZSTR_LEN(str); memcpy(ZSTR_VAL(s), ZSTR_VAL(str), ZSTR_LEN(s) + 1); ZCSG(interned_strings).top = STRTAB_NEXT(s); + /* Publish last, a process killed before this only leaks the string */ + ZEND_ATOMIC_FENCE_RELEASE(); + *hash_slot = STRTAB_STR_TO_POS(&ZCSG(interned_strings), s); finish: /* Transfer CE_CACHE map ptr slot to new interned string. @@ -2776,7 +2778,6 @@ static void zend_reset_cache_vars(void) ZCSG(misses) = 0; ZCSG(blacklist_misses) = 0; ZSMMG(wasted_shared_memory) = 0; - ZCSG(restart_pending) = false; ZCSG(force_restart_time) = 0; ZCSG(map_ptr_last) = CG(map_ptr_last); ZCSG(map_ptr_static_last) = zend_map_ptr_static_last; @@ -2864,18 +2865,6 @@ ZEND_RINIT_FUNCTION(zend_accelerator) if (accel_is_inactive()) { #endif zend_accel_error(ACCEL_LOG_DEBUG, "Restarting!"); - ZCSG(restart_pending) = false; - switch ZCSG(restart_reason) { - case ACCEL_RESTART_OOM: - ZCSG(oom_restarts)++; - break; - case ACCEL_RESTART_HASH: - ZCSG(hash_restarts)++; - break; - case ACCEL_RESTART_USER: - ZCSG(manual_restarts)++; - break; - } accel_restart_enter(); zend_map_ptr_reset(); @@ -2901,6 +2890,20 @@ ZEND_RINIT_FUNCTION(zend_accelerator) } else { ZCSG(last_restart_time)++; } + switch ZCSG(restart_reason) { + case ACCEL_RESTART_OOM: + ZCSG(oom_restarts)++; + break; + case ACCEL_RESTART_HASH: + ZCSG(hash_restarts)++; + break; + case ACCEL_RESTART_USER: + ZCSG(manual_restarts)++; + break; + } + /* Clear last, so the next request redoes a restart that was killed midway */ + ZEND_ATOMIC_FENCE_RELEASE(); + ZCSG(restart_pending) = false; accel_restart_leave(); #ifndef ZEND_WIN32 accel_restart_end(); @@ -2910,17 +2913,26 @@ ZEND_RINIT_FUNCTION(zend_accelerator) zend_shared_alloc_unlock(); } - ZCG(accelerator_enabled) = ZCSG(accelerator_enabled); + /* Let readers drain for a pending restart, even one a killed process left enabled */ + ZCG(accelerator_enabled) = ZCSG(accelerator_enabled) && !ZCSG(restart_pending); + /* Pairs with the release before clearing restart_pending, so last_restart_time is current */ + ZEND_ATOMIC_FENCE_ACQUIRE(); #ifndef ZEND_WIN32 /* This is the only place where a request starts to use SHM */ if (ZCG(accelerator_enabled)) { - if (accel_reader_enter()) { + if (!accel_reader_enter()) { + ZCG(accelerator_enabled) = false; + } else if (ZCSG(restart_pending)) { + /* A restart may have been killed while this request waited for the reader lock */ + ZCG(counted) = true; + accel_deactivate_sub(); + ZCG(counted) = false; + ZCG(accelerator_enabled) = false; + } else { ZCG(counted) = true; /* SHM-backed keys may be added to the PCRE cache again */ ZCG(pcre_reseted) = false; - } else { - ZCG(accelerator_enabled) = false; } } #endif @@ -3686,16 +3698,16 @@ void zend_accel_schedule_restart(zend_accel_restart_reason reason) HANDLE_BLOCK_INTERRUPTIONS(); SHM_UNPROTECT(); - ZCSG(restart_pending) = true; ZCSG(restart_reason) = reason; ZCSG(cache_status_before_restart) = ZCSG(accelerator_enabled); - ZCSG(accelerator_enabled) = false; - if (ZCG(accel_directives).force_restart_timeout) { ZCSG(force_restart_time) = zend_accel_get_time() + ZCG(accel_directives).force_restart_timeout; } else { ZCSG(force_restart_time) = 0; } + ZEND_ATOMIC_FENCE_RELEASE(); + ZCSG(restart_pending) = true; + ZCSG(accelerator_enabled) = false; SHM_PROTECT(); HANDLE_UNBLOCK_INTERRUPTIONS(); } diff --git a/ext/opcache/jit/zend_jit_trace.c b/ext/opcache/jit/zend_jit_trace.c index 36a0baa8c9ff..5cd2e437b7a4 100644 --- a/ext/opcache/jit/zend_jit_trace.c +++ b/ext/opcache/jit/zend_jit_trace.c @@ -7554,11 +7554,12 @@ static zend_jit_trace_stop zend_jit_compile_root_trace(zend_jit_trace_rec *trace t->exit_counters = ZEND_JIT_EXIT_COUNTERS; ZEND_JIT_EXIT_COUNTERS += t->exit_count; - - ((zend_op*)opline)->handler = handler; - + /* Bookkeeping first: reachable code needs its slot, and the JITED flag to be invalidated */ ZEND_JIT_TRACE_NUM++; ZEND_OP_TRACE_INFO(opline, offset)->trace_flags |= ZEND_JIT_TRACE_JITED; + ZEND_ATOMIC_FENCE_RELEASE(); + + ((zend_op*)opline)->handler = handler; ret = ZEND_JIT_TRACE_STOP_COMPILED; } else if (t->exit_count >= ZEND_JIT_TRACE_MAX_EXITS || @@ -8104,6 +8105,8 @@ int ZEND_FASTCALL zend_jit_trace_hot_root(zend_execute_data *execute_data, const /* Lock-free check if the root trace was already JIT-ed or blacklist-ed in another process */ if (ZEND_OP_TRACE_INFO(opline, offset)->trace_flags & (ZEND_JIT_TRACE_JITED|ZEND_JIT_TRACE_BLACKLISTED)) { + /* A process killed before installing the trace left the counting handler, which we re-enter */ + ++(*ZEND_OP_TRACE_INFO(opline, offset)->counter); return 0; } @@ -8386,6 +8389,14 @@ static zend_jit_trace_stop zend_jit_compile_side_trace(zend_jit_trace_rec *trace t->constants = constants; } + t->exit_counters = ZEND_JIT_EXIT_COUNTERS; + ZEND_JIT_EXIT_COUNTERS += t->exit_count; + + zend_jit_traces[zend_jit_traces[parent_num].root].child_count++; + /* Reserve the slot before the code that refers to it becomes reachable */ + ZEND_JIT_TRACE_NUM++; + ZEND_ATOMIC_FENCE_RELEASE(); + zend_jit_link_side_trace( zend_jit_traces[parent_num].code_start, zend_jit_traces[parent_num].code_size, @@ -8393,11 +8404,6 @@ static zend_jit_trace_stop zend_jit_compile_side_trace(zend_jit_trace_rec *trace exit_num, handler); - t->exit_counters = ZEND_JIT_EXIT_COUNTERS; - ZEND_JIT_EXIT_COUNTERS += t->exit_count; - - zend_jit_traces[zend_jit_traces[parent_num].root].child_count++; - ZEND_JIT_TRACE_NUM++; zend_jit_traces[parent_num].exit_info[exit_num].flags |= ZEND_JIT_EXIT_JITED; ret = ZEND_JIT_TRACE_STOP_COMPILED;