Skip to contentSkip to conversation

bpf: Introduce deferred task context execution

PATCH Superseded Mykyta Yatsenko 5 participants 18 messages Sep 18, 2025 – Sep 19, 2025

Subsystems (4)

v6 of 8 Superseded Latest is v8 →

lore
Share & tools
lore
#0 lore raw

v6 cover bpf: Introduce deferred task context execution

From: Mykyta Yatsenko <[email protected]>

This patch introduces a new mechanism for BPF programs to schedule deferred execution in the context of a specific task using the kernel’s task_work infrastructure.

The new bpf_task_work interface enables BPF use cases that require sleepable subprogram execution within task context, for example, scheduling sleepable function from the context that does not allow sleepable, such as NMI.

Introduced kfuncs bpf_task_work_schedule_signal() and bpf_task_work_schedule_resume() for scheduling BPF callbacks correspond to different modes used by task_work (TWA_SIGNAL or TWA_RESUME).

The implementation manages scheduling state via metadata objects (struct bpf_task_work_context). Pointers to bpf_task_work_context are stored in BPF map values. State transitions are handled via an atomic state machine (bpf_task_work_state) to ensure correctness under concurrent usage and deletion, lifetime is guarded by refcounting and RCU Tasks Trace.
Kfuncs call task_work_add() indirectly via irq_work to avoid locking in potentially NMI context.

Changelog:
---
v5 -> v6
v5: https://lore.kernel.org/bpf/[email protected]/
* Fixing readability in verifier.c:check_map_field_pointer()
* Removing BUG_ON from helpers.c

v4 -> v5
v4:
https://lore.kernel.org/all/[email protected]/
* Fix invalid/null pointer dereference bug, reported by syzbot
* Nits in selftests

v3 -> v4
v3: https://lore.kernel.org/all/[email protected]/
* Modify async callback return value processing in verifier, to allow
non-zero return values.
* Change return type of the callback from void to int, as verifier
expects scalar value.
* Switched to void* for bpf_map API kfunc arguments to avoid casts.
* Addressing numerous nits and small improvements.

v2 -> v3
v2: https://lore.kernel.org/all/[email protected]/
* Introduce ref counting
* Add patches with minor verifier and btf.c refactorings to avoid code
duplication
* Rework initiation of the task work scheduling to handle race with map
usercnt dropping to zero

Mykyta Yatsenko (8):
bpf: refactor special field-type detection
bpf: extract generic helper from process_timer_func()
bpf: htab: extract helper for freeing special structs
bpf: verifier: permit non-zero returns from async callbacks
bpf: bpf task work plumbing
bpf: extract map key pointer calculation
bpf: task work scheduling kfuncs
selftests/bpf: BPF task work scheduling tests

 include/linux/bpf.h                           |  11 +
 include/uapi/linux/bpf.h                      |   4 +
 kernel/bpf/arraymap.c                         |   8 +-
 kernel/bpf/btf.c                              |  63 ++-
 kernel/bpf/hashtab.c                          |  43 +-
 kernel/bpf/helpers.c                          | 377 +++++++++++++++++-
 kernel/bpf/syscall.c                          |  16 +-
 kernel/bpf/verifier.c                         | 168 +++++++-
 tools/include/uapi/linux/bpf.h                |   4 +
 .../selftests/bpf/prog_tests/test_task_work.c | 150 +++++++
 tools/testing/selftests/bpf/progs/task_work.c | 107 +++++
 .../selftests/bpf/progs/task_work_fail.c      |  96 +++++
 12 files changed, 950 insertions(+), 97 deletions(-)
 create mode 100644 tools/testing/selftests/bpf/prog_tests/test_task_work.c
 create mode 100644 tools/testing/selftests/bpf/progs/task_work.c
 create mode 100644 tools/testing/selftests/bpf/progs/task_work_fail.c
Signature · 1 line
2.51.0
#1 lore raw

PATCH v6 1/8 bpf: refactor special field-type detection

From: Mykyta Yatsenko <[email protected]>

Reduce code duplication in detection of the known special field types in map values. This refactoring helps to avoid copying a chunk of code in the next patch of the series.

---
 kernel/bpf/btf.c | 56 +++++++++++++++++-------------------------------
 1 file changed, 20 insertions(+), 36 deletions(-)
kernel/bpf/btf.c +20 -36
index 64739308902f..a1a9bc589518 100644
@@ -3488,44 +3488,28 @@ static int btf_get_field_type(const struct btf *btf, const struct btf_type *var_
 			      u32 field_mask, u32 *seen_mask,
 			      int *align, int *sz)
 {
-	int type = 0;
+	const struct {
+		enum btf_field_type type;
+		const char *const name;
+	} field_types[] = { { BPF_SPIN_LOCK, "bpf_spin_lock" },
+			    { BPF_RES_SPIN_LOCK, "bpf_res_spin_lock" },
+			    { BPF_TIMER, "bpf_timer" },
+			    { BPF_WORKQUEUE, "bpf_wq" }};
+	int type = 0, i;
 	const char *name = __btf_name_by_offset(btf, var_type->name_off);
+	const char *field_type_name;
+	enum btf_field_type field_type;
 
-	if (field_mask & BPF_SPIN_LOCK) {
-		if (!strcmp(name, "bpf_spin_lock")) {
-			if (*seen_mask & BPF_SPIN_LOCK)
-				return -E2BIG;
-			*seen_mask |= BPF_SPIN_LOCK;
-			type = BPF_SPIN_LOCK;
-			goto end;
-		}
-	}
-	if (field_mask & BPF_RES_SPIN_LOCK) {
-		if (!strcmp(name, "bpf_res_spin_lock")) {
-			if (*seen_mask & BPF_RES_SPIN_LOCK)
-				return -E2BIG;
-			*seen_mask |= BPF_RES_SPIN_LOCK;
-			type = BPF_RES_SPIN_LOCK;
-			goto end;
-		}
-	}
-	if (field_mask & BPF_TIMER) {
-		if (!strcmp(name, "bpf_timer")) {
-			if (*seen_mask & BPF_TIMER)
-				return -E2BIG;
-			*seen_mask |= BPF_TIMER;
-			type = BPF_TIMER;
-			goto end;
-		}
-	}
-	if (field_mask & BPF_WORKQUEUE) {
-		if (!strcmp(name, "bpf_wq")) {
-			if (*seen_mask & BPF_WORKQUEUE)
-				return -E2BIG;
-			*seen_mask |= BPF_WORKQUEUE;
-			type = BPF_WORKQUEUE;
-			goto end;
-		}
+	for (i = 0; i < ARRAY_SIZE(field_types); ++i) {
+		field_type = field_types[i].type;
+		field_type_name = field_types[i].name;
+		if (!(field_mask & field_type) || strcmp(name, field_type_name))
+			continue;
+		if (*seen_mask & field_type)
+			return -E2BIG;
+		*seen_mask |= field_type;
+		type = field_type;
+		goto end;
 	}
 	field_mask_test_name(BPF_LIST_HEAD, "bpf_list_head");
 	field_mask_test_name(BPF_LIST_NODE, "bpf_list_node");
Signature · 1 line
2.51.0
#2 lore raw

Re: [PATCH bpf-next v6 1/8] bpf: refactor special field-type detection

Quoted 71 lines · Mykyta Yatsenko

From: Mykyta Yatsenko <[email protected]>

Reduce code duplication in detection of the known special field types in map values. This refactoring helps to avoid copying a chunk of code in the next patch of the series.

---
  kernel/bpf/btf.c | 56 +++++++++++++++++-------------------------------
  1 file changed, 20 insertions(+), 36 deletions(-)
kernel/bpf/btf.c +12 -36
index 64739308902f..a1a9bc589518 100644
@@ -3488,44 +3488,28 @@ static int btf_get_field_type(const struct btf *btf, const struct btf_type *var_
  			      u32 field_mask, u32 *seen_mask,
  			      int *align, int *sz)
  {
-	int type = 0;
+	const struct {
+		enum btf_field_type type;
+		const char *const name;
+	} field_types[] = { { BPF_SPIN_LOCK, "bpf_spin_lock" },
+			    { BPF_RES_SPIN_LOCK, "bpf_res_spin_lock" },
+			    { BPF_TIMER, "bpf_timer" },
+			    { BPF_WORKQUEUE, "bpf_wq" }};
+	int type = 0, i;
  	const char *name = __btf_name_by_offset(btf, var_type->name_off);
+	const char *field_type_name;
+	enum btf_field_type field_type;
 
-	if (field_mask & BPF_SPIN_LOCK) {
-		if (!strcmp(name, "bpf_spin_lock")) {
-			if (*seen_mask & BPF_SPIN_LOCK)
-				return -E2BIG;
-			*seen_mask |= BPF_SPIN_LOCK;
-			type = BPF_SPIN_LOCK;
-			goto end;
-		}
-	}
-	if (field_mask & BPF_RES_SPIN_LOCK) {
-		if (!strcmp(name, "bpf_res_spin_lock")) {
-			if (*seen_mask & BPF_RES_SPIN_LOCK)
-				return -E2BIG;
-			*seen_mask |= BPF_RES_SPIN_LOCK;
-			type = BPF_RES_SPIN_LOCK;
-			goto end;
-		}
-	}
-	if (field_mask & BPF_TIMER) {
-		if (!strcmp(name, "bpf_timer")) {
-			if (*seen_mask & BPF_TIMER)
-				return -E2BIG;
-			*seen_mask |= BPF_TIMER;
-			type = BPF_TIMER;
-			goto end;
-		}
-	}
-	if (field_mask & BPF_WORKQUEUE) {
-		if (!strcmp(name, "bpf_wq")) {
-			if (*seen_mask & BPF_WORKQUEUE)
-				return -E2BIG;
-			*seen_mask |= BPF_WORKQUEUE;
-			type = BPF_WORKQUEUE;
-			goto end;
-		}
+	for (i = 0; i < ARRAY_SIZE(field_types); ++i) {
+		field_type = field_types[i].type;

kernel/bpf/btf.c static int btf_get_field_type(const struct btf *btf, const struct btf_type *var_ · line 3514

Quoted 10 lines
kernel/bpf/btf.c +8 -0
+		field_type_name = field_types[i].name;
+		if (!(field_mask & field_type) || strcmp(name, field_type_name))
+			continue;
+		if (*seen_mask & field_type)
+			return -E2BIG;
+		*seen_mask |= field_type;
+		type = field_type;
+		goto end;
  	}
  	field_mask_test_name(BPF_LIST_HEAD, "bpf_list_head");

How about extending the scope of the refactor by also handling btf_get_field_type in the loop? For example, add a "bool is_unique" to field_types and check seen_mask when is_unique == true.

Quoted 1 line
kernel/bpf/btf.c +0 -0
  	field_mask_test_name(BPF_LIST_NODE, "bpf_list_node");
#3 lore raw

Re: [PATCH bpf-next v6 1/8] bpf: refactor special field-type detection

Quoted 79 lines · Amery Hung
Quoted 76 lines · Mykyta Yatsenko

From: Mykyta Yatsenko <[email protected]>

Reduce code duplication in detection of the known special field types in map values. This refactoring helps to avoid copying a chunk of code in the next patch of the series.

---
  kernel/bpf/btf.c | 56 +++++++++++++++++-------------------------------

  1 file changed, 20 insertions(+), 36 deletions(-)
kernel/bpf/btf.c +0 -0
index 64739308902f..a1a9bc589518 100644
@@ -3488,44 +3488,28 @@ static int btf_get_field_type(const struct 

btf *btf, const struct btf_type *var_
                    u32 field_mask, u32 *seen_mask,
                    int *align, int *sz)
  {
-    int type = 0;
+    const struct {
+        enum btf_field_type type;
+        const char *const name;
+    } field_types[] = { { BPF_SPIN_LOCK, "bpf_spin_lock" }, +                { BPF_RES_SPIN_LOCK, "bpf_res_spin_lock" }, +                { BPF_TIMER, "bpf_timer" },
+                { BPF_WORKQUEUE, "bpf_wq" }};
+    int type = 0, i;
      const char *name = __btf_name_by_offset(btf, var_type->name_off); +    const char *field_type_name;
+    enum btf_field_type field_type;
  -    if (field_mask & BPF_SPIN_LOCK) {
-        if (!strcmp(name, "bpf_spin_lock")) {
-            if (*seen_mask & BPF_SPIN_LOCK)
-                return -E2BIG;
-            *seen_mask |= BPF_SPIN_LOCK;
-            type = BPF_SPIN_LOCK;
-            goto end;
-        }
-    }
-    if (field_mask & BPF_RES_SPIN_LOCK) {
-        if (!strcmp(name, "bpf_res_spin_lock")) {
-            if (*seen_mask & BPF_RES_SPIN_LOCK)
-                return -E2BIG;
-            *seen_mask |= BPF_RES_SPIN_LOCK;
-            type = BPF_RES_SPIN_LOCK;
-            goto end;
-        }
-    }
-    if (field_mask & BPF_TIMER) {
-        if (!strcmp(name, "bpf_timer")) {
-            if (*seen_mask & BPF_TIMER)
-                return -E2BIG;
-            *seen_mask |= BPF_TIMER;
-            type = BPF_TIMER;
-            goto end;
-        }
-    }
-    if (field_mask & BPF_WORKQUEUE) {
-        if (!strcmp(name, "bpf_wq")) {
-            if (*seen_mask & BPF_WORKQUEUE)
-                return -E2BIG;
-            *seen_mask |= BPF_WORKQUEUE;
-            type = BPF_WORKQUEUE;
-            goto end;
-        }
+    for (i = 0; i < ARRAY_SIZE(field_types); ++i) {
+        field_type = field_types[i].type;
+        field_type_name = field_types[i].name;
+        if (!(field_mask & field_type) || strcmp(name, field_type_name))
+            continue;
+        if (*seen_mask & field_type)

Quoted 8 lines
Quoted 6 lines

+            return -E2BIG;
+        *seen_mask |= field_type;
+        type = field_type;
+        goto end;
      }
      field_mask_test_name(BPF_LIST_HEAD, "bpf_list_head");

How about extending the scope of the refactor by also handling btf_get_field_type in the loop? For example, add a "bool is_unique" to field_types and check seen_mask when is_unique == true.

sounds good, I can include it in the next version.

Quoted 3 lines
Quoted 2 lines

field_mask_test_name(BPF_LIST_NODE, "bpf_list_node");

#4 lore raw

PATCH v6 2/8 bpf: extract generic helper from process_timer_func()

In reply to Mykyta Yatsenko

From: Mykyta Yatsenko <[email protected]>

Refactor the verifier by pulling the common logic from process_timer_func() into a dedicated helper. This allows reusing process_async_func() helper for verifying bpf_task_work struct in the next patch.

---
 kernel/bpf/verifier.c | 46 ++++++++++++++++++++++++++++++++-----------
 1 file changed, 35 insertions(+), 11 deletions(-)
kernel/bpf/verifier.c +35 -11
index b9394f8fac0e..c5a341ecbbaf 100644
@@ -8520,34 +8520,58 @@ static int process_spin_lock(struct bpf_verifier_env *env, int regno, int flags)
 	return 0;
 }
 
-static int process_timer_func(struct bpf_verifier_env *env, int regno,
-			      struct bpf_call_arg_meta *meta)
+/* Check if @regno is a pointer to a specific field in a map value */
+static int check_map_field_pointer(struct bpf_verifier_env *env, u32 regno,
+				   enum btf_field_type field_type)
 {
 	struct bpf_reg_state *regs = cur_regs(env), *reg = &regs[regno];
 	bool is_const = tnum_is_const(reg->var_off);
 	struct bpf_map *map = reg->map_ptr;
 	u64 val = reg->var_off.value;
+	const char *struct_name = btf_field_type_name(field_type);
+	int field_off = -1;
 
 	if (!is_const) {
 		verbose(env,
-			"R%d doesn't have constant offset. bpf_timer has to be at the constant offset\n",
-			regno);
+			"R%d doesn't have constant offset. %s has to be at the constant offset\n",
+			regno, struct_name);
 		return -EINVAL;
 	}
 	if (!map->btf) {
-		verbose(env, "map '%s' has to have BTF in order to use bpf_timer\n",
-			map->name);
+		verbose(env, "map '%s' has to have BTF in order to use %s\n", map->name,
+			struct_name);
 		return -EINVAL;
 	}
-	if (!btf_record_has_field(map->record, BPF_TIMER)) {
-		verbose(env, "map '%s' has no valid bpf_timer\n", map->name);
+	if (!btf_record_has_field(map->record, field_type)) {
+		verbose(env, "map '%s' has no valid %s\n", map->name, struct_name);
 		return -EINVAL;
 	}
-	if (map->record->timer_off != val + reg->off) {
-		verbose(env, "off %lld doesn't point to 'struct bpf_timer' that is at %d\n",
-			val + reg->off, map->record->timer_off);
+	switch (field_type) {
+	case BPF_TIMER:
+		field_off = map->record->timer_off;
+		break;
+	default:
+		break;
+	}
+	if (field_off != val + reg->off) {
+		verbose(env, "off %lld doesn't point to 'struct %s' that is at %d\n",
+			val + reg->off, struct_name, field_off);
 		return -EINVAL;
 	}
+	return 0;
+}
+
+static int process_timer_func(struct bpf_verifier_env *env, int regno,
+			      struct bpf_call_arg_meta *meta)
+{
+	struct bpf_reg_state *regs = cur_regs(env), *reg = &regs[regno];
+	struct bpf_map *map = reg->map_ptr;
+	int err;
+
+	err = check_map_field_pointer(env, regno, BPF_TIMER);
+	if (err)
+		return err;
+
 	if (meta->map_ptr) {
 		verifier_bug(env, "Two map pointers in a timer helper");
 		return -EFAULT;
Signature · 1 line
2.51.0
#5 lore raw

Re: [PATCH bpf-next v6 2/8] bpf: extract generic helper from process_timer_func()

Quoted 56 lines · Mykyta Yatsenko

From: Mykyta Yatsenko <[email protected]>

Refactor the verifier by pulling the common logic from process_timer_func() into a dedicated helper. This allows reusing process_async_func() helper for verifying bpf_task_work struct in the next patch.

---
 kernel/bpf/verifier.c | 46 ++++++++++++++++++++++++++++++++-----------
 1 file changed, 35 insertions(+), 11 deletions(-)
kernel/bpf/verifier.c +11 -8
index b9394f8fac0e..c5a341ecbbaf 100644
@@ -8520,34 +8520,58 @@ static int process_spin_lock(struct bpf_verifier_env *env, int regno, int flags)
        return 0;
 }
 
-static int process_timer_func(struct bpf_verifier_env *env, int regno,
-                             struct bpf_call_arg_meta *meta)
+/* Check if @regno is a pointer to a specific field in a map value */
+static int check_map_field_pointer(struct bpf_verifier_env *env, u32 regno,
+                                  enum btf_field_type field_type)
 {
        struct bpf_reg_state *regs = cur_regs(env), *reg = &regs[regno];
        bool is_const = tnum_is_const(reg->var_off);
        struct bpf_map *map = reg->map_ptr;
        u64 val = reg->var_off.value;
+       const char *struct_name = btf_field_type_name(field_type);
+       int field_off = -1;
 
        if (!is_const) {
                verbose(env,
-                       "R%d doesn't have constant offset. bpf_timer has to be at the constant offset\n",
-                       regno);
+                       "R%d doesn't have constant offset. %s has to be at the constant offset\n",
+                       regno, struct_name);
                return -EINVAL;
        }
        if (!map->btf) {
-               verbose(env, "map '%s' has to have BTF in order to use bpf_timer\n",
-                       map->name);
+               verbose(env, "map '%s' has to have BTF in order to use %s\n", map->name,
+                       struct_name);
                return -EINVAL;
        }
-       if (!btf_record_has_field(map->record, BPF_TIMER)) {
-               verbose(env, "map '%s' has no valid bpf_timer\n", map->name);
+       if (!btf_record_has_field(map->record, field_type)) {
+               verbose(env, "map '%s' has no valid %s\n", map->name, struct_name);

kernel/bpf/verifier.c static int process_spin_lock(struct bpf_verifier_env *env, int regno, int flags) · line 8553

Quoted 10 lines
kernel/bpf/verifier.c +5 -3
                return -EINVAL;
        }
-       if (map->record->timer_off != val + reg->off) {
-               verbose(env, "off %lld doesn't point to 'struct bpf_timer' that is at %d\n",
-                       val + reg->off, map->record->timer_off);
+       switch (field_type) {
+       case BPF_TIMER:
+               field_off = map->record->timer_off;
+               break;
+       default:

verbose(env, "WE FORGOT TO UPDATE THIS SWITCH?");
return -EINVAL;

kernel/bpf/verifier.c static int process_spin_lock(struct bpf_verifier_env *env, int regno, int flags) · line 8556

Quoted 3 lines
kernel/bpf/verifier.c +3 -0
+               break;
+       }
+       if (field_off != val + reg->off) {

can val + reg->off end up being equal to -1? Even if not, why do we need to worry about this?

Quoted 24 lines
kernel/bpf/verifier.c +16 -1
+               verbose(env, "off %lld doesn't point to 'struct %s' that is at %d\n",
+                       val + reg->off, struct_name, field_off);
                return -EINVAL;
        }
+       return 0;
+}
+
+static int process_timer_func(struct bpf_verifier_env *env, int regno,
+                             struct bpf_call_arg_meta *meta)
+{
+       struct bpf_reg_state *regs = cur_regs(env), *reg = &regs[regno];
+       struct bpf_map *map = reg->map_ptr;
+       int err;
+
+       err = check_map_field_pointer(env, regno, BPF_TIMER);
+       if (err)
+               return err;
+
        if (meta->map_ptr) {
                verifier_bug(env, "Two map pointers in a timer helper");
                return -EFAULT;
--

2.51.0

#6 lore raw

PATCH v6 3/8 bpf: htab: extract helper for freeing special structs

In reply to Mykyta Yatsenko

From: Mykyta Yatsenko <[email protected]>

Extract the cleanup of known embedded structs into the dedicated helper. Remove duplication and introduce a single source of truth for freeing special embedded structs in hashtab.

---
 kernel/bpf/hashtab.c | 24 ++++++++++++------------
 1 file changed, 12 insertions(+), 12 deletions(-)
kernel/bpf/hashtab.c +12 -12
index 71f9931ac64c..2319f8f8fa3e 100644
@@ -215,6 +215,16 @@ static bool htab_has_extra_elems(struct bpf_htab *htab)
 	return !htab_is_percpu(htab) && !htab_is_lru(htab) && !is_fd_htab(htab);
 }
 
+static void htab_free_internal_structs(struct bpf_htab *htab, struct htab_elem *elem)
+{
+	if (btf_record_has_field(htab->map.record, BPF_TIMER))
+		bpf_obj_free_timer(htab->map.record,
+				   htab_elem_value(elem, htab->map.key_size));
+	if (btf_record_has_field(htab->map.record, BPF_WORKQUEUE))
+		bpf_obj_free_workqueue(htab->map.record,
+				       htab_elem_value(elem, htab->map.key_size));
+}
+
 static void htab_free_prealloced_timers_and_wq(struct bpf_htab *htab)
 {
 	u32 num_entries = htab->map.max_entries;
@@ -227,12 +237,7 @@ static void htab_free_prealloced_timers_and_wq(struct bpf_htab *htab)
 		struct htab_elem *elem;
 
 		elem = get_htab_elem(htab, i);
-		if (btf_record_has_field(htab->map.record, BPF_TIMER))
-			bpf_obj_free_timer(htab->map.record,
-					   htab_elem_value(elem, htab->map.key_size));
-		if (btf_record_has_field(htab->map.record, BPF_WORKQUEUE))
-			bpf_obj_free_workqueue(htab->map.record,
-					       htab_elem_value(elem, htab->map.key_size));
+		htab_free_internal_structs(htab, elem);
 		cond_resched();
 	}
 }
@@ -1502,12 +1507,7 @@ static void htab_free_malloced_timers_and_wq(struct bpf_htab *htab)
 
 		hlist_nulls_for_each_entry(l, n, head, hash_node) {
 			/* We only free timer on uref dropping to zero */
-			if (btf_record_has_field(htab->map.record, BPF_TIMER))
-				bpf_obj_free_timer(htab->map.record,
-						   htab_elem_value(l, htab->map.key_size));
-			if (btf_record_has_field(htab->map.record, BPF_WORKQUEUE))
-				bpf_obj_free_workqueue(htab->map.record,
-						       htab_elem_value(l, htab->map.key_size));
+			htab_free_internal_structs(htab, l);
 		}
 		cond_resched_rcu();
 	}
Signature · 1 line
2.51.0
#7 lore raw

PATCH v6 4/8 bpf: verifier: permit non-zero returns from async callbacks

In reply to Mykyta Yatsenko

From: Mykyta Yatsenko <[email protected]>

The verifier currently enforces a zero return value for all async callbacks—a constraint originally introduced for bpf_timer. That restriction is too narrow for other async use cases.

Relax the rule by allowing non-zero return codes from async callbacks in general, while preserving the zero-return requirement for bpf_timer to maintain its existing semantics.

---
 kernel/bpf/verifier.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
kernel/bpf/verifier.c +2 -3
index c5a341ecbbaf..f760b16b9b3f 100644
@@ -10871,7 +10871,7 @@ static int set_timer_callback_state(struct bpf_verifier_env *env,
 	__mark_reg_not_init(env, &callee->regs[BPF_REG_4]);
 	__mark_reg_not_init(env, &callee->regs[BPF_REG_5]);
 	callee->in_async_callback_fn = true;
-	callee->callback_ret_range = retval_range(0, 1);
+	callee->callback_ret_range = retval_range(0, 0);
 	return 0;
 }
 
@@ -17156,9 +17156,8 @@ static int check_return_code(struct bpf_verifier_env *env, int regno, const char
 	}
 
 	if (frame->in_async_callback_fn) {
-		/* enforce return zero from async callbacks like timer */
 		exit_ctx = "At async callback return";
-		range = retval_range(0, 0);
+		range = frame->callback_ret_range;
 		goto enforce_retval;
 	}
 
Signature · 1 line
2.51.0
#8 lore raw

PATCH v6 5/8 bpf: bpf task work plumbing

In reply to Mykyta Yatsenko

From: Mykyta Yatsenko <[email protected]>

This patch adds necessary plumbing in verifier, syscall and maps to support handling new kfunc bpf_task_work_schedule and kernel structure bpf_task_work. The idea is similar to how we already handle bpf_wq and bpf_timer.
verifier changes validate calls to bpf_task_work_schedule to make sure it is safe and expected invariants hold.
btf part is required to detect bpf_task_work structure inside map value and store its offset, which will be used in the next patch to calculate key and value addresses.
arraymap and hashtab changes are needed to handle freeing of the
bpf_task_work: run code needed to deinitialize it, for example cancel task_work callback if possible.
The use of bpf_task_work and proper implementation for kfuncs are introduced in the next patch.

---
 include/linux/bpf.h            |  11 ++++
 include/uapi/linux/bpf.h       |   4 ++
 kernel/bpf/arraymap.c          |   8 ++-
 kernel/bpf/btf.c               |   9 ++-
 kernel/bpf/hashtab.c           |  19 +++---
 kernel/bpf/helpers.c           |  40 +++++++++++
 kernel/bpf/syscall.c           |  16 ++++-
 kernel/bpf/verifier.c          | 117 +++++++++++++++++++++++++++++++--
 tools/include/uapi/linux/bpf.h |   4 ++
 9 files changed, 209 insertions(+), 19 deletions(-)
include/linux/bpf.h +11 -0
index 8f6e87f0f3a8..febb4ca68401 100644
@@ -206,6 +206,7 @@ enum btf_field_type {
 	BPF_WORKQUEUE  = (1 << 10),
 	BPF_UPTR       = (1 << 11),
 	BPF_RES_SPIN_LOCK = (1 << 12),
+	BPF_TASK_WORK  = (1 << 13),
 };
 
 enum bpf_cgroup_storage_type {
@@ -259,6 +260,7 @@ struct btf_record {
 	int timer_off;
 	int wq_off;
 	int refcount_off;
+	int task_work_off;
 	struct btf_field fields[];
 };
 
@@ -358,6 +360,8 @@ static inline const char *btf_field_type_name(enum btf_field_type type)
 		return "bpf_rb_node";
 	case BPF_REFCOUNT:
 		return "bpf_refcount";
+	case BPF_TASK_WORK:
+		return "bpf_task_work";
 	default:
 		WARN_ON_ONCE(1);
 		return "unknown";
@@ -396,6 +400,8 @@ static inline u32 btf_field_type_size(enum btf_field_type type)
 		return sizeof(struct bpf_rb_node);
 	case BPF_REFCOUNT:
 		return sizeof(struct bpf_refcount);
+	case BPF_TASK_WORK:
+		return sizeof(struct bpf_task_work);
 	default:
 		WARN_ON_ONCE(1);
 		return 0;
@@ -428,6 +434,8 @@ static inline u32 btf_field_type_align(enum btf_field_type type)
 		return __alignof__(struct bpf_rb_node);
 	case BPF_REFCOUNT:
 		return __alignof__(struct bpf_refcount);
+	case BPF_TASK_WORK:
+		return __alignof__(struct bpf_task_work);
 	default:
 		WARN_ON_ONCE(1);
 		return 0;
@@ -459,6 +467,7 @@ static inline void bpf_obj_init_field(const struct btf_field *field, void *addr)
 	case BPF_KPTR_REF:
 	case BPF_KPTR_PERCPU:
 	case BPF_UPTR:
+	case BPF_TASK_WORK:
 		break;
 	default:
 		WARN_ON_ONCE(1);
@@ -595,6 +604,7 @@ void copy_map_value_locked(struct bpf_map *map, void *dst, void *src,
 			   bool lock_src);
 void bpf_timer_cancel_and_free(void *timer);
 void bpf_wq_cancel_and_free(void *timer);
+void bpf_task_work_cancel_and_free(void *timer);
 void bpf_list_head_free(const struct btf_field *field, void *list_head,
 			struct bpf_spin_lock *spin_lock);
 void bpf_rb_root_free(const struct btf_field *field, void *rb_root,
@@ -2417,6 +2427,7 @@ struct btf_record *btf_record_dup(const struct btf_record *rec);
 bool btf_record_equal(const struct btf_record *rec_a, const struct btf_record *rec_b);
 void bpf_obj_free_timer(const struct btf_record *rec, void *obj);
 void bpf_obj_free_workqueue(const struct btf_record *rec, void *obj);
+void bpf_obj_free_task_work(const struct btf_record *rec, void *obj);
 void bpf_obj_free_fields(const struct btf_record *rec, void *obj);
 void __bpf_obj_drop_impl(void *p, const struct btf_record *rec, bool percpu);
 
include/uapi/linux/bpf.h +4 -0
index 233de8677382..5b42faff9aeb 100644
@@ -7418,6 +7418,10 @@ struct bpf_timer {
 	__u64 __opaque[2];
 } __attribute__((aligned(8)));
 
+struct bpf_task_work {
+	__u64 __opaque;
+} __attribute__((aligned(8)));
+
 struct bpf_wq {
 	__u64 __opaque[2];
 } __attribute__((aligned(8)));
kernel/bpf/arraymap.c +5 -3
index 3d080916faf9..4130d8e76dff 100644
@@ -431,7 +431,7 @@ static void *array_map_vmalloc_addr(struct bpf_array *array)
 	return (void *)round_down((unsigned long)array, PAGE_SIZE);
 }
 
-static void array_map_free_timers_wq(struct bpf_map *map)
+static void array_map_free_internal_structs(struct bpf_map *map)
 {
 	struct bpf_array *array = container_of(map, struct bpf_array, map);
 	int i;
@@ -439,12 +439,14 @@ static void array_map_free_timers_wq(struct bpf_map *map)
 	/* We don't reset or free fields other than timer and workqueue
 	 * on uref dropping to zero.
 	 */
-	if (btf_record_has_field(map->record, BPF_TIMER | BPF_WORKQUEUE)) {
+	if (btf_record_has_field(map->record, BPF_TIMER | BPF_WORKQUEUE | BPF_TASK_WORK)) {
 		for (i = 0; i < array->map.max_entries; i++) {
 			if (btf_record_has_field(map->record, BPF_TIMER))
 				bpf_obj_free_timer(map->record, array_map_elem_ptr(array, i));
 			if (btf_record_has_field(map->record, BPF_WORKQUEUE))
 				bpf_obj_free_workqueue(map->record, array_map_elem_ptr(array, i));
+			if (btf_record_has_field(map->record, BPF_TASK_WORK))
+				bpf_obj_free_task_work(map->record, array_map_elem_ptr(array, i));
 		}
 	}
 }
@@ -783,7 +785,7 @@ const struct bpf_map_ops array_map_ops = {
 	.map_alloc = array_map_alloc,
 	.map_free = array_map_free,
 	.map_get_next_key = array_map_get_next_key,
-	.map_release_uref = array_map_free_timers_wq,
+	.map_release_uref = array_map_free_internal_structs,
 	.map_lookup_elem = array_map_lookup_elem,
 	.map_update_elem = array_map_update_elem,
 	.map_delete_elem = array_map_delete_elem,
kernel/bpf/btf.c +8 -1
index a1a9bc589518..95933d8a5fd0 100644
@@ -3494,7 +3494,8 @@ static int btf_get_field_type(const struct btf *btf, const struct btf_type *var_
 	} field_types[] = { { BPF_SPIN_LOCK, "bpf_spin_lock" },
 			    { BPF_RES_SPIN_LOCK, "bpf_res_spin_lock" },
 			    { BPF_TIMER, "bpf_timer" },
-			    { BPF_WORKQUEUE, "bpf_wq" }};
+			    { BPF_WORKQUEUE, "bpf_wq" },
+			    { BPF_TASK_WORK, "bpf_task_work" } };
 	int type = 0, i;
 	const char *name = __btf_name_by_offset(btf, var_type->name_off);
 	const char *field_type_name;
@@ -3677,6 +3678,7 @@ static int btf_find_field_one(const struct btf *btf,
 	case BPF_LIST_NODE:
 	case BPF_RB_NODE:
 	case BPF_REFCOUNT:
+	case BPF_TASK_WORK:
 		ret = btf_find_struct(btf, var_type, off, sz, field_type,
 				      info_cnt ? &info[0] : &tmp);
 		if (ret < 0)
@@ -3969,6 +3971,7 @@ struct btf_record *btf_parse_fields(const struct btf *btf, const struct btf_type
 	rec->timer_off = -EINVAL;
 	rec->wq_off = -EINVAL;
 	rec->refcount_off = -EINVAL;
+	rec->task_work_off = -EINVAL;
 	for (i = 0; i < cnt; i++) {
 		field_type_size = btf_field_type_size(info_arr[i].type);
 		if (info_arr[i].off + field_type_size > value_size) {
@@ -4008,6 +4011,10 @@ struct btf_record *btf_parse_fields(const struct btf *btf, const struct btf_type
 			/* Cache offset for faster lookup at runtime */
 			rec->wq_off = rec->fields[i].offset;
 			break;
+		case BPF_TASK_WORK:
+			WARN_ON_ONCE(rec->task_work_off >= 0);
+			rec->task_work_off = rec->fields[i].offset;
+			break;
 		case BPF_REFCOUNT:
 			WARN_ON_ONCE(rec->refcount_off >= 0);
 			/* Cache offset for faster lookup at runtime */
kernel/bpf/hashtab.c +11 -8
index 2319f8f8fa3e..c2fcd0cd51e5 100644
@@ -223,9 +223,12 @@ static void htab_free_internal_structs(struct bpf_htab *htab, struct htab_elem *
 	if (btf_record_has_field(htab->map.record, BPF_WORKQUEUE))
 		bpf_obj_free_workqueue(htab->map.record,
 				       htab_elem_value(elem, htab->map.key_size));
+	if (btf_record_has_field(htab->map.record, BPF_TASK_WORK))
+		bpf_obj_free_task_work(htab->map.record,
+				       htab_elem_value(elem, htab->map.key_size));
 }
 
-static void htab_free_prealloced_timers_and_wq(struct bpf_htab *htab)
+static void htab_free_prealloced_internal_structs(struct bpf_htab *htab)
 {
 	u32 num_entries = htab->map.max_entries;
 	int i;
@@ -1495,7 +1498,7 @@ static void delete_all_elements(struct bpf_htab *htab)
 	}
 }
 
-static void htab_free_malloced_timers_and_wq(struct bpf_htab *htab)
+static void htab_free_malloced_internal_structs(struct bpf_htab *htab)
 {
 	int i;
 
@@ -1514,16 +1517,16 @@ static void htab_free_malloced_timers_and_wq(struct bpf_htab *htab)
 	rcu_read_unlock();
 }
 
-static void htab_map_free_timers_and_wq(struct bpf_map *map)
+static void htab_map_free_internal_structs(struct bpf_map *map)
 {
 	struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
 
 	/* We only free timer and workqueue on uref dropping to zero */
-	if (btf_record_has_field(htab->map.record, BPF_TIMER | BPF_WORKQUEUE)) {
+	if (btf_record_has_field(htab->map.record, BPF_TIMER | BPF_WORKQUEUE | BPF_TASK_WORK)) {
 		if (!htab_is_prealloc(htab))
-			htab_free_malloced_timers_and_wq(htab);
+			htab_free_malloced_internal_structs(htab);
 		else
-			htab_free_prealloced_timers_and_wq(htab);
+			htab_free_prealloced_internal_structs(htab);
 	}
 }
 
@@ -2255,7 +2258,7 @@ const struct bpf_map_ops htab_map_ops = {
 	.map_alloc = htab_map_alloc,
 	.map_free = htab_map_free,
 	.map_get_next_key = htab_map_get_next_key,
-	.map_release_uref = htab_map_free_timers_and_wq,
+	.map_release_uref = htab_map_free_internal_structs,
 	.map_lookup_elem = htab_map_lookup_elem,
 	.map_lookup_and_delete_elem = htab_map_lookup_and_delete_elem,
 	.map_update_elem = htab_map_update_elem,
@@ -2276,7 +2279,7 @@ const struct bpf_map_ops htab_lru_map_ops = {
 	.map_alloc = htab_map_alloc,
 	.map_free = htab_map_free,
 	.map_get_next_key = htab_map_get_next_key,
-	.map_release_uref = htab_map_free_timers_and_wq,
+	.map_release_uref = htab_map_free_internal_structs,
 	.map_lookup_elem = htab_lru_map_lookup_elem,
 	.map_lookup_and_delete_elem = htab_lru_map_lookup_and_delete_elem,
 	.map_lookup_elem_sys_only = htab_lru_map_lookup_elem_sys,
kernel/bpf/helpers.c +40 -0
index 588bc7e36436..0fddf7d0954b 100644
@@ -3738,8 +3738,48 @@ __bpf_kfunc int bpf_strstr(const char *s1__ign, const char *s2__ign)
 	return bpf_strnstr(s1__ign, s2__ign, XATTR_SIZE_MAX);
 }
 
+typedef int (*bpf_task_work_callback_t)(struct bpf_map *map, void *key, void *value);
+
+/**
+ * bpf_task_work_schedule_signal - Schedule BPF callback using task_work_add with TWA_SIGNAL mode
+ * @task: Task struct for which callback should be scheduled
+ * @tw: Pointer to struct bpf_task_work in BPF map value for internal bookkeeping
+ * @map__map: bpf_map that embeds struct bpf_task_work in the values
+ * @callback: pointer to BPF subprogram to call
+ * @aux__prog: user should pass NULL
+ *
+ * Return: 0 if task work has been scheduled successfully, negative error code otherwise
+ */
+__bpf_kfunc int bpf_task_work_schedule_signal(struct task_struct *task, struct bpf_task_work *tw,
+					      void *map__map, bpf_task_work_callback_t callback,
+					      void *aux__prog)
+{
+	return 0;
+}
+
+/**
+ * bpf_task_work_schedule_resume - Schedule BPF callback using task_work_add with TWA_RESUME mode
+ * @task: Task struct for which callback should be scheduled
+ * @tw: Pointer to struct bpf_task_work in BPF map value for internal bookkeeping
+ * @map__map: bpf_map that embeds struct bpf_task_work in the values
+ * @callback: pointer to BPF subprogram to call
+ * @aux__prog: user should pass NULL
+ *
+ * Return: 0 if task work has been scheduled successfully, negative error code otherwise
+ */
+__bpf_kfunc int bpf_task_work_schedule_resume(struct task_struct *task, struct bpf_task_work *tw,
+					      void *map__map, bpf_task_work_callback_t callback,
+					      void *aux__prog)
+{
+	return 0;
+}
+
 __bpf_kfunc_end_defs();
 
+void bpf_task_work_cancel_and_free(void *val)
+{
+}
+
 BTF_KFUNCS_START(generic_btf_ids)
 #ifdef CONFIG_CRASH_DUMP
 BTF_ID_FLAGS(func, crash_kexec, KF_DESTRUCTIVE)
kernel/bpf/syscall.c +15 -1
index 0fbfa8532c39..b099ea40532e 100644
@@ -672,6 +672,7 @@ void btf_record_free(struct btf_record *rec)
 		case BPF_TIMER:
 		case BPF_REFCOUNT:
 		case BPF_WORKQUEUE:
+		case BPF_TASK_WORK:
 			/* Nothing to release */
 			break;
 		default:
@@ -725,6 +726,7 @@ struct btf_record *btf_record_dup(const struct btf_record *rec)
 		case BPF_TIMER:
 		case BPF_REFCOUNT:
 		case BPF_WORKQUEUE:
+		case BPF_TASK_WORK:
 			/* Nothing to acquire */
 			break;
 		default:
@@ -783,6 +785,13 @@ void bpf_obj_free_workqueue(const struct btf_record *rec, void *obj)
 	bpf_wq_cancel_and_free(obj + rec->wq_off);
 }
 
+void bpf_obj_free_task_work(const struct btf_record *rec, void *obj)
+{
+	if (WARN_ON_ONCE(!btf_record_has_field(rec, BPF_TASK_WORK)))
+		return;
+	bpf_task_work_cancel_and_free(obj + rec->task_work_off);
+}
+
 void bpf_obj_free_fields(const struct btf_record *rec, void *obj)
 {
 	const struct btf_field *fields;
@@ -807,6 +816,9 @@ void bpf_obj_free_fields(const struct btf_record *rec, void *obj)
 		case BPF_WORKQUEUE:
 			bpf_wq_cancel_and_free(field_ptr);
 			break;
+		case BPF_TASK_WORK:
+			bpf_task_work_cancel_and_free(field_ptr);
+			break;
 		case BPF_KPTR_UNREF:
 			WRITE_ONCE(*(u64 *)field_ptr, 0);
 			break;
@@ -1237,7 +1249,8 @@ static int map_check_btf(struct bpf_map *map, struct bpf_token *token,
 
 	map->record = btf_parse_fields(btf, value_type,
 				       BPF_SPIN_LOCK | BPF_RES_SPIN_LOCK | BPF_TIMER | BPF_KPTR | BPF_LIST_HEAD |
-				       BPF_RB_ROOT | BPF_REFCOUNT | BPF_WORKQUEUE | BPF_UPTR,
+				       BPF_RB_ROOT | BPF_REFCOUNT | BPF_WORKQUEUE | BPF_UPTR |
+				       BPF_TASK_WORK,
 				       map->value_size);
 	if (!IS_ERR_OR_NULL(map->record)) {
 		int i;
@@ -1269,6 +1282,7 @@ static int map_check_btf(struct bpf_map *map, struct bpf_token *token,
 				break;
 			case BPF_TIMER:
 			case BPF_WORKQUEUE:
+			case BPF_TASK_WORK:
 				if (map->map_type != BPF_MAP_TYPE_HASH &&
 				    map->map_type != BPF_MAP_TYPE_LRU_HASH &&
 				    map->map_type != BPF_MAP_TYPE_ARRAY) {
kernel/bpf/verifier.c +111 -6

232 lines not on this page · raw message

tools/include/uapi/linux/bpf.h +4 -0
index 233de8677382..5b42faff9aeb 100644
@@ -7418,6 +7418,10 @@ struct bpf_timer {
 	__u64 __opaque[2];
 } __attribute__((aligned(8)));
 
+struct bpf_task_work {
+	__u64 __opaque;
+} __attribute__((aligned(8)));
+
 struct bpf_wq {
 	__u64 __opaque[2];
 } __attribute__((aligned(8)));
Signature · 1 line
2.51.0
#9 lore raw

PATCH v6 6/8 bpf: extract map key pointer calculation

In reply to Mykyta Yatsenko

From: Mykyta Yatsenko <[email protected]>

Calculation of the BPF map key, given the pointer to a value is duplicated in a couple of places in helpers already, in the next patch another use case is introduced as well.
This patch extracts that functionality into a separate function.

---
 kernel/bpf/helpers.c | 30 +++++++++++++-----------------
 1 file changed, 13 insertions(+), 17 deletions(-)
kernel/bpf/helpers.c +13 -17
index 0fddf7d0954b..2d6a2fe6c374 100644
@@ -1081,6 +1081,17 @@ const struct bpf_func_proto bpf_snprintf_proto = {
 	.arg5_type	= ARG_CONST_SIZE_OR_ZERO,
 };
 
+static void *map_key_from_value(struct bpf_map *map, void *value, u32 *arr_idx)
+{
+	if (map->map_type == BPF_MAP_TYPE_ARRAY) {
+		struct bpf_array *array = container_of(map, struct bpf_array, map);
+
+		*arr_idx = ((char *)value - array->value) / array->elem_size;
+		return arr_idx;
+	}
+	return (void *)value - round_up(map->key_size, 8);
+}
+
 struct bpf_async_cb {
 	struct bpf_map *map;
 	struct bpf_prog *prog;
@@ -1163,15 +1174,8 @@ static enum hrtimer_restart bpf_timer_cb(struct hrtimer *hrtimer)
 	 * bpf_map_delete_elem() 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);
-	}
+	key = map_key_from_value(map, value, &idx);
 
 	callback_fn((u64)(long)map, (u64)(long)key, (u64)(long)value, 0, 0);
 	/* The verifier checked that return value is zero. */
@@ -1197,15 +1201,7 @@ static void bpf_wq_work(struct work_struct *work)
 	if (!callback_fn)
 		return;
 
-	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);
-	}
+	key = map_key_from_value(map, value, &idx);
 
         rcu_read_lock_trace();
         migrate_disable();
Signature · 1 line
2.51.0
#10 lore raw

PATCH v6 7/8 bpf: task work scheduling kfuncs

In reply to Mykyta Yatsenko

From: Mykyta Yatsenko <[email protected]>

Implementation of the new bpf_task_work_schedule kfuncs, that let a BPF program schedule task_work callbacks for a target task:
* bpf_task_work_schedule_signal() - schedules with TWA_SIGNAL
* bpf_task_work_schedule_resume() - schedules with TWA_RESUME

Each map value should embed a struct bpf_task_work, which the kernel side pairs with struct bpf_task_work_kern, containing a pointer to struct bpf_task_work_ctx, that maintains metadata relevant for the concrete callback scheduling.

A small state machine and refcounting scheme ensures safe reuse and teardown. State transitions:

    _______________________________
    |                             |
    v                             |
[standby] ---> [pending] --> [scheduling] --> [scheduled]
    ^                             |________________|_________
    |                                                       |
    |                                                       v
    |                                                   [running]
    |_______________________________________________________|

All states may transition into FREED state:
[pending] [scheduling] [scheduled] [running] [standby] -> [freed]

A FREED terminal state coordinates with map-value
deletion (bpf_task_work_cancel_and_free()).

Scheduling itself is deferred via irq_work to keep the kfunc callable from NMI context.

Lifetime is guarded with refcount_t + RCU Tasks Trace.

Main components:
* struct bpf_task_work_context – Metadata and state management per task
work.
* enum bpf_task_work_state – A state machine to serialize work scheduling and execution.
* bpf_task_work_schedule() – The central helper that initiates
scheduling.
* bpf_task_work_acquire_ctx() - Attempts to take ownership of the context, pointed by passed struct bpf_task_work, allocates new context if none exists yet.
* bpf_task_work_callback() – Invoked when the actual task_work runs.
* bpf_task_work_irq() – An intermediate step (runs in softirq context)
to enqueue task work.
* bpf_task_work_cancel_and_free() – Cleanup for deleted BPF map entries.

Flow of successful task work scheduling
1) bpf_task_work_schedule_* is called from BPF code.
2) Transition state from STANDBY to PENDING, mark context as owned by this task work scheduler
3) irq_work_queue() schedules bpf_task_work_irq().
4) Transition state from PENDING to SCHEDULING (noop if transition successful)
5) bpf_task_work_irq() attempts task_work_add(). If successful, state transitions to SCHEDULED.
6) Task work calls bpf_task_work_callback(), which transition state to RUNNING.
7) BPF callback is executed
8) Context is cleaned up, refcounts released, context state set back to STANDBY.

---
 kernel/bpf/helpers.c | 311 ++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 309 insertions(+), 2 deletions(-)
kernel/bpf/helpers.c +309 -2
index 2d6a2fe6c374..448d341dd4b7 100644
@@ -25,6 +25,8 @@
 #include <linux/kasan.h>
 #include <linux/bpf_verifier.h>
 #include <linux/uaccess.h>
+#include <linux/task_work.h>
+#include <linux/irq_work.h>
 
 #include "../../lib/kstrtox.h"
 
@@ -3736,6 +3738,284 @@ __bpf_kfunc int bpf_strstr(const char *s1__ign, const char *s2__ign)
 
 typedef int (*bpf_task_work_callback_t)(struct bpf_map *map, void *key, void *value);
 
+enum bpf_task_work_state {
+	/* bpf_task_work is ready to be used */
+	BPF_TW_STANDBY = 0,
+	/* irq work scheduling in progress */
+	BPF_TW_PENDING,
+	/* task work scheduling in progress */
+	BPF_TW_SCHEDULING,
+	/* task work is scheduled successfully */
+	BPF_TW_SCHEDULED,
+	/* callback is running */
+	BPF_TW_RUNNING,
+	/* associated BPF map value is deleted */
+	BPF_TW_FREED,
+};
+
+struct bpf_task_work_ctx {
+	enum bpf_task_work_state state;
+	refcount_t refcnt;
+	struct callback_head work;
+	struct irq_work irq_work;
+	/* bpf_prog that schedules task work */
+	struct bpf_prog *prog;
+	/* task for which callback is scheduled */
+	struct task_struct *task;
+	/* the map and map value associated with this context */
+	struct bpf_map *map;
+	void *map_val;
+	enum task_work_notify_mode mode;
+	bpf_task_work_callback_t callback_fn;
+	struct rcu_head rcu;
+} __aligned(8);
+
+/* Actual type for struct bpf_task_work */
+struct bpf_task_work_kern {
+	struct bpf_task_work_ctx *ctx;
+};
+
+static void bpf_task_work_ctx_free_rcu_gp(struct rcu_head *rcu)
+{
+	struct bpf_task_work_ctx *ctx = container_of(rcu, struct bpf_task_work_ctx, rcu);
+
+	/* bpf_mem_free expects migration to be disabled */
+	migrate_disable();
+	bpf_mem_free(&bpf_global_ma, ctx);
+	migrate_enable();
+}
+
+static void bpf_task_work_ctx_free_mult_rcu_gp(struct rcu_head *rcu)
+{
+	if (rcu_trace_implies_rcu_gp())
+		bpf_task_work_ctx_free_rcu_gp(rcu);
+	else
+		call_rcu(rcu, bpf_task_work_ctx_free_rcu_gp);
+}
+
+static void bpf_task_work_ctx_reset(struct bpf_task_work_ctx *ctx)
+{
+	if (ctx->prog) {
+		bpf_prog_put(ctx->prog);
+		ctx->prog = NULL;
+	}
+	if (ctx->task) {
+		bpf_task_release(ctx->task);
+		ctx->task = NULL;
+	}
+}
+
+static bool bpf_task_work_ctx_tryget(struct bpf_task_work_ctx *ctx)
+{
+	return refcount_inc_not_zero(&ctx->refcnt);
+}
+
+static void bpf_task_work_ctx_put(struct bpf_task_work_ctx *ctx)
+{
+	if (!refcount_dec_and_test(&ctx->refcnt))
+		return;
+
+	bpf_task_work_ctx_reset(ctx);
+	call_rcu_tasks_trace(&ctx->rcu, bpf_task_work_ctx_free_mult_rcu_gp);
+}
+
+static void bpf_task_work_cancel(struct bpf_task_work_ctx *ctx)
+{
+	/*
+	 * Scheduled task_work callback holds ctx ref, so if we successfully
+	 * cancelled, we put that ref on callback's behalf. If we couldn't
+	 * cancel, callback will inevitably run or has already completed
+	 * running, and it would have taken care of its ctx ref itself.
+	 */
+	if (task_work_cancel(ctx->task, &ctx->work))
+		bpf_task_work_ctx_put(ctx);
+}
+
+static void bpf_task_work_callback(struct callback_head *cb)
+{
+	struct bpf_task_work_ctx *ctx = container_of(cb, struct bpf_task_work_ctx, work);
+	enum bpf_task_work_state state;
+	u32 idx;
+	void *key;
+
+	/* Read lock is needed to protect ctx and map key/value access */
+	guard(rcu_tasks_trace)();
+	/*
+	 * This callback may start running before bpf_task_work_irq() switched to
+	 * SCHEDULED state, so handle both transition variants SCHEDULING|SCHEDULED -> RUNNING.
+	 */
+	state = cmpxchg(&ctx->state, BPF_TW_SCHEDULING, BPF_TW_RUNNING);
+	if (state == BPF_TW_SCHEDULED)
+		state = cmpxchg(&ctx->state, BPF_TW_SCHEDULED, BPF_TW_RUNNING);
+	if (state == BPF_TW_FREED) {
+		bpf_task_work_ctx_put(ctx);
+		return;
+	}
+
+	key = (void *)map_key_from_value(ctx->map, ctx->map_val, &idx);
+
+	migrate_disable();
+	ctx->callback_fn(ctx->map, key, ctx->map_val);
+	migrate_enable();
+
+	bpf_task_work_ctx_reset(ctx);
+	(void)cmpxchg(&ctx->state, BPF_TW_RUNNING, BPF_TW_STANDBY);
+
+	bpf_task_work_ctx_put(ctx);
+}
+
+static void bpf_task_work_irq(struct irq_work *irq_work)
+{
+	struct bpf_task_work_ctx *ctx = container_of(irq_work, struct bpf_task_work_ctx, irq_work);
+	enum bpf_task_work_state state;
+	int err;
+
+	guard(rcu_tasks_trace)();
+
+	if (cmpxchg(&ctx->state, BPF_TW_PENDING, BPF_TW_SCHEDULING) != BPF_TW_PENDING) {
+		bpf_task_work_ctx_put(ctx);
+		return;
+	}
+
+	err = task_work_add(ctx->task, &ctx->work, ctx->mode);
+	if (err) {
+		bpf_task_work_ctx_reset(ctx);
+		/*
+		 * try to switch back to STANDBY for another task_work reuse, but we might have
+		 * gone to FREED already, which is fine as we already cleaned up after ourselves
+		 */
+		(void)cmpxchg(&ctx->state, BPF_TW_SCHEDULING, BPF_TW_STANDBY);
+		bpf_task_work_ctx_put(ctx);
+		return;
+	}
+
+	/*
+	 * It's technically possible for just scheduled task_work callback to
+	 * complete running by now, going SCHEDULING -> RUNNING and then
+	 * dropping its ctx refcount. Instead of capturing extra ref just to
+	 * protected below ctx->state access, we rely on RCU protection to
+	 * perform below SCHEDULING -> SCHEDULED attempt.
+	 */
+	state = cmpxchg(&ctx->state, BPF_TW_SCHEDULING, BPF_TW_SCHEDULED);
+	if (state == BPF_TW_FREED)
+		bpf_task_work_cancel(ctx); /* clean up if we switched into FREED state */
+}
+
+static struct bpf_task_work_ctx *bpf_task_work_fetch_ctx(struct bpf_task_work *tw,
+							 struct bpf_map *map)
+{
+	struct bpf_task_work_kern *twk = (void *)tw;
+	struct bpf_task_work_ctx *ctx, *old_ctx;
+
+	ctx = READ_ONCE(twk->ctx);
+	if (ctx)
+		return ctx;
+
+	ctx = bpf_mem_alloc(&bpf_global_ma, sizeof(struct bpf_task_work_ctx));
+	if (!ctx)
+		return ERR_PTR(-ENOMEM);
+
+	memset(ctx, 0, sizeof(*ctx));
+	refcount_set(&ctx->refcnt, 1); /* map's own ref */
+	ctx->state = BPF_TW_STANDBY;
+
+	old_ctx = cmpxchg(&twk->ctx, NULL, ctx);
+	if (old_ctx) {
+		/*
+		 * tw->ctx is set by concurrent BPF program, release allocated
+		 * memory and try to reuse already set context.
+		 */
+		bpf_mem_free(&bpf_global_ma, ctx);
+		return old_ctx;
+	}
+
+	return ctx; /* Success */
+}
+
+static struct bpf_task_work_ctx *bpf_task_work_acquire_ctx(struct bpf_task_work *tw,
+							   struct bpf_map *map)
+{
+	struct bpf_task_work_ctx *ctx;
+
+	/* early check to avoid any work, we'll double check at the end again */
+	if (!atomic64_read(&map->usercnt))
+		return ERR_PTR(-EBUSY);
+
+	ctx = bpf_task_work_fetch_ctx(tw, map);
+	if (IS_ERR(ctx))
+		return ctx;
+
+	/* try to get ref for task_work callback to hold */
+	if (!bpf_task_work_ctx_tryget(ctx))
+		return ERR_PTR(-EBUSY);
+
+	if (cmpxchg(&ctx->state, BPF_TW_STANDBY, BPF_TW_PENDING) != BPF_TW_STANDBY) {
+		/* lost acquiring race or map_release_uref() stole it from us, put ref and bail */
+		bpf_task_work_ctx_put(ctx);
+		return ERR_PTR(-EBUSY);
+	}
+
+	/*
+	 * Double check that map->usercnt wasn't dropped while we were
+	 * preparing context, and if it was, we need to clean up as if
+	 * map_release_uref() was called; bpf_task_work_cancel_and_free()
+	 * is safe to be called twice on the same task work
+	 */
+	if (!atomic64_read(&map->usercnt)) {
+		/* drop ref we just got for task_work callback itself */
+		bpf_task_work_ctx_put(ctx);
+		/* transfer map's ref into cancel_and_free() */
+		bpf_task_work_cancel_and_free(tw);
+		return ERR_PTR(-EBUSY);
+	}
+
+	return ctx;
+}
+
+static int bpf_task_work_schedule(struct task_struct *task, struct bpf_task_work *tw,
+				  struct bpf_map *map, bpf_task_work_callback_t callback_fn,
+				  struct bpf_prog_aux *aux, enum task_work_notify_mode mode)
+{
+	struct bpf_prog *prog;
+	struct bpf_task_work_ctx *ctx;
+	int err;
+
+	BTF_TYPE_EMIT(struct bpf_task_work);
+
+	prog = bpf_prog_inc_not_zero(aux->prog);
+	if (IS_ERR(prog))
+		return -EBADF;
+	task = bpf_task_acquire(task);
+	if (!task) {
+		err = -EBADF;
+		goto release_prog;
+	}
+
+	ctx = bpf_task_work_acquire_ctx(tw, map);
+	if (IS_ERR(ctx)) {
+		err = PTR_ERR(ctx);
+		goto release_all;
+	}
+
+	ctx->task = task;
+	ctx->callback_fn = callback_fn;
+	ctx->prog = prog;
+	ctx->mode = mode;
+	ctx->map = map;
+	ctx->map_val = (void *)tw - map->record->task_work_off;
+	init_task_work(&ctx->work, bpf_task_work_callback);
+	init_irq_work(&ctx->irq_work, bpf_task_work_irq);
+
+	irq_work_queue(&ctx->irq_work);
+	return 0;
+
+release_all:
+	bpf_task_release(task);
+release_prog:
+	bpf_prog_put(prog);
+	return err;
+}
+
 /**
  * bpf_task_work_schedule_signal - Schedule BPF callback using task_work_add with TWA_SIGNAL mode
  * @task: Task struct for which callback should be scheduled
@@ -3750,7 +4030,7 @@ __bpf_kfunc int bpf_task_work_schedule_signal(struct task_struct *task, struct b
 					      void *map__map, bpf_task_work_callback_t callback,
 					      void *aux__prog)
 {
-	return 0;
+	return bpf_task_work_schedule(task, tw, map__map, callback, aux__prog, TWA_SIGNAL);
 }
 
 /**
@@ -3767,13 +4047,38 @@ __bpf_kfunc int bpf_task_work_schedule_resume(struct task_struct *task, struct b
 					      void *map__map, bpf_task_work_callback_t callback,
 					      void *aux__prog)
 {
-	return 0;
+	return bpf_task_work_schedule(task, tw, map__map, callback, aux__prog, TWA_RESUME);
 }
 
 __bpf_kfunc_end_defs();
 
+static void bpf_task_work_cancel_scheduled(struct irq_work *irq_work)
+{
+	struct bpf_task_work_ctx *ctx = container_of(irq_work, struct bpf_task_work_ctx, irq_work);
+
+	bpf_task_work_cancel(ctx); /* this might put task_work callback's ref */
+	bpf_task_work_ctx_put(ctx); /* and here we put map's own ref that was transferred to us */
+}
+
 void bpf_task_work_cancel_and_free(void *val)
 {
+	struct bpf_task_work_kern *twk = val;
+	struct bpf_task_work_ctx *ctx;
+	enum bpf_task_work_state state;
+
+	ctx = xchg(&twk->ctx, NULL);
+	if (!ctx)
+		return;
+
+	state = xchg(&ctx->state, BPF_TW_FREED);
+	if (state == BPF_TW_SCHEDULED) {
+		/* run in irq_work to avoid locks in NMI */
+		init_irq_work(&ctx->irq_work, bpf_task_work_cancel_scheduled);
+		irq_work_queue(&ctx->irq_work);
+		return;
+	}
+
+	bpf_task_work_ctx_put(ctx); /* put bpf map's ref */
 }
 
 BTF_KFUNCS_START(generic_btf_ids)
@@ -3910,6 +4215,8 @@ BTF_ID_FLAGS(func, bpf_strnstr);
 BTF_ID_FLAGS(func, bpf_cgroup_read_xattr, KF_RCU)
 #endif
 BTF_ID_FLAGS(func, bpf_stream_vprintk, KF_TRUSTED_ARGS)
+BTF_ID_FLAGS(func, bpf_task_work_schedule_signal, KF_TRUSTED_ARGS)
+BTF_ID_FLAGS(func, bpf_task_work_schedule_resume, KF_TRUSTED_ARGS)
 BTF_KFUNCS_END(common_btf_ids)
 
 static const struct btf_kfunc_id_set common_kfunc_set = {
Signature · 1 line
2.51.0
#11 lore raw

Re: [PATCH bpf-next v6 7/8] bpf: task work scheduling kfuncs

Quoted 30 lines · Mykyta Yatsenko
kernel/bpf/helpers.c +29 -0
+static struct bpf_task_work_ctx *bpf_task_work_acquire_ctx(struct bpf_task_work *tw,
+                                                          struct bpf_map *map)
+{
+       struct bpf_task_work_ctx *ctx;
+
+       /* early check to avoid any work, we'll double check at the end again */
+       if (!atomic64_read(&map->usercnt))
+               return ERR_PTR(-EBUSY);
+
+       ctx = bpf_task_work_fetch_ctx(tw, map);
+       if (IS_ERR(ctx))
+               return ctx;
+
+       /* try to get ref for task_work callback to hold */
+       if (!bpf_task_work_ctx_tryget(ctx))
+               return ERR_PTR(-EBUSY);
+
+       if (cmpxchg(&ctx->state, BPF_TW_STANDBY, BPF_TW_PENDING) != BPF_TW_STANDBY) {
+               /* lost acquiring race or map_release_uref() stole it from us, put ref and bail */
+               bpf_task_work_ctx_put(ctx);
+               return ERR_PTR(-EBUSY);
+       }
+
+       /*
+        * Double check that map->usercnt wasn't dropped while we were
+        * preparing context, and if it was, we need to clean up as if
+        * map_release_uref() was called; bpf_task_work_cancel_and_free()
+        * is safe to be called twice on the same task work
+        */

kernel/bpf/helpers.c __bpf_kfunc int bpf_strstr(const char *s1__ign, const char *s2__ign) · line 3973

Quoted 10 lines
kernel/bpf/helpers.c +10 -0
+       if (!atomic64_read(&map->usercnt)) {
+               /* drop ref we just got for task_work callback itself */
+               bpf_task_work_ctx_put(ctx);
+               /* transfer map's ref into cancel_and_free() */
+               bpf_task_work_cancel_and_free(tw);
+               return ERR_PTR(-EBUSY);
+       }
+
+       return ctx;
+}

If I understood the logic correctly the usercnt handling is very much best effort: "let's try to detect usercnt==0 and clean thing up, but if we don't detect it should be ok too". I think it distracts from the main state transition logic. I think it's better to remove both map->usercnt checks and comment how the race with release_uref() is handled through the state transitions.

Why above usercnt==0 check is racy?
Because usercnt could have become zero right after this atomic64_read(). Then valid ctx (though maybe detached) would have been returned to bpf_task_work_schedule(), and it would proceed with irq_work_queue().
tw->ctx either already xchg-ed to NULL or will be soon.

The bpf_task_work_irq() callback would fire eventually it will do
+ if (cmpxchg(&ctx->state, BPF_TW_PENDING, BPF_TW_SCHEDULING) !=
BPF_TW_PENDING) {

if releas_uref() already did bpf_task_work_cancel_and_free() then ctx->state == BPF_TW_FREED and
+ bpf_task_work_ctx_put(ctx);
+ return;
+ }
will be called on this detached ctx.

but xchg(&ctx->state, BPF_TW_FREED) might not have been done. so the code will proceed...
and further it looks correct when it comes to handling races with cancel_and_free().

The point that usercnt==0 or not doesn't change thing. We don't check it in the steps after acquire_ctx().
It looks to me these two checks in bpf_task_work_acquire_ctx() don't fix any race.
It seems to me they can be removed without affecting correctness, and if so, let's remove them to avoid misleading readers and ourselves in the future that they matter.

Note, similar usercnt checks in bpf_timer are not analogous, since they're done under lock with async->cb manipulations.

Also I believe Eduard requested stess test to be part of patchset. Please include it. I'd like to see what kind of stress testing was done. Patch 8 is just basic sanity.

#12 lore raw

Re: [PATCH bpf-next v6 7/8] bpf: task work scheduling kfuncs

Quoted 80 lines · Alexei Starovoitov
Quoted 29 lines · Mykyta Yatsenko
kernel/bpf/helpers.c +29 -0
+static struct bpf_task_work_ctx *bpf_task_work_acquire_ctx(struct bpf_task_work *tw,
+                                                          struct bpf_map *map)
+{
+       struct bpf_task_work_ctx *ctx;
+
+       /* early check to avoid any work, we'll double check at the end again */
+       if (!atomic64_read(&map->usercnt))
+               return ERR_PTR(-EBUSY);
+
+       ctx = bpf_task_work_fetch_ctx(tw, map);
+       if (IS_ERR(ctx))
+               return ctx;
+
+       /* try to get ref for task_work callback to hold */
+       if (!bpf_task_work_ctx_tryget(ctx))
+               return ERR_PTR(-EBUSY);
+
+       if (cmpxchg(&ctx->state, BPF_TW_STANDBY, BPF_TW_PENDING) != BPF_TW_STANDBY) {
+               /* lost acquiring race or map_release_uref() stole it from us, put ref and bail */
+               bpf_task_work_ctx_put(ctx);
+               return ERR_PTR(-EBUSY);
+       }
+
+       /*
+        * Double check that map->usercnt wasn't dropped while we were
+        * preparing context, and if it was, we need to clean up as if
+        * map_release_uref() was called; bpf_task_work_cancel_and_free()
+        * is safe to be called twice on the same task work
+        */

kernel/bpf/helpers.c __bpf_kfunc int bpf_strstr(const char *s1__ign, const char *s2__ign) · line 3973

Quoted 10 lines
kernel/bpf/helpers.c +10 -0
+       if (!atomic64_read(&map->usercnt)) {
+               /* drop ref we just got for task_work callback itself */
+               bpf_task_work_ctx_put(ctx);
+               /* transfer map's ref into cancel_and_free() */
+               bpf_task_work_cancel_and_free(tw);
+               return ERR_PTR(-EBUSY);
+       }
+
+       return ctx;
+}

If I understood the logic correctly the usercnt handling is very much best effort: "let's try to detect usercnt==0 and clean thing up, but if we don't detect it should be ok too". I think it distracts from the main state transition logic. I think it's better to remove both map->usercnt checks and comment how the race with release_uref() is handled through the state transitions.

Why above usercnt==0 check is racy?
Because usercnt could have become zero right after this atomic64_read(). Then valid ctx (though maybe detached) would have been returned to bpf_task_work_schedule(), and it would proceed with irq_work_queue().
tw->ctx either already xchg-ed to NULL or will be soon.

The bpf_task_work_irq() callback would fire eventually it will do
+ if (cmpxchg(&ctx->state, BPF_TW_PENDING, BPF_TW_SCHEDULING) !=
BPF_TW_PENDING) {

if releas_uref() already did bpf_task_work_cancel_and_free() then ctx->state == BPF_TW_FREED and
+ bpf_task_work_ctx_put(ctx);
+ return;
+ }
will be called on this detached ctx.

but xchg(&ctx->state, BPF_TW_FREED) might not have been done. so the code will proceed...
and further it looks correct when it comes to handling races with cancel_and_free().

The point that usercnt==0 or not doesn't change thing. We don't check it in the steps after acquire_ctx().
It looks to me these two checks in bpf_task_work_acquire_ctx() don't fix any race.
It seems to me they can be removed without affecting correctness, and if so, let's remove them to avoid misleading readers and ourselves in the future that they matter.

Quoted 7 lines

Note, similar usercnt checks in bpf_timer are not analogous, since they're done under lock with async->cb manipulations.

Also I believe Eduard requested stess test to be part of patchset. Please include it. I'd like to see what kind of stress testing was done. Patch 8 is just basic sanity.

An example of race condition I'm handling is:
Imagine usercnt gets to 0, and then for some map value cancel_and_free() (detach context) races with bpf_task_work_fetch_ctx() (attach context), if this race resolves to context first being detached (by cancel_and_free()) and then new one attached (by scheduling codepath).
Detached context state is set to FREED and it's deallocated.
But newly attached context state is STANDBY (cancel_and_free() has never seen it) and map holds the refcnt to it, which never go to 0, as cancel_and_free() for the element has been already called, so we never free it.

It's not a problem if usercnt goes to 0 after we attached a context, because cancel_and_free() will detach and
put the refcnt, and scheduling codepath will see the FREED state.

Other thing is checking usercnt == 0 before context initialization - that's preventing allocation and attach a context to a map that has already done cleanup. Cleanup won't happen again and this new context is leaked.

Trying to summarize:
 1) First check for usercnt == 0 is needed so that we don't allocate contexts for map that has already cleaned up.
 2) Second check is needed in case of clean up is triggered during context initialization/attach, potentially leading to newly attached context leak, as cancel_and_free() was called for old context and won't be called for new one.
 3) After context initialization/attach is done, we don't need checking for usercnt, as cancel_and_free() will detach and transition to FREED. The state transition will be seen by scheduling codepath.

#13 lore raw

Re: [PATCH bpf-next v6 7/8] bpf: task work scheduling kfuncs

Quoted 110 lines · Mykyta Yatsenko
Quoted 80 lines · Alexei Starovoitov
Quoted 29 lines · Mykyta Yatsenko
kernel/bpf/helpers.c +29 -0
+static struct bpf_task_work_ctx *bpf_task_work_acquire_ctx(struct bpf_task_work *tw,
+                                                          struct bpf_map *map)
+{
+       struct bpf_task_work_ctx *ctx;
+
+       /* early check to avoid any work, we'll double check at the end again */
+       if (!atomic64_read(&map->usercnt))
+               return ERR_PTR(-EBUSY);
+
+       ctx = bpf_task_work_fetch_ctx(tw, map);
+       if (IS_ERR(ctx))
+               return ctx;
+
+       /* try to get ref for task_work callback to hold */
+       if (!bpf_task_work_ctx_tryget(ctx))
+               return ERR_PTR(-EBUSY);
+
+       if (cmpxchg(&ctx->state, BPF_TW_STANDBY, BPF_TW_PENDING) != BPF_TW_STANDBY) {
+               /* lost acquiring race or map_release_uref() stole it from us, put ref and bail */
+               bpf_task_work_ctx_put(ctx);
+               return ERR_PTR(-EBUSY);
+       }
+
+       /*
+        * Double check that map->usercnt wasn't dropped while we were
+        * preparing context, and if it was, we need to clean up as if
+        * map_release_uref() was called; bpf_task_work_cancel_and_free()
+        * is safe to be called twice on the same task work
+        */

kernel/bpf/helpers.c __bpf_kfunc int bpf_strstr(const char *s1__ign, const char *s2__ign) · line 3973

Quoted 10 lines
kernel/bpf/helpers.c +10 -0
+       if (!atomic64_read(&map->usercnt)) {
+               /* drop ref we just got for task_work callback itself */
+               bpf_task_work_ctx_put(ctx);
+               /* transfer map's ref into cancel_and_free() */
+               bpf_task_work_cancel_and_free(tw);
+               return ERR_PTR(-EBUSY);
+       }
+
+       return ctx;
+}

If I understood the logic correctly the usercnt handling is very much best effort: "let's try to detect usercnt==0 and clean thing up, but if we don't detect it should be ok too". I think it distracts from the main state transition logic. I think it's better to remove both map->usercnt checks and comment how the race with release_uref() is handled through the state transitions.

Why above usercnt==0 check is racy?
Because usercnt could have become zero right after this atomic64_read(). Then valid ctx (though maybe detached) would have been returned to bpf_task_work_schedule(), and it would proceed with irq_work_queue().
tw->ctx either already xchg-ed to NULL or will be soon.

The bpf_task_work_irq() callback would fire eventually it will do
+ if (cmpxchg(&ctx->state, BPF_TW_PENDING, BPF_TW_SCHEDULING) !=
BPF_TW_PENDING) {

if releas_uref() already did bpf_task_work_cancel_and_free() then ctx->state == BPF_TW_FREED and
+ bpf_task_work_ctx_put(ctx);
+ return;
+ }
will be called on this detached ctx.

but xchg(&ctx->state, BPF_TW_FREED) might not have been done. so the code will proceed...
and further it looks correct when it comes to handling races with cancel_and_free().

The point that usercnt==0 or not doesn't change thing. We don't check it in the steps after acquire_ctx().
It looks to me these two checks in bpf_task_work_acquire_ctx() don't fix any race.
It seems to me they can be removed without affecting correctness, and if so, let's remove them to avoid misleading readers and ourselves in the future that they matter.

Quoted 7 lines

Note, similar usercnt checks in bpf_timer are not analogous, since they're done under lock with async->cb manipulations.

Also I believe Eduard requested stess test to be part of patchset. Please include it. I'd like to see what kind of stress testing was done. Patch 8 is just basic sanity.

An example of race condition I'm handling is:
Imagine usercnt gets to 0, and then for some map value cancel_and_free() (detach context) races with bpf_task_work_fetch_ctx() (attach context), if this race resolves to context first being detached (by cancel_and_free())
and then new one attached (by scheduling codepath).
Detached context state is set to FREED and it's deallocated. But newly attached context state is STANDBY (cancel_and_free() has never seen it)
and map holds the refcnt to it, which never go to 0, as cancel_and_free() for the element has been already called, so we never free it.

It's not a problem if usercnt goes to 0 after we attached a context, because cancel_and_free() will detach and
put the refcnt, and scheduling codepath will see the FREED state.

Other thing is checking usercnt == 0 before context initialization - that's preventing allocation and attach a context to a map that has already done cleanup. Cleanup won't happen again and this new context is leaked.

Quoted 13 lines

Trying to summarize:
1) First check for usercnt == 0 is needed so that we don't allocate
contexts
for map that has already cleaned up.
2) Second check is needed in case of clean up is triggered during context
initialization/attach, potentially leading to newly attached context leak, as cancel_and_free() was called for old context and won't be called for new one.
3) After context initialization/attach is done, we don't need checking
for usercnt,
as cancel_and_free() will detach and transition to FREED. The state transition will
be seen by scheduling codepath.

I agree, I don't see how we can avoid checking usercnt. Maybe we don't need to check it twice, i.e. optimistically first and then again after cmpxchg and can only do it once.
Since this is not a common case it's nbd that we have to put and deallocate stuff. But if we don't do usercnt check at all, we will leak memory.
Once usercnt drops to zero and task work lingers in map value, nothing will come and free it anymore, short of user deleting the map value manually, which is not guaranteed.

#14 lore raw

Re: [PATCH bpf-next v6 7/8] bpf: task work scheduling kfuncs

Quoted 127 lines · Kumar Kartikeya Dwivedi
Quoted 110 lines · Mykyta Yatsenko
Quoted 80 lines · Alexei Starovoitov
Quoted 29 lines · Mykyta Yatsenko
kernel/bpf/helpers.c +29 -0
+static struct bpf_task_work_ctx *bpf_task_work_acquire_ctx(struct bpf_task_work *tw,
+                                                          struct bpf_map *map)
+{
+       struct bpf_task_work_ctx *ctx;
+
+       /* early check to avoid any work, we'll double check at the end again */
+       if (!atomic64_read(&map->usercnt))
+               return ERR_PTR(-EBUSY);
+
+       ctx = bpf_task_work_fetch_ctx(tw, map);
+       if (IS_ERR(ctx))
+               return ctx;
+
+       /* try to get ref for task_work callback to hold */
+       if (!bpf_task_work_ctx_tryget(ctx))
+               return ERR_PTR(-EBUSY);
+
+       if (cmpxchg(&ctx->state, BPF_TW_STANDBY, BPF_TW_PENDING) != BPF_TW_STANDBY) {
+               /* lost acquiring race or map_release_uref() stole it from us, put ref and bail */
+               bpf_task_work_ctx_put(ctx);
+               return ERR_PTR(-EBUSY);
+       }
+
+       /*
+        * Double check that map->usercnt wasn't dropped while we were
+        * preparing context, and if it was, we need to clean up as if
+        * map_release_uref() was called; bpf_task_work_cancel_and_free()
+        * is safe to be called twice on the same task work
+        */

kernel/bpf/helpers.c __bpf_kfunc int bpf_strstr(const char *s1__ign, const char *s2__ign) · line 3973

Quoted 10 lines
kernel/bpf/helpers.c +10 -0
+       if (!atomic64_read(&map->usercnt)) {
+               /* drop ref we just got for task_work callback itself */
+               bpf_task_work_ctx_put(ctx);
+               /* transfer map's ref into cancel_and_free() */
+               bpf_task_work_cancel_and_free(tw);
+               return ERR_PTR(-EBUSY);
+       }
+
+       return ctx;
+}

If I understood the logic correctly the usercnt handling is very much best effort: "let's try to detect usercnt==0 and clean thing up, but if we don't detect it should be ok too". I think it distracts from the main state transition logic. I think it's better to remove both map->usercnt checks and comment how the race with release_uref() is handled through the state transitions.

Why above usercnt==0 check is racy?
Because usercnt could have become zero right after this atomic64_read(). Then valid ctx (though maybe detached) would have been returned to bpf_task_work_schedule(), and it would proceed with irq_work_queue().
tw->ctx either already xchg-ed to NULL or will be soon.

The bpf_task_work_irq() callback would fire eventually it will do
+ if (cmpxchg(&ctx->state, BPF_TW_PENDING, BPF_TW_SCHEDULING) !=
BPF_TW_PENDING) {

if releas_uref() already did bpf_task_work_cancel_and_free() then ctx->state == BPF_TW_FREED and
+ bpf_task_work_ctx_put(ctx);
+ return;
+ }
will be called on this detached ctx.

but xchg(&ctx->state, BPF_TW_FREED) might not have been done. so the code will proceed...
and further it looks correct when it comes to handling races with cancel_and_free().

The point that usercnt==0 or not doesn't change thing. We don't check it in the steps after acquire_ctx().
It looks to me these two checks in bpf_task_work_acquire_ctx() don't fix any race.
It seems to me they can be removed without affecting correctness, and if so, let's remove them to avoid misleading readers and ourselves in the future that they matter.

Quoted 7 lines

Note, similar usercnt checks in bpf_timer are not analogous, since they're done under lock with async->cb manipulations.

Also I believe Eduard requested stess test to be part of patchset. Please include it. I'd like to see what kind of stress testing was done. Patch 8 is just basic sanity.

An example of race condition I'm handling is:
Imagine usercnt gets to 0, and then for some map value cancel_and_free() (detach context) races with bpf_task_work_fetch_ctx() (attach context), if this race resolves to context first being detached (by cancel_and_free())
and then new one attached (by scheduling codepath).
Detached context state is set to FREED and it's deallocated. But newly attached context state is STANDBY (cancel_and_free() has never seen it)
and map holds the refcnt to it, which never go to 0, as cancel_and_free() for the element has been already called, so we never free it.

It's not a problem if usercnt goes to 0 after we attached a context, because cancel_and_free() will detach and
put the refcnt, and scheduling codepath will see the FREED state.

Other thing is checking usercnt == 0 before context initialization - that's preventing allocation and attach a context to a map that has already done cleanup. Cleanup won't happen again and this new context is leaked.

Quoted 13 lines

Trying to summarize:
1) First check for usercnt == 0 is needed so that we don't allocate
contexts
for map that has already cleaned up.
2) Second check is needed in case of clean up is triggered during context
initialization/attach, potentially leading to newly attached context leak, as cancel_and_free() was called for old context and won't be called for new one.
3) After context initialization/attach is done, we don't need checking
for usercnt,
as cancel_and_free() will detach and transition to FREED. The state transition will
be seen by scheduling codepath.

Quoted 4 lines

I agree, I don't see how we can avoid checking usercnt. Maybe we don't need to check it twice, i.e. optimistically first and then again after cmpxchg and can only do it once.
Since this is not a common case it's nbd that we have to put and

You can have a very long-lived BPF map that is only referenced from loaded/attached BPF program with usercnt==0, so it's not necessarily a short-lived state. From a correctness standpoint, yes, we can omit the first check, but I'd still do it.

Quoted 5 lines

deallocate stuff. But if we don't do usercnt check at all, we will leak memory.
Once usercnt drops to zero and task work lingers in map value, nothing will come and free it anymore, short of user deleting the map value manually, which is not guaranteed.

#15 lore raw

Re: [PATCH bpf-next v6 7/8] bpf: task work scheduling kfuncs

Quoted 29 lines · Andrii Nakryiko
Quoted 24 lines
Quoted 10 lines

It's not a problem if usercnt goes to 0 after we attached a context, because cancel_and_free() will detach and
put the refcnt, and scheduling codepath will see the FREED state.

Other thing is checking usercnt == 0 before context initialization - that's preventing allocation and attach a context to a map that has already done cleanup. Cleanup won't happen again and this new context is leaked.

Quoted 13 lines

Trying to summarize:
1) First check for usercnt == 0 is needed so that we don't allocate
contexts
for map that has already cleaned up.
2) Second check is needed in case of clean up is triggered during context
initialization/attach, potentially leading to newly attached context leak, as cancel_and_free() was called for old context and won't be called for new one.
3) After context initialization/attach is done, we don't need checking
for usercnt,
as cancel_and_free() will detach and transition to FREED. The state transition will
be seen by scheduling codepath.

Quoted 4 lines

I agree, I don't see how we can avoid checking usercnt. Maybe we don't need to check it twice, i.e. optimistically first and then again after cmpxchg and can only do it once.
Since this is not a common case it's nbd that we have to put and

Quoted 10 lines

You can have a very long-lived BPF map that is only referenced from loaded/attached BPF program with usercnt==0, so it's not necessarily a short-lived state. From a correctness standpoint, yes, we can omit the first check, but I'd still do it.

Quoted 5 lines

deallocate stuff. But if we don't do usercnt check at all, we will leak memory.
Once usercnt drops to zero and task work lingers in map value, nothing will come and free it anymore, short of user deleting the map value manually, which is not guaranteed.

I feel there is a confusion what that usercnt==0 check was for in bpf_timer.
The usercnt==0 check is there to prevent circular dependency of references between prog and map. Only those two. In other words, so that Ctrl-C of a user process should stop all bpf progs including timers.
The code that prevents arm/re-arm when usercnt==0 is a side effect in some way.

bpf_async_kern doesn't have refcnt.
Here bpf_task_work_ctx has a refcnt, but it's not participating in the loop formation.
The loop is prog->map->prog.
prog owns a map. The verifier did bpf_map_get() before adding a map pointer to prog->aux->used_maps.
bpf_timer_set_callback() and bpf_task_work_schedule() are incrementing prog refcnt. Something has to drop prog's refcnt because map's refcnt will get to zero only after prog gets to zero. That's the reason for release_uref() existence and two refcnts in a map. map->refcnt and map->usercnt.
And the rule: when usercnt goes to zero make sure that map doesn't hold prog's refcnt indirectly through their elements. All map elements cleaned from timers twice.
When map->usercnt goes to zero and then when map->refcnt goes to zero.

Cancelling the timer is a choice to make Ctrl-C behave as what users expect. Ctrl-C should stop everything. But it doesn't have to be that way.
While drop prog->refcnt is mandatory.
Otherwise timer callback will run, the prog will execute, and if prog doesn't rearm the timer or explicitly deletes the map element, the prog and map will stay in memory forever not doing anything, while nothing is user space holding a reference to a map and to a prog.
prog->refcnt == 1, map->refcnt == 1, map->usercnt == 0

Here you've introduced a refcnt in bpf_task_work_ctx and loop doesn't form anymore (as far as I can see).

The bpf_task_work_ctx->refcnt rules in this patch:
- refcnt is held when map_value->tw != NULL
- refcnt is held when bpf_task_work_ctx is scheduled

When bpf_task_work_callback() finally runs it will drop bpf_task_work_ctx->refcnt which will drop prog's refcnt. bpf_timers don't have this logic to drop prog's refcnt after the run.

So release_uref() drops one bpf_task_work_ctx->refcnt
then bpf_task_work_callback() will drop the second and last ctx->refcnt. That will drop prog->refcnt.
And if there is nothing in user space holding the prog
the prog->refcnt==0 which will trigger map->refcnt--
and free of the map which will delete all elements.

So the bpf_timer issue of prog finished and remains in memory forever is solved differently here.
I think good Ctrl-C behavior is also addressed, since release_uref cancels task_work.

I could be missing something, of course.

#16 lore raw

Re: [PATCH bpf-next v6 7/8] bpf: task work scheduling kfuncs

Quoted 97 lines · Alexei Starovoitov
Quoted 29 lines · Andrii Nakryiko
Quoted 24 lines
Quoted 10 lines

It's not a problem if usercnt goes to 0 after we attached a context, because cancel_and_free() will detach and
put the refcnt, and scheduling codepath will see the FREED state.

Other thing is checking usercnt == 0 before context initialization - that's preventing allocation and attach a context to a map that has already done cleanup. Cleanup won't happen again and this new context is leaked.

Quoted 13 lines

Trying to summarize:
1) First check for usercnt == 0 is needed so that we don't allocate
contexts
for map that has already cleaned up.
2) Second check is needed in case of clean up is triggered during context
initialization/attach, potentially leading to newly attached context leak, as cancel_and_free() was called for old context and won't be called for new one.
3) After context initialization/attach is done, we don't need checking
for usercnt,
as cancel_and_free() will detach and transition to FREED. The state transition will
be seen by scheduling codepath.

Quoted 4 lines

I agree, I don't see how we can avoid checking usercnt. Maybe we don't need to check it twice, i.e. optimistically first and then again after cmpxchg and can only do it once.
Since this is not a common case it's nbd that we have to put and

Quoted 10 lines

You can have a very long-lived BPF map that is only referenced from loaded/attached BPF program with usercnt==0, so it's not necessarily a short-lived state. From a correctness standpoint, yes, we can omit the first check, but I'd still do it.

Quoted 5 lines

deallocate stuff. But if we don't do usercnt check at all, we will leak memory.
Once usercnt drops to zero and task work lingers in map value, nothing will come and free it anymore, short of user deleting the map value manually, which is not guaranteed.

I feel there is a confusion what that usercnt==0 check was for in bpf_timer.
The usercnt==0 check is there to prevent circular dependency of references between prog and map. Only those two. In other words, so that Ctrl-C of a user process should stop all bpf progs including timers.
The code that prevents arm/re-arm when usercnt==0 is a side effect in some way.

bpf_async_kern doesn't have refcnt.
Here bpf_task_work_ctx has a refcnt, but it's not participating in the loop formation.
The loop is prog->map->prog.
prog owns a map. The verifier did bpf_map_get() before adding a map pointer to prog->aux->used_maps.
bpf_timer_set_callback() and bpf_task_work_schedule() are incrementing prog refcnt. Something has to drop prog's refcnt because map's refcnt will get to zero only after prog gets to zero. That's the reason for release_uref() existence and two refcnts in a map. map->refcnt and map->usercnt.
And the rule: when usercnt goes to zero make sure that map doesn't hold prog's refcnt indirectly through their elements. All map elements cleaned from timers twice.
When map->usercnt goes to zero and then when map->refcnt goes to zero.

Cancelling the timer is a choice to make Ctrl-C behave as what users expect. Ctrl-C should stop everything. But it doesn't have to be that way.
While drop prog->refcnt is mandatory.
Otherwise timer callback will run, the prog will execute, and if prog doesn't rearm the timer or explicitly deletes the map element, the prog and map will stay in memory forever not doing anything, while nothing is user space holding a reference to a map and to a prog.
prog->refcnt == 1, map->refcnt == 1, map->usercnt == 0

Here you've introduced a refcnt in bpf_task_work_ctx and loop doesn't form anymore (as far as I can see).

The bpf_task_work_ctx->refcnt rules in this patch:
- refcnt is held when map_value->tw != NULL
- refcnt is held when bpf_task_work_ctx is scheduled

When bpf_task_work_callback() finally runs it will drop bpf_task_work_ctx->refcnt which will drop prog's refcnt. bpf_timers don't have this logic to drop prog's refcnt after the run.

So release_uref() drops one bpf_task_work_ctx->refcnt
then bpf_task_work_callback() will drop the second and last ctx->refcnt. That will drop prog->refcnt.
And if there is nothing in user space holding the prog
the prog->refcnt==0 which will trigger map->refcnt--
and free of the map which will delete all elements.

Quoted 6 lines

So the bpf_timer issue of prog finished and remains in memory forever is solved differently here.
I think good Ctrl-C behavior is also addressed, since release_uref cancels task_work.

I could be missing something, of course.

We discussed it offline with Andrii.
What I missed in the above is the case where release_uref() clears ctx to NULL, but the program might still
use that map value and if both usercnt checks
are removed it can allocate ctx, schedule and run a callback. That's not a clean Ctrl-C behavior.
It's not a correctness issue from kernel pov.
There will be no dangling detached prog/map combo.
Some bpf_timers users already have this logic in their services:
when they unpin the bpf map they expect all timers
to be stopped and not fire again.
The prog might still be attached and can run,
but the map with usercnt==0 is effectively poisoned.
So let's leave 2nd check after state became pending
and update the comment to explain that we're not fixing a race or correctness issue.
The usercnt==0 means that nor processes or bpffs are holding a reference to the map and new callbacks should not be scheduled. This is a policy choice.
Not sure how to phrase all that succinctly.

#17 lore raw

PATCH v6 8/8 selftests/bpf: BPF task work scheduling tests

In reply to Mykyta Yatsenko

From: Mykyta Yatsenko <[email protected]>

Introducing selftests that check BPF task work scheduling mechanism. Validate that verifier does not accepts incorrect calls to bpf_task_work_schedule kfunc.

---
 .../selftests/bpf/prog_tests/test_task_work.c | 150 ++++++++++++++++++
 tools/testing/selftests/bpf/progs/task_work.c | 107 +++++++++++++
 .../selftests/bpf/progs/task_work_fail.c      |  96 +++++++++++
 3 files changed, 353 insertions(+)
 create mode 100644 tools/testing/selftests/bpf/prog_tests/test_task_work.c
 create mode 100644 tools/testing/selftests/bpf/progs/task_work.c
 create mode 100644 tools/testing/selftests/bpf/progs/task_work_fail.c
tools/testing/selftests/bpf/prog_tests/test_task_work.c new file +150 -0
new file mode 100644
index 000000000000..666585270fbf
@@ -0,0 +1,150 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (c) 2025 Meta Platforms, Inc. and affiliates. */
+#include <test_progs.h>
+#include <string.h>
+#include <stdio.h>
+#include "task_work.skel.h"
+#include "task_work_fail.skel.h"
+#include <linux/bpf.h>
+#include <linux/perf_event.h>
+#include <sys/syscall.h>
+#include <time.h>
+
+static int perf_event_open(__u32 type, __u64 config, int pid)
+{
+	struct perf_event_attr attr = {
+		.type = type,
+		.config = config,
+		.size = sizeof(struct perf_event_attr),
+		.sample_period = 100000,
+	};
+
+	return syscall(__NR_perf_event_open, &attr, pid, -1, -1, 0);
+}
+
+struct elem {
+	char data[128];
+	struct bpf_task_work tw;
+};
+
+static int verify_map(struct bpf_map *map, const char *expected_data)
+{
+	int err;
+	struct elem value;
+	int processed_values = 0;
+	int k, sz;
+
+	sz = bpf_map__max_entries(map);
+	for (k = 0; k < sz; ++k) {
+		err = bpf_map__lookup_elem(map, &k, sizeof(int), &value, sizeof(struct elem), 0);
+		if (err)
+			continue;
+		if (!ASSERT_EQ(strcmp(expected_data, value.data), 0, "map data")) {
+			fprintf(stderr, "expected '%s', found '%s' in %s map", expected_data,
+				value.data, bpf_map__name(map));
+			return 2;
+		}
+		processed_values++;
+	}
+
+	return processed_values == 0;
+}
+
+static void task_work_run(const char *prog_name, const char *map_name)
+{
+	struct task_work *skel;
+	struct bpf_program *prog;
+	struct bpf_map *map;
+	struct bpf_link *link;
+	int err, pe_fd = 0, pid, status, pipefd[2];
+	char user_string[] = "hello world";
+
+	if (!ASSERT_NEQ(pipe(pipefd), -1, "pipe"))
+		return;
+
+	pid = fork();
+	if (pid == 0) {
+		__u64 num = 1;
+		int i;
+		char buf;
+
+		close(pipefd[1]);
+		read(pipefd[0], &buf, sizeof(buf));
+		close(pipefd[0]);
+
+		for (i = 0; i < 10000; ++i)
+			num *= time(0) % 7;
+		(void)num;
+		exit(0);
+	}
+	ASSERT_GT(pid, 0, "fork() failed");
+
+	skel = task_work__open();
+	if (!ASSERT_OK_PTR(skel, "task_work__open"))
+		return;
+
+	bpf_object__for_each_program(prog, skel->obj) {
+		bpf_program__set_autoload(prog, false);
+	}
+
+	prog = bpf_object__find_program_by_name(skel->obj, prog_name);
+	if (!ASSERT_OK_PTR(prog, "prog_name"))
+		goto cleanup;
+	bpf_program__set_autoload(prog, true);
+	skel->bss->user_ptr = (char *)user_string;
+
+	err = task_work__load(skel);
+	if (!ASSERT_OK(err, "skel_load"))
+		goto cleanup;
+
+	pe_fd = perf_event_open(PERF_TYPE_HARDWARE, PERF_COUNT_HW_CPU_CYCLES, pid);
+	if (pe_fd == -1 && (errno == ENOENT || errno == EOPNOTSUPP)) {
+		printf("%s:SKIP:no PERF_COUNT_HW_CPU_CYCLES\n", __func__);
+		test__skip();
+		goto cleanup;
+	}
+	if (!ASSERT_NEQ(pe_fd, -1, "pe_fd")) {
+		fprintf(stderr, "perf_event_open errno: %d, pid: %d\n", errno, pid);
+		goto cleanup;
+	}
+
+	link = bpf_program__attach_perf_event(prog, pe_fd);
+	if (!ASSERT_OK_PTR(link, "attach_perf_event"))
+		goto cleanup;
+
+	close(pipefd[0]);
+	write(pipefd[1], user_string, 1);
+	close(pipefd[1]);
+	/* Wait to collect some samples */
+	waitpid(pid, &status, 0);
+	pid = 0;
+	map = bpf_object__find_map_by_name(skel->obj, map_name);
+	if (!ASSERT_OK_PTR(map, "find map_name"))
+		goto cleanup;
+	if (!ASSERT_OK(verify_map(map, user_string), "verify map"))
+		goto cleanup;
+cleanup:
+	if (pe_fd >= 0)
+		close(pe_fd);
+	task_work__destroy(skel);
+	if (pid) {
+		close(pipefd[0]);
+		write(pipefd[1], user_string, 1);
+		close(pipefd[1]);
+		waitpid(pid, &status, 0);
+	}
+}
+
+void test_task_work(void)
+{
+	if (test__start_subtest("test_task_work_hash_map"))
+		task_work_run("oncpu_hash_map", "hmap");
+
+	if (test__start_subtest("test_task_work_array_map"))
+		task_work_run("oncpu_array_map", "arrmap");
+
+	if (test__start_subtest("test_task_work_lru_map"))
+		task_work_run("oncpu_lru_map", "lrumap");
+
+	RUN_TESTS(task_work_fail);
+}
tools/testing/selftests/bpf/progs/task_work.c new file +107 -0

107 lines not on this page · raw message

tools/testing/selftests/bpf/progs/task_work_fail.c new file +96 -0

96 lines not on this page · raw message

Signature · 1 line
2.51.0