From: Peter Zijlstra <peterz@infradead.org>
To: Alexei Starovoitov <ast@fb.com>
Cc: "David S. Miller" <davem@davemloft.net>,
Ingo Molnar <mingo@kernel.org>,
Steven Rostedt <rostedt@goodmis.org>,
Wang Nan <wangnan0@huawei.com>,
Daniel Borkmann <daniel@iogearbox.net>,
Brendan Gregg <brendan.d.gregg@gmail.com>,
netdev@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH net-next 2/3] bpf: introduce BPF_MAP_TYPE_STACK_TRACE
Date: Thu, 25 Feb 2016 15:23:28 +0100 [thread overview]
Message-ID: <20160225142328.GN6357@twins.programming.kicks-ass.net> (raw)
In-Reply-To: <1455767939-2700534-3-git-send-email-ast@fb.com>
On Wed, Feb 17, 2016 at 07:58:58PM -0800, Alexei Starovoitov wrote:
> +static u64 bpf_get_stackid(u64 r1, u64 r2, u64 flags, u64 r4, u64 r5)
> +{
> + struct pt_regs *regs = (struct pt_regs *) (long) r1;
> + struct bpf_map *map = (struct bpf_map *) (long) r2;
> + struct bpf_stack_map *smap = container_of(map, struct bpf_stack_map, map);
> + struct perf_callchain_entry *trace;
> + struct stack_map_bucket *bucket, *new_bucket, *old_bucket;
> + u32 max_depth = map->value_size / 8;
> + /* stack_map_alloc() checks that max_depth <= PERF_MAX_STACK_DEPTH */
> + u32 init_nr = PERF_MAX_STACK_DEPTH - max_depth;
> + u32 skip = flags & BPF_F_SKIP_FIELD_MASK;
> + u32 hash, id, trace_nr, trace_len;
> + bool user = flags & BPF_F_USER_STACK;
> + bool kernel = !user;
> + u64 *ips;
> +
> + if (unlikely(flags & ~(BPF_F_SKIP_FIELD_MASK | BPF_F_USER_STACK |
> + BPF_F_FAST_STACK_CMP | BPF_F_REUSE_STACKID)))
> + return -EINVAL;
> +
> + trace = get_perf_callchain(regs, init_nr, kernel, user, false, false);
> +
> + if (unlikely(!trace))
> + /* couldn't fetch the stack trace */
> + return -EFAULT;
> +
> + /* get_perf_callchain() guarantees that trace->nr >= init_nr
> + * and trace-nr <= PERF_MAX_STACK_DEPTH, so trace_nr <= max_depth
> + */
> + trace_nr = trace->nr - init_nr;
> +
> + if (trace_nr <= skip)
> + /* skipping more than usable stack trace */
> + return -EFAULT;
> +
> + trace_nr -= skip;
> + trace_len = trace_nr * sizeof(u64);
> + ips = trace->ip + skip + init_nr;
> + hash = jhash2((u32 *)ips, trace_len / sizeof(u32), 0);
> + id = hash & (smap->n_buckets - 1);
Its not at all clear where the corresponding rcu_read_lock() is at.
> + bucket = rcu_dereference(smap->buckets[id]);
> +
> + if (bucket && bucket->hash == hash) {
> + if (flags & BPF_F_FAST_STACK_CMP)
> + return id;
> + if (bucket->nr == trace_nr &&
> + memcmp(bucket->ip, ips, trace_len) == 0)
> + return id;
> + }
> +
> + /* this call stack is not in the map, try to add it */
> + if (bucket && !(flags & BPF_F_REUSE_STACKID))
> + return -EEXIST;
> +
> + new_bucket = kmalloc(sizeof(struct stack_map_bucket) + map->value_size,
> + GFP_ATOMIC | __GFP_NOWARN);
> + if (unlikely(!new_bucket))
> + return -ENOMEM;
> +
> + memcpy(new_bucket->ip, ips, trace_len);
> + memset(new_bucket->ip + trace_len / 8, 0, map->value_size - trace_len);
> + new_bucket->hash = hash;
> + new_bucket->nr = trace_nr;
> +
> + old_bucket = xchg(&smap->buckets[id], new_bucket);
> + if (old_bucket)
> + kfree_rcu(old_bucket, rcu);
> + return id;
> +}
next prev parent reply other threads:[~2016-02-25 14:23 UTC|newest]
Thread overview: 16+ messages / expand[flat|nested] mbox.gz Atom feed top
2016-02-18 3:58 [PATCH net-next 0/3] bpf_get_stackid() and stack_trace map Alexei Starovoitov
2016-02-18 3:58 ` [PATCH net-next 1/3] perf: generalize perf_callchain Alexei Starovoitov
2016-02-25 14:18 ` Peter Zijlstra
2016-02-25 16:37 ` Alexei Starovoitov
2016-02-25 16:45 ` Peter Zijlstra
2016-02-25 16:48 ` Peter Zijlstra
2016-02-25 16:47 ` Peter Zijlstra
2016-02-25 17:27 ` Alexei Starovoitov
2016-02-18 3:58 ` [PATCH net-next 2/3] bpf: introduce BPF_MAP_TYPE_STACK_TRACE Alexei Starovoitov
2016-02-25 14:23 ` Peter Zijlstra [this message]
2016-02-25 16:42 ` Alexei Starovoitov
2016-02-25 16:50 ` Peter Zijlstra
2016-02-18 3:58 ` [PATCH net-next 3/3] samples/bpf: offwaketime example Alexei Starovoitov
2016-02-20 5:25 ` [PATCH net-next 0/3] bpf_get_stackid() and stack_trace map David Miller
2016-02-25 14:24 ` Peter Zijlstra
2016-02-25 16:44 ` David Miller
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=20160225142328.GN6357@twins.programming.kicks-ass.net \
--to=peterz@infradead.org \
--cc=ast@fb.com \
--cc=brendan.d.gregg@gmail.com \
--cc=daniel@iogearbox.net \
--cc=davem@davemloft.net \
--cc=linux-kernel@vger.kernel.org \
--cc=mingo@kernel.org \
--cc=netdev@vger.kernel.org \
--cc=rostedt@goodmis.org \
--cc=wangnan0@huawei.com \
/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
all inboxes | Powered by JetHome®