Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

RDMA/core: Use refcount_t instead of atomic_t on refcount of iwcm_id_private

The refcount_t API will WARN on underflow and overflow of a reference
counter, and avoid use-after-free risks.

Link: https://lore.kernel.org/r/1622194663-2383-2-git-send-email-liweihang@huawei.com
Signed-off-by: Weihang Li <liweihang@huawei.com>
Signed-off-by: Jason Gunthorpe <jgg@nvidia.com>

authored by

Weihang Li and committed by
Jason Gunthorpe
60dff56d 61c7d826

+5 -6
+4 -5
drivers/infiniband/core/iwcm.c
··· 211 211 */ 212 212 static int iwcm_deref_id(struct iwcm_id_private *cm_id_priv) 213 213 { 214 - BUG_ON(atomic_read(&cm_id_priv->refcount)==0); 215 - if (atomic_dec_and_test(&cm_id_priv->refcount)) { 214 + if (refcount_dec_and_test(&cm_id_priv->refcount)) { 216 215 BUG_ON(!list_empty(&cm_id_priv->work_list)); 217 216 free_cm_id(cm_id_priv); 218 217 return 1; ··· 224 225 { 225 226 struct iwcm_id_private *cm_id_priv; 226 227 cm_id_priv = container_of(cm_id, struct iwcm_id_private, id); 227 - atomic_inc(&cm_id_priv->refcount); 228 + refcount_inc(&cm_id_priv->refcount); 228 229 } 229 230 230 231 static void rem_ref(struct iw_cm_id *cm_id) ··· 256 257 cm_id_priv->id.add_ref = add_ref; 257 258 cm_id_priv->id.rem_ref = rem_ref; 258 259 spin_lock_init(&cm_id_priv->lock); 259 - atomic_set(&cm_id_priv->refcount, 1); 260 + refcount_set(&cm_id_priv->refcount, 1); 260 261 init_waitqueue_head(&cm_id_priv->connect_wait); 261 262 init_completion(&cm_id_priv->destroy_comp); 262 263 INIT_LIST_HEAD(&cm_id_priv->work_list); ··· 1093 1094 } 1094 1095 } 1095 1096 1096 - atomic_inc(&cm_id_priv->refcount); 1097 + refcount_inc(&cm_id_priv->refcount); 1097 1098 if (list_empty(&cm_id_priv->work_list)) { 1098 1099 list_add_tail(&work->list, &cm_id_priv->work_list); 1099 1100 queue_work(iwcm_wq, &work->work);
+1 -1
drivers/infiniband/core/iwcm.h
··· 52 52 wait_queue_head_t connect_wait; 53 53 struct list_head work_list; 54 54 spinlock_t lock; 55 - atomic_t refcount; 55 + refcount_t refcount; 56 56 struct list_head work_free_list; 57 57 }; 58 58