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 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677
|
/*******************************************************************************
Copyright (c) 2015-2023 NVIDIA Corporation
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
*******************************************************************************/
#include "uvm_forward_decl.h"
#include "uvm_thread_context.h"
#include "uvm_linux.h"
#include "uvm_common.h"
// Thread local storage implementation.
//
// The global data structure that contains the set of active thread contexts
// is a table of UVM_THREAD_CONTEXT_TABLE_SIZE entries of type
// uvm_thread_context_table_entry_t.
// Each entry contains a small array of UVM_THREAD_CONTEXT_ARRAY_SIZE entries,
// a red-black tree, and a lock protecting the tree.
//
// The thread_context_non_interrupt_table_entry() function maps the current task
// (i.e. the current thread context) to a table entry. That function also
// recommends a position within the entry's array, but that index can be safely
// ignored: the thread context can be located in any array slot, or in the
// red-black tree.
//
// The described global data structures try to minimize contention among
// threads at two levels. First, thread_context_non_interrupt_table_entry()
// relies on a hash function to evenly spread threads among table entries.
// Second, when several threads are mapped to the same table entry, the same
// hash function spreads them evenly among the array entries, which can
// be independently and atomically updated. If the array is full, the thread
// context of the current task is stored in the red-black tree of the table
// entry, which is protected by a single lock.
//
// Both the table and array entries are cache aligned to avoid false sharing
// overheads due to cache thrashing between concurrent operations on separate
// thread contexts.
#define UVM_THREAD_CONTEXT_ARRAY_SIZE 8
typedef struct {
void *acquired[UVM_LOCK_ORDER_COUNT];
} uvm_thread_context_lock_acquired_t;
typedef struct {
// If zero, the entry is empty. Otherwise, task is equal to the value of
// get_current() for the thread associated with thread_context;
atomic64_t task;
uvm_thread_context_t *thread_context;
} ____cacheline_aligned_in_smp uvm_thread_context_array_entry_t;
// The thread's context information is stored in the array or the red-black
// tree.
typedef struct {
// Small array where thread contexts are stored first. Each array entry
// can be atomically claimed or released.
uvm_thread_context_array_entry_t array[UVM_THREAD_CONTEXT_ARRAY_SIZE];
// Red-black tree, used when the array is full. A red-black tree is chosen
// because additions and removals are frequent operations: every time the
// UVM module is entered, there is one addition, one removal, and one
// lookup. The same UVM call may result on additional lookups.
struct rb_root tree;
// Spinlock protecting the tree. A raw lock is chosen because UVM locks
// rely on thread context information to be available for lock tracking.
spinlock_t tree_lock;
} ____cacheline_aligned_in_smp uvm_thread_context_table_entry_t;
// Global data structure containing all the active thread contexts
static uvm_thread_context_table_entry_t g_thread_context_table[UVM_THREAD_CONTEXT_TABLE_SIZE];
static bool g_thread_context_table_initialized __read_mostly = false;
// Per CPU context wrapper, used for interrupt context. Zero initialized.
static DEFINE_PER_CPU(uvm_thread_context_wrapper_t, interrupt_thread_context_wrapper);
// Array of acquired locks, used in the interrupt path. The non-interrupt path
// allocates the array when locking for the first time.
static DEFINE_PER_CPU(uvm_thread_context_lock_acquired_t, interrupt_thread_context_lock_acquired);
static void thread_context_non_interrupt_remove(uvm_thread_context_t *thread_context,
uvm_thread_context_table_entry_t *thread_context_entry);
bool uvm_thread_context_wrapper_is_used(void)
{
// The wrapper contains lock information. While uvm_record_lock_X
// routines are a no-op outside of debug mode, unit tests do invoke their
// internal counterparts __uvm_record_lock_X. To add coverage, lock
// information is made available in develop and release modes if the
// builtin tests are enabled.
return UVM_IS_DEBUG() || uvm_enable_builtin_tests;
}
bool uvm_thread_context_global_initialized(void)
{
return g_thread_context_table_initialized;
}
void uvm_thread_context_global_init(void)
{
size_t table_index;
UVM_ASSERT(!uvm_thread_context_global_initialized());
for (table_index = 0; table_index < UVM_THREAD_CONTEXT_TABLE_SIZE; table_index++) {
uvm_thread_context_table_entry_t *table_entry = g_thread_context_table + table_index;
spin_lock_init(&table_entry->tree_lock);
table_entry->tree = RB_ROOT;
}
g_thread_context_table_initialized = true;
}
void uvm_thread_context_global_exit(void)
{
size_t table_index;
uvm_thread_context_t *curr_thread_context = uvm_thread_context();
UVM_ASSERT(uvm_thread_context_global_initialized());
// Search for thread contexts that were added but never removed.
// There should be one thread context left: the one added by the UVM module
// exit routine that invoked this function. To prevent the exit routine from
// deleting its thread context after deinitialization of the global table,
// it is deleted here. uvm_thread_context_remove will detect that the global
// shutdown already happened and skip.
for (table_index = 0; table_index < UVM_THREAD_CONTEXT_TABLE_SIZE; table_index++) {
size_t array_index;
struct rb_node *node;
uvm_thread_context_table_entry_t *table_entry = g_thread_context_table + table_index;
for (array_index = 0; array_index < UVM_THREAD_CONTEXT_ARRAY_SIZE; array_index++) {
uvm_thread_context_t *thread_context;
uvm_thread_context_array_entry_t *array_entry = table_entry->array + array_index;
NvU64 task = atomic64_read(&array_entry->task);
if (task == 0)
continue;
thread_context = array_entry->thread_context;
UVM_ASSERT_MSG(thread_context == curr_thread_context,
"Left-over thread_context 0x%llx task 0x%llx\n",
(NvU64) thread_context,
(NvU64) thread_context->task);
thread_context_non_interrupt_remove(thread_context, table_entry);
}
node = rb_first(&table_entry->tree);
while (node) {
uvm_thread_context_t *thread_context = rb_entry(node, uvm_thread_context_t, node);
UVM_ASSERT_MSG(thread_context == curr_thread_context,
"Left-over thread_context 0x%llx task 0x%llx\n",
(NvU64) thread_context,
(NvU64) thread_context->task);
thread_context_non_interrupt_remove(thread_context, table_entry);
node = rb_first(&table_entry->tree);
}
}
g_thread_context_table_initialized = false;
}
static uvm_thread_context_t *thread_context_non_interrupt_tree_search(struct rb_root *root, struct task_struct *task)
{
struct rb_node *node = root->rb_node;
uintptr_t task_uintptr = (uintptr_t) task;
while (node) {
uvm_thread_context_t *thread_context = rb_entry(node, uvm_thread_context_t, node);
uintptr_t thread_context_task_uintptr = (uintptr_t) thread_context->task;
if (thread_context_task_uintptr == task_uintptr)
return thread_context;
node = (thread_context_task_uintptr > task_uintptr)? node->rb_left : node->rb_right;
}
return NULL;
}
static bool thread_context_non_interrupt_tree_insert(struct rb_root *root, uvm_thread_context_t *new_thread_context)
{
struct rb_node **node_ptr = &root->rb_node;
struct rb_node *node = root->rb_node;
struct rb_node *parent = NULL;
const struct task_struct *task = new_thread_context->task;
uintptr_t task_uintptr = (uintptr_t) task;
while (node) {
uvm_thread_context_t *thread_context = rb_entry(node, uvm_thread_context_t, node);
uintptr_t thread_context_task_uintptr = (uintptr_t) thread_context->task;
if (thread_context_task_uintptr == task_uintptr)
return false;
parent = node;
node_ptr = (thread_context_task_uintptr > task_uintptr) ? &node->rb_left : &node->rb_right;
node = *node_ptr;
}
rb_link_node(&new_thread_context->node, parent, node_ptr);
rb_insert_color(&new_thread_context->node, root);
return true;
}
static void thread_context_lock_interrupt_patch_acquired(uvm_thread_context_lock_t *context_lock)
{
uvm_thread_context_lock_acquired_t *thread_context_lock_acquired;
UVM_ASSERT(in_interrupt());
UVM_ASSERT(context_lock->acquired == NULL);
// Stich the preallocated, per-CPU array to the thread context lock.
thread_context_lock_acquired = &get_cpu_var(interrupt_thread_context_lock_acquired);
put_cpu_var(interrupt_thread_context_lock_acquired);
context_lock->acquired = (void**) thread_context_lock_acquired;
}
static uvm_thread_context_lock_t *thread_context_lock_of(uvm_thread_context_t *thread_context)
{
uvm_thread_context_wrapper_t *thread_context_wrapper;
uvm_thread_context_lock_t *context_lock;
if (!uvm_thread_context_wrapper_is_used())
return NULL;
thread_context_wrapper = container_of(thread_context, uvm_thread_context_wrapper_t, context);
context_lock = &thread_context_wrapper->context_lock;
// When the wrapper is used, the thread context lock is always present but
// its acquired locks array may not, due to a failed allocation. Instead of
// working around the missing array, pretend that the entire lock context
// does not exist. This situation can only happen in non-interrupt paths.
if (context_lock->acquired == NULL) {
if (in_interrupt())
thread_context_lock_interrupt_patch_acquired(context_lock);
else
return NULL;
}
return context_lock;
}
static void thread_context_non_interrupt_init(uvm_thread_context_t *thread_context)
{
UVM_ASSERT(!in_interrupt());
thread_context->array_index = UVM_THREAD_CONTEXT_ARRAY_SIZE;
if (uvm_thread_context_wrapper_is_used()) {
uvm_thread_context_wrapper_t *thread_context_wrapper;
uvm_thread_context_lock_t *context_lock;
thread_context_wrapper = container_of(thread_context, uvm_thread_context_wrapper_t, context);
context_lock = &thread_context_wrapper->context_lock;
memset(context_lock, 0, sizeof(*context_lock));
// If this allocation fails, the lock context will appear as not
// present, but the rest of the thread context is usable.
context_lock->acquired = kmalloc(sizeof(context_lock->acquired[0]) * UVM_LOCK_ORDER_COUNT, NV_UVM_GFP_FLAGS);
}
}
static void thread_context_non_interrupt_deinit(uvm_thread_context_t *thread_context)
{
uvm_thread_context_lock_t *context_lock;
UVM_ASSERT(!in_interrupt());
context_lock = thread_context_lock_of(thread_context);
if (context_lock != NULL) {
UVM_ASSERT(__uvm_check_all_unlocked(context_lock));
kfree(context_lock->acquired);
context_lock->acquired = NULL;
}
}
// Return the table entry and array index within that entry where the thread
// context of the current task is located.
//
// The array index should be interpreted as a hint: the thread context of the
// current taks may be stored at a different array index, or in the tree.
static uvm_thread_context_table_entry_t *thread_context_non_interrupt_table_entry(size_t *array_index_hint)
{
size_t table_index;
NvU64 current_ptr = (NvU64) current;
NvU32 hash = jhash_2words((NvU32) current_ptr, (NvU32) (current_ptr >> 32), 0);
BUILD_BUG_ON(UVM_THREAD_CONTEXT_TABLE_SIZE > (1 << 16));
BUILD_BUG_ON(UVM_THREAD_CONTEXT_ARRAY_SIZE > (1 << 16));
UVM_ASSERT(!in_interrupt());
// The upper 16 bits of the hash value index the table; the lower 16
// index the array
table_index = (hash >> 16) % UVM_THREAD_CONTEXT_TABLE_SIZE;
if (array_index_hint != NULL)
*array_index_hint = hash % UVM_THREAD_CONTEXT_ARRAY_SIZE;
return g_thread_context_table + table_index;
}
static uvm_thread_context_t *thread_context_non_interrupt(void)
{
unsigned long flags;
size_t i, array_index;
uvm_thread_context_t *thread_context;
uvm_thread_context_table_entry_t *table_entry = thread_context_non_interrupt_table_entry(&array_index);
for (i = array_index; i < (UVM_THREAD_CONTEXT_ARRAY_SIZE + array_index); i++) {
size_t curr_array_index = i % UVM_THREAD_CONTEXT_ARRAY_SIZE;
uvm_thread_context_array_entry_t *array_entry = table_entry->array + curr_array_index;
if (atomic64_read(&array_entry->task) == (NvU64) current) {
thread_context = array_entry->thread_context;
UVM_ASSERT(thread_context != NULL);
UVM_ASSERT(thread_context->array_index == curr_array_index);
return thread_context;
}
}
spin_lock_irqsave(&table_entry->tree_lock, flags);
thread_context = thread_context_non_interrupt_tree_search(&table_entry->tree, current);
spin_unlock_irqrestore(&table_entry->tree_lock, flags);
return thread_context;
}
static uvm_thread_context_t *thread_context_interrupt(void)
{
uvm_thread_context_wrapper_t *thread_context_wrapper;
// As we are in interrupt anyway it would be best to just use this_cpu_ptr()
// but it was added in 2.6.33 and the interface is non-trivial to implement
// prior to that.
thread_context_wrapper = &get_cpu_var(interrupt_thread_context_wrapper);
put_cpu_var(interrupt_thread_context_wrapper);
return &thread_context_wrapper->context;
}
static uvm_thread_context_t *thread_context_current(void)
{
return in_interrupt() ? thread_context_interrupt() : thread_context_non_interrupt();
}
bool uvm_thread_context_present(void)
{
return thread_context_current() != NULL;
}
uvm_thread_context_t *uvm_thread_context(void)
{
uvm_thread_context_t *thread_context = thread_context_current();
// If this assertion fires is probably because an entry point into the
// UVM module has not been wrapped with a UVM_ENTRY_X macro. The entry point
// to wrap is the first nvidia-uvm function in the error call stack printed
// by the assertion.
UVM_ASSERT(thread_context != NULL);
return thread_context;
}
// The addition logic takes into account that there may be a different thread
// context already associated with the given task. This happens in the uncommon
// case of re-entering the UVM module. Therefore, it is worth approaching the
// addition in a optimistic (speculative) fashion: if a slot is empty in the
// array, it is immediately taken. Should we discover later on that the task
// already has a thread context associated with it in the rest of the array or
// the tree, the previously claimed array slot is released.
static bool thread_context_non_interrupt_add(uvm_thread_context_t *thread_context,
uvm_thread_context_table_entry_t *table_entry,
size_t array_index_hint)
{
size_t i;
NvU64 task;
unsigned long flags;
bool added;
UVM_ASSERT(!in_interrupt());
UVM_ASSERT(thread_context != NULL);
UVM_ASSERT(table_entry != NULL);
UVM_ASSERT(table_entry - g_thread_context_table >= 0);
UVM_ASSERT(table_entry - g_thread_context_table < UVM_THREAD_CONTEXT_TABLE_SIZE);
UVM_ASSERT(array_index_hint < UVM_THREAD_CONTEXT_ARRAY_SIZE);
thread_context_non_interrupt_init(thread_context);
UVM_ASSERT(thread_context->array_index == UVM_THREAD_CONTEXT_ARRAY_SIZE);
task = (NvU64) thread_context->task;
UVM_ASSERT(task > 0);
for (i = array_index_hint; i < (array_index_hint + UVM_THREAD_CONTEXT_ARRAY_SIZE); i++) {
const size_t curr_array_index = i % UVM_THREAD_CONTEXT_ARRAY_SIZE;
uvm_thread_context_array_entry_t *array_entry = table_entry->array + curr_array_index;
if (thread_context->array_index == UVM_THREAD_CONTEXT_ARRAY_SIZE) {
NvU64 old = atomic64_cmpxchg(&array_entry->task, 0, task);
// Task already added a different thread context. The current thread
// context has not been inserted but needs to be freed.
if (old == task) {
thread_context_non_interrupt_deinit(thread_context);
return false;
}
// Speculatively add the current thread context.
if (old == 0)
thread_context->array_index = curr_array_index;
}
else if (atomic64_read(&array_entry->task) == task) {
// Task already added a different thread context to the array, so
// undo the speculative insertion
atomic64_set(&table_entry->array[thread_context->array_index].task, 0);
thread_context_non_interrupt_deinit(thread_context);
return false;
}
}
spin_lock_irqsave(&table_entry->tree_lock, flags);
if (thread_context->array_index == UVM_THREAD_CONTEXT_ARRAY_SIZE) {
// If the task already added a different thread context to the tree,
// there is nothing to undo because the current thread context has not
// been inserted.
added = thread_context_non_interrupt_tree_insert(&table_entry->tree, thread_context);
}
else if (thread_context_non_interrupt_tree_search(&table_entry->tree, thread_context->task) != NULL) {
// Task already added a different thread context to the tree, so undo
// the speculative insertion
atomic64_set(&table_entry->array[thread_context->array_index].task, 0);
added = false;
}
else {
// Speculative insertion succeeded: a thread context associated with the
// same task has not been found in the array or the tree.
table_entry->array[thread_context->array_index].thread_context = thread_context;
added = true;
}
if (!added)
thread_context_non_interrupt_deinit(thread_context);
spin_unlock_irqrestore(&table_entry->tree_lock, flags);
return added;
}
bool uvm_thread_context_add(uvm_thread_context_t *thread_context)
{
uvm_thread_context_table_entry_t *table_entry;
size_t array_index;
UVM_ASSERT(thread_context != NULL);
UVM_ASSERT(!in_interrupt());
// Initialize the thread context table. This can only happen when loading
// the UVM module
if (!uvm_thread_context_global_initialized())
uvm_thread_context_global_init();
thread_context->task = current;
thread_context->ignore_hmm_invalidate_va_block = NULL;
table_entry = thread_context_non_interrupt_table_entry(&array_index);
return thread_context_non_interrupt_add(thread_context, table_entry, array_index);
}
bool uvm_thread_context_add_at(uvm_thread_context_t *thread_context, size_t table_index)
{
uvm_thread_context_table_entry_t *table_entry;
UVM_ASSERT(uvm_enable_builtin_tests != 0);
UVM_ASSERT(uvm_thread_context_global_initialized());
table_entry = g_thread_context_table + table_index;
return thread_context_non_interrupt_add(thread_context, table_entry, 0);
}
static void thread_context_non_interrupt_remove(uvm_thread_context_t *thread_context,
uvm_thread_context_table_entry_t *table_entry)
{
NvU32 array_index;
UVM_ASSERT(!in_interrupt());
UVM_ASSERT(thread_context != NULL);
UVM_ASSERT(table_entry != NULL);
UVM_ASSERT(table_entry - g_thread_context_table >= 0);
UVM_ASSERT(table_entry - g_thread_context_table < UVM_THREAD_CONTEXT_TABLE_SIZE);
array_index = thread_context->array_index;
UVM_ASSERT(array_index <= UVM_THREAD_CONTEXT_ARRAY_SIZE);
// We cannot use RB_EMPTY_NODE to determine if the thread context is in the
// tree, because the tree lock is not held and we haven't called RB_CLEAR_NODE.
// If the thread context is indeed in the tree, concurrent operations on
// the parent pointer/color of the thread context's node could result in
// RB_EMPTY_NODE(thread_context->node) being true.
if (array_index != UVM_THREAD_CONTEXT_ARRAY_SIZE) {
uvm_thread_context_array_entry_t *array_entry = table_entry->array + array_index;
UVM_ASSERT(array_index < UVM_THREAD_CONTEXT_ARRAY_SIZE);
UVM_ASSERT(atomic64_read(&array_entry->task) == (NvU64) thread_context->task);
// Clear the task. The memory barrier prevents the write from being
// moved before a previous (in program order) write to the entry's
// thread_context field in thread_context_non_interrupt_add.
//
// A more detailed explanation about why the memory barrier is needed
// before an atomic write, and why we are not using a different flavor
// of atomic write such as atomic64_set_release, can be found in
// uvm_gpu_semaphore.c:update_completed_value_locked().
smp_mb__before_atomic();
atomic64_set(&array_entry->task, 0);
}
else {
unsigned long flags;
spin_lock_irqsave(&table_entry->tree_lock, flags);
rb_erase(&thread_context->node, &table_entry->tree);
spin_unlock_irqrestore(&table_entry->tree_lock, flags);
}
thread_context_non_interrupt_deinit(thread_context);
}
void uvm_thread_context_remove(uvm_thread_context_t *thread_context)
{
uvm_thread_context_table_entry_t *table_entry;
UVM_ASSERT(thread_context != NULL);
UVM_ASSERT(!in_interrupt());
// If the thread context table has been deinitialized, then we must be in
// the UVM module unload path, and the thread context added during the call
// of uvm_exit has already been removed in the global deinitialization.
if (!uvm_thread_context_global_initialized())
return;
UVM_ASSERT(thread_context->task == current);
UVM_ASSERT(uvm_thread_context() == thread_context);
table_entry = thread_context_non_interrupt_table_entry(NULL);
thread_context_non_interrupt_remove(thread_context, table_entry);
}
void uvm_thread_context_remove_at(uvm_thread_context_t *thread_context, size_t table_index)
{
uvm_thread_context_table_entry_t *table_entry = g_thread_context_table + table_index;
UVM_ASSERT(uvm_enable_builtin_tests != 0);
thread_context_non_interrupt_remove(thread_context, table_entry);
}
// Move operation
// -Lock information is copied to the destination, and cleared in the source.
// -Locations in the global array or tree are not copied nor cleared, since
// they may be needed for a later removal of the source, and are no longer
// valid after it.
// -When adding new members to the thread context, consider if they need to be
// moved
static void thread_context_move(uvm_thread_context_t *dst, uvm_thread_context_t *src)
{
uvm_thread_context_lock_t *src_context_lock, *dst_context_lock;
UVM_ASSERT(uvm_enable_builtin_tests != 0);
src_context_lock = thread_context_lock_of(src);
dst_context_lock = thread_context_lock_of(dst);
if ((dst_context_lock != NULL) && (src_context_lock != NULL)) {
size_t acquired_size = sizeof(src_context_lock->acquired[0]) * UVM_LOCK_ORDER_COUNT;
dst_context_lock->skip_lock_tracking = src_context_lock->skip_lock_tracking;
src_context_lock->skip_lock_tracking = false;
// Note that the locks are not released, even when they appear as such
// if we query the source thread context. They are still acquired in the
// destination context.
bitmap_copy(dst_context_lock->acquired_lock_orders,
src_context_lock->acquired_lock_orders,
UVM_LOCK_ORDER_COUNT);
bitmap_zero(src_context_lock->acquired_lock_orders, UVM_LOCK_ORDER_COUNT);
bitmap_copy(dst_context_lock->exclusive_acquired_lock_orders,
src_context_lock->exclusive_acquired_lock_orders,
UVM_LOCK_ORDER_COUNT);
bitmap_zero(src_context_lock->exclusive_acquired_lock_orders, UVM_LOCK_ORDER_COUNT);
bitmap_copy(dst_context_lock->out_of_order_acquired_lock_orders,
src_context_lock->out_of_order_acquired_lock_orders,
UVM_LOCK_ORDER_COUNT);
bitmap_zero(src_context_lock->out_of_order_acquired_lock_orders, UVM_LOCK_ORDER_COUNT);
memcpy(dst_context_lock->acquired, src_context_lock->acquired, acquired_size);
}
}
void uvm_thread_context_save(uvm_thread_context_t *dst)
{
thread_context_non_interrupt_init(dst);
thread_context_move(dst, uvm_thread_context());
}
void uvm_thread_context_restore(uvm_thread_context_t *src)
{
thread_context_move(uvm_thread_context(), src);
thread_context_non_interrupt_deinit(src);
}
uvm_thread_context_lock_t *uvm_thread_context_lock_get(void)
{
return thread_context_lock_of(uvm_thread_context());
}
void uvm_thread_context_lock_disable_tracking(void)
{
uvm_thread_context_lock_t *context_lock = thread_context_lock_of(uvm_thread_context());
if (context_lock == NULL)
return;
++context_lock->skip_lock_tracking;
UVM_ASSERT(context_lock->skip_lock_tracking != 0);
}
void uvm_thread_context_lock_enable_tracking(void)
{
uvm_thread_context_lock_t *context_lock = thread_context_lock_of(uvm_thread_context());
if (context_lock == NULL)
return;
UVM_ASSERT(context_lock->skip_lock_tracking > 0);
--context_lock->skip_lock_tracking;
}
|