diff options
Diffstat (limited to 'kernel/livepatch/shadow.c')
-rw-r--r-- | kernel/livepatch/shadow.c | 108 |
1 files changed, 71 insertions, 37 deletions
diff --git a/kernel/livepatch/shadow.c b/kernel/livepatch/shadow.c index fdac27588d60..83958c814439 100644 --- a/kernel/livepatch/shadow.c +++ b/kernel/livepatch/shadow.c | |||
@@ -113,8 +113,10 @@ void *klp_shadow_get(void *obj, unsigned long id) | |||
113 | } | 113 | } |
114 | EXPORT_SYMBOL_GPL(klp_shadow_get); | 114 | EXPORT_SYMBOL_GPL(klp_shadow_get); |
115 | 115 | ||
116 | static void *__klp_shadow_get_or_alloc(void *obj, unsigned long id, void *data, | 116 | static void *__klp_shadow_get_or_alloc(void *obj, unsigned long id, |
117 | size_t size, gfp_t gfp_flags, bool warn_on_exist) | 117 | size_t size, gfp_t gfp_flags, |
118 | klp_shadow_ctor_t ctor, void *ctor_data, | ||
119 | bool warn_on_exist) | ||
118 | { | 120 | { |
119 | struct klp_shadow *new_shadow; | 121 | struct klp_shadow *new_shadow; |
120 | void *shadow_data; | 122 | void *shadow_data; |
@@ -125,18 +127,15 @@ static void *__klp_shadow_get_or_alloc(void *obj, unsigned long id, void *data, | |||
125 | if (shadow_data) | 127 | if (shadow_data) |
126 | goto exists; | 128 | goto exists; |
127 | 129 | ||
128 | /* Allocate a new shadow variable for use inside the lock below */ | 130 | /* |
131 | * Allocate a new shadow variable. Fill it with zeroes by default. | ||
132 | * More complex setting can be done by @ctor function. But it is | ||
133 | * called only when the buffer is really used (under klp_shadow_lock). | ||
134 | */ | ||
129 | new_shadow = kzalloc(size + sizeof(*new_shadow), gfp_flags); | 135 | new_shadow = kzalloc(size + sizeof(*new_shadow), gfp_flags); |
130 | if (!new_shadow) | 136 | if (!new_shadow) |
131 | return NULL; | 137 | return NULL; |
132 | 138 | ||
133 | new_shadow->obj = obj; | ||
134 | new_shadow->id = id; | ||
135 | |||
136 | /* Initialize the shadow variable if data provided */ | ||
137 | if (data) | ||
138 | memcpy(new_shadow->data, data, size); | ||
139 | |||
140 | /* Look for <obj, id> again under the lock */ | 139 | /* Look for <obj, id> again under the lock */ |
141 | spin_lock_irqsave(&klp_shadow_lock, flags); | 140 | spin_lock_irqsave(&klp_shadow_lock, flags); |
142 | shadow_data = klp_shadow_get(obj, id); | 141 | shadow_data = klp_shadow_get(obj, id); |
@@ -150,6 +149,22 @@ static void *__klp_shadow_get_or_alloc(void *obj, unsigned long id, void *data, | |||
150 | goto exists; | 149 | goto exists; |
151 | } | 150 | } |
152 | 151 | ||
152 | new_shadow->obj = obj; | ||
153 | new_shadow->id = id; | ||
154 | |||
155 | if (ctor) { | ||
156 | int err; | ||
157 | |||
158 | err = ctor(obj, new_shadow->data, ctor_data); | ||
159 | if (err) { | ||
160 | spin_unlock_irqrestore(&klp_shadow_lock, flags); | ||
161 | kfree(new_shadow); | ||
162 | pr_err("Failed to construct shadow variable <%p, %lx> (%d)\n", | ||
163 | obj, id, err); | ||
164 | return NULL; | ||
165 | } | ||
166 | } | ||
167 | |||
153 | /* No <obj, id> found, so attach the newly allocated one */ | 168 | /* No <obj, id> found, so attach the newly allocated one */ |
154 | hash_add_rcu(klp_shadow_hash, &new_shadow->node, | 169 | hash_add_rcu(klp_shadow_hash, &new_shadow->node, |
155 | (unsigned long)new_shadow->obj); | 170 | (unsigned long)new_shadow->obj); |
@@ -170,26 +185,32 @@ exists: | |||
170 | * klp_shadow_alloc() - allocate and add a new shadow variable | 185 | * klp_shadow_alloc() - allocate and add a new shadow variable |
171 | * @obj: pointer to parent object | 186 | * @obj: pointer to parent object |
172 | * @id: data identifier | 187 | * @id: data identifier |
173 | * @data: pointer to data to attach to parent | ||
174 | * @size: size of attached data | 188 | * @size: size of attached data |
175 | * @gfp_flags: GFP mask for allocation | 189 | * @gfp_flags: GFP mask for allocation |
190 | * @ctor: custom constructor to initialize the shadow data (optional) | ||
191 | * @ctor_data: pointer to any data needed by @ctor (optional) | ||
192 | * | ||
193 | * Allocates @size bytes for new shadow variable data using @gfp_flags. | ||
194 | * The data are zeroed by default. They are further initialized by @ctor | ||
195 | * function if it is not NULL. The new shadow variable is then added | ||
196 | * to the global hashtable. | ||
176 | * | 197 | * |
177 | * Allocates @size bytes for new shadow variable data using @gfp_flags | 198 | * If an existing <obj, id> shadow variable can be found, this routine will |
178 | * and copies @size bytes from @data into the new shadow variable's own | 199 | * issue a WARN, exit early and return NULL. |
179 | * data space. If @data is NULL, @size bytes are still allocated, but | ||
180 | * no copy is performed. The new shadow variable is then added to the | ||
181 | * global hashtable. | ||
182 | * | 200 | * |
183 | * If an existing <obj, id> shadow variable can be found, this routine | 201 | * This function guarantees that the constructor function is called only when |
184 | * will issue a WARN, exit early and return NULL. | 202 | * the variable did not exist before. The cost is that @ctor is called |
203 | * in atomic context under a spin lock. | ||
185 | * | 204 | * |
186 | * Return: the shadow variable data element, NULL on duplicate or | 205 | * Return: the shadow variable data element, NULL on duplicate or |
187 | * failure. | 206 | * failure. |
188 | */ | 207 | */ |
189 | void *klp_shadow_alloc(void *obj, unsigned long id, void *data, | 208 | void *klp_shadow_alloc(void *obj, unsigned long id, |
190 | size_t size, gfp_t gfp_flags) | 209 | size_t size, gfp_t gfp_flags, |
210 | klp_shadow_ctor_t ctor, void *ctor_data) | ||
191 | { | 211 | { |
192 | return __klp_shadow_get_or_alloc(obj, id, data, size, gfp_flags, true); | 212 | return __klp_shadow_get_or_alloc(obj, id, size, gfp_flags, |
213 | ctor, ctor_data, true); | ||
193 | } | 214 | } |
194 | EXPORT_SYMBOL_GPL(klp_shadow_alloc); | 215 | EXPORT_SYMBOL_GPL(klp_shadow_alloc); |
195 | 216 | ||
@@ -197,37 +218,51 @@ EXPORT_SYMBOL_GPL(klp_shadow_alloc); | |||
197 | * klp_shadow_get_or_alloc() - get existing or allocate a new shadow variable | 218 | * klp_shadow_get_or_alloc() - get existing or allocate a new shadow variable |
198 | * @obj: pointer to parent object | 219 | * @obj: pointer to parent object |
199 | * @id: data identifier | 220 | * @id: data identifier |
200 | * @data: pointer to data to attach to parent | ||
201 | * @size: size of attached data | 221 | * @size: size of attached data |
202 | * @gfp_flags: GFP mask for allocation | 222 | * @gfp_flags: GFP mask for allocation |
223 | * @ctor: custom constructor to initialize the shadow data (optional) | ||
224 | * @ctor_data: pointer to any data needed by @ctor (optional) | ||
203 | * | 225 | * |
204 | * Returns a pointer to existing shadow data if an <obj, id> shadow | 226 | * Returns a pointer to existing shadow data if an <obj, id> shadow |
205 | * variable is already present. Otherwise, it creates a new shadow | 227 | * variable is already present. Otherwise, it creates a new shadow |
206 | * variable like klp_shadow_alloc(). | 228 | * variable like klp_shadow_alloc(). |
207 | * | 229 | * |
208 | * This function guarantees that only one shadow variable exists with | 230 | * This function guarantees that only one shadow variable exists with the given |
209 | * the given @id for the given @obj. It also guarantees that the shadow | 231 | * @id for the given @obj. It also guarantees that the constructor function |
210 | * variable will be initialized by the given @data only when it did not | 232 | * will be called only when the variable did not exist before. The cost is |
211 | * exist before. | 233 | * that @ctor is called in atomic context under a spin lock. |
212 | * | 234 | * |
213 | * Return: the shadow variable data element, NULL on failure. | 235 | * Return: the shadow variable data element, NULL on failure. |
214 | */ | 236 | */ |
215 | void *klp_shadow_get_or_alloc(void *obj, unsigned long id, void *data, | 237 | void *klp_shadow_get_or_alloc(void *obj, unsigned long id, |
216 | size_t size, gfp_t gfp_flags) | 238 | size_t size, gfp_t gfp_flags, |
239 | klp_shadow_ctor_t ctor, void *ctor_data) | ||
217 | { | 240 | { |
218 | return __klp_shadow_get_or_alloc(obj, id, data, size, gfp_flags, false); | 241 | return __klp_shadow_get_or_alloc(obj, id, size, gfp_flags, |
242 | ctor, ctor_data, false); | ||
219 | } | 243 | } |
220 | EXPORT_SYMBOL_GPL(klp_shadow_get_or_alloc); | 244 | EXPORT_SYMBOL_GPL(klp_shadow_get_or_alloc); |
221 | 245 | ||
246 | static void klp_shadow_free_struct(struct klp_shadow *shadow, | ||
247 | klp_shadow_dtor_t dtor) | ||
248 | { | ||
249 | hash_del_rcu(&shadow->node); | ||
250 | if (dtor) | ||
251 | dtor(shadow->obj, shadow->data); | ||
252 | kfree_rcu(shadow, rcu_head); | ||
253 | } | ||
254 | |||
222 | /** | 255 | /** |
223 | * klp_shadow_free() - detach and free a <obj, id> shadow variable | 256 | * klp_shadow_free() - detach and free a <obj, id> shadow variable |
224 | * @obj: pointer to parent object | 257 | * @obj: pointer to parent object |
225 | * @id: data identifier | 258 | * @id: data identifier |
259 | * @dtor: custom callback that can be used to unregister the variable | ||
260 | * and/or free data that the shadow variable points to (optional) | ||
226 | * | 261 | * |
227 | * This function releases the memory for this <obj, id> shadow variable | 262 | * This function releases the memory for this <obj, id> shadow variable |
228 | * instance, callers should stop referencing it accordingly. | 263 | * instance, callers should stop referencing it accordingly. |
229 | */ | 264 | */ |
230 | void klp_shadow_free(void *obj, unsigned long id) | 265 | void klp_shadow_free(void *obj, unsigned long id, klp_shadow_dtor_t dtor) |
231 | { | 266 | { |
232 | struct klp_shadow *shadow; | 267 | struct klp_shadow *shadow; |
233 | unsigned long flags; | 268 | unsigned long flags; |
@@ -239,8 +274,7 @@ void klp_shadow_free(void *obj, unsigned long id) | |||
239 | (unsigned long)obj) { | 274 | (unsigned long)obj) { |
240 | 275 | ||
241 | if (klp_shadow_match(shadow, obj, id)) { | 276 | if (klp_shadow_match(shadow, obj, id)) { |
242 | hash_del_rcu(&shadow->node); | 277 | klp_shadow_free_struct(shadow, dtor); |
243 | kfree_rcu(shadow, rcu_head); | ||
244 | break; | 278 | break; |
245 | } | 279 | } |
246 | } | 280 | } |
@@ -252,11 +286,13 @@ EXPORT_SYMBOL_GPL(klp_shadow_free); | |||
252 | /** | 286 | /** |
253 | * klp_shadow_free_all() - detach and free all <*, id> shadow variables | 287 | * klp_shadow_free_all() - detach and free all <*, id> shadow variables |
254 | * @id: data identifier | 288 | * @id: data identifier |
289 | * @dtor: custom callback that can be used to unregister the variable | ||
290 | * and/or free data that the shadow variable points to (optional) | ||
255 | * | 291 | * |
256 | * This function releases the memory for all <*, id> shadow variable | 292 | * This function releases the memory for all <*, id> shadow variable |
257 | * instances, callers should stop referencing them accordingly. | 293 | * instances, callers should stop referencing them accordingly. |
258 | */ | 294 | */ |
259 | void klp_shadow_free_all(unsigned long id) | 295 | void klp_shadow_free_all(unsigned long id, klp_shadow_dtor_t dtor) |
260 | { | 296 | { |
261 | struct klp_shadow *shadow; | 297 | struct klp_shadow *shadow; |
262 | unsigned long flags; | 298 | unsigned long flags; |
@@ -266,10 +302,8 @@ void klp_shadow_free_all(unsigned long id) | |||
266 | 302 | ||
267 | /* Delete all <*, id> from hash */ | 303 | /* Delete all <*, id> from hash */ |
268 | hash_for_each(klp_shadow_hash, i, shadow, node) { | 304 | hash_for_each(klp_shadow_hash, i, shadow, node) { |
269 | if (klp_shadow_match(shadow, shadow->obj, id)) { | 305 | if (klp_shadow_match(shadow, shadow->obj, id)) |
270 | hash_del_rcu(&shadow->node); | 306 | klp_shadow_free_struct(shadow, dtor); |
271 | kfree_rcu(shadow, rcu_head); | ||
272 | } | ||
273 | } | 307 | } |
274 | 308 | ||
275 | spin_unlock_irqrestore(&klp_shadow_lock, flags); | 309 | spin_unlock_irqrestore(&klp_shadow_lock, flags); |