Skip to contentSkip to conversation

vfree in timerfunciton causes kernel crash

DISCUSSION Mikkel Christiansen 2 participants 2 messages Apr 16, 2004

lore
Share & tools
lore
#0 lore raw

Hi

Idea: a module allocates memory (vmalloc) for userspace program which then craches. Due to lack of activity timer is expires and free's the unused memory (vfree). (see tc_core.c later in this mail for details)

Problem: when timer expires and vfree is called then kernel crashes - or rather freezes silently.

Can anyone explain why this happens? a kernel bug?

Cheers
Mikkel

kernel 2.6.5

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/spinlock.h>
#include <linux/vmalloc.h>
#include <linux/timer.h>
#include <linux/sched.h>

#define MODULE_NAME "tf"

#ifdef __KERNEL__

int *buf;
struct timer_list timer;

static void timeoutfun(unsigned long b) {
printk("tf: timeoutfun\n");
vfree(buf);
}

struct timer_list timer;

int __init init_tf(void)
{
printk("init_tf\n");
buf = vmalloc(10*sizeof(int));

init_timer(&timer); /* Initialization of the timer */

  timer.function = &timeoutfun;
  timer.data     = 10;
  timer.expires  = jiffies + (10 * HZ); /* 1 sec */
  
  add_timer(&timer);

return 0;
}

void __exit exit_tf(void)
{
printk("exit_tf\n");
vfree(buf);
}

module_init(init_tf);
module_exit(exit_tf);
MODULE_LICENSE("GPL");

#endif

KERNEL_SOURCE = /home/mixxel/pack/linux-2.6.4
PWD = pwd
obj-m := tf.o

tf-objs := tf_core.o

default:

	make -C ${KERNEL_SOURCE} SUBDIRS=${PWD} modules

clean:

	rm *.{o,ko} .*.cmd
#1 lore raw
Quoted 11 lines · Mikkel Christiansen

Idea: a module allocates memory (vmalloc) for userspace program which then craches.
Due to lack of activity timer is expires and free's the unused memory (vfree).
(see tc_core.c later in this mail for details)
Problem: when timer expires and vfree is called then kernel crashes - or rather freezes silently.
Can anyone explain why this happens? a kernel bug?
Cheers
Mikkel
kernel 2.6.5

Use schedule_work() to do this from process context. It's a programming error (specifically a deadlock) to vfree() from interrupt context or with interrupts disabled.

-- wli