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

Merge patch series "can: kvaser_pciefd: Fix ISR race conditions"

Axel Forsman <axfo@kvaser.com> says:

This patch series fixes a couple of race conditions in the
kvaser_pciefd driver surfaced by enabling MSI interrupts and the new
Kvaser PCIe 8xCAN.

Changes since version 2:
* Rebase onto linux-can/main to resolve del_timer()/timer_delete()
merge conflict.
* Reword 2nd commit message slightly.

Changes since version 1:
* Change type of srb_cmd_reg from "__le32 __iomem *" to
"void __iomem *".
* Maintain TX FIFO count in driver instead of querying HW.
* Stop queue at end of .start_xmit() if full.

Link: https://patch.msgid.link/20250520114332.8961-1-axfo@kvaser.com
Signed-off-by: Marc Kleine-Budde <mkl@pengutronix.de>

+103 -81
+103 -81
drivers/net/can/kvaser_pciefd.c
··· 16 16 #include <linux/netdevice.h> 17 17 #include <linux/pci.h> 18 18 #include <linux/timer.h> 19 + #include <net/netdev_queues.h> 19 20 20 21 MODULE_LICENSE("Dual BSD/GPL"); 21 22 MODULE_AUTHOR("Kvaser AB <support@kvaser.com>"); ··· 411 410 void __iomem *reg_base; 412 411 struct can_berr_counter bec; 413 412 u8 cmd_seq; 413 + u8 tx_max_count; 414 + u8 tx_idx; 415 + u8 ack_idx; 414 416 int err_rep_cnt; 415 - int echo_idx; 417 + unsigned int completed_tx_pkts; 418 + unsigned int completed_tx_bytes; 416 419 spinlock_t lock; /* Locks sensitive registers (e.g. MODE) */ 417 - spinlock_t echo_lock; /* Locks the message echo buffer */ 418 420 struct timer_list bec_poll_timer; 419 421 struct completion start_comp, flush_comp; 420 422 }; ··· 718 714 int ret; 719 715 struct kvaser_pciefd_can *can = netdev_priv(netdev); 720 716 717 + can->tx_idx = 0; 718 + can->ack_idx = 0; 719 + 721 720 ret = open_candev(netdev); 722 721 if (ret) 723 722 return ret; ··· 752 745 timer_delete(&can->bec_poll_timer); 753 746 } 754 747 can->can.state = CAN_STATE_STOPPED; 748 + netdev_reset_queue(netdev); 755 749 close_candev(netdev); 756 750 757 751 return ret; 758 752 } 759 753 754 + static unsigned int kvaser_pciefd_tx_avail(const struct kvaser_pciefd_can *can) 755 + { 756 + return can->tx_max_count - (READ_ONCE(can->tx_idx) - READ_ONCE(can->ack_idx)); 757 + } 758 + 760 759 static int kvaser_pciefd_prepare_tx_packet(struct kvaser_pciefd_tx_packet *p, 761 - struct kvaser_pciefd_can *can, 760 + struct can_priv *can, u8 seq, 762 761 struct sk_buff *skb) 763 762 { 764 763 struct canfd_frame *cf = (struct canfd_frame *)skb->data; 765 764 int packet_size; 766 - int seq = can->echo_idx; 767 765 768 766 memset(p, 0, sizeof(*p)); 769 - if (can->can.ctrlmode & CAN_CTRLMODE_ONE_SHOT) 767 + if (can->ctrlmode & CAN_CTRLMODE_ONE_SHOT) 770 768 p->header[1] |= KVASER_PCIEFD_TPACKET_SMS; 771 769 772 770 if (cf->can_id & CAN_RTR_FLAG) ··· 794 782 } else { 795 783 p->header[1] |= 796 784 FIELD_PREP(KVASER_PCIEFD_RPACKET_DLC_MASK, 797 - can_get_cc_dlc((struct can_frame *)cf, can->can.ctrlmode)); 785 + can_get_cc_dlc((struct can_frame *)cf, can->ctrlmode)); 798 786 } 799 787 800 788 p->header[1] |= FIELD_PREP(KVASER_PCIEFD_PACKET_SEQ_MASK, seq); ··· 809 797 struct net_device *netdev) 810 798 { 811 799 struct kvaser_pciefd_can *can = netdev_priv(netdev); 812 - unsigned long irq_flags; 813 800 struct kvaser_pciefd_tx_packet packet; 801 + unsigned int seq = can->tx_idx & (can->can.echo_skb_max - 1); 802 + unsigned int frame_len; 814 803 int nr_words; 815 - u8 count; 816 804 817 805 if (can_dev_dropped_skb(netdev, skb)) 818 806 return NETDEV_TX_OK; 807 + if (!netif_subqueue_maybe_stop(netdev, 0, kvaser_pciefd_tx_avail(can), 1, 1)) 808 + return NETDEV_TX_BUSY; 819 809 820 - nr_words = kvaser_pciefd_prepare_tx_packet(&packet, can, skb); 810 + nr_words = kvaser_pciefd_prepare_tx_packet(&packet, &can->can, seq, skb); 821 811 822 - spin_lock_irqsave(&can->echo_lock, irq_flags); 823 812 /* Prepare and save echo skb in internal slot */ 824 - can_put_echo_skb(skb, netdev, can->echo_idx, 0); 825 - 826 - /* Move echo index to the next slot */ 827 - can->echo_idx = (can->echo_idx + 1) % can->can.echo_skb_max; 813 + WRITE_ONCE(can->can.echo_skb[seq], NULL); 814 + frame_len = can_skb_get_frame_len(skb); 815 + can_put_echo_skb(skb, netdev, seq, frame_len); 816 + netdev_sent_queue(netdev, frame_len); 817 + WRITE_ONCE(can->tx_idx, can->tx_idx + 1); 828 818 829 819 /* Write header to fifo */ 830 820 iowrite32(packet.header[0], ··· 850 836 KVASER_PCIEFD_KCAN_FIFO_LAST_REG); 851 837 } 852 838 853 - count = FIELD_GET(KVASER_PCIEFD_KCAN_TX_NR_PACKETS_CURRENT_MASK, 854 - ioread32(can->reg_base + KVASER_PCIEFD_KCAN_TX_NR_PACKETS_REG)); 855 - /* No room for a new message, stop the queue until at least one 856 - * successful transmit 857 - */ 858 - if (count >= can->can.echo_skb_max || can->can.echo_skb[can->echo_idx]) 859 - netif_stop_queue(netdev); 860 - spin_unlock_irqrestore(&can->echo_lock, irq_flags); 839 + netif_subqueue_maybe_stop(netdev, 0, kvaser_pciefd_tx_avail(can), 1, 1); 861 840 862 841 return NETDEV_TX_OK; 863 842 } ··· 977 970 can->kv_pcie = pcie; 978 971 can->cmd_seq = 0; 979 972 can->err_rep_cnt = 0; 973 + can->completed_tx_pkts = 0; 974 + can->completed_tx_bytes = 0; 980 975 can->bec.txerr = 0; 981 976 can->bec.rxerr = 0; 982 977 ··· 992 983 tx_nr_packets_max = 993 984 FIELD_GET(KVASER_PCIEFD_KCAN_TX_NR_PACKETS_MAX_MASK, 994 985 ioread32(can->reg_base + KVASER_PCIEFD_KCAN_TX_NR_PACKETS_REG)); 986 + can->tx_max_count = min(KVASER_PCIEFD_CAN_TX_MAX_COUNT, tx_nr_packets_max - 1); 995 987 996 988 can->can.clock.freq = pcie->freq; 997 - can->can.echo_skb_max = min(KVASER_PCIEFD_CAN_TX_MAX_COUNT, tx_nr_packets_max - 1); 998 - can->echo_idx = 0; 999 - spin_lock_init(&can->echo_lock); 989 + can->can.echo_skb_max = roundup_pow_of_two(can->tx_max_count); 1000 990 spin_lock_init(&can->lock); 1001 991 1002 992 can->can.bittiming_const = &kvaser_pciefd_bittiming_const; ··· 1209 1201 skb = alloc_canfd_skb(priv->dev, &cf); 1210 1202 if (!skb) { 1211 1203 priv->dev->stats.rx_dropped++; 1212 - return -ENOMEM; 1204 + return 0; 1213 1205 } 1214 1206 1215 1207 cf->len = can_fd_dlc2len(dlc); ··· 1221 1213 skb = alloc_can_skb(priv->dev, (struct can_frame **)&cf); 1222 1214 if (!skb) { 1223 1215 priv->dev->stats.rx_dropped++; 1224 - return -ENOMEM; 1216 + return 0; 1225 1217 } 1226 1218 can_frame_set_cc_len((struct can_frame *)cf, dlc, priv->ctrlmode); 1227 1219 } ··· 1239 1231 priv->dev->stats.rx_packets++; 1240 1232 kvaser_pciefd_set_skb_timestamp(pcie, skb, p->timestamp); 1241 1233 1242 - return netif_rx(skb); 1234 + netif_rx(skb); 1235 + 1236 + return 0; 1243 1237 } 1244 1238 1245 1239 static void kvaser_pciefd_change_state(struct kvaser_pciefd_can *can, ··· 1520 1510 netdev_dbg(can->can.dev, "Packet was flushed\n"); 1521 1511 } else { 1522 1512 int echo_idx = FIELD_GET(KVASER_PCIEFD_PACKET_SEQ_MASK, p->header[0]); 1523 - int len; 1524 - u8 count; 1513 + unsigned int len, frame_len = 0; 1525 1514 struct sk_buff *skb; 1526 1515 1516 + if (echo_idx != (can->ack_idx & (can->can.echo_skb_max - 1))) 1517 + return 0; 1527 1518 skb = can->can.echo_skb[echo_idx]; 1528 - if (skb) 1529 - kvaser_pciefd_set_skb_timestamp(pcie, skb, p->timestamp); 1530 - len = can_get_echo_skb(can->can.dev, echo_idx, NULL); 1531 - count = FIELD_GET(KVASER_PCIEFD_KCAN_TX_NR_PACKETS_CURRENT_MASK, 1532 - ioread32(can->reg_base + KVASER_PCIEFD_KCAN_TX_NR_PACKETS_REG)); 1519 + if (!skb) 1520 + return 0; 1521 + kvaser_pciefd_set_skb_timestamp(pcie, skb, p->timestamp); 1522 + len = can_get_echo_skb(can->can.dev, echo_idx, &frame_len); 1533 1523 1534 - if (count < can->can.echo_skb_max && netif_queue_stopped(can->can.dev)) 1535 - netif_wake_queue(can->can.dev); 1524 + /* Pairs with barrier in kvaser_pciefd_start_xmit() */ 1525 + smp_store_release(&can->ack_idx, can->ack_idx + 1); 1526 + can->completed_tx_pkts++; 1527 + can->completed_tx_bytes += frame_len; 1536 1528 1537 1529 if (!one_shot_fail) { 1538 1530 can->can.dev->stats.tx_bytes += len; ··· 1650 1638 { 1651 1639 int pos = 0; 1652 1640 int res = 0; 1641 + unsigned int i; 1653 1642 1654 1643 do { 1655 1644 res = kvaser_pciefd_read_packet(pcie, &pos, dma_buf); 1656 1645 } while (!res && pos > 0 && pos < KVASER_PCIEFD_DMA_SIZE); 1657 1646 1647 + /* Report ACKs in this buffer to BQL en masse for correct periods */ 1648 + for (i = 0; i < pcie->nr_channels; ++i) { 1649 + struct kvaser_pciefd_can *can = pcie->can[i]; 1650 + 1651 + if (!can->completed_tx_pkts) 1652 + continue; 1653 + netif_subqueue_completed_wake(can->can.dev, 0, 1654 + can->completed_tx_pkts, 1655 + can->completed_tx_bytes, 1656 + kvaser_pciefd_tx_avail(can), 1); 1657 + can->completed_tx_pkts = 0; 1658 + can->completed_tx_bytes = 0; 1659 + } 1660 + 1658 1661 return res; 1659 1662 } 1660 1663 1661 - static u32 kvaser_pciefd_receive_irq(struct kvaser_pciefd *pcie) 1664 + static void kvaser_pciefd_receive_irq(struct kvaser_pciefd *pcie) 1662 1665 { 1666 + void __iomem *srb_cmd_reg = KVASER_PCIEFD_SRB_ADDR(pcie) + KVASER_PCIEFD_SRB_CMD_REG; 1663 1667 u32 irq = ioread32(KVASER_PCIEFD_SRB_ADDR(pcie) + KVASER_PCIEFD_SRB_IRQ_REG); 1664 1668 1665 - if (irq & KVASER_PCIEFD_SRB_IRQ_DPD0) 1666 - kvaser_pciefd_read_buffer(pcie, 0); 1669 + iowrite32(irq, KVASER_PCIEFD_SRB_ADDR(pcie) + KVASER_PCIEFD_SRB_IRQ_REG); 1667 1670 1668 - if (irq & KVASER_PCIEFD_SRB_IRQ_DPD1) 1671 + if (irq & KVASER_PCIEFD_SRB_IRQ_DPD0) { 1672 + kvaser_pciefd_read_buffer(pcie, 0); 1673 + iowrite32(KVASER_PCIEFD_SRB_CMD_RDB0, srb_cmd_reg); /* Rearm buffer */ 1674 + } 1675 + 1676 + if (irq & KVASER_PCIEFD_SRB_IRQ_DPD1) { 1669 1677 kvaser_pciefd_read_buffer(pcie, 1); 1678 + iowrite32(KVASER_PCIEFD_SRB_CMD_RDB1, srb_cmd_reg); /* Rearm buffer */ 1679 + } 1670 1680 1671 1681 if (unlikely(irq & KVASER_PCIEFD_SRB_IRQ_DOF0 || 1672 1682 irq & KVASER_PCIEFD_SRB_IRQ_DOF1 || 1673 1683 irq & KVASER_PCIEFD_SRB_IRQ_DUF0 || 1674 1684 irq & KVASER_PCIEFD_SRB_IRQ_DUF1)) 1675 1685 dev_err(&pcie->pci->dev, "DMA IRQ error 0x%08X\n", irq); 1676 - 1677 - iowrite32(irq, KVASER_PCIEFD_SRB_ADDR(pcie) + KVASER_PCIEFD_SRB_IRQ_REG); 1678 - return irq; 1679 1686 } 1680 1687 1681 1688 static void kvaser_pciefd_transmit_irq(struct kvaser_pciefd_can *can) ··· 1722 1691 struct kvaser_pciefd *pcie = (struct kvaser_pciefd *)dev; 1723 1692 const struct kvaser_pciefd_irq_mask *irq_mask = pcie->driver_data->irq_mask; 1724 1693 u32 pci_irq = ioread32(KVASER_PCIEFD_PCI_IRQ_ADDR(pcie)); 1725 - u32 srb_irq = 0; 1726 - u32 srb_release = 0; 1727 1694 int i; 1728 1695 1729 1696 if (!(pci_irq & irq_mask->all)) 1730 1697 return IRQ_NONE; 1731 1698 1699 + iowrite32(0, KVASER_PCIEFD_PCI_IEN_ADDR(pcie)); 1700 + 1732 1701 if (pci_irq & irq_mask->kcan_rx0) 1733 - srb_irq = kvaser_pciefd_receive_irq(pcie); 1702 + kvaser_pciefd_receive_irq(pcie); 1734 1703 1735 1704 for (i = 0; i < pcie->nr_channels; i++) { 1736 1705 if (pci_irq & irq_mask->kcan_tx[i]) 1737 1706 kvaser_pciefd_transmit_irq(pcie->can[i]); 1738 1707 } 1739 1708 1740 - if (srb_irq & KVASER_PCIEFD_SRB_IRQ_DPD0) 1741 - srb_release |= KVASER_PCIEFD_SRB_CMD_RDB0; 1742 - 1743 - if (srb_irq & KVASER_PCIEFD_SRB_IRQ_DPD1) 1744 - srb_release |= KVASER_PCIEFD_SRB_CMD_RDB1; 1745 - 1746 - if (srb_release) 1747 - iowrite32(srb_release, KVASER_PCIEFD_SRB_ADDR(pcie) + KVASER_PCIEFD_SRB_CMD_REG); 1709 + iowrite32(irq_mask->all, KVASER_PCIEFD_PCI_IEN_ADDR(pcie)); 1748 1710 1749 1711 return IRQ_HANDLED; 1750 1712 } ··· 1757 1733 } 1758 1734 } 1759 1735 1736 + static void kvaser_pciefd_disable_irq_srcs(struct kvaser_pciefd *pcie) 1737 + { 1738 + unsigned int i; 1739 + 1740 + /* Masking PCI_IRQ is insufficient as running ISR will unmask it */ 1741 + iowrite32(0, KVASER_PCIEFD_SRB_ADDR(pcie) + KVASER_PCIEFD_SRB_IEN_REG); 1742 + for (i = 0; i < pcie->nr_channels; ++i) 1743 + iowrite32(0, pcie->can[i]->reg_base + KVASER_PCIEFD_KCAN_IEN_REG); 1744 + } 1745 + 1760 1746 static int kvaser_pciefd_probe(struct pci_dev *pdev, 1761 1747 const struct pci_device_id *id) 1762 1748 { 1763 1749 int ret; 1764 1750 struct kvaser_pciefd *pcie; 1765 1751 const struct kvaser_pciefd_irq_mask *irq_mask; 1766 - void __iomem *irq_en_base; 1767 1752 1768 1753 pcie = devm_kzalloc(&pdev->dev, sizeof(*pcie), GFP_KERNEL); 1769 1754 if (!pcie) ··· 1838 1805 KVASER_PCIEFD_SRB_ADDR(pcie) + KVASER_PCIEFD_SRB_IEN_REG); 1839 1806 1840 1807 /* Enable PCI interrupts */ 1841 - irq_en_base = KVASER_PCIEFD_PCI_IEN_ADDR(pcie); 1842 - iowrite32(irq_mask->all, irq_en_base); 1808 + iowrite32(irq_mask->all, KVASER_PCIEFD_PCI_IEN_ADDR(pcie)); 1843 1809 /* Ready the DMA buffers */ 1844 1810 iowrite32(KVASER_PCIEFD_SRB_CMD_RDB0, 1845 1811 KVASER_PCIEFD_SRB_ADDR(pcie) + KVASER_PCIEFD_SRB_CMD_REG); ··· 1852 1820 return 0; 1853 1821 1854 1822 err_free_irq: 1855 - /* Disable PCI interrupts */ 1856 - iowrite32(0, irq_en_base); 1823 + kvaser_pciefd_disable_irq_srcs(pcie); 1857 1824 free_irq(pcie->pci->irq, pcie); 1858 1825 1859 1826 err_pci_free_irq_vectors: ··· 1875 1844 return ret; 1876 1845 } 1877 1846 1878 - static void kvaser_pciefd_remove_all_ctrls(struct kvaser_pciefd *pcie) 1879 - { 1880 - int i; 1881 - 1882 - for (i = 0; i < pcie->nr_channels; i++) { 1883 - struct kvaser_pciefd_can *can = pcie->can[i]; 1884 - 1885 - if (can) { 1886 - iowrite32(0, can->reg_base + KVASER_PCIEFD_KCAN_IEN_REG); 1887 - unregister_candev(can->can.dev); 1888 - timer_delete(&can->bec_poll_timer); 1889 - kvaser_pciefd_pwm_stop(can); 1890 - free_candev(can->can.dev); 1891 - } 1892 - } 1893 - } 1894 - 1895 1847 static void kvaser_pciefd_remove(struct pci_dev *pdev) 1896 1848 { 1897 1849 struct kvaser_pciefd *pcie = pci_get_drvdata(pdev); 1850 + unsigned int i; 1898 1851 1899 - kvaser_pciefd_remove_all_ctrls(pcie); 1852 + for (i = 0; i < pcie->nr_channels; ++i) { 1853 + struct kvaser_pciefd_can *can = pcie->can[i]; 1900 1854 1901 - /* Disable interrupts */ 1902 - iowrite32(0, KVASER_PCIEFD_SRB_ADDR(pcie) + KVASER_PCIEFD_SRB_CTRL_REG); 1903 - iowrite32(0, KVASER_PCIEFD_PCI_IEN_ADDR(pcie)); 1855 + unregister_candev(can->can.dev); 1856 + timer_delete(&can->bec_poll_timer); 1857 + kvaser_pciefd_pwm_stop(can); 1858 + } 1904 1859 1860 + kvaser_pciefd_disable_irq_srcs(pcie); 1905 1861 free_irq(pcie->pci->irq, pcie); 1906 1862 pci_free_irq_vectors(pcie->pci); 1863 + 1864 + for (i = 0; i < pcie->nr_channels; ++i) 1865 + free_candev(pcie->can[i]->can.dev); 1866 + 1907 1867 pci_iounmap(pdev, pcie->reg_base); 1908 1868 pci_release_regions(pdev); 1909 1869 pci_disable_device(pdev);