Skip to content

Commit 1a5645b

Browse files
fweisbecdavem330
authored andcommitted
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: Frederic Weisbecker <fweisbec@gmail.com> Acked-by: Evgeniy Polyakov <zbr@ioremap.net> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent f15fbcd commit 1a5645b

3 files changed

Lines changed: 87 additions & 20 deletions

File tree

‎drivers/connector/cn_queue.c‎

Lines changed: 69 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
/*
22
* cn_queue.c
3-
*
3+
*
44
* 2004-2005 Copyright (c) Evgeniy Polyakov <johnpol@2ka.mipt.ru>
55
* All rights reserved.
6-
*
6+
*
77
* This program is free software; you can redistribute it and/or modify
88
* it under the terms of the GNU General Public License as published by
99
* the Free Software Foundation; either version 2 of the License, or
@@ -31,6 +31,48 @@
3131
#include <linux/connector.h>
3232
#include <linux/delay.h>
3333

34+
35+
/*
36+
* This job is sent to the kevent workqueue.
37+
* While no event is once sent to any callback, the connector workqueue
38+
* is not created to avoid a useless waiting kernel task.
39+
* Once the first event is received, we create this dedicated workqueue which
40+
* is necessary because the flow of data can be high and we don't want
41+
* to encumber keventd with that.
42+
*/
43+
static void cn_queue_create(struct work_struct *work)
44+
{
45+
struct cn_queue_dev *dev;
46+
47+
dev = container_of(work, struct cn_queue_dev, wq_creation);
48+
49+
dev->cn_queue = create_singlethread_workqueue(dev->name);
50+
/* If we fail, we will use keventd for all following connector jobs */
51+
WARN_ON(!dev->cn_queue);
52+
}
53+
54+
/*
55+
* Queue a data sent to a callback.
56+
* If the connector workqueue is already created, we queue the job on it.
57+
* Otherwise, we queue the job to kevent and queue the connector workqueue
58+
* creation too.
59+
*/
60+
int queue_cn_work(struct cn_callback_entry *cbq, struct work_struct *work)
61+
{
62+
struct cn_queue_dev *pdev = cbq->pdev;
63+
64+
if (likely(pdev->cn_queue))
65+
return queue_work(pdev->cn_queue, work);
66+
67+
/* Don't create the connector workqueue twice */
68+
if (atomic_inc_return(&pdev->wq_requested) == 1)
69+
schedule_work(&pdev->wq_creation);
70+
else
71+
atomic_dec(&pdev->wq_requested);
72+
73+
return schedule_work(work);
74+
}
75+
3476
void cn_queue_wrapper(struct work_struct *work)
3577
{
3678
struct cn_callback_entry *cbq =
@@ -58,14 +100,17 @@ static struct cn_callback_entry *cn_queue_alloc_callback_entry(char *name, struc
58100
snprintf(cbq->id.name, sizeof(cbq->id.name), "%s", name);
59101
memcpy(&cbq->id.id, id, sizeof(struct cb_id));
60102
cbq->data.callback = callback;
61-
103+
62104
INIT_WORK(&cbq->work, &cn_queue_wrapper);
63105
return cbq;
64106
}
65107

66108
static void cn_queue_free_callback(struct cn_callback_entry *cbq)
67109
{
68-
flush_workqueue(cbq->pdev->cn_queue);
110+
/* The first jobs have been sent to kevent, flush them too */
111+
flush_scheduled_work();
112+
if (cbq->pdev->cn_queue)
113+
flush_workqueue(cbq->pdev->cn_queue);
69114

70115
kfree(cbq);
71116
}
@@ -143,24 +188,37 @@ struct cn_queue_dev *cn_queue_alloc_dev(char *name, struct sock *nls)
143188
atomic_set(&dev->refcnt, 0);
144189
INIT_LIST_HEAD(&dev->queue_list);
145190
spin_lock_init(&dev->queue_lock);
191+
init_waitqueue_head(&dev->wq_created);
146192

147193
dev->nls = nls;
148194

149-
dev->cn_queue = create_singlethread_workqueue(dev->name);
150-
if (!dev->cn_queue) {
151-
kfree(dev);
152-
return NULL;
153-
}
195+
INIT_WORK(&dev->wq_creation, cn_queue_create);
154196

155197
return dev;
156198
}
157199

158200
void cn_queue_free_dev(struct cn_queue_dev *dev)
159201
{
160202
struct cn_callback_entry *cbq, *n;
203+
long timeout;
204+
DEFINE_WAIT(wait);
205+
206+
/* Flush the first pending jobs queued on kevent */
207+
flush_scheduled_work();
208+
209+
/* If the connector workqueue creation is still pending, wait for it */
210+
prepare_to_wait(&dev->wq_created, &wait, TASK_UNINTERRUPTIBLE);
211+
if (atomic_read(&dev->wq_requested) && !dev->cn_queue) {
212+
timeout = schedule_timeout(HZ * 2);
213+
if (!timeout && !dev->cn_queue)
214+
WARN_ON(1);
215+
}
216+
finish_wait(&dev->wq_created, &wait);
161217

162-
flush_workqueue(dev->cn_queue);
163-
destroy_workqueue(dev->cn_queue);
218+
if (dev->cn_queue) {
219+
flush_workqueue(dev->cn_queue);
220+
destroy_workqueue(dev->cn_queue);
221+
}
164222

165223
spin_lock_bh(&dev->queue_lock);
166224
list_for_each_entry_safe(cbq, n, &dev->queue_list, callback_entry)

‎drivers/connector/connector.c‎

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
/*
22
* connector.c
3-
*
3+
*
44
* 2004-2005 Copyright (c) Evgeniy Polyakov <johnpol@2ka.mipt.ru>
55
* All rights reserved.
6-
*
6+
*
77
* This program is free software; you can redistribute it and/or modify
88
* it under the terms of the GNU General Public License as published by
99
* the Free Software Foundation; either version 2 of the License, or
@@ -145,14 +145,13 @@ static int cn_call_callback(struct cn_msg *msg, void (*destruct_data)(void *), v
145145
__cbq->data.ddata = data;
146146
__cbq->data.destruct_data = destruct_data;
147147

148-
if (queue_work(dev->cbdev->cn_queue,
149-
&__cbq->work))
148+
if (queue_cn_work(__cbq, &__cbq->work))
150149
err = 0;
151150
else
152151
err = -EINVAL;
153152
} else {
154153
struct cn_callback_data *d;
155-
154+
156155
err = -ENOMEM;
157156
__new_cbq = kzalloc(sizeof(struct cn_callback_entry), GFP_ATOMIC);
158157
if (__new_cbq) {
@@ -163,10 +162,12 @@ static int cn_call_callback(struct cn_msg *msg, void (*destruct_data)(void *), v
163162
d->destruct_data = destruct_data;
164163
d->free = __new_cbq;
165164

165+
__new_cbq->pdev = __cbq->pdev;
166+
166167
INIT_WORK(&__new_cbq->work,
167168
&cn_queue_wrapper);
168169

169-
if (queue_work(dev->cbdev->cn_queue,
170+
if (queue_cn_work(__new_cbq,
170171
&__new_cbq->work))
171172
err = 0;
172173
else {
@@ -237,15 +238,15 @@ static void cn_notify(struct cb_id *id, u32 notify_event)
237238

238239
req = (struct cn_notify_req *)ctl->data;
239240
for (i = 0; i < ctl->idx_notify_num; ++i, ++req) {
240-
if (id->idx >= req->first &&
241+
if (id->idx >= req->first &&
241242
id->idx < req->first + req->range) {
242243
idx_found = 1;
243244
break;
244245
}
245246
}
246247

247248
for (i = 0; i < ctl->val_notify_num; ++i, ++req) {
248-
if (id->val >= req->first &&
249+
if (id->val >= req->first &&
249250
id->val < req->first + req->range) {
250251
val_found = 1;
251252
break;
@@ -459,7 +460,7 @@ static int __devinit cn_init(void)
459460
netlink_kernel_release(dev->nls);
460461
return -EINVAL;
461462
}
462-
463+
463464
cn_already_initialized = 1;
464465

465466
err = cn_add_callback(&dev->id, "connector", &cn_callback);

‎include/linux/connector.h‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,12 @@ struct cn_queue_dev {
109109
unsigned char name[CN_CBQ_NAMELEN];
110110

111111
struct workqueue_struct *cn_queue;
112+
/* Sent to kevent to create cn_queue only when needed */
113+
struct work_struct wq_creation;
114+
/* Tell if the wq_creation job is pending/completed */
115+
atomic_t wq_requested;
116+
/* Wait for cn_queue to be created */
117+
wait_queue_head_t wq_created;
112118

113119
struct list_head queue_list;
114120
spinlock_t queue_lock;
@@ -164,6 +170,8 @@ int cn_netlink_send(struct cn_msg *, u32, gfp_t);
164170
int cn_queue_add_callback(struct cn_queue_dev *dev, char *name, struct cb_id *id, void (*callback)(void *));
165171
void cn_queue_del_callback(struct cn_queue_dev *dev, struct cb_id *id);
166172

173+
int queue_cn_work(struct cn_callback_entry *cbq, struct work_struct *work);
174+
167175
struct cn_queue_dev *cn_queue_alloc_dev(char *name, struct sock *);
168176
void cn_queue_free_dev(struct cn_queue_dev *dev);
169177

0 commit comments

Comments
 (0)