Guard prof related mallctl with opt_prof.

The prof initialization is done only when opt_prof is true.  This change makes
sure the prof_* mallctls only have limited read access (i.e. no access to prof
internals) when opt_prof is false.

In addition, initialize the global prof mutexes even if opt_prof is false.  This
makes sure the mutex stats are set properly.
This commit is contained in:
Qi Wang 2021-09-08 10:58:04 -07:00 committed by Qi Wang
parent 6e848a005e
commit 523cfa55c5
3 changed files with 79 additions and 67 deletions

View File

@ -2337,7 +2337,7 @@ thread_prof_name_ctl(tsd_t *tsd, const size_t *mib,
size_t newlen) {
int ret;
if (!config_prof) {
if (!config_prof || !opt_prof) {
return ENOENT;
}
@ -2374,8 +2374,12 @@ thread_prof_active_ctl(tsd_t *tsd, const size_t *mib,
return ENOENT;
}
oldval = prof_thread_active_get(tsd);
oldval = opt_prof ? prof_thread_active_get(tsd) : false;
if (newp != NULL) {
if (!opt_prof) {
ret = ENOENT;
goto label_return;
}
if (newlen != sizeof(bool)) {
ret = EINVAL;
goto label_return;
@ -3128,6 +3132,10 @@ prof_thread_active_init_ctl(tsd_t *tsd, const size_t *mib,
}
if (newp != NULL) {
if (!opt_prof) {
ret = ENOENT;
goto label_return;
}
if (newlen != sizeof(bool)) {
ret = EINVAL;
goto label_return;
@ -3135,7 +3143,8 @@ prof_thread_active_init_ctl(tsd_t *tsd, const size_t *mib,
oldval = prof_thread_active_init_set(tsd_tsdn(tsd),
*(bool *)newp);
} else {
oldval = prof_thread_active_init_get(tsd_tsdn(tsd));
oldval = opt_prof ? prof_thread_active_init_get(tsd_tsdn(tsd)) :
false;
}
READ(oldval, bool);
@ -3161,13 +3170,19 @@ prof_active_ctl(tsd_t *tsd, const size_t *mib, size_t miblen,
goto label_return;
}
bool val = *(bool *)newp;
if (!opt_prof && val) {
ret = ENOENT;
goto label_return;
if (!opt_prof) {
if (val) {
ret = ENOENT;
goto label_return;
} else {
/* No change needed (already off). */
oldval = false;
}
} else {
oldval = prof_active_set(tsd_tsdn(tsd), val);
}
oldval = prof_active_set(tsd_tsdn(tsd), val);
} else {
oldval = prof_active_get(tsd_tsdn(tsd));
oldval = opt_prof ? prof_active_get(tsd_tsdn(tsd)) : false;
}
READ(oldval, bool);
@ -3182,7 +3197,7 @@ prof_dump_ctl(tsd_t *tsd, const size_t *mib, size_t miblen,
int ret;
const char *filename = NULL;
if (!config_prof) {
if (!config_prof || !opt_prof) {
return ENOENT;
}
@ -3210,13 +3225,17 @@ prof_gdump_ctl(tsd_t *tsd, const size_t *mib, size_t miblen,
}
if (newp != NULL) {
if (!opt_prof) {
ret = ENOENT;
goto label_return;
}
if (newlen != sizeof(bool)) {
ret = EINVAL;
goto label_return;
}
oldval = prof_gdump_set(tsd_tsdn(tsd), *(bool *)newp);
} else {
oldval = prof_gdump_get(tsd_tsdn(tsd));
oldval = opt_prof ? prof_gdump_get(tsd_tsdn(tsd)) : false;
}
READ(oldval, bool);
@ -3231,7 +3250,7 @@ prof_prefix_ctl(tsd_t *tsd, const size_t *mib, size_t miblen,
int ret;
const char *prefix = NULL;
if (!config_prof) {
if (!config_prof || !opt_prof) {
return ENOENT;
}
@ -3251,7 +3270,7 @@ prof_reset_ctl(tsd_t *tsd, const size_t *mib, size_t miblen,
int ret;
size_t lg_sample = lg_prof_sample;
if (!config_prof) {
if (!config_prof || !opt_prof) {
return ENOENT;
}
@ -3278,7 +3297,7 @@ prof_log_start_ctl(tsd_t *tsd, const size_t *mib, size_t miblen, void *oldp,
const char *filename = NULL;
if (!config_prof) {
if (!config_prof || !opt_prof) {
return ENOENT;
}
@ -3298,7 +3317,7 @@ label_return:
static int
prof_log_stop_ctl(tsd_t *tsd, const size_t *mib, size_t miblen, void *oldp,
size_t *oldlenp, void *newp, size_t newlen) {
if (!config_prof) {
if (!config_prof || !opt_prof) {
return ENOENT;
}

View File

@ -554,72 +554,65 @@ bool
prof_boot2(tsd_t *tsd, base_t *base) {
cassert(config_prof);
if (opt_prof) {
unsigned i;
/*
* Initialize the global mutexes unconditionally to maintain correct
* stats when opt_prof is false.
*/
if (malloc_mutex_init(&prof_active_mtx, "prof_active",
WITNESS_RANK_PROF_ACTIVE, malloc_mutex_rank_exclusive)) {
return true;
}
if (malloc_mutex_init(&prof_gdump_mtx, "prof_gdump",
WITNESS_RANK_PROF_GDUMP, malloc_mutex_rank_exclusive)) {
return true;
}
if (malloc_mutex_init(&prof_thread_active_init_mtx,
"prof_thread_active_init", WITNESS_RANK_PROF_THREAD_ACTIVE_INIT,
malloc_mutex_rank_exclusive)) {
return true;
}
if (malloc_mutex_init(&bt2gctx_mtx, "prof_bt2gctx",
WITNESS_RANK_PROF_BT2GCTX, malloc_mutex_rank_exclusive)) {
return true;
}
if (malloc_mutex_init(&tdatas_mtx, "prof_tdatas",
WITNESS_RANK_PROF_TDATAS, malloc_mutex_rank_exclusive)) {
return true;
}
if (malloc_mutex_init(&next_thr_uid_mtx, "prof_next_thr_uid",
WITNESS_RANK_PROF_NEXT_THR_UID, malloc_mutex_rank_exclusive)) {
return true;
}
if (malloc_mutex_init(&prof_stats_mtx, "prof_stats",
WITNESS_RANK_PROF_STATS, malloc_mutex_rank_exclusive)) {
return true;
}
if (malloc_mutex_init(&prof_dump_filename_mtx,
"prof_dump_filename", WITNESS_RANK_PROF_DUMP_FILENAME,
malloc_mutex_rank_exclusive)) {
return true;
}
if (malloc_mutex_init(&prof_dump_mtx, "prof_dump",
WITNESS_RANK_PROF_DUMP, malloc_mutex_rank_exclusive)) {
return true;
}
if (opt_prof) {
lg_prof_sample = opt_lg_prof_sample;
prof_unbias_map_init();
prof_active = opt_prof_active;
if (malloc_mutex_init(&prof_active_mtx, "prof_active",
WITNESS_RANK_PROF_ACTIVE, malloc_mutex_rank_exclusive)) {
return true;
}
prof_gdump_val = opt_prof_gdump;
if (malloc_mutex_init(&prof_gdump_mtx, "prof_gdump",
WITNESS_RANK_PROF_GDUMP, malloc_mutex_rank_exclusive)) {
return true;
}
prof_thread_active_init = opt_prof_thread_active_init;
if (malloc_mutex_init(&prof_thread_active_init_mtx,
"prof_thread_active_init",
WITNESS_RANK_PROF_THREAD_ACTIVE_INIT,
malloc_mutex_rank_exclusive)) {
return true;
}
if (prof_data_init(tsd)) {
return true;
}
if (malloc_mutex_init(&bt2gctx_mtx, "prof_bt2gctx",
WITNESS_RANK_PROF_BT2GCTX, malloc_mutex_rank_exclusive)) {
return true;
}
if (malloc_mutex_init(&tdatas_mtx, "prof_tdatas",
WITNESS_RANK_PROF_TDATAS, malloc_mutex_rank_exclusive)) {
return true;
}
next_thr_uid = 0;
if (malloc_mutex_init(&next_thr_uid_mtx, "prof_next_thr_uid",
WITNESS_RANK_PROF_NEXT_THR_UID,
malloc_mutex_rank_exclusive)) {
return true;
}
if (malloc_mutex_init(&prof_stats_mtx, "prof_stats",
WITNESS_RANK_PROF_STATS, malloc_mutex_rank_exclusive)) {
return true;
}
if (prof_idump_accum_init()) {
return true;
}
if (malloc_mutex_init(&prof_dump_filename_mtx,
"prof_dump_filename", WITNESS_RANK_PROF_DUMP_FILENAME,
malloc_mutex_rank_exclusive)) {
return true;
}
if (malloc_mutex_init(&prof_dump_mtx, "prof_dump",
WITNESS_RANK_PROF_DUMP, malloc_mutex_rank_exclusive)) {
return true;
}
if (opt_prof_final && opt_prof_prefix[0] != '\0' &&
atexit(prof_fdump) != 0) {
malloc_write("<jemalloc>: Error in atexit()\n");
@ -643,7 +636,7 @@ prof_boot2(tsd_t *tsd, base_t *base) {
if (gctx_locks == NULL) {
return true;
}
for (i = 0; i < PROF_NCTX_LOCKS; i++) {
for (unsigned i = 0; i < PROF_NCTX_LOCKS; i++) {
if (malloc_mutex_init(&gctx_locks[i], "prof_gctx",
WITNESS_RANK_PROF_GCTX,
malloc_mutex_rank_exclusive)) {
@ -656,7 +649,7 @@ prof_boot2(tsd_t *tsd, base_t *base) {
if (tdata_locks == NULL) {
return true;
}
for (i = 0; i < PROF_NTDATA_LOCKS; i++) {
for (unsigned i = 0; i < PROF_NTDATA_LOCKS; i++) {
if (malloc_mutex_init(&tdata_locks[i], "prof_tdata",
WITNESS_RANK_PROF_TDATA,
malloc_mutex_rank_exclusive)) {

View File

@ -914,7 +914,7 @@ TEST_BEGIN(test_prof_active) {
old = true;
expect_d_eq(mallctl("prof.active", &old, &len, &active, len), ENOENT,
"Setting prof_active to true should fail when opt_prof is off");
expect_true(old, "old valud should not be touched when mallctl fails");
expect_true(old, "old value should not be touched when mallctl fails");
active = false;
expect_d_eq(mallctl("prof.active", NULL, NULL, &active, len), 0,
"Setting prof_active to false should succeed when opt_prof is off");