Commit 1a5645bc authored by Frederic Weisbecker's avatar Frederic Weisbecker Committed by David S. Miller

connector: create connector workqueue only while needed once

The netlink connector uses its own workqueue to relay the datas sent
from userspace to the appropriate callback.  If you launch the test
from Documentation/connector and change it a bit to send a high flow
of data, you will see thousands of events coming to the "cqueue"
workqueue by looking at the workqueue tracer.

This flow of events can be sent very quickly. So, to not encumber the
kevent workqueue and delay other jobs, the "cqueue" workqueue should
remain.

But this workqueue is pointless most of the time, it will always be
created (assuming you have built it of course) although only
developpers with specific needs will use it.

So avoid this "most of the time useless task", this patch proposes to
create this workqueue only when needed once.  The first jobs to be
sent to connector callbacks will be sent to kevent while the "cqueue"
thread creation will be scheduled to kevent too.

The following jobs will continue to be scheduled to keventd until the
cqueue workqueue is created, and then the rest of the jobs will
continue to perform as usual, through this dedicated workqueue.

Each time I tested this patch, only the first event was sent to
keventd, the rest has been sent to cqueue which have been created
quickly.

Also, this patch fixes some trailing whitespaces on the connector files.
Signed-off-by: default avatarFrederic Weisbecker <fweisbec@gmail.com>
Acked-by: default avatarEvgeniy Polyakov <zbr@ioremap.net>
Signed-off-by: default avatarDavid S. Miller <davem@davemloft.net>
parent f15fbcd7
...@@ -31,6 +31,48 @@ ...@@ -31,6 +31,48 @@
#include <linux/connector.h> #include <linux/connector.h>
#include <linux/delay.h> #include <linux/delay.h>
/*
* This job is sent to the kevent workqueue.
* While no event is once sent to any callback, the connector workqueue
* is not created to avoid a useless waiting kernel task.
* Once the first event is received, we create this dedicated workqueue which
* is necessary because the flow of data can be high and we don't want
* to encumber keventd with that.
*/
static void cn_queue_create(struct work_struct *work)
{
struct cn_queue_dev *dev;
dev = container_of(work, struct cn_queue_dev, wq_creation);
dev->cn_queue = create_singlethread_workqueue(dev->name);
/* If we fail, we will use keventd for all following connector jobs */
WARN_ON(!dev->cn_queue);
}
/*
* Queue a data sent to a callback.
* If the connector workqueue is already created, we queue the job on it.
* Otherwise, we queue the job to kevent and queue the connector workqueue
* creation too.
*/
int queue_cn_work(struct cn_callback_entry *cbq, struct work_struct *work)
{
struct cn_queue_dev *pdev = cbq->pdev;
if (likely(pdev->cn_queue))
return queue_work(pdev->cn_queue, work);
/* Don't create the connector workqueue twice */
if (atomic_inc_return(&pdev->wq_requested) == 1)
schedule_work(&pdev->wq_creation);
else
atomic_dec(&pdev->wq_requested);
return schedule_work(work);
}
void cn_queue_wrapper(struct work_struct *work) void cn_queue_wrapper(struct work_struct *work)
{ {
struct cn_callback_entry *cbq = struct cn_callback_entry *cbq =
...@@ -65,6 +107,9 @@ static struct cn_callback_entry *cn_queue_alloc_callback_entry(char *name, struc ...@@ -65,6 +107,9 @@ static struct cn_callback_entry *cn_queue_alloc_callback_entry(char *name, struc
static void cn_queue_free_callback(struct cn_callback_entry *cbq) static void cn_queue_free_callback(struct cn_callback_entry *cbq)
{ {
/* The first jobs have been sent to kevent, flush them too */
flush_scheduled_work();
if (cbq->pdev->cn_queue)
flush_workqueue(cbq->pdev->cn_queue); flush_workqueue(cbq->pdev->cn_queue);
kfree(cbq); kfree(cbq);
...@@ -143,14 +188,11 @@ struct cn_queue_dev *cn_queue_alloc_dev(char *name, struct sock *nls) ...@@ -143,14 +188,11 @@ struct cn_queue_dev *cn_queue_alloc_dev(char *name, struct sock *nls)
atomic_set(&dev->refcnt, 0); atomic_set(&dev->refcnt, 0);
INIT_LIST_HEAD(&dev->queue_list); INIT_LIST_HEAD(&dev->queue_list);
spin_lock_init(&dev->queue_lock); spin_lock_init(&dev->queue_lock);
init_waitqueue_head(&dev->wq_created);
dev->nls = nls; dev->nls = nls;
dev->cn_queue = create_singlethread_workqueue(dev->name); INIT_WORK(&dev->wq_creation, cn_queue_create);
if (!dev->cn_queue) {
kfree(dev);
return NULL;
}
return dev; return dev;
} }
...@@ -158,9 +200,25 @@ struct cn_queue_dev *cn_queue_alloc_dev(char *name, struct sock *nls) ...@@ -158,9 +200,25 @@ struct cn_queue_dev *cn_queue_alloc_dev(char *name, struct sock *nls)
void cn_queue_free_dev(struct cn_queue_dev *dev) void cn_queue_free_dev(struct cn_queue_dev *dev)
{ {
struct cn_callback_entry *cbq, *n; struct cn_callback_entry *cbq, *n;
long timeout;
DEFINE_WAIT(wait);
/* Flush the first pending jobs queued on kevent */
flush_scheduled_work();
/* If the connector workqueue creation is still pending, wait for it */
prepare_to_wait(&dev->wq_created, &wait, TASK_UNINTERRUPTIBLE);
if (atomic_read(&dev->wq_requested) && !dev->cn_queue) {
timeout = schedule_timeout(HZ * 2);
if (!timeout && !dev->cn_queue)
WARN_ON(1);
}
finish_wait(&dev->wq_created, &wait);
if (dev->cn_queue) {
flush_workqueue(dev->cn_queue); flush_workqueue(dev->cn_queue);
destroy_workqueue(dev->cn_queue); destroy_workqueue(dev->cn_queue);
}
spin_lock_bh(&dev->queue_lock); spin_lock_bh(&dev->queue_lock);
list_for_each_entry_safe(cbq, n, &dev->queue_list, callback_entry) list_for_each_entry_safe(cbq, n, &dev->queue_list, callback_entry)
......
...@@ -145,8 +145,7 @@ static int cn_call_callback(struct cn_msg *msg, void (*destruct_data)(void *), v ...@@ -145,8 +145,7 @@ static int cn_call_callback(struct cn_msg *msg, void (*destruct_data)(void *), v
__cbq->data.ddata = data; __cbq->data.ddata = data;
__cbq->data.destruct_data = destruct_data; __cbq->data.destruct_data = destruct_data;
if (queue_work(dev->cbdev->cn_queue, if (queue_cn_work(__cbq, &__cbq->work))
&__cbq->work))
err = 0; err = 0;
else else
err = -EINVAL; err = -EINVAL;
...@@ -163,10 +162,12 @@ static int cn_call_callback(struct cn_msg *msg, void (*destruct_data)(void *), v ...@@ -163,10 +162,12 @@ static int cn_call_callback(struct cn_msg *msg, void (*destruct_data)(void *), v
d->destruct_data = destruct_data; d->destruct_data = destruct_data;
d->free = __new_cbq; d->free = __new_cbq;
__new_cbq->pdev = __cbq->pdev;
INIT_WORK(&__new_cbq->work, INIT_WORK(&__new_cbq->work,
&cn_queue_wrapper); &cn_queue_wrapper);
if (queue_work(dev->cbdev->cn_queue, if (queue_cn_work(__new_cbq,
&__new_cbq->work)) &__new_cbq->work))
err = 0; err = 0;
else { else {
......
...@@ -109,6 +109,12 @@ struct cn_queue_dev { ...@@ -109,6 +109,12 @@ struct cn_queue_dev {
unsigned char name[CN_CBQ_NAMELEN]; unsigned char name[CN_CBQ_NAMELEN];
struct workqueue_struct *cn_queue; struct workqueue_struct *cn_queue;
/* Sent to kevent to create cn_queue only when needed */
struct work_struct wq_creation;
/* Tell if the wq_creation job is pending/completed */
atomic_t wq_requested;
/* Wait for cn_queue to be created */
wait_queue_head_t wq_created;
struct list_head queue_list; struct list_head queue_list;
spinlock_t queue_lock; spinlock_t queue_lock;
...@@ -164,6 +170,8 @@ int cn_netlink_send(struct cn_msg *, u32, gfp_t); ...@@ -164,6 +170,8 @@ int cn_netlink_send(struct cn_msg *, u32, gfp_t);
int cn_queue_add_callback(struct cn_queue_dev *dev, char *name, struct cb_id *id, void (*callback)(void *)); int cn_queue_add_callback(struct cn_queue_dev *dev, char *name, struct cb_id *id, void (*callback)(void *));
void cn_queue_del_callback(struct cn_queue_dev *dev, struct cb_id *id); void cn_queue_del_callback(struct cn_queue_dev *dev, struct cb_id *id);
int queue_cn_work(struct cn_callback_entry *cbq, struct work_struct *work);
struct cn_queue_dev *cn_queue_alloc_dev(char *name, struct sock *); struct cn_queue_dev *cn_queue_alloc_dev(char *name, struct sock *);
void cn_queue_free_dev(struct cn_queue_dev *dev); void cn_queue_free_dev(struct cn_queue_dev *dev);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment