{"id":445,"date":"2020-08-18T19:23:47","date_gmt":"2020-08-18T20:23:47","guid":{"rendered":"http:\/\/www.linux-tutorial.info\/?page_id=77"},"modified":"2020-08-22T19:26:17","modified_gmt":"2020-08-22T20:26:17","slug":"this-is-the-page-title-toplevel-278","status":"publish","type":"page","link":"http:\/\/www.linux-tutorial.info\/?page_id=445","title":{"rendered":"Modules"},"content":{"rendered":"\n<title>Modules<\/title>\n<p>\nThis chapter describes how the Linux kernel can dynamically load functions,\nfor example filesystems, only when they are needed.\n<p>\nLinux is a monolithic <glossary>kernel<\/glossary>;\nthat is, it is one, single, large program\nwhere all the functional components of the kernel have access to all\nof its internal data structures and routines.\nThe alternative is to have a micro-kernel structure where\nthe functional pieces of the <glossary>kernel<\/glossary> are broken out into separate units\nwith strict communication mechanisms between them.\nThis makes adding new components into the kernel via the configuration\nprocess rather time consuming.\nSay you wanted to use a SCSI driver for an NCR 810 <glossary>SCSI<\/glossary> and you had\nnot built it into the kernel.\nYou would have to configure and then build a new kernel before you\ncould use the NCR 810.\nThere is an alternative, Linux allows you to dynamically load\nand unload components of the operating system as you need them.\nLinux modules are lumps of code that can be dynamically linked into the\nkernel at any point after the system has booted.\nThey can be unlinked from the kernel and removed when they are no longer\nneeded.\nMostly Linux kernel modules are device drivers, pseudo-device drivers\nsuch as network drivers, or file-systems.\n<p>\nYou can either load and unload Linux <glossary>kernel<\/glossary> modules explicitly  using\nthe <command>insmod<\/command> and <command>rmmod<\/command> commands or the kernel itself can demand\nthat the kernel daemon (<command>kerneld<\/command>) loads and unloads the modules as they are needed.\n<p>\nDynamically loading code as it is needed is attractive as it keeps the\nkernel size to a minimum and makes the <glossary>kernel<\/glossary> very flexible.\nMy current Intel kernel uses modules extensively and is only 406Kbytes long.\nI only occasionally use <tt>VFAT<\/tt> file systems and so I build my Linux kernel\nto automatically load the <tt>VFAT<\/tt> file system module as I mount a <tt>VFAT<\/tt>\npartition.\nWhen I have unmounted the <tt>VFAT<\/tt> partition the system detects that I no\nlonger need the <tt>VFAT<\/tt> file system module and removes it from the system.\nModules can also be useful for trying out new <glossary>kernel<\/glossary> code without having\nto rebuild and reboot the <glossary>kernel<\/glossary> every time you try it out.\nNothing, though, is for free and there is a slight performance and\nmemory penalty associated with kernel modules.\nThere is a little more code that a loadable module must provide\nand this and the extra data structures take a little more memory.\nThere is also a level of indirection introduced that makes accesses of\nkernel resources slightly less efficient for modules.\n<p>\nOnce a Linux module has been loaded it is as much a part of the kernel\nas any normal <glossary>kernel<\/glossary> code.\nIt has the same rights and responsibilities as any kernel code; in other\nwords, Linux kernel modules can crash the kernel just like all kernel\ncode or device drivers can.\n<p>\nSo that modules can use the <glossary>kernel<\/glossary> resources that they need, they must\nbe able to find them.\nSay a module needs to call <tt>kmalloc()<\/tt>, the kernel memory allocation\nroutine.\nAt the time that it is built, a module does not know where in memory\n<tt>kmalloc()<\/tt> is,  so when\nthe module is loaded, the <glossary>kernel<\/glossary> must fix up all of the module&#8217;s\nreferences to <tt>kmalloc()<\/tt> before the module can work.\nThe kernel keeps a list of all of the kernel&#8217;s resources in the kernel symbol\ntable so that it can resolve references to those resources from the modules\nas they are loaded.\nLinux allows module stacking, this is where one module requires the\nservices of another module.\nFor example, the <tt>VFAT<\/tt> file system module requires the services\nof the <tt>FAT<\/tt> file system module as the <tt>VFAT<\/tt> file system is more\nor less a set of extensions to the <tt>FAT<\/tt> file system.\nOne module requiring services or resources from another module is very\nsimilar to the situation where a module requires services and resources\nfrom the kernel itself.\nOnly here the required services are in another, previously loaded\nmodule.\nAs each module is loaded, the <glossary>kernel<\/glossary> modifies the kernel symbol table,\nadding to it all of the resources or symbols exported by the newly\nloaded module.\nThis means that, when the next module is loaded, it has access to\nthe services of the already loaded modules.\n<p>\nWhen an attempt is made to unload a module, the <glossary>kernel<\/glossary> needs to know\nthat the module is unused and it needs some way of notifying the module\nthat it is about to be unloaded.\nThat way the module will be able to free up any system resources that it\nhas allocated, for example <glossary>kernel<\/glossary> memory or <glossary>interrupt<\/glossary>s, before it is\nremoved from the kernel.\nWhen the module is unloaded, the kernel removes any symbols that that module\nexported into the kernel symbol table.\n<p>\nApart from the ability of a loaded module to crash the operating system\nby being badly written, it presents another danger.\nWhat happens if you load a module built for an earlier or later <glossary>kernel<\/glossary>\nthan the one that you are now running?\nThis may cause a problem if, say, the module makes a call to a kernel routine\nand supplies the wrong arguments.\nThe kernel can optionally protect against this by making rigorous version\nchecks on the module as it is loaded.\n","protected":false},"excerpt":{"rendered":"<p>Modules This chapter describes how the Linux kernel can dynamically load functions, for example filesystems, only when they are needed. Linux is a monolithic kernel; that is, it is one, single, large program where all the functional components of the &hellip; <a href=\"http:\/\/www.linux-tutorial.info\/?page_id=445\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-445","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"http:\/\/www.linux-tutorial.info\/index.php?rest_route=\/wp\/v2\/pages\/445","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.linux-tutorial.info\/index.php?rest_route=\/wp\/v2\/pages"}],"about":[{"href":"http:\/\/www.linux-tutorial.info\/index.php?rest_route=\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"http:\/\/www.linux-tutorial.info\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.linux-tutorial.info\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=445"}],"version-history":[{"count":1,"href":"http:\/\/www.linux-tutorial.info\/index.php?rest_route=\/wp\/v2\/pages\/445\/revisions"}],"predecessor-version":[{"id":630,"href":"http:\/\/www.linux-tutorial.info\/index.php?rest_route=\/wp\/v2\/pages\/445\/revisions\/630"}],"wp:attachment":[{"href":"http:\/\/www.linux-tutorial.info\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=445"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}