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.

Input: xpad - use guard notation when acquiring mutex and spinlock

Using guard notation makes the code more compact and error handling
more robust by ensuring that locks are released in all code paths
when control leaves critical section.

Link: https://lore.kernel.org/r/20240904043104.1030257-7-dmitry.torokhov@gmail.com
Signed-off-by: Dmitry Torokhov <dmitry.torokhov@gmail.com>

+34 -65
+34 -65
drivers/input/joystick/xpad.c
··· 1289 1289 struct device *dev = &xpad->intf->dev; 1290 1290 int status = urb->status; 1291 1291 int error; 1292 - unsigned long flags; 1293 1292 1294 - spin_lock_irqsave(&xpad->odata_lock, flags); 1293 + guard(spinlock_irqsave)(&xpad->odata_lock); 1295 1294 1296 1295 switch (status) { 1297 1296 case 0: ··· 1324 1325 xpad->irq_out_active = false; 1325 1326 } 1326 1327 } 1327 - 1328 - spin_unlock_irqrestore(&xpad->odata_lock, flags); 1329 1328 } 1330 1329 1331 1330 static int xpad_init_output(struct usb_interface *intf, struct usb_xpad *xpad, ··· 1388 1391 { 1389 1392 struct xpad_output_packet *packet = 1390 1393 &xpad->out_packets[XPAD_OUT_CMD_IDX]; 1391 - unsigned long flags; 1392 - int retval; 1393 1394 1394 - spin_lock_irqsave(&xpad->odata_lock, flags); 1395 + guard(spinlock_irqsave)(&xpad->odata_lock); 1395 1396 1396 1397 packet->data[0] = 0x08; 1397 1398 packet->data[1] = 0x00; ··· 1408 1413 1409 1414 /* Reset the sequence so we send out presence first */ 1410 1415 xpad->last_out_packet = -1; 1411 - retval = xpad_try_sending_next_out_packet(xpad); 1412 - 1413 - spin_unlock_irqrestore(&xpad->odata_lock, flags); 1414 - 1415 - return retval; 1416 + return xpad_try_sending_next_out_packet(xpad); 1416 1417 } 1417 1418 1418 1419 static int xpad_start_xbox_one(struct usb_xpad *xpad) 1419 1420 { 1420 - unsigned long flags; 1421 - int retval; 1421 + int error; 1422 1422 1423 1423 if (usb_ifnum_to_if(xpad->udev, GIP_WIRED_INTF_AUDIO)) { 1424 1424 /* ··· 1422 1432 * Controller for Series X|S (0x20d6:0x200e) to report the 1423 1433 * guide button. 1424 1434 */ 1425 - retval = usb_set_interface(xpad->udev, 1426 - GIP_WIRED_INTF_AUDIO, 0); 1427 - if (retval) 1435 + error = usb_set_interface(xpad->udev, 1436 + GIP_WIRED_INTF_AUDIO, 0); 1437 + if (error) 1428 1438 dev_warn(&xpad->dev->dev, 1429 1439 "unable to disable audio interface: %d\n", 1430 - retval); 1440 + error); 1431 1441 } 1432 1442 1433 - spin_lock_irqsave(&xpad->odata_lock, flags); 1443 + guard(spinlock_irqsave)(&xpad->odata_lock); 1434 1444 1435 1445 /* 1436 1446 * Begin the init sequence by attempting to send a packet. ··· 1438 1448 * sending any packets from the output ring. 1439 1449 */ 1440 1450 xpad->init_seq = 0; 1441 - retval = xpad_try_sending_next_out_packet(xpad); 1442 - 1443 - spin_unlock_irqrestore(&xpad->odata_lock, flags); 1444 - 1445 - return retval; 1451 + return xpad_try_sending_next_out_packet(xpad); 1446 1452 } 1447 1453 1448 1454 static void xpadone_ack_mode_report(struct usb_xpad *xpad, u8 seq_num) 1449 1455 { 1450 - unsigned long flags; 1451 1456 struct xpad_output_packet *packet = 1452 1457 &xpad->out_packets[XPAD_OUT_CMD_IDX]; 1453 1458 static const u8 mode_report_ack[] = { ··· 1450 1465 0x00, GIP_CMD_VIRTUAL_KEY, GIP_OPT_INTERNAL, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00 1451 1466 }; 1452 1467 1453 - spin_lock_irqsave(&xpad->odata_lock, flags); 1468 + guard(spinlock_irqsave)(&xpad->odata_lock); 1454 1469 1455 1470 packet->len = sizeof(mode_report_ack); 1456 1471 memcpy(packet->data, mode_report_ack, packet->len); ··· 1460 1475 /* Reset the sequence so we send out the ack now */ 1461 1476 xpad->last_out_packet = -1; 1462 1477 xpad_try_sending_next_out_packet(xpad); 1463 - 1464 - spin_unlock_irqrestore(&xpad->odata_lock, flags); 1465 1478 } 1466 1479 1467 1480 #ifdef CONFIG_JOYSTICK_XPAD_FF ··· 1469 1486 struct xpad_output_packet *packet = &xpad->out_packets[XPAD_OUT_FF_IDX]; 1470 1487 __u16 strong; 1471 1488 __u16 weak; 1472 - int retval; 1473 - unsigned long flags; 1474 1489 1475 1490 if (effect->type != FF_RUMBLE) 1476 1491 return 0; ··· 1476 1495 strong = effect->u.rumble.strong_magnitude; 1477 1496 weak = effect->u.rumble.weak_magnitude; 1478 1497 1479 - spin_lock_irqsave(&xpad->odata_lock, flags); 1498 + guard(spinlock_irqsave)(&xpad->odata_lock); 1480 1499 1481 1500 switch (xpad->xtype) { 1482 1501 case XTYPE_XBOX: ··· 1542 1561 dev_dbg(&xpad->dev->dev, 1543 1562 "%s - rumble command sent to unsupported xpad type: %d\n", 1544 1563 __func__, xpad->xtype); 1545 - retval = -EINVAL; 1546 - goto out; 1564 + return -EINVAL; 1547 1565 } 1548 1566 1549 - retval = xpad_try_sending_next_out_packet(xpad); 1550 - 1551 - out: 1552 - spin_unlock_irqrestore(&xpad->odata_lock, flags); 1553 - return retval; 1567 + return xpad_try_sending_next_out_packet(xpad); 1554 1568 } 1555 1569 1556 1570 static int xpad_init_ff(struct usb_xpad *xpad) ··· 1598 1622 { 1599 1623 struct xpad_output_packet *packet = 1600 1624 &xpad->out_packets[XPAD_OUT_LED_IDX]; 1601 - unsigned long flags; 1602 1625 1603 1626 command %= 16; 1604 1627 1605 - spin_lock_irqsave(&xpad->odata_lock, flags); 1628 + guard(spinlock_irqsave)(&xpad->odata_lock); 1606 1629 1607 1630 switch (xpad->xtype) { 1608 1631 case XTYPE_XBOX360: ··· 1631 1656 } 1632 1657 1633 1658 xpad_try_sending_next_out_packet(xpad); 1634 - 1635 - spin_unlock_irqrestore(&xpad->odata_lock, flags); 1636 1659 } 1637 1660 1638 1661 /* ··· 1755 1782 1756 1783 static void xpad360w_poweroff_controller(struct usb_xpad *xpad) 1757 1784 { 1758 - unsigned long flags; 1759 1785 struct xpad_output_packet *packet = 1760 1786 &xpad->out_packets[XPAD_OUT_CMD_IDX]; 1761 1787 1762 - spin_lock_irqsave(&xpad->odata_lock, flags); 1788 + guard(spinlock_irqsave)(&xpad->odata_lock); 1763 1789 1764 1790 packet->data[0] = 0x00; 1765 1791 packet->data[1] = 0x00; ··· 1778 1806 /* Reset the sequence so we send out poweroff now */ 1779 1807 xpad->last_out_packet = -1; 1780 1808 xpad_try_sending_next_out_packet(xpad); 1781 - 1782 - spin_unlock_irqrestore(&xpad->odata_lock, flags); 1783 1809 } 1784 1810 1785 1811 static int xpad360w_start_input(struct usb_xpad *xpad) ··· 2201 2231 if (auto_poweroff && xpad->pad_present) 2202 2232 xpad360w_poweroff_controller(xpad); 2203 2233 } else { 2204 - mutex_lock(&input->mutex); 2234 + guard(mutex)(&input->mutex); 2235 + 2205 2236 if (input_device_enabled(input)) 2206 2237 xpad_stop_input(xpad); 2207 - mutex_unlock(&input->mutex); 2208 2238 } 2209 2239 2210 2240 xpad_stop_output(xpad); ··· 2216 2246 { 2217 2247 struct usb_xpad *xpad = usb_get_intfdata(intf); 2218 2248 struct input_dev *input = xpad->dev; 2219 - int retval = 0; 2220 2249 2221 - if (xpad->xtype == XTYPE_XBOX360W) { 2222 - retval = xpad360w_start_input(xpad); 2223 - } else { 2224 - mutex_lock(&input->mutex); 2225 - if (input_device_enabled(input)) { 2226 - retval = xpad_start_input(xpad); 2227 - } else if (xpad->xtype == XTYPE_XBOXONE) { 2228 - /* 2229 - * Even if there are no users, we'll send Xbox One pads 2230 - * the startup sequence so they don't sit there and 2231 - * blink until somebody opens the input device again. 2232 - */ 2233 - retval = xpad_start_xbox_one(xpad); 2234 - } 2235 - mutex_unlock(&input->mutex); 2250 + if (xpad->xtype == XTYPE_XBOX360W) 2251 + return xpad360w_start_input(xpad); 2252 + 2253 + guard(mutex)(&input->mutex); 2254 + 2255 + if (input_device_enabled(input)) 2256 + return xpad_start_input(xpad); 2257 + 2258 + if (xpad->xtype == XTYPE_XBOXONE) { 2259 + /* 2260 + * Even if there are no users, we'll send Xbox One pads 2261 + * the startup sequence so they don't sit there and 2262 + * blink until somebody opens the input device again. 2263 + */ 2264 + return xpad_start_xbox_one(xpad); 2236 2265 } 2237 2266 2238 - return retval; 2267 + return 0; 2239 2268 } 2240 2269 2241 2270 static struct usb_driver xpad_driver = {