@@ -169,7 +169,7 @@ struct file_operations proc_sys_file_operations = {
169169
170170extern struct proc_dir_entry * proc_sys_root ;
171171
172- static void register_proc_table (ctl_table * , struct proc_dir_entry * );
172+ static void register_proc_table (ctl_table * , struct proc_dir_entry * , void * );
173173static void unregister_proc_table (ctl_table * , struct proc_dir_entry * );
174174#endif
175175
@@ -992,10 +992,51 @@ static ctl_table dev_table[] = {
992992
993993extern void init_irq_proc (void );
994994
995+ static DEFINE_SPINLOCK (sysctl_lock );
996+
997+ /* called under sysctl_lock */
998+ static int use_table (struct ctl_table_header * p )
999+ {
1000+ if (unlikely (p -> unregistering ))
1001+ return 0 ;
1002+ p -> used ++ ;
1003+ return 1 ;
1004+ }
1005+
1006+ /* called under sysctl_lock */
1007+ static void unuse_table (struct ctl_table_header * p )
1008+ {
1009+ if (!-- p -> used )
1010+ if (unlikely (p -> unregistering ))
1011+ complete (p -> unregistering );
1012+ }
1013+
1014+ /* called under sysctl_lock, will reacquire if has to wait */
1015+ static void start_unregistering (struct ctl_table_header * p )
1016+ {
1017+ /*
1018+ * if p->used is 0, nobody will ever touch that entry again;
1019+ * we'll eliminate all paths to it before dropping sysctl_lock
1020+ */
1021+ if (unlikely (p -> used )) {
1022+ struct completion wait ;
1023+ init_completion (& wait );
1024+ p -> unregistering = & wait ;
1025+ spin_unlock (& sysctl_lock );
1026+ wait_for_completion (& wait );
1027+ spin_lock (& sysctl_lock );
1028+ }
1029+ /*
1030+ * do not remove from the list until nobody holds it; walking the
1031+ * list in do_sysctl() relies on that.
1032+ */
1033+ list_del_init (& p -> ctl_entry );
1034+ }
1035+
9951036void __init sysctl_init (void )
9961037{
9971038#ifdef CONFIG_PROC_FS
998- register_proc_table (root_table , proc_sys_root );
1039+ register_proc_table (root_table , proc_sys_root , & root_table_header );
9991040 init_irq_proc ();
10001041#endif
10011042}
@@ -1004,6 +1045,7 @@ int do_sysctl(int __user *name, int nlen, void __user *oldval, size_t __user *ol
10041045 void __user * newval , size_t newlen )
10051046{
10061047 struct list_head * tmp ;
1048+ int error = - ENOTDIR ;
10071049
10081050 if (nlen <= 0 || nlen >= CTL_MAXNAME )
10091051 return - ENOTDIR ;
@@ -1012,20 +1054,30 @@ int do_sysctl(int __user *name, int nlen, void __user *oldval, size_t __user *ol
10121054 if (!oldlenp || get_user (old_len , oldlenp ))
10131055 return - EFAULT ;
10141056 }
1057+ spin_lock (& sysctl_lock );
10151058 tmp = & root_table_header .ctl_entry ;
10161059 do {
10171060 struct ctl_table_header * head =
10181061 list_entry (tmp , struct ctl_table_header , ctl_entry );
10191062 void * context = NULL ;
1020- int error = parse_table (name , nlen , oldval , oldlenp ,
1063+
1064+ if (!use_table (head ))
1065+ continue ;
1066+
1067+ spin_unlock (& sysctl_lock );
1068+
1069+ error = parse_table (name , nlen , oldval , oldlenp ,
10211070 newval , newlen , head -> ctl_table ,
10221071 & context );
10231072 kfree (context );
1073+
1074+ spin_lock (& sysctl_lock );
1075+ unuse_table (head );
10241076 if (error != - ENOTDIR )
1025- return error ;
1026- tmp = tmp -> next ;
1027- } while ( tmp != & root_table_header . ctl_entry );
1028- return - ENOTDIR ;
1077+ break ;
1078+ } while (( tmp = tmp -> next ) != & root_table_header . ctl_entry ) ;
1079+ spin_unlock ( & sysctl_lock );
1080+ return error ;
10291081}
10301082
10311083asmlinkage long sys_sysctl (struct __sysctl_args __user * args )
@@ -1236,12 +1288,16 @@ struct ctl_table_header *register_sysctl_table(ctl_table * table,
12361288 return NULL ;
12371289 tmp -> ctl_table = table ;
12381290 INIT_LIST_HEAD (& tmp -> ctl_entry );
1291+ tmp -> used = 0 ;
1292+ tmp -> unregistering = NULL ;
1293+ spin_lock (& sysctl_lock );
12391294 if (insert_at_head )
12401295 list_add (& tmp -> ctl_entry , & root_table_header .ctl_entry );
12411296 else
12421297 list_add_tail (& tmp -> ctl_entry , & root_table_header .ctl_entry );
1298+ spin_unlock (& sysctl_lock );
12431299#ifdef CONFIG_PROC_FS
1244- register_proc_table (table , proc_sys_root );
1300+ register_proc_table (table , proc_sys_root , tmp );
12451301#endif
12461302 return tmp ;
12471303}
@@ -1255,10 +1311,13 @@ struct ctl_table_header *register_sysctl_table(ctl_table * table,
12551311 */
12561312void unregister_sysctl_table (struct ctl_table_header * header )
12571313{
1258- list_del (& header -> ctl_entry );
1314+ might_sleep ();
1315+ spin_lock (& sysctl_lock );
1316+ start_unregistering (header );
12591317#ifdef CONFIG_PROC_FS
12601318 unregister_proc_table (header -> ctl_table , proc_sys_root );
12611319#endif
1320+ spin_unlock (& sysctl_lock );
12621321 kfree (header );
12631322}
12641323
@@ -1269,7 +1328,7 @@ void unregister_sysctl_table(struct ctl_table_header * header)
12691328#ifdef CONFIG_PROC_FS
12701329
12711330/* Scan the sysctl entries in table and add them all into /proc */
1272- static void register_proc_table (ctl_table * table , struct proc_dir_entry * root )
1331+ static void register_proc_table (ctl_table * table , struct proc_dir_entry * root , void * set )
12731332{
12741333 struct proc_dir_entry * de ;
12751334 int len ;
@@ -1305,13 +1364,14 @@ static void register_proc_table(ctl_table * table, struct proc_dir_entry *root)
13051364 de = create_proc_entry (table -> procname , mode , root );
13061365 if (!de )
13071366 continue ;
1367+ de -> set = set ;
13081368 de -> data = (void * ) table ;
13091369 if (table -> proc_handler )
13101370 de -> proc_fops = & proc_sys_file_operations ;
13111371 }
13121372 table -> de = de ;
13131373 if (de -> mode & S_IFDIR )
1314- register_proc_table (table -> child , de );
1374+ register_proc_table (table -> child , de , set );
13151375 }
13161376}
13171377
@@ -1336,6 +1396,13 @@ static void unregister_proc_table(ctl_table * table, struct proc_dir_entry *root
13361396 continue ;
13371397 }
13381398
1399+ /*
1400+ * In any case, mark the entry as goner; we'll keep it
1401+ * around if it's busy, but we'll know to do nothing with
1402+ * its fields. We are under sysctl_lock here.
1403+ */
1404+ de -> data = NULL ;
1405+
13391406 /* Don't unregister proc entries that are still being used.. */
13401407 if (atomic_read (& de -> count ))
13411408 continue ;
@@ -1349,27 +1416,38 @@ static ssize_t do_rw_proc(int write, struct file * file, char __user * buf,
13491416 size_t count , loff_t * ppos )
13501417{
13511418 int op ;
1352- struct proc_dir_entry * de ;
1419+ struct proc_dir_entry * de = PDE ( file -> f_dentry -> d_inode ) ;
13531420 struct ctl_table * table ;
13541421 size_t res ;
1355- ssize_t error ;
1356-
1357- de = PDE (file -> f_dentry -> d_inode );
1358- if (!de || !de -> data )
1359- return - ENOTDIR ;
1360- table = (struct ctl_table * ) de -> data ;
1361- if (!table || !table -> proc_handler )
1362- return - ENOTDIR ;
1363- op = (write ? 002 : 004 );
1364- if (ctl_perm (table , op ))
1365- return - EPERM ;
1422+ ssize_t error = - ENOTDIR ;
13661423
1367- res = count ;
1368-
1369- error = (* table -> proc_handler ) (table , write , file , buf , & res , ppos );
1370- if (error )
1371- return error ;
1372- return res ;
1424+ spin_lock (& sysctl_lock );
1425+ if (de && de -> data && use_table (de -> set )) {
1426+ /*
1427+ * at that point we know that sysctl was not unregistered
1428+ * and won't be until we finish
1429+ */
1430+ spin_unlock (& sysctl_lock );
1431+ table = (struct ctl_table * ) de -> data ;
1432+ if (!table || !table -> proc_handler )
1433+ goto out ;
1434+ error = - EPERM ;
1435+ op = (write ? 002 : 004 );
1436+ if (ctl_perm (table , op ))
1437+ goto out ;
1438+
1439+ /* careful: calling conventions are nasty here */
1440+ res = count ;
1441+ error = (* table -> proc_handler )(table , write , file ,
1442+ buf , & res , ppos );
1443+ if (!error )
1444+ error = res ;
1445+ out :
1446+ spin_lock (& sysctl_lock );
1447+ unuse_table (de -> set );
1448+ }
1449+ spin_unlock (& sysctl_lock );
1450+ return error ;
13731451}
13741452
13751453static int proc_opensys (struct inode * inode , struct file * file )
0 commit comments