lists.openwall.net   lists  /  announce  owl-users  owl-dev  john-users  john-dev  passwdqc-users  yescrypt  popa3d-users  /  oss-security  kernel-hardening  musl  sabotage  tlsify  passwords  /  crypt-dev  xvendor  /  Bugtraq  Full-Disclosure  linux-kernel  linux-netdev  linux-ext4  linux-hardening  linux-cve-announce  PHC 
Open Source and information security mailing list archives
 
Hash Suite: Windows password security audit tool. GUI, reports in PDF.
[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <87eecbxx9l.fsf@toke.dk>
Date:   Tue, 06 Jul 2021 17:44:22 +0200
From:   Toke Høiland-Jørgensen <toke@...hat.com>
To:     "luwei (O)" <luwei32@...wei.com>,
        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.

"luwei (O)" <luwei32@...wei.com> writes:

> 在 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 ?

The callback just needs to reschedule itself by calling
bpf_timer_start() again before exiting... :)

-Toke

Powered by blists - more mailing lists

Powered by Openwall GNU/*/Linux Powered by OpenVZ