There is a data race between cdsysctlinit https://reviews.freebsd.org/source/src/browse/main/sys/cam/scsi/scsi_cd.c$508 and cdcheckmedia https://reviews.freebsd.org/source/src/browse/main/sys/cam/scsi/scsi_cd.c$2680 . Both functions change softc->flags without synchronization.
We encountered the following scenario:
- One thread enters cdsysctlinit
- Another thread enters g_raid_taste → g_access → cdopen → cdcheckmedia
- Both threads change softc->flags at the same time
- cdcheckmedia fails to set CD_FLAG_MEDIA_WAIT and hangs off there https://reviews.freebsd.org/source/src/browse/main/sys/cam/scsi/scsi_cd.c$2701
To fix we lock periph in cdsysctlinit