2018-01-13 19:27:08

by Karim Eshapa

[permalink] [raw]
Subject: [PATCH] Remove structure passing and assignment to save stack and no coping structures.

Signed-off-by: Karim Eshapa <[email protected]>

Thanks,
Karim
---
include/linux/tnum.h | 2 +-
kernel/bpf/tnum.c | 13 +++++++------
kernel/bpf/verifier.c | 12 ++++++++----
3 files changed, 16 insertions(+), 11 deletions(-)

diff --git a/include/linux/tnum.h b/include/linux/tnum.h
index 0d2d3da..ddb1250 100644
--- a/include/linux/tnum.h
+++ b/include/linux/tnum.h
@@ -26,7 +26,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);
/* Subtract two tnums, return @a - @b */
struct tnum tnum_sub(struct tnum a, struct tnum b);
/* Bitwise-AND, return @a & @b */
diff --git a/kernel/bpf/tnum.c b/kernel/bpf/tnum.c
index 1f4bf68..f7f8b10 100644
--- a/kernel/bpf/tnum.c
+++ b/kernel/bpf/tnum.c
@@ -43,16 +43,17 @@ struct tnum tnum_rshift(struct tnum a, u8 shift)
return TNUM(a.value >> shift, a.mask >> shift);
}

-struct tnum tnum_add(struct tnum a, struct tnum b)
+void tnum_add(struct tnum *res, struct tnum *a, struct tnum *b)
{
u64 sm, sv, sigma, chi, mu;

- sm = a.mask + b.mask;
- sv = a.value + b.value;
+ sm = a->mask + b->mask;
+ sv = a->value + b->value;
sigma = sm + sv;
chi = sigma ^ sv;
- mu = chi | a.mask | b.mask;
- return TNUM(sv & ~mu, mu);
+ mu = chi | a->mask | b->mask;
+ res->value = (sv & ~mu);
+ res->mask = mu;
}

struct tnum tnum_sub(struct tnum a, struct tnum b)
@@ -102,7 +103,7 @@ static struct tnum hma(struct tnum acc, u64 value, u64 mask)
{
while (mask) {
if (mask & 1)
- acc = tnum_add(acc, TNUM(0, value));
+ tnum_add(&acc, &acc, &TNUM(0, value));
mask >>= 1;
value <<= 1;
}
diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c
index b414d6b..4acc16c 100644
--- a/kernel/bpf/verifier.c
+++ b/kernel/bpf/verifier.c
@@ -999,7 +999,8 @@ static int check_pkt_ptr_alignment(struct bpf_verifier_env *env,
*/
ip_align = 2;

- reg_off = tnum_add(reg->var_off, tnum_const(ip_align + reg->off + off));
+ tnum_add(&reg_off, &reg->var_off,
+ &tnum_const(ip_align + reg->off + off));
if (!tnum_is_aligned(reg_off, size)) {
char tn_buf[48];

@@ -1024,7 +1025,8 @@ static int check_generic_ptr_alignment(struct bpf_verifier_env *env,
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_const(reg->off + off));
if (!tnum_is_aligned(reg_off, size)) {
char tn_buf[48];

@@ -1971,7 +1973,8 @@ static int adjust_ptr_min_max_vals(struct bpf_verifier_env *env,
dst_reg->umin_value = umin_ptr + umin_val;
dst_reg->umax_value = umax_ptr + umax_val;
}
- 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);
dst_reg->off = ptr_reg->off;
if (reg_is_pkt_pointer(ptr_reg)) {
dst_reg->id = ++env->id_gen;
@@ -2108,7 +2111,8 @@ static int adjust_scalar_min_max_vals(struct bpf_verifier_env *env,
dst_reg->umin_value += umin_val;
dst_reg->umax_value += umax_val;
}
- dst_reg->var_off = tnum_add(dst_reg->var_off, src_reg.var_off);
+ tnum_add(&dst_reg->var_off, &dst_reg->var_off,
+ &src_reg.var_off);
break;
case BPF_SUB:
if (signed_sub_overflows(dst_reg->smin_value, smax_val) ||
--
2.7.4


2018-01-16 06:58:08

by kernel test robot

[permalink] [raw]
Subject: Re: [PATCH] Remove structure passing and assignment to save stack and no coping structures.

Hi Karim,

Thank you for the patch! Perhaps something to improve:

[auto build test WARNING on linus/master]
[also build test WARNING on v4.15-rc8 next-20180115]
[if your patch is applied to the wrong git tree, please drop us a note to help improve the system]

url: https://github.com/0day-ci/linux/commits/Karim-Eshapa/Remove-structure-passing-and-assignment-to-save-stack-and-no-coping-structures/20180116-130502
reproduce:
# apt-get install sparse
make ARCH=x86_64 allmodconfig
make C=1 CF=-D__CHECK_ENDIAN__


sparse warnings: (new ones prefixed by >>)

>> kernel/bpf/verifier.c:1002:29: sparse: incorrect type in argument 2 (different modifiers) @@ expected struct tnum @@ got structstruct tnum @@
kernel/bpf/verifier.c:1002:29: expected struct tnum
kernel/bpf/verifier.c:1002:29: got struct tnum const
>> kernel/bpf/verifier.c:1003:17: sparse: not addressable
kernel/bpf/verifier.c:1028:29: sparse: incorrect type in argument 2 (different modifiers) @@ expected struct tnum @@ got structstruct tnum @@
kernel/bpf/verifier.c:1028:29: expected struct tnum
kernel/bpf/verifier.c:1028:29: got struct tnum const
kernel/bpf/verifier.c:1029:17: sparse: not addressable
kernel/bpf/verifier.c:1969:46: sparse: incorrect type in argument 2 (different modifiers) @@ expected struct tnum @@ got structstruct tnum @@
kernel/bpf/verifier.c:1969:46: expected struct tnum
kernel/bpf/verifier.c:1969:46: got struct tnum const
>> kernel/bpf/verifier.c:1970:26: sparse: incorrect type in argument 3 (different modifiers) @@ expected struct tnum lib @@ got structstruct tnum lib @@
kernel/bpf/verifier.c:1970:26: expected struct tnum lib
kernel/bpf/verifier.c:1970:26: got struct tnum const
kernel/bpf/verifier.c:4527:38: sparse: subtraction of Share your drugs
>> kernel/bpf/verifier.c:1002:17: sparse: call with no type!
kernel/bpf/verifier.c:1028:17: sparse: call with no type!
kernel/bpf/verifier.c: In function 'check_pkt_ptr_alignment':
kernel/bpf/verifier.c:1003:3: error: lvalue required as unary '&' operand
&tnum_const(ip_align + reg->off + off));
^
kernel/bpf/verifier.c:1002:21: warning: passing argument 2 of 'tnum_add' discards 'const' qualifier from pointer target type
tnum_add(&reg_off, &reg->var_off,
^
In file included from include/linux/bpf_verifier.h:12:0,
from kernel/bpf/verifier.c:17:
include/linux/tnum.h:29:6: note: expected 'struct tnum but argument is of type 'const struct tnum
void tnum_add(struct tnum struct tnum struct tnum
^~~~~~~~
kernel/bpf/verifier.c: In function 'check_generic_ptr_alignment':
kernel/bpf/verifier.c:1029:3: error: lvalue required as unary '&' operand
&tnum_const(reg->off + off));
^
kernel/bpf/verifier.c:1028:21: warning: passing argument 2 of 'tnum_add' discards 'const' qualifier from pointer target type
tnum_add(&reg_off, &reg->var_off,
^
In file included from include/linux/bpf_verifier.h:12:0,
from kernel/bpf/verifier.c:17:
include/linux/tnum.h:29:6: note: expected 'struct tnum but argument is of type 'const struct tnum
void tnum_add(struct tnum struct tnum struct tnum
^~~~~~~~
kernel/bpf/verifier.c: In function 'adjust_ptr_min_max_vals':
kernel/bpf/verifier.c:1969:31: warning: passing argument 2 of 'tnum_add' discards 'const' qualifier from pointer target type
tnum_add(&dst_reg->var_off, &ptr_reg->var_off,
^
In file included from include/linux/bpf_verifier.h:12:0,
from kernel/bpf/verifier.c:17:
include/linux/tnum.h:29:6: note: expected 'struct tnum but argument is of type 'const struct tnum
void tnum_add(struct tnum struct tnum struct tnum
^~~~~~~~
kernel/bpf/verifier.c:1970:4: warning: passing argument 3 of 'tnum_add' discards 'const' qualifier from pointer target type
&off_reg->var_off);
^
In file included from include/linux/bpf_verifier.h:12:0,
from kernel/bpf/verifier.c:17:
include/linux/tnum.h:29:6: note: expected 'struct tnum but argument is of type 'const struct tnum
void tnum_add(struct tnum struct tnum struct tnum
^~~~~~~~

vim +1002 kernel/bpf/verifier.c

980
981 static int check_pkt_ptr_alignment(struct bpf_verifier_env *env,
982 const struct bpf_reg_state *reg,
983 int off, int size, bool strict)
984 {
985 struct tnum reg_off;
986 int ip_align;
987
988 /* Byte size accesses are always allowed. */
989 if (!strict || size == 1)
990 return 0;
991
992 /* For platforms that do not have a Kconfig enabling
993 * CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS the value of
994 * NET_IP_ALIGN is universally set to '2'. And on platforms
995 * that do set CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS, we get
996 * to this code only in strict mode where we want to emulate
997 * the NET_IP_ALIGN==2 checking. Therefore use an
998 * unconditional IP align value of '2'.
999 */
1000 ip_align = 2;
1001
> 1002 tnum_add(&reg_off, &reg->var_off,
> 1003 &tnum_const(ip_align + reg->off + off));
1004 if (!tnum_is_aligned(reg_off, size)) {
1005 char tn_buf[48];
1006
1007 tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off);
1008 verbose(env,
1009 "misaligned packet access off %d+%s+%d+%d size %d\n",
1010 ip_align, tn_buf, reg->off, off, size);
1011 return -EACCES;
1012 }
1013
1014 return 0;
1015 }
1016
1017 static int check_generic_ptr_alignment(struct bpf_verifier_env *env,
1018 const struct bpf_reg_state *reg,
1019 const char *pointer_desc,
1020 int off, int size, bool strict)
1021 {
1022 struct tnum reg_off;
1023
1024 /* Byte size accesses are always allowed. */
1025 if (!strict || size == 1)
1026 return 0;
1027
> 1028 tnum_add(&reg_off, &reg->var_off,
1029 &tnum_const(reg->off + off));
1030 if (!tnum_is_aligned(reg_off, size)) {
1031 char tn_buf[48];
1032
1033 tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off);
1034 verbose(env, "misaligned %saccess off %s+%d+%d size %d\n",
1035 pointer_desc, tn_buf, reg->off, off, size);
1036 return -EACCES;
1037 }
1038
1039 return 0;
1040 }
1041

---
0-DAY kernel test infrastructure Open Source Technology Center
https://lists.01.org/pipermail/kbuild-all Intel Corporation