mem/mtest07: Convert shm_test to new LTP API and fix race conditions
PATCH 1/1 mem/mtest07: Convert shm_test to new LTP API and fix race conditions
The legacy shm_test implementation suffered from severe race conditions between paired reader and writer threads due to a complete lack of synchronization. One thread could complete its work, mark the shared memory segment for deletion with IPC_RMID, and detach. This caused shm_nattch to drop to 0, prompting the kernel to immediately reclaim the segment. Consequently, the peer thread's subsequent shmat() call failed with EINVAL ("Invalid argument"):
pid[4717]: shmat_rd_wr(): shmget():success got segment id 11
pid[4717]: do_shmat_shmadt(): got shmat address = 0x7fff8aba0000
pid[4717]: shmat_rd_wr(): shmget():success got segment id 11
rm_shared_mem(): shmctl(): Invalid argument
pid[4717]: do_shmat_shmadt(): shmat_addr = 0xffffffffffffffff
pid[4717]: shmat_rd_wr(): shmget():success got segment id 12
pid[4717]: do_shmat_shmadt(): got shmat address = 0x7fff8aba0000
do_shmat_shmadt(): shmat(): Invalid argument
pid[4717]: shmat_rd_wr(): shmget():success got segment id 13
pid[4717]: do_shmat_shmadt(): got shmat address = 0x7fff8aba0000
pid[4717]: shmat_rd_wr(): shmget():success got segment id 14Refactor the test to use the modern LTP API (tst_test.h):
- Coordinate reader and writer thread pairs using POSIX barriers to ensure safe segment attachment, memory access, single-thread IPC_RMID deletion.
- Add an explicit data verification oracle where the reader thread checks that data written by the writer matches expectations ('Y').
- Drop hand-rolled iteration loops (-l) and delegate iteration driving to the LTP framework's native -i option, updating runtest/mm to run 20 concurrent threads across 10 iterations.
- Query getpagesize() dynamically instead of assuming 4 KB pages.
- Use IPC_PRIVATE for segment creation instead of hardcoded keys.
--- runtest/mm | 2 +- testcases/kernel/mem/mtest07/shm_test.c | 483 ++++++++---------------- 2 files changed, 151 insertions(+), 334 deletions(-)
runtest/mm +1 -1
@@ -33,7 +33,7 @@ shmt08 shmt08 shmt09 shmt09 shmt10 shmt10 -shm_test01 shm_test -l 10 -t 2 +shm_test01 shm_test -i 10 -t 20 mallocstress01 mallocstress mmapstress01 mmapstress01
testcases/kernel/mem/mtest07/shm_test.c +150 -333
511 lines not on this page · raw message
Signature · 5 lines
2.55.0 -- Mailing list info: https://lists.linux.it/listinfo/ltp