#include "funnel-workqueue.h"
#include <linux/atomic.h>
#include <linux/cache.h>
#include <linux/completion.h>
#include <linux/err.h>
#include <linux/kthread.h>
#include <linux/percpu.h>
#include "funnel-queue.h"
#include "logger.h"
#include "memory-alloc.h"
#include "numeric.h"
#include "permassert.h"
#include "string-utils.h"
#include "completion.h"
#include "status-codes.h"
static DEFINE_PER_CPU(unsigned int, service_queue_rotor);
struct vdo_work_queue {
char *name;
bool round_robin_mode;
struct vdo_thread *owner;
const struct vdo_work_queue_type *type;
};
struct simple_work_queue {
struct vdo_work_queue common;
struct funnel_queue *priority_lists[VDO_WORK_Q_MAX_PRIORITY + 1];
void *private;
wait_queue_head_t waiting_worker_threads ____cacheline_aligned;
atomic_t idle;
struct task_struct *thread;
struct completion *started;
};
struct round_robin_work_queue {
struct vdo_work_queue common;
struct simple_work_queue **service_queues;
unsigned int num_service_queues;
};
static inline struct simple_work_queue *as_simple_work_queue(struct vdo_work_queue *queue)
{
return ((queue == NULL) ?
NULL : container_of(queue, struct simple_work_queue, common));
}
static inline struct round_robin_work_queue *as_round_robin_work_queue(struct vdo_work_queue *queue)
{
return ((queue == NULL) ?
NULL :
container_of(queue, struct round_robin_work_queue, common));
}
static struct vdo_completion *poll_for_completion(struct simple_work_queue *queue)
{
int i;
for (i = queue->common.type->max_priority; i >= 0; i--) {
struct funnel_queue_entry *link = vdo_funnel_queue_poll(queue->priority_lists[i]);
if (link != NULL)
return container_of(link, struct vdo_completion, work_queue_entry_link);
}
return NULL;
}
static void enqueue_work_queue_completion(struct simple_work_queue *queue,
struct vdo_completion *completion)
{
VDO_ASSERT_LOG_ONLY(completion->my_queue == NULL,
"completion %px (fn %px) to enqueue (%px) is not already queued (%px)",
completion, completion->callback, queue, completion->my_queue);
if (completion->priority == VDO_WORK_Q_DEFAULT_PRIORITY)
completion->priority = queue->common.type->default_priority;
if (VDO_ASSERT(completion->priority <= queue->common.type->max_priority,
"priority is in range for queue") != VDO_SUCCESS)
completion->priority = 0;
completion->my_queue = &queue->common;
vdo_funnel_queue_put(queue->priority_lists[completion->priority],
&completion->work_queue_entry_link);
smp_mb();
if ((atomic_read(&queue->idle) != 1) || (atomic_cmpxchg(&queue->idle, 1, 0) != 1))
return;
wake_up(&queue->waiting_worker_threads);
}
static void run_start_hook(struct simple_work_queue *queue)
{
if (queue->common.type->start != NULL)
queue->common.type->start(queue->private);
}
static void run_finish_hook(struct simple_work_queue *queue)
{
if (queue->common.type->finish != NULL)
queue->common.type->finish(queue->private);
}
static struct vdo_completion *wait_for_next_completion(struct simple_work_queue *queue)
{
struct vdo_completion *completion;
DEFINE_WAIT(wait);
while (true) {
prepare_to_wait(&queue->waiting_worker_threads, &wait,
TASK_INTERRUPTIBLE);
atomic_set(&queue->idle, 1);
smp_mb();
completion = poll_for_completion(queue);
if (completion != NULL)
break;
if (kthread_should_stop())
break;
schedule();
completion = poll_for_completion(queue);
if (completion != NULL)
break;
}
finish_wait(&queue->waiting_worker_threads, &wait);
atomic_set(&queue->idle, 0);
return completion;
}
static void process_completion(struct simple_work_queue *queue,
struct vdo_completion *completion)
{
if (VDO_ASSERT(completion->my_queue == &queue->common,
"completion %px from queue %px marked as being in this queue (%px)",
completion, queue, completion->my_queue) == VDO_SUCCESS)
completion->my_queue = NULL;
vdo_run_completion(completion);
}
static void service_work_queue(struct simple_work_queue *queue)
{
run_start_hook(queue);
while (true) {
struct vdo_completion *completion = poll_for_completion(queue);
if (completion == NULL)
completion = wait_for_next_completion(queue);
if (completion == NULL) {
break;
}
process_completion(queue, completion);
cond_resched();
}
run_finish_hook(queue);
}
static int work_queue_runner(void *ptr)
{
struct simple_work_queue *queue = ptr;
complete(queue->started);
service_work_queue(queue);
return 0;
}
static void free_simple_work_queue(struct simple_work_queue *queue)
{
unsigned int i;
for (i = 0; i <= VDO_WORK_Q_MAX_PRIORITY; i++)
vdo_free_funnel_queue(queue->priority_lists[i]);
vdo_free(queue->common.name);
vdo_free(queue);
}
static void free_round_robin_work_queue(struct round_robin_work_queue *queue)
{
struct simple_work_queue **queue_table = queue->service_queues;
unsigned int count = queue->num_service_queues;
unsigned int i;
queue->service_queues = NULL;
for (i = 0; i < count; i++)
free_simple_work_queue(queue_table[i]);
vdo_free(queue_table);
vdo_free(queue->common.name);
vdo_free(queue);
}
void vdo_free_work_queue(struct vdo_work_queue *queue)
{
if (queue == NULL)
return;
vdo_finish_work_queue(queue);
if (queue->round_robin_mode)
free_round_robin_work_queue(as_round_robin_work_queue(queue));
else
free_simple_work_queue(as_simple_work_queue(queue));
}
static int make_simple_work_queue(const char *thread_name_prefix, const char *name,
struct vdo_thread *owner, void *private,
const struct vdo_work_queue_type *type,
struct simple_work_queue **queue_ptr)
{
DECLARE_COMPLETION_ONSTACK(started);
struct simple_work_queue *queue;
int i;
struct task_struct *thread = NULL;
int result;
VDO_ASSERT_LOG_ONLY((type->max_priority <= VDO_WORK_Q_MAX_PRIORITY),
"queue priority count %u within limit %u", type->max_priority,
VDO_WORK_Q_MAX_PRIORITY);
result = vdo_allocate(1, "simple work queue", &queue);
if (result != VDO_SUCCESS)
return result;
queue->private = private;
queue->started = &started;
queue->common.type = type;
queue->common.owner = owner;
init_waitqueue_head(&queue->waiting_worker_threads);
result = vdo_duplicate_string(name, "queue name", &queue->common.name);
if (result != VDO_SUCCESS) {
vdo_free(queue);
return -ENOMEM;
}
for (i = 0; i <= type->max_priority; i++) {
result = vdo_make_funnel_queue(&queue->priority_lists[i]);
if (result != VDO_SUCCESS) {
free_simple_work_queue(queue);
return result;
}
}
thread = kthread_run(work_queue_runner, queue, "%s:%s", thread_name_prefix,
queue->common.name);
if (IS_ERR(thread)) {
free_simple_work_queue(queue);
return (int) PTR_ERR(thread);
}
queue->thread = thread;
wait_for_completion(&started);
*queue_ptr = queue;
return VDO_SUCCESS;
}
int vdo_make_work_queue(const char *thread_name_prefix, const char *name,
struct vdo_thread *owner, const struct vdo_work_queue_type *type,
unsigned int thread_count, void *thread_privates[],
struct vdo_work_queue **queue_ptr)
{
struct round_robin_work_queue *queue;
int result;
char thread_name[TASK_COMM_LEN];
unsigned int i;
if (thread_count == 1) {
struct simple_work_queue *simple_queue;
void *context = ((thread_privates != NULL) ? thread_privates[0] : NULL);
result = make_simple_work_queue(thread_name_prefix, name, owner, context,
type, &simple_queue);
if (result == VDO_SUCCESS)
*queue_ptr = &simple_queue->common;
return result;
}
result = vdo_allocate(1, "round-robin work queue", &queue);
if (result != VDO_SUCCESS)
return result;
result = vdo_allocate(thread_count, "subordinate work queues", &queue->service_queues);
if (result != VDO_SUCCESS) {
vdo_free(queue);
return result;
}
queue->num_service_queues = thread_count;
queue->common.round_robin_mode = true;
queue->common.owner = owner;
result = vdo_duplicate_string(name, "queue name", &queue->common.name);
if (result != VDO_SUCCESS) {
vdo_free(queue->service_queues);
vdo_free(queue);
return -ENOMEM;
}
*queue_ptr = &queue->common;
for (i = 0; i < thread_count; i++) {
void *context = ((thread_privates != NULL) ? thread_privates[i] : NULL);
snprintf(thread_name, sizeof(thread_name), "%s%u", name, i);
result = make_simple_work_queue(thread_name_prefix, thread_name, owner,
context, type, &queue->service_queues[i]);
if (result != VDO_SUCCESS) {
queue->num_service_queues = i;
vdo_free_work_queue(vdo_forget(*queue_ptr));
return result;
}
}
return VDO_SUCCESS;
}
static void finish_simple_work_queue(struct simple_work_queue *queue)
{
if (queue->thread == NULL)
return;
kthread_stop(queue->thread);
queue->thread = NULL;
}
static void finish_round_robin_work_queue(struct round_robin_work_queue *queue)
{
struct simple_work_queue **queue_table = queue->service_queues;
unsigned int count = queue->num_service_queues;
unsigned int i;
for (i = 0; i < count; i++)
finish_simple_work_queue(queue_table[i]);
}
void vdo_finish_work_queue(struct vdo_work_queue *queue)
{
if (queue == NULL)
return;
if (queue->round_robin_mode)
finish_round_robin_work_queue(as_round_robin_work_queue(queue));
else
finish_simple_work_queue(as_simple_work_queue(queue));
}
static void dump_simple_work_queue(struct simple_work_queue *queue)
{
const char *thread_status = "no threads";
char task_state_report = '-';
if (queue->thread != NULL) {
task_state_report = task_state_to_char(queue->thread);
thread_status = atomic_read(&queue->idle) ? "idle" : "running";
}
vdo_log_info("workQ %px (%s) %s (%c)", &queue->common, queue->common.name,
thread_status, task_state_report);
}
void vdo_dump_work_queue(struct vdo_work_queue *queue)
{
if (queue->round_robin_mode) {
struct round_robin_work_queue *round_robin = as_round_robin_work_queue(queue);
unsigned int i;
for (i = 0; i < round_robin->num_service_queues; i++)
dump_simple_work_queue(round_robin->service_queues[i]);
} else {
dump_simple_work_queue(as_simple_work_queue(queue));
}
}
static void get_function_name(void *pointer, char *buffer, size_t buffer_length)
{
if (pointer == NULL) {
strscpy(buffer, "-", buffer_length);
} else {
char *space;
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wformat"
snprintf(buffer, buffer_length, "%.*ps", buffer_length - 1, pointer);
#pragma GCC diagnostic pop
space = strchr(buffer, ' ');
if (space != NULL)
*space = '\0';
}
}
void vdo_dump_completion_to_buffer(struct vdo_completion *completion, char *buffer,
size_t length)
{
size_t current_length =
scnprintf(buffer, length, "%.*s/", TASK_COMM_LEN,
(completion->my_queue == NULL ? "-" : completion->my_queue->name));
if (current_length < length - 1) {
get_function_name((void *) completion->callback, buffer + current_length,
length - current_length);
}
}
void vdo_enqueue_work_queue(struct vdo_work_queue *queue,
struct vdo_completion *completion)
{
struct simple_work_queue *simple_queue = NULL;
if (!queue->round_robin_mode) {
simple_queue = as_simple_work_queue(queue);
} else {
struct round_robin_work_queue *round_robin = as_round_robin_work_queue(queue);
unsigned int rotor = this_cpu_inc_return(service_queue_rotor);
unsigned int index = rotor % round_robin->num_service_queues;
simple_queue = round_robin->service_queues[index];
}
enqueue_work_queue_completion(simple_queue, completion);
}
static struct simple_work_queue *get_current_thread_work_queue(void)
{
if (in_interrupt())
return NULL;
if (kthread_func(current) != work_queue_runner)
return NULL;
return kthread_data(current);
}
struct vdo_work_queue *vdo_get_current_work_queue(void)
{
struct simple_work_queue *queue = get_current_thread_work_queue();
return (queue == NULL) ? NULL : &queue->common;
}
struct vdo_thread *vdo_get_work_queue_owner(struct vdo_work_queue *queue)
{
return queue->owner;
}
void *vdo_get_work_queue_private_data(void)
{
struct simple_work_queue *queue = get_current_thread_work_queue();
return (queue != NULL) ? queue->private : NULL;
}
bool vdo_work_queue_type_is(struct vdo_work_queue *queue,
const struct vdo_work_queue_type *type)
{
return (queue->type == type);
}