1 /* 2 * Copyright (c) 2018, 2021 The Linux Foundation. All rights reserved. 3 * Copyright (c) 2022-2024 Qualcomm Innovation Center, Inc. All rights reserved. 4 * 5 * Permission to use, copy, modify, and/or distribute this software for 6 * any purpose with or without fee is hereby granted, provided that the 7 * above copyright notice and this permission notice appear in all 8 * copies. 9 * 10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL 11 * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED 12 * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE 13 * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL 14 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR 15 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER 16 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR 17 * PERFORMANCE OF THIS SOFTWARE. 18 */ 19 20 /** 21 * DOC: qdf_talloc.c 22 * 23 * OS-independent talloc implementation 24 */ 25 26 #ifdef WLAN_TALLOC_DEBUG 27 28 #include "i_qdf_talloc.h" 29 #include "qdf_hashtable.h" 30 #include "qdf_list.h" 31 #include "qdf_mc_timer.h" 32 #include "qdf_mem.h" 33 #include "qdf_module.h" 34 #include "qdf_status.h" 35 #include "qdf_str.h" 36 #include "qdf_talloc.h" 37 38 #define QDF_TALLOC_MAX_BYTES __page_size 39 #define QDF_TALLOC_SLEEP_TIMEOUT_MS 300 40 #define QDF_TALLOC_FUNC_NAME_SIZE 48 41 #define QDF_TALLOC_HT_BITS 8 /* 256 buckets */ 42 43 static void 44 __qdf_talloc_log_nomem(const size_t size, const char *func, const uint16_t line) 45 { 46 qdf_nofl_info("Failed to alloc %zuB; via %s():%d", size, func, line); 47 } 48 49 static void * 50 __qdf_zalloc_auto(const size_t size, const char *func, const uint16_t line) 51 { 52 unsigned long start, duration; 53 void *ptr; 54 55 start = qdf_mc_timer_get_system_time(); 56 ptr = __zalloc_auto(size); 57 duration = qdf_mc_timer_get_system_time() - start; 58 59 if (duration > QDF_TALLOC_SLEEP_TIMEOUT_MS) 60 qdf_nofl_info("Alloc slept; %lums, %zuB; via %s():%d", 61 duration, size, func, line); 62 63 if (!ptr) { 64 __qdf_talloc_log_nomem(size, func, line); 65 return NULL; 66 } 67 68 qdf_mem_kmalloc_inc(__qdf_alloc_size(ptr)); 69 70 return ptr; 71 } 72 73 static void * 74 __qdf_zalloc_atomic(const size_t size, const char *func, const uint16_t line) 75 { 76 void *ptr; 77 78 ptr = __zalloc_atomic(size); 79 if (!ptr) { 80 __qdf_talloc_log_nomem(size, func, line); 81 return NULL; 82 } 83 84 qdf_mem_kmalloc_inc(__qdf_alloc_size(ptr)); 85 86 return ptr; 87 } 88 89 static void __qdf_free(const void *ptr) 90 { 91 qdf_mem_kmalloc_dec(__qdf_alloc_size(ptr)); 92 93 __k_free(ptr); 94 } 95 96 static qdf_ht_declare(__qdf_talloc_meta_ht, QDF_TALLOC_HT_BITS); 97 static qdf_spinlock_t __qdf_talloc_meta_lock; 98 99 /** 100 * struct qdf_talloc_parent_meta - parent/children metadata for memory tracking 101 * @entry: entry for membership in the parent hashtable 102 * @key: parent 103 * @children: list of associated children 104 */ 105 struct qdf_talloc_parent_meta { 106 struct qdf_ht_entry entry; 107 uintptr_t key; 108 qdf_list_t children; 109 }; 110 111 static struct qdf_talloc_parent_meta * 112 qdf_talloc_parent_meta_alloc(const void *parent, 113 const char *func, const uint16_t line) 114 { 115 struct qdf_talloc_parent_meta *pmeta; 116 117 pmeta = __qdf_zalloc_atomic(sizeof(*pmeta), func, line); 118 if (!pmeta) 119 return NULL; 120 121 pmeta->key = (uintptr_t)parent; 122 qdf_list_create(&pmeta->children, 0); 123 qdf_ht_add(__qdf_talloc_meta_ht, &pmeta->entry, pmeta->key); 124 125 return pmeta; 126 } 127 128 static void qdf_talloc_parent_meta_free(struct qdf_talloc_parent_meta *pmeta) 129 { 130 qdf_ht_remove(&pmeta->entry); 131 qdf_list_destroy(&pmeta->children); 132 __k_free(pmeta); 133 } 134 135 static struct qdf_talloc_parent_meta * 136 qdf_talloc_parent_meta_lookup(const void *parent) 137 { 138 struct qdf_talloc_parent_meta *pmeta; 139 uintptr_t key = (uintptr_t)parent; 140 141 qdf_ht_get(__qdf_talloc_meta_ht, pmeta, entry, key, key); 142 143 return pmeta; 144 } 145 146 /** 147 * struct qdf_talloc_child_meta - talloc child debug information 148 * @parent: parent pointer used during allocation for leak tracking 149 * @node: list node for membership in @parent's children list 150 * @func: name of the function that requested the allocation 151 * @line: line number of the call site in @func 152 * @size: size of the allocation in bytes 153 * @guard: a known value, used to detect out-of-bounds access 154 */ 155 struct qdf_talloc_child_meta { 156 const void *parent; 157 qdf_list_node_t node; 158 char func[QDF_TALLOC_FUNC_NAME_SIZE]; 159 uint16_t line; 160 uint32_t size; 161 uint32_t guard; 162 }; 163 164 /** 165 * struct qdf_talloc_header - talloc debug header information 166 * @meta: child allocation metadata 167 * @guard: a known value, used to detect out-of-bounds access 168 */ 169 struct qdf_talloc_header { 170 struct qdf_talloc_child_meta meta; 171 uint32_t guard; 172 }; 173 174 /** 175 * struct qdf_talloc_trailer - talloc debug trailer information 176 * @guard: a known value, used to detect out-of-bounds access 177 */ 178 struct qdf_talloc_trailer { 179 uint32_t guard; 180 }; 181 182 static uint32_t QDF_TALLOC_GUARD = 0xaabbeeff; 183 184 #define QDF_TALLOC_DEBUG_SIZE \ 185 (sizeof(struct qdf_talloc_header) + sizeof(struct qdf_talloc_trailer)) 186 187 static struct qdf_talloc_header *qdf_talloc_header(void *ptr) 188 { 189 return (struct qdf_talloc_header *)ptr - 1; 190 } 191 192 static void *qdf_talloc_ptr(struct qdf_talloc_header *header) 193 { 194 return header + 1; 195 } 196 197 static struct qdf_talloc_trailer * 198 qdf_talloc_trailer(struct qdf_talloc_header *header) 199 { 200 void *ptr = qdf_talloc_ptr(header); 201 size_t size = header->meta.size; 202 203 return (struct qdf_talloc_trailer *)((uint8_t *)ptr + size); 204 } 205 206 static void qdf_talloc_meta_init(struct qdf_talloc_header *header, 207 const void *parent, const size_t size, 208 const char *func, const uint16_t line) 209 { 210 struct qdf_talloc_trailer *trailer; 211 212 /* copy the function name to support multi-*.ko configurations */ 213 qdf_str_lcopy(header->meta.func, func, sizeof(header->meta.func)); 214 header->meta.parent = parent; 215 header->meta.line = line; 216 header->meta.size = size; 217 header->guard = QDF_TALLOC_GUARD; 218 219 trailer = qdf_talloc_trailer(header); 220 trailer->guard = QDF_TALLOC_GUARD; 221 } 222 223 static bool qdf_talloc_meta_assert_valid(struct qdf_talloc_header *header, 224 const char *func, const uint16_t line) 225 { 226 struct qdf_talloc_trailer *trailer = qdf_talloc_trailer(header); 227 bool is_valid = true; 228 229 if (header->guard != QDF_TALLOC_GUARD) { 230 qdf_nofl_alert("Corrupted header guard 0x%x (expected 0x%x)", 231 header->guard, QDF_TALLOC_GUARD); 232 is_valid = false; 233 } 234 235 if (header->meta.size > QDF_TALLOC_MAX_BYTES) { 236 qdf_nofl_alert("Corrupted allocation size %u (expected <= %zu)", 237 header->meta.size, QDF_TALLOC_MAX_BYTES); 238 is_valid = false; 239 } 240 241 if (!qdf_list_node_in_any_list(&header->meta.node)) { 242 qdf_nofl_alert("Corrupted header node or double free"); 243 is_valid = false; 244 } 245 246 if (trailer->guard != QDF_TALLOC_GUARD) { 247 qdf_nofl_alert("Corrupted trailer guard 0x%x (expected 0x%x)", 248 trailer->guard, QDF_TALLOC_GUARD); 249 is_valid = false; 250 } 251 252 if (!is_valid) 253 QDF_DEBUG_PANIC("Fatal memory error detected @ %s():%d", 254 func, line); 255 256 return is_valid; 257 } 258 259 static void qdf_leaks_print_header(void) 260 { 261 qdf_nofl_alert("-----------------------------------------------------"); 262 qdf_nofl_alert(" size function():line"); 263 qdf_nofl_alert("-----------------------------------------------------"); 264 } 265 266 static uint32_t qdf_leaks_print(const struct qdf_talloc_parent_meta *pmeta) 267 { 268 struct qdf_talloc_child_meta *cmeta; 269 uint32_t count = 0; 270 271 qdf_list_for_each(&pmeta->children, cmeta, node) { 272 qdf_nofl_alert("%6uB @ %s():%u", 273 cmeta->size, cmeta->func, cmeta->line); 274 count++; 275 } 276 277 return count; 278 } 279 280 #define qdf_leaks_panic(count, func, line) \ 281 QDF_DEBUG_PANIC("%u fatal memory leaks detected @ %s():%u", \ 282 count, func, line) 283 284 QDF_STATUS qdf_talloc_feature_init(void) 285 { 286 qdf_spinlock_create(&__qdf_talloc_meta_lock); 287 qdf_ht_init(__qdf_talloc_meta_ht); 288 289 return QDF_STATUS_SUCCESS; 290 } 291 qdf_export_symbol(qdf_talloc_feature_init); 292 293 void qdf_talloc_feature_deinit(void) 294 { 295 qdf_spin_lock_bh(&__qdf_talloc_meta_lock); 296 297 if (!qdf_ht_empty(__qdf_talloc_meta_ht)) { 298 struct qdf_talloc_parent_meta *pmeta; 299 uint32_t count = 0; 300 int i; 301 302 qdf_leaks_print_header(); 303 304 qdf_ht_for_each(__qdf_talloc_meta_ht, i, pmeta, entry) 305 count += qdf_leaks_print(pmeta); 306 307 qdf_leaks_panic(count, __func__, __LINE__); 308 } 309 310 qdf_spin_unlock_bh(&__qdf_talloc_meta_lock); 311 312 qdf_ht_deinit(__qdf_talloc_meta_ht); 313 qdf_spinlock_destroy(&__qdf_talloc_meta_lock); 314 } 315 qdf_export_symbol(qdf_talloc_feature_deinit); 316 317 static QDF_STATUS qdf_talloc_meta_insert(struct qdf_talloc_header *header, 318 const char *func, const uint16_t line) 319 { 320 struct qdf_talloc_child_meta *cmeta = &header->meta; 321 struct qdf_talloc_parent_meta *pmeta; 322 323 pmeta = qdf_talloc_parent_meta_lookup(cmeta->parent); 324 if (!pmeta) 325 pmeta = qdf_talloc_parent_meta_alloc(cmeta->parent, func, line); 326 if (!pmeta) 327 return QDF_STATUS_E_NOMEM; 328 329 qdf_list_insert_back(&pmeta->children, &cmeta->node); 330 331 return QDF_STATUS_SUCCESS; 332 } 333 334 void *__qdf_talloc_fl(const void *parent, const size_t size, 335 const char *func, const uint16_t line) 336 { 337 QDF_STATUS status; 338 struct qdf_talloc_header *header; 339 340 QDF_BUG(parent); 341 if (!parent) 342 return NULL; 343 344 QDF_BUG(size <= QDF_TALLOC_MAX_BYTES); 345 if (size > QDF_TALLOC_MAX_BYTES) 346 return NULL; 347 348 header = __qdf_zalloc_auto(size + QDF_TALLOC_DEBUG_SIZE, func, line); 349 if (!header) 350 return NULL; 351 352 qdf_talloc_meta_init(header, parent, size, func, line); 353 354 qdf_spin_lock_bh(&__qdf_talloc_meta_lock); 355 status = qdf_talloc_meta_insert(header, func, line); 356 qdf_spin_unlock_bh(&__qdf_talloc_meta_lock); 357 358 if (QDF_IS_STATUS_ERROR(status)) { 359 __qdf_free(header); 360 return NULL; 361 } 362 363 return qdf_talloc_ptr(header); 364 } 365 qdf_export_symbol(__qdf_talloc_fl); 366 367 static void 368 __qdf_talloc_assert_no_children(const void *parent, 369 const char *func, const uint16_t line) 370 { 371 struct qdf_talloc_parent_meta *pmeta; 372 uint32_t count; 373 374 pmeta = qdf_talloc_parent_meta_lookup(parent); 375 if (!pmeta) 376 return; 377 378 qdf_leaks_print_header(); 379 count = qdf_leaks_print(pmeta); 380 qdf_leaks_panic(count, func, line); 381 } 382 383 static void qdf_talloc_meta_remove(struct qdf_talloc_header *header, 384 const char *func, const uint16_t line) 385 { 386 struct qdf_talloc_child_meta *cmeta = &header->meta; 387 struct qdf_talloc_parent_meta *pmeta; 388 389 __qdf_talloc_assert_no_children(qdf_talloc_ptr(header), func, line); 390 391 pmeta = qdf_talloc_parent_meta_lookup(cmeta->parent); 392 if (!pmeta) { 393 QDF_DEBUG_PANIC("double-free or free-no-allocate @ %s():%u", 394 func, line); 395 return; 396 } 397 398 qdf_list_remove_node(&pmeta->children, &cmeta->node); 399 400 if (qdf_list_empty(&pmeta->children)) 401 qdf_talloc_parent_meta_free(pmeta); 402 } 403 404 void __qdf_tfree_fl(void *ptr, const char *func, const uint16_t line) 405 { 406 struct qdf_talloc_header *header; 407 408 QDF_BUG(ptr); 409 if (!ptr) 410 return; 411 412 header = qdf_talloc_header(ptr); 413 qdf_talloc_meta_assert_valid(header, func, line); 414 415 qdf_spin_lock_bh(&__qdf_talloc_meta_lock); 416 qdf_talloc_meta_remove(header, func, line); 417 qdf_spin_unlock_bh(&__qdf_talloc_meta_lock); 418 419 __qdf_free(header); 420 } 421 qdf_export_symbol(__qdf_tfree_fl); 422 423 void qdf_talloc_assert_no_children_fl(const void *parent, 424 const char *func, const uint16_t line) 425 { 426 qdf_spin_lock_bh(&__qdf_talloc_meta_lock); 427 __qdf_talloc_assert_no_children(parent, func, line); 428 qdf_spin_unlock_bh(&__qdf_talloc_meta_lock); 429 } 430 qdf_export_symbol(qdf_talloc_assert_no_children_fl); 431 432 #endif /* WLAN_TALLOC_DEBUG */ 433 434