From patchwork Sat Mar 26 13:25:33 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 554373 Delivered-To: patch@linaro.org Received: by 2002:a05:7000:1248:0:0:0:0 with SMTP id z8csp6442807mag; Sat, 26 Mar 2022 06:58:20 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyk7P2t1EIRi5aEMc0NLpU5L4pNO/LZN3cvnDubWlBsHo97Vmn7Stx3f8E9gdvvvquHpFrp X-Received: by 2002:a05:622a:8b:b0:2e0:75f0:c18d with SMTP id o11-20020a05622a008b00b002e075f0c18dmr14101261qtw.488.1648303100744; Sat, 26 Mar 2022 06:58:20 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1648303100; cv=none; d=google.com; s=arc-20160816; b=U3ZhRLXoe0kFCkdi+vxzVrIi9tPd4E65YmjudtDugY21WvIqY01bXikkh6a76oeZqO PQIGcftgEZHobBWXFO4Up1mr33nTJGA/354gtcAVxNkVPlWxw4kRFrzJbCyeTY8vYeh4 dvvkyLLrYcbr23SFw08ZzxhUPutPCdgpPLpfv8jswdOBTnlofcx1FJfrj16uuLevctfi 6Lh4s87u/EqKSKj7tZ8Qe4GYtNofy2jr+kElzG+iR1Xhgqzq3rzob7uJ8rNOiRFnirDi XoPn0V6ZE0vEBPtsps75cG98fQ4LPPolUwcK8EU3xcRCaCtSLDPhORivdswMlpmA4oNK WiEQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:cc:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:to:from :dkim-signature; bh=aXHC8lLdzIN7Te/CRAft3NVYbrUQIuuG52xMXpJ9gWU=; b=ylzqfj/ocHQKrf5hjG57k+baj74FIHya+H70bvjAXX0aeq0yu0OjtCUdx+4AmEV8+h WUK9a4FFPtlvqdWkSvesDhGpnxmS7UiDdbt//4AS7cyOQ/J53b0ah7nnJon3zGHWfhSP Lf1nH36N42fZ7Y+2bu3P6gzykJWnY4dgU6tEQrT7pIK4RJ/vLOD+9uKv04zfPLBAenmX UBbY5GgbUV/ZnASXawPdZC246sCdOC7jfkIcLVj+t9vW9Hv6YZtekkIEv/hdEu/7/wDn ub/ulmX3TXPBAzJNxq39B1oIFarVcyQc+wNgBuhvG5hljmHRBBNYJE7bBAtSy03wwzvF 6dlw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=Lunsatfu; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from lists.gnu.org (lists.gnu.org. [209.51.188.17]) by mx.google.com with ESMTPS id by1-20020a05621405a100b00440e46a7b7asi3372019qvb.24.2022.03.26.06.58.20 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Sat, 26 Mar 2022 06:58:20 -0700 (PDT) Received-SPF: pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=Lunsatfu; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from localhost ([::1]:39402 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1nY6vs-0007UY-8x for patch@linaro.org; Sat, 26 Mar 2022 09:58:20 -0400 Received: from eggs.gnu.org ([209.51.188.92]:34902) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nY6UG-0002yt-Ev for qemu-devel@nongnu.org; Sat, 26 Mar 2022 09:29:49 -0400 Received: from [2001:4860:4864:20::2b] (port=43761 helo=mail-oa1-x2b.google.com) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1nY6UE-0005zS-TC for qemu-devel@nongnu.org; Sat, 26 Mar 2022 09:29:48 -0400 Received: by mail-oa1-x2b.google.com with SMTP id 586e51a60fabf-de2cb87f6aso10708639fac.10 for ; Sat, 26 Mar 2022 06:29:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=aXHC8lLdzIN7Te/CRAft3NVYbrUQIuuG52xMXpJ9gWU=; b=LunsatfujAtg43ZGgevckQNX/CqHKuYL6aBGWGKcvEyz+MoX5JaM7kUf+WTkIj5gx2 PE55uFgHMsL81mjt34cdgzxKJobhvCrDo0qUwjDMdPXG+MHW+gkVQintXOCF4eM7X8Rx VE/WJ84uGfAtPG92LUqP2+P59n33MgcyKIy45jfQn02ITZMQ6/BiEtbE79uorvH1r7CV naxJDtz78LKu+ksFWSWB5T7AeULZ++LgEfw3hkOKnbpoil6yarqW0KbhRcci1VsP1fI/ /oLHLAOqhjjZqvAWbta8A0tInF191bcnOva7lT03gawAWwJt9sjzDExj1lFMh0Sqo6r7 w5Bg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=aXHC8lLdzIN7Te/CRAft3NVYbrUQIuuG52xMXpJ9gWU=; b=5aH8eP0frvlQytiAA8Vgoe0z69PJ0EW6NxJufK8egGDpdqbimf7rlezH9PP9hla2cg SSy9ZVXvnOqFvNHjwvR0e4T4EwrMfRaUITgNW6MBSV/pxxV/9IKrkkauCSvDnyLKuCeK aMZGyYaIHxa/OgC4Cs5IyobHcwFnGFMgJfVfyYs8f8WE48wVlwx/Fy1ORvNOzNLoPUmS YXALvbQw84bwrj9WOZcSipnxyLx0JsGhl3ARmU3+JsqVhaA3Rh3xt9cNv1CFcypFj5ps Uu99ZzmESL7dpMWWOeEvTnXGYTZJgzGP45ObcBAfweYliygT0/aI4bJUpahYBImwhVTX zkrg== X-Gm-Message-State: AOAM532dYJ8F+xjm3PEkFenP54FkRlBXNPIacp2wmZGM2BSVTdSBINpe 82N26aQ99063HfAs0NxPSyMF5tV4LZ2KrB2m X-Received: by 2002:a05:6871:28b:b0:de:282:34ef with SMTP id i11-20020a056871028b00b000de028234efmr7076648oae.287.1648301385957; Sat, 26 Mar 2022 06:29:45 -0700 (PDT) Received: from localhost.localdomain (168.189-204-159.bestelclientes.com.mx. [189.204.159.168]) by smtp.gmail.com with ESMTPSA id s24-20020a056808209800b002da3b9bf8e0sm4394509oiw.32.2022.03.26.06.29.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 26 Mar 2022 06:29:45 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Subject: [PATCH v2 38/39] util/log: Limit RCUCloseFILE to file closing Date: Sat, 26 Mar 2022 07:25:33 -0600 Message-Id: <20220326132534.543738-47-richard.henderson@linaro.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220326132534.543738-1-richard.henderson@linaro.org> References: <20220326132534.543738-1-richard.henderson@linaro.org> MIME-Version: 1.0 X-Host-Lookup-Failed: Reverse DNS lookup failed for 2001:4860:4864:20::2b (failed) Received-SPF: pass client-ip=2001:4860:4864:20::2b; envelope-from=richard.henderson@linaro.org; helo=mail-oa1-x2b.google.com X-Spam_score_int: -6 X-Spam_score: -0.7 X-Spam_bar: / X-Spam_report: (-0.7 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, PDS_HP_HELO_NORDNS=0.659, RDNS_NONE=0.793, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=no autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: alex.bennee@linaro.org Errors-To: qemu-devel-bounces+patch=linaro.org@nongnu.org Sender: "Qemu-devel" Use FILE* for global_file. We can perform an rcu_read on that just as easily as RCUCloseFILE*. This simplifies a couple of places, where previously we required taking the rcu_read_lock simply to avoid racing to dereference RCUCloseFile->fd. Only allocate the RCUCloseFile prior to call_rcu. Signed-off-by: Richard Henderson Reviewed-by: Alex Bennée --- v2: Work around problem with qatomic_rcu_read vs struct _IO_FILE on musl. --- util/log.c | 68 +++++++++++++++++++++++++----------------------------- 1 file changed, 32 insertions(+), 36 deletions(-) diff --git a/util/log.c b/util/log.c index eb6e85b2f0..df0710720f 100644 --- a/util/log.c +++ b/util/log.c @@ -37,7 +37,7 @@ typedef struct RCUCloseFILE { /* Mutex covering the other global_* variables. */ static QemuMutex global_mutex; static char *global_filename; -static RCUCloseFILE *global_file; +static FILE *global_file; int qemu_loglevel; static int log_append = 0; @@ -46,46 +46,44 @@ static GArray *debug_regions; /* Returns true if qemu_log() will really write somewhere. */ bool qemu_log_enabled(void) { - return global_file != NULL; + return qatomic_read(&global_file) != NULL; } /* Returns true if qemu_log() will write somewhere other than stderr. */ bool qemu_log_separate(void) { - RCUCloseFILE *logfile; - bool res = false; - - rcu_read_lock(); - logfile = qatomic_rcu_read(&global_file); - if (logfile && logfile->fd != stderr) { - res = true; - } - rcu_read_unlock(); - return res; + FILE *logfile = qatomic_read(&global_file); + return logfile && logfile != stderr; } /* Lock/unlock output. */ FILE *qemu_log_trylock(void) { - RCUCloseFILE *logfile; + FILE *logfile; rcu_read_lock(); - logfile = qatomic_rcu_read(&global_file); + /* + * FIXME: typeof_strip_qual, as used by qatomic_rcu_read, + * does not work with pointers to undefined structures, + * such as we have with struct _IO_FILE and musl libc. + * Since all we want is a read of a pointer, cast to void**, + * which does work with typeof_strip_qual. + */ + logfile = qatomic_rcu_read((void **)&global_file); if (logfile) { - qemu_flockfile(logfile->fd); - return logfile->fd; + qemu_flockfile(logfile); } else { rcu_read_unlock(); - return NULL; } + return logfile; } -void qemu_log_unlock(FILE *fd) +void qemu_log_unlock(FILE *logfile) { - if (fd) { - fflush(fd); - qemu_funlockfile(fd); + if (logfile) { + fflush(logfile); + qemu_funlockfile(logfile); rcu_read_unlock(); } } @@ -110,9 +108,7 @@ static void __attribute__((__constructor__)) startup(void) static void rcu_close_file(RCUCloseFILE *r) { - if (r->fd != stderr) { - fclose(r->fd); - } + fclose(r->fd); g_free(r); } @@ -122,7 +118,7 @@ static bool qemu_set_log_internal(const char *filename, bool changed_name, { bool need_to_open_file; bool daemonized; - RCUCloseFILE *logfile; + FILE *logfile; QEMU_LOCK_GUARD(&global_mutex); logfile = global_file; @@ -176,37 +172,37 @@ static bool qemu_set_log_internal(const char *filename, bool changed_name, if (logfile && (!need_to_open_file || changed_name)) { qatomic_rcu_set(&global_file, NULL); - call_rcu(logfile, rcu_close_file, rcu); + if (logfile != stderr) { + RCUCloseFILE *r = g_new0(RCUCloseFILE, 1); + r->fd = logfile; + call_rcu(r, rcu_close_file, rcu); + } logfile = NULL; } if (!logfile && need_to_open_file) { - FILE *fd; - if (filename) { - fd = fopen(filename, log_append ? "a" : "w"); - if (!fd) { + logfile = fopen(filename, log_append ? "a" : "w"); + if (!logfile) { error_setg_errno(errp, errno, "Error opening logfile %s", filename); return false; } /* In case we are a daemon redirect stderr to logfile */ if (daemonized) { - dup2(fileno(fd), STDERR_FILENO); - fclose(fd); + dup2(fileno(logfile), STDERR_FILENO); + fclose(logfile); /* This will skip closing logfile in rcu_close_file. */ - fd = stderr; + logfile = stderr; } } else { /* Default to stderr if no log file specified */ assert(!daemonized); - fd = stderr; + logfile = stderr; } log_append = 1; - logfile = g_new0(RCUCloseFILE, 1); - logfile->fd = fd; qatomic_rcu_set(&global_file, logfile); } return true;