cn_queue.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * cn_queue.c
  4. *
  5. * 2004+ Copyright (c) Evgeniy Polyakov <zbr@ioremap.net>
  6. * All rights reserved.
  7. */
  8. #include <linux/kernel.h>
  9. #include <linux/module.h>
  10. #include <linux/list.h>
  11. #include <linux/workqueue.h>
  12. #include <linux/spinlock.h>
  13. #include <linux/slab.h>
  14. #include <linux/skbuff.h>
  15. #include <linux/suspend.h>
  16. #include <linux/connector.h>
  17. #include <linux/delay.h>
  18. static struct cn_callback_entry *
  19. cn_queue_alloc_callback_entry(struct cn_queue_dev *dev, const char *name,
  20. struct cb_id *id,
  21. void (*callback)(struct cn_msg *,
  22. struct netlink_skb_parms *))
  23. {
  24. struct cn_callback_entry *cbq;
  25. cbq = kzalloc(sizeof(*cbq), GFP_KERNEL);
  26. if (!cbq) {
  27. pr_err("Failed to create new callback queue.\n");
  28. return NULL;
  29. }
  30. refcount_set(&cbq->refcnt, 1);
  31. atomic_inc(&dev->refcnt);
  32. cbq->pdev = dev;
  33. snprintf(cbq->id.name, sizeof(cbq->id.name), "%s", name);
  34. memcpy(&cbq->id.id, id, sizeof(struct cb_id));
  35. cbq->callback = callback;
  36. return cbq;
  37. }
  38. void cn_queue_release_callback(struct cn_callback_entry *cbq)
  39. {
  40. if (!refcount_dec_and_test(&cbq->refcnt))
  41. return;
  42. atomic_dec(&cbq->pdev->refcnt);
  43. kfree(cbq);
  44. }
  45. int cn_cb_equal(struct cb_id *i1, struct cb_id *i2)
  46. {
  47. return ((i1->idx == i2->idx) && (i1->val == i2->val));
  48. }
  49. int cn_queue_add_callback(struct cn_queue_dev *dev, const char *name,
  50. struct cb_id *id,
  51. void (*callback)(struct cn_msg *,
  52. struct netlink_skb_parms *))
  53. {
  54. struct cn_callback_entry *cbq, *__cbq;
  55. int found = 0;
  56. cbq = cn_queue_alloc_callback_entry(dev, name, id, callback);
  57. if (!cbq)
  58. return -ENOMEM;
  59. spin_lock_bh(&dev->queue_lock);
  60. list_for_each_entry(__cbq, &dev->queue_list, callback_entry) {
  61. if (cn_cb_equal(&__cbq->id.id, id)) {
  62. found = 1;
  63. break;
  64. }
  65. }
  66. if (!found)
  67. list_add_tail(&cbq->callback_entry, &dev->queue_list);
  68. spin_unlock_bh(&dev->queue_lock);
  69. if (found) {
  70. cn_queue_release_callback(cbq);
  71. return -EINVAL;
  72. }
  73. cbq->seq = 0;
  74. cbq->group = cbq->id.id.idx;
  75. return 0;
  76. }
  77. void cn_queue_del_callback(struct cn_queue_dev *dev, struct cb_id *id)
  78. {
  79. struct cn_callback_entry *cbq, *n;
  80. int found = 0;
  81. spin_lock_bh(&dev->queue_lock);
  82. list_for_each_entry_safe(cbq, n, &dev->queue_list, callback_entry) {
  83. if (cn_cb_equal(&cbq->id.id, id)) {
  84. list_del(&cbq->callback_entry);
  85. found = 1;
  86. break;
  87. }
  88. }
  89. spin_unlock_bh(&dev->queue_lock);
  90. if (found)
  91. cn_queue_release_callback(cbq);
  92. }
  93. struct cn_queue_dev *cn_queue_alloc_dev(const char *name, struct sock *nls)
  94. {
  95. struct cn_queue_dev *dev;
  96. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  97. if (!dev)
  98. return NULL;
  99. snprintf(dev->name, sizeof(dev->name), "%s", name);
  100. atomic_set(&dev->refcnt, 0);
  101. INIT_LIST_HEAD(&dev->queue_list);
  102. spin_lock_init(&dev->queue_lock);
  103. dev->nls = nls;
  104. return dev;
  105. }
  106. void cn_queue_free_dev(struct cn_queue_dev *dev)
  107. {
  108. struct cn_callback_entry *cbq, *n;
  109. spin_lock_bh(&dev->queue_lock);
  110. list_for_each_entry_safe(cbq, n, &dev->queue_list, callback_entry)
  111. list_del(&cbq->callback_entry);
  112. spin_unlock_bh(&dev->queue_lock);
  113. while (atomic_read(&dev->refcnt)) {
  114. pr_info("Waiting for %s to become free: refcnt=%d.\n",
  115. dev->name, atomic_read(&dev->refcnt));
  116. msleep(1000);
  117. }
  118. kfree(dev);
  119. dev = NULL;
  120. }