perf: Do the comm inheritance per thread in event__process_task
[safe/jmp/linux-2.6] / net / llc / llc_conn.c
index ab9e6d7..ba137a6 100644 (file)
@@ -13,6 +13,7 @@
  */
 
 #include <linux/init.h>
+#include <linux/slab.h>
 #include <net/llc_sap.h>
 #include <net/llc_conn.h>
 #include <net/sock.h>
@@ -58,7 +59,7 @@ int sysctl_llc2_busy_timeout = LLC2_BUSY_TIME * HZ;
 int llc_conn_state_process(struct sock *sk, struct sk_buff *skb)
 {
        int rc;
-       struct llc_sock *llc = llc_sk(sk);
+       struct llc_sock *llc = llc_sk(skb->sk);
        struct llc_conn_state_ev *ev = llc_conn_ev(skb);
 
        /*
@@ -68,18 +69,17 @@ int llc_conn_state_process(struct sock *sk, struct sk_buff *skb)
         */
        skb_get(skb);
        ev->ind_prim = ev->cfm_prim = 0;
-       rc = llc_conn_service(sk, skb); /* sending event to state machine */
+       /*
+        * Send event to state machine
+        */
+       rc = llc_conn_service(skb->sk, skb);
        if (unlikely(rc != 0)) {
-               printk(KERN_ERR "%s: llc_conn_service failed\n", __FUNCTION__);
+               printk(KERN_ERR "%s: llc_conn_service failed\n", __func__);
                goto out_kfree_skb;
        }
 
        if (unlikely(!ev->ind_prim && !ev->cfm_prim)) {
                /* indicate or confirm not required */
-               /* XXX this is not very pretty, perhaps we should store
-                * XXX indicate/confirm-needed state in the llc_conn_state_ev
-                * XXX control block of the SKB instead? -DaveM
-                */
                if (!skb->next)
                        goto out_kfree_skb;
                goto out_skb_put;
@@ -96,22 +96,18 @@ int llc_conn_state_process(struct sock *sk, struct sk_buff *skb)
                         * shouldn't happen
                         */
                        printk(KERN_ERR "%s: sock_queue_rcv_skb failed!\n",
-                              __FUNCTION__);
+                              __func__);
                        kfree_skb(skb);
                }
                break;
-       case LLC_CONN_PRIM: {
-               struct sock *parent = skb->sk;
-
-               skb_orphan(skb);
+       case LLC_CONN_PRIM:
                /*
-                * Set the skb->sk to the new struct sock, so that at accept
-                * type the upper layer can get the newly created struct sock.
+                * Can't be sock_queue_rcv_skb, because we have to leave the
+                * skb->sk pointing to the newly created struct sock in
+                * llc_conn_handler. -acme
                 */
-               skb->sk = sk;
-               skb_queue_tail(&parent->sk_receive_queue, skb);
-               sk->sk_state_change(parent);
-       }
+               skb_queue_tail(&sk->sk_receive_queue, skb);
+               sk->sk_state_change(sk);
                break;
        case LLC_DISC_PRIM:
                sock_hold(sk);
@@ -121,8 +117,8 @@ int llc_conn_state_process(struct sock *sk, struct sk_buff *skb)
                        sk->sk_socket->state  = SS_UNCONNECTED;
                        sk->sk_state          = TCP_CLOSE;
                        if (!sock_flag(sk, SOCK_DEAD)) {
-                               sk->sk_state_change(sk);
                                sock_set_flag(sk, SOCK_DEAD);
+                               sk->sk_state_change(sk);
                        }
                }
                kfree_skb(skb);
@@ -133,13 +129,13 @@ int llc_conn_state_process(struct sock *sk, struct sk_buff *skb)
                 * FIXME:
                 * RESET is not being notified to upper layers for now
                 */
-               printk(KERN_INFO "%s: received a reset ind!\n", __FUNCTION__);
+               printk(KERN_INFO "%s: received a reset ind!\n", __func__);
                kfree_skb(skb);
                break;
        default:
                if (ev->ind_prim) {
                        printk(KERN_INFO "%s: received unknown %d prim!\n",
-                               __FUNCTION__, ev->ind_prim);
+                               __func__, ev->ind_prim);
                        kfree_skb(skb);
                }
                /* No indication */
@@ -180,12 +176,12 @@ int llc_conn_state_process(struct sock *sk, struct sk_buff *skb)
                 * FIXME:
                 * RESET is not being notified to upper layers for now
                 */
-               printk(KERN_INFO "%s: received a reset conf!\n", __FUNCTION__);
+               printk(KERN_INFO "%s: received a reset conf!\n", __func__);
                break;
        default:
                if (ev->cfm_prim) {
                        printk(KERN_INFO "%s: received unknown %d prim!\n",
-                                       __FUNCTION__, ev->cfm_prim);
+                                       __func__, ev->cfm_prim);
                        break;
                }
                goto out_skb_put; /* No confirmation */
@@ -333,8 +329,7 @@ int llc_conn_remove_acked_pdus(struct sock *sk, u8 nr, u16 *how_many_unacked)
 
        for (i = 0; i < pdu_pos && i < q_len; i++) {
                skb = skb_dequeue(&llc->pdu_unack_q);
-               if (skb)
-                       kfree_skb(skb);
+               kfree_skb(skb);
                nbr_acked++;
        }
 out:
@@ -474,8 +469,21 @@ static int llc_exec_conn_trans_actions(struct sock *sk,
        return rc;
 }
 
+static inline bool llc_estab_match(const struct llc_sap *sap,
+                                  const struct llc_addr *daddr,
+                                  const struct llc_addr *laddr,
+                                  const struct sock *sk)
+{
+       struct llc_sock *llc = llc_sk(sk);
+
+       return llc->laddr.lsap == laddr->lsap &&
+               llc->daddr.lsap == daddr->lsap &&
+               llc_mac_match(llc->laddr.mac, laddr->mac) &&
+               llc_mac_match(llc->daddr.mac, daddr->mac);
+}
+
 /**
- *     llc_lookup_established - Finds connection for the remote/local sap/mac
+ *     __llc_lookup_established - Finds connection for the remote/local sap/mac
  *     @sap: SAP
  *     @daddr: address of remote LLC (MAC + SAP)
  *     @laddr: address of local LLC (MAC + SAP)
@@ -483,28 +491,101 @@ static int llc_exec_conn_trans_actions(struct sock *sk,
  *     Search connection list of the SAP and finds connection using the remote
  *     mac, remote sap, local mac, and local sap. Returns pointer for
  *     connection found, %NULL otherwise.
+ *     Caller has to make sure local_bh is disabled.
  */
-struct sock *llc_lookup_established(struct llc_sap *sap, struct llc_addr *daddr,
-                                   struct llc_addr *laddr)
+static struct sock *__llc_lookup_established(struct llc_sap *sap,
+                                            struct llc_addr *daddr,
+                                            struct llc_addr *laddr)
 {
        struct sock *rc;
-       struct hlist_node *node;
+       struct hlist_nulls_node *node;
+       int slot = llc_sk_laddr_hashfn(sap, laddr);
+       struct hlist_nulls_head *laddr_hb = &sap->sk_laddr_hash[slot];
+
+       rcu_read_lock();
+again:
+       sk_nulls_for_each_rcu(rc, node, laddr_hb) {
+               if (llc_estab_match(sap, daddr, laddr, rc)) {
+                       /* Extra checks required by SLAB_DESTROY_BY_RCU */
+                       if (unlikely(!atomic_inc_not_zero(&rc->sk_refcnt)))
+                               goto again;
+                       if (unlikely(llc_sk(rc)->sap != sap ||
+                                    !llc_estab_match(sap, daddr, laddr, rc))) {
+                               sock_put(rc);
+                               continue;
+                       }
+                       goto found;
+               }
+       }
+       rc = NULL;
+       /*
+        * if the nulls value we got at the end of this lookup is
+        * not the expected one, we must restart lookup.
+        * We probably met an item that was moved to another chain.
+        */
+       if (unlikely(get_nulls_value(node) != slot))
+               goto again;
+found:
+       rcu_read_unlock();
+       return rc;
+}
 
-       read_lock_bh(&sap->sk_list.lock);
-       sk_for_each(rc, node, &sap->sk_list.list) {
-               struct llc_sock *llc = llc_sk(rc);
+struct sock *llc_lookup_established(struct llc_sap *sap,
+                                   struct llc_addr *daddr,
+                                   struct llc_addr *laddr)
+{
+       struct sock *sk;
 
-               if (llc->laddr.lsap == laddr->lsap &&
-                   llc->daddr.lsap == daddr->lsap &&
-                   llc_mac_match(llc->laddr.mac, laddr->mac) &&
-                   llc_mac_match(llc->daddr.mac, daddr->mac)) {
-                       sock_hold(rc);
+       local_bh_disable();
+       sk = __llc_lookup_established(sap, daddr, laddr);
+       local_bh_enable();
+       return sk;
+}
+
+static inline bool llc_listener_match(const struct llc_sap *sap,
+                                     const struct llc_addr *laddr,
+                                     const struct sock *sk)
+{
+       struct llc_sock *llc = llc_sk(sk);
+
+       return sk->sk_type == SOCK_STREAM && sk->sk_state == TCP_LISTEN &&
+               llc->laddr.lsap == laddr->lsap &&
+               llc_mac_match(llc->laddr.mac, laddr->mac);
+}
+
+static struct sock *__llc_lookup_listener(struct llc_sap *sap,
+                                         struct llc_addr *laddr)
+{
+       struct sock *rc;
+       struct hlist_nulls_node *node;
+       int slot = llc_sk_laddr_hashfn(sap, laddr);
+       struct hlist_nulls_head *laddr_hb = &sap->sk_laddr_hash[slot];
+
+       rcu_read_lock();
+again:
+       sk_nulls_for_each_rcu(rc, node, laddr_hb) {
+               if (llc_listener_match(sap, laddr, rc)) {
+                       /* Extra checks required by SLAB_DESTROY_BY_RCU */
+                       if (unlikely(!atomic_inc_not_zero(&rc->sk_refcnt)))
+                               goto again;
+                       if (unlikely(llc_sk(rc)->sap != sap ||
+                                    !llc_listener_match(sap, laddr, rc))) {
+                               sock_put(rc);
+                               continue;
+                       }
                        goto found;
                }
        }
        rc = NULL;
+       /*
+        * if the nulls value we got at the end of this lookup is
+        * not the expected one, we must restart lookup.
+        * We probably met an item that was moved to another chain.
+        */
+       if (unlikely(get_nulls_value(node) != slot))
+               goto again;
 found:
-       read_unlock_bh(&sap->sk_list.lock);
+       rcu_read_unlock();
        return rc;
 }
 
@@ -516,31 +597,29 @@ found:
  *     Search connection list of the SAP and finds connection listening on
  *     local mac, and local sap. Returns pointer for parent socket found,
  *     %NULL otherwise.
+ *     Caller has to make sure local_bh is disabled.
  */
 static struct sock *llc_lookup_listener(struct llc_sap *sap,
                                        struct llc_addr *laddr)
 {
-       struct sock *rc;
-       struct hlist_node *node;
+       static struct llc_addr null_addr;
+       struct sock *rc = __llc_lookup_listener(sap, laddr);
 
-       read_lock_bh(&sap->sk_list.lock);
-       sk_for_each(rc, node, &sap->sk_list.list) {
-               struct llc_sock *llc = llc_sk(rc);
+       if (!rc)
+               rc = __llc_lookup_listener(sap, &null_addr);
 
-               if (rc->sk_type == SOCK_STREAM && rc->sk_state == TCP_LISTEN &&
-                   llc->laddr.lsap == laddr->lsap &&
-                   (llc_mac_match(llc->laddr.mac, laddr->mac) ||
-                    llc_mac_null(llc->laddr.mac))) {
-                       sock_hold(rc);
-                       goto found;
-               }
-       }
-       rc = NULL;
-found:
-       read_unlock_bh(&sap->sk_list.lock);
        return rc;
 }
 
+static struct sock *__llc_lookup(struct llc_sap *sap,
+                                struct llc_addr *daddr,
+                                struct llc_addr *laddr)
+{
+       struct sock *sk = __llc_lookup_established(sap, daddr, laddr);
+
+       return sk ? : llc_lookup_listener(sap, laddr);
+}
+
 /**
  *     llc_data_accept_state - designates if in this state data can be sent.
  *     @state: state of connection.
@@ -629,15 +708,22 @@ static int llc_find_offset(int state, int ev_type)
  *     @sap: SAP
  *     @sk: socket
  *
- *     This function adds a socket to sk_list of a SAP.
+ *     This function adds a socket to the hash tables of a SAP.
  */
 void llc_sap_add_socket(struct llc_sap *sap, struct sock *sk)
 {
+       struct llc_sock *llc = llc_sk(sk);
+       struct hlist_head *dev_hb = llc_sk_dev_hash(sap, llc->dev->ifindex);
+       struct hlist_nulls_head *laddr_hb = llc_sk_laddr_hash(sap, &llc->laddr);
+
        llc_sap_hold(sap);
-       write_lock_bh(&sap->sk_list.lock);
        llc_sk(sk)->sap = sap;
-       sk_add_node(sk, &sap->sk_list.list);
-       write_unlock_bh(&sap->sk_list.lock);
+
+       spin_lock_bh(&sap->sk_lock);
+       sap->sk_count++;
+       sk_nulls_add_node_rcu(sk, laddr_hb);
+       hlist_add_head(&llc->dev_hash_node, dev_hb);
+       spin_unlock_bh(&sap->sk_lock);
 }
 
 /**
@@ -645,14 +731,18 @@ void llc_sap_add_socket(struct llc_sap *sap, struct sock *sk)
  *     @sap: SAP
  *     @sk: socket
  *
- *     This function removes a connection from sk_list.list of a SAP if
+ *     This function removes a connection from the hash tables of a SAP if
  *     the connection was in this list.
  */
 void llc_sap_remove_socket(struct llc_sap *sap, struct sock *sk)
 {
-       write_lock_bh(&sap->sk_list.lock);
-       sk_del_node_init(sk);
-       write_unlock_bh(&sap->sk_list.lock);
+       struct llc_sock *llc = llc_sk(sk);
+
+       spin_lock_bh(&sap->sk_lock);
+       sk_nulls_del_node_init_rcu(sk);
+       hlist_del(&llc->dev_hash_node);
+       sap->sk_count--;
+       spin_unlock_bh(&sap->sk_lock);
        llc_sap_put(sap);
 }
 
@@ -666,15 +756,34 @@ void llc_sap_remove_socket(struct llc_sap *sap, struct sock *sk)
 static int llc_conn_rcv(struct sock* sk, struct sk_buff *skb)
 {
        struct llc_conn_state_ev *ev = llc_conn_ev(skb);
-       struct llc_sock *llc = llc_sk(sk);
 
-       if (!llc->dev)
-               llc->dev = skb->dev;
        ev->type   = LLC_CONN_EV_TYPE_PDU;
        ev->reason = 0;
        return llc_conn_state_process(sk, skb);
 }
 
+static struct sock *llc_create_incoming_sock(struct sock *sk,
+                                            struct net_device *dev,
+                                            struct llc_addr *saddr,
+                                            struct llc_addr *daddr)
+{
+       struct sock *newsk = llc_sk_alloc(sock_net(sk), sk->sk_family, GFP_ATOMIC,
+                                         sk->sk_prot);
+       struct llc_sock *newllc, *llc = llc_sk(sk);
+
+       if (!newsk)
+               goto out;
+       newllc = llc_sk(newsk);
+       memcpy(&newllc->laddr, daddr, sizeof(newllc->laddr));
+       memcpy(&newllc->daddr, saddr, sizeof(newllc->daddr));
+       newllc->dev = dev;
+       dev_hold(dev);
+       llc_sap_add_socket(llc->sap, newsk);
+       llc_sap_hold(llc->sap);
+out:
+       return newsk;
+}
+
 void llc_conn_handler(struct llc_sap *sap, struct sk_buff *skb)
 {
        struct llc_addr saddr, daddr;
@@ -685,46 +794,53 @@ void llc_conn_handler(struct llc_sap *sap, struct sk_buff *skb)
        llc_pdu_decode_da(skb, daddr.mac);
        llc_pdu_decode_dsap(skb, &daddr.lsap);
 
-       sk = llc_lookup_established(sap, &saddr, &daddr);
-       if (!sk) {
+       sk = __llc_lookup(sap, &saddr, &daddr);
+       if (!sk)
+               goto drop;
+
+       bh_lock_sock(sk);
+       /*
+        * This has to be done here and not at the upper layer ->accept
+        * method because of the way the PROCOM state machine works:
+        * it needs to set several state variables (see, for instance,
+        * llc_adm_actions_2 in net/llc/llc_c_st.c) and send a packet to
+        * the originator of the new connection, and this state has to be
+        * in the newly created struct sock private area. -acme
+        */
+       if (unlikely(sk->sk_state == TCP_LISTEN)) {
+               struct sock *newsk = llc_create_incoming_sock(sk, skb->dev,
+                                                             &saddr, &daddr);
+               if (!newsk)
+                       goto drop_unlock;
+               skb_set_owner_r(skb, newsk);
+       } else {
                /*
-                * Didn't find an active connection; verify if there
-                * is a listening socket for this llc addr
+                * Can't be skb_set_owner_r, this will be done at the
+                * llc_conn_state_process function, later on, when we will use
+                * skb_queue_rcv_skb to send it to upper layers, this is
+                * another trick required to cope with how the PROCOM state
+                * machine works. -acme
                 */
-               struct llc_sock *llc;
-               struct sock *parent = llc_lookup_listener(sap, &daddr);
-
-               if (!parent) {
-                       dprintk("llc_lookup_listener failed!\n");
-                       goto drop;
-               }
-
-               sk = llc_sk_alloc(parent->sk_family, GFP_ATOMIC, parent->sk_prot);
-               if (!sk) {
-                       sock_put(parent);
-                       goto drop;
-               }
-               llc = llc_sk(sk);
-               memcpy(&llc->laddr, &daddr, sizeof(llc->laddr));
-               memcpy(&llc->daddr, &saddr, sizeof(llc->daddr));
-               llc_sap_add_socket(sap, sk);
-               sock_hold(sk);
-               skb_set_owner_r(skb, parent);
-               sock_put(parent);
+               skb->sk = sk;
        }
-       bh_lock_sock(sk);
        if (!sock_owned_by_user(sk))
                llc_conn_rcv(sk, skb);
        else {
-               dprintk("%s: adding to backlog...\n", __FUNCTION__);
+               dprintk("%s: adding to backlog...\n", __func__);
                llc_set_backlog_type(skb, LLC_PACKET);
-               sk_add_backlog(sk, skb);
+               if (sk_add_backlog(sk, skb))
+                       goto drop_unlock;
        }
+out:
        bh_unlock_sock(sk);
        sock_put(sk);
        return;
 drop:
        kfree_skb(skb);
+       return;
+drop_unlock:
+       kfree_skb(skb);
+       goto out;
 }
 
 #undef LLC_REFCNT_DEBUG
@@ -759,7 +875,7 @@ static int llc_backlog_rcv(struct sock *sk, struct sk_buff *skb)
                else
                        goto out_kfree_skb;
        } else {
-               printk(KERN_ERR "%s: invalid skb in backlog\n", __FUNCTION__);
+               printk(KERN_ERR "%s: invalid skb in backlog\n", __func__);
                goto out_kfree_skb;
        }
 out:
@@ -783,30 +899,26 @@ static void llc_sk_init(struct sock* sk)
        llc->inc_cntr = llc->dec_cntr = 2;
        llc->dec_step = llc->connect_step = 1;
 
-       init_timer(&llc->ack_timer.timer);
+       setup_timer(&llc->ack_timer.timer, llc_conn_ack_tmr_cb,
+                       (unsigned long)sk);
        llc->ack_timer.expire         = sysctl_llc2_ack_timeout;
-       llc->ack_timer.timer.data     = (unsigned long)sk;
-       llc->ack_timer.timer.function = llc_conn_ack_tmr_cb;
 
-       init_timer(&llc->pf_cycle_timer.timer);
+       setup_timer(&llc->pf_cycle_timer.timer, llc_conn_pf_cycle_tmr_cb,
+                       (unsigned long)sk);
        llc->pf_cycle_timer.expire         = sysctl_llc2_p_timeout;
-       llc->pf_cycle_timer.timer.data     = (unsigned long)sk;
-       llc->pf_cycle_timer.timer.function = llc_conn_pf_cycle_tmr_cb;
 
-       init_timer(&llc->rej_sent_timer.timer);
+       setup_timer(&llc->rej_sent_timer.timer, llc_conn_rej_tmr_cb,
+                       (unsigned long)sk);
        llc->rej_sent_timer.expire         = sysctl_llc2_rej_timeout;
-       llc->rej_sent_timer.timer.data     = (unsigned long)sk;
-       llc->rej_sent_timer.timer.function = llc_conn_rej_tmr_cb;
 
-       init_timer(&llc->busy_state_timer.timer);
+       setup_timer(&llc->busy_state_timer.timer, llc_conn_busy_tmr_cb,
+                       (unsigned long)sk);
        llc->busy_state_timer.expire         = sysctl_llc2_busy_timeout;
-       llc->busy_state_timer.timer.data     = (unsigned long)sk;
-       llc->busy_state_timer.timer.function = llc_conn_busy_tmr_cb;
 
        llc->n2 = 2;   /* max retransmit */
        llc->k  = 2;   /* tx win size, will adjust dynam */
        llc->rw = 128; /* rx win size (opt and equal to
-                       * tx_win of remote LLC) */
+                       * tx_win of remote LLC) */
        skb_queue_head_init(&llc->pdu_unack_q);
        sk->sk_backlog_rcv = llc_backlog_rcv;
 }
@@ -819,9 +931,9 @@ static void llc_sk_init(struct sock* sk)
  *     Allocates a LLC sock and initializes it. Returns the new LLC sock
  *     or %NULL if there's no memory available for one
  */
-struct sock *llc_sk_alloc(int family, int priority, struct proto *prot)
+struct sock *llc_sk_alloc(struct net *net, int family, gfp_t priority, struct proto *prot)
 {
-       struct sock *sk = sk_alloc(family, priority, prot, 1);
+       struct sock *sk = sk_alloc(net, family, priority, prot);
 
        if (!sk)
                goto out;
@@ -830,7 +942,7 @@ struct sock *llc_sk_alloc(int family, int priority, struct proto *prot)
 #ifdef LLC_REFCNT_DEBUG
        atomic_inc(&llc_sock_nr);
        printk(KERN_DEBUG "LLC socket %p created in %s, now we have %d alive\n", sk,
-               __FUNCTION__, atomic_read(&llc_sock_nr));
+               __func__, atomic_read(&llc_sock_nr));
 #endif
 out:
        return sk;
@@ -850,7 +962,7 @@ void llc_sk_free(struct sock *sk)
        /* Stop all (possibly) running timers */
        llc_conn_ac_stop_all_timers(sk, NULL);
 #ifdef DEBUG_LLC_CONN_ALLOC
-       printk(KERN_INFO "%s: unackq=%d, txq=%d\n", __FUNCTION__,
+       printk(KERN_INFO "%s: unackq=%d, txq=%d\n", __func__,
                skb_queue_len(&llc->pdu_unack_q),
                skb_queue_len(&sk->sk_write_queue));
 #endif
@@ -860,13 +972,13 @@ void llc_sk_free(struct sock *sk)
 #ifdef LLC_REFCNT_DEBUG
        if (atomic_read(&sk->sk_refcnt) != 1) {
                printk(KERN_DEBUG "Destruction of LLC sock %p delayed in %s, cnt=%d\n",
-                       sk, __FUNCTION__, atomic_read(&sk->sk_refcnt));
+                       sk, __func__, atomic_read(&sk->sk_refcnt));
                printk(KERN_DEBUG "%d LLC sockets are still alive\n",
                        atomic_read(&llc_sock_nr));
        } else {
                atomic_dec(&llc_sock_nr);
                printk(KERN_DEBUG "LLC socket %p released in %s, %d are still alive\n", sk,
-                       __FUNCTION__, atomic_read(&llc_sock_nr));
+                       __func__, atomic_read(&llc_sock_nr));
        }
 #endif
        sock_put(sk);