Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 4 additions & 12 deletions src/uct/cuda/cuda_ipc/cuda_ipc_cache.c
Original file line number Diff line number Diff line change
Expand Up @@ -238,9 +238,7 @@ static void uct_cuda_ipc_cache_evict_lru(uct_cuda_ipc_cache_t *cache)
}

if (region->refcount > 0) {
/* In-use region -- pull off LRU, it will be re-added on release */
ucs_list_del(&region->lru_list);
region->in_lru = 0;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't it always 1 now?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, in_lru should always be 1 now because every region in cache should be in lru. However, I think it worth keeping the field and the assert ucs_assertv(region->in_lru) for debuggability and to detect potential future issues in CI, given the small memory cost of 1 byte (uint8_t)

/* In-use region -- keep on LRU, revisit on next eviction pass */
continue;
}

Expand Down Expand Up @@ -590,10 +588,6 @@ ucs_status_t uct_cuda_ipc_unmap_memhandle(pid_t pid, ucs_sys_ns_t pid_ns,
if (!region->refcount && !cache_enabled) {
ucs_assert(region->mapped_addr == mapped_addr);
uct_cuda_ipc_cache_region_destroy(cache, region);
} else if (!region->in_lru) {
/* Region becomes an eviction candidate -- add to LRU tail */
ucs_list_add_tail(&cache->lru_list, &region->lru_list);
region->in_lru = 1;
}

pthread_rwlock_unlock(&cache->lock);
Expand Down Expand Up @@ -648,12 +642,10 @@ UCS_PROFILE_FUNC(ucs_status_t, uct_cuda_ipc_map_memhandle,
UCS_PGT_REGION_FMT, cache->name, (void *)key->d_bptr,
key->b_len, UCS_PGT_REGION_ARG(&region->super));

/* Move to LRU tail (most recently used) */
if (region->in_lru) {
ucs_list_del(&region->lru_list);
}
/* Move to LRU tail (most recently used). Region is always on LRU
* while alive, so unconditional del/add_tail is safe. */
ucs_list_del(&region->lru_list);
ucs_list_add_tail(&cache->lru_list, &region->lru_list);
region->in_lru = 1;

*mapped_addr = region->mapped_addr;
ucs_assert(region->refcount < UINT64_MAX);
Expand Down
54 changes: 43 additions & 11 deletions test/gtest/uct/cuda/test_cuda_ipc_md.cc
Original file line number Diff line number Diff line change
Expand Up @@ -302,22 +302,32 @@ class test_cuda_ipc_cache_lru : public ucs::test {
void release_region(uct_cuda_ipc_cache_region_t *region) {
ASSERT_GE(region->refcount, 1UL);
region->refcount--;
if (!region->in_lru) {
ucs_list_add_tail(&m_cache->lru_list, &region->lru_list);
region->in_lru = 1;
}
}

void reacquire_region(uct_cuda_ipc_cache_region_t *region) {
/* Move to LRU tail (most recently used) */
if (region->in_lru) {
ucs_list_del(&region->lru_list);
}
ucs_list_del(&region->lru_list);
ucs_list_add_tail(&m_cache->lru_list, &region->lru_list);
region->in_lru = 1;
region->refcount++;
}

void destroy_region(uct_cuda_ipc_cache_region_t *region)
{
ucs_status_t status = ucs_pgtable_remove(&m_cache->pgtable,
&region->super);
ASSERT_EQ(UCS_OK, status);

ASSERT_TRUE(region->in_lru);
ucs_list_del(&region->lru_list);
region->in_lru = 0;

ASSERT_GT(m_cache->num_regions, 0UL);
m_cache->num_regions--;
m_cache->total_size -= region->key.b_len;

ucs_free(region);
}

void evict_lru() {
uct_cuda_ipc_cache_region_t *region, *tmp;

Expand All @@ -328,9 +338,6 @@ class test_cuda_ipc_cache_lru : public ucs::test {
}

if (region->refcount > 0) {
/* In-use -- pull off LRU, will be re-added on release */
ucs_list_del(&region->lru_list);
region->in_lru = 0;
continue;
}

Expand Down Expand Up @@ -530,3 +537,28 @@ UCS_TEST_F(test_cuda_ipc_cache_lru, unlimited) {
EXPECT_TRUE(pgtable_has(i));
}
}

UCS_TEST_F(test_cuda_ipc_cache_lru, stale_destroy_while_in_use) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you verify the test fails before the PR?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did now

/* Check that evict_lru does not pull in-use regions off
* the LRU. This could cause a failure if region is destroyed
* while not in LRU. */
create_cache(0 /* force eviction on every insert */, SIZE_MAX);

uct_cuda_ipc_cache_region_t *r1 = insert_region(0);
ASSERT_EQ(1UL, r1->refcount);
ASSERT_EQ(1, r1->in_lru);

evict_lru();

/* Core assertion: in-use region must stay on LRU after eviction */
EXPECT_EQ(1, r1->in_lru);
EXPECT_EQ(1UL, m_cache->num_regions);
EXPECT_TRUE(pgtable_has(0));

/* Simulate the stale-buffer_id destroy path */
destroy_region(r1);

EXPECT_EQ(0UL, m_cache->num_regions);
EXPECT_EQ(0UL, m_cache->total_size);
EXPECT_TRUE(ucs_list_is_empty(&m_cache->lru_list));
}
Loading