Skip to contentSkip to conversation

bpf: Support 64-bit pointers to kfuncs

PATCH Superseded Ilya Leoshkevich 3 participants 5 messages Apr 5, 2023 – Apr 6, 2023

Subsystems (4)

v6 of 7 Superseded Latest is v7 →

lore
Share & tools
lore

Ilya Leoshkevich

author maintainer
#0 lore raw

PATCH v6 1/1 bpf: Support 64-bit pointers to kfuncs

test_ksyms_module fails to emit a kfunc call targeting a module on s390x, because the verifier stores the difference between kfunc address and __bpf_call_base in bpf_insn.imm, which is s32, and modules are roughly (1 << 42) bytes away from the kernel on s390x.

Fix by keeping BTF id in bpf_insn.imm for BPF_PSEUDO_KFUNC_CALLs, and storing the absolute address in bpf_kfunc_desc.

Introduce bpf_jit_supports_far_kfunc_call() in order to limit this new behavior to the s390x JIT. Otherwise other JITs need to be modified, which is not desired.

Introduce bpf_get_kfunc_addr() instead of exposing both find_kfunc_desc() and struct bpf_kfunc_desc.

In addition to sorting kfuncs by imm, also sort them by offset, in order to handle conflicting imms from different modules. Do this on all architectures in order to simplify code.

Factor out resolving specialized kfuncs (XPD and dynptr) from fixup_kfunc_call(). This was required in the first place, because fixup_kfunc_call() uses find_kfunc_desc(), which returns a const pointer, so it's not possible to modify kfunc addr without stripping const, which is not nice. It also removes repetition of code like:

	if (bpf_jit_supports_far_kfunc_call())
		desc->addr = func;
	else
		insn->imm = BPF_CALL_IMM(func);

and separates kfunc_desc_tab fixups from kfunc_call fixups.

---

v5: https://lore.kernel.org/bpf/[email protected]/
v5 -> v6: Fix build with !CONFIG_BPF_SYSCALL by moving bpf_get_kfunc_addr()
declaration outside of the respective ifdef.

v4: https://lore.kernel.org/bpf/[email protected]/
v4 -> v5: Fix issues identified by Andrii:
- Improve bpf_get_kfunc_addr() argument naming.
- Do overflow check only in !bpf_jit_supports_far_kfunc_call() case.
- Fix kfunc_desc_cmp_by_imm_off() bug when passing huge values.
- Update fixup_kfunc_call() comment to reflect the new logic.

v3: https://lore.kernel.org/bpf/[email protected]/
v3 -> v4: Use Jiri's proposal and make it work on s390x.

 arch/s390/net/bpf_jit_comp.c |   5 ++
 include/linux/bpf.h          |   3 +
 include/linux/filter.h       |   1 +
 kernel/bpf/core.c            |  11 +++
 kernel/bpf/verifier.c        | 137 +++++++++++++++++++++++++----------
 5 files changed, 117 insertions(+), 40 deletions(-)
arch/s390/net/bpf_jit_comp.c +5 -0
index d0846ba818ee..7102e4b674a0 100644
@@ -2001,6 +2001,11 @@ bool bpf_jit_supports_kfunc_call(void)
 	return true;
 }
 
+bool bpf_jit_supports_far_kfunc_call(void)
+{
+	return true;
+}
+
 int bpf_arch_text_poke(void *ip, enum bpf_text_poke_type t,
 		       void *old_addr, void *new_addr)
 {
include/linux/bpf.h +3 -0
index 002a811b6b90..37115c729a0e 100644
@@ -3035,4 +3035,7 @@ static inline gfp_t bpf_memcg_flags(gfp_t flags)
 	return flags;
 }
 
+int bpf_get_kfunc_addr(const struct bpf_prog *prog, u32 func_id,
+		       u16 btf_fd_idx, u8 **func_addr);
+
 #endif /* _LINUX_BPF_H */
include/linux/filter.h +1 -0
index 5364b0c52c1d..bbce89937fde 100644
@@ -920,6 +920,7 @@ void bpf_jit_compile(struct bpf_prog *prog);
 bool bpf_jit_needs_zext(void);
 bool bpf_jit_supports_subprog_tailcalls(void);
 bool bpf_jit_supports_kfunc_call(void);
+bool bpf_jit_supports_far_kfunc_call(void);
 bool bpf_helper_changes_pkt_data(void *func);
 
 static inline bool bpf_dump_raw_ok(const struct cred *cred)
kernel/bpf/core.c +11 -0
index b297e9f60ca1..7a75fdfd707e 100644
@@ -1187,6 +1187,7 @@ int bpf_jit_get_func_addr(const struct bpf_prog *prog,
 	s16 off = insn->off;
 	s32 imm = insn->imm;
 	u8 *addr;
+	int err;
 
 	*func_addr_fixed = insn->src_reg != BPF_PSEUDO_CALL;
 	if (!*func_addr_fixed) {
@@ -1201,6 +1202,11 @@ int bpf_jit_get_func_addr(const struct bpf_prog *prog,
 			addr = (u8 *)prog->aux->func[off]->bpf_func;
 		else
 			return -EINVAL;
+	} else if (insn->src_reg == BPF_PSEUDO_KFUNC_CALL &&
+		   bpf_jit_supports_far_kfunc_call()) {
+		err = bpf_get_kfunc_addr(prog, insn->imm, insn->off, &addr);
+		if (err)
+			return err;
 	} else {
 		/* Address of a BPF helper call. Since part of the core
 		 * kernel, it's always at a fixed location. __bpf_call_base
@@ -2732,6 +2738,11 @@ bool __weak bpf_jit_supports_kfunc_call(void)
 	return false;
 }
 
+bool __weak bpf_jit_supports_far_kfunc_call(void)
+{
+	return false;
+}
+
 /* To execute LD_ABS/LD_IND instructions __bpf_prog_run() may call
  * skb_copy_bits(), so provide a weak definition of it for NET-less config.
  */
kernel/bpf/verifier.c +97 -40

240 lines not on this page · raw message

Signature · 1 line
2.39.2
#1 lore raw
kernel test robot noticed the following build errors:

Hi Ilya,

kernel test robot noticed the following build errors:

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

url:    https://github.com/intel-lab-lkp/linux/commits/Ilya-Leoshkevich/bpf-Support-64-bit-pointers-to-kfuncs/20230406-053713
base:   https://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf-next.git master
patch link:    https://lore.kernel.org/r/20230405213453.49756-1-iii%40linux.ibm.com
patch subject: [PATCH bpf-next v6] bpf: Support 64-bit pointers to kfuncs
config: i386-randconfig-c001-20230403 (https://download.01.org/0day-ci/archive/20230406/[email protected]/config)
compiler: gcc-11 (Debian 11.3.0-8) 11.3.0

reproduce (this is a W=1 build):
# https://github.com/intel-lab-lkp/linux/commit/2a9559efd98d24493ac5c889a3ae03dd66b0de26
git remote add linux-review https://github.com/intel-lab-lkp/linux
git fetch --no-tags linux-review Ilya-Leoshkevich/bpf-Support-64-bit-pointers-to-kfuncs/20230406-053713 git checkout 2a9559efd98d24493ac5c889a3ae03dd66b0de26
# save the config file
mkdir build_dir && cp config build_dir/.config
make W=1 O=build_dir ARCH=i386 olddefconfig
make W=1 O=build_dir ARCH=i386 SHELL=/bin/bash

If you fix the issue, kindly add following tag where applicable
| Reported-by: kernel test robot <[email protected]>
| Link: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All errors (new ones prefixed by >>):

ld: kernel/bpf/core.o: in function `bpf_jit_get_func_addr':

>> kernel/bpf/core.c:1207: undefined reference to `bpf_get_kfunc_addr'

vim +1207 kernel/bpf/core.c

1182
1183 int bpf_jit_get_func_addr(const struct bpf_prog *prog,
1184 const struct bpf_insn *insn, bool extra_pass,
1185 u64 *func_addr, bool *func_addr_fixed)
1186 {
1187 s16 off = insn->off;
1188 s32 imm = insn->imm;
1189 u8 *addr;
1190 int err;
1191
1192 *func_addr_fixed = insn->src_reg != BPF_PSEUDO_CALL;
1193 if (!*func_addr_fixed) {
1194 /* Place-holder address till the last pass has collected
1195 * all addresses for JITed subprograms in which case we
1196 * can pick them up from prog->aux.
1197 */
1198 if (!extra_pass)
1199 addr = NULL;
1200 else if (prog->aux->func &&
1201 off >= 0 && off < prog->aux->func_cnt)
1202 addr = (u8 *)prog->aux->func[off]->bpf_func;
1203 else
1204 return -EINVAL;
1205 } else if (insn->src_reg == BPF_PSEUDO_KFUNC_CALL &&
1206 bpf_jit_supports_far_kfunc_call()) {

> 1207			err = bpf_get_kfunc_addr(prog, insn->imm, insn->off, &addr);

1208 if (err)
1209 return err;
1210 } else {
1211 /* Address of a BPF helper call. Since part of the core
1212 * kernel, it's always at a fixed location. __bpf_call_base
1213 * and the helper with imm relative to it are both in core
1214 * kernel.
1215 */
1216 addr = (u8 *)__bpf_call_base + imm;
1217 }
1218
1219 *func_addr = (unsigned long)addr;
1220 return 0;
1221 }
1222

Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests
#2 lore raw
In reply to Ilya Leoshkevich

On Wed, Apr 05, 2023 at 11:34:53PM +0200, Ilya Leoshkevich wrote:

SNIP

Quoted 21 lines
kernel/bpf/verifier.c +13 -0
+int bpf_get_kfunc_addr(const struct bpf_prog *prog, u32 func_id,
+		       u16 btf_fd_idx, u8 **func_addr)
+{
+	const struct bpf_kfunc_desc *desc;
+
+	desc = find_kfunc_desc(prog, func_id, btf_fd_idx);
+	if (!desc)
+		return -EFAULT;
+
+	*func_addr = (u8 *)desc->addr;
+	return 0;
+}
+
 static struct btf *__find_kfunc_desc_btf(struct bpf_verifier_env *env,
 					 s16 offset)
 {
@@ -2672,14 +2691,19 @@ static int add_kfunc_call(struct bpf_verifier_env *env, u32 func_id, s16 offset)
 		return -EINVAL;
 	}
 

kernel/bpf/verifier.c static int add_kfunc_call(struct bpf_verifier_env *env, u32 func_id, s16 offset) · line 2697

Quoted 10 lines
kernel/bpf/verifier.c +4 -6
-	call_imm = BPF_CALL_IMM(addr);
-	/* Check whether or not the relative offset overflows desc->imm */
-	if ((unsigned long)(s32)call_imm != call_imm) {
-		verbose(env, "address of kernel function %s is out of range\n",
-			func_name);
-		return -EINVAL;
+	if (bpf_jit_supports_far_kfunc_call()) {
+		call_imm = func_id;
+	} else {
+		call_imm = BPF_CALL_IMM(addr);

we compute call_imm again in fixup_kfunc_call, seems like we could store the address and the func_id in desc and have fixup_kfunc_call do the insn->imm setup

kernel/bpf/verifier.c static int add_kfunc_call(struct bpf_verifier_env *env, u32 func_id, s16 offset) · line 2706

Quoted 9 lines
kernel/bpf/verifier.c +7 -0
+		/* Check whether the relative offset overflows desc->imm */
+		if ((unsigned long)(s32)call_imm != call_imm) {
+			verbose(env, "address of kernel function %s is out of range\n",
+				func_name);
+			return -EINVAL;
+		}
 	}
 
+

nit, extra line

Quoted 32 lines
kernel/bpf/verifier.c +6 -5
 	if (bpf_dev_bound_kfunc_id(func_id)) {
 		err = bpf_dev_bound_kfunc_check(&env->log, prog_aux);
 		if (err)
@@ -2690,6 +2714,7 @@ static int add_kfunc_call(struct bpf_verifier_env *env, u32 func_id, s16 offset)
 	desc->func_id = func_id;
 	desc->imm = call_imm;
 	desc->offset = offset;
+	desc->addr = addr;
 	err = btf_distill_func_proto(&env->log, desc_btf,
 				     func_proto, func_name,
 				     &desc->func_model);
@@ -2699,19 +2724,19 @@ static int add_kfunc_call(struct bpf_verifier_env *env, u32 func_id, s16 offset)
 	return err;
 }
 
-static int kfunc_desc_cmp_by_imm(const void *a, const void *b)
+static int kfunc_desc_cmp_by_imm_off(const void *a, const void *b)
 {
 	const struct bpf_kfunc_desc *d0 = a;
 	const struct bpf_kfunc_desc *d1 = b;
 
-	if (d0->imm > d1->imm)
-		return 1;
-	else if (d0->imm < d1->imm)
-		return -1;
+	if (d0->imm != d1->imm)
+		return d0->imm < d1->imm ? -1 : 1;
+	if (d0->offset != d1->offset)
+		return d0->offset < d1->offset ? -1 : 1;
 	return 0;
 }
 

SNIP

Quoted 25 lines
kernel/bpf/verifier.c +25 -0
+/* replace a generic kfunc with a specialized version if necessary */
+static void fixup_kfunc_desc(struct bpf_verifier_env *env,
+			     struct bpf_kfunc_desc *desc)
+{
+	struct bpf_prog *prog = env->prog;
+	u32 func_id = desc->func_id;
+	u16 offset = desc->offset;
+	bool seen_direct_write;
+	void *xdp_kfunc;
+	bool is_rdonly;
+
+	if (bpf_dev_bound_kfunc_id(func_id)) {
+		xdp_kfunc = bpf_dev_bound_resolve_kfunc(prog, func_id);
+		if (xdp_kfunc) {
+			desc->addr = (unsigned long)xdp_kfunc;
+			return;
+		}
+		/* fallback to default kfunc when not supported by netdev */
+	}
+
+	if (offset)
+		return;
+
+	if (func_id == special_kfunc_list[KF_bpf_dynptr_from_skb]) {
+		seen_direct_write = env->seen_direct_write;

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

Quoted 10 lines
kernel/bpf/verifier.c +10 -0
+		is_rdonly = !may_access_direct_pkt_data(env, NULL, BPF_WRITE);
+
+		if (is_rdonly)
+			desc->addr = (unsigned long)bpf_dynptr_from_skb_rdonly;
+
+		/* restore env->seen_direct_write to its original value, since
+		 * may_access_direct_pkt_data mutates it
+		 */
+		env->seen_direct_write = seen_direct_write;
+	}

could we do this directly in add_kfunc_call?

thanks,
jirka

Quoted 93 lines
kernel/bpf/verifier.c +22 -26
+}
+
+static void fixup_kfunc_desc_tab(struct bpf_verifier_env *env)
+{
+	struct bpf_kfunc_desc_tab *tab = env->prog->aux->kfunc_tab;
+	u32 i;
+
+	if (!tab)
+		return;
+
+	for (i = 0; i < tab->nr_descs; i++)
+		fixup_kfunc_desc(env, &tab->descs[i]);
+}
+
 static int fixup_kfunc_call(struct bpf_verifier_env *env, struct bpf_insn *insn,
 			    struct bpf_insn *insn_buf, int insn_idx, int *cnt)
 {
 	const struct bpf_kfunc_desc *desc;
-	void *xdp_kfunc;
 
 	if (!insn->imm) {
 		verbose(env, "invalid kernel function call not eliminated in verifier pass\n");
@@ -17355,18 +17429,9 @@ static int fixup_kfunc_call(struct bpf_verifier_env *env, struct bpf_insn *insn,
 
 	*cnt = 0;
 
-	if (bpf_dev_bound_kfunc_id(insn->imm)) {
-		xdp_kfunc = bpf_dev_bound_resolve_kfunc(env->prog, insn->imm);
-		if (xdp_kfunc) {
-			insn->imm = BPF_CALL_IMM(xdp_kfunc);
-			return 0;
-		}
-
-		/* fallback to default kfunc when not supported by netdev */
-	}
-
-	/* insn->imm has the btf func_id. Replace it with
-	 * an address (relative to __bpf_call_base).
+	/* insn->imm has the btf func_id. Replace it with an offset relative to
+	 * __bpf_call_base, unless the JIT needs to call functions that are
+	 * further than 32 bits away (bpf_jit_supports_far_kfunc_call()).
 	 */
 	desc = find_kfunc_desc(env->prog, insn->imm, insn->off);
 	if (!desc) {
@@ -17375,7 +17440,8 @@ static int fixup_kfunc_call(struct bpf_verifier_env *env, struct bpf_insn *insn,
 		return -EFAULT;
 	}
 
-	insn->imm = desc->imm;
+	if (!bpf_jit_supports_far_kfunc_call())
+		insn->imm = BPF_CALL_IMM(desc->addr);
 	if (insn->off)
 		return 0;
 	if (desc->func_id == special_kfunc_list[KF_bpf_obj_new_impl]) {
@@ -17400,17 +17466,6 @@ static int fixup_kfunc_call(struct bpf_verifier_env *env, struct bpf_insn *insn,
 		   desc->func_id == special_kfunc_list[KF_bpf_rdonly_cast]) {
 		insn_buf[0] = BPF_MOV64_REG(BPF_REG_0, BPF_REG_1);
 		*cnt = 1;
-	} else if (desc->func_id == special_kfunc_list[KF_bpf_dynptr_from_skb]) {
-		bool seen_direct_write = env->seen_direct_write;
-		bool is_rdonly = !may_access_direct_pkt_data(env, NULL, BPF_WRITE);
-
-		if (is_rdonly)
-			insn->imm = BPF_CALL_IMM(bpf_dynptr_from_skb_rdonly);
-
-		/* restore env->seen_direct_write to its original value, since
-		 * may_access_direct_pkt_data mutates it
-		 */
-		env->seen_direct_write = seen_direct_write;
 	}
 	return 0;
 }
@@ -17433,6 +17488,8 @@ static int do_misc_fixups(struct bpf_verifier_env *env)
 	struct bpf_map *map_ptr;
 	int i, ret, cnt, delta = 0;
 
+	fixup_kfunc_desc_tab(env);
+
 	for (i = 0; i < insn_cnt; i++, insn++) {
 		/* Make divide-by-zero exceptions impossible. */
 		if (insn->code == (BPF_ALU64 | BPF_MOD | BPF_X) ||
@@ -17940,7 +17997,7 @@ static int do_misc_fixups(struct bpf_verifier_env *env)
 		}
 	}
 
-	sort_kfunc_descs_by_imm(env->prog);
+	sort_kfunc_descs_by_imm_off(env->prog);
 
 	return 0;
 }
Signature · 1 line
2.39.2
author maintainer
#3 lore raw
Quoted 40 lines · Jiri Olsa

On Wed, Apr 05, 2023 at 11:34:53PM +0200, Ilya Leoshkevich wrote:

SNIP

Quoted 26 lines

kernel/bpf/verifier.c +13 -0
+int bpf_get_kfunc_addr(const struct bpf_prog *prog, u32 func_id,
+                      u16 btf_fd_idx, u8 **func_addr)
+{
+       const struct bpf_kfunc_desc *desc;
+
+       desc = find_kfunc_desc(prog, func_id, btf_fd_idx);
+       if (!desc)
+               return -EFAULT;
+
+       *func_addr = (u8 *)desc->addr;
+       return 0;
+}
+

 static struct btf *__find_kfunc_desc_btf(struct bpf_verifier_env *env,
                                         s16 offset)
 {

kernel/bpf/verifier.c +0 -0
@@ -2672,14 +2691,19 @@ static int add_kfunc_call(struct
 bpf_verifier_env *env, u32 func_id, s16 offset)
                 return -EINVAL;
         }


-       call_imm = BPF_CALL_IMM(addr);
-       /* Check whether or not the relative offset overflows desc-

Quoted 1 line

imm */

Quoted 9 lines

-       if ((unsigned long)(s32)call_imm != call_imm) { -               verbose(env, "address of kernel function %s is out of range\n",
-                       func_name);
-               return -EINVAL;
+       if (bpf_jit_supports_far_kfunc_call()) {
+               call_imm = func_id;
+       } else {
+               call_imm = BPF_CALL_IMM(addr);

Quoted 4 lines

we compute call_imm again in fixup_kfunc_call, seems like we could store
the address and the func_id in desc and have fixup_kfunc_call do the insn->imm setup

We can drop this diff in fixup_kfunc_call():

- insn->imm = desc->imm;
+ if (!bpf_jit_supports_far_kfunc_call())
+ insn->imm = BPF_CALL_IMM(desc->addr);

in order to avoid duplicating the imm calculation logic, but I'm not sure if we want to move the entire desc->imm setup there.

For example, fixup_kfunc_call() considers kfunc_tab const, which is a nice property that I think is worth keeping.

Another option would be to drop desc->imm, but having it is very convenient for doing lookups the same way on all architectures.

Quoted 12 lines
Quoted 11 lines

+               /* Check whether the relative offset overflows desc->imm */
+               if ((unsigned long)(s32)call_imm != call_imm) { +                       verbose(env, "address of kernel function %s is out of range\n",
+                               func_name);
+                       return -EINVAL;
+               }
        }

+

Quoted 1 line

nit, extra line

Ouch. Thanks for spotting this.

Quoted 75 lines
Quoted 36 lines

        if (bpf_dev_bound_kfunc_id(func_id)) { err = bpf_dev_bound_kfunc_check(&env->log, prog_aux);
                if (err)

kernel/bpf/verifier.c +1 -0
@@ -2690,6 +2714,7 @@ static int add_kfunc_call(struct
 bpf_verifier_env *env, u32 func_id, s16 offset)
         desc->func_id = func_id;
         desc->imm = call_imm;
         desc->offset = offset;
+       desc->addr = addr;
         err = btf_distill_func_proto(&env->log, desc_btf,
                                      func_proto, func_name,

                                     &desc->func_model);

kernel/bpf/verifier.c +0 -0
@@ -2699,19 +2724,19 @@ static int add_kfunc_call(struct
 bpf_verifier_env *env, u32 func_id, s16 offset)
         return err;
  }

kernel/bpf/verifier.c +1 -1
-static int kfunc_desc_cmp_by_imm(const void *a, const void *b)
+static int kfunc_desc_cmp_by_imm_off(const void *a, const void *b)

 {
        const struct bpf_kfunc_desc *d0 = a; const struct bpf_kfunc_desc *d1 = b;

-       if (d0->imm > d1->imm)
-               return 1;
-       else if (d0->imm < d1->imm)
-               return -1;
+       if (d0->imm != d1->imm)
+               return d0->imm < d1->imm ? -1 : 1; +       if (d0->offset != d1->offset)
+               return d0->offset < d1->offset ? -1 : 1; return 0;
 }

SNIP

Quoted 36 lines

+/* replace a generic kfunc with a specialized version if necessary */

kernel/bpf/verifier.c +12 -0
+static void fixup_kfunc_desc(struct bpf_verifier_env *env,
+                            struct bpf_kfunc_desc *desc)
+{
+       struct bpf_prog *prog = env->prog;
+       u32 func_id = desc->func_id;
+       u16 offset = desc->offset;
+       bool seen_direct_write;
+       void *xdp_kfunc;
+       bool is_rdonly;
+
+       if (bpf_dev_bound_kfunc_id(func_id)) {
+               xdp_kfunc = bpf_dev_bound_resolve_kfunc(prog,

func_id);
+               if (xdp_kfunc) {
+                       desc->addr = (unsigned long)xdp_kfunc; +                       return;
+               }
+               /* fallback to default kfunc when not supported by netdev */
+       }

kernel/bpf/verifier.c +5 -0
+
+       if (offset)
+               return;
+
+       if (func_id == special_kfunc_list[KF_bpf_dynptr_from_skb])

{
+               seen_direct_write = env->seen_direct_write; +               is_rdonly = !may_access_direct_pkt_data(env, NULL, BPF_WRITE);
+
+               if (is_rdonly)
+                       desc->addr = (unsigned long)bpf_dynptr_from_skb_rdonly;
+

Quoted 8 lines
Quoted 6 lines

+               /* restore env->seen_direct_write to its original value, since
+                * may_access_direct_pkt_data mutates it +                */
+               env->seen_direct_write = seen_direct_write; +       }

could we do this directly in add_kfunc_call?

Initially I thought that it wasn't possible, because may_access_direct_pkt_data() may depend on data gathered during verification. But on a second look that's simply not the case, so this code can indeed be moved to add_kfunc_call().

Quoted 3 lines

thanks,
jirka

[...]

#4 lore raw
Quoted 52 lines · Ilya Leoshkevich
Quoted 40 lines · Jiri Olsa

On Wed, Apr 05, 2023 at 11:34:53PM +0200, Ilya Leoshkevich wrote:

SNIP

Quoted 26 lines

kernel/bpf/verifier.c +13 -0
+int bpf_get_kfunc_addr(const struct bpf_prog *prog, u32 func_id,
+                      u16 btf_fd_idx, u8 **func_addr)
+{
+       const struct bpf_kfunc_desc *desc;
+
+       desc = find_kfunc_desc(prog, func_id, btf_fd_idx);
+       if (!desc)
+               return -EFAULT;
+
+       *func_addr = (u8 *)desc->addr;
+       return 0;
+}
+

 static struct btf *__find_kfunc_desc_btf(struct bpf_verifier_env *env,
                                         s16 offset)
 {

kernel/bpf/verifier.c +0 -0
@@ -2672,14 +2691,19 @@ static int add_kfunc_call(struct
 bpf_verifier_env *env, u32 func_id, s16 offset)
                 return -EINVAL;
         }


-       call_imm = BPF_CALL_IMM(addr);
-       /* Check whether or not the relative offset overflows desc-

Quoted 1 line

imm */

Quoted 9 lines

-       if ((unsigned long)(s32)call_imm != call_imm) { -               verbose(env, "address of kernel function %s is out of range\n",
-                       func_name);
-               return -EINVAL;
+       if (bpf_jit_supports_far_kfunc_call()) {
+               call_imm = func_id;
+       } else {
+               call_imm = BPF_CALL_IMM(addr);

Quoted 4 lines

we compute call_imm again in fixup_kfunc_call, seems like we could store
the address and the func_id in desc and have fixup_kfunc_call do the insn->imm setup

We can drop this diff in fixup_kfunc_call():

kernel/bpf/verifier.c +2 -1
-       insn->imm = desc->imm;
+       if (!bpf_jit_supports_far_kfunc_call())
+               insn->imm = BPF_CALL_IMM(desc->addr);
Quoted 8 lines

in order to avoid duplicating the imm calculation logic, but I'm not sure if we want to move the entire desc->imm setup there.

For example, fixup_kfunc_call() considers kfunc_tab const, which is a nice property that I think is worth keeping.

Another option would be to drop desc->imm, but having it is very convenient for doing lookups the same way on all architectures.

ok, I see.. so should we do following in fixup_kfunc_call:

	if (!bpf_jit_supports_far_kfunc_call())
		insn->imm = desc->imm;

by default there's func_id in insn->imm

jirka

Quoted 111 lines
Quoted 13 lines
Quoted 12 lines

+               /* Check whether the relative offset overflows desc->imm */
+               if ((unsigned long)(s32)call_imm != call_imm) { +                       verbose(env, "address of kernel function %s is out of range\n",
+                               func_name);
+                       return -EINVAL;
+               }
        }

+

Quoted 1 line

nit, extra line

Ouch. Thanks for spotting this.

Quoted 75 lines
Quoted 36 lines

        if (bpf_dev_bound_kfunc_id(func_id)) { err = bpf_dev_bound_kfunc_check(&env->log, prog_aux);
                if (err)

kernel/bpf/verifier.c +1 -0
@@ -2690,6 +2714,7 @@ static int add_kfunc_call(struct
 bpf_verifier_env *env, u32 func_id, s16 offset)
         desc->func_id = func_id;
         desc->imm = call_imm;
         desc->offset = offset;
+       desc->addr = addr;
         err = btf_distill_func_proto(&env->log, desc_btf,
                                      func_proto, func_name,

                                     &desc->func_model);

kernel/bpf/verifier.c +0 -0
@@ -2699,19 +2724,19 @@ static int add_kfunc_call(struct
 bpf_verifier_env *env, u32 func_id, s16 offset)
         return err;
  }

kernel/bpf/verifier.c +1 -1
-static int kfunc_desc_cmp_by_imm(const void *a, const void *b)
+static int kfunc_desc_cmp_by_imm_off(const void *a, const void *b)

 {
        const struct bpf_kfunc_desc *d0 = a; const struct bpf_kfunc_desc *d1 = b;

-       if (d0->imm > d1->imm)
-               return 1;
-       else if (d0->imm < d1->imm)
-               return -1;
+       if (d0->imm != d1->imm)
+               return d0->imm < d1->imm ? -1 : 1; +       if (d0->offset != d1->offset)
+               return d0->offset < d1->offset ? -1 : 1; return 0;
 }

SNIP

Quoted 36 lines

+/* replace a generic kfunc with a specialized version if necessary */

kernel/bpf/verifier.c +12 -0
+static void fixup_kfunc_desc(struct bpf_verifier_env *env,
+                            struct bpf_kfunc_desc *desc)
+{
+       struct bpf_prog *prog = env->prog;
+       u32 func_id = desc->func_id;
+       u16 offset = desc->offset;
+       bool seen_direct_write;
+       void *xdp_kfunc;
+       bool is_rdonly;
+
+       if (bpf_dev_bound_kfunc_id(func_id)) {
+               xdp_kfunc = bpf_dev_bound_resolve_kfunc(prog,

func_id);
+               if (xdp_kfunc) {
+                       desc->addr = (unsigned long)xdp_kfunc; +                       return;
+               }
+               /* fallback to default kfunc when not supported by netdev */
+       }

kernel/bpf/verifier.c +5 -0
+
+       if (offset)
+               return;
+
+       if (func_id == special_kfunc_list[KF_bpf_dynptr_from_skb])

{
+               seen_direct_write = env->seen_direct_write; +               is_rdonly = !may_access_direct_pkt_data(env, NULL, BPF_WRITE);
+
+               if (is_rdonly)
+                       desc->addr = (unsigned long)bpf_dynptr_from_skb_rdonly;
+

Quoted 8 lines
Quoted 6 lines

+               /* restore env->seen_direct_write to its original value, since
+                * may_access_direct_pkt_data mutates it +                */
+               env->seen_direct_write = seen_direct_write; +       }

could we do this directly in add_kfunc_call?

Initially I thought that it wasn't possible, because may_access_direct_pkt_data() may depend on data gathered during verification. But on a second look that's simply not the case, so this code can indeed be moved to add_kfunc_call().

Quoted 3 lines

thanks,
jirka

[...]