From: eLinux.org

Deferred Initcalls

Contents

Introduction

An “initcall” is the initialization function for a module which is
statically linked into the Linux kernel. Running of initcalls can
consume a large amount of time during bootup. However, some modules do
not need to be initialized so early, in order for the embedded device to
become usable for its primary purpose. These initcalls can be run later,
after first boot (and possibly first use) has completed.

For example, many digital cameras have USB buses, which need to be
initialized in order for the camera to be used as a mass storage device
(to download pictures to a desktop computer). However, the USB buses do
not need to be initialized for the primary use of the camera, which is
to take pictures. In order to be able to take a picture as soon as
possible after booting, initialization of the USB system in the kernel
can be deferred until after the primary boot sequence for the camera.

Description

Using a short patch (available for kernel version 2.6.27) it is possible
to avoid running certain initcalls at bootup time. The way this patch
works is that the developer selects some initcalls that they want to
defer and modifies the declaration of those initcalls.

When the system is booted, those initcalls are not called in their
normal sequence. When the system is done with primary boot, an
application from user space triggers the calling of the deferred
initcalls, using a flag in the /proc filesystem.

When the flag is set, the deferred initcalls are run, and the kernel
memory for the “init” segment is finally freed.

How to use

To use deferred initcalls, first you need to identify the modules that
are not required for the primary functionality of the product, and which
are taking too long to initialize. (See Initcall
Debug
for this.)

Using the example above of the digital camera and USB, you could
identify uhci_hcd_usb and ehci_hcd_init as two initcalls that could
be deferred.

Change the module init routine declaration for the initcalls you wish to
defer. This is done in the Linux kernel source code. For example,
change:

  1. module_init(foo_init)
  2. deferred_module_init(foo_init)

Modules marked like this are not initialized during kernel boot

After main init, do:

  1. cat /proc/deferred_initcalls

This will cause the kernel to run all deferred initcalls. Also the .init
section memory is freed by kernel. The contents of
/proc/deferred_initcalls will return 0 if deferred initcalls were not
yet run, and 1 otherwise on subsequent reads.

deferred USB initcall example

As a test, on an X86 desktop system, I deferred the initialization of
the USB subsystem on a 2.6.27 kernel, by using deferred_module_init on
the functions: ehci_hcd_init and uhci_hcd_init

This resulted in a total times savings of 530 milliseconds, during the
kernel boot phase. (Of course, this time was used subsequently when the
deferred initcalls were triggered later on.)

Specifially, I changed:

  1. module_init(ehci_hcd_init)

to

  1. deferred_modle_init(echi_hcd_init)

and

  1. module_init(uhci_hcd_init)

to

  1. deferred_module_init(uhci_hcd_init)

Patch

Here is the main deferred initcalls patch for 2.6.26, 2.6.27:
Media:Deferred_initcalls.patch

For 2.6.28 the forward-ported patch is here:
Media:Deferred_initcalls-2.6.28.patch

For 3.10 the patch is here:
Media:0001-Port-deferred-initcalls-to-3.10.patch

Here (inline) is a patch showing modification of USB and IDE initcalls
to be be deferred initcalls:
(This patch is also available downloadable as:
Media:Defer-usb-and-ide-initcalls.patch)

  1. commit e7a5b8bb6a5d04054dec1e85d53bbe115059d0d0
  2. Author: Tim Bird <tim.bird@am.sony.com>
  3. Date: Fri Sep 12 11:35:58 2008 -0700
  4. Use deferred_module_init on long-probing IDE and USB modules.
  5. These modules were taking about 700 ms and 400 ms, respectively
  6. to initialize. On many embedded systems, these initializations
  7. can be done after major boot activity is completed, with no
  8. loss of functionality.
  9. diff --git a/drivers/ata/ata_piix.c b/drivers/ata/ata_piix.c
  10. index e9e32ed..cb2ebf3 100644
  11. --- a/drivers/ata/ata_piix.c
  12. +++ b/drivers/ata/ata_piix.c
  13. @@ -1494,5 +1494,5 @@ static void __exit piix_exit(void)
  14. pci_unregister_driver(&piix_pci_driver);
  15. }
  16. -module_init(piix_init);
  17. +deferred_module_init(piix_init);
  18. module_exit(piix_exit);
  19. diff --git a/drivers/usb/host/ehci-hcd.c b/drivers/usb/host/ehci-hcd.c
  20. index 8409e07..44a8340 100644
  21. --- a/drivers/usb/host/ehci-hcd.c
  22. +++ b/drivers/usb/host/ehci-hcd.c
  23. @@ -1107,7 +1107,7 @@ clean0:
  24. #endif
  25. return retval;
  26. }
  27. -module_init(ehci_hcd_init);
  28. +deferred_module_init(ehci_hcd_init);
  29. static void __exit ehci_hcd_cleanup(void)
  30. {
  31. diff --git a/drivers/usb/host/uhci-hcd.c b/drivers/usb/host/uhci-hcd.c
  32. index 3a7bfe7..9c27ef0 100644
  33. --- a/drivers/usb/host/uhci-hcd.c
  34. +++ b/drivers/usb/host/uhci-hcd.c
  35. @@ -999,7 +999,7 @@ static void __exit uhci_hcd_cleanup(void)
  36. kfree(errbuf);
  37. }
  38. -module_init(uhci_hcd_init);
  39. +deferred_module_init(uhci_hcd_init);
  40. module_exit(uhci_hcd_cleanup);
  41. MODULE_AUTHOR(DRIVER_AUTHOR);

Categories: