Skip to contentSkip to conversation

bpf: Add ability to pin bpf timer to calling CPU

PATCH Superseded David Vernet 2 participants 5 messages Oct 2, 2023 – Oct 3, 2023

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 bpf: Add ability to pin bpf timer to calling CPU

BPF supports creating high resolution timers using bpf_timer_* helper functions. Currently, only the BPF_F_TIMER_ABS flag is supported, which specifies that the timeout should be interpreted as absolute time. It would also be useful to be able to pin that timer to a core. For example, if you wanted to make a subset of cores run without timer interrupts, and only have the timer be invoked on a single core.

This patch adds support for this with a new BPF_F_TIMER_CPU_PIN flag. When specified, the HRTIMER_MODE_PINNED flag is passed to hrtimer_start(). A subsequent patch will update selftests to validate.

---
 include/uapi/linux/bpf.h       | 4 ++++
 kernel/bpf/helpers.c           | 5 ++++-
 tools/include/uapi/linux/bpf.h | 4 ++++
 3 files changed, 12 insertions(+), 1 deletion(-)
include/uapi/linux/bpf.h +4 -0
index 70bfa997e896..a7d4a1a69f21 100644
@@ -5096,6 +5096,8 @@ union bpf_attr {
  *		**BPF_F_TIMER_ABS**
  *			Start the timer in absolute expire value instead of the
  *			default relative one.
+ *		**BPF_F_TIMER_CPU_PIN**
+ *			Timer will be pinned to the CPU of the caller.
  *
  *	Return
  *		0 on success.
@@ -7309,9 +7311,11 @@ struct bpf_core_relo {
  * Flags to control bpf_timer_start() behaviour.
  *     - BPF_F_TIMER_ABS: Timeout passed is absolute time, by default it is
  *       relative to current time.
+ *     - BPF_F_TIMER_CPU_PIN: Timer will be pinned to the CPU of the caller.
  */
 enum {
 	BPF_F_TIMER_ABS = (1ULL << 0),
+	BPF_F_TIMER_CPU_PIN = (1ULL << 1),
 };
 
 /* BPF numbers iterator state */
kernel/bpf/helpers.c +4 -1
index dd1c69ee3375..d2840dd5b00d 100644
@@ -1272,7 +1272,7 @@ BPF_CALL_3(bpf_timer_start, struct bpf_timer_kern *, timer, u64, nsecs, u64, fla
 
 	if (in_nmi())
 		return -EOPNOTSUPP;
-	if (flags > BPF_F_TIMER_ABS)
+	if (flags & ~(BPF_F_TIMER_ABS | BPF_F_TIMER_CPU_PIN))
 		return -EINVAL;
 	__bpf_spin_lock_irqsave(&timer->lock);
 	t = timer->timer;
@@ -1286,6 +1286,9 @@ BPF_CALL_3(bpf_timer_start, struct bpf_timer_kern *, timer, u64, nsecs, u64, fla
 	else
 		mode = HRTIMER_MODE_REL_SOFT;
 
+	if (flags & BPF_F_TIMER_CPU_PIN)
+		mode |= HRTIMER_MODE_PINNED;
+
 	hrtimer_start(&t->timer, ns_to_ktime(nsecs), mode);
 out:
 	__bpf_spin_unlock_irqrestore(&timer->lock);
tools/include/uapi/linux/bpf.h +4 -0
index 70bfa997e896..a7d4a1a69f21 100644
@@ -5096,6 +5096,8 @@ union bpf_attr {
  *		**BPF_F_TIMER_ABS**
  *			Start the timer in absolute expire value instead of the
  *			default relative one.
+ *		**BPF_F_TIMER_CPU_PIN**
+ *			Timer will be pinned to the CPU of the caller.
  *
  *	Return
  *		0 on success.
@@ -7309,9 +7311,11 @@ struct bpf_core_relo {
  * Flags to control bpf_timer_start() behaviour.
  *     - BPF_F_TIMER_ABS: Timeout passed is absolute time, by default it is
  *       relative to current time.
+ *     - BPF_F_TIMER_CPU_PIN: Timer will be pinned to the CPU of the caller.
  */
 enum {
 	BPF_F_TIMER_ABS = (1ULL << 0),
+	BPF_F_TIMER_CPU_PIN = (1ULL << 1),
 };
 
 /* BPF numbers iterator state */
Signature · 1 line
2.41.0
#1 lore raw

PATCH 2/2 bpf/selftests: Test pinning bpf timer to a core

Now that we support pinning a BPF timer to the current core, we should test it with some selftests. This patch adds two new testcases to the timer suite, which verifies that a BPF timer both with and without BPF_F_TIMER_ABS, can be pinned to the calling core with BPF_F_TIMER_CPU_PIN.

---
 .../testing/selftests/bpf/prog_tests/timer.c  |  4 +
 tools/testing/selftests/bpf/progs/timer.c     | 75 +++++++++++++++++++
 2 files changed, 79 insertions(+)
tools/testing/selftests/bpf/prog_tests/timer.c +4 -0
index 290c21dbe65a..d8bc838445ec 100644
@@ -14,6 +14,7 @@ static int timer(struct timer *timer_skel)
 
 	ASSERT_EQ(timer_skel->data->callback_check, 52, "callback_check1");
 	ASSERT_EQ(timer_skel->data->callback2_check, 52, "callback2_check1");
+	ASSERT_EQ(timer_skel->bss->pinned_callback_check, 0, "pinned_callback_check1");
 
 	prog_fd = bpf_program__fd(timer_skel->progs.test1);
 	err = bpf_prog_test_run_opts(prog_fd, &topts);
@@ -32,6 +33,9 @@ static int timer(struct timer *timer_skel)
 	/* check that timer_cb3() was executed twice */
 	ASSERT_EQ(timer_skel->bss->abs_data, 12, "abs_data");
 
+	/* check that timer_cb_pinned() was executed twice */
+	ASSERT_EQ(timer_skel->bss->pinned_callback_check, 2, "pinned_callback_check");
+
 	/* check that there were no errors in timer execution */
 	ASSERT_EQ(timer_skel->bss->err, 0, "err");
 
tools/testing/selftests/bpf/progs/timer.c +75 -0

90 lines not on this page · raw message

Signature · 1 line
2.41.0
Acked-by on v1 1/2
#4 lore raw
In reply to David Vernet
Quoted 11 lines · David Vernet

BPF supports creating high resolution timers using bpf_timer_* helper functions. Currently, only the BPF_F_TIMER_ABS flag is supported, which specifies that the timeout should be interpreted as absolute time. It would also be useful to be able to pin that timer to a core. For example, if you wanted to make a subset of cores run without timer interrupts, and only have the timer be invoked on a single core.

Quoted 5 lines

This patch adds support for this with a new BPF_F_TIMER_CPU_PIN flag. When specified, the HRTIMER_MODE_PINNED flag is passed to hrtimer_start(). A subsequent patch will update selftests to validate.

Acked-by on v1 2/2
#2 lore raw

Re: [PATCH bpf-next 2/2] bpf/selftests: Test pinning bpf timer to a core

In reply to David Vernet
Quoted 11 lines · David Vernet

Now that we support pinning a BPF timer to the current core, we should test it with some selftests. This patch adds two new testcases to the timer suite, which verifies that a BPF timer both with and without BPF_F_TIMER_ABS, can be pinned to the calling core with BPF_F_TIMER_CPU_PIN.

With one nit/question below.

Quoted 38 lines

---
.../testing/selftests/bpf/prog_tests/timer.c | 4 +
tools/testing/selftests/bpf/progs/timer.c | 75 +++++++++++++++++++

2 files changed, 79 insertions(+)
tools/testing/selftests/bpf/prog_tests/timer.c +4 -0
index 290c21dbe65a..d8bc838445ec 100644
@@ -14,6 +14,7 @@ static int timer(struct timer *timer_skel)
 
 ASSERT_EQ(timer_skel->data->callback_check, 52, "callback_check1");
 ASSERT_EQ(timer_skel->data->callback2_check, 52, "callback2_check1");
+ ASSERT_EQ(timer_skel->bss->pinned_callback_check, 0, "pinned_callback_check1");
 
 prog_fd = bpf_program__fd(timer_skel->progs.test1);
 err = bpf_prog_test_run_opts(prog_fd, &topts);
@@ -32,6 +33,9 @@ static int timer(struct timer *timer_skel)
 /* check that timer_cb3() was executed twice */
 ASSERT_EQ(timer_skel->bss->abs_data, 12, "abs_data");
 
+ /* check that timer_cb_pinned() was executed twice */
+ ASSERT_EQ(timer_skel->bss->pinned_callback_check, 2, "pinned_callback_check");
+
 /* check that there were no errors in timer execution */
 ASSERT_EQ(timer_skel->bss->err, 0, "err");
 
tools/testing/selftests/bpf/progs/timer.c +3 -0
index 9a16d95213e1..0112b9c038b4 100644
@@ -53,12 +53,28 @@ struct {
 __type(value, struct elem);
 } abs_timer SEC(".maps");
 
+struct {
+ __uint(type, BPF_MAP_TYPE_ARRAY);
+ __uint(max_entries, 1);

tools/testing/selftests/bpf/progs/timer.c struct { · line 68

Quoted 10 lines
tools/testing/selftests/bpf/progs/timer.c +10 -0
+ __type(key, int);
+ __type(value, struct elem);
+} soft_timer_pinned SEC(".maps");
+
+struct {
+ __uint(type, BPF_MAP_TYPE_ARRAY);
+ __uint(max_entries, 1);
+ __type(key, int);
+ __type(value, struct elem);
+} abs_timer_pinned SEC(".maps");

nit: I think we can also do something like the following, but I am not sure whether this style is not recommended.

tools/testing/selftests/bpf/progs/timer.c +1 -1
index 9a16d95213e1..638eeebcd6c9 100644
@@ -51,7 +51,7 @@ struct {
        __uint(max_entries, 1);
        __type(key, int);
        __type(value, struct elem);
-} abs_timer SEC(".maps");
+} abs_timer SEC(".maps"), soft_timer_pinned SEC(".maps"), abs_timer_pinned SEC(".maps");
 
 __u64 bss_data;
 __u64 abs_data;

[...]

#3 lore raw

Re: [PATCH bpf-next 2/2] bpf/selftests: Test pinning bpf timer to a core

Quoted 73 lines · Song Liu
Quoted 11 lines · David Vernet

Now that we support pinning a BPF timer to the current core, we should test it with some selftests. This patch adds two new testcases to the timer suite, which verifies that a BPF timer both with and without BPF_F_TIMER_ABS, can be pinned to the calling core with BPF_F_TIMER_CPU_PIN.

With one nit/question below.

Quoted 38 lines

---
.../testing/selftests/bpf/prog_tests/timer.c | 4 +
tools/testing/selftests/bpf/progs/timer.c | 75 +++++++++++++++++++

2 files changed, 79 insertions(+)
tools/testing/selftests/bpf/prog_tests/timer.c +4 -0
index 290c21dbe65a..d8bc838445ec 100644
@@ -14,6 +14,7 @@ static int timer(struct timer *timer_skel)
 
 ASSERT_EQ(timer_skel->data->callback_check, 52, "callback_check1");
 ASSERT_EQ(timer_skel->data->callback2_check, 52, "callback2_check1");
+ ASSERT_EQ(timer_skel->bss->pinned_callback_check, 0, "pinned_callback_check1");
 
 prog_fd = bpf_program__fd(timer_skel->progs.test1);
 err = bpf_prog_test_run_opts(prog_fd, &topts);
@@ -32,6 +33,9 @@ static int timer(struct timer *timer_skel)
 /* check that timer_cb3() was executed twice */
 ASSERT_EQ(timer_skel->bss->abs_data, 12, "abs_data");
 
+ /* check that timer_cb_pinned() was executed twice */
+ ASSERT_EQ(timer_skel->bss->pinned_callback_check, 2, "pinned_callback_check");
+
 /* check that there were no errors in timer execution */
 ASSERT_EQ(timer_skel->bss->err, 0, "err");
 
tools/testing/selftests/bpf/progs/timer.c +3 -0
index 9a16d95213e1..0112b9c038b4 100644
@@ -53,12 +53,28 @@ struct {
 __type(value, struct elem);
 } abs_timer SEC(".maps");
 
+struct {
+ __uint(type, BPF_MAP_TYPE_ARRAY);
+ __uint(max_entries, 1);

tools/testing/selftests/bpf/progs/timer.c struct { · line 68

Quoted 10 lines
tools/testing/selftests/bpf/progs/timer.c +10 -0
+ __type(key, int);
+ __type(value, struct elem);
+} soft_timer_pinned SEC(".maps");
+
+struct {
+ __uint(type, BPF_MAP_TYPE_ARRAY);
+ __uint(max_entries, 1);
+ __type(key, int);
+ __type(value, struct elem);
+} abs_timer_pinned SEC(".maps");

nit: I think we can also do something like the following, but I am not sure whether this style is not recommended.

tools/testing/selftests/bpf/progs/timer.c +0 -0
index 9a16d95213e1..638eeebcd6c9 100644

tools/testing/selftests/bpf/progs/timer.c struct { · line 54

Quoted 5 lines
tools/testing/selftests/bpf/progs/timer.c +1 -1
@@ -51,7 +51,7 @@ struct {
        __uint(max_entries, 1);
        __type(key, int);
        __type(value, struct elem);
-} abs_timer SEC(".maps");
+} abs_timer SEC(".maps"), soft_timer_pinned SEC(".maps"), abs_timer_pinned SEC(".maps");

This looks like a nice readability improvement / cleanup to me. If nobody objects, I'd say let's apply it.

Thanks,
David