Documentation/translations/zh_CN/core-api/memory-hotplug.rst
Source file repositories/reference/linux-study-clean/Documentation/translations/zh_CN/core-api/memory-hotplug.rst
File Facts
- System
- Linux kernel
- Corpus path
Documentation/translations/zh_CN/core-api/memory-hotplug.rst- Extension
.rst- Size
- 4342 bytes
- Lines
- 120
- Domain
- Support Tooling And Documentation
- Bucket
- Documentation
- Inferred role
- Support Tooling And Documentation: documentation
- Status
- atlas-only
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 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
struct memory_notify
Annotated Snippet
struct memory_notify {
unsigned long start_pfn;
unsigned long nr_pages;
int status_change_nid;
}
- start_pfn是在线/离线内存的start_pfn。
- nr_pages是在线/离线内存的页数。
- status_change_nid是当nodemask的N_MEMORY被(将)设置/清除时设置的节点id。这
意味着一个新的(没上线的)节点通过联机获得新的内存,而一个节点失去了所有的内
存。如果这个值为-1,那么nodemask的状态就不会改变。
如果 status_changed_nid* >= 0,回调应该在必要时为节点创建/丢弃结构体。
回调程序应返回 ``include/linux/notifier.h`` 中定义的NOTIFY_DONE, NOTIFY_OK,
NOTIFY_BAD, NOTIFY_STOP中的一个值。
NOTIFY_DONE和NOTIFY_OK对进一步处理没有影响。
NOTIFY_BAD是作为对MEM_GOING_ONLINE、MEM_GOING_OFFLINE、MEM_ONLINE或MEM_OFFLINE
动作的回应,用于取消热插拔。它停止对通知队列的进一步处理。
NOTIFY_STOP停止对通知队列的进一步处理。
内部锁
======
当添加/删除使用内存块设备(即普通RAM)的内存时,device_hotplug_lock应该被保持
为:
- 针对在线/离线请求进行同步(例如,通过sysfs)。这样一来,内存块设备只有在内存
被完全添加后才能被用户空间访问(.online/.state属性)。而在删除内存时,我们知
道没有人在临界区。
- 与CPU热拔插或类似操作同步(例如ACPI和PPC相关操作)
特别是,在添加内存和用户空间试图以比预期更快的速度上线该内存时,有可能出现锁反转,
使用device_hotplug_lock可以避免此情况:
- device_online()将首先接受device_lock(),然后是mem_hotplug_lock。
- add_memory_resource()将首先使用mem_hotplug_lock,然后是device_lock()(在创
建设备时,在bus_add_device()期间)。
由于在使用device_lock()之前,设备对用户空间是可见的,这可能导致锁的反转。
内存的上线/下线应该通过device_online()/device_offline()完成————确保它与通过
sysfs进行的操作正确同步。建议持有device_hotplug_lock(例如,保护online_type)。
当添加/删除/上线/下线内存或者添加/删除异构或设备内存时,我们应该始终持有写模式的
mem_hotplug_lock,以序列化内存热插拔(例如访问全局/区域变量)。
此外,mem_hotplug_lock(与device_hotplug_lock相反)在读取模式下允许一个相当
有效的get_online_mems/put_online_mems实现,所以访问内存的代码可以防止该内存
消失。
Annotation
- Detected declarations: `struct memory_notify`.
- Atlas domain: Support Tooling And Documentation / Documentation.
- Implementation status: atlas-only.
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.