Skip to content

drivers/note: noteram, noterpmsg and note driver fixes - #22250

Merged
xiaoxiang781216 merged 17 commits into
apache:masterfrom
huojianchaoyy:nx-tr-note-driver-fixes-v2
Nov 25, 2026
Merged

xiaoxiang781216 merged 17 commits into
apache:masterfrom
huojianchaoyy:nx-tr-note-driver-fixes-v2

Conversation

@huojianchaoyy

@huojianchaoyy huojianchaoyy commented Nov 24, 2026 •

Copy link
Copy Markdown

Note: Please adhere to Contributing Guidelines.

Summary

Note driver fixes from Xiaomi Vela: noteram, the rpmsg note client (noterpmsg) and server, and the note core.

noteram:

  • note: noteram reads multiple binary data at a time: a binary read() returns as many whole notes as fit in the buffer instead of one note per call. note: avoid data loss when reading binary data then keeps a note that does not fit for the next read() instead of dropping it.
  • noteram: make sure buffer is aligned: the text dump casts its stack buffer to struct note_common_s.
  • noteram: fix cmake missing section directive: CMake passes CONFIG_DRIVERS_NOTERAM_SECTION as Make.defs does.

noterpmsg and the rpmsg note server:

  • drivers/note: Fix data address alignment trap cause by sched_note_add.: noterpmsg stores the notes at NOTE_ALIGN() offsets and sched_note_add() steps over them by NOTE_ALIGN(), so the server reads every note from an aligned address. The patch keeps the uintptr_t pointer arithmetic of master in sched_note_add().
  • noterpmsg: overwrite noterpmsg when send rpmsg fail: the tail moves by the same padded stride, and the oldest notes are overwritten when the transfer fails. drivers/note: bound the noterpmsg batch by the padded length: a batch can no longer be larger than the rpmsg payload.
  • noterpmsg: check rpmsg ept status before send message: nothing is sent before the endpoint is bound; rpmsg_send_nocopy() would poll and sleep for it with drv->lock held.
  • note/noterpmsg_driver: avoid context switch after spin_lock_irqsave: the wait for a tx buffer happens without drv->lock and with interrupts enabled.
  • note: put the rpmsg note buffer into the bss section: the buffer leaves the initialized g_noterpmsg_driver (.data).
  • drivers/note/Kconfig: note rpmsg should depends on RPMSG instead RPTUN.

note core and build:

  • driver/note: fix overflowed integer argument: BUFFER_SIZE and a static_assert for the stack buffers of sched_note_event_ip() and sched_note_vprintf_ip().
  • note: Unify note length format: note_add() gets the nc_length of the note.
  • cmake: fix note cmake compile error: CMake builds notestream_driver.c for DRIVERS_NOTEFILE / DRIVERS_NOTELOWEROUT; master fails to link with CMake and CONFIG_DRIVERS_NOTEFILE=y (see Testing).
  • drivers/note: rename notelog spinlock callback to notelog_spinlock: name only.
  • drivers/note: stop note_add() walking off a malformed packet: master has this change as 0184bd1, ported to sched_note_add() while the notes on the wire were not padded, so it compares the unpadded nc_length with what is left. With the padded stride a last note without its padding passed that test and wrapped len around; the check now uses the padded length, as the original change does.

One follow-up commit: drivers/note: add the missing blank line in noteram_read() (nxstyle), right after the noteram read change.

Impact

  • The notes on the rpmsg wire are padded to NOTE_ALIGN(): noterpmsg and the note server both need this PR, a client and a server built from trees on different sides of it do not understand each other.
  • noteram binary read(): several notes per call, still back to back (system/trace writes them out as they come, tools/parsetrace.py walks them by nc_length). A read() whose buffer cannot hold the next note returns 0 instead of -EFBIG, and the note stays in the buffer.
  • DRIVERS_NOTERPMSG and DRIVERS_NOTERPMSG_SERVER depend on RPMSG instead of RPTUN. RPTUN selects RPMSG through RPMSG_VIRTIO, so existing configurations keep them.
  • No API or default value change.

Testing

Host: Ubuntu 22.04 x86_64, GCC 13.4.0 (sim), Arm GNU Toolchain 13.2.rel1, aarch64-none-elf GCC 13.4.0, CMake 3.23 + Ninja 1.10, QEMU 10.2.1. Base: master 91dd24b. The tests below ran on the same patches on master c890786 (git range-diff shows no change; the 9 commits in between touch arch/arm, i2c and virtio), and the head builds were repeated on 91dd24b (sim:note SMP, sim:rpproxy, sim:rpserver, qemu-armv7a:nsh, mps3-an547:nsh).

./tools/checkpatch.sh -c -u -m -g HEAD~17..HEAD passes.

Every commit builds (CMake, incremental) with no new warning:

  • sim:note; sim:note without DRIVERS_NOTERAM_TEXT_DUMP;
  • sim:note with SMP, DRIVERS_NOTEFILE and DRIVERS_NOTERAM_SECTION=".bss.noteram": master and the commits before "cmake: fix note cmake compile error" fail with undefined reference to 'notefile_register', the later ones build, and g_ramnote_buffer is in .bss.noteram in noteram_driver.c.o;
  • sim:rpproxy with DRIVERS_NOTERPMSG, sim:rpserver with DRIVERS_NOTERPMSG_SERVER and DRIVERS_NOTERAM.

The head also builds for qemu-armv7a:nsh (noteram, switch and dump notes, filter), qemu-armv8a:nsh_smp (noteram, notelog, spinlock notes), and with Make for sim:note (SMP, NOTEFILE, NOTERAM_SECTION) and sim:rpproxy (NOTERPMSG).

sim:note, trace start; sleep 1; trace stop; trace dump -b /host/a.bin, then walking the file on the host by nc_length:

master:  66499 bytes, 2885 notes, the walk ends exactly at the end of the file
this PR: 66355 bytes, 2879 notes, the walk ends exactly at the end of the file

trace dump (text) prints the same kind of output on both (2882 lines).

sim:rpserver + sim:rpproxy, with switch notes on the proxy and the local notes of the server filtered out (SCHED_INSTRUMENTATION_FILTER_DEFAULT_MODE=0): cat /dev/note/ram on the server shows the notes of the proxy (1820 sched_switch lines and sched_wakeup_new: comm=netinit pid=9 in one run, 3098 sched_switch lines in another). On master the proxy crashed at boot in 5 runs out of 5 (SIGSEGV), with this PR in none of 5 runs; an earlier test of the branch saw one such crash in 5 runs, which is not investigated here.

Not tested: real hardware, DRIVERS_NOTELOWEROUT at run time (sim and qemu-armv7a have no up_lowputc()).

zzby0 and others added 17 commits November 24, 2026 16:34
sched_note_event_ip() caps the note at sizeof(data) - 1 bytes and then
passes length - SIZEOF_NOTE_EVENT(0) to memcpy().  Static analysis
reports that argument as a possible integer overflow, because nothing
ties the size of the stack buffer to the size of the note header.

Name the buffer size BUFFER_SIZE, use it for the buffers of
sched_note_event_ip() and sched_note_vprintf_ip(), and check at build
time that it is at least sizeof(struct note_event_s).

Assisted-by: Kiro:claude-opus-5-5
Signed-off-by: guanyi <guanyi@xiaomi.com>
In binary mode noteram_read() returned one note per call, whatever the
size of the caller's buffer, so reading out the buffer took one read()
per note.  Copy as many whole notes as fit into the buffer, taking the
lock only once, and return the number of bytes copied.

Assisted-by: Kiro:claude-opus-5-5
Signed-off-by: yinshengkai <yinshengkai@xiaomi.com>
nxstyle wants a blank line after the declaration of nread.  No
functional change.

Assisted-by: Kiro:claude-opus-5-5
Signed-off-by: huojianchao <huojianchao@xiaomi.com>
The size of the structure is padded by default, but the data in noteram is not padded
Mixing sizeof and nc_length in the code will cause errors during binary parsing

Signed-off-by: yinshengkai <yinshengkai@xiaomi.com>
If there is not enough space left when reading data, data will be lost.

Signed-off-by: yinshengkai <yinshengkai@xiaomi.com>
notestream_driver.c is missing from CMakeLists.txt.

Signed-off-by: xuxingliang <xuxingliang@xiaomi.com>
Need to specifiy the section noteram resides in.
This is done in Makefile but not cmake.

Signed-off-by: xuxingliang <xuxingliang@xiaomi.com>
All the rpmsg services should depend on RPMSG instead of RPTUN after the
rpmsg framework was refactored, as most of them already do.

Assisted-by: Kiro:claude-opus-5-5
Signed-off-by: Bowen Wang <wangbowen6@xiaomi.com>
Solve the problem of address misalignment exception triggered
when sched_note_add parses note data.

Assisted-by: Kiro:claude-opus-5-5
Signed-off-by: yukangzhi <yukangzhi@xiaomi.com>
noterpmsg buffer will be overflow when send rpmsg fail,
So need release noterpmsg buffer before overwrite.

Signed-off-by: zhangyuan29 <zhangyuan29@xiaomi.com>
noterpmsg_transfer() runs with drv->lock held and interrupts disabled.
Until the remote side binds the endpoint, rpmsg_send_nocopy() polls
is_rpmsg_ept_ready() and sleeps between the polls (openamp patch 0004),
which must not happen there.

Check the endpoint first and return false while it is not ready:
noterpmsg_work() retries after NOTE_RPMSG_WORK_DELAY and noterpmsg_add()
makes room by overwriting the oldest notes.

Assisted-by: Kiro:claude-opus-5-5
Signed-off-by: zhangyuan29 <zhangyuan29@xiaomi.com>
noterpmsg_add() called noterpmsg_transfer(drv, true) with drv->lock held
and interrupts disabled, and noterpmsg_work() held the lock around the
whole transfer.  With wait set, rpmsg_get_tx_payload_buffer() sleeps
until the remote side returns a buffer, so the task could switch out
inside the critical section.

Take drv->lock in noterpmsg_transfer() only after getting the tx buffer,
drop it in noterpmsg_add() around the transfer, and queue the work after
the unlock.

Assisted-by: Kiro:claude-opus-5-5
Signed-off-by: Bowen Wang <wangbowen6@xiaomi.com>
g_noterpmsg_driver has initializers, so the
CONFIG_DRIVERS_NOTERPMSG_BUFSIZE bytes of buffer embedded in it go to
.data and take the same space in the flash image.  Move the buffer to a
static array of its own, which goes to .bss, and point the driver at
it.

Assisted-by: Kiro:claude-opus-5-5
Signed-off-by: yinshengkai <yinshengkai@xiaomi.com>
buffer is converted to pointer to struct note_common_note *, thus needs to be aligned.

Signed-off-by: xuxingliang <xuxingliang@xiaomi.com>
The spinlock callback of notelog is the only notelog callback without
the notelog_ prefix, and it has the name of the note_spinlock() macro of
note_driver.c.  Rename it to notelog_spinlock to match the other
notelog_* callbacks.  No functional change.

Assisted-by: Kiro:claude-opus-5-5
Signed-off-by: Bowen Wang <wangbowen6@xiaomi.com>
noterpmsg_transfer() fills an rpmsg payload with as many whole notes as
fit.  It tested the unaligned nc_length against the space left while
accumulating the padded stride, so len could end up to seven bytes past
the payload it was measured against, and both the memcpy and the
rpmsg_send_nocopy() that follow run on that oversized len.

space=36 with a 33-byte note is enough: the test passes at 33 and len
becomes 40.  Simulating the ring over random note lengths and payload
capacities trips an assertion on len <= space within a few thousand
records.

Test the padded stride instead, which is what len accumulates.

Assisted-by: Kiro
Signed-off-by: huojianchao <huojianchao@xiaomi.com>
0184bd1 ported this change to sched_note_add() at a time when the
notes on the rpmsg wire were not padded, so it compares nc_length with
what is left of the packet.  Since "drivers/note: Fix data address
alignment trap cause by sched_note_add." the walk steps by
NOTE_ALIGN(notelen), and a last note that comes without its padding
passes that test, then wraps len around.  Compare the padded length,
as the original change does.

Assisted-by: Kiro:claude-opus-5-5
Signed-off-by: huojianchao <huojianchao@xiaomi.com>
@github-actions github-actions Bot added Area: Drivers Drivers issues Size: M The size of the change in this PR is medium labels Nov 24, 2026
@github-actions

Copy link
Copy Markdown

@xiaoxiang781216
xiaoxiang781216 merged commit 024bec2 into apache:master Nov 25, 2026
58 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Area: Drivers Drivers issues Size: M The size of the change in this PR is medium

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants