简体   繁体   中英

Kmalloc doesn't work correctly

I'm having some problems with this system call and probably with kmalloc.

Well , basically I'm building a system call , and this system call and the kernel are compiled without a problem. But when I call my system call , the shell shows a lot of message and then the computer crash.

I think the problem is with the kmalloc , because when I remove the kmalloc and other code that envolves the kmalloc the system call works perfectly.

For example: bloqueados = kmalloc(sizeof(struct bloqueio),GFP_ATOMIC);

Just to add : Maybe it's ocurring a deadlock , but I don't know accuratelly.

Thank you.

This is the code :

#include <linux/linkage.h>
#include <linux/io_block_unblock.h>
//#include <linux/printk.h>
#include <linux/kernel.h>
#include <linux/unistd.h>
#include <linux/stddef.h>
#include <linux/slab.h>
#include <linux/gfp.h>
#include <linux/unistd.h>
#include <linux/uaccess.h>
#include <linux/fs.h>
#include <linux/mount.h>
#include <linux/genhd.h>
#include <linux/device.h>

struct bloqueio *bloqueados;
EXPORT_SYMBOLL(bloqueados);
asmlinkage long sys_io_block(const char __user *particao, const char __user *arquivo){
    int id_inode;
    struct file *arq;
    char *part;
    struct bloqueio *tmp;
    arq  = filp_open(arquivo,O_CREAT, S_IRWXU);
    printk(KERN_EMERG "\nstruct file criado para o arquivo %s",arquivo);
    id_inode = arq->f_path.dentry->d_inode->i_ino;
    printk(KERN_EMERG "\nO inode do arquivo %s e  %d",arquivo, id_inode);
    //part = arq->f_path.mnt->mnt_sb->s_bdev->bd_part->__dev.init_name;
    //printk(KERN_EMERG "\nA particao a qual o arquivo se encontra e %s",part);
    if(bloqueados == NULL){
            bloqueados = kmalloc(sizeof(struct bloqueio),GFP_ATOMIC);//<--------------
            if(!bloqueados){
                    printk(KERN_EMERG "\n Erro ao alocar memoria");
                    return 0;
            }
            bloqueados->inode = id_inode;
            bloqueados->tipo = 0;
            bloqueados->prox = NULL;
    }
    else
    {
            tmp = kmalloc(sizeof(struct bloqueio), GFP_ATOMIC);//<--------------------
            if(!bloqueados){
                    printk(KERN_EMERG "\n Erro ao alocar memoria");
                    return 0;
            }
            tmp->inode = id_inode;
            tmp->tipo = 0;
            tmp->prox = bloqueados->prox;
            bloqueados->prox = tmp;
    }
    return 1;

}

Just to add , this is the struct bloqueio.

extern int contem(const char *arquivo);

struct bloqueio{
   int inode;
   int tipo;   // 0 -> arquivo   1 -> partição
   struct bloqueio *prox;
};

 extern struct bloqueio *bloqueados;

Apparently I can't comment yet, so I will ask a couple questions as an answer...

I see "EXPORT_SYMBOLL(bloqueados);" ... was this a mistype just in your copy/paste or is that mistyped in the actual code? This could be a problem...

Whats the reason you are using GFP_ATOMIC vs GFP_KERNEL?

This may help,

http://www.linuxjournal.com/article/6930

It recommends not using GFP_ATOMIC if you don't absolutely need it... because it can fail. I might be missing something, but I don't see why you need it... you don't appear to be using any locks.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM