GCC's -Wall includes -Wsign-compare while clang does not. Since BPF programs are built with clang we need to add this flag explicitly to catch problematic comparisons like: int i = -1; unsigned int j = 1; if (i < j) // this is false. long i = -1; unsigned int j = 1; if (i < j) // this is true. C standard for reference: - If either operand is unsigned long the other shall be converted to unsigned long. - Otherwise, if one operand is a long int and the other unsigned int, then if a long int can represent all the values of an unsigned int, the unsigned int shall be converted to a long int; otherwise both operands shall be converted to unsigned long int. - Otherwise, if either operand is long, the other shall be converted to long. - Otherwise, if either operand is unsigned, the other shall be converted to unsigned. Unfortunately clang's -Wsign-compare is very noisy. It complains about (s32)a == (u32)b which is safe and doen't have surprising behavior. This patch fixes some of the issues. It needs a follow up to fix the rest. Signed-off-by: Alexei Starovoitov <ast@kernel.org> Signed-off-by: Andrii Nakryiko <andrii@kernel.org> Acked-by: Jiri Olsa <jolsa@kernel.org> Acked-by: Kumar Kartikeya Dwivedi <memxor@gmail.com> Link: https://lore.kernel.org/bpf/20231226191148.48536-2-alexei.starovoitov@gmail.com
89 lines
2.0 KiB
C
89 lines
2.0 KiB
C
// SPDX-License-Identifier: GPL-2.0
|
|
/* Copyright (c) 2020 Facebook */
|
|
#include "bpf_iter.h"
|
|
#include <bpf/bpf_helpers.h>
|
|
#include <bpf/bpf_tracing.h>
|
|
|
|
char _license[] SEC("license") = "GPL";
|
|
|
|
uint32_t tid = 0;
|
|
int num_unknown_tid = 0;
|
|
int num_known_tid = 0;
|
|
|
|
SEC("iter/task")
|
|
int dump_task(struct bpf_iter__task *ctx)
|
|
{
|
|
struct seq_file *seq = ctx->meta->seq;
|
|
struct task_struct *task = ctx->task;
|
|
static char info[] = " === END ===";
|
|
|
|
if (task == (void *)0) {
|
|
BPF_SEQ_PRINTF(seq, "%s\n", info);
|
|
return 0;
|
|
}
|
|
|
|
if (task->pid != (pid_t)tid)
|
|
num_unknown_tid++;
|
|
else
|
|
num_known_tid++;
|
|
|
|
if (ctx->meta->seq_num == 0)
|
|
BPF_SEQ_PRINTF(seq, " tgid gid\n");
|
|
|
|
BPF_SEQ_PRINTF(seq, "%8d %8d\n", task->tgid, task->pid);
|
|
return 0;
|
|
}
|
|
|
|
int num_expected_failure_copy_from_user_task = 0;
|
|
int num_success_copy_from_user_task = 0;
|
|
|
|
SEC("iter.s/task")
|
|
int dump_task_sleepable(struct bpf_iter__task *ctx)
|
|
{
|
|
struct seq_file *seq = ctx->meta->seq;
|
|
struct task_struct *task = ctx->task;
|
|
static const char info[] = " === END ===";
|
|
struct pt_regs *regs;
|
|
void *ptr;
|
|
uint32_t user_data = 0;
|
|
int ret;
|
|
|
|
if (task == (void *)0) {
|
|
BPF_SEQ_PRINTF(seq, "%s\n", info);
|
|
return 0;
|
|
}
|
|
|
|
/* Read an invalid pointer and ensure we get an error */
|
|
ptr = NULL;
|
|
ret = bpf_copy_from_user_task(&user_data, sizeof(uint32_t), ptr, task, 0);
|
|
if (ret) {
|
|
++num_expected_failure_copy_from_user_task;
|
|
} else {
|
|
BPF_SEQ_PRINTF(seq, "%s\n", info);
|
|
return 0;
|
|
}
|
|
|
|
/* Try to read the contents of the task's instruction pointer from the
|
|
* remote task's address space.
|
|
*/
|
|
regs = (struct pt_regs *)bpf_task_pt_regs(task);
|
|
if (regs == (void *)0) {
|
|
BPF_SEQ_PRINTF(seq, "%s\n", info);
|
|
return 0;
|
|
}
|
|
ptr = (void *)PT_REGS_IP(regs);
|
|
|
|
ret = bpf_copy_from_user_task(&user_data, sizeof(uint32_t), ptr, task, 0);
|
|
if (ret) {
|
|
BPF_SEQ_PRINTF(seq, "%s\n", info);
|
|
return 0;
|
|
}
|
|
++num_success_copy_from_user_task;
|
|
|
|
if (ctx->meta->seq_num == 0)
|
|
BPF_SEQ_PRINTF(seq, " tgid gid data\n");
|
|
|
|
BPF_SEQ_PRINTF(seq, "%8d %8d %8d\n", task->tgid, task->pid, user_data);
|
|
return 0;
|
|
}
|