Changeset View
Changeset View
Standalone View
Standalone View
contrib/ofed/libmlx5/buf.c
Show First 20 Lines • Show All 314 Lines • ▼ Show 20 Lines | static void free_huge_buf(struct mlx5_context *ctx, struct mlx5_buf *buf) | ||||
if (bitmap_empty(&buf->hmem->bitmap)) { | if (bitmap_empty(&buf->hmem->bitmap)) { | ||||
TAILQ_REMOVE(&ctx->hugetlb_list, buf->hmem, entry); | TAILQ_REMOVE(&ctx->hugetlb_list, buf->hmem, entry); | ||||
mlx5_spin_unlock(&ctx->hugetlb_lock); | mlx5_spin_unlock(&ctx->hugetlb_lock); | ||||
free_huge_mem(buf->hmem); | free_huge_mem(buf->hmem); | ||||
} else | } else | ||||
mlx5_spin_unlock(&ctx->hugetlb_lock); | mlx5_spin_unlock(&ctx->hugetlb_lock); | ||||
} | } | ||||
void mlx5_free_buf_extern(struct mlx5_context *ctx, struct mlx5_buf *buf) | |||||
{ | |||||
ibv_dofork_range(buf->buf, buf->length); | |||||
ctx->extern_alloc.free(buf->buf, ctx->extern_alloc.data); | |||||
} | |||||
int mlx5_alloc_buf_extern(struct mlx5_context *ctx, struct mlx5_buf *buf, | |||||
size_t size) | |||||
{ | |||||
void *addr; | |||||
addr = ctx->extern_alloc.alloc(size, ctx->extern_alloc.data); | |||||
if (addr || size == 0) { | |||||
if (ibv_dontfork_range(addr, size)) { | |||||
mlx5_dbg(stderr, MLX5_DBG_CONTIG, | |||||
"External mode dontfork_range failed\n"); | |||||
ctx->extern_alloc.free(addr, | |||||
ctx->extern_alloc.data); | |||||
return -1; | |||||
} | |||||
buf->buf = addr; | |||||
buf->length = size; | |||||
buf->type = MLX5_ALLOC_TYPE_EXTERNAL; | |||||
return 0; | |||||
} | |||||
mlx5_dbg(stderr, MLX5_DBG_CONTIG, "External alloc failed\n"); | |||||
return -1; | |||||
} | |||||
int mlx5_alloc_prefered_buf(struct mlx5_context *mctx, | int mlx5_alloc_prefered_buf(struct mlx5_context *mctx, | ||||
struct mlx5_buf *buf, | struct mlx5_buf *buf, | ||||
size_t size, int page_size, | size_t size, int page_size, | ||||
enum mlx5_alloc_type type, | enum mlx5_alloc_type type, | ||||
const char *component) | const char *component) | ||||
{ | { | ||||
int ret; | int ret; | ||||
Show All 26 Lines | if (!ret) | ||||
return 0; | return 0; | ||||
if (type == MLX5_ALLOC_TYPE_CONTIG) | if (type == MLX5_ALLOC_TYPE_CONTIG) | ||||
return -1; | return -1; | ||||
mlx5_dbg(stderr, MLX5_DBG_CONTIG, | mlx5_dbg(stderr, MLX5_DBG_CONTIG, | ||||
"Contig allocation failed, fallback to default mode\n"); | "Contig allocation failed, fallback to default mode\n"); | ||||
} | } | ||||
if (type == MLX5_ALLOC_TYPE_EXTERNAL) | |||||
return mlx5_alloc_buf_extern(mctx, buf, size); | |||||
return mlx5_alloc_buf(buf, size, page_size); | return mlx5_alloc_buf(buf, size, page_size); | ||||
} | } | ||||
int mlx5_free_actual_buf(struct mlx5_context *ctx, struct mlx5_buf *buf) | int mlx5_free_actual_buf(struct mlx5_context *ctx, struct mlx5_buf *buf) | ||||
{ | { | ||||
int err = 0; | int err = 0; | ||||
switch (buf->type) { | switch (buf->type) { | ||||
case MLX5_ALLOC_TYPE_ANON: | case MLX5_ALLOC_TYPE_ANON: | ||||
mlx5_free_buf(buf); | mlx5_free_buf(buf); | ||||
break; | break; | ||||
case MLX5_ALLOC_TYPE_HUGE: | case MLX5_ALLOC_TYPE_HUGE: | ||||
free_huge_buf(ctx, buf); | free_huge_buf(ctx, buf); | ||||
break; | break; | ||||
case MLX5_ALLOC_TYPE_CONTIG: | case MLX5_ALLOC_TYPE_CONTIG: | ||||
mlx5_free_buf_contig(ctx, buf); | mlx5_free_buf_contig(ctx, buf); | ||||
break; | break; | ||||
case MLX5_ALLOC_TYPE_EXTERNAL: | |||||
mlx5_free_buf_extern(ctx, buf); | |||||
break; | |||||
default: | default: | ||||
fprintf(stderr, "Bad allocation type\n"); | fprintf(stderr, "Bad allocation type\n"); | ||||
} | } | ||||
return err; | return err; | ||||
} | } | ||||
/* This function computes log2(v) rounded up. | /* This function computes log2(v) rounded up. | ||||
Show All 16 Lines | for (i = 4; i >= 0; i--) { | ||||
} | } | ||||
} | } | ||||
/* Rounding up if required */ | /* Rounding up if required */ | ||||
r += !!(input_val & ((1 << r) - 1)); | r += !!(input_val & ((1 << r) - 1)); | ||||
return r; | return r; | ||||
} | } | ||||
void mlx5_get_alloc_type(const char *component, | bool mlx5_is_extern_alloc(struct mlx5_context *context) | ||||
{ | |||||
return context->extern_alloc.alloc && context->extern_alloc.free; | |||||
} | |||||
void mlx5_get_alloc_type(struct mlx5_context *context, | |||||
const char *component, | |||||
enum mlx5_alloc_type *alloc_type, | enum mlx5_alloc_type *alloc_type, | ||||
enum mlx5_alloc_type default_type) | enum mlx5_alloc_type default_type) | ||||
{ | { | ||||
char *env_value; | char *env_value; | ||||
char name[128]; | char name[128]; | ||||
if (mlx5_is_extern_alloc(context)) { | |||||
*alloc_type = MLX5_ALLOC_TYPE_EXTERNAL; | |||||
return; | |||||
} | |||||
snprintf(name, sizeof(name), "%s_ALLOC_TYPE", component); | snprintf(name, sizeof(name), "%s_ALLOC_TYPE", component); | ||||
*alloc_type = default_type; | *alloc_type = default_type; | ||||
env_value = getenv(name); | env_value = getenv(name); | ||||
if (env_value) { | if (env_value) { | ||||
if (!strcasecmp(env_value, "ANON")) | if (!strcasecmp(env_value, "ANON")) | ||||
▲ Show 20 Lines • Show All 140 Lines • Show Last 20 Lines |