Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/linux/concepts/timers.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,6 @@ The callback is invoked with a pointer to the map, map key, and map value associ

A callback can chose to re-schedule its own timer by calling [`bpf_timer_start`](../helper-function/bpf_timer_start.md) on `value->timer`. Thus making it possible to not just have a one shot delay from a given eBPF program run, but to have a periodic function running endlessly after just a single trigger event.

Canceling a timer with [`bpf_timer_cancel`](../helper-function/bpf_timer_cancel.md) also detaches its callback. A canceled timer is still initialized, calling [`bpf_timer_init`](../helper-function/bpf_timer_init.md) on it again returns `-EBUSY`, but starting it returns `-EINVAL` until [`bpf_timer_set_callback`](../helper-function/bpf_timer_set_callback.md) has assigned a new callback. So the cycle to reuse a canceled timer is `bpf_timer_set_callback` followed by [`bpf_timer_start`](../helper-function/bpf_timer_start.md), without a new `bpf_timer_init`.


15 changes: 13 additions & 2 deletions docs/linux/helper-function/bpf_timer_cancel.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,19 @@ Cancel the timer and wait for callback_fn to finish if it was running.

## Usage

!!! example "Docs could be improved"
This part of the docs is incomplete, contributions are very welcome
Canceling a timer does more than stopping it: the kernel also detaches the callback and drops the reference to the program that owns it ([`drop_prog_refcnt`](https://elixir.bootlin.com/linux/v6.18/source/kernel/bpf/helpers.c#L1457)). The timer itself stays initialized, only its association with a callback is gone. This has been the behavior since timers were introduced in v5.15.

Because of this, restarting a canceled timer takes one more step than one might expect. Calling [`bpf_timer_start`](bpf_timer_start.md) right after a cancel fails with `-EINVAL`, the same error as for a timer that never had a callback. Assign a callback again first:

```c
bpf_timer_cancel(&elem->timer);

/* Later, to re-arm the same timer: */
bpf_timer_set_callback(&elem->timer, timer_callback); /* required again after a cancel */
bpf_timer_start(&elem->timer, 1000000, 0);
```

[`bpf_timer_init`](bpf_timer_init.md) must not be called again: the timer is still initialized, and re-initializing an initialized timer fails with `-EBUSY`.

### Program types

Expand Down
2 changes: 2 additions & 0 deletions docs/linux/helper-function/bpf_timer_set_callback.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ The function passed to `callback_fn` should have the following signature:
!!! example "Docs could be improved"
This part of the docs is incomplete, contributions are very welcome

This helper must be called again after a [`bpf_timer_cancel`](bpf_timer_cancel.md): canceling detaches the callback from the timer, so a new one has to be assigned before the timer can be started again. The timer does not have to be re-initialized for this.

### Program types

This helper call can be used in the following program types:
Expand Down
2 changes: 2 additions & 0 deletions docs/linux/helper-function/bpf_timer_start.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ _flags_ can be one of:
!!! example "Docs could be improved"
This part of the docs is incomplete, contributions are very welcome

Note that a timer loses its callback when it is canceled. Calling this helper after a [`bpf_timer_cancel`](bpf_timer_cancel.md) therefore returns `-EINVAL` until a new callback is assigned with [`bpf_timer_set_callback`](bpf_timer_set_callback.md).

### Program types

This helper call can be used in the following program types:
Expand Down