@@ -105,6 +105,91 @@ struct bpf_prog *bpf_prog_alloc(unsigned int size, gfp_t gfp_extra_flags)
105
105
}
106
106
EXPORT_SYMBOL_GPL (bpf_prog_alloc );
107
107
108
+ int bpf_prog_alloc_jited_linfo (struct bpf_prog * prog )
109
+ {
110
+ if (!prog -> aux -> nr_linfo || !prog -> jit_requested )
111
+ return 0 ;
112
+
113
+ prog -> aux -> jited_linfo = kcalloc (prog -> aux -> nr_linfo ,
114
+ sizeof (* prog -> aux -> jited_linfo ),
115
+ GFP_KERNEL | __GFP_NOWARN );
116
+ if (!prog -> aux -> jited_linfo )
117
+ return - ENOMEM ;
118
+
119
+ return 0 ;
120
+ }
121
+
122
+ void bpf_prog_free_jited_linfo (struct bpf_prog * prog )
123
+ {
124
+ kfree (prog -> aux -> jited_linfo );
125
+ prog -> aux -> jited_linfo = NULL ;
126
+ }
127
+
128
+ void bpf_prog_free_unused_jited_linfo (struct bpf_prog * prog )
129
+ {
130
+ if (prog -> aux -> jited_linfo && !prog -> aux -> jited_linfo [0 ])
131
+ bpf_prog_free_jited_linfo (prog );
132
+ }
133
+
134
+ /* The jit engine is responsible to provide an array
135
+ * for insn_off to the jited_off mapping (insn_to_jit_off).
136
+ *
137
+ * The idx to this array is the insn_off. Hence, the insn_off
138
+ * here is relative to the prog itself instead of the main prog.
139
+ * This array has one entry for each xlated bpf insn.
140
+ *
141
+ * jited_off is the byte off to the last byte of the jited insn.
142
+ *
143
+ * Hence, with
144
+ * insn_start:
145
+ * The first bpf insn off of the prog. The insn off
146
+ * here is relative to the main prog.
147
+ * e.g. if prog is a subprog, insn_start > 0
148
+ * linfo_idx:
149
+ * The prog's idx to prog->aux->linfo and jited_linfo
150
+ *
151
+ * jited_linfo[linfo_idx] = prog->bpf_func
152
+ *
153
+ * For i > linfo_idx,
154
+ *
155
+ * jited_linfo[i] = prog->bpf_func +
156
+ * insn_to_jit_off[linfo[i].insn_off - insn_start - 1]
157
+ */
158
+ void bpf_prog_fill_jited_linfo (struct bpf_prog * prog ,
159
+ const u32 * insn_to_jit_off )
160
+ {
161
+ u32 linfo_idx , insn_start , insn_end , nr_linfo , i ;
162
+ const struct bpf_line_info * linfo ;
163
+ void * * jited_linfo ;
164
+
165
+ if (!prog -> aux -> jited_linfo )
166
+ /* Userspace did not provide linfo */
167
+ return ;
168
+
169
+ linfo_idx = prog -> aux -> linfo_idx ;
170
+ linfo = & prog -> aux -> linfo [linfo_idx ];
171
+ insn_start = linfo [0 ].insn_off ;
172
+ insn_end = insn_start + prog -> len ;
173
+
174
+ jited_linfo = & prog -> aux -> jited_linfo [linfo_idx ];
175
+ jited_linfo [0 ] = prog -> bpf_func ;
176
+
177
+ nr_linfo = prog -> aux -> nr_linfo - linfo_idx ;
178
+
179
+ for (i = 1 ; i < nr_linfo && linfo [i ].insn_off < insn_end ; i ++ )
180
+ /* The verifier ensures that linfo[i].insn_off is
181
+ * strictly increasing
182
+ */
183
+ jited_linfo [i ] = prog -> bpf_func +
184
+ insn_to_jit_off [linfo [i ].insn_off - insn_start - 1 ];
185
+ }
186
+
187
+ void bpf_prog_free_linfo (struct bpf_prog * prog )
188
+ {
189
+ bpf_prog_free_jited_linfo (prog );
190
+ kvfree (prog -> aux -> linfo );
191
+ }
192
+
108
193
struct bpf_prog * bpf_prog_realloc (struct bpf_prog * fp_old , unsigned int size ,
109
194
gfp_t gfp_extra_flags )
110
195
{
@@ -294,6 +379,26 @@ static int bpf_adj_branches(struct bpf_prog *prog, u32 pos, u32 delta,
294
379
return ret ;
295
380
}
296
381
382
+ static void bpf_adj_linfo (struct bpf_prog * prog , u32 off , u32 delta )
383
+ {
384
+ struct bpf_line_info * linfo ;
385
+ u32 i , nr_linfo ;
386
+
387
+ nr_linfo = prog -> aux -> nr_linfo ;
388
+ if (!nr_linfo || !delta )
389
+ return ;
390
+
391
+ linfo = prog -> aux -> linfo ;
392
+
393
+ for (i = 0 ; i < nr_linfo ; i ++ )
394
+ if (off < linfo [i ].insn_off )
395
+ break ;
396
+
397
+ /* Push all off < linfo[i].insn_off by delta */
398
+ for (; i < nr_linfo ; i ++ )
399
+ linfo [i ].insn_off += delta ;
400
+ }
401
+
297
402
struct bpf_prog * bpf_patch_insn_single (struct bpf_prog * prog , u32 off ,
298
403
const struct bpf_insn * patch , u32 len )
299
404
{
@@ -349,6 +454,8 @@ struct bpf_prog *bpf_patch_insn_single(struct bpf_prog *prog, u32 off,
349
454
*/
350
455
BUG_ON (bpf_adj_branches (prog_adj , off , insn_delta , false));
351
456
457
+ bpf_adj_linfo (prog_adj , off , insn_delta );
458
+
352
459
return prog_adj ;
353
460
}
354
461
@@ -1591,13 +1698,20 @@ struct bpf_prog *bpf_prog_select_runtime(struct bpf_prog *fp, int *err)
1591
1698
* be JITed, but falls back to the interpreter.
1592
1699
*/
1593
1700
if (!bpf_prog_is_dev_bound (fp -> aux )) {
1701
+ * err = bpf_prog_alloc_jited_linfo (fp );
1702
+ if (* err )
1703
+ return fp ;
1704
+
1594
1705
fp = bpf_int_jit_compile (fp );
1595
- #ifdef CONFIG_BPF_JIT_ALWAYS_ON
1596
1706
if (!fp -> jited ) {
1707
+ bpf_prog_free_jited_linfo (fp );
1708
+ #ifdef CONFIG_BPF_JIT_ALWAYS_ON
1597
1709
* err = - ENOTSUPP ;
1598
1710
return fp ;
1599
- }
1600
1711
#endif
1712
+ } else {
1713
+ bpf_prog_free_unused_jited_linfo (fp );
1714
+ }
1601
1715
} else {
1602
1716
* err = bpf_prog_offload_compile (fp );
1603
1717
if (* err )
0 commit comments