Skip to content

Commit

Permalink
libbpf-tools: Fix renaming of the state field of task_struct
Browse files Browse the repository at this point in the history
Kernel commit 2f064a59a1 ("sched: Change task_struct::state") changes
the name of task_struct::state to task_struct::__state, which breaks
several libbpf tools. Fix them by utilizing the libbpf CO-RE support.

Signed-off-by: Hengqi Chen <[email protected]>
  • Loading branch information
chenhengqi authored and yonghong-song committed Oct 16, 2021
1 parent fb09f59 commit 8b350fd
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 5 deletions.
29 changes: 29 additions & 0 deletions libbpf-tools/core_fixes.bpf.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/* SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) */
/* Copyright (c) 2021 Hengqi Chen */

#ifndef __CORE_FIXES_BPF_H
#define __CORE_FIXES_BPF_H

#include <vmlinux.h>
#include <bpf/bpf_core_read.h>

/**
* commit 2f064a59a1 ("sched: Change task_struct::state") changes
* the name of task_struct::state to task_struct::__state
* see:
* https://github.com/torvalds/linux/commit/2f064a59a1
*/
struct task_struct___x {
unsigned int __state;
};

static __s64 get_task_state(void *task)
{
struct task_struct___x *t = task;

if (bpf_core_field_exists(t->__state))
return t->__state;
return ((struct task_struct *)task)->state;
}

#endif /* __CORE_FIXES_BPF_H */
3 changes: 2 additions & 1 deletion libbpf-tools/cpudist.bpf.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <bpf/bpf_tracing.h>
#include "cpudist.h"
#include "bits.bpf.h"
#include "core_fixes.bpf.h"

#define TASK_RUNNING 0

Expand Down Expand Up @@ -88,7 +89,7 @@ int BPF_PROG(sched_switch, bool preempt, struct task_struct *prev,
store_start(prev_tgid, prev_pid, ts);
update_hist(next, tgid, pid, ts);
} else {
if (prev->state == TASK_RUNNING)
if (get_task_state(prev) == TASK_RUNNING)
update_hist(prev, prev_tgid, prev_pid, ts);
store_start(tgid, pid, ts);
}
Expand Down
3 changes: 2 additions & 1 deletion libbpf-tools/offcputime.bpf.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <bpf/bpf_core_read.h>
#include <bpf/bpf_tracing.h>
#include "offcputime.h"
#include "core_fixes.bpf.h"

#define PF_KTHREAD 0x00200000 /* I am a kernel thread */
#define MAX_ENTRIES 10240
Expand Down Expand Up @@ -51,7 +52,7 @@ static bool allow_record(struct task_struct *t)
return false;
else if (kernel_threads_only && !(t->flags & PF_KTHREAD))
return false;
if (state != -1 && t->state != state)
if (state != -1 && get_task_state(t) != state)
return false;
return true;
}
Expand Down
3 changes: 2 additions & 1 deletion libbpf-tools/runqlat.bpf.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "runqlat.h"
#include "bits.bpf.h"
#include "maps.bpf.h"
#include "core_fixes.bpf.h"

#define MAX_ENTRIES 10240
#define TASK_RUNNING 0
Expand Down Expand Up @@ -69,7 +70,7 @@ int BPF_PROG(sched_swith, bool preempt, struct task_struct *prev,
u32 pid, hkey;
s64 delta;

if (prev->state == TASK_RUNNING)
if (get_task_state(prev) == TASK_RUNNING)
trace_enqueue(prev->tgid, prev->pid);

pid = next->pid;
Expand Down
4 changes: 2 additions & 2 deletions libbpf-tools/runqslower.bpf.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <vmlinux.h>
#include <bpf/bpf_helpers.h>
#include "runqslower.h"
#include "core_fixes.bpf.h"

#define TASK_RUNNING 0

Expand Down Expand Up @@ -69,11 +70,10 @@ int handle__sched_switch(u64 *ctx)
struct task_struct *next = (struct task_struct *)ctx[2];
struct event event = {};
u64 *tsp, delta_us;
long state;
u32 pid;

/* ivcsw: treat like an enqueue event and store timestamp */
if (prev->state == TASK_RUNNING)
if (get_task_state(prev) == TASK_RUNNING)
trace_enqueue(prev->tgid, prev->pid);

pid = next->pid;
Expand Down

0 comments on commit 8b350fd

Please sign in to comment.