qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Richard Henderson <richard.henderson@linaro.org>
To: qemu-devel@nongnu.org
Cc: "Alex Bennée" <alex.bennee@linaro.org>
Subject: [PULL 07/39] util/log: Rename qemu_log_lock to qemu_log_trylock
Date: Wed, 20 Apr 2022 11:05:46 -0700	[thread overview]
Message-ID: <20220420180618.1183855-8-richard.henderson@linaro.org> (raw)
In-Reply-To: <20220420180618.1183855-1-richard.henderson@linaro.org>

This function can fail, which makes it more like ftrylockfile
or pthread_mutex_trylock than flockfile or pthread_mutex_lock,
so rename it.

To closer match the other trylock functions, release rcu_read_lock
along the failure path, so that qemu_log_unlock need not be called
on failure.

Reviewed-by: Alex Bennée <alex.bennee@linaro.org>
Signed-off-by: Richard Henderson <richard.henderson@linaro.org>
Message-Id: <20220417183019.755276-8-richard.henderson@linaro.org>
---
 include/exec/log.h          | 2 +-
 include/qemu/log.h          | 2 +-
 accel/tcg/cpu-exec.c        | 2 +-
 accel/tcg/translate-all.c   | 2 +-
 accel/tcg/translator.c      | 2 +-
 cpu.c                       | 2 +-
 hw/net/can/can_sja1000.c    | 2 +-
 net/can/can_socketcan.c     | 2 +-
 target/i386/tcg/translate.c | 2 +-
 tcg/tcg.c                   | 8 ++++----
 tests/unit/test-logging.c   | 2 +-
 util/log.c                  | 6 ++++--
 12 files changed, 18 insertions(+), 16 deletions(-)

diff --git a/include/exec/log.h b/include/exec/log.h
index 3c7fa65ead..fb595d1eb7 100644
--- a/include/exec/log.h
+++ b/include/exec/log.h
@@ -71,7 +71,7 @@ static inline void log_disas(const void *code, unsigned long size)
 /* page_dump() output to the log file: */
 static inline void log_page_dump(const char *operation)
 {
-    FILE *logfile = qemu_log_lock();
+    FILE *logfile = qemu_log_trylock();
     if (logfile) {
         qemu_log("page layout changed following %s\n", operation);
         page_dump(logfile);
diff --git a/include/qemu/log.h b/include/qemu/log.h
index 6a6b1efce5..d090faf22a 100644
--- a/include/qemu/log.h
+++ b/include/qemu/log.h
@@ -67,7 +67,7 @@ static inline bool qemu_log_separate(void)
 
 /* Lock/unlock output. */
 
-FILE *qemu_log_lock(void);
+FILE *qemu_log_trylock(void);
 void qemu_log_unlock(FILE *fd);
 
 /* Logging functions: */
diff --git a/accel/tcg/cpu-exec.c b/accel/tcg/cpu-exec.c
index 083ada76b1..f61b056571 100644
--- a/accel/tcg/cpu-exec.c
+++ b/accel/tcg/cpu-exec.c
@@ -214,7 +214,7 @@ static inline void log_cpu_exec(target_ulong pc, CPUState *cpu,
 
 #if defined(DEBUG_DISAS)
         if (qemu_loglevel_mask(CPU_LOG_TB_CPU)) {
-            FILE *logfile = qemu_log_lock();
+            FILE *logfile = qemu_log_trylock();
             int flags = 0;
 
             if (qemu_loglevel_mask(CPU_LOG_TB_FPU)) {
diff --git a/accel/tcg/translate-all.c b/accel/tcg/translate-all.c
index a6e03c1e50..5937d6b7b2 100644
--- a/accel/tcg/translate-all.c
+++ b/accel/tcg/translate-all.c
@@ -1527,7 +1527,7 @@ TranslationBlock *tb_gen_code(CPUState *cpu,
 #ifdef DEBUG_DISAS
     if (qemu_loglevel_mask(CPU_LOG_TB_OUT_ASM) &&
         qemu_log_in_addr_range(tb->pc)) {
-        FILE *logfile = qemu_log_lock();
+        FILE *logfile = qemu_log_trylock();
         int code_size, data_size;
         const tcg_target_ulong *rx_data_gen_ptr;
         size_t chunk_start;
diff --git a/accel/tcg/translator.c b/accel/tcg/translator.c
index f06c314266..1506755f3c 100644
--- a/accel/tcg/translator.c
+++ b/accel/tcg/translator.c
@@ -139,7 +139,7 @@ void translator_loop(const TranslatorOps *ops, DisasContextBase *db,
 #ifdef DEBUG_DISAS
     if (qemu_loglevel_mask(CPU_LOG_TB_IN_ASM)
         && qemu_log_in_addr_range(db->pc_first)) {
-        FILE *logfile = qemu_log_lock();
+        FILE *logfile = qemu_log_trylock();
         qemu_log("----------------\n");
         ops->disas_log(db, cpu);
         qemu_log("\n");
diff --git a/cpu.c b/cpu.c
index 0fef70e18e..9db144761d 100644
--- a/cpu.c
+++ b/cpu.c
@@ -399,7 +399,7 @@ void cpu_abort(CPUState *cpu, const char *fmt, ...)
     fprintf(stderr, "\n");
     cpu_dump_state(cpu, stderr, CPU_DUMP_FPU | CPU_DUMP_CCOP);
     if (qemu_log_separate()) {
-        FILE *logfile = qemu_log_lock();
+        FILE *logfile = qemu_log_trylock();
         qemu_log("qemu: fatal: ");
         qemu_log_vprintf(fmt, ap2);
         qemu_log("\n");
diff --git a/hw/net/can/can_sja1000.c b/hw/net/can/can_sja1000.c
index 3ba803e947..300313dfb8 100644
--- a/hw/net/can/can_sja1000.c
+++ b/hw/net/can/can_sja1000.c
@@ -247,7 +247,7 @@ int can_sja_accept_filter(CanSJA1000State *s,
 static void can_display_msg(const char *prefix, const qemu_can_frame *msg)
 {
     int i;
-    FILE *logfile = qemu_log_lock();
+    FILE *logfile = qemu_log_trylock();
 
     qemu_log("%s%03X [%01d] %s %s",
              prefix,
diff --git a/net/can/can_socketcan.c b/net/can/can_socketcan.c
index 4b68f60c6b..4a505643e9 100644
--- a/net/can/can_socketcan.c
+++ b/net/can/can_socketcan.c
@@ -76,7 +76,7 @@ QEMU_BUILD_BUG_ON(offsetof(qemu_can_frame, data)
 static void can_host_socketcan_display_msg(struct qemu_can_frame *msg)
 {
     int i;
-    FILE *logfile = qemu_log_lock();
+    FILE *logfile = qemu_log_trylock();
     qemu_log("[cansocketcan]: %03X [%01d] %s %s",
              msg->can_id & QEMU_CAN_EFF_MASK,
              msg->can_dlc,
diff --git a/target/i386/tcg/translate.c b/target/i386/tcg/translate.c
index 3ba1c99fff..f3cffee520 100644
--- a/target/i386/tcg/translate.c
+++ b/target/i386/tcg/translate.c
@@ -2580,7 +2580,7 @@ static void gen_unknown_opcode(CPUX86State *env, DisasContext *s)
     gen_illegal_opcode(s);
 
     if (qemu_loglevel_mask(LOG_UNIMP)) {
-        FILE *logfile = qemu_log_lock();
+        FILE *logfile = qemu_log_trylock();
         target_ulong pc = s->pc_start, end = s->pc;
 
         qemu_log("ILLOPC: " TARGET_FMT_lx ":", pc);
diff --git a/tcg/tcg.c b/tcg/tcg.c
index f8542529d0..abe4ef7799 100644
--- a/tcg/tcg.c
+++ b/tcg/tcg.c
@@ -756,7 +756,7 @@ void tcg_prologue_init(TCGContext *s)
 
 #ifdef DEBUG_DISAS
     if (qemu_loglevel_mask(CPU_LOG_TB_OUT_ASM)) {
-        FILE *logfile = qemu_log_lock();
+        FILE *logfile = qemu_log_trylock();
         qemu_log("PROLOGUE: [size=%zu]\n", prologue_size);
         if (s->data_gen_ptr) {
             size_t code_size = s->data_gen_ptr - s->code_gen_ptr;
@@ -4200,7 +4200,7 @@ int tcg_gen_code(TCGContext *s, TranslationBlock *tb)
 #ifdef DEBUG_DISAS
     if (unlikely(qemu_loglevel_mask(CPU_LOG_TB_OP)
                  && qemu_log_in_addr_range(tb->pc))) {
-        FILE *logfile = qemu_log_lock();
+        FILE *logfile = qemu_log_trylock();
         qemu_log("OP:\n");
         tcg_dump_ops(s, false);
         qemu_log("\n");
@@ -4245,7 +4245,7 @@ int tcg_gen_code(TCGContext *s, TranslationBlock *tb)
 #ifdef DEBUG_DISAS
         if (unlikely(qemu_loglevel_mask(CPU_LOG_TB_OP_IND)
                      && qemu_log_in_addr_range(tb->pc))) {
-            FILE *logfile = qemu_log_lock();
+            FILE *logfile = qemu_log_trylock();
             qemu_log("OP before indirect lowering:\n");
             tcg_dump_ops(s, false);
             qemu_log("\n");
@@ -4266,7 +4266,7 @@ int tcg_gen_code(TCGContext *s, TranslationBlock *tb)
 #ifdef DEBUG_DISAS
     if (unlikely(qemu_loglevel_mask(CPU_LOG_TB_OP_OPT)
                  && qemu_log_in_addr_range(tb->pc))) {
-        FILE *logfile = qemu_log_lock();
+        FILE *logfile = qemu_log_trylock();
         qemu_log("OP after optimization and liveness analysis:\n");
         tcg_dump_ops(s, true);
         qemu_log("\n");
diff --git a/tests/unit/test-logging.c b/tests/unit/test-logging.c
index d071400ebc..4c595356ae 100644
--- a/tests/unit/test-logging.c
+++ b/tests/unit/test-logging.c
@@ -162,7 +162,7 @@ static void test_logfile_lock(gconstpointer data)
      * our handle remains valid for use due to RCU.
      */
     qemu_set_log_filename(file_path, &error_abort);
-    logfile = qemu_log_lock();
+    logfile = qemu_log_trylock();
     g_assert(logfile);
     fprintf(logfile, "%s 1st write to file\n", __func__);
     fflush(logfile);
diff --git a/util/log.c b/util/log.c
index 1a3121610a..6b7b358573 100644
--- a/util/log.c
+++ b/util/log.c
@@ -36,15 +36,17 @@ static GArray *debug_regions;
 
 /* Lock/unlock output. */
 
-FILE *qemu_log_lock(void)
+FILE *qemu_log_trylock(void)
 {
     QemuLogFile *logfile;
+
     rcu_read_lock();
     logfile = qatomic_rcu_read(&qemu_logfile);
     if (logfile) {
         qemu_flockfile(logfile->fd);
         return logfile->fd;
     } else {
+        rcu_read_unlock();
         return NULL;
     }
 }
@@ -53,8 +55,8 @@ void qemu_log_unlock(FILE *fd)
 {
     if (fd) {
         qemu_funlockfile(fd);
+        rcu_read_unlock();
     }
-    rcu_read_unlock();
 }
 
 /* Return the number of characters emitted.  */
-- 
2.34.1



  parent reply	other threads:[~2022-04-20 18:11 UTC|newest]

Thread overview: 41+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-04-20 18:05 [PULL 00/39] Logging cleanup and per-thread logfiles Richard Henderson
2022-04-20 18:05 ` [PULL 01/39] util/log: Drop manual log buffering Richard Henderson
2022-04-20 18:05 ` [PULL 02/39] target/hexagon: Remove qemu_set_log in hexagon_translate_init Richard Henderson
2022-04-20 18:05 ` [PULL 03/39] util/log: Return bool from qemu_set_log_filename Richard Henderson
2022-04-20 18:05 ` [PULL 04/39] util/log: Pass Error pointer to qemu_set_log Richard Henderson
2022-04-20 18:05 ` [PULL 05/39] os-posix: Use qemu_log_enabled Richard Henderson
2022-04-20 18:05 ` [PULL 06/39] util/log: Move qemu_log_lock, qemu_log_unlock out of line Richard Henderson
2022-04-20 18:05 ` Richard Henderson [this message]
2022-04-20 18:05 ` [PULL 08/39] hw/xen: Split out xen_pv_output_msg Richard Henderson
2022-04-20 18:05 ` [PULL 09/39] *: Use fprintf between qemu_log_trylock/unlock Richard Henderson
2022-04-20 18:05 ` [PULL 10/39] util/log: Remove qemu_log_vprintf Richard Henderson
2022-04-20 18:05 ` [PULL 11/39] tcg: Pass the locked filepointer to tcg_dump_ops Richard Henderson
2022-04-20 18:05 ` [PULL 12/39] exec/translator: Pass the locked filepointer to disas_log hook Richard Henderson
2022-04-20 18:05 ` [PULL 13/39] exec/log: Remove log_disas and log_target_disas Richard Henderson
2022-04-20 18:05 ` [PULL 14/39] accel/tcg: Use cpu_dump_state between qemu_log_trylock/unlock Richard Henderson
2022-04-20 18:05 ` [PULL 15/39] target/nios2: Remove log_cpu_state from reset Richard Henderson
2022-04-20 18:05 ` [PULL 16/39] util/log: Use qemu_log_trylock/unlock in qemu_log Richard Henderson
2022-04-20 18:05 ` [PULL 17/39] util/log: Drop return value from qemu_log Richard Henderson
2022-04-20 18:05 ` [PULL 18/39] util/log: Mark qemu_log_trylock as G_GNUC_WARN_UNUSED_RESULT Richard Henderson
2022-04-20 18:05 ` [PULL 19/39] util/log: Remove qemu_log_flush Richard Henderson
2022-04-20 18:05 ` [PULL 20/39] util/log: Drop call to setvbuf Richard Henderson
2022-04-20 18:06 ` [PULL 21/39] bsd-user: Expand log_page_dump inline Richard Henderson
2022-04-20 18:06 ` [PULL 22/39] linux-user: " Richard Henderson
2022-04-20 18:06 ` [PULL 23/39] tests/unit: Do not reference QemuLogFile directly Richard Henderson
2022-04-20 18:06 ` [PULL 24/39] include/exec/log: " Richard Henderson
2022-04-20 18:06 ` [PULL 25/39] include/qemu/log: Move entire implementation out-of-line Richard Henderson
2022-04-20 18:06 ` [PULL 26/39] sysemu/os-win32: Test for and use _lock_file/_unlock_file Richard Henderson
2022-04-20 18:06 ` [PULL 27/39] util/log: Introduce qemu_set_log_filename_flags Richard Henderson
2022-04-20 18:06 ` [PULL 28/39] bsd-user: Use qemu_set_log_filename_flags Richard Henderson
2022-04-20 18:06 ` [PULL 29/39] linux-user: " Richard Henderson
2022-04-20 18:06 ` [PULL 30/39] softmmu: " Richard Henderson
2022-04-20 18:06 ` [PULL 31/39] util/log: Remove qemu_log_close Richard Henderson
2022-04-20 18:06 ` [PULL 32/39] util/log: Rename logfilename to global_filename Richard Henderson
2022-04-20 18:06 ` [PULL 33/39] util/log: Rename qemu_logfile to global_file Richard Henderson
2022-04-20 18:06 ` [PULL 34/39] util/log: Rename qemu_logfile_mutex to global_mutex Richard Henderson
2022-04-20 18:06 ` [PULL 35/39] util/log: Hoist the eval of is_daemonized in qemu_set_log_internal Richard Henderson
2022-04-20 18:06 ` [PULL 36/39] util/log: Combine two logfile closes Richard Henderson
2022-04-20 18:06 ` [PULL 37/39] util/log: Rename QemuLogFile to RCUCloseFILE Richard Henderson
2022-04-20 18:06 ` [PULL 38/39] util/log: Limit RCUCloseFILE to file closing Richard Henderson
2022-04-20 18:06 ` [PULL 39/39] util/log: Support per-thread log files Richard Henderson
2022-04-20 23:42 ` [PULL 00/39] Logging cleanup and per-thread logfiles Richard Henderson

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220420180618.1183855-8-richard.henderson@linaro.org \
    --to=richard.henderson@linaro.org \
    --cc=alex.bennee@linaro.org \
    --cc=qemu-devel@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).