jingfei195887 opened a new pull request, #21829:
URL: https://github.com/apache/nuttx/pull/21829
## Summary
* `file_mq_vopen()` creates a new message queue in two steps: it reserves
the inode with `inode_reserve()`, drops the inode tree lock, and only then
marks the inode as a message queue and sets `i_ops` and `i_private`.
Another thread calling `mq_open()` on the same name in that window finds
the half-initialized inode, fails the `INODE_IS_MQUEUE()` check and gets
`ENXIO`.
* Allocate the message queue first and fill in the inode before
`inode_unlock()`, so other threads only ever see a complete message queue.
If `inode_reserve()` loses the race with `-EEXIST`, free the message
queue and retry the lookup, which then opens the queue the other thread
created (or returns `EEXIST` for `O_EXCL`).
## Impact
* New or changed feature: NO.
* Impact on user: concurrent `mq_open(O_CREAT)` of the same name no longer
fails with `ENXIO`.
* Impact on build: NO.
* Impact on hardware: NO.
* Impact on documentation: NO.
* Impact on security: NO.
* Impact on compatibility: NO.
## Testing
I confirm that changes are verified on local setup and works as intended:
* Build Host: Ubuntu 22.04 x86_64, gcc (Ubuntu 13.1.0-8ubuntu1~22.04) 13.1.0
* Target: `sim:smp` (4 CPUs) and `sim:nsh`, tested on master 18767f5318;
master is now 6c4992aca8 and no new commit touches fs/mqueue. Verified on
sim only.
4 threads wait on a barrier and then call
`mq_open("/fsup_mqr", O_RDWR | O_CREAT, 0666, NULL)` at the same time,
300 rounds per run (1200 opens), unlinking the queue after each round.
Testing logs before change (3 runs):
```
mqrace: 300 rounds x 4 threads, failed opens=44 in 18 rounds, first errno=6
mqrace: 300 rounds x 4 threads, failed opens=52 in 24 rounds, first errno=6
mqrace: 300 rounds x 4 threads, failed opens=51 in 20 rounds, first errno=6
```
Testing logs after change (5 runs, all the same):
```
mqrace: 300 rounds x 4 threads, failed opens=0 in 0 rounds, first errno=0
```
* `./tools/checkpatch.sh -m -g master..HEAD`: all checks pass.
* `sim:nsh` Make and CMake builds pass with no new warnings; `ostest`
exits with status 0.
## PR verification Self-Check
* [x] This PR introduces only one functional change.
* [x] I have updated all required description fields above.
* [x] My PR adheres to Contributing Guidelines and Documentation (git commit
title and message, coding standard, etc).
* [ ] My PR is still work in progress (not ready for review).
* [x] My PR is ready for review and can be safely merged into a codebase.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]