Skip to contentSkip to conversation

Rusty's module talk at the Kernel Summit

DISCUSSION Adam J. Richter 3 participants 4 messages Jul 3, 2002

lore
Share & tools
lore
#0 lore raw
Quoted 17 lines · Keith Owens
Quoted 6 lines · Adam J. Richter

As an extereme illustration, imagine a module with 4095 bytes
of non-init data and 2 bytes of init data. With the .init section loaded, the module will occupy two pages. Freeing the .init section will free an entire page, making 4096 bytes available to the system, even though only two bytes were in the .init section.

Agreed, so let's look at some real figures. The tar ball below contains

A patch against kernel 2.5.24 to use init sections for module code and data.

A patch against modutils 2.4.16 to disable error checks. We are not loading the modules, just getting data about their size.

Quoted 6 lines

A Perl script to read the output from the patched insmod and work out what would be saved by discarding init sections.

Two reports from running the script against 2.5.24 with everything that will build as a module. One report is from discarding both code and data.init, the other report is discarding just data.init.

Cool. Out of curiosity, is there some reason you need a
patched version of modutils for extracting this information, rather than reading the output of "objdump --section-headers"?

[...]

Quoted 2 lines

The total saving over all 2.5.24 modules is 4% of the total module sizes, rounded to page boundaries.

As individual space optimizations go, 4% is respectable,
especially for something that has no cost, helps detect bugs and simplifies the kernel. It is hard to think of many potential other space improvements that would as effective, especially as function of implementation effort. In comparison, my vmlinux is 5% init sections. So, if init sections are worth it for the core kernel, they should be worth it for modules.

Quoted 1 line

Most of that saving comes from a few modules.

This makes me wonder if __init procedures are not being
aggressively identified. I wonder if people would use __init a little more if they knew they would get the benefit of it in the module case. Perhaps someday someone will write a tool to identify procedures that are only called from init sections.

Quoted 7 lines

There is a lot of arch dependent work required to adjust the in-module tables to correctly record which code has been discarded. If the tables are not adjusted then we run the risk of applying unwind or exception recovery to the wrong areas.

I don't see that the complexity required to adjust the arch dependent tables is worth the small saving.

I don't follow you. Right now, I don't think one would have
to write any new kernel code to load init sections and the non-init sections as two separate kernel modules, but perhaps I'm probably missing something.

[..]

Quoted 9 lines
Quoted 3 lines

It would also be possible to achieve space savings for modules
with non-init text+data+bss sizes smaller than a page by allocating their space with kmalloc(...,__GFP_HIGHMEM) instead of vmalloc.

That requires kfree() but kfree does not unmap the area. Any buggy code that accesses the module after rmmod (which is the main problem with module unload) will not be detected. vfree unmaps the entire module on removal. An oops to detect buggy code is better that a silent data corruption.

I do not believe that there is any guarantee that a subsequent
vmalloc() will not remap the same virtual addresses, and I do not believe that there is any guarantee that a kfree'd area will remain mapped. So, in both cases, there are no guarantees.

Kernel modules have been a way of life for me for years, and I
don't think I've ever caught a kernel bug by the mechanism that you describe. However, I see no harm in having a debugging option that always vmalloc'ed kernel modules. This faciilty could be entirely configuarable from user level by having insmod allocate a module of *exactly* one page for modules that were less than a page (since you would only want to kmalloc modules that were *less* than one page).

Quoted 13 lines
Quoted 4 lines

Here is what I have in mind. I believe that removal of .init
sections could be implemented entirely in user land (aside from removing the include/inux/init.h code that disables init sections for modules). Insmod would allocate two kernel modules, one for the init

Quoted 8 lines

sections and the other for the regular sections. Insmod would resolve references between the two sections. The temporary module for the init sections would be loaded first, with no initialization routine. The module with the real data would be loaded second, and would run the initialization routine (even if the initialization routine were in the temporary init module). When the initialization routine completed, regardless of sucess or failure, the temporary init module would be unloaded.

Quoted 5 lines

I looked at that several years ago and discarded the idea. There may be references from the init code/data to the main code/data. Those references cannot be resolved until the second module has known addresses, which requires insmod to keep track of two modules at once before either can be loaded.

I do not understand how this is problem. As far as I know,
there is nothing preventing one from doing two create_module calls followed by two init_module calls, so there should be no problem allocating the kernel modules. The init module would be loaded first, and would not run any initiailzation routine. So, both modules would be in kernel memory before any code was run.

Quoted 3 lines

It also requires insmod to split the tables that refer to the init code. For example, insmod would have to separate __ex_table and .modinfo data according to which sub-module each entry referred to.

As I understand it, __ex_table is just for copy_{to,from}_uesr,
which would almost never be done from init sections, so it can go in the non-init section. The core kernel already deals with the same issue.

The .modinfo section is not something that would be loaded
into kernel memory. The MODLE_PARM entries may refer to locations in either kernel module, but I don't see how that is a problem.

Quoted 2 lines

All things considered, loading as two modules is too much modutils work and maintenance for too little gain.

Obviously it's not for me to tell you to write software for
me. I just hope you'll accept a good patch if someone develops one.

Adam J. Richter     __     ______________   575 Oroville Road
[email protected]     \ /                  Milpitas, California 95035
+1 408 309-6081         | g g d r a s i l   United States of America

"Free Software For The Rest Of Us."

#1 lore raw
Quoted 10 lines · Adam J. Richter
Quoted 2 lines

The total saving over all 2.5.24 modules is 4% of the total module sizes, rounded to page boundaries.

As individual space optimizations go, 4% is respectable,
especially for something that has no cost, helps detect bugs and simplifies the kernel. It is hard to think of many potential other space improvements that would as effective, especially as function of implementation effort. In comparison, my vmlinux is 5% init sections. So, if init sections are worth it for the core kernel, they should be worth it for modules.

Ehmmm. You normally load one big 1Mb kernel, freeing about 40 or 50k at init time.

You normally load a couple of modules, totalling much less.

Hmm. Just checked on a system with sound as modules, I see half a megabyte of modules. So maybe that 20k is worth it. On the other hand, you only load half a megabyte of shit if you have the RAM to spare. 20k is not worth the time I spend typing this....

Quoted 7 lines
Quoted 1 line

Most of that saving comes from a few modules.

This makes me wonder if __init procedures are not being
aggressively identified. I wonder if people would use __init a little more if they knew they would get the benefit of it in the module case. Perhaps someday someone will write a tool to identify procedures that are only called from init sections.

Sometimes the "error path" will try to reset/reinit the chip. You will not see that happening during a normal usage cycle, but you will get bitten if you remove the init based on an actual call-trace....

Quoted 2 lines

Kernel modules have been a way of life for me for years, and I
don't think I've ever caught a kernel bug by the mechanism that you

This happens often enough "during development" that the bugs get fixed before you get to see them....

Quoted 5 lines

describe. However, I see no harm in having a debugging option that always vmalloc'ed kernel modules. This faciilty could be entirely configuarable from user level by having insmod allocate a module of *exactly* one page for modules that were less than a page (since you would only want to kmalloc modules that were *less* than one page).

As far as I know, kmallocing more than half a page will actually allocate the whole page.

Roger.

Signature · 4 lines
** [email protected] ** http://www.BitWizard.nl/ ** +31-15-2137555 **
*-- BitWizard writes Linux device drivers for any device you may have! --*
* There are old pilots, and there are bold pilots. 
* There are also old, bald pilots. 
#2 lore raw
In reply to Adam J. Richter
Quoted 12 lines · Adam J. Richter
Quoted 10 lines · Keith Owens

Agreed, so let's look at some real figures. The tar ball below contains

A patch against kernel 2.5.24 to use init sections for module code and data.

A patch against modutils 2.4.16 to disable error checks. We are not loading the modules, just getting data about their size.

A Perl script to read the output from the patched insmod and work out what would be saved by discarding init sections.

Quoted 7 lines
Quoted 3 lines

Two reports from running the script against 2.5.24 with everything that will build as a module. One report is from discarding both code and data.init, the other report is discarding just data.init.

Cool. Out of curiosity, is there some reason you need a
patched version of modutils for extracting this information, rather than reading the output of "objdump --section-headers"?

It was easier and more accurate to patch insmod to ignore errors than to replicate all of insmod's processing in another program. Especially when insmod adds data to the module as it is loaded, that data does not appear in objdump -h.

Quoted 5 lines
Quoted 2 lines

The total saving over all 2.5.24 modules is 4% of the total module sizes, rounded to page boundaries.

As individual space optimizations go, 4% is respectable,
especially for something that has no cost

It is not at no cost. Getting 4% requires arch dependent code to handle all the tables that are affected by partial text removal. I can get 2% for nothing by discarding data.init. Discarding text.init is a lot harder.

Quoted 7 lines
Quoted 2 lines

I don't see that the complexity required to adjust the arch dependent tables is worth the small saving.

I don't follow you. Right now, I don't think one would have
to write any new kernel code to load init sections and the non-init sections as two separate kernel modules, but perhaps I'm probably missing something.

The problem is the partial removal of code when there are tables that point to _all_ the code. Partial code removal requires a lot of work to adjust every table that refers to code and correct them. To make it worse, the tables are arch specific. Most architectures have __ex_table, with different formats for each arch. Some have unwind data, always arch dependent format. MIPS has dbe.

Data is not referenced by any of these tables so a partial discard of data is easy, no side effects to worry about.

BTW, this problem exists for removal of __init code from the kernel as well. The only reason it does not bite us for kernel __init is that the freed area is not reused for executable code, it is used for kmalloc so there is no ambiguity caused by the dangling table data. With modules there is a distinct risk that the freed code area would be reused for another module.

Quoted 6 lines
Quoted 5 lines

I looked at that several years ago and discarded the idea. There may be references from the init code/data to the main code/data. Those references cannot be resolved until the second module has known addresses, which requires insmod to keep track of two modules at once before either can be loaded.

Quoted 6 lines

I do not understand how this is problem. As far as I know,
there is nothing preventing one from doing two create_module calls followed by two init_module calls, so there should be no problem allocating the kernel modules. The init module would be loaded first, and would not run any initiailzation routine. So, both modules would be in kernel memory before any code was run.

It makes insmod much more complicated, it has to load two modules in parallel with unresolved references in both directions. I have seen modules with init code that refers to data and rodata (init -> main) and modules with references from rodata to init (main -> init).

Quoted 2 lines

As I understand it, __ex_table is just for copy_{to,from}_uesr,
which would almost never be done from init sections

__ex_table is used for any code that requires recovery. Mainly copy..user but not exclusively.

Quoted 1 line

The core kernel already deals with the same issue.

It does not. There is no code to adjust any tables after discarding kernel __init sections. We rely on the fact that the discarded kernel area is not reused for executable text.

#3 lore raw
Quoted 9 lines · Keith Owens
Quoted 2 lines · Adam J. Richter

As individual space optimizations go, 4% is respectable,
especially for something that has no cost

It is not at no cost. Getting 4% requires arch dependent code to handle all the tables that are affected by partial text removal. I can get 2% for nothing by discarding data.init. Discarding text.init is a lot harder.

ps. That is not 4% of total memory. It is 4% of the memory allocated to modules.

If you loaded every single 2.5.24 module on ix86 they would occupy 23,527,424 bytes.

The additional memory saved by discarding both text.init and data.init, compared to just the data.init, would be a mere 614,400 bytes. If you loaded every single module, which you would not do.

To get that saving requires patches against 17 architectures. Some architectures have multiple tables, each will require a patch.

Did I mention that I don't think the saving is worth the extra code complexity in the kernel?