drivers/base/memory.c
Source file repositories/reference/linux-study-clean/drivers/base/memory.c
File Facts
- System
- Linux kernel
- Corpus path
drivers/base/memory.c- Extension
.c- Size
- 33182 bytes
- Lines
- 1246
- Domain
- Driver Families
- Bucket
- drivers/base
- Inferred role
- Driver Families: operation-table or driver-model contract
- Status
- pattern implementation candidate
Why This File Exists
Repeatable hardware-adapter layer. Deep compatibility for every driver is out of scope; this atlas records patterns, probe lifecycles, bus glue, IRQ/DMA usage, and links back to core abstractions.
- Repeatable hardware-adapter layer. Deep compatibility for every driver is out of scope; this atlas records patterns, probe lifecycles, bus glue, IRQ/DMA usage, and links back to core abstractions.
- Defines an operation table; this is where Linux turns generic core objects into subsystem-specific behavior.
- 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
linux/module.hlinux/init.hlinux/topology.hlinux/capability.hlinux/device.hlinux/memory.hlinux/memory_hotplug.hlinux/mm.hlinux/stat.hlinux/slab.hlinux/xarray.hlinux/export.hlinux/atomic.hlinux/uaccess.hlinux/kexec.h
Detected Declarations
struct for_each_memory_block_cb_datafunction mhp_online_type_from_strfunction register_memory_notifierfunction unregister_memory_notifierfunction memory_block_releasefunction memory_block_advise_max_sizefunction memory_block_advised_max_sizefunction memory_block_size_bytesfunction phys_index_showfunction removable_showfunction state_showfunction memory_notifyfunction memblk_nr_poisonfunction memory_block_onlinefunction memory_block_offlinefunction memory_block_actionfunction memory_block_change_statefunction memory_subsys_onlinefunction memory_subsys_offlinefunction state_storefunction phys_device_showfunction print_allowed_zonefunction valid_zones_showfunction sizefunction auto_online_blocks_showfunction auto_online_blocks_storefunction crash_hotplug_showfunction probe_storefunction soft_offline_page_storefunction hard_offline_page_storefunction arch_get_memory_phys_devicefunction __add_memory_blockfunction memory_block_add_nid_earlyfunction add_memory_blockfunction remove_memory_blockfunction create_memory_block_devicesfunction remove_memory_block_devicesfunction memory_dev_initfunction funcfunction for_each_memory_block_cbfunction funcfunction memory_group_registerfunction memory_group_register_staticfunction memory_group_register_dynamicfunction memory_group_unregisterfunction walk_dynamic_memory_groupsfunction xa_for_each_markedfunction memblk_nr_poison_inc
Annotated Snippet
static const struct bus_type memory_subsys = {
.name = MEMORY_CLASS_NAME,
.dev_name = MEMORY_CLASS_NAME,
.online = memory_subsys_online,
.offline = memory_subsys_offline,
};
/*
* Memory blocks are cached in a local radix tree to avoid
* a costly linear search for the corresponding device on
* the subsystem bus.
*/
static DEFINE_XARRAY(memory_blocks);
/*
* Memory groups, indexed by memory group id (mgid).
*/
static DEFINE_XARRAY_FLAGS(memory_groups, XA_FLAGS_ALLOC);
#define MEMORY_GROUP_MARK_DYNAMIC XA_MARK_1
static BLOCKING_NOTIFIER_HEAD(memory_chain);
int register_memory_notifier(struct notifier_block *nb)
{
return blocking_notifier_chain_register(&memory_chain, nb);
}
EXPORT_SYMBOL(register_memory_notifier);
void unregister_memory_notifier(struct notifier_block *nb)
{
blocking_notifier_chain_unregister(&memory_chain, nb);
}
EXPORT_SYMBOL(unregister_memory_notifier);
static void memory_block_release(struct device *dev)
{
struct memory_block *mem = to_memory_block(dev);
/* Verify that the altmap is freed */
WARN_ON(mem->altmap);
kfree(mem);
}
/* Max block size to be set by memory_block_advise_max_size */
static unsigned long memory_block_advised_size;
static bool memory_block_advised_size_queried;
/**
* memory_block_advise_max_size() - advise memory hotplug on the max suggested
* block size, usually for alignment.
* @size: suggestion for maximum block size. must be aligned on power of 2.
*
* Early boot software (pre-allocator init) may advise archs on the max block
* size. This value can only decrease after initialization, as the intent is
* to identify the largest supported alignment for all sources.
*
* Use of this value is arch-defined, as is min/max block size.
*
* Return: 0 on success
* -EINVAL if size is 0 or not pow2 aligned
* -EBUSY if value has already been probed
*/
int __init memory_block_advise_max_size(unsigned long size)
{
if (!size || !is_power_of_2(size))
return -EINVAL;
if (memory_block_advised_size_queried)
return -EBUSY;
if (memory_block_advised_size)
memory_block_advised_size = min(memory_block_advised_size, size);
else
memory_block_advised_size = size;
return 0;
}
/**
* memory_block_advised_max_size() - query advised max hotplug block size.
*
* After the first call, the value can never change. Callers looking for the
* actual block size should use memory_block_size_bytes. This interface is
* intended for use by arch-init when initializing the hotplug block size.
*
* Return: advised size in bytes, or 0 if never set.
*/
unsigned long memory_block_advised_max_size(void)
{
memory_block_advised_size_queried = true;
Annotation
- Immediate include surface: `linux/module.h`, `linux/init.h`, `linux/topology.h`, `linux/capability.h`, `linux/device.h`, `linux/memory.h`, `linux/memory_hotplug.h`, `linux/mm.h`.
- Detected declarations: `struct for_each_memory_block_cb_data`, `function mhp_online_type_from_str`, `function register_memory_notifier`, `function unregister_memory_notifier`, `function memory_block_release`, `function memory_block_advise_max_size`, `function memory_block_advised_max_size`, `function memory_block_size_bytes`, `function phys_index_show`, `function removable_show`.
- Atlas domain: Driver Families / drivers/base.
- Implementation status: pattern implementation candidate.
- 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.