Skip to content

Commit aa6b663

Browse files
Fix script loading synchronization issue
1 parent d29c749 commit aa6b663

8 files changed

Lines changed: 93 additions & 86 deletions

File tree

include/linux/filter.h

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1229,14 +1229,4 @@ struct bpf_sockopt_kern {
12291229
s32 retval;
12301230
};
12311231

1232-
#ifdef CONFIG_XDP_LUA
1233-
extern struct list_head lua_state_cpu_list;
1234-
1235-
struct lua_state_cpu {
1236-
struct lua_State *L;
1237-
int cpu;
1238-
struct list_head list;
1239-
};
1240-
#endif /* CONFIG_XDP_LUA */
1241-
12421232
#endif /* __LINUX_FILTER_H__ */

include/net/xdp.h

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,15 @@ 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 {
68+
struct lua_State *L;
69+
spinlock_t *lock;
70+
};
71+
72+
DECLARE_PER_CPU(struct xdplua, xdplua_per_cpu);
73+
#endif /* CONFIG_XDP_LUA */
74+
6675
struct xdp_buff {
6776
void *data;
6877
void *data_end;
@@ -72,7 +81,7 @@ struct xdp_buff {
7281
struct xdp_rxq_info *rxq;
7382
#ifdef CONFIG_XDP_LUA
7483
struct sk_buff *skb;
75-
struct lua_State *L;
84+
struct xdplua *xdplua;
7685
#endif /* CONFIG_XDP_LUA */
7786
};
7887

include/uapi/linux/bpf.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2836,9 +2836,10 @@ union bpf_attr {
28362836
FN(lua_pushmap), \
28372837
FN(lua_pushskb), \
28382838
FN(lua_pushstring), \
2839-
FN(lua_setstate), \
28402839
FN(lua_toboolean), \
2841-
FN(lua_tointeger),
2840+
FN(lua_tointeger), \
2841+
FN(lua_putstate), \
2842+
FN(lua_removestate),
28422843
/* #endif CONFIG_XDP_LUA */
28432844

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

net/core/dev.c

Lines changed: 22 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,9 @@
159159

160160
static DEFINE_SPINLOCK(ptype_lock);
161161
static DEFINE_SPINLOCK(offload_lock);
162+
#ifdef CONFIG_XDP_LUA
163+
static DEFINE_PER_CPU(spinlock_t, lua_state_lock);
164+
#endif
162165
struct list_head ptype_base[PTYPE_HASH_SIZE] __read_mostly;
163166
struct list_head ptype_all __read_mostly; /* Taps */
164167
static struct list_head offload_base __read_mostly;
@@ -171,10 +174,6 @@ static int call_netdevice_notifiers_extack(unsigned long val,
171174
struct netlink_ext_ack *extack);
172175
static struct napi_struct *napi_by_id(unsigned int napi_id);
173176

174-
#ifdef CONFIG_XDP_LUA
175-
struct list_head lua_state_cpu_list;
176-
#endif /* CONFIG_XDP_LUA */
177-
178177
/*
179178
* The @dev_base_head list is protected by @dev_base_lock and the rtnl
180179
* semaphore.
@@ -5198,16 +5197,24 @@ static int generic_xdp_install(struct net_device *dev, struct netdev_bpf *xdp)
51985197
}
51995198

52005199
#ifdef CONFIG_XDP_LUA
5200+
DEFINE_PER_CPU(struct xdplua, xdplua_per_cpu);
5201+
52015202
int generic_xdp_lua_install_prog(char *lua_prog)
52025203
{
5203-
struct lua_state_cpu *sc;
5204+
struct xdplua *sc;
5205+
int i;
52045206

5205-
list_for_each_entry(sc, &lua_state_cpu_list, list) {
5207+
for_each_possible_cpu(i) {
5208+
sc = per_cpu_ptr(&xdplua_per_cpu, i);
5209+
spin_lock_bh(sc->lock);
52065210
if (luaL_dostring(sc->L, lua_prog)) {
52075211
pr_err(KERN_INFO "error: %s\nOn cpu: %d\n",
5208-
lua_tostring(sc->L, -1), sc->cpu);
5212+
lua_tostring(sc->L, -1), i);
5213+
spin_unlock_bh(sc->lock);
52095214
return -EINVAL;
52105215
}
5216+
5217+
spin_unlock_bh(sc->lock);
52115218
}
52125219
return 0;
52135220
}
@@ -9830,9 +9837,6 @@ static struct pernet_operations __net_initdata default_device_ops = {
98309837
static int __init net_dev_init(void)
98319838
{
98329839
int i, rc = -ENOMEM;
9833-
#ifdef CONFIG_XDP_LUA
9834-
struct lua_state_cpu *new_state_cpu;
9835-
#endif /* CONFIG_XDP_LUA */
98369840

98379841
BUG_ON(!dev_boot_phase);
98389842

@@ -9847,9 +9851,6 @@ static int __init net_dev_init(void)
98479851
INIT_LIST_HEAD(&ptype_base[i]);
98489852

98499853
INIT_LIST_HEAD(&offload_base);
9850-
#ifdef CONFIG_XDP_LUA
9851-
INIT_LIST_HEAD(&lua_state_cpu_list);
9852-
#endif /* CONFIG_XDP_LUA */
98539854

98549855
if (register_pernet_subsys(&netdev_net_ops))
98559856
goto out;
@@ -9861,6 +9862,7 @@ static int __init net_dev_init(void)
98619862
for_each_possible_cpu(i) {
98629863
struct work_struct *flush = per_cpu_ptr(&flush_works, i);
98639864
struct softnet_data *sd = &per_cpu(softnet_data, i);
9865+
struct xdplua *xdplua = per_cpu_ptr(&xdplua_per_cpu, i);
98649866

98659867
INIT_WORK(flush, flush_backlog);
98669868

@@ -9880,25 +9882,17 @@ static int __init net_dev_init(void)
98809882
init_gro_hash(&sd->backlog);
98819883
sd->backlog.poll = process_backlog;
98829884
sd->backlog.weight = weight_p;
9883-
98849885
#ifdef CONFIG_XDP_LUA
9885-
new_state_cpu = (struct lua_state_cpu *)
9886-
kmalloc(sizeof(struct lua_state_cpu), GFP_ATOMIC);
9887-
if (!new_state_cpu)
9888-
continue;
9889-
9890-
new_state_cpu->L = luaL_newstate();
9891-
if (!new_state_cpu->L) {
9892-
kfree(new_state_cpu);
9886+
xdplua->L = luaL_newstate();
9887+
if (!xdplua->L) {
9888+
kfree(xdplua);
98939889
continue;
98949890
}
98959891

9896-
luaL_openlibs(new_state_cpu->L);
9897-
luaL_requiref(new_state_cpu->L, "data", luaopen_data, 1);
9898-
lua_pop(new_state_cpu->L, 1);
9899-
new_state_cpu->cpu = i;
9900-
9901-
list_add(&new_state_cpu->list, &lua_state_cpu_list);
9892+
luaL_openlibs(xdplua->L);
9893+
luaL_requiref(xdplua->L, "data", luaopen_data, 1);
9894+
lua_pop(xdplua->L, 1);
9895+
xdplua->lock = per_cpu_ptr(&lua_state_lock, i);
99029896
#endif /* CONFIG_XDP_LUA */
99039897
}
99049898

net/core/filter.c

Lines changed: 49 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -5862,7 +5862,7 @@ BPF_CALL_2(bpf_lua_dataref, struct xdp_buff *, ctx, int, offset) {
58625862
if (offset + ctx->data < ctx->data_end) {
58635863
int data_ref;
58645864

5865-
data_ref = ldata_newref(ctx->L, ctx->data + offset,
5865+
data_ref = ldata_newref(ctx->xdplua->L, ctx->data + offset,
58665866
ctx->data_end - ctx->data - offset);
58675867
return data_ref;
58685868
}
@@ -5880,7 +5880,7 @@ static const struct bpf_func_proto bpf_lua_dataref_proto = {
58805880
};
58815881

58825882
BPF_CALL_2(bpf_lua_dataunref, struct xdp_buff *, ctx, int, data_ref) {
5883-
ldata_unref(ctx->L, data_ref);
5883+
ldata_unref(ctx->xdplua->L, data_ref);
58845884
return 0;
58855885
}
58865886

@@ -5895,16 +5895,16 @@ static const struct bpf_func_proto bpf_lua_dataunref_proto = {
58955895

58965896
BPF_CALL_4(bpf_lua_pcall, struct xdp_buff *, ctx, char *, funcname,
58975897
int, num_args, int, num_rets) {
5898-
if (lua_getglobal(ctx->L, funcname) != LUA_TFUNCTION) {
5898+
if (lua_getglobal(ctx->xdplua->L, funcname) != LUA_TFUNCTION) {
58995899
pr_err("function %s not found\n", funcname);
5900-
lua_pop(ctx->L, num_args);
5900+
lua_pop(ctx->xdplua->L, num_args);
59015901
return 0;
59025902
}
59035903

5904-
lua_insert(ctx->L, 1);
5905-
if (lua_pcall(ctx->L, num_args, num_rets, 0)) {
5906-
pr_err("%s\n", lua_tostring(ctx->L, -1));
5907-
lua_pop(ctx->L, 1);
5904+
lua_insert(ctx->xdplua->L, 1);
5905+
if (lua_pcall(ctx->xdplua->L, num_args, num_rets, 0)) {
5906+
pr_err("%s\n", lua_tostring(ctx->xdplua->L, -1));
5907+
lua_pop(ctx->xdplua->L, 1);
59085908
return 0;
59095909
}
59105910
return num_rets;
@@ -5922,7 +5922,7 @@ static const struct bpf_func_proto bpf_lua_pcall_proto = {
59225922
};
59235923

59245924
BPF_CALL_2(bpf_lua_pop, struct xdp_buff *, ctx, int, index) {
5925-
lua_pop(ctx->L, index);
5925+
lua_pop(ctx->xdplua->L, index);
59265926
return 0;
59275927
}
59285928

@@ -5936,7 +5936,7 @@ static const struct bpf_func_proto bpf_lua_pop_proto = {
59365936
};
59375937

59385938
BPF_CALL_2(bpf_lua_pushinteger, struct xdp_buff *, ctx, int, num) {
5939-
lua_pushinteger(ctx->L, num);
5939+
lua_pushinteger(ctx->xdplua->L, num);
59405940
return 0;
59415941
}
59425942

@@ -5950,7 +5950,7 @@ static const struct bpf_func_proto bpf_lua_pushinteger_proto = {
59505950
};
59515951

59525952
BPF_CALL_2(bpf_lua_pushlightuserdata, struct xdp_buff *, ctx, void *, ptr) {
5953-
lua_pushlightuserdata(ctx->L, ptr);
5953+
lua_pushlightuserdata(ctx->xdplua->L, ptr);
59545954
return 0;
59555955
}
59565956

@@ -5964,12 +5964,12 @@ static const struct bpf_func_proto bpf_lua_pushlightuserdata_proto = {
59645964
};
59655965

59665966
BPF_CALL_2(bpf_lua_pushmap, struct xdp_buff *, ctx, struct bpf_map *, map) {
5967-
lua_pushlightuserdata(ctx->L, map);
5967+
lua_pushlightuserdata(ctx->xdplua->L, map);
59685968
return 0;
59695969
}
59705970

59715971
BPF_CALL_3(bpf_lua_pushlstring, struct xdp_buff *, ctx, const char *, str, size_t, len) {
5972-
lua_pushlstring(ctx->L, str, len);
5972+
lua_pushlstring(ctx->xdplua->L, str, len);
59735973
return 0;
59745974
}
59755975

@@ -5993,7 +5993,7 @@ static const struct bpf_func_proto bpf_lua_pushmap_proto = {
59935993
};
59945994

59955995
BPF_CALL_1(bpf_lua_pushskb, struct xdp_buff *, ctx) {
5996-
lua_pushlightuserdata(ctx->L, ctx->skb);
5996+
lua_pushlightuserdata(ctx->xdplua->L, ctx->skb);
59975997
return 0;
59985998
}
59995999

@@ -6006,7 +6006,7 @@ static const struct bpf_func_proto bpf_lua_pushskb_proto = {
60066006
};
60076007

60086008
BPF_CALL_2(bpf_lua_pushstring, struct xdp_buff *, ctx, const char *, str) {
6009-
lua_pushstring(ctx->L, str);
6009+
lua_pushstring(ctx->xdplua->L, str);
60106010
return 0;
60116011
}
60126012

@@ -6019,29 +6019,8 @@ static const struct bpf_func_proto bpf_lua_pushstring_proto = {
60196019
.arg2_type = ARG_ANYTHING,
60206020
};
60216021

6022-
BPF_CALL_1(bpf_lua_setstate, struct xdp_buff *, ctx){
6023-
struct lua_state_cpu *sc;
6024-
int cpu = smp_processor_id();
6025-
6026-
list_for_each_entry(sc, &lua_state_cpu_list, list) {
6027-
if (sc->cpu == cpu) {
6028-
ctx->L = sc->L;
6029-
break;
6030-
}
6031-
}
6032-
return 0;
6033-
}
6034-
6035-
static const struct bpf_func_proto bpf_lua_setstate_proto = {
6036-
.func = bpf_lua_setstate,
6037-
.gpl_only = false,
6038-
.pkt_access = false,
6039-
.ret_type = RET_VOID,
6040-
.arg1_type = ARG_PTR_TO_CTX,
6041-
};
6042-
60436022
BPF_CALL_2(bpf_lua_toboolean, struct xdp_buff *, ctx, int, index) {
6044-
return lua_toboolean(ctx->L, index);
6023+
return lua_toboolean(ctx->xdplua->L, index);
60456024
}
60466025

60476026
static const struct bpf_func_proto bpf_lua_toboolean_proto = {
@@ -6054,7 +6033,7 @@ static const struct bpf_func_proto bpf_lua_toboolean_proto = {
60546033
};
60556034

60566035
BPF_CALL_2(bpf_lua_tointeger, struct xdp_buff *, ctx, int, index) {
6057-
return lua_tointeger(ctx->L, index);
6036+
return lua_tointeger(ctx->xdplua->L, index);
60586037
}
60596038

60606039
static const struct bpf_func_proto bpf_lua_tointeger_proto = {
@@ -6065,6 +6044,34 @@ static const struct bpf_func_proto bpf_lua_tointeger_proto = {
60656044
.arg1_type = ARG_PTR_TO_CTX,
60666045
.arg2_type = ARG_ANYTHING,
60676046
};
6047+
6048+
BPF_CALL_1(bpf_lua_putstate, struct xdp_buff *, ctx) {
6049+
ctx->xdplua = this_cpu_ptr(&xdplua_per_cpu);
6050+
spin_lock(ctx->xdplua->lock);
6051+
return 0;
6052+
}
6053+
6054+
static const struct bpf_func_proto bpf_lua_putstate_proto = {
6055+
.func = bpf_lua_putstate,
6056+
.gpl_only = false,
6057+
.pkt_access = false,
6058+
.ret_type = RET_VOID,
6059+
.arg1_type = ARG_PTR_TO_CTX,
6060+
};
6061+
6062+
BPF_CALL_1(bpf_lua_removestate, struct xdp_buff *, ctx) {
6063+
spin_unlock(ctx->xdplua->lock);
6064+
ctx->xdplua = NULL;
6065+
return 0;
6066+
}
6067+
6068+
static const struct bpf_func_proto bpf_lua_removestate_proto = {
6069+
.func = bpf_lua_removestate,
6070+
.gpl_only = false,
6071+
.pkt_access = false,
6072+
.ret_type = RET_VOID,
6073+
.arg1_type = ARG_PTR_TO_CTX,
6074+
};
60686075
#endif /* CONFIG_XDP_LUA */
60696076

60706077
bool bpf_helper_changes_pkt_data(void *func)
@@ -6162,12 +6169,14 @@ bpf_base_func_proto(enum bpf_func_id func_id)
61626169
return &bpf_lua_pushskb_proto;
61636170
case BPF_FUNC_lua_pushstring:
61646171
return &bpf_lua_pushstring_proto;
6165-
case BPF_FUNC_lua_setstate:
6166-
return &bpf_lua_setstate_proto;
61676172
case BPF_FUNC_lua_toboolean:
61686173
return &bpf_lua_toboolean_proto;
61696174
case BPF_FUNC_lua_tointeger:
61706175
return &bpf_lua_tointeger_proto;
6176+
case BPF_FUNC_lua_putstate:
6177+
return &bpf_lua_putstate_proto;
6178+
case BPF_FUNC_lua_removestate:
6179+
return &bpf_lua_removestate_proto;
61716180
#endif /* CONFIG_XDP_LUA */
61726181
default:
61736182
return NULL;

samples/bpf/xdplua_map_kern.c

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

30-
bpf_lua_setstate(ctx);
30+
bpf_lua_putstate(ctx);
3131
bpf_lua_pushmap(ctx, &test_map);
3232
bpf_lua_pcall(ctx, updatename, 1, 0);
3333

3434
bpf_lua_pushmap(ctx, &test_map);
3535
bpf_lua_pcall(ctx, lookupname, 1, 0);
36+
bpf_lua_removestate(ctx);
3637

3738
return XDP_PASS;
3839
}

tools/include/uapi/linux/bpf.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2836,9 +2836,10 @@ union bpf_attr {
28362836
FN(lua_pushmap), \
28372837
FN(lua_pushskb), \
28382838
FN(lua_pushstring), \
2839-
FN(lua_setstate), \
28402839
FN(lua_toboolean), \
2841-
FN(lua_tointeger),
2840+
FN(lua_tointeger), \
2841+
FN(lua_putstate), \
2842+
FN(lua_removestate),
28422843
/* #endif CONFIG_XDPLUA */
28432844

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

0 commit comments

Comments
 (0)