preempt_enable(); return false;} #else /* ... !CONFIG_SMP */ static inline void __percpu *mod_percpu(struct module *mod){ return NULL;}static inline int percpu_modalloc(struct module *mod, unsigned long size, unsigned long align){ return -ENOMEM;}static inline void percpu_modfree(struct module *mod){}static unsigned int find_pcpusec(struct load_info *info){ return 0;}static inline void percpu_modcopy(struct module *mod, const void *from, unsigned long size){ /* pcpusec should be 0, and size of that section should be 0. */ BUG_ON(size != 0);}bool is_module_percpu_address(unsigned long addr){ return false;} #endif /* CONFIG_SMP */ #define MODINFO_ATTR(field) \static void setup_modinfo_##field(struct module *mod, const char *s) \{ \ mod->field = kstrdup(s, GFP_KERNEL); \} \static ssize_t show_modinfo_##field(struct module_attribute *mattr, \ struct module_kobject *mk, char *buffer) \{ \ return sprintf(buffer, "%s\n", mk->mod->field); \} \static int modinfo_##field##_exists(struct module *mod) \{ \ return mod->field != NULL; \} \static void free_modinfo_##field(struct module *mod) \{ \ kfree(mod->field); \ mod->field = NULL; \} \static struct module_attribute modinfo_##field = { \ .attr = { .name = __stringify(field), .mode = 0444 }, \ .show = show_modinfo_##field, \ .setup = setup_modinfo_##field, \ .test = modinfo_##field##_exists, \ .free = free_modinfo_##field, \}; MODINFO_ATTR(version);MODINFO_ATTR(srcversion); static char last_unloaded_module[MODULE_NAME_LEN+1]; #ifdef CONFIG_MODULE_UNLOAD EXPORT_TRACEPOINT_SYMBOL(module_get); /* Init the unload section of the module. */static int module_unload_init(struct module *mod){ mod->refptr = alloc_percpu(struct module_ref); if (!mod->refptr) return -ENOMEM; INIT_LIST_HEAD(&mod->source_list); INIT_LIST_HEAD(&mod->target_list); /* Hold reference count during initialization. */ __this_cpu_write(mod->refptr->incs, 1); /* Backwards compatibility macros put refcount during init. */ mod->waiter = current; return 0;} /* Does a already use b? */static int already_uses(struct module *a, struct module *b){ struct module_use *use; list_for_each_entry(use, &b->source_list, source_list) { if (use->source == a) { pr_debug("%s uses %s!\n", a->name, b->name); return 1; } } pr_debug("%s does not use %s!\n", a->name, b->name); return 0;} /* * Module a uses b * - we add 'a' as a "source", 'b' as a "target" of module use * - the module_use is added to the list of 'b' sources (so * 'b' can walk the list to see who sourced them), and of 'a' * targets (so 'a' can see what modules it targets). */static int add_module_usage(struct module *a, struct module *b){ struct module_use *use; pr_debug("Allocating new usage for %s.\n", a->name); use = kmalloc(sizeof(*use), GFP_ATOMIC); if (!use) { printk(KERN_WARNING "%s: out of memory loading\n", a->name); return -ENOMEM; } use->source = a; use->target = b; list_add(&use->source_list, &b->source_list); list_add(&use->target_list, &a->target_list); return 0;} /* Module a uses b: caller needs module_mutex() */int ref_module(struct module *a, struct module *b){ int err; if (b == NULL || already_uses(a, b)) return 0; /* If module isn't available, we fail. */ err = strong_try_module_get(b); if (err) return err; err = add_module_usage(a, b); if (err) { module_put(b); return err; } return 0;}EXPORT_SYMBOL_GPL(ref_module); /* Clear the unload stuff of the module. */static void module_unload_free(struct module *mod){ struct module_use *use, *tmp; mutex_lock(&module_mutex); list_for_each_entry_safe(use, tmp, &mod->target_list, target_list) { struct module *i = use->target; pr_debug("%s unusing %s\n", mod->name, i->name); module_put(i); list_del(&use->source_list); list_del(&use->target_list); kfree(use); } mutex_unlock(&module_mutex); free_percpu(mod->refptr);} #ifdef CONFIG_MODULE_FORCE_UNLOADstatic inline int try_force_unload(unsigned int flags){ int ret = (flags & O_TRUNC); if (ret) add_taint(TAINT_FORCED_RMMOD); return ret;}#elsestatic inline int try_force_unload(unsigned int flags){ return 0;}#endif /* CONFIG_MODULE_FORCE_UNLOAD */ struct stopref{ struct module *mod; int flags; int *forced;}; /* Whole machine is stopped with interrupts off when this runs. */static int __try_stop_module(void *_sref){ struct stopref *sref = _sref; /* If it's not unused, quit unless we're forcing. */ if (module_refcount(sref->mod) != 0) { if (!(*sref->forced = try_force_unload(sref->flags))) return -EWOULDBLOCK; } /* Mark it as dying. */ sref->mod->state = MODULE_STATE_GOING; return 0;} static int try_stop_module(struct module *mod, int flags, int *forced){ if (flags & O_NONBLOCK) { struct stopref sref = { mod, flags, forced }; return stop_machine(__try_stop_module, &sref, NULL); } else { /* We don't need to stop the machine for this. */ mod->state = MODULE_STATE_GOING; synchronize_sched(); return 0; }} unsigned long module_refcount(struct module *mod){ unsigned long incs = 0, decs = 0; int cpu; for_each_possible_cpu(cpu) decs += per_cpu_ptr(mod->refptr, cpu)->decs; /* * ensure the incs are added up after the decs. * module_put ensures incs are visible before decs with smp_wmb. * * This 2-count scheme avoids the situation where the refcount * for CPU0 is read, then CPU0 increments the module refcount, * then CPU1 drops that refcount, then the refcount for CPU1 is * read. We would record a decrement but not its corresponding * increment so we would see a low count (disaster). * * Rare situation? But module_refcount can be preempted, and we * might be tallying up 4096+ CPUs. So it is not impossible. */ smp_rmb(); for_each_possible_cpu(cpu) incs += per_cpu_ptr(mod->refptr, cpu)->incs; return incs - decs;}EXPORT_SYMBOL(module_refcount); /* This exists whether we can unload or not */static void free_module(struct module *mod); static void wait_for_zero_refcount(struct module *mod){ /* Since we might sleep for some time, release the mutex first */ mutex_unlock(&module_mutex); for (;;) { pr_debug("Looking at refcount...\n"); set_current_state(TASK_UNINTERRUPTIBLE); if (module_refcount(mod) == 0) break; schedule(); } current->state = TASK_RUNNING; mutex_lock(&module_mutex);} SYSCALL_DEFINE2(delete_module, const char __user *, name_user, unsigned int, flags){ struct module *mod; char name[MODULE_NAME_LEN]; int ret, forced = 0; if (!capable(CAP_SYS_MODULE) || modules_disabled) return -EPERM; if (strncpy_from_user(name, name_user, MODULE_NAME_LEN-1) < 0) return -EFAULT; name[MODULE_NAME_LEN-1] = '\0'; if (mutex_lock_interruptible(&module_mutex) != 0) return -EINTR; mod = find_module(name); if (!mod) { ret = -ENOENT; goto out; } if (!list_empty(&mod->source_list)) { /* Other modules depend on us: get rid of them first. */ ret = -EWOULDBLOCK; goto out; } /* Doing init or already dying? */ if (mod->state != MODULE_STATE_LIVE) { /* FIXME: if (force), slam module count and wake up waiter --RR */ pr_debug("%s already dying\n", mod->name); ret = -EBUSY; goto out; } /* If it has an init func, it must have an exit func to unload */ if (mod->init && !mod->exit) { forced = try_force_unload(flags); if (!forced) { /* This module can't be removed */ ret = -EBUSY; goto out; } } /* Set this up before setting mod->state */ mod->waiter = current; /* Stop the machine so refcounts can't move and disable module. */ ret = try_stop_module(mod, flags, &forced); if (ret != 0) goto out; /* Never wait if forced. */ if (!forced && module_refcount(mod) != 0) wait_for_zero_refcount(mod); mutex_unlock(&module_mutex); /* Final destruction now no one is using it. */ if (mod->exit != NULL) mod->exit(); blocking_notifier_call_chain(&module_notify_list, MODULE_STATE_GOING, mod); async_synchronize_full(); /* Store the name of the last unloaded module for diagnostic purposes */ strlcpy(last_unloaded_module, mod->name, sizeof(last_unloaded_module)); free_module(mod); return 0;out: mutex_unlock(&module_mutex); return ret;} static inline void print_unload_info(struct seq_file *m, struct module *mod){ struct module_use *use; int printed_something = 0; seq_printf(m, " %lu ", module_refcount(mod)); /* Always include a trailing , so userspace can differentiate between this and the old multi-field proc format. */ list_for_each_entry(use, &mod->source_list, source_list) { printed_something = 1; seq_printf(m, "%s,", use->source->name); } if (mod->init != NULL && mod->exit == NULL) { printed_something = 1; seq_printf(m, "[permanent],"); } if (!printed_something) seq_printf(m, "-");} void __symbol_put(const char *symbol){ struct module *owner; preempt_disable(); if (!find_symbol(symbol, &owner, NULL, true, false)) BUG(); module_put(owner); preempt_enable();}EXPORT_SYMBOL(__symbol_put); /* Note this assumes addr is a function, which it currently always is. */void symbol_put_addr(void *addr){ struct module *modaddr; unsigned long a = (unsigned long)dereference_function_descriptor(addr); if (core_kernel_text(a)) return; /* module_text_address is safe here: we're supposed to have reference * to module from symbol_get, so it can't go away. */ modaddr = __module_text_address(a); BUG_ON(!modaddr); module_put(modaddr);}EXPORT_SYMBOL_GPL(symbol_put_addr); static ssize_t show_refcnt(struct module_attribute *mattr, struct module_kobject *mk, char *buffer){ return sprintf(buffer, "%lu\n", module_refcount(mk->mod));} static struct module_attribute modinfo_refcnt = __ATTR(refcnt, 0444, show_refcnt, NULL); void __module_get(struct module *module){ if (module) { preempt_disable(); __this_cpu_inc(module->refptr->incs); trace_module_get(module, _RET_IP_); preempt_enable(); }}EXPORT_SYMBOL(__module_get); bool try_module_get(struct module *module){ bool ret = true; if (module) { preempt_disable(); if (likely(module_is_live(module))) { __this_cpu_inc(module->refptr->incs); trace_module_get(module, _RET_IP_); } else ret = false; preempt_enable(); } return ret;}EXPORT_SYMBOL(try_module_get); void module_put(struct module *module){ if (module) { preempt_disable(); smp_wmb(); /* see comment in module_refcount */ __this_cpu_inc(module->refptr->decs); trace_module_put(module, _RET_IP_); /* Maybe they're waiting for us to drop reference? */ if (unlikely(!module_is_live(module))) wake_up_process(module->waiter); preempt_enable(); }}EXPORT_SYMBOL(module_put); #else /* !CONFIG_MODULE_UNLOAD */static inline void print_unload_info(struct seq_file *m, struct module *mod){ /* We don't know the usage count, or what modules are using. */ seq_printf(m, " - -");} static inline void module_unload_free(struct module *mod){} int ref_module(struct module *a, struct module *b){ return strong_try_module_get(b);}EXPORT_SYMBOL_GPL(ref_module); static inline int module_unload_init(struct module *mod){ return 0;}#endif /* CONFIG_MODULE_UNLOAD */ static size_t module_flags_taint(struct module *mod, char *buf){ size_t l = 0; if (mod->taints & (1 << TAINT_PROPRIETARY_MODULE)) buf[l++] = 'P'; if (mod->taints & (1 << TAINT_OOT_MODULE)) buf[l++] = 'O'; if (mod->taints & (1 << TAINT_FORCED_MODULE)) buf[l++] = 'F'; if (mod->taints & (1 << TAINT_CRAP)) buf[l++] = 'C'; /* * TAINT_FORCED_RMMOD: could be added. * TAINT_UNSAFE_SMP, TAINT_MACHINE_CHECK, TAINT_BAD_PAGE don't * apply to modules. */ return l;} static ssize_t show_initstate(struct module_attribute *mattr, struct module_kobject *mk, char *buffer){ const char *state = "unknown"; Lines 501–1000 of 3539. Previous lines Load more
preempt_enable(); return false; } #else /* ... !CONFIG_SMP */ static inline void __percpu *mod_percpu(struct module *mod) { return NULL; } static inline int percpu_modalloc(struct module *mod, unsigned long size, unsigned long align) { return -ENOMEM; } static inline void percpu_modfree(struct module *mod) { } static unsigned int find_pcpusec(struct load_info *info) { return 0; } static inline void percpu_modcopy(struct module *mod, const void *from, unsigned long size) { /* pcpusec should be 0, and size of that section should be 0. */ BUG_ON(size != 0); } bool is_module_percpu_address(unsigned long addr) { return false; } #endif /* CONFIG_SMP */ #define MODINFO_ATTR(field) \ static void setup_modinfo_##field(struct module *mod, const char *s) \ { \ mod->field = kstrdup(s, GFP_KERNEL); \ } \ static ssize_t show_modinfo_##field(struct module_attribute *mattr, \ struct module_kobject *mk, char *buffer) \ { \ return sprintf(buffer, "%s\n", mk->mod->field); \ } \ static int modinfo_##field##_exists(struct module *mod) \ { \ return mod->field != NULL; \ } \ static void free_modinfo_##field(struct module *mod) \ { \ kfree(mod->field); \ mod->field = NULL; \ } \ static struct module_attribute modinfo_##field = { \ .attr = { .name = __stringify(field), .mode = 0444 }, \ .show = show_modinfo_##field, \ .setup = setup_modinfo_##field, \ .test = modinfo_##field##_exists, \ .free = free_modinfo_##field, \ }; MODINFO_ATTR(version); MODINFO_ATTR(srcversion); static char last_unloaded_module[MODULE_NAME_LEN+1]; #ifdef CONFIG_MODULE_UNLOAD EXPORT_TRACEPOINT_SYMBOL(module_get); /* Init the unload section of the module. */ static int module_unload_init(struct module *mod) { mod->refptr = alloc_percpu(struct module_ref); if (!mod->refptr) return -ENOMEM; INIT_LIST_HEAD(&mod->source_list); INIT_LIST_HEAD(&mod->target_list); /* Hold reference count during initialization. */ __this_cpu_write(mod->refptr->incs, 1); /* Backwards compatibility macros put refcount during init. */ mod->waiter = current; return 0; } /* Does a already use b? */ static int already_uses(struct module *a, struct module *b) { struct module_use *use; list_for_each_entry(use, &b->source_list, source_list) { if (use->source == a) { pr_debug("%s uses %s!\n", a->name, b->name); return 1; } } pr_debug("%s does not use %s!\n", a->name, b->name); return 0; } /* * Module a uses b * - we add 'a' as a "source", 'b' as a "target" of module use * - the module_use is added to the list of 'b' sources (so * 'b' can walk the list to see who sourced them), and of 'a' * targets (so 'a' can see what modules it targets). */ static int add_module_usage(struct module *a, struct module *b) { struct module_use *use; pr_debug("Allocating new usage for %s.\n", a->name); use = kmalloc(sizeof(*use), GFP_ATOMIC); if (!use) { printk(KERN_WARNING "%s: out of memory loading\n", a->name); return -ENOMEM; } use->source = a; use->target = b; list_add(&use->source_list, &b->source_list); list_add(&use->target_list, &a->target_list); return 0; } /* Module a uses b: caller needs module_mutex() */ int ref_module(struct module *a, struct module *b) { int err; if (b == NULL || already_uses(a, b)) return 0; /* If module isn't available, we fail. */ err = strong_try_module_get(b); if (err) return err; err = add_module_usage(a, b); if (err) { module_put(b); return err; } return 0; } EXPORT_SYMBOL_GPL(ref_module); /* Clear the unload stuff of the module. */ static void module_unload_free(struct module *mod) { struct module_use *use, *tmp; mutex_lock(&module_mutex); list_for_each_entry_safe(use, tmp, &mod->target_list, target_list) { struct module *i = use->target; pr_debug("%s unusing %s\n", mod->name, i->name); module_put(i); list_del(&use->source_list); list_del(&use->target_list); kfree(use); } mutex_unlock(&module_mutex); free_percpu(mod->refptr); } #ifdef CONFIG_MODULE_FORCE_UNLOAD static inline int try_force_unload(unsigned int flags) { int ret = (flags & O_TRUNC); if (ret) add_taint(TAINT_FORCED_RMMOD); return ret; } #else static inline int try_force_unload(unsigned int flags) { return 0; } #endif /* CONFIG_MODULE_FORCE_UNLOAD */ struct stopref { struct module *mod; int flags; int *forced; }; /* Whole machine is stopped with interrupts off when this runs. */ static int __try_stop_module(void *_sref) { struct stopref *sref = _sref; /* If it's not unused, quit unless we're forcing. */ if (module_refcount(sref->mod) != 0) { if (!(*sref->forced = try_force_unload(sref->flags))) return -EWOULDBLOCK; } /* Mark it as dying. */ sref->mod->state = MODULE_STATE_GOING; return 0; } static int try_stop_module(struct module *mod, int flags, int *forced) { if (flags & O_NONBLOCK) { struct stopref sref = { mod, flags, forced }; return stop_machine(__try_stop_module, &sref, NULL); } else { /* We don't need to stop the machine for this. */ mod->state = MODULE_STATE_GOING; synchronize_sched(); return 0; } } unsigned long module_refcount(struct module *mod) { unsigned long incs = 0, decs = 0; int cpu; for_each_possible_cpu(cpu) decs += per_cpu_ptr(mod->refptr, cpu)->decs; /* * ensure the incs are added up after the decs. * module_put ensures incs are visible before decs with smp_wmb. * * This 2-count scheme avoids the situation where the refcount * for CPU0 is read, then CPU0 increments the module refcount, * then CPU1 drops that refcount, then the refcount for CPU1 is * read. We would record a decrement but not its corresponding * increment so we would see a low count (disaster). * * Rare situation? But module_refcount can be preempted, and we * might be tallying up 4096+ CPUs. So it is not impossible. */ smp_rmb(); for_each_possible_cpu(cpu) incs += per_cpu_ptr(mod->refptr, cpu)->incs; return incs - decs; } EXPORT_SYMBOL(module_refcount); /* This exists whether we can unload or not */ static void free_module(struct module *mod); static void wait_for_zero_refcount(struct module *mod) { /* Since we might sleep for some time, release the mutex first */ mutex_unlock(&module_mutex); for (;;) { pr_debug("Looking at refcount...\n"); set_current_state(TASK_UNINTERRUPTIBLE); if (module_refcount(mod) == 0) break; schedule(); } current->state = TASK_RUNNING; mutex_lock(&module_mutex); } SYSCALL_DEFINE2(delete_module, const char __user *, name_user, unsigned int, flags) { struct module *mod; char name[MODULE_NAME_LEN]; int ret, forced = 0; if (!capable(CAP_SYS_MODULE) || modules_disabled) return -EPERM; if (strncpy_from_user(name, name_user, MODULE_NAME_LEN-1) < 0) return -EFAULT; name[MODULE_NAME_LEN-1] = '\0'; if (mutex_lock_interruptible(&module_mutex) != 0) return -EINTR; mod = find_module(name); if (!mod) { ret = -ENOENT; goto out; } if (!list_empty(&mod->source_list)) { /* Other modules depend on us: get rid of them first. */ ret = -EWOULDBLOCK; goto out; } /* Doing init or already dying? */ if (mod->state != MODULE_STATE_LIVE) { /* FIXME: if (force), slam module count and wake up waiter --RR */ pr_debug("%s already dying\n", mod->name); ret = -EBUSY; goto out; } /* If it has an init func, it must have an exit func to unload */ if (mod->init && !mod->exit) { forced = try_force_unload(flags); if (!forced) { /* This module can't be removed */ ret = -EBUSY; goto out; } } /* Set this up before setting mod->state */ mod->waiter = current; /* Stop the machine so refcounts can't move and disable module. */ ret = try_stop_module(mod, flags, &forced); if (ret != 0) goto out; /* Never wait if forced. */ if (!forced && module_refcount(mod) != 0) wait_for_zero_refcount(mod); mutex_unlock(&module_mutex); /* Final destruction now no one is using it. */ if (mod->exit != NULL) mod->exit(); blocking_notifier_call_chain(&module_notify_list, MODULE_STATE_GOING, mod); async_synchronize_full(); /* Store the name of the last unloaded module for diagnostic purposes */ strlcpy(last_unloaded_module, mod->name, sizeof(last_unloaded_module)); free_module(mod); return 0; out: mutex_unlock(&module_mutex); return ret; } static inline void print_unload_info(struct seq_file *m, struct module *mod) { struct module_use *use; int printed_something = 0; seq_printf(m, " %lu ", module_refcount(mod)); /* Always include a trailing , so userspace can differentiate between this and the old multi-field proc format. */ list_for_each_entry(use, &mod->source_list, source_list) { printed_something = 1; seq_printf(m, "%s,", use->source->name); } if (mod->init != NULL && mod->exit == NULL) { printed_something = 1; seq_printf(m, "[permanent],"); } if (!printed_something) seq_printf(m, "-"); } void __symbol_put(const char *symbol) { struct module *owner; preempt_disable(); if (!find_symbol(symbol, &owner, NULL, true, false)) BUG(); module_put(owner); preempt_enable(); } EXPORT_SYMBOL(__symbol_put); /* Note this assumes addr is a function, which it currently always is. */ void symbol_put_addr(void *addr) { struct module *modaddr; unsigned long a = (unsigned long)dereference_function_descriptor(addr); if (core_kernel_text(a)) return; /* module_text_address is safe here: we're supposed to have reference * to module from symbol_get, so it can't go away. */ modaddr = __module_text_address(a); BUG_ON(!modaddr); module_put(modaddr); } EXPORT_SYMBOL_GPL(symbol_put_addr); static ssize_t show_refcnt(struct module_attribute *mattr, struct module_kobject *mk, char *buffer) { return sprintf(buffer, "%lu\n", module_refcount(mk->mod)); } static struct module_attribute modinfo_refcnt = __ATTR(refcnt, 0444, show_refcnt, NULL); void __module_get(struct module *module) { if (module) { preempt_disable(); __this_cpu_inc(module->refptr->incs); trace_module_get(module, _RET_IP_); preempt_enable(); } } EXPORT_SYMBOL(__module_get); bool try_module_get(struct module *module) { bool ret = true; if (module) { preempt_disable(); if (likely(module_is_live(module))) { __this_cpu_inc(module->refptr->incs); trace_module_get(module, _RET_IP_); } else ret = false; preempt_enable(); } return ret; } EXPORT_SYMBOL(try_module_get); void module_put(struct module *module) { if (module) { preempt_disable(); smp_wmb(); /* see comment in module_refcount */ __this_cpu_inc(module->refptr->decs); trace_module_put(module, _RET_IP_); /* Maybe they're waiting for us to drop reference? */ if (unlikely(!module_is_live(module))) wake_up_process(module->waiter); preempt_enable(); } } EXPORT_SYMBOL(module_put); #else /* !CONFIG_MODULE_UNLOAD */ static inline void print_unload_info(struct seq_file *m, struct module *mod) { /* We don't know the usage count, or what modules are using. */ seq_printf(m, " - -"); } static inline void module_unload_free(struct module *mod) { } int ref_module(struct module *a, struct module *b) { return strong_try_module_get(b); } EXPORT_SYMBOL_GPL(ref_module); static inline int module_unload_init(struct module *mod) { return 0; } #endif /* CONFIG_MODULE_UNLOAD */ static size_t module_flags_taint(struct module *mod, char *buf) { size_t l = 0; if (mod->taints & (1 << TAINT_PROPRIETARY_MODULE)) buf[l++] = 'P'; if (mod->taints & (1 << TAINT_OOT_MODULE)) buf[l++] = 'O'; if (mod->taints & (1 << TAINT_FORCED_MODULE)) buf[l++] = 'F'; if (mod->taints & (1 << TAINT_CRAP)) buf[l++] = 'C'; /* * TAINT_FORCED_RMMOD: could be added. * TAINT_UNSAFE_SMP, TAINT_MACHINE_CHECK, TAINT_BAD_PAGE don't * apply to modules. */ return l; } static ssize_t show_initstate(struct module_attribute *mattr, struct module_kobject *mk, char *buffer) { const char *state = "unknown";