This repository has been archived on 2025-09-14. You can view files and clone it, but cannot push or open issues or pull requests.
Files
zhangyang-variable-monitor/source/module/monitor_kernel_lib.c

182 lines
5.5 KiB
C
Raw Normal View History

2023-11-16 13:17:49 +08:00
#include "monitor_kernel.h"
2023-11-16 17:39:26 +08:00
/**
* @brief watch_arg to kernel_watch_arg
*
* @param ptr: kernel space address
* @param warg: watch_arg
* @param k_watch_arg: kernel_watch_arg
* @return unsigned char
*/
static unsigned char w_arg2k_w_arg(void *ptr, watch_arg warg,
kernel_watch_arg *k_watch_arg) {
2023-11-16 13:17:49 +08:00
// k_watch_arg init
k_watch_arg->task_id = warg.task_id;
2023-11-16 21:08:27 -05:00
strncpy(k_watch_arg->name, warg.name, MAX_NAME_LEN + 1); // name
k_watch_arg->name[MAX_NAME_LEN + 1] = '\0'; // just in case
2023-11-16 13:17:49 +08:00
k_watch_arg->kptr = ptr;
k_watch_arg->length_byte = warg.length_byte;
k_watch_arg->threshold = warg.threshold;
k_watch_arg->unsigned_flag = warg.unsigned_flag;
k_watch_arg->greater_flag = warg.greater_flag;
return 0;
}
2023-11-16 17:39:26 +08:00
static void init_mm_tree(mm_tree *mm_tree) {
INIT_RADIX_TREE(&mm_tree->mm_tree, GFP_ATOMIC);
spin_lock_init(&mm_tree->mm_tree_lock);
2023-11-16 13:17:49 +08:00
}
2023-11-16 17:39:26 +08:00
static int init_buffer(unsigned int buf_size) {
2023-11-16 21:08:27 -05:00
init_mm_tree(&mm_tree_struct); // init mm_tree
2023-11-16 17:39:26 +08:00
init_diag_variant_buffer(&load_monitor_variant_buffer, buf_size);
int ret = 0;
ret = alloc_diag_variant_buffer(&load_monitor_variant_buffer);
return ret;
2023-11-16 13:17:49 +08:00
}
2023-11-16 17:39:26 +08:00
/// @brief clear all watch and reset kernel_wtimer_list/kernel_wtimer_num
2023-11-16 13:17:49 +08:00
/// @param
2023-11-16 17:39:26 +08:00
static void clear_all_watch(void) {
printk(KERN_INFO "clear all watch variable\n");
// unmap and release the page
free_all_page_list();
// cancel timer
cancel_all_hrTimer();
// clear timer
kernel_wtimer_num = 0;
memset(kernel_wtimer_list, 0, sizeof(kernel_wtimer_list));
2023-11-16 13:17:49 +08:00
}
2023-11-16 17:39:26 +08:00
/**
* @brief all module function init. orig_X | buffer
*
* @return int
*/
int monitor_init(void) {
int ret = 0;
2023-11-16 13:17:49 +08:00
2023-11-16 21:08:27 -05:00
ret = init_orig_fun(); // init orig_X
if (ret) return ret;
ret = init_buffer(50 * 1024 * 1024); // 50M
if (ret) return -1;
2023-11-16 17:39:26 +08:00
return 0;
2023-11-16 13:17:49 +08:00
}
2023-11-16 17:39:26 +08:00
/**
* @brief monitor exit: clear all watch and free buffer
*
*/
void monitor_exit() {
// clear all watch
clear_all_watch();
// free buffer
destroy_diag_variant_buffer(&load_monitor_variant_buffer);
2023-11-16 21:08:27 -05:00
printk(KERN_INFO "clear all buffer\n");
2023-11-16 17:39:26 +08:00
}
/**
* @brief start watch variable
*
* @param warg: uapi watch_arg
* @return int 0 is success
* !todo: adjust printk
*/
int start_watch_variable(watch_arg warg) {
void *kptr;
2023-11-16 13:17:49 +08:00
kernel_watch_timer *timer = NULL;
2023-11-16 17:39:26 +08:00
kernel_watch_arg k_watch_arg;
// user space address to kernel space address
kptr = convert_user_space_ptr(warg.task_id, (unsigned long)warg.ptr);
if (kptr == NULL) {
printk(KERN_ERR "Cannot access user space\n");
return -EACCES;
}
// check length
if (warg.length_byte != 1 && warg.length_byte != 2 && warg.length_byte != 4 &&
warg.length_byte != 8) {
printk(KERN_ERR "Invalid length %d\n", warg.length_byte);
2023-11-16 13:17:49 +08:00
return -EINVAL;
}
2023-11-16 17:39:26 +08:00
// k_watch_arg init
w_arg2k_w_arg(kptr, warg, &k_watch_arg);
2023-11-16 21:08:27 -05:00
timer = get_timer(warg.time_ns); // get a valuable timer
2023-11-16 13:17:49 +08:00
2023-11-16 17:39:26 +08:00
printk(KERN_INFO "ptr transform kptr: %p\n", kptr);
printk(KERN_INFO "timer: %p\n", timer);
printk(KERN_INFO "timer->sentinel: %d, timer->time_ns: %lld\n",
timer->sentinel, timer->time_ns);
printk(KERN_INFO "timer->hr_timer: %p\n", &timer->hr_timer);
2023-11-16 13:17:49 +08:00
2023-11-16 21:08:27 -05:00
TIMER_CANCEL(timer); // just in case
2023-11-16 17:39:26 +08:00
timer_add_watch(timer, k_watch_arg);
TIMER_START(timer);
2023-11-16 13:17:49 +08:00
2023-11-16 17:39:26 +08:00
printk(KERN_INFO "Start watching var: %s\n", warg.name);
2023-11-16 13:17:49 +08:00
return 0;
}
2023-11-16 17:39:26 +08:00
/**
* @brief clear watch with pid
*
* @param pid
*/
2023-11-16 13:17:49 +08:00
void clear_watch(pid_t pid) {
printk(KERN_INFO "clear pid %d 's watch variable\n", pid);
2023-11-16 21:08:27 -05:00
cancel_all_hrTimer(); // just in case
del_all_kwarg_by_pid(pid); // delete all kwarg with pid
free_page_list(pid); // free page with pid
start_all_hrTimer(); // restart timer
2023-11-16 21:15:51 -05:00
}
/**
* @brief main callback function
*
* @param timer
* @return enum hrtimer_restart
*/
enum hrtimer_restart check_variable_cb(struct hrtimer *timer) {
kernel_watch_timer *k_watch_timer =
container_of(timer, kernel_watch_timer, hr_timer);
int i = 0, j = 0;
int buffer[TIMER_MAX_WATCH_NUM]; // Buffer to store the messages
// // check all watched kernel_watch_arg
// for (i = 0; i < k_watch_timer->sentinel; i++) {
// if (read_and_compare(&k_watch_timer->k_watch_args[i])) {
// // snprintf(buffer + strlen(buffer), sizeof(buffer) - strlen(buffer), "
// // name: %s, threshold: %lld, pid: %d\n",
// // k_watch_timer->k_watch_args[i].name,
// // k_watch_timer->k_watch_args[i].threshold,
// // k_watch_timer->k_watch_args[i].task_id);
// buffer[j] = i;
// j++;
// // printk(KERN_INFO "j: name %s, threshold: %lld\n",
// // k_watch_timer->k_watch_args[i].name,
// // k_watch_timer->k_watch_args[i].threshold);
// // printk(KERN_INFO "j: %d\n", j);
// }
// }
if (j > 0) // if any threshold reached
{
printk("-------------------------------------\n");
printk("-------------watch monitor-----------\n");
printk("Threshold reached:\n");
for (i = 0; i < j; i++) {
printk(" name: %s, threshold: %lld, pid: %d\n",
k_watch_timer->k_watch_args[buffer[i]].name, //! todo
k_watch_timer->k_watch_args[buffer[i]].threshold,
k_watch_timer->k_watch_args[buffer[i]].task_id);
}
// print_task_stack();
// restart timer after 1s
hrtimer_forward(timer, timer->base->get_time(), ktime_set(1, 0)); //! todo
printk("-------------------------------------\n");
} else {
// keep frequency
hrtimer_forward(timer, timer->base->get_time(), k_watch_timer->kt);
}
return HRTIMER_RESTART; // restart timer
2023-11-16 13:17:49 +08:00
}