@@ -4792,10 +4792,21 @@ static const struct bpf_func_proto bpf_lwt_seg6_adjust_srh_proto = {
47924792#endif /* CONFIG_IPV6_SEG6_BPF */
47934793
47944794#ifdef CONFIG_XDP_LUA
4795+ static inline void verify_and_lock (void ) {
4796+ struct xdplua_create_work * lw ;
4797+
4798+ lw = this_cpu_ptr (& luaworks );
4799+ if (!lw -> init ) {
4800+ lw -> init = true;
4801+ spin_lock (& lw -> lock );
4802+ }
4803+ }
4804+
47954805BPF_CALL_2 (bpf_lua_dataref , struct xdp_buff * , ctx , int , offset ) {
47964806 if (offset + ctx -> data < ctx -> data_end ) {
47974807 int data_ref ;
47984808
4809+ verify_and_lock ();
47994810 data_ref = ldata_newref (ctx -> L , ctx -> data + offset ,
48004811 ctx -> data_end - ctx -> data - offset );
48014812 return data_ref ;
@@ -4814,6 +4825,7 @@ static const struct bpf_func_proto bpf_lua_dataref_proto = {
48144825};
48154826
48164827BPF_CALL_2 (bpf_lua_dataunref , struct xdp_buff * , ctx , int , data_ref ) {
4828+ verify_and_lock ();
48174829 ldata_unref (ctx -> L , data_ref );
48184830 return 0 ;
48194831}
@@ -4828,19 +4840,28 @@ static const struct bpf_func_proto bpf_lua_dataunref_proto = {
48284840};
48294841
48304842BPF_CALL_4 (bpf_lua_pcall , struct xdp_buff * , ctx , char * , funcname ,
4831- int , num_args , int , num_rets ) {
4843+ int , num_args , int , num_rets ) {
4844+ int base ;
4845+
4846+ verify_and_lock ();
4847+
4848+ base = lua_gettop (ctx -> L ) - num_args ;
48324849 if (lua_getglobal (ctx -> L , funcname ) != LUA_TFUNCTION ) {
48334850 pr_err ("function %s not found\n" , funcname );
4834- lua_pop ( ctx -> L , num_args ) ;
4835- return 0 ;
4851+ num_rets = 0 ;
4852+ goto clean_state ;
48364853 }
48374854
48384855 lua_insert (ctx -> L , 1 );
48394856 if (lua_pcall (ctx -> L , num_args , num_rets , 0 )) {
48404857 pr_err ("%s\n" , lua_tostring (ctx -> L , -1 ));
4841- lua_pop ( ctx -> L , 1 ) ;
4842- return 0 ;
4858+ num_rets = 0 ;
4859+ goto clean_state ;
48434860 }
4861+
4862+ clean_state :
4863+ base += num_rets ;
4864+ lua_settop (ctx -> L , base );
48444865 return num_rets ;
48454866}
48464867
@@ -4856,6 +4877,7 @@ static const struct bpf_func_proto bpf_lua_pcall_proto = {
48564877};
48574878
48584879BPF_CALL_2 (bpf_lua_pop , struct xdp_buff * , ctx , int , index ) {
4880+ verify_and_lock ();
48594881 lua_pop (ctx -> L , index );
48604882 return 0 ;
48614883}
@@ -4870,6 +4892,7 @@ static const struct bpf_func_proto bpf_lua_pop_proto = {
48704892};
48714893
48724894BPF_CALL_2 (bpf_lua_pushinteger , struct xdp_buff * , ctx , int , num ) {
4895+ verify_and_lock ();
48734896 lua_pushinteger (ctx -> L , num );
48744897 return 0 ;
48754898}
@@ -4884,6 +4907,7 @@ static const struct bpf_func_proto bpf_lua_pushinteger_proto = {
48844907};
48854908
48864909BPF_CALL_2 (bpf_lua_pushlightuserdata , struct xdp_buff * , ctx , void * , ptr ) {
4910+ verify_and_lock ();
48874911 lua_pushlightuserdata (ctx -> L , ptr );
48884912 return 0 ;
48894913}
@@ -4898,6 +4922,7 @@ static const struct bpf_func_proto bpf_lua_pushlightuserdata_proto = {
48984922};
48994923
49004924BPF_CALL_3 (bpf_lua_pushlstring , struct xdp_buff * , ctx , const char * , str , size_t , len ) {
4925+ verify_and_lock ();
49014926 lua_pushlstring (ctx -> L , str , len );
49024927 return 0 ;
49034928}
@@ -4913,6 +4938,7 @@ static const struct bpf_func_proto bpf_lua_pushlstring_proto = {
49134938};
49144939
49154940BPF_CALL_2 (bpf_lua_pushmap , struct xdp_buff * , ctx , struct bpf_map * , map ) {
4941+ verify_and_lock ();
49164942 lua_pushlightuserdata (ctx -> L , map );
49174943 return 0 ;
49184944}
@@ -4927,6 +4953,7 @@ static const struct bpf_func_proto bpf_lua_pushmap_proto = {
49274953};
49284954
49294955BPF_CALL_1 (bpf_lua_pushskb , struct xdp_buff * , ctx ) {
4956+ verify_and_lock ();
49304957 lua_pushlightuserdata (ctx -> L , ctx -> skb );
49314958 return 0 ;
49324959}
@@ -4940,6 +4967,7 @@ static const struct bpf_func_proto bpf_lua_pushskb_proto = {
49404967};
49414968
49424969BPF_CALL_2 (bpf_lua_pushstring , struct xdp_buff * , ctx , const char * , str ) {
4970+ verify_and_lock ();
49434971 lua_pushstring (ctx -> L , str );
49444972 return 0 ;
49454973}
@@ -4953,28 +4981,8 @@ static const struct bpf_func_proto bpf_lua_pushstring_proto = {
49534981 .arg2_type = ARG_ANYTHING ,
49544982};
49554983
4956- BPF_CALL_1 (bpf_lua_setstate , struct xdp_buff * , ctx ){
4957- struct lua_state_cpu * sc ;
4958- int cpu = smp_processor_id ();
4959-
4960- list_for_each_entry (sc , & lua_state_cpu_list , list ) {
4961- if (sc -> cpu == cpu ) {
4962- ctx -> L = sc -> L ;
4963- break ;
4964- }
4965- }
4966- return 0 ;
4967- }
4968-
4969- static const struct bpf_func_proto bpf_lua_setstate_proto = {
4970- .func = bpf_lua_setstate ,
4971- .gpl_only = false,
4972- .pkt_access = false,
4973- .ret_type = RET_VOID ,
4974- .arg1_type = ARG_PTR_TO_CTX ,
4975- };
4976-
49774984BPF_CALL_2 (bpf_lua_toboolean , struct xdp_buff * , ctx , int , index ) {
4985+ verify_and_lock ();
49784986 return lua_toboolean (ctx -> L , index );
49794987}
49804988
@@ -4988,6 +4996,7 @@ static const struct bpf_func_proto bpf_lua_toboolean_proto = {
49884996};
49894997
49904998BPF_CALL_2 (bpf_lua_tointeger , struct xdp_buff * , ctx , int , index ) {
4999+ verify_and_lock ();
49915000 return lua_tointeger (ctx -> L , index );
49925001}
49935002
@@ -5077,8 +5086,6 @@ bpf_base_func_proto(enum bpf_func_id func_id)
50775086 return & bpf_lua_pushskb_proto ;
50785087 case BPF_FUNC_lua_pushstring :
50795088 return & bpf_lua_pushstring_proto ;
5080- case BPF_FUNC_lua_setstate :
5081- return & bpf_lua_setstate_proto ;
50825089 case BPF_FUNC_lua_toboolean :
50835090 return & bpf_lua_toboolean_proto ;
50845091 case BPF_FUNC_lua_tointeger :
0 commit comments