kuid.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /*
  2. * Copyright (c) 2022 Agustina Arzille.
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include <machine/cpu.h>
  18. #include <kern/atomic.h>
  19. #include <kern/cbuf.h>
  20. #include <kern/kuid.h>
  21. #include <kern/panic.h>
  22. #include <kern/rcu.h>
  23. #include <kern/rdxtree.h>
  24. #include <kern/spinlock.h>
  25. struct kuid_map
  26. {
  27. struct spinlock lock;
  28. struct rdxtree tree;
  29. uint32_t free_kuids[64];
  30. struct cbuf cbuf;
  31. int stamp;
  32. uint32_t max_id;
  33. };
  34. static struct kuid_map kuid_maps[KUID_MAX_CLS];
  35. /*
  36. * In order to prevent common bugs where the last used KUID
  37. * is recycled immediately, use at least this many newly created
  38. * KUID's before recycling.
  39. */
  40. #define KUID_MAX_STAMP 16
  41. static int
  42. kuid_map_pop_key (struct kuid_map *map, rdxtree_key_t *keyp,
  43. struct kuid_head *head)
  44. {
  45. uint32_t id;
  46. size_t size = sizeof (id);
  47. if (cbuf_pop (&map->cbuf, &id, &size) != 0)
  48. return (-1);
  49. else if (rdxtree_insert (&map->tree, id, head) != 0)
  50. {
  51. cbuf_push (&map->cbuf, &id, size, false);
  52. return (-1);
  53. }
  54. map->stamp = 0;
  55. assert (size == sizeof (id));
  56. *keyp = id;
  57. return (0);
  58. }
  59. static int
  60. kuid_map_alloc_key (struct kuid_map *map, struct kuid_head *head,
  61. rdxtree_key_t *keyp)
  62. {
  63. int error = rdxtree_insert_alloc (&map->tree, head, keyp);
  64. if (! error)
  65. ++map->stamp;
  66. return (error);
  67. }
  68. static int
  69. kuid_map_alloc_radix (struct kuid_map *map, struct kuid_head *head,
  70. rdxtree_key_t *keyp)
  71. {
  72. if (map->stamp >= KUID_MAX_STAMP)
  73. return (kuid_map_pop_key (map, keyp, head) ?
  74. kuid_map_alloc_key (map, head, keyp) : 0);
  75. return (kuid_map_alloc_key (map, head, keyp) ?
  76. kuid_map_pop_key (map, keyp, head) : 0);
  77. }
  78. int
  79. kuid_alloc (struct kuid_head *head, int cls)
  80. {
  81. rdxtree_key_t key;
  82. assert ((size_t)cls < ARRAY_SIZE (kuid_maps));
  83. {
  84. _Auto map = &kuid_maps[cls];
  85. SPINLOCK_GUARD (&map->lock);
  86. if (kuid_map_alloc_radix (map, head, &key) != 0)
  87. return (EAGAIN);
  88. else if (key > map->max_id)
  89. {
  90. rdxtree_remove (&map->tree, key);
  91. return (EAGAIN);
  92. }
  93. }
  94. assert (key != 0);
  95. head->id = (uint32_t)key;
  96. return (0);
  97. }
  98. struct kuid_head*
  99. kuid_find (uint32_t kuid, int cls)
  100. {
  101. assert ((size_t)cls < ARRAY_SIZE (kuid_maps));
  102. _Auto map = &kuid_maps[cls];
  103. RCU_GUARD ();
  104. struct kuid_head *head = rdxtree_lookup (&map->tree, kuid);
  105. if (! head)
  106. return (head);
  107. while (1)
  108. {
  109. size_t nr_refs = atomic_load_rlx (&head->nr_refs);
  110. if (! nr_refs)
  111. return (NULL);
  112. else if (atomic_cas_bool_acq (&head->nr_refs, nr_refs, nr_refs + 1))
  113. return (head);
  114. cpu_pause ();
  115. }
  116. }
  117. int
  118. kuid_remove (struct kuid_head *head, int cls)
  119. {
  120. assert ((size_t)cls < ARRAY_SIZE (kuid_maps));
  121. if (!head->id)
  122. return (EINVAL);
  123. cpu_flags_t flags;
  124. _Auto map = &kuid_maps[cls];
  125. spinlock_lock_intr_save (&map->lock, &flags);
  126. struct kuid_head *prev = rdxtree_remove (&map->tree, head->id);
  127. if (prev)
  128. cbuf_push (&map->cbuf, &head->id, sizeof (head->id), false);
  129. spinlock_unlock_intr_restore (&map->lock, flags);
  130. if (! prev)
  131. return (ESRCH);
  132. assert (prev == head);
  133. return (0);
  134. }
  135. static void
  136. kuid_map_init (struct kuid_map *map, uint32_t max_id)
  137. {
  138. spinlock_init (&map->lock);
  139. rdxtree_init (&map->tree, RDXTREE_KEY_ALLOC);
  140. cbuf_init (&map->cbuf, map->free_kuids, sizeof (map->free_kuids));
  141. map->max_id = max_id;
  142. // KUID 0 is reserved for kernel specific entities.
  143. if (rdxtree_insert (&map->tree, 0, &map->lock))
  144. panic ("could not reserve KUID 0");
  145. }
  146. static int __init
  147. kuid_setup (void)
  148. {
  149. kuid_map_init (&kuid_maps[KUID_TASK], (1u << 31) - 1);
  150. kuid_map_init (&kuid_maps[KUID_THREAD], (1u << 30) - 1);
  151. return (0);
  152. }
  153. INIT_OP_DEFINE (kuid_setup,
  154. INIT_OP_DEP (spinlock_setup, true),
  155. INIT_OP_DEP (rdxtree_setup, true));