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 84aafcd138068ab767560739ae5261381d2b4286 Mon Sep 17 00:00:00 2001 From: Sazan1209 Date: Tue, 21 May 2024 17:57:38 +0300 Subject: [PATCH 2/3] task3 --- task3/Readme.md | 1 + task3/patch | 84 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 85 insertions(+) create mode 100644 task3/Readme.md create mode 100644 task3/patch diff --git a/task3/Readme.md b/task3/Readme.md new file mode 100644 index 0000000..f8fb28b --- /dev/null +++ b/task3/Readme.md @@ -0,0 +1 @@ +Ожидается, что исходники ядра лежат в dev/linux-6.7.4/ diff --git a/task3/patch b/task3/patch new file mode 100644 index 0000000..3a1f3ea --- /dev/null +++ b/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 567b966a7a706c8fcd18898f75ca84dd509292de Mon Sep 17 00:00:00 2001 From: Sazan1209 Date: Tue, 21 May 2024 17:58:38 +0300 Subject: [PATCH 3/3] Revert "made task 2" This reverts commit 6053de4de91f3207046afa9b4a4a58d7ce451510. --- module2/Makefile | 9 ------- module2/compile.sh | 11 --------- module2/counter.c | 61 ---------------------------------------------- 3 files changed, 81 deletions(-) delete mode 100644 module2/Makefile delete mode 100644 module2/compile.sh delete mode 100644 module2/counter.c diff --git a/module2/Makefile b/module2/Makefile deleted file mode 100644 index 56bd7b6..0000000 --- a/module2/Makefile +++ /dev/null @@ -1,9 +0,0 @@ -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 deleted file mode 100644 index 93c8dbe..0000000 --- a/module2/compile.sh +++ /dev/null @@ -1,11 +0,0 @@ -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 deleted file mode 100644 index 4499d91..0000000 --- a/module2/counter.c +++ /dev/null @@ -1,61 +0,0 @@ -// -// 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