shadow-vars.rst 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. ================
  2. Shadow Variables
  3. ================
  4. Shadow variables are a simple way for livepatch modules to associate
  5. additional "shadow" data with existing data structures. Shadow data is
  6. allocated separately from parent data structures, which are left
  7. unmodified. The shadow variable API described in this document is used
  8. to allocate/add and remove/free shadow variables to/from their parents.
  9. The implementation introduces a global, in-kernel hashtable that
  10. associates pointers to parent objects and a numeric identifier of the
  11. shadow data. The numeric identifier is a simple enumeration that may be
  12. used to describe shadow variable version, class or type, etc. More
  13. specifically, the parent pointer serves as the hashtable key while the
  14. numeric id subsequently filters hashtable queries. Multiple shadow
  15. variables may attach to the same parent object, but their numeric
  16. identifier distinguishes between them.
  17. 1. Brief API summary
  18. ====================
  19. (See the full API usage docbook notes in livepatch/shadow.c.)
  20. A hashtable references all shadow variables. These references are
  21. stored and retrieved through a <obj, id> pair.
  22. * The klp_shadow variable data structure encapsulates both tracking
  23. meta-data and shadow-data:
  24. - meta-data
  25. - obj - pointer to parent object
  26. - id - data identifier
  27. - data[] - storage for shadow data
  28. It is important to note that the klp_shadow_alloc() and
  29. klp_shadow_get_or_alloc() are zeroing the variable by default.
  30. They also allow to call a custom constructor function when a non-zero
  31. value is needed. Callers should provide whatever mutual exclusion
  32. is required.
  33. Note that the constructor is called under klp_shadow_lock spinlock. It allows
  34. to do actions that can be done only once when a new variable is allocated.
  35. * klp_shadow_get() - retrieve a shadow variable data pointer
  36. - search hashtable for <obj, id> pair
  37. * klp_shadow_alloc() - allocate and add a new shadow variable
  38. - search hashtable for <obj, id> pair
  39. - if exists
  40. - WARN and return NULL
  41. - if <obj, id> doesn't already exist
  42. - allocate a new shadow variable
  43. - initialize the variable using a custom constructor and data when provided
  44. - add <obj, id> to the global hashtable
  45. * klp_shadow_get_or_alloc() - get existing or alloc a new shadow variable
  46. - search hashtable for <obj, id> pair
  47. - if exists
  48. - return existing shadow variable
  49. - if <obj, id> doesn't already exist
  50. - allocate a new shadow variable
  51. - initialize the variable using a custom constructor and data when provided
  52. - add <obj, id> pair to the global hashtable
  53. * klp_shadow_free() - detach and free a <obj, id> shadow variable
  54. - find and remove a <obj, id> reference from global hashtable
  55. - if found
  56. - call destructor function if defined
  57. - free shadow variable
  58. * klp_shadow_free_all() - detach and free all <*, id> shadow variables
  59. - find and remove any <*, id> references from global hashtable
  60. - if found
  61. - call destructor function if defined
  62. - free shadow variable
  63. 2. Use cases
  64. ============
  65. (See the example shadow variable livepatch modules in samples/livepatch/
  66. for full working demonstrations.)
  67. For the following use-case examples, consider commit 1d147bfa6429
  68. ("mac80211: fix AP powersave TX vs. wakeup race"), which added a
  69. spinlock to net/mac80211/sta_info.h :: struct sta_info. Each use-case
  70. example can be considered a stand-alone livepatch implementation of this
  71. fix.
  72. Matching parent's lifecycle
  73. ---------------------------
  74. If parent data structures are frequently created and destroyed, it may
  75. be easiest to align their shadow variables lifetimes to the same
  76. allocation and release functions. In this case, the parent data
  77. structure is typically allocated, initialized, then registered in some
  78. manner. Shadow variable allocation and setup can then be considered
  79. part of the parent's initialization and should be completed before the
  80. parent "goes live" (ie, any shadow variable get-API requests are made
  81. for this <obj, id> pair.)
  82. For commit 1d147bfa6429, when a parent sta_info structure is allocated,
  83. allocate a shadow copy of the ps_lock pointer, then initialize it::
  84. #define PS_LOCK 1
  85. struct sta_info *sta_info_alloc(struct ieee80211_sub_if_data *sdata,
  86. const u8 *addr, gfp_t gfp)
  87. {
  88. struct sta_info *sta;
  89. spinlock_t *ps_lock;
  90. /* Parent structure is created */
  91. sta = kzalloc(sizeof(*sta) + hw->sta_data_size, gfp);
  92. /* Attach a corresponding shadow variable, then initialize it */
  93. ps_lock = klp_shadow_alloc(sta, PS_LOCK, sizeof(*ps_lock), gfp,
  94. NULL, NULL);
  95. if (!ps_lock)
  96. goto shadow_fail;
  97. spin_lock_init(ps_lock);
  98. ...
  99. When requiring a ps_lock, query the shadow variable API to retrieve one
  100. for a specific struct sta_info:::
  101. void ieee80211_sta_ps_deliver_wakeup(struct sta_info *sta)
  102. {
  103. spinlock_t *ps_lock;
  104. /* sync with ieee80211_tx_h_unicast_ps_buf */
  105. ps_lock = klp_shadow_get(sta, PS_LOCK);
  106. if (ps_lock)
  107. spin_lock(ps_lock);
  108. ...
  109. When the parent sta_info structure is freed, first free the shadow
  110. variable::
  111. void sta_info_free(struct ieee80211_local *local, struct sta_info *sta)
  112. {
  113. klp_shadow_free(sta, PS_LOCK, NULL);
  114. kfree(sta);
  115. ...
  116. In-flight parent objects
  117. ------------------------
  118. Sometimes it may not be convenient or possible to allocate shadow
  119. variables alongside their parent objects. Or a livepatch fix may
  120. require shadow varibles to only a subset of parent object instances. In
  121. these cases, the klp_shadow_get_or_alloc() call can be used to attach
  122. shadow variables to parents already in-flight.
  123. For commit 1d147bfa6429, a good spot to allocate a shadow spinlock is
  124. inside ieee80211_sta_ps_deliver_wakeup()::
  125. int ps_lock_shadow_ctor(void *obj, void *shadow_data, void *ctor_data)
  126. {
  127. spinlock_t *lock = shadow_data;
  128. spin_lock_init(lock);
  129. return 0;
  130. }
  131. #define PS_LOCK 1
  132. void ieee80211_sta_ps_deliver_wakeup(struct sta_info *sta)
  133. {
  134. spinlock_t *ps_lock;
  135. /* sync with ieee80211_tx_h_unicast_ps_buf */
  136. ps_lock = klp_shadow_get_or_alloc(sta, PS_LOCK,
  137. sizeof(*ps_lock), GFP_ATOMIC,
  138. ps_lock_shadow_ctor, NULL);
  139. if (ps_lock)
  140. spin_lock(ps_lock);
  141. ...
  142. This usage will create a shadow variable, only if needed, otherwise it
  143. will use one that was already created for this <obj, id> pair.
  144. Like the previous use-case, the shadow spinlock needs to be cleaned up.
  145. A shadow variable can be freed just before its parent object is freed,
  146. or even when the shadow variable itself is no longer required.
  147. Other use-cases
  148. ---------------
  149. Shadow variables can also be used as a flag indicating that a data
  150. structure was allocated by new, livepatched code. In this case, it
  151. doesn't matter what data value the shadow variable holds, its existence
  152. suggests how to handle the parent object.
  153. 3. References
  154. =============
  155. * https://github.com/dynup/kpatch
  156. The livepatch implementation is based on the kpatch version of shadow
  157. variables.
  158. * http://files.mkgnu.net/files/dynamos/doc/papers/dynamos_eurosys_07.pdf
  159. Dynamic and Adaptive Updates of Non-Quiescent Subsystems in Commodity
  160. Operating System Kernels (Kritis Makris, Kyung Dong Ryu 2007) presented
  161. a datatype update technique called "shadow data structures".