@@ -2076,7 +2076,7 @@ static int prepare_trampoline(struct jit_ctx *ctx, struct bpf_tramp_image *im,
20762076 /* store return value */
20772077 emit (A64_STR64I (A64_R (0 ), A64_SP , retval_off ), ctx );
20782078 /* 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 ;
20802080 emit (A64_NOP , ctx );
20812081 }
20822082
@@ -2091,7 +2091,7 @@ static int prepare_trampoline(struct jit_ctx *ctx, struct bpf_tramp_image *im,
20912091 run_ctx_off , false);
20922092
20932093 if (flags & BPF_TRAMP_F_CALL_ORIG ) {
2094- im -> ip_epilogue = ctx -> image + ctx -> idx ;
2094+ im -> ip_epilogue = ctx -> ro_image + ctx -> idx ;
20952095 emit_addr_mov_i64 (A64_R (0 ), (const u64 )im , ctx );
20962096 emit_call ((const u64 )__bpf_tramp_exit , ctx );
20972097 }
@@ -2124,9 +2124,6 @@ static int prepare_trampoline(struct jit_ctx *ctx, struct bpf_tramp_image *im,
21242124 emit (A64_RET (A64_R (10 )), ctx );
21252125 }
21262126
2127- if (ctx -> image )
2128- bpf_flush_icache (ctx -> image , ctx -> image + ctx -> idx );
2129-
21302127 kfree (branches );
21312128
21322129 return ctx -> idx ;
@@ -2169,14 +2166,43 @@ int arch_bpf_trampoline_size(const struct btf_func_model *m, u32 flags,
21692166 return ret < 0 ? ret : ret * AARCH64_INSN_SIZE ;
21702167}
21712168
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 ,
21742189 u32 flags , struct bpf_tramp_links * tlinks ,
21752190 void * func_addr )
21762191{
21772192 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+
21782203 struct jit_ctx ctx = {
21792204 .image = image ,
2205+ .ro_image = ro_image ,
21802206 .idx = 0 ,
21812207 };
21822208
@@ -2185,15 +2211,26 @@ int arch_prepare_bpf_trampoline(struct bpf_tramp_image *im, void *image,
21852211 if (nregs > 8 )
21862212 return - ENOTSUPP ;
21872213
2188- jit_fill_hole (image , (unsigned int )(image_end - image ));
2214+ jit_fill_hole (image , (unsigned int )(ro_image_end - ro_image ));
21892215 ret = prepare_trampoline (& ctx , im , tlinks , func_addr , nregs , flags );
21902216
2191- if (ret > 0 && validate_code (& ctx ) < 0 )
2217+ if (ret > 0 && validate_code (& ctx ) < 0 ) {
21922218 ret = - EINVAL ;
2219+ goto out ;
2220+ }
21932221
21942222 if (ret > 0 )
21952223 ret *= AARCH64_INSN_SIZE ;
21962224
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 );
21972234 return ret ;
21982235}
21992236
0 commit comments