tools/lib/perf: use TASK_COMM_LEN_16 instead of hard-coded 16
Subsystems (9)
PATCH v5 10/15 tools/lib/perf: use TASK_COMM_LEN_16 instead of hard-coded 16
Use TASK_COMM_LEN_16 instead of hard-coded 16 to make it more grepable. The comm is set in perf_event__prepare_comm(), which makes the comm always a nul terminated string, so we don't worry about whether it will be truncated or not.
--- tools/lib/perf/include/perf/event.h | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)
tools/lib/perf/include/perf/event.h +3 -2
@@ -6,6 +6,7 @@#include <linux/types.h> #include <linux/limits.h> #include <linux/bpf.h> +#include <linux/sched/task.h> #include <sys/types.h> /* pid_t */ #define event_contains (obj, mem) ((obj).header.size > offsetof(typeof(obj), mem))
@@ -47,7 +48,7 @@ struct perf_record_mmap2 {struct perf_record_comm {struct perf_event_header header;__u32 pid, tid; -char comm[16 ]; +char comm[TASK_COMM_LEN_16 ]; };struct perf_record_namespaces {
@@ -291,7 +292,7 @@ struct perf_record_itrace_start {struct perf_record_thread_map_entry {__u64 pid; -char comm[16 ]; +char comm[TASK_COMM_LEN_16 ]; };struct perf_record_thread_map {
Signature · 1 line
2.17.1
PATCH v5 12/15 tools/perf/test: make perf test adopt to task comm size change
kernel test robot reported a perf-test failure after I extended task comm size from 16 to 24. The failure as follows,
2021-10-13 18:00:46 sudo /usr/src/perf_selftests-x86_64-rhel-8.3-317419b91ef4eff4e2f046088201e4dc4065caa0/tools/perf/perf test 15
15: Parse sched tracepoints fields : FAILED!
The reason is perf-test requires a fixed-size task comm. If we extend task comm size to 24, it will not equil with the required size 16 in perf test.
After some analyzation, I found perf itself can adopt to the size change, for example, below is the output of perf-sched after I extend comm size to 24 -
task 614 ( kthreadd: 84), nr_events: 1 task 615 ( systemd: 843), nr_events: 1 task 616 ( networkd-dispat: 1026), nr_events: 1 task 617 ( systemd: 846), nr_events: 1
$ cat /proc/843/comm networkd-dispatcher
The task comm can be displayed correctly as expected.
Now that task comm can have a different size, then the test should accept the two sizes as possible and pass if it is 16 or 24. In order to do that, a new macro TASK_COMM_LEN_24 is introduced.
After this patch, the perf-test succeeds no matter task comm is 16 or 24 -
15: Parse sched tracepoints fields : Ok
This patch is a preparation for the followup patch.
--- tools/include/linux/sched/task.h | 1 + tools/perf/tests/evsel-tp-sched.c | 26 ++++++++++++++++++++------ 2 files changed, 21 insertions(+), 6 deletions(-)
tools/include/linux/sched/task.h +1 -0
@@ -2,5 +2,6 @@#define _TOOLS_PERF_LINUX_SCHED_TASK_H#define TASK_COMM_LEN_16 16 +#define TASK_COMM_LEN_24 24#endif /* _TOOLS_PERF_LINUX_SCHED_TASK_H */
tools/perf/tests/evsel-tp-sched.c +20 -6
@@ -1,11 +1,13 @@// SPDX-License-Identifier: GPL-2.0 #include <linux/err.h> +#include <linux/sched/task.h> #include <traceevent/event-parse.h> #include "evsel.h" #include "tests.h" #include "debug.h" -static int evsel__test_field (struct evsel *evsel,const char *name,int size,bool should_be_signed) +static int evsel__test_field_alt (struct evsel *evsel,const char *name, +int size,int alternate_size,bool should_be_signed) {struct tep_format_field *field =evsel__field (evsel, name);int is_signed;
@@ -23,15 +25,24 @@ static int evsel__test_field(struct evsel *evsel, const char *name, int size, bo ret =-1 ; } -if (field->size != size) { -pr_debug ("%s: \"%s\" size (%d) should be %d!\n" , +if (field->size != size && field->size != alternate_size) { +pr_debug ("%s: \"%s\" size (%d) should be %d" , evsel->name, name, field->size, size); +if (alternate_size >0 ) +pr_debug (" or %d" , alternate_size); +pr_debug ("!\n" ); ret =-1 ; }return ret; } +static int evsel__test_field (struct evsel *evsel,const char *name, +int size,bool should_be_signed) +{ +return evsel__test_field_alt (evsel, name, size,-1 , should_be_signed); +} +int test__perf_evsel__tp_sched_test (struct test *test__maybe_unused ,int subtest__maybe_unused ) {struct evsel *evsel =evsel__newtp ("sched" ,"sched_switch" );
@@ -42,7 +53,8 @@ int test__perf_evsel__tp_sched_test(struct test *test __maybe_unused, int subtesreturn -1 ; } -if (evsel__test_field (evsel,"prev_comm" ,16 , false)) +if (evsel__test_field_alt (evsel,"prev_comm" ,TASK_COMM_LEN_16 , +TASK_COMM_LEN_24 , false)) ret =-1 ;if (evsel__test_field (evsel,"prev_pid" ,4 , true))
@@ -54,7 +66,8 @@ int test__perf_evsel__tp_sched_test(struct test *test __maybe_unused, int subtesif (evsel__test_field (evsel,"prev_state" ,sizeof (long ), true)) ret =-1 ; -if (evsel__test_field (evsel,"next_comm" ,16 , false)) +if (evsel__test_field_alt (evsel,"next_comm" ,TASK_COMM_LEN_16 , +TASK_COMM_LEN_24 , false)) ret =-1 ;if (evsel__test_field (evsel,"next_pid" ,4 , true))
@@ -72,7 +85,8 @@ int test__perf_evsel__tp_sched_test(struct test *test __maybe_unused, int subtesreturn -1 ; } -if (evsel__test_field (evsel,"comm" ,16 , false)) +if (evsel__test_field_alt (evsel,"comm" ,TASK_COMM_LEN_16 , +TASK_COMM_LEN_24 , false)) ret =-1 ;if (evsel__test_field (evsel,"pid" ,4 , true))
Signature · 1 line
2.17.1
PATCH v5 11/15 tools/bpf/bpftool: use TASK_COMM_LEN_16 instead of hard-coded 16
In reply to Yafang ShaoUse TASK_COMM_LEN_16 instead of hard-coded 16 to make it more grepable. It uses bpf_probe_read_kernel() to get task comm, which may return a string without nul terminator. We should use bpf_probe_read_kernel_str() instead.
--- tools/bpf/bpftool/Makefile | 1 + tools/bpf/bpftool/main.h | 3 ++- tools/bpf/bpftool/skeleton/pid_iter.bpf.c | 4 ++-- tools/bpf/bpftool/skeleton/pid_iter.h | 4 +++- 4 files changed, 8 insertions(+), 4 deletions(-)
tools/bpf/bpftool/Makefile +1 -0
@@ -164,6 +164,7 @@ $(OUTPUT)%.bpf.o: skeleton/%.bpf.c $(OUTPUT)vmlinux.h $(LIBBPF) $(QUIET_CLANG)$(CLANG) \ -I$(if $(OUTPUT),$(OUTPUT),.) \ -I$(srctree)/tools/include/uapi/ \ + -I$(srctree)/tools/include/ \ -I$(LIBBPF_PATH) \ -I$(srctree)/tools/lib \ -g -O2 -Wall -target bpf -c $< -o $@ && $(LLVM_STRIP) -g $@
tools/bpf/bpftool/main.h +2 -1
@@ -12,6 +12,7 @@#include <linux/compiler.h> #include <linux/kernel.h> #include <linux/hashtable.h> +#include <linux/sched/task.h> #include <tools/libc_compat.h> #include <bpf/libbpf.h>
@@ -124,7 +125,7 @@ struct obj_refs_table {struct obj_ref {int pid; -char comm[16 ]; +char comm[TASK_COMM_LEN_16 ]; };struct obj_refs {
tools/bpf/bpftool/skeleton/pid_iter.bpf.c +2 -2
@@ -71,8 +71,8 @@ int iter(struct bpf_iter__task_file *ctx) e.pid = task->tgid; e.id =get_obj_id (file->private_data, obj_type); -bpf_probe_read_kernel (&e.comm,sizeof (e.comm), - task->group_leader->comm); +bpf_probe_read_kernel_str (&e.comm,sizeof (e.comm), + task->group_leader->comm);bpf_seq_write (ctx->meta->seq, &e,sizeof (e));return 0 ;
tools/bpf/bpftool/skeleton/pid_iter.h +3 -1
@@ -3,10 +3,12 @@#ifndef __PID_ITER_H#define __PID_ITER_H +#include <linux/sched/task.h> +struct pid_iter_entry {__u32 id;int pid; -char comm[16 ]; +char comm[TASK_COMM_LEN_16 ]; };#endif
Signature · 1 line
2.17.1
PATCH v5 14/15 sched.h: extend task comm from 16 to 24 for CONFIG_BASE_FULL
In reply to Yafang ShaoWhen I was implementing a new per-cpu kthread cfs_migration, I found the comm of it "cfs_migration/%u" is truncated due to the limitation of TASK_COMM_LEN. For example, the comm of the percpu thread on CPU10~19 are all with the same name "cfs_migration/1", which will confuse the user. This issue is not critical, because we can get the corresponding CPU from the task's Cpus_allowed. But for kthreads correspoinding to other hardware devices, it is not easy to get the detailed device info from task comm, for example,
jbd2/nvme0n1p2-
xfs-reclaim/sdfWe can also shorten the name to work around this problem, but I find there are so many truncated kthreads:
rcu_tasks_kthre
rcu_tasks_rude_
rcu_tasks_trace
poll_mpt3sas0_s
ext4-rsv-conver
xfs-reclaim/sd{a, b, c, ...}
xfs-blockgc/sd{a, b, c, ...}
xfs-inodegc/sd{a, b, c, ...}
audit_send_repl
ecryptfs-kthrea
vfio-irqfd-clea
jbd2/nvme0n1p2-
...We should improve this problem fundamentally.
This patch extends the size of task comm to 24 bytes, which is the same length with workqueue's, for the CONFIG_BASE_FULL case. And for the CONFIG_BASE_SMALL case, the size of task comm is still kept as 16 bytes.
After this patchset, the truncated kthreads listed above will be displayed as:
rcu_tasks_kthread
rcu_tasks_rude_kthread
rcu_tasks_trace_kthread
poll_mpt3sas0_statu
ext4-rsv-conversion
xfs-reclaim/sdf1
xfs-blockgc/sdf1
xfs-inodegc/sdf1
audit_send_reply
ecryptfs-kthread
vfio-irqfd-cleanup
jbd2/nvme0n1p2-8--- include/linux/sched.h | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-)
include/linux/sched.h +10 -3
@@ -274,10 +274,17 @@ struct task_group;#define get_current_state () READ_ONCE(current->__state) -/* To replace the old hard-coded 16 */ -#define TASK_COMM_LEN_16 16 +/* Also to replace the old hard-coded 16 */ +#define TASK_COMM_LEN_16 16 +#define TASK_COMM_LEN_24 24 + +/* Task command name length: */ -#define TASK_COMM_LEN 16 +#if CONFIG_BASE_SMALL +#define TASK_COMM_LEN TASK_COMM_LEN_16 +#else +#define TASK_COMM_LEN TASK_COMM_LEN_24 +#endif extern void scheduler_tick (void );
Signature · 1 line
2.17.1
PATCH v5 13/15 tools/testing/selftests/bpf: use TASK_COMM_LEN_16 instead of hard-coded 16
In reply to Yafang ShaoThe hard-coded 16 is used in various bpf progs. These progs get task comm either via bpf_get_current_comm() or prctl() or bpf_core_read_str(), all of which can work well even if the task comm size is changed.
Below is the detailed information,
bpf_get_current_comm:
progs/test_ringbuf.c
progs/test_ringbuf_multi.c
prctl:
prog_tests/test_overhead.c
prog_tests/trampoline_count.c
bpf_core_read_str:
progs/test_core_reloc_kernel.c
progs/test_sk_storage_tracing.c
We'd better replace the hard-coded 16 with TASK_COMM_LEN_16 to make it more grepable.
--- tools/testing/selftests/bpf/Makefile | 2 +- tools/testing/selftests/bpf/prog_tests/ringbuf.c | 3 ++- tools/testing/selftests/bpf/prog_tests/ringbuf_multi.c | 3 ++- .../testing/selftests/bpf/prog_tests/sk_storage_tracing.c | 3 ++- tools/testing/selftests/bpf/prog_tests/test_overhead.c | 3 ++- tools/testing/selftests/bpf/prog_tests/trampoline_count.c | 3 ++- tools/testing/selftests/bpf/progs/profiler.h | 7 ++++--- tools/testing/selftests/bpf/progs/profiler.inc.h | 8 ++++---- tools/testing/selftests/bpf/progs/pyperf.h | 4 ++-- tools/testing/selftests/bpf/progs/strobemeta.h | 6 +++--- .../testing/selftests/bpf/progs/test_core_reloc_kernel.c | 3 ++- tools/testing/selftests/bpf/progs/test_ringbuf.c | 3 ++- tools/testing/selftests/bpf/progs/test_ringbuf_multi.c | 3 ++- .../testing/selftests/bpf/progs/test_sk_storage_tracing.c | 5 +++-- tools/testing/selftests/bpf/progs/test_skb_helpers.c | 5 ++--- tools/testing/selftests/bpf/progs/test_stacktrace_map.c | 5 +++-- tools/testing/selftests/bpf/progs/test_tracepoint.c | 5 +++-- 17 files changed, 41 insertions(+), 30 deletions(-)
tools/testing/selftests/bpf/Makefile +1 -1
@@ -279,7 +279,7 @@ MENDIAN=$(if $(IS_LITTLE_ENDIAN),-mlittle-endian,-mbig-endian) CLANG_SYS_INCLUDES = $(call get_sys_includes,$(CLANG)) BPF_CFLAGS = -g -D__TARGET_ARCH_$(SRCARCH) $(MENDIAN) \ - -I$(INCLUDE_DIR) -I$(CURDIR) -I$(APIDIR) \ + -I$(INCLUDE_DIR) -I$(CURDIR) -I$(APIDIR) -I${TOOLSINCDIR} \ -I$(abspath $(OUTPUT)/../usr/include) CLANG_CFLAGS = $(CLANG_SYS_INCLUDES) \
tools/testing/selftests/bpf/prog_tests/ringbuf.c +2 -1
@@ -12,6 +12,7 @@#include <sys/sysinfo.h> #include <linux/perf_event.h> #include <linux/ring_buffer.h> +#include <linux/sched/task.h> #include "test_ringbuf.lskel.h" #define EDONE 7777
@@ -22,7 +23,7 @@ struct sample {int pid;int seq;long value; -char comm[16 ]; +char comm[TASK_COMM_LEN_16 ]; };static int sample_cnt;
tools/testing/selftests/bpf/prog_tests/ringbuf_multi.c +2 -1
@@ -2,6 +2,7 @@#define _GNU_SOURCE#include <test_progs.h> #include <sys/epoll.h> +#include <linux/sched/task.h> #include "test_ringbuf_multi.skel.h" static int duration =0 ;
@@ -10,7 +11,7 @@ struct sample {int pid;int seq;long value; -char comm[16 ]; +char comm[TASK_COMM_LEN_16 ]; };static int process_sample (void *ctx,void *data,size_t len)
tools/testing/selftests/bpf/prog_tests/sk_storage_tracing.c +2 -1
@@ -4,6 +4,7 @@#include <sys/types.h> #include <bpf/bpf.h> #include <bpf/libbpf.h> +#include <linux/sched/task.h> #include "test_progs.h" #include "network_helpers.h" #include "test_sk_storage_trace_itself.skel.h"
@@ -15,7 +16,7 @@struct sk_stg {__u32 pid;__u32 last_notclose_state; -char comm[16 ]; +char comm[TASK_COMM_LEN_16 ]; };static struct test_sk_storage_tracing *skel;
tools/testing/selftests/bpf/prog_tests/test_overhead.c +2 -1
@@ -4,6 +4,7 @@#include <sched.h> #include <sys/prctl.h> #include <test_progs.h> +#include <linux/sched/task.h> #define MAX_CNT 100000
@@ -67,7 +68,7 @@ void test_test_overhead(void)struct bpf_object *obj;struct bpf_link *link;int err, duration =0 ; -char comm[16 ] = {}; +char comm[TASK_COMM_LEN_16 ] = {};if (CHECK_FAIL (prctl (PR_GET_NAME , comm,0L ,0L ,0L )))return ;
tools/testing/selftests/bpf/prog_tests/trampoline_count.c +2 -1
@@ -3,6 +3,7 @@#include <sched.h> #include <sys/prctl.h> #include <test_progs.h> +#include <linux/sched/task.h> #define MAX_TRAMP_PROGS 38
@@ -50,7 +51,7 @@ void test_trampoline_count(void)int err, i =0 , duration =0 ;struct bpf_object *obj;struct bpf_link *link; -char comm[16 ] = {}; +char comm[TASK_COMM_LEN_16 ] = {};/* attach 'allowed' trampoline programs */ for (i =0 ; i <MAX_TRAMP_PROGS ; i++) {
tools/testing/selftests/bpf/progs/profiler.h +4 -3
@@ -2,7 +2,8 @@/* Copyright (c) 2020 Facebook */ #pragma once -#define TASK_COMM_LEN 16 +#include <linux/sched/task.h> +#define MAX_ANCESTORS 4#define MAX_PATH 256#define KILL_TARGET_LEN 64
@@ -14,7 +15,7 @@#define MAX_FILEPATH_LENGTH (MAX_PATH_DEPTH * MAX_PATH)#define MAX_CGROUPS_PATH_DEPTH 8 -#define MAX_METADATA_PAYLOAD_LEN TASK_COMM_LEN +#define MAX_METADATA_PAYLOAD_LEN TASK_COMM_LEN_16#define MAX_CGROUP_PAYLOAD_LEN \ (MAX_PATH * 2 + (MAX_PATH * MAX_CGROUPS_PATH_DEPTH))
@@ -25,7 +26,7 @@ (MAX_METADATA_PAYLOAD_LEN + MAX_CGROUP_PAYLOAD_LEN + CTL_MAXNAME + MAX_PATH)#define MAX_KILL_PAYLOAD_LEN \ - (MAX_METADATA_PAYLOAD_LEN + MAX_CGROUP_PAYLOAD_LEN + TASK_COMM_LEN + \ + (MAX_METADATA_PAYLOAD_LEN + MAX_CGROUP_PAYLOAD_LEN + TASK_COMM_LEN_16 + \ KILL_TARGET_LEN)#define MAX_EXEC_PAYLOAD_LEN \
tools/testing/selftests/bpf/progs/profiler.inc.h +4 -4
@@ -344,9 +344,9 @@ static INLINE void* populate_var_metadata(struct var_metadata_t* metadata, metadata->start_time =BPF_CORE_READ (task, start_time); metadata->comm_length =0 ; -size_t comm_length =bpf_core_read_str (payload,TASK_COMM_LEN , &task->comm); +size_t comm_length =bpf_core_read_str (payload,TASK_COMM_LEN_16 , &task->comm);barrier_var (comm_length); -if (comm_length <=TASK_COMM_LEN ) { +if (comm_length <=TASK_COMM_LEN_16 ) {barrier_var (comm_length); metadata->comm_length = comm_length; payload += comm_length;
@@ -648,9 +648,9 @@ int raw_tracepoint__sched_process_exit(void* ctx) kill_data->kill_target_name_length =0 ; kill_data->kill_target_cgroup_proc_length =0 ; -size_t comm_length =bpf_core_read_str (payload,TASK_COMM_LEN , &task->comm); +size_t comm_length =bpf_core_read_str (payload,TASK_COMM_LEN_16 , &task->comm);barrier_var (comm_length); -if (comm_length <=TASK_COMM_LEN ) { +if (comm_length <=TASK_COMM_LEN_16 ) {barrier_var (comm_length); kill_data->kill_target_name_length = comm_length; payload += comm_length;
tools/testing/selftests/bpf/progs/pyperf.h +2 -2
@@ -6,11 +6,11 @@#include <stddef.h> #include <stdbool.h> #include <linux/bpf.h> +#include <linux/sched/task.h> #include <bpf/bpf_helpers.h> #define FUNCTION_NAME_LEN 64#define FILE_NAME_LEN 128 -#define TASK_COMM_LEN 16 typedef struct { int PyThreadState_frame;
@@ -43,7 +43,7 @@ typedef struct { typedef struct { uint32_t pid; uint32_t tid; - char comm[TASK_COMM_LEN]; + char comm[TASK_COMM_LEN_16]; int32_t kernel_stack_id; int32_t user_stack_id; bool thread_current;
tools/testing/selftests/bpf/progs/strobemeta.h +3 -3
@@ -8,12 +8,12 @@#include <linux/ptrace.h> #include <linux/sched.h> #include <linux/types.h> +#include <linux/sched/task.h> #include <bpf/bpf_helpers.h> typedef uint32_t pid_t ;struct task_struct {}; -#define TASK_COMM_LEN 16#define PERF_MAX_STACK_DEPTH 127#define STROBE_TYPE_INVALID 0
@@ -189,7 +189,7 @@ struct strobemeta_payload {struct strobelight_bpf_sample {uint64_t ktime; -char comm[TASK_COMM_LEN ]; +char comm[TASK_COMM_LEN_16 ];pid_t pid;int user_stack_id;int kernel_stack_id;
@@ -520,7 +520,7 @@ int on_event(struct pt_regs *ctx) {return 0 ;/* this will never happen */ sample->pid = pid; -bpf_get_current_comm (&sample->comm,TASK_COMM_LEN ); +bpf_get_current_comm (&sample->comm,TASK_COMM_LEN_16 ); ktime_ns =bpf_ktime_get_ns (); sample->ktime = ktime_ns;
tools/testing/selftests/bpf/progs/test_core_reloc_kernel.c +2 -1
@@ -4,6 +4,7 @@#include <linux/bpf.h> #include <stdint.h> #include <stdbool.h> +#include <linux/sched/task.h> #include <bpf/bpf_helpers.h> #include <bpf/bpf_core_read.h>
@@ -26,7 +27,7 @@ struct core_reloc_kernel_output {struct task_struct {int pid;int tgid; -char comm[16 ]; +char comm[TASK_COMM_LEN_16 ];struct task_struct *group_leader; };
tools/testing/selftests/bpf/progs/test_ringbuf.c +2 -1
@@ -2,6 +2,7 @@// Copyright (c) 2020 Facebook #include <linux/bpf.h> +#include <linux/sched/task.h> #include <bpf/bpf_helpers.h> char _license[] SEC("license") = "GPL";
@@ -10,7 +11,7 @@ struct sample {int pid;int seq;long value; -char comm[16 ]; +char comm[TASK_COMM_LEN_16 ]; };struct {
tools/testing/selftests/bpf/progs/test_ringbuf_multi.c +2 -1
@@ -2,6 +2,7 @@// Copyright (c) 2020 Facebook #include <linux/bpf.h> +#include <linux/sched/task.h> #include <bpf/bpf_helpers.h> char _license[] SEC("license") = "GPL";
@@ -10,7 +11,7 @@ struct sample {int pid;int seq;long value; -char comm[16 ]; +char comm[TASK_COMM_LEN_16 ]; };struct ringbuf_map {
tools/testing/selftests/bpf/progs/test_sk_storage_tracing.c +3 -2
@@ -2,6 +2,7 @@/* Copyright (c) 2020 Facebook */ #include <vmlinux.h> +#include <linux/sched/task.h> #include <bpf/bpf_tracing.h> #include <bpf/bpf_core_read.h> #include <bpf/bpf_helpers.h>
@@ -9,7 +10,7 @@struct sk_stg {__u32 pid;__u32 last_notclose_state; -char comm[16 ]; +char comm[TASK_COMM_LEN_16 ]; };struct {
@@ -27,7 +28,7 @@ struct {__type (value, int); } del_sk_stg_map SEC(".maps"); -char task_comm[16 ] ="" ; +char task_comm[TASK_COMM_LEN_16 ] ="" ; SEC("tp_btf/inet_sock_set_state") int BPF_PROG(trace_inet_sock_set_state, struct sock *sk, int oldstate,
tools/testing/selftests/bpf/progs/test_skb_helpers.c +2 -3
@@ -1,10 +1,9 @@// SPDX-License-Identifier: GPL-2.0-only #include "vmlinux.h" +#include <linux/sched/task.h> #include <bpf/bpf_helpers.h> #include <bpf/bpf_endian.h> -#define TEST_COMM_LEN 16 -struct { __uint(type, BPF_MAP_TYPE_CGROUP_ARRAY); __uint(max_entries, 1);
@@ -18,7 +17,7 @@ SEC("classifier/test_skb_helpers")int test_skb_helpers (struct __sk_buff *skb) {struct task_struct *task; -char comm[TEST_COMM_LEN ]; +char comm[TASK_COMM_LEN_16 ];__u32 tpid; task = (struct task_struct *)bpf_get_current_task ();
tools/testing/selftests/bpf/progs/test_stacktrace_map.c +3 -2
@@ -2,6 +2,7 @@// Copyright (c) 2018 Facebook #include <linux/bpf.h> +#include <linux/sched/task.h> #include <bpf/bpf_helpers.h> #ifndef PERF_MAX_STACK_DEPTH
@@ -41,11 +42,11 @@ struct {/* taken from /sys/kernel/debug/tracing/events/sched/sched_switch/format */ struct sched_switch_args {unsigned long long pad; -char prev_comm[16 ]; +char prev_comm[TASK_COMM_LEN_16 ];int prev_pid;int prev_prio;long long prev_state; -char next_comm[16 ]; +char next_comm[TASK_COMM_LEN_16 ];int next_pid;int next_prio; };
tools/testing/selftests/bpf/progs/test_tracepoint.c +3 -2
@@ -2,16 +2,17 @@// Copyright (c) 2017 Facebook #include <linux/bpf.h> +#include <linux/sched/task.h> #include <bpf/bpf_helpers.h> /* taken from /sys/kernel/debug/tracing/events/sched/sched_switch/format */ struct sched_switch_args {unsigned long long pad; -char prev_comm[16 ]; +char prev_comm[TASK_COMM_LEN_16 ];int prev_pid;int prev_prio;long long prev_state; -char next_comm[16 ]; +char next_comm[TASK_COMM_LEN_16 ];int next_pid;int next_prio; };
Signature · 1 line
2.17.1
PATCH v5 15/15 kernel/kthread: show a warning if kthread's comm is truncated
In reply to Yafang ShaoShow a warning if task comm is truncated. Below is the result of my test case:
truncated kthread comm:I-am-a-kthread-with-lon, pid:14 by 6 characters
--- kernel/kthread.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-)
kernel/kthread.c +6 -1
@@ -399,12 +399,17 @@ struct task_struct *__kthread_create_on_node(int (*threadfn)(void *data),if (!IS_ERR (task)) {static const struct sched_param param = { .sched_priority =0 };char name[TASK_COMM_LEN ]; +int len;/* * task is already visible to other tasks, so updating * COMM must be protected. */ -vsnprintf (name,sizeof (name), namefmt, args); + len =vsnprintf (name,sizeof (name), namefmt, args); +if (len >=TASK_COMM_LEN ) { +pr_warn ("truncated kthread comm:%s, pid:%d by %d characters\n" , + name, task->pid, len -TASK_COMM_LEN +1 ); + }set_task_comm (task, name);/* * root may have changed our (kthreadd's) priority or CPU mask.
Signature · 1 line
2.17.1
Re: [PATCH v5 11/15] tools/bpf/bpftool: use TASK_COMM_LEN_16 instead of hard-coded 16
In reply to Yafang ShaoQuoted 26 lines · Yafang Shao
Use
TASK_COMM_LEN_16instead of hard-coded 16 to make it more grepable. It usesbpf_probe_read_kernel()to get task comm, which may return a string without nul terminator. We should usebpf_probe_read_kernel_str()instead.Signed-off-by Yafang Shao <[email protected]>Cc Mathieu Desnoyers <[email protected]>Cc Arnaldo Carvalho de Melo <[email protected]>Cc Peter Zijlstra <[email protected]>Cc Steven Rostedt <[email protected]>Cc Kees Cook <[email protected]>Cc Al Viro <[email protected]>Cc Petr Mladek <[email protected]>--- tools/bpf/bpftool/Makefile | 1 + tools/bpf/bpftool/main.h | 3 ++- tools/bpf/bpftool/skeleton/pid_iter.bpf.c | 4 ++-- tools/bpf/bpftool/skeleton/pid_iter.h | 4 +++- 4 files changed, 8 insertions(+), 4 deletions(-)tools/bpf/bpftool/Makefile +0
-0
tools/bpf/bpftool/Makefile $(OUTPUT)%.bpf.o: skeleton/%.bpf.c $(OUTPUT)vmlinux.h $(LIBBPF) · line 167
Quoted 4 lines
tools/bpf/bpftool/Makefile +1
-0@@ -164,6 +164,7 @@ $(OUTPUT)%.bpf.o: skeleton/%.bpf.c $(OUTPUT)vmlinux.h $(LIBBPF) $(QUIET_CLANG)$(CLANG) \ -I$(if $(OUTPUT),$(OUTPUT),.) \ -I$(srctree)/tools/include/uapi/ \ + -I$(srctree)/tools/include/ \
bpftool shouldn't rely on internal kernel headers for compilation. If you want to have TASK_COMM_LEN_16 constant for grep-ability, just #define it where appropriate
Quoted 14 lines
tools/bpf/bpftool/Makefile +0
-0-I$(LIBBPF_PATH) \ -I$(srctree)/tools/lib \ -g -O2 -Wall -target bpf -c $< -o $@ && $(LLVM_STRIP) -g $@tools/bpf/bpftool/main.h +1
-0@@ -12,6 +12,7 @@#include <linux/compiler.h> #include <linux/kernel.h> #include <linux/hashtable.h> +#include <linux/sched/task.h> #include <tools/libc_compat.h>
[...]
Re: [PATCH v5 13/15] tools/testing/selftests/bpf: use TASK_COMM_LEN_16 instead of hard-coded 16
In reply to Yafang ShaoQuoted 55 lines · Yafang Shao
The hard-coded 16 is used in various bpf progs. These progs get task comm either via
bpf_get_current_comm()orprctl()orbpf_core_read_str(), all of which can work well even if the task comm size is changed.
Below is the detailed information,
bpf_get_current_comm:
progs/test_ringbuf.c
progs/test_ringbuf_multi.cprctl:
prog_tests/test_overhead.c
prog_tests/trampoline_count.c
bpf_core_read_str:
progs/test_core_reloc_kernel.c
progs/test_sk_storage_tracing.cWe'd better replace the hard-coded 16 with
TASK_COMM_LEN_16to make it more grepable.Signed-off-by Yafang Shao <[email protected]>Cc Mathieu Desnoyers <[email protected]>Cc Arnaldo Carvalho de Melo <[email protected]>Cc Peter Zijlstra <[email protected]>Cc Steven Rostedt <[email protected]>Cc Kees Cook <[email protected]>Cc Al Viro <[email protected]>Cc Petr Mladek <[email protected]>--- tools/testing/selftests/bpf/Makefile | 2 +- tools/testing/selftests/bpf/prog_tests/ringbuf.c | 3 ++- tools/testing/selftests/bpf/prog_tests/ringbuf_multi.c | 3 ++- .../testing/selftests/bpf/prog_tests/sk_storage_tracing.c | 3 ++- tools/testing/selftests/bpf/prog_tests/test_overhead.c | 3 ++- tools/testing/selftests/bpf/prog_tests/trampoline_count.c | 3 ++- tools/testing/selftests/bpf/progs/profiler.h | 7 ++++--- tools/testing/selftests/bpf/progs/profiler.inc.h | 8 ++++---- tools/testing/selftests/bpf/progs/pyperf.h | 4 ++-- tools/testing/selftests/bpf/progs/strobemeta.h | 6 +++--- .../testing/selftests/bpf/progs/test_core_reloc_kernel.c | 3 ++- tools/testing/selftests/bpf/progs/test_ringbuf.c | 3 ++- tools/testing/selftests/bpf/progs/test_ringbuf_multi.c | 3 ++- .../testing/selftests/bpf/progs/test_sk_storage_tracing.c | 5 +++-- tools/testing/selftests/bpf/progs/test_skb_helpers.c | 5 ++--- tools/testing/selftests/bpf/progs/test_stacktrace_map.c | 5 +++-- tools/testing/selftests/bpf/progs/test_tracepoint.c | 5 +++-- 17 files changed, 41 insertions(+), 30 deletions(-)tools/testing/selftests/bpf/Makefile +0
-0
tools/testing/selftests/bpf/Makefile MENDIAN=$(if $(IS_LITTLE_ENDIAN),-mlittle-endian,-mbig-endian) · line 282
Quoted 5 lines
tools/testing/selftests/bpf/Makefile +1
-1@@ -279,7 +279,7 @@ MENDIAN=$(if $(IS_LITTLE_ENDIAN),-mlittle-endian,-mbig-endian) CLANG_SYS_INCLUDES = $(call get_sys_includes,$(CLANG)) BPF_CFLAGS = -g -D__TARGET_ARCH_$(SRCARCH) $(MENDIAN) \ - -I$(INCLUDE_DIR) -I$(CURDIR) -I$(APIDIR) \ + -I$(INCLUDE_DIR) -I$(CURDIR) -I$(APIDIR) -I${TOOLSINCDIR} \
please don't add new include paths unnecessarily. See my comment on another patch, if you add those new constants as enums, they will be automatically available in vmlinux BTF and thus in auto-generated vmlinux.h header (for those programs using it). For others, I'd just leave hard-coded 16 or re-defined TASK_COMM_LEN_16 where appropriate.
Quoted 16 lines
tools/testing/selftests/bpf/Makefile +0
-0-I$(abspath $(OUTPUT)/../usr/include) CLANG_CFLAGS = $(CLANG_SYS_INCLUDES) \tools/testing/selftests/bpf/prog_tests/ringbuf.c +1
-0@@ -12,6 +12,7 @@#include <sys/sysinfo.h> #include <linux/perf_event.h> #include <linux/ring_buffer.h> +#include <linux/sched/task.h> #include "test_ringbuf.lskel.h" #define EDONE 7777
tools/testing/selftests/bpf/prog_tests/ringbuf.c struct sample { · line 26
Quoted 5 lines
tools/testing/selftests/bpf/prog_tests/ringbuf.c +1
-1@@ -22,7 +23,7 @@ struct sample {int pid;int seq;long value; -char comm[16 ]; +char comm[TASK_COMM_LEN_16 ];
how much value is in this "grep-ability", really? I'm not convinced all this code churn is justified.
Quoted 15 lines
tools/testing/selftests/bpf/prog_tests/ringbuf.c +0
-0};static int sample_cnt;tools/testing/selftests/bpf/prog_tests/ringbuf_multi.c +1
-0@@ -2,6 +2,7 @@#define _GNU_SOURCE#include <test_progs.h> #include <sys/epoll.h> +#include <linux/sched/task.h> #include "test_ringbuf_multi.skel.h" static int duration =0 ;
[...]
Re: [PATCH v5 13/15] tools/testing/selftests/bpf: use TASK_COMM_LEN_16 instead of hard-coded 16
Quoted 58 lines · Andrii Nakryiko
Quoted 56 lines · Yafang Shao
The hard-coded 16 is used in various bpf progs. These progs get task comm either via
bpf_get_current_comm()orprctl()orbpf_core_read_str(), all of which can work well even if the task comm size is changed.
Below is the detailed information,
bpf_get_current_comm:
progs/test_ringbuf.c
progs/test_ringbuf_multi.cprctl:
prog_tests/test_overhead.c
prog_tests/trampoline_count.c
bpf_core_read_str:
progs/test_core_reloc_kernel.c
progs/test_sk_storage_tracing.cWe'd better replace the hard-coded 16 with
TASK_COMM_LEN_16to make it more grepable.Signed-off-by Yafang Shao <[email protected]>Cc Mathieu Desnoyers <[email protected]>Cc Arnaldo Carvalho de Melo <[email protected]>Cc Peter Zijlstra <[email protected]>Cc Steven Rostedt <[email protected]>Cc Kees Cook <[email protected]>Cc Al Viro <[email protected]>Cc Petr Mladek <[email protected]>--- tools/testing/selftests/bpf/Makefile | 2 +- tools/testing/selftests/bpf/prog_tests/ringbuf.c | 3 ++- tools/testing/selftests/bpf/prog_tests/ringbuf_multi.c | 3 ++- .../testing/selftests/bpf/prog_tests/sk_storage_tracing.c | 3 ++- tools/testing/selftests/bpf/prog_tests/test_overhead.c | 3 ++- tools/testing/selftests/bpf/prog_tests/trampoline_count.c | 3 ++- tools/testing/selftests/bpf/progs/profiler.h | 7 ++++--- tools/testing/selftests/bpf/progs/profiler.inc.h | 8 ++++---- tools/testing/selftests/bpf/progs/pyperf.h | 4 ++-- tools/testing/selftests/bpf/progs/strobemeta.h | 6 +++--- .../testing/selftests/bpf/progs/test_core_reloc_kernel.c | 3 ++- tools/testing/selftests/bpf/progs/test_ringbuf.c | 3 ++- tools/testing/selftests/bpf/progs/test_ringbuf_multi.c | 3 ++- .../testing/selftests/bpf/progs/test_sk_storage_tracing.c | 5 +++-- tools/testing/selftests/bpf/progs/test_skb_helpers.c | 5 ++--- tools/testing/selftests/bpf/progs/test_stacktrace_map.c | 5 +++-- tools/testing/selftests/bpf/progs/test_tracepoint.c | 5 +++-- 17 files changed, 41 insertions(+), 30 deletions(-)tools/testing/selftests/bpf/Makefile +0
-0@@ -279,7 +279,7 @@ MENDIAN=$(if $(IS_LITTLE_ENDIAN),-mlittle-endian,-mbig-endian)
Quoted 9 lines
tools/testing/selftests/bpf/Makefile MENDIAN=$(if $(IS_LITTLE_ENDIAN),-mlittle-endian,-mbig-endian) · line 282
Quoted 4 lines
tools/testing/selftests/bpf/Makefile +1
-1CLANG_SYS_INCLUDES = $(call get_sys_includes,$(CLANG)) BPF_CFLAGS = -g -D__TARGET_ARCH_$(SRCARCH) $(MENDIAN) \ - -I$(INCLUDE_DIR) -I$(CURDIR) -I$(APIDIR) \ + -I$(INCLUDE_DIR) -I$(CURDIR) -I$(APIDIR) -I${TOOLSINCDIR} \please don't add new include paths unnecessarily. See my comment on another patch, if you add those new constants as enums, they will be automatically available in vmlinux BTF and thus in auto-generated
vmlinux.hheader (for those programs using it).
Yes, after converting it to enum, the BPF programs can get it from the generated vmlinux.h.
Quoted 3 lines
For others, I'd just
leave hard-coded 16 or re-definedTASK_COMM_LEN_16where appropriate.
It seems not all the BPF programs can include the vmlinux.h. What we really care about here is the copy of task comm should be with a nul terminator, if we can assure it, then the size used by the BPF is not important.
I have checked the copy of task comm in all these BPF programs one by one, and replaced the unsafe bpf_probe_read_kernel() with bpf_probe_read_kernel_str(), after that change, I think we can leave hard-coded 16 for the progs which can't include vmlinux.h.
Quoted 42 lines
Quoted 16 lines
tools/testing/selftests/bpf/Makefile +0
-0-I$(abspath $(OUTPUT)/../usr/include) CLANG_CFLAGS = $(CLANG_SYS_INCLUDES) \tools/testing/selftests/bpf/prog_tests/ringbuf.c +1
-0@@ -12,6 +12,7 @@#include <sys/sysinfo.h> #include <linux/perf_event.h> #include <linux/ring_buffer.h> +#include <linux/sched/task.h> #include "test_ringbuf.lskel.h" #define EDONE 7777tools/testing/selftests/bpf/prog_tests/ringbuf.c struct sample { · line 26
Quoted 5 lines
tools/testing/selftests/bpf/prog_tests/ringbuf.c +1
-1@@ -22,7 +23,7 @@ struct sample {int pid;int seq;long value; -char comm[16 ]; +char comm[TASK_COMM_LEN_16 ];how much value is in this "grep-ability", really? I'm not convinced all this code churn is justified.
Quoted 15 lines
tools/testing/selftests/bpf/prog_tests/ringbuf.c +0
-0};static int sample_cnt;tools/testing/selftests/bpf/prog_tests/ringbuf_multi.c +1
-0@@ -2,6 +2,7 @@#define _GNU_SOURCE#include <test_progs.h> #include <sys/epoll.h> +#include <linux/sched/task.h> #include "test_ringbuf_multi.skel.h" static int duration =0 ;[...]
Signature · 2 lines
Thanks Yafang
Re: [PATCH v5 13/15] tools/testing/selftests/bpf: use TASK_COMM_LEN_16 instead of hard-coded 16
Quoted 78 lines · Yafang Shao
Quoted 58 lines · Andrii Nakryiko
Quoted 56 lines · Yafang Shao
The hard-coded 16 is used in various bpf progs. These progs get task comm either via
bpf_get_current_comm()orprctl()orbpf_core_read_str(), all of which can work well even if the task comm size is changed.
Below is the detailed information,
bpf_get_current_comm:
progs/test_ringbuf.c
progs/test_ringbuf_multi.cprctl:
prog_tests/test_overhead.c
prog_tests/trampoline_count.c
bpf_core_read_str:
progs/test_core_reloc_kernel.c
progs/test_sk_storage_tracing.cWe'd better replace the hard-coded 16 with
TASK_COMM_LEN_16to make it more grepable.Signed-off-by Yafang Shao <[email protected]>Cc Mathieu Desnoyers <[email protected]>Cc Arnaldo Carvalho de Melo <[email protected]>Cc Peter Zijlstra <[email protected]>Cc Steven Rostedt <[email protected]>Cc Kees Cook <[email protected]>Cc Al Viro <[email protected]>Cc Petr Mladek <[email protected]>--- tools/testing/selftests/bpf/Makefile | 2 +- tools/testing/selftests/bpf/prog_tests/ringbuf.c | 3 ++- tools/testing/selftests/bpf/prog_tests/ringbuf_multi.c | 3 ++- .../testing/selftests/bpf/prog_tests/sk_storage_tracing.c | 3 ++- tools/testing/selftests/bpf/prog_tests/test_overhead.c | 3 ++- tools/testing/selftests/bpf/prog_tests/trampoline_count.c | 3 ++- tools/testing/selftests/bpf/progs/profiler.h | 7 ++++--- tools/testing/selftests/bpf/progs/profiler.inc.h | 8 ++++---- tools/testing/selftests/bpf/progs/pyperf.h | 4 ++-- tools/testing/selftests/bpf/progs/strobemeta.h | 6 +++--- .../testing/selftests/bpf/progs/test_core_reloc_kernel.c | 3 ++- tools/testing/selftests/bpf/progs/test_ringbuf.c | 3 ++- tools/testing/selftests/bpf/progs/test_ringbuf_multi.c | 3 ++- .../testing/selftests/bpf/progs/test_sk_storage_tracing.c | 5 +++-- tools/testing/selftests/bpf/progs/test_skb_helpers.c | 5 ++--- tools/testing/selftests/bpf/progs/test_stacktrace_map.c | 5 +++-- tools/testing/selftests/bpf/progs/test_tracepoint.c | 5 +++-- 17 files changed, 41 insertions(+), 30 deletions(-)tools/testing/selftests/bpf/Makefile +0
-0@@ -279,7 +279,7 @@ MENDIAN=$(if $(IS_LITTLE_ENDIAN),-mlittle-endian,-mbig-endian)Quoted 9 lines
tools/testing/selftests/bpf/Makefile MENDIAN=$(if $(IS_LITTLE_ENDIAN),-mlittle-endian,-mbig-endian) · line 282
Quoted 4 lines
tools/testing/selftests/bpf/Makefile +1
-1CLANG_SYS_INCLUDES = $(call get_sys_includes,$(CLANG)) BPF_CFLAGS = -g -D__TARGET_ARCH_$(SRCARCH) $(MENDIAN) \ - -I$(INCLUDE_DIR) -I$(CURDIR) -I$(APIDIR) \ + -I$(INCLUDE_DIR) -I$(CURDIR) -I$(APIDIR) -I${TOOLSINCDIR} \please don't add new include paths unnecessarily. See my comment on another patch, if you add those new constants as enums, they will be automatically available in vmlinux BTF and thus in auto-generated
vmlinux.hheader (for those programs using it).Yes, after converting it to enum, the BPF programs can get it from the generated
vmlinux.h.Quoted 3 lines
For others, I'd just
leave hard-coded 16 or re-definedTASK_COMM_LEN_16where appropriate.
Quoted 8 lines
It seems not all the BPF programs can include the
vmlinux.h. What we really care about here is the copy of task comm should be with a nul terminator, if we can assure it, then the size used by the BPF is not important.
I have checked the copy of task comm in all these BPF programs one by one, and replaced the unsafebpf_probe_read_kernel()withbpf_probe_read_kernel_str(), after that change, I think we can leave hard-coded 16 for the progs which can't includevmlinux.h.
SGTM, thanks.
Quoted 48 lines
Quoted 40 lines
Quoted 17 lines
tools/testing/selftests/bpf/Makefile +0
-0-I$(abspath $(OUTPUT)/../usr/include) CLANG_CFLAGS = $(CLANG_SYS_INCLUDES) \tools/testing/selftests/bpf/prog_tests/ringbuf.c +1
-0@@ -12,6 +12,7 @@#include <sys/sysinfo.h> #include <linux/perf_event.h> #include <linux/ring_buffer.h> +#include <linux/sched/task.h> #include "test_ringbuf.lskel.h" #define EDONE 7777tools/testing/selftests/bpf/prog_tests/ringbuf.c struct sample { · line 26
Quoted 5 lines
tools/testing/selftests/bpf/prog_tests/ringbuf.c +1
-1@@ -22,7 +23,7 @@ struct sample {int pid;int seq;long value; -char comm[16 ]; +char comm[TASK_COMM_LEN_16 ];how much value is in this "grep-ability", really? I'm not convinced all this code churn is justified.
Quoted 14 lines
tools/testing/selftests/bpf/prog_tests/ringbuf.c +0
-0};static int sample_cnt;tools/testing/selftests/bpf/prog_tests/ringbuf_multi.c +1
-0@@ -2,6 +2,7 @@#define _GNU_SOURCE#include <test_progs.h> #include <sys/epoll.h> +#include <linux/sched/task.h> #include "test_ringbuf_multi.skel.h" Quoted 3 lines
Quoted 1 line
tools/testing/selftests/bpf/prog_tests/ringbuf_multi.c +0
-0static int duration =0 ;[...]
--
Thanks
Yafang