Linux: hotplug-ng, Tiny Binaries for Autoloading Modules

Submitted by Jeremy
on February 11, 2005 - 10:01am

Greg KH announced his new hotplug based userspace project known as hotplug-ng. He explains, "this collection of code replaces the existing linux-hotplug package with very tiny, compiled executable programs, instead of the existing bash scripts." He goes on to add:

"But why redo this all in .c code? What's wrong with shell scripts? Nothing is wrong with shell scripts, unless you don't want to have an interpreter in your initramfs/initrd and you want to provide /sbin/hotplug and autoload module functionality. Or if you have a huge box that spawns a zillion hotplug events all at once, and you need to be able to handle all of that with the minimum amount of processing time and memory."

Greg noted that he unscientifically noticed a speedup in how long it took for the proper module to be loaded after a usb devices was plugged into his slow laptop. With the old bash based hotplug scripts, this took about 2 seconds. With his new c-based hotplug-ng program, it took less than 1 second. In response to some clarification questions, he replied, "I do realize the whole hotplug process is getting a bit complicated. I hope to write up some good documentation on what all is involved to help clear up some of the confusion that the combination of udev, udevsend, udevd, hal, /etc/hotplug.d/, /etc/dev.d/, and /sbin/hotplug have caused lately."


From: Greg KH [email blocked]
To:  linux-hotplug-devel, [email blocked]
Subject: [ANNOUNCE] hotplug-ng 001 release
Date: 	Thu, 10 Feb 2005 16:40:33 -0800

I'd like to announce, yet-another-hotplug based userspace project:
linux-ng.  This collection of code replaces the existing linux-hotplug
package with very tiny, compiled executable programs, instead of the
existing bash scripts.

It currently provides the following:
	- a /sbin/hotplug multiplexer.  Works identical to the existing
	  bash /sbin/hotplug.
	- autoload programs for usb, scsi, and pci modules.  These
	  programs determine what module needs to be loaded when the
	  kernel emits a hotplug event for these types of devices.  This
	  works just like the existing linux-hotplug scripts, with a few
	  exceptions.

But why redo this all in .c code?  What's wrong with shell scripts?
Nothing is wrong with shell scripts, unless you don't want to have an
interpreter in your initramfs/initrd and you want to provide
/sbin/hotplug and autoload module functionality.  Or if you have a huge
box that spawns a zillion hotplug events all at once, and you need to be
able to handle all of that with the minimum amount of processing time
and memory.

So, how small are these programs?  Take a look:
   text    data     bss     dec     hex filename
   4669      32     124    4825    12d9 hotplug
   5077       8     348    5433    1539 module_pci
   4925       8     412    5345    14e1 module_scsi
   5349       8     348    5705    1649 module_usb

Those are all static binaries, linked with klibc (which is included in
the hotplug-ng package, just like udev.)

This compares to the following bash scripts:
-rwxr-xr-x  1 root root  4412 Feb 10 15:28 /sbin/hotplug
-rw-r--r--  1 root root   702 Sep 24 08:04 /etc/hotplug/blacklist
-rw-r--r--  1 root root  5293 Sep 24 08:04 /etc/hotplug/hotplug.functions
-rwxr-xr-x  1 root root  3739 Sep 24 08:04 /etc/hotplug/pci.agent
-rwxr-xr-x  1 root root  1459 Sep 24 08:04 /etc/hotplug/scsi.agent
-rwxr-xr-x  1 root root 13466 Sep 24 08:04 /etc/hotplug/usb.agent
-rw-r--r--  1 root root 39306 Sep 24 08:04 /etc/hotplug/usb.distmap
-rw-r--r--  1 root root  4364 Sep 24 08:04 /etc/hotplug/usb.handmap
-rw-r--r--  1 root root   189 Sep 24 08:04 /etc/hotplug/usb.usermap

All of which are loaded into memory for each hotplug event (for specific
hotplug events, only that bus type of file is loaded.)

But what about speed?  With a completely unscientific measurement on my
old, slow laptop, it takes about 2 seconds from the time I plug a usb
device into the machine, for the proper module to be loaded.  With the
hotplug-ng program, it takes less than a second.

And for those of you who might remember the old dietHotplug program that
also did the same thing in a tiny amount of space, this project
obsoletes that one.  dietHotplug had to be rebuilt for every kernel that
was used on the system, hotplug-ng uses the ability for modprobe to
determine the module that needs to be loaded based on the module
aliases[1].

The code can be found at:
	kernel.org/pub/linux/utils/kernel/hotplug/hotplug-ng-001.tar.gz
for those who wish to poke around in it.

I still have a few more programs to write to get it up to the same
functionality as the existing hotplug scripts (firmware, ieee1392, etc.)
but those will be done soon.  I'd like to get people's comments on the
idea, and welcome suggestions and even patches :)

hotplug-ng development is done in a BitKeeper repository located at:
	bk://linuxusb.bkbits.net/hotplug-ng

If anyone ever wants a tarball of the current bk tree, just email me.

thanks,

greg k-h

[1] modprobe as it currently works stops loading modules when it finds
an alias that matches.  This does not work for drivers that claim to
support "all devices" and then later on fail on devices that they really
don't support.  For that, all matching drivers need to be loaded for the
system to work properly.  The linux-hotplug scripts handle this
correctly, so if you rely on this functionality, please stick with that
package for now.  I'll be modifying modprobe to add this feature in the
near future.


From: Greg KH [email blocked] Subject: Re: [ANNOUNCE] hotplug-ng 001 release Date: Thu, 10 Feb 2005 16:52:58 -0800 On Thu, Feb 10, 2005 at 04:40:33PM -0800, Greg KH wrote: > I'd like to announce, yet-another-hotplug based userspace project: > linux-ng. Bah. The name is hotplug-ng. Sorry about that... greg k-h
From: Patrick McFarland [email blocked] Subject: Re: [ANNOUNCE] hotplug-ng 001 release Date: Thu, 10 Feb 2005 20:07:23 -0500 Greg KH wrote: > I'd like to announce, yet-another-hotplug based userspace project: > linux-ng. This collection of code replaces the existing linux-hotplug > package with very tiny, compiled executable programs, instead of the > existing bash scripts. > > It currently provides the following: > - a /sbin/hotplug multiplexer. Works identical to the existing > bash /sbin/hotplug. > - autoload programs for usb, scsi, and pci modules. These > programs determine what module needs to be loaded when the > kernel emits a hotplug event for these types of devices. This > works just like the existing linux-hotplug scripts, with a few > exceptions. > > But why redo this all in .c code? What's wrong with shell scripts? > Nothing is wrong with shell scripts, unless you don't want to have an > interpreter in your initramfs/initrd and you want to provide > /sbin/hotplug and autoload module functionality. Or if you have a huge > box that spawns a zillion hotplug events all at once, and you need to be > able to handle all of that with the minimum amount of processing time > and memory. Wow, thats pretty awesome. Just the other day I said, "Why is hotplug written in sh? Isn't that horribly inefficient way of handling something that needs to be done quickly using the least amount of resources possible?" It seems you were reading my mind. Please, continue this project and encourage distros to switch to it (when it exceeds hotplug in functionality and stability). Ubuntu currently is trying to reduce boot time, and I bet something like this would factor in (even a few seconds helps). -- Patrick "Diablo-D3" McFarland || [email blocked] "Computer games don't affect kids; I mean if Pac-Man affected us as kids, we'd all be running around in darkened rooms, munching magic pills and listening to repetitive electronic music." -- Kristian Wilson, Nintendo, Inc, 1989
From: Greg KH [email blocked] Subject: Re: [ANNOUNCE] hotplug-ng 001 release Date: Thu, 10 Feb 2005 17:16:09 -0800 On Thu, Feb 10, 2005 at 08:07:23PM -0500, Patrick McFarland wrote: > > Please, continue this project and encourage distros to switch to it (when > it exceeds hotplug in functionality and stability). Ubuntu currently is > trying to reduce boot time, and I bet something like this would factor in > (even a few seconds helps). Thanks for the kind words. All distros are trying to reduce boot time. I don't think that the module autoload time has been fingered as taking any serious ammount of boot time due to it happening in the background of everything else. But yes, this should help it out (as long as we aren't trading I/O time for CPU time, like it can happen if you don't have a populated modules.alias file...) And yes, I've been in contact with the Ubuntu developer already about using this code, instead of dietHotplug. thanks, greg k-h
From: Bill Nottingham [email blocked] Subject: Re: [ANNOUNCE] hotplug-ng 001 release Date: Thu, 10 Feb 2005 22:18:23 -0500 Greg KH (gregkh@suse.de) said: > I'd like to announce, yet-another-hotplug based userspace project: > linux-ng. This collection of code replaces the existing linux-hotplug > package with very tiny, compiled executable programs, instead of the > existing bash scripts. > > It currently provides the following: > - a /sbin/hotplug multiplexer. Works identical to the existing > bash /sbin/hotplug. How does this interact with current usage of udevsend as the hotplug multiplexer? Bill
From: Greg KH [email blocked] Subject: Re: [ANNOUNCE] hotplug-ng 001 release Date: Thu, 10 Feb 2005 22:46:57 -0800 On Thu, 2005-02-10 at 22:18 -0500, Bill Nottingham wrote: > Greg KH (gregkh@suse.de) said: > > I'd like to announce, yet-another-hotplug based userspace project: > > linux-ng. This collection of code replaces the existing linux-hotplug > > package with very tiny, compiled executable programs, instead of the > > existing bash scripts. > > > > It currently provides the following: > > - a /sbin/hotplug multiplexer. Works identical to the existing > > bash /sbin/hotplug. > > How does this interact with current usage of udevsend as the hotplug > multiplexer? First off, not everyone wants to use udev (I know, horrible thought...) This provides those people a solution to a "I want a tiny /sbin/hotplug" problem. Also, udevstart working as /sbin/hotplug is great for keeping things in order, which is important during normal operation. But during the boot sequence, the odds of getting out-of-order events, or even remove events, is somewhat limited. So, this /sbin/hotplug replacement can work in an initrd/initramfs image when udevstart would be overkill. And finally, even if you do use udevstart to manager /sbin/hotplug events, you still need a module autoloader program. This package provides executables for that problem, if you don't want to (or you can't) use the existing linux-hotplug scripts. udev will never do the module loading logic, so there's no duplication in this case. Hope this helps. I do realize the whole hotplug process is getting a bit complicated. I hope to write up some good documentation on what all is involved to help clear up some of the confusion that the combination of udev, udevsend, udevd, hal, /etc/hotplug.d/, /etc/dev.d/, and /sbin/hotplug have caused lately. thanks, greg k-h

Related Links:

I hope to write up some good

Anonymous (not verified)
on
February 11, 2005 - 12:10pm

I hope to write up some good documentation on what all is involved to help clear up some of the confusion that the combination of udev, udevsend, udevd, hal, /etc/hotplug.d/, /etc/dev.d/, and /sbin/hotplug have caused lately.

Yes, that would be _really_ nice.

_really_ very nice and _good_

Anonymous (not verified)
on
February 11, 2005 - 12:28pm

_really_ very nice and _good_ [Seinfeld fans will know this one]

What about merging all these

Anonymous (not verified)
on
February 18, 2005 - 1:39am

What about merging all these related projects into one?
What about merging all the kernel notification/event mechanisms into one?

A causal observer who is curious.

execline?

Andy Goth (not verified)
on
February 11, 2005 - 1:39pm

How about using execline instead of bash for the hotplug (-ng) scripts? This avoids the need to keep an interpreter in memory after the script starts executing.

Execline was designed for embedded systems, so it's very, very light on memory usage.

Comparison between /bin/sh and execline: "You can have hundreds of execline scripts running simultaneously on an embedded box. Just try that with a shell."

What about Linux starting to

Anonymous (not verified)
on
February 12, 2005 - 3:49pm

What about Linux starting to use a decent shell... like rc

What about shc?

Jakub Narebski (not verified)
on
February 13, 2005 - 7:22am

What about shc (shell compile)? The purpose of it is to allow shell scripts to contain vulnerable data like passwords, without danger of revealing them, but maybe it can be used to speed up the scripts? At least till the tiny C version written from scratch by hotplug-ng devloper(s).

why? what against C code?

Daniele (not verified)
on
February 13, 2005 - 12:20pm

shc is interesting if you want some script perform better and you don't want to write it in C for a number of reason (interface/semantics changing often, could be a good reason).

Greg write this small code in C, and, just to give an example, it compile hotplug multiplexer linking it statically in 4412 byte, original script require 1139 (most is comment), fs store file at least 4096 .. just another block.

Interface of hotplug script could change in a week or (more prob) in a month, that's ok to retouch C code at this frequence and is not very difficult.

Also hotplug is not a matter of configuration, but a matter of auto-configuration, and "auto" say you all: you shouldn't do anything.

Even if it require more storage it spare memory, that is really the more valuable resource for performance. I think memory allocation matter more than it being interpreted: even having a bash instance running, thus taking advance of code sharing, the numer of task for kernel for all that increase. So, even with the same user time, running a bash script could increase system time compared with exec of native code.

Of course, only benchmark can give you the answer.

Shell scripts have a place too

Andy Goth (not verified)
on
February 13, 2005 - 8:28pm

Sure, you can optimize (size and performance) to your heart's content with C code, but then again you can do the same in assembly. But the results are in no way guaranteed to be pretty. With each line of source and each obscure idiom, come potential for more bugs and less maintainability. Likewise the result gains a sort of inertia, resisting improvements to design and interface.

For the short answer, see Master Foo and the Ten Thousand Lines. :^)

Let's take a specific example: string handling. You have several options:

  • Careful use of malloc() and free(). Doing this correctly, every single time, takes a lot of discipline--- it's very easy to make a mistake.
  • Fixed buffers. This is far easier, but artificially clamping string lengths to a limit can cause interoperability problems. And forgetting to clamp string lengths opens the door to buffer overflow exploits.
  • Use of a library. There's good stuff out there, like stralloc and vec, but they're still a pain to use, compared to strings in higher-level languages.
  • Use of a higher-level language. Many choices, but I'll just focus on shell scripting. If your shell internally has all its ducks in a row when it comes to memory allocation, you will never ever have a problem.
  • So let's say you settle on using a shell. You gain, for free, use of already-debugged piping, redirection, signal handling, process execution, forking, pattern matching, and whatever else. With any luck, your source code will be shorter; easier to maintain, study, and customize; more portable; and free to focus on the high-level task at hand.

    But you pay the cost of requiring the continued presence of an interpreter... right? Not necessarily. execline and execlineb are designed to dodge that bullet by simply exec()'ing into the script, storing the "rest" of the script in argv. And there are no "built-in" commands, so there's no duplication of code between, say, bash scanning its list of built-ins and libc scanning the $PATH. Sounds like a bargain.

    Sorry about the post length, and sorry about the double post earlier (browser problem)... but this is just a topic about which I have strong feelings.

    It's not a moby

    Daniele (not verified)
    on
    February 15, 2005 - 8:06am

    You can easily write one hundred line of good and checked C code in 2 hours.

    Also there was no change in hotplug multiplexer for long time.

    It's better to think before to change interfaces.

    hotplug should be stable, and should not change every minor kernel release.

    I have nothing against bash, and nothing against execline (which I know thank to you).

    I have see the source and I cannot see nothing of so carefull need on string management, and the code is very compact and clean, well wroten.

    And, sorry, but I don't see nothing of more obscure in one hundreds of C code than in ten line of perl code.

    p.s.:
    shc produce C code, it does not require shell at all and producede elf is linked against libc not other. I do not know how good is code produced.

    Don't take account about ps

    Daniele (not verified)
    on
    February 15, 2005 - 8:39am

    (on shc)I've just run ldd and I don't read the code.

    Re: shc

    Andy Goth (not verified)
    on
    February 13, 2005 - 7:24pm

    If I read shc(1) correctly, the program produced by shc simply calls the shell repeatedly to interpret the embedded script. It offers neither speed nor size improvements, and most importantly it still requires that the shell be present in the filesystem. All in all, it adds overhead. Its purpose is to shroud the script source and to provide an expiration feature. As such, it doesn't help us out.

    What Linux?

    Anonymous (not verified)
    on
    February 15, 2005 - 4:27am

    What is this Linux you are talking about? Ref Hat? SUSE? Gentoo? Slackware? I didn't know that there is a "standard" shell that "Linux" uses. I do know that there are default shells that the distros prefer (uszually Bash it seems). But just because most distros prefer Bash does not mean that "Linux" uses Bash. There is no "Linux OS", there are several separate distros that all use Linux-kernel and software around it

    Standard Posix and Single Unix shell

    Daniele (not verified)
    on
    February 15, 2005 - 8:55am

    Bash aims to conform to standard posix shell.

    [single unix] http://www.opengroup.org/onlinepubs/007908799/

    [standard shell] http://www.opengroup.org/onlinepubs/007908799/xcu/shellix.html

    "There is no "Linux OS", there are several separate distros that all use Linux-kernel and software around it"

    there is also http://www.linuxbase.org/ that is taken into account.

    This is a matter of freedom: you could no care about standard but probably no one want to use your distro.

    module_sata

    Anonymous Fred (not verified)
    on
    February 11, 2005 - 2:53pm

    I know that SATA hotplug is not yet supported, but in a near future, you may add a module_sata just like the module_scsi.

    http://www.kerneltraffic.org/kernel-traffic/kt20050203_295.html#13

    about SATA hotplug...

    Julio Meca (not verified)
    on
    February 11, 2005 - 3:37pm

    so does that mean that SATA drives aren't still recognized as SATA ones?

    I mean... my SATA drive is currently being mapped as '/dev/sda1' so it sees my drive as if it were of SCSI kind... does that mean that I would have the chance to have it being recognized as SATA and thus the chance to improve it's data transfer using hdparm? currently I can't do any kind of improvemente with that command... :(

    Don't fear that...

    on
    February 11, 2005 - 4:18pm

    No, that means that you are using libata (SATA). This implements an ATA state machine and presents a SCSI type interface. The SCSI interface is a misnomer; What you're *really* talking about is this middle layer that the SCSI layer on Linux is. Why do you think USB drives show up in SCSI?

    Cheers,

    the Vandy Monster

    Hi vandy, can you please give

    Anonymous (not verified)
    on
    February 11, 2005 - 5:21pm

    Hi vandy, can you please give some more information? I mean *why* are usb drives shown up as scsi.. do usb scsi and sata share some code? Would be really nice if you could explain this :-)

    I think the USB mass storage

    Mark Williamson (not verified)
    on
    February 11, 2005 - 6:18pm

    I think the USB mass storage command set resembles the SCSI command set, so I guess this isn't as weird as it looks at first.

    The USB mass storage driver translates between SCSI requests used by the SCSI layer and USB messages. I had occasion to briefly poke around in this code when I was doing USB work.

    currently I can't do any kind

    on
    February 12, 2005 - 12:46am

    currently I can't do any kind of improvemente with that command... :(


    How do you know you need to make any improvements?


    so does that mean that SATA drives aren't still recognized as SATA ones?

    If they weren't recognized as SATA they wouldn't work at all. libata uses the SCSI layer because it doesn't suck nearly as bad as the IDE layer in Linux.


    SATA drivers did the same thing in Windows originally and I believe some still do. I remember reading somewhere a while back that the IDE layer in NT used one big lock for all ports an drives, but I have no idea if that's still accurate.

    about SATA hotplug...

    Julio Meca (not verified)
    on
    February 12, 2005 - 8:17am

    oh, I see... so it works as SATA, thanks god :)

    well... about the improvements, well.. it's just with my IDE drives on my old computer I used to activate DMA using hdparm, but testing the same with some distributions in my current setup, some of them to-be-installed and some of them on livecd, I can't do it, just was wondering why, but if you could gain, let's say, even 1 or 2 megabytes of average throughput, well... that's an improvement, right? ;)

    Julio

    but if you could gain, let's

    on
    February 12, 2005 - 1:55pm

    but if you could gain, let's say, even 1 or 2 megabytes of average throughput, well... that's an improvement, right? ;)


    You can still adjust the readahead with hdparm. I haven't used SATA yet, but if the controller and drive are set right in the BIOS, you shouldn't need to do any tweaking, right? I guess I'm used to not having to fight with that crap since I have mostly SCSI drives.

    That sounds about right

    on
    February 14, 2005 - 6:49pm

    Most of the parameters you can set w/ hdparm (aside from filesystem readahead--a parameter inside the kernel, not in the drive) are PATA-specific kludges. The reason we never needed hdparm for SCSI is that the SCSI protocol is much more sane.

    My understanding is that SATA protocol is roughly the same as SCSI protocol, only over a different transport. I believe there are some differences, but I'd imagine most are there to either: (a) force differentiation between Serial Attached SCSI and SATA, and/or (b) provide backwards compatibility for specific PATA features missing from SCSI.

    Thus, you shouldn't need hdparm for SATA drives for the same reason you didn't need it for SCSI. There may be some tunables in /proc for tuning things like TCQ depth and whatnot, but you won't be mucking with "PIO vs. DMA" and crap like that.

    Code sharing

    on
    February 12, 2005 - 3:56am

    Yes, they do share code - the SCSI layer. The SCSI layer is not so much SCSI as a layer between the hardware and the block device level.

    Cheers,

    Michael

    Indeed

    Anonymous (not verified)
    on
    February 13, 2005 - 7:52pm

    This seems to be a "feature" of operating systems of a Certain Age: they all have a layer that started out as the SCSI subsystem but became more and more general purpose, all the while still called scsi. The AmigaOS had a scsi.device that ended up used for nearly everything disky for a while... until the disk stuff got rearchitected and scsi.device became basically only for IDE devices (?!), windows NT 3.x did roughly the same thing only without the insane last step.

    You'd think they'd call it something like the "Storage HAL" or something - the SCSI/ATAPI command set seems to be the rough (very rough!) "standard" hardware abstraction layer for computer storage devices, even if they're not actually SCSI or ATAPI.

    recommend teTeX

    Anonymous (not verified)
    on
    February 12, 2005 - 12:30am

    > I hope to write up some good documentation

    http://www.tug.org/tetex/tetex-src/INSTALL

    --

    Ubuntu is currently using hot

    Anonymous (not verified)
    on
    February 12, 2005 - 1:36pm

    Ubuntu is currently using hotplug with grepmap to speed up start-up time of hotplug. On old hardware (e.g. PII) this gives a huge performance boost while booting. How does this project relate to this? Does it replace/obsolete this completely?

    re: Ubuntu

    on
    February 12, 2005 - 7:16pm

    That is discussed here in the above thread.

    diethotplug = grepmap?

    Anonymous (not verified)
    on
    February 13, 2005 - 7:30am

    It's not clear to me, as the word grepmap does not even occur in that thread... They are talking about "dietHotplug" in Ubuntu, is that maybe actually their grepmap patched hotplug?

    Fantastic idiea

    Marcelo Pacheco (not verified)
    on
    February 14, 2005 - 8:20am

    Hutplug in sh is realy terrible, just like rc init scripts, fat and slow to do sample things

    rc scripts

    Daniele (not verified)
    on
    February 15, 2005 - 1:58pm

    If there is a agreement on what a start/stop/restart/reload script should/could and how it do, it could be defined an xml schema and a simple "compiler" to native code. It also required a framework for startup (hopefull parallel).

    there is no such agreement.

    Elektra (licence : BSD, GPL, or LGPL)

    on
    February 22, 2005 - 8:48am

    It's already exist :
    http://elektra.sourceforge.net
    For init script : go to 4.3. Key Examples

    Have a look to (Ooo document):
    http://elektra.sourceforge.net/elektra.sxi

    NB against FUD :
    Please, inform you, it's definitely not like windows registry or gnome registry

    Master Foo is right

    Anonymous (not verified)
    on
    February 16, 2005 - 6:29am

    Have you ever tried debugging a headless device which just refuces to boot every now and then? Well, I have. The boot part was C++/C but it's a pain compared to simple /bin/sh scripts which you can edit on device when it happened to boot. Perhaps there's a place for this kins of improvements since diversity is a good thing, but optimization without measurements is, well, not very scientific/wise/good engineering practice.

    IMHO, Master Foo is right on this.

    Comment viewing options

    Select your preferred way to display the comments and click "Save settings" to activate your changes.