Ai
2 Star 4 Fork 2

Rong Tao/linux-5.10.13

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
livepatch-sample.c 2.45 KB
一键复制 编辑 原始数据 按行查看 历史
Rong Tao 提交于 2021-10-11 14:47 +08:00 . livepatch demo
// SPDX-License-Identifier: GPL-2.0-or-later
/*
* livepatch-sample.c - Kernel Live Patching Sample Module
*
* Copyright (C) 2014 Seth Jennings <sjenning@redhat.com>
*/
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/livepatch.h>
#include <linux/sched.h>
#include <linux/sched/loadavg.h>
#include <linux/sched/stat.h>
#include <linux/pid_namespace.h>
#include <linux/proc_fs.h>
/*
* This (dumb) live patch overrides the function that prints the
* kernel boot cmdline when /proc/cmdline is read.
*
* Example:
*
* $ cat /proc/cmdline
* <your cmdline>
*
* $ insmod livepatch-sample.ko
* $ cat /proc/cmdline
* this has been live patched
*
* $ echo 0 > /sys/kernel/livepatch/livepatch_sample/enabled
* $ cat /proc/cmdline
* <your cmdline>
*/
#include <linux/seq_file.h>
static int livepatch_cmdline_proc_show(struct seq_file *m, void *v)
{
seq_printf(m, "%s\n", "this has been live patched");
return 0;
}
#define LOAD_INT(x) ((x) >> FSHIFT)
#define LOAD_FRAC(x) LOAD_INT(((x) & (FIXED_1-1)) * 100)
static int livepatch_loadavg_proc_show(struct seq_file *m, void *v)
{
unsigned long avnrun[3];
#if 0
get_avenrun(avnrun, FIXED_1/200, 0);
seq_printf(m, "RongTao: %lu.%02lu %lu.%02lu %lu.%02lu %ld/%d %d\n",
LOAD_INT(avnrun[0]), LOAD_FRAC(avnrun[0]),
LOAD_INT(avnrun[1]), LOAD_FRAC(avnrun[1]),
LOAD_INT(avnrun[2]), LOAD_FRAC(avnrun[2]),
nr_running(), nr_threads,
idr_get_cursor(&task_active_pid_ns(current)->idr) - 1);
#elif 0
seq_printf(m, "RongTao: %lu.%02lu %lu.%02lu %lu.%02lu\n",
LOAD_INT(avnrun[0]), LOAD_FRAC(avnrun[0]),
LOAD_INT(avnrun[1]), LOAD_FRAC(avnrun[1]),
LOAD_INT(avnrun[2]), LOAD_FRAC(avnrun[2]));
#else
seq_printf(m, "RongTao: 11111\n");
#endif
return 0;
}
/**
* 补丁函数
*/
static struct klp_func funcs[] = {
{
.old_name = "cmdline_proc_show",
.new_func = livepatch_cmdline_proc_show,
},
{
.old_name = "loadavg_proc_show",
.new_func = livepatch_loadavg_proc_show,
}, { }
};
static struct klp_object objs[] = {
{
/* name being NULL means vmlinux */
.funcs = funcs,
}, { }
};
static struct klp_patch patch = {
.mod = THIS_MODULE,
.objs = objs,
};
static int livepatch_init(void)
{
/**
* 使能补丁
*/
return klp_enable_patch(&patch);
}
static void livepatch_exit(void)
{
}
module_init(livepatch_init);
module_exit(livepatch_exit);
MODULE_LICENSE("GPL");
MODULE_INFO(livepatch, "Y");
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C
1
https://gitee.com/rtoax/linux-5.10.13.git
git@gitee.com:rtoax/linux-5.10.13.git
rtoax
linux-5.10.13
linux-5.10.13
master

搜索帮助