mm/memremap.c
Source file repositories/reference/linux-study-clean/mm/memremap.c
File Facts
- System
- Linux kernel
- Corpus path
mm/memremap.c- Extension
.c- Size
- 14834 bytes
- Lines
- 529
- Domain
- Core OS
- Bucket
- Memory Management
- Inferred role
- Core OS: exported/initcall integration point
- Status
- integration implementation candidate
Why This File Exists
Core operating-system implementation surface: boot, tasks, memory, VFS, syscall-facing interfaces, synchronization, credentials, and isolation.
- Core operating-system implementation surface: boot, tasks, memory, VFS, syscall-facing interfaces, synchronization, credentials, and isolation.
- Exports symbols or registers init work; inspect boot/module ordering and who consumes the exported contract.
- 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/device.hlinux/io.hlinux/kasan.hlinux/memory_hotplug.hlinux/memremap.hlinux/swap.hlinux/mm.hlinux/mmzone.hlinux/swapops.hlinux/types.hlinux/wait_bit.hlinux/xarray.hinternal.h
Detected Declarations
function memremapfunction pgmap_array_deletefunction pfn_firstfunction pgmap_pfn_validfunction pfn_endfunction pfn_lenfunction pageunmap_rangefunction memunmap_pagesfunction devm_memremap_pages_releasefunction dev_pagemap_percpu_releasefunction pagemap_rangefunction add_pagesfunction memunmap_pagesfunction devm_memremap_pages_releasefunction devm_memunmap_pagesfunction get_dev_pagemapfunction free_zone_device_foliofunction zone_device_page_initexport memremap_compat_alignexport memunmap_pagesexport memremap_pagesexport devm_memremap_pagesexport devm_memunmap_pagesexport get_dev_pagemapexport zone_device_page_init
Annotated Snippet
if (error) {
mem_hotplug_done();
goto err_kasan;
}
error = arch_add_memory(nid, range->start, range_len(range),
params);
}
if (!error) {
struct zone *zone;
zone = &NODE_DATA(nid)->node_zones[ZONE_DEVICE];
move_pfn_range_to_zone(zone, PHYS_PFN(range->start),
PHYS_PFN(range_len(range)), params->altmap,
MIGRATE_MOVABLE, false);
}
mem_hotplug_done();
if (error)
goto err_add_memory;
/*
* Initialization of the pages has been deferred until now in order
* to allow us to do the work while not holding the hotplug lock.
*/
memmap_init_zone_device(&NODE_DATA(nid)->node_zones[ZONE_DEVICE],
PHYS_PFN(range->start),
PHYS_PFN(range_len(range)), pgmap);
if (pgmap->type != MEMORY_DEVICE_PRIVATE &&
pgmap->type != MEMORY_DEVICE_COHERENT)
percpu_ref_get_many(&pgmap->ref, pfn_len(pgmap, range_id));
return 0;
err_add_memory:
if (!is_private)
kasan_remove_zero_shadow(__va(range->start), range_len(range));
err_kasan:
pfnmap_untrack(PHYS_PFN(range->start), range_len(range));
err_pfn_remap:
pgmap_array_delete(range);
return error;
}
/*
* Not device managed version of devm_memremap_pages, undone by
* memunmap_pages(). Please use devm_memremap_pages if you have a struct
* device available.
*/
void *memremap_pages(struct dev_pagemap *pgmap, int nid)
{
struct mhp_params params = {
.altmap = pgmap_altmap(pgmap),
.pgmap = pgmap,
.pgprot = PAGE_KERNEL,
};
const int nr_range = pgmap->nr_range;
int error, i;
if (WARN_ONCE(!nr_range, "nr_range must be specified\n"))
return ERR_PTR(-EINVAL);
if (WARN_ONCE(pgmap->vmemmap_shift > MAX_FOLIO_ORDER,
"requested folio size unsupported\n"))
return ERR_PTR(-EINVAL);
switch (pgmap->type) {
case MEMORY_DEVICE_PRIVATE:
if (!IS_ENABLED(CONFIG_DEVICE_PRIVATE)) {
WARN(1, "Device private memory not supported\n");
return ERR_PTR(-EINVAL);
}
if (!pgmap->ops || !pgmap->ops->migrate_to_ram) {
WARN(1, "Missing migrate_to_ram method\n");
return ERR_PTR(-EINVAL);
}
if (!pgmap->ops->folio_free) {
WARN(1, "Missing folio_free method\n");
return ERR_PTR(-EINVAL);
}
if (!pgmap->owner) {
WARN(1, "Missing owner\n");
return ERR_PTR(-EINVAL);
}
break;
case MEMORY_DEVICE_COHERENT:
if (!pgmap->ops->folio_free) {
WARN(1, "Missing folio_free method\n");
return ERR_PTR(-EINVAL);
}
Annotation
- Immediate include surface: `linux/device.h`, `linux/io.h`, `linux/kasan.h`, `linux/memory_hotplug.h`, `linux/memremap.h`, `linux/swap.h`, `linux/mm.h`, `linux/mmzone.h`.
- Detected declarations: `function memremap`, `function pgmap_array_delete`, `function pfn_first`, `function pgmap_pfn_valid`, `function pfn_end`, `function pfn_len`, `function pageunmap_range`, `function memunmap_pages`, `function devm_memremap_pages_release`, `function dev_pagemap_percpu_release`.
- Atlas domain: Core OS / Memory Management.
- Implementation status: integration 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.