@@ -5855,10 +5855,22 @@ static const struct bpf_func_proto bpf_tcp_gen_syncookie_proto = {
58555855#endif /* CONFIG_INET */
58565856
58575857#ifdef CONFIG_XDP_LUA
5858+
5859+ static inline void verify_and_lock (void ) {
5860+ struct xdplua_create_work * lw ;
5861+
5862+ lw = this_cpu_ptr (& luaworks );
5863+ if (!lw -> init ) {
5864+ lw -> init = true;
5865+ spin_lock (& lw -> lock );
5866+ }
5867+ }
5868+
58585869BPF_CALL_2 (bpf_lua_dataref , struct xdp_buff * , ctx , int , offset ) {
58595870 if (offset + ctx -> data < ctx -> data_end ) {
58605871 int data_ref ;
58615872
5873+ verify_and_lock ();
58625874 data_ref = ldata_newref (ctx -> L , ctx -> data + offset ,
58635875 ctx -> data_end - ctx -> data - offset );
58645876 return data_ref ;
@@ -5877,6 +5889,7 @@ static const struct bpf_func_proto bpf_lua_dataref_proto = {
58775889};
58785890
58795891BPF_CALL_2 (bpf_lua_dataunref , struct xdp_buff * , ctx , int , data_ref ) {
5892+ verify_and_lock ();
58805893 ldata_unref (ctx -> L , data_ref );
58815894 return 0 ;
58825895}
@@ -5892,18 +5905,28 @@ static const struct bpf_func_proto bpf_lua_dataunref_proto = {
58925905
58935906BPF_CALL_4 (bpf_lua_pcall , struct xdp_buff * , ctx , char * , funcname ,
58945907 int , num_args , int , num_rets ) {
5908+ int base ;
5909+
5910+ verify_and_lock ();
5911+
5912+ base = lua_gettop (ctx -> L ) - num_args ;
58955913 if (lua_getglobal (ctx -> L , funcname ) != LUA_TFUNCTION ) {
58965914 pr_err ("function %s not found\n" , funcname );
5897- lua_pop ( ctx -> L , num_args ) ;
5898- return 0 ;
5915+ num_rets = 0 ;
5916+ goto clean_state ;
58995917 }
59005918
5901- lua_insert (ctx -> L , 1 );
5919+ lua_insert (ctx -> L , base + 1 );
59025920 if (lua_pcall (ctx -> L , num_args , num_rets , 0 )) {
59035921 pr_err ("%s\n" , lua_tostring (ctx -> L , -1 ));
5904- lua_pop ( ctx -> L , 1 ) ;
5905- return 0 ;
5922+ num_rets = 0 ;
5923+ goto clean_state ;
59065924 }
5925+
5926+ base += num_rets ;
5927+
5928+ clean_state :
5929+ lua_settop (ctx -> L , base );
59075930 return num_rets ;
59085931}
59095932
@@ -5919,6 +5942,7 @@ static const struct bpf_func_proto bpf_lua_pcall_proto = {
59195942};
59205943
59215944BPF_CALL_2 (bpf_lua_pop , struct xdp_buff * , ctx , int , index ) {
5945+ verify_and_lock ();
59225946 lua_pop (ctx -> L , index );
59235947 return 0 ;
59245948}
@@ -5933,6 +5957,7 @@ static const struct bpf_func_proto bpf_lua_pop_proto = {
59335957};
59345958
59355959BPF_CALL_2 (bpf_lua_pushinteger , struct xdp_buff * , ctx , int , num ) {
5960+ verify_and_lock ();
59365961 lua_pushinteger (ctx -> L , num );
59375962 return 0 ;
59385963}
@@ -5947,6 +5972,7 @@ static const struct bpf_func_proto bpf_lua_pushinteger_proto = {
59475972};
59485973
59495974BPF_CALL_2 (bpf_lua_pushlightuserdata , struct xdp_buff * , ctx , void * , ptr ) {
5975+ verify_and_lock ();
59505976 lua_pushlightuserdata (ctx -> L , ptr );
59515977 return 0 ;
59525978}
@@ -5961,6 +5987,7 @@ static const struct bpf_func_proto bpf_lua_pushlightuserdata_proto = {
59615987};
59625988
59635989BPF_CALL_2 (bpf_lua_pushmap , struct xdp_buff * , ctx , struct bpf_map * , map ) {
5990+ verify_and_lock ();
59645991 lua_pushlightuserdata (ctx -> L , map );
59655992 return 0 ;
59665993}
@@ -5975,6 +6002,7 @@ static const struct bpf_func_proto bpf_lua_pushmap_proto = {
59756002};
59766003
59776004BPF_CALL_3 (bpf_lua_pushlstring , struct xdp_buff * , ctx , const char * , str , size_t , len ) {
6005+ verify_and_lock ();
59786006 lua_pushlstring (ctx -> L , str , len );
59796007 return 0 ;
59806008}
@@ -5990,6 +6018,7 @@ static const struct bpf_func_proto bpf_lua_pushlstring_proto = {
59906018};
59916019
59926020BPF_CALL_1 (bpf_lua_pushskb , struct xdp_buff * , ctx ) {
6021+ verify_and_lock ();
59936022 lua_pushlightuserdata (ctx -> L , ctx -> skb );
59946023 return 0 ;
59956024}
@@ -6003,6 +6032,7 @@ static const struct bpf_func_proto bpf_lua_pushskb_proto = {
60036032};
60046033
60056034BPF_CALL_2 (bpf_lua_pushstring , struct xdp_buff * , ctx , const char * , str ) {
6035+ verify_and_lock ();
60066036 lua_pushstring (ctx -> L , str );
60076037 return 0 ;
60086038}
@@ -6016,28 +6046,8 @@ static const struct bpf_func_proto bpf_lua_pushstring_proto = {
60166046 .arg2_type = ARG_ANYTHING ,
60176047};
60186048
6019- BPF_CALL_1 (bpf_lua_setstate , struct xdp_buff * , ctx ){
6020- struct lua_state_cpu * sc ;
6021- int cpu = smp_processor_id ();
6022-
6023- list_for_each_entry (sc , & lua_state_cpu_list , list ) {
6024- if (sc -> cpu == cpu ) {
6025- ctx -> L = sc -> L ;
6026- break ;
6027- }
6028- }
6029- return 0 ;
6030- }
6031-
6032- static const struct bpf_func_proto bpf_lua_setstate_proto = {
6033- .func = bpf_lua_setstate ,
6034- .gpl_only = false,
6035- .pkt_access = false,
6036- .ret_type = RET_VOID ,
6037- .arg1_type = ARG_PTR_TO_CTX ,
6038- };
6039-
60406049BPF_CALL_2 (bpf_lua_toboolean , struct xdp_buff * , ctx , int , index ) {
6050+ verify_and_lock ();
60416051 return lua_toboolean (ctx -> L , index );
60426052}
60436053
@@ -6051,6 +6061,7 @@ static const struct bpf_func_proto bpf_lua_toboolean_proto = {
60516061};
60526062
60536063BPF_CALL_2 (bpf_lua_tointeger , struct xdp_buff * , ctx , int , index ) {
6064+ verify_and_lock ();
60546065 return lua_tointeger (ctx -> L , index );
60556066}
60566067
@@ -6161,8 +6172,6 @@ bpf_base_func_proto(enum bpf_func_id func_id)
61616172 return & bpf_lua_pushskb_proto ;
61626173 case BPF_FUNC_lua_pushstring :
61636174 return & bpf_lua_pushstring_proto ;
6164- case BPF_FUNC_lua_setstate :
6165- return & bpf_lua_setstate_proto ;
61666175 case BPF_FUNC_lua_toboolean :
61676176 return & bpf_lua_toboolean_proto ;
61686177 case BPF_FUNC_lua_tointeger :
0 commit comments