@@ -2076,7 +2076,7 @@ static int prepare_trampoline(struct jit_ctx *ctx, struct bpf_tramp_image *im,
2076
2076
/* store return value */
2077
2077
emit (A64_STR64I (A64_R (0 ), A64_SP , retval_off ), ctx );
2078
2078
/* reserve a nop for bpf_tramp_image_put */
2079
- im -> ip_after_call = ctx -> image + ctx -> idx ;
2079
+ im -> ip_after_call = ctx -> ro_image + ctx -> idx ;
2080
2080
emit (A64_NOP , ctx );
2081
2081
}
2082
2082
@@ -2091,7 +2091,7 @@ static int prepare_trampoline(struct jit_ctx *ctx, struct bpf_tramp_image *im,
2091
2091
run_ctx_off , false);
2092
2092
2093
2093
if (flags & BPF_TRAMP_F_CALL_ORIG ) {
2094
- im -> ip_epilogue = ctx -> image + ctx -> idx ;
2094
+ im -> ip_epilogue = ctx -> ro_image + ctx -> idx ;
2095
2095
emit_addr_mov_i64 (A64_R (0 ), (const u64 )im , ctx );
2096
2096
emit_call ((const u64 )__bpf_tramp_exit , ctx );
2097
2097
}
@@ -2124,9 +2124,6 @@ static int prepare_trampoline(struct jit_ctx *ctx, struct bpf_tramp_image *im,
2124
2124
emit (A64_RET (A64_R (10 )), ctx );
2125
2125
}
2126
2126
2127
- if (ctx -> image )
2128
- bpf_flush_icache (ctx -> image , ctx -> image + ctx -> idx );
2129
-
2130
2127
kfree (branches );
2131
2128
2132
2129
return ctx -> idx ;
@@ -2169,14 +2166,43 @@ int arch_bpf_trampoline_size(const struct btf_func_model *m, u32 flags,
2169
2166
return ret < 0 ? ret : ret * AARCH64_INSN_SIZE ;
2170
2167
}
2171
2168
2172
- int arch_prepare_bpf_trampoline (struct bpf_tramp_image * im , void * image ,
2173
- void * image_end , const struct btf_func_model * m ,
2169
+ void * arch_alloc_bpf_trampoline (unsigned int size )
2170
+ {
2171
+ return bpf_prog_pack_alloc (size , jit_fill_hole );
2172
+ }
2173
+
2174
+ void arch_free_bpf_trampoline (void * image , unsigned int size )
2175
+ {
2176
+ bpf_prog_pack_free (image , size );
2177
+ }
2178
+
2179
+ void arch_protect_bpf_trampoline (void * image , unsigned int size )
2180
+ {
2181
+ }
2182
+
2183
+ void arch_unprotect_bpf_trampoline (void * image , unsigned int size )
2184
+ {
2185
+ }
2186
+
2187
+ int arch_prepare_bpf_trampoline (struct bpf_tramp_image * im , void * ro_image ,
2188
+ void * ro_image_end , const struct btf_func_model * m ,
2174
2189
u32 flags , struct bpf_tramp_links * tlinks ,
2175
2190
void * func_addr )
2176
2191
{
2177
2192
int ret , nregs ;
2193
+ void * image , * tmp ;
2194
+ u32 size = ro_image_end - ro_image ;
2195
+
2196
+ /* image doesn't need to be in module memory range, so we can
2197
+ * use kvmalloc.
2198
+ */
2199
+ image = kvmalloc (size , GFP_KERNEL );
2200
+ if (!image )
2201
+ return - ENOMEM ;
2202
+
2178
2203
struct jit_ctx ctx = {
2179
2204
.image = image ,
2205
+ .ro_image = ro_image ,
2180
2206
.idx = 0 ,
2181
2207
};
2182
2208
@@ -2185,15 +2211,26 @@ int arch_prepare_bpf_trampoline(struct bpf_tramp_image *im, void *image,
2185
2211
if (nregs > 8 )
2186
2212
return - ENOTSUPP ;
2187
2213
2188
- jit_fill_hole (image , (unsigned int )(image_end - image ));
2214
+ jit_fill_hole (image , (unsigned int )(ro_image_end - ro_image ));
2189
2215
ret = prepare_trampoline (& ctx , im , tlinks , func_addr , nregs , flags );
2190
2216
2191
- if (ret > 0 && validate_code (& ctx ) < 0 )
2217
+ if (ret > 0 && validate_code (& ctx ) < 0 ) {
2192
2218
ret = - EINVAL ;
2219
+ goto out ;
2220
+ }
2193
2221
2194
2222
if (ret > 0 )
2195
2223
ret *= AARCH64_INSN_SIZE ;
2196
2224
2225
+ tmp = bpf_arch_text_copy (ro_image , image , size );
2226
+ if (IS_ERR (tmp )) {
2227
+ ret = PTR_ERR (tmp );
2228
+ goto out ;
2229
+ }
2230
+
2231
+ bpf_flush_icache (ro_image , ro_image + size );
2232
+ out :
2233
+ kvfree (image );
2197
2234
return ret ;
2198
2235
}
2199
2236
0 commit comments