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