3232
3333#include <linux/file.h>
3434#include <linux/anon_inodes.h>
35+ #include <linux/sched/mm.h>
3536#include <rdma/ib_verbs.h>
3637#include <rdma/uverbs_types.h>
3738#include <linux/rcupdate.h>
@@ -284,11 +285,8 @@ struct ib_uobject *rdma_lookup_get_uobject(const struct uverbs_obj_type *type,
284285 }
285286
286287 ret = uverbs_try_lock_object (uobj , exclusive );
287- if (ret ) {
288- WARN (uobj -> ufile -> cleanup_reason ,
289- "ib_uverbs: Trying to lookup_get while cleanup context\n" );
288+ if (ret )
290289 goto free ;
291- }
292290
293291 return uobj ;
294292free :
@@ -694,6 +692,71 @@ void uverbs_close_fd(struct file *f)
694692 uverbs_uobject_put (uobj );
695693}
696694
695+ static void ufile_disassociate_ucontext (struct ib_ucontext * ibcontext )
696+ {
697+ struct ib_device * ib_dev = ibcontext -> device ;
698+ struct task_struct * owning_process = NULL ;
699+ struct mm_struct * owning_mm = NULL ;
700+
701+ owning_process = get_pid_task (ibcontext -> tgid , PIDTYPE_PID );
702+ if (!owning_process )
703+ return ;
704+
705+ owning_mm = get_task_mm (owning_process );
706+ if (!owning_mm ) {
707+ pr_info ("no mm, disassociate ucontext is pending task termination\n" );
708+ while (1 ) {
709+ put_task_struct (owning_process );
710+ usleep_range (1000 , 2000 );
711+ owning_process = get_pid_task (ibcontext -> tgid ,
712+ PIDTYPE_PID );
713+ if (!owning_process ||
714+ owning_process -> state == TASK_DEAD ) {
715+ pr_info ("disassociate ucontext done, task was terminated\n" );
716+ /* in case task was dead need to release the
717+ * task struct.
718+ */
719+ if (owning_process )
720+ put_task_struct (owning_process );
721+ return ;
722+ }
723+ }
724+ }
725+
726+ down_write (& owning_mm -> mmap_sem );
727+ ib_dev -> disassociate_ucontext (ibcontext );
728+ up_write (& owning_mm -> mmap_sem );
729+ mmput (owning_mm );
730+ put_task_struct (owning_process );
731+ }
732+
733+ /*
734+ * Drop the ucontext off the ufile and completely disconnect it from the
735+ * ib_device
736+ */
737+ static void ufile_destroy_ucontext (struct ib_uverbs_file * ufile ,
738+ enum rdma_remove_reason reason )
739+ {
740+ struct ib_ucontext * ucontext = ufile -> ucontext ;
741+ int ret ;
742+
743+ if (reason == RDMA_REMOVE_DRIVER_REMOVE )
744+ ufile_disassociate_ucontext (ucontext );
745+
746+ put_pid (ucontext -> tgid );
747+ ib_rdmacg_uncharge (& ucontext -> cg_obj , ucontext -> device ,
748+ RDMACG_RESOURCE_HCA_HANDLE );
749+
750+ /*
751+ * FIXME: Drivers are not permitted to fail dealloc_ucontext, remove
752+ * the error return.
753+ */
754+ ret = ucontext -> device -> dealloc_ucontext (ufile -> ucontext );
755+ WARN_ON (ret );
756+
757+ ufile -> ucontext = NULL ;
758+ }
759+
697760static int __uverbs_cleanup_ufile (struct ib_uverbs_file * ufile ,
698761 enum rdma_remove_reason reason )
699762{
@@ -710,7 +773,6 @@ static int __uverbs_cleanup_ufile(struct ib_uverbs_file *ufile,
710773 * We take and release the lock per traversal in order to let
711774 * other threads (which might still use the FDs) chance to run.
712775 */
713- ufile -> cleanup_reason = reason ;
714776 list_for_each_entry_safe (obj , next_obj , & ufile -> uobjects , list ) {
715777 /*
716778 * if we hit this WARN_ON, that means we are
@@ -738,18 +800,43 @@ static int __uverbs_cleanup_ufile(struct ib_uverbs_file *ufile,
738800 return ret ;
739801}
740802
741- void uverbs_cleanup_ufile (struct ib_uverbs_file * ufile , bool device_removed )
803+ /*
804+ * Destroy the uncontext and every uobject associated with it. If called with
805+ * reason != RDMA_REMOVE_CLOSE this will not return until the destruction has
806+ * been completed and ufile->ucontext is NULL.
807+ *
808+ * This is internally locked and can be called in parallel from multiple
809+ * contexts.
810+ */
811+ void uverbs_destroy_ufile_hw (struct ib_uverbs_file * ufile ,
812+ enum rdma_remove_reason reason )
742813{
743- enum rdma_remove_reason reason = device_removed ?
744- RDMA_REMOVE_DRIVER_REMOVE :
745- RDMA_REMOVE_CLOSE ;
814+ if (reason == RDMA_REMOVE_CLOSE ) {
815+ /*
816+ * During destruction we might trigger something that
817+ * synchronously calls release on any file descriptor. For
818+ * this reason all paths that come from file_operations
819+ * release must use try_lock. They can progress knowing that
820+ * there is an ongoing uverbs_destroy_ufile_hw that will clean
821+ * up the driver resources.
822+ */
823+ if (!mutex_trylock (& ufile -> ucontext_lock ))
824+ return ;
825+
826+ } else {
827+ mutex_lock (& ufile -> ucontext_lock );
828+ }
829+
830+ down_write (& ufile -> hw_destroy_rwsem );
746831
747832 /*
748- * Waits for all remove_commit and alloc_commit to finish. Logically, We
749- * want to hold this forever as the context is going to be destroyed,
750- * but we'll release it since it causes a "held lock freed" BUG message.
833+ * If a ucontext was never created then we can't have any uobjects to
834+ * cleanup, nothing to do.
751835 */
752- down_write (& ufile -> hw_destroy_rwsem );
836+ if (!ufile -> ucontext )
837+ goto done ;
838+
839+ ufile -> ucontext -> closing = true;
753840 ufile -> ucontext -> cleanup_retryable = true;
754841 while (!list_empty (& ufile -> uobjects ))
755842 if (__uverbs_cleanup_ufile (ufile , reason )) {
@@ -764,7 +851,11 @@ void uverbs_cleanup_ufile(struct ib_uverbs_file *ufile, bool device_removed)
764851 if (!list_empty (& ufile -> uobjects ))
765852 __uverbs_cleanup_ufile (ufile , reason );
766853
854+ ufile_destroy_ucontext (ufile , reason );
855+
856+ done :
767857 up_write (& ufile -> hw_destroy_rwsem );
858+ mutex_unlock (& ufile -> ucontext_lock );
768859}
769860
770861const struct uverbs_obj_type_class uverbs_fd_class = {
0 commit comments