Skip to content

Commit 1947a56

Browse files
fixup! Fix script loading synchronization issue
1 parent 17af788 commit 1947a56

7 files changed

Lines changed: 120 additions & 93 deletions

File tree

include/net/xdp.h

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,14 +64,18 @@ struct xdp_rxq_info {
6464
} ____cacheline_aligned; /* perf critical, avoid false-sharing */
6565

6666
#ifdef CONFIG_XDP_LUA
67-
struct xdplua {
67+
struct xdplua_create_work {
68+
char *lua_script;
6869
struct lua_State *L;
69-
spinlock_t *lock;
70+
struct work_struct work;
71+
spinlock_t lock;
72+
bool init;
7073
};
7174

72-
DECLARE_PER_CPU(struct xdplua, xdplua_per_cpu);
75+
DECLARE_PER_CPU(struct xdplua_create_work, luaworks);
7376
#endif /* CONFIG_XDP_LUA */
7477

78+
7579
struct xdp_buff {
7680
void *data;
7781
void *data_end;
@@ -81,7 +85,7 @@ struct xdp_buff {
8185
struct xdp_rxq_info *rxq;
8286
#ifdef CONFIG_XDP_LUA
8387
struct sk_buff *skb;
84-
struct xdplua *xdplua;
88+
struct lua_State *L;
8589
#endif /* CONFIG_XDP_LUA */
8690
};
8791

include/uapi/linux/bpf.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2839,8 +2839,6 @@ union bpf_attr {
28392839
FN(lua_pushstring), \
28402840
FN(lua_toboolean), \
28412841
FN(lua_tointeger), \
2842-
FN(lua_putstate), \
2843-
FN(lua_removestate), \
28442842
FN(lua_newpacket), \
28452843
FN(lua_type),
28462844
/* #endif CONFIG_XDP_LUA */

net/core/dev.c

Lines changed: 43 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@
7171
#ifdef CONFIG_XDP_LUA
7272
#include <lua.h>
7373
#include <lauxlib.h>
74+
#include <lstate.h>
7475
#include <lualib.h>
7576
#include <luadata.h>
7677
#endif /* CONFIG_XDP_LUA */
@@ -160,7 +161,7 @@
160161
static DEFINE_SPINLOCK(ptype_lock);
161162
static DEFINE_SPINLOCK(offload_lock);
162163
#ifdef CONFIG_XDP_LUA
163-
static DEFINE_PER_CPU(spinlock_t, lua_state_lock);
164+
DEFINE_PER_CPU(struct xdplua_create_work, luaworks);
164165
#endif
165166
struct list_head ptype_base[PTYPE_HASH_SIZE] __read_mostly;
166167
struct list_head ptype_all __read_mostly; /* Taps */
@@ -4382,11 +4383,21 @@ static u32 netif_receive_generic_xdp(struct sk_buff *skb,
43824383
rxqueue = netif_get_rxqueue(skb);
43834384
xdp->rxq = &rxqueue->xdp_rxq;
43844385
#ifdef CONFIG_XDP_LUA
4386+
struct xdplua_create_work *lw = this_cpu_ptr(&luaworks);
4387+
43854388
xdp->skb = skb;
4389+
xdp->L = lw->L;
43864390
#endif /* CONFIG_XDP_LUA */
43874391

43884392
act = bpf_prog_run_xdp(xdp_prog, xdp);
43894393

4394+
#ifdef CONFIG_XDP_LUA
4395+
if (lw->init) {
4396+
lw->init = false;
4397+
spin_unlock(&lw->lock);
4398+
}
4399+
#endif
4400+
43904401
/* check if bpf_xdp_adjust_head was used */
43914402
off = xdp->data - orig_data;
43924403
if (off) {
@@ -5197,24 +5208,30 @@ static int generic_xdp_install(struct net_device *dev, struct netdev_bpf *xdp)
51975208
}
51985209

51995210
#ifdef CONFIG_XDP_LUA
5200-
DEFINE_PER_CPU(struct xdplua, xdplua_per_cpu);
52015211

5202-
int generic_xdp_lua_install_prog(char *lua_prog)
5203-
{
5204-
struct xdplua *sc;
5205-
int i;
5212+
static void per_cpu_xdp_lua_install(struct work_struct *w) {
5213+
int this_cpu = smp_processor_id();
5214+
struct xdplua_create_work *lw =
5215+
container_of(w, struct xdplua_create_work, work);
52065216

5207-
for_each_possible_cpu(i) {
5208-
sc = per_cpu_ptr(&xdplua_per_cpu, i);
5209-
spin_lock_bh(sc->lock);
5210-
if (luaL_dostring(sc->L, lua_prog)) {
5211-
pr_err(KERN_INFO "error: %s\nOn cpu: %d\n",
5212-
lua_tostring(sc->L, -1), i);
5213-
spin_unlock_bh(sc->lock);
5214-
return -EINVAL;
5215-
}
5217+
spin_lock_bh(&lw->lock);
5218+
if (luaL_dostring(lw->L, lw->lua_script)) {
5219+
pr_err(KERN_INFO "error: %s\nOn cpu: %d\n",
5220+
lua_tostring(lw->L, -1), this_cpu);
5221+
}
5222+
spin_unlock_bh(&lw->lock);
5223+
}
5224+
5225+
int generic_xdp_lua_install_prog(char *lua_script)
5226+
{
5227+
int cpu;
5228+
struct xdplua_create_work *lw;
52165229

5217-
spin_unlock_bh(sc->lock);
5230+
for_each_possible_cpu(cpu) {
5231+
lw = per_cpu_ptr(&luaworks, cpu);
5232+
lw->lua_script = lua_script;
5233+
INIT_WORK(&lw->work, per_cpu_xdp_lua_install);
5234+
schedule_work_on(cpu, &lw->work);
52185235
}
52195236
return 0;
52205237
}
@@ -9862,7 +9879,9 @@ static int __init net_dev_init(void)
98629879
for_each_possible_cpu(i) {
98639880
struct work_struct *flush = per_cpu_ptr(&flush_works, i);
98649881
struct softnet_data *sd = &per_cpu(softnet_data, i);
9865-
struct xdplua *xdplua = per_cpu_ptr(&xdplua_per_cpu, i);
9882+
#ifdef CONFIG_XDP_LUA
9883+
struct xdplua_create_work *lw = per_cpu_ptr(&luaworks, i);
9884+
#endif
98669885

98679886
INIT_WORK(flush, flush_backlog);
98689887

@@ -9883,16 +9902,15 @@ static int __init net_dev_init(void)
98839902
sd->backlog.poll = process_backlog;
98849903
sd->backlog.weight = weight_p;
98859904
#ifdef CONFIG_XDP_LUA
9886-
xdplua->L = luaL_newstate();
9887-
if (!xdplua->L) {
9888-
kfree(xdplua);
9905+
lw->L = luaL_newstate();
9906+
WARN_ON(!lw->L)
9907+
9908+
if (!lw->L)
98899909
continue;
9890-
}
98919910

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);
9911+
luaL_openlibs(lw->L);
9912+
luaL_requiref(lw->L, "data", luaopen_data, 1);
9913+
lua_pop(lw->L, 1);
98969914
#endif /* CONFIG_XDP_LUA */
98979915
}
98989916

0 commit comments

Comments
 (0)