workqueue和timer模块在读取时崩溃

时间:2014-01-26 11:15:27

标签: c timer linux-kernel kernel-module proc

我正在创建一个有三个部分的模块,我正在使用debian 6。

第一个是一个定时器,每125个时钟触发一次,它的功能是挑选系统jiffies,屏蔽它们并将它们包含在一个有10个空格的循环缓冲区中,当80%的空间忙时,一个工作将被添加到将数字放入列表的工作队列中,当任何用户读取/ proc / modtimer条目时,该列表将被删除。

我遇到的问题是:我可以安装模块没有问题,/ proc条目(我将需要2但现在我只有一个实现)创建没有问题,但当我尝试读取内核崩溃。当我读取控制消息时,我认为问题出在发布过程中。我不知道问题是什么。这是我的代码

编辑:经过多次测试后,我的程序崩溃了:“del_timer_sync(& my_timer);”

EDIT2:我删除了除安装/卸载/打开/关闭/读取功能以外的所有内容,因为其他任何内容都无关紧要

EDIT3:它也可能是“add_timer(& my_timer);”

#define PROC_ENTRY "modtimer"
#define PROC_ENTRY_OPS "modconfig"
#define CBUFFER_SIZE 10
#define TRESHOLD_SIZE 80 // percentage of the treshold for the workqueue calling
#define DELAY 125 //125 tics for timer (1 each half a sec)
#define MAX_BUFFER 512

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/list.h>
#include <linux/proc_fs.h>
#include <linux/semaphore.h>
#include <linux/vmalloc.h>
#include <linux/timer.h>
#include <linux/spinlock.h>
#include <linux/timer.h>
#include <linux/workqueue.h>
#include <asm-generic/uaccess.h>
#include "cbuffer.h"

MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("Pseudo-Random number generator for DSO");
MODULE_AUTHOR("Kaostias");

/**********************************************************/
/********************** Declarations **********************/
/**********************************************************/

/*Timer*/
struct timer_list my_timer;

/*Workqueue*/
struct work_struct my_workqueue;

/* The workqueue has been planified but not resolved */
int workqueue_pendiente;

/* /proc entries */
static struct proc_dir_entry *proc_entry, *proc_entry_opts;

/*Semaphore used as spin_lock */
struct semaphore mtx;
int openDevices;

/*Spinlock*/
DEFINE_SPINLOCK(spinlock);

/*List*/
struct list_head mylist = LIST_HEAD_INIT(mylist);
typedef struct {
    int data;
    struct list_head links;
}list_item_t;

/*Buffer of integer numbers*/
cbuffer_t *cbuf;

/*Functions*/
void vacia_list_item(void);
void add_list(int num);
void fire_timer(unsigned long data);
void copy_items_into_list(struct work_struct *work);
static int modtimer_open (struct inode *, struct file *);
static int modtimer_release (struct inode*, struct file *);
static ssize_t modtimer_read (struct file *file, char __user*, size_t nbits, loff_t * offset);
/*Se escribe la entrada PROC_ENTRY_OPS*/
int procOpsWrite( struct file *punterofichero, const char __user *bufferusuario,
                        unsigned long longitud, void *data);
int procOpsRead( char *buffer, char **bufferlocation, off_t offset,
                   int buffer_lenghth, int *eof, void *data );
/*Operaciones de proc PROC_ENTRY*/
static const struct file_operations my_fops = {
    .open = modtimer_open,
    .read = modtimer_read,
    .release = modtimer_release,
};




/**********************************************************/
/****** Install/Uninstall/Open/close/read functions *******/
/**********************************************************/

/*module install*/
int install_module(void){
    int ret = 0;
    printk(KERN_INFO "installing module");
    /*Timer inicialization*/
    my_timer.expires = jiffies + DELAY;
    my_timer.data = 0;
    my_timer.function = fire_timer;
    printk(KERN_INFO "Timer created but not used");

    /*workqueue inicialization*/
    workqueue_pendiente = 0;    
    INIT_WORK(&my_workqueue, copy_items_into_list);
    printk(KERN_INFO "creada workqueue");   

    /* Semaphore inicialization */  
    sema_init(&mtx,1);
    openDevices = 0;
    printk(KERN_INFO "Semapore created");

    /*Spin_lock inicialization*/
    spin_lock_init(&spinlock);
    printk(KERN_INFO "spinlock created");

    /*buffer inicialization*/
    cbuf = create_cbuffer_t (CBUFFER_SIZE);
    printk(KERN_INFO "buffer created");

    /*list Inicialization*/
    printk(KERN_INFO "list created");

    /*  /proc  entries */
    proc_entry = create_proc_entry(PROC_ENTRY,0777, NULL);
    if (proc_entry == NULL) {
            ret = -ENOMEM;
            printk(KERN_INFO "Error: No puedo crear la entrada en proc /proc/%s\n",PROC_ENTRY);
    } else {
        proc_entry->proc_fops=&my_fops;
            printk(KERN_INFO "Entrada /proc/%s creada.\n", PROC_ENTRY);
    }

    proc_entry_opts = create_proc_entry(PROC_ENTRY_OPS,0777, NULL);
    if (proc_entry_opts == NULL) {
            ret = -ENOMEM;
            printk(KERN_INFO "Error: No puedo crear la entrada en proc /proc/%s\n",PROC_ENTRY_OPS);
        remove_proc_entry(PROC_ENTRY, NULL);
            printk(KERN_INFO "Entrada /proc/%s eliminada.\n", PROC_ENTRY);      
    } else {
            proc_entry_opts->read_proc = procOpsRead;
            proc_entry_opts->write_proc =procOpsWrite;
            printk(KERN_INFO "Entrada /proc/%s creada.\n", PROC_ENTRY_OPS);
    }
    printk(KERN_INFO "módulo instalado correctamente");
    return ret;
}

/*Uninstalling module*/
void uninstall_module(void){
    printk(KERN_INFO "Uninstalling module");

    /*  /proc  removal*/
    remove_proc_entry(PROC_ENTRY, NULL);
    printk(KERN_INFO " /proc/%s entry removed.\n", PROC_ENTRY);

    remove_proc_entry(PROC_ENTRY_OPS, NULL);
    printk(KERN_INFO " /proc/%s entry removed.\n", PROC_ENTRY_OPS);

    /*buffer destruction*/
    remove_cbuffer_t (cbuf);

    /*list gets empty*/
    vacia_list_item();

    printk(KERN_INFO "The uninstalling procese has been complete");

}


/* PROC_ENTRY /proc entry is open (Like a file) */
static int modtimer_open (struct inode *inod, struct file *f){
    int ret = 0;
    printk(KERN_INFO "entering modtimer_open");

    // timer is added (Not synchrone yet)
    if(openDevices==0){
        printk(KERN_INFO "Entra");
        my_timer.expires = jiffies + DELAY;
        add_timer(&my_timer);
    }

    openDevices++;

    printk(KERN_INFO "saliendo de modtimer_open");
    return ret;
}
/* PROC_ENTRY /proc entry is closed*/
static int modtimer_release (struct inode *inod, struct file *f){
    int ret = 0;    
    printk(KERN_INFO "entering modtimer_release");



    openDevices--; // In this moment there just will be 1 device open, i will fix this

    printk(KERN_INFO "1");
    del_timer_sync(&my_timer);
    printk(KERN_INFO "2");
    flush_scheduled_work();

    printk(KERN_INFO "modtimer_release cerrado");
    return ret;
}

module_init(install_module);
module_exit(uninstall_module);

1 个答案:

答案 0 :(得分:0)

问题非常简单。我初始化了计时器值,但没有初始化计时器本身。定时器的初始化通过以下功能完成:

init_timer(struct timer_list *timer);

在这个追逐中

init_timer(&my_timer);