Merge remote-tracking branch 'torvalds/master' into perf/core
[linux-2.6-microblaze.git] / net / qrtr / qrtr.c
index edb6ac1..f2efaa4 100644 (file)
@@ -20,6 +20,8 @@
 /* auto-bind range */
 #define QRTR_MIN_EPH_SOCKET 0x4000
 #define QRTR_MAX_EPH_SOCKET 0x7fff
+#define QRTR_EPH_PORT_RANGE \
+               XA_LIMIT(QRTR_MIN_EPH_SOCKET, QRTR_MAX_EPH_SOCKET)
 
 /**
  * struct qrtr_hdr_v1 - (I|R)PCrouter packet header version 1
@@ -106,8 +108,7 @@ static LIST_HEAD(qrtr_all_nodes);
 static DEFINE_MUTEX(qrtr_node_lock);
 
 /* local port allocation management */
-static DEFINE_IDR(qrtr_ports);
-static DEFINE_MUTEX(qrtr_port_lock);
+static DEFINE_XARRAY_ALLOC(qrtr_ports);
 
 /**
  * struct qrtr_node - endpoint node
@@ -271,7 +272,10 @@ static int qrtr_tx_wait(struct qrtr_node *node, int dest_node, int dest_port,
                flow = kzalloc(sizeof(*flow), GFP_KERNEL);
                if (flow) {
                        init_waitqueue_head(&flow->resume_tx);
-                       radix_tree_insert(&node->qrtr_tx_flow, key, flow);
+                       if (radix_tree_insert(&node->qrtr_tx_flow, key, flow)) {
+                               kfree(flow);
+                               flow = NULL;
+                       }
                }
        }
        mutex_unlock(&node->qrtr_tx_lock);
@@ -432,7 +436,7 @@ int qrtr_endpoint_post(struct qrtr_endpoint *ep, const void *data, size_t len)
        struct qrtr_sock *ipc;
        struct sk_buff *skb;
        struct qrtr_cb *cb;
-       unsigned int size;
+       size_t size;
        unsigned int ver;
        size_t hdrlen;
 
@@ -653,7 +657,7 @@ static struct qrtr_sock *qrtr_port_lookup(int port)
                port = 0;
 
        rcu_read_lock();
-       ipc = idr_find(&qrtr_ports, port);
+       ipc = xa_load(&qrtr_ports, port);
        if (ipc)
                sock_hold(&ipc->sk);
        rcu_read_unlock();
@@ -695,9 +699,7 @@ static void qrtr_port_remove(struct qrtr_sock *ipc)
 
        __sock_put(&ipc->sk);
 
-       mutex_lock(&qrtr_port_lock);
-       idr_remove(&qrtr_ports, port);
-       mutex_unlock(&qrtr_port_lock);
+       xa_erase(&qrtr_ports, port);
 
        /* Ensure that if qrtr_port_lookup() did enter the RCU read section we
         * wait for it to up increment the refcount */
@@ -716,29 +718,20 @@ static void qrtr_port_remove(struct qrtr_sock *ipc)
  */
 static int qrtr_port_assign(struct qrtr_sock *ipc, int *port)
 {
-       u32 min_port;
        int rc;
 
-       mutex_lock(&qrtr_port_lock);
        if (!*port) {
-               min_port = QRTR_MIN_EPH_SOCKET;
-               rc = idr_alloc_u32(&qrtr_ports, ipc, &min_port, QRTR_MAX_EPH_SOCKET, GFP_ATOMIC);
-               if (!rc)
-                       *port = min_port;
+               rc = xa_alloc(&qrtr_ports, port, ipc, QRTR_EPH_PORT_RANGE,
+                               GFP_KERNEL);
        } else if (*port < QRTR_MIN_EPH_SOCKET && !capable(CAP_NET_ADMIN)) {
                rc = -EACCES;
        } else if (*port == QRTR_PORT_CTRL) {
-               min_port = 0;
-               rc = idr_alloc_u32(&qrtr_ports, ipc, &min_port, 0, GFP_ATOMIC);
+               rc = xa_insert(&qrtr_ports, 0, ipc, GFP_KERNEL);
        } else {
-               min_port = *port;
-               rc = idr_alloc_u32(&qrtr_ports, ipc, &min_port, *port, GFP_ATOMIC);
-               if (!rc)
-                       *port = min_port;
+               rc = xa_insert(&qrtr_ports, *port, ipc, GFP_KERNEL);
        }
-       mutex_unlock(&qrtr_port_lock);
 
-       if (rc == -ENOSPC)
+       if (rc == -EBUSY)
                return -EADDRINUSE;
        else if (rc < 0)
                return rc;
@@ -752,20 +745,16 @@ static int qrtr_port_assign(struct qrtr_sock *ipc, int *port)
 static void qrtr_reset_ports(void)
 {
        struct qrtr_sock *ipc;
-       int id;
-
-       mutex_lock(&qrtr_port_lock);
-       idr_for_each_entry(&qrtr_ports, ipc, id) {
-               /* Don't reset control port */
-               if (id == 0)
-                       continue;
+       unsigned long index;
 
+       rcu_read_lock();
+       xa_for_each_start(&qrtr_ports, index, ipc, 1) {
                sock_hold(&ipc->sk);
                ipc->sk.sk_err = ENETRESET;
                ipc->sk.sk_error_report(&ipc->sk);
                sock_put(&ipc->sk);
        }
-       mutex_unlock(&qrtr_port_lock);
+       rcu_read_unlock();
 }
 
 /* Bind socket to address.
@@ -1058,6 +1047,11 @@ static int qrtr_recvmsg(struct socket *sock, struct msghdr *msg,
        rc = copied;
 
        if (addr) {
+               /* There is an anonymous 2-byte hole after sq_family,
+                * make sure to clear it.
+                */
+               memset(addr, 0, sizeof(*addr));
+
                addr->sq_family = AF_QIPCRTR;
                addr->sq_node = cb->src_node;
                addr->sq_port = cb->src_port;