History

tty: tty_buffer: invert conditions in __tty_buffer_request_room()

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.

Jiri Slaby (SUSE) committed 3 years ago

Commit ebee41c1 parent 035197c1 file · +22 −22Browse files

Unified · Split

drivers/tty/tty_buffer.c+22−22
@@ -266,28 +266,28 @@ static int __tty_buffer_request_room(struct tty_port *port, size_t size, · 265 unmodified lines
266266 size_t left = (b->flags ? 1 : 2) * b->size - b->used;
267267 bool change = !b->flags && flags;
268268
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+
291291 return size;
292292 }
293293

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t