0

I'm making a kernel module which writes to a file every 100 msecs. Using arch linux and kernel 6.4. timer from kernel/timer.h doesn't seem to work. Is it deprecated? What should I use?

#include <linux/module.h>
#include <linux/fs.h>
#include <linux/timer.h>
#include <linux/jiffies.h>

static int freq_sec;
static char *filepath;

static struct timer_list my_timer;

void timer_callback(struct timer_list * data) {
    printk("Hallo, Kernel %d! %s\n", freq_sec, filepath);
    file_write(filepath, "Hello from kernel module\n", 25);
}

static int __init my_init(void) {
    timer_setup(&my_timer, timer_callback, 0);
    mod_timer(&my_timer, jiffies + msecs_to_jiffies(100));
    
    return 0;
}


Artshellorok
  • 191
  • 1
  • 2
  • 12
  • A timer callback is executed in the **atomic context**. Files cannot be written in the atomic context. – Tsyvarev Sep 02 '23 at 12:54
  • @Tsyvarev how can i solve this problem? – Artshellorok Sep 02 '23 at 13:38
  • "how can i solve this problem?" - As usual, with a workqueue. Or you could drop using timer and add delayed work to the workqueue (e.g. with `schedule_delayed_work`). Any tutorial for Linux kernel programming should describe the usage of workqueues. – Tsyvarev Sep 02 '23 at 15:46

1 Answers1

0

Unfortunately I cannot test your code, but I can suggest you to use HR timers as I did at:

Antonio Petricca
  • 8,891
  • 5
  • 36
  • 74