From 6053de4de91f3207046afa9b4a4a58d7ce451510 Mon Sep 17 00:00:00 2001 From: Sazan1209 Date: Tue, 21 May 2024 17:04:02 +0300 Subject: [PATCH 1/3] made task 2 --- module2/Makefile | 9 +++++++ module2/compile.sh | 11 +++++++++ module2/counter.c | 61 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 81 insertions(+) create mode 100644 module2/Makefile create mode 100644 module2/compile.sh create mode 100644 module2/counter.c diff --git a/module2/Makefile b/module2/Makefile new file mode 100644 index 0000000..56bd7b6 --- /dev/null +++ b/module2/Makefile @@ -0,0 +1,9 @@ +obj-m += counter.o + +ABS_PATH_TO_VROOT ?= ../initramfs +PATH_TO_MODULE_BUILD=$(ABS_PATH_TO_VROOT)/lib/modules/6.7.4/build + +all: + make -C "$(PATH_TO_MODULE_BUILD)" M="$(PWD)" modules +clean: + make -C "$(PATH_TO_MODULE_BUILD)" M="$(PWD)" clean \ No newline at end of file diff --git a/module2/compile.sh b/module2/compile.sh new file mode 100644 index 0000000..93c8dbe --- /dev/null +++ b/module2/compile.sh @@ -0,0 +1,11 @@ +cp -r ../module2 ../../ || exit +cd ../../module2 || exit +make clean || exit +make all || exit +cp counter.ko ../initramfs/lib/modules/6.7.4/ || exit +cd ../initramfs || exit +find . | cpio -ov --format=newc | gzip -9 > ../try/initramfs.gz || exit +cd .. +# cleanup +rm -r module2 +rm initramfs/lib/modules/6.7.4/counter.ko diff --git a/module2/counter.c b/module2/counter.c new file mode 100644 index 0000000..4499d91 --- /dev/null +++ b/module2/counter.c @@ -0,0 +1,61 @@ +// +// Created by mikhail on 21.05.24. +// +#include +#include +#include +#include +#include + +MODULE_LICENSE("MIT"); +MODULE_AUTHOR("Me"); +MODULE_DESCRIPTION("Key press counter"); + +// Counter + +static atomic_t press_count = ATOMIC_INIT(0); + +irqreturn_t irq_handler(int irq, void* dev_id) { + atomic_inc(&press_count); + return IRQ_NONE; +} + +static struct timer_list timer; + +// Timer callback + +static void reschedule_timer(void) { + mod_timer(&timer, jiffies + msecs_to_jiffies(60000)); +} + +void callback(struct timer_list* _) { + int number = atomic_read(&press_count); + pr_info("Characters typed: %d\n", number); + reschedule_timer(); +} + +// Module funcs + +static int __init counter_init(void) { + timer_setup(&timer, callback, 0); + reschedule_timer(); + int err = request_irq(1, // PS/2 irq + irq_handler, + IRQF_SHARED, + "Key counter", + (void*) irq_handler); + if (err) { + del_timer(&timer); + pr_err("request_irq failed with errcode %d\n.", err); + return err; + } + return 0; +} + +static void __exit counter_exit(void) { + free_irq(1, (void*) irq_handler); + del_timer(&timer); +} + +module_init(counter_init); +module_exit(counter_exit); \ No newline at end of file From 46013229b4070dd379c56c16ead07a4281ca86bb Mon Sep 17 00:00:00 2001 From: Sazan1209 Date: Tue, 21 May 2024 17:52:18 +0300 Subject: [PATCH 2/3] Task 3 --- dev/task3/Readme.md | 1 + dev/task3/patch | 84 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 85 insertions(+) create mode 100644 dev/task3/Readme.md create mode 100644 dev/task3/patch diff --git a/dev/task3/Readme.md b/dev/task3/Readme.md new file mode 100644 index 0000000..d63c144 --- /dev/null +++ b/dev/task3/Readme.md @@ -0,0 +1 @@ +Предполагается, что искходники ядра лежат в папке /dev/linux-6.7.4 diff --git a/dev/task3/patch b/dev/task3/patch new file mode 100644 index 0000000..3a1f3ea --- /dev/null +++ b/dev/task3/patch @@ -0,0 +1,84 @@ +diff --git a/dev/linux-6.7.4/fs/proc/base.c b/dev/linux-6.7.4/fs/proc/base.c +index dd31e3b..3f1b03e 100644 +--- a/dev/linux-6.7.4/fs/proc/base.c ++++ b/dev/linux-6.7.4/fs/proc/base.c +@@ -488,6 +488,17 @@ static int proc_pid_schedstat(struct seq_file *m, struct pid_namespace *ns, + } + #endif + ++static int proc_pid_sched_counter(struct seq_file *m, struct pid_namespace *ns, ++ struct pid *pid, struct task_struct *task) ++{ ++ if (unlikely(!sched_info_on())) ++ seq_puts(m, "0\n"); ++ else ++ seq_printf(m, "%d\n", atomic_read(&task->sched_counter)); ++ ++ return 0; ++} ++ + #ifdef CONFIG_LATENCYTOP + static int lstats_show_proc(struct seq_file *m, void *v) + { +@@ -3293,6 +3304,7 @@ static const struct pid_entry tgid_base_stuff[] = { + REG("smaps_rollup", S_IRUGO, proc_pid_smaps_rollup_operations), + REG("pagemap", S_IRUSR, proc_pagemap_operations), + #endif ++ ONE("sched_counter", S_IRUGO, proc_pid_sched_counter), + #ifdef CONFIG_SECURITY + DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations), + #endif +@@ -3642,6 +3654,7 @@ static const struct pid_entry tid_base_stuff[] = { + REG("smaps_rollup", S_IRUGO, proc_pid_smaps_rollup_operations), + REG("pagemap", S_IRUSR, proc_pagemap_operations), + #endif ++ ONE("sched_counter", S_IRUGO, proc_pid_sched_counter), + #ifdef CONFIG_SECURITY + DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations), + #endif +diff --git a/dev/linux-6.7.4/include/linux/sched.h b/dev/linux-6.7.4/include/linux/sched.h +index 292c316..f6e71ab 100644 +--- a/dev/linux-6.7.4/include/linux/sched.h ++++ b/dev/linux-6.7.4/include/linux/sched.h +@@ -751,6 +751,9 @@ struct task_struct { + */ + struct thread_info thread_info; + #endif ++ /* Task 3 field */ ++ atomic_t sched_counter; ++ + unsigned int __state; + + /* saved state for "spinlock sleepers" */ +@@ -1385,6 +1388,7 @@ struct task_struct { + #endif + + #ifdef CONFIG_FUNCTION_GRAPH_TRACER ++ + /* Index of current stored address in ret_stack: */ + int curr_ret_stack; + int curr_ret_depth; +diff --git a/dev/linux-6.7.4/init/init_task.c b/dev/linux-6.7.4/init/init_task.c +index 5727d42..67a83ec 100644 +--- a/dev/linux-6.7.4/init/init_task.c ++++ b/dev/linux-6.7.4/init/init_task.c +@@ -71,6 +71,7 @@ struct task_struct init_task + .thread_info = INIT_THREAD_INFO(init_task), + .stack_refcount = REFCOUNT_INIT(1), + #endif ++ .sched_counter = ATOMIC_INIT(0), + .__state = 0, + .stack = init_stack, + .usage = REFCOUNT_INIT(2), +diff --git a/dev/linux-6.7.4/kernel/sched/core.c b/dev/linux-6.7.4/kernel/sched/core.c +index a708d22..1d7d4d7 100644 +--- a/dev/linux-6.7.4/kernel/sched/core.c ++++ b/dev/linux-6.7.4/kernel/sched/core.c +@@ -6650,6 +6650,7 @@ static void __sched notrace __schedule(unsigned int sched_mode) + } + + next = pick_next_task(rq, prev, &rf); ++ atomic_inc(&next->sched_counter); + clear_tsk_need_resched(prev); + clear_preempt_need_resched(); + #ifdef CONFIG_SCHED_DEBUG From 08edce53eca35b886f0c4e37f65929b5d5ca5f2e Mon Sep 17 00:00:00 2001 From: Sazan1209 Date: Tue, 21 May 2024 17:56:07 +0300 Subject: [PATCH 3/3] Revert "Task 3" This reverts commit 46013229b4070dd379c56c16ead07a4281ca86bb. --- dev/task3/Readme.md | 1 - dev/task3/patch | 84 --------------------------------------------- 2 files changed, 85 deletions(-) delete mode 100644 dev/task3/Readme.md delete mode 100644 dev/task3/patch diff --git a/dev/task3/Readme.md b/dev/task3/Readme.md deleted file mode 100644 index d63c144..0000000 --- a/dev/task3/Readme.md +++ /dev/null @@ -1 +0,0 @@ -Предполагается, что искходники ядра лежат в папке /dev/linux-6.7.4 diff --git a/dev/task3/patch b/dev/task3/patch deleted file mode 100644 index 3a1f3ea..0000000 --- a/dev/task3/patch +++ /dev/null @@ -1,84 +0,0 @@ -diff --git a/dev/linux-6.7.4/fs/proc/base.c b/dev/linux-6.7.4/fs/proc/base.c -index dd31e3b..3f1b03e 100644 ---- a/dev/linux-6.7.4/fs/proc/base.c -+++ b/dev/linux-6.7.4/fs/proc/base.c -@@ -488,6 +488,17 @@ static int proc_pid_schedstat(struct seq_file *m, struct pid_namespace *ns, - } - #endif - -+static int proc_pid_sched_counter(struct seq_file *m, struct pid_namespace *ns, -+ struct pid *pid, struct task_struct *task) -+{ -+ if (unlikely(!sched_info_on())) -+ seq_puts(m, "0\n"); -+ else -+ seq_printf(m, "%d\n", atomic_read(&task->sched_counter)); -+ -+ return 0; -+} -+ - #ifdef CONFIG_LATENCYTOP - static int lstats_show_proc(struct seq_file *m, void *v) - { -@@ -3293,6 +3304,7 @@ static const struct pid_entry tgid_base_stuff[] = { - REG("smaps_rollup", S_IRUGO, proc_pid_smaps_rollup_operations), - REG("pagemap", S_IRUSR, proc_pagemap_operations), - #endif -+ ONE("sched_counter", S_IRUGO, proc_pid_sched_counter), - #ifdef CONFIG_SECURITY - DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations), - #endif -@@ -3642,6 +3654,7 @@ static const struct pid_entry tid_base_stuff[] = { - REG("smaps_rollup", S_IRUGO, proc_pid_smaps_rollup_operations), - REG("pagemap", S_IRUSR, proc_pagemap_operations), - #endif -+ ONE("sched_counter", S_IRUGO, proc_pid_sched_counter), - #ifdef CONFIG_SECURITY - DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations), - #endif -diff --git a/dev/linux-6.7.4/include/linux/sched.h b/dev/linux-6.7.4/include/linux/sched.h -index 292c316..f6e71ab 100644 ---- a/dev/linux-6.7.4/include/linux/sched.h -+++ b/dev/linux-6.7.4/include/linux/sched.h -@@ -751,6 +751,9 @@ struct task_struct { - */ - struct thread_info thread_info; - #endif -+ /* Task 3 field */ -+ atomic_t sched_counter; -+ - unsigned int __state; - - /* saved state for "spinlock sleepers" */ -@@ -1385,6 +1388,7 @@ struct task_struct { - #endif - - #ifdef CONFIG_FUNCTION_GRAPH_TRACER -+ - /* Index of current stored address in ret_stack: */ - int curr_ret_stack; - int curr_ret_depth; -diff --git a/dev/linux-6.7.4/init/init_task.c b/dev/linux-6.7.4/init/init_task.c -index 5727d42..67a83ec 100644 ---- a/dev/linux-6.7.4/init/init_task.c -+++ b/dev/linux-6.7.4/init/init_task.c -@@ -71,6 +71,7 @@ struct task_struct init_task - .thread_info = INIT_THREAD_INFO(init_task), - .stack_refcount = REFCOUNT_INIT(1), - #endif -+ .sched_counter = ATOMIC_INIT(0), - .__state = 0, - .stack = init_stack, - .usage = REFCOUNT_INIT(2), -diff --git a/dev/linux-6.7.4/kernel/sched/core.c b/dev/linux-6.7.4/kernel/sched/core.c -index a708d22..1d7d4d7 100644 ---- a/dev/linux-6.7.4/kernel/sched/core.c -+++ b/dev/linux-6.7.4/kernel/sched/core.c -@@ -6650,6 +6650,7 @@ static void __sched notrace __schedule(unsigned int sched_mode) - } - - next = pick_next_task(rq, prev, &rf); -+ atomic_inc(&next->sched_counter); - clear_tsk_need_resched(prev); - clear_preempt_need_resched(); - #ifdef CONFIG_SCHED_DEBUG