Commit 20fd4d1f authored by Pravin B Shelar's avatar Pravin B Shelar Committed by David S. Miller

gre: Simplify gre protocol registration locking.

Use cmpxchg() for atomic protocol registration which saves
code and data space.
Signed-off-by: default avatarPravin B Shelar <pshelar@nicira.com>
Signed-off-by: default avatarDavid S. Miller <davem@davemloft.net>
parent ac8025a6
...@@ -26,46 +26,32 @@ ...@@ -26,46 +26,32 @@
static const struct gre_protocol __rcu *gre_proto[GREPROTO_MAX] __read_mostly; static const struct gre_protocol __rcu *gre_proto[GREPROTO_MAX] __read_mostly;
static DEFINE_SPINLOCK(gre_proto_lock);
int gre_add_protocol(const struct gre_protocol *proto, u8 version) int gre_add_protocol(const struct gre_protocol *proto, u8 version)
{ {
if (version >= GREPROTO_MAX) if (version >= GREPROTO_MAX)
goto err_out; return -EINVAL;
spin_lock(&gre_proto_lock);
if (gre_proto[version])
goto err_out_unlock;
RCU_INIT_POINTER(gre_proto[version], proto);
spin_unlock(&gre_proto_lock);
return 0;
err_out_unlock: return (cmpxchg((const struct gre_protocol **)&gre_proto[version], NULL, proto) == NULL) ?
spin_unlock(&gre_proto_lock); 0 : -EBUSY;
err_out:
return -1;
} }
EXPORT_SYMBOL_GPL(gre_add_protocol); EXPORT_SYMBOL_GPL(gre_add_protocol);
int gre_del_protocol(const struct gre_protocol *proto, u8 version) int gre_del_protocol(const struct gre_protocol *proto, u8 version)
{ {
int ret;
if (version >= GREPROTO_MAX) if (version >= GREPROTO_MAX)
goto err_out; return -EINVAL;
spin_lock(&gre_proto_lock); ret = (cmpxchg((const struct gre_protocol **)&gre_proto[version], proto, NULL) == proto) ?
if (rcu_dereference_protected(gre_proto[version], 0 : -EBUSY;
lockdep_is_held(&gre_proto_lock)) != proto)
goto err_out_unlock; if (ret)
RCU_INIT_POINTER(gre_proto[version], NULL); return ret;
spin_unlock(&gre_proto_lock);
synchronize_rcu(); synchronize_rcu();
return 0; return 0;
err_out_unlock:
spin_unlock(&gre_proto_lock);
err_out:
return -1;
} }
EXPORT_SYMBOL_GPL(gre_del_protocol); EXPORT_SYMBOL_GPL(gre_del_protocol);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment