Skip to content

Commit a4b51a9

Browse files
ereshetovadavem330
authored andcommitted
drivers, net, mlx5: convert mlx5_cq.refcount from atomic_t to refcount_t
atomic_t variables are currently used to implement reference counters with the following properties: - counter is initialized to 1 using atomic_set() - a resource is freed upon counter reaching zero - once counter reaches zero, its further increments aren't allowed - counter schema uses basic atomic operations (set, inc, inc_not_zero, dec_and_test, etc.) Such atomic variables should be converted to a newly provided refcount_t type and API that prevents accidental counter overflows and underflows. This is important since overflows and underflows can lead to use-after-free situation and be exploitable. The variable mlx5_cq.refcount is used as pure reference counter. Convert it to refcount_t and fix up the operations. Suggested-by: Kees Cook <keescook@chromium.org> Reviewed-by: David Windsor <dwindsor@gmail.com> Reviewed-by: Hans Liljestrand <ishkamiel@gmail.com> Signed-off-by: Elena Reshetova <elena.reshetova@intel.com> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent 17ac99b commit a4b51a9

2 files changed

Lines changed: 10 additions & 10 deletions

File tree

  • drivers/net/ethernet/mellanox/mlx5/core
  • include/linux/mlx5

‎drivers/net/ethernet/mellanox/mlx5/core/cq.c‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ void mlx5_cq_tasklet_cb(unsigned long data)
5858
tasklet_ctx.list) {
5959
list_del_init(&mcq->tasklet_ctx.list);
6060
mcq->tasklet_ctx.comp(mcq);
61-
if (atomic_dec_and_test(&mcq->refcount))
61+
if (refcount_dec_and_test(&mcq->refcount))
6262
complete(&mcq->free);
6363
if (time_after(jiffies, end))
6464
break;
@@ -80,7 +80,7 @@ static void mlx5_add_cq_to_tasklet(struct mlx5_core_cq *cq)
8080
* still arrive.
8181
*/
8282
if (list_empty_careful(&cq->tasklet_ctx.list)) {
83-
atomic_inc(&cq->refcount);
83+
refcount_inc(&cq->refcount);
8484
list_add_tail(&cq->tasklet_ctx.list, &tasklet_ctx->list);
8585
}
8686
spin_unlock_irqrestore(&tasklet_ctx->lock, flags);
@@ -94,7 +94,7 @@ void mlx5_cq_completion(struct mlx5_core_dev *dev, u32 cqn)
9494
spin_lock(&table->lock);
9595
cq = radix_tree_lookup(&table->tree, cqn);
9696
if (likely(cq))
97-
atomic_inc(&cq->refcount);
97+
refcount_inc(&cq->refcount);
9898
spin_unlock(&table->lock);
9999

100100
if (!cq) {
@@ -106,7 +106,7 @@ void mlx5_cq_completion(struct mlx5_core_dev *dev, u32 cqn)
106106

107107
cq->comp(cq);
108108

109-
if (atomic_dec_and_test(&cq->refcount))
109+
if (refcount_dec_and_test(&cq->refcount))
110110
complete(&cq->free);
111111
}
112112

@@ -119,7 +119,7 @@ void mlx5_cq_event(struct mlx5_core_dev *dev, u32 cqn, int event_type)
119119

120120
cq = radix_tree_lookup(&table->tree, cqn);
121121
if (cq)
122-
atomic_inc(&cq->refcount);
122+
refcount_inc(&cq->refcount);
123123

124124
spin_unlock(&table->lock);
125125

@@ -130,7 +130,7 @@ void mlx5_cq_event(struct mlx5_core_dev *dev, u32 cqn, int event_type)
130130

131131
cq->event(cq, event_type);
132132

133-
if (atomic_dec_and_test(&cq->refcount))
133+
if (refcount_dec_and_test(&cq->refcount))
134134
complete(&cq->free);
135135
}
136136

@@ -159,7 +159,7 @@ int mlx5_core_create_cq(struct mlx5_core_dev *dev, struct mlx5_core_cq *cq,
159159
cq->cqn = MLX5_GET(create_cq_out, out, cqn);
160160
cq->cons_index = 0;
161161
cq->arm_sn = 0;
162-
atomic_set(&cq->refcount, 1);
162+
refcount_set(&cq->refcount, 1);
163163
init_completion(&cq->free);
164164
if (!cq->comp)
165165
cq->comp = mlx5_add_cq_to_tasklet;
@@ -222,7 +222,7 @@ int mlx5_core_destroy_cq(struct mlx5_core_dev *dev, struct mlx5_core_cq *cq)
222222
synchronize_irq(cq->irqn);
223223

224224
mlx5_debug_cq_remove(dev, cq);
225-
if (atomic_dec_and_test(&cq->refcount))
225+
if (refcount_dec_and_test(&cq->refcount))
226226
complete(&cq->free);
227227
wait_for_completion(&cq->free);
228228

‎include/linux/mlx5/cq.h‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,15 @@
3535

3636
#include <rdma/ib_verbs.h>
3737
#include <linux/mlx5/driver.h>
38-
38+
#include <linux/refcount.h>
3939

4040
struct mlx5_core_cq {
4141
u32 cqn;
4242
int cqe_sz;
4343
__be32 *set_ci_db;
4444
__be32 *arm_db;
4545
struct mlx5_uars_page *uar;
46-
atomic_t refcount;
46+
refcount_t refcount;
4747
struct completion free;
4848
unsigned vector;
4949
unsigned int irqn;

0 commit comments

Comments
 (0)