Skip to content

Commit 2ea598d

Browse files
bpf: don't use MAX_SELECTORS_MASK for args
Currently, the max number of selectors is the same as the max number of configured args, so we don't have problems due to the fact that we use the MAX_SELECTORS_MASK on arg indexes. This change makes arg index masking use MAX_POSSIBLE_ARGS_INDEX for clarity and to decouple these independent concepts. EVENT_CONFIG_MAX_ARG is replaced everywhere with MAX_POSSIBLE_ARGS because these two values cannot deviate. There are places where we mask an index that is used for both event config and kprobe messages. This consolidation helps in those cases. Convenience masks are added for the usdt and reg arg masks, because these arrays can/do have different sizes than the original arg arrays. There is a limit on the number of args that a user can config. There is also an unrelated limit about which args can be reached within a tracepoint/function signature. Both of these limits are currently 5, but they are logically unrelated limits. However, we use the same arg index mask for both. This change decouples these two concepts from a masking perspective so that we could increase the number of arguments that can be reached from a function signature perspective without increasing the allowed number of configured arguments. Signed-off-by: Andy Strohman <astrohma@isovalent.com>
1 parent daaa560 commit 2ea598d

File tree

4 files changed

+41
-23
lines changed

4 files changed

+41
-23
lines changed

bpf/lib/generic.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,10 @@ struct msg_generic_kprobe {
6969
__u64 user_stack_id; // User Stack trace ID
7070
/* anything above is shared with the userspace so it should match structs MsgGenericKprobe and MsgGenericTracepoint in Go */
7171
char args[24000];
72+
#define MAX_ACCESSIBLE_ARGS 5
73+
/* convenience mask for verifier appeasing*/
74+
#define MAX_ACCESSIBLE_ARGS_MASK 0x7
75+
_Static_assert(MAX_ACCESSIBLE_ARGS - 1 <= MAX_ACCESSIBLE_ARGS_MASK, "Need to update MAX_ACCESSIBLE_ARGS_MASK");
7276
unsigned long a0, a1, a2, a3, a4;
7377
long argsoff[MAX_POSSIBLE_ARGS];
7478
arg_status_t arg_status[MAX_POSSIBLE_ARGS];

bpf/process/event_config.h

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,16 +46,20 @@ struct extract_arg_data {
4646
};
4747

4848
#define MAX_BTF_ARG_DEPTH 10
49-
#define EVENT_CONFIG_MAX_ARG 5
5049
#define EVENT_CONFIG_MAX_USDT_ARG 8
5150
#define EVENT_CONFIG_MAX_REG_ARG 8
51+
/* convenience masks for verifier appeasing*/
52+
#define EVENT_CONFIG_MAX_USDT_ARG_MASK 0x7
53+
_Static_assert(EVENT_CONFIG_MAX_USDT_ARG - 1 <= EVENT_CONFIG_MAX_USDT_ARG_MASK, "Need to update EVENT_CONFIG_MAX_USDT_ARG_MASK");
54+
#define EVENT_CONFIG_MAX_REG_ARG_MASK 0x7
55+
_Static_assert(EVENT_CONFIG_MAX_REG_ARG - 1 <= EVENT_CONFIG_MAX_REG_ARG_MASK, "Need to update EVENT_CONFIG_MAX_REG_ARG_MASK");
5256

5357
struct event_config {
5458
__u32 func_id;
55-
__s32 arg[EVENT_CONFIG_MAX_ARG];
56-
__u32 arm[EVENT_CONFIG_MAX_ARG];
57-
__u32 off[EVENT_CONFIG_MAX_ARG];
58-
__s32 idx[EVENT_CONFIG_MAX_ARG];
59+
__s32 arg[MAX_POSSIBLE_ARGS];
60+
__u32 arm[MAX_POSSIBLE_ARGS];
61+
__u32 off[MAX_POSSIBLE_ARGS];
62+
__s32 idx[MAX_POSSIBLE_ARGS];
5963
__u32 syscall;
6064
__s32 argreturncopy;
6165
__s32 argreturn;
@@ -70,7 +74,7 @@ struct event_config {
7074
__u32 policy_id;
7175
__u32 flags;
7276
__u32 pad;
73-
struct config_btf_arg btf_arg[EVENT_CONFIG_MAX_ARG][MAX_BTF_ARG_DEPTH];
77+
struct config_btf_arg btf_arg[MAX_POSSIBLE_ARGS][MAX_BTF_ARG_DEPTH];
7478
struct config_usdt_arg usdt_arg[EVENT_CONFIG_MAX_USDT_ARG];
7579
struct config_reg_arg reg_arg[EVENT_CONFIG_MAX_REG_ARG];
7680
} __attribute__((packed));
@@ -91,7 +95,7 @@ FUNC_INLINE int arg_idx(int index)
9195

9296
asm volatile("%[index] &= %1 ;\n"
9397
: [index] "+r"(index)
94-
: "i"(MAX_SELECTORS_MASK));
98+
: "i"(MAX_POSSIBLE_ARGS_MASK));
9599
return config->idx[index];
96100
}
97101

bpf/process/generic_calls.h

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -508,9 +508,9 @@ FUNC_INLINE void extract_arg(struct event_config *config, int index, unsigned lo
508508

509509
asm volatile("%[index] &= %1 ;\n"
510510
: [index] "+r"(index)
511-
: "i"(MAX_SELECTORS_MASK));
511+
: "i"(MAX_POSSIBLE_ARGS_MASK));
512512

513-
if (index >= EVENT_CONFIG_MAX_ARG)
513+
if (index >= MAX_POSSIBLE_ARGS)
514514
return;
515515

516516
btf_config = config->btf_arg[index];
@@ -572,7 +572,7 @@ FUNC_INLINE long get_pt_regs_arg(struct pt_regs *ctx, struct event_config *confi
572572

573573
asm volatile("%[index] &= %1 ;\n"
574574
: [index] "+r"(index)
575-
: "i"(MAX_SELECTORS_MASK));
575+
: "i"(EVENT_CONFIG_MAX_REG_ARG_MASK));
576576
reg = &config->reg_arg[index];
577577
shift = 64 - reg->size * 8;
578578

@@ -629,9 +629,17 @@ FUNC_INLINE long generic_read_arg(void *ctx, int index, long off, struct bpf_map
629629
if (!config)
630630
return 0;
631631

632+
#if defined(GENERIC_TRACEPOINT) || defined(GENERIC_USDT)
633+
asm volatile("%[index] &= %1 ;\n"
634+
: [index] "+r"(index)
635+
: "i"(MAX_ACCESSIBLE_ARGS_MASK));
636+
637+
a = (&e->a0)[index];
638+
#endif
639+
632640
asm volatile("%[index] &= %1 ;\n"
633641
: [index] "+r"(index)
634-
: "i"(MAX_SELECTORS_MASK));
642+
: "i"(MAX_POSSIBLE_ARGS_MASK));
635643
ty = config->arg[index];
636644
am = config->arm[index];
637645

@@ -645,14 +653,9 @@ FUNC_INLINE long generic_read_arg(void *ctx, int index, long off, struct bpf_map
645653
e->arg_status[index] = 0;
646654

647655
#if defined(GENERIC_TRACEPOINT) || defined(GENERIC_USDT)
648-
a = (&e->a0)[index];
649656
extract_arg(config, index, &a, &e->arg_status[index]);
650657
#else
651658
arg_index = config->idx[index];
652-
asm volatile("%[arg_index] &= %1 ;\n"
653-
: [arg_index] "+r"(arg_index)
654-
: "i"(MAX_SELECTORS_MASK));
655-
656659
/* Getting argument data based on the source attribute, which is encoded
657660
* in argument meta data, so far it's either:
658661
*
@@ -661,14 +664,21 @@ FUNC_INLINE long generic_read_arg(void *ctx, int index, long off, struct bpf_map
661664
* - current task object
662665
* - real argument value
663666
*/
664-
if (am & ARGM_PT_REGS_PRELOAD)
667+
if (am & ARGM_PT_REGS_PRELOAD) {
665668
a = get_pt_regs_preload_arg(ctx, ty);
666-
else if (am & ARGM_PT_REGS)
669+
} else if (am & ARGM_PT_REGS) {
670+
asm volatile("%[arg_index] &= %1 ;\n"
671+
: [arg_index] "+r"(arg_index)
672+
: "i"(EVENT_CONFIG_MAX_REG_ARG_MASK));
667673
a = get_pt_regs_arg(ctx, config, arg_index);
668-
else if (am & ARGM_CURRENT_TASK)
674+
} else if (am & ARGM_CURRENT_TASK) {
669675
a = get_current_task();
670-
else
676+
} else {
677+
asm volatile("%[arg_index] &= %1 ;\n"
678+
: [arg_index] "+r"(arg_index)
679+
: "i"(MAX_ACCESSIBLE_ARGS_MASK));
671680
a = (&e->a0)[arg_index];
681+
}
672682

673683
extract_arg(config, index, &a, &e->arg_status[index]);
674684

@@ -753,7 +763,7 @@ read_usdt_arg(struct pt_regs *ctx, struct event_config *config, int index)
753763
unsigned long val, off, idx;
754764
int err;
755765

756-
index &= 7;
766+
index &= EVENT_CONFIG_MAX_USDT_ARG_MASK;
757767
arg = &config->usdt_arg[index];
758768

759769
if (arg->type == USDT_ARG_TYPE_NONE)
@@ -1009,7 +1019,7 @@ do_set_action(void *ctx, struct msg_generic_kprobe *e, __u32 arg_idx, __u32 arg_
10091019
if (!config)
10101020
return;
10111021

1012-
arg_idx &= 7;
1022+
arg_idx &= EVENT_CONFIG_MAX_USDT_ARG_MASK;
10131023
arg = &config->usdt_arg[arg_idx];
10141024

10151025
switch (arg->type) {

bpf/process/uprobe_preload.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ preload_pt_regs_arg(struct pt_regs *ctx, struct event_config *config, int index)
5959

6060
asm volatile("%[index] &= %1 ;\n"
6161
: [index] "+r"(index)
62-
: "i"(MAX_SELECTORS_MASK));
62+
: "i"(EVENT_CONFIG_MAX_REG_ARG_MASK));
6363

6464
reg = &config->reg_arg[index];
6565
shift = 64 - reg->size * 8;

0 commit comments

Comments
 (0)