summary |
shortlog |
log |
commit | commitdiff |
tree
raw |
patch |
inline | side by side (from parent 1:
51bc05c)
This patch removes spin_locks at CAN socket creation time by using RCU.
Inspired by the discussion with Kurt van Dijck and Eric Dumazet the RCU code
was partly derived from af_phonet.c
Signed-off-by: Oliver Hartkopp <socketcan@hartkopp.net>
Reviewed-by: Eric Dumazet <eric.dumazet@gmail.com>
Acked-by: Kurt Van Dijck <kurt.van.dijck@eia.be>
Signed-off-by: David S. Miller <davem@davemloft.net>
git-svn-id: svn://svn.berlios.de//socketcan/trunk@1238
030b6a49-0b11-0410-94ab-
b0dab22257f2
/* table of registered CAN protocols */
static struct can_proto *proto_tab[CAN_NPROTO] __read_mostly;
/* table of registered CAN protocols */
static struct can_proto *proto_tab[CAN_NPROTO] __read_mostly;
-static DEFINE_SPINLOCK(proto_tab_lock);
+static DEFINE_MUTEX(proto_tab_lock);
struct timer_list can_stattimer; /* timer for statistics update */
struct s_stats can_stats; /* packet statistics */
struct timer_list can_stattimer; /* timer for statistics update */
struct s_stats can_stats; /* packet statistics */
+static struct can_proto *can_try_module_get(int protocol)
+{
+ struct can_proto *cp;
+
+ rcu_read_lock();
+ cp = rcu_dereference(proto_tab[protocol]);
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,12)
+ if (cp && !try_module_get(cp->prot->owner))
+ cp = NULL;
+#else
+ if (cp && !try_module_get(cp->owner))
+ cp = NULL;
+#endif
+ rcu_read_unlock();
+
+ return cp;
+}
+
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,33)
static int can_create(struct net *net, struct socket *sock, int protocol, int kern)
#elif LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24)
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,33)
static int can_create(struct net *net, struct socket *sock, int protocol, int kern)
#elif LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24)
return -EAFNOSUPPORT;
#endif
return -EAFNOSUPPORT;
#endif
+ cp = can_try_module_get(protocol);
+
- /* try to load protocol module kernel is modular */
- if (!proto_tab[protocol]) {
+ if (!cp) {
+ /* try to load protocol module if kernel is modular */
+
err = request_module("can-proto-%d", protocol);
/*
err = request_module("can-proto-%d", protocol);
/*
if (err && printk_ratelimit())
printk(KERN_ERR "can: request_module "
"(can-proto-%d) failed.\n", protocol);
if (err && printk_ratelimit())
printk(KERN_ERR "can: request_module "
"(can-proto-%d) failed.\n", protocol);
- spin_lock(&proto_tab_lock);
- cp = proto_tab[protocol];
-#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,12)
- if (cp && !try_module_get(cp->prot->owner))
- cp = NULL;
-#else
- if (cp && !try_module_get(cp->owner))
- cp = NULL;
+ cp = can_try_module_get(protocol);
+ }
- spin_unlock(&proto_tab_lock);
/* check for available protocol and correct usage */
/* check for available protocol and correct usage */
return -EPROTONOSUPPORT;
if (cp->type != sock->type) {
return -EPROTONOSUPPORT;
if (cp->type != sock->type) {
- err = -EPROTONOSUPPORT;
- spin_lock(&proto_tab_lock);
+ mutex_lock(&proto_tab_lock);
+
if (proto_tab[proto]) {
printk(KERN_ERR "can: protocol %d already registered\n",
proto);
err = -EBUSY;
} else
if (proto_tab[proto]) {
printk(KERN_ERR "can: protocol %d already registered\n",
proto);
err = -EBUSY;
} else
+ rcu_assign_pointer(proto_tab[proto], cp);
- spin_unlock(&proto_tab_lock);
+ mutex_unlock(&proto_tab_lock);
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,12)
if (err < 0)
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,12)
if (err < 0)
{
int proto = cp->protocol;
{
int proto = cp->protocol;
- spin_lock(&proto_tab_lock);
- if (!proto_tab[proto]) {
- printk(KERN_ERR "BUG: can: protocol %d is not registered\n",
- proto);
- }
- proto_tab[proto] = NULL;
- spin_unlock(&proto_tab_lock);
+ mutex_lock(&proto_tab_lock);
+ BUG_ON(proto_tab[proto] != cp);
+ rcu_assign_pointer(proto_tab[proto], NULL);
+ mutex_unlock(&proto_tab_lock);
+
+ synchronize_rcu();
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,12)
proto_unregister(cp->prot);
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,12)
proto_unregister(cp->prot);