Skip to content

Commit 5154161

Browse files
nandedamanaKernel Patches Daemon
authored andcommitted
bpf: improve the general precision of tnum_mul
This commit addresses a challenge explained in an open question ("How can we incorporate correlation in unknown bits across partial products?") left by Harishankar et al. in their paper: https://arxiv.org/abs/2105.05398 When LSB(a) is uncertain, we know for sure that it is either 0 or 1, from which we could find two possible partial products and take a union. Experiment shows that applying this technique in long multiplication improves the precision in a significant number of cases (at the cost of losing precision in a relatively lower number of cases). This commit also removes the value-mask decomposition technique employed by Harishankar et al., as its direct incorporation did not result in any improvements for the new algorithm. Signed-off-by: Nandakumar Edamana <[email protected]>
1 parent 6bdd33d commit 5154161

File tree

4 files changed

+113
-13
lines changed

4 files changed

+113
-13
lines changed

include/linux/tnum.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@ bool tnum_overlap(struct tnum a, struct tnum b);
5757
/* Return a tnum representing numbers satisfying both @a and @b */
5858
struct tnum tnum_intersect(struct tnum a, struct tnum b);
5959

60+
/* Returns a tnum representing numbers satisfying either @a or @b */
61+
struct tnum tnum_union(struct tnum t1, struct tnum t2);
62+
6063
/* Return @a with all but the lowest @size bytes cleared */
6164
struct tnum tnum_cast(struct tnum a, u8 size);
6265

kernel/bpf/tnum.c

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -116,31 +116,39 @@ struct tnum tnum_xor(struct tnum a, struct tnum b)
116116
return TNUM(v & ~mu, mu);
117117
}
118118

119-
/* Generate partial products by multiplying each bit in the multiplier (tnum a)
120-
* with the multiplicand (tnum b), and add the partial products after
121-
* appropriately bit-shifting them. Instead of directly performing tnum addition
122-
* on the generated partial products, equivalenty, decompose each partial
123-
* product into two tnums, consisting of the value-sum (acc_v) and the
124-
* mask-sum (acc_m) and then perform tnum addition on them. The following paper
125-
* explains the algorithm in more detail: https://arxiv.org/abs/2105.05398.
119+
/* Perform long multiplication, iterating through the trits in a.
120+
* Inside `else if (a.mask & 1)`, instead of simply multiplying b with LSB(a)'s
121+
* uncertainty and accumulating directly, we find two possible partial products
122+
* (one for LSB(a) = 0 and another for LSB(a) = 1), and add their union to the
123+
* accumulator. This addresses an issue pointed out in an open question ("How
124+
* can we incorporate correlation in unknown bits across partial products?")
125+
* left by Harishankar et al. (https://arxiv.org/abs/2105.05398), improving
126+
* the general precision significantly.
126127
*/
127128
struct tnum tnum_mul(struct tnum a, struct tnum b)
128129
{
129-
u64 acc_v = a.value * b.value;
130-
struct tnum acc_m = TNUM(0, 0);
130+
struct tnum acc = TNUM(0, 0);
131131

132132
while (a.value || a.mask) {
133133
/* LSB of tnum a is a certain 1 */
134134
if (a.value & 1)
135-
acc_m = tnum_add(acc_m, TNUM(0, b.mask));
135+
acc = tnum_add(acc, b);
136136
/* LSB of tnum a is uncertain */
137-
else if (a.mask & 1)
138-
acc_m = tnum_add(acc_m, TNUM(0, b.value | b.mask));
137+
else if (a.mask & 1) {
138+
/* acc = tnum_union(acc_0, acc_1), where acc_0 and
139+
* acc_1 are partial accumulators for cases
140+
* LSB(a) = certain 0 and LSB(a) = certain 1.
141+
* acc_0 = acc + 0 * b = acc.
142+
* acc_1 = acc + 1 * b = tnum_add(acc, b).
143+
*/
144+
145+
acc = tnum_union(acc, tnum_add(acc, b));
146+
}
139147
/* Note: no case for LSB is certain 0 */
140148
a = tnum_rshift(a, 1);
141149
b = tnum_lshift(b, 1);
142150
}
143-
return tnum_add(TNUM(acc_v, 0), acc_m);
151+
return acc;
144152
}
145153

146154
bool tnum_overlap(struct tnum a, struct tnum b)
@@ -163,6 +171,19 @@ struct tnum tnum_intersect(struct tnum a, struct tnum b)
163171
return TNUM(v & ~mu, mu);
164172
}
165173

174+
/* Returns a tnum with the uncertainty from both a and b, and in addition, new
175+
* uncertainty at any position that a and b disagree. This represents a
176+
* superset of the union of the concrete sets of both a and b. Despite the
177+
* overapproximation, it is optimal.
178+
*/
179+
struct tnum tnum_union(struct tnum a, struct tnum b)
180+
{
181+
u64 v = a.value & b.value;
182+
u64 mu = (a.value ^ b.value) | a.mask | b.mask;
183+
184+
return TNUM(v & ~mu, mu);
185+
}
186+
166187
struct tnum tnum_cast(struct tnum a, u8 size)
167188
{
168189
a.value &= (1ULL << (size * 8)) - 1;

tools/testing/selftests/bpf/prog_tests/verifier.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
#include "verifier_meta_access.skel.h"
6060
#include "verifier_movsx.skel.h"
6161
#include "verifier_mtu.skel.h"
62+
#include "verifier_mul.skel.h"
6263
#include "verifier_netfilter_ctx.skel.h"
6364
#include "verifier_netfilter_retcode.skel.h"
6465
#include "verifier_bpf_fastcall.skel.h"
@@ -194,6 +195,7 @@ void test_verifier_may_goto_1(void) { RUN(verifier_may_goto_1); }
194195
void test_verifier_may_goto_2(void) { RUN(verifier_may_goto_2); }
195196
void test_verifier_meta_access(void) { RUN(verifier_meta_access); }
196197
void test_verifier_movsx(void) { RUN(verifier_movsx); }
198+
void test_verifier_mul(void) { RUN(verifier_mul); }
197199
void test_verifier_netfilter_ctx(void) { RUN(verifier_netfilter_ctx); }
198200
void test_verifier_netfilter_retcode(void) { RUN(verifier_netfilter_retcode); }
199201
void test_verifier_bpf_fastcall(void) { RUN(verifier_bpf_fastcall); }
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
/* Copyright (c) 2025 Nandakumar Edamana */
3+
#include <linux/bpf.h>
4+
#include <bpf/bpf_helpers.h>
5+
#include <bpf/bpf_tracing.h>
6+
#include "bpf_misc.h"
7+
8+
/* The programs here are meant to test the abstract multiplication
9+
* technique(s) used by the verifier. Using assembly to prevent
10+
* compiler optimizations.
11+
*/
12+
13+
SEC("fentry/bpf_fentry_test1")
14+
void BPF_PROG(mul_0, int x)
15+
{
16+
asm volatile (" \
17+
call %[bpf_get_prandom_u32];\
18+
r0 *= 0;\
19+
if r0 != 0 goto l0_%=;\
20+
r0 = 0;\
21+
goto l1_%=;\
22+
l0_%=:\
23+
r0 = 1;\
24+
l1_%=:\
25+
" :
26+
: __imm(bpf_get_prandom_u32)
27+
: __clobber_all);
28+
}
29+
30+
SEC("fentry/bpf_fentry_test1")
31+
__failure __msg("At program exit the register R0 has smin=1 smax=1 should have been in [0, 0]")
32+
void BPF_PROG(mul_uncertain, int x)
33+
{
34+
asm volatile (" \
35+
call %[bpf_get_prandom_u32];\
36+
r0 *= 0x3;\
37+
if r0 != 0 goto l0_%=;\
38+
r0 = 0;\
39+
goto l1_%=;\
40+
l0_%=:\
41+
r0 = 1;\
42+
l1_%=:\
43+
" :
44+
: __imm(bpf_get_prandom_u32)
45+
: __clobber_all);
46+
}
47+
48+
SEC("fentry/bpf_fentry_test1")
49+
void BPF_PROG(mul_precise, int x)
50+
{
51+
/* First, force the verifier to be uncertain about the value:
52+
* unsigned int a = (bpf_get_prandom_u32() & 0x2) | 0x1;
53+
*
54+
* Assuming the verifier is using tnum, a must be tnum{.v=0x1, .m=0x2}.
55+
* Then a * 0x3 would be m0m1 (m for uncertain). Added imprecision would
56+
* cause the following to fail, because the required return value is 0.:
57+
* return (a * 0x3) & 0x4);
58+
*/
59+
60+
asm volatile ("call %[bpf_get_prandom_u32];\
61+
r0 &= 0x2;\
62+
r0 |= 0x1;\
63+
r0 *= 0x3;\
64+
r0 &= 0x4;\
65+
if r0 != 0 goto l0_%=;\
66+
r0 = 0;\
67+
goto l1_%=;\
68+
l0_%=:\
69+
r0 = 1;\
70+
l1_%=:\
71+
" :
72+
: __imm(bpf_get_prandom_u32)
73+
: __clobber_all);
74+
}

0 commit comments

Comments
 (0)