[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <bcdccd37-6372-859f-824e-c96250361904@huawei.com>
Date: Tue, 6 Jul 2021 21:57:52 +0800
From: "luwei (O)" <luwei32@...wei.com>
To: Alexei Starovoitov <alexei.starovoitov@...il.com>,
<davem@...emloft.net>
CC: <daniel@...earbox.net>, <andrii@...nel.org>,
<netdev@...r.kernel.org>, <bpf@...r.kernel.org>,
<kernel-team@...com>
Subject: Re: [PATCH v4 bpf-next 1/9] bpf: Introduce bpf timers.
在 2021/7/2 3:20 AM, Alexei Starovoitov 写道:
> From: Alexei Starovoitov <ast@...nel.org>
>
> Introduce 'struct bpf_timer { __u64 :64; __u64 :64; };' that can be embedded
> in hash/array/lru maps as a regular field and helpers to operate on it:
>
> // Initialize the timer.
> // First 4 bits of 'flags' specify clockid.
> // Only CLOCK_MONOTONIC, CLOCK_REALTIME, CLOCK_BOOTTIME are allowed.
> long bpf_timer_init(struct bpf_timer *timer, struct bpf_map *map, int flags);
>
> // Configure the timer to call 'callback_fn' static function.
> long bpf_timer_set_callback(struct bpf_timer *timer, void *callback_fn);
>
> // Arm the timer to expire 'nsec' nanoseconds from the current time.
> long bpf_timer_start(struct bpf_timer *timer, u64 nsec, u64 flags);
>
> // Cancel the timer and wait for callback_fn to finish if it was running.
> long bpf_timer_cancel(struct bpf_timer *timer);
>
[...]
> +static enum hrtimer_restart bpf_timer_cb(struct hrtimer *hrtimer)
> +{
> + struct bpf_hrtimer *t = container_of(hrtimer, struct bpf_hrtimer, timer);
> + struct bpf_map *map = t->map;
> + void *value = t->value;
> + struct bpf_timer_kern *timer = value + map->timer_off;
> + struct bpf_prog *prog;
> + void *callback_fn;
> + void *key;
> + u32 idx;
> + int ret;
> +
> + /* The triple underscore bpf_spin_lock is a direct call
> + * to BPF_CALL_1(bpf_spin_lock) which does irqsave.
> + */
> + ____bpf_spin_lock(&timer->lock);
> + /* callback_fn and prog need to match. They're updated together
> + * and have to be read under lock.
> + */
> + prog = t->prog;
> + callback_fn = t->callback_fn;
> +
> + /* wrap bpf subprog invocation with prog->refcnt++ and -- to make
> + * sure that refcnt doesn't become zero when subprog is executing.
> + * Do it under lock to make sure that bpf_timer_start doesn't drop
> + * prev prog refcnt to zero before timer_cb has a chance to bump it.
> + */
> + bpf_prog_inc(prog);
> + ____bpf_spin_unlock(&timer->lock);
> +
> + /* bpf_timer_cb() runs in hrtimer_run_softirq. It doesn't migrate and
> + * cannot be preempted by another bpf_timer_cb() on the same cpu.
> + * Remember the timer this callback is servicing to prevent
> + * deadlock if callback_fn() calls bpf_timer_cancel() on the same timer.
> + */
> + this_cpu_write(hrtimer_running, t);
> + if (map->map_type == BPF_MAP_TYPE_ARRAY) {
> + struct bpf_array *array = container_of(map, struct bpf_array, map);
> +
> + /* compute the key */
> + idx = ((char *)value - array->value) / array->elem_size;
> + key = &idx;
> + } else { /* hash or lru */
> + key = value - round_up(map->key_size, 8);
> + }
> +
> + ret = BPF_CAST_CALL(callback_fn)((u64)(long)map,
> + (u64)(long)key,
> + (u64)(long)value, 0, 0);
> + WARN_ON(ret != 0); /* Next patch moves this check into the verifier */
> + bpf_prog_put(prog);
> +
> + this_cpu_write(hrtimer_running, NULL);
> + return HRTIMER_NORESTART;
> +}
> +
Your patch is awesome, and I tried your example and it works, my
call_back() is called after a certained time. But I am confued that
the bpf_timer_cb() function only returns HRTIMER_NORESTART,
how to trigger a periodic task, that means I want to call my call_back()
periodically, not just for one time ?
[...]
--
Best Regards,
Lu Wei
Powered by blists - more mailing lists