Received: by 2002:a05:7412:1e0b:b0:fc:a2b0:25d7 with SMTP id kr11csp922888rdb; Thu, 15 Feb 2024 22:37:27 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCX9KREF8yCUleceaU7szHDG89/yA5SLbQzb0tlYmyPbBzUgzBM4+1EQ0mdVrMSUDOmKfBrK9CsG6tShl24mFepJN1sru3xtwKynfV835w== X-Google-Smtp-Source: AGHT+IH0b3G5GfWfe2mt32OwEb/c2zQ/92ywGr9RYeecF+XC33UrVKm2z1swaLTEoD+fM+WSjMgW X-Received: by 2002:a05:6808:f12:b0:3c1:3627:76c0 with SMTP id m18-20020a0568080f1200b003c1362776c0mr4914251oiw.12.1708065447674; Thu, 15 Feb 2024 22:37:27 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708065447; cv=pass; d=google.com; s=arc-20160816; b=zZMSGm0CAi1eyeui6ydUyByOZEXzSKS/DMAz8TZwI4r2tMIQw+bqvOih4b3HUq68Mq n95rxz5V+dEbUrRuw00IGlJ7RuW3cZnud2WC6jLRASBozArSY8QdRoGq8IzakERmSc5K JA5TaARqfvYDFZptWQGGMVeWprLfpvdOAey1hLgK9SmcwYxHkAVzdNjwfjgna6BqvfZa wcXFxiq37FOO8YJfiFzX1NAB/fxXsO4qJqJxeGe5D8AwkmUzxFMnHUTRgT4RM27hbA6O RJ+0BfBpQFBdjdVM3ep107sgc4vuUuAtyk82bxbygabidZJb4lDA+q/iNQ5+1wOEXJXM QdmA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:in-reply-to:from:references:cc:to :content-language:subject:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:date:dkim-signature:message-id; bh=Sbr4QPVSTcHUa+AezXbiMdDvlOjSGnMLEs0dFzkc8tc=; fh=qew4fHW2cX42pnRU8QxC4dTLGMJLbnH9VTT+7vqZ3GQ=; b=mUVJkoIvSA54aaaBxu4RH/taMoW1hYS3LSCSK+xRtoQ7Uq0bTCssMJHMNQC4gYXTTU spz5LTP2PnuyefeBHgj04XA6BXtdPZCoqmx/sCqJZTIq29Wp4esiRuVlW+HMfkT96z1W JvftB1YTwTfT0Iq9yozw4d4AR4jztAn4Jb9tfwv+takoO2xEnI68uBc5vwFtsCClTPFh TJbHDJPVXXcgirN0ck3aS1UqJZ0AfDXH/enO3eyaLuXJzC8RqsbV9IUD/HKI/BveoAC9 lN4lBu6NXgu+SU21h6CRCPb++lljM49SEyP6Mm9zrbKNQeADwblkim9I19cmEIlN5cl2 wr3A==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@linux.dev header.s=key1 header.b=tBnKIN5q; arc=pass (i=1 spf=pass spfdomain=linux.dev dkim=pass dkdomain=linux.dev dmarc=pass fromdomain=linux.dev); spf=pass (google.com: domain of linux-kernel+bounces-68109-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-68109-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.dev Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [147.75.48.161]) by mx.google.com with ESMTPS id w20-20020a639354000000b005cdfb96ea62si2457106pgm.243.2024.02.15.22.37.27 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 15 Feb 2024 22:37:27 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-68109-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) client-ip=147.75.48.161; Authentication-Results: mx.google.com; dkim=pass header.i=@linux.dev header.s=key1 header.b=tBnKIN5q; arc=pass (i=1 spf=pass spfdomain=linux.dev dkim=pass dkdomain=linux.dev dmarc=pass fromdomain=linux.dev); spf=pass (google.com: domain of linux-kernel+bounces-68109-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-68109-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.dev Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sy.mirrors.kernel.org (Postfix) with ESMTPS id 95F95B238DD for ; Fri, 16 Feb 2024 06:36:45 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id E3EA714A90; Fri, 16 Feb 2024 06:36:31 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b="tBnKIN5q" Received: from out-184.mta1.migadu.com (out-184.mta1.migadu.com [95.215.58.184]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 3D27A134A9 for ; Fri, 16 Feb 2024 06:36:29 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=95.215.58.184 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708065391; cv=none; b=kDjKJOh7hO5nrjTY5mioQsZS3Gsp+Y9Tb7knHUnquMgCheL5feem4ZinG4PsVajKAqD1qE+RvB0e0yhhqtaz0Y/8TBteIQ9jZxBPHTjUiwiP4QKnYdQfj6QzWuAx/tDc7ZFNd/YGV1xV0wM9GnD5pWdNAiGWoBy9dYnjpt6MWEE= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708065391; c=relaxed/simple; bh=8c1US9OauGX6MoQAcSIvGGnaJf+45MAgZiqviPZZqa0=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=IteYWb4m9FY5f9CE6aHntkWb1+PqGGZYTh2CnfH6HsF6jZ0mW3bW5XRh8r6yYwPnUpib27Lu063gAl73zaGYh1DcJDN+p5xrUr6MzIFFQxYG5rvNgz6bhhsQwRmD3uI99ExRZcf8MK8cPrW+KB1OWTwN3HpqaZyrx3uIQlafGTU= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b=tBnKIN5q; arc=none smtp.client-ip=95.215.58.184 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev Message-ID: DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1708065387; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=Sbr4QPVSTcHUa+AezXbiMdDvlOjSGnMLEs0dFzkc8tc=; b=tBnKIN5qT9O82LzS5wQNyKs9/9Jb+2Nc3qdyyuiBtmOVizx2dqWVe5a8J1MYRI23lPllFO j/SZsfaf+P6X/lHOIi+2MCjlLrur4E4RLAT/YArdbMOhYy0W/dVa7NmBcz64oHTYxea4q9 /yoOzUZ8yBH9dlUy1WcGM+fUCpQtCk8= Date: Thu, 15 Feb 2024 22:36:16 -0800 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Subject: Re: [PATCH RFC bpf-next v2 02/10] bpf/helpers: introduce sleepable timers Content-Language: en-US To: Benjamin Tissoires Cc: bpf@vger.kernel.org, linux-kernel@vger.kernel.org, linux-input@vger.kernel.org, linux-doc@vger.kernel.org, linux-kselftest@vger.kernel.org, Alexei Starovoitov , Daniel Borkmann , John Fastabend , Andrii Nakryiko , Eduard Zingerman , Song Liu , Yonghong Song , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , Jiri Kosina , Benjamin Tissoires , Jonathan Corbet , Shuah Khan References: <20240214-hid-bpf-sleepable-v2-0-5756b054724d@kernel.org> <20240214-hid-bpf-sleepable-v2-2-5756b054724d@kernel.org> X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. From: Martin KaFai Lau In-Reply-To: <20240214-hid-bpf-sleepable-v2-2-5756b054724d@kernel.org> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Migadu-Flow: FLOW_OUT On 2/14/24 9:18 AM, Benjamin Tissoires wrote: > +static void bpf_timer_work_cb(struct work_struct *work) > +{ > + struct bpf_hrtimer *t = container_of(work, struct bpf_hrtimer, work); > + struct bpf_map *map = t->map; > + void *value = t->value; > + bpf_callback_t callback_fn; > + void *key; > + u32 idx; > + > + BTF_TYPE_EMIT(struct bpf_timer); > + > + rcu_read_lock(); > + callback_fn = rcu_dereference(t->sleepable_cb_fn); > + rcu_read_unlock(); I took a very brief look at patch 2. One thing that may worth to ask here, the rcu_read_unlock() seems to be done too early. It is protecting the t->sleepable_cb_fn (?), so should it be done after finished using the callback_fn? A high level design question. The intention of the new bpf_timer_set_sleepable_cb() kfunc is actually to delay work to a workqueue. It is useful to delay work from the bpf_timer_cb and it may also useful to delay work from other bpf running context (e.g. the networking hooks like "tc"). The bpf_timer_set_sleepable_cb() seems to be unnecessary forcing delay-work must be done in a bpf_timer_cb. Have you thought about if it is possible to create a more generic kfunc like bpf_schedule_work() to delay work to a workqueue ? > + if (!callback_fn) > + return; > + > + /* FIXME: do we need any locking? */ > + 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); > + } > + > + /* FIXME: this crashes the system with > + * BUG: kernel NULL pointer dereference, address: 000000000000000b > + */ > + /* callback_fn((u64)(long)map, (u64)(long)key, (u64)(long)value, 0, 0); */ > + /* The verifier checked that return value is zero. */ > +} > + [ ... ] > +/* FIXME: use kernel doc style */ > +/* Description > + * Configure the timer to call *callback_fn* static function in a > + * sleepable context. > + * Return > + * 0 on success. > + * **-EINVAL** if *timer* was not initialized with bpf_timer_init() earlier. > + * **-EPERM** if *timer* is in a map that doesn't have any user references. > + * The user space should either hold a file descriptor to a map with timers > + * or pin such map in bpffs. When map is unpinned or file descriptor is > + * closed all timers in the map will be cancelled and freed. > + */ > +__bpf_kfunc int bpf_timer_set_sleepable_cb(struct bpf_timer_kern *timer, > + int (callback_fn)(void *map, int *key, struct bpf_timer *timer)) > +{ > + struct bpf_throw_ctx ctx = {}; > + > + /* FIXME: definietely not sure this is OK */ > + arch_bpf_stack_walk(bpf_stack_walker, &ctx); > + WARN_ON_ONCE(!ctx.aux); > + > + if (!ctx.aux) > + return -EINVAL; > + > + return __bpf_timer_set_callback(timer, (void *)callback_fn, ctx.aux, true); > +} > +