#include <sys/types.h>
#include <sys/malloc.h>
#include <sys/gtaskqueue.h>
#include <sys/proc.h>
#include <sys/sched.h>
#include <linux/compiler.h>
#include <linux/interrupt.h>
#include <linux/compat.h>
#define TASKLET_ST_IDLE 0
#define TASKLET_ST_BUSY 1
#define TASKLET_ST_EXEC 2
#define TASKLET_ST_LOOP 3
#define TASKLET_ST_CMPSET(ts, old, new) \
atomic_cmpset_int((volatile u_int *)&(ts)->tasklet_state, old, new)
#define TASKLET_ST_SET(ts, new) \
WRITE_ONCE(*(volatile u_int *)&(ts)->tasklet_state, new)
#define TASKLET_ST_GET(ts) \
READ_ONCE(*(volatile u_int *)&(ts)->tasklet_state)
struct tasklet_worker {
struct mtx mtx;
TAILQ_HEAD(tasklet_list, tasklet_struct) head;
struct grouptask gtask;
} __aligned(CACHE_LINE_SIZE);
#define TASKLET_WORKER_LOCK(tw) mtx_lock(&(tw)->mtx)
#define TASKLET_WORKER_UNLOCK(tw) mtx_unlock(&(tw)->mtx)
DPCPU_DEFINE_STATIC(struct tasklet_worker, tasklet_worker);
static void
tasklet_handler(void *arg)
{
struct tasklet_worker *tw = (struct tasklet_worker *)arg;
struct tasklet_struct *ts;
struct tasklet_struct *last;
linux_set_current(curthread);
TASKLET_WORKER_LOCK(tw);
last = TAILQ_LAST(&tw->head, tasklet_list);
while (1) {
ts = TAILQ_FIRST(&tw->head);
if (ts == NULL)
break;
TAILQ_REMOVE(&tw->head, ts, entry);
if (!atomic_read(&ts->count)) {
TASKLET_WORKER_UNLOCK(tw);
do {
TASKLET_ST_SET(ts, TASKLET_ST_EXEC);
if (ts->use_callback)
ts->callback(ts);
else
ts->func(ts->data);
} while (TASKLET_ST_CMPSET(ts, TASKLET_ST_EXEC,
TASKLET_ST_IDLE) == 0);
TASKLET_WORKER_LOCK(tw);
} else {
TAILQ_INSERT_TAIL(&tw->head, ts, entry);
}
if (ts == last)
break;
}
TASKLET_WORKER_UNLOCK(tw);
}
static void
tasklet_subsystem_init(void *arg __unused)
{
struct tasklet_worker *tw;
char buf[32];
int i;
CPU_FOREACH(i) {
if (CPU_ABSENT(i))
continue;
tw = DPCPU_ID_PTR(i, tasklet_worker);
mtx_init(&tw->mtx, "linux_tasklet", NULL, MTX_DEF);
TAILQ_INIT(&tw->head);
GROUPTASK_INIT(&tw->gtask, 0, tasklet_handler, tw);
snprintf(buf, sizeof(buf), "softirq%d", i);
taskqgroup_attach_cpu(qgroup_softirq, &tw->gtask,
"tasklet", i, NULL, NULL, buf);
}
}
SYSINIT(linux_tasklet, SI_SUB_TASKQ, SI_ORDER_THIRD, tasklet_subsystem_init, NULL);
static void
tasklet_subsystem_uninit(void *arg __unused)
{
struct tasklet_worker *tw;
int i;
taskqgroup_drain_all(qgroup_softirq);
CPU_FOREACH(i) {
if (CPU_ABSENT(i))
continue;
tw = DPCPU_ID_PTR(i, tasklet_worker);
taskqgroup_detach(qgroup_softirq, &tw->gtask);
mtx_destroy(&tw->mtx);
}
}
SYSUNINIT(linux_tasklet, SI_SUB_TASKQ, SI_ORDER_THIRD, tasklet_subsystem_uninit, NULL);
void
tasklet_init(struct tasklet_struct *ts,
tasklet_func_t *func, unsigned long data)
{
ts->entry.tqe_prev = NULL;
ts->entry.tqe_next = NULL;
ts->func = func;
ts->callback = NULL;
ts->data = data;
atomic_set_int(&ts->tasklet_state, TASKLET_ST_IDLE);
atomic_set(&ts->count, 0);
ts->use_callback = false;
}
void
tasklet_setup(struct tasklet_struct *ts, tasklet_callback_t *c)
{
ts->entry.tqe_prev = NULL;
ts->entry.tqe_next = NULL;
ts->func = NULL;
ts->callback = c;
ts->data = 0;
atomic_set_int(&ts->tasklet_state, TASKLET_ST_IDLE);
atomic_set(&ts->count, 0);
ts->use_callback = true;
}
void
local_bh_enable(void)
{
sched_unpin();
}
void
local_bh_disable(void)
{
sched_pin();
}
void
tasklet_schedule(struct tasklet_struct *ts)
{
if (atomic_read(&ts->count))
return;
if (TASKLET_ST_CMPSET(ts, TASKLET_ST_EXEC, TASKLET_ST_LOOP)) {
} else if (TASKLET_ST_CMPSET(ts, TASKLET_ST_IDLE, TASKLET_ST_BUSY)) {
struct tasklet_worker *tw;
tw = &DPCPU_GET(tasklet_worker);
TASKLET_WORKER_LOCK(tw);
TAILQ_INSERT_TAIL(&tw->head, ts, entry);
GROUPTASK_ENQUEUE(&tw->gtask);
TASKLET_WORKER_UNLOCK(tw);
} else {
}
}
void
tasklet_kill(struct tasklet_struct *ts)
{
WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL, "tasklet_kill() can sleep");
while (TASKLET_ST_GET(ts) != TASKLET_ST_IDLE)
pause("W", 1);
}
void
tasklet_enable(struct tasklet_struct *ts)
{
atomic_dec(&ts->count);
}
void
tasklet_disable(struct tasklet_struct *ts)
{
atomic_inc(&ts->count);
tasklet_unlock_wait(ts);
}
void
tasklet_disable_nosync(struct tasklet_struct *ts)
{
atomic_inc(&ts->count);
barrier();
}
int
tasklet_trylock(struct tasklet_struct *ts)
{
return (TASKLET_ST_CMPSET(ts, TASKLET_ST_IDLE, TASKLET_ST_BUSY));
}
void
tasklet_unlock(struct tasklet_struct *ts)
{
TASKLET_ST_SET(ts, TASKLET_ST_IDLE);
}
void
tasklet_unlock_wait(struct tasklet_struct *ts)
{
WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL, "tasklet_kill() can sleep");
while (TASKLET_ST_GET(ts) != TASKLET_ST_IDLE)
pause("W", 1);
}