]> git.ipfire.org Git - thirdparty/kmod.git/commit
Fix race while loading modules
authorHarish Jenny K N <harish_kandiga@mentor.com>
Sun, 22 Feb 2015 18:41:07 +0000 (15:41 -0300)
committerLucas De Marchi <lucas.demarchi@intel.com>
Sat, 28 Feb 2015 17:15:22 +0000 (14:15 -0300)
commitfd44a98ae2eb5eb32161088954ab21e58e19dfc4
treefe68fd61cde2253e5741a1b439a05a1183f09b8e
parentfae77516b405e8e685afd08acff71209a90609f7
Fix race while loading modules

usecase: two sd cards are being mounted in parallel at same time on
dual core. example modules which are getting loaded is nls_cp437.
While one module is being loaded , it starts creating sysfs files.
meanwhile on other core, modprobe might return saying the module
is KMOD_MODULE_BUILTIN, which might result in not mounting sd card.

Experiments done to prove the issue in kmod.
Added sleep in kernel module.c at the place of creation of sysfs files.
Then tried `modprobe nls_cp437` from two different shells.
While the first was still waiting for its completion ,
the second one returned saying the module is built-in.

[ Lucas:

  The problem is that the creation of /sys/module/<name> and
  /sys/module/<name>/initstate are not atomic. There's a small window in
  which the directory exists but the initstate file was still not
  created.

  Built-in modules can be handled by searching the modules.builtin file.
  We actually lose some "modules" that create entries in /sys/modules
  (e.g. vt) and are not in modules.builtin file: only those that can be
  compiled as module are present in this file.

  We enforce mod->builtin to always be up-to-date when
  kmod_module_get_initstate() is called. This way if the directory
  exists but the initstate doesn't, we can be sure this is because the
  module is in the "coming" state, i.e. kernel didn't create the file
  yet, but since builtin modules were already handled by checking our
  index the only reason for that to happen is that we hit the race
  condition.

  I also added some tweaks to the patch, so we don't repeat the code for builtin
  lookup.  ]
libkmod/libkmod-internal.h
libkmod/libkmod-module.c
libkmod/libkmod.c