Skip to content

Commit 667a86a

Browse files
Alexei Starovoitovanakryiko
authored andcommitted
bpf: Disasm support for addr_space_cast instruction.
LLVM generates rX = addr_space_cast(rY, dst_addr_space, src_addr_space) instruction when pointers in non-zero address space are used by the bpf program. Recognize this insn in uapi and in bpf disassembler. Signed-off-by: Alexei Starovoitov <ast@kernel.org> Signed-off-by: Andrii Nakryiko <andrii@kernel.org> Acked-by: Kumar Kartikeya Dwivedi <memxor@gmail.com> Link: https://lore.kernel.org/bpf/20240308010812.89848-3-alexei.starovoitov@gmail.com
1 parent 3174603 commit 667a86a

3 files changed

Lines changed: 18 additions & 0 deletions

File tree

include/uapi/linux/bpf.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1339,6 +1339,10 @@ enum {
13391339
*/
13401340
#define BPF_PSEUDO_KFUNC_CALL 2
13411341

1342+
enum bpf_addr_space_cast {
1343+
BPF_ADDR_SPACE_CAST = 1,
1344+
};
1345+
13421346
/* flags for BPF_MAP_UPDATE_ELEM command */
13431347
enum {
13441348
BPF_ANY = 0, /* create new element or update existing */

kernel/bpf/disasm.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,12 @@ static bool is_movsx(const struct bpf_insn *insn)
166166
(insn->off == 8 || insn->off == 16 || insn->off == 32);
167167
}
168168

169+
static bool is_addr_space_cast(const struct bpf_insn *insn)
170+
{
171+
return insn->code == (BPF_ALU64 | BPF_MOV | BPF_X) &&
172+
insn->off == BPF_ADDR_SPACE_CAST;
173+
}
174+
169175
void print_bpf_insn(const struct bpf_insn_cbs *cbs,
170176
const struct bpf_insn *insn,
171177
bool allow_ptr_leaks)
@@ -184,6 +190,10 @@ void print_bpf_insn(const struct bpf_insn_cbs *cbs,
184190
insn->code, class == BPF_ALU ? 'w' : 'r',
185191
insn->dst_reg, class == BPF_ALU ? 'w' : 'r',
186192
insn->dst_reg);
193+
} else if (is_addr_space_cast(insn)) {
194+
verbose(cbs->private_data, "(%02x) r%d = addr_space_cast(r%d, %d, %d)\n",
195+
insn->code, insn->dst_reg,
196+
insn->src_reg, ((u32)insn->imm) >> 16, (u16)insn->imm);
187197
} else if (BPF_SRC(insn->code) == BPF_X) {
188198
verbose(cbs->private_data, "(%02x) %c%d %s %s%c%d\n",
189199
insn->code, class == BPF_ALU ? 'w' : 'r',

tools/include/uapi/linux/bpf.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1339,6 +1339,10 @@ enum {
13391339
*/
13401340
#define BPF_PSEUDO_KFUNC_CALL 2
13411341

1342+
enum bpf_addr_space_cast {
1343+
BPF_ADDR_SPACE_CAST = 1,
1344+
};
1345+
13421346
/* flags for BPF_MAP_UPDATE_ELEM command */
13431347
enum {
13441348
BPF_ANY = 0, /* create new element or update existing */

0 commit comments

Comments
 (0)