* [PATCH net-next-2.6 v2] can: convert protocol handling to RCU
@ 2011-04-05 18:01 Oliver Hartkopp
2011-04-06 9:27 ` Kurt Van Dijck
0 siblings, 1 reply; 5+ messages in thread
From: Oliver Hartkopp @ 2011-04-05 18:01 UTC (permalink / raw)
To: David Miller
Cc: Linux Netdev List, Kurt Van Dijck, Eric Dumazet, Urs Thuermann
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>
---
diff --git a/net/can/af_can.c b/net/can/af_can.c
index 733d66f..a8dcaa4 100644
--- a/net/can/af_can.c
+++ b/net/can/af_can.c
@@ -85,7 +85,7 @@ static struct kmem_cache *rcv_cache __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 */
@@ -115,6 +115,19 @@ static void can_sock_destruct(struct sock *sk)
skb_queue_purge(&sk->sk_receive_queue);
}
+static struct can_proto *can_try_module_get(int protocol)
+{
+ struct can_proto *cp;
+
+ rcu_read_lock();
+ cp = rcu_dereference(proto_tab[protocol]);
+ if (cp && !try_module_get(cp->prot->owner))
+ cp = NULL;
+ rcu_read_unlock();
+
+ return cp;
+}
+
static int can_create(struct net *net, struct socket *sock, int protocol,
int kern)
{
@@ -130,9 +143,12 @@ static int can_create(struct net *net, struct socket *sock, int protocol,
if (!net_eq(net, &init_net))
return -EAFNOSUPPORT;
+ cp = can_try_module_get(protocol);
+
#ifdef CONFIG_MODULES
- /* 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);
/*
@@ -143,22 +159,18 @@ static int can_create(struct net *net, struct socket *sock, int protocol,
if (err && printk_ratelimit())
printk(KERN_ERR "can: request_module "
"(can-proto-%d) failed.\n", protocol);
+
+ cp = can_try_module_get(protocol);
}
#endif
- spin_lock(&proto_tab_lock);
- cp = proto_tab[protocol];
- if (cp && !try_module_get(cp->prot->owner))
- cp = NULL;
- spin_unlock(&proto_tab_lock);
-
/* check for available protocol and correct usage */
if (!cp)
return -EPROTONOSUPPORT;
if (cp->type != sock->type) {
- err = -EPROTONOSUPPORT;
+ err = -EPROTOTYPE;
goto errout;
}
@@ -694,15 +706,16 @@ int can_proto_register(struct can_proto *cp)
if (err < 0)
return err;
- 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
- proto_tab[proto] = cp;
+ rcu_assign_pointer(proto_tab[proto], cp);
- spin_unlock(&proto_tab_lock);
+ mutex_unlock(&proto_tab_lock);
if (err < 0)
proto_unregister(cp->prot);
@@ -719,13 +732,12 @@ void can_proto_unregister(struct can_proto *cp)
{
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();
proto_unregister(cp->prot);
}
^ permalink raw reply related [flat|nested] 5+ messages in thread
* Re: [PATCH net-next-2.6 v2] can: convert protocol handling to RCU
2011-04-05 18:01 [PATCH net-next-2.6 v2] can: convert protocol handling to RCU Oliver Hartkopp
@ 2011-04-06 9:27 ` Kurt Van Dijck
2011-04-06 14:39 ` Oliver Hartkopp
0 siblings, 1 reply; 5+ messages in thread
From: Kurt Van Dijck @ 2011-04-06 9:27 UTC (permalink / raw)
To: Oliver Hartkopp
Cc: David Miller, Linux Netdev List, Eric Dumazet, Urs Thuermann
On Tue, Apr 05, 2011 at 08:01:16PM +0200, Oliver Hartkopp wrote:
> This patch removes spin_locks at CAN socket creation time by using RCU.
Good thing.
I'm interested in RCU also for my J1939 code.
I do have some questions below. These are mainly since I'm not yet used
to RCU. Btw, I must have missed v1 of the patch, so I may ask 'already
resolved' things...
>
>
> +static struct can_proto *can_try_module_get(int protocol)
> +{
> + struct can_proto *cp;
> +
> + rcu_read_lock();
> + cp = rcu_dereference(proto_tab[protocol]);
> + if (cp && !try_module_get(cp->prot->owner))
After the xxx_get, is the 'cp' pointer persistent?
> + cp = NULL;
> + rcu_read_unlock();
> +
> + return cp;
> +}
> +
> static int can_create(struct net *net, struct socket *sock, int protocol,
> int kern)
> {
> @@ -130,9 +143,12 @@ static int can_create(struct net *net, struct socket *sock, int protocol,
> if (!net_eq(net, &init_net))
> return -EAFNOSUPPORT;
>
> + cp = can_try_module_get(protocol);
> +
>
[...]
> /* check for available protocol and correct usage */
>
> if (!cp)
> return -EPROTONOSUPPORT;
>
> if (cp->type != sock->type) {
I don't see how this will evaluate to true?
can_proto_register takes care of it.
> - err = -EPROTONOSUPPORT;
> + err = -EPROTOTYPE;
> goto errout;
> }
>
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH net-next-2.6 v2] can: convert protocol handling to RCU
2011-04-06 9:27 ` Kurt Van Dijck
@ 2011-04-06 14:39 ` Oliver Hartkopp
2011-04-06 15:34 ` Kurt Van Dijck
0 siblings, 1 reply; 5+ messages in thread
From: Oliver Hartkopp @ 2011-04-06 14:39 UTC (permalink / raw)
To: Kurt Van Dijck
Cc: David Miller, Linux Netdev List, Eric Dumazet, Urs Thuermann
On 06.04.2011 11:27, Kurt Van Dijck wrote:
> On Tue, Apr 05, 2011 at 08:01:16PM +0200, Oliver Hartkopp wrote:
>>
>> +static struct can_proto *can_try_module_get(int protocol)
>> +{
>> + struct can_proto *cp;
>> +
>> + rcu_read_lock();
>> + cp = rcu_dereference(proto_tab[protocol]);
>> + if (cp && !try_module_get(cp->prot->owner))
> After the xxx_get, is the 'cp' pointer persistent?
try_module_get() increases the usage counter of the module - therefore it is.
It is protected until module_put(cp->prot->owner) at the end of can_create() .
>> /* check for available protocol and correct usage */
>>
>> if (!cp)
>> return -EPROTONOSUPPORT;
>>
>> if (cp->type != sock->type) {
> I don't see how this will evaluate to true?
> can_proto_register takes care of it.
This check compares the type of the socket that is to be created with the type
that's defined for this protocol.
E.g. if you would give
s = socket(PF_CAN, SOCK_STREAM, CAN_RAW);
instead of the correct
s = socket(PF_CAN, SOCK_RAW, CAN_RAW);
you will get this error.
>> - err = -EPROTONOSUPPORT;
>> + err = -EPROTOTYPE;
>> goto errout;
>> }
>>
Regards,
Oliver
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH net-next-2.6 v2] can: convert protocol handling to RCU
2011-04-06 14:39 ` Oliver Hartkopp
@ 2011-04-06 15:34 ` Kurt Van Dijck
2011-04-06 19:36 ` David Miller
0 siblings, 1 reply; 5+ messages in thread
From: Kurt Van Dijck @ 2011-04-06 15:34 UTC (permalink / raw)
To: Oliver Hartkopp
Cc: David Miller, Linux Netdev List, Eric Dumazet, Urs Thuermann
On Wed, Apr 06, 2011 at 04:39:03PM +0200, Oliver Hartkopp wrote:
> On 06.04.2011 11:27, Kurt Van Dijck wrote:
> > On Tue, Apr 05, 2011 at 08:01:16PM +0200, Oliver Hartkopp wrote:
>
> >>
> >> +static struct can_proto *can_try_module_get(int protocol)
> >> +{
> >> + struct can_proto *cp;
> >> +
> >> + rcu_read_lock();
> >> + cp = rcu_dereference(proto_tab[protocol]);
> >> + if (cp && !try_module_get(cp->prot->owner))
> > After the xxx_get, is the 'cp' pointer persistent?
>
> try_module_get() increases the usage counter of the module - therefore it is.
> It is protected until module_put(cp->prot->owner) at the end of can_create() .
Ok, I understand correctly now.
>
> >> /* check for available protocol and correct usage */
> >>
> >> if (!cp)
> >> return -EPROTONOSUPPORT;
> >>
> >> if (cp->type != sock->type) {
> > I don't see how this will evaluate to true?
> > can_proto_register takes care of it.
>
> This check compares the type of the socket that is to be created with the type
> that's defined for this protocol.
>
> E.g. if you would give
>
> s = socket(PF_CAN, SOCK_STREAM, CAN_RAW);
>
> instead of the correct
>
> s = socket(PF_CAN, SOCK_RAW, CAN_RAW);
>
> you will get this error.
Right, I see my mistake now.
>
> >> - err = -EPROTONOSUPPORT;
> >> + err = -EPROTOTYPE;
> >> goto errout;
> >> }
> >>
>
> Regards,
> Oliver
Regards,
Kurt
Acked-by: Kurt Van Dijck <kurt.van.dijck@eia.be>
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2011-04-06 19:36 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2011-04-05 18:01 [PATCH net-next-2.6 v2] can: convert protocol handling to RCU Oliver Hartkopp
2011-04-06 9:27 ` Kurt Van Dijck
2011-04-06 14:39 ` Oliver Hartkopp
2011-04-06 15:34 ` Kurt Van Dijck
2011-04-06 19:36 ` David Miller
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).