From 637a0f5af2309a2a51574e078fddfc38fecf5d4e Mon Sep 17 00:00:00 2001 From: YAMAMOTO Takashi Date: Thu, 14 Nov 2024 16:45:41 +0900 Subject: [PATCH] SYSLOG_DEFAULT: avoid using critical section in certain cases Note that SYSLOG_DEFAULT can be used in extreme situations, including very early in the boot. --- drivers/syslog/syslog_channel.c | 63 ++++++++++++++++++++++++++------- 1 file changed, 51 insertions(+), 12 deletions(-) diff --git a/drivers/syslog/syslog_channel.c b/drivers/syslog/syslog_channel.c index 8038271d5944d..a9bc9e87ef501 100644 --- a/drivers/syslog/syslog_channel.c +++ b/drivers/syslog/syslog_channel.c @@ -33,6 +33,7 @@ #include #include +#include #ifdef CONFIG_RAMLOG_SYSLOG # include @@ -217,6 +218,30 @@ g_syslog_channel[CONFIG_SYSLOG_MAX_CHANNELS] = * Private Functions ****************************************************************************/ +#if defined(CONFIG_SYSLOG_DEFAULT) && defined(CONFIG_ARCH_LOWPUTC) + +/**************************************************************************** + * Name: csection_available + * + * Description: + * Return true if the critical section is available. + * + ****************************************************************************/ + +static bool csection_available(void) +{ + /* Degrade the critical section in a few cases: + * + * a) early in the boot, where tasks are not available + * + * b) after a panic, where taking a lock can make the situation worse + */ + + return OSINIT_TASK_READY() && g_nx_initstate != OSINIT_PANIC; +} + +#endif /* defined(CONFIG_SYSLOG_DEFAULT) && defined(CONFIG_ARCH_LOWPUTC) */ + /**************************************************************************** * Name: syslog_default_putc * @@ -230,13 +255,20 @@ g_syslog_channel[CONFIG_SYSLOG_MAX_CHANNELS] = static int syslog_default_putc(FAR syslog_channel_t *channel, int ch) { # ifdef CONFIG_ARCH_LOWPUTC - /* See https://github.com/apache/nuttx/issues/14662 - * about what this critical section is for. - */ + if (csection_available()) + { + /* See https://github.com/apache/nuttx/issues/14662 + * about what this critical section is for. + */ - irqstate_t flags = enter_critical_section(); - up_putc(ch); - leave_critical_section(flags); + irqstate_t flags = enter_critical_section(); + up_putc(ch); + leave_critical_section(flags); + } + else + { + up_putc(ch); + } # endif UNUSED(channel); @@ -247,13 +279,20 @@ static ssize_t syslog_default_write(FAR syslog_channel_t *channel, FAR const char *buffer, size_t buflen) { # ifdef CONFIG_ARCH_LOWPUTC - /* See https://github.com/apache/nuttx/issues/14662 - * about what this critical section is for. - */ + if (csection_available()) + { + /* See https://github.com/apache/nuttx/issues/14662 + * about what this critical section is for. + */ - irqstate_t flags = enter_critical_section(); - up_nputs(buffer, buflen); - leave_critical_section(flags); + irqstate_t flags = enter_critical_section(); + up_nputs(buffer, buflen); + leave_critical_section(flags); + } + else + { + up_nputs(buffer, buflen); + } # endif UNUSED(channel);