xref: /linux/kernel/livepatch/core.h (revision ecba29f434a8fa333356d54d2491d174c4aab8de)
1b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */
210517429SJiri Kosina #ifndef _LIVEPATCH_CORE_H
310517429SJiri Kosina #define _LIVEPATCH_CORE_H
410517429SJiri Kosina 
593862e38SJoe Lawrence #include <linux/livepatch.h>
693862e38SJoe Lawrence 
710517429SJiri Kosina extern struct mutex klp_mutex;
868007289SPetr Mladek extern struct list_head klp_patches;
910517429SJiri Kosina 
10*ecba29f4SPetr Mladek #define klp_for_each_patch_safe(patch, tmp_patch)		\
11*ecba29f4SPetr Mladek 	list_for_each_entry_safe(patch, tmp_patch, &klp_patches, list)
12*ecba29f4SPetr Mladek 
13*ecba29f4SPetr Mladek #define klp_for_each_patch(patch)	\
14*ecba29f4SPetr Mladek 	list_for_each_entry(patch, &klp_patches, list)
15*ecba29f4SPetr Mladek 
16958ef1e3SPetr Mladek void klp_free_patch_start(struct klp_patch *patch);
17e1452b60SJason Baron void klp_discard_replaced_patches(struct klp_patch *new_patch);
18d697bad5SPetr Mladek void klp_discard_nops(struct klp_patch *new_patch);
19958ef1e3SPetr Mladek 
2093862e38SJoe Lawrence static inline bool klp_is_object_loaded(struct klp_object *obj)
2193862e38SJoe Lawrence {
2293862e38SJoe Lawrence 	return !obj->name || obj->mod;
2393862e38SJoe Lawrence }
2493862e38SJoe Lawrence 
2593862e38SJoe Lawrence static inline int klp_pre_patch_callback(struct klp_object *obj)
2693862e38SJoe Lawrence {
275aaf1ab5SPetr Mladek 	int ret = 0;
2893862e38SJoe Lawrence 
295aaf1ab5SPetr Mladek 	if (obj->callbacks.pre_patch)
305aaf1ab5SPetr Mladek 		ret = (*obj->callbacks.pre_patch)(obj);
3193862e38SJoe Lawrence 
3293862e38SJoe Lawrence 	obj->callbacks.post_unpatch_enabled = !ret;
3393862e38SJoe Lawrence 
3493862e38SJoe Lawrence 	return ret;
3593862e38SJoe Lawrence }
3693862e38SJoe Lawrence 
3793862e38SJoe Lawrence static inline void klp_post_patch_callback(struct klp_object *obj)
3893862e38SJoe Lawrence {
3993862e38SJoe Lawrence 	if (obj->callbacks.post_patch)
4093862e38SJoe Lawrence 		(*obj->callbacks.post_patch)(obj);
4193862e38SJoe Lawrence }
4293862e38SJoe Lawrence 
4393862e38SJoe Lawrence static inline void klp_pre_unpatch_callback(struct klp_object *obj)
4493862e38SJoe Lawrence {
4593862e38SJoe Lawrence 	if (obj->callbacks.pre_unpatch)
4693862e38SJoe Lawrence 		(*obj->callbacks.pre_unpatch)(obj);
4793862e38SJoe Lawrence }
4893862e38SJoe Lawrence 
4993862e38SJoe Lawrence static inline void klp_post_unpatch_callback(struct klp_object *obj)
5093862e38SJoe Lawrence {
5193862e38SJoe Lawrence 	if (obj->callbacks.post_unpatch_enabled &&
5293862e38SJoe Lawrence 	    obj->callbacks.post_unpatch)
5393862e38SJoe Lawrence 		(*obj->callbacks.post_unpatch)(obj);
545aaf1ab5SPetr Mladek 
555aaf1ab5SPetr Mladek 	obj->callbacks.post_unpatch_enabled = false;
5693862e38SJoe Lawrence }
5793862e38SJoe Lawrence 
5810517429SJiri Kosina #endif /* _LIVEPATCH_CORE_H */
59