Skip to content

Commit

Permalink
net/mlx5: Limit non-privileged commands
Browse files Browse the repository at this point in the history
Limit non-privileged UID commands to half of the available command slots
when privileged UIDs are present.
Privileged throttle commands will not be limited.

Use an xarray to store privileged UIDs. Add insert and remove functions
for privileged UIDs management.

Non-user commands (with uid 0) are not limited.

Signed-off-by: Chiara Meiohas <cmeiohas@nvidia.com>
Reviewed-by: Moshe Shemesh <moshe@nvidia.com>
Reviewed-by: Tariq Toukan <tariqt@nvidia.com>
Link: https://patch.msgid.link/d2f3dd9a0dbad3c9f2b4bb0723837995e4e06de2.1740574103.git.leon@kernel.org
Signed-off-by: Leon Romanovsky <leon@kernel.org>
  • Loading branch information
Chiara Meiohas authored and Leon Romanovsky committed Mar 8, 2025
1 parent 0a34fad commit f9deed0
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 8 deletions.
85 changes: 77 additions & 8 deletions drivers/net/ethernet/mellanox/mlx5/core/cmd.c
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,11 @@ static u16 in_to_opcode(void *in)
return MLX5_GET(mbox_in, in, opcode);
}

static u16 in_to_uid(void *in)
{
return MLX5_GET(mbox_in, in, uid);
}

/* Returns true for opcodes that might be triggered very frequently and throttle
* the command interface. Limit their command slots usage.
*/
Expand Down Expand Up @@ -823,7 +828,7 @@ static void cmd_status_print(struct mlx5_core_dev *dev, void *in, void *out)

opcode = in_to_opcode(in);
op_mod = MLX5_GET(mbox_in, in, op_mod);
uid = MLX5_GET(mbox_in, in, uid);
uid = in_to_uid(in);
status = MLX5_GET(mbox_out, out, status);

if (!uid && opcode != MLX5_CMD_OP_DESTROY_MKEY &&
Expand Down Expand Up @@ -1871,6 +1876,17 @@ static int is_manage_pages(void *in)
return in_to_opcode(in) == MLX5_CMD_OP_MANAGE_PAGES;
}

static bool mlx5_has_privileged_uid(struct mlx5_core_dev *dev)
{
return !xa_empty(&dev->cmd.vars.privileged_uids);
}

static bool mlx5_cmd_is_privileged_uid(struct mlx5_core_dev *dev,
u16 uid)
{
return !!xa_load(&dev->cmd.vars.privileged_uids, uid);
}

/* Notes:
* 1. Callback functions may not sleep
* 2. Page queue commands do not support asynchrous completion
Expand All @@ -1882,6 +1898,8 @@ static int cmd_exec(struct mlx5_core_dev *dev, void *in, int in_size, void *out,
struct mlx5_cmd_msg *inb, *outb;
u16 opcode = in_to_opcode(in);
bool throttle_locked = false;
bool unpriv_locked = false;
u16 uid = in_to_uid(in);
int pages_queue;
gfp_t gfp;
u8 token;
Expand All @@ -1894,7 +1912,12 @@ static int cmd_exec(struct mlx5_core_dev *dev, void *in, int in_size, void *out,
/* The semaphore is already held for callback commands. It was
* acquired in mlx5_cmd_exec_cb()
*/
if (mlx5_cmd_is_throttle_opcode(opcode)) {
if (uid && mlx5_has_privileged_uid(dev)) {
if (!mlx5_cmd_is_privileged_uid(dev, uid)) {
unpriv_locked = true;
down(&dev->cmd.vars.unprivileged_sem);
}
} else if (mlx5_cmd_is_throttle_opcode(opcode)) {
throttle_locked = true;
down(&dev->cmd.vars.throttle_sem);
}
Expand Down Expand Up @@ -1943,6 +1966,9 @@ static int cmd_exec(struct mlx5_core_dev *dev, void *in, int in_size, void *out,
out_up:
if (throttle_locked)
up(&dev->cmd.vars.throttle_sem);
if (unpriv_locked)
up(&dev->cmd.vars.unprivileged_sem);

return err;
}

Expand Down Expand Up @@ -2105,17 +2131,21 @@ static void mlx5_cmd_exec_cb_handler(int status, void *_work)
struct mlx5_async_ctx *ctx;
struct mlx5_core_dev *dev;
bool throttle_locked;
bool unpriv_locked;

ctx = work->ctx;
dev = ctx->dev;
throttle_locked = work->throttle_locked;
unpriv_locked = work->unpriv_locked;
status = cmd_status_err(dev, status, work->opcode, work->op_mod, work->out);
work->user_callback(status, work);
/* Can't access "work" from this point on. It could have been freed in
* the callback.
*/
if (throttle_locked)
up(&dev->cmd.vars.throttle_sem);
if (unpriv_locked)
up(&dev->cmd.vars.unprivileged_sem);
if (atomic_dec_and_test(&ctx->num_inflight))
complete(&ctx->inflight_done);
}
Expand All @@ -2124,6 +2154,8 @@ int mlx5_cmd_exec_cb(struct mlx5_async_ctx *ctx, void *in, int in_size,
void *out, int out_size, mlx5_async_cbk_t callback,
struct mlx5_async_work *work)
{
struct mlx5_core_dev *dev = ctx->dev;
u16 uid;
int ret;

work->ctx = ctx;
Expand All @@ -2132,18 +2164,29 @@ int mlx5_cmd_exec_cb(struct mlx5_async_ctx *ctx, void *in, int in_size,
work->op_mod = MLX5_GET(mbox_in, in, op_mod);
work->out = out;
work->throttle_locked = false;
work->unpriv_locked = false;
uid = in_to_uid(in);

if (WARN_ON(!atomic_inc_not_zero(&ctx->num_inflight)))
return -EIO;

if (mlx5_cmd_is_throttle_opcode(in_to_opcode(in))) {
if (down_trylock(&ctx->dev->cmd.vars.throttle_sem)) {
if (uid && mlx5_has_privileged_uid(dev)) {
if (!mlx5_cmd_is_privileged_uid(dev, uid)) {
if (down_trylock(&dev->cmd.vars.unprivileged_sem)) {
ret = -EBUSY;
goto dec_num_inflight;
}
work->unpriv_locked = true;
}
} else if (mlx5_cmd_is_throttle_opcode(in_to_opcode(in))) {
if (down_trylock(&dev->cmd.vars.throttle_sem)) {
ret = -EBUSY;
goto dec_num_inflight;
}
work->throttle_locked = true;
}

ret = cmd_exec(ctx->dev, in, in_size, out, out_size,
ret = cmd_exec(dev, in, in_size, out, out_size,
mlx5_cmd_exec_cb_handler, work, false);
if (ret)
goto sem_up;
Expand All @@ -2152,7 +2195,9 @@ int mlx5_cmd_exec_cb(struct mlx5_async_ctx *ctx, void *in, int in_size,

sem_up:
if (work->throttle_locked)
up(&ctx->dev->cmd.vars.throttle_sem);
up(&dev->cmd.vars.throttle_sem);
if (work->unpriv_locked)
up(&dev->cmd.vars.unprivileged_sem);
dec_num_inflight:
if (atomic_dec_and_test(&ctx->num_inflight))
complete(&ctx->inflight_done);
Expand Down Expand Up @@ -2390,10 +2435,16 @@ int mlx5_cmd_enable(struct mlx5_core_dev *dev)
sema_init(&cmd->vars.sem, cmd->vars.max_reg_cmds);
sema_init(&cmd->vars.pages_sem, 1);
sema_init(&cmd->vars.throttle_sem, DIV_ROUND_UP(cmd->vars.max_reg_cmds, 2));
sema_init(&cmd->vars.unprivileged_sem,
DIV_ROUND_UP(cmd->vars.max_reg_cmds, 2));

xa_init(&cmd->vars.privileged_uids);

cmd->pool = dma_pool_create("mlx5_cmd", mlx5_core_dma_dev(dev), size, align, 0);
if (!cmd->pool)
return -ENOMEM;
if (!cmd->pool) {
err = -ENOMEM;
goto err_destroy_xa;
}

err = alloc_cmd_page(dev, cmd);
if (err)
Expand Down Expand Up @@ -2427,6 +2478,8 @@ int mlx5_cmd_enable(struct mlx5_core_dev *dev)
free_cmd_page(dev, cmd);
err_free_pool:
dma_pool_destroy(cmd->pool);
err_destroy_xa:
xa_destroy(&dev->cmd.vars.privileged_uids);
return err;
}

Expand All @@ -2439,10 +2492,26 @@ void mlx5_cmd_disable(struct mlx5_core_dev *dev)
destroy_msg_cache(dev);
free_cmd_page(dev, cmd);
dma_pool_destroy(cmd->pool);
xa_destroy(&dev->cmd.vars.privileged_uids);
}

void mlx5_cmd_set_state(struct mlx5_core_dev *dev,
enum mlx5_cmdif_state cmdif_state)
{
dev->cmd.state = cmdif_state;
}

int mlx5_cmd_add_privileged_uid(struct mlx5_core_dev *dev, u16 uid)
{
return xa_insert(&dev->cmd.vars.privileged_uids, uid,
xa_mk_value(uid), GFP_KERNEL);
}
EXPORT_SYMBOL(mlx5_cmd_add_privileged_uid);

void mlx5_cmd_remove_privileged_uid(struct mlx5_core_dev *dev, u16 uid)
{
void *data = xa_erase(&dev->cmd.vars.privileged_uids, uid);

WARN(!data, "Privileged UID %u does not exist\n", uid);
}
EXPORT_SYMBOL(mlx5_cmd_remove_privileged_uid);
5 changes: 5 additions & 0 deletions include/linux/mlx5/driver.h
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,8 @@ struct mlx5_cmd {
struct semaphore sem;
struct semaphore pages_sem;
struct semaphore throttle_sem;
struct semaphore unprivileged_sem;
struct xarray privileged_uids;
} vars;
enum mlx5_cmdif_state state;
void *cmd_alloc_buf;
Expand Down Expand Up @@ -990,6 +992,7 @@ struct mlx5_async_work {
u16 opcode; /* cmd opcode */
u16 op_mod; /* cmd op_mod */
u8 throttle_locked:1;
u8 unpriv_locked:1;
void *out; /* pointer to the cmd output buffer */
};

Expand Down Expand Up @@ -1020,6 +1023,8 @@ int mlx5_cmd_exec(struct mlx5_core_dev *dev, void *in, int in_size, void *out,
int mlx5_cmd_exec_polling(struct mlx5_core_dev *dev, void *in, int in_size,
void *out, int out_size);
bool mlx5_cmd_is_down(struct mlx5_core_dev *dev);
int mlx5_cmd_add_privileged_uid(struct mlx5_core_dev *dev, u16 uid);
void mlx5_cmd_remove_privileged_uid(struct mlx5_core_dev *dev, u16 uid);

void mlx5_core_uplink_netdev_set(struct mlx5_core_dev *mdev, struct net_device *netdev);
void mlx5_core_uplink_netdev_event_replay(struct mlx5_core_dev *mdev);
Expand Down

0 comments on commit f9deed0

Please sign in to comment.