[Mod] Backup at 23.10.14

This commit is contained in:
gaoyang3513
2024-10-14 15:48:10 +08:00
parent ff8c43208e
commit dbdc68d7ab
7 changed files with 311 additions and 5 deletions

View File

@ -0,0 +1,85 @@
#*******************************************************************************
# HangZhou Hikvision Digital Technology Co., Ltd. All Right Reserved.
# Author :
# Version : V1.0.0 2020.10.21
# Description :
# Note : yegaoyang@hikvision.com.cn Modified 2020.10.21
#*******************************************************************************
#*******************************************************************************
# Path information
#*******************************************************************************
LOCAL_DIR := $(realpath $(dir $(lastword $(MAKEFILE_LIST))))
KERNEL_DIR ?= /home/gaoyang3513/Source/06-SG20x/02-Projects/SDK_SG200x_V2/linux_5.10/build/sg2000_milkv_duos_glibc_arm64_sd
#KERNEL_DIR ?= $(HOME)/Source/06-SG20x/02-Projects/SDK_SG200x_V2/linux_5.10/build/sg2000_milkv_duos_glibc_arm64_sd/
# ParDirectory
LIBS_DIR :=
INCLUDE_DIR :=
# Subdirectory
# Output
OUTPUT_DIR := $(LOCAL_DIR)/output
INSTALL_DIR ?= $(LOCAL_DIR)/__install
#*******************************************************************************
# Variables
#*******************************************************************************
MULTI_CORES ?= $(shell grep -c ^processor /proc/cpuinfo)
MOD_NAME := test_panic
#*******************************************************************************
# Compile configure
#*******************************************************************************
ARCH ?= arm64
CROSS_COMPILE ?= ${HOME}/Source/06-SG20x/02-Projects/SDK_SG200x_V2/host-tools/gcc/gcc-linaro-7.3.1-2018.05-x86_64_aarch64-linux-gnu/bin/aarch64-linux-gnu-
CC := $(CROSS_COMPILE)gcc
LD := $(CROSS_COMPILE)ld
AR := $(CROSS_COMPILE)ar
STRIP := $(CROSS_COMPILE)strip
export ARCH CROSS_COMPILE
#*******************************************************************************
# Targets
#*******************************************************************************
.PHONY: all install clean
ifeq ($(KERNELRELEASE),)
all: init
@$(MAKE) modules -C $(KERNEL_DIR) M=$(LOCAL_DIR) -j$(MULTI_CORES)
# @$(MAKE) modules_install -C $(KERNEL_DIR) M=$(LOCAL_DIR) INSTALL_MOD_PATH=$(KERNEL_DIR)/_install_modules INSTALL_MOD_DIR=private
clean:
# File
@for n in *.o *.ko *.mod.c *.mod *.cmd *.symvers *.order; do \
find $(LOCAL_DIR) -type f -name $$n -exec rm {} \;;\
done
# Directory
@if [ -d $(LOCAL_DIR)/output ]; then rm -rf $(LOCAL_DIR)/output; fi;
@if [ -d $(LOCAL_DIR)/__install ]; then rm -rf $(LOCAL_DIR)/__install; fi;
@if [ -d $(LOCAL_DIR)/.tmp_versions ]; then rm -rf $(LOCAL_DIR)/.tmp_versions; fi;
install:
@mkdir -p $(INSTALL_DIR)/lib/modules/private
@install -m 644 -D $(MOD_NAME).ko $(INSTALL_DIR)/lib/modules/private;
init:
@mkdir -p $(OUTPUT_DIR)
else
obj-m := $(MOD_NAME).o
$(MOD_NAME)-objs := test_ps.o
INC_DIRS :=
ccflags-y +=
EXTRA_CFLAGS += -Wno-error=date-time # Fix compile error on gcc 4.9 and later
EXTRA_CFLAGS += -Wno-date-time
endif # ifeq ($(KERNELRELEASE),)

140
01-Debug/01-Panic/test_ps.c Normal file
View File

@ -0,0 +1,140 @@
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/slab.h>
#include <linux/delay.h>
#include <linux/vmalloc.h>
#include <linux/rcupdate.h>
#include <linux/kthread.h>
#include <linux/mutex.h>
static unsigned int test_type;
module_param(test_type, uint, 0400);
MODULE_PARM_DESC(test_type, "set to 1 to try to OOM (default 0)");
static int trigger_oops(void)
{
int *ptr = (int *)0; // 强制类型转换0地址为指针并尝试读取
printk(KERN_ALERT "Dereferenced NULL pointer value: %d\n", *ptr);
return 0;
}
static int trigger_oom(void)
{
void *memory = NULL;
size_t memory_size = 0;
printk(KERN_INFO "oom_trigger: Initializing the oom_trigger LKM\n");
memory_size = 100*1024*1024;
while (1) { // 尝试分配大块内存
memory = vmalloc(memory_size);
if (!memory) {
printk(KERN_ALERT "oom_trigger: Memory allocation failed\n");
break;
}
// 仅为防止编译器优化,实际不访问分配的内存
memset(memory, 0, memory_size);
}
return 0;
}
static int test_data = 0;
static struct rcu_head test_rcu_head;
static void bad_rcu_callback(struct rcu_head *head)
{
// 违规操作在RCU回调函数中睡眠这可能导致RCU grace period过长
msleep(1000);
// 违规操作修改被RCU保护的数据结构
test_data++; // 不应在RCU回调中修改全局变量
pr_info("Bad RCU callback executed\n");
}
static void trigger_rcu(void)
{
pr_info("RCU error module loaded\n");
call_rcu(&test_rcu_head, bad_rcu_callback);
}
static int is_ready_thread1, is_ready_thread2;
static struct task_struct *thread1;
static struct task_struct *thread2;
static DEFINE_MUTEX(mutex_a);
static DEFINE_MUTEX(mutex_b);
static int thread_func(void *data)
{
char *name = (char *) data;
if (!strcmp(name, "Thread1")) {
printk(KERN_INFO "%s: Acquiring mutex A...\n", name);
mutex_lock(&mutex_a);
is_ready_thread1 = 1;
while (!is_ready_thread2);
printk(KERN_INFO "%s: Got mutex A. Now trying to acquire mutex B...\n", name);
mutex_lock(&mutex_b); // This will block, causing deadlock
printk(KERN_INFO "%s: Got mutex B.\n", name);
} else {
printk(KERN_INFO "%s: Acquiring mutex B...\n", name);
mutex_lock(&mutex_b);
is_ready_thread2 = 1;
while (!is_ready_thread1);
printk(KERN_INFO "%s: Got mutex B. Now trying to acquire mutex A...\n", name);
mutex_lock(&mutex_a); // This will block, causing deadlock
printk(KERN_INFO "%s: Got mutex A.\n", name);
}
return 0;
}
static int trigger_deadlock(void)
{
thread1 = kthread_run(thread_func, (void *) "Thread1", "thread1");
if (IS_ERR(thread1)) {
printk(KERN_ERR "Failed to create thread1\n");
return PTR_ERR(thread1);
}
thread2 = kthread_run(thread_func, (void *) "Thread2", "thread2");
if (IS_ERR(thread2)) {
printk(KERN_ERR "Failed to create thread2\n");
kthread_stop(thread1);
return PTR_ERR(thread2);
}
return 0;
}
static int trigger_init(void) {
if (test_type == 0)
trigger_oops();
else if (test_type == 1)
trigger_oom();
else if (test_type == 2)
trigger_rcu();
else if (test_type == 3)
trigger_deadlock();
return 0; // 加载模块不应该有返回值但这里返回0避免编译警告。
}
static void __exit trigger_exit(void) {
if (test_type == 3) {
kthread_stop(thread1);
kthread_stop(thread2);
printk(KERN_INFO "Deadlock trigger exit.\n");
}
}
module_init(trigger_init);
module_exit(trigger_exit);
MODULE_LICENSE("GPL");

81
01-Debug/Makefile Normal file
View File

@ -0,0 +1,81 @@
#*******************************************************************************
# HangZhou Hikvision Digital Technology Co., Ltd. All Right Reserved.
# Author :
# Version : V1.0.0 2020.10.21
# Description :
# Note : yegaoyang@hikvision.com.cn Modified 2020.10.21
#*******************************************************************************
#*******************************************************************************
# Path information
#*******************************************************************************
LOCAL_DIR := $(realpath $(dir $(lastword $(MAKEFILE_LIST))))
KERNEL_DIR ?= /home/gaoyang3513/Source/06-SG20x/02-Projects/SDK_SG200x_V2/linux_5.10/build/sg2000_milkv_duos_glibc_arm64_sd
#KERNEL_DIR ?= $(HOME)/Source/06-SG20x/02-Projects/SDK_SG200x_V2/linux_5.10/build/sg2000_milkv_duos_glibc_arm64_sd/
# ParDirectory
LIBS_DIR :=
INCLUDE_DIR :=
# Subdirectory
SUB_DIRS := $(LOCAL_DIR)/01-Panic
# Output
OUTPUT_DIR := $(LOCAL_DIR)/output
INSTALL_DIR ?= $(LOCAL_DIR)/__install
#*******************************************************************************
# Variables
#*******************************************************************************
MULTI_CORES ?= $(shell grep -c ^processor /proc/cpuinfo)
MOD_NAME := test_panic
#*******************************************************************************
# Compile configure
#*******************************************************************************
ARCH ?= arm64
CROSS_COMPILE ?= ${HOME}/Source/06-SG20x/02-Projects/SDK_SG200x_V2/host-tools/gcc/gcc-linaro-7.3.1-2018.05-x86_64_aarch64-linux-gnu/bin/aarch64-linux-gnu-
CC := $(CROSS_COMPILE)gcc
LD := $(CROSS_COMPILE)ld
AR := $(CROSS_COMPILE)ar
STRIP := $(CROSS_COMPILE)strip
export ARCH CROSS_COMPILE
#*******************************************************************************
# Targets
#*******************************************************************************
.PHONY: init all clean install menuconfig
ifeq ($(KERNELRELEASE),)
all: init
@for sub in $(SUB_DIRS); do \
$(MAKE) -C $$sub || exit "$$?"; \
$(MAKE) install INSTALL_DIR=$(OUTPUT_DIR) -C $$sub || exit "$$?"; \
done;
clean:
@for sub in $(SUB_DIRS); do \
$(MAKE) clean -C $$sub || exit "$$?"; \
done;
# Directory
@if [ -d $(LOCAL_DIR)/output ]; then rm -rf $(LOCAL_DIR)/output; fi;
@if [ -d $(LOCAL_DIR)/__install ]; then rm -rf $(LOCAL_DIR)/__install; fi;
install:
@mkdir -p $(INSTALL_DIR)
@cp -arf $(OUTPUT_DIR)/. $(INSTALL_DIR)
init:
@mkdir -p $(OUTPUT_DIR);
else
# called from kernel build system: just declare what our modules are
obj-y += $(SUB_DIRS)
INC_DIRS :=
subdir-ccflags-y += $(addprefix -I, $(INC_DIRS))
endif # ifeq ($(KERNELRELEASE),)

View File

@ -24,7 +24,7 @@ INSTALL_DIR ?= $(LOCAL_DIR)/__install
#*******************************************************************************
MULTI_CORES ?= $(shell grep -c ^processor /proc/cpuinfo)
SUB_DIRS := spi/
SUB_DIRS := 01-Debug/
#*******************************************************************************
# Compile configure

View File

@ -49,7 +49,7 @@ else
all: init
@$(MAKE) modules -C $(KERNEL_DIR) M=$(LOCAL_DIR) -j$(MULTI_CORES)
@$(MAKE) modules_install -C $(KERNEL_DIR) M=$(LOCAL_DIR) INSTALL_MOD_PATH=$(KERNEL_DIR)/_install_modules INSTALL_MOD_DIR=private
# @$(MAKE) modules_install -C $(KERNEL_DIR) M=$(LOCAL_DIR) INSTALL_MOD_PATH=$(KERNEL_DIR)/_install_modules INSTALL_MOD_DIR=private
clean:
# File

View File

@ -49,7 +49,7 @@ else
all: init
@$(MAKE) modules -C $(KERNEL_DIR) M=$(LOCAL_DIR) -j$(MULTI_CORES)
@$(MAKE) modules_install -C $(KERNEL_DIR) M=$(LOCAL_DIR) INSTALL_MOD_PATH=$(KERNEL_DIR)/_install_modules INSTALL_MOD_DIR=private
# @$(MAKE) modules_install -C $(KERNEL_DIR) M=$(LOCAL_DIR) INSTALL_MOD_PATH=$(KERNEL_DIR)/_install_modules INSTALL_MOD_DIR=private
clean:
# File

View File

@ -12,8 +12,8 @@
LOCAL_DIR := $(realpath $(dir $(lastword $(MAKEFILE_LIST))))
# Subdirectory
PN5180_DIR := 01-NFC_PN5180
CMT2301_DIR := 02-Sub1G_CMT2301
PN5180_DIR := $(LOCAL_DIR)/01-NFC_PN5180
CMT2301_DIR := $(LOCAL_DIR)/02-Sub1G_CMT2301
OUTPUT_DIR := $(LOCAL_DIR)/output
INSTALL_DIR ?= $(LOCAL_DIR)/__install