1// SPDX-License-Identifier: GPL-2.0
2/*
3 * This file contains common tag-based KASAN code.
4 *
5 * Copyright (c) 2018 Google, Inc.
6 * Copyright (c) 2020 Google, Inc.
7 */
8
9#include <linux/init.h>
10#include <linux/kasan.h>
11#include <linux/kernel.h>
12#include <linux/memory.h>
13#include <linux/mm.h>
14#include <linux/static_key.h>
15#include <linux/string.h>
16#include <linux/types.h>
17
18#include "kasan.h"
19
20void kasan_set_free_info(struct kmem_cache *cache,
21 void *object, u8 tag)
22{
23 struct kasan_alloc_meta *alloc_meta;
24 u8 idx = 0;
25
26 alloc_meta = kasan_get_alloc_meta(cache, object);
27 if (!alloc_meta)
28 return;
29
30#ifdef CONFIG_KASAN_TAGS_IDENTIFY
31 idx = alloc_meta->free_track_idx;
32 alloc_meta->free_pointer_tag[idx] = tag;
33 alloc_meta->free_track_idx = (idx + 1) % KASAN_NR_FREE_STACKS;
34#endif
35
36 kasan_set_track(&alloc_meta->free_track[idx], GFP_NOWAIT);
37}
38
39struct kasan_track *kasan_get_free_track(struct kmem_cache *cache,
40 void *object, u8 tag)
41{
42 struct kasan_alloc_meta *alloc_meta;
43 int i = 0;
44
45 alloc_meta = kasan_get_alloc_meta(cache, object);
46 if (!alloc_meta)
47 return NULL;
48
49#ifdef CONFIG_KASAN_TAGS_IDENTIFY
50 for (i = 0; i < KASAN_NR_FREE_STACKS; i++) {
51 if (alloc_meta->free_pointer_tag[i] == tag)
52 break;
53 }
54 if (i == KASAN_NR_FREE_STACKS)
55 i = alloc_meta->free_track_idx;
56#endif
57
58 return &alloc_meta->free_track[i];
59}
60

source code of linux/mm/kasan/tags.c