Skip to contentSkip to conversation

Sleepable BPF programs on cgroup {get,set}sockopt

RFC Superseded [email protected] 5 participants 19 messages Aug 11, 2023 – Aug 14, 2023

Subsystems (5)
Kselftest BPF BPF selftests BPF core +1 more subsystems

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

v2 cover Sleepable BPF programs on cgroup {get,set}sockopt

From: Kui-Feng Lee <[email protected]>

Major Changes from v1:

- Add bpf_so_optval_copy_to() and bpf_so_optval_copy_to_r() to copy data from a dynptr or raw buffer to the optval of a context, either in the kernel or user space, to simplify BPF programs.

- Restrict to having atmost one instance of dynptr initialized by bpf_so_optval_from() at any moment. It simplifies the memory management of the optval buffer in kernel.

- Fix the issue of bpf_prog_array_free() by replacing it with bpf_prog_array_free_sleepable().

Make BPF programs attached on cgroup/{get,set}sockopt hooks sleepable and able to call bpf_copy_from_user() and bpf_copy_to_user(), a new kfunc.

The Issue with CGroup {get,set}sockopt Hooks
============================================

Calling {get,set}sockopt from user space, optval is a pointer to a buffer. The format of the buffer depends on the level and optname, and its size is specified by optlen. The buffer is used by user space programs to pass values to setsockopt and retrieve values from getsockopt.

The problem is that BPF programs protected by RCU read lock cannot access the buffers located in user space. This is because these programs are non-sleepable and using copy_from_user() or copy_to_user() to access user space memory can result in paging.

The kernel makes a copy of the buffer specified by optval and optlen in kernel space before passing it to the cgroup {get,set}sockopt hooks. After the hooks are executed, the content of the buffer in kernel space is copied to user space if necessary.

Programs may send a significant amount of data, stored in buffer indicated by optval, to the kernel. One example is iptables, which can send several megabytes to the kernel. However, BPF programs on the hooks can only see up to the first PAGE_SIZE bytes of the buffer. The optlen value that BPF programs observe may appear to be PAGE_SIZE, but in reality, it is larger than that. On the other hand, the value of optlen represents the amount of data retrieved by getsockopt(). Additionally, both the buffer content and optlen can be modified by BPF programs.

Kernel may wrongly modify the value of optlen returned to user space to PAGE_SIZE. This can happen because the kernel cannot distinguish if the value was set by BPF programs or by the kernel itself.

To fix it, we perform various hacks; for example, the commit d8fe449a9c51 ("bpf: Don't return EINVAL from {get,set}sockopt when optlen > PAGE_SIZE") and the commit 29ebbba7d461 ("bpf: Don't EFAULT for {g,s}setsockopt with
wrong optlen").

Make CGroup {get,set}sockopt Hooks Sleepable
============================================

The long term solution is to make these hooks sleepable to enable BPF programs call copy_from_user() and copy_to_user(),
a.k.a. bpf_copy_from_user() and bpf_copy_to_user(). It prevents manipulation of optval and optlen values, and allows BPF programs to access the complete contents of the buffer referenced by optval.

Mix Sleepable and Non-Sleepable Programs
========================================

Installing both sleepable and non-sleepable programs simultaneously on the same hook leads to the mixing of sleepable and non-sleepable programs. For programs that cannot sleep, the kernel first copies data from the user buffer to a kernel buffer before invoking BPF programs. This process introduces intricate interactions between sleepable and non-sleepable programs.

For instance, due to kernel copies for non-sleepable programs, a sleepable program may receive optval in either the user space or the kernel space. These two scenarios require different handling approaches to update the buffer pointed to by optval. Consequently, sleepable programs can become significantly complex.

To simplify the programs, we introduce a set of kfuncs that enable data copying to optval without requiring knowledge of the underlying details. (bpf_so_optval_copy_to() and bpf_so_optval_copy_to_r())

---
v1: https://lore.kernel.org/bpf/[email protected]/

Kui-Feng Lee (6):
bpf: enable sleepable BPF programs attached to
cgroup/{get,set}sockopt.
bpf: Prevent BPF programs from access the buffer pointed by
user_optval.
bpf: rename bpf_copy_to_user().
bpf: Provide bpf_copy_from_user() and bpf_copy_to_user().
bpf: Add a new dynptr type for CGRUP_SOCKOPT.
bpf: Add test cases for sleepable BPF programs of the CGROUP_SOCKOPT
type.

 include/linux/bpf.h                           |   7 +-
 include/linux/filter.h                        |   1 +
 include/uapi/linux/bpf.h                      |  11 +
 kernel/bpf/btf.c                              |   3 +
 kernel/bpf/cgroup.c                           | 254 ++++++++++++++----
 kernel/bpf/helpers.c                          | 233 ++++++++++++++++
 kernel/bpf/syscall.c                          |   8 +-
 kernel/bpf/verifier.c                         | 120 ++++++---
 tools/include/uapi/linux/bpf.h                |  11 +
 tools/lib/bpf/libbpf.c                        |   2 +
 .../testing/selftests/bpf/bpf_experimental.h  |  44 +++
 tools/testing/selftests/bpf/bpf_kfuncs.h      |  47 ++++
 .../selftests/bpf/prog_tests/sockopt_sk.c     |  34 ++-
 .../testing/selftests/bpf/progs/sockopt_sk.c  | 242 +++++++++++++++++
 .../selftests/bpf/verifier/sleepable.c        |   2 +-
 15 files changed, 927 insertions(+), 92 deletions(-)
Signature · 1 line
2.34.1
#1 lore raw

RFC v2 1/6 bpf: enable sleepable BPF programs attached to cgroup/{get,set}sockopt.

From: Kui-Feng Lee <[email protected]>

Enable sleepable cgroup/{get,set}sockopt hooks.

The sleepable BPF programs attached to cgroup/{get,set}sockopt hooks may received a pointer to the optval in user space instead of a kernel copy. ctx->user_optval and ctx->user_optval_end are the pointers to the begin and end of the user space buffer if receiving a user space buffer. ctx->optval and ctx->optval_end will be a kernel copy if receiving a kernel space buffer.

A program receives a user space buffer if ctx->flags & BPF_SOCKOPT_FLAG_OPTVAL_USER is true, otherwise it receives a kernel space buffer. The BPF programs should not read/write from/to a user space buffer dirrectly. It should access the buffer through bpf_copy_from_user() and bpf_copy_to_user() provided in the following patches.

---
 include/linux/filter.h         |   1 +
 include/uapi/linux/bpf.h       |   7 +
 kernel/bpf/cgroup.c            | 229 ++++++++++++++++++++++++++-------
 kernel/bpf/verifier.c          |   7 +-
 tools/include/uapi/linux/bpf.h |   7 +
 tools/lib/bpf/libbpf.c         |   2 +
 6 files changed, 206 insertions(+), 47 deletions(-)
include/linux/filter.h +1 -0
index 761af6b3cf2b..54169a641d40 100644
@@ -1337,6 +1337,7 @@ struct bpf_sockopt_kern {
 	s32		level;
 	s32		optname;
 	s32		optlen;
+	u32		flags;
 	/* for retval in struct bpf_cg_run_ctx */
 	struct task_struct *current_task;
 	/* Temporary "register" for indirect stores to ppos. */
include/uapi/linux/bpf.h +7 -0
index 70da85200695..fff6f7dff408 100644
@@ -7138,6 +7138,13 @@ struct bpf_sockopt {
 	__s32	optname;
 	__s32	optlen;
 	__s32	retval;
+
+	__u32	flags;
+};
+
+enum bpf_sockopt_flags {
+	/* optval is a pointer to user space memory */
+	BPF_SOCKOPT_FLAG_OPTVAL_USER	= (1U << 0),
 };
 
 struct bpf_pidns_info {
kernel/bpf/cgroup.c +184 -45

383 lines not on this page · raw message

kernel/bpf/verifier.c +5 -2
index 132f25dab931..fbc0096693e7 100644
@@ -19538,9 +19538,11 @@ static bool can_be_sleepable(struct bpf_prog *prog)
 			return false;
 		}
 	}
+
 	return prog->type == BPF_PROG_TYPE_LSM ||
 	       prog->type == BPF_PROG_TYPE_KPROBE /* only for uprobes */ ||
-	       prog->type == BPF_PROG_TYPE_STRUCT_OPS;
+	       prog->type == BPF_PROG_TYPE_STRUCT_OPS ||
+	       prog->type == BPF_PROG_TYPE_CGROUP_SOCKOPT;
 }
 
 static int check_attach_btf_id(struct bpf_verifier_env *env)
@@ -19562,7 +19564,8 @@ static int check_attach_btf_id(struct bpf_verifier_env *env)
 	}
 
 	if (prog->aux->sleepable && !can_be_sleepable(prog)) {
-		verbose(env, "Only fentry/fexit/fmod_ret, lsm, iter, uprobe, and struct_ops programs can be sleepable\n");
+		verbose(env, "Only fentry/fexit/fmod_ret, lsm, iter, uprobe,"
+			"cgroup, and struct_ops programs can be sleepable\n");
 		return -EINVAL;
 	}
 
tools/include/uapi/linux/bpf.h +7 -0
index 70da85200695..fff6f7dff408 100644
@@ -7138,6 +7138,13 @@ struct bpf_sockopt {
 	__s32	optname;
 	__s32	optlen;
 	__s32	retval;
+
+	__u32	flags;
+};
+
+enum bpf_sockopt_flags {
+	/* optval is a pointer to user space memory */
+	BPF_SOCKOPT_FLAG_OPTVAL_USER	= (1U << 0),
 };
 
 struct bpf_pidns_info {
tools/lib/bpf/libbpf.c +2 -0
index 17883f5a44b9..3be9270bbc33 100644
@@ -8766,7 +8766,9 @@ static const struct bpf_sec_def section_defs[] = {
 	SEC_DEF("cgroup/getsockname6",	CGROUP_SOCK_ADDR, BPF_CGROUP_INET6_GETSOCKNAME, SEC_ATTACHABLE),
 	SEC_DEF("cgroup/sysctl",	CGROUP_SYSCTL, BPF_CGROUP_SYSCTL, SEC_ATTACHABLE),
 	SEC_DEF("cgroup/getsockopt",	CGROUP_SOCKOPT, BPF_CGROUP_GETSOCKOPT, SEC_ATTACHABLE),
+	SEC_DEF("cgroup/getsockopt.s",	CGROUP_SOCKOPT, BPF_CGROUP_GETSOCKOPT, SEC_ATTACHABLE | SEC_SLEEPABLE),
 	SEC_DEF("cgroup/setsockopt",	CGROUP_SOCKOPT, BPF_CGROUP_SETSOCKOPT, SEC_ATTACHABLE),
+	SEC_DEF("cgroup/setsockopt.s",	CGROUP_SOCKOPT, BPF_CGROUP_SETSOCKOPT, SEC_ATTACHABLE | SEC_SLEEPABLE),
 	SEC_DEF("cgroup/dev",		CGROUP_DEVICE, BPF_CGROUP_DEVICE, SEC_ATTACHABLE_OPT),
 	SEC_DEF("struct_ops+",		STRUCT_OPS, 0, SEC_NONE),
 	SEC_DEF("struct_ops.s+",	STRUCT_OPS, 0, SEC_SLEEPABLE),
Signature · 1 line
2.34.1
#2 lore raw

Re: [RFC bpf-next v2 1/6] bpf: enable sleepable BPF programs attached to cgroup/{get,set}sockopt.

Quoted 139 lines · On 08/10, [email protected] wrote:

From: Kui-Feng Lee <[email protected]>

Enable sleepable cgroup/{get,set}sockopt hooks.

The sleepable BPF programs attached to cgroup/{get,set}sockopt hooks may received a pointer to the optval in user space instead of a kernel copy. ctx->user_optval and ctx->user_optval_end are the pointers to the begin and end of the user space buffer if receiving a user space buffer. ctx->optval and ctx->optval_end will be a kernel copy if receiving a kernel space buffer.

A program receives a user space buffer if ctx->flags & BPF_SOCKOPT_FLAG_OPTVAL_USER is true, otherwise it receives a kernel space buffer. The BPF programs should not read/write from/to a user space buffer dirrectly. It should access the buffer through bpf_copy_from_user() and bpf_copy_to_user() provided in the following patches.

---
 include/linux/filter.h         |   1 +
 include/uapi/linux/bpf.h       |   7 +
 kernel/bpf/cgroup.c            | 229 ++++++++++++++++++++++++++-------
 kernel/bpf/verifier.c          |   7 +-
 tools/include/uapi/linux/bpf.h |   7 +
 tools/lib/bpf/libbpf.c         |   2 +
 6 files changed, 206 insertions(+), 47 deletions(-)
include/linux/filter.h +1 -0
index 761af6b3cf2b..54169a641d40 100644
@@ -1337,6 +1337,7 @@ struct bpf_sockopt_kern {
 	s32		level;
 	s32		optname;
 	s32		optlen;
+	u32		flags;
 	/* for retval in struct bpf_cg_run_ctx */
 	struct task_struct *current_task;
 	/* Temporary "register" for indirect stores to ppos. */
include/uapi/linux/bpf.h +7 -0
index 70da85200695..fff6f7dff408 100644
@@ -7138,6 +7138,13 @@ struct bpf_sockopt {
 	__s32	optname;
 	__s32	optlen;
 	__s32	retval;
+
+	__u32	flags;
+};
+
+enum bpf_sockopt_flags {
+	/* optval is a pointer to user space memory */
+	BPF_SOCKOPT_FLAG_OPTVAL_USER	= (1U << 0),
 };
 
 struct bpf_pidns_info {
kernel/bpf/cgroup.c +41 -6
index 5b2741aa0d9b..59489d9619a3 100644
@@ -28,25 +28,46 @@ EXPORT_SYMBOL(cgroup_bpf_enabled_key);
  * function pointer.
  */
 static __always_inline int
-bpf_prog_run_array_cg(const struct cgroup_bpf *cgrp,
-		      enum cgroup_bpf_attach_type atype,
-		      const void *ctx, bpf_prog_run_fn run_prog,
-		      int retval, u32 *ret_flags)
+bpf_prog_run_array_cg_cb(const struct cgroup_bpf *cgrp,
+			 enum cgroup_bpf_attach_type atype,
+			 const void *ctx, bpf_prog_run_fn run_prog,
+			 int retval, u32 *ret_flags,
+			 int (*progs_cb)(void *, const struct bpf_prog_array_item *),
+			 void *progs_cb_arg)
 {
 	const struct bpf_prog_array_item *item;
 	const struct bpf_prog *prog;
 	const struct bpf_prog_array *array;
 	struct bpf_run_ctx *old_run_ctx;
 	struct bpf_cg_run_ctx run_ctx;
+	bool do_sleepable;
 	u32 func_ret;
+	int err;
+
+	do_sleepable =
+		atype == CGROUP_SETSOCKOPT || atype == CGROUP_GETSOCKOPT;
 
 	run_ctx.retval = retval;
 	migrate_disable();
-	rcu_read_lock();
+	if (do_sleepable) {
+		might_fault();
+		rcu_read_lock_trace();
+	} else
+		rcu_read_lock();
 	array = rcu_dereference(cgrp->effective[atype]);
 	item = &array->items[0];
+
+	if (progs_cb) {
+		err = progs_cb(progs_cb_arg, item);
+		if (err)
+			return err;
+	}
+
 	old_run_ctx = bpf_set_run_ctx(&run_ctx.run_ctx);
 	while ((prog = READ_ONCE(item->prog))) {
+		if (do_sleepable && !prog->aux->sleepable)
+			rcu_read_lock();
+
 		run_ctx.prog_item = item;
 		func_ret = run_prog(prog, ctx);
 		if (ret_flags) {
@@ -56,13 +77,48 @@ bpf_prog_run_array_cg(const struct cgroup_bpf *cgrp,
 		if (!func_ret && !IS_ERR_VALUE((long)run_ctx.retval))
 			run_ctx.retval = -EPERM;
 		item++;
+
+		if (do_sleepable && !prog->aux->sleepable)
+			rcu_read_unlock();
 	}
 	bpf_reset_run_ctx(old_run_ctx);
-	rcu_read_unlock();
+	if (do_sleepable)
+		rcu_read_unlock_trace();
+	else
+		rcu_read_unlock();
 	migrate_enable();
 	return run_ctx.retval;
 }
 
+static __always_inline void
+count_sleepable_prog_cg(const struct bpf_prog_array_item *item,
+			int *sleepable, int *non_sleepable)
+{
+	const struct bpf_prog *prog;
+	bool ret = true;
+
+	*sleepable = 0;

kernel/bpf/cgroup.c bpf_prog_run_array_cg(const struct cgroup_bpf *cgrp, · line 110

Quoted 10 lines
kernel/bpf/cgroup.c +10 -0
+	*non_sleepable = 0;
+
+	while (ret && (prog = READ_ONCE(item->prog))) {
+		if (prog->aux->sleepable)
+			(*sleepable)++;
+		else
+			(*non_sleepable)++;
+		item++;
+	}
+}

Can we maintain this info in the slow attach/detach path? Running the loop every time we get a sockopt syscall another time seems like something we can avoid? (even though it's a slow path, seems like we can do better?)

Quoted 320 lines
kernel/bpf/cgroup.c +133 -39
+
+static __always_inline int
+bpf_prog_run_array_cg(const struct cgroup_bpf *cgrp,
+		      enum cgroup_bpf_attach_type atype,
+		      const void *ctx, bpf_prog_run_fn run_prog,
+		      int retval, u32 *ret_flags)
+{
+	return bpf_prog_run_array_cg_cb(cgrp, atype, ctx, run_prog, retval,
+					ret_flags, NULL, NULL);
+}
+
 unsigned int __cgroup_bpf_run_lsm_sock(const void *ctx,
 				       const struct bpf_insn *insn)
 {
@@ -307,7 +363,7 @@ static void cgroup_bpf_release(struct work_struct *work)
 		old_array = rcu_dereference_protected(
 				cgrp->bpf.effective[atype],
 				lockdep_is_held(&cgroup_mutex));
-		bpf_prog_array_free(old_array);
+		bpf_prog_array_free_sleepable(old_array);
 	}
 
 	list_for_each_entry_safe(storage, stmp, storages, list_cg) {
@@ -451,7 +507,7 @@ static void activate_effective_progs(struct cgroup *cgrp,
 	/* free prog array after grace period, since __cgroup_bpf_run_*()
 	 * might be still walking the array
 	 */
-	bpf_prog_array_free(old_array);
+	bpf_prog_array_free_sleepable(old_array);
 }
 
 /**
@@ -491,7 +547,7 @@ int cgroup_bpf_inherit(struct cgroup *cgrp)
 	return 0;
 cleanup:
 	for (i = 0; i < NR; i++)
-		bpf_prog_array_free(arrays[i]);
+		bpf_prog_array_free_sleepable(arrays[i]);
 
 	for (p = cgroup_parent(cgrp); p; p = cgroup_parent(p))
 		cgroup_bpf_put(p);
@@ -525,7 +581,7 @@ static int update_effective_progs(struct cgroup *cgrp,
 
 		if (percpu_ref_is_zero(&desc->bpf.refcnt)) {
 			if (unlikely(desc->bpf.inactive)) {
-				bpf_prog_array_free(desc->bpf.inactive);
+				bpf_prog_array_free_sleepable(desc->bpf.inactive);
 				desc->bpf.inactive = NULL;
 			}
 			continue;
@@ -544,7 +600,7 @@ static int update_effective_progs(struct cgroup *cgrp,
 	css_for_each_descendant_pre(css, &cgrp->self) {
 		struct cgroup *desc = container_of(css, struct cgroup, self);
 
-		bpf_prog_array_free(desc->bpf.inactive);
+		bpf_prog_array_free_sleepable(desc->bpf.inactive);
 		desc->bpf.inactive = NULL;
 	}
 
@@ -1740,7 +1796,7 @@ int __cgroup_bpf_run_filter_sysctl(struct ctl_table_header *head,
 
 #ifdef CONFIG_NET
 static int sockopt_alloc_buf(struct bpf_sockopt_kern *ctx, int max_optlen,
-			     struct bpf_sockopt_buf *buf)
+			     struct bpf_sockopt_buf *buf, bool force_alloc)
 {
 	if (unlikely(max_optlen < 0))
 		return -EINVAL;
@@ -1752,7 +1808,7 @@ static int sockopt_alloc_buf(struct bpf_sockopt_kern *ctx, int max_optlen,
 		max_optlen = PAGE_SIZE;
 	}
 
-	if (max_optlen <= sizeof(buf->data)) {
+	if (max_optlen <= sizeof(buf->data) && !force_alloc) {
 		/* When the optval fits into BPF_SOCKOPT_KERN_BUF_SIZE
 		 * bytes avoid the cost of kzalloc.
 		 */
@@ -1773,7 +1829,8 @@ static int sockopt_alloc_buf(struct bpf_sockopt_kern *ctx, int max_optlen,
 static void sockopt_free_buf(struct bpf_sockopt_kern *ctx,
 			     struct bpf_sockopt_buf *buf)
 {
-	if (ctx->optval == buf->data)
+	if (ctx->optval == buf->data ||
+	    ctx->flags & BPF_SOCKOPT_FLAG_OPTVAL_USER)
 		return;
 	kfree(ctx->optval);
 }
@@ -1781,7 +1838,50 @@ static void sockopt_free_buf(struct bpf_sockopt_kern *ctx,
 static bool sockopt_buf_allocated(struct bpf_sockopt_kern *ctx,
 				  struct bpf_sockopt_buf *buf)
 {
-	return ctx->optval != buf->data;
+	return ctx->optval != buf->data &&
+		!(ctx->flags & BPF_SOCKOPT_FLAG_OPTVAL_USER);
+}
+
+struct filter_sockopt_cb_args {
+	struct bpf_sockopt_kern *ctx;
+	struct bpf_sockopt_buf *buf;
+	int max_optlen;
+};
+
+static int filter_setsockopt_progs_cb(void *arg,
+				      const struct bpf_prog_array_item *item)
+{
+	struct filter_sockopt_cb_args *cb_args = arg;
+	struct bpf_sockopt_kern *ctx = cb_args->ctx;
+	char *optval = ctx->optval;
+	int sleepable_cnt, non_sleepable_cnt;
+	int max_optlen;
+
+	count_sleepable_prog_cg(item, &sleepable_cnt, &non_sleepable_cnt);
+
+	if (!non_sleepable_cnt)
+		return 0;
+
+	/* Allocate a bit more than the initial user buffer for
+	 * BPF program. The canonical use case is overriding
+	 * TCP_CONGESTION(nv) to TCP_CONGESTION(cubic).
+	 */
+	max_optlen = max_t(int, 16, ctx->optlen);
+	/* We need to force allocating from heap if there are sleepable
+	 * programs since they may created dynptrs from ctx->optval. In
+	 * this case, dynptrs will try to free the buffer that is actually
+	 * on the stack without this flag.
+	 */
+	max_optlen = sockopt_alloc_buf(ctx, max_optlen, cb_args->buf,
+				       !!sleepable_cnt);
+	if (max_optlen < 0)
+		return max_optlen;
+
+	if (copy_from_user(ctx->optval, optval,
+			   min(ctx->optlen, max_optlen)) != 0)
+		return -EFAULT;
+
+	return 0;
 }
 
 int __cgroup_bpf_run_filter_setsockopt(struct sock *sk, int *level,
@@ -1795,27 +1895,22 @@ int __cgroup_bpf_run_filter_setsockopt(struct sock *sk, int *level,
 		.level = *level,
 		.optname = *optname,
 	};
+	struct filter_sockopt_cb_args cb_args = {
+		.ctx = &ctx,
+		.buf = &buf,
+	};
 	int ret, max_optlen;
 
-	/* Allocate a bit more than the initial user buffer for
-	 * BPF program. The canonical use case is overriding
-	 * TCP_CONGESTION(nv) to TCP_CONGESTION(cubic).
-	 */
-	max_optlen = max_t(int, 16, *optlen);
-	max_optlen = sockopt_alloc_buf(&ctx, max_optlen, &buf);
-	if (max_optlen < 0)
-		return max_optlen;
-
+	max_optlen = *optlen;
 	ctx.optlen = *optlen;
-
-	if (copy_from_user(ctx.optval, optval, min(*optlen, max_optlen)) != 0) {
-		ret = -EFAULT;
-		goto out;
-	}
+	ctx.optval = optval;
+	ctx.optval_end = optval + *optlen;
+	ctx.flags = BPF_SOCKOPT_FLAG_OPTVAL_USER;
 
 	lock_sock(sk);
-	ret = bpf_prog_run_array_cg(&cgrp->bpf, CGROUP_SETSOCKOPT,
-				    &ctx, bpf_prog_run, 0, NULL);
+	ret = bpf_prog_run_array_cg_cb(&cgrp->bpf, CGROUP_SETSOCKOPT,
+				       &ctx, bpf_prog_run, 0, NULL,
+				       filter_setsockopt_progs_cb, &cb_args);
 	release_sock(sk);
 
 	if (ret)
@@ -1824,7 +1919,8 @@ int __cgroup_bpf_run_filter_setsockopt(struct sock *sk, int *level,
 	if (ctx.optlen == -1) {
 		/* optlen set to -1, bypass kernel */
 		ret = 1;
-	} else if (ctx.optlen > max_optlen || ctx.optlen < -1) {
+	} else if (ctx.optlen > (ctx.optval_end - ctx.optval) ||
+		   ctx.optlen < -1) {
 		/* optlen is out of bounds */
 		if (*optlen > PAGE_SIZE && ctx.optlen >= 0) {
 			pr_info_once("bpf setsockopt: ignoring program buffer with optlen=%d (max_optlen=%d)\n",
@@ -1846,6 +1942,8 @@ int __cgroup_bpf_run_filter_setsockopt(struct sock *sk, int *level,
 		 */
 		if (ctx.optlen != 0) {
 			*optlen = ctx.optlen;
+			if (ctx.flags & BPF_SOCKOPT_FLAG_OPTVAL_USER)
+				return 0;
 			/* We've used bpf_sockopt_kern->buf as an intermediary
 			 * storage, but the BPF program indicates that we need
 			 * to pass this data to the kernel setsockopt handler.
@@ -1874,6 +1972,36 @@ int __cgroup_bpf_run_filter_setsockopt(struct sock *sk, int *level,
 	return ret;
 }
 
+static int filter_getsockopt_progs_cb(void *arg,
+				      const struct bpf_prog_array_item *item)
+{
+	struct filter_sockopt_cb_args *cb_args = arg;
+	struct bpf_sockopt_kern *ctx = cb_args->ctx;
+	int sleepable_cnt, non_sleepable_cnt;
+	int max_optlen;
+	char *optval;
+
+	count_sleepable_prog_cg(item, &sleepable_cnt, &non_sleepable_cnt);
+
+	if (!non_sleepable_cnt)
+		return 0;
+
+	optval = ctx->optval;
+	max_optlen = sockopt_alloc_buf(ctx, cb_args->max_optlen,
+				       cb_args->buf, false);
+	if (max_optlen < 0)
+		return max_optlen;
+
+	if (copy_from_user(ctx->optval, optval,
+			   min(ctx->optlen, max_optlen)) != 0)
+		return -EFAULT;
+
+	ctx->flags = 0;
+	cb_args->max_optlen = max_optlen;
+
+	return 0;
+}
+
 int __cgroup_bpf_run_filter_getsockopt(struct sock *sk, int level,
 				       int optname, char __user *optval,
 				       int __user *optlen, int max_optlen,
@@ -1887,15 +2015,16 @@ int __cgroup_bpf_run_filter_getsockopt(struct sock *sk, int level,
 		.optname = optname,
 		.current_task = current,
 	};
+	struct filter_sockopt_cb_args cb_args = {
+		.ctx = &ctx,
+		.buf = &buf,
+		.max_optlen = max_optlen,
+	};
 	int orig_optlen;
 	int ret;
 
 	orig_optlen = max_optlen;
 	ctx.optlen = max_optlen;
-	max_optlen = sockopt_alloc_buf(&ctx, max_optlen, &buf);
-	if (max_optlen < 0)
-		return max_optlen;
-
 	if (!retval) {
 		/* If kernel getsockopt finished successfully,
 		 * copy whatever was returned to the user back
@@ -1914,18 +2043,19 @@ int __cgroup_bpf_run_filter_getsockopt(struct sock *sk, int level,
 			goto out;
 		}
 		orig_optlen = ctx.optlen;
-
-		if (copy_from_user(ctx.optval, optval,
-				   min(ctx.optlen, max_optlen)) != 0) {
-			ret = -EFAULT;
-			goto out;
-		}
 	}
 
+	ctx.optval = optval;
+	ctx.optval_end = optval + max_optlen;
+	ctx.flags = BPF_SOCKOPT_FLAG_OPTVAL_USER;
+
 	lock_sock(sk);
-	ret = bpf_prog_run_array_cg(&cgrp->bpf, CGROUP_GETSOCKOPT,
-				    &ctx, bpf_prog_run, retval, NULL);
+	ret = bpf_prog_run_array_cg_cb(&cgrp->bpf, CGROUP_GETSOCKOPT,
+				       &ctx, bpf_prog_run, retval, NULL,
+				       filter_getsockopt_progs_cb,
+				       &cb_args);
 	release_sock(sk);
+	max_optlen = cb_args.max_optlen;
 
 	if (ret < 0)
 		goto out;
@@ -1942,7 +2072,9 @@ int __cgroup_bpf_run_filter_getsockopt(struct sock *sk, int level,
 	}
 
 	if (ctx.optlen != 0) {
-		if (optval && copy_to_user(optval, ctx.optval, ctx.optlen)) {
+		if (optval &&
+		    !(ctx.flags & BPF_SOCKOPT_FLAG_OPTVAL_USER) &&
+		    copy_to_user(optval, ctx.optval, ctx.optlen)) {
 			ret = -EFAULT;
 			goto out;
 		}
@@ -2388,6 +2520,10 @@ static bool cg_sockopt_is_valid_access(int off, int size,
 		if (size != size_default)
 			return false;
 		return prog->expected_attach_type == BPF_CGROUP_GETSOCKOPT;
+	case offsetof(struct bpf_sockopt, flags):
+		if (size != sizeof(__u32))
+			return false;
+		break;
 	default:
 		if (size != size_default)
 			return false;
@@ -2481,6 +2617,9 @@ static u32 cg_sockopt_convert_ctx_access(enum bpf_access_type type,
 	case offsetof(struct bpf_sockopt, optval_end):
 		*insn++ = CG_SOCKOPT_READ_FIELD(optval_end);
 		break;
+	case offsetof(struct bpf_sockopt, flags):
+		*insn++ = CG_SOCKOPT_READ_FIELD(flags);
+		break;
 	}
 
 	return insn - insn_buf;
kernel/bpf/verifier.c +0 -0
index 132f25dab931..fbc0096693e7 100644

kernel/bpf/verifier.c static bool can_be_sleepable(struct bpf_prog *prog) · line 19541

Quoted 4 lines
kernel/bpf/verifier.c +1 -0
@@ -19538,9 +19538,11 @@ static bool can_be_sleepable(struct bpf_prog *prog)
 			return false;
 		}
 	}
+

Extra newline?

Quoted 9 lines
kernel/bpf/verifier.c +2 -1
 	return prog->type == BPF_PROG_TYPE_LSM ||
 	       prog->type == BPF_PROG_TYPE_KPROBE /* only for uprobes */ ||
-	       prog->type == BPF_PROG_TYPE_STRUCT_OPS;
+	       prog->type == BPF_PROG_TYPE_STRUCT_OPS ||
+	       prog->type == BPF_PROG_TYPE_CGROUP_SOCKOPT;
 }
 
 static int check_attach_btf_id(struct bpf_verifier_env *env)

kernel/bpf/verifier.c static int check_attach_btf_id(struct bpf_verifier_env *env) · line 19568

Quoted 6 lines
kernel/bpf/verifier.c +2 -1
@@ -19562,7 +19564,8 @@ static int check_attach_btf_id(struct bpf_verifier_env *env)
 	}
 
 	if (prog->aux->sleepable && !can_be_sleepable(prog)) {
-		verbose(env, "Only fentry/fexit/fmod_ret, lsm, iter, uprobe, and struct_ops programs can be sleepable\n");
+		verbose(env, "Only fentry/fexit/fmod_ret, lsm, iter, uprobe,"
+			"cgroup, and struct_ops programs can be sleepable\n");

I don't think we split the lines even if they are too long. Makes them ungreppable :-(

Quoted 9 lines
kernel/bpf/verifier.c +0 -0
 		return -EINVAL;
 	}
 
tools/include/uapi/linux/bpf.h +0 -0
index 70da85200695..fff6f7dff408 100644
@@ -7138,6 +7138,13 @@ struct bpf_sockopt {
 	__s32	optname;

tools/include/uapi/linux/bpf.h struct bpf_sockopt { · line 7148

Quoted 10 lines
tools/include/uapi/linux/bpf.h +7 -0
 	__s32	optlen;
 	__s32	retval;
+
+	__u32	flags;
+};
+
+enum bpf_sockopt_flags {
+	/* optval is a pointer to user space memory */
+	BPF_SOCKOPT_FLAG_OPTVAL_USER	= (1U << 0),
 };

Why do we need to export the flag? Do we still want the users to do something based on the presence/absence of BPF_SOCKOPT_FLAG_OPTVAL_USER?

#3 lore raw

Re: [RFC bpf-next v2 1/6] bpf: enable sleepable BPF programs attached to cgroup/{get,set}sockopt.

Quoted 147 lines · Stanislav Fomichev
Quoted 146 lines · On 08/10, [email protected] wrote:

From: Kui-Feng Lee <[email protected]>

Enable sleepable cgroup/{get,set}sockopt hooks.

The sleepable BPF programs attached to cgroup/{get,set}sockopt hooks may received a pointer to the optval in user space instead of a kernel copy. ctx->user_optval and ctx->user_optval_end are the pointers to the begin and end of the user space buffer if receiving a user space buffer. ctx->optval and ctx->optval_end will be a kernel copy if receiving a kernel space buffer.

A program receives a user space buffer if ctx->flags & BPF_SOCKOPT_FLAG_OPTVAL_USER is true, otherwise it receives a kernel space buffer. The BPF programs should not read/write from/to a user space buffer dirrectly. It should access the buffer through bpf_copy_from_user() and bpf_copy_to_user() provided in the following patches.

---
  include/linux/filter.h         |   1 +
  include/uapi/linux/bpf.h       |   7 +
  kernel/bpf/cgroup.c            | 229 ++++++++++++++++++++++++++-------
  kernel/bpf/verifier.c          |   7 +-
  tools/include/uapi/linux/bpf.h |   7 +
  tools/lib/bpf/libbpf.c         |   2 +
  6 files changed, 206 insertions(+), 47 deletions(-)
include/linux/filter.h +1 -0
index 761af6b3cf2b..54169a641d40 100644
@@ -1337,6 +1337,7 @@ struct bpf_sockopt_kern {
  	s32		level;
  	s32		optname;
  	s32		optlen;
+	u32		flags;
  	/* for retval in struct bpf_cg_run_ctx */
  	struct task_struct *current_task;
  	/* Temporary "register" for indirect stores to ppos. */
include/uapi/linux/bpf.h +7 -0
index 70da85200695..fff6f7dff408 100644
@@ -7138,6 +7138,13 @@ struct bpf_sockopt {
  	__s32	optname;
  	__s32	optlen;
  	__s32	retval;
+
+	__u32	flags;
+};
+
+enum bpf_sockopt_flags {
+	/* optval is a pointer to user space memory */
+	BPF_SOCKOPT_FLAG_OPTVAL_USER	= (1U << 0),
  };
 
  struct bpf_pidns_info {
kernel/bpf/cgroup.c +48 -6
index 5b2741aa0d9b..59489d9619a3 100644
@@ -28,25 +28,46 @@ EXPORT_SYMBOL(cgroup_bpf_enabled_key);
   * function pointer.
   */
  static __always_inline int
-bpf_prog_run_array_cg(const struct cgroup_bpf *cgrp,
-		      enum cgroup_bpf_attach_type atype,
-		      const void *ctx, bpf_prog_run_fn run_prog,
-		      int retval, u32 *ret_flags)
+bpf_prog_run_array_cg_cb(const struct cgroup_bpf *cgrp,
+			 enum cgroup_bpf_attach_type atype,
+			 const void *ctx, bpf_prog_run_fn run_prog,
+			 int retval, u32 *ret_flags,
+			 int (*progs_cb)(void *, const struct bpf_prog_array_item *),
+			 void *progs_cb_arg)
  {
  	const struct bpf_prog_array_item *item;
  	const struct bpf_prog *prog;
  	const struct bpf_prog_array *array;
  	struct bpf_run_ctx *old_run_ctx;
  	struct bpf_cg_run_ctx run_ctx;
+	bool do_sleepable;
  	u32 func_ret;
+	int err;
+
+	do_sleepable =
+		atype == CGROUP_SETSOCKOPT || atype == CGROUP_GETSOCKOPT;
 
  	run_ctx.retval = retval;
  	migrate_disable();
-	rcu_read_lock();
+	if (do_sleepable) {
+		might_fault();
+		rcu_read_lock_trace();
+	} else
+		rcu_read_lock();
  	array = rcu_dereference(cgrp->effective[atype]);
  	item = &array->items[0];
+
+	if (progs_cb) {
+		err = progs_cb(progs_cb_arg, item);
+		if (err)
+			return err;
+	}
+
  	old_run_ctx = bpf_set_run_ctx(&run_ctx.run_ctx);
  	while ((prog = READ_ONCE(item->prog))) {
+		if (do_sleepable && !prog->aux->sleepable)
+			rcu_read_lock();
+
  		run_ctx.prog_item = item;
  		func_ret = run_prog(prog, ctx);
  		if (ret_flags) {
@@ -56,13 +77,48 @@ bpf_prog_run_array_cg(const struct cgroup_bpf *cgrp,
  		if (!func_ret && !IS_ERR_VALUE((long)run_ctx.retval))
  			run_ctx.retval = -EPERM;
  		item++;
+
+		if (do_sleepable && !prog->aux->sleepable)
+			rcu_read_unlock();
  	}
  	bpf_reset_run_ctx(old_run_ctx);
-	rcu_read_unlock();
+	if (do_sleepable)
+		rcu_read_unlock_trace();
+	else
+		rcu_read_unlock();
  	migrate_enable();
  	return run_ctx.retval;
  }
 
+static __always_inline void
+count_sleepable_prog_cg(const struct bpf_prog_array_item *item,
+			int *sleepable, int *non_sleepable)
+{
+	const struct bpf_prog *prog;
+	bool ret = true;
+
+	*sleepable = 0;
+	*non_sleepable = 0;
+
+	while (ret && (prog = READ_ONCE(item->prog))) {
+		if (prog->aux->sleepable)
+			(*sleepable)++;
+		else
+			(*non_sleepable)++;
Quoted 8 lines

kernel/bpf/cgroup.c bpf_prog_run_array_cg(const struct cgroup_bpf *cgrp, · line 110

Quoted 3 lines
kernel/bpf/cgroup.c +3 -0
+		item++;
+	}
+}

Can we maintain this info in the slow attach/detach path? Running the loop every time we get a sockopt syscall another time seems like something we can avoid? (even though it's a slow path, seems like we can do better?)

I can move this function into compute_effective_progs().

Quoted 370 lines
Quoted 321 lines
kernel/bpf/cgroup.c +133 -39
+
+static __always_inline int
+bpf_prog_run_array_cg(const struct cgroup_bpf *cgrp,
+		      enum cgroup_bpf_attach_type atype,
+		      const void *ctx, bpf_prog_run_fn run_prog,
+		      int retval, u32 *ret_flags)
+{
+	return bpf_prog_run_array_cg_cb(cgrp, atype, ctx, run_prog, retval,
+					ret_flags, NULL, NULL);
+}
+
  unsigned int __cgroup_bpf_run_lsm_sock(const void *ctx,
  				       const struct bpf_insn *insn)
  {
@@ -307,7 +363,7 @@ static void cgroup_bpf_release(struct work_struct *work)
  		old_array = rcu_dereference_protected(
  				cgrp->bpf.effective[atype],
  				lockdep_is_held(&cgroup_mutex));
-		bpf_prog_array_free(old_array);
+		bpf_prog_array_free_sleepable(old_array);
  	}
 
  	list_for_each_entry_safe(storage, stmp, storages, list_cg) {
@@ -451,7 +507,7 @@ static void activate_effective_progs(struct cgroup *cgrp,
  	/* free prog array after grace period, since __cgroup_bpf_run_*()
  	 * might be still walking the array
  	 */
-	bpf_prog_array_free(old_array);
+	bpf_prog_array_free_sleepable(old_array);
  }
 
  /**
@@ -491,7 +547,7 @@ int cgroup_bpf_inherit(struct cgroup *cgrp)
  	return 0;
  cleanup:
  	for (i = 0; i < NR; i++)
-		bpf_prog_array_free(arrays[i]);
+		bpf_prog_array_free_sleepable(arrays[i]);
 
  	for (p = cgroup_parent(cgrp); p; p = cgroup_parent(p))
  		cgroup_bpf_put(p);
@@ -525,7 +581,7 @@ static int update_effective_progs(struct cgroup *cgrp,
 
  		if (percpu_ref_is_zero(&desc->bpf.refcnt)) {
  			if (unlikely(desc->bpf.inactive)) {
-				bpf_prog_array_free(desc->bpf.inactive);
+				bpf_prog_array_free_sleepable(desc->bpf.inactive);
  				desc->bpf.inactive = NULL;
  			}
  			continue;
@@ -544,7 +600,7 @@ static int update_effective_progs(struct cgroup *cgrp,
  	css_for_each_descendant_pre(css, &cgrp->self) {
  		struct cgroup *desc = container_of(css, struct cgroup, self);
 
-		bpf_prog_array_free(desc->bpf.inactive);
+		bpf_prog_array_free_sleepable(desc->bpf.inactive);
  		desc->bpf.inactive = NULL;
  	}
 
@@ -1740,7 +1796,7 @@ int __cgroup_bpf_run_filter_sysctl(struct ctl_table_header *head,
 
  #ifdef CONFIG_NET
  static int sockopt_alloc_buf(struct bpf_sockopt_kern *ctx, int max_optlen,
-			     struct bpf_sockopt_buf *buf)
+			     struct bpf_sockopt_buf *buf, bool force_alloc)
  {
  	if (unlikely(max_optlen < 0))
  		return -EINVAL;
@@ -1752,7 +1808,7 @@ static int sockopt_alloc_buf(struct bpf_sockopt_kern *ctx, int max_optlen,
  		max_optlen = PAGE_SIZE;
  	}
 
-	if (max_optlen <= sizeof(buf->data)) {
+	if (max_optlen <= sizeof(buf->data) && !force_alloc) {
  		/* When the optval fits into BPF_SOCKOPT_KERN_BUF_SIZE
  		 * bytes avoid the cost of kzalloc.
  		 */
@@ -1773,7 +1829,8 @@ static int sockopt_alloc_buf(struct bpf_sockopt_kern *ctx, int max_optlen,
  static void sockopt_free_buf(struct bpf_sockopt_kern *ctx,
  			     struct bpf_sockopt_buf *buf)
  {
-	if (ctx->optval == buf->data)
+	if (ctx->optval == buf->data ||
+	    ctx->flags & BPF_SOCKOPT_FLAG_OPTVAL_USER)
  		return;
  	kfree(ctx->optval);
  }
@@ -1781,7 +1838,50 @@ static void sockopt_free_buf(struct bpf_sockopt_kern *ctx,
  static bool sockopt_buf_allocated(struct bpf_sockopt_kern *ctx,
  				  struct bpf_sockopt_buf *buf)
  {
-	return ctx->optval != buf->data;
+	return ctx->optval != buf->data &&
+		!(ctx->flags & BPF_SOCKOPT_FLAG_OPTVAL_USER);
+}
+
+struct filter_sockopt_cb_args {
+	struct bpf_sockopt_kern *ctx;
+	struct bpf_sockopt_buf *buf;
+	int max_optlen;
+};
+
+static int filter_setsockopt_progs_cb(void *arg,
+				      const struct bpf_prog_array_item *item)
+{
+	struct filter_sockopt_cb_args *cb_args = arg;
+	struct bpf_sockopt_kern *ctx = cb_args->ctx;
+	char *optval = ctx->optval;
+	int sleepable_cnt, non_sleepable_cnt;
+	int max_optlen;
+
+	count_sleepable_prog_cg(item, &sleepable_cnt, &non_sleepable_cnt);
+
+	if (!non_sleepable_cnt)
+		return 0;
+
+	/* Allocate a bit more than the initial user buffer for
+	 * BPF program. The canonical use case is overriding
+	 * TCP_CONGESTION(nv) to TCP_CONGESTION(cubic).
+	 */
+	max_optlen = max_t(int, 16, ctx->optlen);
+	/* We need to force allocating from heap if there are sleepable
+	 * programs since they may created dynptrs from ctx->optval. In
+	 * this case, dynptrs will try to free the buffer that is actually
+	 * on the stack without this flag.
+	 */
+	max_optlen = sockopt_alloc_buf(ctx, max_optlen, cb_args->buf,
+				       !!sleepable_cnt);
+	if (max_optlen < 0)
+		return max_optlen;
+
+	if (copy_from_user(ctx->optval, optval,
+			   min(ctx->optlen, max_optlen)) != 0)
+		return -EFAULT;
+
+	return 0;
  }
 
  int __cgroup_bpf_run_filter_setsockopt(struct sock *sk, int *level,
@@ -1795,27 +1895,22 @@ int __cgroup_bpf_run_filter_setsockopt(struct sock *sk, int *level,
  		.level = *level,
  		.optname = *optname,
  	};
+	struct filter_sockopt_cb_args cb_args = {
+		.ctx = &ctx,
+		.buf = &buf,
+	};
  	int ret, max_optlen;
 
-	/* Allocate a bit more than the initial user buffer for
-	 * BPF program. The canonical use case is overriding
-	 * TCP_CONGESTION(nv) to TCP_CONGESTION(cubic).
-	 */
-	max_optlen = max_t(int, 16, *optlen);
-	max_optlen = sockopt_alloc_buf(&ctx, max_optlen, &buf);
-	if (max_optlen < 0)
-		return max_optlen;
-
+	max_optlen = *optlen;
  	ctx.optlen = *optlen;
-
-	if (copy_from_user(ctx.optval, optval, min(*optlen, max_optlen)) != 0) {
-		ret = -EFAULT;
-		goto out;
-	}
+	ctx.optval = optval;
+	ctx.optval_end = optval + *optlen;
+	ctx.flags = BPF_SOCKOPT_FLAG_OPTVAL_USER;
 
  	lock_sock(sk);
-	ret = bpf_prog_run_array_cg(&cgrp->bpf, CGROUP_SETSOCKOPT,
-				    &ctx, bpf_prog_run, 0, NULL);
+	ret = bpf_prog_run_array_cg_cb(&cgrp->bpf, CGROUP_SETSOCKOPT,
+				       &ctx, bpf_prog_run, 0, NULL,
+				       filter_setsockopt_progs_cb, &cb_args);
  	release_sock(sk);
 
  	if (ret)
@@ -1824,7 +1919,8 @@ int __cgroup_bpf_run_filter_setsockopt(struct sock *sk, int *level,
  	if (ctx.optlen == -1) {
  		/* optlen set to -1, bypass kernel */
  		ret = 1;
-	} else if (ctx.optlen > max_optlen || ctx.optlen < -1) {
+	} else if (ctx.optlen > (ctx.optval_end - ctx.optval) ||
+		   ctx.optlen < -1) {
  		/* optlen is out of bounds */
  		if (*optlen > PAGE_SIZE && ctx.optlen >= 0) {
  			pr_info_once("bpf setsockopt: ignoring program buffer with optlen=%d (max_optlen=%d)\n",
@@ -1846,6 +1942,8 @@ int __cgroup_bpf_run_filter_setsockopt(struct sock *sk, int *level,
  		 */
  		if (ctx.optlen != 0) {
  			*optlen = ctx.optlen;
+			if (ctx.flags & BPF_SOCKOPT_FLAG_OPTVAL_USER)
+				return 0;
  			/* We've used bpf_sockopt_kern->buf as an intermediary
  			 * storage, but the BPF program indicates that we need
  			 * to pass this data to the kernel setsockopt handler.
@@ -1874,6 +1972,36 @@ int __cgroup_bpf_run_filter_setsockopt(struct sock *sk, int *level,
  	return ret;
  }
 
+static int filter_getsockopt_progs_cb(void *arg,
+				      const struct bpf_prog_array_item *item)
+{
+	struct filter_sockopt_cb_args *cb_args = arg;
+	struct bpf_sockopt_kern *ctx = cb_args->ctx;
+	int sleepable_cnt, non_sleepable_cnt;
+	int max_optlen;
+	char *optval;
+
+	count_sleepable_prog_cg(item, &sleepable_cnt, &non_sleepable_cnt);
+
+	if (!non_sleepable_cnt)
+		return 0;
+
+	optval = ctx->optval;
+	max_optlen = sockopt_alloc_buf(ctx, cb_args->max_optlen,
+				       cb_args->buf, false);
+	if (max_optlen < 0)
+		return max_optlen;
+
+	if (copy_from_user(ctx->optval, optval,
+			   min(ctx->optlen, max_optlen)) != 0)
+		return -EFAULT;
+
+	ctx->flags = 0;
+	cb_args->max_optlen = max_optlen;
+
+	return 0;
+}
+
  int __cgroup_bpf_run_filter_getsockopt(struct sock *sk, int level,
  				       int optname, char __user *optval,
  				       int __user *optlen, int max_optlen,
@@ -1887,15 +2015,16 @@ int __cgroup_bpf_run_filter_getsockopt(struct sock *sk, int level,
  		.optname = optname,
  		.current_task = current,
  	};
+	struct filter_sockopt_cb_args cb_args = {
+		.ctx = &ctx,
+		.buf = &buf,
+		.max_optlen = max_optlen,
+	};
  	int orig_optlen;
  	int ret;
 
  	orig_optlen = max_optlen;
  	ctx.optlen = max_optlen;
-	max_optlen = sockopt_alloc_buf(&ctx, max_optlen, &buf);
-	if (max_optlen < 0)
-		return max_optlen;
-
  	if (!retval) {
  		/* If kernel getsockopt finished successfully,
  		 * copy whatever was returned to the user back
@@ -1914,18 +2043,19 @@ int __cgroup_bpf_run_filter_getsockopt(struct sock *sk, int level,
  			goto out;
  		}
  		orig_optlen = ctx.optlen;
-
-		if (copy_from_user(ctx.optval, optval,
-				   min(ctx.optlen, max_optlen)) != 0) {
-			ret = -EFAULT;
-			goto out;
-		}
  	}
 
+	ctx.optval = optval;
+	ctx.optval_end = optval + max_optlen;
+	ctx.flags = BPF_SOCKOPT_FLAG_OPTVAL_USER;
+
  	lock_sock(sk);
-	ret = bpf_prog_run_array_cg(&cgrp->bpf, CGROUP_GETSOCKOPT,
-				    &ctx, bpf_prog_run, retval, NULL);
+	ret = bpf_prog_run_array_cg_cb(&cgrp->bpf, CGROUP_GETSOCKOPT,
+				       &ctx, bpf_prog_run, retval, NULL,
+				       filter_getsockopt_progs_cb,
+				       &cb_args);
  	release_sock(sk);
+	max_optlen = cb_args.max_optlen;
 
  	if (ret < 0)
  		goto out;
@@ -1942,7 +2072,9 @@ int __cgroup_bpf_run_filter_getsockopt(struct sock *sk, int level,
  	}
 
  	if (ctx.optlen != 0) {
-		if (optval && copy_to_user(optval, ctx.optval, ctx.optlen)) {
+		if (optval &&
+		    !(ctx.flags & BPF_SOCKOPT_FLAG_OPTVAL_USER) &&
+		    copy_to_user(optval, ctx.optval, ctx.optlen)) {
  			ret = -EFAULT;
  			goto out;
  		}
@@ -2388,6 +2520,10 @@ static bool cg_sockopt_is_valid_access(int off, int size,
  		if (size != size_default)
  			return false;
  		return prog->expected_attach_type == BPF_CGROUP_GETSOCKOPT;
+	case offsetof(struct bpf_sockopt, flags):
+		if (size != sizeof(__u32))
+			return false;
+		break;
  	default:
  		if (size != size_default)
  			return false;
@@ -2481,6 +2617,9 @@ static u32 cg_sockopt_convert_ctx_access(enum bpf_access_type type,
  	case offsetof(struct bpf_sockopt, optval_end):
  		*insn++ = CG_SOCKOPT_READ_FIELD(optval_end);
  		break;
+	case offsetof(struct bpf_sockopt, flags):
+		*insn++ = CG_SOCKOPT_READ_FIELD(flags);
+		break;
  	}
 
  	return insn - insn_buf;
kernel/bpf/verifier.c +0 -0
index 132f25dab931..fbc0096693e7 100644

kernel/bpf/verifier.c static bool can_be_sleepable(struct bpf_prog *prog) · line 19541

Quoted 4 lines
kernel/bpf/verifier.c +1 -0
@@ -19538,9 +19538,11 @@ static bool can_be_sleepable(struct bpf_prog *prog)
  			return false;
  		}
  	}
+

Extra newline?

Quoted 9 lines
kernel/bpf/verifier.c +2 -1
  	return prog->type == BPF_PROG_TYPE_LSM ||
  	       prog->type == BPF_PROG_TYPE_KPROBE /* only for uprobes */ ||
-	       prog->type == BPF_PROG_TYPE_STRUCT_OPS;
+	       prog->type == BPF_PROG_TYPE_STRUCT_OPS ||
+	       prog->type == BPF_PROG_TYPE_CGROUP_SOCKOPT;
  }
 
  static int check_attach_btf_id(struct bpf_verifier_env *env)

kernel/bpf/verifier.c static int check_attach_btf_id(struct bpf_verifier_env *env) · line 19568

Quoted 6 lines
kernel/bpf/verifier.c +2 -1
@@ -19562,7 +19564,8 @@ static int check_attach_btf_id(struct bpf_verifier_env *env)
  	}
 
  	if (prog->aux->sleepable && !can_be_sleepable(prog)) {
-		verbose(env, "Only fentry/fexit/fmod_ret, lsm, iter, uprobe, and struct_ops programs can be sleepable\n");
+		verbose(env, "Only fentry/fexit/fmod_ret, lsm, iter, uprobe,"
+			"cgroup, and struct_ops programs can be sleepable\n");

I don't think we split the lines even if they are too long. Makes them ungreppable :-(

Quoted 9 lines
kernel/bpf/verifier.c +0 -0
  		return -EINVAL;
  	}
 
tools/include/uapi/linux/bpf.h +0 -0
index 70da85200695..fff6f7dff408 100644
@@ -7138,6 +7138,13 @@ struct bpf_sockopt {
  	__s32	optname;

tools/include/uapi/linux/bpf.h struct bpf_sockopt { · line 7148

Quoted 10 lines
tools/include/uapi/linux/bpf.h +7 -0
  	__s32	optlen;
  	__s32	retval;
+
+	__u32	flags;
+};
+
+enum bpf_sockopt_flags {
+	/* optval is a pointer to user space memory */
+	BPF_SOCKOPT_FLAG_OPTVAL_USER	= (1U << 0),
  };

Why do we need to export the flag? Do we still want the users to do something based on the presence/absence of BPF_SOCKOPT_FLAG_OPTVAL_USER?

#4 lore raw

Re: [RFC bpf-next v2 1/6] bpf: enable sleepable BPF programs attached to cgroup/{get,set}sockopt.

In reply to Stanislav Fomichev
Quoted 494 lines · Stanislav Fomichev
Quoted 139 lines · On 08/10, [email protected] wrote:

From: Kui-Feng Lee <[email protected]>

Enable sleepable cgroup/{get,set}sockopt hooks.

The sleepable BPF programs attached to cgroup/{get,set}sockopt hooks may received a pointer to the optval in user space instead of a kernel copy. ctx->user_optval and ctx->user_optval_end are the pointers to the begin and end of the user space buffer if receiving a user space buffer. ctx->optval and ctx->optval_end will be a kernel copy if receiving a kernel space buffer.

A program receives a user space buffer if ctx->flags & BPF_SOCKOPT_FLAG_OPTVAL_USER is true, otherwise it receives a kernel space buffer. The BPF programs should not read/write from/to a user space buffer dirrectly. It should access the buffer through bpf_copy_from_user() and bpf_copy_to_user() provided in the following patches.

---
  include/linux/filter.h         |   1 +
  include/uapi/linux/bpf.h       |   7 +
  kernel/bpf/cgroup.c            | 229 ++++++++++++++++++++++++++-------
  kernel/bpf/verifier.c          |   7 +-
  tools/include/uapi/linux/bpf.h |   7 +
  tools/lib/bpf/libbpf.c         |   2 +
  6 files changed, 206 insertions(+), 47 deletions(-)
include/linux/filter.h +1 -0
index 761af6b3cf2b..54169a641d40 100644
@@ -1337,6 +1337,7 @@ struct bpf_sockopt_kern {
  	s32		level;
  	s32		optname;
  	s32		optlen;
+	u32		flags;
  	/* for retval in struct bpf_cg_run_ctx */
  	struct task_struct *current_task;
  	/* Temporary "register" for indirect stores to ppos. */
include/uapi/linux/bpf.h +7 -0
index 70da85200695..fff6f7dff408 100644
@@ -7138,6 +7138,13 @@ struct bpf_sockopt {
  	__s32	optname;
  	__s32	optlen;
  	__s32	retval;
+
+	__u32	flags;
+};
+
+enum bpf_sockopt_flags {
+	/* optval is a pointer to user space memory */
+	BPF_SOCKOPT_FLAG_OPTVAL_USER	= (1U << 0),
  };
 
  struct bpf_pidns_info {
kernel/bpf/cgroup.c +41 -6
index 5b2741aa0d9b..59489d9619a3 100644
@@ -28,25 +28,46 @@ EXPORT_SYMBOL(cgroup_bpf_enabled_key);
   * function pointer.
   */
  static __always_inline int
-bpf_prog_run_array_cg(const struct cgroup_bpf *cgrp,
-		      enum cgroup_bpf_attach_type atype,
-		      const void *ctx, bpf_prog_run_fn run_prog,
-		      int retval, u32 *ret_flags)
+bpf_prog_run_array_cg_cb(const struct cgroup_bpf *cgrp,
+			 enum cgroup_bpf_attach_type atype,
+			 const void *ctx, bpf_prog_run_fn run_prog,
+			 int retval, u32 *ret_flags,
+			 int (*progs_cb)(void *, const struct bpf_prog_array_item *),
+			 void *progs_cb_arg)
  {
  	const struct bpf_prog_array_item *item;
  	const struct bpf_prog *prog;
  	const struct bpf_prog_array *array;
  	struct bpf_run_ctx *old_run_ctx;
  	struct bpf_cg_run_ctx run_ctx;
+	bool do_sleepable;
  	u32 func_ret;
+	int err;
+
+	do_sleepable =
+		atype == CGROUP_SETSOCKOPT || atype == CGROUP_GETSOCKOPT;
 
  	run_ctx.retval = retval;
  	migrate_disable();
-	rcu_read_lock();
+	if (do_sleepable) {
+		might_fault();
+		rcu_read_lock_trace();
+	} else
+		rcu_read_lock();
  	array = rcu_dereference(cgrp->effective[atype]);
  	item = &array->items[0];
+
+	if (progs_cb) {
+		err = progs_cb(progs_cb_arg, item);
+		if (err)
+			return err;
+	}
+
  	old_run_ctx = bpf_set_run_ctx(&run_ctx.run_ctx);
  	while ((prog = READ_ONCE(item->prog))) {
+		if (do_sleepable && !prog->aux->sleepable)
+			rcu_read_lock();
+
  		run_ctx.prog_item = item;
  		func_ret = run_prog(prog, ctx);
  		if (ret_flags) {
@@ -56,13 +77,48 @@ bpf_prog_run_array_cg(const struct cgroup_bpf *cgrp,
  		if (!func_ret && !IS_ERR_VALUE((long)run_ctx.retval))
  			run_ctx.retval = -EPERM;
  		item++;
+
+		if (do_sleepable && !prog->aux->sleepable)
+			rcu_read_unlock();
  	}
  	bpf_reset_run_ctx(old_run_ctx);
-	rcu_read_unlock();
+	if (do_sleepable)
+		rcu_read_unlock_trace();
+	else
+		rcu_read_unlock();
  	migrate_enable();
  	return run_ctx.retval;
  }
 
+static __always_inline void
+count_sleepable_prog_cg(const struct bpf_prog_array_item *item,
+			int *sleepable, int *non_sleepable)
+{
+	const struct bpf_prog *prog;
+	bool ret = true;
+
+	*sleepable = 0;

kernel/bpf/cgroup.c bpf_prog_run_array_cg(const struct cgroup_bpf *cgrp, · line 110

Quoted 10 lines
kernel/bpf/cgroup.c +10 -0
+	*non_sleepable = 0;
+
+	while (ret && (prog = READ_ONCE(item->prog))) {
+		if (prog->aux->sleepable)
+			(*sleepable)++;
+		else
+			(*non_sleepable)++;
+		item++;
+	}
+}

Can we maintain this info in the slow attach/detach path? Running the loop every time we get a sockopt syscall another time seems like something we can avoid? (even though it's a slow path, seems like we can do better?)

Quoted 320 lines
kernel/bpf/cgroup.c +133 -39
+
+static __always_inline int
+bpf_prog_run_array_cg(const struct cgroup_bpf *cgrp,
+		      enum cgroup_bpf_attach_type atype,
+		      const void *ctx, bpf_prog_run_fn run_prog,
+		      int retval, u32 *ret_flags)
+{
+	return bpf_prog_run_array_cg_cb(cgrp, atype, ctx, run_prog, retval,
+					ret_flags, NULL, NULL);
+}
+
  unsigned int __cgroup_bpf_run_lsm_sock(const void *ctx,
  				       const struct bpf_insn *insn)
  {
@@ -307,7 +363,7 @@ static void cgroup_bpf_release(struct work_struct *work)
  		old_array = rcu_dereference_protected(
  				cgrp->bpf.effective[atype],
  				lockdep_is_held(&cgroup_mutex));
-		bpf_prog_array_free(old_array);
+		bpf_prog_array_free_sleepable(old_array);
  	}
 
  	list_for_each_entry_safe(storage, stmp, storages, list_cg) {
@@ -451,7 +507,7 @@ static void activate_effective_progs(struct cgroup *cgrp,
  	/* free prog array after grace period, since __cgroup_bpf_run_*()
  	 * might be still walking the array
  	 */
-	bpf_prog_array_free(old_array);
+	bpf_prog_array_free_sleepable(old_array);
  }
 
  /**
@@ -491,7 +547,7 @@ int cgroup_bpf_inherit(struct cgroup *cgrp)
  	return 0;
  cleanup:
  	for (i = 0; i < NR; i++)
-		bpf_prog_array_free(arrays[i]);
+		bpf_prog_array_free_sleepable(arrays[i]);
 
  	for (p = cgroup_parent(cgrp); p; p = cgroup_parent(p))
  		cgroup_bpf_put(p);
@@ -525,7 +581,7 @@ static int update_effective_progs(struct cgroup *cgrp,
 
  		if (percpu_ref_is_zero(&desc->bpf.refcnt)) {
  			if (unlikely(desc->bpf.inactive)) {
-				bpf_prog_array_free(desc->bpf.inactive);
+				bpf_prog_array_free_sleepable(desc->bpf.inactive);
  				desc->bpf.inactive = NULL;
  			}
  			continue;
@@ -544,7 +600,7 @@ static int update_effective_progs(struct cgroup *cgrp,
  	css_for_each_descendant_pre(css, &cgrp->self) {
  		struct cgroup *desc = container_of(css, struct cgroup, self);
 
-		bpf_prog_array_free(desc->bpf.inactive);
+		bpf_prog_array_free_sleepable(desc->bpf.inactive);
  		desc->bpf.inactive = NULL;
  	}
 
@@ -1740,7 +1796,7 @@ int __cgroup_bpf_run_filter_sysctl(struct ctl_table_header *head,
 
  #ifdef CONFIG_NET
  static int sockopt_alloc_buf(struct bpf_sockopt_kern *ctx, int max_optlen,
-			     struct bpf_sockopt_buf *buf)
+			     struct bpf_sockopt_buf *buf, bool force_alloc)
  {
  	if (unlikely(max_optlen < 0))
  		return -EINVAL;
@@ -1752,7 +1808,7 @@ static int sockopt_alloc_buf(struct bpf_sockopt_kern *ctx, int max_optlen,
  		max_optlen = PAGE_SIZE;
  	}
 
-	if (max_optlen <= sizeof(buf->data)) {
+	if (max_optlen <= sizeof(buf->data) && !force_alloc) {
  		/* When the optval fits into BPF_SOCKOPT_KERN_BUF_SIZE
  		 * bytes avoid the cost of kzalloc.
  		 */
@@ -1773,7 +1829,8 @@ static int sockopt_alloc_buf(struct bpf_sockopt_kern *ctx, int max_optlen,
  static void sockopt_free_buf(struct bpf_sockopt_kern *ctx,
  			     struct bpf_sockopt_buf *buf)
  {
-	if (ctx->optval == buf->data)
+	if (ctx->optval == buf->data ||
+	    ctx->flags & BPF_SOCKOPT_FLAG_OPTVAL_USER)
  		return;
  	kfree(ctx->optval);
  }
@@ -1781,7 +1838,50 @@ static void sockopt_free_buf(struct bpf_sockopt_kern *ctx,
  static bool sockopt_buf_allocated(struct bpf_sockopt_kern *ctx,
  				  struct bpf_sockopt_buf *buf)
  {
-	return ctx->optval != buf->data;
+	return ctx->optval != buf->data &&
+		!(ctx->flags & BPF_SOCKOPT_FLAG_OPTVAL_USER);
+}
+
+struct filter_sockopt_cb_args {
+	struct bpf_sockopt_kern *ctx;
+	struct bpf_sockopt_buf *buf;
+	int max_optlen;
+};
+
+static int filter_setsockopt_progs_cb(void *arg,
+				      const struct bpf_prog_array_item *item)
+{
+	struct filter_sockopt_cb_args *cb_args = arg;
+	struct bpf_sockopt_kern *ctx = cb_args->ctx;
+	char *optval = ctx->optval;
+	int sleepable_cnt, non_sleepable_cnt;
+	int max_optlen;
+
+	count_sleepable_prog_cg(item, &sleepable_cnt, &non_sleepable_cnt);
+
+	if (!non_sleepable_cnt)
+		return 0;
+
+	/* Allocate a bit more than the initial user buffer for
+	 * BPF program. The canonical use case is overriding
+	 * TCP_CONGESTION(nv) to TCP_CONGESTION(cubic).
+	 */
+	max_optlen = max_t(int, 16, ctx->optlen);
+	/* We need to force allocating from heap if there are sleepable
+	 * programs since they may created dynptrs from ctx->optval. In
+	 * this case, dynptrs will try to free the buffer that is actually
+	 * on the stack without this flag.
+	 */
+	max_optlen = sockopt_alloc_buf(ctx, max_optlen, cb_args->buf,
+				       !!sleepable_cnt);
+	if (max_optlen < 0)
+		return max_optlen;
+
+	if (copy_from_user(ctx->optval, optval,
+			   min(ctx->optlen, max_optlen)) != 0)
+		return -EFAULT;
+
+	return 0;
  }
 
  int __cgroup_bpf_run_filter_setsockopt(struct sock *sk, int *level,
@@ -1795,27 +1895,22 @@ int __cgroup_bpf_run_filter_setsockopt(struct sock *sk, int *level,
  		.level = *level,
  		.optname = *optname,
  	};
+	struct filter_sockopt_cb_args cb_args = {
+		.ctx = &ctx,
+		.buf = &buf,
+	};
  	int ret, max_optlen;
 
-	/* Allocate a bit more than the initial user buffer for
-	 * BPF program. The canonical use case is overriding
-	 * TCP_CONGESTION(nv) to TCP_CONGESTION(cubic).
-	 */
-	max_optlen = max_t(int, 16, *optlen);
-	max_optlen = sockopt_alloc_buf(&ctx, max_optlen, &buf);
-	if (max_optlen < 0)
-		return max_optlen;
-
+	max_optlen = *optlen;
  	ctx.optlen = *optlen;
-
-	if (copy_from_user(ctx.optval, optval, min(*optlen, max_optlen)) != 0) {
-		ret = -EFAULT;
-		goto out;
-	}
+	ctx.optval = optval;
+	ctx.optval_end = optval + *optlen;
+	ctx.flags = BPF_SOCKOPT_FLAG_OPTVAL_USER;
 
  	lock_sock(sk);
-	ret = bpf_prog_run_array_cg(&cgrp->bpf, CGROUP_SETSOCKOPT,
-				    &ctx, bpf_prog_run, 0, NULL);
+	ret = bpf_prog_run_array_cg_cb(&cgrp->bpf, CGROUP_SETSOCKOPT,
+				       &ctx, bpf_prog_run, 0, NULL,
+				       filter_setsockopt_progs_cb, &cb_args);
  	release_sock(sk);
 
  	if (ret)
@@ -1824,7 +1919,8 @@ int __cgroup_bpf_run_filter_setsockopt(struct sock *sk, int *level,
  	if (ctx.optlen == -1) {
  		/* optlen set to -1, bypass kernel */
  		ret = 1;
-	} else if (ctx.optlen > max_optlen || ctx.optlen < -1) {
+	} else if (ctx.optlen > (ctx.optval_end - ctx.optval) ||
+		   ctx.optlen < -1) {
  		/* optlen is out of bounds */
  		if (*optlen > PAGE_SIZE && ctx.optlen >= 0) {
  			pr_info_once("bpf setsockopt: ignoring program buffer with optlen=%d (max_optlen=%d)\n",
@@ -1846,6 +1942,8 @@ int __cgroup_bpf_run_filter_setsockopt(struct sock *sk, int *level,
  		 */
  		if (ctx.optlen != 0) {
  			*optlen = ctx.optlen;
+			if (ctx.flags & BPF_SOCKOPT_FLAG_OPTVAL_USER)
+				return 0;
  			/* We've used bpf_sockopt_kern->buf as an intermediary
  			 * storage, but the BPF program indicates that we need
  			 * to pass this data to the kernel setsockopt handler.
@@ -1874,6 +1972,36 @@ int __cgroup_bpf_run_filter_setsockopt(struct sock *sk, int *level,
  	return ret;
  }
 
+static int filter_getsockopt_progs_cb(void *arg,
+				      const struct bpf_prog_array_item *item)
+{
+	struct filter_sockopt_cb_args *cb_args = arg;
+	struct bpf_sockopt_kern *ctx = cb_args->ctx;
+	int sleepable_cnt, non_sleepable_cnt;
+	int max_optlen;
+	char *optval;
+
+	count_sleepable_prog_cg(item, &sleepable_cnt, &non_sleepable_cnt);
+
+	if (!non_sleepable_cnt)
+		return 0;
+
+	optval = ctx->optval;
+	max_optlen = sockopt_alloc_buf(ctx, cb_args->max_optlen,
+				       cb_args->buf, false);
+	if (max_optlen < 0)
+		return max_optlen;
+
+	if (copy_from_user(ctx->optval, optval,
+			   min(ctx->optlen, max_optlen)) != 0)
+		return -EFAULT;
+
+	ctx->flags = 0;
+	cb_args->max_optlen = max_optlen;
+
+	return 0;
+}
+
  int __cgroup_bpf_run_filter_getsockopt(struct sock *sk, int level,
  				       int optname, char __user *optval,
  				       int __user *optlen, int max_optlen,
@@ -1887,15 +2015,16 @@ int __cgroup_bpf_run_filter_getsockopt(struct sock *sk, int level,
  		.optname = optname,
  		.current_task = current,
  	};
+	struct filter_sockopt_cb_args cb_args = {
+		.ctx = &ctx,
+		.buf = &buf,
+		.max_optlen = max_optlen,
+	};
  	int orig_optlen;
  	int ret;
 
  	orig_optlen = max_optlen;
  	ctx.optlen = max_optlen;
-	max_optlen = sockopt_alloc_buf(&ctx, max_optlen, &buf);
-	if (max_optlen < 0)
-		return max_optlen;
-
  	if (!retval) {
  		/* If kernel getsockopt finished successfully,
  		 * copy whatever was returned to the user back
@@ -1914,18 +2043,19 @@ int __cgroup_bpf_run_filter_getsockopt(struct sock *sk, int level,
  			goto out;
  		}
  		orig_optlen = ctx.optlen;
-
-		if (copy_from_user(ctx.optval, optval,
-				   min(ctx.optlen, max_optlen)) != 0) {
-			ret = -EFAULT;
-			goto out;
-		}
  	}
 
+	ctx.optval = optval;
+	ctx.optval_end = optval + max_optlen;
+	ctx.flags = BPF_SOCKOPT_FLAG_OPTVAL_USER;
+
  	lock_sock(sk);
-	ret = bpf_prog_run_array_cg(&cgrp->bpf, CGROUP_GETSOCKOPT,
-				    &ctx, bpf_prog_run, retval, NULL);
+	ret = bpf_prog_run_array_cg_cb(&cgrp->bpf, CGROUP_GETSOCKOPT,
+				       &ctx, bpf_prog_run, retval, NULL,
+				       filter_getsockopt_progs_cb,
+				       &cb_args);
  	release_sock(sk);
+	max_optlen = cb_args.max_optlen;
 
  	if (ret < 0)
  		goto out;
@@ -1942,7 +2072,9 @@ int __cgroup_bpf_run_filter_getsockopt(struct sock *sk, int level,
  	}
 
  	if (ctx.optlen != 0) {
-		if (optval && copy_to_user(optval, ctx.optval, ctx.optlen)) {
+		if (optval &&
+		    !(ctx.flags & BPF_SOCKOPT_FLAG_OPTVAL_USER) &&
+		    copy_to_user(optval, ctx.optval, ctx.optlen)) {
  			ret = -EFAULT;
  			goto out;
  		}
@@ -2388,6 +2520,10 @@ static bool cg_sockopt_is_valid_access(int off, int size,
  		if (size != size_default)
  			return false;
  		return prog->expected_attach_type == BPF_CGROUP_GETSOCKOPT;
+	case offsetof(struct bpf_sockopt, flags):
+		if (size != sizeof(__u32))
+			return false;
+		break;
  	default:
  		if (size != size_default)
  			return false;
@@ -2481,6 +2617,9 @@ static u32 cg_sockopt_convert_ctx_access(enum bpf_access_type type,
  	case offsetof(struct bpf_sockopt, optval_end):
  		*insn++ = CG_SOCKOPT_READ_FIELD(optval_end);
  		break;
+	case offsetof(struct bpf_sockopt, flags):
+		*insn++ = CG_SOCKOPT_READ_FIELD(flags);
+		break;
  	}
 
  	return insn - insn_buf;
kernel/bpf/verifier.c +0 -0
index 132f25dab931..fbc0096693e7 100644

kernel/bpf/verifier.c static bool can_be_sleepable(struct bpf_prog *prog) · line 19541

Quoted 4 lines
kernel/bpf/verifier.c +1 -0
@@ -19538,9 +19538,11 @@ static bool can_be_sleepable(struct bpf_prog *prog)
  			return false;
  		}
  	}
+

Extra newline?

Quoted 11 lines
kernel/bpf/verifier.c +2 -1
  	return prog->type == BPF_PROG_TYPE_LSM ||
  	       prog->type == BPF_PROG_TYPE_KPROBE /* only for uprobes */ ||
-	       prog->type == BPF_PROG_TYPE_STRUCT_OPS;
+	       prog->type == BPF_PROG_TYPE_STRUCT_OPS ||
+	       prog->type == BPF_PROG_TYPE_CGROUP_SOCKOPT;
  }
 
  static int check_attach_btf_id(struct bpf_verifier_env *env)
@@ -19562,7 +19564,8 @@ static int check_attach_btf_id(struct bpf_verifier_env *env)
  	}
 
Quoted 7 lines

kernel/bpf/verifier.c static int check_attach_btf_id(struct bpf_verifier_env *env) · line 19568

Quoted 4 lines
kernel/bpf/verifier.c +2 -1
  	if (prog->aux->sleepable && !can_be_sleepable(prog)) {
-		verbose(env, "Only fentry/fexit/fmod_ret, lsm, iter, uprobe, and struct_ops programs can be sleepable\n");
+		verbose(env, "Only fentry/fexit/fmod_ret, lsm, iter, uprobe,"
+			"cgroup, and struct_ops programs can be sleepable\n");

I don't think we split the lines even if they are too long. Makes them ungreppable :-(

Got it!

Quoted 16 lines
Quoted 16 lines
kernel/bpf/verifier.c +0 -0
  		return -EINVAL;
  	}
 
tools/include/uapi/linux/bpf.h +4 -0
index 70da85200695..fff6f7dff408 100644
@@ -7138,6 +7138,13 @@ struct bpf_sockopt {
  	__s32	optname;
  	__s32	optlen;
  	__s32	retval;
+
+	__u32	flags;
+};
+
Quoted 8 lines

tools/include/uapi/linux/bpf.h struct bpf_sockopt { · line 7148

Quoted 4 lines
tools/include/uapi/linux/bpf.h +3 -0
+enum bpf_sockopt_flags {
+	/* optval is a pointer to user space memory */
+	BPF_SOCKOPT_FLAG_OPTVAL_USER	= (1U << 0),
  };

Why do we need to export the flag? Do we still want the users to do something based on the presence/absence of BPF_SOCKOPT_FLAG_OPTVAL_USER?

I will remove this flag from the view of BPF programs.

#5 lore raw

RFC v2 2/6 bpf: Prevent BPF programs from access the buffer pointed by user_optval.

In reply to [email protected]

From: Kui-Feng Lee <[email protected]>

Since the buffer pointed by ctx->user_optval is in user space, BPF programs in kernel space should not access it directly. They should use bpf_copy_from_user() and bpf_copy_to_user() to move data between user and kernel space.

---
 kernel/bpf/cgroup.c   | 16 +++++++++--
 kernel/bpf/verifier.c | 66 +++++++++++++++++++++----------------------
 2 files changed, 47 insertions(+), 35 deletions(-)
kernel/bpf/cgroup.c +14 -2
index 59489d9619a3..5bf3115b265c 100644
@@ -2509,12 +2509,24 @@ static bool cg_sockopt_is_valid_access(int off, int size,
 	case offsetof(struct bpf_sockopt, optval):
 		if (size != sizeof(__u64))
 			return false;
-		info->reg_type = PTR_TO_PACKET;
+		if (prog->aux->sleepable)
+			/* Prohibit access to the memory pointed by optval
+			 * in sleepable programs.
+			 */
+			info->reg_type = PTR_TO_PACKET | MEM_USER;
+		else
+			info->reg_type = PTR_TO_PACKET;
 		break;
 	case offsetof(struct bpf_sockopt, optval_end):
 		if (size != sizeof(__u64))
 			return false;
-		info->reg_type = PTR_TO_PACKET_END;
+		if (prog->aux->sleepable)
+			/* Prohibit access to the memory pointed by
+			 * optval_end in sleepable programs.
+			 */
+			info->reg_type = PTR_TO_PACKET_END | MEM_USER;
+		else
+			info->reg_type = PTR_TO_PACKET_END;
 		break;
 	case offsetof(struct bpf_sockopt, retval):
 		if (size != size_default)
kernel/bpf/verifier.c +33 -33

124 lines not on this page · raw message

Signature · 1 line
2.34.1
reviewer
#6 lore raw

Re: [RFC bpf-next v2 2/6] bpf: Prevent BPF programs from access the buffer pointed by user_optval.

Quoted 9 lines · On 8/10/23 9:31 PM, [email protected] wrote:

From: Kui-Feng Lee <[email protected]>

Since the buffer pointed by ctx->user_optval is in user space, BPF programs in kernel space should not access it directly. They should use bpf_copy_from_user() and bpf_copy_to_user() to move data between user and kernel space.

You probably only want

since it matches email 'From'. Also,
From: Kui-Feng Lee <[email protected]>
is different from email 'From'. Strange.

[...]

#7 lore raw

Re: [RFC bpf-next v2 2/6] bpf: Prevent BPF programs from access the buffer pointed by user_optval.

Quoted 10 lines · Yonghong Song
Quoted 7 lines · On 8/10/23 9:31 PM, [email protected] wrote:

From: Kui-Feng Lee <[email protected]>

Since the buffer pointed by ctx->user_optval is in user space, BPF programs in kernel space should not access it directly.  They should use bpf_copy_from_user() and bpf_copy_to_user() to move data between user and kernel space.

Quoted 8 lines
Quoted 2 lines

You probably only want

since it matches email 'From'. Also,
   From: Kui-Feng Lee <[email protected]>
is different from email 'From'. Strange.

Got it!sssssssssssssss

Quoted 2 lines

[...]

#8 lore raw

RFC v2 3/6 bpf: rename bpf_copy_to_user().

In reply to [email protected]

From: Kui-Feng Lee <[email protected]>

Rename bpf_copy_to_user() to avoid confliction with the BPF kfunc.

---
 kernel/bpf/syscall.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
kernel/bpf/syscall.c +4 -4
index 7f4e8c357a6a..81b6e60cc030 100644
@@ -3297,8 +3297,8 @@ static void bpf_raw_tp_link_show_fdinfo(const struct bpf_link *link,
 		   raw_tp_link->btp->tp->name);
 }
 
-static int bpf_copy_to_user(char __user *ubuf, const char *buf, u32 ulen,
-			    u32 len)
+static int _bpf_copy_to_user(char __user *ubuf, const char *buf, u32 ulen,
+			     u32 len)
 {
 	if (ulen >= len + 1) {
 		if (copy_to_user(ubuf, buf, len + 1))
@@ -3334,7 +3334,7 @@ static int bpf_raw_tp_link_fill_link_info(const struct bpf_link *link,
 	if (!ubuf)
 		return 0;
 
-	return bpf_copy_to_user(ubuf, tp_name, ulen, tp_len);
+	return _bpf_copy_to_user(ubuf, tp_name, ulen, tp_len);
 }
 
 static const struct bpf_link_ops bpf_raw_tp_link_lops = {
@@ -3388,7 +3388,7 @@ static int bpf_perf_link_fill_common(const struct perf_event *event,
 
 	if (buf) {
 		len = strlen(buf);
-		err = bpf_copy_to_user(uname, buf, ulen, len);
+		err = _bpf_copy_to_user(uname, buf, ulen, len);
 		if (err)
 			return err;
 	} else {
Signature · 1 line
2.34.1
#9 lore raw

RFC v2 4/6 bpf: Provide bpf_copy_from_user() and bpf_copy_to_user().

In reply to [email protected]

From: Kui-Feng Lee <[email protected]>

Provide bpf_copy_from_user() and bpf_copy_to_user() to the BPF programs attached to cgroup/{set,get}sockopt. bpf_copy_to_user() is a new kfunc to copy data from an kernel space buffer to a user space buffer. They are only available for sleepable BPF programs. bpf_copy_to_user() is only available to the BPF programs attached to cgroup/getsockopt.

---
 kernel/bpf/cgroup.c  |  6 ++++++
 kernel/bpf/helpers.c | 31 +++++++++++++++++++++++++++++++
 2 files changed, 37 insertions(+)
kernel/bpf/cgroup.c +6 -0
index 5bf3115b265c..c15a72860d2a 100644
@@ -2461,6 +2461,12 @@ cg_sockopt_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
 #endif
 	case BPF_FUNC_perf_event_output:
 		return &bpf_event_output_data_proto;
+
+	case BPF_FUNC_copy_from_user:
+		if (prog->aux->sleepable)
+			return &bpf_copy_from_user_proto;
+		return NULL;
+
 	default:
 		return bpf_base_func_proto(func_id);
 	}
kernel/bpf/helpers.c +31 -0
index eb91cae0612a..ff240db1512c 100644
@@ -669,6 +669,26 @@ const struct bpf_func_proto bpf_copy_from_user_proto = {
 	.arg3_type	= ARG_ANYTHING,
 };
 
+/**
+ * int bpf_copy_to_user(void *dst, u32 size, const void *kern_ptr)
+ *     Description
+ *             Read *size* bytes from kernel space address *kern_ptr* and
+ *              store the data in user space address *dst*. This is a
+ *              wrapper of **copy_to_user**\ ().
+ *     Return
+ *             0 on success, or a negative error in case of failure.
+ */
+__bpf_kfunc int bpf_copy_to_user(void *dst__uninit, u32 dst__sz,
+				 const void *src__ign)
+{
+	int ret = copy_to_user(dst__uninit, src__ign, dst__sz);
+
+	if (unlikely(ret))
+		return -EFAULT;
+
+	return ret;
+}
+
 BPF_CALL_5(bpf_copy_from_user_task, void *, dst, u32, size,
 	   const void __user *, user_ptr, struct task_struct *, tsk, u64, flags)
 {
@@ -2456,6 +2476,7 @@ BTF_ID_FLAGS(func, bpf_cgroup_from_id, KF_ACQUIRE | KF_RET_NULL)
 BTF_ID_FLAGS(func, bpf_task_under_cgroup, KF_RCU)
 #endif
 BTF_ID_FLAGS(func, bpf_task_from_pid, KF_ACQUIRE | KF_RET_NULL)
+BTF_ID_FLAGS(func, bpf_copy_to_user, KF_SLEEPABLE)
 BTF_SET8_END(generic_btf_ids)
 
 static const struct btf_kfunc_id_set generic_kfunc_set = {
@@ -2494,6 +2515,15 @@ static const struct btf_kfunc_id_set common_kfunc_set = {
 	.set   = &common_btf_ids,
 };
 
+BTF_SET8_START(cgroup_common_btf_ids)
+BTF_ID_FLAGS(func, bpf_copy_to_user, KF_SLEEPABLE)
+BTF_SET8_END(cgroup_common_btf_ids)
+
+static const struct btf_kfunc_id_set cgroup_kfunc_set = {
+	.owner	= THIS_MODULE,
+	.set	= &cgroup_common_btf_ids,
+};
+
 static int __init kfunc_init(void)
 {
 	int ret;
@@ -2513,6 +2543,7 @@ static int __init kfunc_init(void)
 	ret = register_btf_kfunc_id_set(BPF_PROG_TYPE_TRACING, &generic_kfunc_set);
 	ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_SCHED_CLS, &generic_kfunc_set);
 	ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_STRUCT_OPS, &generic_kfunc_set);
+	ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_CGROUP_SOCKOPT, &cgroup_kfunc_set);
 	ret = ret ?: register_btf_id_dtor_kfuncs(generic_dtors,
 						  ARRAY_SIZE(generic_dtors),
 						  THIS_MODULE);
Signature · 1 line
2.34.1
#10 lore raw

Re: [RFC bpf-next v2 4/6] bpf: Provide bpf_copy_from_user() and bpf_copy_to_user().

Quoted 19 lines · On 08/10, [email protected] wrote:

From: Kui-Feng Lee <[email protected]>

Provide bpf_copy_from_user() and bpf_copy_to_user() to the BPF programs attached to cgroup/{set,get}sockopt. bpf_copy_to_user() is a new kfunc to copy data from an kernel space buffer to a user space buffer. They are only available for sleepable BPF programs. bpf_copy_to_user() is only available to the BPF programs attached to cgroup/getsockopt.

---
 kernel/bpf/cgroup.c  |  6 ++++++
 kernel/bpf/helpers.c | 31 +++++++++++++++++++++++++++++++
 2 files changed, 37 insertions(+)
kernel/bpf/cgroup.c +0 -0
index 5bf3115b265c..c15a72860d2a 100644

kernel/bpf/cgroup.c cg_sockopt_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) · line 2468

Quoted 8 lines
kernel/bpf/cgroup.c +5 -0
@@ -2461,6 +2461,12 @@ cg_sockopt_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
 #endif
 	case BPF_FUNC_perf_event_output:
 		return &bpf_event_output_data_proto;
+
+	case BPF_FUNC_copy_from_user:
+		if (prog->aux->sleepable)
+			return &bpf_copy_from_user_proto;
+		return NULL;

If we just allow copy to/from, I'm not sure I understand how the buffer sharing between sleepable/non-sleepable works.

Let's assume I have two progs in the chain:
1. non-sleepable - copies the buffer, does some modifications; since we don't copy the buffer back after every prog run, the modifications stay in the kernel buffer
2. sleepable - runs and just gets the user pointer? does it mean this sleepable program doesn't see the changes from (1)?

IOW, do we need some custom sockopt copy_to/from that handle this potential buffer location transparently or am I missing something?

Assuming we want to support this at all. If we do, might deserve a selftest.

#11 lore raw

Re: [RFC bpf-next v2 4/6] bpf: Provide bpf_copy_from_user() and bpf_copy_to_user().

Quoted 39 lines · Stanislav Fomichev
Quoted 19 lines · On 08/10, [email protected] wrote:

From: Kui-Feng Lee <[email protected]>

Provide bpf_copy_from_user() and bpf_copy_to_user() to the BPF programs attached to cgroup/{set,get}sockopt. bpf_copy_to_user() is a new kfunc to copy data from an kernel space buffer to a user space buffer. They are only available for sleepable BPF programs. bpf_copy_to_user() is only available to the BPF programs attached to cgroup/getsockopt.

---
  kernel/bpf/cgroup.c  |  6 ++++++
  kernel/bpf/helpers.c | 31 +++++++++++++++++++++++++++++++
  2 files changed, 37 insertions(+)
kernel/bpf/cgroup.c +0 -0
index 5bf3115b265c..c15a72860d2a 100644

kernel/bpf/cgroup.c cg_sockopt_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) · line 2468

Quoted 8 lines
kernel/bpf/cgroup.c +5 -0
@@ -2461,6 +2461,12 @@ cg_sockopt_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
  #endif
  	case BPF_FUNC_perf_event_output:
  		return &bpf_event_output_data_proto;
+
+	case BPF_FUNC_copy_from_user:
+		if (prog->aux->sleepable)
+			return &bpf_copy_from_user_proto;
+		return NULL;

If we just allow copy to/from, I'm not sure I understand how the buffer sharing between sleepable/non-sleepable works.

Let's assume I have two progs in the chain:
1. non-sleepable - copies the buffer, does some modifications; since we don't copy the buffer back after every prog run, the modifications stay in the kernel buffer
2. sleepable - runs and just gets the user pointer? does it mean this sleepable program doesn't see the changes from (1)?

Quoted 5 lines

IOW, do we need some custom sockopt copy_to/from that handle this potential buffer location transparently or am I missing something?

Assuming we want to support this at all. If we do, might deserve a selftest.

It is why BPF_SOCKOPT_FLAG_OPTVAL_USER is there.
It helps programs to make a right decision.
However, I am going to remove bpf_copy_from_user()
since we have bpf_so_optval_copy_to() and bpf_so_optval_copy_to_r(). Does it make sense to you?

#12 lore raw

Re: [RFC bpf-next v2 4/6] bpf: Provide bpf_copy_from_user() and bpf_copy_to_user().

Quoted 36 lines · Kui-Feng Lee
Quoted 33 lines · Stanislav Fomichev
Quoted 21 lines · On 08/10, [email protected] wrote:

From: Kui-Feng Lee <[email protected]>

Provide bpf_copy_from_user() and bpf_copy_to_user() to the BPF programs attached to cgroup/{set,get}sockopt. bpf_copy_to_user() is a new kfunc to copy data from an kernel space buffer to a user space buffer. They are only available for sleepable BPF programs. bpf_copy_to_user() is only available to the BPF programs attached to cgroup/getsockopt.

---
  kernel/bpf/cgroup.c  |  6 ++++++ kernel/bpf/helpers.c | 31 +++++++++++++++++++++++++++++++

  2 files changed, 37 insertions(+)
kernel/bpf/cgroup.c +0 -0
index 5bf3115b265c..c15a72860d2a 100644
@@ -2461,6 +2461,12 @@ cg_sockopt_func_proto(enum bpf_func_id 
 func_id, const struct bpf_prog *prog)
   #endif
Quoted 7 lines

      case BPF_FUNC_perf_event_output:
          return &bpf_event_output_data_proto;
+
+    case BPF_FUNC_copy_from_user:
+        if (prog->aux->sleepable)
+            return &bpf_copy_from_user_proto;
+        return NULL;

If we just allow copy to/from, I'm not sure I understand how the buffer sharing between sleepable/non-sleepable works.

Quoted 6 lines
Quoted 6 lines

Let's assume I have two progs in the chain:
1. non-sleepable - copies the buffer, does some modifications; since we don't copy the buffer back after every prog run, the modifications stay in the kernel buffer
2. sleepable - runs and just gets the user pointer? does it mean this sleepable program doesn't see the changes from (1)?

It is still visible from sleepable programs. Sleepable programs will receive a pointer to the buffer in the kernel. And, BPF_SOCKOPT_FLAG_OPTVAL_USER is clear.

Quoted 12 lines
Quoted 6 lines

IOW, do we need some custom sockopt copy_to/from that handle this potential buffer location transparently or am I missing something?

Assuming we want to support this at all. If we do, might deserve a selftest.

It is why BPF_SOCKOPT_FLAG_OPTVAL_USER is there.
It helps programs to make a right decision.
However, I am going to remove bpf_copy_from_user()
since we have bpf_so_optval_copy_to() and bpf_so_optval_copy_to_r(). Does it make sense to you?

#13 lore raw

Re: [RFC bpf-next v2 4/6] bpf: Provide bpf_copy_from_user() and bpf_copy_to_user().

Quoted 49 lines · Kui-Feng Lee
Quoted 39 lines · Kui-Feng Lee
Quoted 36 lines · Stanislav Fomichev
Quoted 24 lines · On 08/10, [email protected] wrote:

From: Kui-Feng Lee <[email protected]>

Provide bpf_copy_from_user() and bpf_copy_to_user() to the BPF programs attached to cgroup/{set,get}sockopt. bpf_copy_to_user() is a new kfunc to
copy data from an kernel space buffer to a user space buffer. They are only
available for sleepable BPF programs. bpf_copy_to_user() is only available
to the BPF programs attached to cgroup/getsockopt.

---
  kernel/bpf/cgroup.c  |  6 ++++++ kernel/bpf/helpers.c | 31 +++++++++++++++++++++++++++++++

  2 files changed, 37 insertions(+)
kernel/bpf/cgroup.c +0 -0
index 5bf3115b265c..c15a72860d2a 100644
@@ -2461,6 +2461,12 @@ cg_sockopt_func_proto(enum bpf_func_id
 func_id, const struct bpf_prog *prog)
   #endif
Quoted 7 lines

      case BPF_FUNC_perf_event_output:
          return &bpf_event_output_data_proto;
+
+    case BPF_FUNC_copy_from_user:
+        if (prog->aux->sleepable)
+            return &bpf_copy_from_user_proto;
+        return NULL;

If we just allow copy to/from, I'm not sure I understand how the buffer sharing between sleepable/non-sleepable works.

Quoted 6 lines
Quoted 6 lines

Let's assume I have two progs in the chain:
1. non-sleepable - copies the buffer, does some modifications; since we don't copy the buffer back after every prog run, the modifications stay in the kernel buffer
2. sleepable - runs and just gets the user pointer? does it mean this sleepable program doesn't see the changes from (1)?

Quoted 16 lines

It is still visible from sleepable programs. Sleepable programs will receive a pointer to the buffer in the kernel. And, BPF_SOCKOPT_FLAG_OPTVAL_USER is clear.

Quoted 12 lines
Quoted 6 lines

IOW, do we need some custom sockopt copy_to/from that handle this potential buffer location transparently or am I missing something?

Assuming we want to support this at all. If we do, might deserve a selftest.

It is why BPF_SOCKOPT_FLAG_OPTVAL_USER is there.
It helps programs to make a right decision.
However, I am going to remove bpf_copy_from_user()
since we have bpf_so_optval_copy_to() and bpf_so_optval_copy_to_r(). Does it make sense to you?

Ah, so that's where it's handled. I didn't read that far :-) In this case yes, let's have only those helpers.

Btw, do we also really need bpf_so_optval_copy_to_r? If we are doing dynptr, let's only have bpf_so_optval_copy_to version?

I'd also call them something like bpf_sockopt_copy_{to,from}. That "_so_optval_" is not super intuitive imho.

#14 lore raw

Re: [RFC bpf-next v2 4/6] bpf: Provide bpf_copy_from_user() and bpf_copy_to_user().

Quoted 67 lines · Stanislav Fomichev
Quoted 49 lines · Kui-Feng Lee
Quoted 39 lines · Kui-Feng Lee
Quoted 36 lines · Stanislav Fomichev
Quoted 22 lines · On 08/10, [email protected] wrote:

From: Kui-Feng Lee <[email protected]>

Provide bpf_copy_from_user() and bpf_copy_to_user() to the BPF programs attached to cgroup/{set,get}sockopt. bpf_copy_to_user() is a new kfunc to
copy data from an kernel space buffer to a user space buffer. They are only
available for sleepable BPF programs. bpf_copy_to_user() is only available
to the BPF programs attached to cgroup/getsockopt.

---
   kernel/bpf/cgroup.c  |  6 ++++++
   kernel/bpf/helpers.c | 31 +++++++++++++++++++++++++++++++
   2 files changed, 37 insertions(+)
kernel/bpf/cgroup.c +0 -0
index 5bf3115b265c..c15a72860d2a 100644

kernel/bpf/cgroup.c cg_sockopt_func_proto(enum bpf_func_id · line 2469

Quoted 9 lines
kernel/bpf/cgroup.c +5 -0
@@ -2461,6 +2461,12 @@ cg_sockopt_func_proto(enum bpf_func_id
 func_id, const struct bpf_prog *prog)
   #endif
       case BPF_FUNC_perf_event_output:
           return &bpf_event_output_data_proto;
+
+    case BPF_FUNC_copy_from_user:
+        if (prog->aux->sleepable)
+            return &bpf_copy_from_user_proto;
+        return NULL;

If we just allow copy to/from, I'm not sure I understand how the buffer sharing between sleepable/non-sleepable works.

Quoted 6 lines
Quoted 6 lines

Let's assume I have two progs in the chain:
1. non-sleepable - copies the buffer, does some modifications; since we don't copy the buffer back after every prog run, the modifications stay in the kernel buffer
2. sleepable - runs and just gets the user pointer? does it mean this sleepable program doesn't see the changes from (1)?

Quoted 16 lines

It is still visible from sleepable programs. Sleepable programs will receive a pointer to the buffer in the kernel. And, BPF_SOCKOPT_FLAG_OPTVAL_USER is clear.

Quoted 12 lines
Quoted 6 lines

IOW, do we need some custom sockopt copy_to/from that handle this potential buffer location transparently or am I missing something?

Assuming we want to support this at all. If we do, might deserve a selftest.

It is why BPF_SOCKOPT_FLAG_OPTVAL_USER is there.
It helps programs to make a right decision.
However, I am going to remove bpf_copy_from_user()
since we have bpf_so_optval_copy_to() and bpf_so_optval_copy_to_r(). Does it make sense to you?

Quoted 5 lines

Ah, so that's where it's handled. I didn't read that far :-) In this case yes, let's have only those helpers.

Btw, do we also really need bpf_so_optval_copy_to_r? If we are doing dynptr, let's only have bpf_so_optval_copy_to version?

Don't you think bpf_so_optval_copy_to_r() is handy to copy a simple string to the user space?

Quoted 3 lines

I'd also call them something like bpf_sockopt_copy_{to,from}. That "_so_optval_" is not super intuitive imho.

Agree!

#15 lore raw

Re: [RFC bpf-next v2 4/6] bpf: Provide bpf_copy_from_user() and bpf_copy_to_user().

Quoted 71 lines · Kui-Feng Lee
Quoted 67 lines · Stanislav Fomichev
Quoted 65 lines · Kui-Feng Lee
Quoted 39 lines · Kui-Feng Lee
Quoted 36 lines · Stanislav Fomichev
Quoted 22 lines · On 08/10, [email protected] wrote:

From: Kui-Feng Lee <[email protected]>

Provide bpf_copy_from_user() and bpf_copy_to_user() to the BPF programs attached to cgroup/{set,get}sockopt. bpf_copy_to_user() is a new kfunc to
copy data from an kernel space buffer to a user space buffer. They are only
available for sleepable BPF programs. bpf_copy_to_user() is only available
to the BPF programs attached to cgroup/getsockopt.

---
   kernel/bpf/cgroup.c  |  6 ++++++
   kernel/bpf/helpers.c | 31 +++++++++++++++++++++++++++++++
   2 files changed, 37 insertions(+)
kernel/bpf/cgroup.c +0 -0
index 5bf3115b265c..c15a72860d2a 100644

kernel/bpf/cgroup.c cg_sockopt_func_proto(enum bpf_func_id · line 2469

Quoted 9 lines
kernel/bpf/cgroup.c +5 -0
@@ -2461,6 +2461,12 @@ cg_sockopt_func_proto(enum bpf_func_id
 func_id, const struct bpf_prog *prog)
   #endif
       case BPF_FUNC_perf_event_output:
           return &bpf_event_output_data_proto;
+
+    case BPF_FUNC_copy_from_user:
+        if (prog->aux->sleepable)
+            return &bpf_copy_from_user_proto;
+        return NULL;

If we just allow copy to/from, I'm not sure I understand how the buffer sharing between sleepable/non-sleepable works.

Quoted 6 lines
Quoted 6 lines

Let's assume I have two progs in the chain:
1. non-sleepable - copies the buffer, does some modifications; since we don't copy the buffer back after every prog run, the modifications stay in the kernel buffer
2. sleepable - runs and just gets the user pointer? does it mean this sleepable program doesn't see the changes from (1)?

It is still visible from sleepable programs. Sleepable programs will receive a pointer to the buffer in the kernel. And, BPF_SOCKOPT_FLAG_OPTVAL_USER is clear.

Quoted 12 lines
Quoted 6 lines

IOW, do we need some custom sockopt copy_to/from that handle this potential buffer location transparently or am I missing something?

Assuming we want to support this at all. If we do, might deserve a selftest.

It is why BPF_SOCKOPT_FLAG_OPTVAL_USER is there.
It helps programs to make a right decision.
However, I am going to remove bpf_copy_from_user()
since we have bpf_so_optval_copy_to() and bpf_so_optval_copy_to_r(). Does it make sense to you?

Quoted 8 lines
Quoted 5 lines

Ah, so that's where it's handled. I didn't read that far :-) In this case yes, let's have only those helpers.

Btw, do we also really need bpf_so_optval_copy_to_r? If we are doing dynptr, let's only have bpf_so_optval_copy_to version?

Don't you think bpf_so_optval_copy_to_r() is handy to copy a simple string to the user space?

Yeah, it is convenient, but it feels like something we can avoid if we are using dynptrs (exclusively)?
Can the programs have a simple wrapper around bpf_so_optval_from+bpf_so_optval_copy_to to provide the same ptr-based api?

static inline void bpf_so_optval_copy_to_r(sopt, ptr, sz) {
bpf_so_optval_alloc(sopt, sz, &dynptr); some_dynptr_copy_to(&dynptr, ptr, siz); bpf_so_optval_copy_to(sopt, &dynptr); bpf_so_optval_release(&dynptr);
}

Or maybe we should instead have a new kfunc that turns sopt ptr+sz into a dynptr?

static inline void bpf_so_optval_copy_to_r(sopt, ptr, sz) {
bpf_so_optval_from_user_ptr(sopt, &dynptr); bpf_so_optval_copy_to(sopt, &dynptr); bpf_so_optval_release(&dynptr);
}

That probably fits better into dynptr world?

#16 lore raw

RFC v2 5/6 bpf: Add a new dynptr type for CGRUP_SOCKOPT.

In reply to [email protected]

From: Kui-Feng Lee <[email protected]>

The new dynptr type (BPF_DYNPTR_TYPE_CGROUP_SOCKOPT) will be used by BPF programs to create a buffer that can be installed on ctx to replace exisiting optval or user_optval.

Installation is only allowed if ctx->flags & BPF_SOCKOPT_FLAG_OPTVAL_REPLACE is true. It is enabled only for sleepable programs on the cgroup/setsockopt hook. BPF programs can install a new buffer holding by a dynptr to increase the size of optval passed to setsockopt().

Installation is not enabled for cgroup/getsockopt since you can not increased a buffer created, by user program, to return data from getsockopt().

---
 include/linux/bpf.h            |   7 +-
 include/uapi/linux/bpf.h       |   4 +
 kernel/bpf/btf.c               |   3 +
 kernel/bpf/cgroup.c            |   5 +-
 kernel/bpf/helpers.c           | 202 +++++++++++++++++++++++++++++++++
 kernel/bpf/verifier.c          |  47 +++++++-
 tools/include/uapi/linux/bpf.h |   4 +
 7 files changed, 268 insertions(+), 4 deletions(-)
include/linux/bpf.h +6 -1
index abe75063630b..618ca061f319 100644
@@ -663,12 +663,15 @@ enum bpf_type_flag {
 	/* DYNPTR points to xdp_buff */
 	DYNPTR_TYPE_XDP		= BIT(16 + BPF_BASE_TYPE_BITS),
 
+	/* DYNPTR points to optval buffer of bpf_sockopt */
+	DYNPTR_TYPE_CGROUP_SOCKOPT = BIT(17 + BPF_BASE_TYPE_BITS),
+
 	__BPF_TYPE_FLAG_MAX,
 	__BPF_TYPE_LAST_FLAG	= __BPF_TYPE_FLAG_MAX - 1,
 };
 
 #define DYNPTR_TYPE_FLAG_MASK	(DYNPTR_TYPE_LOCAL | DYNPTR_TYPE_RINGBUF | DYNPTR_TYPE_SKB \
-				 | DYNPTR_TYPE_XDP)
+				 | DYNPTR_TYPE_XDP | DYNPTR_TYPE_CGROUP_SOCKOPT)
 
 /* Max number of base types. */
 #define BPF_BASE_TYPE_LIMIT	(1UL << BPF_BASE_TYPE_BITS)
@@ -1206,6 +1209,8 @@ enum bpf_dynptr_type {
 	BPF_DYNPTR_TYPE_SKB,
 	/* Underlying data is a xdp_buff */
 	BPF_DYNPTR_TYPE_XDP,
+	/* Underlying data is for the optval of a cgroup sock */
+	BPF_DYNPTR_TYPE_CGROUP_SOCKOPT,
 };
 
 int bpf_dynptr_check_size(u32 size);
include/uapi/linux/bpf.h +4 -0
index fff6f7dff408..c648a7a2b985 100644
@@ -7145,6 +7145,10 @@ struct bpf_sockopt {
 enum bpf_sockopt_flags {
 	/* optval is a pointer to user space memory */
 	BPF_SOCKOPT_FLAG_OPTVAL_USER	= (1U << 0),
+	/* able to install new optval */
+	BPF_SOCKOPT_FLAG_OPTVAL_REPLACE	= (1U << 1),
+	/* optval is referenced by a dynptr */
+	BPF_SOCKOPT_FLAG_OPTVAL_DYNPTR	= (1U << 2),
 };
 
 struct bpf_pidns_info {
kernel/bpf/btf.c +3 -0
index 249657c466dd..6d6a040688be 100644
@@ -217,6 +217,7 @@ enum btf_kfunc_hook {
 	BTF_KFUNC_HOOK_SOCKET_FILTER,
 	BTF_KFUNC_HOOK_LWT,
 	BTF_KFUNC_HOOK_NETFILTER,
+	BTF_KFUNC_HOOK_CGROUP_SOCKOPT,
 	BTF_KFUNC_HOOK_MAX,
 };
 
@@ -7846,6 +7847,8 @@ static int bpf_prog_type_to_kfunc_hook(enum bpf_prog_type prog_type)
 		return BTF_KFUNC_HOOK_LWT;
 	case BPF_PROG_TYPE_NETFILTER:
 		return BTF_KFUNC_HOOK_NETFILTER;
+	case BPF_PROG_TYPE_CGROUP_SOCKOPT:
+		return BTF_KFUNC_HOOK_CGROUP_SOCKOPT;
 	default:
 		return BTF_KFUNC_HOOK_MAX;
 	}
kernel/bpf/cgroup.c +4 -1
index c15a72860d2a..196391c6716a 100644
@@ -1877,6 +1877,8 @@ static int filter_setsockopt_progs_cb(void *arg,
 	if (max_optlen < 0)
 		return max_optlen;
 
+	ctx->flags = BPF_SOCKOPT_FLAG_OPTVAL_REPLACE;
+
 	if (copy_from_user(ctx->optval, optval,
 			   min(ctx->optlen, max_optlen)) != 0)
 		return -EFAULT;
@@ -1905,7 +1907,8 @@ int __cgroup_bpf_run_filter_setsockopt(struct sock *sk, int *level,
 	ctx.optlen = *optlen;
 	ctx.optval = optval;
 	ctx.optval_end = optval + *optlen;
-	ctx.flags = BPF_SOCKOPT_FLAG_OPTVAL_USER;
+	ctx.flags = BPF_SOCKOPT_FLAG_OPTVAL_USER |
+		BPF_SOCKOPT_FLAG_OPTVAL_REPLACE;
 
 	lock_sock(sk);
 	ret = bpf_prog_run_array_cg_cb(&cgrp->bpf, CGROUP_SETSOCKOPT,
kernel/bpf/helpers.c +202 -0

238 lines not on this page · raw message

kernel/bpf/verifier.c +45 -2

107 lines not on this page · raw message

tools/include/uapi/linux/bpf.h +4 -0
index fff6f7dff408..c648a7a2b985 100644
@@ -7145,6 +7145,10 @@ struct bpf_sockopt {
 enum bpf_sockopt_flags {
 	/* optval is a pointer to user space memory */
 	BPF_SOCKOPT_FLAG_OPTVAL_USER	= (1U << 0),
+	/* able to install new optval */
+	BPF_SOCKOPT_FLAG_OPTVAL_REPLACE	= (1U << 1),
+	/* optval is referenced by a dynptr */
+	BPF_SOCKOPT_FLAG_OPTVAL_DYNPTR	= (1U << 2),
 };
 
 struct bpf_pidns_info {
Signature · 1 line
2.34.1
#17 lore raw

Re: [RFC bpf-next v2 5/6] bpf: Add a new dynptr type for CGRUP_SOCKOPT.

kernel test robot noticed the following build warnings:

Hi,

[This is a private test report for your RFC patch.] kernel test robot noticed the following build warnings:

[auto build test WARNING on bpf-next/master]

url:    https://github.com/intel-lab-lkp/linux/commits/thinker-li-gmail-com/bpf-enable-sleepable-BPF-programs-attached-to-cgroup-get-set-sockopt/20230811-123333
base:   https://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf-next.git master
patch link:    https://lore.kernel.org/r/20230811043127.1318152-6-thinker.li%40gmail.com
patch subject: [RFC bpf-next v2 5/6] bpf: Add a new dynptr type for CGRUP_SOCKOPT.
config: x86_64-randconfig-r081-20230814 (https://download.01.org/0day-ci/archive/20230814/[email protected]/config)
compiler: gcc-12 (Debian 12.2.0-14) 12.2.0
reproduce: (https://download.01.org/0day-ci/archive/20230814/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

sparse warnings: (new ones prefixed by >>)
kernel/bpf/helpers.c:684:32: sparse: sparse: incorrect type in argument 1 (different address spaces) @@ expected void [noderef] __user *to @@ got void *dst__uninit @@
kernel/bpf/helpers.c:684:32: sparse: expected void [noderef] __user *to
kernel/bpf/helpers.c:684:32: sparse: got void *dst__uninit

>> kernel/bpf/helpers.c:2588:55: sparse: sparse: incorrect type in argument 2 (different address spaces) @@     expected void const [noderef] __user *from @@     got unsigned char [usertype] *optval @@
   kernel/bpf/helpers.c:2588:55: sparse:     expected void const [noderef] __user *from
   kernel/bpf/helpers.c:2588:55: sparse:     got unsigned char [usertype] *optval
>> kernel/bpf/helpers.c:2619:45: sparse: sparse: incorrect type in argument 1 (different address spaces) @@     expected void [noderef] __user *to @@     got unsigned char [usertype] *optval @@

kernel/bpf/helpers.c:2619:45: sparse: expected void [noderef] __user *to
kernel/bpf/helpers.c:2619:45: sparse: got unsigned char [usertype] *optval
kernel/bpf/helpers.c:2446:18: sparse: sparse: context imbalance in 'bpf_rcu_read_lock' - wrong count at exit kernel/bpf/helpers.c: note: in included file (through include/linux/workqueue.h, include/linux/bpf.h):
include/linux/rcupdate.h:780:9: sparse: sparse: context imbalance in 'bpf_rcu_read_unlock' - unexpected unlock

vim +2588 kernel/bpf/helpers.c

2548
2549 /* Initialize a sockopt dynptr from a user or installed optval pointer.
2550 *
2551 * sopt->optval can be a user pointer or a kernel pointer. A kernel pointer
2552 * can be a buffer allocated by the caller of the BPF program or a buffer
2553 * installed by other BPF programs through bpf_so_optval_install().
2554 *
2555 * Atmost one dynptr shall be created by this function at any moment, or
2556 * it will return -EINVAL. You can create another dypptr by this function
2557 * after release the previous one by bpf_so_optval_release().
2558 *
2559 * A dynptr that is initialized when optval is a user pointer is an
2560 * exception. In this case, the dynptr will point to a kernel buffer with
2561 * the same content as the user buffer. To simplify the code, users should
2562 * always make sure having only one dynptr initialized by this function at
2563 * any moment.
2564 */
2565 __bpf_kfunc int bpf_so_optval_from(struct bpf_sockopt *sopt,
2566 struct bpf_dynptr_kern *ptr__uninit,
2567 unsigned int size)
2568 {
2569 struct bpf_sockopt_kern *sopt_kern = (struct bpf_sockopt_kern *)sopt;
2570 int err;
2571
2572 bpf_dynptr_set_null(ptr__uninit);
2573
2574 if (size > (sopt_kern->optval_end - sopt_kern->optval))
2575 return -EINVAL;
2576
2577 if (size == 0)
2578 size = min(sopt_kern->optlen,
2579 (int)(sopt_kern->optval_end - sopt_kern->optval));
2580
2581 if (sopt_kern->flags & BPF_SOCKOPT_FLAG_OPTVAL_DYNPTR)
2582 return -EINVAL;
2583
2584 if (sopt_kern->flags & BPF_SOCKOPT_FLAG_OPTVAL_USER) {
2585 err = bpf_so_optval_alloc(sopt, sopt_kern->optlen, ptr__uninit);
2586 if (err >= 0)
2587 err = copy_from_user(ptr__uninit->data,

> 2588						     sopt_kern->optval,
  2589						     size);
  2590			return err;
  2591		}
  2592	
  2593		bpf_dynptr_init(ptr__uninit, sopt_kern->optval,
  2594				BPF_DYNPTR_TYPE_CGROUP_SOCKOPT, 0,
  2595				size);
  2596		sopt_kern->flags |= BPF_SOCKOPT_FLAG_OPTVAL_DYNPTR;
  2597	
  2598		return size;
  2599	}
  2600	
  2601	/**
  2602	 * int bpf_so_optval_copy_to_r(struct bpf_sockopt *sopt,
  2603	 *                             void *ptr, u32 ptr__sz)
  2604	 *     Description
  2605	 *             Copy data from *ptr* to *sopt->optval*.
  2606	 *     Return
  2607	 *             >= 0 on success, or a negative error in case of failure.
  2608	 */
  2609	__bpf_kfunc int bpf_so_optval_copy_to_r(struct bpf_sockopt *sopt,
  2610						void *ptr, u32 ptr__sz)
  2611	{
  2612		struct bpf_sockopt_kern *sopt_kern = (struct bpf_sockopt_kern *)sopt;
  2613		int ret;
  2614	
  2615		if (ptr__sz > (sopt_kern->optval_end - sopt_kern->optval))
  2616			return -EINVAL;
  2617	
  2618		if (sopt_kern->flags & BPF_SOCKOPT_FLAG_OPTVAL_USER) {
> 2619			ret = copy_to_user(sopt_kern->optval, ptr,

2620 ptr__sz);
2621 if (unlikely(ret))
2622 return -EFAULT;
2623 } else {
2624 /* Use memmove() in case of optval & ptr overlap. */
2625 memmove(sopt_kern->optval, ptr, ptr__sz);
2626 ret = ptr__sz;
2627 }
2628
2629 return ret;
2630 }
2631

Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
#18 lore raw

RFC v2 6/6 bpf: Add test cases for sleepable BPF programs of the CGROUP_SOCKOPT type.

In reply to [email protected]

From: Kui-Feng Lee <[email protected]>

Do the same test as non-sleepable ones.

---
 .../testing/selftests/bpf/bpf_experimental.h  |  44 ++++
 tools/testing/selftests/bpf/bpf_kfuncs.h      |  47 ++++
 .../selftests/bpf/prog_tests/sockopt_sk.c     |  34 ++-
 .../testing/selftests/bpf/progs/sockopt_sk.c  | 242 ++++++++++++++++++
 .../selftests/bpf/verifier/sleepable.c        |   2 +-
 5 files changed, 366 insertions(+), 3 deletions(-)
tools/testing/selftests/bpf/bpf_experimental.h +44 -0
index 209811b1993a..553c60818996 100644
@@ -131,4 +131,48 @@ extern int bpf_rbtree_add_impl(struct bpf_rb_root *root, struct bpf_rb_node *nod
  */
 extern struct bpf_rb_node *bpf_rbtree_first(struct bpf_rb_root *root) __ksym;
 
+/*
+ *     Description
+ *             Copy data from *ptr* to *sopt->optval*.
+ *     Return
+ *             >= 0 on success, or a negative error in case of failure.
+ */
+extern int bpf_so_optval_copy_to_r(struct bpf_sockopt *sopt,
+				   void *ptr, u32 ptr__sz) __ksym;
+/*
+ *     Description
+ *             Copy data from *ptr* to *sopt->optval*.
+ *     Return
+ *             >= 0 on success, or a negative error in case of failure.
+ */
+extern int bpf_so_optval_copy_to(struct bpf_sockopt *sopt,
+				 struct bpf_dynptr *ptr) __ksym;
+
+/* Description
+ *	Allocate a buffer of 'size' bytes for being installed as optval.
+ * Returns
+ *	> 0 on success, the size of the allocated buffer
+ *	-ENOMEM or -EINVAL on failure
+ */
+extern int bpf_so_optval_alloc(struct bpf_sockopt *sopt, int size,
+			       struct bpf_dynptr *ptr__uninit) __ksym;
+
+/* Description
+ *	Install the buffer pointed to by 'ptr' as optval.
+ * Returns
+ *	0 on success
+ *	-EINVAL if the buffer is too small
+ */
+extern int bpf_so_optval_install(struct bpf_sockopt *sopt,
+				 struct bpf_dynptr *ptr) __ksym;
+
+/* Description
+ *	Release the buffer allocated by bpf_so_optval_alloc.
+ * Returns
+ *	0 on success
+ *	-EINVAL if the buffer was not allocated by bpf_so_optval_alloc
+ */
+extern int bpf_so_optval_release(struct bpf_sockopt *sopt,
+				 struct bpf_dynptr *ptr) __ksym;
+
 #endif
tools/testing/selftests/bpf/bpf_kfuncs.h +47 -0

51 lines not on this page · raw message

tools/testing/selftests/bpf/prog_tests/sockopt_sk.c +32 -2
index 05d0e07da394..e18a40d89860 100644
@@ -92,6 +92,7 @@ static int getsetsockopt(void)
 	}
 	if (buf.u8[0] != 0x01) {
 		log_err("Unexpected buf[0] 0x%02x != 0x01", buf.u8[0]);
+		log_err("optlen %d", optlen);
 		goto err;
 	}
 
@@ -220,7 +221,7 @@ static int getsetsockopt(void)
 	return -1;
 }
 
-static void run_test(int cgroup_fd)
+static void run_test_nonsleepable(int cgroup_fd)
 {
 	struct sockopt_sk *skel;
 
@@ -246,6 +247,32 @@ static void run_test(int cgroup_fd)
 	sockopt_sk__destroy(skel);
 }
 
+static void run_test_sleepable(int cgroup_fd)
+{
+	struct sockopt_sk *skel;
+
+	skel = sockopt_sk__open_and_load();
+	if (!ASSERT_OK_PTR(skel, "skel_load"))
+		goto cleanup;
+
+	skel->bss->page_size = getpagesize();
+
+	skel->links._setsockopt_s =
+		bpf_program__attach_cgroup(skel->progs._setsockopt_s, cgroup_fd);
+	if (!ASSERT_OK_PTR(skel->links._setsockopt_s, "setsockopt_link"))
+		goto cleanup;
+
+	skel->links._getsockopt_s =
+		bpf_program__attach_cgroup(skel->progs._getsockopt_s, cgroup_fd);
+	if (!ASSERT_OK_PTR(skel->links._getsockopt_s, "getsockopt_link"))
+		goto cleanup;
+
+	ASSERT_OK(getsetsockopt(), "getsetsockopt");
+
+cleanup:
+	sockopt_sk__destroy(skel);
+}
+
 void test_sockopt_sk(void)
 {
 	int cgroup_fd;
@@ -254,6 +281,9 @@ void test_sockopt_sk(void)
 	if (!ASSERT_GE(cgroup_fd, 0, "join_cgroup /sockopt_sk"))
 		return;
 
-	run_test(cgroup_fd);
+	if (test__start_subtest("nonsleepable"))
+		run_test_nonsleepable(cgroup_fd);
+	if (test__start_subtest("sleepable"))
+		run_test_sleepable(cgroup_fd);
 	close(cgroup_fd);
 }
tools/testing/selftests/bpf/progs/sockopt_sk.c +242 -0

257 lines not on this page · raw message

tools/testing/selftests/bpf/verifier/sleepable.c +1 -1
index 1f0d2bdc673f..4b6c1117ec9f 100644
@@ -85,7 +85,7 @@
 	.expected_attach_type = BPF_TRACE_RAW_TP,
 	.kfunc = "sched_switch",
 	.result = REJECT,
-	.errstr = "Only fentry/fexit/fmod_ret, lsm, iter, uprobe, and struct_ops programs can be sleepable",
+	.errstr = "Only fentry/fexit/fmod_ret, lsm, iter, uprobe, cgroup, and struct_ops programs can be sleepable",
 	.flags = BPF_F_SLEEPABLE,
 	.runs = -1,
 },
Signature · 1 line
2.34.1