c - 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);
这是否意味着自旋锁没有所有权?
解决方案
推荐阅读
- kubernetes - 使用 Nginx 入口对 Pod 进行负载平衡
- firebase - 如何使 Firebase 存储 URL 更短?
- javascript - 检查字符串是否与其他字符串匹配(同一行中的多个条件)
- c# - 启用 ssl 与 AWS documentDb 的超时连接
- javascript - 同一个对象怎么会在不同的浏览器中返回不同的instanceof?
- java - 为什么用 Sink.asPublisher 创建的 Publisher 在被 BroadcastHub 消费时不工作?
- azure-data-factory - 如何在 Azure 数据工厂中激活数据流(预览版)?
- java - Moneta 1.3 ExchangeRateProvider.getExchangeRate(base, term) 返回 null
- javascript - 使用 javascript 库进行图像处理
- reactjs - 将 forwardRef 与 Link 组件一起使用