From: "Steven Rostedt (VMware)" <rostedt@xxxxxxxxxxx> Add a pthread_mutex per instance such that modifications being done on specific instances do not interfere with each other. This also includes a new "toplevel_lock" that is to be used when modifying the toplevel instance. Convert the filter_mutex over to the toplevel/instance locking. Signed-off-by: Steven Rostedt (VMware) <rostedt@xxxxxxxxxxx> --- include/tracefs-local.h | 13 ++++++++----- src/tracefs-instance.c | 5 +++++ src/tracefs-tools.c | 8 +++++--- 3 files changed, 18 insertions(+), 8 deletions(-) diff --git a/include/tracefs-local.h b/include/tracefs-local.h index 060de7e..6865611 100644 --- a/include/tracefs-local.h +++ b/include/tracefs-local.h @@ -15,13 +15,16 @@ do { if (!(1/!(cond))) { } } while (0) struct tracefs_instance { - char *trace_dir; - char *name; - int flags; - int ftrace_filter_fd; - int ftrace_notrace_fd; + char *trace_dir; + char *name; + pthread_mutex_t lock; + int flags; + int ftrace_filter_fd; + int ftrace_notrace_fd; }; +extern pthread_mutex_t toplevel_lock; + /* Can be overridden */ void warning(const char *fmt, ...); diff --git a/src/tracefs-instance.c b/src/tracefs-instance.c index 9f45624..599c3a7 100644 --- a/src/tracefs-instance.c +++ b/src/tracefs-instance.c @@ -15,6 +15,7 @@ #include <fcntl.h> #include <dirent.h> #include <limits.h> +#include <pthread.h> #include "tracefs.h" #include "tracefs-local.h" @@ -43,6 +44,9 @@ static struct tracefs_instance *instance_alloc(const char *trace_dir, const char goto error; } + if (pthread_mutex_init(&instance->lock, NULL) < 0) + goto error; + instance->ftrace_filter_fd = -1; instance->ftrace_notrace_fd = -1; @@ -69,6 +73,7 @@ void tracefs_instance_free(struct tracefs_instance *instance) return; free(instance->trace_dir); free(instance->name); + pthread_mutex_destroy(&instance->lock); free(instance); } diff --git a/src/tracefs-tools.c b/src/tracefs-tools.c index b41806b..d48062c 100644 --- a/src/tracefs-tools.c +++ b/src/tracefs-tools.c @@ -19,6 +19,8 @@ #include "tracefs.h" #include "tracefs-local.h" +__hidden pthread_mutex_t toplevel_lock = PTHREAD_MUTEX_INITIALIZER; + #define TRACE_CTRL "tracing_on" #define TRACE_FILTER "set_ftrace_filter" #define TRACE_NOTRACE "set_ftrace_notrace" @@ -27,7 +29,6 @@ /* File descriptor for Top level set_ftrace_filter */ static int ftrace_filter_fd = -1; static int ftrace_notrace_fd = -1; -static pthread_mutex_t filter_lock = PTHREAD_MUTEX_INITIALIZER; static const char * const options_map[] = { "unknown", @@ -755,6 +756,7 @@ static int update_filter(const char *filter_path, int *fd, bool reset = flags & TRACEFS_FL_RESET; bool cont = flags & TRACEFS_FL_CONTINUE; bool future = flags & TRACEFS_FL_FUTURE; + pthread_mutex_t *lock = instance ? &instance->lock : &toplevel_lock; int open_flags; int ret = 1; @@ -764,7 +766,7 @@ static int update_filter(const char *filter_path, int *fd, return 1; } - pthread_mutex_lock(&filter_lock); + pthread_mutex_lock(lock); /* RESET is only allowed if the file is not opened yet */ if (reset && *fd >= 0) { @@ -839,7 +841,7 @@ static int update_filter(const char *filter_path, int *fd, out_free: free_func_list(func_list); out: - pthread_mutex_unlock(&filter_lock); + pthread_mutex_unlock(lock); return ret; } -- 2.29.2