Skip to contentSkip to conversation

ROUTE: Avoid sparse warnings

IPV4 Eric Dumazet 4 participants 10 messages Jan 7, 2008 – Jan 8, 2008

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

CHECK net/ipv4/route.c
net/ipv4/route.c:298:2: warning: context imbalance in 'rt_cache_get_first' - wrong count at exit net/ipv4/route.c:307:3: warning: context imbalance in 'rt_cache_get_next' - unexpected unlock net/ipv4/route.c:346:3: warning: context imbalance in 'rt_cache_seq_stop' - unexpected unlock

net/ipv4/route.c +6 -4
index 10915bb..fec0571 100644
@@ -289,11 +289,11 @@ static struct rtable *rt_cache_get_first(struct seq_file *seq)
 	struct rt_cache_iter_state *st = seq->private;
 
 	for (st->bucket = rt_hash_mask; st->bucket >= 0; --st->bucket) {
-		rcu_read_lock_bh();
 		r = rt_hash_table[st->bucket].chain;
 		if (r)
 			break;
 		rcu_read_unlock_bh();
+		rcu_read_lock_bh();
 	}
 	return r;
 }
@@ -305,9 +305,9 @@ static struct rtable *rt_cache_get_next(struct seq_file *seq, struct rtable *r)
 	r = r->u.dst.rt_next;
 	while (!r) {
 		rcu_read_unlock_bh();
+		rcu_read_lock_bh();
 		if (--st->bucket < 0)
 			break;
-		rcu_read_lock_bh();
 		r = rt_hash_table[st->bucket].chain;
 	}
 	return r;
@@ -324,7 +324,9 @@ static struct rtable *rt_cache_get_idx(struct seq_file *seq, loff_t pos)
 }
 
 static void *rt_cache_seq_start(struct seq_file *seq, loff_t *pos)
+	__acquires(RCU_BH)
 {
+	rcu_read_lock_bh();
 	return *pos ? rt_cache_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
 }
 
@@ -341,9 +343,9 @@ static void *rt_cache_seq_next(struct seq_file *seq, void *v, loff_t *pos)
 }
 
 static void rt_cache_seq_stop(struct seq_file *seq, void *v)
+	__releases(RCU_BH)
 {
-	if (v && v != SEQ_START_TOKEN)
-		rcu_read_unlock_bh();
+	rcu_read_unlock_bh();
 }
 
 static int rt_cache_seq_show(struct seq_file *seq, void *v)
#1 lore raw
Quoted 12 lines · Eric Dumazet

CHECK net/ipv4/route.c
net/ipv4/route.c:298:2: warning: context imbalance in 'rt_cache_get_first' - wrong count at exit net/ipv4/route.c:307:3: warning: context imbalance in 'rt_cache_get_next' - unexpected unlock net/ipv4/route.c:346:3: warning: context imbalance in 'rt_cache_seq_stop' - unexpected unlock

net/ipv4/route.c +0 -0
index 10915bb..fec0571 100644

net/ipv4/route.c static struct rtable *rt_cache_get_first(struct seq_file *seq) · line 296

Quoted 9 lines
net/ipv4/route.c +1 -1
@@ -289,11 +289,11 @@ static struct rtable *rt_cache_get_first(struct seq_file *seq)
       struct rt_cache_iter_state *st = seq->private;
 
       for (st->bucket = rt_hash_mask; st->bucket >= 0; --st->bucket) {
-               rcu_read_lock_bh();
               r = rt_hash_table[st->bucket].chain;
               if (r)
                       break;
               rcu_read_unlock_bh();
+               rcu_read_lock_bh();

If we have to change perfectly working code to silence sparse then either sparse or we are doing something wrong.

This is not the only spot where we conditionally hold the lock. There's got to be a better fix than changing all of them to hold locks unconditionally.

Cheers,

Signature · 4 lines
Visit Openswan at http://www.openswan.org/
Email: Herbert Xu ~{PmV>HI~} <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
#2 lore raw
Quoted 26 lines · On Mon, 07 Jan 2008 23:11:53 +1100 Herbert Xu <[email protected]>…
Quoted 12 lines · Eric Dumazet

CHECK net/ipv4/route.c
net/ipv4/route.c:298:2: warning: context imbalance in 'rt_cache_get_first' - wrong count at exit net/ipv4/route.c:307:3: warning: context imbalance in 'rt_cache_get_next' - unexpected unlock net/ipv4/route.c:346:3: warning: context imbalance in 'rt_cache_seq_stop' - unexpected unlock

net/ipv4/route.c +0 -0
index 10915bb..fec0571 100644

net/ipv4/route.c static struct rtable *rt_cache_get_first(struct seq_file *seq) · line 296

Quoted 9 lines
net/ipv4/route.c +1 -1
@@ -289,11 +289,11 @@ static struct rtable *rt_cache_get_first(struct seq_file *seq)
       struct rt_cache_iter_state *st = seq->private;
 
       for (st->bucket = rt_hash_mask; st->bucket >= 0; --st->bucket) {
-               rcu_read_lock_bh();
               r = rt_hash_table[st->bucket].chain;
               if (r)
                       break;
               rcu_read_unlock_bh();
+               rcu_read_lock_bh();

If we have to change perfectly working code to silence sparse then either sparse or we are doing something wrong.

Quoted 3 lines

This is not the only spot where we conditionally hold the lock. There's got to be a better fix than changing all of them to hold locks unconditionally.

Maybe sparse (or me :) ) is a litle bit dumb :(

You are right other functions conditionally hold some lock(s), but in this case this is not really necessary / worth the complexity.

AFAIK, this patch reduces complexity and text size.

#3 lore raw
Quoted 2 lines · Eric Dumazet

AFAIK, this patch reduces complexity and text size.

But if we had loads of empty hash buckets couldn't this potentially increase latency by disabling BH longer than before?

Cheers,

Signature · 4 lines
Visit Openswan at http://www.openswan.org/
Email: Herbert Xu ~{PmV>HI~} <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
#4 lore raw
Quoted 5 lines · Herbert Xu
Quoted 1 line · Eric Dumazet

AFAIK, this patch reduces complexity and text size.

But if we had loads of empty hash buckets couldn't this potentially increase latency by disabling BH longer than before?

Well, we call rcu_read_unlock_bh()/rcu_read_lock_bh() for each bucket, empty or not, before and after patch, so we dont change latency.

#5 lore raw
Quoted 3 lines · Eric Dumazet

Well, we call rcu_read_unlock_bh()/rcu_read_lock_bh() for each bucket, empty or not, before and after patch, so we dont change latency.

Oh I see. Your patch looks good then. But we still need a solution in general unless we're to avoid all uses of conditional locking.

Cheers,

Signature · 4 lines
Visit Openswan at http://www.openswan.org/
Email: Herbert Xu ~{PmV>HI~} <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
maintainer
#6 lore raw
In reply to Eric Dumazet

From: Eric Dumazet <[email protected]>
Date: Mon, 7 Jan 2008 12:01:17 +0100

Quoted 14 lines

CHECK net/ipv4/route.c
net/ipv4/route.c:298:2: warning: context imbalance in 'rt_cache_get_first' - wrong count at exit net/ipv4/route.c:307:3: warning: context imbalance in 'rt_cache_get_next' - unexpected unlock net/ipv4/route.c:346:3: warning: context imbalance in 'rt_cache_seq_stop' - unexpected unlock

net/ipv4/route.c +0 -0
index 10915bb..fec0571 100644
@@ -289,11 +289,11 @@ static struct rtable *rt_cache_get_first(struct seq_file *seq)
 	struct rt_cache_iter_state *st = seq->private;
 

net/ipv4/route.c static struct rtable *rt_cache_get_first(struct seq_file *seq) · line 299

Quoted 10 lines
net/ipv4/route.c +1 -1
 	for (st->bucket = rt_hash_mask; st->bucket >= 0; --st->bucket) {
-		rcu_read_lock_bh();
 		r = rt_hash_table[st->bucket].chain;
 		if (r)
 			break;
 		rcu_read_unlock_bh();
+		rcu_read_lock_bh();
 	}
 	return r;
 }

Like for Herbert, this patch leaves a bad taste in my mouth.

I don't understand, is it the case that sparse doesn't like that rt_cache_get_first() returns with rcu_read_lock_bh() held? That's kind of rediculious.

Furthermore, these:

	rcu_read_unlock_bh()
	rcu_read_lock_bh()

sequences are at best funny looking. For other lock types we would look at this and ask "Does this even accomplish anything reliably?"

The answer here is that it wants the preempt_enable() to run to get any potential kernel preemptions executed. It also allows any pending software interrupts to run.

So this does something reliably only because rcu_read_unlock_bh() has specific and explicit side effects.

I don't know, to me it just looks awful :-) I better understood the original code.

We can continue splitting hairs over this but I'll hold off on this patch for now. :)

#7 lore raw
Quoted 30 lines · David Miller

From: Eric Dumazet <[email protected]>
Date: Mon, 7 Jan 2008 12:01:17 +0100

Quoted 14 lines

CHECK net/ipv4/route.c
net/ipv4/route.c:298:2: warning: context imbalance in 'rt_cache_get_first' - wrong count at exit net/ipv4/route.c:307:3: warning: context imbalance in 'rt_cache_get_next' - unexpected unlock net/ipv4/route.c:346:3: warning: context imbalance in 'rt_cache_seq_stop' - unexpected unlock

net/ipv4/route.c +0 -0
index 10915bb..fec0571 100644
@@ -289,11 +289,11 @@ static struct rtable *rt_cache_get_first(struct seq_file *seq)
 	struct rt_cache_iter_state *st = seq->private;
 

net/ipv4/route.c static struct rtable *rt_cache_get_first(struct seq_file *seq) · line 299

Quoted 10 lines
net/ipv4/route.c +1 -1
 	for (st->bucket = rt_hash_mask; st->bucket >= 0; --st->bucket) {
-		rcu_read_lock_bh();
 		r = rt_hash_table[st->bucket].chain;
 		if (r)
 			break;
 		rcu_read_unlock_bh();
+		rcu_read_lock_bh();
 	}
 	return r;
 }

Like for Herbert, this patch leaves a bad taste in my mouth.

Quoted 3 lines

I don't understand, is it the case that sparse doesn't like that rt_cache_get_first() returns with rcu_read_lock_bh() held? That's kind of rediculious.

Not exactly.
It warns us because rt_cache_get_first() can returns with RCU_BH *acquired* or not. I find this warning very usefull. In rt_cache_get_first() this warning is a false alarm.

Quoted 8 lines

Furthermore, these:

	rcu_read_unlock_bh()
	rcu_read_lock_bh()

sequences are at best funny looking. For other lock types we would look at this and ask "Does this even accomplish anything reliably?"

Well, original code exactly does the same thing.

Quoted 8 lines

The answer here is that it wants the preempt_enable() to run to get any potential kernel preemptions executed. It also allows any pending software interrupts to run.

So this does something reliably only because rcu_read_unlock_bh() has specific and explicit side effects.

I will post a patch to introduce a helper function, so that this is clearly documented and not relying on side effects. Actual implementation has latency problems on empty hash tables if CONFIG_PREEMPT=n

Quoted 5 lines

I don't know, to me it just looks awful :-) I better understood the original code.

We can continue splitting hairs over this but I'll hold off on this patch for now. :)

Fair enough

Thanks

maintainer
#8 lore raw

From: Eric Dumazet <[email protected]>
Date: Tue, 08 Jan 2008 08:53:10 +0100

Quoted 2 lines

It warns us because rt_cache_get_first() can returns with RCU_BH *acquired* or not.

As Herbert mentioned that's a pretty often used paradigm called conditional locking. :-)

#9 lore raw
In reply to Eric Dumazet
Quoted 1 line · Eric Dumazet

David Miller a écrit :

...

Quoted 15 lines
Quoted 7 lines

Furthermore, these:

    rcu_read_unlock_bh()
    rcu_read_lock_bh()

sequences are at best funny looking. For other lock types we would look at this and ask "Does this even accomplish anything reliably?"

Well, original code exactly does the same thing.

Quoted 5 lines

The answer here is that it wants the preempt_enable() to run to get any potential kernel preemptions executed. It also allows any pending software interrupts to run.

Quoted 12 lines
Quoted 3 lines

So this does something reliably only because rcu_read_unlock_bh() has specific and explicit side effects.

I will post a patch to introduce a helper function, so that this is clearly documented and not relying on side effects. Actual implementation has latency
problems on empty hash tables if CONFIG_PREEMPT=n

Quoted 2 lines

I don't know, to me it just looks awful :-) I better understood the original code.

It seems this patch only made it more visible how it currently works. I don't know what changes do you plan for this helper function, but my proposal is to add some counter and break this rcu only after looping for some time. Alternatively cpu_relax() could be probably used between these "locks". Without this probably some cache problems are possible, but you know this better, I guess.

Regards,
Jarek P.