Documentation/translations/zh_CN/kernel-hacking/hacking.rst
Source file repositories/reference/linux-study-clean/Documentation/translations/zh_CN/kernel-hacking/hacking.rst
File Facts
- System
- Linux kernel
- Corpus path
Documentation/translations/zh_CN/kernel-hacking/hacking.rst- Extension
.rst- Size
- 29630 bytes
- Lines
- 708
- Domain
- Support Tooling And Documentation
- Bucket
- Documentation
- Inferred role
- Support Tooling And Documentation: operation-table or driver-model contract
- Status
- pattern implementation candidate
Why This File Exists
Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.
- Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.
- Defines an operation table; this is where Linux turns generic core objects into subsystem-specific behavior.
- Touches user memory; correctness depends on fault-safe copying and privilege boundary handling.
- Uses kernel synchronization; read lock ordering, sleepability, and interrupt context assumptions before translating.
- Allocates kernel memory; connect allocation flags and lifetime to context constraints.
- Defines or uses C structs; map object ownership, embedded links, reference counts, and lock ownership.
Dependency Surface
- No C-style include directives detected by the generator.
Detected Declarations
function sys_mycall
Annotated Snippet
:c:type:`struct file_operations <file_operations>` 结构体中,此字段应设置为
宏 ``THIS_MODULE`` 。
等待队列 ``include/linux/wait.h``
====================================
**[睡眠]**
等待队列用于等待某程序在条件为真时唤醒另一程序。必须小心使用,以确保没有竞争
条件。先声明一个 :c:type:`wait_queue_head_t` ,然后对希望等待该条件的进程声明
一个关于它们自己的 :c:type:`wait_queue_entry_t` ,并将其放入队列中。
声明
-----
使用 :c:func:`DECLARE_WAIT_QUEUE_HEAD()` 宏声明一个 ``wait_queue_head_t`` ,
或者在初始化代码中使用 :c:func:`init_waitqueue_head()` 程序。
排队
-----
将自己放在等待队列中相当复杂,因为你必须在检查条件之前将自己放入队列中。有一
个宏可以来执行此操作: :c:func:`wait_event_interruptible()`
( ``include/linux/wait.h`` )第一个参数是等待队列头,第二个参数是计算的表达
式;当该表达式为true时宏返回0,或者在接收到信号时返回 ``-ERESTARTSYS`` 。
:c:func:`wait_event()` 版本会忽略信号。
唤醒排队任务
-------------
调用 :c:func:`wake_up()` ( ``include/linux/wait.h`` ),它将唤醒队列中的所有
进程。例外情况:如果有一个进程设置了 ``TASK_EXCLUSIVE`` ,队列的其余部分将不
会被唤醒。这个基本函数的其他变体也可以在同一个头文件中使用。
原子操作
=========
某些操作在所有平台上都有保证。第一类为操作 :c:type:`atomic_t`
( ``include/asm/atomic.h`` )的函数;它包含一个有符号整数(至少32位长),
您必须使用这些函数来操作或读取 :c:type:`atomic_t` 变量。
:c:func:`atomic_read()` 和 :c:func:`atomic_set()` 获取并设置计数器,还有
:c:func:`atomic_add()` ,:c:func:`atomic_sub()` ,:c:func:`atomic_inc()` ,
:c:func:`atomic_dec()` 和 :c:func:`atomic_dec_and_test()` (如果递减为零,
则返回true)。
是的。它在原子变量为零时返回true(即!=0)。
请注意,这些函数比普通的算术运算速度慢,因此不应过度使用。
第二类原子操作是在 ``unsigned long`` ( ``include/linux/bitops.h`` )上的
原子位操作。这些操作通常采用指向位模式(bit pattern)的指针,第0位是最低有效
位。:c:func:`set_bit()`,:c:func:`clear_bit()` 和 :c:func:`change_bit()` 设置、
清除和更改给定位。:c:func:`test_and_set_bit()` ,:c:func:`test_and_clear_bit()`
和 :c:func:`test_and_change_bit()` 执行相同的操作,但如果之前设置了位,则返回
true;这些对于原子设置标志特别有用。
可以使用大于 ``BITS_PER_LONG`` 位的位索引调用这些操作。但结果在大端序平台上
不太正常,所以最好不要这样做。
符号
=====
在内核内部,正常的链接规则仍然适用(即除非用static关键字将符号声明为文件范围,
否则它可以在内核中的任何位置使用)。但是对于模块,会保留一个特殊可导出符号表,
该表将入口点限制为内核内部。模块也可以导出符号。
:c:func:`EXPORT_SYMBOL()`
-------------------------
定义于 ``include/linux/export.h``
Annotation
- Detected declarations: `function sys_mycall`.
- Atlas domain: Support Tooling And Documentation / Documentation.
- Implementation status: pattern implementation candidate.
- This snippet crosses the user/kernel memory boundary; validate fault handling and access checks before translating the pattern.
- Synchronization appears in or near this file; preserve lock ordering, sleepability, and interrupt-context constraints.
Implementation Notes
- This generated page is the file-by-file coverage layer; curated subsystem chapters should link here when they synthesize a multi-file control flow.
- Core OS pages should be promoted from atlas-only to deep-reviewed when they explain data structures, invariants, locking, lifecycle, and C implementation snippets.
- Driver-family pages are intentionally pattern-oriented unless they are part of the selected PCIe/NVMe representative device path.