Skip to contentSkip to conversation

kernel:bpf Remove structure passing and assignment to save stack and no coping structures

PATCH In review Karim Eshapa 2 participants 2 messages Jan 14, 2018

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 kernel:bpf Remove structure passing and assignment to save stack and no coping structures

Quoted 6 lines
Quoted 6 lines

Use pointers to structure as arguments to function instead of coping structures and less stack size. Also transfer TNUM(_v, _m) to tnum.h file to be used in differnet files for creating anonymous structures statically.

...

Quoted 11 lines
Quoted 11 lines
(quoted diff) +3 -1
+/* Statically tnum constant */
+#define TNUM(_v, _m) (struct tnum){.value = _v, .mask = _m}
 /* Represent a known constant as a tnum. */
 struct tnum tnum_const(u64 value);
 /* A completely unknown value */
@@ -26,7 +28,7 @@ struct tnum tnum_lshift(struct tnum a, u8 shift);
 /* Shift a tnum right (by a fixed shift) */
 struct tnum tnum_rshift(struct tnum a, u8 shift);
 /* Add two tnums, return @a + @b */
-struct tnum tnum_add(struct tnum a, struct tnum b);
+void tnum_add(struct tnum *res, struct tnum *a, struct tnum *b);

...

Quoted 12 lines
Quoted 12 lines
(quoted diff) +2 -3
-     reg_off = tnum_add(reg->var_off, tnum_const(ip_align + reg->off + off));
+     tnum_add(&reg_off, &reg->var_off, &TNUM(ip_align + reg->off + off, 0));
      if (!tnum_is_aligned(reg_off, size)) {
              char tn_buf[48];
@@ -1023,8 +1023,7 @@ static int check_generic_ptr_alignment(struct bpf_verifier_env *env,
      /* Byte size accesses are always allowed. */
      if (!strict || size == 1)
              return 0;
-
-     reg_off = tnum_add(reg->var_off, tnum_const(reg->off + off));
+     tnum_add(&reg_off, &reg->var_off, &TNUM(reg->off + off, 0));

...

Quoted 8 lines

Review

Quoted 3 lines
(quoted diff) +2 -1
-             dst_reg->var_off = tnum_add(ptr_reg->var_off, off_reg->var_off);
+             tnum_add(&dst_reg->var_off, &ptr_reg->var_off,
+                     &off_reg->var_off);

Is it gnu or intel style of argumnets ? where is src or dest ? Can the same pointer be used as src and as dst ? etc, etc I don't think it saves stack either.
I'd rather leave things as-is.

It's not specific style but it's recommended when passing structure specially if the structures have large sizes.
and (dest, src0, src1) respectively.Although tnum structure isn't large but it saves stack,we have 2 structure passed before calling and 1 returned to receive the return value.

Quoted 1 line

I think that looks much worse and error prone.

I don't actually see errors unless inentionally passing wrong parameters.

Thanks,
Karim

#1 lore raw
Quoted 41 lines · Karim Eshapa
Quoted 6 lines
Quoted 6 lines

Use pointers to structure as arguments to function instead of coping structures and less stack size. Also transfer TNUM(_v, _m) to tnum.h file to be used in differnet files for creating anonymous structures statically.

...

Quoted 11 lines
Quoted 11 lines
(quoted diff) +3 -1
+/* Statically tnum constant */
+#define TNUM(_v, _m) (struct tnum){.value = _v, .mask = _m}
 /* Represent a known constant as a tnum. */
 struct tnum tnum_const(u64 value);
 /* A completely unknown value */
@@ -26,7 +28,7 @@ struct tnum tnum_lshift(struct tnum a, u8 shift);
 /* Shift a tnum right (by a fixed shift) */
 struct tnum tnum_rshift(struct tnum a, u8 shift);
 /* Add two tnums, return @a + @b */
-struct tnum tnum_add(struct tnum a, struct tnum b);
+void tnum_add(struct tnum *res, struct tnum *a, struct tnum *b);

...

Quoted 12 lines
Quoted 12 lines
(quoted diff) +2 -3
-     reg_off = tnum_add(reg->var_off, tnum_const(ip_align + reg->off + off));
+     tnum_add(&reg_off, &reg->var_off, &TNUM(ip_align + reg->off + off, 0));
      if (!tnum_is_aligned(reg_off, size)) {
              char tn_buf[48];
@@ -1023,8 +1023,7 @@ static int check_generic_ptr_alignment(struct bpf_verifier_env *env,
      /* Byte size accesses are always allowed. */
      if (!strict || size == 1)
              return 0;
-
-     reg_off = tnum_add(reg->var_off, tnum_const(reg->off + off));
+     tnum_add(&reg_off, &reg->var_off, &TNUM(reg->off + off, 0));

...

Quoted 8 lines

Review

Quoted 3 lines
(quoted diff) +2 -1
-             dst_reg->var_off = tnum_add(ptr_reg->var_off, off_reg->var_off);
+             tnum_add(&dst_reg->var_off, &ptr_reg->var_off,
+                     &off_reg->var_off);

Is it gnu or intel style of argumnets ? where is src or dest ? Can the same pointer be used as src and as dst ? etc, etc I don't think it saves stack either.
I'd rather leave things as-is.

Quoted 4 lines

It's not specific style but it's recommended when passing structure specially if the structures have large sizes.
and (dest, src0, src1) respectively.Although tnum structure isn't large but it saves stack,we have 2 structure passed before calling and 1 returned to receive the return value.

1. your patch has compile time warnings
2. it doesn't reduce stack size.
For two functions that use tnum_add:
adjust_ptr_min_max_vals() before and after has exactly the same. check_ptr_alignment() after your patch _increased_ stack size.
3. text of verifier.o shrank 133 bytes while tnum.o increased 198

Please do your homework next time.
tnum code will stay as-is.