Skip to content

Commit 48fd118

Browse files
cernekeerustyrussell
authored andcommitted
module: Fix performance regression on modules with large symbol tables
Looking at /proc/kallsyms, one starts to ponder whether all of the extra strtab-related complexity in module.c is worth the memory savings. Instead of making the add_kallsyms() loop even more complex, I tried the other route of deleting the strmap logic and naively copying each string into core_strtab with no consideration for consolidating duplicates. Performance on an "already exists" insmod of nvidia.ko (runs add_kallsyms() but does not actually initialize the module): Original scheme: 1.230s With naive copying: 0.058s Extra space used: 35k (of a 408k module). Signed-off-by: Kevin Cernekee <cernekee@gmail.com> Signed-off-by: Rusty Russell <rusty@rustcorp.com.au> LKML-Reference: <73defb5e4bca04a6431392cc341112b1@localhost>
1 parent 70b1e91 commit 48fd118

1 file changed

Lines changed: 21 additions & 44 deletions

File tree

‎kernel/module.c‎

Lines changed: 21 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,6 @@ struct load_info {
138138
unsigned long len;
139139
Elf_Shdr *sechdrs;
140140
char *secstrings, *strtab;
141-
unsigned long *strmap;
142141
unsigned long symoffs, stroffs;
143142
struct _ddebug *debug;
144143
unsigned int num_debug;
@@ -2178,12 +2177,19 @@ static bool is_core_symbol(const Elf_Sym *src, const Elf_Shdr *sechdrs,
21782177
return true;
21792178
}
21802179

2180+
/*
2181+
* We only allocate and copy the strings needed by the parts of symtab
2182+
* we keep. This is simple, but has the effect of making multiple
2183+
* copies of duplicates. We could be more sophisticated, see
2184+
* linux-kernel thread starting with
2185+
* <73defb5e4bca04a6431392cc341112b1@localhost>.
2186+
*/
21812187
static void layout_symtab(struct module *mod, struct load_info *info)
21822188
{
21832189
Elf_Shdr *symsect = info->sechdrs + info->index.sym;
21842190
Elf_Shdr *strsect = info->sechdrs + info->index.str;
21852191
const Elf_Sym *src;
2186-
unsigned int i, nsrc, ndst;
2192+
unsigned int i, nsrc, ndst, strtab_size;
21872193

21882194
/* Put symbol section at end of init part of module. */
21892195
symsect->sh_flags |= SHF_ALLOC;
@@ -2194,38 +2200,23 @@ static void layout_symtab(struct module *mod, struct load_info *info)
21942200
src = (void *)info->hdr + symsect->sh_offset;
21952201
nsrc = symsect->sh_size / sizeof(*src);
21962202

2197-
/*
2198-
* info->strmap has a '1' bit for each byte of .strtab we want to
2199-
* keep resident in mod->core_strtab. Everything else in .strtab
2200-
* is unreferenced by the symbols in mod->core_symtab, and will be
2201-
* discarded when add_kallsyms() compacts the string table.
2202-
*/
2203-
for (ndst = i = 1; i < nsrc; ++i, ++src)
2203+
/* Compute total space required for the core symbols' strtab. */
2204+
for (ndst = i = strtab_size = 1; i < nsrc; ++i, ++src)
22042205
if (is_core_symbol(src, info->sechdrs, info->hdr->e_shnum)) {
2205-
unsigned int j = src->st_name;
2206-
2207-
while (!__test_and_set_bit(j, info->strmap)
2208-
&& info->strtab[j])
2209-
++j;
2210-
++ndst;
2206+
strtab_size += strlen(&info->strtab[src->st_name]) + 1;
2207+
ndst++;
22112208
}
22122209

22132210
/* Append room for core symbols at end of core part. */
22142211
info->symoffs = ALIGN(mod->core_size, symsect->sh_addralign ?: 1);
2215-
mod->core_size = info->symoffs + ndst * sizeof(Elf_Sym);
2212+
info->stroffs = mod->core_size = info->symoffs + ndst * sizeof(Elf_Sym);
2213+
mod->core_size += strtab_size;
22162214

22172215
/* Put string table section at end of init part of module. */
22182216
strsect->sh_flags |= SHF_ALLOC;
22192217
strsect->sh_entsize = get_offset(mod, &mod->init_size, strsect,
22202218
info->index.str) | INIT_OFFSET_MASK;
22212219
DEBUGP("\t%s\n", info->secstrings + strsect->sh_name);
2222-
2223-
/* Append room for core symbols' strings at end of core part. */
2224-
info->stroffs = mod->core_size;
2225-
2226-
/* First strtab byte (and first symtab entry) are zeroes. */
2227-
__set_bit(0, info->strmap);
2228-
mod->core_size += bitmap_weight(info->strmap, strsect->sh_size);
22292220
}
22302221

22312222
static void add_kallsyms(struct module *mod, const struct load_info *info)
@@ -2246,22 +2237,19 @@ static void add_kallsyms(struct module *mod, const struct load_info *info)
22462237
mod->symtab[i].st_info = elf_type(&mod->symtab[i], info);
22472238

22482239
mod->core_symtab = dst = mod->module_core + info->symoffs;
2240+
mod->core_strtab = s = mod->module_core + info->stroffs;
22492241
src = mod->symtab;
22502242
*dst = *src;
2243+
*s++ = 0;
22512244
for (ndst = i = 1; i < mod->num_symtab; ++i, ++src) {
22522245
if (!is_core_symbol(src, info->sechdrs, info->hdr->e_shnum))
22532246
continue;
2247+
22542248
dst[ndst] = *src;
2255-
dst[ndst].st_name = bitmap_weight(info->strmap,
2256-
dst[ndst].st_name);
2257-
++ndst;
2249+
dst[ndst++].st_name = s - mod->core_strtab;
2250+
s += strlcpy(s, &mod->strtab[src->st_name], KSYM_NAME_LEN) + 1;
22582251
}
22592252
mod->core_num_syms = ndst;
2260-
2261-
mod->core_strtab = s = mod->module_core + info->stroffs;
2262-
for (*s = 0, i = 1; i < info->sechdrs[info->index.str].sh_size; ++i)
2263-
if (test_bit(i, info->strmap))
2264-
*++s = mod->strtab[i];
22652253
}
22662254
#else
22672255
static inline void layout_symtab(struct module *mod, struct load_info *info)
@@ -2751,27 +2739,18 @@ static struct module *layout_and_allocate(struct load_info *info)
27512739
this is done generically; there doesn't appear to be any
27522740
special cases for the architectures. */
27532741
layout_sections(mod, info);
2754-
2755-
info->strmap = kzalloc(BITS_TO_LONGS(info->sechdrs[info->index.str].sh_size)
2756-
* sizeof(long), GFP_KERNEL);
2757-
if (!info->strmap) {
2758-
err = -ENOMEM;
2759-
goto free_percpu;
2760-
}
27612742
layout_symtab(mod, info);
27622743

27632744
/* Allocate and move to the final place */
27642745
err = move_module(mod, info);
27652746
if (err)
2766-
goto free_strmap;
2747+
goto free_percpu;
27672748

27682749
/* Module has been copied to its final place now: return it. */
27692750
mod = (void *)info->sechdrs[info->index.mod].sh_addr;
27702751
kmemleak_load_module(mod, info);
27712752
return mod;
27722753

2773-
free_strmap:
2774-
kfree(info->strmap);
27752754
free_percpu:
27762755
percpu_modfree(mod);
27772756
out:
@@ -2781,7 +2760,6 @@ static struct module *layout_and_allocate(struct load_info *info)
27812760
/* mod is no longer valid after this! */
27822761
static void module_deallocate(struct module *mod, struct load_info *info)
27832762
{
2784-
kfree(info->strmap);
27852763
percpu_modfree(mod);
27862764
module_free(mod, mod->module_init);
27872765
module_free(mod, mod->module_core);
@@ -2911,8 +2889,7 @@ static struct module *load_module(void __user *umod,
29112889
if (err < 0)
29122890
goto unlink;
29132891

2914-
/* Get rid of temporary copy and strmap. */
2915-
kfree(info.strmap);
2892+
/* Get rid of temporary copy. */
29162893
free_copy(&info);
29172894

29182895
/* Done! */

0 commit comments

Comments
 (0)