RDMA/rxe: Fix rxe_alloc() bug
Subsystems (2)
cover RDMA/rxe: Fix rxe_alloc() bug
This short series of patches corrects a bug introduced and then addresses several issues raised during discussion of the bug and the proposed fix.
The first patch fixes a real bug but the other four are stylistic and cleanup changes.
*** BLURB HERE ***
Bob Pearson (5):
RDMA/rxe: Fix bug in rxe_alloc
RDMA/rxe: Fix misleading comments and names RDMA/rxe: Remove RXE_POOL_ATOMIC
RDMA/rxe: Remove calls to ib_device_try_get/put RDMA/rxe: Remove unneeded pool->flag
drivers/infiniband/sw/rxe/rxe_mcast.c | 8 +-- drivers/infiniband/sw/rxe/rxe_pool.c | 90 ++++++++++----------------- drivers/infiniband/sw/rxe/rxe_pool.h | 62 ++++++++---------- 3 files changed, 63 insertions(+), 97 deletions(-)
Signature · 1 line
2.27.0
PATCH 1/5 RDMA/rxe: Fix bug in rxe_alloc
A recent patch which added an 'unlocked' version of rxe_alloc introduced a bug causing kzalloc(..., GFP_KERNEL) to be called while holding a spin lock. This patch corrects that error.
rxe_alloc_nl() should always be called while holding the pool->pool_lock so the 2nd argument to kzalloc there should be GFP_ATOMIC.
rxe_alloc() prior to the change only locked the code around checking that pool->state is RXE_POOL_STATE_VALID to avoid races between working threads and a thread shutting down the rxe driver. This patch reverts rxe_alloc() to this behavior so the lock is not held when kzalloc() is called.
--- drivers/infiniband/sw/rxe/rxe_pool.c | 41 ++++++++++++++++++++++++---- 1 file changed, 35 insertions(+), 6 deletions(-)
drivers/infiniband/sw/rxe/rxe_pool.c +35 -6
@@ -343,8 +343,6 @@ void *rxe_alloc_nl(struct rxe_pool *pool)struct rxe_pool_entry *elem;u8 *obj; -might_sleep_if (!(pool->flags &RXE_POOL_ATOMIC )); -if (pool->state !=RXE_POOL_STATE_VALID )return NULL ;
@@ -356,8 +354,7 @@ void *rxe_alloc_nl(struct rxe_pool *pool)if (atomic_inc_return (&pool->num_elem) > pool->max_elem) goto out_cnt; - obj =kzalloc (info->size, (pool->flags &RXE_POOL_ATOMIC ) ? -GFP_ATOMIC :GFP_KERNEL ); + obj =kzalloc (info->size,GFP_ATOMIC );if (!obj) goto out_cnt;
@@ -378,14 +375,46 @@ void *rxe_alloc_nl(struct rxe_pool *pool)void *rxe_alloc (struct rxe_pool *pool) { -u8 *obj;unsigned long flags; +struct rxe_type_info *info = &rxe_type_info[pool->type]; +struct rxe_pool_entry *elem; +u8 *obj; + +might_sleep_if (!(pool->flags &RXE_POOL_ATOMIC ));read_lock_irqsave (&pool->pool_lock, flags); - obj =rxe_alloc_nl (pool); +if (pool->state !=RXE_POOL_STATE_VALID ) { +read_unlock_irqrestore (&pool->pool_lock, flags); +return NULL ; + } + +kref_get (&pool->ref_cnt);read_unlock_irqrestore (&pool->pool_lock, flags); +if (!ib_device_try_get (&pool->rxe->ib_dev)) + goto out_put_pool; + +if (atomic_inc_return (&pool->num_elem) > pool->max_elem) + goto out_cnt; + + obj =kzalloc (info->size, (pool->flags &RXE_POOL_ATOMIC ) ? +GFP_ATOMIC :GFP_KERNEL ); +if (!obj) + goto out_cnt; + + elem = (struct rxe_pool_entry *)(obj + info->elem_offset); + + elem->pool = pool; +kref_init (&elem->ref_cnt); +return obj; + +out_cnt: +atomic_dec (&pool->num_elem); +ib_device_put (&pool->rxe->ib_dev); +out_put_pool: +rxe_pool_put (pool); +return NULL ; }int __rxe_add_to_pool (struct rxe_pool *pool,struct rxe_pool_entry *elem)
Signature · 1 line
2.27.0
PATCH 2/5 RDMA/rxe: Fix misleading comments and names
In reply to Bob PearsonThe names and comments of the 'unlocked' pool APIs are very misleading and not what was intended. This patch replaces 'rxe_xxx_nl' with 'rxe_xxx__' with comments indicating that the caller is expected to hold the rxe pool lock.
--- drivers/infiniband/sw/rxe/rxe_mcast.c | 8 ++-- drivers/infiniband/sw/rxe/rxe_pool.c | 22 +++++------ drivers/infiniband/sw/rxe/rxe_pool.h | 55 +++++++++++++-------------- 3 files changed, 42 insertions(+), 43 deletions(-)
drivers/infiniband/sw/rxe/rxe_mcast.c +4 -4
@@ -15,18 +15,18 @@ static struct rxe_mc_grp *create_grp(struct rxe_dev *rxe,int err;struct rxe_mc_grp *grp; - grp =rxe_alloc_nl (&rxe->mc_grp_pool); + grp =rxe_alloc__ (&rxe->mc_grp_pool);if (!grp)return ERR_PTR (-ENOMEM );INIT_LIST_HEAD (&grp->qp_list);spin_lock_init (&grp->mcg_lock); grp->rxe = rxe; -rxe_add_key_nl (grp, mgid); +rxe_add_key__ (grp, mgid); err =rxe_mcast_add (rxe, mgid);if (unlikely (err)) { -rxe_drop_key_nl (grp); +rxe_drop_key__ (grp);rxe_drop_ref (grp);return ERR_PTR (err); }
@@ -47,7 +47,7 @@ int rxe_mcast_get_grp(struct rxe_dev *rxe, union ib_gid *mgid,write_lock_irqsave (&pool->pool_lock, flags); - grp =rxe_pool_get_key_nl (pool, mgid); + grp =rxe_pool_get_key__ (pool, mgid);if (grp) goto done;
drivers/infiniband/sw/rxe/rxe_pool.c +11 -11
@@ -266,7 +266,7 @@ static void insert_key(struct rxe_pool *pool, struct rxe_pool_entry *new)return ; } -void __rxe_add_key_nl (struct rxe_pool_entry *elem,void *key) +void __rxe_add_key__ (struct rxe_pool_entry *elem,void *key) {struct rxe_pool *pool = elem->pool;
@@ -280,11 +280,11 @@ void __rxe_add_key(struct rxe_pool_entry *elem, void *key)unsigned long flags;write_lock_irqsave (&pool->pool_lock, flags); -__rxe_add_key_nl (elem, key); +__rxe_add_key__ (elem, key);write_unlock_irqrestore (&pool->pool_lock, flags); } -void __rxe_drop_key_nl (struct rxe_pool_entry *elem) +void __rxe_drop_key__ (struct rxe_pool_entry *elem) {struct rxe_pool *pool = elem->pool;
@@ -297,11 +297,11 @@ void __rxe_drop_key(struct rxe_pool_entry *elem)unsigned long flags;write_lock_irqsave (&pool->pool_lock, flags); -__rxe_drop_key_nl (elem); +__rxe_drop_key__ (elem);write_unlock_irqrestore (&pool->pool_lock, flags); } -void __rxe_add_index_nl (struct rxe_pool_entry *elem) +void __rxe_add_index__ (struct rxe_pool_entry *elem) {struct rxe_pool *pool = elem->pool;
@@ -315,11 +315,11 @@ void __rxe_add_index(struct rxe_pool_entry *elem)unsigned long flags;write_lock_irqsave (&pool->pool_lock, flags); -__rxe_add_index_nl (elem); +__rxe_add_index__ (elem);write_unlock_irqrestore (&pool->pool_lock, flags); } -void __rxe_drop_index_nl (struct rxe_pool_entry *elem) +void __rxe_drop_index__ (struct rxe_pool_entry *elem) {struct rxe_pool *pool = elem->pool;
@@ -333,11 +333,11 @@ void __rxe_drop_index(struct rxe_pool_entry *elem)unsigned long flags;write_lock_irqsave (&pool->pool_lock, flags); -__rxe_drop_index_nl (elem); +__rxe_drop_index__ (elem);write_unlock_irqrestore (&pool->pool_lock, flags); } -void *rxe_alloc_nl (struct rxe_pool *pool) +void *rxe_alloc__ (struct rxe_pool *pool) {struct rxe_type_info *info = &rxe_type_info[pool->type];struct rxe_pool_entry *elem;
@@ -507,7 +507,7 @@ void *rxe_pool_get_index(struct rxe_pool *pool, u32 index)return obj; } -void *rxe_pool_get_key_nl (struct rxe_pool *pool,void *key) +void *rxe_pool_get_key__ (struct rxe_pool *pool,void *key) {struct rxe_type_info *info = &rxe_type_info[pool->type];struct rb_node *node;
@@ -551,7 +551,7 @@ void *rxe_pool_get_key(struct rxe_pool *pool, void *key)unsigned long flags;read_lock_irqsave (&pool->pool_lock, flags); - obj =rxe_pool_get_key_nl (pool, key); + obj =rxe_pool_get_key__ (pool, key);read_unlock_irqrestore (&pool->pool_lock, flags);return obj;
drivers/infiniband/sw/rxe/rxe_pool.h +27 -28
98 lines not on this page · raw message
Signature · 1 line
2.27.0
Jason Gunthorpe
maintainerRe: [PATCH for-next 2/5] RDMA/rxe: Fix misleading comments and names
Quoted 18 lines · Bob Pearson
The names and comments of the 'unlocked' pool APIs are very misleading and not what was intended. This patch replaces '
rxe_xxx_nl' with 'rxe_xxx__' with comments indicating that the caller is expected to hold the rxe pool lock.Reported-by Hillf Danton <[email protected]>Signed-off-by Bob Pearson <[email protected]>--- drivers/infiniband/sw/rxe/rxe_mcast.c | 8 ++-- drivers/infiniband/sw/rxe/rxe_pool.c | 22 +++++------ drivers/infiniband/sw/rxe/rxe_pool.h | 55 +++++++++++++-------------- 3 files changed, 42 insertions(+), 43 deletions(-)drivers/infiniband/sw/rxe/rxe_mcast.c +0
-0
drivers/infiniband/sw/rxe/rxe_mcast.c static struct rxe_mc_grp *create_grp(struct rxe_dev *rxe, · line 18
Quoted 5 lines
drivers/infiniband/sw/rxe/rxe_mcast.c +1
-1@@ -15,18 +15,18 @@ static struct rxe_mc_grp *create_grp(struct rxe_dev *rxe,int err;struct rxe_mc_grp *grp; - grp =rxe_alloc_nl (&rxe->mc_grp_pool); + grp =rxe_alloc__ (&rxe->mc_grp_pool);
Everything else seems fine, but this trailing __ is too weird
If a lock is supposed to be held then name it foo_locked() or locked_()
If it auto-locks then name it foo()
If there is some #define wrapper then it is
#define foo() __foo()
Jason
RE: [PATCH for-next 2/5] RDMA/rxe: Fix misleading comments and names
-----Original Message-----
From: Jason Gunthorpe <[email protected]>
Sent: Friday, January 22, 2021 1:27 PM
To: Bob Pearson <[email protected]>
Cc: [email protected]; [email protected]; Pearson, Robert B <[email protected]>; Hillf Danton <[email protected]>
Subject: Re: [PATCH for-next 2/5] RDMA/rxe: Fix misleading comments and names
Quoted 19 lines · Bob Pearson
The names and comments of the 'unlocked' pool APIs are very misleading and not what was intended. This patch replaces '
rxe_xxx_nl' with 'rxe_xxx__' with comments indicating that the caller is expected to hold the rxe pool lock.Reported-by Hillf Danton <[email protected]>Signed-off-by Bob Pearson <[email protected]>--- drivers/infiniband/sw/rxe/rxe_mcast.c | 8 ++-- drivers/infiniband/sw/rxe/rxe_pool.c | 22 +++++------ drivers/infiniband/sw/rxe/rxe_pool.h | 55 +++++++++++++-------------- 3 files changed, 42 insertions(+), 43 deletions(-)drivers/infiniband/sw/rxe/rxe_mcast.c +0
-0
drivers/infiniband/sw/rxe/rxe_mcast.c static struct rxe_mc_grp *create_grp(struct rxe_dev *rxe, · line 18
Quoted 5 lines
drivers/infiniband/sw/rxe/rxe_mcast.c +1
-1@@ -15,18 +15,18 @@ static struct rxe_mc_grp *create_grp(struct rxe_dev *rxe,int err;struct rxe_mc_grp *grp; - grp =rxe_alloc_nl (&rxe->mc_grp_pool); + grp =rxe_alloc__ (&rxe->mc_grp_pool);
Everything else seems fine, but this trailing __ is too weird
If a lock is supposed to be held then name it foo_locked() or locked_()
If it auto-locks then name it foo()
If there is some #define wrapper then it is
#define foo() __foo()
Jason
I have both in some cases. A #define and a locked version in some cases. I'll try xxx_locked(). To be clear that means caller should hold some lock, yes?
bob
Jason Gunthorpe
maintainerRe: [PATCH for-next 2/5] RDMA/rxe: Fix misleading comments and names
Quoted 3 lines · Robert B
I have both in some cases. A #define and a locked version in some cases. I'll try
xxx_locked(). To be clear that means caller should hold some lock, yes?
Yes
Jason
PATCH 3/5 RDMA/rxe: Remove RXE_POOL_ATOMIC
In reply to Bob Pearsonrxe_alloc() used the RXE_POOL_ATOMIC flag in rxe_type_info to select GFP_ATOMIC in calls to kzalloc(). This was intended to handle cases where an object could be created in interrupt context. This no longer occurs since allocating those objects has moved into the core so this flag is not necessary. An incorrect use of this flag was still present for rxe_mc_elem objects and is removed.
--- drivers/infiniband/sw/rxe/rxe_pool.c | 6 +----- drivers/infiniband/sw/rxe/rxe_pool.h | 1 - 2 files changed, 1 insertion(+), 6 deletions(-)
drivers/infiniband/sw/rxe/rxe_pool.c +1 -5
@@ -84,7 +84,6 @@ struct rxe_type_info rxe_type_info[RXE_NUM_TYPES] = { .name = "rxe-mc_elem", .size = sizeof(struct rxe_mc_elem), .elem_offset = offsetof(struct rxe_mc_elem, pelem), - .flags = RXE_POOL_ATOMIC, }, };
@@ -380,8 +379,6 @@ void *rxe_alloc(struct rxe_pool *pool)struct rxe_pool_entry *elem;u8 *obj; -might_sleep_if (!(pool->flags &RXE_POOL_ATOMIC )); -read_lock_irqsave (&pool->pool_lock, flags);if (pool->state !=RXE_POOL_STATE_VALID ) {read_unlock_irqrestore (&pool->pool_lock, flags);
@@ -397,8 +394,7 @@ void *rxe_alloc(struct rxe_pool *pool)if (atomic_inc_return (&pool->num_elem) > pool->max_elem) goto out_cnt; - obj =kzalloc (info->size, (pool->flags &RXE_POOL_ATOMIC ) ? -GFP_ATOMIC :GFP_KERNEL ); + obj =kzalloc (info->size,GFP_KERNEL );if (!obj) goto out_cnt;
drivers/infiniband/sw/rxe/rxe_pool.h +0 -1
@@ -11,7 +11,6 @@#define RXE_POOL_CACHE_FLAGS (0)enum rxe_pool_flags { -RXE_POOL_ATOMIC =BIT (0 ),RXE_POOL_INDEX =BIT (1 ),RXE_POOL_KEY =BIT (2 ),RXE_POOL_NO_ALLOC =BIT (4 ),
Signature · 1 line
2.27.0
PATCH 4/5 RDMA/rxe: Remove calls to ib_device_try_get/put
In reply to Bob Pearsonrxe_pool.c had redundant attempts to reference count rxe objects taking references to the pools and also the ib_device. This patch eliminates the references to the ib_device which add no value.
--- drivers/infiniband/sw/rxe/rxe_pool.c | 13 ------------- 1 file changed, 13 deletions(-)
drivers/infiniband/sw/rxe/rxe_pool.c +0 -13
@@ -347,9 +347,6 @@ void *rxe_alloc__(struct rxe_pool *pool)kref_get (&pool->ref_cnt); -if (!ib_device_try_get (&pool->rxe->ib_dev)) - goto out_put_pool; -if (atomic_inc_return (&pool->num_elem) > pool->max_elem) goto out_cnt;
@@ -366,7 +363,6 @@ void *rxe_alloc__(struct rxe_pool *pool) out_cnt:atomic_dec (&pool->num_elem); -ib_device_put (&pool->rxe->ib_dev); out_put_pool:rxe_pool_put (pool);return NULL ;
@@ -388,9 +384,6 @@ void *rxe_alloc(struct rxe_pool *pool)kref_get (&pool->ref_cnt);read_unlock_irqrestore (&pool->pool_lock, flags); -if (!ib_device_try_get (&pool->rxe->ib_dev)) - goto out_put_pool; -if (atomic_inc_return (&pool->num_elem) > pool->max_elem) goto out_cnt;
@@ -407,7 +400,6 @@ void *rxe_alloc(struct rxe_pool *pool) out_cnt:atomic_dec (&pool->num_elem); -ib_device_put (&pool->rxe->ib_dev); out_put_pool:rxe_pool_put (pool);return NULL ;
@@ -425,9 +417,6 @@ int __rxe_add_to_pool(struct rxe_pool *pool, struct rxe_pool_entry *elem)kref_get (&pool->ref_cnt);read_unlock_irqrestore (&pool->pool_lock, flags); -if (!ib_device_try_get (&pool->rxe->ib_dev)) - goto out_put_pool; -if (atomic_inc_return (&pool->num_elem) > pool->max_elem) goto out_cnt;
@@ -438,7 +427,6 @@ int __rxe_add_to_pool(struct rxe_pool *pool, struct rxe_pool_entry *elem) out_cnt:atomic_dec (&pool->num_elem); -ib_device_put (&pool->rxe->ib_dev); out_put_pool:rxe_pool_put (pool);return -EINVAL ;
@@ -461,7 +449,6 @@ void rxe_elem_release(struct kref *kref) }atomic_dec (&pool->num_elem); -ib_device_put (&pool->rxe->ib_dev);rxe_pool_put (pool); }
Signature · 1 line
2.27.0
PATCH 5/5 RDMA/rxe: Remove unneeded pool->flag
In reply to Bob PearsonThe rxe driver used the field pool->flag to mark a pool as it was begin shut down and checked this flag in several pool APIs to validate that the pool had not been shut dowm. This is really unneeded because the pools were not marked invalid unless the entire driver was being removed at which point no functional APIs should or could be executing. This patch removes this flag, associated enums and spinlock code, and unneeded labels.
--- drivers/infiniband/sw/rxe/rxe_pool.c | 38 ---------------------------- drivers/infiniband/sw/rxe/rxe_pool.h | 6 ----- 2 files changed, 44 deletions(-)
drivers/infiniband/sw/rxe/rxe_pool.c +0 -38
@@ -159,8 +159,6 @@ int rxe_pool_init( pool->key.key_size = rxe_type_info[type].key_size; } - pool->state =RXE_POOL_STATE_VALID ; - out:return err; }
@@ -169,7 +167,6 @@ static void rxe_pool_release(struct kref *kref) { struct rxe_pool *pool = container_of(kref, struct rxe_pool, ref_cnt); - pool->state =RXE_POOL_STATE_INVALID ;kfree (pool->index.table); }
@@ -180,14 +177,9 @@ static void rxe_pool_put(struct rxe_pool *pool)void rxe_pool_cleanup (struct rxe_pool *pool) { -unsigned long flags; - -write_lock_irqsave (&pool->pool_lock, flags); - pool->state =RXE_POOL_STATE_INVALID ;if (atomic_read (&pool->num_elem) >0 )pr_warn ("%s pool destroyed with unfree'd elem\n" ,pool_name (pool)); -write_unlock_irqrestore (&pool->pool_lock, flags);rxe_pool_put (pool); }
@@ -342,9 +334,6 @@ void *rxe_alloc__(struct rxe_pool *pool)struct rxe_pool_entry *elem;u8 *obj; -if (pool->state !=RXE_POOL_STATE_VALID ) -return NULL ; -kref_get (&pool->ref_cnt);if (atomic_inc_return (&pool->num_elem) > pool->max_elem)
@@ -363,26 +352,17 @@ void *rxe_alloc__(struct rxe_pool *pool) out_cnt:atomic_dec (&pool->num_elem); -out_put_pool:rxe_pool_put (pool);return NULL ; }void *rxe_alloc (struct rxe_pool *pool) { -unsigned long flags;struct rxe_type_info *info = &rxe_type_info[pool->type];struct rxe_pool_entry *elem;u8 *obj; -read_lock_irqsave (&pool->pool_lock, flags); -if (pool->state !=RXE_POOL_STATE_VALID ) { -read_unlock_irqrestore (&pool->pool_lock, flags); -return NULL ; - } -kref_get (&pool->ref_cnt); -read_unlock_irqrestore (&pool->pool_lock, flags);if (atomic_inc_return (&pool->num_elem) > pool->max_elem) goto out_cnt;
@@ -400,22 +380,13 @@ void *rxe_alloc(struct rxe_pool *pool) out_cnt:atomic_dec (&pool->num_elem); -out_put_pool:rxe_pool_put (pool);return NULL ; }int __rxe_add_to_pool (struct rxe_pool *pool,struct rxe_pool_entry *elem) { -unsigned long flags; - -read_lock_irqsave (&pool->pool_lock, flags); -if (pool->state !=RXE_POOL_STATE_VALID ) { -read_unlock_irqrestore (&pool->pool_lock, flags); -return -EINVAL ; - }kref_get (&pool->ref_cnt); -read_unlock_irqrestore (&pool->pool_lock, flags);if (atomic_inc_return (&pool->num_elem) > pool->max_elem) goto out_cnt;
@@ -427,7 +398,6 @@ int __rxe_add_to_pool(struct rxe_pool *pool, struct rxe_pool_entry *elem) out_cnt:atomic_dec (&pool->num_elem); -out_put_pool:rxe_pool_put (pool);return -EINVAL ; }
@@ -462,9 +432,6 @@ void *rxe_pool_get_index(struct rxe_pool *pool, u32 index)read_lock_irqsave (&pool->pool_lock, flags); -if (pool->state !=RXE_POOL_STATE_VALID ) - goto out; - node = pool->index.tree.rb_node;while (node) {
@@ -485,7 +452,6 @@ void *rxe_pool_get_index(struct rxe_pool *pool, u32 index) obj =NULL ; } -out:read_unlock_irqrestore (&pool->pool_lock, flags);return obj; }
@@ -498,9 +464,6 @@ void *rxe_pool_get_key__(struct rxe_pool *pool, void *key)u8 *obj =NULL ;int cmp; -if (pool->state !=RXE_POOL_STATE_VALID ) - goto out; - node = pool->key.tree.rb_node;while (node) {
@@ -524,7 +487,6 @@ void *rxe_pool_get_key__(struct rxe_pool *pool, void *key) obj =NULL ; } -out:return obj; }
drivers/infiniband/sw/rxe/rxe_pool.h +0 -6
@@ -46,11 +46,6 @@ struct rxe_type_info {extern struct rxe_type_info rxe_type_info[]; -enum rxe_pool_state { -RXE_POOL_STATE_INVALID , -RXE_POOL_STATE_VALID , -}; -struct rxe_pool_entry {struct rxe_pool *pool;struct kref ref_cnt;
@@ -70,7 +65,6 @@ struct rxe_pool {size_t elem_size;struct kref ref_cnt;void (*cleanup)(struct rxe_pool_entry *obj); -enum rxe_pool_state state;enum rxe_pool_flags flags;enum rxe_elem_type type;
Signature · 1 line
2.27.0