1 2 The Lockronomicon 3 4Your guide to the ancient and twisted locking policies of the tty layer and 5the warped logic behind them. Beware all ye who read on. 6 7FIXME: still need to work out the full set of BKL assumptions and document 8them so they can eventually be killed off. 9 10 11Line Discipline 12--------------- 13 14Line disciplines are registered with tty_register_ldisc() passing the 15discipline number and the ldisc structure. At the point of registration the 16discipline must be ready to use and it is possible it will get used before 17the call returns success. If the call returns an error then it won't get 18called. Do not re-use ldisc numbers as they are part of the userspace ABI 19and writing over an existing ldisc will cause demons to eat your computer. 20After the return the ldisc data has been copied so you may free your own 21copy of the structure. You must not re-register over the top of the line 22discipline even with the same data or your computer again will be eaten by 23demons. 24 25In order to remove a line discipline call tty_register_ldisc passing NULL. 26In ancient times this always worked. In modern times the function will 27return -EBUSY if the ldisc is currently in use. Since the ldisc referencing 28code manages the module counts this should not usually be a concern. 29 30Heed this warning: the reference count field of the registered copies of the 31tty_ldisc structure in the ldisc table counts the number of lines using this 32discipline. The reference count of the tty_ldisc structure within a tty 33counts the number of active users of the ldisc at this instant. In effect it 34counts the number of threads of execution within an ldisc method (plus those 35about to enter and exit although this detail matters not). 36 37Line Discipline Methods 38----------------------- 39 40TTY side interfaces: 41 42close() - This is called on a terminal when the line 43 discipline is being unplugged. At the point of 44 execution no further users will enter the 45 ldisc code for this tty. Can sleep. 46 47open() - Called when the line discipline is attached to 48 the terminal. No other call into the line 49 discipline for this tty will occur until it 50 completes successfully. Can sleep. 51 52write() - A process is writing data from user space 53 through the line discipline. Multiple write calls 54 are serialized by the tty layer for the ldisc. May 55 sleep. 56 57flush_buffer() - May be called at any point between open and close. 58 59chars_in_buffer() - Report the number of bytes in the buffer. 60 61set_termios() - Called on termios structure changes. The caller 62 passes the old termios data and the current data 63 is in the tty. Currently can be parallel entered 64 and ordering isn't predictable - FIXME 65 66read() - Move data from the line discipline to the user. 67 Multiple read calls may occur in parallel and the 68 ldisc must deal with serialization issues. May 69 sleep. 70 71poll() - Check the status for the poll/select calls. Multiple 72 poll calls may occur in parallel. May sleep. 73 74ioctl() - Called when an ioctl is handed to the tty layer 75 that might be for the ldisc. Multiple ioctl calls 76 may occur in parallel. May sleep. 77 78Driver Side Interfaces: 79 80receive_buf() - Hand buffers of bytes from the driver to the ldisc 81 for processing. Semantics currently rather 82 mysterious 8( 83 84receive_room() - Can be called by the driver layer at any time when 85 the ldisc is opened. The ldisc must be able to 86 handle the reported amount of data at that instant. 87 Synchronization between active receive_buf and 88 receive_room calls is down to the driver not the 89 ldisc. Must not sleep. 90 91write_wakeup() - May be called at any point between open and close. 92 The TTY_DO_WRITE_WAKEUP flag indicates if a call 93 is needed but always races versus calls. Thus the 94 ldisc must be careful about setting order and to 95 handle unexpected calls. Must not sleep. 96 97 98Locking 99 100Callers to the line discipline functions from the tty layer are required to 101take line discipline locks. The same is true of calls from the driver side 102but not yet enforced. 103 104Three calls are now provided 105 106 ldisc = tty_ldisc_ref(tty); 107 108takes a handle to the line discipline in the tty and returns it. If no ldisc 109is currently attached or the ldisc is being closed and re-opened at this 110point then NULL is returned. While this handle is held the ldisc will not 111change or go away. 112 113 tty_ldisc_deref(ldisc) 114 115Returns the ldisc reference and allows the ldisc to be closed. Returning the 116reference takes away your right to call the ldisc functions until you take 117a new reference. 118 119 ldisc = tty_ldisc_ref_wait(tty); 120 121Performs the same function as tty_ldisc_ref except that it will wait for an 122ldisc change to complete and then return a reference to the new ldisc. 123 124While these functions are slightly slower than the old code they should have 125minimal impact as most receive logic uses the flip buffers and they only 126need to take a reference when they push bits up through the driver. 127 128A caution: The ldisc->open(), ldisc->close() and driver->set_ldisc 129functions are called with the ldisc unavailable. Thus tty_ldisc_ref will 130fail in this situation if used within these functions. Ldisc and driver 131code calling its own functions must be careful in this case. 132 133 134Driver Interface 135---------------- 136 137open() - Called when a device is opened. May sleep 138 139close() - Called when a device is closed. At the point of 140 return from this call the driver must make no 141 further ldisc calls of any kind. May sleep 142 143write() - Called to write bytes to the device. May not 144 sleep. May occur in parallel in special cases. 145 Because this includes panic paths drivers generally 146 shouldn't try and do clever locking here. 147 148put_char() - Stuff a single character onto the queue. The 149 driver is guaranteed following up calls to 150 flush_chars. 151 152flush_chars() - Ask the kernel to write put_char queue 153 154write_room() - Return the number of characters tht can be stuffed 155 into the port buffers without overflow (or less). 156 The ldisc is responsible for being intelligent 157 about multi-threading of write_room/write calls 158 159ioctl() - Called when an ioctl may be for the driver 160 161set_termios() - Called on termios change, serialized against 162 itself by a semaphore. May sleep. 163 164set_ldisc() - Notifier for discipline change. At the point this 165 is done the discipline is not yet usable. Can now 166 sleep (I think) 167 168throttle() - Called by the ldisc to ask the driver to do flow 169 control. Serialization including with unthrottle 170 is the job of the ldisc layer. 171 172unthrottle() - Called by the ldisc to ask the driver to stop flow 173 control. 174 175stop() - Ldisc notifier to the driver to stop output. As with 176 throttle the serializations with start() are down 177 to the ldisc layer. 178 179start() - Ldisc notifier to the driver to start output. 180 181hangup() - Ask the tty driver to cause a hangup initiated 182 from the host side. [Can sleep ??] 183 184break_ctl() - Send RS232 break. Can sleep. Can get called in 185 parallel, driver must serialize (for now), and 186 with write calls. 187 188wait_until_sent() - Wait for characters to exit the hardware queue 189 of the driver. Can sleep 190 191send_xchar() - Send XON/XOFF and if possible jump the queue with 192 it in order to get fast flow control responses. 193 Cannot sleep ?? 194 195