Closing adds a counted disable_irq reference but never releases it. A transfer can also hold its own reference across deferred work. Draining that work can abandon the transfer's reference, so merely adding one enable_irq to close is insufficient. Track one driver-owned disable reference and prevent transfer completion from enabling it while close drains the work. Reject IRQ publication once close begins, restore the port's interrupt and data state while ownership is still held, release the port, then drop the driver's IRQ reference. The polling mode skips the invalid IRQ sentinel in the shared helper. Found by the author's in-house static analysis tool. Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2") Cc: stable@vger.kernel.org Link: https://lore.kernel.org/all/179086213620.434549.7621820175200159527@kernel.org/ Signed-off-by: Hongyan Xu Signed-off-by: Zehao Gao --- Discovery: found by the author's in-house static analysis tool. drivers/net/plip/plip.c | 165 +++++++++++++++++++++++++++++++--------- 1 file changed, 131 insertions(+), 34 deletions(-) diff --git a/drivers/net/plip/plip.c b/drivers/net/plip/plip.c index dc3d258..f27c6a0 100644 --- a/drivers/net/plip/plip.c +++ b/drivers/net/plip/plip.c @@ -122,8 +122,6 @@ static const char version[] = "NET3 PLIP version 2.4-parport gniibe@mri.co.jp\n" #endif static const unsigned int net_debug = NET_DEBUG; -#define ENABLE(irq) if (irq != -1) enable_irq(irq) -#define DISABLE(irq) if (irq != -1) disable_irq(irq) /* In micro second */ #define PLIP_DELAY_UNIT 1 @@ -217,11 +215,58 @@ struct net_local { int is_deferred; int port_owner; int should_relinquish; + bool closing; + bool irq_disabled; spinlock_t lock; atomic_t kill_timer; struct completion killed_timer_cmp; }; +/* + * Take one IRQ-disable reference and wait for handlers to finish. Must be + * called from process context with nl->lock not held: synchronize_irq() may + * sleep. + */ +static void plip_disable_irq(struct net_device *dev) +{ + struct net_local *nl = netdev_priv(dev); + unsigned long flags; + + if (dev->irq == -1) + return; + + spin_lock_irqsave(&nl->lock, flags); + if (!nl->irq_disabled) { + nl->irq_disabled = true; + disable_irq_nosync(dev->irq); + } + spin_unlock_irqrestore(&nl->lock, flags); + synchronize_irq(dev->irq); +} + +/* + * Drop the driver's IRQ reference unless close is in progress. Takes + * nl->lock internally and must not be called with it held. + */ +static void plip_enable_irq(struct net_device *dev) +{ + struct net_local *nl = netdev_priv(dev); + unsigned long flags; + bool enable = false; + + if (dev->irq == -1) + return; + + spin_lock_irqsave(&nl->lock, flags); + if (nl->irq_disabled && !nl->closing) { + nl->irq_disabled = false; + enable = true; + } + spin_unlock_irqrestore(&nl->lock, flags); + if (enable) + enable_irq(dev->irq); +} + static inline void enable_parport_interrupts (struct net_device *dev) { if (dev->irq != -1) @@ -300,6 +345,7 @@ plip_init_netdev(struct net_device *dev) nl->port_owner = 0; + nl->closing = true; /* Initialize constants */ nl->trigger = PLIP_TRIGGER_WAIT; @@ -405,13 +451,10 @@ plip_bh_timeout_error(struct net_device *dev, struct net_local *nl, { unsigned char c0; /* - * This is tricky. If we got here from the beginning of send (either - * with ERROR or HS_TIMEOUT) we have IRQ enabled. Otherwise it's - * already disabled. With the old variant of {enable,disable}_irq() - * extra disable_irq() was a no-op. Now it became mortal - it's - * unbalanced and thus we'll never re-enable IRQ (until rmmod plip, - * that is). So we have to treat HS_TIMEOUT and ERROR from send - * in a special way. + * plip_disable_irq() takes the send path's single IRQ-disable + * reference and skips the operation when that reference is already + * held, so a retried timeout needs no separate "was it enabled" + * test. */ spin_lock_irq(&nl->lock); @@ -462,8 +505,7 @@ plip_bh_timeout_error(struct net_device *dev, struct net_local *nl, } spin_unlock_irq(&nl->lock); if (error == HS_TIMEOUT) { - DISABLE(dev->irq); - synchronize_irq(dev->irq); + plip_disable_irq(dev); } disable_parport_interrupts (dev); netif_stop_queue (dev); @@ -595,8 +637,7 @@ plip_receive_packet(struct net_device *dev, struct net_local *nl, switch (rcv->state) { case PLIP_PK_TRIGGER: - DISABLE(dev->irq); - /* Don't need to synchronize irq, as we can safely ignore it */ + plip_disable_irq(dev); disable_parport_interrupts (dev); write_data (dev, 0x01); /* send ACK */ if (net_debug > 2) @@ -615,7 +656,7 @@ plip_receive_packet(struct net_device *dev, struct net_local *nl, nl->connection = PLIP_CN_SEND; schedule_delayed_work(&nl->deferred, 1); enable_parport_interrupts (dev); - ENABLE(dev->irq); + plip_enable_irq(dev); return OK; } } else { @@ -693,13 +734,13 @@ plip_receive_packet(struct net_device *dev, struct net_local *nl, spin_unlock_irq(&nl->lock); schedule_work(&nl->immediate); enable_parport_interrupts (dev); - ENABLE(dev->irq); + plip_enable_irq(dev); return OK; } else { nl->connection = PLIP_CN_NONE; spin_unlock_irq(&nl->lock); enable_parport_interrupts (dev); - ENABLE(dev->irq); + plip_enable_irq(dev); return OK; } } @@ -790,8 +831,7 @@ plip_send_packet(struct net_device *dev, struct net_local *nl, c0 = read_status(dev); if (c0 & 0x08) { spin_unlock_irq(&nl->lock); - DISABLE(dev->irq); - synchronize_irq(dev->irq); + plip_disable_irq(dev); if (nl->connection == PLIP_CN_RECEIVE) { /* Interrupted. We don't need to enable irq, @@ -799,7 +839,7 @@ plip_send_packet(struct net_device *dev, struct net_local *nl, /* Yes, we do. New variant of {enable,disable}_irq *counts* them. -- AV */ - ENABLE(dev->irq); + plip_enable_irq(dev); dev->stats.collisions++; return OK; } @@ -868,7 +908,7 @@ plip_send_packet(struct net_device *dev, struct net_local *nl, nl->is_deferred = 1; schedule_delayed_work(&nl->deferred, 1); enable_parport_interrupts (dev); - ENABLE(dev->irq); + plip_enable_irq(dev); return OK; } return OK; @@ -906,7 +946,7 @@ plip_error(struct net_device *dev, struct net_local *nl, nl->should_relinquish = 0; netif_start_queue (dev); enable_parport_interrupts (dev); - ENABLE(dev->irq); + plip_enable_irq(dev); netif_wake_queue (dev); } else { nl->is_deferred = 1; @@ -930,6 +970,8 @@ plip_interrupt(void *dev_id) rcv = &nl->rcv_data; spin_lock_irqsave (&nl->lock, flags); + if (nl->closing) + goto out_unlock; c0 = read_status(dev); if ((c0 & 0xf8) != 0xc0) { @@ -965,6 +1007,7 @@ plip_interrupt(void *dev_id) break; } +out_unlock: spin_unlock_irqrestore(&nl->lock, flags); } @@ -1072,6 +1115,7 @@ plip_open(struct net_device *dev) struct in_device *in_dev; /* parport_claim() can make the interrupt handler reachable. */ + spin_lock_irq(&nl->lock); nl->rcv_data.state = PLIP_PK_DONE; nl->snd_data.state = PLIP_PK_DONE; nl->rcv_data.skb = NULL; @@ -1080,11 +1124,14 @@ plip_open(struct net_device *dev) nl->is_deferred = 0; nl->should_relinquish = 0; enable_work(&nl->immediate); - enable_work(&nl->deferred.work); + enable_delayed_work(&nl->deferred); + nl->closing = false; + spin_unlock_irq(&nl->lock); /* Grab the port */ if (!nl->port_owner) { if (parport_claim(nl->pardev)) { + nl->closing = true; disable_work_sync(&nl->immediate); disable_delayed_work_sync(&nl->deferred); return -EAGAIN; @@ -1138,10 +1185,13 @@ plip_close(struct net_device *dev) struct net_local *nl = netdev_priv(dev); struct plip_local *snd = &nl->snd_data; struct plip_local *rcv = &nl->rcv_data; + bool owned; - netif_stop_queue (dev); - DISABLE(dev->irq); - synchronize_irq(dev->irq); + netif_stop_queue(dev); + spin_lock_irq(&nl->lock); + nl->closing = true; + spin_unlock_irq(&nl->lock); + plip_disable_irq(dev); if (dev->irq == -1) { @@ -1153,14 +1203,23 @@ plip_close(struct net_device *dev) disable_work_sync(&nl->immediate); disable_delayed_work_sync(&nl->deferred); -#ifdef NOTDEF - outb(0x00, PAR_DATA(dev)); -#endif + /* + * closing is still set, so preempt() refuses to take the port while + * the IRQ source is disabled and the data register is restored. + */ + spin_lock_irq(&nl->lock); + owned = nl->port_owner; nl->is_deferred = 0; nl->connection = PLIP_CN_NONE; - if (nl->port_owner) { - parport_release(nl->pardev); + spin_unlock_irq(&nl->lock); + + if (owned) { + disable_parport_interrupts(dev); + write_data(dev, 0x00); + spin_lock_irq(&nl->lock); nl->port_owner = 0; + spin_unlock_irq(&nl->lock); + parport_release(nl->pardev); } snd->state = PLIP_PK_DONE; @@ -1174,10 +1233,16 @@ plip_close(struct net_device *dev) rcv->skb = NULL; } -#ifdef NOTDEF - /* Reset. */ - outb(0x00, PAR_CONTROL(dev)); -#endif + if (nl->irq_disabled) { + nl->irq_disabled = false; + enable_irq(dev->irq); + } + + /* The port has been released; allow other clients to claim it. */ + spin_lock_irq(&nl->lock); + nl->closing = false; + spin_unlock_irq(&nl->lock); + return 0; } @@ -1186,14 +1251,25 @@ plip_preempt(void *handle) { struct net_device *dev = (struct net_device *)handle; struct net_local *nl = netdev_priv(dev); + unsigned long flags; + + spin_lock_irqsave(&nl->lock, flags); /* Stand our ground if a datagram is on the wire */ if (nl->connection != PLIP_CN_NONE) { nl->should_relinquish = 1; + spin_unlock_irqrestore(&nl->lock, flags); + return 1; + } + + /* Let close restore the port state before the port changes hands. */ + if (nl->closing) { + spin_unlock_irqrestore(&nl->lock, flags); return 1; } nl->port_owner = 0; /* Remember that we released the bus */ + spin_unlock_irqrestore(&nl->lock, flags); return 0; } @@ -1202,6 +1278,7 @@ plip_wakeup(void *handle) { struct net_device *dev = (struct net_device *)handle; struct net_local *nl = netdev_priv(dev); + unsigned long flags; if (nl->port_owner) { /* Why are we being woken up? */ @@ -1217,8 +1294,28 @@ plip_wakeup(void *handle) /* Don't need the port when the interface is down */ return; + /* close() is releasing the port; do not take it back underneath it. */ + spin_lock_irqsave(&nl->lock, flags); + if (nl->closing) { + spin_unlock_irqrestore(&nl->lock, flags); + return; + } + spin_unlock_irqrestore(&nl->lock, flags); + if (!parport_claim(nl->pardev)) { + /* + * close() may have started while we were claiming the port. + * Recheck under the lock that guards its ownership snapshot + * and hand the port straight back if so. + */ + spin_lock_irqsave(&nl->lock, flags); + if (nl->closing) { + spin_unlock_irqrestore(&nl->lock, flags); + parport_release(nl->pardev); + return; + } nl->port_owner = 1; + spin_unlock_irqrestore(&nl->lock, flags); /* Clear the data port. */ write_data (dev, 0x00); } -- 2.50.1.windows.1