public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Benjamin Tissoires <bentiss@kernel.org>
To: Alexei Starovoitov <ast@kernel.org>,
	 Daniel Borkmann <daniel@iogearbox.net>,
	Andrii Nakryiko <andrii@kernel.org>,
	 Martin KaFai Lau <martin.lau@linux.dev>,
	 Eduard Zingerman <eddyz87@gmail.com>, Song Liu <song@kernel.org>,
	 Yonghong Song <yonghong.song@linux.dev>,
	 John Fastabend <john.fastabend@gmail.com>,
	KP Singh <kpsingh@kernel.org>,
	 Stanislav Fomichev <sdf@google.com>, Hao Luo <haoluo@google.com>,
	 Jiri Olsa <jolsa@kernel.org>, Mykola Lysenko <mykolal@fb.com>,
	 Shuah Khan <shuah@kernel.org>
Cc: bpf@vger.kernel.org, linux-kernel@vger.kernel.org,
	 linux-kselftest@vger.kernel.org,
	Benjamin Tissoires <bentiss@kernel.org>
Subject: [PATCH bpf-next v2 04/16] bpf: replace bpf_timer_cancel_and_free with a generic helper
Date: Sat, 20 Apr 2024 11:09:04 +0200	[thread overview]
Message-ID: <20240420-bpf_wq-v2-4-6c986a5a741f@kernel.org> (raw)
In-Reply-To: <20240420-bpf_wq-v2-0-6c986a5a741f@kernel.org>

Same reason than most bpf_timer* functions, we need almost the same for
workqueues.
So extract the generic part out of it so bpf_wq_cancel_and_free can reuse
it.

Signed-off-by: Benjamin Tissoires <bentiss@kernel.org>

---

no changes in v2
---
 kernel/bpf/helpers.c | 42 +++++++++++++++++++++++++-----------------
 1 file changed, 25 insertions(+), 17 deletions(-)

diff --git a/kernel/bpf/helpers.c b/kernel/bpf/helpers.c
index d0a645b09d3d..78847f444f79 100644
--- a/kernel/bpf/helpers.c
+++ b/kernel/bpf/helpers.c
@@ -1413,36 +1413,44 @@ static const struct bpf_func_proto bpf_timer_cancel_proto = {
 	.arg1_type	= ARG_PTR_TO_TIMER,
 };
 
-/* This function is called by map_delete/update_elem for individual element and
- * by ops->map_release_uref when the user space reference to a map reaches zero.
- */
-void bpf_timer_cancel_and_free(void *val)
+static struct bpf_async_cb *__bpf_async_cancel_and_free(struct bpf_async_kern *async)
 {
-	struct bpf_async_kern *timer = val;
-	struct bpf_hrtimer *t;
+	struct bpf_async_cb *cb;
 
-	/* Performance optimization: read timer->timer without lock first. */
-	if (!READ_ONCE(timer->timer))
-		return;
+	/* Performance optimization: read async->cb without lock first. */
+	if (!READ_ONCE(async->cb))
+		return NULL;
 
-	__bpf_spin_lock_irqsave(&timer->lock);
+	__bpf_spin_lock_irqsave(&async->lock);
 	/* re-read it under lock */
-	t = timer->timer;
-	if (!t)
+	cb = async->cb;
+	if (!cb)
 		goto out;
-	drop_prog_refcnt(&t->cb);
+	drop_prog_refcnt(cb);
 	/* The subsequent bpf_timer_start/cancel() helpers won't be able to use
 	 * this timer, since it won't be initialized.
 	 */
-	WRITE_ONCE(timer->timer, NULL);
+	WRITE_ONCE(async->cb, NULL);
 out:
-	__bpf_spin_unlock_irqrestore(&timer->lock);
+	__bpf_spin_unlock_irqrestore(&async->lock);
+	return cb;
+}
+
+/* This function is called by map_delete/update_elem for individual element and
+ * by ops->map_release_uref when the user space reference to a map reaches zero.
+ */
+void bpf_timer_cancel_and_free(void *val)
+{
+	struct bpf_hrtimer *t;
+
+	t = (struct bpf_hrtimer *)__bpf_async_cancel_and_free(val);
+
 	if (!t)
 		return;
 	/* Cancel the timer and wait for callback to complete if it was running.
 	 * If hrtimer_cancel() can be safely called it's safe to call kfree(t)
 	 * right after for both preallocated and non-preallocated maps.
-	 * The timer->timer = NULL was already done and no code path can
+	 * The async->cb = NULL was already done and no code path can
 	 * see address 't' anymore.
 	 *
 	 * Check that bpf_map_delete/update_elem() wasn't called from timer
@@ -1451,7 +1459,7 @@ void bpf_timer_cancel_and_free(void *val)
 	 * return -1). Though callback_fn is still running on this cpu it's
 	 * safe to do kfree(t) because bpf_timer_cb() read everything it needed
 	 * from 't'. The bpf subprog callback_fn won't be able to access 't',
-	 * since timer->timer = NULL was already done. The timer will be
+	 * since async->cb = NULL was already done. The timer will be
 	 * effectively cancelled because bpf_timer_cb() will return
 	 * HRTIMER_NORESTART.
 	 */

-- 
2.44.0


  parent reply	other threads:[~2024-04-20  9:09 UTC|newest]

Thread overview: 28+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-04-20  9:09 [PATCH bpf-next v2 00/16] Introduce bpf_wq Benjamin Tissoires
2024-04-20  9:09 ` [PATCH bpf-next v2 01/16] bpf: make timer data struct more generic Benjamin Tissoires
2024-04-20  9:09 ` [PATCH bpf-next v2 02/16] bpf: replace bpf_timer_init with a generic helper Benjamin Tissoires
2024-04-20  9:09 ` [PATCH bpf-next v2 03/16] bpf: replace bpf_timer_set_callback " Benjamin Tissoires
2024-04-20  9:09 ` Benjamin Tissoires [this message]
2024-04-20  9:09 ` [PATCH bpf-next v2 05/16] bpf: add support for bpf_wq user type Benjamin Tissoires
2024-04-20  9:09 ` [PATCH bpf-next v2 06/16] tools: sync include/uapi/linux/bpf.h Benjamin Tissoires
2024-04-20  9:09 ` [PATCH bpf-next v2 07/16] bpf: verifier: bail out if the argument is not a map Benjamin Tissoires
2024-04-20  9:09 ` [PATCH bpf-next v2 08/16] bpf: add support for KF_ARG_PTR_TO_WORKQUEUE Benjamin Tissoires
2024-04-20  9:09 ` [PATCH bpf-next v2 09/16] bpf: allow struct bpf_wq to be embedded in arraymaps and hashmaps Benjamin Tissoires
2024-04-24  2:51   ` Alexei Starovoitov
2024-04-20  9:09 ` [PATCH bpf-next v2 10/16] selftests/bpf: add bpf_wq tests Benjamin Tissoires
2024-04-24  2:56   ` Alexei Starovoitov
2024-04-24 21:07     ` Alexei Starovoitov
2024-04-20  9:09 ` [PATCH bpf-next v2 11/16] bpf: wq: add bpf_wq_init Benjamin Tissoires
2024-04-24  2:55   ` Alexei Starovoitov
2024-04-24 15:06     ` Alexei Starovoitov
2024-04-24 16:14       ` Alexei Starovoitov
2024-04-25  8:50         ` Benjamin Tissoires
2024-04-20  9:09 ` [PATCH bpf-next v2 12/16] selftests/bpf: wq: add bpf_wq_init() checks Benjamin Tissoires
2024-04-25  0:28   ` Andrii Nakryiko
2024-04-25  8:53     ` Benjamin Tissoires
2024-04-20  9:09 ` [PATCH bpf-next v2 13/16] bpf: wq: add bpf_wq_set_callback_impl Benjamin Tissoires
2024-04-24  2:56   ` Alexei Starovoitov
2024-04-20  9:09 ` [PATCH bpf-next v2 14/16] selftests/bpf: add checks for bpf_wq_set_callback() Benjamin Tissoires
2024-04-20  9:09 ` [PATCH bpf-next v2 15/16] bpf: add bpf_wq_start Benjamin Tissoires
2024-04-20  9:09 ` [PATCH bpf-next v2 16/16] selftests/bpf: wq: add bpf_wq_start() checks Benjamin Tissoires
2024-04-24  3:00 ` [PATCH bpf-next v2 00/16] Introduce bpf_wq patchwork-bot+netdevbpf

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20240420-bpf_wq-v2-4-6c986a5a741f@kernel.org \
    --to=bentiss@kernel.org \
    --cc=andrii@kernel.org \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=eddyz87@gmail.com \
    --cc=haoluo@google.com \
    --cc=john.fastabend@gmail.com \
    --cc=jolsa@kernel.org \
    --cc=kpsingh@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=martin.lau@linux.dev \
    --cc=mykolal@fb.com \
    --cc=sdf@google.com \
    --cc=shuah@kernel.org \
    --cc=song@kernel.org \
    --cc=yonghong.song@linux.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox