mm/kfence/core.c
Source file repositories/reference/linux-study-clean/mm/kfence/core.c
File Facts
- System
- Linux kernel
- Corpus path
mm/kfence/core.c- Extension
.c- Size
- 40934 bytes
- Lines
- 1350
- 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/atomic.hlinux/bug.hlinux/debugfs.hlinux/hash.hlinux/irq_work.hlinux/jhash.hlinux/kasan-enabled.hlinux/kcsan-checks.hlinux/kfence.hlinux/kmemleak.hlinux/list.hlinux/lockdep.hlinux/log2.hlinux/memblock.hlinux/moduleparam.hlinux/nodemask.hlinux/notifier.hlinux/panic_notifier.hlinux/random.hlinux/rcupdate.hlinux/reboot.hlinux/sched/clock.hlinux/seq_file.hlinux/slab.hlinux/spinlock.hlinux/string.hasm/kfence.hkfence.h
Detected Declarations
enum kfence_counter_idfunction param_set_sample_intervalfunction param_get_sample_intervalfunction should_skip_coveredfunction get_alloc_stack_hashfunction Addsfunction containedfunction kfence_protectfunction kfence_unprotectfunction metadata_to_pageaddrfunction kfence_obj_allocatedfunction metadata_update_statefunction check_canary_bytefunction set_canaryfunction check_canaryfunction kfence_guarded_freefunction rcu_guarded_freefunction kfence_init_poolfunction kfence_init_pool_earlyfunction stats_showfunction start_objectfunction stop_objectfunction show_objectfunction kfence_debugfs_initfunction kfence_check_all_canaryfunction kfence_check_canary_callbackfunction kfence_reboot_callbackfunction wake_up_kfence_timerfunction queuefunction kfence_alloc_pool_and_metadatafunction kfence_init_enablefunction kfence_initfunction kfence_init_latefunction kfence_enable_latefunction kfence_shutdown_cachefunction propertiesfunction waitqueue_activefunction kfence_ksizefunction __kfence_freefunction kfence_handle_page_faultfunction __kfence_allocexport kfence_sample_intervalexport __kfence_pool
Annotated Snippet
if (unlikely(__cond)) { \
WRITE_ONCE(kfence_enabled, false); \
disabled_by_warn = true; \
} \
__cond; \
})
/* === Data ================================================================= */
bool kfence_enabled __read_mostly;
static bool disabled_by_warn __read_mostly;
unsigned long kfence_sample_interval __read_mostly = CONFIG_KFENCE_SAMPLE_INTERVAL;
EXPORT_SYMBOL_GPL(kfence_sample_interval); /* Export for test modules. */
#ifdef MODULE_PARAM_PREFIX
#undef MODULE_PARAM_PREFIX
#endif
#define MODULE_PARAM_PREFIX "kfence."
static int kfence_enable_late(void);
static int param_set_sample_interval(const char *val, const struct kernel_param *kp)
{
unsigned long num;
int ret = kstrtoul(val, 0, &num);
if (ret < 0)
return ret;
/* Using 0 to indicate KFENCE is disabled. */
if (!num && READ_ONCE(kfence_enabled)) {
pr_info("disabled\n");
WRITE_ONCE(kfence_enabled, false);
}
if (num && kasan_hw_tags_enabled()) {
pr_info("disabled as KASAN HW tags are enabled\n");
return -EINVAL;
}
*((unsigned long *)kp->arg) = num;
if (num && !READ_ONCE(kfence_enabled) && system_state != SYSTEM_BOOTING)
return disabled_by_warn ? -EINVAL : kfence_enable_late();
return 0;
}
static int param_get_sample_interval(char *buffer, const struct kernel_param *kp)
{
if (!READ_ONCE(kfence_enabled))
return sprintf(buffer, "0\n");
return param_get_ulong(buffer, kp);
}
static const struct kernel_param_ops sample_interval_param_ops = {
.set = param_set_sample_interval,
.get = param_get_sample_interval,
};
module_param_cb(sample_interval, &sample_interval_param_ops, &kfence_sample_interval, 0600);
/* Pool usage% threshold when currently covered allocations are skipped. */
static unsigned long kfence_skip_covered_thresh __read_mostly = 75;
module_param_named(skip_covered_thresh, kfence_skip_covered_thresh, ulong, 0644);
/* Allocation burst count: number of excess KFENCE allocations per sample. */
static unsigned int kfence_burst __read_mostly;
module_param_named(burst, kfence_burst, uint, 0644);
/* If true, use a deferrable timer. */
static bool kfence_deferrable __read_mostly = IS_ENABLED(CONFIG_KFENCE_DEFERRABLE);
module_param_named(deferrable, kfence_deferrable, bool, 0444);
/* If true, check all canary bytes on panic. */
static bool kfence_check_on_panic __read_mostly;
module_param_named(check_on_panic, kfence_check_on_panic, bool, 0444);
/* The pool of pages used for guard pages and objects. */
char *__kfence_pool __read_mostly;
EXPORT_SYMBOL(__kfence_pool); /* Export for test modules. */
/*
* Per-object metadata, with one-to-one mapping of object metadata to
* backing pages (in __kfence_pool).
*/
static_assert(CONFIG_KFENCE_NUM_OBJECTS > 0);
struct kfence_metadata *kfence_metadata __read_mostly;
/*
* If kfence_metadata is not NULL, it may be accessed by kfence_shutdown_cache().
Annotation
- Immediate include surface: `linux/atomic.h`, `linux/bug.h`, `linux/debugfs.h`, `linux/hash.h`, `linux/irq_work.h`, `linux/jhash.h`, `linux/kasan-enabled.h`, `linux/kcsan-checks.h`.
- Detected declarations: `enum kfence_counter_id`, `function param_set_sample_interval`, `function param_get_sample_interval`, `function should_skip_covered`, `function get_alloc_stack_hash`, `function Adds`, `function contained`, `function kfence_protect`, `function kfence_unprotect`, `function metadata_to_pageaddr`.
- 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.