int __init __symbol_put_init(void) { const char * symbol_name ; const char * mod_name ; struct module * fmodule ; symbol_name = "symbol_A"; mod_name = "test_module"; //定义待查找的模块名为“test_module” fmodule = find_module( mod_name ); //调用查找模块函数 if(fmodule != NULL ) { printk("<0>before calling __symbol_put,\n"); printk("<0>ref of %s is: %d\n",mod_name, module_refcount(fmodule)); __symbol_put(symbol_name); //will dec the count printk("<0>after calling __symbol_put,\n"); printk("<0>ref of %s is: %d\n",mod_name, module_refcount(fmodule)); } else { printk("<0>find %s failed!\n", mod_name ); } return 0; }
void xproto_put(const xproto_table_t *xtable) { BUG_ON(!xtable); #ifdef CONFIG_MODULE_UNLOAD DBG(GENERAL, "%s refcount was %d\n", xtable->name, module_refcount(xtable->owner)); BUG_ON(module_refcount(xtable->owner) <= 0); #endif module_put(xtable->owner); }
static int _put_module(struct module *mod) { int count = 0; if(mod == NULL) { DE("module is NULL.\n"); return -1; } mod->state = MODULE_STATE_LIVE; /*mod->exit = force_exit;*/ count = module_refcount(mod); while(count) { module_put(mod); --count; } #if 0 #ifdef CONFIG_SMP DE("CONFIG_SMP.\n"); for(i = 0;i < NR_CPUS; i++) { refparams = (local_t *)&mod->refparams[i]; local_set(refparams, 0); } #else DE("NO CONFIG_SMP.\n"); local_set(&mod->ref, 0); #endif #endif return 0; }
const xproto_table_t *xproto_get(xpd_type_t cardtype) { const xproto_table_t *xtable; if(cardtype >= XPD_TYPE_NOMODULE) return NULL; xtable = xprotocol_tables[cardtype]; if(!xtable) { /* Try to load the relevant module */ int ret = request_module(XPD_TYPE_PREFIX "%d", cardtype); if(ret != 0) { NOTICE("%s: Failed to load module for type=%d. exit status=%d.\n", __FUNCTION__, cardtype, ret); /* Drop through: we may be lucky... */ } xtable = xprotocol_tables[cardtype]; } if(xtable) { BUG_ON(!xtable->owner); #ifdef CONFIG_MODULE_UNLOAD DBG(GENERAL, "%s refcount was %d\n", xtable->name, module_refcount(xtable->owner)); #endif if(!try_module_get(xtable->owner)) { ERR("%s: try_module_get for %s failed.\n", __FUNCTION__, xtable->name); return NULL; } } return xtable; }
static int my_open( struct inode *inode, struct file *file ) { static int count = 0 ; p_kbuff = kbuff = kmem_cache_alloc( mycache, GFP_KERNEL ); pr_info( "[ALLOCATING OBJECT] kbuff - %p\n", kbuff ); printk( KERN_INFO "Current reference count: %d - %d times opened\n", module_refcount( THIS_MODULE ), count ); return 0; }
/** * This function is called when a user wants to use this device * and has called the open function. * * The function will keep a count of how many people * tried to open it and increments it each time * this function is called * * The function prints out two pieces of information * 1. Number of times open() was called on this device * 2. Number of processes accessing this device right now * * Return value * Always returns SUCCESS * */ static int char_dev_open(struct inode *inode, struct file *file) { static int counter = 0; counter++; printk(KERN_INFO "Number of times open() was called: %d\n", counter); printk (KERN_INFO " MAJOR number = %d, MINOR number = %d\n",imajor (inode), iminor (inode)); printk(KERN_INFO "Process id of the current process: %d\n",current->pid ); printk (KERN_INFO "ref=%d\n", module_refcount(THIS_MODULE)); return SUCCESS; }
int tilt_sensor_open(struct inode *inode, struct file *filp) { if (module_refcount(THIS_MODULE) > DEV_ACCESS_NUM) { return -EBUSY; } try_module_get(THIS_MODULE); printk("tilt_sensor_open() : successful.\n"); return 0; }
static int __init hello_init(void) { //current 当前进程对象 printk(KERN_INFO "current process name:%s pid:%i\n", current->comm, current->pid); printk(KERN_INFO "param is:%d\n", param); printk(KERN_INFO "Hello world enter\n"); printk(KERN_INFO "param is:%d\n", param); printk(KERN_ALERT "This module:%p==%p/n", &__this_module, THIS_MODULE); printk(KERN_ALERT "module: state:%d\n", THIS_MODULE->state); printk(KERN_ALERT "module: name:%s\n", THIS_MODULE->name); printk("module: name:%s\n", THIS_MODULE->name); printk("module refcount:%d\n", module_refcount(THIS_MODULE)); /// try_module_get inc refcount //remod 在hello_exit 之前检查refcount 如果不为1 则无法卸载掉模块 /*printk("module get:%d\n", module_get(THIS_MODULE));*/ printk("module refcount:%d\n", module_refcount(THIS_MODULE)); return 0; }
int __init try_module_get_init(void) { int ret ; const char * name; struct module * fmodule; name = "test_module"; //定义待查找的模块名为“test_module” fmodule = find_module( name ); //调用查找模块函数 if( fmodule != NULL ) { printk("<0>before calling try_module_get,\n"); printk("<0>refs of %s is: %d\n",name, module_refcount(fmodule)); ret = try_module_get(fmodule); printk("<0>after calling try_module_get,\n"); printk("<0>ret = %d\n",ret); printk("<0>refs of %s is: %d\n",name, module_refcount(fmodule)); } return 0; }
static int mycdrv_open(struct inode *inode, struct file *file) { static int counter = 0; pr_info(" attempting to open device: %s:\n", MYDEV_NAME); pr_info(" MAJOR number = %d, MINOR number = %d\n", imajor(inode), iminor(inode)); counter++; pr_info(" successfully open device: %s:\n\n", MYDEV_NAME); pr_info("I have been opened %d times since being loaded\n", counter); pr_info("ref=%d\n", (int)module_refcount(THIS_MODULE)); return 0; }
int __init __module_ref_addr_init(void) { local_t * addr; unsigned int cpu = get_cpu(); //获取当前cpu ID /*addr 为指向当前模块引用计数的指针*/ addr = __module_ref_addr( THIS_MODULE, cpu ); printk("<0>addr: %lx\n", (unsigned long)addr); printk("<0>originally,\n"); //输出初始时当前模块的引用计数 printk("<0>refs of this module is: %d\n",module_refcount(THIS_MODULE)); local_inc(addr); //实现将addr所指向的内容加1 printk("<0>after calling local_inc,\n"); printk("<0>refs of this module is: %d\n",module_refcount(THIS_MODULE)); local_dec(addr); //实现将addr所指向的内容减1 printk("<0>after calling local_dec,\n"); printk("<0>refs of this module is: %d\n",module_refcount(THIS_MODULE)); put_cpu(); //允许抢占 preempt_enable( ) return 0; }
static int mycdrv_open (struct inode *inode, struct file *file) { static int counter = 0; char *kbuf = kmalloc (KBUF_SIZE, GFP_KERNEL); file->private_data = kbuf; printk (KERN_INFO " attempting to open device: %s:\n", MYDEV_NAME); printk (KERN_INFO " MAJOR number = %d, MINOR number = %d\n", imajor (inode), iminor (inode)); counter++; printk (KERN_INFO " successfully open device: %s:\n\n", MYDEV_NAME); printk (KERN_INFO "I have been opened %d times since being loaded\n", counter); printk (KERN_INFO "ref=%d\n", module_refcount (THIS_MODULE)); return 0; }
static int proc_read_drivers_callback(struct device_driver *driver, void *d) { char **p = d; struct pcmcia_driver *p_drv = container_of(driver, struct pcmcia_driver, drv); *p += sprintf(*p, "%-24.24s 1 %d\n", p_drv->drv.name, #ifdef CONFIG_MODULE_UNLOAD (p_drv->owner) ? module_refcount(p_drv->owner) : 1 #else 1 #endif ); d = (void *) p; return 0; }
static int mycdrv_open (struct inode *inode, struct file *file) { static int counter = 0; printk (KERN_INFO " attempting to open device: %s:\n", MYDEV_NAME); printk (KERN_INFO " MAJOR number = %d, MINOR number = %d\n", imajor (inode), iminor (inode)); counter++; printk (KERN_INFO " successfully open device: %s:\n\n", MYDEV_NAME); printk (KERN_INFO "I have been opened %d times since being loaded\n", counter); printk (KERN_INFO "ref=%d\n", module_refcount (THIS_MODULE)); printk (KERN_INFO "/n rc from requesting module mod_fun is: %d\n", request_module ("%s", "lab1_module2")); return 0; }
static int mycdrv_open (struct inode *inode, struct file *file) { static int counter = 0; printk (KERN_INFO " attempting to open device: %s:\n", MYDEV_NAME); printk (KERN_INFO " MAJOR number = %d, MINOR number = %d\n", imajor (inode), iminor (inode)); counter++; printk (KERN_INFO " successfully open device: %s:\n\n", MYDEV_NAME); printk (KERN_INFO "I have been opened %d times since being loaded\n", counter); printk (KERN_INFO "ref=%d\n", module_refcount (THIS_MODULE)); /* turn this on to inhibit seeking */ /* file->f_mode = file->f_mode & ~FMODE_LSEEK; */ return 0; }
// -------- stats -------------------------------------------------------- static int stats_read(struct seq_file* seq, void* v) { int relative_usage = (fifo.empty.count*100)/fifo.size; seq_printf(seq, "size: %lu\nused: %d\nempty: %d\nusage percent: %d\n\ncurrent seq_no: %llu\ninsertitions: %lu\nremovals: %lu\n\naccess count: %lu\n\n", fifo.size, fifo.empty.count, fifo.full.count, relative_usage, fifo.seq_no, fifo.insertitions, fifo.removals, module_refcount(THIS_MODULE)); return 0; }