Merge branch 'upstream-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/jlbec/configfs

* 'upstream-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/jlbec/configfs:
configfs: Rework configfs_depend_item() locking and make lockdep happy
configfs: Silence lockdep on mkdir() and rmdir()

+178 -59
+3
fs/configfs/configfs_internal.h
··· 39 umode_t s_mode; 40 struct dentry * s_dentry; 41 struct iattr * s_iattr; 42 }; 43 44 #define CONFIGFS_ROOT 0x0001
··· 39 umode_t s_mode; 40 struct dentry * s_dentry; 41 struct iattr * s_iattr; 42 + #ifdef CONFIG_LOCKDEP 43 + int s_depth; 44 + #endif 45 }; 46 47 #define CONFIGFS_ROOT 0x0001
+137 -59
fs/configfs/dir.c
··· 78 .d_delete = configfs_d_delete, 79 }; 80 81 /* 82 * Allocates a new configfs_dirent and links it to the parent configfs_dirent 83 */ 84 - static struct configfs_dirent *configfs_new_dirent(struct configfs_dirent * parent_sd, 85 - void * element) 86 { 87 struct configfs_dirent * sd; 88 ··· 180 INIT_LIST_HEAD(&sd->s_links); 181 INIT_LIST_HEAD(&sd->s_children); 182 sd->s_element = element; 183 spin_lock(&configfs_dirent_lock); 184 if (parent_sd->s_type & CONFIGFS_USET_DROPPING) { 185 spin_unlock(&configfs_dirent_lock); ··· 226 { 227 struct configfs_dirent * sd; 228 229 - sd = configfs_new_dirent(parent_sd, element); 230 if (IS_ERR(sd)) 231 return PTR_ERR(sd); 232 233 sd->s_mode = mode; 234 - sd->s_type = type; 235 sd->s_dentry = dentry; 236 if (dentry) { 237 dentry->d_fsdata = configfs_get(sd); ··· 274 error = configfs_make_dirent(p->d_fsdata, d, k, mode, 275 CONFIGFS_DIR | CONFIGFS_USET_CREATING); 276 if (!error) { 277 error = configfs_create(d, mode, init_dir); 278 if (!error) { 279 inc_nlink(p->d_inode); ··· 877 * error, as rmdir() would. 878 */ 879 mutex_lock_nested(&dentry->d_inode->i_mutex, I_MUTEX_CHILD); 880 ret = populate_groups(to_config_group(item)); 881 if (ret) { 882 configfs_detach_item(item); 883 dentry->d_inode->i_flags |= S_DEAD; 884 } 885 mutex_unlock(&dentry->d_inode->i_mutex); 886 if (ret) 887 d_delete(dentry); ··· 1006 * Note, btw, that this can be called at *any* time, even when a configfs 1007 * subsystem isn't registered, or when configfs is loading or unloading. 1008 * Just like configfs_register_subsystem(). So we take the same 1009 - * precautions. We pin the filesystem. We lock each i_mutex _in_order_ 1010 - * on our way down the tree. If we can find the target item in the 1011 * configfs tree, it must be part of the subsystem tree as well, so we 1012 - * do not need the subsystem semaphore. Holding the i_mutex chain locks 1013 - * out mkdir() and rmdir(), who might be racing us. 1014 */ 1015 1016 /* ··· 1023 * do that so we can unlock it if we find nothing. 1024 * 1025 * Here we do a depth-first search of the dentry hierarchy looking for 1026 - * our object. We take i_mutex on each step of the way down. IT IS 1027 - * ESSENTIAL THAT i_mutex LOCKING IS ORDERED. If we come back up a branch, 1028 - * we'll drop the i_mutex. 1029 * 1030 - * If the target is not found, -ENOENT is bubbled up and we have released 1031 - * all locks. If the target was found, the locks will be cleared by 1032 - * configfs_depend_rollback(). 1033 * 1034 * This adds a requirement that all config_items be unique! 1035 * 1036 - * This is recursive because the locking traversal is tricky. There isn't 1037 * much on the stack, though, so folks that need this function - be careful 1038 * about your stack! Patches will be accepted to make it iterative. 1039 */ ··· 1049 1050 BUG_ON(!origin || !sd); 1051 1052 - /* Lock this guy on the way down */ 1053 - mutex_lock(&sd->s_dentry->d_inode->i_mutex); 1054 if (sd->s_element == target) /* Boo-yah */ 1055 goto out; 1056 1057 list_for_each_entry(child_sd, &sd->s_children, s_sibling) { 1058 - if (child_sd->s_type & CONFIGFS_DIR) { 1059 ret = configfs_depend_prep(child_sd->s_dentry, 1060 target); 1061 if (!ret) ··· 1064 } 1065 1066 /* We looped all our children and didn't find target */ 1067 - mutex_unlock(&sd->s_dentry->d_inode->i_mutex); 1068 ret = -ENOENT; 1069 1070 out: 1071 return ret; 1072 - } 1073 - 1074 - /* 1075 - * This is ONLY called if configfs_depend_prep() did its job. So we can 1076 - * trust the entire path from item back up to origin. 1077 - * 1078 - * We walk backwards from item, unlocking each i_mutex. We finish by 1079 - * unlocking origin. 1080 - */ 1081 - static void configfs_depend_rollback(struct dentry *origin, 1082 - struct config_item *item) 1083 - { 1084 - struct dentry *dentry = item->ci_dentry; 1085 - 1086 - while (dentry != origin) { 1087 - mutex_unlock(&dentry->d_inode->i_mutex); 1088 - dentry = dentry->d_parent; 1089 - } 1090 - 1091 - mutex_unlock(&origin->d_inode->i_mutex); 1092 } 1093 1094 int configfs_depend_item(struct configfs_subsystem *subsys, ··· 1110 1111 /* Ok, now we can trust subsys/s_item */ 1112 1113 - /* Scan the tree, locking i_mutex recursively, return 0 if found */ 1114 ret = configfs_depend_prep(subsys_sd->s_dentry, target); 1115 if (ret) 1116 - goto out_unlock_fs; 1117 1118 - /* We hold all i_mutexes from the subsystem down to the target */ 1119 p = target->ci_dentry->d_fsdata; 1120 p->s_dependent_count += 1; 1121 1122 - configfs_depend_rollback(subsys_sd->s_dentry, target); 1123 - 1124 out_unlock_fs: 1125 mutex_unlock(&configfs_sb->s_root->d_inode->i_mutex); 1126 ··· 1149 struct configfs_dirent *sd; 1150 1151 /* 1152 - * Since we can trust everything is pinned, we just need i_mutex 1153 - * on the item. 1154 */ 1155 - mutex_lock(&target->ci_dentry->d_inode->i_mutex); 1156 1157 sd = target->ci_dentry->d_fsdata; 1158 BUG_ON(sd->s_dependent_count < 1); ··· 1163 * After this unlock, we cannot trust the item to stay alive! 1164 * DO NOT REFERENCE item after this unlock. 1165 */ 1166 - mutex_unlock(&target->ci_dentry->d_inode->i_mutex); 1167 } 1168 EXPORT_SYMBOL(configfs_undepend_item); 1169 ··· 1363 if (sd->s_type & CONFIGFS_USET_DEFAULT) 1364 return -EPERM; 1365 1366 - /* 1367 - * Here's where we check for dependents. We're protected by 1368 - * i_mutex. 1369 - */ 1370 - if (sd->s_dependent_count) 1371 - return -EBUSY; 1372 - 1373 /* Get a working ref until we have the child */ 1374 parent_item = configfs_get_config_item(dentry->d_parent); 1375 subsys = to_config_group(parent_item)->cg_subsys; ··· 1386 1387 mutex_lock(&configfs_symlink_mutex); 1388 spin_lock(&configfs_dirent_lock); 1389 - ret = configfs_detach_prep(dentry, &wait_mutex); 1390 - if (ret) 1391 - configfs_detach_rollback(dentry); 1392 spin_unlock(&configfs_dirent_lock); 1393 mutex_unlock(&configfs_symlink_mutex); 1394 ··· 1507 */ 1508 err = -ENOENT; 1509 if (configfs_dirent_is_ready(parent_sd)) { 1510 - file->private_data = configfs_new_dirent(parent_sd, NULL); 1511 if (IS_ERR(file->private_data)) 1512 err = PTR_ERR(file->private_data); 1513 else
··· 78 .d_delete = configfs_d_delete, 79 }; 80 81 + #ifdef CONFIG_LOCKDEP 82 + 83 + /* 84 + * Helpers to make lockdep happy with our recursive locking of default groups' 85 + * inodes (see configfs_attach_group() and configfs_detach_group()). 86 + * We put default groups i_mutexes in separate classes according to their depth 87 + * from the youngest non-default group ancestor. 88 + * 89 + * For a non-default group A having default groups A/B, A/C, and A/C/D, default 90 + * groups A/B and A/C will have their inode's mutex in class 91 + * default_group_class[0], and default group A/C/D will be in 92 + * default_group_class[1]. 93 + * 94 + * The lock classes are declared and assigned in inode.c, according to the 95 + * s_depth value. 96 + * The s_depth value is initialized to -1, adjusted to >= 0 when attaching 97 + * default groups, and reset to -1 when all default groups are attached. During 98 + * attachment, if configfs_create() sees s_depth > 0, the lock class of the new 99 + * inode's mutex is set to default_group_class[s_depth - 1]. 100 + */ 101 + 102 + static void configfs_init_dirent_depth(struct configfs_dirent *sd) 103 + { 104 + sd->s_depth = -1; 105 + } 106 + 107 + static void configfs_set_dir_dirent_depth(struct configfs_dirent *parent_sd, 108 + struct configfs_dirent *sd) 109 + { 110 + int parent_depth = parent_sd->s_depth; 111 + 112 + if (parent_depth >= 0) 113 + sd->s_depth = parent_depth + 1; 114 + } 115 + 116 + static void 117 + configfs_adjust_dir_dirent_depth_before_populate(struct configfs_dirent *sd) 118 + { 119 + /* 120 + * item's i_mutex class is already setup, so s_depth is now only 121 + * used to set new sub-directories s_depth, which is always done 122 + * with item's i_mutex locked. 123 + */ 124 + /* 125 + * sd->s_depth == -1 iff we are a non default group. 126 + * else (we are a default group) sd->s_depth > 0 (see 127 + * create_dir()). 128 + */ 129 + if (sd->s_depth == -1) 130 + /* 131 + * We are a non default group and we are going to create 132 + * default groups. 133 + */ 134 + sd->s_depth = 0; 135 + } 136 + 137 + static void 138 + configfs_adjust_dir_dirent_depth_after_populate(struct configfs_dirent *sd) 139 + { 140 + /* We will not create default groups anymore. */ 141 + sd->s_depth = -1; 142 + } 143 + 144 + #else /* CONFIG_LOCKDEP */ 145 + 146 + static void configfs_init_dirent_depth(struct configfs_dirent *sd) 147 + { 148 + } 149 + 150 + static void configfs_set_dir_dirent_depth(struct configfs_dirent *parent_sd, 151 + struct configfs_dirent *sd) 152 + { 153 + } 154 + 155 + static void 156 + configfs_adjust_dir_dirent_depth_before_populate(struct configfs_dirent *sd) 157 + { 158 + } 159 + 160 + static void 161 + configfs_adjust_dir_dirent_depth_after_populate(struct configfs_dirent *sd) 162 + { 163 + } 164 + 165 + #endif /* CONFIG_LOCKDEP */ 166 + 167 /* 168 * Allocates a new configfs_dirent and links it to the parent configfs_dirent 169 */ 170 + static struct configfs_dirent *configfs_new_dirent(struct configfs_dirent *parent_sd, 171 + void *element, int type) 172 { 173 struct configfs_dirent * sd; 174 ··· 94 INIT_LIST_HEAD(&sd->s_links); 95 INIT_LIST_HEAD(&sd->s_children); 96 sd->s_element = element; 97 + sd->s_type = type; 98 + configfs_init_dirent_depth(sd); 99 spin_lock(&configfs_dirent_lock); 100 if (parent_sd->s_type & CONFIGFS_USET_DROPPING) { 101 spin_unlock(&configfs_dirent_lock); ··· 138 { 139 struct configfs_dirent * sd; 140 141 + sd = configfs_new_dirent(parent_sd, element, type); 142 if (IS_ERR(sd)) 143 return PTR_ERR(sd); 144 145 sd->s_mode = mode; 146 sd->s_dentry = dentry; 147 if (dentry) { 148 dentry->d_fsdata = configfs_get(sd); ··· 187 error = configfs_make_dirent(p->d_fsdata, d, k, mode, 188 CONFIGFS_DIR | CONFIGFS_USET_CREATING); 189 if (!error) { 190 + configfs_set_dir_dirent_depth(p->d_fsdata, d->d_fsdata); 191 error = configfs_create(d, mode, init_dir); 192 if (!error) { 193 inc_nlink(p->d_inode); ··· 789 * error, as rmdir() would. 790 */ 791 mutex_lock_nested(&dentry->d_inode->i_mutex, I_MUTEX_CHILD); 792 + configfs_adjust_dir_dirent_depth_before_populate(sd); 793 ret = populate_groups(to_config_group(item)); 794 if (ret) { 795 configfs_detach_item(item); 796 dentry->d_inode->i_flags |= S_DEAD; 797 } 798 + configfs_adjust_dir_dirent_depth_after_populate(sd); 799 mutex_unlock(&dentry->d_inode->i_mutex); 800 if (ret) 801 d_delete(dentry); ··· 916 * Note, btw, that this can be called at *any* time, even when a configfs 917 * subsystem isn't registered, or when configfs is loading or unloading. 918 * Just like configfs_register_subsystem(). So we take the same 919 + * precautions. We pin the filesystem. We lock configfs_dirent_lock. 920 + * If we can find the target item in the 921 * configfs tree, it must be part of the subsystem tree as well, so we 922 + * do not need the subsystem semaphore. Holding configfs_dirent_lock helps 923 + * locking out mkdir() and rmdir(), who might be racing us. 924 */ 925 926 /* ··· 933 * do that so we can unlock it if we find nothing. 934 * 935 * Here we do a depth-first search of the dentry hierarchy looking for 936 + * our object. 937 + * We deliberately ignore items tagged as dropping since they are virtually 938 + * dead, as well as items in the middle of attachment since they virtually 939 + * do not exist yet. This completes the locking out of racing mkdir() and 940 + * rmdir(). 941 + * Note: subdirectories in the middle of attachment start with s_type = 942 + * CONFIGFS_DIR|CONFIGFS_USET_CREATING set by create_dir(). When 943 + * CONFIGFS_USET_CREATING is set, we ignore the item. The actual set of 944 + * s_type is in configfs_new_dirent(), which has configfs_dirent_lock. 945 * 946 + * If the target is not found, -ENOENT is bubbled up. 947 * 948 * This adds a requirement that all config_items be unique! 949 * 950 + * This is recursive. There isn't 951 * much on the stack, though, so folks that need this function - be careful 952 * about your stack! Patches will be accepted to make it iterative. 953 */ ··· 955 956 BUG_ON(!origin || !sd); 957 958 if (sd->s_element == target) /* Boo-yah */ 959 goto out; 960 961 list_for_each_entry(child_sd, &sd->s_children, s_sibling) { 962 + if ((child_sd->s_type & CONFIGFS_DIR) && 963 + !(child_sd->s_type & CONFIGFS_USET_DROPPING) && 964 + !(child_sd->s_type & CONFIGFS_USET_CREATING)) { 965 ret = configfs_depend_prep(child_sd->s_dentry, 966 target); 967 if (!ret) ··· 970 } 971 972 /* We looped all our children and didn't find target */ 973 ret = -ENOENT; 974 975 out: 976 return ret; 977 } 978 979 int configfs_depend_item(struct configfs_subsystem *subsys, ··· 1037 1038 /* Ok, now we can trust subsys/s_item */ 1039 1040 + spin_lock(&configfs_dirent_lock); 1041 + /* Scan the tree, return 0 if found */ 1042 ret = configfs_depend_prep(subsys_sd->s_dentry, target); 1043 if (ret) 1044 + goto out_unlock_dirent_lock; 1045 1046 + /* 1047 + * We are sure that the item is not about to be removed by rmdir(), and 1048 + * not in the middle of attachment by mkdir(). 1049 + */ 1050 p = target->ci_dentry->d_fsdata; 1051 p->s_dependent_count += 1; 1052 1053 + out_unlock_dirent_lock: 1054 + spin_unlock(&configfs_dirent_lock); 1055 out_unlock_fs: 1056 mutex_unlock(&configfs_sb->s_root->d_inode->i_mutex); 1057 ··· 1072 struct configfs_dirent *sd; 1073 1074 /* 1075 + * Since we can trust everything is pinned, we just need 1076 + * configfs_dirent_lock. 1077 */ 1078 + spin_lock(&configfs_dirent_lock); 1079 1080 sd = target->ci_dentry->d_fsdata; 1081 BUG_ON(sd->s_dependent_count < 1); ··· 1086 * After this unlock, we cannot trust the item to stay alive! 1087 * DO NOT REFERENCE item after this unlock. 1088 */ 1089 + spin_unlock(&configfs_dirent_lock); 1090 } 1091 EXPORT_SYMBOL(configfs_undepend_item); 1092 ··· 1286 if (sd->s_type & CONFIGFS_USET_DEFAULT) 1287 return -EPERM; 1288 1289 /* Get a working ref until we have the child */ 1290 parent_item = configfs_get_config_item(dentry->d_parent); 1291 subsys = to_config_group(parent_item)->cg_subsys; ··· 1316 1317 mutex_lock(&configfs_symlink_mutex); 1318 spin_lock(&configfs_dirent_lock); 1319 + /* 1320 + * Here's where we check for dependents. We're protected by 1321 + * configfs_dirent_lock. 1322 + * If no dependent, atomically tag the item as dropping. 1323 + */ 1324 + ret = sd->s_dependent_count ? -EBUSY : 0; 1325 + if (!ret) { 1326 + ret = configfs_detach_prep(dentry, &wait_mutex); 1327 + if (ret) 1328 + configfs_detach_rollback(dentry); 1329 + } 1330 spin_unlock(&configfs_dirent_lock); 1331 mutex_unlock(&configfs_symlink_mutex); 1332 ··· 1429 */ 1430 err = -ENOENT; 1431 if (configfs_dirent_is_ready(parent_sd)) { 1432 + file->private_data = configfs_new_dirent(parent_sd, NULL, 0); 1433 if (IS_ERR(file->private_data)) 1434 err = PTR_ERR(file->private_data); 1435 else
+38
fs/configfs/inode.c
··· 33 #include <linux/backing-dev.h> 34 #include <linux/capability.h> 35 #include <linux/sched.h> 36 37 #include <linux/configfs.h> 38 #include "configfs_internal.h" 39 40 extern struct super_block * configfs_sb; 41 ··· 155 return inode; 156 } 157 158 int configfs_create(struct dentry * dentry, int mode, int (*init)(struct inode *)) 159 { 160 int error = 0; ··· 199 struct inode *p_inode = dentry->d_parent->d_inode; 200 p_inode->i_mtime = p_inode->i_ctime = CURRENT_TIME; 201 } 202 goto Proceed; 203 } 204 else
··· 33 #include <linux/backing-dev.h> 34 #include <linux/capability.h> 35 #include <linux/sched.h> 36 + #include <linux/lockdep.h> 37 38 #include <linux/configfs.h> 39 #include "configfs_internal.h" 40 + 41 + #ifdef CONFIG_LOCKDEP 42 + static struct lock_class_key default_group_class[MAX_LOCK_DEPTH]; 43 + #endif 44 45 extern struct super_block * configfs_sb; 46 ··· 150 return inode; 151 } 152 153 + #ifdef CONFIG_LOCKDEP 154 + 155 + static void configfs_set_inode_lock_class(struct configfs_dirent *sd, 156 + struct inode *inode) 157 + { 158 + int depth = sd->s_depth; 159 + 160 + if (depth > 0) { 161 + if (depth <= ARRAY_SIZE(default_group_class)) { 162 + lockdep_set_class(&inode->i_mutex, 163 + &default_group_class[depth - 1]); 164 + } else { 165 + /* 166 + * In practice the maximum level of locking depth is 167 + * already reached. Just inform about possible reasons. 168 + */ 169 + printk(KERN_INFO "configfs: Too many levels of inodes" 170 + " for the locking correctness validator.\n"); 171 + printk(KERN_INFO "Spurious warnings may appear.\n"); 172 + } 173 + } 174 + } 175 + 176 + #else /* CONFIG_LOCKDEP */ 177 + 178 + static void configfs_set_inode_lock_class(struct configfs_dirent *sd, 179 + struct inode *inode) 180 + { 181 + } 182 + 183 + #endif /* CONFIG_LOCKDEP */ 184 + 185 int configfs_create(struct dentry * dentry, int mode, int (*init)(struct inode *)) 186 { 187 int error = 0; ··· 162 struct inode *p_inode = dentry->d_parent->d_inode; 163 p_inode->i_mtime = p_inode->i_ctime = CURRENT_TIME; 164 } 165 + configfs_set_inode_lock_class(sd, inode); 166 goto Proceed; 167 } 168 else