diff --git a/net/can/isotp.c b/net/can/isotp.c
index 7a90ba3f1cf1..8229b6a69ecd 100644
--- a/net/can/isotp.c
+++ b/net/can/isotp.c
@@ -182,6 +182,7 @@ struct isotp_sock {
struct list_head notifier;
wait_queue_head_t wait;
spinlock_t rx_lock; /* protect single thread state machine */
+ struct mutex conf_lock; /* protects bind and setsockopt */
};
static LIST_HEAD(isotp_notifier_list);@@ -1497,7 +1498,7 @@ static int isotp_release(struct socket *sock)
list_del(&so->notifier);
spin_unlock(&isotp_notifier_lock);
- lock_sock(sk);
+ mutex_lock(&so->conf_lock);
/* remove current filters & unregister
* the tracked reference so->dev keeps the device's receive lists
@@ -1519,6 +1520,8 @@ static int isotp_release(struct socket *sock)
WRITE_ONCE(so->bound, 0);
so->dev = NULL;
+ mutex_unlock(&so->conf_lock);
+
/* Always wait for a grace period before touching the timers below.
* A concurrent NETDEV_UNREGISTER may have already unregistered our
* filters and cleared so->bound in isotp_notify() without waiting
@@ -1535,7 +1538,6 @@ static int isotp_release(struct socket *sock)
sock_orphan(sk);
sock->sk = NULL;
- release_sock(sk);
sock_prot_inuse_add(net, sk->sk_prot, -1);
sock_put(sk);
@@ -1585,8 +1587,8 @@ static int isotp_bind(struct socket *sock, struct sockaddr_unsized *uaddr, int l
if (!addr->can_ifindex)
return -ENODEV;
- /* lock_sock() serializes the binding with isotp_notify() */
- lock_sock(sk);
+ /* so->conf_lock serializes the binding with isotp_notify() */
+ mutex_lock(&so->conf_lock);
if (so->bound) {
err = -EINVAL;@@ -1596,7 +1598,7 @@ static int isotp_bind(struct socket *sock, struct sockaddr_unsized *uaddr, int l
/* A transmission or reception that outlived a previous binding
* (unbound by NETDEV_UNREGISTER) may still be draining; the FC/echo
* and RX watchdog timers bound how long this takes. Checked together
- * with so->bound in the same lock_sock() section above, so there is
+ * with so->bound in the same so->conf_lock section above, so there is
* no window in which a concurrent isotp_notify() could be missed.
*/
if (READ_ONCE(so->tx.state) != ISOTP_IDLE ||
@@ -1686,7 +1688,7 @@ static int isotp_bind(struct socket *sock, struct sockaddr_unsized *uaddr, int l
sk_error_report(sk);
}
- release_sock(sk);
+ mutex_unlock(&so->conf_lock);
return err;
}
@@ -1790,6 +1792,7 @@ static int isotp_setsockopt(struct socket *sock, int level, int optname,
{
struct sock *sk = sock->sk;
+ struct isotp_sock *so = isotp_sk(sk);
union {
struct can_isotp_options opt;
struct can_isotp_fc_options rxfc;@@ -1802,7 +1805,7 @@ static int isotp_setsockopt(struct socket *sock, int level, int optname,
if (level != SOL_CAN_ISOTP)
return -EINVAL;
- /* copy the option values from user space before lock_sock() */
+ /* copy the option values from user space before taking so->conf_lock */
switch (optname) {
case CAN_ISOTP_OPTS:
size = sizeof(val.opt);@@ -1831,9 +1834,9 @@ static int isotp_setsockopt(struct socket *sock, int level, int optname,
if (copy_from_sockptr(&val, optval, size))
return -EFAULT;
- lock_sock(sk);
+ mutex_lock(&so->conf_lock);
ret = isotp_setsockopt_locked(sock, optname, &val);
- release_sock(sk);
+ mutex_unlock(&so->conf_lock);
return ret;
}
@@ -1906,9 +1909,9 @@ static void isotp_notify(struct isotp_sock *so, unsigned long msg,
switch (msg) {
case NETDEV_UNREGISTER:
- lock_sock(sk);
+ mutex_lock(&so->conf_lock);
if (so->dev != dev) {
- release_sock(sk);
+ mutex_unlock(&so->conf_lock);
break;
}
@@ -1932,7 +1935,7 @@ static void isotp_notify(struct isotp_sock *so, unsigned long msg,
sk->sk_err = ENODEV;
if (!sock_flag(sk, SOCK_DEAD))
sk_error_report(sk);
- release_sock(sk);
+ mutex_unlock(&so->conf_lock);
break;
case NETDEV_DOWN:
@@ -1991,6 +1994,7 @@ static int isotp_init(struct sock *sk)
so->ifindex = 0;
so->bound = 0;
so->dev = NULL;
+ mutex_init(&so->conf_lock);
so->opt.flags = CAN_ISOTP_DEFAULT_FLAGS;
so->opt.ext_address = CAN_ISOTP_DEFAULT_EXT_ADDRESS;
--
2.53.0