Commit 80af10e2 authored by Ahmed S. Darwish's avatar Ahmed S. Darwish Committed by Luis Henriques

can: kvaser_usb: Fix tx queue start/stop race conditions

commit a9dc960c upstream.

A number of tx queue wake-up events went missing due to the
outlined scenario below. Start state is a pool of 16 tx URBs,
active tx_urbs count = 15, with the netdev tx queue open.

CPU #1 [softirq]                         CPU #2 [softirq]
start_xmit()                             tx_acknowledge()
................                         ................

atomic_inc(&tx_urbs);
if (atomic_read(&tx_urbs) >= 16) {
                        -->
                                         atomic_dec(&tx_urbs);
                                         netif_wake_queue();
                                         return;
                        <--
    netif_stop_queue();
}

At the end, the correct state expected is a 15 tx_urbs count
value with the tx queue state _open_. Due to the race, we get
the same tx_urbs value but with the tx queue state _stopped_.
The wake-up event is completely lost.

Thus avoid hand-rolled concurrency mechanisms and use a proper
lock for contexts and tx queue protection.
Signed-off-by: default avatarAhmed S. Darwish <ahmed.darwish@valeo.com>
Signed-off-by: default avatarMarc Kleine-Budde <mkl@pengutronix.de>
[ luis: backported to 3.16: adjusted context ]
Signed-off-by: default avatarLuis Henriques <luis.henriques@canonical.com>
parent 0816dd89
...@@ -12,6 +12,7 @@ ...@@ -12,6 +12,7 @@
* Copyright (C) 2012 Olivier Sobrie <olivier@sobrie.be> * Copyright (C) 2012 Olivier Sobrie <olivier@sobrie.be>
*/ */
#include <linux/spinlock.h>
#include <linux/kernel.h> #include <linux/kernel.h>
#include <linux/completion.h> #include <linux/completion.h>
#include <linux/module.h> #include <linux/module.h>
...@@ -298,10 +299,11 @@ struct kvaser_usb { ...@@ -298,10 +299,11 @@ struct kvaser_usb {
struct kvaser_usb_net_priv { struct kvaser_usb_net_priv {
struct can_priv can; struct can_priv can;
atomic_t active_tx_urbs; spinlock_t tx_contexts_lock;
struct usb_anchor tx_submitted; int active_tx_contexts;
struct kvaser_usb_tx_urb_context tx_contexts[MAX_TX_URBS]; struct kvaser_usb_tx_urb_context tx_contexts[MAX_TX_URBS];
struct usb_anchor tx_submitted;
struct completion start_comp, stop_comp; struct completion start_comp, stop_comp;
struct kvaser_usb *dev; struct kvaser_usb *dev;
...@@ -506,6 +508,7 @@ static void kvaser_usb_tx_acknowledge(const struct kvaser_usb *dev, ...@@ -506,6 +508,7 @@ static void kvaser_usb_tx_acknowledge(const struct kvaser_usb *dev,
struct can_frame *cf; struct can_frame *cf;
u8 channel = msg->u.tx_acknowledge.channel; u8 channel = msg->u.tx_acknowledge.channel;
u8 tid = msg->u.tx_acknowledge.tid; u8 tid = msg->u.tx_acknowledge.tid;
unsigned long flags;
if (channel >= dev->nchannels) { if (channel >= dev->nchannels) {
dev_err(dev->udev->dev.parent, dev_err(dev->udev->dev.parent,
...@@ -545,12 +548,15 @@ static void kvaser_usb_tx_acknowledge(const struct kvaser_usb *dev, ...@@ -545,12 +548,15 @@ static void kvaser_usb_tx_acknowledge(const struct kvaser_usb *dev,
stats->tx_packets++; stats->tx_packets++;
stats->tx_bytes += context->dlc; stats->tx_bytes += context->dlc;
can_get_echo_skb(priv->netdev, context->echo_index);
context->echo_index = MAX_TX_URBS; spin_lock_irqsave(&priv->tx_contexts_lock, flags);
atomic_dec(&priv->active_tx_urbs);
can_get_echo_skb(priv->netdev, context->echo_index);
context->echo_index = MAX_TX_URBS;
--priv->active_tx_contexts;
netif_wake_queue(priv->netdev); netif_wake_queue(priv->netdev);
spin_unlock_irqrestore(&priv->tx_contexts_lock, flags);
} }
static void kvaser_usb_simple_msg_callback(struct urb *urb) static void kvaser_usb_simple_msg_callback(struct urb *urb)
...@@ -611,17 +617,6 @@ static int kvaser_usb_simple_msg_async(struct kvaser_usb_net_priv *priv, ...@@ -611,17 +617,6 @@ static int kvaser_usb_simple_msg_async(struct kvaser_usb_net_priv *priv,
return 0; return 0;
} }
static void kvaser_usb_unlink_tx_urbs(struct kvaser_usb_net_priv *priv)
{
int i;
usb_kill_anchored_urbs(&priv->tx_submitted);
atomic_set(&priv->active_tx_urbs, 0);
for (i = 0; i < MAX_TX_URBS; i++)
priv->tx_contexts[i].echo_index = MAX_TX_URBS;
}
static void kvaser_usb_rx_error(const struct kvaser_usb *dev, static void kvaser_usb_rx_error(const struct kvaser_usb *dev,
const struct kvaser_msg *msg) const struct kvaser_msg *msg)
{ {
...@@ -1181,6 +1176,24 @@ static int kvaser_usb_open(struct net_device *netdev) ...@@ -1181,6 +1176,24 @@ static int kvaser_usb_open(struct net_device *netdev)
return err; return err;
} }
static void kvaser_usb_reset_tx_urb_contexts(struct kvaser_usb_net_priv *priv)
{
int i;
priv->active_tx_contexts = 0;
for (i = 0; i < MAX_TX_URBS; i++)
priv->tx_contexts[i].echo_index = MAX_TX_URBS;
}
/* This method might sleep. Do not call it in the atomic context
* of URB completions.
*/
static void kvaser_usb_unlink_tx_urbs(struct kvaser_usb_net_priv *priv)
{
usb_kill_anchored_urbs(&priv->tx_submitted);
kvaser_usb_reset_tx_urb_contexts(priv);
}
static void kvaser_usb_unlink_all_urbs(struct kvaser_usb *dev) static void kvaser_usb_unlink_all_urbs(struct kvaser_usb *dev)
{ {
int i; int i;
...@@ -1299,6 +1312,7 @@ static netdev_tx_t kvaser_usb_start_xmit(struct sk_buff *skb, ...@@ -1299,6 +1312,7 @@ static netdev_tx_t kvaser_usb_start_xmit(struct sk_buff *skb,
void *buf; void *buf;
struct kvaser_msg *msg; struct kvaser_msg *msg;
int i, err, ret = NETDEV_TX_OK; int i, err, ret = NETDEV_TX_OK;
unsigned long flags;
if (can_dropped_invalid_skb(netdev, skb)) if (can_dropped_invalid_skb(netdev, skb))
return NETDEV_TX_OK; return NETDEV_TX_OK;
...@@ -1342,12 +1356,21 @@ static netdev_tx_t kvaser_usb_start_xmit(struct sk_buff *skb, ...@@ -1342,12 +1356,21 @@ static netdev_tx_t kvaser_usb_start_xmit(struct sk_buff *skb,
if (cf->can_id & CAN_RTR_FLAG) if (cf->can_id & CAN_RTR_FLAG)
msg->u.tx_can.flags |= MSG_FLAG_REMOTE_FRAME; msg->u.tx_can.flags |= MSG_FLAG_REMOTE_FRAME;
spin_lock_irqsave(&priv->tx_contexts_lock, flags);
for (i = 0; i < ARRAY_SIZE(priv->tx_contexts); i++) { for (i = 0; i < ARRAY_SIZE(priv->tx_contexts); i++) {
if (priv->tx_contexts[i].echo_index == MAX_TX_URBS) { if (priv->tx_contexts[i].echo_index == MAX_TX_URBS) {
context = &priv->tx_contexts[i]; context = &priv->tx_contexts[i];
context->echo_index = i;
can_put_echo_skb(skb, netdev, context->echo_index);
++priv->active_tx_contexts;
if (priv->active_tx_contexts >= MAX_TX_URBS)
netif_stop_queue(netdev);
break; break;
} }
} }
spin_unlock_irqrestore(&priv->tx_contexts_lock, flags);
/* This should never happen; it implies a flow control bug */ /* This should never happen; it implies a flow control bug */
if (!context) { if (!context) {
...@@ -1359,7 +1382,6 @@ static netdev_tx_t kvaser_usb_start_xmit(struct sk_buff *skb, ...@@ -1359,7 +1382,6 @@ static netdev_tx_t kvaser_usb_start_xmit(struct sk_buff *skb,
} }
context->priv = priv; context->priv = priv;
context->echo_index = i;
context->dlc = cf->can_dlc; context->dlc = cf->can_dlc;
msg->u.tx_can.tid = context->echo_index; msg->u.tx_can.tid = context->echo_index;
...@@ -1371,18 +1393,17 @@ static netdev_tx_t kvaser_usb_start_xmit(struct sk_buff *skb, ...@@ -1371,18 +1393,17 @@ static netdev_tx_t kvaser_usb_start_xmit(struct sk_buff *skb,
kvaser_usb_write_bulk_callback, context); kvaser_usb_write_bulk_callback, context);
usb_anchor_urb(urb, &priv->tx_submitted); usb_anchor_urb(urb, &priv->tx_submitted);
can_put_echo_skb(skb, netdev, context->echo_index);
atomic_inc(&priv->active_tx_urbs);
if (atomic_read(&priv->active_tx_urbs) >= MAX_TX_URBS)
netif_stop_queue(netdev);
err = usb_submit_urb(urb, GFP_ATOMIC); err = usb_submit_urb(urb, GFP_ATOMIC);
if (unlikely(err)) { if (unlikely(err)) {
spin_lock_irqsave(&priv->tx_contexts_lock, flags);
can_free_echo_skb(netdev, context->echo_index); can_free_echo_skb(netdev, context->echo_index);
context->echo_index = MAX_TX_URBS;
--priv->active_tx_contexts;
netif_wake_queue(netdev);
spin_unlock_irqrestore(&priv->tx_contexts_lock, flags);
atomic_dec(&priv->active_tx_urbs);
usb_unanchor_urb(urb); usb_unanchor_urb(urb);
stats->tx_dropped++; stats->tx_dropped++;
...@@ -1509,7 +1530,7 @@ static int kvaser_usb_init_one(struct usb_interface *intf, ...@@ -1509,7 +1530,7 @@ static int kvaser_usb_init_one(struct usb_interface *intf,
struct kvaser_usb *dev = usb_get_intfdata(intf); struct kvaser_usb *dev = usb_get_intfdata(intf);
struct net_device *netdev; struct net_device *netdev;
struct kvaser_usb_net_priv *priv; struct kvaser_usb_net_priv *priv;
int i, err; int err;
err = kvaser_usb_send_simple_msg(dev, CMD_RESET_CHIP, channel); err = kvaser_usb_send_simple_msg(dev, CMD_RESET_CHIP, channel);
if (err) if (err)
...@@ -1523,19 +1544,17 @@ static int kvaser_usb_init_one(struct usb_interface *intf, ...@@ -1523,19 +1544,17 @@ static int kvaser_usb_init_one(struct usb_interface *intf,
priv = netdev_priv(netdev); priv = netdev_priv(netdev);
init_usb_anchor(&priv->tx_submitted);
init_completion(&priv->start_comp); init_completion(&priv->start_comp);
init_completion(&priv->stop_comp); init_completion(&priv->stop_comp);
init_usb_anchor(&priv->tx_submitted);
atomic_set(&priv->active_tx_urbs, 0);
for (i = 0; i < ARRAY_SIZE(priv->tx_contexts); i++)
priv->tx_contexts[i].echo_index = MAX_TX_URBS;
priv->dev = dev; priv->dev = dev;
priv->netdev = netdev; priv->netdev = netdev;
priv->channel = channel; priv->channel = channel;
spin_lock_init(&priv->tx_contexts_lock);
kvaser_usb_reset_tx_urb_contexts(priv);
priv->can.state = CAN_STATE_STOPPED; priv->can.state = CAN_STATE_STOPPED;
priv->can.clock.freq = CAN_USB_CLOCK; priv->can.clock.freq = CAN_USB_CLOCK;
priv->can.bittiming_const = &kvaser_usb_bittiming_const; priv->can.bittiming_const = &kvaser_usb_bittiming_const;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment