18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci// Copyright (C) 2018 Joe Lawrence <joe.lawrence@redhat.com>
38c2ecf20Sopenharmony_ci
48c2ecf20Sopenharmony_ci#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
58c2ecf20Sopenharmony_ci
68c2ecf20Sopenharmony_ci#include <linux/module.h>
78c2ecf20Sopenharmony_ci#include <linux/kernel.h>
88c2ecf20Sopenharmony_ci#include <linux/livepatch.h>
98c2ecf20Sopenharmony_ci
108c2ecf20Sopenharmony_cistatic int pre_patch_ret;
118c2ecf20Sopenharmony_cimodule_param(pre_patch_ret, int, 0644);
128c2ecf20Sopenharmony_ciMODULE_PARM_DESC(pre_patch_ret, "pre_patch_ret (default=0)");
138c2ecf20Sopenharmony_ci
148c2ecf20Sopenharmony_cistatic const char *const module_state[] = {
158c2ecf20Sopenharmony_ci	[MODULE_STATE_LIVE]	= "[MODULE_STATE_LIVE] Normal state",
168c2ecf20Sopenharmony_ci	[MODULE_STATE_COMING]	= "[MODULE_STATE_COMING] Full formed, running module_init",
178c2ecf20Sopenharmony_ci	[MODULE_STATE_GOING]	= "[MODULE_STATE_GOING] Going away",
188c2ecf20Sopenharmony_ci	[MODULE_STATE_UNFORMED]	= "[MODULE_STATE_UNFORMED] Still setting it up",
198c2ecf20Sopenharmony_ci};
208c2ecf20Sopenharmony_ci
218c2ecf20Sopenharmony_cistatic void callback_info(const char *callback, struct klp_object *obj)
228c2ecf20Sopenharmony_ci{
238c2ecf20Sopenharmony_ci	if (obj->mod)
248c2ecf20Sopenharmony_ci		pr_info("%s: %s -> %s\n", callback, obj->mod->name,
258c2ecf20Sopenharmony_ci			module_state[obj->mod->state]);
268c2ecf20Sopenharmony_ci	else
278c2ecf20Sopenharmony_ci		pr_info("%s: vmlinux\n", callback);
288c2ecf20Sopenharmony_ci}
298c2ecf20Sopenharmony_ci
308c2ecf20Sopenharmony_ci/* Executed on object patching (ie, patch enablement) */
318c2ecf20Sopenharmony_cistatic int pre_patch_callback(struct klp_object *obj)
328c2ecf20Sopenharmony_ci{
338c2ecf20Sopenharmony_ci	callback_info(__func__, obj);
348c2ecf20Sopenharmony_ci	return pre_patch_ret;
358c2ecf20Sopenharmony_ci}
368c2ecf20Sopenharmony_ci
378c2ecf20Sopenharmony_ci/* Executed on object unpatching (ie, patch disablement) */
388c2ecf20Sopenharmony_cistatic void post_patch_callback(struct klp_object *obj)
398c2ecf20Sopenharmony_ci{
408c2ecf20Sopenharmony_ci	callback_info(__func__, obj);
418c2ecf20Sopenharmony_ci}
428c2ecf20Sopenharmony_ci
438c2ecf20Sopenharmony_ci/* Executed on object unpatching (ie, patch disablement) */
448c2ecf20Sopenharmony_cistatic void pre_unpatch_callback(struct klp_object *obj)
458c2ecf20Sopenharmony_ci{
468c2ecf20Sopenharmony_ci	callback_info(__func__, obj);
478c2ecf20Sopenharmony_ci}
488c2ecf20Sopenharmony_ci
498c2ecf20Sopenharmony_ci/* Executed on object unpatching (ie, patch disablement) */
508c2ecf20Sopenharmony_cistatic void post_unpatch_callback(struct klp_object *obj)
518c2ecf20Sopenharmony_ci{
528c2ecf20Sopenharmony_ci	callback_info(__func__, obj);
538c2ecf20Sopenharmony_ci}
548c2ecf20Sopenharmony_ci
558c2ecf20Sopenharmony_cistatic void patched_work_func(struct work_struct *work)
568c2ecf20Sopenharmony_ci{
578c2ecf20Sopenharmony_ci	pr_info("%s\n", __func__);
588c2ecf20Sopenharmony_ci}
598c2ecf20Sopenharmony_ci
608c2ecf20Sopenharmony_cistatic struct klp_func no_funcs[] = {
618c2ecf20Sopenharmony_ci	{}
628c2ecf20Sopenharmony_ci};
638c2ecf20Sopenharmony_ci
648c2ecf20Sopenharmony_cistatic struct klp_func busymod_funcs[] = {
658c2ecf20Sopenharmony_ci	{
668c2ecf20Sopenharmony_ci		.old_name = "busymod_work_func",
678c2ecf20Sopenharmony_ci		.new_func = patched_work_func,
688c2ecf20Sopenharmony_ci	}, {}
698c2ecf20Sopenharmony_ci};
708c2ecf20Sopenharmony_ci
718c2ecf20Sopenharmony_cistatic struct klp_object objs[] = {
728c2ecf20Sopenharmony_ci	{
738c2ecf20Sopenharmony_ci		.name = NULL,	/* vmlinux */
748c2ecf20Sopenharmony_ci		.funcs = no_funcs,
758c2ecf20Sopenharmony_ci		.callbacks = {
768c2ecf20Sopenharmony_ci			.pre_patch = pre_patch_callback,
778c2ecf20Sopenharmony_ci			.post_patch = post_patch_callback,
788c2ecf20Sopenharmony_ci			.pre_unpatch = pre_unpatch_callback,
798c2ecf20Sopenharmony_ci			.post_unpatch = post_unpatch_callback,
808c2ecf20Sopenharmony_ci		},
818c2ecf20Sopenharmony_ci	},	{
828c2ecf20Sopenharmony_ci		.name = "test_klp_callbacks_mod",
838c2ecf20Sopenharmony_ci		.funcs = no_funcs,
848c2ecf20Sopenharmony_ci		.callbacks = {
858c2ecf20Sopenharmony_ci			.pre_patch = pre_patch_callback,
868c2ecf20Sopenharmony_ci			.post_patch = post_patch_callback,
878c2ecf20Sopenharmony_ci			.pre_unpatch = pre_unpatch_callback,
888c2ecf20Sopenharmony_ci			.post_unpatch = post_unpatch_callback,
898c2ecf20Sopenharmony_ci		},
908c2ecf20Sopenharmony_ci	},	{
918c2ecf20Sopenharmony_ci		.name = "test_klp_callbacks_busy",
928c2ecf20Sopenharmony_ci		.funcs = busymod_funcs,
938c2ecf20Sopenharmony_ci		.callbacks = {
948c2ecf20Sopenharmony_ci			.pre_patch = pre_patch_callback,
958c2ecf20Sopenharmony_ci			.post_patch = post_patch_callback,
968c2ecf20Sopenharmony_ci			.pre_unpatch = pre_unpatch_callback,
978c2ecf20Sopenharmony_ci			.post_unpatch = post_unpatch_callback,
988c2ecf20Sopenharmony_ci		},
998c2ecf20Sopenharmony_ci	}, { }
1008c2ecf20Sopenharmony_ci};
1018c2ecf20Sopenharmony_ci
1028c2ecf20Sopenharmony_cistatic struct klp_patch patch = {
1038c2ecf20Sopenharmony_ci	.mod = THIS_MODULE,
1048c2ecf20Sopenharmony_ci	.objs = objs,
1058c2ecf20Sopenharmony_ci};
1068c2ecf20Sopenharmony_ci
1078c2ecf20Sopenharmony_cistatic int test_klp_callbacks_demo_init(void)
1088c2ecf20Sopenharmony_ci{
1098c2ecf20Sopenharmony_ci	return klp_enable_patch(&patch);
1108c2ecf20Sopenharmony_ci}
1118c2ecf20Sopenharmony_ci
1128c2ecf20Sopenharmony_cistatic void test_klp_callbacks_demo_exit(void)
1138c2ecf20Sopenharmony_ci{
1148c2ecf20Sopenharmony_ci}
1158c2ecf20Sopenharmony_ci
1168c2ecf20Sopenharmony_cimodule_init(test_klp_callbacks_demo_init);
1178c2ecf20Sopenharmony_cimodule_exit(test_klp_callbacks_demo_exit);
1188c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
1198c2ecf20Sopenharmony_ciMODULE_INFO(livepatch, "Y");
1208c2ecf20Sopenharmony_ciMODULE_AUTHOR("Joe Lawrence <joe.lawrence@redhat.com>");
1218c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("Livepatch test: livepatch demo");
122