Skip to contentSkip to conversation

Notify user space when a struct_ops object is detached/unregisterd

PATCH Superseded Kui-Feng Lee 5 participants 27 messages Apr 29, 2024 – May 3, 2024

Subsystems (5)

v1 of 7 Superseded Latest is v7 →

lore
Share & tools
lore
#0 lore raw

cover Notify user space when a struct_ops object is detached/unregisterd

The subsystems consuming struct_ops objects may need to detach or unregister a struct_ops object due to errors or other reasons. It would be useful to notify user space programs so that error recovery or logging can be carried out.

We offer a function for the subsystems to remove an object that was previously registered with them. The BPF struct_ops will detach the corresponding link if one exists and send an event through epoll. Additionally, we allow user space programs to disconnect a struct_ops map from the attached link.

The user space programs could add an entry to an epoll file descriptor for the link that a struct_ops object has been attached to, and receive notifications when the object is detached from the link by user space programs or the subsystem consuming the object.

However, bpf struct_ops maps without BPF_F_LINK are not supported here. Although, the subsystems still can unregister them through the function provided here, but the user space program doesn't get notified since they don't have links.

Kui-Feng Lee (6):
bpf: add a pointer of the attached link to bpf_struct_ops_map.
bpf: export bpf_link_inc_not_zero().
bpf: provide a function to unregister struct_ops objects from
consumers.
bpf: detach a bpf_struct_ops_map from a link.
bpf: support epoll from bpf struct_ops links. selftests/bpf: test detaching struct_ops links.

 include/linux/bpf.h                           |   9 +
 kernel/bpf/bpf_struct_ops.c                   | 186 +++++++++++++++++-
 kernel/bpf/syscall.c                          |  17 +-
 .../selftests/bpf/bpf_testmod/bpf_testmod.c   |  18 +-
 .../selftests/bpf/bpf_testmod/bpf_testmod.h   |   1 +
 .../bpf/prog_tests/test_struct_ops_module.c   | 104 ++++++++++
 .../selftests/bpf/progs/struct_ops_module.c   |   7 +
 7 files changed, 328 insertions(+), 14 deletions(-)
Signature · 1 line
2.34.1
#1 lore raw

PATCH 1/6 bpf: add a pointer of the attached link to bpf_struct_ops_map.

To facilitate the upcoming unregistring struct_ops objects from the systems consuming objects, a pointer of the attached link is added to allow for accessing the attached link of a bpf_struct_ops_map directly from the map itself.

Previously, a st_map could be attached to multiple links. This patch now enforces only one link attached at most.

---
 kernel/bpf/bpf_struct_ops.c | 47 ++++++++++++++++++++++++++++++++++---
 1 file changed, 44 insertions(+), 3 deletions(-)
kernel/bpf/bpf_struct_ops.c +44 -3
index 86c7884abaf8..072e3416c987 100644
@@ -20,6 +20,8 @@ struct bpf_struct_ops_value {
 
 #define MAX_TRAMP_IMAGE_PAGES 8
 
+struct bpf_struct_ops_link;
+
 struct bpf_struct_ops_map {
 	struct bpf_map map;
 	struct rcu_head rcu;
@@ -39,6 +41,8 @@ struct bpf_struct_ops_map {
 	void *image_pages[MAX_TRAMP_IMAGE_PAGES];
 	/* The owner moduler's btf. */
 	struct btf *btf;
+	/* The link is attached by this map. */
+	struct bpf_struct_ops_link __rcu *attached;
 	/* uvalue->data stores the kernel struct
 	 * (e.g. tcp_congestion_ops) that is more useful
 	 * to userspace than the kvalue.  For example,
@@ -1048,6 +1052,22 @@ static bool bpf_struct_ops_valid_to_reg(struct bpf_map *map)
 		smp_load_acquire(&st_map->kvalue.common.state) == BPF_STRUCT_OPS_STATE_READY;
 }
 
+/* Set the attached link of a map.
+ *
+ * Return the current value of the st_map->attached.
+ */
+static inline struct bpf_struct_ops_link *map_attached(struct bpf_struct_ops_map *st_map,
+						       struct bpf_struct_ops_link *st_link)
+{
+	return unrcu_pointer(cmpxchg(&st_map->attached, NULL, st_link));
+}
+
+/* Reset the attached link of a map */
+static inline void map_attached_null(struct bpf_struct_ops_map *st_map)
+{
+	rcu_assign_pointer(st_map->attached, NULL);
+}
+
 static void bpf_struct_ops_map_link_dealloc(struct bpf_link *link)
 {
 	struct bpf_struct_ops_link *st_link;
@@ -1061,6 +1081,7 @@ static void bpf_struct_ops_map_link_dealloc(struct bpf_link *link)
 		 * bpf_struct_ops_link_create() fails to register.
 		 */
 		st_map->st_ops_desc->st_ops->unreg(&st_map->kvalue.data);
+		map_attached_null(st_map);
 		bpf_map_put(&st_map->map);
 	}
 	kfree(st_link);
@@ -1125,9 +1146,21 @@ static int bpf_struct_ops_map_link_update(struct bpf_link *link, struct bpf_map
 		goto err_out;
 	}
 
+	if (likely(st_map != old_st_map) && map_attached(st_map, st_link)) {
+		/* The map is already in use */
+		err = -EBUSY;
+		goto err_out;
+	}
+
 	err = st_map->st_ops_desc->st_ops->update(st_map->kvalue.data, old_st_map->kvalue.data);
-	if (err)
+	if (err) {
+		if (st_map != old_st_map)
+			map_attached_null(st_map);
 		goto err_out;
+	}
+
+	if (likely(st_map != old_st_map))
+		map_attached_null(old_st_map);
 
 	bpf_map_inc(new_map);
 	rcu_assign_pointer(st_link->map, new_map);
@@ -1172,20 +1205,28 @@ int bpf_struct_ops_link_create(union bpf_attr *attr)
 	}
 	bpf_link_init(&link->link, BPF_LINK_TYPE_STRUCT_OPS, &bpf_struct_ops_map_lops, NULL);
 
+	if (map_attached(st_map, link)) {
+		err = -EBUSY;
+		goto err_out;
+	}
+
 	err = bpf_link_prime(&link->link, &link_primer);
 	if (err)
-		goto err_out;
+		goto err_out_attached;
 
 	err = st_map->st_ops_desc->st_ops->reg(st_map->kvalue.data);
 	if (err) {
 		bpf_link_cleanup(&link_primer);
+		/* The link has been free by bpf_link_cleanup() */
 		link = NULL;
-		goto err_out;
+		goto err_out_attached;
 	}
 	RCU_INIT_POINTER(link->map, map);
 
 	return bpf_link_settle(&link_primer);
 
+err_out_attached:
+	map_attached_null(st_map);
 err_out:
 	bpf_map_put(map);
 	kfree(link);
Signature · 1 line
2.34.1
#2 lore raw

Re: [PATCH bpf-next 1/6] bpf: add a pointer of the attached link to bpf_struct_ops_map.

Quoted 8 lines · Kui-Feng Lee

To facilitate the upcoming unregistring struct_ops objects from the systems consuming objects, a pointer of the attached link is added to allow for accessing the attached link of a bpf_struct_ops_map directly from the map itself.

Previously, a st_map could be attached to multiple links. This patch now enforces only one link attached at most.

I'd like to avoid this restriction, in principle. We don't enforce that BPF program should be attached through a single BPF link, so I don't think we should allow that for maps. Worst case you can keep a list of attached links.

Quoted 116 lines
---
 kernel/bpf/bpf_struct_ops.c | 47 ++++++++++++++++++++++++++++++++++---
 1 file changed, 44 insertions(+), 3 deletions(-)
kernel/bpf/bpf_struct_ops.c +44 -4
index 86c7884abaf8..072e3416c987 100644
@@ -20,6 +20,8 @@ struct bpf_struct_ops_value {
 
 #define MAX_TRAMP_IMAGE_PAGES 8
 
+struct bpf_struct_ops_link;
+
 struct bpf_struct_ops_map {
        struct bpf_map map;
        struct rcu_head rcu;
@@ -39,6 +41,8 @@ struct bpf_struct_ops_map {
        void *image_pages[MAX_TRAMP_IMAGE_PAGES];
        /* The owner moduler's btf. */
        struct btf *btf;
+       /* The link is attached by this map. */
+       struct bpf_struct_ops_link __rcu *attached;
        /* uvalue->data stores the kernel struct
         * (e.g. tcp_congestion_ops) that is more useful
         * to userspace than the kvalue.  For example,
@@ -1048,6 +1052,22 @@ static bool bpf_struct_ops_valid_to_reg(struct bpf_map *map)
                smp_load_acquire(&st_map->kvalue.common.state) == BPF_STRUCT_OPS_STATE_READY;
 }
 
+/* Set the attached link of a map.
+ *
+ * Return the current value of the st_map->attached.
+ */
+static inline struct bpf_struct_ops_link *map_attached(struct bpf_struct_ops_map *st_map,
+                                                      struct bpf_struct_ops_link *st_link)
+{
+       return unrcu_pointer(cmpxchg(&st_map->attached, NULL, st_link));
+}
+
+/* Reset the attached link of a map */
+static inline void map_attached_null(struct bpf_struct_ops_map *st_map)
+{
+       rcu_assign_pointer(st_map->attached, NULL);
+}
+
 static void bpf_struct_ops_map_link_dealloc(struct bpf_link *link)
 {
        struct bpf_struct_ops_link *st_link;
@@ -1061,6 +1081,7 @@ static void bpf_struct_ops_map_link_dealloc(struct bpf_link *link)
                 * bpf_struct_ops_link_create() fails to register.
                 */
                st_map->st_ops_desc->st_ops->unreg(&st_map->kvalue.data);
+               map_attached_null(st_map);
                bpf_map_put(&st_map->map);
        }
        kfree(st_link);
@@ -1125,9 +1146,21 @@ static int bpf_struct_ops_map_link_update(struct bpf_link *link, struct bpf_map
                goto err_out;
        }
 
+       if (likely(st_map != old_st_map) && map_attached(st_map, st_link)) {
+               /* The map is already in use */
+               err = -EBUSY;
+               goto err_out;
+       }
+
        err = st_map->st_ops_desc->st_ops->update(st_map->kvalue.data, old_st_map->kvalue.data);
-       if (err)
+       if (err) {
+               if (st_map != old_st_map)
+                       map_attached_null(st_map);
                goto err_out;
+       }
+
+       if (likely(st_map != old_st_map))
+               map_attached_null(old_st_map);
 
        bpf_map_inc(new_map);
        rcu_assign_pointer(st_link->map, new_map);
@@ -1172,20 +1205,28 @@ int bpf_struct_ops_link_create(union bpf_attr *attr)
        }
        bpf_link_init(&link->link, BPF_LINK_TYPE_STRUCT_OPS, &bpf_struct_ops_map_lops, NULL);
 
+       if (map_attached(st_map, link)) {
+               err = -EBUSY;
+               goto err_out;
+       }
+
        err = bpf_link_prime(&link->link, &link_primer);
        if (err)
-               goto err_out;
+               goto err_out_attached;
 
        err = st_map->st_ops_desc->st_ops->reg(st_map->kvalue.data);
        if (err) {
                bpf_link_cleanup(&link_primer);
+               /* The link has been free by bpf_link_cleanup() */
                link = NULL;
-               goto err_out;
+               goto err_out_attached;
        }
        RCU_INIT_POINTER(link->map, map);
 
        return bpf_link_settle(&link_primer);
 
+err_out_attached:
+       map_attached_null(st_map);
 err_out:
        bpf_map_put(map);
        kfree(link);
--

2.34.1

#3 lore raw

Re: [PATCH bpf-next 1/6] bpf: add a pointer of the attached link to bpf_struct_ops_map.

Quoted 7 lines · Andrii Nakryiko
Quoted 6 lines · Kui-Feng Lee

To facilitate the upcoming unregistring struct_ops objects from the systems consuming objects, a pointer of the attached link is added to allow for accessing the attached link of a bpf_struct_ops_map directly from the map itself.

Quoted 7 lines
Quoted 2 lines

Previously, a st_map could be attached to multiple links. This patch now enforces only one link attached at most.

I'd like to avoid this restriction, in principle. We don't enforce that BPF program should be attached through a single BPF link, so I don't think we should allow that for maps. Worst case you can keep a list of attached links.

Agree!

Quoted 117 lines
Quoted 117 lines
---
  kernel/bpf/bpf_struct_ops.c | 47 ++++++++++++++++++++++++++++++++++---
  1 file changed, 44 insertions(+), 3 deletions(-)
kernel/bpf/bpf_struct_ops.c +44 -4
index 86c7884abaf8..072e3416c987 100644
@@ -20,6 +20,8 @@ struct bpf_struct_ops_value {
 
  #define MAX_TRAMP_IMAGE_PAGES 8
 
+struct bpf_struct_ops_link;
+
  struct bpf_struct_ops_map {
         struct bpf_map map;
         struct rcu_head rcu;
@@ -39,6 +41,8 @@ struct bpf_struct_ops_map {
         void *image_pages[MAX_TRAMP_IMAGE_PAGES];
         /* The owner moduler's btf. */
         struct btf *btf;
+       /* The link is attached by this map. */
+       struct bpf_struct_ops_link __rcu *attached;
         /* uvalue->data stores the kernel struct
          * (e.g. tcp_congestion_ops) that is more useful
          * to userspace than the kvalue.  For example,
@@ -1048,6 +1052,22 @@ static bool bpf_struct_ops_valid_to_reg(struct bpf_map *map)
                 smp_load_acquire(&st_map->kvalue.common.state) == BPF_STRUCT_OPS_STATE_READY;
  }
 
+/* Set the attached link of a map.
+ *
+ * Return the current value of the st_map->attached.
+ */
+static inline struct bpf_struct_ops_link *map_attached(struct bpf_struct_ops_map *st_map,
+                                                      struct bpf_struct_ops_link *st_link)
+{
+       return unrcu_pointer(cmpxchg(&st_map->attached, NULL, st_link));
+}
+
+/* Reset the attached link of a map */
+static inline void map_attached_null(struct bpf_struct_ops_map *st_map)
+{
+       rcu_assign_pointer(st_map->attached, NULL);
+}
+
  static void bpf_struct_ops_map_link_dealloc(struct bpf_link *link)
  {
         struct bpf_struct_ops_link *st_link;
@@ -1061,6 +1081,7 @@ static void bpf_struct_ops_map_link_dealloc(struct bpf_link *link)
                  * bpf_struct_ops_link_create() fails to register.
                  */
                 st_map->st_ops_desc->st_ops->unreg(&st_map->kvalue.data);
+               map_attached_null(st_map);
                 bpf_map_put(&st_map->map);
         }
         kfree(st_link);
@@ -1125,9 +1146,21 @@ static int bpf_struct_ops_map_link_update(struct bpf_link *link, struct bpf_map
                 goto err_out;
         }
 
+       if (likely(st_map != old_st_map) && map_attached(st_map, st_link)) {
+               /* The map is already in use */
+               err = -EBUSY;
+               goto err_out;
+       }
+
         err = st_map->st_ops_desc->st_ops->update(st_map->kvalue.data, old_st_map->kvalue.data);
-       if (err)
+       if (err) {
+               if (st_map != old_st_map)
+                       map_attached_null(st_map);
                 goto err_out;
+       }
+
+       if (likely(st_map != old_st_map))
+               map_attached_null(old_st_map);
 
         bpf_map_inc(new_map);
         rcu_assign_pointer(st_link->map, new_map);
@@ -1172,20 +1205,28 @@ int bpf_struct_ops_link_create(union bpf_attr *attr)
         }
         bpf_link_init(&link->link, BPF_LINK_TYPE_STRUCT_OPS, &bpf_struct_ops_map_lops, NULL);
 
+       if (map_attached(st_map, link)) {
+               err = -EBUSY;
+               goto err_out;
+       }
+
         err = bpf_link_prime(&link->link, &link_primer);
         if (err)
-               goto err_out;
+               goto err_out_attached;
 
         err = st_map->st_ops_desc->st_ops->reg(st_map->kvalue.data);
         if (err) {
                 bpf_link_cleanup(&link_primer);
+               /* The link has been free by bpf_link_cleanup() */
                 link = NULL;
-               goto err_out;
+               goto err_out_attached;
         }
         RCU_INIT_POINTER(link->map, map);
 
         return bpf_link_settle(&link_primer);
 
+err_out_attached:
+       map_attached_null(st_map);
  err_out:
         bpf_map_put(map);
         kfree(link);
--

2.34.1

#4 lore raw

PATCH 2/6 bpf: export bpf_link_inc_not_zero().

In reply to Kui-Feng Lee

Make bpf_link_inc_not_zero() available to be used by bpf_struct_ops.c later.

---
 include/linux/bpf.h  | 1 +
 kernel/bpf/syscall.c | 2 +-
 2 files changed, 2 insertions(+), 1 deletion(-)
include/linux/bpf.h +1 -0
index 5034c1b4ded7..8a1500764332 100644
@@ -2324,6 +2324,7 @@ int bpf_link_prime(struct bpf_link *link, struct bpf_link_primer *primer);
 int bpf_link_settle(struct bpf_link_primer *primer);
 void bpf_link_cleanup(struct bpf_link_primer *primer);
 void bpf_link_inc(struct bpf_link *link);
+struct bpf_link *bpf_link_inc_not_zero(struct bpf_link *link);
 void bpf_link_put(struct bpf_link *link);
 int bpf_link_new_fd(struct bpf_link *link);
 struct bpf_link *bpf_link_get_from_fd(u32 ufd);
kernel/bpf/syscall.c +1 -1
index 7d392ec83655..4a2f95c4b2ac 100644
@@ -5364,7 +5364,7 @@ static int link_detach(union bpf_attr *attr)
 	return ret;
 }
 
-static struct bpf_link *bpf_link_inc_not_zero(struct bpf_link *link)
+struct bpf_link *bpf_link_inc_not_zero(struct bpf_link *link)
 {
 	return atomic64_fetch_add_unless(&link->refcnt, 1, 0) ? link : ERR_PTR(-ENOENT);
 }
Signature · 1 line
2.34.1
#5 lore raw

PATCH 3/6 bpf: provide a function to unregister struct_ops objects from consumers.

In reply to Kui-Feng Lee

bpf_struct_ops_kvalue_unreg() unregisters the struct_ops map specified by the pointer passed in. A subsystem could use this function to unregister a struct_ops object that was previously registered to it.

In effect, bpf_struct_ops_kvalue_unreg() detaches the corresponding st_map of an object from the link if there is one.

---
 include/linux/bpf.h         |  6 +++
 kernel/bpf/bpf_struct_ops.c | 97 ++++++++++++++++++++++++++++++++++---
 2 files changed, 97 insertions(+), 6 deletions(-)
include/linux/bpf.h +6 -0
index 8a1500764332..eeeed4b1bd32 100644
@@ -1793,6 +1793,7 @@ static inline void bpf_module_put(const void *data, struct module *owner)
 		module_put(owner);
 }
 int bpf_struct_ops_link_create(union bpf_attr *attr);
+bool bpf_struct_ops_kvalue_unreg(void *data);
 
 #ifdef CONFIG_NET
 /* Define it here to avoid the use of forward declaration */
@@ -1843,6 +1844,11 @@ static inline void bpf_struct_ops_desc_release(struct bpf_struct_ops_desc *st_op
 {
 }
 
+static inline bool bpf_struct_ops_kvalue_unreg(void *data)
+{
+	return false;
+}
+
 #endif
 
 #if defined(CONFIG_CGROUP_BPF) && defined(CONFIG_BPF_LSM)
kernel/bpf/bpf_struct_ops.c +91 -6

140 lines not on this page · raw message

Signature · 1 line
2.34.1
#6 lore raw

Re: [PATCH bpf-next 3/6] bpf: provide a function to unregister struct_ops objects from consumers.

Quoted 6 lines · Kui-Feng Lee

+/* Called from the subsystem that consume the struct_ops.
+ *
+ * The caller should protected this function by holding rcu_read_lock() to
+ * ensure "data" is valid. However, this function may unlock rcu
+ * temporarily. The caller should not rely on the preceding rcu_read_lock()
+ * after returning from this function.

This temporarily losing rcu_read_lock protection is error prone. The caller should do the inc_not_zero() instead if it is needed.

I feel the approach in patch 1 and 3 is a little box-ed in by the earlier tcp-cc usage that tried to fit into the kernel module reg/unreg paradigm and hide as much bpf details as possible from tcp-cc. This is not necessarily true now for other subsystem which has bpf struct_ops from day one.

The epoll detach notification is link only. Can this kernel side specific unreg be limited to struct_ops link only? During reg, a rcu protected link could be passed to the subsystem. That subsystem becomes a kernel user of the bpf link and it can call link_detach(link) to detach. Pseudo code:

struct link __rcu *link;

rcu_read_lock();
ref_link = rcu_dereference(link)
if (ref_link)

	ref_link = bpf_link_inc_not_zero(ref_link);

rcu_read_unlock();

if (!IS_ERR_OR_NULL(ref_link)) {
	bpf_struct_ops_map_link_detach(ref_link);
	bpf_link_put(ref_link);
}
Quoted 70 lines

+ *
+ * Return true if unreg() success. If a call fails, it means some other
+ * task has unrgistered or is unregistering the same object.
+ */

kernel/bpf/bpf_struct_ops.c +66 -0
+bool bpf_struct_ops_kvalue_unreg(void *data)
+{
+	struct bpf_struct_ops_map *st_map =
+		container_of(data, struct bpf_struct_ops_map, kvalue.data);
+	enum bpf_struct_ops_state prev_state;
+	struct bpf_struct_ops_link *st_link;
+	bool ret = false;
+
+	/* The st_map and st_link should be protected by rcu_read_lock(),
+	 * or they may have been free when we try to increase their
+	 * refcount.
+	 */
+	if (IS_ERR(bpf_map_inc_not_zero(&st_map->map)))
+		/* The map is already gone */
+		return false;
+
+	prev_state = cmpxchg(&st_map->kvalue.common.state,
+			     BPF_STRUCT_OPS_STATE_INUSE,
+			     BPF_STRUCT_OPS_STATE_TOBEFREE);
+	if (prev_state == BPF_STRUCT_OPS_STATE_INUSE) {
+		st_map->st_ops_desc->st_ops->unreg(data);
+		/* Pair with bpf_map_inc() for reg() */
+		bpf_map_put(&st_map->map);
+		/* Pair with bpf_map_inc_not_zero() above */
+		bpf_map_put(&st_map->map);
+		return true;
+	}
+	if (prev_state != BPF_STRUCT_OPS_STATE_READY)
+		goto fail;
+
+	/* With BPF_F_LINK */
+
+	st_link = rcu_dereference(st_map->attached);
+	if (!st_link || !bpf_link_inc_not_zero(&st_link->link))
+		/* The map is on the way to unregister */
+		goto fail;
+
+	rcu_read_unlock();
+	mutex_lock(&update_mutex);
+
+	if (rcu_dereference_protected(st_link->map, true) != &st_map->map)
+		/* The map should be unregistered already or on the way to
+		 * be unregistered.
+		 */
+		goto fail_unlock;
+
+	st_map->st_ops_desc->st_ops->unreg(data);
+
+	map_attached_null(st_map);
+	rcu_assign_pointer(st_link->map, NULL);
+	/* Pair with bpf_map_get() in bpf_struct_ops_link_create() or
+	 * bpf_map_inc() in bpf_struct_ops_map_link_update().
+	 */
+	bpf_map_put(&st_map->map);
+
+	ret = true;
+
+fail_unlock:
+	mutex_unlock(&update_mutex);
+	rcu_read_lock();
+	bpf_link_put(&st_link->link);
+fail:
+	bpf_map_put(&st_map->map);
+	return ret;
+}
+EXPORT_SYMBOL_GPL(bpf_struct_ops_kvalue_unreg);
#7 lore raw

Re: [PATCH bpf-next 3/6] bpf: provide a function to unregister struct_ops objects from consumers.

Quoted 24 lines · Martin KaFai Lau
Quoted 8 lines · Kui-Feng Lee

+/* Called from the subsystem that consume the struct_ops.
+ *
+ * The caller should protected this function by holding rcu_read_lock() to
+ * ensure "data" is valid. However, this function may unlock rcu
+ * temporarily. The caller should not rely on the preceding rcu_read_lock()
+ * after returning from this function.

This temporarily losing rcu_read_lock protection is error prone. The caller should do the inc_not_zero() instead if it is needed.

I feel the approach in patch 1 and 3 is a little box-ed in by the earlier tcp-cc usage that tried to fit into the kernel module reg/unreg paradigm and hide as much bpf details as possible from tcp-cc. This is not necessarily true now for other subsystem which has bpf struct_ops from day one.

The epoll detach notification is link only. Can this kernel side specific unreg be limited to struct_ops link only? During reg, a rcu protected link could be passed to the subsystem. That subsystem becomes a kernel user of the bpf link and it can call link_detach(link) to detach. Pseudo code:

struct link __rcu *link;

rcu_read_lock();
ref_link = rcu_dereference(link)
if (ref_link)
    ref_link = bpf_link_inc_not_zero(ref_link);
rcu_read_unlock();

Quoted 4 lines

if (!IS_ERR_OR_NULL(ref_link)) {
    bpf_struct_ops_map_link_detach(ref_link);
    bpf_link_put(ref_link);
}

Since not every struct_ops map has a link, we need a callback in additional to ops->reg to register links with subsystems. If the callback is ops->reg_link, struct_ops will call ops->reg_link if a subsystem provide it and the map is registered through a link, or it should call ops->reg.

Quoted 72 lines
Quoted 71 lines

+ *
+ * Return true if unreg() success. If a call fails, it means some other
+ * task has unrgistered or is unregistering the same object.
+ */

kernel/bpf/bpf_struct_ops.c +66 -0
+bool bpf_struct_ops_kvalue_unreg(void *data)
+{
+    struct bpf_struct_ops_map *st_map =
+        container_of(data, struct bpf_struct_ops_map, kvalue.data);
+    enum bpf_struct_ops_state prev_state;
+    struct bpf_struct_ops_link *st_link;
+    bool ret = false;
+
+    /* The st_map and st_link should be protected by rcu_read_lock(),
+     * or they may have been free when we try to increase their
+     * refcount.
+     */
+    if (IS_ERR(bpf_map_inc_not_zero(&st_map->map)))
+        /* The map is already gone */
+        return false;
+
+    prev_state = cmpxchg(&st_map->kvalue.common.state,
+                 BPF_STRUCT_OPS_STATE_INUSE,
+                 BPF_STRUCT_OPS_STATE_TOBEFREE);
+    if (prev_state == BPF_STRUCT_OPS_STATE_INUSE) {
+        st_map->st_ops_desc->st_ops->unreg(data);
+        /* Pair with bpf_map_inc() for reg() */
+        bpf_map_put(&st_map->map);
+        /* Pair with bpf_map_inc_not_zero() above */
+        bpf_map_put(&st_map->map);
+        return true;
+    }
+    if (prev_state != BPF_STRUCT_OPS_STATE_READY)
+        goto fail;
+
+    /* With BPF_F_LINK */
+
+    st_link = rcu_dereference(st_map->attached);
+    if (!st_link || !bpf_link_inc_not_zero(&st_link->link))
+        /* The map is on the way to unregister */
+        goto fail;
+
+    rcu_read_unlock();
+    mutex_lock(&update_mutex);
+
+    if (rcu_dereference_protected(st_link->map, true) != &st_map->map)
+        /* The map should be unregistered already or on the way to
+         * be unregistered.
+         */
+        goto fail_unlock;
+
+    st_map->st_ops_desc->st_ops->unreg(data);
+
+    map_attached_null(st_map);
+    rcu_assign_pointer(st_link->map, NULL);
+    /* Pair with bpf_map_get() in bpf_struct_ops_link_create() or
+     * bpf_map_inc() in bpf_struct_ops_map_link_update().
+     */
+    bpf_map_put(&st_map->map);
+
+    ret = true;
+
+fail_unlock:
+    mutex_unlock(&update_mutex);
+    rcu_read_lock();
+    bpf_link_put(&st_link->link);
+fail:
+    bpf_map_put(&st_map->map);
+    return ret;
+}
+EXPORT_SYMBOL_GPL(bpf_struct_ops_kvalue_unreg);
#8 lore raw

Re: [PATCH bpf-next 3/6] bpf: provide a function to unregister struct_ops objects from consumers.

Quoted 31 lines · Kui-Feng Lee
Quoted 22 lines · Martin KaFai Lau
Quoted 6 lines · Kui-Feng Lee

+/* Called from the subsystem that consume the struct_ops.
+ *
+ * The caller should protected this function by holding rcu_read_lock() to
+ * ensure "data" is valid. However, this function may unlock rcu
+ * temporarily. The caller should not rely on the preceding rcu_read_lock()
+ * after returning from this function.

This temporarily losing rcu_read_lock protection is error prone. The caller should do the inc_not_zero() instead if it is needed.

I feel the approach in patch 1 and 3 is a little box-ed in by the earlier tcp-cc usage that tried to fit into the kernel module reg/unreg paradigm and hide as much bpf details as possible from tcp-cc. This is not necessarily true now for other subsystem which has bpf struct_ops from day one.

The epoll detach notification is link only. Can this kernel side specific unreg be limited to struct_ops link only? During reg, a rcu protected link could be passed to the subsystem. That subsystem becomes a kernel user of the bpf link and it can call link_detach(link) to detach. Pseudo code:

struct link __rcu *link;

rcu_read_lock();
ref_link = rcu_dereference(link)
if (ref_link)
     ref_link = bpf_link_inc_not_zero(ref_link);
rcu_read_unlock();

Quoted 4 lines

if (!IS_ERR_OR_NULL(ref_link)) {
     bpf_struct_ops_map_link_detach(ref_link);
     bpf_link_put(ref_link);
}

Quoted 4 lines

Since not every struct_ops map has a link, we need a callback in additional to ops->reg to register links with subsystems. If the callback is ops->reg_link, struct_ops will call ops->reg_link if a subsystem provide it and the map is registered through a link, or it should call ops->reg.

I would just add a link pointer arg to the existing reg(). The same probably needs to be done for unreg(). Pass a NULL as the link if it does not have one during reg(). If the subsystem chooses to enforce link only struct_ops, it can reject if link is not provided during reg().

#9 lore raw

Re: [PATCH bpf-next 3/6] bpf: provide a function to unregister struct_ops objects from consumers.

In reply to Martin KaFai Lau
Quoted 26 lines · Martin KaFai Lau
Quoted 6 lines · Kui-Feng Lee

+/* Called from the subsystem that consume the struct_ops.
+ *
+ * The caller should protected this function by holding rcu_read_lock() to
+ * ensure "data" is valid. However, this function may unlock rcu
+ * temporarily. The caller should not rely on the preceding rcu_read_lock()
+ * after returning from this function.

This temporarily losing rcu_read_lock protection is error prone. The caller should do the inc_not_zero() instead if it is needed.

I feel the approach in patch 1 and 3 is a little box-ed in by the earlier tcp-cc usage that tried to fit into the kernel module reg/unreg paradigm and hide as much bpf details as possible from tcp-cc. This is not necessarily true now for other subsystem which has bpf struct_ops from day one.

The epoll detach notification is link only. Can this kernel side specific unreg be limited to struct_ops link only? During reg, a rcu protected link could be passed to the subsystem. That subsystem becomes a kernel user of the bpf link and it can call link_detach(link) to detach. Pseudo code:

struct link __rcu *link;

rcu_read_lock();
ref_link = rcu_dereference(link)
if (ref_link)
    ref_link = bpf_link_inc_not_zero(ref_link);
rcu_read_unlock();

if (!IS_ERR_OR_NULL(ref_link)) {
    bpf_struct_ops_map_link_detach(ref_link);
    bpf_link_put(ref_link);
}

[ ... ]

Quoted 30 lines
Quoted 30 lines

+ *
+ * Return true if unreg() success. If a call fails, it means some other
+ * task has unrgistered or is unregistering the same object.
+ */

kernel/bpf/bpf_struct_ops.c +25 -0
+bool bpf_struct_ops_kvalue_unreg(void *data)
+{
+    struct bpf_struct_ops_map *st_map =
+        container_of(data, struct bpf_struct_ops_map, kvalue.data);
+    enum bpf_struct_ops_state prev_state;
+    struct bpf_struct_ops_link *st_link;
+    bool ret = false;
+
+    /* The st_map and st_link should be protected by rcu_read_lock(),
+     * or they may have been free when we try to increase their
+     * refcount.
+     */
+    if (IS_ERR(bpf_map_inc_not_zero(&st_map->map)))
+        /* The map is already gone */
+        return false;
+
+    prev_state = cmpxchg(&st_map->kvalue.common.state,
+                 BPF_STRUCT_OPS_STATE_INUSE,
+                 BPF_STRUCT_OPS_STATE_TOBEFREE);
+    if (prev_state == BPF_STRUCT_OPS_STATE_INUSE) {
+        st_map->st_ops_desc->st_ops->unreg(data);
+        /* Pair with bpf_map_inc() for reg() */
+        bpf_map_put(&st_map->map);
+        /* Pair with bpf_map_inc_not_zero() above */
+        bpf_map_put(&st_map->map);
Quoted 8 lines

kernel/bpf/bpf_struct_ops.c static void bpf_struct_ops_map_link_dealloc(struct bpf_link *link) · line 1129

Quoted 8 lines

+        return true;
+    }
+    if (prev_state != BPF_STRUCT_OPS_STATE_READY) +        goto fail;

kernel/bpf/bpf_struct_ops.c +4 -0
+
+    /* With BPF_F_LINK */
+
+    st_link = rcu_dereference(st_map->attached);

From looking at the change in bpf_struct_ops_map_link_dealloc() in patch 1 again, I am not sure st_link is rcu gp protected either. bpf_struct_ops_map_link_dealloc() is still just kfree(st_link).

I also don't think it needs to complicate it further by making st_link go through rcu only for this use case. The subsystem must have its own lock to protect parallel reg() and unreg(). tcp-cc has tcp_cong_list_lock. From looking at scx, scx has scx_ops_enable_mutex. When it tries to do unreg itself by calling bpf_struct_ops_map_link_detach(link), it needs to acquire its own lock to ensure a parallel unreg() has not happened. Pseudo code:

struct bpf_link *link;

static void scx_ops_detach_by_kernel(void)

{
	struct bpf_link *ref_link;

	mutex_lock(&scx_ops_enable_mutex);
	ref_link = link;
	if (ref_link)
		ref_link = bpf_link_inc_not_zero(ref_link);
	mutex_unlock(&scx_ops_enable_mutex);

	if (!IS_ERR_OR_NULL(ref_link)) {
		ref_link->ops->detach(ref_link);
		bpf_link_put(ref_link);
	}
}
Quoted 35 lines
Quoted 33 lines

+    if (!st_link || !bpf_link_inc_not_zero(&st_link->link)) +        /* The map is on the way to unregister */ +        goto fail;

kernel/bpf/bpf_struct_ops.c +30 -0
+
+    rcu_read_unlock();
+    mutex_lock(&update_mutex);
+
+    if (rcu_dereference_protected(st_link->map, true) != &st_map->map)
+        /* The map should be unregistered already or on the way to
+         * be unregistered.
+         */
+        goto fail_unlock;
+
+    st_map->st_ops_desc->st_ops->unreg(data);
+
+    map_attached_null(st_map);
+    rcu_assign_pointer(st_link->map, NULL);
+    /* Pair with bpf_map_get() in bpf_struct_ops_link_create() or
+     * bpf_map_inc() in bpf_struct_ops_map_link_update().
+     */
+    bpf_map_put(&st_map->map);
+
+    ret = true;
+
+fail_unlock:
+    mutex_unlock(&update_mutex);
+    rcu_read_lock();
+    bpf_link_put(&st_link->link);
+fail:
+    bpf_map_put(&st_map->map);
+    return ret;
+}
+EXPORT_SYMBOL_GPL(bpf_struct_ops_kvalue_unreg);
#10 lore raw

Re: [PATCH bpf-next 3/6] bpf: provide a function to unregister struct_ops objects from consumers.

Quoted 86 lines · Martin KaFai Lau
Quoted 26 lines · Martin KaFai Lau
Quoted 6 lines · Kui-Feng Lee

+/* Called from the subsystem that consume the struct_ops.
+ *
+ * The caller should protected this function by holding rcu_read_lock() to
+ * ensure "data" is valid. However, this function may unlock rcu
+ * temporarily. The caller should not rely on the preceding rcu_read_lock()
+ * after returning from this function.

This temporarily losing rcu_read_lock protection is error prone. The caller should do the inc_not_zero() instead if it is needed.

I feel the approach in patch 1 and 3 is a little box-ed in by the earlier tcp-cc usage that tried to fit into the kernel module reg/unreg paradigm and hide as much bpf details as possible from tcp-cc. This is not necessarily true now for other subsystem which has bpf struct_ops from day one.

The epoll detach notification is link only. Can this kernel side specific unreg be limited to struct_ops link only? During reg, a rcu protected link could be passed to the subsystem. That subsystem becomes a kernel user of the bpf link and it can call link_detach(link) to detach. Pseudo code:

struct link __rcu *link;

rcu_read_lock();
ref_link = rcu_dereference(link)
if (ref_link)
     ref_link = bpf_link_inc_not_zero(ref_link);
rcu_read_unlock();

if (!IS_ERR_OR_NULL(ref_link)) {
     bpf_struct_ops_map_link_detach(ref_link);
     bpf_link_put(ref_link);
}

[ ... ]

Quoted 30 lines
Quoted 30 lines

+ *
+ * Return true if unreg() success. If a call fails, it means some other
+ * task has unrgistered or is unregistering the same object.
+ */

kernel/bpf/bpf_struct_ops.c +25 -0
+bool bpf_struct_ops_kvalue_unreg(void *data)
+{
+    struct bpf_struct_ops_map *st_map =
+        container_of(data, struct bpf_struct_ops_map, kvalue.data);
+    enum bpf_struct_ops_state prev_state;
+    struct bpf_struct_ops_link *st_link;
+    bool ret = false;
+
+    /* The st_map and st_link should be protected by rcu_read_lock(),
+     * or they may have been free when we try to increase their
+     * refcount.
+     */
+    if (IS_ERR(bpf_map_inc_not_zero(&st_map->map)))
+        /* The map is already gone */
+        return false;
+
+    prev_state = cmpxchg(&st_map->kvalue.common.state,
+                 BPF_STRUCT_OPS_STATE_INUSE,
+                 BPF_STRUCT_OPS_STATE_TOBEFREE);
+    if (prev_state == BPF_STRUCT_OPS_STATE_INUSE) {
+        st_map->st_ops_desc->st_ops->unreg(data);
+        /* Pair with bpf_map_inc() for reg() */
+        bpf_map_put(&st_map->map);
+        /* Pair with bpf_map_inc_not_zero() above */
+        bpf_map_put(&st_map->map);
Quoted 8 lines

kernel/bpf/bpf_struct_ops.c static void bpf_struct_ops_map_link_dealloc(struct bpf_link *link) · line 1129

Quoted 8 lines

+        return true;
+    }
+    if (prev_state != BPF_STRUCT_OPS_STATE_READY) +        goto fail;

kernel/bpf/bpf_struct_ops.c +4 -0
+
+    /* With BPF_F_LINK */
+
+    st_link = rcu_dereference(st_map->attached);

From looking at the change in bpf_struct_ops_map_link_dealloc() in patch 1
again, I am not sure st_link is rcu gp protected either. bpf_struct_ops_map_link_dealloc() is still just kfree(st_link).

I also don't think it needs to complicate it further by making st_link go through rcu only for this use case. The subsystem must have its own lock to protect parallel reg() and unreg(). tcp-cc has tcp_cong_list_lock. From looking at scx, scx has scx_ops_enable_mutex. When it tries to do unreg itself by calling bpf_struct_ops_map_link_detach(link), it needs to acquire its own lock to ensure a parallel unreg() has not happened. Pseudo code:

struct bpf_link *link;

static void scx_ops_detach_by_kernel(void)
{
    struct bpf_link *ref_link;

    mutex_lock(&scx_ops_enable_mutex); ref_link = link;
    if (ref_link)
        ref_link = bpf_link_inc_not_zero(ref_link); mutex_unlock(&scx_ops_enable_mutex);

Quoted 5 lines

    if (!IS_ERR_OR_NULL(ref_link)) {
        ref_link->ops->detach(ref_link);
        bpf_link_put(ref_link);
    }
}

and patch 1 should no longer be needed.

Quoted 38 lines
Quoted 36 lines
Quoted 34 lines

+    if (!st_link || !bpf_link_inc_not_zero(&st_link->link)) +        /* The map is on the way to unregister */ +        goto fail;

kernel/bpf/bpf_struct_ops.c +30 -0
+
+    rcu_read_unlock();
+    mutex_lock(&update_mutex);
+
+    if (rcu_dereference_protected(st_link->map, true) != &st_map->map)
+        /* The map should be unregistered already or on the way to
+         * be unregistered.
+         */
+        goto fail_unlock;
+
+    st_map->st_ops_desc->st_ops->unreg(data);
+
+    map_attached_null(st_map);
+    rcu_assign_pointer(st_link->map, NULL);
+    /* Pair with bpf_map_get() in bpf_struct_ops_link_create() or
+     * bpf_map_inc() in bpf_struct_ops_map_link_update().
+     */
+    bpf_map_put(&st_map->map);
+
+    ret = true;
+
+fail_unlock:
+    mutex_unlock(&update_mutex);
+    rcu_read_lock();
+    bpf_link_put(&st_link->link);
+fail:
+    bpf_map_put(&st_map->map);
+    return ret;
+}
+EXPORT_SYMBOL_GPL(bpf_struct_ops_kvalue_unreg);
#11 lore raw

Re: [PATCH bpf-next 3/6] bpf: provide a function to unregister struct_ops objects from consumers.

In reply to Martin KaFai Lau
Quoted 65 lines · Martin KaFai Lau
Quoted 28 lines · Martin KaFai Lau
Quoted 8 lines · Kui-Feng Lee

+/* Called from the subsystem that consume the struct_ops.
+ *
+ * The caller should protected this function by holding rcu_read_lock() to
+ * ensure "data" is valid. However, this function may unlock rcu
+ * temporarily. The caller should not rely on the preceding rcu_read_lock()
+ * after returning from this function.

This temporarily losing rcu_read_lock protection is error prone. The caller should do the inc_not_zero() instead if it is needed.

I feel the approach in patch 1 and 3 is a little box-ed in by the earlier tcp-cc usage that tried to fit into the kernel module reg/unreg paradigm and hide as much bpf details as possible from tcp-cc. This is not necessarily true now for other subsystem which has bpf struct_ops from day one.

The epoll detach notification is link only. Can this kernel side specific unreg be limited to struct_ops link only? During reg, a rcu protected link could be passed to the subsystem. That subsystem becomes a kernel user of the bpf link and it can call link_detach(link) to detach. Pseudo code:

struct link __rcu *link;

rcu_read_lock();
ref_link = rcu_dereference(link)
if (ref_link)
     ref_link = bpf_link_inc_not_zero(ref_link);
rcu_read_unlock();

if (!IS_ERR_OR_NULL(ref_link)) {
     bpf_struct_ops_map_link_detach(ref_link);
     bpf_link_put(ref_link);
}

[ ... ]

Quoted 33 lines
Quoted 33 lines

+ *
+ * Return true if unreg() success. If a call fails, it means some other
+ * task has unrgistered or is unregistering the same object.
+ */

kernel/bpf/bpf_struct_ops.c +28 -0
+bool bpf_struct_ops_kvalue_unreg(void *data)
+{
+    struct bpf_struct_ops_map *st_map =
+        container_of(data, struct bpf_struct_ops_map, kvalue.data);
+    enum bpf_struct_ops_state prev_state;
+    struct bpf_struct_ops_link *st_link;
+    bool ret = false;
+
+    /* The st_map and st_link should be protected by rcu_read_lock(),
+     * or they may have been free when we try to increase their
+     * refcount.
+     */
+    if (IS_ERR(bpf_map_inc_not_zero(&st_map->map)))
+        /* The map is already gone */
+        return false;
+
+    prev_state = cmpxchg(&st_map->kvalue.common.state,
+                 BPF_STRUCT_OPS_STATE_INUSE,
+                 BPF_STRUCT_OPS_STATE_TOBEFREE);
+    if (prev_state == BPF_STRUCT_OPS_STATE_INUSE) {
+        st_map->st_ops_desc->st_ops->unreg(data);
+        /* Pair with bpf_map_inc() for reg() */
+        bpf_map_put(&st_map->map);
+        /* Pair with bpf_map_inc_not_zero() above */
+        bpf_map_put(&st_map->map);
+        return true;
+    }
+    if (prev_state != BPF_STRUCT_OPS_STATE_READY)
Quoted 8 lines
Quoted 5 lines

kernel/bpf/bpf_struct_ops.c static void bpf_struct_ops_map_link_dealloc(struct bpf_link *link) · line 1129

Quoted 5 lines

+        goto fail;

kernel/bpf/bpf_struct_ops.c +4 -0
+
+    /* With BPF_F_LINK */
+
+    st_link = rcu_dereference(st_map->attached);

From looking at the change in bpf_struct_ops_map_link_dealloc() in
patch 1 again, I am not sure st_link is rcu gp protected either. bpf_struct_ops_map_link_dealloc() is still just kfree(st_link).

I am not sure what you mean.
With the implementation of this version, st_link should be rcu protected. The backward pointer, "attached", from st_map to st_link will be reset before kfree(). So, if the caller hold rcu_read_lock(), a st_link should be valid as long as it can be reached from a st_map.

Quoted 57 lines

I also don't think it needs to complicate it further by making st_link go through rcu only for this use case. The subsystem must have its own lock to protect parallel reg() and unreg(). tcp-cc has tcp_cong_list_lock. From looking at scx, scx has scx_ops_enable_mutex. When it tries to do unreg itself by calling bpf_struct_ops_map_link_detach(link), it needs to acquire its own lock to ensure a parallel unreg() has not happened. Pseudo code:

struct bpf_link *link;

static void scx_ops_detach_by_kernel(void)
{
    struct bpf_link *ref_link;

    mutex_lock(&scx_ops_enable_mutex); ref_link = link;
    if (ref_link)
        ref_link = bpf_link_inc_not_zero(ref_link); mutex_unlock(&scx_ops_enable_mutex);

    if (!IS_ERR_OR_NULL(ref_link)) {
        ref_link->ops->detach(ref_link);
        bpf_link_put(ref_link);
    }
}

Quoted 35 lines
Quoted 33 lines

+    if (!st_link || !bpf_link_inc_not_zero(&st_link->link)) +        /* The map is on the way to unregister */ +        goto fail;

kernel/bpf/bpf_struct_ops.c +30 -0
+
+    rcu_read_unlock();
+    mutex_lock(&update_mutex);
+
+    if (rcu_dereference_protected(st_link->map, true) != &st_map->map)
+        /* The map should be unregistered already or on the way to
+         * be unregistered.
+         */
+        goto fail_unlock;
+
+    st_map->st_ops_desc->st_ops->unreg(data);
+
+    map_attached_null(st_map);
+    rcu_assign_pointer(st_link->map, NULL);
+    /* Pair with bpf_map_get() in bpf_struct_ops_link_create() or
+     * bpf_map_inc() in bpf_struct_ops_map_link_update().
+     */
+    bpf_map_put(&st_map->map);
+
+    ret = true;
+
+fail_unlock:
+    mutex_unlock(&update_mutex);
+    rcu_read_lock();
+    bpf_link_put(&st_link->link);
+fail:
+    bpf_map_put(&st_map->map);
+    return ret;
+}
+EXPORT_SYMBOL_GPL(bpf_struct_ops_kvalue_unreg);
#12 lore raw

Re: [PATCH bpf-next 3/6] bpf: provide a function to unregister struct_ops objects from consumers.

Quoted 5 lines · Kui-Feng Lee
Quoted 5 lines
Quoted 5 lines

kernel/bpf/bpf_struct_ops.c static void bpf_struct_ops_map_link_dealloc(struct bpf_link *link) · line 1115

Quoted 5 lines
kernel/bpf/bpf_struct_ops.c +3 -0
+    prev_state = cmpxchg(&st_map->kvalue.common.state,
+                 BPF_STRUCT_OPS_STATE_INUSE,
+                 BPF_STRUCT_OPS_STATE_TOBEFREE);

All the uses of cmpxchg mean that there are races. I know there is already a case for it in the existing code and maybe it's justified.
But I would very much prefer the whole code converted to clean locks without cmpxchg tricks.

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

kernel/bpf/bpf_struct_ops.c static void bpf_struct_ops_map_link_dealloc(struct bpf_link *link) · line 1122

Quoted 7 lines
kernel/bpf/bpf_struct_ops.c +7 -0
+    if (prev_state == BPF_STRUCT_OPS_STATE_INUSE) {
+        st_map->st_ops_desc->st_ops->unreg(data);
+        /* Pair with bpf_map_inc() for reg() */
+        bpf_map_put(&st_map->map);
+        /* Pair with bpf_map_inc_not_zero() above */
+        bpf_map_put(&st_map->map);
+        return true;

I haven't tried to follow the logic, but double bpf_map_put on the same map screams that this is broken.
Do proper locks everywhere.
inc_not_zero and cmpxchg shouldn't be needed.
keep it simple.

#13 lore raw

Re: [PATCH bpf-next 3/6] bpf: provide a function to unregister struct_ops objects from consumers.

Quoted 22 lines · Alexei Starovoitov
Quoted 5 lines · Kui-Feng Lee
Quoted 5 lines
Quoted 5 lines

kernel/bpf/bpf_struct_ops.c static void bpf_struct_ops_map_link_dealloc(struct bpf_link *link) · line 1115

Quoted 5 lines
kernel/bpf/bpf_struct_ops.c +3 -0
+    prev_state = cmpxchg(&st_map->kvalue.common.state,
+                 BPF_STRUCT_OPS_STATE_INUSE,
+                 BPF_STRUCT_OPS_STATE_TOBEFREE);

All the uses of cmpxchg mean that there are races. I know there is already a case for it in the existing code and maybe it's justified.
But I would very much prefer the whole code converted to clean locks without cmpxchg tricks.

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

kernel/bpf/bpf_struct_ops.c static void bpf_struct_ops_map_link_dealloc(struct bpf_link *link) · line 1122

Quoted 7 lines
kernel/bpf/bpf_struct_ops.c +7 -0
+    if (prev_state == BPF_STRUCT_OPS_STATE_INUSE) {
+        st_map->st_ops_desc->st_ops->unreg(data);
+        /* Pair with bpf_map_inc() for reg() */
+        bpf_map_put(&st_map->map);
+        /* Pair with bpf_map_inc_not_zero() above */
+        bpf_map_put(&st_map->map);
+        return true;
Quoted 5 lines

I haven't tried to follow the logic, but double bpf_map_put on the same map screams that this is broken.
Do proper locks everywhere.
inc_not_zero and cmpxchg shouldn't be needed.
keep it simple.

Got it!

#14 lore raw

Re: [PATCH bpf-next 3/6] bpf: provide a function to unregister struct_ops objects from consumers.

In reply to Kui-Feng Lee
Quoted 74 lines · Kui-Feng Lee
Quoted 62 lines · Martin KaFai Lau
Quoted 26 lines · Martin KaFai Lau
Quoted 6 lines · Kui-Feng Lee

+/* Called from the subsystem that consume the struct_ops.
+ *
+ * The caller should protected this function by holding rcu_read_lock() to
+ * ensure "data" is valid. However, this function may unlock rcu
+ * temporarily. The caller should not rely on the preceding rcu_read_lock()
+ * after returning from this function.

This temporarily losing rcu_read_lock protection is error prone. The caller should do the inc_not_zero() instead if it is needed.

I feel the approach in patch 1 and 3 is a little box-ed in by the earlier tcp-cc usage that tried to fit into the kernel module reg/unreg paradigm and hide as much bpf details as possible from tcp-cc. This is not necessarily true now for other subsystem which has bpf struct_ops from day one.

The epoll detach notification is link only. Can this kernel side specific unreg be limited to struct_ops link only? During reg, a rcu protected link could be passed to the subsystem. That subsystem becomes a kernel user of the bpf link and it can call link_detach(link) to detach. Pseudo code:

struct link __rcu *link;

rcu_read_lock();
ref_link = rcu_dereference(link)
if (ref_link)
     ref_link = bpf_link_inc_not_zero(ref_link);
rcu_read_unlock();

if (!IS_ERR_OR_NULL(ref_link)) {
     bpf_struct_ops_map_link_detach(ref_link);
     bpf_link_put(ref_link);
}

[ ... ]

Quoted 32 lines
Quoted 32 lines

+ *
+ * Return true if unreg() success. If a call fails, it means some other
+ * task has unrgistered or is unregistering the same object.
+ */

kernel/bpf/bpf_struct_ops.c +27 -0
+bool bpf_struct_ops_kvalue_unreg(void *data)
+{
+    struct bpf_struct_ops_map *st_map =
+        container_of(data, struct bpf_struct_ops_map, kvalue.data);
+    enum bpf_struct_ops_state prev_state;
+    struct bpf_struct_ops_link *st_link;
+    bool ret = false;
+
+    /* The st_map and st_link should be protected by rcu_read_lock(),
+     * or they may have been free when we try to increase their
+     * refcount.
+     */
+    if (IS_ERR(bpf_map_inc_not_zero(&st_map->map)))
+        /* The map is already gone */
+        return false;
+
+    prev_state = cmpxchg(&st_map->kvalue.common.state,
+                 BPF_STRUCT_OPS_STATE_INUSE,
+                 BPF_STRUCT_OPS_STATE_TOBEFREE);
+    if (prev_state == BPF_STRUCT_OPS_STATE_INUSE) {
+        st_map->st_ops_desc->st_ops->unreg(data);
+        /* Pair with bpf_map_inc() for reg() */
+        bpf_map_put(&st_map->map);
+        /* Pair with bpf_map_inc_not_zero() above */
+        bpf_map_put(&st_map->map);
+        return true;
+    }
Quoted 8 lines
Quoted 6 lines

kernel/bpf/bpf_struct_ops.c static void bpf_struct_ops_map_link_dealloc(struct bpf_link *link) · line 1129

Quoted 6 lines

+    if (prev_state != BPF_STRUCT_OPS_STATE_READY) +        goto fail;

kernel/bpf/bpf_struct_ops.c +4 -0
+
+    /* With BPF_F_LINK */
+
+    st_link = rcu_dereference(st_map->attached);

 From looking at the change in bpf_struct_ops_map_link_dealloc() in patch 1 again, I am not sure st_link is rcu gp protected either. bpf_struct_ops_map_link_dealloc() is still just kfree(st_link).

Quoted 1 line

I am not sure what you mean.

I meant this should be kfree_rcu(st_link, ...) instead of kfree(st_link).

The temporarily losing rcu_read_lock and other complexities in this function are too subtle. It is hard to reason and should be simplified even if tradeoff is needed. Please consider the ideas in my earlier code snippet about using the subsystem existing lock during reg/unreg/detach and limit it to link only first.

Quoted 62 lines

With the implementation of this version, st_link should be rcu protected. The backward pointer, "attached", from st_map to st_link will be reset before kfree(). So, if the caller hold rcu_read_lock(), a st_link should be valid as long as it can be reached from a st_map.

Quoted 57 lines

I also don't think it needs to complicate it further by making st_link go through rcu only for this use case. The subsystem must have its own lock to protect parallel reg() and unreg(). tcp-cc has tcp_cong_list_lock. From looking at scx, scx has scx_ops_enable_mutex. When it tries to do unreg itself by calling bpf_struct_ops_map_link_detach(link), it needs to acquire its own lock to ensure a parallel unreg() has not happened. Pseudo code:

struct bpf_link *link;

static void scx_ops_detach_by_kernel(void)
{
     struct bpf_link *ref_link;

     mutex_lock(&scx_ops_enable_mutex); ref_link = link;
     if (ref_link)
         ref_link = bpf_link_inc_not_zero(ref_link); mutex_unlock(&scx_ops_enable_mutex);

     if (!IS_ERR_OR_NULL(ref_link)) {
         ref_link->ops->detach(ref_link);
         bpf_link_put(ref_link);
     }
}

Quoted 35 lines
Quoted 33 lines

+    if (!st_link || !bpf_link_inc_not_zero(&st_link->link)) +        /* The map is on the way to unregister */ +        goto fail;

kernel/bpf/bpf_struct_ops.c +30 -0
+
+    rcu_read_unlock();
+    mutex_lock(&update_mutex);
+
+    if (rcu_dereference_protected(st_link->map, true) != &st_map->map)
+        /* The map should be unregistered already or on the way to
+         * be unregistered.
+         */
+        goto fail_unlock;
+
+    st_map->st_ops_desc->st_ops->unreg(data);
+
+    map_attached_null(st_map);
+    rcu_assign_pointer(st_link->map, NULL);
+    /* Pair with bpf_map_get() in bpf_struct_ops_link_create() or
+     * bpf_map_inc() in bpf_struct_ops_map_link_update().
+     */
+    bpf_map_put(&st_map->map);
+
+    ret = true;
+
+fail_unlock:
+    mutex_unlock(&update_mutex);
+    rcu_read_lock();
+    bpf_link_put(&st_link->link);
+fail:
+    bpf_map_put(&st_map->map);
+    return ret;
+}
+EXPORT_SYMBOL_GPL(bpf_struct_ops_kvalue_unreg);
#15 lore raw

PATCH 4/6 bpf: detach a bpf_struct_ops_map from a link.

In reply to Kui-Feng Lee

Allow user space programs to detach a struct_ops map from the attached link.

---
 kernel/bpf/bpf_struct_ops.c | 28 ++++++++++++++++++++++++++++
 1 file changed, 28 insertions(+)
kernel/bpf/bpf_struct_ops.c +28 -0
index 8e79b02a1ccb..4a8a7e5ffc56 100644
@@ -1252,8 +1252,36 @@ static int bpf_struct_ops_map_link_update(struct bpf_link *link, struct bpf_map
 	return err;
 }
 
+static int bpf_struct_ops_map_link_detach(struct bpf_link *link)
+{
+	struct bpf_struct_ops_link *st_link;
+	struct bpf_struct_ops_map *st_map;
+	struct bpf_map *map;
+
+	mutex_lock(&update_mutex);
+
+	st_link = container_of(link, struct bpf_struct_ops_link, link);
+	map = rcu_dereference_protected(st_link->map, lockdep_is_held(&update_mutex));
+	if (!map) {
+		mutex_unlock(&update_mutex);
+		return -ENOENT;
+	}
+	st_map = container_of(map, struct bpf_struct_ops_map, map);
+
+	st_map->st_ops_desc->st_ops->unreg(&st_map->kvalue.data);
+
+	map_attached_null(st_map);
+	rcu_assign_pointer(st_link->map, NULL);
+	bpf_map_put(map);
+
+	mutex_unlock(&update_mutex);
+
+	return 0;
+}
+
 static const struct bpf_link_ops bpf_struct_ops_map_lops = {
 	.dealloc = bpf_struct_ops_map_link_dealloc,
+	.detach = bpf_struct_ops_map_link_detach,
 	.show_fdinfo = bpf_struct_ops_map_link_show_fdinfo,
 	.fill_link_info = bpf_struct_ops_map_link_fill_link_info,
 	.update_map = bpf_struct_ops_map_link_update,
Signature · 1 line
2.34.1
#16 lore raw

PATCH 5/6 bpf: support epoll from bpf struct_ops links.

In reply to Kui-Feng Lee

Add epoll support to bpf struct_ops links to trigger EPOLLHUP event upon detachment.

---
 include/linux/bpf.h         |  2 ++
 kernel/bpf/bpf_struct_ops.c | 14 ++++++++++++++
 kernel/bpf/syscall.c        | 15 +++++++++++++++
 3 files changed, 31 insertions(+)
include/linux/bpf.h +2 -0
index eeeed4b1bd32..a4550b927352 100644
@@ -1574,6 +1574,7 @@ struct bpf_link {
 	const struct bpf_link_ops *ops;
 	struct bpf_prog *prog;
 	struct work_struct work;
+	wait_queue_head_t wait_hup;
 };
 
 struct bpf_link_ops {
@@ -1587,6 +1588,7 @@ struct bpf_link_ops {
 			      struct bpf_link_info *info);
 	int (*update_map)(struct bpf_link *link, struct bpf_map *new_map,
 			  struct bpf_map *old_map);
+	__poll_t (*poll)(struct file *file, struct poll_table_struct *pts);
 };
 
 struct bpf_tramp_link {
kernel/bpf/bpf_struct_ops.c +14 -0
index 4a8a7e5ffc56..f19b6a76591a 100644
@@ -12,6 +12,7 @@
 #include <linux/mutex.h>
 #include <linux/btf_ids.h>
 #include <linux/rcupdate_wait.h>
+#include <linux/poll.h>
 
 struct bpf_struct_ops_value {
 	struct bpf_struct_ops_common_value common;
@@ -1149,6 +1150,8 @@ bool bpf_struct_ops_kvalue_unreg(void *data)
 	 */
 	bpf_map_put(&st_map->map);
 
+	wake_up_interruptible_poll(&st_link->link.wait_hup, EPOLLHUP);
+
 	ret = true;
 
 fail_unlock:
@@ -1276,15 +1279,26 @@ static int bpf_struct_ops_map_link_detach(struct bpf_link *link)
 
 	mutex_unlock(&update_mutex);
 
+	wake_up_interruptible_poll(&st_link->link.wait_hup, EPOLLHUP);
+
 	return 0;
 }
 
+static __poll_t bpf_struct_ops_map_link_poll(struct file *file,
+					     struct poll_table_struct *ptrs)
+{
+	struct bpf_struct_ops_link *st_link = file->private_data;
+
+	return (st_link->map) ? 0 : EPOLLHUP | EPOLLERR;
+}
+
 static const struct bpf_link_ops bpf_struct_ops_map_lops = {
 	.dealloc = bpf_struct_ops_map_link_dealloc,
 	.detach = bpf_struct_ops_map_link_detach,
 	.show_fdinfo = bpf_struct_ops_map_link_show_fdinfo,
 	.fill_link_info = bpf_struct_ops_map_link_fill_link_info,
 	.update_map = bpf_struct_ops_map_link_update,
+	.poll = bpf_struct_ops_map_link_poll,
 };
 
 int bpf_struct_ops_link_create(union bpf_attr *attr)
kernel/bpf/syscall.c +15 -0
index 4a2f95c4b2ac..b4dbca04d4f5 100644
@@ -2990,6 +2990,7 @@ void bpf_link_init(struct bpf_link *link, enum bpf_link_type type,
 	link->id = 0;
 	link->ops = ops;
 	link->prog = prog;
+	init_waitqueue_head(&link->wait_hup);
 }
 
 static void bpf_link_free_id(int id)
@@ -3108,6 +3109,19 @@ static void bpf_link_show_fdinfo(struct seq_file *m, struct file *filp)
 }
 #endif
 
+static __poll_t bpf_link_poll(struct file *file, struct poll_table_struct *pts)
+{
+	struct bpf_link *link = file->private_data;
+
+	if (link->ops->poll) {
+		poll_wait(file, &link->wait_hup, pts);
+
+		return link->ops->poll(file, pts);
+	}
+
+	return 0;
+}
+
 static const struct file_operations bpf_link_fops = {
 #ifdef CONFIG_PROC_FS
 	.show_fdinfo	= bpf_link_show_fdinfo,
@@ -3115,6 +3129,7 @@ static const struct file_operations bpf_link_fops = {
 	.release	= bpf_link_release,
 	.read		= bpf_dummy_read,
 	.write		= bpf_dummy_write,
+	.poll		= bpf_link_poll,
 };
 
 static int bpf_link_alloc_id(struct bpf_link *link)
Signature · 1 line
2.34.1
#17 lore raw

Re: [PATCH bpf-next 5/6] bpf: support epoll from bpf struct_ops links.

Quoted 16 lines · Kui-Feng Lee

Add epoll support to bpf struct_ops links to trigger EPOLLHUP event upon detachment.

---
 include/linux/bpf.h         |  2 ++
 kernel/bpf/bpf_struct_ops.c | 14 ++++++++++++++
 kernel/bpf/syscall.c        | 15 +++++++++++++++
 3 files changed, 31 insertions(+)
include/linux/bpf.h +0 -0
index eeeed4b1bd32..a4550b927352 100644

include/linux/bpf.h struct bpf_link { · line 1577

Quoted 4 lines
include/linux/bpf.h +1 -0
@@ -1574,6 +1574,7 @@ struct bpf_link {
        const struct bpf_link_ops *ops;
        struct bpf_prog *prog;
        struct work_struct work;
+       wait_queue_head_t wait_hup;

let's keep it struct_ops-specific, there is no need to pay for this for all existing BPF link types. We can always generalize later, if necessary.

pw-bot: cr

Quoted 11 lines
include/linux/bpf.h +1 -0
 };
 
 struct bpf_link_ops {
@@ -1587,6 +1588,7 @@ struct bpf_link_ops {
                              struct bpf_link_info *info);
        int (*update_map)(struct bpf_link *link, struct bpf_map *new_map,
                          struct bpf_map *old_map);
+       __poll_t (*poll)(struct file *file, struct poll_table_struct *pts);
 };
 
 struct bpf_tramp_link {

[...]

#18 lore raw

Re: [PATCH bpf-next 5/6] bpf: support epoll from bpf struct_ops links.

Quoted 12 lines · Andrii Nakryiko
Quoted 11 lines · Kui-Feng Lee

Add epoll support to bpf struct_ops links to trigger EPOLLHUP event upon detachment.

---
  include/linux/bpf.h         |  2 ++
  kernel/bpf/bpf_struct_ops.c | 14 ++++++++++++++
  kernel/bpf/syscall.c        | 15 +++++++++++++++
  3 files changed, 31 insertions(+)
Quoted 14 lines

include/linux/bpf.h struct bpf_link { · line 1577

Quoted 9 lines
include/linux/bpf.h +1 -0
index eeeed4b1bd32..a4550b927352 100644
@@ -1574,6 +1574,7 @@ struct bpf_link {
         const struct bpf_link_ops *ops;
         struct bpf_prog *prog;
         struct work_struct work;
+       wait_queue_head_t wait_hup;

let's keep it struct_ops-specific, there is no need to pay for this for all existing BPF link types. We can always generalize later, if necessary.

sure!

Quoted 16 lines

pw-bot: cr

Quoted 11 lines
include/linux/bpf.h +1 -0
  };
 
  struct bpf_link_ops {
@@ -1587,6 +1588,7 @@ struct bpf_link_ops {
                               struct bpf_link_info *info);
         int (*update_map)(struct bpf_link *link, struct bpf_map *new_map,
                           struct bpf_map *old_map);
+       __poll_t (*poll)(struct file *file, struct poll_table_struct *pts);
  };
 
  struct bpf_tramp_link {

[...]

#19 lore raw

PATCH 6/6 selftests/bpf: test detaching struct_ops links.

In reply to Kui-Feng Lee

Verify whether a user space program is informed through epoll with EPOLLHUP when a struct_ops object is detached or unregistered using the function bpf_struct_ops_kvalue_unreg() or BPF_LINK_DETACH.

---
 .../selftests/bpf/bpf_testmod/bpf_testmod.c   |  18 ++-
 .../selftests/bpf/bpf_testmod/bpf_testmod.h   |   1 +
 .../bpf/prog_tests/test_struct_ops_module.c   | 104 ++++++++++++++++++
 .../selftests/bpf/progs/struct_ops_module.c   |   7 ++
 4 files changed, 126 insertions(+), 4 deletions(-)
tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.c +14 -4
index 39ad96a18123..e526ccfad8bf 100644
@@ -539,16 +539,20 @@ static int bpf_testmod_ops_init_member(const struct btf_type *t,
 				       const struct btf_member *member,
 				       void *kdata, const void *udata)
 {
+	struct bpf_testmod_ops *ops = kdata;
+
 	if (member->offset == offsetof(struct bpf_testmod_ops, data) * 8) {
 		/* For data fields, this function has to copy it and return
 		 * 1 to indicate that the data has been handled by the
 		 * struct_ops type, or the verifier will reject the map if
 		 * the value of the data field is not zero.
 		 */
-		((struct bpf_testmod_ops *)kdata)->data = ((struct bpf_testmod_ops *)udata)->data;
-		return 1;
-	}
-	return 0;
+		ops->data = ((struct bpf_testmod_ops *)udata)->data;
+	} else if (member->offset == offsetof(struct bpf_testmod_ops, do_unreg) * 8) {
+		ops->do_unreg = ((struct bpf_testmod_ops *)udata)->do_unreg;
+	} else
+		return 0;
+	return 1;
 }
 
 static const struct btf_kfunc_id_set bpf_testmod_kfunc_set = {
@@ -572,6 +576,12 @@ static int bpf_dummy_reg(void *kdata)
 	if (ops->test_2)
 		ops->test_2(4, ops->data);
 
+	if (ops->do_unreg) {
+		rcu_read_lock();
+		bpf_struct_ops_kvalue_unreg(kdata);
+		rcu_read_unlock();
+	}
+
 	return 0;
 }
 
tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.h +1 -0
index 23fa1872ee67..ee8d4a2cd187 100644
@@ -43,6 +43,7 @@ struct bpf_testmod_ops {
 		int b;
 	} unsupported;
 	int data;
+	bool do_unreg;
 
 	/* The following pointers are used to test the maps having multiple
 	 * pages of trampolines.
tools/testing/selftests/bpf/prog_tests/test_struct_ops_module.c +104 -0

121 lines not on this page · raw message

tools/testing/selftests/bpf/progs/struct_ops_module.c +7 -0
index 63b065dae002..7a697a7dd0ac 100644
@@ -81,3 +81,10 @@ struct bpf_testmod_ops___incompatible testmod_incompatible = {
 	.test_2 = (void *)test_2,
 	.data = 3,
 };
+
+SEC(".struct_ops.link")
+struct bpf_testmod_ops testmod_do_unreg = {
+	.test_1 = (void *)test_1,
+	.test_2 = (void *)test_2,
+	.do_unreg = true,
+};
Signature · 1 line
2.34.1
#20 lore raw

Re: [PATCH bpf-next 6/6] selftests/bpf: test detaching struct_ops links.

Quoted 13 lines · Kui-Feng Lee

Verify whether a user space program is informed through epoll with EPOLLHUP when a struct_ops object is detached or unregistered using the function bpf_struct_ops_kvalue_unreg() or BPF_LINK_DETACH.

---
 .../selftests/bpf/bpf_testmod/bpf_testmod.c   |  18 ++-
 .../selftests/bpf/bpf_testmod/bpf_testmod.h   |   1 +
 .../bpf/prog_tests/test_struct_ops_module.c   | 104 ++++++++++++++++++
 .../selftests/bpf/progs/struct_ops_module.c   |   7 ++
 4 files changed, 126 insertions(+), 4 deletions(-)

[...]

tools/testing/selftests/bpf/progs/struct_ops_module.c

Quoted 4 lines
tools/testing/selftests/bpf/progs/struct_ops_module.c +0 -0
index 63b065dae002..7a697a7dd0ac 100644

this file is a bit overloaded with code for various subtests, it's quite hard already to follow what's going on, I suggest adding a separate .c file for this new subtest

Quoted 14 lines
tools/testing/selftests/bpf/progs/struct_ops_module.c +7 -1
@@ -81,3 +81,10 @@ struct bpf_testmod_ops___incompatible testmod_incompatible = {
        .test_2 = (void *)test_2,
        .data = 3,
 };
+
+SEC(".struct_ops.link")
+struct bpf_testmod_ops testmod_do_unreg = {
+       .test_1 = (void *)test_1,
+       .test_2 = (void *)test_2,
+       .do_unreg = true,
+};
--

2.34.1

#21 lore raw

Re: [PATCH bpf-next 6/6] selftests/bpf: test detaching struct_ops links.

Quoted 17 lines · Andrii Nakryiko
Quoted 13 lines · Kui-Feng Lee

Verify whether a user space program is informed through epoll with EPOLLHUP when a struct_ops object is detached or unregistered using the function bpf_struct_ops_kvalue_unreg() or BPF_LINK_DETACH.

---
  .../selftests/bpf/bpf_testmod/bpf_testmod.c   |  18 ++-
  .../selftests/bpf/bpf_testmod/bpf_testmod.h   |   1 +
  .../bpf/prog_tests/test_struct_ops_module.c   | 104 ++++++++++++++++++
  .../selftests/bpf/progs/struct_ops_module.c   |   7 ++
  4 files changed, 126 insertions(+), 4 deletions(-)

[...]

Quoted 8 lines

tools/testing/selftests/bpf/progs/struct_ops_module.c

Quoted 4 lines
tools/testing/selftests/bpf/progs/struct_ops_module.c +0 -0
index 63b065dae002..7a697a7dd0ac 100644

this file is a bit overloaded with code for various subtests, it's quite hard already to follow what's going on, I suggest adding a separate .c file for this new subtest

Ok!

Quoted 15 lines
Quoted 15 lines
tools/testing/selftests/bpf/progs/struct_ops_module.c +7 -1
@@ -81,3 +81,10 @@ struct bpf_testmod_ops___incompatible testmod_incompatible = {
         .test_2 = (void *)test_2,
         .data = 3,
  };
+
+SEC(".struct_ops.link")
+struct bpf_testmod_ops testmod_do_unreg = {
+       .test_1 = (void *)test_1,
+       .test_2 = (void *)test_2,
+       .do_unreg = true,
+};
--

2.34.1

#22 lore raw

Re: [PATCH bpf-next 6/6] selftests/bpf: test detaching struct_ops links.

In reply to Kui-Feng Lee

tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.c static int bpf_dummy_reg(void *kdata) · line 581

Quoted 7 lines · Kui-Feng Lee
tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.c +3 -0
@@ -572,6 +576,12 @@ static int bpf_dummy_reg(void *kdata)
  	if (ops->test_2)
  		ops->test_2(4, ops->data);
 
+	if (ops->do_unreg) {
+		rcu_read_lock();
+		bpf_struct_ops_kvalue_unreg(kdata);

Instead of unreg() immediately before the reg() has returned, the test should reflect more on how the subsystem can use it in practice. The subsystem does not do unreg() during reg().

It also needs to test a case when the link is created and successfully registered to the subsystem. The user space does BPF_LINK_DETACH first and then the subsystem does link->ops->detach() by itself later.

It can create a kfunc in bpf_testmod.c to trigger the subsystem to do link->ops->detach(). The kfunc can be called by a SEC("syscall") bpf prog which is run by bpf_prog_test_run_opts(). The test_progs can then decide on the timing when to do link->ops->detach() to test different cases.

Quoted 5 lines
tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.c +3 -0
+		rcu_read_unlock();
+	}
+
  	return 0;
  }
#23 lore raw

Re: [PATCH bpf-next 6/6] selftests/bpf: test detaching struct_ops links.

Quoted 11 lines · Martin KaFai Lau

tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.c static int bpf_dummy_reg(void *kdata) · line 580

Quoted 6 lines · Kui-Feng Lee
tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.c +3 -0
@@ -572,6 +576,12 @@ static int bpf_dummy_reg(void *kdata)
       if (ops->test_2)
           ops->test_2(4, ops->data);
+    if (ops->do_unreg) {
+        rcu_read_lock();
+        bpf_struct_ops_kvalue_unreg(kdata);

Instead of unreg() immediately before the reg() has returned, the test should reflect more on how the subsystem can use it in practice. The subsystem does not do unreg() during reg().

It also needs to test a case when the link is created and successfully registered to the subsystem. The user space does BPF_LINK_DETACH first and then the subsystem does link->ops->detach() by itself later.

agree

Quoted 2 lines

It can create a kfunc in bpf_testmod.c to trigger the subsystem to do link->ops->detach(). The kfunc can be called by a SEC("syscall") bpf prog which is run by bpf_prog_test_run_opts(). The test_progs can then decide on the timing when to do link->ops->detach() to test different cases.

What is the purpose of this part?
If it goes through link->ops->detach(), it should work just like to call bpf_link_detach() twice on the same link from the user space. Do you want to make sure detaching a link twice work?

Quoted 7 lines
Quoted 6 lines
tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.c +3 -0
+        rcu_read_unlock();
+    }
+
       return 0;
   }
#24 lore raw

Re: [PATCH bpf-next 6/6] selftests/bpf: test detaching struct_ops links.

Quoted 19 lines · Kui-Feng Lee
Quoted 11 lines · Martin KaFai Lau

tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.c static int bpf_dummy_reg(void *kdata) · line 580

Quoted 6 lines · Kui-Feng Lee
tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.c +3 -0
@@ -572,6 +576,12 @@ static int bpf_dummy_reg(void *kdata)
       if (ops->test_2)
           ops->test_2(4, ops->data);
+    if (ops->do_unreg) {
+        rcu_read_lock();
+        bpf_struct_ops_kvalue_unreg(kdata);

Instead of unreg() immediately before the reg() has returned, the test should reflect more on how the subsystem can use it in practice. The subsystem does not do unreg() during reg().

It also needs to test a case when the link is created and successfully registered to the subsystem. The user space does BPF_LINK_DETACH first and >> then the subsystem does link->ops->detach() by itself later.

agree

Quoted 1 line
Quoted 6 lines
Quoted 1 line

It can create a kfunc in bpf_testmod.c to trigger the subsystem to do link->ops->detach(). The kfunc can be called by a SEC("syscall") bpf prog which is run by bpf_prog_test_run_opts(). The test_progs can then decide on the timing when to do link->ops->detach() to test different cases.

What is the purpose of this part?
If it goes through link->ops->detach(), it should work just like to call bpf_link_detach() twice on the same link from the user space. Do you want to make sure detaching a link twice work?

It is not quite what I meant and apparently link detach twice on the same valid (i.e. refcnt non zero) link won't work.

Anyhow, the idea is to show how the racing case may work in patch 3 (when userspace tries to detach and the subsystem tries to detach/unreg itself also). I was suggesting the kfunc idea such that the test_progs can have better control on the timing on when to ask the subsystem to unreg/detach itself instead of having to do the unreg() during the reg() as in patch 6 here. If kfunc does not make sense and there is a better way to do this, feel free to ignore.

#25 lore raw

Re: [PATCH bpf-next 6/6] selftests/bpf: test detaching struct_ops links.

Quoted 22 lines · Martin KaFai Lau
Quoted 21 lines · Kui-Feng Lee
Quoted 11 lines · Martin KaFai Lau

tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.c static int bpf_dummy_reg(void *kdata) · line 580

Quoted 6 lines · Kui-Feng Lee
tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.c +3 -0
@@ -572,6 +576,12 @@ static int bpf_dummy_reg(void *kdata)
       if (ops->test_2)
           ops->test_2(4, ops->data);
+    if (ops->do_unreg) {
+        rcu_read_lock();
+        bpf_struct_ops_kvalue_unreg(kdata);

Instead of unreg() immediately before the reg() has returned, the test should reflect more on how the subsystem can use it in practice. The subsystem does not do unreg() during reg().

It also needs to test a case when the link is created and successfully registered to the subsystem. The user space does BPF_LINK_DETACH first and  >> then the subsystem does link->ops->detach() by itself later.

agree

Quoted 2 lines

It can create a kfunc in bpf_testmod.c to trigger the subsystem to do link->ops->detach(). The kfunc can be called by a SEC("syscall") bpf prog which is run by bpf_prog_test_run_opts(). The test_progs can then decide on the timing when to do link->ops->detach() to test different cases.

Quoted 9 lines
Quoted 4 lines

What is the purpose of this part?
If it goes through link->ops->detach(), it should work just like to call bpf_link_detach() twice on the same link from the user space. Do you want to make sure detaching a link twice work?

It is not quite what I meant and apparently link detach twice on the same valid (i.e. refcnt non zero) link won't work.

Anyhow, the idea is to show how the racing case may work in patch 3 (when userspace tries to detach and the subsystem tries to detach/unreg itself also). I was suggesting the kfunc idea such that the test_progs can have better control on the timing on when to ask the subsystem to unreg/detach itself instead of having to do the unreg() during the reg() as in patch 6 here. If kfunc does not make sense and there is a better way to do this, feel free to ignore.

Ok! I think the case you are talking more like to happen when the link is destroyed, but bpf_struct_ops_map_link_dealloc() has not finished yet. Calling link->ops->detach() at this point may cause a racing since bpf_struct_ops_map_link_dealloc() doesn't acquire update_mutex.

Calling link->ops->detach() immediately after BPF_LINK_DETACH would not cause any racing since bpf_struct_ops_map_link_detach() always acquires update_mutex. They will be executed sequentially, and call st_map->ops->reg() sequentially as well.

I will add a test case to call link->ops->detach() after close the fd of the link.

#26 lore raw

Re: [PATCH bpf-next 6/6] selftests/bpf: test detaching struct_ops links.

Quoted 33 lines · Kui-Feng Lee
Quoted 30 lines · Martin KaFai Lau
Quoted 20 lines · Kui-Feng Lee
Quoted 10 lines · Martin KaFai Lau

tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.c static int bpf_dummy_reg(void *kdata) · line 580

Quoted 6 lines · Kui-Feng Lee
tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.c +3 -0
@@ -572,6 +576,12 @@ static int bpf_dummy_reg(void *kdata)
       if (ops->test_2)
           ops->test_2(4, ops->data);
+    if (ops->do_unreg) {
+        rcu_read_lock();
+        bpf_struct_ops_kvalue_unreg(kdata);

Instead of unreg() immediately before the reg() has returned, the test should reflect more on how the subsystem can use it in practice. The subsystem does not do unreg() during reg().

Quoted 2 lines

It also needs to test a case when the link is created and successfully registered to the subsystem. The user space does BPF_LINK_DETACH first and

Quoted 1 line
Quoted 1 line

then the subsystem does link->ops->detach() by itself later.

agree

Quoted 1 line
Quoted 6 lines
Quoted 1 line

It can create a kfunc in bpf_testmod.c to trigger the subsystem to do link->ops->detach(). The kfunc can be called by a SEC("syscall") bpf prog which is run by bpf_prog_test_run_opts(). The test_progs can then decide on the timing when to do link->ops->detach() to test different cases.

What is the purpose of this part?
If it goes through link->ops->detach(), it should work just like to call bpf_link_detach() twice on the same link from the user space. Do you want to make sure detaching a link twice work?

It is not quite what I meant and apparently link detach twice on the same valid (i.e. refcnt non zero) link won't work.

Quoted 7 lines
Quoted 2 lines

Anyhow, the idea is to show how the racing case may work in patch 3 (when userspace tries to detach and the subsystem tries to detach/unreg itself also). I was suggesting the kfunc idea such that the test_progs can have better control on the timing on when to ask the subsystem to unreg/detach itself instead of having to do the unreg() during the reg() as in patch 6 here. If kfunc does not make sense and there is a better way to do this, feel free to ignore.

Ok! I think the case you are talking more like to happen when the link is destroyed, but bpf_struct_ops_map_link_dealloc() has not finished yet. Calling link->ops->detach() at this point may cause a racing since bpf_struct_ops_map_link_dealloc() doesn't acquire update_mutex.

Yes, adding link_dealloc() (i.e. close the link) in between will be a good test too.

With or without link_dealloc()/close(), the idea is to test this race (user space detach and/or dealloc vs subsystem detach/unreg) or at least show how the subsystem should do it. I was merely suggesting to use kfunc (may be there is a better way and feel free to ignore). The details of the testing steps could be adjusted based on how patch 3 will look like.

Quoted 5 lines

Calling link->ops->detach() immediately after BPF_LINK_DETACH would not cause any racing since bpf_struct_ops_map_link_detach() always acquires update_mutex. They will be executed sequentially, and call st_map->ops->reg() sequentially as well.

I didn't meant the detach() itself is racy or not. That part is fine. It is more about the link that the subsystem is holding. I feel how patch 3 will look like may be something different from my current thinking. If this test does not make sense based on how patch 3 will look like, feel free to ignore also.

Quoted 4 lines

I will add a test case to call link->ops->detach() after close the fd of the link.