diff mbox series

[v2] ipv4: fix a RCU-list lock in fib_triestat_seq_show

Message ID 20200325220100.7863-1-cai@lca.pw
State Accepted
Delegated to: David Miller
Headers show
Series [v2] ipv4: fix a RCU-list lock in fib_triestat_seq_show | expand

Commit Message

Qian Cai March 25, 2020, 10:01 p.m. UTC
fib_triestat_seq_show() calls hlist_for_each_entry_rcu(tb, head,
tb_hlist) without rcu_read_lock() will trigger a warning,

 net/ipv4/fib_trie.c:2579 RCU-list traversed in non-reader section!!

 other info that might help us debug this:

 rcu_scheduler_active = 2, debug_locks = 1
 1 lock held by proc01/115277:
  #0: c0000014507acf00 (&p->lock){+.+.}-{3:3}, at: seq_read+0x58/0x670

 Call Trace:
  dump_stack+0xf4/0x164 (unreliable)
  lockdep_rcu_suspicious+0x140/0x164
  fib_triestat_seq_show+0x750/0x880
  seq_read+0x1a0/0x670
  proc_reg_read+0x10c/0x1b0
  __vfs_read+0x3c/0x70
  vfs_read+0xac/0x170
  ksys_read+0x7c/0x140
  system_call+0x5c/0x68

Fix it by adding a pair of rcu_read_lock/unlock() and use
cond_resched_rcu() to avoid the situation where walking of a large
number of items  may prevent scheduling for a long time.

Signed-off-by: Qian Cai <cai@lca.pw>
---

Use cond_resched_rcu() from Eric.

 net/ipv4/fib_trie.c | 3 +++
 1 file changed, 3 insertions(+)

Comments

David Miller March 27, 2020, 3:27 a.m. UTC | #1
From: Qian Cai <cai@lca.pw>
Date: Wed, 25 Mar 2020 18:01:00 -0400

> fib_triestat_seq_show() calls hlist_for_each_entry_rcu(tb, head,
> tb_hlist) without rcu_read_lock() will trigger a warning,
> 
>  net/ipv4/fib_trie.c:2579 RCU-list traversed in non-reader section!!
> 
>  other info that might help us debug this:
> 
>  rcu_scheduler_active = 2, debug_locks = 1
>  1 lock held by proc01/115277:
>   #0: c0000014507acf00 (&p->lock){+.+.}-{3:3}, at: seq_read+0x58/0x670
> 
>  Call Trace:
>   dump_stack+0xf4/0x164 (unreliable)
>   lockdep_rcu_suspicious+0x140/0x164
>   fib_triestat_seq_show+0x750/0x880
>   seq_read+0x1a0/0x670
>   proc_reg_read+0x10c/0x1b0
>   __vfs_read+0x3c/0x70
>   vfs_read+0xac/0x170
>   ksys_read+0x7c/0x140
>   system_call+0x5c/0x68
> 
> Fix it by adding a pair of rcu_read_lock/unlock() and use
> cond_resched_rcu() to avoid the situation where walking of a large
> number of items  may prevent scheduling for a long time.
> 
> Signed-off-by: Qian Cai <cai@lca.pw>

Eric, please review.
Eric Dumazet March 27, 2020, 5:41 a.m. UTC | #2
On 3/26/20 8:27 PM, David Miller wrote:
> From: Qian Cai <cai@lca.pw>
> Date: Wed, 25 Mar 2020 18:01:00 -0400

>> Fix it by adding a pair of rcu_read_lock/unlock() and use
>> cond_resched_rcu() to avoid the situation where walking of a large
>> number of items  may prevent scheduling for a long time.
>>
>> Signed-off-by: Qian Cai <cai@lca.pw>
> 
> Eric, please review.
> 

Patch looks good to me, thanks !

Reviewed-by: Eric Dumazet <edumazet@google.com>
David Miller March 30, 2020, 4:52 a.m. UTC | #3
From: Qian Cai <cai@lca.pw>
Date: Wed, 25 Mar 2020 18:01:00 -0400

> fib_triestat_seq_show() calls hlist_for_each_entry_rcu(tb, head,
> tb_hlist) without rcu_read_lock() will trigger a warning,
> 
>  net/ipv4/fib_trie.c:2579 RCU-list traversed in non-reader section!!
> 
>  other info that might help us debug this:
> 
>  rcu_scheduler_active = 2, debug_locks = 1
>  1 lock held by proc01/115277:
>   #0: c0000014507acf00 (&p->lock){+.+.}-{3:3}, at: seq_read+0x58/0x670
> 
>  Call Trace:
>   dump_stack+0xf4/0x164 (unreliable)
>   lockdep_rcu_suspicious+0x140/0x164
>   fib_triestat_seq_show+0x750/0x880
>   seq_read+0x1a0/0x670
>   proc_reg_read+0x10c/0x1b0
>   __vfs_read+0x3c/0x70
>   vfs_read+0xac/0x170
>   ksys_read+0x7c/0x140
>   system_call+0x5c/0x68
> 
> Fix it by adding a pair of rcu_read_lock/unlock() and use
> cond_resched_rcu() to avoid the situation where walking of a large
> number of items  may prevent scheduling for a long time.
> 
> Signed-off-by: Qian Cai <cai@lca.pw>

Applied and queued up for -stable, thanks.
diff mbox series

Patch

diff --git a/net/ipv4/fib_trie.c b/net/ipv4/fib_trie.c
index ff0c24371e33..3be0affbabd3 100644
--- a/net/ipv4/fib_trie.c
+++ b/net/ipv4/fib_trie.c
@@ -2577,6 +2577,7 @@  static int fib_triestat_seq_show(struct seq_file *seq, void *v)
 		   " %zd bytes, size of tnode: %zd bytes.\n",
 		   LEAF_SIZE, TNODE_SIZE(0));
 
+	rcu_read_lock();
 	for (h = 0; h < FIB_TABLE_HASHSZ; h++) {
 		struct hlist_head *head = &net->ipv4.fib_table_hash[h];
 		struct fib_table *tb;
@@ -2596,7 +2597,9 @@  static int fib_triestat_seq_show(struct seq_file *seq, void *v)
 			trie_show_usage(seq, t->stats);
 #endif
 		}
+		cond_resched_rcu();
 	}
+	rcu_read_unlock();
 
 	return 0;
 }