Skip to content

Commit

Permalink
do not use BPF_F_REUSE_STACKID incorrectly
Browse files Browse the repository at this point in the history
Do not use BPF_F_REUSE_STACKID if the stack id is used
together with process specific info like
pid/tgid/comm. Using BPF_F_REUSE_STACKID may cause
stack id pointing to a different stack later on.

Signed-off-by: Yonghong Song <yhs@fb.com>
  • Loading branch information
yonghong-song committed Nov 27, 2019
1 parent a1a04f4 commit 90f2086
Show file tree
Hide file tree
Showing 7 changed files with 11 additions and 11 deletions.
4 changes: 2 additions & 2 deletions tools/deadlock.c
Expand Up @@ -95,7 +95,7 @@ int trace_mutex_acquire(struct pt_regs *ctx, void *mutex_addr) {
}

u64 stack_id =
stack_traces.get_stackid(ctx, BPF_F_USER_STACK | BPF_F_REUSE_STACKID);
stack_traces.get_stackid(ctx, BPF_F_USER_STACK);

int added_mutex = 0;
#pragma unroll
Expand Down Expand Up @@ -190,7 +190,7 @@ int trace_clone(struct pt_regs *ctx, unsigned long flags, void *child_stack,
struct thread_created_leaf_t thread_created_leaf = {};
thread_created_leaf.parent_pid = bpf_get_current_pid_tgid();
thread_created_leaf.stack_id =
stack_traces.get_stackid(ctx, BPF_F_USER_STACK | BPF_F_REUSE_STACKID);
stack_traces.get_stackid(ctx, BPF_F_USER_STACK);
bpf_get_current_comm(&thread_created_leaf.comm,
sizeof(thread_created_leaf.comm));

Expand Down
2 changes: 1 addition & 1 deletion tools/memleak.py
Expand Up @@ -399,7 +399,7 @@ def run_command_get_pid(command):
size_filter = "if (size > %d) return 0;" % max_size
bpf_source = bpf_source.replace("SIZE_FILTER", size_filter)

stack_flags = "BPF_F_REUSE_STACKID"
stack_flags = "0"
if not kernel_trace:
stack_flags += "|BPF_F_USER_STACK"
bpf_source = bpf_source.replace("STACK_FLAGS", stack_flags)
Expand Down
4 changes: 2 additions & 2 deletions tools/old/profile.py
Expand Up @@ -209,9 +209,9 @@ def positive_nonzero_int(val):

# handle stack args
kernel_stack_get = "stack_traces.get_stackid(args, " \
"%d | BPF_F_REUSE_STACKID)" % skip
"%d)" % skip
user_stack_get = \
"stack_traces.get_stackid(args, BPF_F_REUSE_STACKID | BPF_F_USER_STACK)"
"stack_traces.get_stackid(args, BPF_F_USER_STACK)"
stack_context = ""
if args.user_stacks_only:
stack_context = "user"
Expand Down
4 changes: 2 additions & 2 deletions tools/stackcount.py
Expand Up @@ -110,14 +110,14 @@ def load(self):
if self.user_stack:
stack_trace += """
key.user_stack_id = stack_traces.get_stackid(
%s, BPF_F_REUSE_STACKID | BPF_F_USER_STACK
%s, BPF_F_USER_STACK
);""" % (ctx_name)
else:
stack_trace += "key.user_stack_id = -1;"
if self.kernel_stack:
stack_trace += """
key.kernel_stack_id = stack_traces.get_stackid(
%s, BPF_F_REUSE_STACKID
%s, 0
);""" % (ctx_name)
else:
stack_trace += "key.kernel_stack_id = -1;"
Expand Down
2 changes: 1 addition & 1 deletion tools/stacksnoop.lua
Expand Up @@ -32,7 +32,7 @@ void trace_stack(struct pt_regs *ctx) {
u32 pid = bpf_get_current_pid_tgid();
FILTER
struct data_t data = {};
data.stack_id = stack_traces.get_stackid(ctx, BPF_F_REUSE_STACKID),
data.stack_id = stack_traces.get_stackid(ctx, 0),
data.pid = pid;
bpf_get_current_comm(&data.comm, sizeof(data.comm));
events.perf_submit(ctx, &data, sizeof(data));
Expand Down
4 changes: 2 additions & 2 deletions tools/trace.py
Expand Up @@ -487,12 +487,12 @@ def generate_program(self, include_self):
if self.user_stack:
stack_trace += """
__data.user_stack_id = %s.get_stackid(
%s, BPF_F_REUSE_STACKID | BPF_F_USER_STACK
%s, BPF_F_USER_STACK
);""" % (self.stacks_name, ctx_name)
if self.kernel_stack:
stack_trace += """
__data.kernel_stack_id = %s.get_stackid(
%s, BPF_F_REUSE_STACKID
%s, 0
);""" % (self.stacks_name, ctx_name)

text = heading + """
Expand Down
2 changes: 1 addition & 1 deletion tools/wakeuptime.py
Expand Up @@ -135,7 +135,7 @@ def signal_ignore(signal, frame):
struct key_t key = {};
key.w_k_stack_id = stack_traces.get_stackid(ctx, BPF_F_REUSE_STACKID);
key.w_k_stack_id = stack_traces.get_stackid(ctx, 0);
bpf_probe_read(&key.target, sizeof(key.target), p->comm);
bpf_get_current_comm(&key.waker, sizeof(key.waker));
Expand Down

0 comments on commit 90f2086

Please sign in to comment.