我正在为 Linux 内核开发一个内核模块。基本上,我需要做的是用一些特殊的字符设备填充 /dev。在此类设备上进行写操作时,我需要存储消息,然后在超时后将其提供给读者。我考虑过使用 Linux 工作队列来实现它并且它可以工作,但是,我不得不为每个写入请求使用 vmalloc() 分配动态内存:
struct delayed_message {
struct delayed_work delayed_work;
struct list_head list;
struct group *group;
struct message *message;
};
delayed_message = vmalloc(sizeof(struct delayed_message));
if (!delayed_message) {
printk(KERN_ERR "%s: delayed write error: vmalloc failed on delayed message allocation\n",
KBUILD_MODNAME);
ret = -ENOMEM;
goto free_message_buffer;
}
INIT_DELAYED_WORK(&delayed_message->delayed_work, &publisher_work);
delayed_message->group = group;
delayed_message->message = message;
queue_delayed_work(group->work_queue, &delayed_message->delayed_work, msecs_to_jiffies(group->delay_ms));
// Keep the pointer to the delayed message
spin_lock(&group->delayed_messages_spinlock);
list_add_tail(&delayed_message->list, &group->delayed_messages);
spin_unlock(&group->delayed_messages_spinlock);
有没有一种安全的方法可以在不干扰工作队列的情况下回收内存?