Skip to content

Commit

Permalink
Avoid recursive locking in apcu_entry()
Browse files Browse the repository at this point in the history
Don't try to acquire any recursive cache locks if the current
thread is already inside apcu_entry().

This means that it is now safe to call other apcu_* functions
inside apcu_entry().
  • Loading branch information
nikic committed Feb 7, 2021
1 parent 61f3c16 commit 6e73f57
Show file tree
Hide file tree
Showing 8 changed files with 101 additions and 66 deletions.
78 changes: 29 additions & 49 deletions apc_cache.c
Original file line number Diff line number Diff line change
Expand Up @@ -504,15 +504,15 @@ PHP_APCU_API zend_bool apc_cache_store(
}

/* execute an insertion */
if (!APC_WLOCK(cache->header)) {
if (!apc_cache_wlock(cache)) {
free_entry(cache, entry);
return 0;
}

php_apc_try {
ret = apc_cache_wlocked_insert(cache, entry, exclusive);
} php_apc_finally {
APC_WUNLOCK(cache->header);
apc_cache_wunlock(cache);
} php_apc_end_try();

if (!ret) {
Expand Down Expand Up @@ -701,8 +701,7 @@ PHP_APCU_API void apc_cache_clear(apc_cache_t* cache)
return;
}

/* lock header */
if (!APC_WLOCK(cache->header)) {
if (!apc_cache_wlock(cache)) {
return;
}

Expand All @@ -713,8 +712,7 @@ PHP_APCU_API void apc_cache_clear(apc_cache_t* cache)
cache->header->stime = apc_time();
cache->header->nexpunges = 0;

/* unlock header */
APC_WUNLOCK(cache->header);
apc_cache_wunlock(cache);
}
/* }}} */

Expand All @@ -734,7 +732,7 @@ PHP_APCU_API void apc_cache_default_expunge(apc_cache_t* cache, size_t size)
t = apc_time();

/* get the lock for header */
if (!APC_WLOCK(cache->header)) {
if (!apc_cache_wlock(cache)) {
return;
}

Expand Down Expand Up @@ -783,8 +781,7 @@ PHP_APCU_API void apc_cache_default_expunge(apc_cache_t* cache, size_t size)
}
}

/* unlock header */
APC_WUNLOCK(cache->header);
apc_cache_wunlock(cache);
}
/* }}} */

Expand All @@ -797,12 +794,12 @@ PHP_APCU_API apc_cache_entry_t *apc_cache_find(apc_cache_t* cache, zend_string *
return NULL;
}

if (!APC_RLOCK(cache->header)) {
if (!apc_cache_rlock(cache)) {
return NULL;
}

entry = apc_cache_rlocked_find_incref(cache, key, t);
APC_RUNLOCK(cache->header);
apc_cache_runlock(cache);

return entry;
}
Expand All @@ -818,12 +815,12 @@ PHP_APCU_API zend_bool apc_cache_fetch(apc_cache_t* cache, zend_string *key, tim
return 0;
}

if (!APC_RLOCK(cache->header)) {
if (!apc_cache_rlock(cache)) {
return 0;
}

entry = apc_cache_rlocked_find_incref(cache, key, t);
APC_RUNLOCK(cache->header);
apc_cache_runlock(cache);

if (!entry) {
return 0;
Expand All @@ -847,12 +844,12 @@ PHP_APCU_API zend_bool apc_cache_exists(apc_cache_t* cache, zend_string *key, ti
return 0;
}

if (!APC_RLOCK(cache->header)) {
if (!apc_cache_rlock(cache)) {
return 0;
}

entry = apc_cache_rlocked_find_nostat(cache, key, t);
APC_RUNLOCK(cache->header);
apc_cache_runlock(cache);

return entry != NULL;
}
Expand All @@ -872,7 +869,7 @@ PHP_APCU_API zend_bool apc_cache_update(
}

retry_update:
if (!APC_WLOCK(cache->header)) {
if (!apc_cache_wlock(cache)) {
return 0;
}

Expand All @@ -884,11 +881,11 @@ PHP_APCU_API zend_bool apc_cache_update(
entry->mtime = t;
}

APC_WUNLOCK(cache->header);
apc_cache_wunlock(cache);
return retval;
}

APC_WUNLOCK(cache->header);
apc_cache_wunlock(cache);
if (insert_if_not_found) {
/* Failed to find matching entry. Add key with value 0 and run the updater again. */
zval val;
Expand Down Expand Up @@ -922,7 +919,7 @@ PHP_APCU_API zend_bool apc_cache_atomic_update_long(
}

retry_update:
if (!APC_RLOCK(cache->header)) {
if (!apc_cache_rlock(cache)) {
return 0;
}

Expand All @@ -934,11 +931,11 @@ PHP_APCU_API zend_bool apc_cache_atomic_update_long(
entry->mtime = t;
}

APC_RUNLOCK(cache->header);
apc_cache_runlock(cache);
return retval;
}

APC_RUNLOCK(cache->header);
apc_cache_runlock(cache);
if (insert_if_not_found) {
/* Failed to find matching entry. Add key with value 0 and run the updater again. */
zval val;
Expand Down Expand Up @@ -971,8 +968,7 @@ PHP_APCU_API zend_bool apc_cache_delete(apc_cache_t *cache, zend_string *key)
/* calculate hash and slot */
apc_cache_hash_slot(cache, key, &h, &s);

/* lock cache */
if (!APC_WLOCK(cache->header)) {
if (!apc_cache_wlock(cache)) {
return 0;
}

Expand All @@ -985,16 +981,14 @@ PHP_APCU_API zend_bool apc_cache_delete(apc_cache_t *cache, zend_string *key)
/* executing removal */
apc_cache_wlocked_remove_entry(cache, entry);

/* unlock header */
APC_WUNLOCK(cache->header);
apc_cache_wunlock(cache);
return 1;
}

entry = &(*entry)->next;
}

/* unlock header */
APC_WUNLOCK(cache->header);
apc_cache_wunlock(cache);
return 0;
}
/* }}} */
Expand Down Expand Up @@ -1074,7 +1068,7 @@ PHP_APCU_API zend_bool apc_cache_info(zval *info, apc_cache_t *cache, zend_bool
return 0;
}

if (!APC_RLOCK(cache->header)) {
if (!apc_cache_rlock(cache)) {
return 0;
}

Expand Down Expand Up @@ -1127,7 +1121,7 @@ PHP_APCU_API zend_bool apc_cache_info(zval *info, apc_cache_t *cache, zend_bool
add_assoc_zval(info, "slot_distribution", &slots);
}
} php_apc_finally {
APC_RUNLOCK(cache->header);
apc_cache_runlock(cache);
} php_apc_end_try();

return 1;
Expand All @@ -1148,7 +1142,7 @@ PHP_APCU_API void apc_cache_stat(apc_cache_t *cache, zend_string *key, zval *sta
/* calculate hash and slot */
apc_cache_hash_slot(cache, key, &h, &s);

if (!APC_RLOCK(cache->header)) {
if (!apc_cache_rlock(cache)) {
return;
}

Expand All @@ -1174,7 +1168,7 @@ PHP_APCU_API void apc_cache_stat(apc_cache_t *cache, zend_string *key, zval *sta
entry = entry->next;
}
} php_apc_finally {
APC_RUNLOCK(cache->header);
apc_cache_runlock(cache);
} php_apc_end_try();
}

Expand Down Expand Up @@ -1234,19 +1228,11 @@ PHP_APCU_API void apc_cache_entry(apc_cache_t *cache, zend_string *key, zend_fca
return;
}

#ifndef APC_LOCK_RECURSIVE
if (APCG(recursion)++ == 0) {
if (!APC_WLOCK(cache->header)) {
APCG(recursion)--;
return;
}
}
#else
if (!APC_WLOCK(cache->header)) {
if (!apc_cache_wlock(cache)) {
return;
}
#endif

APCG(entry_level)++;
php_apc_try {
entry = apc_cache_rlocked_find_incref(cache, key, now);
if (!entry) {
Expand All @@ -1271,14 +1257,8 @@ PHP_APCU_API void apc_cache_entry(apc_cache_t *cache, zend_string *key, zend_fca
apc_cache_entry_release(cache, entry);
}
} php_apc_finally {
#ifndef APC_LOCK_RECURSIVE
if (--APCG(recursion) == 0) {
APC_WUNLOCK(cache->header);
}
#else
APC_WUNLOCK(cache->header);
#endif

APCG(entry_level)--;
apc_cache_wunlock(cache);
} php_apc_end_try();
}
/*}}}*/
Expand Down
38 changes: 38 additions & 0 deletions apc_cache.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
#include "apc.h"
#include "apc_sma.h"
#include "apc_lock.h"
#include "apc_globals.h"
#include "TSRM.h"

typedef struct apc_cache_slam_key_t apc_cache_slam_key_t;
Expand Down Expand Up @@ -276,6 +277,43 @@ PHP_APCU_API void apc_cache_default_expunge(apc_cache_t* cache, size_t size);
*/
PHP_APCU_API void apc_cache_entry(apc_cache_t *cache, zend_string *key, zend_fcall_info *fci, zend_fcall_info_cache *fcc, zend_long ttl, zend_long now, zval *return_value);

/* apcu_entry() holds a write lock on the cache while executing user code.
* That code may call other apcu_* functions, which also try to acquire a
* read or write lock, which would deadlock. As such, don't try to acquire a
* lock if the current thread is inside apcu_entry().
*
* Whether the current thread is inside apcu_entry() is tracked by APCG(entry_level).
* This breaks the self-contained apc_cache_t abstraction, but is currently
* necessary because the entry_level needs to be tracked per-thread, while
* apc_cache_t is a per-process structure.
*/

static inline zend_bool apc_cache_wlock(apc_cache_t *cache) {
if (!APCG(entry_level)) {
return WLOCK(&cache->header->lock);
}
return 1;
}

static inline void apc_cache_wunlock(apc_cache_t *cache) {
if (!APCG(entry_level)) {
WUNLOCK(&cache->header->lock);
}
}

static inline zend_bool apc_cache_rlock(apc_cache_t *cache) {
if (!APCG(entry_level)) {
return RLOCK(&cache->header->lock);
}
return 1;
}

static inline void apc_cache_runlock(apc_cache_t *cache) {
if (!APCG(entry_level)) {
RUNLOCK(&cache->header->lock);
}
}

#endif

/*
Expand Down
8 changes: 4 additions & 4 deletions apc_globals.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@
#define APC_GLOBALS_H

#include "apc.h"
#include "apc_cache.h"
#include "apc_stack.h"

ZEND_BEGIN_MODULE_GLOBALS(apcu)
/* configuration parameters */
Expand Down Expand Up @@ -62,7 +60,8 @@ ZEND_BEGIN_MODULE_GLOBALS(apcu)

char *serializer_name; /* the serializer config option */

volatile unsigned recursion;
/* Nesting level of apcu_entry calls. */
unsigned int entry_level;
ZEND_END_MODULE_GLOBALS(apcu)

/* (the following is defined in php_apc.c) */
Expand All @@ -74,7 +73,8 @@ ZEND_EXTERN_MODULE_GLOBALS(apcu)
# define APCG(v) (apcu_globals.v)
#endif

extern apc_cache_t* apc_user_cache;
extern struct _apc_cache_t* apc_user_cache;

#endif

/*
Expand Down
12 changes: 6 additions & 6 deletions apc_iterator.c
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ static int apc_iterator_fetch_active(apc_iterator_t *iterator) {
apc_iterator_item_dtor(apc_stack_pop(iterator->stack));
}

if (!APC_RLOCK(apc_user_cache->header)) {
if (!apc_cache_rlock(apc_user_cache)) {
return count;
}

Expand All @@ -241,7 +241,7 @@ static int apc_iterator_fetch_active(apc_iterator_t *iterator) {
}
} php_apc_finally {
iterator->stack_idx = 0;
APC_RUNLOCK(apc_user_cache->header)
apc_cache_runlock(apc_user_cache);
} php_apc_end_try();

return count;
Expand All @@ -253,7 +253,7 @@ static int apc_iterator_fetch_deleted(apc_iterator_t *iterator) {
int count = 0;
apc_iterator_item_t *item;

if (!APC_RLOCK(apc_user_cache->header)) {
if (!apc_cache_rlock(apc_user_cache)) {
return count;
}

Expand All @@ -277,7 +277,7 @@ static int apc_iterator_fetch_deleted(apc_iterator_t *iterator) {
} php_apc_finally {
iterator->slot_idx += count;
iterator->stack_idx = 0;
APC_RUNLOCK(apc_user_cache->header);
apc_cache_runlock(apc_user_cache);
} php_apc_end_try();

return count;
Expand All @@ -289,7 +289,7 @@ static void apc_iterator_totals(apc_iterator_t *iterator) {
time_t t = apc_time();
int i;

if (!APC_RLOCK(apc_user_cache->header)) {
if (!apc_cache_rlock(apc_user_cache)) {
return;
}

Expand All @@ -309,7 +309,7 @@ static void apc_iterator_totals(apc_iterator_t *iterator) {
}
} php_apc_finally {
iterator->totals_flag = 1;
APC_RUNLOCK(apc_user_cache->header);
apc_cache_runlock(apc_user_cache);
} php_apc_end_try();
}
/* }}} */
Expand Down
6 changes: 0 additions & 6 deletions apc_lock.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,6 @@ PHP_APCU_API void apc_lock_destroy(apc_lock_t *lock); /* }}} */
#define RUNLOCK(lock) { apc_lock_runlock(lock); HANDLE_UNBLOCK_INTERRUPTIONS(); }
/* }}} */

/* {{{ object locking macros */
#define APC_WLOCK(o) WLOCK(&(o)->lock)
#define APC_WUNLOCK(o) WUNLOCK(&(o)->lock)
#define APC_RLOCK(o) RLOCK(&(o)->lock)
#define APC_RUNLOCK(o) RUNLOCK(&(o)->lock) /* }}} */

/* atomic operations */
#ifdef PHP_WIN32
# ifdef _WIN64
Expand Down
Loading

0 comments on commit 6e73f57

Please sign in to comment.