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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
|
/* -*- linux-c -*-
* Memory allocation functions
* Copyright (C) 2005 Red Hat Inc.
*
* This file is part of systemtap, and is free software. You can
* redistribute it and/or modify it under the terms of the GNU General
* Public License (GPL); either version 2, or (at your option) any
* later version.
*/
#ifndef _ALLOC_C_
#define _ALLOC_C_
/** @file alloc.c
* @brief Memory functions.
*/
/** @addtogroup alloc Memory Functions
* Basic malloc/calloc/free functions. These will be changed so
* that memory allocation errors will call a handler. The default will
* send a signal to the user-space daemon that will trigger the module to
* be unloaded.
* @{
*/
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,15)
/**
* vmalloc_node - allocate virtually contiguous memory
*
* @size: allocation size
* @node: preferred node
*
* This vmalloc variant try to allocate memory from a preferred node.
* This code is from Eric Dumazet, posted to the LKML.
* FIXME: The version in the mm kernel is different. Should probably
* switch if is is easily backported.
*/
#ifdef CONFIG_NUMA
/* Until we get something working */
#define vmalloc_node(size,node) vmalloc(size)
#else
#define vmalloc_node(size,node) vmalloc(size)
#endif /* CONFIG_NUMA */
#endif /* LINUX_VERSION_CODE */
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,12)
#define kmalloc_node(size,flags,node) kmalloc(size,flags)
#endif /* LINUX_VERSION_CODE */
/** Allocates memory within a probe.
* This is used for small allocations from within a running
* probe where the process cannot sleep.
* @param len Number of bytes to allocate.
* @return a valid pointer on success or NULL on failure.
* @note Not currently used by the runtime. Deprecate?
*/
void *_stp_alloc(size_t len)
{
void *ptr = kmalloc(len, GFP_ATOMIC);
if (unlikely(ptr == NULL))
_stp_error("_stp_alloc failed.\n");
return ptr;
}
void *_stp_alloc_cpu(size_t len, int cpu)
{
void *ptr = kmalloc_node(len, GFP_ATOMIC, cpu_to_node(cpu));
if (unlikely(ptr == NULL))
_stp_error("_stp_alloc failed.\n");
return ptr;
}
/** Allocates and clears memory within a probe.
* This is used for small allocations from within a running
* probe where the process cannot sleep.
* @param len Number of bytes to allocate.
* @return a valid pointer on success or NULL on failure.
* @note Not currently used by the runtime. Deprecate?
*/
void *_stp_calloc(size_t len)
{
void *ptr = _stp_alloc(len);
if (likely(ptr))
memset(ptr, 0, len);
return ptr;
}
/** Allocates and clears memory outside a probe.
* This is typically used in the module initialization to
* allocate new maps, lists, etc.
* @param len Number of bytes to allocate.
* @return a valid pointer on success or NULL on failure.
*/
void *_stp_valloc(size_t len)
{
void *ptr = vmalloc(len);
if (likely(ptr))
memset(ptr, 0, len);
else
_stp_error("_stp_valloc failed.\n");
return ptr;
}
void *_stp_valloc_cpu(size_t len, int cpu)
{
void *ptr = vmalloc_node(len, cpu_to_node(cpu));
if (likely(ptr))
memset(ptr, 0, len);
else
_stp_error("_stp_valloc failed.\n");
return ptr;
}
struct _stp_percpu_data {
void *ptrs[NR_CPUS];
void *data;
};
#ifdef CONFIG_SMP
/**
* __stp_valloc_percpu - allocate one copy of the object for every present
* cpu in the system, using vmalloc and zeroing them.
* Objects should be dereferenced using the per_cpu_ptr macro only.
*
* @size: how many bytes of memory are required.
* @align: the alignment, which can't be greater than SMP_CACHE_BYTES.
*/
static void *__stp_valloc_percpu(size_t size, size_t align)
{
int i;
struct _stp_percpu_data *pdata = kmalloc(sizeof (*pdata), GFP_KERNEL);
if (!pdata)
return NULL;
for (i = 0; i < NR_CPUS; i++) {
if (!cpu_possible(i))
continue;
pdata->ptrs[i] = vmalloc_node(size, cpu_to_node(i));
if (!pdata->ptrs[i])
goto unwind_oom;
memset(pdata->ptrs[i], 0, size);
}
/* Catch derefs w/o wrappers */
return (void *) (~(unsigned long) pdata);
unwind_oom:
while (--i >= 0) {
if (!cpu_possible(i))
continue;
vfree(pdata->ptrs[i]);
}
kfree(pdata);
return NULL;
}
void _stp_vfree_percpu(const void *objp)
{
int i;
struct _stp_percpu_data *p = (struct _stp_percpu_data *) (~(unsigned long) objp);
for (i = 0; i < NR_CPUS; i++) {
if (!cpu_possible(i))
continue;
vfree(p->ptrs[i]);
}
kfree(p);
}
#else
static inline void *__stp_valloc_percpu(size_t size, size_t align)
{
void *ret = kmalloc(size, GFP_KERNEL);
if (ret)
memset(ret, 0, size);
return ret;
}
void _stp_vfree_percpu(const void *ptr)
{
kfree(ptr);
}
#endif
#define _stp_valloc_percpu(type) \
((type *)(__stp_valloc_percpu(sizeof(type), __alignof__(type))))
#define _stp_percpu_dptr(ptr) (((struct _stp_percpu_data *)~(unsigned long)(ptr))->data)
/** Frees memory allocated by _stp_alloc or _stp_calloc.
* @param ptr pointer to memory to free
* @note Not currently used by the runtime. Deprecate?
*/
void _stp_free(void *ptr)
{
if (likely(ptr))
kfree(ptr);
}
/** Frees memory allocated by _stp_valloc.
* @param ptr pointer to memory to free
*/
void _stp_vfree(void *ptr)
{
if (likely(ptr))
vfree(ptr);
}
/** @} */
#endif /* _ALLOC_C_ */
|