提交 b76f5a84 编写于 作者: O Octavian Purdila 提交者: David S. Miller

llc: convert the socket list to RCU locking

For the reclamation phase we use the SLAB_DESTROY_BY_RCU mechanism,
which require some extra checks in the lookup code:

a) If the current socket was released, reallocated & inserted in
another list it will short circuit the iteration for the current list,
thus we need to restart the lookup.

b) If the current socket was released, reallocated & inserted in the
same list we just need to recheck it matches the look-up criteria and
if not we can skip to the next element.

In this case there is no need to restart the lookup, since sockets are
inserted at the start of the list and the worst that will happen is
that we will iterate throught some of the list elements more then
once.

Note that the /proc and multicast delivery was not yet converted to
RCU, it still uses spinlocks for protection.
Signed-off-by: NOctavian Purdila <opurdila@ixiacom.com>
Signed-off-by: NDavid S. Miller <davem@davemloft.net>
上级 abf9d537
...@@ -16,6 +16,7 @@ ...@@ -16,6 +16,7 @@
#include <linux/if_ether.h> #include <linux/if_ether.h>
#include <linux/list.h> #include <linux/list.h>
#include <linux/spinlock.h> #include <linux/spinlock.h>
#include <linux/rculist_nulls.h>
#include <asm/atomic.h> #include <asm/atomic.h>
...@@ -53,10 +54,8 @@ struct llc_sap { ...@@ -53,10 +54,8 @@ struct llc_sap {
struct net_device *orig_dev); struct net_device *orig_dev);
struct llc_addr laddr; struct llc_addr laddr;
struct list_head node; struct list_head node;
struct { spinlock_t sk_lock;
rwlock_t lock; struct hlist_nulls_head sk_list;
struct hlist_head list;
} sk_list;
}; };
#define LLC_DEST_INVALID 0 /* Invalid LLC PDU type */ #define LLC_DEST_INVALID 0 /* Invalid LLC PDU type */
......
...@@ -140,6 +140,7 @@ static struct proto llc_proto = { ...@@ -140,6 +140,7 @@ static struct proto llc_proto = {
.name = "LLC", .name = "LLC",
.owner = THIS_MODULE, .owner = THIS_MODULE,
.obj_size = sizeof(struct llc_sock), .obj_size = sizeof(struct llc_sock),
.slab_flags = SLAB_DESTROY_BY_RCU,
}; };
/** /**
......
...@@ -468,6 +468,19 @@ static int llc_exec_conn_trans_actions(struct sock *sk, ...@@ -468,6 +468,19 @@ static int llc_exec_conn_trans_actions(struct sock *sk,
return rc; 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 * @sap: SAP
...@@ -484,23 +497,26 @@ static struct sock *__llc_lookup_established(struct llc_sap *sap, ...@@ -484,23 +497,26 @@ static struct sock *__llc_lookup_established(struct llc_sap *sap,
struct llc_addr *laddr) struct llc_addr *laddr)
{ {
struct sock *rc; struct sock *rc;
struct hlist_node *node; struct hlist_nulls_node *node;
read_lock(&sap->sk_list.lock); rcu_read_lock();
sk_for_each(rc, node, &sap->sk_list.list) { again:
struct llc_sock *llc = llc_sk(rc); sk_nulls_for_each_rcu(rc, node, &sap->sk_list) {
if (llc_estab_match(sap, daddr, laddr, rc)) {
if (llc->laddr.lsap == laddr->lsap && /* Extra checks required by SLAB_DESTROY_BY_RCU */
llc->daddr.lsap == daddr->lsap && if (unlikely(!atomic_inc_not_zero(&rc->sk_refcnt)))
llc_mac_match(llc->laddr.mac, laddr->mac) && goto again;
llc_mac_match(llc->daddr.mac, daddr->mac)) { if (unlikely(llc_sk(rc)->sap != sap ||
sock_hold(rc); !llc_estab_match(sap, daddr, laddr, rc))) {
sock_put(rc);
continue;
}
goto found; goto found;
} }
} }
rc = NULL; rc = NULL;
found: found:
read_unlock(&sap->sk_list.lock); rcu_read_unlock();
return rc; return rc;
} }
...@@ -516,6 +532,18 @@ struct sock *llc_lookup_established(struct llc_sap *sap, ...@@ -516,6 +532,18 @@ struct sock *llc_lookup_established(struct llc_sap *sap,
return sk; 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) ||
llc_mac_null(llc->laddr.mac));
}
/** /**
* llc_lookup_listener - Finds listener for local MAC + SAP * llc_lookup_listener - Finds listener for local MAC + SAP
* @sap: SAP * @sap: SAP
...@@ -530,23 +558,26 @@ static struct sock *llc_lookup_listener(struct llc_sap *sap, ...@@ -530,23 +558,26 @@ static struct sock *llc_lookup_listener(struct llc_sap *sap,
struct llc_addr *laddr) struct llc_addr *laddr)
{ {
struct sock *rc; struct sock *rc;
struct hlist_node *node; struct hlist_nulls_node *node;
read_lock(&sap->sk_list.lock); rcu_read_lock();
sk_for_each(rc, node, &sap->sk_list.list) { again:
struct llc_sock *llc = llc_sk(rc); sk_nulls_for_each_rcu(rc, node, &sap->sk_list) {
if (llc_listener_match(sap, laddr, rc)) {
if (rc->sk_type == SOCK_STREAM && rc->sk_state == TCP_LISTEN && /* Extra checks required by SLAB_DESTROY_BY_RCU */
llc->laddr.lsap == laddr->lsap && if (unlikely(!atomic_inc_not_zero(&rc->sk_refcnt)))
(llc_mac_match(llc->laddr.mac, laddr->mac) || goto again;
llc_mac_null(llc->laddr.mac))) { if (unlikely(llc_sk(rc)->sap != sap ||
sock_hold(rc); !llc_listener_match(sap, laddr, rc))) {
sock_put(rc);
continue;
}
goto found; goto found;
} }
} }
rc = NULL; rc = NULL;
found: found:
read_unlock(&sap->sk_list.lock); rcu_read_unlock();
return rc; return rc;
} }
...@@ -652,10 +683,10 @@ static int llc_find_offset(int state, int ev_type) ...@@ -652,10 +683,10 @@ static int llc_find_offset(int state, int ev_type)
void llc_sap_add_socket(struct llc_sap *sap, struct sock *sk) void llc_sap_add_socket(struct llc_sap *sap, struct sock *sk)
{ {
llc_sap_hold(sap); llc_sap_hold(sap);
write_lock_bh(&sap->sk_list.lock); spin_lock_bh(&sap->sk_lock);
llc_sk(sk)->sap = sap; llc_sk(sk)->sap = sap;
sk_add_node(sk, &sap->sk_list.list); sk_nulls_add_node_rcu(sk, &sap->sk_list);
write_unlock_bh(&sap->sk_list.lock); spin_unlock_bh(&sap->sk_lock);
} }
/** /**
...@@ -663,14 +694,14 @@ void llc_sap_add_socket(struct llc_sap *sap, struct sock *sk) ...@@ -663,14 +694,14 @@ void llc_sap_add_socket(struct llc_sap *sap, struct sock *sk)
* @sap: SAP * @sap: SAP
* @sk: socket * @sk: socket
* *
* This function removes a connection from sk_list.list of a SAP if * This function removes a connection from sk_list of a SAP if
* the connection was in this list. * the connection was in this list.
*/ */
void llc_sap_remove_socket(struct llc_sap *sap, struct sock *sk) void llc_sap_remove_socket(struct llc_sap *sap, struct sock *sk)
{ {
write_lock_bh(&sap->sk_list.lock); spin_lock_bh(&sap->sk_lock);
sk_del_node_init(sk); sk_nulls_del_node_init_rcu(sk);
write_unlock_bh(&sap->sk_list.lock); spin_unlock_bh(&sap->sk_lock);
llc_sap_put(sap); llc_sap_put(sap);
} }
......
...@@ -37,7 +37,8 @@ static struct llc_sap *llc_sap_alloc(void) ...@@ -37,7 +37,8 @@ static struct llc_sap *llc_sap_alloc(void)
if (sap) { if (sap) {
/* sap->laddr.mac - leave as a null, it's filled by bind */ /* sap->laddr.mac - leave as a null, it's filled by bind */
sap->state = LLC_SAP_STATE_ACTIVE; sap->state = LLC_SAP_STATE_ACTIVE;
rwlock_init(&sap->sk_list.lock); spin_lock_init(&sap->sk_lock);
INIT_HLIST_NULLS_HEAD(&sap->sk_list, 0);
atomic_set(&sap->refcnt, 1); atomic_set(&sap->refcnt, 1);
} }
return sap; return sap;
...@@ -142,7 +143,7 @@ struct llc_sap *llc_sap_open(unsigned char lsap, ...@@ -142,7 +143,7 @@ struct llc_sap *llc_sap_open(unsigned char lsap,
*/ */
void llc_sap_close(struct llc_sap *sap) void llc_sap_close(struct llc_sap *sap)
{ {
WARN_ON(!hlist_empty(&sap->sk_list.list)); WARN_ON(!hlist_nulls_empty(&sap->sk_list));
llc_del_sap(sap); llc_del_sap(sap);
kfree(sap); kfree(sap);
} }
......
...@@ -34,19 +34,19 @@ static struct sock *llc_get_sk_idx(loff_t pos) ...@@ -34,19 +34,19 @@ static struct sock *llc_get_sk_idx(loff_t pos)
{ {
struct list_head *sap_entry; struct list_head *sap_entry;
struct llc_sap *sap; struct llc_sap *sap;
struct hlist_node *node; struct hlist_nulls_node *node;
struct sock *sk = NULL; struct sock *sk = NULL;
list_for_each(sap_entry, &llc_sap_list) { list_for_each(sap_entry, &llc_sap_list) {
sap = list_entry(sap_entry, struct llc_sap, node); sap = list_entry(sap_entry, struct llc_sap, node);
read_lock_bh(&sap->sk_list.lock); spin_lock_bh(&sap->sk_lock);
sk_for_each(sk, node, &sap->sk_list.list) { sk_nulls_for_each(sk, node, &sap->sk_list) {
if (!pos) if (!pos)
goto found; goto found;
--pos; --pos;
} }
read_unlock_bh(&sap->sk_list.lock); spin_unlock_bh(&sap->sk_lock);
} }
sk = NULL; sk = NULL;
found: found:
...@@ -73,25 +73,25 @@ static void *llc_seq_next(struct seq_file *seq, void *v, loff_t *pos) ...@@ -73,25 +73,25 @@ static void *llc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
goto out; goto out;
} }
sk = v; sk = v;
next = sk_next(sk); next = sk_nulls_next(sk);
if (next) { if (next) {
sk = next; sk = next;
goto out; goto out;
} }
llc = llc_sk(sk); llc = llc_sk(sk);
sap = llc->sap; sap = llc->sap;
read_unlock_bh(&sap->sk_list.lock); spin_unlock_bh(&sap->sk_lock);
sk = NULL; sk = NULL;
for (;;) { for (;;) {
if (sap->node.next == &llc_sap_list) if (sap->node.next == &llc_sap_list)
break; break;
sap = list_entry(sap->node.next, struct llc_sap, node); sap = list_entry(sap->node.next, struct llc_sap, node);
read_lock_bh(&sap->sk_list.lock); spin_lock_bh(&sap->sk_lock);
if (!hlist_empty(&sap->sk_list.list)) { if (!hlist_nulls_empty(&sap->sk_list)) {
sk = sk_head(&sap->sk_list.list); sk = sk_nulls_head(&sap->sk_list);
break; break;
} }
read_unlock_bh(&sap->sk_list.lock); spin_unlock_bh(&sap->sk_lock);
} }
out: out:
return sk; return sk;
...@@ -104,7 +104,7 @@ static void llc_seq_stop(struct seq_file *seq, void *v) ...@@ -104,7 +104,7 @@ static void llc_seq_stop(struct seq_file *seq, void *v)
struct llc_sock *llc = llc_sk(sk); struct llc_sock *llc = llc_sk(sk);
struct llc_sap *sap = llc->sap; struct llc_sap *sap = llc->sap;
read_unlock_bh(&sap->sk_list.lock); spin_unlock_bh(&sap->sk_lock);
} }
read_unlock_bh(&llc_sap_list_lock); read_unlock_bh(&llc_sap_list_lock);
} }
......
...@@ -297,6 +297,17 @@ static void llc_sap_rcv(struct llc_sap *sap, struct sk_buff *skb, ...@@ -297,6 +297,17 @@ static void llc_sap_rcv(struct llc_sap *sap, struct sk_buff *skb,
llc_sap_state_process(sap, skb); llc_sap_state_process(sap, skb);
} }
static inline bool llc_dgram_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_DGRAM &&
llc->laddr.lsap == laddr->lsap &&
llc_mac_match(llc->laddr.mac, laddr->mac);
}
/** /**
* llc_lookup_dgram - Finds dgram socket for the local sap/mac * llc_lookup_dgram - Finds dgram socket for the local sap/mac
* @sap: SAP * @sap: SAP
...@@ -309,25 +320,41 @@ static struct sock *llc_lookup_dgram(struct llc_sap *sap, ...@@ -309,25 +320,41 @@ static struct sock *llc_lookup_dgram(struct llc_sap *sap,
const struct llc_addr *laddr) const struct llc_addr *laddr)
{ {
struct sock *rc; struct sock *rc;
struct hlist_node *node; struct hlist_nulls_node *node;
read_lock_bh(&sap->sk_list.lock); rcu_read_lock_bh();
sk_for_each(rc, node, &sap->sk_list.list) { again:
struct llc_sock *llc = llc_sk(rc); sk_nulls_for_each_rcu(rc, node, &sap->sk_list) {
if (llc_dgram_match(sap, laddr, rc)) {
if (rc->sk_type == SOCK_DGRAM && /* Extra checks required by SLAB_DESTROY_BY_RCU */
llc->laddr.lsap == laddr->lsap && if (unlikely(!atomic_inc_not_zero(&rc->sk_refcnt)))
llc_mac_match(llc->laddr.mac, laddr->mac)) { goto again;
sock_hold(rc); if (unlikely(llc_sk(rc)->sap != sap ||
!llc_dgram_match(sap, laddr, rc))) {
sock_put(rc);
continue;
}
goto found; goto found;
} }
} }
rc = NULL; rc = NULL;
found: found:
read_unlock_bh(&sap->sk_list.lock); rcu_read_unlock_bh();
return rc; return rc;
} }
static inline bool llc_mcast_match(const struct llc_sap *sap,
const struct llc_addr *laddr,
const struct sk_buff *skb,
const struct sock *sk)
{
struct llc_sock *llc = llc_sk(sk);
return sk->sk_type == SOCK_DGRAM &&
llc->laddr.lsap == laddr->lsap &&
llc->dev == skb->dev;
}
/** /**
* llc_sap_mcast - Deliver multicast PDU's to all matching datagram sockets. * llc_sap_mcast - Deliver multicast PDU's to all matching datagram sockets.
* @sap: SAP * @sap: SAP
...@@ -341,20 +368,13 @@ static void llc_sap_mcast(struct llc_sap *sap, ...@@ -341,20 +368,13 @@ static void llc_sap_mcast(struct llc_sap *sap,
struct sk_buff *skb) struct sk_buff *skb)
{ {
struct sock *sk; struct sock *sk;
struct hlist_node *node; struct hlist_nulls_node *node;
read_lock_bh(&sap->sk_list.lock); spin_lock_bh(&sap->sk_lock);
sk_for_each(sk, node, &sap->sk_list.list) { sk_nulls_for_each_rcu(sk, node, &sap->sk_list) {
struct llc_sock *llc = llc_sk(sk);
struct sk_buff *skb1; struct sk_buff *skb1;
if (sk->sk_type != SOCK_DGRAM) if (!llc_mcast_match(sap, laddr, skb, sk))
continue;
if (llc->laddr.lsap != laddr->lsap)
continue;
if (llc->dev != skb->dev)
continue; continue;
skb1 = skb_clone(skb, GFP_ATOMIC); skb1 = skb_clone(skb, GFP_ATOMIC);
...@@ -365,7 +385,7 @@ static void llc_sap_mcast(struct llc_sap *sap, ...@@ -365,7 +385,7 @@ static void llc_sap_mcast(struct llc_sap *sap,
llc_sap_rcv(sap, skb1, sk); llc_sap_rcv(sap, skb1, sk);
sock_put(sk); sock_put(sk);
} }
read_unlock_bh(&sap->sk_list.lock); spin_unlock_bh(&sap->sk_lock);
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册