Skip to content

Commit

Permalink
net: qrtr: Make qrtr_port_lookup() use RCU
Browse files Browse the repository at this point in the history
The important part of qrtr_port_lookup() wrt synchronization is that the
function returns a reference counted struct qrtr_sock, or fail.

As such we need only to ensure that an decrement of the object's
refcount happens inbetween the finding of the object in the idr and
qrtr_port_lookup()'s own increment of the object.

By using RCU and putting a synchronization point after we remove the
mapping from the idr, but before it can be released we achieve this -
with the benefit of not having to hold the mutex in qrtr_port_lookup().

Signed-off-by: Bjorn Andersson <bjorn.andersson@linaro.org>
Signed-off-by: David S. Miller <davem@davemloft.net>
  • Loading branch information
Bjorn Andersson authored and David S. Miller committed Jan 15, 2020
1 parent 0a7e0d0 commit f16a4b2
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions net/qrtr/qrtr.c
Original file line number Diff line number Diff line change
Expand Up @@ -646,11 +646,11 @@ static struct qrtr_sock *qrtr_port_lookup(int port)
if (port == QRTR_PORT_CTRL)
port = 0;

mutex_lock(&qrtr_port_lock);
rcu_read_lock();
ipc = idr_find(&qrtr_ports, port);
if (ipc)
sock_hold(&ipc->sk);
mutex_unlock(&qrtr_port_lock);
rcu_read_unlock();

return ipc;
}
Expand Down Expand Up @@ -692,6 +692,10 @@ static void qrtr_port_remove(struct qrtr_sock *ipc)
mutex_lock(&qrtr_port_lock);
idr_remove(&qrtr_ports, port);
mutex_unlock(&qrtr_port_lock);

/* Ensure that if qrtr_port_lookup() did enter the RCU read section we
* wait for it to up increment the refcount */
synchronize_rcu();
}

/* Assign port number to socket.
Expand Down

0 comments on commit f16a4b2

Please sign in to comment.