首页 > 解决方案 > spin_unlock 成功,内核模块中没有 spin_lock

问题描述

我写了一个基本的内核代码来检查我是否可以从其他内核控制路径调用 spin_unlock 而不调用 spin_lock 并且它有效。

#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/delay.h>
#include <linux/spinlock.h>
#include <linux/slab.h>
#include <linux/kthread.h>

MODULE_LICENSE("GPL");
spinlock_t *my_lock;
struct task_struct *mythread;

static int threadfn(void *data)
{
    pr_info("Kernel thread running on processor:%d\n", smp_processor_id());
    msleep(1000);
    pr_info("Kernel thread unlocking without calling lock on processor:%d\n", smp_processor_id());
    spin_unlock(my_lock);
    return 0;
}

static int __init test_hello_init(void)
{
    my_lock = kmalloc(sizeof(spinlock_t), GFP_KERNEL);
    spin_lock_init(my_lock);
    pr_info("Init function running on processor:%d\n", smp_processor_id());
    spin_lock(my_lock);
    pr_info("Init function locked once on processor:%d\n", smp_processor_id());
    mythread = kthread_run(threadfn, NULL, "mythread");
    pr_info("Init function starting kernel thread\n");
    pr_info("Init function trying to lock twice on processor:%d\n", smp_processor_id());
    spin_lock(my_lock);
    pr_info("Init function locked on processor:%d\n", smp_processor_id());
    spin_unlock(my_lock);
    pr_info("Init function unlocked on processor:%d\n", smp_processor_id());
    kfree(my_lock);
    return -1;
}

static void __exit test_hello_exit(void)
{
}

module_init(test_hello_init);
module_exit(test_hello_exit);

这是否意味着自旋锁没有所有权?

标签: clinuxconcurrencylinux-kernellinux-device-driver

解决方案


推荐阅读