Repository navigation
os/kernel/mqueue: allocate messages by requested length instead of fixed MQ_MAXMSGSIZE - #7643
hyunjongkim123 wants to merge 4 commits into
Conversation
|
|
||
| if (mqmsgblock) { | ||
| struct mqueue_msg_s *mqmsg = mqmsgblock; | ||
| FAR char *ptr = (FAR char *)mqmsgblock; |
There was a problem hiding this comment.
We don't need FAR anymore.
You can remove FAR.
There was a problem hiding this comment.
I'll address it
There was a problem hiding this comment.
Please remove all FAR in this PR.
| * messages reserved for interrupt handlers. | ||
| */ | ||
|
|
||
| if (up_interrupt_context()) { |
There was a problem hiding this comment.
Previously the ISR path entered the critical section only once to check both g_msgfree and g_msgfreeirq.
In this PR, the worst case (msglen <= MQ_PREALLOC_BYTES and g_msgfree is empty in interrupt context), it enters and leaves the critical section twice.
Could we keep both lookups under a single critical section for the ISR path to avoid redundant lock/unlock overhead?
There was a problem hiding this comment.
Agreed. I'll restructure mq_msgalloc() so both pool lokups happen under a single critical section.
fd5b1de to
5a9e8a0
Compare
|
Could you update bk7239n defconfgs and rtl8730e/loadable_apps and rtl8730e/flat_apps? |
|
Do we need to update kernel_tc about this change? |
| uint8_t priority; /* priority of message */ | ||
| size_t msglen; /* Message data length */ | ||
| char mail[MQ_MAX_BYTES]; /* Message data */ | ||
| char mail[1]; /* Message data (payload follows) */ |
There was a problem hiding this comment.
char mail[]; is better than char mail[1];
5a9e8a0 to
4a32572
Compare
…GSIZE Every message was a fixed struct mqueue_msg_s with a CONFIG_MQ_MAXMSGSIZE byte payload embedded in it. The pre-allocated pool was made of those structs, and when the pool ran out mq_msgalloc() took the same fixed struct from the heap, so a task sending a few bytes still paid for the largest message any queue may ever carry. Restructure the message so its payload is sized when it is allocated: - struct mqueue_msg_s ends in a flexible payload (char mail[]) instead of mail[MQ_MAX_BYTES]. A message holding an n byte payload is MQ_MSG_SIZE(n) bytes. The pool block is walked by byte stride, MQ_MSG_BLOCK(n), instead of being indexed as a struct array. - Add CONFIG_PREALLOC_MQ_MSGSIZE, the payload capacity of the CONFIG_PREALLOC_MQ_MSGS pool entries. CONFIG_MQ_MAXMSGSIZE keeps its role as the upper bound for mq_open() msgsize and as the entry size of the interrupt pool, which has no heap fallback. The default is MQ_MAXMSGSIZE, so a board that does not set it behaves as before. - mq_msgalloc(msglen) takes a pool entry when msglen fits in CONFIG_PREALLOC_MQ_MSGSIZE and one is free. When the pool is exhausted, or msglen is larger than a pool entry, a task gets kmm_malloc(MQ_MSG_SIZE(msglen)): exactly the length requested rather than CONFIG_MQ_MAXMSGSIZE. An ISR still falls back to the interrupt pool. mq_send()/mq_timedsend() pass msglen. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> (cherry picked from commit 0bd9be9489b499a1ca0c6f6db762e3ce4df2316f)
4a32572 to
c473c2e
Compare
Change the pre-allocated mqueue message pool to 10 messages of 256 bytes (CONFIG_PREALLOC_MQ_MSGS=10, CONFIG_PREALLOC_MQ_MSGSIZE=256) and align CONFIG_MQ_MAXMSGSIZE to 600 on all rtl8730e configs to optimize internal memory usage. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Change the pre-allocated mqueue message pool to 10 messages of 256 bytes (CONFIG_PREALLOC_MQ_MSGS=10, CONFIG_PREALLOC_MQ_MSGSIZE=256) and align CONFIG_MQ_MAXMSGSIZE to 600 on all bk7239n configs to optimize internal memory usage. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Change the pre-allocated mqueue message pool to 10 messages of 256 bytes (CONFIG_PREALLOC_MQ_MSGS=10, CONFIG_PREALLOC_MQ_MSGSIZE=256) and align CONFIG_MQ_MAXMSGSIZE to 600 on all rtl8721csm configs to optimize internal memory usage. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
c473c2e to
0f55882
Compare
kernel_tc: mqueue test cases
No test case references the message struct, mq_msgalloc() or the pool. They use only the POSIX mq API, so the allocation source (pool or heap) is not visible to them.
fixed it |
🤖 AI Code Review ·
|
|
|
||
| if (ret == OK) { | ||
| mqmsg = mq_msgalloc(); | ||
| mqmsg = mq_msgalloc(msglen); |
There was a problem hiding this comment.
When mq_msgalloc returns ERROR, mq_timedsend will return OK.
Could you check this scenario?
|
|
||
| config PREALLOC_MQ_MSGSIZE | ||
| int "Payload size of pre-allocated messages" | ||
| default MQ_MAXMSGSIZE |
There was a problem hiding this comment.
We should add range in this config and MQ_MAXMSGSIZE.
Because mq_msgblockalloc can accept uint16_t parameter type.
mangeshraut712
left a comment
There was a problem hiding this comment.
Reviewed against the merge-base. Not an approval — that's for a maintainer.
The allocator rework is correct and the overflow-safety chain closes. The one thing I'd want a maintainer to see before merging is the memory-layout change on 24 defconfigs, which the description doesn't cover.
Size arithmetic is safe — the chain is closed
MQ_MSG_SIZE(n) = offsetof(mail) + n could wrap for an n near SIZE_MAX, so I traced every caller:
mq_open→mq_msgqalloc.c:121rejectsattr->mq_msgsize > MQ_MAX_BYTES, boundingmaxmsgsize.mq_verifysend→mq_sndinternal.c:144rejectsmsglen > mqdes->msgq->maxmsgsize.- Both send paths (
mq_send.c:175,mq_timedsend.c:258/314) callmq_verifysendbeforemq_msgalloc(msglen).
So msglen ≤ MQ_MAX_BYTES on every path into MQ_MSG_SIZE(), and the pool stride MQ_MSG_BLOCK(bufsize) × nmsgs is a uint16_t count times a small stride — no overflow. mq_msgalloc itself doesn't re-check, which is fine given the above but worth a one-line comment pointing at mq_verifysend as the guard.
Byte-stride pool walk is correctly aligned
mq_msgblockalloc takes the block from kmm_malloc (maximally aligned), and stride = MQ_MSG_BLOCK(bufsize) is rounded to sizeof(void *), so every ptr += stride lands on a valid struct mqueue_msg_s * boundary. Good.
Header is 12 bytes on 32-bit ARM (next 4 + type 1 + priority 1 + 2 pad + msglen 4), which matters for the numbers below.
The defconfig changes are a real trade-off, not plumbing
All 24 defconfigs get PREALLOC_MQ_MSGS=10 and PREALLOC_MQ_MSGSIZE=256, and MQ_MAXMSGSIZE=600 where it was lower. What that means per board family (h ≈ 12):
| boards | before | after | Δ |
|---|---|---|---|
10 (hello, tc, tls_test, …) |
MAXMSGSIZE=32, MSGS=4 → 4×44 ≈ 176 B |
10×268 ≈ 2.7 KB | +2.5 KB, max msg 32→600 |
4 (loadable_apps) |
MAXMSGSIZE=256, MSGS=32 → 32×268 ≈ 8.6 KB |
10×268 ≈ 2.7 KB | −5.9 KB, 32→10 free entries |
14 (xip_all, …) |
MAXMSGSIZE=600, MSGS=4 → 4×612 ≈ 2.4 KB |
10×268 ≈ 2.7 KB | ≈ same, more entries |
And the interrupt pool keeps MQ_MAX_BYTES with NUM_INTERRUPT_MSGS=32 (it has no heap fallback). On the 10 boards where MAXMSGSIZE was 32, that pool grows 32×44 ≈ 1.4 KB → 32×612 ≈ 19.5 KB. That's the largest single delta in the PR and it's static allocation, so it shows up on every build of those boards.
None of this is wrong — the Kconfig default (MQ_MAXMSGSIZE) genuinely preserves old behaviour for boards that don't opt in, as you say. But these 24 boards do opt in, and the body reads as if the defconfig edits are bookkeeping. A short "Memory impact" section with the table above would let a board owner evaluate their board in seconds.
Minor
mq_send.c/mq_timedsend.cstill have an ISR branch callingmq_msgalloc(msglen)with the full-sizeMQ_ALLOC_IRQpool — correct, and the newMQ_ALLOC_*tags make the three allocators easy to tell apart. Nice.- The
#erroronPREALLOC_MQ_MSGSIZE > MQ_MAXMSGSIZEcatches the one misconfiguration that would make a pool entry unable to hold a legal message. Good.
Every message was a fixed struct mqueue_msg_s with a CONFIG_MQ_MAXMSGSIZE
byte payload embedded in it. The pre-allocated pool was made of those
structs, and when the pool ran out mq_msgalloc() took the same fixed
struct from the heap, so a task sending a few bytes still paid for the
largest message any queue may ever carry.
Restructure the message so its payload is sized when it is allocated:
struct mqueue_msg_s ends in a flexible payload (char mail[]) instead
of mail[MQ_MAX_BYTES]. A message holding an n byte payload is
MQ_MSG_SIZE(n) bytes. The pool block is walked by byte stride,
MQ_MSG_BLOCK(n), instead of being indexed as a struct array.
Add CONFIG_PREALLOC_MQ_MSGSIZE, the payload capacity of the
CONFIG_PREALLOC_MQ_MSGS pool entries. CONFIG_MQ_MAXMSGSIZE keeps its
role as the upper bound for mq_open() msgsize and as the entry size of
the interrupt pool, which has no heap fallback. The default is
MQ_MAXMSGSIZE, so a board that does not set it behaves as before.
mq_msgalloc(msglen) takes a pool entry when msglen fits in
CONFIG_PREALLOC_MQ_MSGSIZE and one is free. When the pool is exhausted,
or msglen is larger than a pool entry, a task gets
kmm_malloc(MQ_MSG_SIZE(msglen)): exactly the length requested rather
than CONFIG_MQ_MAXMSGSIZE. An ISR still falls back to the interrupt
pool. mq_send()/mq_timedsend() pass msglen.