mctp: make __mctp_dev_get() take a refcount hold
authorMatt Johnston <matt@codeconstruct.com.au>
Tue, 22 Feb 2022 04:17:38 +0000 (12:17 +0800)
committerDavid S. Miller <davem@davemloft.net>
Wed, 23 Feb 2022 12:29:15 +0000 (12:29 +0000)
commitdc121c0084910db985cf1c8ba6fce5d8c307cc02
treed9a2d56d1ded9a273b032beb317d36ea86ffb6f3
parent4767b7e2ed818e97851d236d23f2e2ffa90af6ec
mctp: make __mctp_dev_get() take a refcount hold

Previously there was a race that could allow the mctp_dev refcount
to hit zero:

rcu_read_lock();
mdev = __mctp_dev_get(dev);
// mctp_unregister() happens here, mdev->refs hits zero
mctp_dev_hold(dev);
rcu_read_unlock();

Now we make __mctp_dev_get() take the hold itself. It is safe to test
against the zero refcount because __mctp_dev_get() is called holding
rcu_read_lock and mctp_dev uses kfree_rcu().

Reported-by: Jakub Kicinski <kuba@kernel.org>
Signed-off-by: Matt Johnston <matt@codeconstruct.com.au>
Signed-off-by: David S. Miller <davem@davemloft.net>
net/mctp/device.c
net/mctp/route.c
net/mctp/test/utils.c