@@ -3620,27 +3620,6 @@ static int post_relocation(struct module *mod, const struct load_info *info)
36203620 return module_finalize (info -> hdr , info -> sechdrs , mod );
36213621}
36223622
3623- /* Is this module of this name done loading? No locks held. */
3624- static bool finished_loading (const char * name )
3625- {
3626- struct module * mod ;
3627- bool ret ;
3628-
3629- /*
3630- * The module_mutex should not be a heavily contended lock;
3631- * if we get the occasional sleep here, we'll go an extra iteration
3632- * in the wait_event_interruptible(), which is harmless.
3633- */
3634- sched_annotate_sleep ();
3635- mutex_lock (& module_mutex );
3636- mod = find_module_all (name , strlen (name ), true);
3637- ret = !mod || mod -> state == MODULE_STATE_LIVE
3638- || mod -> state == MODULE_STATE_GOING ;
3639- mutex_unlock (& module_mutex );
3640-
3641- return ret ;
3642- }
3643-
36443623/* Call module constructors. */
36453624static void do_mod_ctors (struct module * mod )
36463625{
@@ -3808,6 +3787,63 @@ static int may_init_module(void)
38083787 return 0 ;
38093788}
38103789
3790+ /* Is this module of this name done loading? No locks held. */
3791+ static bool finished_loading (const char * name )
3792+ {
3793+ struct module * mod ;
3794+ bool ret ;
3795+
3796+ /*
3797+ * The module_mutex should not be a heavily contended lock;
3798+ * if we get the occasional sleep here, we'll go an extra iteration
3799+ * in the wait_event_interruptible(), which is harmless.
3800+ */
3801+ sched_annotate_sleep ();
3802+ mutex_lock (& module_mutex );
3803+ mod = find_module_all (name , strlen (name ), true);
3804+ ret = !mod || mod -> state == MODULE_STATE_LIVE
3805+ || mod -> state == MODULE_STATE_GOING ;
3806+ mutex_unlock (& module_mutex );
3807+
3808+ return ret ;
3809+ }
3810+
3811+ /* Must be called with module_mutex held */
3812+ static int module_patient_check_exists (const char * name )
3813+ {
3814+ struct module * old ;
3815+ int err = 0 ;
3816+
3817+ old = find_module_all (name , strlen (name ), true);
3818+ if (old == NULL )
3819+ return 0 ;
3820+
3821+ if (old -> state == MODULE_STATE_COMING ||
3822+ old -> state == MODULE_STATE_UNFORMED ) {
3823+ /* Wait in case it fails to load. */
3824+ mutex_unlock (& module_mutex );
3825+ err = wait_event_interruptible (module_wq ,
3826+ finished_loading (name ));
3827+ mutex_lock (& module_mutex );
3828+ if (err )
3829+ return err ;
3830+
3831+ /* The module might have gone in the meantime. */
3832+ old = find_module_all (name , strlen (name ), true);
3833+ }
3834+
3835+ /*
3836+ * We are here only when the same module was being loaded. Do
3837+ * not try to load it again right now. It prevents long delays
3838+ * caused by serialized module load failures. It might happen
3839+ * when more devices of the same type trigger load of
3840+ * a particular module.
3841+ */
3842+ if (old && old -> state == MODULE_STATE_LIVE )
3843+ return - EEXIST ;
3844+ return - EBUSY ;
3845+ }
3846+
38113847/*
38123848 * We try to place it in the list now to make sure it's unique before
38133849 * we dedicate too many resources. In particular, temporary percpu
@@ -3816,49 +3852,21 @@ static int may_init_module(void)
38163852static int add_unformed_module (struct module * mod )
38173853{
38183854 int err ;
3819- struct module * old ;
38203855
38213856 mod -> state = MODULE_STATE_UNFORMED ;
38223857
38233858 mutex_lock (& module_mutex );
3824- old = find_module_all (mod -> name , strlen (mod -> name ), true);
3825- if (old != NULL ) {
3826- if (old -> state == MODULE_STATE_COMING
3827- || old -> state == MODULE_STATE_UNFORMED ) {
3828- /* Wait in case it fails to load. */
3829- mutex_unlock (& module_mutex );
3830- err = wait_event_interruptible (module_wq ,
3831- finished_loading (mod -> name ));
3832- if (err )
3833- goto out_unlocked ;
3834-
3835- /* The module might have gone in the meantime. */
3836- mutex_lock (& module_mutex );
3837- old = find_module_all (mod -> name , strlen (mod -> name ),
3838- true);
3839- }
3840-
3841- /*
3842- * We are here only when the same module was being loaded. Do
3843- * not try to load it again right now. It prevents long delays
3844- * caused by serialized module load failures. It might happen
3845- * when more devices of the same type trigger load of
3846- * a particular module.
3847- */
3848- if (old && old -> state == MODULE_STATE_LIVE )
3849- err = - EEXIST ;
3850- else
3851- err = - EBUSY ;
3859+ err = module_patient_check_exists (mod -> name );
3860+ if (err )
38523861 goto out ;
3853- }
3862+
38543863 mod_update_bounds (mod );
38553864 list_add_rcu (& mod -> list , & modules );
38563865 mod_tree_insert (mod );
38573866 err = 0 ;
38583867
38593868out :
38603869 mutex_unlock (& module_mutex );
3861- out_unlocked :
38623870 return err ;
38633871}
38643872
0 commit comments