Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

Merge tag 'modules-for-v4.13' of git://git.kernel.org/pub/scm/linux/kernel/git/jeyu/linux

Pull modules updates from Jessica Yu:
"Summary of modules changes for the 4.13 merge window:

- Minor code cleanups

- Avoid accessing mod struct prior to checking module struct version,
from Kees

- Fix racy atomic inc/dec logic of kmod_concurrent_max in kmod, from
Luis"

* tag 'modules-for-v4.13' of git://git.kernel.org/pub/scm/linux/kernel/git/jeyu/linux:
module: make the modinfo name const
kmod: reduce atomic operations on kmod_concurrent and simplify
module: use list_for_each_entry_rcu() on find_module_all()
kernel/module.c: suppress warning about unused nowarn variable
module: Add module name to modinfo
module: Pass struct load_info into symbol checks

+80 -60
+18 -22
kernel/kmod.c
··· 45 45 46 46 #include <trace/events/module.h> 47 47 48 - extern int max_threads; 49 - 50 48 #define CAP_BSET (void *)1 51 49 #define CAP_PI (void *)2 52 50 ··· 54 56 static DECLARE_RWSEM(umhelper_sem); 55 57 56 58 #ifdef CONFIG_MODULES 59 + /* 60 + * Assuming: 61 + * 62 + * threads = div64_u64((u64) totalram_pages * (u64) PAGE_SIZE, 63 + * (u64) THREAD_SIZE * 8UL); 64 + * 65 + * If you need less than 50 threads would mean we're dealing with systems 66 + * smaller than 3200 pages. This assuems you are capable of having ~13M memory, 67 + * and this would only be an be an upper limit, after which the OOM killer 68 + * would take effect. Systems like these are very unlikely if modules are 69 + * enabled. 70 + */ 71 + #define MAX_KMOD_CONCURRENT 50 72 + static atomic_t kmod_concurrent_max = ATOMIC_INIT(MAX_KMOD_CONCURRENT); 57 73 58 74 /* 59 75 modprobe_path is set via /proc/sys. ··· 139 127 { 140 128 va_list args; 141 129 char module_name[MODULE_NAME_LEN]; 142 - unsigned int max_modprobes; 143 130 int ret; 144 - static atomic_t kmod_concurrent = ATOMIC_INIT(0); 145 - #define MAX_KMOD_CONCURRENT 50 /* Completely arbitrary value - KAO */ 146 131 static int kmod_loop_msg; 147 132 148 133 /* ··· 163 154 if (ret) 164 155 return ret; 165 156 166 - /* If modprobe needs a service that is in a module, we get a recursive 167 - * loop. Limit the number of running kmod threads to max_threads/2 or 168 - * MAX_KMOD_CONCURRENT, whichever is the smaller. A cleaner method 169 - * would be to run the parents of this process, counting how many times 170 - * kmod was invoked. That would mean accessing the internals of the 171 - * process tables to get the command line, proc_pid_cmdline is static 172 - * and it is not worth changing the proc code just to handle this case. 173 - * KAO. 174 - * 175 - * "trace the ppid" is simple, but will fail if someone's 176 - * parent exits. I think this is as good as it gets. --RR 177 - */ 178 - max_modprobes = min(max_threads/2, MAX_KMOD_CONCURRENT); 179 - atomic_inc(&kmod_concurrent); 180 - if (atomic_read(&kmod_concurrent) > max_modprobes) { 157 + if (atomic_dec_if_positive(&kmod_concurrent_max) < 0) { 181 158 /* We may be blaming an innocent here, but unlikely */ 182 159 if (kmod_loop_msg < 5) { 183 160 printk(KERN_ERR ··· 171 176 module_name); 172 177 kmod_loop_msg++; 173 178 } 174 - atomic_dec(&kmod_concurrent); 175 179 return -ENOMEM; 176 180 } 177 181 ··· 178 184 179 185 ret = call_modprobe(module_name, wait ? UMH_WAIT_PROC : UMH_WAIT_EXEC); 180 186 181 - atomic_dec(&kmod_concurrent); 187 + atomic_inc(&kmod_concurrent_max); 188 + 182 189 return ret; 183 190 } 184 191 EXPORT_SYMBOL(__request_module); 192 + 185 193 #endif /* CONFIG_MODULES */ 186 194 187 195 static void call_usermodehelper_freeinfo(struct subprocess_info *info)
+61 -38
kernel/module.c
··· 300 300 EXPORT_SYMBOL(unregister_module_notifier); 301 301 302 302 struct load_info { 303 + const char *name; 303 304 Elf_Ehdr *hdr; 304 305 unsigned long len; 305 306 Elf_Shdr *sechdrs; ··· 601 600 602 601 module_assert_mutex_or_preempt(); 603 602 604 - list_for_each_entry(mod, &modules, list) { 603 + list_for_each_entry_rcu(mod, &modules, list) { 605 604 if (!even_unformed && mod->state == MODULE_STATE_UNFORMED) 606 605 continue; 607 606 if (strlen(mod->name) == len && !memcmp(mod->name, name, len)) ··· 1274 1273 return *(u32 *)((void *)crc + *crc); 1275 1274 } 1276 1275 1277 - static int check_version(Elf_Shdr *sechdrs, 1278 - unsigned int versindex, 1276 + static int check_version(const struct load_info *info, 1279 1277 const char *symname, 1280 1278 struct module *mod, 1281 1279 const s32 *crc) 1282 1280 { 1281 + Elf_Shdr *sechdrs = info->sechdrs; 1282 + unsigned int versindex = info->index.vers; 1283 1283 unsigned int i, num_versions; 1284 1284 struct modversion_info *versions; 1285 1285 ··· 1314 1312 } 1315 1313 1316 1314 /* Broken toolchain. Warn once, then let it go.. */ 1317 - pr_warn_once("%s: no symbol version for %s\n", mod->name, symname); 1315 + pr_warn_once("%s: no symbol version for %s\n", info->name, symname); 1318 1316 return 1; 1319 1317 1320 1318 bad_version: 1321 1319 pr_warn("%s: disagrees about version of symbol %s\n", 1322 - mod->name, symname); 1320 + info->name, symname); 1323 1321 return 0; 1324 1322 } 1325 1323 1326 - static inline int check_modstruct_version(Elf_Shdr *sechdrs, 1327 - unsigned int versindex, 1324 + static inline int check_modstruct_version(const struct load_info *info, 1328 1325 struct module *mod) 1329 1326 { 1330 1327 const s32 *crc; ··· 1339 1338 BUG(); 1340 1339 } 1341 1340 preempt_enable(); 1342 - return check_version(sechdrs, versindex, 1343 - VMLINUX_SYMBOL_STR(module_layout), mod, crc); 1341 + return check_version(info, VMLINUX_SYMBOL_STR(module_layout), 1342 + mod, crc); 1344 1343 } 1345 1344 1346 1345 /* First part is kernel version, which we ignore if module has crcs. */ ··· 1354 1353 return strcmp(amagic, bmagic) == 0; 1355 1354 } 1356 1355 #else 1357 - static inline int check_version(Elf_Shdr *sechdrs, 1358 - unsigned int versindex, 1356 + static inline int check_version(const struct load_info *info, 1359 1357 const char *symname, 1360 1358 struct module *mod, 1361 1359 const s32 *crc) ··· 1362 1362 return 1; 1363 1363 } 1364 1364 1365 - static inline int check_modstruct_version(Elf_Shdr *sechdrs, 1366 - unsigned int versindex, 1365 + static inline int check_modstruct_version(const struct load_info *info, 1367 1366 struct module *mod) 1368 1367 { 1369 1368 return 1; ··· 1398 1399 if (!sym) 1399 1400 goto unlock; 1400 1401 1401 - if (!check_version(info->sechdrs, info->index.vers, name, mod, crc)) { 1402 + if (!check_version(info, name, mod, crc)) { 1402 1403 sym = ERR_PTR(-EINVAL); 1403 1404 goto getname; 1404 1405 } ··· 1661 1662 } 1662 1663 #endif /* CONFIG_KALLSYMS */ 1663 1664 1664 - static void add_usage_links(struct module *mod) 1665 - { 1666 - #ifdef CONFIG_MODULE_UNLOAD 1667 - struct module_use *use; 1668 - int nowarn; 1669 - 1670 - mutex_lock(&module_mutex); 1671 - list_for_each_entry(use, &mod->target_list, target_list) { 1672 - nowarn = sysfs_create_link(use->target->holders_dir, 1673 - &mod->mkobj.kobj, mod->name); 1674 - } 1675 - mutex_unlock(&module_mutex); 1676 - #endif 1677 - } 1678 - 1679 1665 static void del_usage_links(struct module *mod) 1680 1666 { 1681 1667 #ifdef CONFIG_MODULE_UNLOAD ··· 1671 1687 sysfs_remove_link(use->target->holders_dir, mod->name); 1672 1688 mutex_unlock(&module_mutex); 1673 1689 #endif 1690 + } 1691 + 1692 + static int add_usage_links(struct module *mod) 1693 + { 1694 + int ret = 0; 1695 + #ifdef CONFIG_MODULE_UNLOAD 1696 + struct module_use *use; 1697 + 1698 + mutex_lock(&module_mutex); 1699 + list_for_each_entry(use, &mod->target_list, target_list) { 1700 + ret = sysfs_create_link(use->target->holders_dir, 1701 + &mod->mkobj.kobj, mod->name); 1702 + if (ret) 1703 + break; 1704 + } 1705 + mutex_unlock(&module_mutex); 1706 + if (ret) 1707 + del_usage_links(mod); 1708 + #endif 1709 + return ret; 1674 1710 } 1675 1711 1676 1712 static int module_add_modinfo_attrs(struct module *mod) ··· 1801 1797 if (err) 1802 1798 goto out_unreg_param; 1803 1799 1804 - add_usage_links(mod); 1800 + err = add_usage_links(mod); 1801 + if (err) 1802 + goto out_unreg_modinfo_attrs; 1803 + 1805 1804 add_sect_attrs(mod, info); 1806 1805 add_notes_attrs(mod, info); 1807 1806 1808 1807 kobject_uevent(&mod->mkobj.kobj, KOBJ_ADD); 1809 1808 return 0; 1810 1809 1810 + out_unreg_modinfo_attrs: 1811 + module_remove_modinfo_attrs(mod); 1811 1812 out_unreg_param: 1812 1813 module_param_sysfs_remove(mod); 1813 1814 out_unreg_holders: ··· 2919 2910 info->index.vers = 0; /* Pretend no __versions section! */ 2920 2911 else 2921 2912 info->index.vers = find_sec(info, "__versions"); 2922 - info->index.info = find_sec(info, ".modinfo"); 2923 - info->sechdrs[info->index.info].sh_flags &= ~(unsigned long)SHF_ALLOC; 2924 2913 info->sechdrs[info->index.vers].sh_flags &= ~(unsigned long)SHF_ALLOC; 2914 + 2915 + info->index.info = find_sec(info, ".modinfo"); 2916 + if (!info->index.info) 2917 + info->name = "(missing .modinfo section)"; 2918 + else 2919 + info->name = get_modinfo(info, "name"); 2920 + info->sechdrs[info->index.info].sh_flags &= ~(unsigned long)SHF_ALLOC; 2921 + 2925 2922 return 0; 2926 2923 } 2927 2924 ··· 2967 2952 2968 2953 info->index.mod = find_sec(info, ".gnu.linkonce.this_module"); 2969 2954 if (!info->index.mod) { 2970 - pr_warn("No module found in object\n"); 2955 + pr_warn("%s: No module found in object\n", 2956 + info->name ?: "(missing .modinfo name field)"); 2971 2957 return ERR_PTR(-ENOEXEC); 2972 2958 } 2973 2959 /* This is temporary: point mod into copy of data. */ 2974 2960 mod = (void *)info->sechdrs[info->index.mod].sh_addr; 2975 2961 2962 + /* 2963 + * If we didn't load the .modinfo 'name' field, fall back to 2964 + * on-disk struct mod 'name' field. 2965 + */ 2966 + if (!info->name) 2967 + info->name = mod->name; 2968 + 2976 2969 if (info->index.sym == 0) { 2977 - pr_warn("%s: module has no symbols (stripped?)\n", mod->name); 2970 + pr_warn("%s: module has no symbols (stripped?)\n", info->name); 2978 2971 return ERR_PTR(-ENOEXEC); 2979 2972 } 2980 2973 2981 2974 info->index.pcpu = find_pcpusec(info); 2982 2975 2983 2976 /* Check module struct version now, before we try to use module. */ 2984 - if (!check_modstruct_version(info->sechdrs, info->index.vers, mod)) 2977 + if (!check_modstruct_version(info, mod)) 2985 2978 return ERR_PTR(-ENOEXEC); 2986 2979 2987 2980 return mod; ··· 3010 2987 return err; 3011 2988 } else if (!same_magic(modmagic, vermagic, info->index.vers)) { 3012 2989 pr_err("%s: version magic '%s' should be '%s'\n", 3013 - mod->name, modmagic, vermagic); 2990 + info->name, modmagic, vermagic); 3014 2991 return -ENOEXEC; 3015 2992 } 3016 2993 ··· 3260 3237 3261 3238 /* module_blacklist is a comma-separated list of module names */ 3262 3239 static char *module_blacklist; 3263 - static bool blacklisted(char *module_name) 3240 + static bool blacklisted(const char *module_name) 3264 3241 { 3265 3242 const char *p; 3266 3243 size_t len; ··· 3290 3267 if (IS_ERR(mod)) 3291 3268 return mod; 3292 3269 3293 - if (blacklisted(mod->name)) 3270 + if (blacklisted(info->name)) 3294 3271 return ERR_PTR(-EPERM); 3295 3272 3296 3273 err = check_modinfo(mod, info, flags);
+1
scripts/mod/modpost.c
··· 2126 2126 buf_printf(b, "#include <linux/compiler.h>\n"); 2127 2127 buf_printf(b, "\n"); 2128 2128 buf_printf(b, "MODULE_INFO(vermagic, VERMAGIC_STRING);\n"); 2129 + buf_printf(b, "MODULE_INFO(name, KBUILD_MODNAME);\n"); 2129 2130 buf_printf(b, "\n"); 2130 2131 buf_printf(b, "__visible struct module __this_module\n"); 2131 2132 buf_printf(b, "__attribute__((section(\".gnu.linkonce.this_module\"))) = {\n");