I am developing a kernel module for the Linux kernel. Basically, what I need to do is populating /dev with some special char devices. Upon a write operation on such devices, I need to store the message and then make it available to readers after a timeout. I thought about implementing this with Linux work queues and it works, however, I am forced to allocate dynamic memory with vmalloc() for every write request:
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);
Is there a safe way to reclaim the memory without interfering with the work queue?