Skip to content

Commit

Permalink
freelist: use uintptr_t where appropriate
Browse files Browse the repository at this point in the history
stack-info: PR: #560, branch: aws-nslick/stack/7
  • Loading branch information
aws-nslick committed Sep 4, 2024
1 parent 66a5242 commit ffbace2
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 44 deletions.
10 changes: 5 additions & 5 deletions include/nccl_ofi_freelist.h
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ int nccl_ofi_freelist_add(nccl_ofi_freelist_t *freelist,
/*
* Set memcheck guards of freelist entry's user data to accessible but undefined
*/
static inline void nccl_ofi_freelist_entry_set_undefined(nccl_ofi_freelist_t *freelist, void *entry_p)
static inline void nccl_ofi_freelist_entry_set_undefined(nccl_ofi_freelist_t *freelist, uintptr_t entry_p)
{
size_t user_entry_size = freelist->entry_size - MEMCHECK_REDZONE_SIZE;

Expand Down Expand Up @@ -269,11 +269,11 @@ static inline void *nccl_ofi_freelist_entry_alloc(nccl_ofi_freelist_t *freelist)
}

entry = freelist->entries;
nccl_net_ofi_mem_defined_unaligned(entry, sizeof(*entry));
nccl_net_ofi_mem_defined_unaligned((uintptr_t)entry, sizeof(*entry));

freelist->entries = entry->next;
buf = entry->ptr;
nccl_ofi_freelist_entry_set_undefined(freelist, buf);
nccl_ofi_freelist_entry_set_undefined(freelist, (uintptr_t)buf);

cleanup:
nccl_net_ofi_mutex_unlock(&freelist->lock);
Expand All @@ -300,7 +300,7 @@ static inline void nccl_ofi_freelist_entry_free(nccl_ofi_freelist_t *freelist, v

if (freelist->have_reginfo) {
entry = (struct nccl_ofi_freelist_elem_t *)((char*)entry_p + freelist->reginfo_offset);
nccl_net_ofi_mem_defined_unaligned(entry, sizeof(*entry));
nccl_net_ofi_mem_defined_unaligned((uintptr_t)entry, sizeof(*entry));
} else {
entry = (struct nccl_ofi_freelist_elem_t *)entry_p;
entry->ptr = (void *)entry;
Expand All @@ -309,7 +309,7 @@ static inline void nccl_ofi_freelist_entry_free(nccl_ofi_freelist_t *freelist, v
entry->next = freelist->entries;
freelist->entries = entry;

nccl_net_ofi_mem_noaccess(entry_p, user_entry_size);
nccl_net_ofi_mem_noaccess((uintptr_t)entry_p, user_entry_size);

nccl_net_ofi_mutex_unlock(&freelist->lock);
}
Expand Down
30 changes: 15 additions & 15 deletions include/nccl_ofi_memcheck.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ static_assert(MEMCHECK_REDZONE_SIZE % MEMCHECK_GRANULARITY == 0,
* @param size
* The size of the memory area.
*/
static inline void nccl_net_ofi_mem_defined(void *data, size_t size);
static inline void nccl_net_ofi_mem_defined(uintptr_t data, size_t size);

/**
* Mark a memory area as having an undefined content. This can result
Expand All @@ -65,7 +65,7 @@ static inline void nccl_net_ofi_mem_defined(void *data, size_t size);
* @param size
* The size of the memory area.
*/
static inline void nccl_net_ofi_mem_undefined(void *data, size_t size);
static inline void nccl_net_ofi_mem_undefined(uintptr_t data, size_t size);

/**
* Mark a memory area as being invalid for read or write accesss. Any
Expand All @@ -77,38 +77,38 @@ static inline void nccl_net_ofi_mem_undefined(void *data, size_t size);
* @param size
* The size of the memory area.
*/
static inline void nccl_net_ofi_mem_noaccess(void *data, size_t size);
static inline void nccl_net_ofi_mem_noaccess(uintptr_t data, size_t size);

/**
* Same as nccl_net_ofi_mem_defined() except that guard is applied to
* memory region [NCCL_OFI_ROUND_DOWN(data, MEMCHECK_GRANULARITY), data + size).
*/
static inline void nccl_net_ofi_mem_defined_unaligned(void *data, size_t size)
static inline void nccl_net_ofi_mem_defined_unaligned(uintptr_t data, size_t size)
{
void *aligned = (void *)NCCL_OFI_ROUND_DOWN((uintptr_t)data, MEMCHECK_GRANULARITY);
size_t offset = data - aligned;
uintptr_t aligned = NCCL_OFI_ROUND_DOWN(data, MEMCHECK_GRANULARITY);
ptrdiff_t offset = data - aligned;
nccl_net_ofi_mem_defined(data - offset, size + offset);
}

/**
* Same as nccl_net_ofi_mem_undefined() except that guard is applied to
* memory region [NCCL_OFI_ROUND_DOWN(data, MEMCHECK_GRANULARITY), data + size).
*/
static inline void nccl_net_ofi_mem_undefined_unaligned(void *data, size_t size)
static inline void nccl_net_ofi_mem_undefined_unaligned(uintptr_t data, size_t size)
{
void *aligned = (void *)NCCL_OFI_ROUND_DOWN((uintptr_t)data, MEMCHECK_GRANULARITY);
size_t offset = data - aligned;
uintptr_t aligned = NCCL_OFI_ROUND_DOWN(data, MEMCHECK_GRANULARITY);
ptrdiff_t offset = data - aligned;
nccl_net_ofi_mem_undefined(data - offset, size + offset);
}

/**
* Same as nccl_net_ofi_mem_noaccess() except that guard is applied to
* memory region [NCCL_OFI_ROUND_DOWN(data, MEMCHECK_GRANULARITY), data + size).
*/
static inline void nccl_net_ofi_mem_noaccess_unaligned(void *data, size_t size)
static inline void nccl_net_ofi_mem_noaccess_unaligned(uintptr_t data, size_t size)
{
void *aligned = (void *)NCCL_OFI_ROUND_DOWN((uintptr_t)data, MEMCHECK_GRANULARITY);
size_t offset = data - aligned;
uintptr_t aligned = NCCL_OFI_ROUND_DOWN(data, MEMCHECK_GRANULARITY);
ptrdiff_t offset = data - aligned;
nccl_net_ofi_mem_noaccess(data - offset, size + offset);
}

Expand All @@ -127,7 +127,7 @@ static inline void nccl_net_ofi_mem_noaccess_unaligned(void *data, size_t size)
* @param size
* The size of the memory area.
*/
static inline void nccl_net_ofi_mem_create_mempool(void *handle, void *data, size_t size);
static inline void nccl_net_ofi_mem_create_mempool(void *handle, uintptr_t data, size_t size);

/**
* Destroy a memory allocator.
Expand All @@ -150,7 +150,7 @@ static inline void nccl_net_ofi_mem_destroy_mempool(void *handle);
* @param size
* The size of the memory area.
*/
static inline void nccl_net_ofi_mem_mempool_alloc(void *handle, void *data, size_t size);
static inline void nccl_net_ofi_mem_mempool_alloc(void *handle, uintptr_t data, size_t size);

/**
* Indicate that a deallocate has occured to a memory allocator. The
Expand All @@ -167,7 +167,7 @@ static inline void nccl_net_ofi_mem_mempool_alloc(void *handle, void *data, size
* @param size
* The size of the memory area.
*/
static inline void nccl_net_ofi_mem_mempool_free(void *handle, void *data, size_t size);
static inline void nccl_net_ofi_mem_mempool_free(void *handle, uintptr_t data, size_t size);

#ifdef __cplusplus
} // End extern "C"
Expand Down
6 changes: 3 additions & 3 deletions include/nccl_ofi_memcheck_asan.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ static inline void nccl_net_ofi_mem_noaccess(void *data, size_t size)
__asan_poison_memory_region(data, size);
}

static inline void nccl_net_ofi_mem_create_mempool(void *handle, void *data, size_t size)
static inline void nccl_net_ofi_mem_create_mempool(void *handle, uintptr_t data, size_t size)
{
nccl_net_ofi_mem_noaccess(data, size);
}
Expand All @@ -46,12 +46,12 @@ static inline void nccl_net_ofi_mem_destroy_mempool(void *handle)
/* Cannot posion without knowing mempool data and size */
}

static inline void nccl_net_ofi_mem_mempool_alloc(void *handle, void *data, size_t size)
static inline void nccl_net_ofi_mem_mempool_alloc(void *handle, uintptr_t data, size_t size)
{
nccl_net_ofi_mem_undefined(data, size);
}

static inline void nccl_net_ofi_mem_mempool_free(void *handle, void *data, size_t size)
static inline void nccl_net_ofi_mem_mempool_free(void *handle, uintptr_t data, size_t size)
{
nccl_net_ofi_mem_noaccess(data, size);
}
Expand Down
12 changes: 6 additions & 6 deletions include/nccl_ofi_memcheck_nop.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,31 +16,31 @@ extern "C" {
* By including this module, calls to the interface will result in nops.
*/

static inline void nccl_net_ofi_mem_defined(void *data, size_t size)
static inline void nccl_net_ofi_mem_defined(uintptr_t data, size_t size)
{
}

static inline void nccl_net_ofi_mem_undefined(void *data, size_t size)
static inline void nccl_net_ofi_mem_undefined(uintptr_t data, size_t size)
{
}

static inline void nccl_net_ofi_mem_noaccess(void *data, size_t size)
static inline void nccl_net_ofi_mem_noaccess(uintptr_t data, size_t size)
{
}

static inline void nccl_net_ofi_mem_create_mempool(void *handle, void *data, size_t size)
static inline void nccl_net_ofi_mem_create_mempool(void *handle, uintptr_t data, size_t size)
{
}

static inline void nccl_net_ofi_mem_destroy_mempool(void *handle)
{
}

static inline void nccl_net_ofi_mem_mempool_alloc(void *handle, void *data, size_t size)
static inline void nccl_net_ofi_mem_mempool_alloc(void *handle, uintptr_t data, size_t size)
{
}

static inline void nccl_net_ofi_mem_mempool_free(void *handle, void *data, size_t size)
static inline void nccl_net_ofi_mem_mempool_free(void *handle, uintptr_t data, size_t size)
{
}

Expand Down
12 changes: 6 additions & 6 deletions include/nccl_ofi_memcheck_valgrind.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,22 @@ extern "C" {
#include <valgrind/valgrind.h>
#include <valgrind/memcheck.h>

static inline void nccl_net_ofi_mem_defined(void *data, size_t size)
static inline void nccl_net_ofi_mem_defined(uintptr_t data, size_t size)
{
VALGRIND_MAKE_MEM_DEFINED(data, size);
}

static inline void nccl_net_ofi_mem_undefined(void *data, size_t size)
static inline void nccl_net_ofi_mem_undefined(uintptr_t data, size_t size)
{
VALGRIND_MAKE_MEM_UNDEFINED(data, size);
}

static inline void nccl_net_ofi_mem_noaccess(void *data, size_t size)
static inline void nccl_net_ofi_mem_noaccess(uintptr_t data, size_t size)
{
VALGRIND_MAKE_MEM_NOACCESS(data, size);
}

static inline void nccl_net_ofi_mem_create_mempool(void *handle, void *data, size_t size)
static inline void nccl_net_ofi_mem_create_mempool(void *handle, uintptr_t data, size_t size)
{
nccl_net_ofi_mem_noaccess(data, size);
VALGRIND_CREATE_MEMPOOL(handle, 0, 0);
Expand All @@ -38,12 +38,12 @@ static inline void nccl_net_ofi_mem_destroy_mempool(void *handle)
VALGRIND_DESTROY_MEMPOOL(handle);
}

static inline void nccl_net_ofi_mem_mempool_alloc(void *handle, void *data, size_t size)
static inline void nccl_net_ofi_mem_mempool_alloc(void *handle, uintptr_t data, size_t size)
{
VALGRIND_MEMPOOL_ALLOC(handle, data, size);
}

static inline void nccl_net_ofi_mem_mempool_free(void *handle, void *data, size_t size)
static inline void nccl_net_ofi_mem_mempool_free(void *handle, uintptr_t data, size_t size)
{
VALGRIND_MEMPOOL_FREE(handle, data);
}
Expand Down
16 changes: 8 additions & 8 deletions src/nccl_ofi_freelist.c
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ int nccl_ofi_freelist_fini(nccl_ofi_freelist_t *freelist)

while (freelist->blocks) {
struct nccl_ofi_freelist_block_t *block = freelist->blocks;
nccl_net_ofi_mem_defined(block, sizeof(struct nccl_ofi_freelist_block_t));
nccl_net_ofi_mem_defined((uintptr_t)block, sizeof(struct nccl_ofi_freelist_block_t));
void *memory = block->memory;
size_t size = block->memory_size;
freelist->blocks = block->next;
Expand All @@ -188,7 +188,7 @@ int nccl_ofi_freelist_fini(nccl_ofi_freelist_t *freelist)
* needs to be performed manually since reallocation
* of the same memory via mmap() is invisible to
* ASAN. */
nccl_net_ofi_mem_undefined(memory, size);
nccl_net_ofi_mem_undefined((uintptr_t)memory, size);
ret = nccl_net_ofi_dealloc_mr_buffer(memory, size);
if (ret != 0) {
NCCL_OFI_WARN("Unable to deallocate MR buffer(%d)", ret);
Expand Down Expand Up @@ -248,9 +248,9 @@ int nccl_ofi_freelist_add(nccl_ofi_freelist_t *freelist,
char *b_end = (char *)(block + 1);
char *b_end_aligned = (char *)NCCL_OFI_ROUND_DOWN((uintptr_t)b_end,
(uintptr_t)MEMCHECK_GRANULARITY);
nccl_net_ofi_mem_noaccess(b_end_aligned,
nccl_net_ofi_mem_noaccess((uintptr_t)b_end_aligned,
block_mem_size - (b_end_aligned - buffer));
nccl_net_ofi_mem_undefined(b_end_aligned, b_end - b_end_aligned);
nccl_net_ofi_mem_undefined((uintptr_t)b_end_aligned, b_end - b_end_aligned);

if (freelist->regmr_fn) {
/*
Expand All @@ -270,7 +270,7 @@ int nccl_ofi_freelist_add(nccl_ofi_freelist_t *freelist,
* needs to be performed manually since reallocation
* of the same memory via mmap() is invisible to
* ASAN. */
nccl_net_ofi_mem_undefined(block->memory, block_mem_size);
nccl_net_ofi_mem_undefined((uintptr_t)block->memory, block_mem_size);
int dret = nccl_net_ofi_dealloc_mr_buffer(block->memory, block_mem_size);
if (dret != 0) {
NCCL_OFI_WARN("Unable to deallocate MR buffer(%d)", ret);
Expand All @@ -288,7 +288,7 @@ int nccl_ofi_freelist_add(nccl_ofi_freelist_t *freelist,
size_t user_entry_size = freelist->entry_size - freelist->memcheck_redzone_size;

/* Add redzone before entry */
nccl_net_ofi_mem_noaccess(buffer, freelist->memcheck_redzone_size);
nccl_net_ofi_mem_noaccess((uintptr_t)buffer, freelist->memcheck_redzone_size);
buffer += freelist->memcheck_redzone_size;

if (freelist->have_reginfo) {
Expand All @@ -306,13 +306,13 @@ int nccl_ofi_freelist_add(nccl_ofi_freelist_t *freelist,
freelist->entries = entry;
freelist->num_allocated_entries++;

nccl_net_ofi_mem_noaccess(entry->ptr, user_entry_size);
nccl_net_ofi_mem_noaccess((uintptr_t)entry->ptr, user_entry_size);

buffer += user_entry_size;
}

/* Block structure will not be accessed until freelist is destroyed */
nccl_net_ofi_mem_noaccess(block, sizeof(struct nccl_ofi_freelist_block_t));
nccl_net_ofi_mem_noaccess((uintptr_t)block, sizeof(struct nccl_ofi_freelist_block_t));

return 0;
}
2 changes: 1 addition & 1 deletion src/nccl_ofi_rdma.c
Original file line number Diff line number Diff line change
Expand Up @@ -4313,7 +4313,7 @@ static int post_bounce_buffer(nccl_net_ofi_rdma_req_t *req,
* gets re-posted */
nccl_net_ofi_rdma_ep_t *ep = bounce_data->ep;
nccl_ofi_freelist_entry_set_undefined(ep->bounce_buff_fl,
bounce_fl_item);
(uintptr_t)bounce_fl_item);

req->state = NCCL_OFI_RDMA_REQ_CREATED;
ssize_t rc =
Expand Down

0 comments on commit ffbace2

Please sign in to comment.