Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

serial: serial_core: use guard()s

Having all the new guards, use them in the serial_core code. This
improves readability, makes error handling easier, and marks locked
portions of code explicit.

Signed-off-by: "Jiri Slaby (SUSE)" <jirislaby@kernel.org>
Link: https://lore.kernel.org/r/20250814072456.182853-9-jirislaby@kernel.org
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

authored by

Jiri Slaby (SUSE) and committed by
Greg Kroah-Hartman
56609c05 793b4501

+59 -84
+59 -84
drivers/tty/serial/serial_core.c
··· 177 177 static void 178 178 uart_update_mctrl(struct uart_port *port, unsigned int set, unsigned int clear) 179 179 { 180 - unsigned long flags; 181 180 unsigned int old; 182 181 183 - uart_port_lock_irqsave(port, &flags); 182 + guard(uart_port_lock_irqsave)(port); 184 183 old = port->mctrl; 185 184 port->mctrl = (old & ~clear) | set; 186 185 if (old != port->mctrl && !(port->rs485.flags & SER_RS485_ENABLED)) 187 186 port->ops->set_mctrl(port, port->mctrl); 188 - uart_port_unlock_irqrestore(port, flags); 189 187 } 190 188 191 189 #define uart_set_mctrl(port, set) uart_update_mctrl(port, set, 0) ··· 218 220 /* 219 221 * Set modem status enables based on termios cflag 220 222 */ 221 - uart_port_lock_irq(uport); 223 + guard(uart_port_lock_irq)(uport); 222 224 if (termios->c_cflag & CRTSCTS) 223 225 uport->status |= UPSTAT_CTS_ENABLE; 224 226 else ··· 239 241 else 240 242 __uart_start(state); 241 243 } 242 - uart_port_unlock_irq(uport); 243 244 } 244 245 245 246 static int uart_alloc_xmit_buf(struct tty_port *port) ··· 708 711 { 709 712 struct uart_state *state = tty->driver_data; 710 713 struct uart_port *port; 711 - unsigned long flags; 712 714 713 715 port = uart_port_ref(state); 714 716 if (!port) ··· 716 720 if (port->ops->send_xchar) 717 721 port->ops->send_xchar(port, ch); 718 722 else { 719 - uart_port_lock_irqsave(port, &flags); 723 + guard(uart_port_lock_irqsave)(port); 720 724 port->x_char = ch; 721 725 if (ch) 722 726 port->ops->start_tx(port); 723 - uart_port_unlock_irqrestore(port, flags); 724 727 } 725 728 uart_port_deref(port); 726 729 } ··· 1084 1089 struct uart_state *state = tty->driver_data; 1085 1090 struct tty_port *port = &state->port; 1086 1091 struct uart_port *uport; 1087 - int result; 1088 1092 1089 1093 guard(mutex)(&port->mutex); 1090 1094 ··· 1091 1097 if (!uport || tty_io_error(tty)) 1092 1098 return -EIO; 1093 1099 1094 - uart_port_lock_irq(uport); 1095 - result = uport->mctrl; 1096 - result |= uport->ops->get_mctrl(uport); 1097 - uart_port_unlock_irq(uport); 1100 + guard(uart_port_lock_irq)(uport); 1098 1101 1099 - return result; 1102 + return uport->mctrl | uport->ops->get_mctrl(uport); 1100 1103 } 1101 1104 1102 1105 static int ··· 1217 1226 uport = uart_port_ref(state); 1218 1227 if (!uport) 1219 1228 return -EIO; 1220 - uart_port_lock_irq(uport); 1221 - memcpy(&cprev, &uport->icount, sizeof(struct uart_icount)); 1222 - uart_enable_ms(uport); 1223 - uart_port_unlock_irq(uport); 1229 + scoped_guard(uart_port_lock_irq, uport) { 1230 + memcpy(&cprev, &uport->icount, sizeof(struct uart_icount)); 1231 + uart_enable_ms(uport); 1232 + } 1224 1233 1225 1234 add_wait_queue(&port->delta_msr_wait, &wait); 1226 1235 for (;;) { 1227 - uart_port_lock_irq(uport); 1228 - memcpy(&cnow, &uport->icount, sizeof(struct uart_icount)); 1229 - uart_port_unlock_irq(uport); 1236 + scoped_guard(uart_port_lock_irq, uport) 1237 + memcpy(&cnow, &uport->icount, sizeof(struct uart_icount)); 1230 1238 1231 1239 set_current_state(TASK_INTERRUPTIBLE); 1232 1240 ··· 1420 1430 static int uart_rs485_config(struct uart_port *port) 1421 1431 { 1422 1432 struct serial_rs485 *rs485 = &port->rs485; 1423 - unsigned long flags; 1424 1433 int ret; 1425 1434 1426 1435 if (!(rs485->flags & SER_RS485_ENABLED)) ··· 1429 1440 uart_set_rs485_termination(port, rs485); 1430 1441 uart_set_rs485_rx_during_tx(port, rs485); 1431 1442 1432 - uart_port_lock_irqsave(port, &flags); 1433 - ret = port->rs485_config(port, NULL, rs485); 1434 - uart_port_unlock_irqrestore(port, flags); 1443 + scoped_guard(uart_port_lock_irqsave, port) 1444 + ret = port->rs485_config(port, NULL, rs485); 1435 1445 if (ret) { 1436 1446 memset(rs485, 0, sizeof(*rs485)); 1437 1447 /* unset GPIOs */ ··· 1444 1456 static int uart_get_rs485_config(struct uart_port *port, 1445 1457 struct serial_rs485 __user *rs485) 1446 1458 { 1447 - unsigned long flags; 1448 1459 struct serial_rs485 aux; 1449 1460 1450 - uart_port_lock_irqsave(port, &flags); 1451 - aux = port->rs485; 1452 - uart_port_unlock_irqrestore(port, flags); 1461 + scoped_guard(uart_port_lock_irqsave, port) 1462 + aux = port->rs485; 1453 1463 1454 1464 if (copy_to_user(rs485, &aux, sizeof(aux))) 1455 1465 return -EFAULT; ··· 1460 1474 { 1461 1475 struct serial_rs485 rs485; 1462 1476 int ret; 1463 - unsigned long flags; 1464 1477 1465 1478 if (!(port->rs485_supported.flags & SER_RS485_ENABLED)) 1466 1479 return -ENOTTY; ··· 1474 1489 uart_set_rs485_termination(port, &rs485); 1475 1490 uart_set_rs485_rx_during_tx(port, &rs485); 1476 1491 1477 - uart_port_lock_irqsave(port, &flags); 1478 - ret = port->rs485_config(port, &tty->termios, &rs485); 1479 - if (!ret) { 1480 - port->rs485 = rs485; 1492 + scoped_guard(uart_port_lock_irqsave, port) { 1493 + ret = port->rs485_config(port, &tty->termios, &rs485); 1494 + if (!ret) { 1495 + port->rs485 = rs485; 1481 1496 1482 - /* Reset RTS and other mctrl lines when disabling RS485 */ 1483 - if (!(rs485.flags & SER_RS485_ENABLED)) 1484 - port->ops->set_mctrl(port, port->mctrl); 1497 + /* Reset RTS and other mctrl lines when disabling RS485 */ 1498 + if (!(rs485.flags & SER_RS485_ENABLED)) 1499 + port->ops->set_mctrl(port, port->mctrl); 1500 + } 1485 1501 } 1486 - uart_port_unlock_irqrestore(port, flags); 1487 1502 if (ret) { 1488 1503 /* restore old GPIO settings */ 1489 1504 gpiod_set_value_cansleep(port->rs485_term_gpio, ··· 1502 1517 static int uart_get_iso7816_config(struct uart_port *port, 1503 1518 struct serial_iso7816 __user *iso7816) 1504 1519 { 1505 - unsigned long flags; 1506 1520 struct serial_iso7816 aux; 1507 1521 1508 1522 if (!port->iso7816_config) 1509 1523 return -ENOTTY; 1510 1524 1511 - uart_port_lock_irqsave(port, &flags); 1512 - aux = port->iso7816; 1513 - uart_port_unlock_irqrestore(port, flags); 1525 + scoped_guard(uart_port_lock_irqsave, port) 1526 + aux = port->iso7816; 1514 1527 1515 1528 if (copy_to_user(iso7816, &aux, sizeof(aux))) 1516 1529 return -EFAULT; ··· 1520 1537 struct serial_iso7816 __user *iso7816_user) 1521 1538 { 1522 1539 struct serial_iso7816 iso7816; 1523 - int i, ret; 1524 - unsigned long flags; 1540 + int i; 1525 1541 1526 1542 if (!port->iso7816_config) 1527 1543 return -ENOTTY; ··· 1536 1554 if (iso7816.reserved[i]) 1537 1555 return -EINVAL; 1538 1556 1539 - uart_port_lock_irqsave(port, &flags); 1540 - ret = port->iso7816_config(port, &iso7816); 1541 - uart_port_unlock_irqrestore(port, flags); 1542 - if (ret) 1543 - return ret; 1557 + scoped_guard(uart_port_lock_irqsave, port) { 1558 + int ret = port->iso7816_config(port, &iso7816); 1559 + if (ret) 1560 + return ret; 1561 + } 1544 1562 1545 1563 if (copy_to_user(iso7816_user, &port->iso7816, sizeof(port->iso7816))) 1546 1564 return -EFAULT; ··· 1752 1770 if (WARN(!uport, "detached port still initialized!\n")) 1753 1771 return; 1754 1772 1755 - uart_port_lock_irq(uport); 1756 - uport->ops->stop_rx(uport); 1757 - uart_port_unlock_irq(uport); 1773 + scoped_guard(uart_port_lock_irq, uport) 1774 + uport->ops->stop_rx(uport); 1758 1775 1759 1776 serial_base_port_shutdown(uport); 1760 1777 uart_port_shutdown(port); ··· 2025 2044 pm_state = state->pm_state; 2026 2045 if (pm_state != UART_PM_STATE_ON) 2027 2046 uart_change_pm(state, UART_PM_STATE_ON); 2028 - uart_port_lock_irq(uport); 2029 - status = uport->ops->get_mctrl(uport); 2030 - uart_port_unlock_irq(uport); 2047 + scoped_guard(uart_port_lock_irq, uport) 2048 + status = uport->ops->get_mctrl(uport); 2031 2049 if (pm_state != UART_PM_STATE_ON) 2032 2050 uart_change_pm(state, pm_state); 2033 2051 ··· 2335 2355 */ 2336 2356 if (!console_suspend_enabled && uart_console(uport)) { 2337 2357 if (uport->ops->start_rx) { 2338 - uart_port_lock_irq(uport); 2358 + guard(uart_port_lock_irq)(uport); 2339 2359 uport->ops->stop_rx(uport); 2340 - uart_port_unlock_irq(uport); 2341 2360 } 2342 2361 device_set_awake_path(uport->dev); 2343 2362 return 0; ··· 2352 2373 tty_port_set_suspended(port, true); 2353 2374 tty_port_set_initialized(port, false); 2354 2375 2355 - uart_port_lock_irq(uport); 2356 - ops->stop_tx(uport); 2357 - if (!(uport->rs485.flags & SER_RS485_ENABLED)) 2358 - ops->set_mctrl(uport, 0); 2359 - /* save mctrl so it can be restored on resume */ 2360 - mctrl = uport->mctrl; 2361 - uport->mctrl = 0; 2362 - ops->stop_rx(uport); 2363 - uart_port_unlock_irq(uport); 2376 + scoped_guard(uart_port_lock_irq, uport) { 2377 + ops->stop_tx(uport); 2378 + if (!(uport->rs485.flags & SER_RS485_ENABLED)) 2379 + ops->set_mctrl(uport, 0); 2380 + /* save mctrl so it can be restored on resume */ 2381 + mctrl = uport->mctrl; 2382 + uport->mctrl = 0; 2383 + ops->stop_rx(uport); 2384 + } 2364 2385 2365 2386 /* 2366 2387 * Wait for the transmitter to empty. ··· 2429 2450 uart_change_pm(state, UART_PM_STATE_ON); 2430 2451 uport->ops->set_termios(uport, &termios, NULL); 2431 2452 if (!console_suspend_enabled && uport->ops->start_rx) { 2432 - uart_port_lock_irq(uport); 2453 + guard(uart_port_lock_irq)(uport); 2433 2454 uport->ops->start_rx(uport); 2434 - uart_port_unlock_irq(uport); 2435 2455 } 2436 2456 if (console_suspend_enabled) 2437 2457 console_resume(uport->cons); ··· 2441 2463 int ret; 2442 2464 2443 2465 uart_change_pm(state, UART_PM_STATE_ON); 2444 - uart_port_lock_irq(uport); 2445 - if (!(uport->rs485.flags & SER_RS485_ENABLED)) 2446 - ops->set_mctrl(uport, 0); 2447 - uart_port_unlock_irq(uport); 2466 + scoped_guard(uart_port_lock_irq, uport) 2467 + if (!(uport->rs485.flags & SER_RS485_ENABLED)) 2468 + ops->set_mctrl(uport, 0); 2448 2469 if (console_suspend_enabled || !uart_console(uport)) { 2449 2470 /* Protected by port mutex for now */ 2450 2471 struct tty_struct *tty = port->tty; ··· 2453 2476 if (tty) 2454 2477 uart_change_line_settings(tty, state, NULL); 2455 2478 uart_rs485_config(uport); 2456 - uart_port_lock_irq(uport); 2457 - if (!(uport->rs485.flags & SER_RS485_ENABLED)) 2458 - ops->set_mctrl(uport, uport->mctrl); 2459 - ops->start_tx(uport); 2460 - uart_port_unlock_irq(uport); 2479 + scoped_guard(uart_port_lock_irq, uport) { 2480 + if (!(uport->rs485.flags & SER_RS485_ENABLED)) 2481 + ops->set_mctrl(uport, uport->mctrl); 2482 + ops->start_tx(uport); 2483 + } 2461 2484 tty_port_set_initialized(port, true); 2462 2485 } else { 2463 2486 /* ··· 2551 2574 } 2552 2575 2553 2576 if (port->type != PORT_UNKNOWN) { 2554 - unsigned long flags; 2555 - 2556 2577 uart_report_port(drv, port); 2557 2578 2558 2579 /* Synchronize with possible boot console. */ ··· 2565 2590 * keep the DTR setting that is set in uart_set_options() 2566 2591 * We probably don't need a spinlock around this, but 2567 2592 */ 2568 - uart_port_lock_irqsave(port, &flags); 2569 - port->mctrl &= TIOCM_DTR; 2570 - if (!(port->rs485.flags & SER_RS485_ENABLED)) 2571 - port->ops->set_mctrl(port, port->mctrl); 2572 - uart_port_unlock_irqrestore(port, flags); 2593 + scoped_guard(uart_port_lock_irqsave, port) { 2594 + port->mctrl &= TIOCM_DTR; 2595 + if (!(port->rs485.flags & SER_RS485_ENABLED)) 2596 + port->ops->set_mctrl(port, port->mctrl); 2597 + } 2573 2598 2574 2599 uart_rs485_config(port); 2575 2600