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 mcast_port

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-6-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
58d33b4f cd74db6c

+4 -4
+4 -4
drivers/infiniband/core/multicast.c
··· 61 61 struct mcast_device *dev; 62 62 spinlock_t lock; 63 63 struct rb_root table; 64 - atomic_t refcount; 64 + refcount_t refcount; 65 65 struct completion comp; 66 66 u32 port_num; 67 67 }; ··· 178 178 179 179 static void deref_port(struct mcast_port *port) 180 180 { 181 - if (atomic_dec_and_test(&port->refcount)) 181 + if (refcount_dec_and_test(&port->refcount)) 182 182 complete(&port->comp); 183 183 } 184 184 ··· 589 589 kfree(group); 590 590 group = cur_group; 591 591 } else 592 - atomic_inc(&port->refcount); 592 + refcount_inc(&port->refcount); 593 593 found: 594 594 atomic_inc(&group->refcount); 595 595 spin_unlock_irqrestore(&port->lock, flags); ··· 840 840 spin_lock_init(&port->lock); 841 841 port->table = RB_ROOT; 842 842 init_completion(&port->comp); 843 - atomic_set(&port->refcount, 1); 843 + refcount_set(&port->refcount, 1); 844 844 ++count; 845 845 } 846 846