cn_queue.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /*
  2. * cn_queue.c
  3. *
  4. * 2004+ Copyright (c) Evgeniy Polyakov <zbr@ioremap.net>
  5. * All rights reserved.
  6. *
  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
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. *
  21. */
  22. #include <linux/kernel.h>
  23. #include <linux/module.h>
  24. #include <linux/list.h>
  25. #include <linux/workqueue.h>
  26. #include <linux/spinlock.h>
  27. #include <linux/slab.h>
  28. #include <linux/skbuff.h>
  29. #include <linux/suspend.h>
  30. #include <linux/connector.h>
  31. #include <linux/delay.h>
  32. /*
  33. * This job is sent to the kevent workqueue.
  34. * While no event is once sent to any callback, the connector workqueue
  35. * is not created to avoid a useless waiting kernel task.
  36. * Once the first event is received, we create this dedicated workqueue which
  37. * is necessary because the flow of data can be high and we don't want
  38. * to encumber keventd with that.
  39. */
  40. static void cn_queue_create(struct work_struct *work)
  41. {
  42. struct cn_queue_dev *dev;
  43. dev = container_of(work, struct cn_queue_dev, wq_creation);
  44. dev->cn_queue = create_singlethread_workqueue(dev->name);
  45. /* If we fail, we will use keventd for all following connector jobs */
  46. WARN_ON(!dev->cn_queue);
  47. }
  48. /*
  49. * Queue a data sent to a callback.
  50. * If the connector workqueue is already created, we queue the job on it.
  51. * Otherwise, we queue the job to kevent and queue the connector workqueue
  52. * creation too.
  53. */
  54. int queue_cn_work(struct cn_callback_entry *cbq, struct work_struct *work)
  55. {
  56. struct cn_queue_dev *pdev = cbq->pdev;
  57. if (likely(pdev->cn_queue))
  58. return queue_work(pdev->cn_queue, work);
  59. /* Don't create the connector workqueue twice */
  60. if (atomic_inc_return(&pdev->wq_requested) == 1)
  61. schedule_work(&pdev->wq_creation);
  62. else
  63. atomic_dec(&pdev->wq_requested);
  64. return schedule_work(work);
  65. }
  66. void cn_queue_wrapper(struct work_struct *work)
  67. {
  68. struct cn_callback_entry *cbq =
  69. container_of(work, struct cn_callback_entry, work);
  70. struct cn_callback_data *d = &cbq->data;
  71. struct cn_msg *msg = NLMSG_DATA(nlmsg_hdr(d->skb));
  72. struct netlink_skb_parms *nsp = &NETLINK_CB(d->skb);
  73. d->callback(msg, nsp);
  74. kfree_skb(d->skb);
  75. d->skb = NULL;
  76. kfree(d->free);
  77. }
  78. static struct cn_callback_entry *
  79. cn_queue_alloc_callback_entry(char *name, struct cb_id *id,
  80. void (*callback)(struct cn_msg *, struct netlink_skb_parms *))
  81. {
  82. struct cn_callback_entry *cbq;
  83. cbq = kzalloc(sizeof(*cbq), GFP_KERNEL);
  84. if (!cbq) {
  85. printk(KERN_ERR "Failed to create new callback queue.\n");
  86. return NULL;
  87. }
  88. snprintf(cbq->id.name, sizeof(cbq->id.name), "%s", name);
  89. memcpy(&cbq->id.id, id, sizeof(struct cb_id));
  90. cbq->data.callback = callback;
  91. INIT_WORK(&cbq->work, &cn_queue_wrapper);
  92. return cbq;
  93. }
  94. static void cn_queue_free_callback(struct cn_callback_entry *cbq)
  95. {
  96. /* The first jobs have been sent to kevent, flush them too */
  97. flush_scheduled_work();
  98. if (cbq->pdev->cn_queue)
  99. flush_workqueue(cbq->pdev->cn_queue);
  100. kfree(cbq);
  101. }
  102. int cn_cb_equal(struct cb_id *i1, struct cb_id *i2)
  103. {
  104. return ((i1->idx == i2->idx) && (i1->val == i2->val));
  105. }
  106. int cn_queue_add_callback(struct cn_queue_dev *dev, char *name, struct cb_id *id,
  107. void (*callback)(struct cn_msg *, struct netlink_skb_parms *))
  108. {
  109. struct cn_callback_entry *cbq, *__cbq;
  110. int found = 0;
  111. cbq = cn_queue_alloc_callback_entry(name, id, callback);
  112. if (!cbq)
  113. return -ENOMEM;
  114. atomic_inc(&dev->refcnt);
  115. cbq->pdev = dev;
  116. spin_lock_bh(&dev->queue_lock);
  117. list_for_each_entry(__cbq, &dev->queue_list, callback_entry) {
  118. if (cn_cb_equal(&__cbq->id.id, id)) {
  119. found = 1;
  120. break;
  121. }
  122. }
  123. if (!found)
  124. list_add_tail(&cbq->callback_entry, &dev->queue_list);
  125. spin_unlock_bh(&dev->queue_lock);
  126. if (found) {
  127. cn_queue_free_callback(cbq);
  128. atomic_dec(&dev->refcnt);
  129. return -EINVAL;
  130. }
  131. cbq->seq = 0;
  132. cbq->group = cbq->id.id.idx;
  133. return 0;
  134. }
  135. void cn_queue_del_callback(struct cn_queue_dev *dev, struct cb_id *id)
  136. {
  137. struct cn_callback_entry *cbq, *n;
  138. int found = 0;
  139. spin_lock_bh(&dev->queue_lock);
  140. list_for_each_entry_safe(cbq, n, &dev->queue_list, callback_entry) {
  141. if (cn_cb_equal(&cbq->id.id, id)) {
  142. list_del(&cbq->callback_entry);
  143. found = 1;
  144. break;
  145. }
  146. }
  147. spin_unlock_bh(&dev->queue_lock);
  148. if (found) {
  149. cn_queue_free_callback(cbq);
  150. atomic_dec(&dev->refcnt);
  151. }
  152. }
  153. struct cn_queue_dev *cn_queue_alloc_dev(char *name, struct sock *nls)
  154. {
  155. struct cn_queue_dev *dev;
  156. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  157. if (!dev)
  158. return NULL;
  159. snprintf(dev->name, sizeof(dev->name), "%s", name);
  160. atomic_set(&dev->refcnt, 0);
  161. INIT_LIST_HEAD(&dev->queue_list);
  162. spin_lock_init(&dev->queue_lock);
  163. init_waitqueue_head(&dev->wq_created);
  164. dev->nls = nls;
  165. INIT_WORK(&dev->wq_creation, cn_queue_create);
  166. return dev;
  167. }
  168. void cn_queue_free_dev(struct cn_queue_dev *dev)
  169. {
  170. struct cn_callback_entry *cbq, *n;
  171. long timeout;
  172. DEFINE_WAIT(wait);
  173. /* Flush the first pending jobs queued on kevent */
  174. flush_scheduled_work();
  175. /* If the connector workqueue creation is still pending, wait for it */
  176. prepare_to_wait(&dev->wq_created, &wait, TASK_UNINTERRUPTIBLE);
  177. if (atomic_read(&dev->wq_requested) && !dev->cn_queue) {
  178. timeout = schedule_timeout(HZ * 2);
  179. if (!timeout && !dev->cn_queue)
  180. WARN_ON(1);
  181. }
  182. finish_wait(&dev->wq_created, &wait);
  183. if (dev->cn_queue) {
  184. flush_workqueue(dev->cn_queue);
  185. destroy_workqueue(dev->cn_queue);
  186. }
  187. spin_lock_bh(&dev->queue_lock);
  188. list_for_each_entry_safe(cbq, n, &dev->queue_list, callback_entry)
  189. list_del(&cbq->callback_entry);
  190. spin_unlock_bh(&dev->queue_lock);
  191. while (atomic_read(&dev->refcnt)) {
  192. printk(KERN_INFO "Waiting for %s to become free: refcnt=%d.\n",
  193. dev->name, atomic_read(&dev->refcnt));
  194. msleep(1000);
  195. }
  196. kfree(dev);
  197. dev = NULL;
  198. }