include cleanup: Update gfp.h and slab.h includes to prepare for breaking implicit...
[safe/jmp/linux-2.6] / net / llc / llc_conn.c
index 042b24a..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>
@@ -73,16 +74,12 @@ int llc_conn_state_process(struct sock *sk, struct sk_buff *skb)
         */
        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;
@@ -99,7 +96,7 @@ 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;
@@ -132,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 */
@@ -179,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 */
@@ -332,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:
@@ -473,6 +469,19 @@ 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
  *     @sap: SAP
@@ -489,23 +498,35 @@ static struct sock *__llc_lookup_established(struct llc_sap *sap,
                                             struct llc_addr *laddr)
 {
        struct sock *rc;
-       struct hlist_node *node;
-
-       read_lock(&sap->sk_list.lock);
-       sk_for_each(rc, node, &sap->sk_list.list) {
-               struct llc_sock *llc = llc_sk(rc);
-
-               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);
+       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:
-       read_unlock(&sap->sk_list.lock);
+       rcu_read_unlock();
        return rc;
 }
 
@@ -521,6 +542,53 @@ struct sock *llc_lookup_established(struct llc_sap *sap,
        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:
+       rcu_read_unlock();
+       return rc;
+}
+
 /**
  *     llc_lookup_listener - Finds listener for local MAC + SAP
  *     @sap: SAP
@@ -534,24 +602,12 @@ struct sock *llc_lookup_established(struct llc_sap *sap,
 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(&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(&sap->sk_list.lock);
        return rc;
 }
 
@@ -652,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);
 }
 
 /**
@@ -668,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);
 }
 
@@ -700,7 +767,7 @@ static struct sock *llc_create_incoming_sock(struct sock *sk,
                                             struct llc_addr *saddr,
                                             struct llc_addr *daddr)
 {
-       struct sock *newsk = llc_sk_alloc(sk->sk_family, GFP_ATOMIC,
+       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);
 
@@ -759,9 +826,10 @@ void llc_conn_handler(struct llc_sap *sap, struct sk_buff *skb)
        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);
@@ -807,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:
@@ -831,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;
 }
@@ -867,10 +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, unsigned int __nocast 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;
@@ -879,7 +942,7 @@ struct sock *llc_sk_alloc(int family, unsigned int __nocast priority,
 #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;
@@ -899,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
@@ -909,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);