Received: by 2002:a05:7412:1e0b:b0:fc:a2b0:25d7 with SMTP id kr11csp960325rdb; Fri, 16 Feb 2024 00:21:52 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCVP1wRJRzh6gv1Mp7qyAYBpjC/E0CPP/sm3DxMnDTWTGqENHdyuDDrNGClkkEsJ8GsmXXCIKj6V7xHc8TGPOB/5Zm9AwzqkYISsHvRr/Q== X-Google-Smtp-Source: AGHT+IHLyRaqLgyazAZZM0iuT2TVp41l3jm5lr5G5ld7l/zO2IH5OD1f6RQ2/wjjFnxl7pxrxhsm X-Received: by 2002:a05:622a:1108:b0:42c:26b1:6af3 with SMTP id e8-20020a05622a110800b0042c26b16af3mr4264306qty.42.1708071712361; Fri, 16 Feb 2024 00:21:52 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708071712; cv=pass; d=google.com; s=arc-20160816; b=OKvaoYrDcqLwGHP2ULKYwnPBhB867wEN6jbrdXPSm6BDN0/vIn3vg7eZFP8m5E2uIF ko5OuBvjwiOx5g4il2w/P8v3u+yVKPqbhGD5FGEW5DaWhVYD+DUspvsEo5bBgYUyRq9c syS3bcaYBTTAs/KgjIAC9tyy+mfZevsD8QaktjDaVUUUl7AbV91TWAwONToT3FFlXBGN V3WV0p6lhOIlhrkcOZLxdLmszZWM/S0bG8uMAPmaWgVHeFYBaNSWEcgyc+Zvz5iywBsH aBqY650sByW0z0lDb915XR7HI0vVVGb4xgAW/XK3GUbN1OEsiE/zPbpwCi1IVDWu62oL k/SA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=in-reply-to:content-disposition:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:message-id:subject:cc :to:from:date:dkim-signature; bh=wMs8BMN6MWzNmwl/fPrJSx5B8wo6hGbTGonsnJ2MAnU=; fh=ICqoRu1U6FX8OLKKwJQ/s/8gCkvUtuTy+jYXTHIdD+o=; b=nU3lfRLFhcrgNUriX8FijYlA2yl35RLHlsnHfVnvSrFIIGngxLYTI6IwGhfQscGl/Y CqGF+KTJC7DxgbMsND0lvudgjEFLEUpFx0q14C7evKtcEN9izvNJ6jIgqOSn2ypk3RfT RtnhRmhuMzGzSZ8JFbWYyQtjjSOKh//nzFBXoF/SPoWlVMuJRhjKTvaiOCFAgupSGEp7 /XWlb14FZyCcLh/PDtHyNDEcwfwrEtL1XqMOWWdGZDwUJ10U2haLjAg3T3c8XxpU1H0P iMIBDvhcgoB6H2hGsZAk2XcCCDzbL5tsQuNJt4X5I2aCova5R2DbXlEoh85LYCrzYUWH hnJA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=Hhi5FVbw; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-68221-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-68221-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id u11-20020a05622a010b00b0042dc9057affsi3622514qtw.128.2024.02.16.00.21.52 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 16 Feb 2024 00:21:52 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-68221-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) client-ip=147.75.199.223; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=Hhi5FVbw; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-68221-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-68221-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org 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 ny.mirrors.kernel.org (Postfix) with ESMTPS id 142CF1C21153 for ; Fri, 16 Feb 2024 08:21:52 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 32F4B1B952; Fri, 16 Feb 2024 08:13:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="Hhi5FVbw" Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 349681B94A; Fri, 16 Feb 2024 08:13:16 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708071197; cv=none; b=Amo2pjih51gsyV+JiQKiklmfrfruJGQt5i6CaAbQf8iiqL9KPbl+h3qQXfxB2CAdvwTb4aBRFpAY5OkxaQX14YYf8ILV9l1yRse221yRoqjYvqZilfP7q6YjfxfnDuwIP+xPQkgt3yC5eK9nQxHVyharHileLm+bQ5U96OkQJTE= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708071197; c=relaxed/simple; bh=2yAzUFrshahCJDeF/Txc7JdTyHzEPst4ox4Ao1JajmU=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=ROD6BN9KPPb9VpUB1M7IyBIJC+6uR57uiwD3FIMS5cxSu/HBunSWah6gOTk/AaNqGFT8I5oJxTbCgtED+lUmf6raNXdS+QA5Onc/2rKcaQySvffccb/Y5vB61Ee8MaCJC/eEDEqOGMf6qkdLOBXkhpDji7d+tHeVCc7d+YEddnk= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=Hhi5FVbw; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 30CDEC433F1; Fri, 16 Feb 2024 08:13:08 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1708071196; bh=2yAzUFrshahCJDeF/Txc7JdTyHzEPst4ox4Ao1JajmU=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=Hhi5FVbwJBQGJrQ5LdUmS8j/Le4dlaV8h1U5wH5BlNyGQrrM2XqyMxRnsnAKFbMZx SFQaP2/aGi379Z5SHpZ7wDWSp5e8G7tgjc9AzIziwGmtse4q48U46iFQcjxylokg1P K/KhkeBJZ0tIrTsozcIJ3KwcSyKyhvNudlZ1i40K93iF9DdiWTEHMtM1Ctlw8Rn0FL 87TYT0ZzNruWA3tPglsbxs14kOOquqHn11S8ZJYb+kCD+bHh6l0sKaOq/r8DsSVhnE pjRSDqix+iI+/rl9GYYLZNkr/LrWhcipZI7K9C8xWWE9FHcnzdDXIKPhtSPxTBM90q HQKdGiuY7w7pQ== Date: Fri, 16 Feb 2024 09:13:03 +0100 From: Benjamin Tissoires To: Martin KaFai Lau 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 Subject: Re: [PATCH RFC bpf-next v2 02/10] bpf/helpers: introduce sleepable timers Message-ID: References: <20240214-hid-bpf-sleepable-v2-0-5756b054724d@kernel.org> <20240214-hid-bpf-sleepable-v2-2-5756b054724d@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: On Feb 15 2024, Martin KaFai Lau wrote: > 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? Probably :) TBH, everytime I work with RCUs I spent countless hours trying to re-understand everything, and in this case I'm currently in the "let's make it work" process than fixing concurrency issues. I still gave it a shot in case it solves my issue, but no, I still have the crash. But given that callback_fn might sleep, isn't it an issue to keep the RCU_reader lock so long? (we don't seem to call synchronize_rcu() so it might be fine, but I'd like the confirmation from someone else). > > 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. Basically I'm just a monkey here. I've been told that I should use bpf_timer[0]. But my implementation is not finished, as Alexei mentioned that we should bypass hrtimer if I'm not wrong [1]. > > Have you thought about if it is possible to create a more generic kfunc like > bpf_schedule_work() to delay work to a workqueue ? > AFAIU if we were to have a separate bpf_schedule_work(), we still need all of the infra of bpf_timer, because we need to keep the programs around in the same way bpf_timer does. So basically, bpf_timer will not only be about hrtimers, but anything that need to run an async callback. I submitted this RFC v2 not for the "this is ready", but mostly because there is a crash and I can't see where it comes from, and I suspect this is from a piece I do not understand (translation from the BPF langage into actual elf assembly). Cheers, Benjamin [0] https://lore.kernel.org/bpf/ztou4yyrsdfmmhdwgu2f2noartpqklhvtbw7vj2ptk54eqohvb@qci7bcnbd56q/T/#mc9cab17138b13c83299f0836ca0b2dde0643ea4b [1] https://lore.kernel.org/bpf/ztou4yyrsdfmmhdwgu2f2noartpqklhvtbw7vj2ptk54eqohvb@qci7bcnbd56q/T/#mf59824ad625992b980afbc4f27c83e76245815e7 > > > > + 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); > > +} > > + >