Netdev List
 help / color / mirror / Atom feed
From: Yonghong Song <yhs@fb.com>
To: Alexei Starovoitov <ast@fb.com>, <daniel@iogearbox.net>,
	<netdev@vger.kernel.org>
Cc: <kernel-team@fb.com>
Subject: Re: [PATCH bpf] bpf/tracing: fix a deadlock in perf_event_detach_bpf_prog
Date: Mon, 9 Apr 2018 11:41:18 -0700	[thread overview]
Message-ID: <a2e305b4-760f-6e19-6043-81d1a1f85e48@fb.com> (raw)
In-Reply-To: <88effd87-1356-38b4-96e5-b90ab869b2a8@fb.com>



On 4/9/18 9:47 AM, Alexei Starovoitov wrote:
> On 4/9/18 9:18 AM, Yonghong Song wrote:
>> syzbot reported a possible deadlock in perf_event_detach_bpf_prog.
> ...
>> @@ -985,16 +986,31 @@ int perf_event_query_prog_array(struct 
>> perf_event *event, void __user *info)
>>          return -EINVAL;
>>      if (copy_from_user(&query, uquery, sizeof(query)))
>>          return -EFAULT;
>> -    if (query.ids_len > BPF_TRACE_MAX_PROGS)
>> +
>> +    ids_len = query.ids_len;
>> +    if (ids_len > BPF_TRACE_MAX_PROGS)
>>          return -E2BIG;
>> +    ids = kcalloc(ids_len, sizeof(u32), GFP_USER | __GFP_NOWARN);
>> +    if (!ids)
>> +        return -ENOMEM;
>>
>>      mutex_lock(&bpf_event_mutex);
>>      ret = bpf_prog_array_copy_info(event->tp_event->prog_array,
>> -                       uquery->ids,
>> -                       query.ids_len,
>> -                       &uquery->prog_cnt);
>> +                       ids,
>> +                       ids_len,
>> +                       &prog_cnt);
>>      mutex_unlock(&bpf_event_mutex);
>>
>> +    if (!ret || ret == -ENOSPC) {
>> +        if (copy_to_user(&uquery->prog_cnt, &prog_cnt, 
>> sizeof(prog_cnt)) ||
>> +            copy_to_user(uquery->ids, ids, ids_len * sizeof(u32))) {
>> +            ret = -EFAULT;
>> +            goto out;
>> +        }
>> +    }
>> +
>> +out:
>> +    kfree(ids);
> 
> alloc/free just to avoid this locking dependency feels suboptimal.

We actually already did kcalloc/kfree in bpf_prog_array_copy_to_user.
In that function, we did not copy_to_user one id at a time.
We allocate a temporary array and store the result there
and at the end, we call one copy_to_user to copy to the user buffer.

The patch here just moved this allocation and associated copy_to_user
out of the function and bpf_event_mutex. It did not introduce new
allocations.

> 
> may be we can get rid of bpf_event_mutex in some cases?
> the perf event itself is locked via perf_event_ctx_lock() when we're
> calling perf_event_query_prog_array, perf_event_attach|detach_bpf_prog.
> I forgot what was the motivation for us to introduce it in the
> first place.

The original motivation for the lock to make sure bpf_prog_array
does not change during middle of attach/detach/query. it looks like
we have:
    . perf_event_attach|query under perf_event_ctx_lock
    . perf_event_detach not under perf_event_ctx_lock
Introducing perf_event_ctx_lock to perf_event_detach could still
have the deadlock.

> 

  reply	other threads:[~2018-04-09 18:42 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-04-09 16:18 [PATCH bpf] bpf/tracing: fix a deadlock in perf_event_detach_bpf_prog Yonghong Song
2018-04-09 16:47 ` Alexei Starovoitov
2018-04-09 18:41   ` Yonghong Song [this message]
2018-04-10  0:51     ` Alexei Starovoitov

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=a2e305b4-760f-6e19-6043-81d1a1f85e48@fb.com \
    --to=yhs@fb.com \
    --cc=ast@fb.com \
    --cc=daniel@iogearbox.net \
    --cc=kernel-team@fb.com \
    --cc=netdev@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox