[LTP] semctl01: tolerate SEM_STAT races under parallel LTP runs
linuxtestproject.agent@gmail.com
linuxtestproject.agent@gmail.com
Tue Jul 21 01:13:35 CEST 2026
Hi Stephen,
On Mon, 20 Jul 2026 18:49:30 -0400, Stephen Bertram wrote:
> semctl01: tolerate SEM_STAT races under parallel LTP runs
> +static void setup(void)
> +{
> + sem_id = SAFE_SEMGET(IPC_PRIVATE, PSEMS, IPC_CREAT | IPC_EXCL | SEM_RA);
> +}
Moving the SAFE_SEMGET into setup() breaks multi-iteration runs.
The old code had a lazy-init guard inside verify_semctl():
if (sem_id == -1)
sem_id = SAFE_SEMGET(IPC_PRIVATE, PSEMS, IPC_CREAT | IPC_EXCL | SEM_RA);
func_rmid() sets sem_id = -1 after IPC_RMID. On the next iteration
verify_semctl(0) calls SAFE_SEMCTL(sem_id=-1, 0, IPC_STAT, ...) which
returns EINVAL and triggers TBROK. Running ./semctl01 -i 2 reproduces
this immediately.
The lazy-init guard (or equivalent per-iteration re-creation) needs to
be kept.
> +static int do_sem_stat(union semun arg)
> +{
> + int idx, rval;
> + unsigned int retries = 5;
> +
> + do {
> + idx = semctl(0, 0, IPC_INFO, (union semun)&ipc_buf);
> + if (idx < 0)
> + tst_brk(TBROK | TERRNO, "semctl(0, 0, IPC_INFO)");
IPC_INFO is a helper call here, not the syscall under test. Can
SAFE_SEMCTL be used instead?
idx = SAFE_SEMCTL(0, 0, IPC_INFO, (union semun)&ipc_buf);
The behaviour is identical; SAFE_SEMCTL already calls tst_brk on
failure.
> + rval = semctl(idx, 0, SEM_STAT, arg);
> + if (rval >= 0) {
> + sem_index = idx;
> + return rval;
> + }
After this patch sem_index has no remaining readers. The *semid field
was removed from struct tcases so the variable is now write-only:
- func_iinfo() writes sem_index = hidx (and sem_index = 0 on TFAIL)
- do_sem_stat() writes sem_index = idx
But nothing reads it. Can sem_index and all three assignment sites be
removed?
Verdict - Needs revision
---
Note:
The agent can sometimes produce false positives although often its
findings are genuine. If you find issues with the review, please
comment this email or ignore the suggestions.
Regards,
LTP AI Reviewer
More information about the ltp
mailing list