Signed-off-by: Wenchao Hao <haowenchao2@huawei.com>
This commit is contained in:
Wenchao Hao 2024-06-13 15:49:10 +08:00
parent 3d4e51ff8c
commit 3bd8ea3958
4 changed files with 129 additions and 6 deletions

View File

@ -1,10 +1,11 @@
KDIR=/mnt/mainline KDIR=/mnt/mainline
#KDIR=/usr/src/kernels/$(shell uname -r) #KDIR=/usr/src/kernels/$(shell uname -r)
obj-m += demo.o #obj-m += demo.o
obj-m += blk_kprobe.o #obj-m += blk_kprobe.o
obj-m += kobject-example.o #obj-m += kobject-example.o
obj-m += kprobe_trace.o #obj-m += kprobe_trace.o
obj-m += lockup.o
all: all:
make -C $(KDIR) M=$(shell pwd) modules make -C $(KDIR) M=$(shell pwd) modules

View File

@ -17,6 +17,7 @@ void test_func2(struct test_struct p);
void test_func1(struct test_struct *p) void test_func1(struct test_struct *p)
{ {
printk("%d\n", p->val);
printk("%s\n", p->name); printk("%s\n", p->name);
printk("%s\n", p->pname); printk("%s\n", p->pname);
printk("%d\n", p->arr[1]); printk("%d\n", p->arr[1]);
@ -25,6 +26,7 @@ void test_func1(struct test_struct *p)
void test_func2(struct test_struct p) void test_func2(struct test_struct p)
{ {
printk("%d\n", p.val);
printk("%s\n", p.name); printk("%s\n", p.name);
printk("%s\n", p.pname); printk("%s\n", p.pname);
printk("%d\n", p.arr[1]); printk("%d\n", p.arr[1]);

120
lockup.c Normal file
View File

@ -0,0 +1,120 @@
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/cdev.h>
#include <linux/workqueue.h>
MODULE_LICENSE("GPL v2");
static struct work_struct dwork;
static struct workqueue_struct *wq;
static void demo_work(struct work_struct * unused)
{
int i = 0, j = 0;
// set_worker_desc("hwc");
printk("[cpu%d]: %s %d\n", raw_smp_processor_id(), __func__, __LINE__);
while(j < 2000) {
for (i = 0; i < 10000000; i++)
if (i % 100000 == 0)
printk("[cpu%d]: %s %d, i is %d, j is %d\n", raw_smp_processor_id(), __func__, __LINE__, i, j);
j++;
}
}
static ssize_t demo_read(struct file *filp, char __user *buff, size_t size, loff_t *pos)
{
preempt_disable();
printk("[cpu%d]: %s %d\n", raw_smp_processor_id(), __func__, __LINE__);
while(1);
return size;
}
static ssize_t demo_write(struct file *filp, const char __user *buff, size_t size, loff_t *pos)
{
printk("[cpu%d]: %s %d\n", raw_smp_processor_id(), __func__, __LINE__);
local_irq_disable();
while(1);
printk("[cpu%d]: %s %d\n", raw_smp_processor_id(), __func__, __LINE__);
return size;
}
static int demo_open(struct inode *inode, struct file *filp)
{
printk("%s %d\n", __func__, __LINE__);
return 0;
}
static int demo_release(struct inode *inode, struct file *filp)
{
printk("%s %d\n", __func__, __LINE__);
return 0;
}
struct file_operations demo_fops = {
.owner = THIS_MODULE,
.read = demo_read,
.write = demo_write,
.open = demo_open,
.release = demo_release,
};
static dev_t dev;
static struct cdev *cdev;
static int demo_init(void)
{
int rc;
INIT_WORK(&dwork, demo_work);
rc = alloc_chrdev_region(&dev, 0, 1, "demo");
if (rc) {
printk("failed to alloc chrdev number\n");
return rc;
}
cdev = cdev_alloc();
if (!cdev) {
printk("failed to alloc cdev\n");
rc = -ENOMEM;
goto out_unregister_chrdev;
}
cdev->owner = THIS_MODULE;
cdev_init(cdev, &demo_fops);
rc = cdev_add(cdev, dev, 1);
if (rc) {
printk("failed to add char device\n");
goto out_free_cdev;
}
wq = create_workqueue("demo123456789abcdefghijklmnopq");
queue_work(wq, &dwork);
return 0;
out_free_cdev:
kobject_put(&cdev->kobj);
out_unregister_chrdev:
unregister_chrdev_region(dev, 1);
return rc;
}
static void demo_exit(void)
{
cdev_del(cdev);
unregister_chrdev_region(dev, 1);
destroy_workqueue(wq);
}
module_init(demo_init);
module_exit(demo_exit);

View File

@ -1,6 +1,6 @@
#!/bin/sh #!/bin/sh
major=$(cat /proc/devices | grep demo) major=$(cat /proc/devices | grep demo | awk '{print $1}')
mknode /dev/demo c $major 0 mknod /dev/demo c $major 0