Home > other >  There to create a character device used in kfree function problems
There to create a character device used in kfree function problems

Time:09-24

Use version there way to create a character device, automatically generating device nodes, application equipment, static use cdev_alloc function dynamically allocated among the core structure struct cdev, finally successfully create character device, can also use a function pointer specified function, can use the rmmod command uninstall the module, but use insmod command module loading again after unloading, can appear problem, in my __exit function in the order from the front after the release of the device number, cancel such operation, do not know why the kfree function block is normal, if need be, can I put my post code and the phenomenon, the underlying module code:
 
# include "Linux kernel/h"
# include "Linux/module. H"
# include "Linux/fs. H"
# include "Linux/cdev. H"
# include "Linux/device. H"
# include "Linux/slab. H"



# define CDEV_CREAT_NAME "cdev_my_name_dynamic
"# define CLASS_CREAT_NAME "class_my_name_dynamic
"# define DEV_NAME_IN_ROOT_DEV "linux_26_dynamic_
"

Typedef struct __CHDEV {
Dev_t char_dev;
Unsigned major;
Unsigned baseminor;
Unsigned count;
Char * name;
} __CHDEV_TypeDef;

__CHDEV_TypeDef chdev={
. Char_dev=0,
Major=250,//the default 250
. Baseminor=8,//start device number value
. Count=3,
.name=CDEV_CREAT_NAME,
};

Struct cdev * p_chrdev=NULL;

Int my_chrdev_open (struct inode *, struct file *);
Int my_chrdev_close (struct inode *, struct file *);
Struct file_operations fops={
The owner=THIS_MODULE,
The open=my_chrdev_open,
Release=my_chrdev_close,

};

Struct class * p_class=NULL;



Static int __init my_cdev_init (void)
{
int i=0;
Synthetic complete equipment/* */
no.Chdev. Char_dev=MKDEV (chdev. Major, chdev baseminor);
/* application equipment, static */
If (register_chrdev_region (chdev char_dev, chdev. Count, chdev. Name)!=0) {
Printk (" static application equipment failure! \n");
Goto register_chr_err;
}
Printk (" static application equipment, success! \n");
Initialization/* core structure, dynamic allocation */
P_chrdev=cdev_alloc ();
If (p_chrdev==NULL) {
Printk (" core structure initialization failed! \n");
Goto cdev_alloc_err;
}
Printk (" core structure initial success! \n");
P_chrdev - & gt; Ops=& amp; Fops.
P_chrdev - & gt; The owner=THIS_MODULE;
/* registered plant */
If (cdev_add (p_chrdev, chdev char_dev, chdev, count) & lt; 0 {
Printk (" registered equipment failure! \n");
Goto cdev_add_err;
}
Printk (" registered plant success! \n");
/* create device class */
P_class=class_create (THIS_MODULE, CLASS_CREAT_NAME);
If (p_class & lt; (struct class *) 0) {
Printk (" failed to create device class! \n");
Goto class_create_err;
}
Printk (" create device class success! \n");
/* create a device node */
For (I=MINOR (chdev char_dev); iIf (device_create (p_class, NULL, MKDEV (chdev. Major, I), NULL, "% s % d", DEV_NAME_IN_ROOT_DEV, I) & lt; (struct device *) 0) {
Printk (" create a device node failure! \n");
Goto dev_create_err;
}
The else {
Printk (" to create a device node % s % d success! The \ n ", DEV_NAME_IN_ROOT_DEV, I);
}
}
return 0;
Dev_create_err:
For (I -; I>=MINOR (chdev. Char_dev); I -) {
Device_destroy (p_class, I);
}
Class_destroy (p_class);
Class_create_err:
Cdev_del (p_chrdev);
Cdev_add_err:
Kfree (p_chrdev);
Cdev_alloc_err:
Unregister_chrdev_region (chdev char_dev, chdev. Count);
Register_chr_err:
return -ENOMEM;
}


The static void __exit my_cdev_exit (void)
{
int i=0;
For (I=MINOR (chdev char_dev); iDevice_destroy (p_class, MKDEV (chdev. Major, I));
Printk (" destroy the device node % s % d success! The \ n ", DEV_NAME_IN_ROOT_DEV, I);
}
Class_destroy (p_class);
Printk (" % s destruction class success! \ n ", CLASS_CREAT_NAME);
Cdev_del (p_chrdev);
Printk (" equipment logout success! \n");
Unregister_chrdev_region (chdev char_dev, chdev. Count);
Printk (" device number release success! \n");
Kfree (p_chrdev);
Printk (" core structure release success! \n");
Printk (" uninstall modules to complete! \n");
}

Int my_chrdev_open (struct inode * I, struct file * j)
{
Printk (" % d, % s \ n ", __LINE__, __FUNCTION__);
return 0;
}

Int my_chrdev_close (struct inode * I, struct file * j)
{
Printk (" % d, % s \ n ", __LINE__, __FUNCTION__);
return 0;
}


The module_init (my_cdev_init);
Module_exit (my_cdev_exit);

MODULE_LICENSE (" GPL ");


The upper test code:
 
# include
#include
#include
#include
Int main (int arg c, char * argv [])
{
The FILE * fp=fopen (argv [1], "a");
If (fp==NULL)
{

Perror (" fopen ");
return -1;
}
Printf (" main: open success \ n ");
Sleep (3);
Close (fp);
return 0;
}


Load module:

The upper test:

Uninstall modules:

Reload the module:


CodePudding user response:

To find the reason,
 void unregister_chrdev_region (dev_t from, unsigned count) 
{
Dev_t to=the from + count;
Dev_t n, next;

nullnullnullnullnullnullnullnull
  • Related