0

我正在制作一个包含三个部分的模块。我在 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);
4

1 回答 1

0

问题很简单。我已经初始化了计时器值,但没有初始化计时器本身。定时器的初始化是通过以下函数完成的:

init_timer(struct timer_list *timer);

在这场追逐中

init_timer(&my_timer); 
于 2014-01-26T22:02:31.463 回答