[Add] 新增pstore测试模块:test_ps.ko
This commit is contained in:
2
Kconfig
2
Kconfig
@ -30,3 +30,5 @@ source "crypto/Kconfig"
|
|||||||
source "lib/Kconfig"
|
source "lib/Kconfig"
|
||||||
|
|
||||||
source "lib/Kconfig.debug"
|
source "lib/Kconfig.debug"
|
||||||
|
|
||||||
|
source "gaoyang3513/Kconfig"
|
||||||
|
|||||||
2
Makefile
2
Makefile
@ -624,6 +624,7 @@ net-y := net/
|
|||||||
libs-y := lib/
|
libs-y := lib/
|
||||||
core-y := usr/
|
core-y := usr/
|
||||||
virt-y := virt/
|
virt-y := virt/
|
||||||
|
drivers-y += gaoyang3513/
|
||||||
endif # KBUILD_EXTMOD
|
endif # KBUILD_EXTMOD
|
||||||
|
|
||||||
ifeq ($(dot-config),1)
|
ifeq ($(dot-config),1)
|
||||||
@ -1130,6 +1131,7 @@ export KBUILD_LDS := arch/$(SRCARCH)/kernel/vmlinux.lds
|
|||||||
export LDFLAGS_vmlinux
|
export LDFLAGS_vmlinux
|
||||||
# used by scripts/package/Makefile
|
# used by scripts/package/Makefile
|
||||||
export KBUILD_ALLDIRS := $(sort $(filter-out arch/%,$(vmlinux-alldirs)) arch Documentation include samples scripts tools)
|
export KBUILD_ALLDIRS := $(sort $(filter-out arch/%,$(vmlinux-alldirs)) arch Documentation include samples scripts tools)
|
||||||
|
export KBUILD_ALLDIRS += $(sort $(KBUILD_ALLDIRS) gaoyang3513)
|
||||||
|
|
||||||
vmlinux-deps := $(KBUILD_LDS) $(KBUILD_VMLINUX_INIT) $(KBUILD_VMLINUX_MAIN) $(KBUILD_VMLINUX_LIBS)
|
vmlinux-deps := $(KBUILD_LDS) $(KBUILD_VMLINUX_INIT) $(KBUILD_VMLINUX_MAIN) $(KBUILD_VMLINUX_LIBS)
|
||||||
|
|
||||||
|
|||||||
@ -798,3 +798,5 @@ CONFIG_RCU_CPU_STALL_TIMEOUT=60
|
|||||||
CONFIG_FUNCTION_TRACER=y
|
CONFIG_FUNCTION_TRACER=y
|
||||||
CONFIG_BLK_DEV_IO_TRACE=y
|
CONFIG_BLK_DEV_IO_TRACE=y
|
||||||
CONFIG_LKDTM=y
|
CONFIG_LKDTM=y
|
||||||
|
|
||||||
|
CONFIG_TESTING_PSTORE=m
|
||||||
|
|||||||
7
gaoyang3513/Kconfig
Normal file
7
gaoyang3513/Kconfig
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
# SPDX-License-Identifier: GPL-2.0
|
||||||
|
|
||||||
|
menu "Gaoyang3513 Custom"
|
||||||
|
|
||||||
|
source "gaoyang3513/testings/Kconfig"
|
||||||
|
|
||||||
|
endmenu
|
||||||
2
gaoyang3513/Makefile
Normal file
2
gaoyang3513/Makefile
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
|
||||||
|
obj-y += testings/
|
||||||
8
gaoyang3513/testings/Kconfig
Normal file
8
gaoyang3513/testings/Kconfig
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
#
|
||||||
|
# Testings configuration
|
||||||
|
#
|
||||||
|
|
||||||
|
config TESTING_PSTORE
|
||||||
|
tristate "Testing Pstore"
|
||||||
|
---help---
|
||||||
|
Testing Pstore
|
||||||
2
gaoyang3513/testings/Makefile
Normal file
2
gaoyang3513/testings/Makefile
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
|
||||||
|
obj-$(CONFIG_TESTING_PSTORE) += pstore/
|
||||||
71
gaoyang3513/testings/pstore/Makefile
Normal file
71
gaoyang3513/testings/pstore/Makefile
Normal file
@ -0,0 +1,71 @@
|
|||||||
|
#*******************************************************************************
|
||||||
|
# Gaoyang3513 All Right Reserved.
|
||||||
|
# Author :
|
||||||
|
# Version : V1.0.0 202x.xx.xx
|
||||||
|
# Description :
|
||||||
|
# Note : gaoyang3513@163.com Createe 202x.xx.xx
|
||||||
|
#*******************************************************************************
|
||||||
|
|
||||||
|
#*******************************************************************************
|
||||||
|
# Path information
|
||||||
|
#*******************************************************************************
|
||||||
|
LOCAL_DIR := $(realpath $(dir $(lastword $(MAKEFILE_LIST))))
|
||||||
|
KERNEL_DIR ?= $(realpath $(LOCAL_DIR)/../../../kernel)
|
||||||
|
|
||||||
|
# Subdirectory
|
||||||
|
OUTPUT_DIR := $(LOCAL_DIR)/output
|
||||||
|
INSTALL_DIR ?= $(LOCAL_DIR)/__install
|
||||||
|
#$(info Output directoty : $(OUTPUT_DIR))
|
||||||
|
#$(info Install directoty: $(INSTALL_DIR))
|
||||||
|
|
||||||
|
#*******************************************************************************
|
||||||
|
# Variables
|
||||||
|
#*******************************************************************************
|
||||||
|
MULTI_CORES ?= $(shell grep -c ^processor /proc/cpuinfo)
|
||||||
|
|
||||||
|
#*******************************************************************************
|
||||||
|
# Compile configure
|
||||||
|
#*******************************************************************************
|
||||||
|
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
|
||||||
|
|
||||||
|
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:
|
||||||
|
@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;
|
||||||
|
@if [ -d $(LOCAL_DIR)/.tmp_versions ]; then rm -rf $(LOCAL_DIR)/.tmp_versions; fi;
|
||||||
|
# File
|
||||||
|
@for f in {Module.symvers,modules.order}; do \
|
||||||
|
if [ -e $$f ]; then rm -rf $$f; fi; \
|
||||||
|
done
|
||||||
|
|
||||||
|
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-$(CONFIG_TESTING_PSTORE) += test_ps.o
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
endif # ifeq ($(KERNELRELEASE),)
|
||||||
82
gaoyang3513/testings/pstore/test_ps.c
Normal file
82
gaoyang3513/testings/pstore/test_ps.c
Normal file
@ -0,0 +1,82 @@
|
|||||||
|
#include <linux/kernel.h>
|
||||||
|
#include <linux/module.h>
|
||||||
|
#include <linux/slab.h>
|
||||||
|
#include <linux/delay.h>
|
||||||
|
#include <linux/vmalloc.h>
|
||||||
|
#include <linux/rcupdate.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 trigger_init(void) {
|
||||||
|
if (test_type == 0)
|
||||||
|
trigger_oom();
|
||||||
|
else if (test_type == 1)
|
||||||
|
trigger_oops();
|
||||||
|
else if (test_type == 2)
|
||||||
|
trigger_rcu();
|
||||||
|
|
||||||
|
return 0; // 加载模块不应该有返回值,但这里返回0避免编译警告。
|
||||||
|
}
|
||||||
|
|
||||||
|
static void __exit trigger_exit(void) {
|
||||||
|
printk(KERN_INFO "oom_trigger: Exiting the oom_trigger LKM\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
module_init(trigger_init);
|
||||||
|
module_exit(trigger_exit);
|
||||||
|
|
||||||
|
MODULE_LICENSE("GPL");
|
||||||
Reference in New Issue
Block a user