Skip to content

Commit c9f766b

Browse files
committed
Merge branch 'bpf-obj-name-misc'
Martin KaFai Lau says: ==================== bpf: Misc improvements and a new usage on bpf obj name The first two patches make improvements on the bpf obj name. The last patch adds the prog name to kallsyms. ==================== Signed-off-by: David S. Miller <[email protected]>
2 parents f192970 + 368211f commit c9f766b

File tree

5 files changed

+27
-15
lines changed

5 files changed

+27
-15
lines changed

include/linux/bpf.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ struct bpf_map {
5656
struct work_struct work;
5757
atomic_t usercnt;
5858
struct bpf_map *inner_map_meta;
59-
u8 name[BPF_OBJ_NAME_LEN];
59+
char name[BPF_OBJ_NAME_LEN];
6060
};
6161

6262
/* function argument constraints */
@@ -189,7 +189,7 @@ struct bpf_prog_aux {
189189
struct bpf_prog *prog;
190190
struct user_struct *user;
191191
u64 load_time; /* ns since boottime */
192-
u8 name[BPF_OBJ_NAME_LEN];
192+
char name[BPF_OBJ_NAME_LEN];
193193
union {
194194
struct work_struct work;
195195
struct rcu_head rcu;

include/uapi/linux/bpf.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ union bpf_attr {
230230
__u32 numa_node; /* numa node (effective only if
231231
* BPF_F_NUMA_NODE is set).
232232
*/
233-
__u8 map_name[BPF_OBJ_NAME_LEN];
233+
char map_name[BPF_OBJ_NAME_LEN];
234234
};
235235

236236
struct { /* anonymous struct used by BPF_MAP_*_ELEM commands */
@@ -253,7 +253,7 @@ union bpf_attr {
253253
__aligned_u64 log_buf; /* user supplied buffer */
254254
__u32 kern_version; /* checked when prog_type=kprobe */
255255
__u32 prog_flags;
256-
__u8 prog_name[BPF_OBJ_NAME_LEN];
256+
char prog_name[BPF_OBJ_NAME_LEN];
257257
};
258258

259259
struct { /* anonymous struct used by BPF_OBJ_* commands */
@@ -888,7 +888,7 @@ struct bpf_prog_info {
888888
__u32 created_by_uid;
889889
__u32 nr_map_ids;
890890
__aligned_u64 map_ids;
891-
__u8 name[BPF_OBJ_NAME_LEN];
891+
char name[BPF_OBJ_NAME_LEN];
892892
} __attribute__((aligned(8)));
893893

894894
struct bpf_map_info {
@@ -898,7 +898,7 @@ struct bpf_map_info {
898898
__u32 value_size;
899899
__u32 max_entries;
900900
__u32 map_flags;
901-
__u8 name[BPF_OBJ_NAME_LEN];
901+
char name[BPF_OBJ_NAME_LEN];
902902
} __attribute__((aligned(8)));
903903

904904
/* User bpf_sock_ops struct to access socket values and specify request ops

kernel/bpf/core.c

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -309,12 +309,25 @@ bpf_get_prog_addr_region(const struct bpf_prog *prog,
309309

310310
static void bpf_get_prog_name(const struct bpf_prog *prog, char *sym)
311311
{
312+
const char *end = sym + KSYM_NAME_LEN;
313+
312314
BUILD_BUG_ON(sizeof("bpf_prog_") +
313-
sizeof(prog->tag) * 2 + 1 > KSYM_NAME_LEN);
315+
sizeof(prog->tag) * 2 +
316+
/* name has been null terminated.
317+
* We should need +1 for the '_' preceding
318+
* the name. However, the null character
319+
* is double counted between the name and the
320+
* sizeof("bpf_prog_") above, so we omit
321+
* the +1 here.
322+
*/
323+
sizeof(prog->aux->name) > KSYM_NAME_LEN);
314324

315325
sym += snprintf(sym, KSYM_NAME_LEN, "bpf_prog_");
316326
sym = bin2hex(sym, prog->tag, sizeof(prog->tag));
317-
*sym = 0;
327+
if (prog->aux->name[0])
328+
snprintf(sym, (size_t)(end - sym), "_%s", prog->aux->name);
329+
else
330+
*sym = 0;
318331
}
319332

320333
static __always_inline unsigned long

kernel/bpf/syscall.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,8 @@ static int bpf_obj_name_cpy(char *dst, const char *src)
322322
{
323323
const char *end = src + BPF_OBJ_NAME_LEN;
324324

325+
memset(dst, 0, BPF_OBJ_NAME_LEN);
326+
325327
/* Copy all isalnum() and '_' char */
326328
while (src < end && *src) {
327329
if (!isalnum(*src) && *src != '_')
@@ -333,9 +335,6 @@ static int bpf_obj_name_cpy(char *dst, const char *src)
333335
if (src == end)
334336
return -EINVAL;
335337

336-
/* '\0' terminates dst */
337-
*dst = 0;
338-
339338
return 0;
340339
}
341340

tools/include/uapi/linux/bpf.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ union bpf_attr {
230230
__u32 numa_node; /* numa node (effective only if
231231
* BPF_F_NUMA_NODE is set).
232232
*/
233-
__u8 map_name[BPF_OBJ_NAME_LEN];
233+
char map_name[BPF_OBJ_NAME_LEN];
234234
};
235235

236236
struct { /* anonymous struct used by BPF_MAP_*_ELEM commands */
@@ -253,7 +253,7 @@ union bpf_attr {
253253
__aligned_u64 log_buf; /* user supplied buffer */
254254
__u32 kern_version; /* checked when prog_type=kprobe */
255255
__u32 prog_flags;
256-
__u8 prog_name[BPF_OBJ_NAME_LEN];
256+
char prog_name[BPF_OBJ_NAME_LEN];
257257
};
258258

259259
struct { /* anonymous struct used by BPF_OBJ_* commands */
@@ -871,7 +871,7 @@ struct bpf_prog_info {
871871
__u32 created_by_uid;
872872
__u32 nr_map_ids;
873873
__aligned_u64 map_ids;
874-
__u8 name[BPF_OBJ_NAME_LEN];
874+
char name[BPF_OBJ_NAME_LEN];
875875
} __attribute__((aligned(8)));
876876

877877
struct bpf_map_info {
@@ -881,7 +881,7 @@ struct bpf_map_info {
881881
__u32 value_size;
882882
__u32 max_entries;
883883
__u32 map_flags;
884-
__u8 name[BPF_OBJ_NAME_LEN];
884+
char name[BPF_OBJ_NAME_LEN];
885885
} __attribute__((aligned(8)));
886886

887887
/* User bpf_sock_ops struct to access socket values and specify request ops

0 commit comments

Comments
 (0)