@@ -313,8 +313,11 @@ struct load_info {
313313 } index ;
314314};
315315
316- /* We require a truly strong try_module_get(): 0 means failure due to
317- ongoing or failed initialization etc. */
316+ /*
317+ * We require a truly strong try_module_get(): 0 means success.
318+ * Otherwise an error is returned due to ongoing or failed
319+ * initialization etc.
320+ */
318321static inline int strong_try_module_get (struct module * mod )
319322{
320323 BUG_ON (mod && mod -> state == MODULE_STATE_UNFORMED );
@@ -330,7 +333,7 @@ static inline void add_taint_module(struct module *mod, unsigned flag,
330333 enum lockdep_ok lockdep_ok )
331334{
332335 add_taint (flag , lockdep_ok );
333- mod -> taints |= ( 1U << flag );
336+ set_bit ( flag , & mod -> taints );
334337}
335338
336339/*
@@ -1138,24 +1141,13 @@ static inline int module_unload_init(struct module *mod)
11381141static size_t module_flags_taint (struct module * mod , char * buf )
11391142{
11401143 size_t l = 0 ;
1144+ int i ;
1145+
1146+ for (i = 0 ; i < TAINT_FLAGS_COUNT ; i ++ ) {
1147+ if (taint_flags [i ].module && test_bit (i , & mod -> taints ))
1148+ buf [l ++ ] = taint_flags [i ].true ;
1149+ }
11411150
1142- if (mod -> taints & (1 << TAINT_PROPRIETARY_MODULE ))
1143- buf [l ++ ] = 'P' ;
1144- if (mod -> taints & (1 << TAINT_OOT_MODULE ))
1145- buf [l ++ ] = 'O' ;
1146- if (mod -> taints & (1 << TAINT_FORCED_MODULE ))
1147- buf [l ++ ] = 'F' ;
1148- if (mod -> taints & (1 << TAINT_CRAP ))
1149- buf [l ++ ] = 'C' ;
1150- if (mod -> taints & (1 << TAINT_UNSIGNED_MODULE ))
1151- buf [l ++ ] = 'E' ;
1152- if (mod -> taints & (1 << TAINT_LIVEPATCH ))
1153- buf [l ++ ] = 'K' ;
1154- /*
1155- * TAINT_FORCED_RMMOD: could be added.
1156- * TAINT_CPU_OUT_OF_SPEC, TAINT_MACHINE_CHECK, TAINT_BAD_PAGE don't
1157- * apply to modules.
1158- */
11591151 return l ;
11601152}
11611153
@@ -1911,6 +1903,9 @@ static void frob_writable_data(const struct module_layout *layout,
19111903/* livepatching wants to disable read-only so it can frob module. */
19121904void module_disable_ro (const struct module * mod )
19131905{
1906+ if (!rodata_enabled )
1907+ return ;
1908+
19141909 frob_text (& mod -> core_layout , set_memory_rw );
19151910 frob_rodata (& mod -> core_layout , set_memory_rw );
19161911 frob_ro_after_init (& mod -> core_layout , set_memory_rw );
@@ -1920,6 +1915,9 @@ void module_disable_ro(const struct module *mod)
19201915
19211916void module_enable_ro (const struct module * mod , bool after_init )
19221917{
1918+ if (!rodata_enabled )
1919+ return ;
1920+
19231921 frob_text (& mod -> core_layout , set_memory_ro );
19241922 frob_rodata (& mod -> core_layout , set_memory_ro );
19251923 frob_text (& mod -> init_layout , set_memory_ro );
@@ -1952,6 +1950,9 @@ void set_all_modules_text_rw(void)
19521950{
19531951 struct module * mod ;
19541952
1953+ if (!rodata_enabled )
1954+ return ;
1955+
19551956 mutex_lock (& module_mutex );
19561957 list_for_each_entry_rcu (mod , & modules , list ) {
19571958 if (mod -> state == MODULE_STATE_UNFORMED )
@@ -1968,9 +1969,18 @@ void set_all_modules_text_ro(void)
19681969{
19691970 struct module * mod ;
19701971
1972+ if (!rodata_enabled )
1973+ return ;
1974+
19711975 mutex_lock (& module_mutex );
19721976 list_for_each_entry_rcu (mod , & modules , list ) {
1973- if (mod -> state == MODULE_STATE_UNFORMED )
1977+ /*
1978+ * Ignore going modules since it's possible that ro
1979+ * protection has already been disabled, otherwise we'll
1980+ * run into protection faults at module deallocation.
1981+ */
1982+ if (mod -> state == MODULE_STATE_UNFORMED ||
1983+ mod -> state == MODULE_STATE_GOING )
19741984 continue ;
19751985
19761986 frob_text (& mod -> core_layout , set_memory_ro );
@@ -1981,10 +1991,12 @@ void set_all_modules_text_ro(void)
19811991
19821992static void disable_ro_nx (const struct module_layout * layout )
19831993{
1984- frob_text (layout , set_memory_rw );
1985- frob_rodata (layout , set_memory_rw );
1994+ if (rodata_enabled ) {
1995+ frob_text (layout , set_memory_rw );
1996+ frob_rodata (layout , set_memory_rw );
1997+ frob_ro_after_init (layout , set_memory_rw );
1998+ }
19861999 frob_rodata (layout , set_memory_x );
1987- frob_ro_after_init (layout , set_memory_rw );
19882000 frob_ro_after_init (layout , set_memory_x );
19892001 frob_writable_data (layout , set_memory_x );
19902002}
@@ -3709,6 +3721,7 @@ static int load_module(struct load_info *info, const char __user *uargs,
37093721 sysfs_cleanup :
37103722 mod_sysfs_teardown (mod );
37113723 coming_cleanup :
3724+ mod -> state = MODULE_STATE_GOING ;
37123725 blocking_notifier_call_chain (& module_notify_list ,
37133726 MODULE_STATE_GOING , mod );
37143727 klp_module_going (mod );
@@ -4042,6 +4055,10 @@ int module_kallsyms_on_each_symbol(int (*fn)(void *, const char *,
40424055}
40434056#endif /* CONFIG_KALLSYMS */
40444057
4058+ /* Maximum number of characters written by module_flags() */
4059+ #define MODULE_FLAGS_BUF_SIZE (TAINT_FLAGS_COUNT + 4)
4060+
4061+ /* Keep in sync with MODULE_FLAGS_BUF_SIZE !!! */
40454062static char * module_flags (struct module * mod , char * buf )
40464063{
40474064 int bx = 0 ;
@@ -4086,7 +4103,7 @@ static void m_stop(struct seq_file *m, void *p)
40864103static int m_show (struct seq_file * m , void * p )
40874104{
40884105 struct module * mod = list_entry (p , struct module , list );
4089- char buf [8 ];
4106+ char buf [MODULE_FLAGS_BUF_SIZE ];
40904107
40914108 /* We always ignore unformed modules. */
40924109 if (mod -> state == MODULE_STATE_UNFORMED )
@@ -4257,7 +4274,7 @@ EXPORT_SYMBOL_GPL(__module_text_address);
42574274void print_modules (void )
42584275{
42594276 struct module * mod ;
4260- char buf [8 ];
4277+ char buf [MODULE_FLAGS_BUF_SIZE ];
42614278
42624279 printk (KERN_DEFAULT "Modules linked in:" );
42634280 /* Most callers should already have preempt disabled, but make sure */
0 commit comments