mm/kmsan/instrumentation.c
Source file repositories/reference/linux-study-clean/mm/kmsan/instrumentation.c
File Facts
- System
- Linux kernel
- Corpus path
mm/kmsan/instrumentation.c- Extension
.c- Size
- 10264 bytes
- Lines
- 331
- 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.
- Defines or uses C structs; map object ownership, embedded links, reference counts, and lock ownership.
Dependency Surface
kmsan.hlinux/gfp.hlinux/kmsan.hlinux/kmsan_string.hlinux/mm.hlinux/uaccess.h
Detected Declarations
function Copyrightfunction get_shadow_origin_ptrfunction __msan_metadata_ptr_for_load_nfunction __msan_metadata_ptr_for_store_nfunction __msan_instrument_asm_storefunction clwbfunction get_param0_metadatafunction set_retval_metadatafunction __msan_chain_originfunction __msan_poison_allocafunction __msan_unpoison_allocafunction __msan_warningexport __msan_metadata_ptr_for_load_nexport __msan_metadata_ptr_for_store_nexport __msan_instrument_asm_storeexport __msan_memmoveexport __msan_memcpyexport __msan_memsetexport __msan_chain_originexport __msan_poison_allocaexport __msan_unpoison_allocaexport __msan_warningexport __msan_get_context_state
Annotated Snippet
// SPDX-License-Identifier: GPL-2.0
/*
* KMSAN compiler API.
*
* This file implements __msan_XXX hooks that Clang inserts into the code
* compiled with -fsanitize=kernel-memory.
* See Documentation/dev-tools/kmsan.rst for more information on how KMSAN
* instrumentation works.
*
* Copyright (C) 2017-2022 Google LLC
* Author: Alexander Potapenko <glider@google.com>
*
*/
#include "kmsan.h"
#include <linux/gfp.h>
#include <linux/kmsan.h>
#include <linux/kmsan_string.h>
#include <linux/mm.h>
#include <linux/uaccess.h>
static inline bool is_bad_asm_addr(void *addr, uintptr_t size, bool is_store)
{
if (IS_ENABLED(CONFIG_ARCH_HAS_NON_OVERLAPPING_ADDRESS_SPACE) &&
(u64)addr < TASK_SIZE)
return true;
if (!kmsan_get_metadata(addr, KMSAN_META_SHADOW))
return true;
return false;
}
static inline struct shadow_origin_ptr
get_shadow_origin_ptr(void *addr, u64 size, bool store)
{
unsigned long ua_flags = user_access_save();
struct shadow_origin_ptr ret;
ret = kmsan_get_shadow_origin_ptr(addr, size, store);
user_access_restore(ua_flags);
return ret;
}
/*
* KMSAN instrumentation functions follow. They are not declared elsewhere in
* the kernel code, so they are preceded by prototypes, to silence
* -Wmissing-prototypes warnings.
*/
/* Get shadow and origin pointers for a memory load with non-standard size. */
struct shadow_origin_ptr __msan_metadata_ptr_for_load_n(void *addr,
uintptr_t size);
struct shadow_origin_ptr __msan_metadata_ptr_for_load_n(void *addr,
uintptr_t size)
{
return get_shadow_origin_ptr(addr, size, /*store*/ false);
}
EXPORT_SYMBOL(__msan_metadata_ptr_for_load_n);
/* Get shadow and origin pointers for a memory store with non-standard size. */
struct shadow_origin_ptr __msan_metadata_ptr_for_store_n(void *addr,
uintptr_t size);
struct shadow_origin_ptr __msan_metadata_ptr_for_store_n(void *addr,
uintptr_t size)
{
return get_shadow_origin_ptr(addr, size, /*store*/ true);
}
EXPORT_SYMBOL(__msan_metadata_ptr_for_store_n);
/*
* Declare functions that obtain shadow/origin pointers for loads and stores
* with fixed size.
*/
#define DECLARE_METADATA_PTR_GETTER(size) \
struct shadow_origin_ptr __msan_metadata_ptr_for_load_##size( \
void *addr); \
struct shadow_origin_ptr __msan_metadata_ptr_for_load_##size( \
void *addr) \
{ \
return get_shadow_origin_ptr(addr, size, /*store*/ false); \
} \
EXPORT_SYMBOL(__msan_metadata_ptr_for_load_##size); \
struct shadow_origin_ptr __msan_metadata_ptr_for_store_##size( \
void *addr); \
struct shadow_origin_ptr __msan_metadata_ptr_for_store_##size( \
void *addr) \
{ \
return get_shadow_origin_ptr(addr, size, /*store*/ true); \
} \
EXPORT_SYMBOL(__msan_metadata_ptr_for_store_##size)
Annotation
- Immediate include surface: `kmsan.h`, `linux/gfp.h`, `linux/kmsan.h`, `linux/kmsan_string.h`, `linux/mm.h`, `linux/uaccess.h`.
- Detected declarations: `function Copyright`, `function get_shadow_origin_ptr`, `function __msan_metadata_ptr_for_load_n`, `function __msan_metadata_ptr_for_store_n`, `function __msan_instrument_asm_store`, `function clwb`, `function get_param0_metadata`, `function set_retval_metadata`, `function __msan_chain_origin`, `function __msan_poison_alloca`.
- Atlas domain: Core OS / Memory Management.
- Implementation status: integration implementation candidate.
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.