tools/testing/selftests/bpf/progs/timer_start_deadlock.c

Source file repositories/reference/linux-study-clean/tools/testing/selftests/bpf/progs/timer_start_deadlock.c

File Facts

System
Linux kernel
Corpus path
tools/testing/selftests/bpf/progs/timer_start_deadlock.c
Extension
.c
Size
1582 bytes
Lines
71
Domain
Support Tooling And Documentation
Bucket
tools
Inferred role
Support Tooling And Documentation: implementation source
Status
source implementation candidate

Why This File Exists

Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.

Dependency Surface

Detected Declarations

Annotated Snippet

struct elem {
	struct bpf_timer timer;
};

struct {
	__uint(type, BPF_MAP_TYPE_ARRAY);
	__uint(max_entries, 1);
	__type(key, int);
	__type(value, struct elem);
} timer_map SEC(".maps");

volatile int in_timer_start;
volatile int tp_called;

static int timer_cb(void *map, int *key, struct elem *value)
{
	return 0;
}

SEC("tp_btf/hrtimer_start")
int BPF_PROG(tp_hrtimer_start, struct hrtimer *hrtimer, enum hrtimer_mode mode, bool was_armed)
{
	struct bpf_timer *timer;
	int key = 0;

	if (!in_timer_start || !was_armed)
		return 0;

	tp_called = 1;
	timer = bpf_map_lookup_elem(&timer_map, &key);

	/*
	 * Call bpf_timer_start() from the tracepoint within hrtimer logic
	 * on the same timer to make sure it doesn't deadlock.
	 */
	bpf_timer_start(timer, 1000000000, 0);
	return 0;
}

SEC("syscall")
int start_timer(void *ctx)
{
	struct bpf_timer *timer;
	int key = 0;

	timer = bpf_map_lookup_elem(&timer_map, &key);
	/* claude may complain here that there is no NULL check. Ignoring it. */
	bpf_timer_init(timer, &timer_map, CLOCK_MONOTONIC);
	bpf_timer_set_callback(timer, timer_cb);

	/*
	 * call hrtimer_start() twice, so that 2nd call does
	 * trace_hrtimer_start(was_armed=1) tracepoint.
	 */
	in_timer_start = 1;
	bpf_timer_start(timer, 1000000000, 0);
	bpf_timer_start(timer, 1000000000, 0);
	in_timer_start = 0;
	return 0;
}

Annotation

Implementation Notes