Re: [PATCH net] net: net_enable_timestamp() can be called from irq contexts

2017-03-02 Thread David Miller
From: Eric Dumazet 
Date: Wed, 01 Mar 2017 14:28:39 -0800

> From: Eric Dumazet 
> 
> It is now very clear that silly TCP listeners might play with
> enabling/disabling timestamping while new children are added
> to their accept queue.
> 
> Meaning net_enable_timestamp() can be called from BH context
> while current state of the static key is not enabled.
> 
> Lets play safe and allow all contexts.
> 
> The work queue is scheduled only under the problematic cases,
> which are the static key enable/disable transition, to not slow down
> critical paths.
> 
> This extends and improves what we did in commit 5fa8bbda38c6 ("net: use
> a work queue to defer net_disable_timestamp() work")
> 
> Fixes: b90e5794c5bd ("net: dont call jump_label_dec from irq context")
> Signed-off-by: Eric Dumazet 
> Reported-by: Dmitry Vyukov 

Applied and queued up for -stable.


[PATCH net] net: net_enable_timestamp() can be called from irq contexts

2017-03-01 Thread Eric Dumazet
From: Eric Dumazet 

It is now very clear that silly TCP listeners might play with
enabling/disabling timestamping while new children are added
to their accept queue.

Meaning net_enable_timestamp() can be called from BH context
while current state of the static key is not enabled.

Lets play safe and allow all contexts.

The work queue is scheduled only under the problematic cases,
which are the static key enable/disable transition, to not slow down
critical paths.

This extends and improves what we did in commit 5fa8bbda38c6 ("net: use
a work queue to defer net_disable_timestamp() work")

Fixes: b90e5794c5bd ("net: dont call jump_label_dec from irq context")
Signed-off-by: Eric Dumazet 
Reported-by: Dmitry Vyukov 
---
 net/core/dev.c |   35 +++
 1 file changed, 31 insertions(+), 4 deletions(-)

diff --git a/net/core/dev.c b/net/core/dev.c
index 
e63bf61b19be029e30ac40443c0e2edb24de4a73..8637b2b71f3d4751366a2ca5ba46579e6a5fa953
 100644
--- a/net/core/dev.c
+++ b/net/core/dev.c
@@ -1698,27 +1698,54 @@ EXPORT_SYMBOL_GPL(net_dec_egress_queue);
 static struct static_key netstamp_needed __read_mostly;
 #ifdef HAVE_JUMP_LABEL
 static atomic_t netstamp_needed_deferred;
+static atomic_t netstamp_wanted;
 static void netstamp_clear(struct work_struct *work)
 {
int deferred = atomic_xchg(_needed_deferred, 0);
+   int wanted;
 
-   while (deferred--)
-   static_key_slow_dec(_needed);
+   wanted = atomic_add_return(deferred, _wanted);
+   if (wanted > 0)
+   static_key_enable(_needed);
+   else
+   static_key_disable(_needed);
 }
 static DECLARE_WORK(netstamp_work, netstamp_clear);
 #endif
 
 void net_enable_timestamp(void)
 {
+#ifdef HAVE_JUMP_LABEL
+   int wanted;
+
+   while (1) {
+   wanted = atomic_read(_wanted);
+   if (wanted <= 0)
+   break;
+   if (atomic_cmpxchg(_wanted, wanted, wanted + 1) == 
wanted)
+   return;
+   }
+   atomic_inc(_needed_deferred);
+   schedule_work(_work);
+#else
static_key_slow_inc(_needed);
+#endif
 }
 EXPORT_SYMBOL(net_enable_timestamp);
 
 void net_disable_timestamp(void)
 {
 #ifdef HAVE_JUMP_LABEL
-   /* net_disable_timestamp() can be called from non process context */
-   atomic_inc(_needed_deferred);
+   int wanted;
+
+   while (1) {
+   wanted = atomic_read(_wanted);
+   if (wanted <= 1)
+   break;
+   if (atomic_cmpxchg(_wanted, wanted, wanted - 1) == 
wanted)
+   return;
+   }
+   atomic_dec(_needed_deferred);
schedule_work(_work);
 #else
static_key_slow_dec(_needed);