shadow.c 9.3 KB

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