@@ -4797,10 +4797,21 @@ static const struct bpf_func_proto bpf_lwt_seg6_adjust_srh_proto = {
47974797#endif /* CONFIG_IPV6_SEG6_BPF */
47984798
47994799#ifdef CONFIG_XDP_LUA
4800+ static inline void verify_and_lock (void ) {
4801+ struct xdplua_create_work * lw ;
4802+
4803+ lw = this_cpu_ptr (& luaworks );
4804+ if (!lw -> init ) {
4805+ lw -> init = true;
4806+ spin_lock (& lw -> lock );
4807+ }
4808+ }
4809+
48004810BPF_CALL_2 (bpf_lua_dataref , struct xdp_buff * , ctx , int , offset ) {
48014811 if (offset + ctx -> data < ctx -> data_end ) {
48024812 int data_ref ;
48034813
4814+ verify_and_lock ();
48044815 data_ref = ldata_newref (ctx -> L , ctx -> data + offset ,
48054816 ctx -> data_end - ctx -> data - offset );
48064817 return data_ref ;
@@ -4819,6 +4830,7 @@ static const struct bpf_func_proto bpf_lua_dataref_proto = {
48194830};
48204831
48214832BPF_CALL_2 (bpf_lua_dataunref , struct xdp_buff * , ctx , int , data_ref ) {
4833+ verify_and_lock ();
48224834 ldata_unref (ctx -> L , data_ref );
48234835 return 0 ;
48244836}
@@ -4833,19 +4845,28 @@ static const struct bpf_func_proto bpf_lua_dataunref_proto = {
48334845};
48344846
48354847BPF_CALL_4 (bpf_lua_pcall , struct xdp_buff * , ctx , char * , funcname ,
4836- int , num_args , int , num_rets ) {
4848+ int , num_args , int , num_rets ) {
4849+ int base ;
4850+
4851+ verify_and_lock ();
4852+
4853+ base = lua_gettop (ctx -> L ) - num_args ;
48374854 if (lua_getglobal (ctx -> L , funcname ) != LUA_TFUNCTION ) {
48384855 pr_err ("function %s not found\n" , funcname );
4839- lua_pop ( ctx -> L , num_args ) ;
4840- return 0 ;
4856+ num_rets = 0 ;
4857+ goto clean_state ;
48414858 }
48424859
48434860 lua_insert (ctx -> L , 1 );
48444861 if (lua_pcall (ctx -> L , num_args , num_rets , 0 )) {
48454862 pr_err ("%s\n" , lua_tostring (ctx -> L , -1 ));
4846- lua_pop ( ctx -> L , 1 ) ;
4847- return 0 ;
4863+ num_rets = 0 ;
4864+ goto clean_state ;
48484865 }
4866+
4867+ clean_state :
4868+ base += num_rets ;
4869+ lua_settop (ctx -> L , base );
48494870 return num_rets ;
48504871}
48514872
@@ -4861,6 +4882,7 @@ static const struct bpf_func_proto bpf_lua_pcall_proto = {
48614882};
48624883
48634884BPF_CALL_2 (bpf_lua_pop , struct xdp_buff * , ctx , int , index ) {
4885+ verify_and_lock ();
48644886 lua_pop (ctx -> L , index );
48654887 return 0 ;
48664888}
@@ -4875,6 +4897,7 @@ static const struct bpf_func_proto bpf_lua_pop_proto = {
48754897};
48764898
48774899BPF_CALL_2 (bpf_lua_pushinteger , struct xdp_buff * , ctx , int , num ) {
4900+ verify_and_lock ();
48784901 lua_pushinteger (ctx -> L , num );
48794902 return 0 ;
48804903}
@@ -4889,6 +4912,7 @@ static const struct bpf_func_proto bpf_lua_pushinteger_proto = {
48894912};
48904913
48914914BPF_CALL_2 (bpf_lua_pushlightuserdata , struct xdp_buff * , ctx , void * , ptr ) {
4915+ verify_and_lock ();
48924916 lua_pushlightuserdata (ctx -> L , ptr );
48934917 return 0 ;
48944918}
@@ -4903,6 +4927,7 @@ static const struct bpf_func_proto bpf_lua_pushlightuserdata_proto = {
49034927};
49044928
49054929BPF_CALL_3 (bpf_lua_pushlstring , struct xdp_buff * , ctx , const char * , str , size_t , len ) {
4930+ verify_and_lock ();
49064931 lua_pushlstring (ctx -> L , str , len );
49074932 return 0 ;
49084933}
@@ -4918,6 +4943,7 @@ static const struct bpf_func_proto bpf_lua_pushlstring_proto = {
49184943};
49194944
49204945BPF_CALL_2 (bpf_lua_pushmap , struct xdp_buff * , ctx , struct bpf_map * , map ) {
4946+ verify_and_lock ();
49214947 lua_pushlightuserdata (ctx -> L , map );
49224948 return 0 ;
49234949}
@@ -4932,6 +4958,7 @@ static const struct bpf_func_proto bpf_lua_pushmap_proto = {
49324958};
49334959
49344960BPF_CALL_1 (bpf_lua_pushskb , struct xdp_buff * , ctx ) {
4961+ verify_and_lock ();
49354962 lua_pushlightuserdata (ctx -> L , ctx -> skb );
49364963 return 0 ;
49374964}
@@ -4945,6 +4972,7 @@ static const struct bpf_func_proto bpf_lua_pushskb_proto = {
49454972};
49464973
49474974BPF_CALL_2 (bpf_lua_pushstring , struct xdp_buff * , ctx , const char * , str ) {
4975+ verify_and_lock ();
49484976 lua_pushstring (ctx -> L , str );
49494977 return 0 ;
49504978}
@@ -4958,28 +4986,8 @@ static const struct bpf_func_proto bpf_lua_pushstring_proto = {
49584986 .arg2_type = ARG_ANYTHING ,
49594987};
49604988
4961- BPF_CALL_1 (bpf_lua_setstate , struct xdp_buff * , ctx ){
4962- struct lua_state_cpu * sc ;
4963- int cpu = smp_processor_id ();
4964-
4965- list_for_each_entry (sc , & lua_state_cpu_list , list ) {
4966- if (sc -> cpu == cpu ) {
4967- ctx -> L = sc -> L ;
4968- break ;
4969- }
4970- }
4971- return 0 ;
4972- }
4973-
4974- static const struct bpf_func_proto bpf_lua_setstate_proto = {
4975- .func = bpf_lua_setstate ,
4976- .gpl_only = false,
4977- .pkt_access = false,
4978- .ret_type = RET_VOID ,
4979- .arg1_type = ARG_PTR_TO_CTX ,
4980- };
4981-
49824989BPF_CALL_2 (bpf_lua_toboolean , struct xdp_buff * , ctx , int , index ) {
4990+ verify_and_lock ();
49834991 return lua_toboolean (ctx -> L , index );
49844992}
49854993
@@ -4993,6 +5001,7 @@ static const struct bpf_func_proto bpf_lua_toboolean_proto = {
49935001};
49945002
49955003BPF_CALL_2 (bpf_lua_tointeger , struct xdp_buff * , ctx , int , index ) {
5004+ verify_and_lock ();
49965005 return lua_tointeger (ctx -> L , index );
49975006}
49985007
@@ -5082,8 +5091,6 @@ bpf_base_func_proto(enum bpf_func_id func_id)
50825091 return & bpf_lua_pushskb_proto ;
50835092 case BPF_FUNC_lua_pushstring :
50845093 return & bpf_lua_pushstring_proto ;
5085- case BPF_FUNC_lua_setstate :
5086- return & bpf_lua_setstate_proto ;
50875094 case BPF_FUNC_lua_toboolean :
50885095 return & bpf_lua_toboolean_proto ;
50895096 case BPF_FUNC_lua_tointeger :
0 commit comments