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

IB/usnic: Fix potential deadlock

Acquiring the rtnl lock while holding usdev_lock could result in a
deadlock.

For example:

usnic_ib_query_port()
| mutex_lock(&us_ibdev->usdev_lock)
| ib_get_eth_speed()
| rtnl_lock()

rtnl_lock()
| usnic_ib_netdevice_event()
| mutex_lock(&us_ibdev->usdev_lock)

This commit moves the usdev_lock acquisition after the rtnl lock has been
released.

This is safe to do because usdev_lock is not protecting anything being
accessed in ib_get_eth_speed(). Hence, the correct order of holding locks
(rtnl -> usdev_lock) is not violated.

Signed-off-by: Parvi Kaustubhi <pkaustub@cisco.com>
Signed-off-by: Jason Gunthorpe <jgg@mellanox.com>

authored by

Parvi Kaustubhi and committed by
Jason Gunthorpe
8036e90f 50c582de

+7 -4
+7 -4
drivers/infiniband/hw/usnic/usnic_ib_verbs.c
··· 336 336 337 337 usnic_dbg("\n"); 338 338 339 - mutex_lock(&us_ibdev->usdev_lock); 340 339 if (ib_get_eth_speed(ibdev, port, &props->active_speed, 341 - &props->active_width)) { 342 - mutex_unlock(&us_ibdev->usdev_lock); 340 + &props->active_width)) 343 341 return -EINVAL; 344 - } 345 342 343 + /* 344 + * usdev_lock is acquired after (and not before) ib_get_eth_speed call 345 + * because acquiring rtnl_lock in ib_get_eth_speed, while holding 346 + * usdev_lock could lead to a deadlock. 347 + */ 348 + mutex_lock(&us_ibdev->usdev_lock); 346 349 /* props being zeroed by the caller, avoid zeroing it here */ 347 350 348 351 props->lid = 0;