Re: [PATCH 2/4] Add use of RCU for qemu_logfile.
On Thu, 7 Nov 2019 at 11:24, Alex Bennée wrote: > > Robert Foley writes: > > diff --git a/include/qemu/log.h b/include/qemu/log.h > > index a91105b2ad..975de18e23 100644 > > --- a/include/qemu/log.h > > +++ b/include/qemu/log.h > > @@ -3,9 +3,17 @@ > > > > /* A small part of this API is split into its own header */ > > #include "qemu/log-for-trace.h" > > +#include "qemu/rcu.h" > > + > > +struct QemuLogFile { > > +struct rcu_head rcu; > > +FILE *fd; > > +}; > > +typedef struct QemuLogFile QemuLogFile; > > If you are declaring a typedef then do it in one: > > typedef struct QemuLogFile { ... > > We only really use the second form for opaque structs where the handle > is passed around but the contents hidden from the rest of QEMU. > OK, makes sense. Will correct it. Thanks for explaining. > > > > /* Private global variable, don't use */ > > -extern FILE *qemu_logfile; > > +extern QemuLogFile *qemu_logfile; > > + > > Do we need multiple Not 100% sure on the meaning here. Are you asking if we need to extern this? We have a few other cases outside of the log module where this is getting used so the extern is needed here. > > > > /* > > * The new API: > > @@ -25,7 +33,17 @@ static inline bool qemu_log_enabled(void) > > */ > > static inline bool qemu_log_separate(void) > > { > > -return qemu_logfile != NULL && qemu_logfile != stderr; > > +QemuLogFile *logfile; > > + > > +if (qemu_log_enabled()) { > > +rcu_read_lock(); > > +logfile = atomic_rcu_read(&qemu_logfile); > > +if (logfile && logfile->fd != stderr) { > > +return true; > > +} > > +rcu_read_unlock(); > > +} > > +return false; > > This is unbalanced as you'll have incremented the reader count. Also > qemu_log_enabled() is also synonymous with logfile existing so you could > fold that into: > > bool res = false; > > rcu_read_lock(); > *logfile = atomic_rcu_read(&qemu_logfile); > if (logfile && logfile->fd != stderr) { > res = true; > } > rcu_read_unlock(); > return res; > > There is technically a race there as the answer may become invalid after > qemu_log_separate() returns. However given the users I don't see what > could fail afterwards. > I agree, will make these changes. > > } > > > > #define CPU_LOG_TB_OUT_ASM (1 << 0) > > @@ -55,12 +73,23 @@ static inline bool qemu_log_separate(void) > > > > static inline void qemu_log_lock(void) > > { > > -qemu_flockfile(qemu_logfile); > > +QemuLogFile *logfile; > > +rcu_read_lock(); > > +logfile = atomic_rcu_read(&qemu_logfile); > > +if (logfile) { > > +qemu_flockfile(logfile->fd); > > +} > > +rcu_read_unlock(); > > } > > static inline FILE *fd qemu_log_lock(void) > { QemuLogFile *logfile; > rcu_read_lock(); logfile = atomic_rcu_read(&qemu_logfile); if (logfile) { > qemu_flockfile(logfile->fd); return logfile->fd; > } else { > rcu_read_unlock(); > return NULL; > } > } > > static inline qemu_log_unlock(FILE *fd) > { > if (fd) { > qemu_funlockfile(fd); > rcu_read_unlock(); > } > } > > While the rcu_read_lock() is in progress then we won't ever finish with > the fd - which we don't want to do until the file locking is finished. Agree with the adjustments you made to qemu_log_lock(). I updated the code above with a few tweaks for the QemuLogFile type returned by atomic_rcu_read(). Does this look OK or is there any other adjustment we should make here? The intent here was for qemu_log_lock() to hold the rcu_read_lock() until after we can qemu_funlockfile(fd). The idea being that we want to prevent the fclose(fd) from happening by holding the rcu_read_lock() across the qemu_log_lock() until qemu_log_unlock(). So we have the qemu_funlockfile(fd) first, and then once we're done with the fd, it is safe to rcu_read_unlock(). > > > diff --git a/util/log.c b/util/log.c > > @@ -65,6 +70,8 @@ static bool log_uses_own_buffers; > > /* enable or disable low levels log */ > > void qemu_set_log(int log_flags) > > { > > +QemuLogFile *logfile; > > + > > qemu_loglevel = log_flags; > > #ifdef CONFIG_TRACE_LOG > > qemu_loglevel |= LOG_TRACE; > > @@ -77,43 +84,51 @@ void qemu_set_log(int log_flags) > > qemu_mutex_lock(&qemu_logfile_mutex); > > if (!qemu_logfile && > > (is_daemonized() ? logfilename != NULL : qemu_loglevel)) { > > +logfile = g_new0(QemuLogFile, 1); > > if (logfilename) { > > You can assume logfilename exists as glib memory allocations would > abort() otherwise. This is good to know about the glib memory allocations as it relates to the logfile above, since we did not add any check for null on allocation. We are thinking that logfilename might be NULL if we either never set it with a call to qemu_set_log_filename(), or (with our intended new fix) if we took an error actually opening the file in qemu_set_log_fi
Re: [PATCH 2/4] Add use of RCU for qemu_logfile.
Robert Foley writes: > This now allows changing the logfile while logging is active, > and also solves the issue of a seg fault while changing the logfile. > > Any read access to the qemu_logfile handle will use > the rcu_read_lock()/unlock() around the use of the handle. > To fetch the handle we will use atomic_rcu_read(). > We also in many cases do a check for validity of the > logfile handle before using it to deal with the case where the > file is closed and set to NULL. > > The cases where we write to the qemu_logfile will use atomic_rcu_set(). > Writers will also use call_rcu() with a newly added qemu_logfile_free > function for freeing/closing when readers have finished. > > Signed-off-by: Robert Foley > --- > include/qemu/log.h | 47 > util/log.c | 78 ++ > include/exec/log.h | 33 +--- > tcg/tcg.c | 12 +-- > 4 files changed, 138 insertions(+), 32 deletions(-) > > diff --git a/include/qemu/log.h b/include/qemu/log.h > index a91105b2ad..975de18e23 100644 > --- a/include/qemu/log.h > +++ b/include/qemu/log.h > @@ -3,9 +3,17 @@ > > /* A small part of this API is split into its own header */ > #include "qemu/log-for-trace.h" > +#include "qemu/rcu.h" > + > +struct QemuLogFile { > +struct rcu_head rcu; > +FILE *fd; > +}; > +typedef struct QemuLogFile QemuLogFile; If you are declaring a typedef then do it in one: typedef struct QemuLogFile { ... We only really use the second form for opaque structs where the handle is passed around but the contents hidden from the rest of QEMU. > > /* Private global variable, don't use */ > -extern FILE *qemu_logfile; > +extern QemuLogFile *qemu_logfile; > + Do we need multiple > > /* > * The new API: > @@ -25,7 +33,17 @@ static inline bool qemu_log_enabled(void) > */ > static inline bool qemu_log_separate(void) > { > -return qemu_logfile != NULL && qemu_logfile != stderr; > +QemuLogFile *logfile; > + > +if (qemu_log_enabled()) { > +rcu_read_lock(); > +logfile = atomic_rcu_read(&qemu_logfile); > +if (logfile && logfile->fd != stderr) { > +return true; > +} > +rcu_read_unlock(); > +} > +return false; This is unbalanced as you'll have incremented the reader count. Also qemu_log_enabled() is also synonymous with logfile existing so you could fold that into: bool res = false; rcu_read_lock(); *logfile = atomic_rcu_read(&qemu_logfile); if (logfile && logfile->fd != stderr) { res = true; } rcu_read_unlock(); return res; There is technically a race there as the answer may become invalid after qemu_log_separate() returns. However given the users I don't see what could fail afterwards. > } > > #define CPU_LOG_TB_OUT_ASM (1 << 0) > @@ -55,12 +73,23 @@ static inline bool qemu_log_separate(void) > > static inline void qemu_log_lock(void) > { > -qemu_flockfile(qemu_logfile); > +QemuLogFile *logfile; > +rcu_read_lock(); > +logfile = atomic_rcu_read(&qemu_logfile); > +if (logfile) { > +qemu_flockfile(logfile->fd); > +} > +rcu_read_unlock(); > } static inline FILE *fd qemu_log_lock(void) { FILE *fd; rcu_read_lock(); fd = atomic_rcu_read(&qemu_logfile); if (fd) { qemu_flockfile(fd); return fd; } else { rcu_read_unlock(); return NULL; } } static inline qemu_log_unlock(FILE *fd) { if (fd) { qemu_funlockfile(fd); rcu_read_unlock(); } } While the rcu_read_lock() is in progress then we won't ever finish with the fd - which we don't want to do until the file locking is finished. > > /* Logging functions: */ > @@ -70,9 +99,14 @@ static inline void qemu_log_unlock(void) > static inline void GCC_FMT_ATTR(1, 0) > qemu_log_vprintf(const char *fmt, va_list va) > { > -if (qemu_logfile) { > -vfprintf(qemu_logfile, fmt, va); > +QemuLogFile *logfile; > + > +rcu_read_lock(); > +logfile = atomic_rcu_read(&qemu_logfile); > +if (logfile) { > +vfprintf(logfile->fd, fmt, va); > } > +rcu_read_unlock(); > } > > /* log only if a bit is set on the current loglevel mask: > @@ -129,5 +163,6 @@ void qemu_print_log_usage(FILE *f); > void qemu_log_flush(void); > /* Close the log file */ > void qemu_log_close(void); > +void qemu_logfile_free(QemuLogFile *logfile); > > #endif > diff --git a/util/log.c b/util/log.c > index dff2f98c8c..d409532b8f 100644 > --- a/util/log.c > +++ b/util/log.c > @@ -29,7 +29,7 @@ > static char *logfilename; > static bool qemu_logfile_initialized; > static QemuMutex qemu_logfile_mutex; > -FILE *qemu_logfile; > +QemuLogFile *qemu_logfile; > int qemu_loglevel; > static int log_append = 0; > static GArray *debug_regions; > @@ -38,10 +38,14 @@ static GArray *debug_regions; > int qemu_log(const char *fmt, ...) > { > int ret = 0; > -if (qemu_logfile) { > +QemuLogFil
[PATCH 2/4] Add use of RCU for qemu_logfile.
This now allows changing the logfile while logging is active, and also solves the issue of a seg fault while changing the logfile. Any read access to the qemu_logfile handle will use the rcu_read_lock()/unlock() around the use of the handle. To fetch the handle we will use atomic_rcu_read(). We also in many cases do a check for validity of the logfile handle before using it to deal with the case where the file is closed and set to NULL. The cases where we write to the qemu_logfile will use atomic_rcu_set(). Writers will also use call_rcu() with a newly added qemu_logfile_free function for freeing/closing when readers have finished. Signed-off-by: Robert Foley --- include/qemu/log.h | 47 util/log.c | 78 ++ include/exec/log.h | 33 +--- tcg/tcg.c | 12 +-- 4 files changed, 138 insertions(+), 32 deletions(-) diff --git a/include/qemu/log.h b/include/qemu/log.h index a91105b2ad..975de18e23 100644 --- a/include/qemu/log.h +++ b/include/qemu/log.h @@ -3,9 +3,17 @@ /* A small part of this API is split into its own header */ #include "qemu/log-for-trace.h" +#include "qemu/rcu.h" + +struct QemuLogFile { +struct rcu_head rcu; +FILE *fd; +}; +typedef struct QemuLogFile QemuLogFile; /* Private global variable, don't use */ -extern FILE *qemu_logfile; +extern QemuLogFile *qemu_logfile; + /* * The new API: @@ -25,7 +33,17 @@ static inline bool qemu_log_enabled(void) */ static inline bool qemu_log_separate(void) { -return qemu_logfile != NULL && qemu_logfile != stderr; +QemuLogFile *logfile; + +if (qemu_log_enabled()) { +rcu_read_lock(); +logfile = atomic_rcu_read(&qemu_logfile); +if (logfile && logfile->fd != stderr) { +return true; +} +rcu_read_unlock(); +} +return false; } #define CPU_LOG_TB_OUT_ASM (1 << 0) @@ -55,12 +73,23 @@ static inline bool qemu_log_separate(void) static inline void qemu_log_lock(void) { -qemu_flockfile(qemu_logfile); +QemuLogFile *logfile; +rcu_read_lock(); +logfile = atomic_rcu_read(&qemu_logfile); +if (logfile) { +qemu_flockfile(logfile->fd); +} +rcu_read_unlock(); } static inline void qemu_log_unlock(void) { -qemu_funlockfile(qemu_logfile); +QemuLogFile *logfile; +logfile = atomic_rcu_read(&qemu_logfile); +if (logfile) { +qemu_funlockfile(logfile->fd); +} +rcu_read_unlock(); } /* Logging functions: */ @@ -70,9 +99,14 @@ static inline void qemu_log_unlock(void) static inline void GCC_FMT_ATTR(1, 0) qemu_log_vprintf(const char *fmt, va_list va) { -if (qemu_logfile) { -vfprintf(qemu_logfile, fmt, va); +QemuLogFile *logfile; + +rcu_read_lock(); +logfile = atomic_rcu_read(&qemu_logfile); +if (logfile) { +vfprintf(logfile->fd, fmt, va); } +rcu_read_unlock(); } /* log only if a bit is set on the current loglevel mask: @@ -129,5 +163,6 @@ void qemu_print_log_usage(FILE *f); void qemu_log_flush(void); /* Close the log file */ void qemu_log_close(void); +void qemu_logfile_free(QemuLogFile *logfile); #endif diff --git a/util/log.c b/util/log.c index dff2f98c8c..d409532b8f 100644 --- a/util/log.c +++ b/util/log.c @@ -29,7 +29,7 @@ static char *logfilename; static bool qemu_logfile_initialized; static QemuMutex qemu_logfile_mutex; -FILE *qemu_logfile; +QemuLogFile *qemu_logfile; int qemu_loglevel; static int log_append = 0; static GArray *debug_regions; @@ -38,10 +38,14 @@ static GArray *debug_regions; int qemu_log(const char *fmt, ...) { int ret = 0; -if (qemu_logfile) { +QemuLogFile *logfile; + +rcu_read_lock(); +logfile = atomic_rcu_read(&qemu_logfile); +if (logfile) { va_list ap; va_start(ap, fmt); -ret = vfprintf(qemu_logfile, fmt, ap); +ret = vfprintf(logfile->fd, fmt, ap); va_end(ap); /* Don't pass back error results. */ @@ -49,6 +53,7 @@ int qemu_log(const char *fmt, ...) ret = 0; } } +rcu_read_unlock(); return ret; } @@ -65,6 +70,8 @@ static bool log_uses_own_buffers; /* enable or disable low levels log */ void qemu_set_log(int log_flags) { +QemuLogFile *logfile; + qemu_loglevel = log_flags; #ifdef CONFIG_TRACE_LOG qemu_loglevel |= LOG_TRACE; @@ -77,43 +84,51 @@ void qemu_set_log(int log_flags) qemu_mutex_lock(&qemu_logfile_mutex); if (!qemu_logfile && (is_daemonized() ? logfilename != NULL : qemu_loglevel)) { +logfile = g_new0(QemuLogFile, 1); if (logfilename) { -qemu_logfile = fopen(logfilename, log_append ? "a" : "w"); -if (!qemu_logfile) { +logfile->fd = fopen(logfilename, log_append ? "a" : "w"); +if (!logfile->fd) { +g_free(logfile); perror(logfilename); _exit(1);