diff options
| -rw-r--r-- | drivers/connector/cn_queue.c | 80 | ||||
| -rw-r--r-- | drivers/connector/connector.c | 19 | ||||
| -rw-r--r-- | include/linux/connector.h | 8 |
3 files changed, 87 insertions, 20 deletions
diff --git a/drivers/connector/cn_queue.c b/drivers/connector/cn_queue.c index b6fe7e7a2c2f..c769ef269fb5 100644 --- a/drivers/connector/cn_queue.c +++ b/drivers/connector/cn_queue.c | |||
| @@ -1,9 +1,9 @@ | |||
| 1 | /* | 1 | /* |
| 2 | * cn_queue.c | 2 | * cn_queue.c |
| 3 | * | 3 | * |
| 4 | * 2004-2005 Copyright (c) Evgeniy Polyakov <johnpol@2ka.mipt.ru> | 4 | * 2004-2005 Copyright (c) Evgeniy Polyakov <johnpol@2ka.mipt.ru> |
| 5 | * All rights reserved. | 5 | * All rights reserved. |
| 6 | * | 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify | 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by | 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 2 of the License, or | 9 | * the Free Software Foundation; either version 2 of the License, or |
| @@ -31,6 +31,48 @@ | |||
| 31 | #include <linux/connector.h> | 31 | #include <linux/connector.h> |
| 32 | #include <linux/delay.h> | 32 | #include <linux/delay.h> |
| 33 | 33 | ||
| 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 | |||
| 34 | void cn_queue_wrapper(struct work_struct *work) | 76 | void cn_queue_wrapper(struct work_struct *work) |
| 35 | { | 77 | { |
| 36 | struct cn_callback_entry *cbq = | 78 | struct cn_callback_entry *cbq = |
| @@ -58,14 +100,17 @@ static struct cn_callback_entry *cn_queue_alloc_callback_entry(char *name, struc | |||
| 58 | snprintf(cbq->id.name, sizeof(cbq->id.name), "%s", name); | 100 | snprintf(cbq->id.name, sizeof(cbq->id.name), "%s", name); |
| 59 | memcpy(&cbq->id.id, id, sizeof(struct cb_id)); | 101 | memcpy(&cbq->id.id, id, sizeof(struct cb_id)); |
| 60 | cbq->data.callback = callback; | 102 | cbq->data.callback = callback; |
| 61 | 103 | ||
| 62 | INIT_WORK(&cbq->work, &cn_queue_wrapper); | 104 | INIT_WORK(&cbq->work, &cn_queue_wrapper); |
| 63 | return cbq; | 105 | return cbq; |
| 64 | } | 106 | } |
| 65 | 107 | ||
| 66 | static void cn_queue_free_callback(struct cn_callback_entry *cbq) | 108 | static void cn_queue_free_callback(struct cn_callback_entry *cbq) |
| 67 | { | 109 | { |
| 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); | ||
| 69 | 114 | ||
| 70 | kfree(cbq); | 115 | kfree(cbq); |
| 71 | } | 116 | } |
| @@ -143,14 +188,11 @@ struct cn_queue_dev *cn_queue_alloc_dev(char *name, struct sock *nls) | |||
| 143 | atomic_set(&dev->refcnt, 0); | 188 | atomic_set(&dev->refcnt, 0); |
| 144 | INIT_LIST_HEAD(&dev->queue_list); | 189 | INIT_LIST_HEAD(&dev->queue_list); |
| 145 | spin_lock_init(&dev->queue_lock); | 190 | spin_lock_init(&dev->queue_lock); |
| 191 | init_waitqueue_head(&dev->wq_created); | ||
| 146 | 192 | ||
| 147 | dev->nls = nls; | 193 | dev->nls = nls; |
| 148 | 194 | ||
| 149 | dev->cn_queue = create_singlethread_workqueue(dev->name); | 195 | INIT_WORK(&dev->wq_creation, cn_queue_create); |
| 150 | if (!dev->cn_queue) { | ||
| 151 | kfree(dev); | ||
| 152 | return NULL; | ||
| 153 | } | ||
| 154 | 196 | ||
| 155 | return dev; | 197 | return dev; |
| 156 | } | 198 | } |
| @@ -158,9 +200,25 @@ struct cn_queue_dev *cn_queue_alloc_dev(char *name, struct sock *nls) | |||
| 158 | void cn_queue_free_dev(struct cn_queue_dev *dev) | 200 | void cn_queue_free_dev(struct cn_queue_dev *dev) |
| 159 | { | 201 | { |
| 160 | struct cn_callback_entry *cbq, *n; | 202 | 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); | ||
| 161 | 217 | ||
| 162 | flush_workqueue(dev->cn_queue); | 218 | if (dev->cn_queue) { |
| 163 | destroy_workqueue(dev->cn_queue); | 219 | flush_workqueue(dev->cn_queue); |
| 220 | destroy_workqueue(dev->cn_queue); | ||
| 221 | } | ||
| 164 | 222 | ||
| 165 | spin_lock_bh(&dev->queue_lock); | 223 | spin_lock_bh(&dev->queue_lock); |
| 166 | list_for_each_entry_safe(cbq, n, &dev->queue_list, callback_entry) | 224 | list_for_each_entry_safe(cbq, n, &dev->queue_list, callback_entry) |
diff --git a/drivers/connector/connector.c b/drivers/connector/connector.c index bf4830082a13..fd336c5a9057 100644 --- a/drivers/connector/connector.c +++ b/drivers/connector/connector.c | |||
| @@ -1,9 +1,9 @@ | |||
| 1 | /* | 1 | /* |
| 2 | * connector.c | 2 | * connector.c |
| 3 | * | 3 | * |
| 4 | * 2004-2005 Copyright (c) Evgeniy Polyakov <johnpol@2ka.mipt.ru> | 4 | * 2004-2005 Copyright (c) Evgeniy Polyakov <johnpol@2ka.mipt.ru> |
| 5 | * All rights reserved. | 5 | * All rights reserved. |
| 6 | * | 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify | 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by | 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 2 of the License, or | 9 | * 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 | |||
| 145 | __cbq->data.ddata = data; | 145 | __cbq->data.ddata = data; |
| 146 | __cbq->data.destruct_data = destruct_data; | 146 | __cbq->data.destruct_data = destruct_data; |
| 147 | 147 | ||
| 148 | if (queue_work(dev->cbdev->cn_queue, | 148 | if (queue_cn_work(__cbq, &__cbq->work)) |
| 149 | &__cbq->work)) | ||
| 150 | err = 0; | 149 | err = 0; |
| 151 | else | 150 | else |
| 152 | err = -EINVAL; | 151 | err = -EINVAL; |
| 153 | } else { | 152 | } else { |
| 154 | struct cn_callback_data *d; | 153 | struct cn_callback_data *d; |
| 155 | 154 | ||
| 156 | err = -ENOMEM; | 155 | err = -ENOMEM; |
| 157 | __new_cbq = kzalloc(sizeof(struct cn_callback_entry), GFP_ATOMIC); | 156 | __new_cbq = kzalloc(sizeof(struct cn_callback_entry), GFP_ATOMIC); |
| 158 | if (__new_cbq) { | 157 | if (__new_cbq) { |
| @@ -163,10 +162,12 @@ static int cn_call_callback(struct cn_msg *msg, void (*destruct_data)(void *), v | |||
| 163 | d->destruct_data = destruct_data; | 162 | d->destruct_data = destruct_data; |
| 164 | d->free = __new_cbq; | 163 | d->free = __new_cbq; |
| 165 | 164 | ||
| 165 | __new_cbq->pdev = __cbq->pdev; | ||
| 166 | |||
| 166 | INIT_WORK(&__new_cbq->work, | 167 | INIT_WORK(&__new_cbq->work, |
| 167 | &cn_queue_wrapper); | 168 | &cn_queue_wrapper); |
| 168 | 169 | ||
| 169 | if (queue_work(dev->cbdev->cn_queue, | 170 | if (queue_cn_work(__new_cbq, |
| 170 | &__new_cbq->work)) | 171 | &__new_cbq->work)) |
| 171 | err = 0; | 172 | err = 0; |
| 172 | else { | 173 | else { |
| @@ -237,7 +238,7 @@ static void cn_notify(struct cb_id *id, u32 notify_event) | |||
| 237 | 238 | ||
| 238 | req = (struct cn_notify_req *)ctl->data; | 239 | req = (struct cn_notify_req *)ctl->data; |
| 239 | for (i = 0; i < ctl->idx_notify_num; ++i, ++req) { | 240 | for (i = 0; i < ctl->idx_notify_num; ++i, ++req) { |
| 240 | if (id->idx >= req->first && | 241 | if (id->idx >= req->first && |
| 241 | id->idx < req->first + req->range) { | 242 | id->idx < req->first + req->range) { |
| 242 | idx_found = 1; | 243 | idx_found = 1; |
| 243 | break; | 244 | break; |
| @@ -245,7 +246,7 @@ static void cn_notify(struct cb_id *id, u32 notify_event) | |||
| 245 | } | 246 | } |
| 246 | 247 | ||
| 247 | for (i = 0; i < ctl->val_notify_num; ++i, ++req) { | 248 | for (i = 0; i < ctl->val_notify_num; ++i, ++req) { |
| 248 | if (id->val >= req->first && | 249 | if (id->val >= req->first && |
| 249 | id->val < req->first + req->range) { | 250 | id->val < req->first + req->range) { |
| 250 | val_found = 1; | 251 | val_found = 1; |
| 251 | break; | 252 | break; |
| @@ -459,7 +460,7 @@ static int __devinit cn_init(void) | |||
| 459 | netlink_kernel_release(dev->nls); | 460 | netlink_kernel_release(dev->nls); |
| 460 | return -EINVAL; | 461 | return -EINVAL; |
| 461 | } | 462 | } |
| 462 | 463 | ||
| 463 | cn_already_initialized = 1; | 464 | cn_already_initialized = 1; |
| 464 | 465 | ||
| 465 | err = cn_add_callback(&dev->id, "connector", &cn_callback); | 466 | err = cn_add_callback(&dev->id, "connector", &cn_callback); |
diff --git a/include/linux/connector.h b/include/linux/connector.h index 34f2789d9b9b..fc65d219d88c 100644 --- a/include/linux/connector.h +++ b/include/linux/connector.h | |||
| @@ -109,6 +109,12 @@ struct cn_queue_dev { | |||
| 109 | unsigned char name[CN_CBQ_NAMELEN]; | 109 | unsigned char name[CN_CBQ_NAMELEN]; |
| 110 | 110 | ||
| 111 | struct workqueue_struct *cn_queue; | 111 | 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; | ||
| 112 | 118 | ||
| 113 | struct list_head queue_list; | 119 | struct list_head queue_list; |
| 114 | spinlock_t queue_lock; | 120 | spinlock_t queue_lock; |
| @@ -164,6 +170,8 @@ int cn_netlink_send(struct cn_msg *, u32, gfp_t); | |||
| 164 | int cn_queue_add_callback(struct cn_queue_dev *dev, char *name, struct cb_id *id, void (*callback)(void *)); | 170 | int cn_queue_add_callback(struct cn_queue_dev *dev, char *name, struct cb_id *id, void (*callback)(void *)); |
| 165 | void cn_queue_del_callback(struct cn_queue_dev *dev, struct cb_id *id); | 171 | void cn_queue_del_callback(struct cn_queue_dev *dev, struct cb_id *id); |
| 166 | 172 | ||
| 173 | int queue_cn_work(struct cn_callback_entry *cbq, struct work_struct *work); | ||
| 174 | |||
| 167 | struct cn_queue_dev *cn_queue_alloc_dev(char *name, struct sock *); | 175 | struct cn_queue_dev *cn_queue_alloc_dev(char *name, struct sock *); |
| 168 | void cn_queue_free_dev(struct cn_queue_dev *dev); | 176 | void cn_queue_free_dev(struct cn_queue_dev *dev); |
| 169 | 177 | ||
