Re: [PATCH] bpf: Call rcu_read_unlock() before copy_to_user()

2015-01-22 Thread Alexei Starovoitov
On Thu, Jan 22, 2015 at 9:54 AM, Michael Holzheu
 wrote:
>> > So call rcu_read_unlock() before copy_to_user(). We can
>> > release the lock earlier because it is not needed for copy_to_user().
>>
>> we cannot move the rcu unlock this way, since it protects the value.
>> So we need to copy the value while still under rcu.
>
> Ok, right. I assume you will provide the correct fix.

sure :) will do.
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/


Re: [PATCH] bpf: Call rcu_read_unlock() before copy_to_user()

2015-01-22 Thread Michael Holzheu
On Thu, 22 Jan 2015 09:27:21 -0800
Alexei Starovoitov  wrote:

> On Thu, Jan 22, 2015 at 7:57 AM, Michael Holzheu
>  wrote:
> > We must not hold locks when calling copy_to_user():
> >
> > BUG: sleeping function called from invalid context at mm/memory.c:3732
> > in_atomic(): 0, irqs_disabled(): 0, pid: 671, name: test_maps
> > 1 lock held by test_maps/671:
> >  #0:  (rcu_read_lock){..}, at: [<00264190>] 
> > map_lookup_elem+0xe8/0x260
> > Preemption disabled at:[<001be3b6>] vprintk_default+0x56/0x68
> >
> > CPU: 0 PID: 671 Comm: test_maps Not tainted 3.19.0-rc5-00117-g5eb11d6-dirty 
> > #424
> >1e447bb0 1e447c40 0002 
> >1e447ce0 1e447c58 1e447c58 00115c8a
> > 00c08246 00c27e8a 000b
> >1e447ca0 1e447c40  
> > 00115c8a 1e447c40 1e447ca0
> > Call Trace:
> > ([<00115b7e>] show_trace+0x12e/0x150)
> >  [<00115c40>] show_stack+0xa0/0x100
> >  [<009b163c>] dump_stack+0x74/0xc8
> >  [<0017424a>] ___might_sleep+0x23a/0x248
> >  [<002b58e8>] might_fault+0x70/0xe8
> >  [<00264230>] map_lookup_elem+0x188/0x260
> >  [<00264716>] SyS_bpf+0x20e/0x840
> >  [<009bbe3a>] system_call+0xd6/0x24c
> >  [<03fffd15f566>] 0x3fffd15f566
> > 1 lock held by test_maps/671:
> >  #0:  (rcu_read_lock){..}, at: [<00264190>] 
> > map_lookup_elem+0xe8/0x260
> >
> > So call rcu_read_unlock() before copy_to_user(). We can
> > release the lock earlier because it is not needed for copy_to_user().
> 
> we cannot move the rcu unlock this way, since it protects the value.
> So we need to copy the value while still under rcu.

Ok, right. I assume you will provide the correct fix.

> I'm puzzled how I missed this warning.
> I guess you have CONFIG_PREEMPT_RCU=y ?

Yes.

Regards,
Michael

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/


Re: [PATCH] bpf: Call rcu_read_unlock() before copy_to_user()

2015-01-22 Thread Alexei Starovoitov
On Thu, Jan 22, 2015 at 7:57 AM, Michael Holzheu
 wrote:
> We must not hold locks when calling copy_to_user():
>
> BUG: sleeping function called from invalid context at mm/memory.c:3732
> in_atomic(): 0, irqs_disabled(): 0, pid: 671, name: test_maps
> 1 lock held by test_maps/671:
>  #0:  (rcu_read_lock){..}, at: [<00264190>] 
> map_lookup_elem+0xe8/0x260
> Preemption disabled at:[<001be3b6>] vprintk_default+0x56/0x68
>
> CPU: 0 PID: 671 Comm: test_maps Not tainted 3.19.0-rc5-00117-g5eb11d6-dirty 
> #424
>1e447bb0 1e447c40 0002 
>1e447ce0 1e447c58 1e447c58 00115c8a
> 00c08246 00c27e8a 000b
>1e447ca0 1e447c40  
> 00115c8a 1e447c40 1e447ca0
> Call Trace:
> ([<00115b7e>] show_trace+0x12e/0x150)
>  [<00115c40>] show_stack+0xa0/0x100
>  [<009b163c>] dump_stack+0x74/0xc8
>  [<0017424a>] ___might_sleep+0x23a/0x248
>  [<002b58e8>] might_fault+0x70/0xe8
>  [<00264230>] map_lookup_elem+0x188/0x260
>  [<00264716>] SyS_bpf+0x20e/0x840
>  [<009bbe3a>] system_call+0xd6/0x24c
>  [<03fffd15f566>] 0x3fffd15f566
> 1 lock held by test_maps/671:
>  #0:  (rcu_read_lock){..}, at: [<00264190>] 
> map_lookup_elem+0xe8/0x260
>
> So call rcu_read_unlock() before copy_to_user(). We can
> release the lock earlier because it is not needed for copy_to_user().

we cannot move the rcu unlock this way, since it protects the value.
So we need to copy the value while still under rcu.
I'm puzzled how I missed this warning.
I guess you have CONFIG_PREEMPT_RCU=y ?
and if (in_atomic()) return; as part of might_fault() hid it.
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/


[PATCH] bpf: Call rcu_read_unlock() before copy_to_user()

2015-01-22 Thread Michael Holzheu
We must not hold locks when calling copy_to_user():

BUG: sleeping function called from invalid context at mm/memory.c:3732
in_atomic(): 0, irqs_disabled(): 0, pid: 671, name: test_maps
1 lock held by test_maps/671:
 #0:  (rcu_read_lock){..}, at: [<00264190>] 
map_lookup_elem+0xe8/0x260
Preemption disabled at:[<001be3b6>] vprintk_default+0x56/0x68

CPU: 0 PID: 671 Comm: test_maps Not tainted 3.19.0-rc5-00117-g5eb11d6-dirty #424
   1e447bb0 1e447c40 0002  
   1e447ce0 1e447c58 1e447c58 00115c8a 
    00c08246 00c27e8a 000b 
   1e447ca0 1e447c40   
    00115c8a 1e447c40 1e447ca0 
Call Trace:
([<00115b7e>] show_trace+0x12e/0x150)
 [<00115c40>] show_stack+0xa0/0x100
 [<009b163c>] dump_stack+0x74/0xc8
 [<0017424a>] ___might_sleep+0x23a/0x248
 [<002b58e8>] might_fault+0x70/0xe8
 [<00264230>] map_lookup_elem+0x188/0x260
 [<00264716>] SyS_bpf+0x20e/0x840
 [<009bbe3a>] system_call+0xd6/0x24c
 [<03fffd15f566>] 0x3fffd15f566
1 lock held by test_maps/671:
 #0:  (rcu_read_lock){..}, at: [<00264190>] 
map_lookup_elem+0xe8/0x260

So call rcu_read_unlock() before copy_to_user(). We can
release the lock earlier because it is not needed for copy_to_user().

Signed-off-by: Michael Holzheu 
---
 kernel/bpf/syscall.c |7 +++
 1 file changed, 3 insertions(+), 4 deletions(-)

--- a/kernel/bpf/syscall.c
+++ b/kernel/bpf/syscall.c
@@ -172,17 +172,16 @@ static int map_lookup_elem(union bpf_att
err = -ENOENT;
rcu_read_lock();
value = map->ops->map_lookup_elem(map, key);
+   rcu_read_unlock();
if (!value)
-   goto err_unlock;
+   goto free_key;
 
err = -EFAULT;
if (copy_to_user(uvalue, value, map->value_size) != 0)
-   goto err_unlock;
+   goto free_key;
 
err = 0;
 
-err_unlock:
-   rcu_read_unlock();
 free_key:
kfree(key);
 err_put:

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/


[PATCH] bpf: Call rcu_read_unlock() before copy_to_user()

2015-01-22 Thread Michael Holzheu
We must not hold locks when calling copy_to_user():

BUG: sleeping function called from invalid context at mm/memory.c:3732
in_atomic(): 0, irqs_disabled(): 0, pid: 671, name: test_maps
1 lock held by test_maps/671:
 #0:  (rcu_read_lock){..}, at: [00264190] 
map_lookup_elem+0xe8/0x260
Preemption disabled at:[001be3b6] vprintk_default+0x56/0x68

CPU: 0 PID: 671 Comm: test_maps Not tainted 3.19.0-rc5-00117-g5eb11d6-dirty #424
   1e447bb0 1e447c40 0002  
   1e447ce0 1e447c58 1e447c58 00115c8a 
    00c08246 00c27e8a 000b 
   1e447ca0 1e447c40   
    00115c8a 1e447c40 1e447ca0 
Call Trace:
([00115b7e] show_trace+0x12e/0x150)
 [00115c40] show_stack+0xa0/0x100
 [009b163c] dump_stack+0x74/0xc8
 [0017424a] ___might_sleep+0x23a/0x248
 [002b58e8] might_fault+0x70/0xe8
 [00264230] map_lookup_elem+0x188/0x260
 [00264716] SyS_bpf+0x20e/0x840
 [009bbe3a] system_call+0xd6/0x24c
 [03fffd15f566] 0x3fffd15f566
1 lock held by test_maps/671:
 #0:  (rcu_read_lock){..}, at: [00264190] 
map_lookup_elem+0xe8/0x260

So call rcu_read_unlock() before copy_to_user(). We can
release the lock earlier because it is not needed for copy_to_user().

Signed-off-by: Michael Holzheu holz...@linux.vnet.ibm.com
---
 kernel/bpf/syscall.c |7 +++
 1 file changed, 3 insertions(+), 4 deletions(-)

--- a/kernel/bpf/syscall.c
+++ b/kernel/bpf/syscall.c
@@ -172,17 +172,16 @@ static int map_lookup_elem(union bpf_att
err = -ENOENT;
rcu_read_lock();
value = map-ops-map_lookup_elem(map, key);
+   rcu_read_unlock();
if (!value)
-   goto err_unlock;
+   goto free_key;
 
err = -EFAULT;
if (copy_to_user(uvalue, value, map-value_size) != 0)
-   goto err_unlock;
+   goto free_key;
 
err = 0;
 
-err_unlock:
-   rcu_read_unlock();
 free_key:
kfree(key);
 err_put:

--
To unsubscribe from this list: send the line unsubscribe linux-kernel in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/


Re: [PATCH] bpf: Call rcu_read_unlock() before copy_to_user()

2015-01-22 Thread Alexei Starovoitov
On Thu, Jan 22, 2015 at 9:54 AM, Michael Holzheu
holz...@linux.vnet.ibm.com wrote:
  So call rcu_read_unlock() before copy_to_user(). We can
  release the lock earlier because it is not needed for copy_to_user().

 we cannot move the rcu unlock this way, since it protects the value.
 So we need to copy the value while still under rcu.

 Ok, right. I assume you will provide the correct fix.

sure :) will do.
--
To unsubscribe from this list: send the line unsubscribe linux-kernel in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/


Re: [PATCH] bpf: Call rcu_read_unlock() before copy_to_user()

2015-01-22 Thread Michael Holzheu
On Thu, 22 Jan 2015 09:27:21 -0800
Alexei Starovoitov alexei.starovoi...@gmail.com wrote:

 On Thu, Jan 22, 2015 at 7:57 AM, Michael Holzheu
 holz...@linux.vnet.ibm.com wrote:
  We must not hold locks when calling copy_to_user():
 
  BUG: sleeping function called from invalid context at mm/memory.c:3732
  in_atomic(): 0, irqs_disabled(): 0, pid: 671, name: test_maps
  1 lock held by test_maps/671:
   #0:  (rcu_read_lock){..}, at: [00264190] 
  map_lookup_elem+0xe8/0x260
  Preemption disabled at:[001be3b6] vprintk_default+0x56/0x68
 
  CPU: 0 PID: 671 Comm: test_maps Not tainted 3.19.0-rc5-00117-g5eb11d6-dirty 
  #424
 1e447bb0 1e447c40 0002 
 1e447ce0 1e447c58 1e447c58 00115c8a
  00c08246 00c27e8a 000b
 1e447ca0 1e447c40  
  00115c8a 1e447c40 1e447ca0
  Call Trace:
  ([00115b7e] show_trace+0x12e/0x150)
   [00115c40] show_stack+0xa0/0x100
   [009b163c] dump_stack+0x74/0xc8
   [0017424a] ___might_sleep+0x23a/0x248
   [002b58e8] might_fault+0x70/0xe8
   [00264230] map_lookup_elem+0x188/0x260
   [00264716] SyS_bpf+0x20e/0x840
   [009bbe3a] system_call+0xd6/0x24c
   [03fffd15f566] 0x3fffd15f566
  1 lock held by test_maps/671:
   #0:  (rcu_read_lock){..}, at: [00264190] 
  map_lookup_elem+0xe8/0x260
 
  So call rcu_read_unlock() before copy_to_user(). We can
  release the lock earlier because it is not needed for copy_to_user().
 
 we cannot move the rcu unlock this way, since it protects the value.
 So we need to copy the value while still under rcu.

Ok, right. I assume you will provide the correct fix.

 I'm puzzled how I missed this warning.
 I guess you have CONFIG_PREEMPT_RCU=y ?

Yes.

Regards,
Michael

--
To unsubscribe from this list: send the line unsubscribe linux-kernel in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/


Re: [PATCH] bpf: Call rcu_read_unlock() before copy_to_user()

2015-01-22 Thread Alexei Starovoitov
On Thu, Jan 22, 2015 at 7:57 AM, Michael Holzheu
holz...@linux.vnet.ibm.com wrote:
 We must not hold locks when calling copy_to_user():

 BUG: sleeping function called from invalid context at mm/memory.c:3732
 in_atomic(): 0, irqs_disabled(): 0, pid: 671, name: test_maps
 1 lock held by test_maps/671:
  #0:  (rcu_read_lock){..}, at: [00264190] 
 map_lookup_elem+0xe8/0x260
 Preemption disabled at:[001be3b6] vprintk_default+0x56/0x68

 CPU: 0 PID: 671 Comm: test_maps Not tainted 3.19.0-rc5-00117-g5eb11d6-dirty 
 #424
1e447bb0 1e447c40 0002 
1e447ce0 1e447c58 1e447c58 00115c8a
 00c08246 00c27e8a 000b
1e447ca0 1e447c40  
 00115c8a 1e447c40 1e447ca0
 Call Trace:
 ([00115b7e] show_trace+0x12e/0x150)
  [00115c40] show_stack+0xa0/0x100
  [009b163c] dump_stack+0x74/0xc8
  [0017424a] ___might_sleep+0x23a/0x248
  [002b58e8] might_fault+0x70/0xe8
  [00264230] map_lookup_elem+0x188/0x260
  [00264716] SyS_bpf+0x20e/0x840
  [009bbe3a] system_call+0xd6/0x24c
  [03fffd15f566] 0x3fffd15f566
 1 lock held by test_maps/671:
  #0:  (rcu_read_lock){..}, at: [00264190] 
 map_lookup_elem+0xe8/0x260

 So call rcu_read_unlock() before copy_to_user(). We can
 release the lock earlier because it is not needed for copy_to_user().

we cannot move the rcu unlock this way, since it protects the value.
So we need to copy the value while still under rcu.
I'm puzzled how I missed this warning.
I guess you have CONFIG_PREEMPT_RCU=y ?
and if (in_atomic()) return; as part of might_fault() hid it.
--
To unsubscribe from this list: send the line unsubscribe linux-kernel in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/