shadow.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * shadow.c - Shadow Variables
  4. *
  5. * Copyright (C) 2014 Josh Poimboeuf <[email protected]>
  6. * Copyright (C) 2014 Seth Jennings <[email protected]>
  7. * Copyright (C) 2017 Joe Lawrence <[email protected]>
  8. */
  9. /**
  10. * DOC: Shadow variable API concurrency notes:
  11. *
  12. * The shadow variable API provides a simple relationship between an
  13. * <obj, id> pair and a pointer value. It is the responsibility of the
  14. * caller to provide any mutual exclusion required of the shadow data.
  15. *
  16. * Once a shadow variable is attached to its parent object via the
  17. * klp_shadow_*alloc() API calls, it is considered live: any subsequent
  18. * call to klp_shadow_get() may then return the shadow variable's data
  19. * pointer. Callers of klp_shadow_*alloc() should prepare shadow data
  20. * accordingly.
  21. *
  22. * The klp_shadow_*alloc() API calls may allocate memory for new shadow
  23. * variable structures. Their implementation does not call kmalloc
  24. * inside any spinlocks, but API callers should pass GFP flags according
  25. * to their specific needs.
  26. *
  27. * The klp_shadow_hash is an RCU-enabled hashtable and is safe against
  28. * concurrent klp_shadow_free() and klp_shadow_get() operations.
  29. */
  30. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  31. #include <linux/hashtable.h>
  32. #include <linux/slab.h>
  33. #include <linux/livepatch.h>
  34. static DEFINE_HASHTABLE(klp_shadow_hash, 12);
  35. /*
  36. * klp_shadow_lock provides exclusive access to the klp_shadow_hash and
  37. * the shadow variables it references.
  38. */
  39. static DEFINE_SPINLOCK(klp_shadow_lock);
  40. /**
  41. * struct klp_shadow - shadow variable structure
  42. * @node: klp_shadow_hash hash table node
  43. * @rcu_head: RCU is used to safely free this structure
  44. * @obj: pointer to parent object
  45. * @id: data identifier
  46. * @data: data area
  47. */
  48. struct klp_shadow {
  49. struct hlist_node node;
  50. struct rcu_head rcu_head;
  51. void *obj;
  52. unsigned long id;
  53. char data[];
  54. };
  55. /**
  56. * klp_shadow_match() - verify a shadow variable matches given <obj, id>
  57. * @shadow: shadow variable to match
  58. * @obj: pointer to parent object
  59. * @id: data identifier
  60. *
  61. * Return: true if the shadow variable matches.
  62. */
  63. static inline bool klp_shadow_match(struct klp_shadow *shadow, void *obj,
  64. unsigned long id)
  65. {
  66. return shadow->obj == obj && shadow->id == id;
  67. }
  68. /**
  69. * klp_shadow_get() - retrieve a shadow variable data pointer
  70. * @obj: pointer to parent object
  71. * @id: data identifier
  72. *
  73. * Return: the shadow variable data element, NULL on failure.
  74. */
  75. void *klp_shadow_get(void *obj, unsigned long id)
  76. {
  77. struct klp_shadow *shadow;
  78. rcu_read_lock();
  79. hash_for_each_possible_rcu(klp_shadow_hash, shadow, node,
  80. (unsigned long)obj) {
  81. if (klp_shadow_match(shadow, obj, id)) {
  82. rcu_read_unlock();
  83. return shadow->data;
  84. }
  85. }
  86. rcu_read_unlock();
  87. return NULL;
  88. }
  89. EXPORT_SYMBOL_GPL(klp_shadow_get);
  90. static void *__klp_shadow_get_or_alloc(void *obj, unsigned long id,
  91. size_t size, gfp_t gfp_flags,
  92. klp_shadow_ctor_t ctor, void *ctor_data,
  93. bool warn_on_exist)
  94. {
  95. struct klp_shadow *new_shadow;
  96. void *shadow_data;
  97. unsigned long flags;
  98. /* Check if the shadow variable already exists */
  99. shadow_data = klp_shadow_get(obj, id);
  100. if (shadow_data)
  101. goto exists;
  102. /*
  103. * Allocate a new shadow variable. Fill it with zeroes by default.
  104. * More complex setting can be done by @ctor function. But it is
  105. * called only when the buffer is really used (under klp_shadow_lock).
  106. */
  107. new_shadow = kzalloc(size + sizeof(*new_shadow), gfp_flags);
  108. if (!new_shadow)
  109. return NULL;
  110. /* Look for <obj, id> again under the lock */
  111. spin_lock_irqsave(&klp_shadow_lock, flags);
  112. shadow_data = klp_shadow_get(obj, id);
  113. if (unlikely(shadow_data)) {
  114. /*
  115. * Shadow variable was found, throw away speculative
  116. * allocation.
  117. */
  118. spin_unlock_irqrestore(&klp_shadow_lock, flags);
  119. kfree(new_shadow);
  120. goto exists;
  121. }
  122. new_shadow->obj = obj;
  123. new_shadow->id = id;
  124. if (ctor) {
  125. int err;
  126. err = ctor(obj, new_shadow->data, ctor_data);
  127. if (err) {
  128. spin_unlock_irqrestore(&klp_shadow_lock, flags);
  129. kfree(new_shadow);
  130. pr_err("Failed to construct shadow variable <%p, %lx> (%d)\n",
  131. obj, id, err);
  132. return NULL;
  133. }
  134. }
  135. /* No <obj, id> found, so attach the newly allocated one */
  136. hash_add_rcu(klp_shadow_hash, &new_shadow->node,
  137. (unsigned long)new_shadow->obj);
  138. spin_unlock_irqrestore(&klp_shadow_lock, flags);
  139. return new_shadow->data;
  140. exists:
  141. if (warn_on_exist) {
  142. WARN(1, "Duplicate shadow variable <%p, %lx>\n", obj, id);
  143. return NULL;
  144. }
  145. return shadow_data;
  146. }
  147. /**
  148. * klp_shadow_alloc() - allocate and add a new shadow variable
  149. * @obj: pointer to parent object
  150. * @id: data identifier
  151. * @size: size of attached data
  152. * @gfp_flags: GFP mask for allocation
  153. * @ctor: custom constructor to initialize the shadow data (optional)
  154. * @ctor_data: pointer to any data needed by @ctor (optional)
  155. *
  156. * Allocates @size bytes for new shadow variable data using @gfp_flags.
  157. * The data are zeroed by default. They are further initialized by @ctor
  158. * function if it is not NULL. The new shadow variable is then added
  159. * to the global hashtable.
  160. *
  161. * If an existing <obj, id> shadow variable can be found, this routine will
  162. * issue a WARN, exit early and return NULL.
  163. *
  164. * This function guarantees that the constructor function is called only when
  165. * the variable did not exist before. The cost is that @ctor is called
  166. * in atomic context under a spin lock.
  167. *
  168. * Return: the shadow variable data element, NULL on duplicate or
  169. * failure.
  170. */
  171. void *klp_shadow_alloc(void *obj, unsigned long id,
  172. size_t size, gfp_t gfp_flags,
  173. klp_shadow_ctor_t ctor, void *ctor_data)
  174. {
  175. return __klp_shadow_get_or_alloc(obj, id, size, gfp_flags,
  176. ctor, ctor_data, true);
  177. }
  178. EXPORT_SYMBOL_GPL(klp_shadow_alloc);
  179. /**
  180. * klp_shadow_get_or_alloc() - get existing or allocate a new shadow variable
  181. * @obj: pointer to parent object
  182. * @id: data identifier
  183. * @size: size of attached data
  184. * @gfp_flags: GFP mask for allocation
  185. * @ctor: custom constructor to initialize the shadow data (optional)
  186. * @ctor_data: pointer to any data needed by @ctor (optional)
  187. *
  188. * Returns a pointer to existing shadow data if an <obj, id> shadow
  189. * variable is already present. Otherwise, it creates a new shadow
  190. * variable like klp_shadow_alloc().
  191. *
  192. * This function guarantees that only one shadow variable exists with the given
  193. * @id for the given @obj. It also guarantees that the constructor function
  194. * will be called only when the variable did not exist before. The cost is
  195. * that @ctor is called in atomic context under a spin lock.
  196. *
  197. * Return: the shadow variable data element, NULL on failure.
  198. */
  199. void *klp_shadow_get_or_alloc(void *obj, unsigned long id,
  200. size_t size, gfp_t gfp_flags,
  201. klp_shadow_ctor_t ctor, void *ctor_data)
  202. {
  203. return __klp_shadow_get_or_alloc(obj, id, size, gfp_flags,
  204. ctor, ctor_data, false);
  205. }
  206. EXPORT_SYMBOL_GPL(klp_shadow_get_or_alloc);
  207. static void klp_shadow_free_struct(struct klp_shadow *shadow,
  208. klp_shadow_dtor_t dtor)
  209. {
  210. hash_del_rcu(&shadow->node);
  211. if (dtor)
  212. dtor(shadow->obj, shadow->data);
  213. kfree_rcu(shadow, rcu_head);
  214. }
  215. /**
  216. * klp_shadow_free() - detach and free a <obj, id> shadow variable
  217. * @obj: pointer to parent object
  218. * @id: data identifier
  219. * @dtor: custom callback that can be used to unregister the variable
  220. * and/or free data that the shadow variable points to (optional)
  221. *
  222. * This function releases the memory for this <obj, id> shadow variable
  223. * instance, callers should stop referencing it accordingly.
  224. */
  225. void klp_shadow_free(void *obj, unsigned long id, klp_shadow_dtor_t dtor)
  226. {
  227. struct klp_shadow *shadow;
  228. unsigned long flags;
  229. spin_lock_irqsave(&klp_shadow_lock, flags);
  230. /* Delete <obj, id> from hash */
  231. hash_for_each_possible(klp_shadow_hash, shadow, node,
  232. (unsigned long)obj) {
  233. if (klp_shadow_match(shadow, obj, id)) {
  234. klp_shadow_free_struct(shadow, dtor);
  235. break;
  236. }
  237. }
  238. spin_unlock_irqrestore(&klp_shadow_lock, flags);
  239. }
  240. EXPORT_SYMBOL_GPL(klp_shadow_free);
  241. /**
  242. * klp_shadow_free_all() - detach and free all <_, id> shadow variables
  243. * @id: data identifier
  244. * @dtor: custom callback that can be used to unregister the variable
  245. * and/or free data that the shadow variable points to (optional)
  246. *
  247. * This function releases the memory for all <_, id> shadow variable
  248. * instances, callers should stop referencing them accordingly.
  249. */
  250. void klp_shadow_free_all(unsigned long id, klp_shadow_dtor_t dtor)
  251. {
  252. struct klp_shadow *shadow;
  253. unsigned long flags;
  254. int i;
  255. spin_lock_irqsave(&klp_shadow_lock, flags);
  256. /* Delete all <_, id> from hash */
  257. hash_for_each(klp_shadow_hash, i, shadow, node) {
  258. if (klp_shadow_match(shadow, shadow->obj, id))
  259. klp_shadow_free_struct(shadow, dtor);
  260. }
  261. spin_unlock_irqrestore(&klp_shadow_lock, flags);
  262. }
  263. EXPORT_SYMBOL_GPL(klp_shadow_free_all);