syslog/ramlog: Survive writes made before the OS is ready.

The RAM log is the natural home for boot messages, yet writing to it
during early boot could crash the system it was meant to describe.
ramlog_addbuf took the critical section on every write, and
enter_critical_section consults the current task; on ports whose
first syslog output happens before the task lists exist, that lookup
walks uninitialized state and faults.  The notification path was
worse still, locking a scheduler that did not exist yet.

Guard both.  Before the task lists exist, plain interrupt masking
protects the buffer just as well, since there is only one thread of
control; and readers are only notified once there is an operating
system to notify them through.  The bytes land in the buffer either
way, so nothing logged before the OS is ready is lost.

Found on the EIC7700X port, which logs from its start routine before
the MMU is up: enabling RAMLOG_SYSLOG there turned the boot into a
silent wedge two characters in.  With this change the same
configuration boots and `dmesg` replays the full early history.

Assisted-by: Claude:claude-opus-5
Signed-off-by: Justin Hammond <justin@dynam.ac>
This commit is contained in:
Justin Hammond 2026-08-07 10:09:40 +08:00 committed by Alan C. Assis
parent d95d8c0fb1
commit 875e86bd35

View file

@ -43,6 +43,7 @@
#include <sys/boardctl.h>
#include <nuttx/arch.h>
#include <nuttx/init.h>
#include <nuttx/kmalloc.h>
#include <nuttx/spinlock.h>
#include <nuttx/semaphore.h>
@ -365,13 +366,34 @@ static ssize_t ramlog_addbuf(FAR struct ramlog_dev_s *priv,
size_t buflen = len;
irqstate_t flags;
/* Disable interrupts (in case we are NOT called from interrupt handler) */
/* Disable interrupts (in case we are NOT called from interrupt handler).
*
* Not enter_critical_section(): it consults the current task, and on
* some ports this channel takes syslog output before the task lists
* exist. Masking interrupts protects the buffer just as well while
* there is only one thread of control.
*/
flags = enter_critical_section();
if (OSINIT_TASK_READY())
{
flags = enter_critical_section();
}
else
{
flags = up_irq_save();
}
if (ramlog_ratelimit(priv))
{
leave_critical_section(flags);
if (OSINIT_TASK_READY())
{
leave_critical_section(flags);
}
else
{
up_irq_restore(flags);
}
return len;
}
@ -391,9 +413,13 @@ static ssize_t ramlog_addbuf(FAR struct ramlog_dev_s *priv,
ramlog_copybuf(priv, buffer, buflen);
/* Was anything written? */
/* Was anything written? Notify only once the scheduler exists; early
* boot output reaches here long before it does, and locking a scheduler
* that is not there yet faults. The bytes land in the buffer either
* way.
*/
if (len > 0)
if (len > 0 && OSINIT_OS_READY())
{
/* Lock the scheduler do NOT switch out */
@ -424,7 +450,15 @@ static ssize_t ramlog_addbuf(FAR struct ramlog_dev_s *priv,
* probably retry, causing same error condition again.
*/
leave_critical_section(flags);
if (OSINIT_TASK_READY())
{
leave_critical_section(flags);
}
else
{
up_irq_restore(flags);
}
return len;
}