mm/kasan/report_hw_tags.c
Source file repositories/reference/linux-study-clean/mm/kasan/report_hw_tags.c
File Facts
- System
- Linux kernel
- Corpus path
mm/kasan/report_hw_tags.c- Extension
.c- Size
- 1767 bytes
- Lines
- 72
- Domain
- Core OS
- Bucket
- Memory Management
- Inferred role
- Core OS: implementation source
- Status
- source 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.
- Defines or uses C structs; map object ownership, embedded links, reference counts, and lock ownership.
Dependency Surface
linux/kasan.hlinux/kernel.hlinux/memory.hlinux/mm.hlinux/string.hlinux/types.hkasan.h
Detected Declarations
function Copyrightfunction kasan_get_alloc_sizefunction kasan_metadata_fetch_rowfunction kasan_print_tags
Annotated Snippet
// SPDX-License-Identifier: GPL-2.0
/*
* This file contains hardware tag-based KASAN specific error reporting code.
*
* Copyright (c) 2020 Google, Inc.
* Author: Andrey Konovalov <andreyknvl@google.com>
*/
#include <linux/kasan.h>
#include <linux/kernel.h>
#include <linux/memory.h>
#include <linux/mm.h>
#include <linux/string.h>
#include <linux/types.h>
#include "kasan.h"
const void *kasan_find_first_bad_addr(const void *addr, size_t size)
{
/*
* Hardware Tag-Based KASAN only calls this function for normal memory
* accesses, and thus addr points precisely to the first bad address
* with an invalid (and present) memory tag. Therefore:
* 1. Return the address as is without walking memory tags.
* 2. Skip the addr_has_metadata check.
*/
return kasan_reset_tag(addr);
}
size_t kasan_get_alloc_size(void *object, struct kmem_cache *cache)
{
size_t size = 0;
int i = 0;
u8 memory_tag;
/*
* Skip the addr_has_metadata check, as this function only operates on
* slab memory, which must have metadata.
*/
/*
* The loop below returns 0 for freed objects, for which KASAN cannot
* calculate the allocation size based on the metadata.
*/
while (size < cache->object_size) {
memory_tag = hw_get_mem_tag(object + i * KASAN_GRANULE_SIZE);
if (memory_tag != KASAN_TAG_INVALID)
size += KASAN_GRANULE_SIZE;
else
return size;
i++;
}
return cache->object_size;
}
void kasan_metadata_fetch_row(char *buffer, void *row)
{
int i;
for (i = 0; i < META_BYTES_PER_ROW; i++)
buffer[i] = hw_get_mem_tag(row + i * KASAN_GRANULE_SIZE);
}
void kasan_print_tags(u8 addr_tag, const void *addr)
{
u8 memory_tag = hw_get_mem_tag((void *)addr);
pr_err("Pointer tag: [%02x], memory tag: [%02x]\n",
addr_tag, memory_tag);
}
Annotation
- Immediate include surface: `linux/kasan.h`, `linux/kernel.h`, `linux/memory.h`, `linux/mm.h`, `linux/string.h`, `linux/types.h`, `kasan.h`.
- Detected declarations: `function Copyright`, `function kasan_get_alloc_size`, `function kasan_metadata_fetch_row`, `function kasan_print_tags`.
- Atlas domain: Core OS / Memory Management.
- Implementation status: source 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.