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.

Dependency Surface

Detected Declarations

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

Implementation Notes