Received: by 2002:ab2:6203:0:b0:1f5:f2ab:c469 with SMTP id o3csp1135672lqt; Sat, 20 Apr 2024 02:10:21 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCVMMw/PsaMvVN4AtwG3SrCSjEIOPSVTrs7wa0mSruAcvwf+sg3RDAuESIw2H+aUa22IydfU3Ls9ztx8v3pgpyiocs8i+BJOUJG5VOPdqA== X-Google-Smtp-Source: AGHT+IGTN+WlIXDxvqLJHSkPxsUT7E19w06+m45NTMUjy8xoTp7m3V25tFBsOT36qDNsEpPG7z4y X-Received: by 2002:a0c:f28c:0:b0:6a0:544f:f608 with SMTP id k12-20020a0cf28c000000b006a0544ff608mr4957340qvl.14.1713604221746; Sat, 20 Apr 2024 02:10:21 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1713604221; cv=pass; d=google.com; s=arc-20160816; b=vr4Mxd3H3iWyagbwiZi+FTE7JY9LkwY0q3aJ5jmI8pj+izi7DBxi+Y1DqrBSIWO6Oj sdgegxA/goIQTIJrhm4z3ojtmLxx96FgEP3pBcJ14TDOrW3uNKICZ3Z/0RgCqkuRIoBZ /cq2OesxZsWtWwELfEvmRWZ6oATPe+G8uiKwHiBHJyd/eMYQuWtj3ub0DBdVRFZcoZSs 7rEk3NVK5EaYMmHowcBdANcQ1miu9+5N0zIeYlLCZb++cdefeTGWsSvbhR8zD4FyqFDo vVQO01dImywy+L2NCkyQe+JFzOlQoheefnclJoMOFTXtwj5chV9tt4M52+nski0dGUce zibQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:list-unsubscribe:list-subscribe:list-id:precedence :subject:date:from:dkim-signature; bh=0hTUrYvfTY3H77QVIQnr49RGWPdFUJFyJ2auxOem/hk=; fh=Cxwo/A+C89tjOzVq8R/o/DH+l+P6Lpt8VwbqwppZI0Y=; b=s3Pl8bChBnwg+Zr2HMrog2Y+a53dP05TNGwvsJudvmegDI5TYxpu2Q1ERFXv8QFGhc 19SG29jH4DqO5eawpU+BqRFBfoZBX/YmqY0dtRH67OnCye2WPkpmt1Lmle/+HmKdJwoU C3/CHkoNP08vM2VNdeSFluRZyOWDaOUxg3Wf657rz5Nz8eVgQxJ4g/xGBAOzhFXU+EfX 8Ja0ff3p7O4cQY9VZrQFuybT9F4Lgy3lZ5HdIJL/UoTwL1he6pwBcjQS1fxtgoLeuyh9 s+ewh4Rfc33AcS8BjQ4PgFntba13WJ8qd9CVoXoHwSHDuwXI6YDC+xjYuoEGWBZbakfZ 2rAA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=dYJsNppG; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-152186-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-152186-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 hf1-20020a0562140e8100b0069944521906si5895445qvb.58.2024.04.20.02.10.21 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 20 Apr 2024 02:10:21 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-152186-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=dYJsNppG; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-152186-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-152186-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 7AAF71C2142B for ; Sat, 20 Apr 2024 09:10:21 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 2D7772575A; Sat, 20 Apr 2024 09:09:36 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="dYJsNppG" 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 472F2156CF; Sat, 20 Apr 2024 09:09:34 +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=1713604175; cv=none; b=QMUkfaWqyCSlz6zrOUfv7pRcczkdOEWO70FwL17r1hIuwTN/Odm75vGn7hfFUsICNEQm6oWLmQCt7CD3VOaRCmFcjSfyz48q5zhyHffrn9DyU+ScLjE91dNoLwSkU0yBy+B8xm+4vtHNGz52qRvFqKm6XfE3VilfJemY/Mz3rSI= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713604175; c=relaxed/simple; bh=VQfGhwhrnwYRiFiAc2+faftcX/oGynkRaXtZ8qVn5Kc=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=F8X1Yk3fxMyJqgiC8xZf2Ck3kJeFIS8GeezOghAvvLM0RREmf3x5gv5xXzq8pKFJqIAry/TBqBg6uDJ0mpJcXCxPTU9h+K6trw7WOS872gaOFgE+hzSzEJTGjX67x418hYFn/+uxAkInkk8yqr6bd/rdYlRKHqBlIs8UXp/jhI4= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=dYJsNppG; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 5ED15C113CE; Sat, 20 Apr 2024 09:09:31 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1713604174; bh=VQfGhwhrnwYRiFiAc2+faftcX/oGynkRaXtZ8qVn5Kc=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=dYJsNppGP//IyC6bu86Vv1hmYR7WhpS55kn4Avd5m1WcUvghdTPOkxvn0bA5R/3Ch PkNmIPYRRabDb+KwKAKjKs8mpzzxuXZ8U311YjYdwPwSW7jczrCcp6O/R2AR+TtvWU lHB5HrgNIcBkemeX1KlFtOpk5PHxdWqds8d2R0NT6hHuUo16qrIiMjPOFcGgjI7Vog r+Cp5JyY7/v4+SXpHcnBrVV/jMjnx08arjDtR/qay7Vx2078OYbSAgPbbuYDV5gGjm DYjtOtKyu5sHaehDrqqc7fN/H69NzltMGNL73kF6/H4NV7g59P74iDFnQ0M8V1CC4L MRurIpZ0pY0HA== From: Benjamin Tissoires Date: Sat, 20 Apr 2024 11:09:03 +0200 Subject: [PATCH bpf-next v2 03/16] bpf: replace bpf_timer_set_callback with a generic helper 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="utf-8" Content-Transfer-Encoding: 7bit Message-Id: <20240420-bpf_wq-v2-3-6c986a5a741f@kernel.org> References: <20240420-bpf_wq-v2-0-6c986a5a741f@kernel.org> In-Reply-To: <20240420-bpf_wq-v2-0-6c986a5a741f@kernel.org> To: Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Martin KaFai Lau , Eduard Zingerman , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , Mykola Lysenko , Shuah Khan Cc: bpf@vger.kernel.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, Benjamin Tissoires X-Mailer: b4 0.12.4 X-Developer-Signature: v=1; a=ed25519-sha256; t=1713604159; l=2795; i=bentiss@kernel.org; s=20230215; h=from:subject:message-id; bh=VQfGhwhrnwYRiFiAc2+faftcX/oGynkRaXtZ8qVn5Kc=; b=4DwjQCnnRkxE8weXo846Osuldu+zxjvty7McBVDKbAca7xFVbkfSUjx+PM9mXnmY9PKGUaRzb vUrNMLKGlxNCeGa34TAYGiDkgNEWJioGtmRY6T5yYaqH6gq4r9u8DRZ X-Developer-Key: i=bentiss@kernel.org; a=ed25519; pk=7D1DyAVh6ajCkuUTudt/chMuXWIJHlv2qCsRkIizvFw= In the same way we have a generic __bpf_async_init(), we also need to share code between timer and workqueue for the set_callback call. We just add an unused flags parameter, as it will be used for workqueues. Signed-off-by: Benjamin Tissoires --- no changes in v2 --- kernel/bpf/helpers.c | 29 ++++++++++++++++++----------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/kernel/bpf/helpers.c b/kernel/bpf/helpers.c index 1e6d1011303b..d0a645b09d3d 100644 --- a/kernel/bpf/helpers.c +++ b/kernel/bpf/helpers.c @@ -1262,22 +1262,23 @@ static const struct bpf_func_proto bpf_timer_init_proto = { .arg3_type = ARG_ANYTHING, }; -BPF_CALL_3(bpf_timer_set_callback, struct bpf_async_kern *, timer, void *, callback_fn, - struct bpf_prog_aux *, aux) +static int __bpf_async_set_callback(struct bpf_async_kern *async, void *callback_fn, + struct bpf_prog_aux *aux, unsigned int flags, + enum bpf_async_type type) { struct bpf_prog *prev, *prog = aux->prog; - struct bpf_hrtimer *t; + struct bpf_async_cb *cb; int ret = 0; if (in_nmi()) return -EOPNOTSUPP; - __bpf_spin_lock_irqsave(&timer->lock); - t = timer->timer; - if (!t) { + __bpf_spin_lock_irqsave(&async->lock); + cb = async->cb; + if (!cb) { ret = -EINVAL; goto out; } - if (!atomic64_read(&t->cb.map->usercnt)) { + if (!atomic64_read(&cb->map->usercnt)) { /* maps with timers must be either held by user space * or pinned in bpffs. Otherwise timer might still be * running even when bpf prog is detached and user space @@ -1286,7 +1287,7 @@ BPF_CALL_3(bpf_timer_set_callback, struct bpf_async_kern *, timer, void *, callb ret = -EPERM; goto out; } - prev = t->cb.prog; + prev = cb->prog; if (prev != prog) { /* Bump prog refcnt once. Every bpf_timer_set_callback() * can pick different callback_fn-s within the same prog. @@ -1299,14 +1300,20 @@ BPF_CALL_3(bpf_timer_set_callback, struct bpf_async_kern *, timer, void *, callb if (prev) /* Drop prev prog refcnt when swapping with new prog */ bpf_prog_put(prev); - t->cb.prog = prog; + cb->prog = prog; } - rcu_assign_pointer(t->cb.callback_fn, callback_fn); + rcu_assign_pointer(cb->callback_fn, callback_fn); out: - __bpf_spin_unlock_irqrestore(&timer->lock); + __bpf_spin_unlock_irqrestore(&async->lock); return ret; } +BPF_CALL_3(bpf_timer_set_callback, struct bpf_async_kern *, timer, void *, callback_fn, + struct bpf_prog_aux *, aux) +{ + return __bpf_async_set_callback(timer, callback_fn, aux, 0, BPF_ASYNC_TYPE_TIMER); +} + static const struct bpf_func_proto bpf_timer_set_callback_proto = { .func = bpf_timer_set_callback, .gpl_only = true, -- 2.44.0