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

net: enic: Cure the enic api locking trainwreck

enic_dev_wait() has a BUG_ON(in_interrupt()).

Chasing the callers of enic_dev_wait() revealed the gems of enic_reset()
and enic_tx_hang_reset() which are both invoked through work queues in
order to be able to call rtnl_lock(). So far so good.

After locking rtnl both functions acquire enic::enic_api_lock which
serializes against the (ab)use from infiniband. This is where the
trainwreck starts.

enic::enic_api_lock is a spin_lock() which implicitly disables preemption,
but both functions invoke a ton of functions under that lock which can
sleep. The BUG_ON(in_interrupt()) does not trigger in that case because it
can't detect the preempt disabled condition.

This clearly has never been tested with any of the mandatory debug options
for 7+ years, which would have caught that for sure.

Cure it by adding a enic_api_busy member to struct enic, which is modified
and evaluated with enic::enic_api_lock held.

If enic_api_devcmd_proxy_by_index() observes enic::enic_api_busy as true,
it drops enic::enic_api_lock and busy waits for enic::enic_api_busy to
become false.

It would be smarter to wait for a completion of that busy period, but
enic_api_devcmd_proxy_by_index() is called with other spin locks held which
obviously can't sleep.

Remove the BUG_ON(in_interrupt()) check as well because it's incomplete and
with proper debugging enabled the problem would have been caught from the
debug checks in schedule_timeout().

Fixes: 0b038566c0ea ("drivers/net: enic: Add an interface for USNIC to interact with firmware")
Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
Signed-off-by: David S. Miller <davem@davemloft.net>

authored by

Thomas Gleixner and committed by
David S. Miller
a53b59ec 2ec13cbc

+28 -6
+1
drivers/net/ethernet/cisco/enic/enic.h
··· 169 169 u16 num_vfs; 170 170 #endif 171 171 spinlock_t enic_api_lock; 172 + bool enic_api_busy; 172 173 struct enic_port_profile *pp; 173 174 174 175 /* work queue cache line section */
+6
drivers/net/ethernet/cisco/enic/enic_api.c
··· 34 34 struct vnic_dev *vdev = enic->vdev; 35 35 36 36 spin_lock(&enic->enic_api_lock); 37 + while (enic->enic_api_busy) { 38 + spin_unlock(&enic->enic_api_lock); 39 + cpu_relax(); 40 + spin_lock(&enic->enic_api_lock); 41 + } 42 + 37 43 spin_lock_bh(&enic->devcmd_lock); 38 44 39 45 vnic_dev_cmd_proxy_by_index_start(vdev, vf);
+21 -6
drivers/net/ethernet/cisco/enic/enic_main.c
··· 2107 2107 int done; 2108 2108 int err; 2109 2109 2110 - BUG_ON(in_interrupt()); 2111 - 2112 2110 err = start(vdev, arg); 2113 2111 if (err) 2114 2112 return err; ··· 2295 2297 rss_hash_bits, rss_base_cpu, rss_enable); 2296 2298 } 2297 2299 2300 + static void enic_set_api_busy(struct enic *enic, bool busy) 2301 + { 2302 + spin_lock(&enic->enic_api_lock); 2303 + enic->enic_api_busy = busy; 2304 + spin_unlock(&enic->enic_api_lock); 2305 + } 2306 + 2298 2307 static void enic_reset(struct work_struct *work) 2299 2308 { 2300 2309 struct enic *enic = container_of(work, struct enic, reset); ··· 2311 2306 2312 2307 rtnl_lock(); 2313 2308 2314 - spin_lock(&enic->enic_api_lock); 2309 + /* Stop any activity from infiniband */ 2310 + enic_set_api_busy(enic, true); 2311 + 2315 2312 enic_stop(enic->netdev); 2316 2313 enic_dev_soft_reset(enic); 2317 2314 enic_reset_addr_lists(enic); ··· 2321 2314 enic_set_rss_nic_cfg(enic); 2322 2315 enic_dev_set_ig_vlan_rewrite_mode(enic); 2323 2316 enic_open(enic->netdev); 2324 - spin_unlock(&enic->enic_api_lock); 2317 + 2318 + /* Allow infiniband to fiddle with the device again */ 2319 + enic_set_api_busy(enic, false); 2320 + 2325 2321 call_netdevice_notifiers(NETDEV_REBOOT, enic->netdev); 2326 2322 2327 2323 rtnl_unlock(); ··· 2336 2326 2337 2327 rtnl_lock(); 2338 2328 2339 - spin_lock(&enic->enic_api_lock); 2329 + /* Stop any activity from infiniband */ 2330 + enic_set_api_busy(enic, true); 2331 + 2340 2332 enic_dev_hang_notify(enic); 2341 2333 enic_stop(enic->netdev); 2342 2334 enic_dev_hang_reset(enic); ··· 2347 2335 enic_set_rss_nic_cfg(enic); 2348 2336 enic_dev_set_ig_vlan_rewrite_mode(enic); 2349 2337 enic_open(enic->netdev); 2350 - spin_unlock(&enic->enic_api_lock); 2338 + 2339 + /* Allow infiniband to fiddle with the device again */ 2340 + enic_set_api_busy(enic, false); 2341 + 2351 2342 call_netdevice_notifiers(NETDEV_REBOOT, enic->netdev); 2352 2343 2353 2344 rtnl_unlock();