Skip to content

Commit

Permalink
net: inet_add_protocol() can use cmpxchg()
Browse files Browse the repository at this point in the history
Use cmpxchg() to get rid of spinlocks in inet_add_protocol() and
friends.

inet_protos[] & inet6_protos[] are moved to read_mostly section

Signed-off-by: Eric Dumazet <[email protected]>
Signed-off-by: David S. Miller <[email protected]>
  • Loading branch information
Eric Dumazet authored and davem330 committed Sep 9, 2010
1 parent 92e32ea commit e038600
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 54 deletions.
31 changes: 5 additions & 26 deletions net/ipv4/protocol.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,29 +28,17 @@
#include <linux/spinlock.h>
#include <net/protocol.h>

const struct net_protocol *inet_protos[MAX_INET_PROTOS] ____cacheline_aligned_in_smp;
static DEFINE_SPINLOCK(inet_proto_lock);
const struct net_protocol *inet_protos[MAX_INET_PROTOS] __read_mostly;

/*
* Add a protocol handler to the hash tables
*/

int inet_add_protocol(const struct net_protocol *prot, unsigned char protocol)
{
int hash, ret;
int hash = protocol & (MAX_INET_PROTOS - 1);

hash = protocol & (MAX_INET_PROTOS - 1);

spin_lock_bh(&inet_proto_lock);
if (inet_protos[hash]) {
ret = -1;
} else {
inet_protos[hash] = prot;
ret = 0;
}
spin_unlock_bh(&inet_proto_lock);

return ret;
return !cmpxchg(&inet_protos[hash], NULL, prot) ? 0 : -1;
}
EXPORT_SYMBOL(inet_add_protocol);

Expand All @@ -60,18 +48,9 @@ EXPORT_SYMBOL(inet_add_protocol);

int inet_del_protocol(const struct net_protocol *prot, unsigned char protocol)
{
int hash, ret;

hash = protocol & (MAX_INET_PROTOS - 1);
int ret, hash = protocol & (MAX_INET_PROTOS - 1);

spin_lock_bh(&inet_proto_lock);
if (inet_protos[hash] == prot) {
inet_protos[hash] = NULL;
ret = 0;
} else {
ret = -1;
}
spin_unlock_bh(&inet_proto_lock);
ret = (cmpxchg(&inet_protos[hash], prot, NULL) == prot) ? 0 : -1;

synchronize_net();

Expand Down
32 changes: 4 additions & 28 deletions net/ipv6/protocol.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,28 +25,14 @@
#include <linux/spinlock.h>
#include <net/protocol.h>

const struct inet6_protocol *inet6_protos[MAX_INET_PROTOS];
static DEFINE_SPINLOCK(inet6_proto_lock);

const struct inet6_protocol *inet6_protos[MAX_INET_PROTOS] __read_mostly;

int inet6_add_protocol(const struct inet6_protocol *prot, unsigned char protocol)
{
int ret, hash = protocol & (MAX_INET_PROTOS - 1);

spin_lock_bh(&inet6_proto_lock);

if (inet6_protos[hash]) {
ret = -1;
} else {
inet6_protos[hash] = prot;
ret = 0;
}

spin_unlock_bh(&inet6_proto_lock);
int hash = protocol & (MAX_INET_PROTOS - 1);

return ret;
return !cmpxchg(&inet6_protos[hash], NULL, prot) ? 0 : -1;
}

EXPORT_SYMBOL(inet6_add_protocol);

/*
Expand All @@ -57,20 +43,10 @@ int inet6_del_protocol(const struct inet6_protocol *prot, unsigned char protocol
{
int ret, hash = protocol & (MAX_INET_PROTOS - 1);

spin_lock_bh(&inet6_proto_lock);

if (inet6_protos[hash] != prot) {
ret = -1;
} else {
inet6_protos[hash] = NULL;
ret = 0;
}

spin_unlock_bh(&inet6_proto_lock);
ret = (cmpxchg(&inet6_protos[hash], prot, NULL) == prot) ? 0 : -1;

synchronize_net();

return ret;
}

EXPORT_SYMBOL(inet6_del_protocol);

0 comments on commit e038600

Please sign in to comment.