kuid.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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/mutex.h>
  22. #include <kern/panic.h>
  23. #include <kern/rcu.h>
  24. #include <kern/rdxtree.h>
  25. struct kuid_map
  26. {
  27. struct mutex 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, struct kuid_head *head,
  43. rdxtree_key_t *keyp)
  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. typedef int (*kuid_key_fn_t) (struct kuid_map *, struct kuid_head *,
  69. rdxtree_key_t *);
  70. static const kuid_key_fn_t KUID_METHODS[] =
  71. { kuid_map_alloc_key, kuid_map_pop_key };
  72. static int
  73. kuid_map_alloc_radix (struct kuid_map *map, struct kuid_head *head,
  74. rdxtree_key_t *keyp)
  75. {
  76. int ix = map->stamp >= KUID_MAX_STAMP;
  77. return (KUID_METHODS[ix] (map, head, keyp) != 0 ?
  78. KUID_METHODS[ix ^ 1] (map, head, keyp) : 0);
  79. }
  80. int
  81. kuid_alloc (struct kuid_head *head, int cls)
  82. {
  83. rdxtree_key_t key;
  84. assert ((size_t)cls < ARRAY_SIZE (kuid_maps));
  85. {
  86. _Auto map = &kuid_maps[cls];
  87. MUTEX_GUARD (&map->lock);
  88. if (kuid_map_alloc_radix (map, head, &key) != 0)
  89. return (EAGAIN);
  90. else if (key > map->max_id)
  91. {
  92. rdxtree_remove (&map->tree, key);
  93. return (EAGAIN);
  94. }
  95. }
  96. assert (key != 0);
  97. head->id = (uint32_t)key;
  98. return (0);
  99. }
  100. struct kuid_head*
  101. kuid_find (uint32_t kuid, int cls)
  102. {
  103. assert ((size_t)cls < ARRAY_SIZE (kuid_maps));
  104. _Auto map = &kuid_maps[cls];
  105. RCU_GUARD ();
  106. struct kuid_head *head = rdxtree_lookup (&map->tree, kuid);
  107. if (! head)
  108. return (head);
  109. return (atomic_try_inc (&head->nr_refs, ATOMIC_ACQUIRE) ? head : NULL);
  110. }
  111. int
  112. kuid_remove (struct kuid_head *head, int cls)
  113. {
  114. assert ((size_t)cls < ARRAY_SIZE (kuid_maps));
  115. if (!head->id)
  116. return (EINVAL);
  117. _Auto map = &kuid_maps[cls];
  118. mutex_lock (&map->lock);
  119. struct kuid_head *prev = rdxtree_remove (&map->tree, head->id);
  120. if (prev)
  121. cbuf_push (&map->cbuf, &head->id, sizeof (head->id), false);
  122. mutex_unlock (&map->lock);
  123. if (! prev)
  124. return (ESRCH);
  125. assert (prev == head);
  126. return (0);
  127. }
  128. static void
  129. kuid_map_init (struct kuid_map *map, uint32_t max_id)
  130. {
  131. mutex_init (&map->lock);
  132. rdxtree_init (&map->tree, RDXTREE_KEY_ALLOC);
  133. cbuf_init (&map->cbuf, map->free_kuids, sizeof (map->free_kuids));
  134. map->max_id = max_id;
  135. // KUID 0 is reserved for kernel specific entities.
  136. if (rdxtree_insert (&map->tree, 0, &map->lock))
  137. panic ("could not reserve KUID 0");
  138. }
  139. static int __init
  140. kuid_setup (void)
  141. {
  142. kuid_map_init (&kuid_maps[KUID_TASK], (1u << 31) - 1);
  143. kuid_map_init (&kuid_maps[KUID_THREAD], (1u << 30) - 1);
  144. return (0);
  145. }
  146. INIT_OP_DEFINE (kuid_setup,
  147. INIT_OP_DEP (mutex_setup, true),
  148. INIT_OP_DEP (rdxtree_setup, true));