Skip to contentSkip to conversation

Call a module's init function only after the module is successfully added

PATCH In review Joe Auricchio 3 participants 6 messages Jul 21, 2009 – Jul 25, 2009

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Call a module's init function only after the module is successfully added

Hi,

* kern/dl.c (grub_dl_load_core): Call grub_dl_call_init only after successful grub_dl_add

With this patch the module's init function will no longer be called:
- if the module is already loaded
- if malloc fails in grub_dl_add
- before the module is added to grub_dl_head

The first two are errors and cause the load to abort. The module shouldn't try to init if it's only going to abort. I don't know much about grub_dl_head, so I don't know if #3 could have caused problems.

-joe

-=-=-=-=-=-=-=-=-=-=-=-=-

grub2/kern/dl.c +2 -1
@@ -558,16 +558,17 @@
  grub_dl_flush_cache (mod);
 
  grub_dprintf ("modules", "module name: %s\n", mod->name);
  grub_dprintf ("modules", "init function: %p\n", mod->init);
-  grub_dl_call_init (mod);
 
  if (grub_dl_add (mod))
    {
      grub_dl_unload (mod);
      return 0;
    }
 
+  grub_dl_call_init (mod);
+
  return mod;

}

/* Load a module from the file FILENAME. */

#1 lore raw
Quoted 5 lines · Joe Auricchio

Hi,

* kern/dl.c (grub_dl_load_core): Call grub_dl_call_init only after successful grub_dl_add

Quoted 8 lines

With this patch the module's init function will no longer be called:
- if the module is already loaded
- if malloc fails in grub_dl_add
- before the module is added to grub_dl_head

The first two are errors and cause the load to abort. The module shouldn't try to init if it's only going to abort. I don't know much about grub_dl_head, so I don't know if #3 could have caused problems.

I have no objections.

By the way, kern/dl.c have some unused functions (grub_dl_unload_all). Also, it looks like the memory for the module information (grub_dl_t) is allocated twice - first in grub_dl_load_core(), then in grub_dl_add() as part of grub_dl_list_t.

grub_dl_load() can return either memory from the list that shouldn't be freed by the caller, or the result of grub_dl_load_file(), which is allocated and is not referenced anywhere.

Signature · 2 lines
Regards,
Pavel Roskin
#2 lore raw
Quoted 1 line · Pavel Roskin

By the way, kern/dl.c have some unused functions (grub_dl_unload_all).

I was wrong, I didn't check assembler files.

Quoted 3 lines

Also, it looks like the memory for the module information (grub_dl_t) is allocated twice - first in grub_dl_load_core(), then in grub_dl_add() as part of grub_dl_list_t.

I was wrong here too. I'm too accustomed to Linux style where typedefs are discouraged. The list keeps a pointer, not a copy.

I'll apply your patch if it passes testing.

Signature · 2 lines
Regards,
Pavel Roskin
#3 lore raw
In reply to Pavel Roskin
Quoted 2 lines · Pavel Roskin

By the way, kern/dl.c have some unused functions (grub_dl_unload_all).

grub_machine_fini() used to rely on this, but at some point we concluded it wasn't useful to change machine state when loading an OS, other than things specific to each particular loader.

Signature · 5 lines
Robert Millan

  The DRM opt-in fallacy: "Your data belongs to us. We will decide when (and
  how) you may access your data; but nobody's threatening your freedom: we
  still allow you to remove your data and not access it at all."
#4 lore raw
Quoted 7 lines · Robert Millan
Quoted 2 lines · Pavel Roskin

By the way, kern/dl.c have some unused functions (grub_dl_unload_all).

grub_machine_fini() used to rely on this, but at some point we concluded it wasn't useful to change machine state when loading an OS, other than things specific to each particular loader.

As I said, I missed calls in *.S files. grub_linux16_boot() and grub_chainloader_real_boot() still use it. Perhaps they shouldn't.

Signature · 2 lines
Regards,
Pavel Roskin
#5 lore raw
Quoted 11 lines · Pavel Roskin
Quoted 7 lines · Robert Millan
Quoted 2 lines · Pavel Roskin

By the way, kern/dl.c have some unused functions (grub_dl_unload_all).

grub_machine_fini() used to rely on this, but at some point we concluded it wasn't useful to change machine state when loading an OS, other than things specific to each particular loader.

As I said, I missed calls in *.S files. grub_linux16_boot() and grub_chainloader_real_boot() still use it. Perhaps they shouldn't.

linux16.mod needs to transfer control to Linux with VGA in known state (text mode), so grub_console_fini() has to be called, but this can be simplified. There's no reason we have to unload all modules just to archieve this effect IMHO.

Same applies for chainloader.

linux.mod can transfer control both in text or graphical mode, so it doesn't have this problem.

Signature · 5 lines
Robert Millan

  The DRM opt-in fallacy: "Your data belongs to us. We will decide when (and
  how) you may access your data; but nobody's threatening your freedom: we
  still allow you to remove your data and not access it at all."