net/sched: act_police: fix race condition on state variables
authorDavide Caratti <dcaratti@redhat.com>
Tue, 20 Nov 2018 21:18:44 +0000 (22:18 +0100)
committerDavid S. Miller <davem@davemloft.net>
Tue, 20 Nov 2018 22:59:58 +0000 (14:59 -0800)
commitf2cbd485282014132851bf37cb2ca624a456275d
tree60b84978c054decb6e70e7dd99e294220d347c64
parentb1d982330134bbba371821bc850da6fa4bfa3271
net/sched: act_police: fix race condition on state variables

after 'police' configuration parameters were converted to use RCU instead
of spinlock, the state variables used to compute the traffic rate (namely
'tcfp_toks', 'tcfp_ptoks' and 'tcfp_t_c') are erroneously read/updated in
the traffic path without any protection.

Use a dedicated spinlock to avoid race conditions on these variables, and
ensure proper cache-line alignment. In this way, 'police' is still faster
than what we observed when 'tcf_lock' was used in the traffic path _ i.e.
reverting commit 2d550dbad83c ("net/sched: act_police: don't use spinlock
in the data path"). Moreover, we preserve the throughput improvement that
was obtained after 'police' started using per-cpu counters, when 'avrate'
is used instead of 'rate'.

Changes since v1 (thanks to Eric Dumazet):
- call ktime_get_ns() before acquiring the lock in the traffic path
- use a dedicated spinlock instead of tcf_lock
- improve cache-line usage

Fixes: 2d550dbad83c ("net/sched: act_police: don't use spinlock in the data path")
Reported-and-suggested-by: Eric Dumazet <eric.dumazet@gmail.com>
Signed-off-by: Davide Caratti <dcaratti@redhat.com>
Reviewed-by: Eric Dumazet <edumazet@google.com>
net/sched/act_police.c