{"id":418,"date":"2025-12-16T10:44:40","date_gmt":"2025-12-16T02:44:40","guid":{"rendered":"https:\/\/www.garfield-blog.top\/?p=418"},"modified":"2025-12-16T10:49:42","modified_gmt":"2025-12-16T02:49:42","slug":"%e3%80%90linux%e3%80%91linux%e6%a8%a1%e5%9d%97%e7%9b%b8%e5%85%b3api","status":"publish","type":"post","link":"https:\/\/www.garfield-blog.top\/?p=418","title":{"rendered":"\u3010Linux\u3011Linux\u6a21\u5757\u6ce8\u518c\u63a5\u53e3"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">\u6a21\u5757\u5165\u53e3\u548c\u51fa\u53e3\u6ce8\u518cAPI<\/h2>\n\n\n\n<p>\u5728\u5404\u4e2a<code>linux<\/code>\u5b50\u6a21\u5757\u4e2d\u7ecf\u5e38\u53ef\u4ee5\u770b\u5230\u7684<code>module_init<\/code>\u548c<code>module_exit<\/code>\u4e24\u4e2a\u5b8f\uff0c\u8fd9\u5206\u522b\u6ce8\u518c\u4e86\u6a21\u5757\u7684\u521d\u59cb\u5316\u548c\u7ed3\u675f\u90e8\u5206\u9700\u8981\u8c03\u7528\u7684\u51fd\u6570<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>module_init(demo_init)\uff1a<\/strong><br><code>module_init<\/code>\u5b8f\u7684\u4f5c\u7528\u662f\u544a\u8bc9\u5185\u6838\uff0c\u5f53\u8fd9\u4e2a\u6a21\u5757\u88ab\u52a0\u8f7d\u65f6\uff0c\u5e94\u8be5\u6267\u884c\u540d\u4e3a <code>demo_init<\/code> \u7684\u51fd\u6570\u3002<code>demo_init<\/code> \u51fd\u6570\u901a\u5e38\u5305\u542b\u6a21\u5757\u542f\u52a8\u65f6\u9700\u8981\u6267\u884c\u7684\u6240\u6709\u521d\u59cb\u5316\u4ee3\u7801\uff0c\u6bd4\u5982\u5206\u914d\u8d44\u6e90\u3001\u6ce8\u518c\u8bbe\u5907\u9a71\u52a8\u3001\u521b\u5efa\u5185\u6838\u7ebf\u7a0b\u7b49\u3002\u5982\u679c <code>demo_init<\/code> \u51fd\u6570\u6210\u529f\u6267\u884c\u5e76\u8fd4\u56de <code>0<\/code>\uff0c\u6a21\u5757\u5c31\u4f1a\u6210\u529f\u52a0\u8f7d\uff1b\u5982\u679c\u8fd4\u56de\u9519\u8bef\u7801\uff0c\u6a21\u5757\u52a0\u8f7d\u5c31\u4f1a\u5931\u8d25<\/li>\n\n\n\n<li><strong>module_exit(demo_exit)\uff1a<\/strong><br><code>module_exit<\/code>\u5b8f\u7684\u4f5c\u7528\u662f\u544a\u8bc9\u5185\u6838\uff0c\u5f53\u8fd9\u4e2a\u6a21\u5757\u88ab\u5378\u8f7d\u65f6\uff0c\u5e94\u8be5\u6267\u884c\u540d\u4e3a <code>demo_exit<\/code> \u7684\u51fd\u6570\u3002<code>demo_exit<\/code> \u51fd\u6570\u8d1f\u8d23\u6267\u884c\u6a21\u5757\u9000\u51fa\u65f6\u6240\u9700\u7684\u6240\u6709\u6e05\u7406\u5de5\u4f5c\uff0c\u4f8b\u5982\u91ca\u653e\u5728 <code>demo_init<\/code> \u4e2d\u5206\u914d\u7684\u8d44\u6e90\u3001\u6ce8\u9500\u8bbe\u5907\u9a71\u52a8\u3001\u505c\u6b62\u5185\u6838\u7ebf\u7a0b\u7b49\u3002\u786e\u4fdd\u6240\u6709\u8d44\u6e90\u90fd\u88ab\u6b63\u786e\u91ca\u653e\u5bf9\u4e8e\u7ef4\u6301\u7cfb\u7edf\u7a33\u5b9a\u6027\u81f3\u5173\u91cd\u8981<\/li>\n<\/ul>\n\n\n\n<p>\u4ece\u4e0b\u6587\u4e2d\u53ef\u4ee5\u770b\u5230<code>module_init<\/code>\u548c<code>module_exit<\/code>\u5b8f\u6709\u7740\u4e24\u79cd\u4e0d\u540c\u7684\u5b9e\u73b0\u65b9\u5f0f\uff0c\u4f7f\u7528\u4e86<code>MODULE<\/code>\u7f16\u8bd1\u53c2\u6570\u6765\u8fdb\u884c\u533a\u5206\uff0c\u5176\u4e3b\u8981\u7684\u4f5c\u7528\u662f<strong>\u63a7\u5236\u6700\u7ec8\u7f16\u8bd1\u4ea7\u7269\u662f.KO\u6587\u4ef6\u8fd8\u662f\u5b8c\u6574\u7684Linux\u955c\u50cf<\/strong>\u3002\u4e0d\u7ba1\u662f\u54ea\u4e00\u79cd\u60c5\u51b5\uff0c\u4e8c\u8005\u6700\u7ec8\u5c55\u5f00\u90fd\u662f\u4e00\u7cfb\u5217\u5b8f\u7684\u5d4c\u5957\uff0c\u5b8f\u7684\u6700\u7ec8\u5c55\u5f00\u662f\u7684<code>inline<\/code>\u51fd\u6570\u548c<code>GCC<\/code>\u7f16\u8bd1\u53c2\u6570\u3002\u4e5f\u5c31\u662f\u8bf4\u8fd9\u4e00\u7cfb\u5217\u7684\u903b\u8f91\u5b9e\u9645\u4e0a\u4f1a<strong>\u5728\u7a0b\u5e8f\u7684\u7f16\u8bd1\u9636\u6bb5\u5b8c\u6210<\/strong><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">module_init\u539f\u7406\u5206\u6790<\/h3>\n\n\n\n<p>\u67e5\u770b<code>Linux 5.4<\/code>\u6e90\u7801\u53ef\u4ee5\u770b\u5230<code>module_init<\/code>\u5b8f\u5bf9\u5e94\u6e90\u4ee3\u7801\uff0c\u53ef\u4ee5\u770b\u5230<code>module_init<\/code>\u5bf9\u5e94\u7740\u4e24\u79cd\u4e0d\u540c\u7684\u5b9e\u73b0\u3002<code>MODULE<\/code> \u5b8f\u672c\u8eab\u5e76\u4e0d\u662f\u76f4\u63a5\u5b9a\u4e49\u67d0\u4e2a\u503c\u6216\u51fd\u6570\uff0c\u800c\u662f\u4e00\u4e2a<strong>\u7f16\u8bd1\u65f6\u5f00\u5173<\/strong>\u3002\u7f16\u8bd1\u5668\u901a\u5e38\u4f1a\u901a\u8fc7\u547d\u4ee4\u884c\u53c2\u6570\uff08\u4f8b\u5982 <code>-DMODULE<\/code>\uff09\u6765\u5b9a\u4e49 <code>MODULE<\/code> \u5b8f<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/linux-5.4\/include\/linux\/module.h\n#ifndef MODULE\n#define module_init(x)    __initcall(x);\n#else\n#define module_init(initfn)                    \\\n    static inline initcall_t __maybe_unused __inittest(void)        \\\n    { return initfn; }                    \\\n    int init_module(void) __copy(initfn) __attribute__((alias(#initfn)));\n#endif<\/code><\/pre>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong><code>MODULE<\/code> \u5b8f\u5f00\u542f\u65f6\u7684\u903b\u8f91<\/strong> <strong>\u6e90\u4ee3\u7801\uff1a<\/strong> <code>\/\/linux-5.4\/include\/linux\/module.h #define module_init(initfn) \\ static inline initcall_t __maybe_unused __inittest(void) \\ { return initfn; } \\ int init_module(void) __copy(initfn) __attribute__((alias(#initfn)));<\/code> <strong>\u6838\u5fc3\u601d\u60f3\uff1a<\/strong> \u5f53\u6587\u4ef6\u88ab\u7f16\u8bd1\u6210\u5185\u6838\u6a21\u5757\u4e5f\u5c31\u662f<code>.KO<\/code>\u6587\u4ef6\u65f6\uff0c\u4f1a\u6709\u660e\u786e\u7684 <code>init_module<\/code> \u51fd\u6570\u4f5c\u4e3a\u52a0\u8f7d\u65f6\u7684\u5165\u53e3\u70b9\u3002\u8fd9\u6bb5\u5b8f\u4ee3\u7801\u7684\u76ee\u7684\u662f\u5c06\u7528\u6237\u63d0\u4f9b\u7684\u521d\u59cb\u5316\u51fd\u6570 (<code>initfn<\/code>) \u8bbe\u7f6e\u4e3a\u5185\u6838\u6a21\u5757\u7684\u5b98\u65b9\u5165\u53e3\u70b9 (<code>init_module<\/code>)\u3002\u5b83\u901a\u8fc7<code>GCC<\/code>\u7684 <code>alias<\/code> \u5c5e\u6027\u4f7f\u5f97 <code>init_module<\/code> \u76f4\u63a5\u6307\u5411 <code>initfn<\/code> \u7684\u4ee3\u7801<\/li>\n\n\n\n<li><strong><code>MODULE<\/code> \u5b8f\u5173\u95ed\uff08\u9ed8\u8ba4\u60c5\u51b5\uff09\u65f6\u7684\u903b\u8f91<\/strong> <strong>\u6e90\u4ee3\u7801\uff1a<\/strong> \u901a\u8fc7\u67e5\u770b<code>linux 5.4<\/code>\u7684\u5bf9\u5e94\u6e90\u4ee3\u7801\uff0c\u5c55\u5f00\u5404\u4e2a\u5b8f\u53ef\u4ee5\u770b\u5230\u6700\u7ec8\u7684\u8c03\u7528 <code>\/\/linux-5.4\/include\/linux\/init.h #define ___define_initcall(fn, id, __sec) \\ static initcall_t __initcall_##fn##id __used \\ __attribute__((__section__(#__sec \".init\"))) = fn; #define __define_initcall(fn, id) ___define_initcall(fn, id, .initcall##id) #define device_initcall(fn) __define_initcall(fn, 6) #define __initcall(fn) device_initcall(fn) #define module_init(x) __initcall(x);<\/code> <strong>\u6838\u5fc3\u601d\u60f3\uff1a<\/strong> \u4e3b\u8981\u7684\u76ee\u7684\u662f\u4fee\u6539\u6700\u7ec8\u7f16\u8bd1\u751f\u6210\u7684<code>ELF<\/code>\u4e8c\u8fdb\u5236\u6587\u4ef6\u4e2d\u7684\u90e8\u5206\uff0c\u5c06\u9700\u8981\u6267\u884c\u7684\u51fd\u6570\u6307\u9488\u653e\u5728\u67d0\u4e2a\u7279\u5b9a\u7684\u4f4d\u7f6e <code>___define_initcall<\/code> \u5b8f\u7684\u76ee\u7684\u662f\u83b7\u53d6\u4e00\u4e2a\u51fd\u6570 <code>fn<\/code>\uff0c\u5e76\u521b\u5efa\u4e00\u4e2a\u6307\u5411\u8be5\u51fd\u6570\u7684\u9759\u6001\u51fd\u6570\u6307\u9488\u3002\u8fd9\u4e2a\u51fd\u6570\u6307\u9488\u4f1a\u88ab\u8d4b\u4e88\u4e00\u4e2a\u57fa\u4e8e <code>fn<\/code> \u548c <code>id<\/code> \u7684\u552f\u4e00\u540d\u79f0\uff0c\u5e76\u88ab\u6807\u8bb0\u4e3a\u201c\u5df2\u4f7f\u7528\u201d\uff08<code>__used<\/code>\uff09\uff0c\u4ee5\u9632\u6b62\u88ab\u7f16\u8bd1\u5668\u4f18\u5316\u6389\u3002\u6700\u5173\u952e\u7684\u662f\uff0c\u8fd9\u4e2a\u51fd\u6570\u6307\u9488\u4f1a\u88ab\u653e\u7f6e\u5728\u4e00\u4e2a\u7279\u6b8a\u7684\u5185\u5b58\u6bb5\u4e2d\uff0c\u6bb5\u540d\u7531 <code>__sec<\/code> \u53c2\u6570\u548c <code>.init<\/code> \u540e\u7f00\u6784\u6210\uff08\u4f8b\u5982 <code>\".initcall0.init\"<\/code>\u3001<code>\".initcall1.init\"<\/code> \u7b49\uff09<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">module_exit\u539f\u7406\u5206\u6790<\/h3>\n\n\n\n<p>\u67e5\u770b<code>Linux 5.4<\/code>\u6e90\u7801\u53ef\u4ee5\u770b\u5230<code>module_exit<\/code>\u5b8f\u5bf9\u5e94\u6e90\u4ee3\u7801\uff0c\u53ef\u4ee5\u770b\u5230<code>module_exit<\/code>\u5bf9\u5e94\u7740\u4e24\u79cd\u4e0d\u540c\u7684\u5b9e\u73b0\u3002<code>MODULE<\/code> \u5b8f\u672c\u8eab\u5e76\u4e0d\u662f\u76f4\u63a5\u5b9a\u4e49\u67d0\u4e2a\u503c\u6216\u51fd\u6570\uff0c\u800c\u662f\u4e00\u4e2a<strong>\u7f16\u8bd1\u65f6\u5f00\u5173<\/strong>\u3002\u7f16\u8bd1\u5668\u901a\u5e38\u4f1a\u901a\u8fc7\u547d\u4ee4\u884c\u53c2\u6570\uff08\u4f8b\u5982 <code>-DMODULE<\/code>\uff09\u6765\u5b9a\u4e49 <code>MODULE<\/code> \u5b8f<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/linux-5.4\/include\/linux\/module.h\n#ifndef MODULE\n#define module_exit(x)    __exitcall(x);\n#else\n#define module_exit(exitfn)                    \\\n    static inline exitcall_t __maybe_unused __exittest(void)        \\\n    { return exitfn; }                    \\\n    void cleanup_module(void) __copy(exitfn) __attribute__((alias(#exitfn)));\n#endif<\/code><\/pre>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>MODULE\u5b8f\u5f00\u542f\u65f6\u7684\u903b\u8f91<\/strong> <strong>\u6e90\u4ee3\u7801\uff1a<\/strong> <code>\/\/linux-5.4\/include\/linux\/module.h #define module_exit(exitfn) \\ static inline exitcall_t __maybe_unused __exittest(void) \\ { return exitfn; } \\ void cleanup_module(void) __copy(exitfn) __attribute__((alias(#exitfn)));<\/code> <strong>\u6838\u5fc3\u601d\u60f3\uff1a<\/strong> \u5f53\u6587\u4ef6\u88ab\u7f16\u8bd1\u6210\u5185\u6838\u6a21\u5757\u4e5f\u5c31\u662f<code>.KO<\/code>\u6587\u4ef6\u65f6\uff0c\u4f1a\u6709\u660e\u786e\u7684 <code>cleanup_module<\/code> \u51fd\u6570\u4f5c\u4e3a\u5378\u8f7d\u65f6\u7684\u5165\u53e3\u70b9\u3002\u5c06 <code>cleanup_module<\/code> \u51fd\u6570\u8bbe\u7f6e\u4e3a <code>exitfn<\/code> \u51fd\u6570\u7684\u4e00\u4e2a\u522b\u540d\u3002<code>#exitfn<\/code> \u4f7f\u7528\u9884\u5904\u7406\u5668\u7684\u5b57\u7b26\u4e32\u5316\u64cd\u4f5c\u7b26\u5c06 <code>exitfn<\/code> \u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u5b57\u9762\u91cf\u3002\u56e0\u6b64\uff0c\u5f53\u5185\u6838\u8c03\u7528 <code>cleanup_module<\/code> \u65f6\uff0c\u5b9e\u9645\u4e0a\u4f1a\u76f4\u63a5\u6267\u884c\u5f00\u53d1\u8005\u63d0\u4f9b\u7684 <code>exitfn<\/code> \u51fd\u6570\u7684\u4ee3\u7801<\/li>\n\n\n\n<li><strong>MODULE\u5b8f\u5173\u95ed\u65f6\u7684\u903b\u8f91<\/strong> <strong>\u6e90\u4ee3\u7801\uff1a<\/strong> <code>\/\/linux-5.4\/include\/linux\/module.h #define __exit_call __used __section(.exitcall.exit) #define __exitcall(fn) \\ static exitcall_t __exitcall_##fn __exit_call = fn #define module_exit(x) __exitcall(x)<\/code> <strong>\u6838\u5fc3\u601d\u60f3\uff1a<\/strong> \u7efc\u4e0a\u6240\u8ff0\uff0c<code>__exitcall<\/code> \u5b8f\u7684\u76ee\u7684\u662f\u63d0\u4f9b\u4e00\u79cd\u673a\u5236\uff0c\u7528\u4e8e\u6ce8\u518c\u5728\u7a0b\u5e8f\u6216\u6a21\u5757\u9000\u51fa\u65f6\u9700\u8981\u8c03\u7528\u7684\u51fd\u6570\u3002\u5b83\u901a\u8fc7\u521b\u5efa\u4e00\u4e2a\u9759\u6001\u7684\u3001\u5177\u6709\u552f\u4e00\u540d\u79f0\u7684\u51fd\u6570\u6307\u9488\uff0c\u5e76\u5c06\u5176\u521d\u59cb\u5316\u4e3a\u6307\u5b9a\u7684\u51fd\u6570\u5730\u5740\u3002\u901a\u8fc7 <code>.exitcall.exit<\/code> \u6bb5\u5c5e\u6027\uff0c\u6240\u6709\u8fd9\u6837\u5b9a\u4e49\u7684\u51fd\u6570\u6307\u9488\u4f1a\u88ab\u94fe\u63a5\u5668\u6536\u96c6\u5230\u5185\u5b58\u4e2d\u7684\u4e00\u4e2a\u7279\u5b9a\u533a\u57df\u3002\u4e4b\u540e\uff0c\u5728\u9000\u51fa\u6d41\u7a0b\u4e2d\uff0c\u7cfb\u7edf\u53ef\u4ee5\u904d\u5386\u8fd9\u4e2a\u533a\u57df\uff0c\u5e76\u8c03\u7528\u6240\u6709\u6ce8\u518c\u7684\u9000\u51fa\u51fd\u6570\u3002<\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\">\u53c2\u8003\u6587\u6863<\/h2>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p><a href=\"https:\/\/github.com\/torvalds\/linux\/releases\/tag\/v5.4\">Release v5.4 \u00b7 torvalds\/linux<\/a><\/p>\n\n\n\n<p><a href=\"https:\/\/www.archlinuxcn.org\/\">Arch Linux<\/a><\/p>\n<\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>\u6a21\u5757\u5165\u53e3\u548c\u51fa\u53e3\u6ce8\u518cAPI \u5728\u5404\u4e2alinux\u5b50\u6a21\u5757\u4e2d\u7ecf\u5e38\u53ef\u4ee5\u770b\u5230\u7684module_init\u548cmodule_exit [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_import_markdown_pro_load_document_selector":0,"_import_markdown_pro_submit_text_textarea":"","footnotes":""},"categories":[6],"tags":[],"class_list":["post-418","post","type-post","status-publish","format-standard","hentry","category-linux-knowledge"],"_links":{"self":[{"href":"https:\/\/www.garfield-blog.top\/index.php?rest_route=\/wp\/v2\/posts\/418","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.garfield-blog.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.garfield-blog.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.garfield-blog.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.garfield-blog.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=418"}],"version-history":[{"count":2,"href":"https:\/\/www.garfield-blog.top\/index.php?rest_route=\/wp\/v2\/posts\/418\/revisions"}],"predecessor-version":[{"id":424,"href":"https:\/\/www.garfield-blog.top\/index.php?rest_route=\/wp\/v2\/posts\/418\/revisions\/424"}],"wp:attachment":[{"href":"https:\/\/www.garfield-blog.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=418"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.garfield-blog.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=418"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.garfield-blog.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=418"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}