Module Name: src Committed By: riastradh Date: Tue Oct 25 23:21:13 UTC 2022
Modified Files: src/sys/dev: cons.c src/sys/kern: subr_prf.c tty.c src/sys/sys: tty.h Log Message: constty(4): Make MP-safe, take three. Access to the global constty variable is coordinated as follows: 1. Setting constty to nonnull, with atomic_store_release, is allowed only under the new adaptive constty_lock in thread context. This serializes TIOCCONS operations and ensures unlocked readers can safely use a constty pointer read with atomic_load_consume. 2. Changing constty from nonnull to null, with atomic_cas_ptr, is allowed in any context -- printf(9) uses this to disable a broken constty. 3. Reading constty under constty_lock is allowed with atomic_load_relaxed, because while constty_lock is held, it can only be made null by some other thread/CPU, never made nonnull. 4. Reading constty outside constty_lock is allowed with atomic_load_consume in a pserialize read section -- constty is only ever made nonnull with atomic_store_release, in (1). ttyclose will wait for all these pserialize read sections to complete before flushing the tty. 5. To continue to use a struct tty pointer in (4) after the pserialize read section has completed, caller must use tty_acquire during the pserialize read section and then tty_release when done. ttyclose will wait for all these references to drain before returning. These access rules allow us to serialize TIOCCONS, and safely destroy ttys, without putting any locks on the access paths like printf(9) that use constty. Once we set D_MPSAFE, operations on /dev/console will contend only with other users of the same tty as constty, which will be an improvement over contending with all other kernel lock users in the system. Changes second time around: - Fix initialization of ok in cons.c cn_redirect. - Fix reversed sense of conditional in subr_prf.c putone. Changes third time around: - Initialize ttyref_cv so we don't panic when trying to use it, leading to infinite loop when panic tries to take tty_lock to print the panic message while we already hold tty_lock. To generate a diff of this commit: cvs rdiff -u -r1.90 -r1.91 src/sys/dev/cons.c cvs rdiff -u -r1.193 -r1.194 src/sys/kern/subr_prf.c cvs rdiff -u -r1.305 -r1.306 src/sys/kern/tty.c cvs rdiff -u -r1.101 -r1.102 src/sys/sys/tty.h Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files.