We are used to handle "bad" states in the 'if's in the kernel. Refactor (invert the two conditions in) __tty_buffer_request_room(), so that the code returns from the fast paths immediately instead of postponing to the heavy end of the function.
Unified · Split
| @@ -266,28 +266,28 @@ static int __tty_buffer_request_room(struct tty_port *port, size_t size, · 265 unmodified lines | ||
| 266 | 266 | size_t left = (b->flags ? 1 : 2) * b->size - b->used; |
| 267 | 267 | bool change = !b->flags && flags; |
| 268 | 268 | |
| 269 | − if (change || left < size) { | |
| 270 | − /* This is the slow path - looking for new buffers to use */ | |
| 271 | − n = tty_buffer_alloc(port, size); | |
| 272 | − if (n != NULL) { | |
| 273 | − n->flags = flags; | |
| 274 | − buf->tail = n; | |
| 275 | − /* | |
| 276 | − * Paired w/ acquire in flush_to_ldisc() and lookahead_bufs() | |
| 277 | − * ensures they see all buffer data. | |
| 278 | − */ | |
| 279 | − smp_store_release(&b->commit, b->used); | |
| 280 | − /* | |
| 281 | − * Paired w/ acquire in flush_to_ldisc() and lookahead_bufs() | |
| 282 | − * ensures the latest commit value can be read before the head | |
| 283 | − * is advanced to the next buffer. | |
| 284 | − */ | |
| 285 | − smp_store_release(&b->next, n); | |
| 286 | − } else if (change) | |
| 287 | − size = 0; | |
| 288 | − else | |
| 289 | − size = left; | |
| 290 | − } | |
| 269 | + if (!change && left >= size) | |
| 270 | + return size; | |
| 271 | + | |
| 272 | + /* This is the slow path - looking for new buffers to use */ | |
| 273 | + n = tty_buffer_alloc(port, size); | |
| 274 | + if (n == NULL) | |
| 275 | + return change ? 0 : left; | |
| 276 | + | |
| 277 | + n->flags = flags; | |
| 278 | + buf->tail = n; | |
| 279 | + /* | |
| 280 | + * Paired w/ acquire in flush_to_ldisc() and lookahead_bufs() | |
| 281 | + * ensures they see all buffer data. | |
| 282 | + */ | |
| 283 | + smp_store_release(&b->commit, b->used); | |
| 284 | + /* | |
| 285 | + * Paired w/ acquire in flush_to_ldisc() and lookahead_bufs() | |
| 286 | + * ensures the latest commit value can be read before the head | |
| 287 | + * is advanced to the next buffer. | |
| 288 | + */ | |
| 289 | + smp_store_release(&b->next, n); | |
| 290 | + | |
| 291 | 291 | return size; |
| 292 | 292 | } |
| 293 | 293 | |