Skip to content

Commit d8e92c3

Browse files
Fix script loading synchronization issue
1 parent 8343c60 commit d8e92c3

File tree

8 files changed

+99
-75
lines changed

8 files changed

+99
-75
lines changed

include/linux/filter.h

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1099,13 +1099,4 @@ struct bpf_sock_ops_kern {
10991099
*/
11001100
};
11011101

1102-
#ifdef CONFIG_XDP_LUA
1103-
extern struct list_head lua_state_cpu_list;
1104-
1105-
struct lua_state_cpu {
1106-
struct lua_State *L;
1107-
int cpu;
1108-
struct list_head list;
1109-
};
1110-
#endif /* CONFIG_XDP_LUA */
11111102
#endif /* __LINUX_FILTER_H__ */

include/net/xdp.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,19 @@ struct xdp_rxq_info {
6363
struct xdp_mem_info mem;
6464
} ____cacheline_aligned; /* perf critical, avoid false-sharing */
6565

66+
#ifdef CONFIG_XDP_LUA
67+
struct xdplua_create_work {
68+
char lua_script[8192];
69+
struct lua_State *L;
70+
struct work_struct work;
71+
spinlock_t lock;
72+
bool init;
73+
};
74+
75+
DECLARE_PER_CPU(struct xdplua_create_work, luaworks);
76+
#endif /* CONFIG_XDP_LUA */
77+
78+
6679
struct xdp_buff {
6780
void *data;
6881
void *data_end;

include/uapi/linux/bpf.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2240,9 +2240,10 @@ union bpf_attr {
22402240
FN(lua_pushmap), \
22412241
FN(lua_pushskb), \
22422242
FN(lua_pushstring), \
2243-
FN(lua_setstate), \
22442243
FN(lua_toboolean), \
2245-
FN(lua_tointeger),
2244+
FN(lua_tointeger), \
2245+
FN(lua_newpacket), \
2246+
FN(lua_type),
22462247
/* #endif CONFIG_XDP_LUA */
22472248

22482249
/* integer value in 'imm' field of BPF_CALL instruction selects which helper

net/core/dev.c

Lines changed: 48 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@
7575
#ifdef CONFIG_XDP_LUA
7676
#include <lua.h>
7777
#include <lauxlib.h>
78+
#include <lstate.h>
7879
#include <lualib.h>
7980
#include <luadata.h>
8081
#endif /* CONFIG_XDP_LUA */
@@ -164,6 +165,9 @@
164165

165166
static DEFINE_SPINLOCK(ptype_lock);
166167
static DEFINE_SPINLOCK(offload_lock);
168+
#ifdef CONFIG_XDP_LUA
169+
DEFINE_PER_CPU(struct xdplua_create_work, luaworks);
170+
#endif
167171
struct list_head ptype_base[PTYPE_HASH_SIZE] __read_mostly;
168172
struct list_head ptype_all __read_mostly; /* Taps */
169173
static struct list_head offload_base __read_mostly;
@@ -173,10 +177,6 @@ static int call_netdevice_notifiers_info(unsigned long val,
173177
struct netdev_notifier_info *info);
174178
static struct napi_struct *napi_by_id(unsigned int napi_id);
175179

176-
#ifdef CONFIG_XDP_LUA
177-
struct list_head lua_state_cpu_list;
178-
#endif /* CONFIG_XDP_LUA */
179-
180180
/*
181181
* The @dev_base_head list is protected by @dev_base_lock and the rtnl
182182
* semaphore.
@@ -4310,6 +4310,9 @@ static u32 netif_receive_generic_xdp(struct sk_buff *skb,
43104310
bool orig_bcast;
43114311
int hlen, off;
43124312
u32 mac_len;
4313+
#ifdef CONFIG_XDP_LUA
4314+
struct xdplua_create_work *lw;
4315+
#endif /* CONFIG_XDP_LUA */
43134316

43144317
/* Reinjected packets coming from act_mirred or similar should
43154318
* not get XDP generic processing.
@@ -4355,11 +4358,21 @@ static u32 netif_receive_generic_xdp(struct sk_buff *skb,
43554358
rxqueue = netif_get_rxqueue(skb);
43564359
xdp->rxq = &rxqueue->xdp_rxq;
43574360
#ifdef CONFIG_XDP_LUA
4361+
lw = this_cpu_ptr(&luaworks);
4362+
43584363
xdp->skb = skb;
4364+
xdp->L = lw->L;
43594365
#endif /* CONFIG_XDP_LUA */
43604366

43614367
act = bpf_prog_run_xdp(xdp_prog, xdp);
43624368

4369+
#ifdef CONFIG_XDP_LUA
4370+
if (lw->init) {
4371+
lw->init = false;
4372+
spin_unlock(&lw->lock);
4373+
}
4374+
#endif /* CONFIG_XDP_LUA */
4375+
43634376
/* check if bpf_xdp_adjust_head was used */
43644377
off = xdp->data - orig_data;
43654378
if (off) {
@@ -5146,16 +5159,29 @@ static int generic_xdp_install(struct net_device *dev, struct netdev_bpf *xdp)
51465159
}
51475160

51485161
#ifdef CONFIG_XDP_LUA
5149-
int generic_xdp_lua_install_prog(char *lua_prog)
5162+
5163+
static void per_cpu_xdp_lua_install(struct work_struct *w) {
5164+
int this_cpu = smp_processor_id();
5165+
struct xdplua_create_work *lw =
5166+
container_of(w, struct xdplua_create_work, work);
5167+
5168+
spin_lock_bh(&lw->lock);
5169+
if (luaL_dostring(lw->L, lw->lua_script)) {
5170+
pr_err(KERN_INFO "error: %s\nOn cpu: %d\n",
5171+
lua_tostring(lw->L, -1), this_cpu);
5172+
}
5173+
spin_unlock_bh(&lw->lock);
5174+
}
5175+
5176+
int generic_xdp_lua_install_prog(char *lua_script)
51505177
{
5151-
struct lua_state_cpu *sc;
5178+
int cpu;
5179+
struct xdplua_create_work *lw;
51525180

5153-
list_for_each_entry(sc, &lua_state_cpu_list, list) {
5154-
if (luaL_dostring(sc->L, lua_prog)) {
5155-
pr_err(KERN_INFO "error: %s\nOn cpu: %d\n",
5156-
lua_tostring(sc->L, -1), sc->cpu);
5157-
return -EINVAL;
5158-
}
5181+
for_each_possible_cpu(cpu) {
5182+
lw = per_cpu_ptr(&luaworks, cpu);
5183+
strcpy(lw->lua_script, lua_script);
5184+
schedule_work_on(cpu, &lw->work);
51595185
}
51605186
return 0;
51615187
}
@@ -9831,9 +9857,6 @@ static struct pernet_operations __net_initdata default_device_ops = {
98319857
static int __init net_dev_init(void)
98329858
{
98339859
int i, rc = -ENOMEM;
9834-
#ifdef CONFIG_XDP_LUA
9835-
struct lua_state_cpu *new_state_cpu;
9836-
#endif /* CONFIG_XDP_LUA */
98379860

98389861
BUG_ON(!dev_boot_phase);
98399862

@@ -9848,9 +9871,6 @@ static int __init net_dev_init(void)
98489871
INIT_LIST_HEAD(&ptype_base[i]);
98499872

98509873
INIT_LIST_HEAD(&offload_base);
9851-
#ifdef CONFIG_XDP_LUA
9852-
INIT_LIST_HEAD(&lua_state_cpu_list);
9853-
#endif /* CONFIG_XDP_LUA */
98549874

98559875
if (register_pernet_subsys(&netdev_net_ops))
98569876
goto out;
@@ -9862,6 +9882,9 @@ static int __init net_dev_init(void)
98629882
for_each_possible_cpu(i) {
98639883
struct work_struct *flush = per_cpu_ptr(&flush_works, i);
98649884
struct softnet_data *sd = &per_cpu(softnet_data, i);
9885+
#ifdef CONFIG_XDP_LUA
9886+
struct xdplua_create_work *lw = per_cpu_ptr(&luaworks, i);
9887+
#endif
98659888

98669889
INIT_WORK(flush, flush_backlog);
98679890

@@ -9881,24 +9904,18 @@ static int __init net_dev_init(void)
98819904
init_gro_hash(&sd->backlog);
98829905
sd->backlog.poll = process_backlog;
98839906
sd->backlog.weight = weight_p;
9884-
98859907
#ifdef CONFIG_XDP_LUA
9886-
new_state_cpu = (struct lua_state_cpu *)
9887-
kmalloc(sizeof(struct lua_state_cpu), GFP_ATOMIC);
9888-
if (!new_state_cpu)
9889-
continue;
9908+
lw->L = luaL_newstate();
9909+
WARN_ON(!lw->L);
98909910

9891-
new_state_cpu->L = luaL_newstate();
9892-
if (!new_state_cpu->L) {
9893-
kfree(new_state_cpu);
9911+
if (!lw->L)
98949912
continue;
9895-
}
98969913

9897-
luaL_openlibs(new_state_cpu->L);
9898-
lua_pop(new_state_cpu->L, 1);
9899-
new_state_cpu->cpu = i;
9914+
luaL_openlibs(lw->L);
9915+
luaL_requiref(lw->L, "data", luaopen_data, 1);
9916+
lua_pop(lw->L, 1);
99009917

9901-
list_add(&new_state_cpu->list, &lua_state_cpu_list);
9918+
INIT_WORK(&lw->work, per_cpu_xdp_lua_install);
99029919
#endif /* CONFIG_XDP_LUA */
99039920
}
99049921

net/core/filter.c

Lines changed: 35 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
48004810
BPF_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

48214832
BPF_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

48354847
BPF_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

48634884
BPF_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

48774899
BPF_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

48914914
BPF_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

49054929
BPF_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

49204945
BPF_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

49344960
BPF_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

49474974
BPF_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-
49824989
BPF_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

49955003
BPF_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:

samples/bpf/xdplua_map_kern.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@ int xdp_lua_test_map_prog(struct xdp_md *ctx)
2727
char lookupname[] = "lookup";
2828
char updatename[] = "update";
2929

30-
bpf_lua_setstate(ctx);
31-
3230
bpf_lua_pushmap(ctx, &test_map);
3331
bpf_lua_pcall(ctx, updatename, 1, 0);
3432

tools/include/uapi/linux/bpf.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2238,7 +2238,6 @@ union bpf_attr {
22382238
FN(lua_pushmap), \
22392239
FN(lua_pushskb), \
22402240
FN(lua_pushstring), \
2241-
FN(lua_setstate), \
22422241
FN(lua_toboolean), \
22432242
FN(lua_tointeger),
22442243
/* #endif CONFIG_XDP_LUA */

0 commit comments

Comments
 (0)