Skip to content

Commit e5dff25

Browse files
AsphalttKernel Patches Daemon
authored andcommitted
bpf: Refactor reporting btf_log_true_size for btf_load
In the next commit, it will be able to report logs via extended common attributes, which will report 'log_true_size' via the extended common attributes meanwhile. Therefore, refactor the way of 'btf_log_true_size' reporting in order to report 'log_true_size' via the extended common attributes easily. Signed-off-by: Leon Hwang <[email protected]>
1 parent b6d3656 commit e5dff25

File tree

3 files changed

+25
-24
lines changed

3 files changed

+25
-24
lines changed

include/linux/btf.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ const char *btf_get_name(const struct btf *btf);
145145
void btf_get(struct btf *btf);
146146
void btf_put(struct btf *btf);
147147
const struct btf_header *btf_header(const struct btf *btf);
148-
int btf_new_fd(const union bpf_attr *attr, bpfptr_t uattr, u32 uattr_sz);
148+
int btf_new_fd(union bpf_attr *attr, bpfptr_t uattr);
149149
struct btf *btf_get_by_fd(int fd);
150150
int btf_get_info_by_fd(const struct btf *btf,
151151
const union bpf_attr *attr,

kernel/bpf/btf.c

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5745,22 +5745,7 @@ static int btf_check_type_tags(struct btf_verifier_env *env,
57455745
return 0;
57465746
}
57475747

5748-
static int finalize_log(struct bpf_verifier_log *log, bpfptr_t uattr, u32 uattr_size)
5749-
{
5750-
u32 log_true_size;
5751-
int err;
5752-
5753-
err = bpf_vlog_finalize(log, &log_true_size);
5754-
5755-
if (uattr_size >= offsetofend(union bpf_attr, btf_log_true_size) &&
5756-
copy_to_bpfptr_offset(uattr, offsetof(union bpf_attr, btf_log_true_size),
5757-
&log_true_size, sizeof(log_true_size)))
5758-
err = -EFAULT;
5759-
5760-
return err;
5761-
}
5762-
5763-
static struct btf *btf_parse(const union bpf_attr *attr, bpfptr_t uattr, u32 uattr_size)
5748+
static struct btf *btf_parse(union bpf_attr *attr, bpfptr_t uattr)
57645749
{
57655750
bpfptr_t btf_data = make_bpfptr(attr->btf, uattr.is_kernel);
57665751
char __user *log_ubuf = u64_to_user_ptr(attr->btf_log_buf);
@@ -5841,7 +5826,7 @@ static struct btf *btf_parse(const union bpf_attr *attr, bpfptr_t uattr, u32 uat
58415826
}
58425827
}
58435828

5844-
err = finalize_log(&env->log, uattr, uattr_size);
5829+
err = bpf_vlog_finalize(&env->log, &attr->btf_log_true_size);
58455830
if (err)
58465831
goto errout_free;
58475832

@@ -5853,7 +5838,7 @@ static struct btf *btf_parse(const union bpf_attr *attr, bpfptr_t uattr, u32 uat
58535838
btf_free_struct_meta_tab(btf);
58545839
errout:
58555840
/* overwrite err with -ENOSPC or -EFAULT */
5856-
ret = finalize_log(&env->log, uattr, uattr_size);
5841+
ret = bpf_vlog_finalize(&env->log, &attr->btf_log_true_size);
58575842
if (ret)
58585843
err = ret;
58595844
errout_free:
@@ -8017,12 +8002,12 @@ static int __btf_new_fd(struct btf *btf)
80178002
return anon_inode_getfd("btf", &btf_fops, btf, O_RDONLY | O_CLOEXEC);
80188003
}
80198004

8020-
int btf_new_fd(const union bpf_attr *attr, bpfptr_t uattr, u32 uattr_size)
8005+
int btf_new_fd(union bpf_attr *attr, bpfptr_t uattr)
80218006
{
80228007
struct btf *btf;
80238008
int ret;
80248009

8025-
btf = btf_parse(attr, uattr, uattr_size);
8010+
btf = btf_parse(attr, uattr);
80268011
if (IS_ERR(btf))
80278012
return PTR_ERR(btf);
80288013

kernel/bpf/syscall.c

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5397,7 +5397,7 @@ static int bpf_obj_get_info_by_fd(const union bpf_attr *attr,
53975397

53985398
#define BPF_BTF_LOAD_LAST_FIELD btf_token_fd
53995399

5400-
static int bpf_btf_load(const union bpf_attr *attr, bpfptr_t uattr, __u32 uattr_size)
5400+
static int bpf_btf_load(union bpf_attr *attr, bpfptr_t uattr)
54015401
{
54025402
struct bpf_token *token = NULL;
54035403

@@ -5424,7 +5424,7 @@ static int bpf_btf_load(const union bpf_attr *attr, bpfptr_t uattr, __u32 uattr_
54245424

54255425
bpf_token_put(token);
54265426

5427-
return btf_new_fd(attr, uattr, uattr_size);
5427+
return btf_new_fd(attr, uattr);
54285428
}
54295429

54305430
#define BPF_BTF_GET_FD_BY_ID_LAST_FIELD fd_by_id_token_fd
@@ -6151,6 +6151,19 @@ static int copy_prog_load_log_true_size(union bpf_attr *attr, bpfptr_t uattr, un
61516151
return 0;
61526152
}
61536153

6154+
static int copy_btf_load_log_true_size(union bpf_attr *attr, bpfptr_t uattr, unsigned int size)
6155+
{
6156+
if (!attr->btf_log_true_size)
6157+
return 0;
6158+
6159+
if (size >= offsetofend(union bpf_attr, btf_log_true_size) &&
6160+
copy_to_bpfptr_offset(uattr, offsetof(union bpf_attr, btf_log_true_size),
6161+
&attr->btf_log_true_size, sizeof(attr->btf_log_true_size)))
6162+
return -EFAULT;
6163+
6164+
return 0;
6165+
}
6166+
61546167
static int __sys_bpf(enum bpf_cmd cmd, bpfptr_t uattr, unsigned int size,
61556168
bpfptr_t uattr_common, unsigned int size_common)
61566169
{
@@ -6257,7 +6270,10 @@ static int __sys_bpf(enum bpf_cmd cmd, bpfptr_t uattr, unsigned int size,
62576270
err = bpf_raw_tracepoint_open(&attr);
62586271
break;
62596272
case BPF_BTF_LOAD:
6260-
err = bpf_btf_load(&attr, uattr, size);
6273+
attr.btf_log_true_size = 0;
6274+
err = bpf_btf_load(&attr, uattr);
6275+
ret = copy_btf_load_log_true_size(&attr, uattr, size);
6276+
err = ret ? ret : err;
62616277
break;
62626278
case BPF_BTF_GET_FD_BY_ID:
62636279
err = bpf_btf_get_fd_by_id(&attr);

0 commit comments

Comments
 (0)