@@ -81,7 +81,7 @@ struct ucma_file {
8181};
8282
8383struct ucma_context {
84- int id ;
84+ u32 id ;
8585 struct completion comp ;
8686 atomic_t ref ;
8787 int events_reported ;
@@ -94,7 +94,7 @@ struct ucma_context {
9494 struct list_head list ;
9595 struct list_head mc_list ;
9696 /* mark that device is in process of destroying the internal HW
97- * resources, protected by the global mut
97+ * resources, protected by the ctx_table lock
9898 */
9999 int closing ;
100100 /* sync between removal event and id destroy, protected by file mut */
@@ -122,8 +122,7 @@ struct ucma_event {
122122 struct work_struct close_work ;
123123};
124124
125- static DEFINE_MUTEX (mut );
126- static DEFINE_IDR (ctx_idr );
125+ static DEFINE_XARRAY_ALLOC (ctx_table );
127126static DEFINE_XARRAY_ALLOC (multicast_table );
128127
129128static const struct file_operations ucma_fops ;
@@ -133,7 +132,7 @@ static inline struct ucma_context *_ucma_find_context(int id,
133132{
134133 struct ucma_context * ctx ;
135134
136- ctx = idr_find ( & ctx_idr , id );
135+ ctx = xa_load ( & ctx_table , id );
137136 if (!ctx )
138137 ctx = ERR_PTR (- ENOENT );
139138 else if (ctx -> file != file || !ctx -> cm_id )
@@ -145,15 +144,15 @@ static struct ucma_context *ucma_get_ctx(struct ucma_file *file, int id)
145144{
146145 struct ucma_context * ctx ;
147146
148- mutex_lock ( & mut );
147+ xa_lock ( & ctx_table );
149148 ctx = _ucma_find_context (id , file );
150149 if (!IS_ERR (ctx )) {
151150 if (ctx -> closing )
152151 ctx = ERR_PTR (- EIO );
153152 else
154153 atomic_inc (& ctx -> ref );
155154 }
156- mutex_unlock ( & mut );
155+ xa_unlock ( & ctx_table );
157156 return ctx ;
158157}
159158
@@ -216,10 +215,7 @@ static struct ucma_context *ucma_alloc_ctx(struct ucma_file *file)
216215 INIT_LIST_HEAD (& ctx -> mc_list );
217216 ctx -> file = file ;
218217
219- mutex_lock (& mut );
220- ctx -> id = idr_alloc (& ctx_idr , ctx , 0 , 0 , GFP_KERNEL );
221- mutex_unlock (& mut );
222- if (ctx -> id < 0 )
218+ if (xa_alloc (& ctx_table , & ctx -> id , ctx , xa_limit_32b , GFP_KERNEL ))
223219 goto error ;
224220
225221 list_add_tail (& ctx -> list , & file -> ctx_list );
@@ -316,9 +312,9 @@ static void ucma_removal_event_handler(struct rdma_cm_id *cm_id)
316312 * handled separately below.
317313 */
318314 if (ctx -> cm_id == cm_id ) {
319- mutex_lock ( & mut );
315+ xa_lock ( & ctx_table );
320316 ctx -> closing = 1 ;
321- mutex_unlock ( & mut );
317+ xa_unlock ( & ctx_table );
322318 queue_work (ctx -> file -> close_wq , & ctx -> close_work );
323319 return ;
324320 }
@@ -520,9 +516,7 @@ static ssize_t ucma_create_id(struct ucma_file *file, const char __user *inbuf,
520516err2 :
521517 rdma_destroy_id (cm_id );
522518err1 :
523- mutex_lock (& mut );
524- idr_remove (& ctx_idr , ctx -> id );
525- mutex_unlock (& mut );
519+ xa_erase (& ctx_table , ctx -> id );
526520 mutex_lock (& file -> mut );
527521 list_del (& ctx -> list );
528522 mutex_unlock (& file -> mut );
@@ -534,13 +528,13 @@ static void ucma_cleanup_multicast(struct ucma_context *ctx)
534528{
535529 struct ucma_multicast * mc , * tmp ;
536530
537- mutex_lock (& mut );
531+ mutex_lock (& ctx -> file -> mut );
538532 list_for_each_entry_safe (mc , tmp , & ctx -> mc_list , list ) {
539533 list_del (& mc -> list );
540534 xa_erase (& multicast_table , mc -> id );
541535 kfree (mc );
542536 }
543- mutex_unlock (& mut );
537+ mutex_unlock (& ctx -> file -> mut );
544538}
545539
546540static void ucma_cleanup_mc_events (struct ucma_multicast * mc )
@@ -611,11 +605,11 @@ static ssize_t ucma_destroy_id(struct ucma_file *file, const char __user *inbuf,
611605 if (copy_from_user (& cmd , inbuf , sizeof (cmd )))
612606 return - EFAULT ;
613607
614- mutex_lock ( & mut );
608+ xa_lock ( & ctx_table );
615609 ctx = _ucma_find_context (cmd .id , file );
616610 if (!IS_ERR (ctx ))
617- idr_remove ( & ctx_idr , ctx -> id );
618- mutex_unlock ( & mut );
611+ __xa_erase ( & ctx_table , ctx -> id );
612+ xa_unlock ( & ctx_table );
619613
620614 if (IS_ERR (ctx ))
621615 return PTR_ERR (ctx );
@@ -627,14 +621,14 @@ static ssize_t ucma_destroy_id(struct ucma_file *file, const char __user *inbuf,
627621 flush_workqueue (ctx -> file -> close_wq );
628622 /* At this point it's guaranteed that there is no inflight
629623 * closing task */
630- mutex_lock ( & mut );
624+ xa_lock ( & ctx_table );
631625 if (!ctx -> closing ) {
632- mutex_unlock ( & mut );
626+ xa_unlock ( & ctx_table );
633627 ucma_put_ctx (ctx );
634628 wait_for_completion (& ctx -> comp );
635629 rdma_destroy_id (ctx -> cm_id );
636630 } else {
637- mutex_unlock ( & mut );
631+ xa_unlock ( & ctx_table );
638632 }
639633
640634 resp .events_reported = ucma_free_ctx (ctx );
@@ -1607,14 +1601,14 @@ static ssize_t ucma_migrate_id(struct ucma_file *new_file,
16071601 * events being added before existing events.
16081602 */
16091603 ucma_lock_files (cur_file , new_file );
1610- mutex_lock ( & mut );
1604+ xa_lock ( & ctx_table );
16111605
16121606 list_move_tail (& ctx -> list , & new_file -> ctx_list );
16131607 ucma_move_events (ctx , new_file );
16141608 ctx -> file = new_file ;
16151609 resp .events_reported = ctx -> events_reported ;
16161610
1617- mutex_unlock ( & mut );
1611+ xa_unlock ( & ctx_table );
16181612 ucma_unlock_files (cur_file , new_file );
16191613
16201614response :
@@ -1749,26 +1743,23 @@ static int ucma_close(struct inode *inode, struct file *filp)
17491743 ctx -> destroying = 1 ;
17501744 mutex_unlock (& file -> mut );
17511745
1752- mutex_lock (& mut );
1753- idr_remove (& ctx_idr , ctx -> id );
1754- mutex_unlock (& mut );
1755-
1746+ xa_erase (& ctx_table , ctx -> id );
17561747 flush_workqueue (file -> close_wq );
17571748 /* At that step once ctx was marked as destroying and workqueue
17581749 * was flushed we are safe from any inflights handlers that
17591750 * might put other closing task.
17601751 */
1761- mutex_lock ( & mut );
1752+ xa_lock ( & ctx_table );
17621753 if (!ctx -> closing ) {
1763- mutex_unlock ( & mut );
1754+ xa_unlock ( & ctx_table );
17641755 ucma_put_ctx (ctx );
17651756 wait_for_completion (& ctx -> comp );
17661757 /* rdma_destroy_id ensures that no event handlers are
17671758 * inflight for that id before releasing it.
17681759 */
17691760 rdma_destroy_id (ctx -> cm_id );
17701761 } else {
1771- mutex_unlock ( & mut );
1762+ xa_unlock ( & ctx_table );
17721763 }
17731764
17741765 ucma_free_ctx (ctx );
@@ -1838,7 +1829,6 @@ static void __exit ucma_cleanup(void)
18381829 unregister_net_sysctl_table (ucma_ctl_table_hdr );
18391830 device_remove_file (ucma_misc .this_device , & dev_attr_abi_version );
18401831 misc_deregister (& ucma_misc );
1841- idr_destroy (& ctx_idr );
18421832}
18431833
18441834module_init (ucma_init );
0 commit comments