@@ -4115,6 +4115,8 @@ static const char *core_relo_kind_str(enum bpf_core_relo_kind kind)
41154115 case BPF_TYPE_ID_TARGET : return "target_type_id" ;
41164116 case BPF_TYPE_EXISTS : return "type_exists" ;
41174117 case BPF_TYPE_SIZE : return "type_size" ;
4118+ case BPF_ENUMVAL_EXISTS : return "enumval_exists" ;
4119+ case BPF_ENUMVAL_VALUE : return "enumval_value" ;
41184120 default : return "unknown" ;
41194121 }
41204122}
@@ -4147,6 +4149,17 @@ static bool core_relo_is_type_based(enum bpf_core_relo_kind kind)
41474149 }
41484150}
41494151
4152+ static bool core_relo_is_enumval_based (enum bpf_core_relo_kind kind )
4153+ {
4154+ switch (kind ) {
4155+ case BPF_ENUMVAL_EXISTS :
4156+ case BPF_ENUMVAL_VALUE :
4157+ return true;
4158+ default :
4159+ return false;
4160+ }
4161+ }
4162+
41504163/*
41514164 * Turn bpf_core_relo into a low- and high-level spec representation,
41524165 * validating correctness along the way, as well as calculating resulting
@@ -4180,6 +4193,9 @@ static bool core_relo_is_type_based(enum bpf_core_relo_kind kind)
41804193 * Type-based relocations (TYPE_EXISTS/TYPE_SIZE,
41814194 * TYPE_ID_LOCAL/TYPE_ID_TARGET) don't capture any field information. Their
41824195 * spec and raw_spec are kept empty.
4196+ *
4197+ * Enum value-based relocations (ENUMVAL_EXISTS/ENUMVAL_VALUE) use access
4198+ * string to specify enumerator's value index that need to be relocated.
41834199 */
41844200static int bpf_core_parse_spec (const struct btf * btf ,
41854201 __u32 type_id ,
@@ -4224,16 +4240,25 @@ static int bpf_core_parse_spec(const struct btf *btf,
42244240 if (spec -> raw_len == 0 )
42254241 return - EINVAL ;
42264242
4227- /* first spec value is always reloc type array index */
42284243 t = skip_mods_and_typedefs (btf , type_id , & id );
42294244 if (!t )
42304245 return - EINVAL ;
42314246
42324247 access_idx = spec -> raw_spec [0 ];
4233- spec -> spec [0 ].type_id = id ;
4234- spec -> spec [0 ].idx = access_idx ;
4248+ acc = & spec -> spec [0 ];
4249+ acc -> type_id = id ;
4250+ acc -> idx = access_idx ;
42354251 spec -> len ++ ;
42364252
4253+ if (core_relo_is_enumval_based (relo_kind )) {
4254+ if (!btf_is_enum (t ) || spec -> raw_len > 1 || access_idx >= btf_vlen (t ))
4255+ return - EINVAL ;
4256+
4257+ /* record enumerator name in a first accessor */
4258+ acc -> name = btf__name_by_offset (btf , btf_enum (t )[access_idx ].name_off );
4259+ return 0 ;
4260+ }
4261+
42374262 if (!core_relo_is_field_based (relo_kind ))
42384263 return - EINVAL ;
42394264
@@ -4676,6 +4701,39 @@ static int bpf_core_spec_match(struct bpf_core_spec *local_spec,
46764701 local_acc = & local_spec -> spec [0 ];
46774702 targ_acc = & targ_spec -> spec [0 ];
46784703
4704+ if (core_relo_is_enumval_based (local_spec -> relo_kind )) {
4705+ size_t local_essent_len , targ_essent_len ;
4706+ const struct btf_enum * e ;
4707+ const char * targ_name ;
4708+
4709+ /* has to resolve to an enum */
4710+ targ_type = skip_mods_and_typedefs (targ_spec -> btf , targ_id , & targ_id );
4711+ if (!btf_is_enum (targ_type ))
4712+ return 0 ;
4713+
4714+ local_essent_len = bpf_core_essential_name_len (local_acc -> name );
4715+
4716+ for (i = 0 , e = btf_enum (targ_type ); i < btf_vlen (targ_type ); i ++ , e ++ ) {
4717+ targ_name = btf__name_by_offset (targ_spec -> btf , e -> name_off );
4718+ targ_essent_len = bpf_core_essential_name_len (targ_name );
4719+ if (targ_essent_len != local_essent_len )
4720+ continue ;
4721+ if (strncmp (local_acc -> name , targ_name , local_essent_len ) == 0 ) {
4722+ targ_acc -> type_id = targ_id ;
4723+ targ_acc -> idx = i ;
4724+ targ_acc -> name = targ_name ;
4725+ targ_spec -> len ++ ;
4726+ targ_spec -> raw_spec [targ_spec -> raw_len ] = targ_acc -> idx ;
4727+ targ_spec -> raw_len ++ ;
4728+ return 1 ;
4729+ }
4730+ }
4731+ return 0 ;
4732+ }
4733+
4734+ if (!core_relo_is_field_based (local_spec -> relo_kind ))
4735+ return - EINVAL ;
4736+
46794737 for (i = 0 ; i < local_spec -> len ; i ++ , local_acc ++ , targ_acc ++ ) {
46804738 targ_type = skip_mods_and_typedefs (targ_spec -> btf , targ_id ,
46814739 & targ_id );
@@ -4880,6 +4938,31 @@ static int bpf_core_calc_type_relo(const struct bpf_core_relo *relo,
48804938 return 0 ;
48814939}
48824940
4941+ static int bpf_core_calc_enumval_relo (const struct bpf_core_relo * relo ,
4942+ const struct bpf_core_spec * spec ,
4943+ __u32 * val )
4944+ {
4945+ const struct btf_type * t ;
4946+ const struct btf_enum * e ;
4947+
4948+ switch (relo -> kind ) {
4949+ case BPF_ENUMVAL_EXISTS :
4950+ * val = spec ? 1 : 0 ;
4951+ break ;
4952+ case BPF_ENUMVAL_VALUE :
4953+ if (!spec )
4954+ return - EUCLEAN ; /* request instruction poisoning */
4955+ t = btf__type_by_id (spec -> btf , spec -> spec [0 ].type_id );
4956+ e = btf_enum (t ) + spec -> spec [0 ].idx ;
4957+ * val = e -> val ;
4958+ break ;
4959+ default :
4960+ return - EOPNOTSUPP ;
4961+ }
4962+
4963+ return 0 ;
4964+ }
4965+
48834966struct bpf_core_relo_res
48844967{
48854968 /* expected value in the instruction, unless validate == false */
@@ -4918,6 +5001,9 @@ static int bpf_core_calc_relo(const struct bpf_program *prog,
49185001 } else if (core_relo_is_type_based (relo -> kind )) {
49195002 err = bpf_core_calc_type_relo (relo , local_spec , & res -> orig_val );
49205003 err = err ?: bpf_core_calc_type_relo (relo , targ_spec , & res -> new_val );
5004+ } else if (core_relo_is_enumval_based (relo -> kind )) {
5005+ err = bpf_core_calc_enumval_relo (relo , local_spec , & res -> orig_val );
5006+ err = err ?: bpf_core_calc_enumval_relo (relo , targ_spec , & res -> new_val );
49215007 }
49225008
49235009 if (err == - EUCLEAN ) {
@@ -4954,6 +5040,11 @@ static void bpf_core_poison_insn(struct bpf_program *prog, int relo_idx,
49545040 insn -> imm = 195896080 ; /* => 0xbad2310 => "bad relo" */
49555041}
49565042
5043+ static bool is_ldimm64 (struct bpf_insn * insn )
5044+ {
5045+ return insn -> code == (BPF_LD | BPF_IMM | BPF_DW );
5046+ }
5047+
49575048/*
49585049 * Patch relocatable BPF instruction.
49595050 *
@@ -4966,6 +5057,7 @@ static void bpf_core_poison_insn(struct bpf_program *prog, int relo_idx,
49665057 * Currently three kinds of BPF instructions are supported:
49675058 * 1. rX = <imm> (assignment with immediate operand);
49685059 * 2. rX += <imm> (arithmetic operations with immediate operand);
5060+ * 3. rX = <imm64> (load with 64-bit immediate value).
49695061 */
49705062static int bpf_core_patch_insn (struct bpf_program * prog ,
49715063 const struct bpf_core_relo * relo ,
@@ -4984,6 +5076,11 @@ static int bpf_core_patch_insn(struct bpf_program *prog,
49845076 class = BPF_CLASS (insn -> code );
49855077
49865078 if (res -> poison ) {
5079+ /* poison second part of ldimm64 to avoid confusing error from
5080+ * verifier about "unknown opcode 00"
5081+ */
5082+ if (is_ldimm64 (insn ))
5083+ bpf_core_poison_insn (prog , relo_idx , insn_idx + 1 , insn + 1 );
49875084 bpf_core_poison_insn (prog , relo_idx , insn_idx , insn );
49885085 return 0 ;
49895086 }
@@ -5012,7 +5109,7 @@ static int bpf_core_patch_insn(struct bpf_program *prog,
50125109 case BPF_ST :
50135110 case BPF_STX :
50145111 if (res -> validate && insn -> off != orig_val ) {
5015- pr_warn ("prog '%s': relo #%d: unexpected insn #%d (LD/ LDX/ST/STX) value: got %u, exp %u -> %u\n" ,
5112+ pr_warn ("prog '%s': relo #%d: unexpected insn #%d (LDX/ST/STX) value: got %u, exp %u -> %u\n" ,
50165113 bpf_program__title (prog , false), relo_idx ,
50175114 insn_idx , insn -> off , orig_val , new_val );
50185115 return - EINVAL ;
@@ -5029,8 +5126,36 @@ static int bpf_core_patch_insn(struct bpf_program *prog,
50295126 bpf_program__title (prog , false), relo_idx , insn_idx ,
50305127 orig_val , new_val );
50315128 break ;
5129+ case BPF_LD : {
5130+ __u64 imm ;
5131+
5132+ if (!is_ldimm64 (insn ) ||
5133+ insn [0 ].src_reg != 0 || insn [0 ].off != 0 ||
5134+ insn_idx + 1 >= prog -> insns_cnt ||
5135+ insn [1 ].code != 0 || insn [1 ].dst_reg != 0 ||
5136+ insn [1 ].src_reg != 0 || insn [1 ].off != 0 ) {
5137+ pr_warn ("prog '%s': relo #%d: insn #%d (LDIMM64) has unexpected form\n" ,
5138+ bpf_program__title (prog , false), relo_idx , insn_idx );
5139+ return - EINVAL ;
5140+ }
5141+
5142+ imm = insn [0 ].imm + ((__u64 )insn [1 ].imm << 32 );
5143+ if (res -> validate && imm != orig_val ) {
5144+ pr_warn ("prog '%s': relo #%d: unexpected insn #%d (LDIMM64) value: got %llu, exp %u -> %u\n" ,
5145+ bpf_program__title (prog , false), relo_idx ,
5146+ insn_idx , imm , orig_val , new_val );
5147+ return - EINVAL ;
5148+ }
5149+
5150+ insn [0 ].imm = new_val ;
5151+ insn [1 ].imm = 0 ; /* currently only 32-bit values are supported */
5152+ pr_debug ("prog '%s': relo #%d: patched insn #%d (LDIMM64) imm64 %llu -> %u\n" ,
5153+ bpf_program__title (prog , false), relo_idx , insn_idx ,
5154+ imm , new_val );
5155+ break ;
5156+ }
50325157 default :
5033- pr_warn ("prog '%s': relo #%d: trying to relocate unrecognized insn #%d, code:%x, src:%x, dst:%x, off:%x, imm:%x\n" ,
5158+ pr_warn ("prog '%s': relo #%d: trying to relocate unrecognized insn #%d, code:0x %x, src:0x %x, dst:0x %x, off:0x %x, imm:0x %x\n" ,
50345159 bpf_program__title (prog , false), relo_idx ,
50355160 insn_idx , insn -> code , insn -> src_reg , insn -> dst_reg ,
50365161 insn -> off , insn -> imm );
@@ -5047,6 +5172,7 @@ static int bpf_core_patch_insn(struct bpf_program *prog,
50475172static void bpf_core_dump_spec (int level , const struct bpf_core_spec * spec )
50485173{
50495174 const struct btf_type * t ;
5175+ const struct btf_enum * e ;
50505176 const char * s ;
50515177 __u32 type_id ;
50525178 int i ;
@@ -5060,6 +5186,15 @@ static void bpf_core_dump_spec(int level, const struct bpf_core_spec *spec)
50605186 if (core_relo_is_type_based (spec -> relo_kind ))
50615187 return ;
50625188
5189+ if (core_relo_is_enumval_based (spec -> relo_kind )) {
5190+ t = skip_mods_and_typedefs (spec -> btf , type_id , NULL );
5191+ e = btf_enum (t ) + spec -> raw_spec [0 ];
5192+ s = btf__name_by_offset (spec -> btf , e -> name_off );
5193+
5194+ libbpf_print (level , "::%s = %u" , s , e -> val );
5195+ return ;
5196+ }
5197+
50635198 if (core_relo_is_field_based (spec -> relo_kind )) {
50645199 for (i = 0 ; i < spec -> len ; i ++ ) {
50655200 if (spec -> spec [i ].name )
0 commit comments