summaryrefslogtreecommitdiff
path: root/include/trace/events/alloc_tag.h
blob: 58d420e645163e5d36c577fa1c6f827010c8e637 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/* SPDX-License-Identifier: GPL-2.0 */
#undef TRACE_SYSTEM
#define TRACE_SYSTEM alloc_tag

#if !defined(_TRACE_ALLOC_TAG_H) || defined(TRACE_HEADER_MULTI_READ)
#define _TRACE_ALLOC_TAG_H

#include <linux/tracepoint.h>

/*
 * alloc_tag_hit is generated at the exact allocation call site and can be
 * used to capture a stack trace.
 *
 * To link this stack trace to the actual allocated memory chunk, tools must
 * correlate this event with the resulting alloc_tag_mem_alloced event. Since
 * multiple threads can hit the same tag simultaneously, tools must match BOTH
 * the `tag` field and the implicitly recorded PID provided by the core
 * tracing subsystem.
 */
TRACE_EVENT(alloc_tag_hit,
	TP_PROTO(struct alloc_tag *tag),

	TP_ARGS(tag),

	TP_STRUCT__entry(__field(struct alloc_tag *, tag)
			 __string(modname, tag->ct.modname ? tag->ct.modname : "NONE")
			 __string(filename, tag->ct.filename)
			 __string(function, tag->ct.function)
			 __field(unsigned int, lineno)
	),

	TP_fast_assign(__entry->tag = tag;
		       __assign_str(modname);
		       __assign_str(filename);
		       __assign_str(function);
		       __entry->lineno = tag->ct.lineno;
	),

	TP_printk("tag %p, module: %s, filename: %s, function %s, lineno %u",
		  __entry->tag,
		  __get_str(modname),
		  __get_str(filename),
		  __get_str(function),
		  __entry->lineno
	)
);

/*
 * alloc_tag_mem_alloced is generated after memory is successfully allocated.
 * It captures the exact byte size.
 *
 * The `ptr` value identifies the memory chunk for tracking its lifecycle
 * (e.g., matching it with alloc_tag_mem_freed).
 * - slab and percpu allocators: address of the object's codetag_ref
 * - page allocator: the head struct page of the allocation
 *
 * Because the kernel isolates active allocations within the task struct
 * (current->alloc_tag), this event will always share the same implicit PID as
 * its corresponding alloc_tag_hit event. Tools should use the combination
 * PID + `tag` to correlate them.
 */
TRACE_EVENT(alloc_tag_mem_alloced,
	TP_PROTO(const void *ptr, struct alloc_tag *tag, size_t bytes),

	TP_ARGS(ptr, tag, bytes),

	TP_STRUCT__entry(__field(const void *, ptr)
			 __field(struct alloc_tag *, tag)
			 __field(size_t, bytes)
	),

	TP_fast_assign(__entry->ptr = ptr;
		       __entry->tag = tag;
		       __entry->bytes = bytes;
	),

	TP_printk("ptr %p, tag %p, bytes %zu",
		  __entry->ptr,
		  __entry->tag,
		  __entry->bytes
	)
);

/*
 * alloc_tag_mem_freed event is generated immediately before memory is
 * freed. The `ptr` value matches the one emitted during allocation,
 * allowing tools to match it to its corresponding allocation and
 * call stack.
 *
 * Page allocator caveat: Pages can be split or moved to another folio
 * after the alloc event with no new events generated for that. As a
 * result, a free event may carry a `ptr` that never appeared in an
 * alloc event, and the `bytes` may be smaller than in the matching
 * alloc event. Tools must not assume alloc bytes == free bytes, nor
 * that every free has a matching alloc.
 */
TRACE_EVENT(alloc_tag_mem_freed,
	TP_PROTO(const void *ptr, struct alloc_tag *tag, size_t bytes),

	TP_ARGS(ptr, tag, bytes),

	TP_STRUCT__entry(__field(const void *, ptr)
			 __field(struct alloc_tag *, tag)
			 __field(size_t, bytes)
	),

	TP_fast_assign(__entry->ptr = ptr;
		       __entry->tag = tag;
		       __entry->bytes = bytes;
	),

	TP_printk("ptr %p, tag %p, bytes %zu",
		  __entry->ptr,
		  __entry->tag,
		  __entry->bytes
	)
);

#endif /* _TRACE_ALLOC_TAG_H */

/* This part must be outside protection */
#include <trace/define_trace.h>