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 39 umode_t s_mode; 40 40 struct dentry * s_dentry; 41 41 struct iattr * s_iattr; 42 + #ifdef CONFIG_LOCKDEP 43 + int s_depth; 44 + #endif 42 45 }; 43 46 44 47 #define CONFIGFS_ROOT 0x0001
+137 -59
fs/configfs/dir.c
··· 78 78 .d_delete = configfs_d_delete, 79 79 }; 80 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 + 81 167 /* 82 168 * Allocates a new configfs_dirent and links it to the parent configfs_dirent 83 169 */ 84 - static struct configfs_dirent *configfs_new_dirent(struct configfs_dirent * parent_sd, 85 - void * element) 170 + static struct configfs_dirent *configfs_new_dirent(struct configfs_dirent *parent_sd, 171 + void *element, int type) 86 172 { 87 173 struct configfs_dirent * sd; 88 174 ··· 180 94 INIT_LIST_HEAD(&sd->s_links); 181 95 INIT_LIST_HEAD(&sd->s_children); 182 96 sd->s_element = element; 97 + sd->s_type = type; 98 + configfs_init_dirent_depth(sd); 183 99 spin_lock(&configfs_dirent_lock); 184 100 if (parent_sd->s_type & CONFIGFS_USET_DROPPING) { 185 101 spin_unlock(&configfs_dirent_lock); ··· 226 138 { 227 139 struct configfs_dirent * sd; 228 140 229 - sd = configfs_new_dirent(parent_sd, element); 141 + sd = configfs_new_dirent(parent_sd, element, type); 230 142 if (IS_ERR(sd)) 231 143 return PTR_ERR(sd); 232 144 233 145 sd->s_mode = mode; 234 - sd->s_type = type; 235 146 sd->s_dentry = dentry; 236 147 if (dentry) { 237 148 dentry->d_fsdata = configfs_get(sd); ··· 274 187 error = configfs_make_dirent(p->d_fsdata, d, k, mode, 275 188 CONFIGFS_DIR | CONFIGFS_USET_CREATING); 276 189 if (!error) { 190 + configfs_set_dir_dirent_depth(p->d_fsdata, d->d_fsdata); 277 191 error = configfs_create(d, mode, init_dir); 278 192 if (!error) { 279 193 inc_nlink(p->d_inode); ··· 877 789 * error, as rmdir() would. 878 790 */ 879 791 mutex_lock_nested(&dentry->d_inode->i_mutex, I_MUTEX_CHILD); 792 + configfs_adjust_dir_dirent_depth_before_populate(sd); 880 793 ret = populate_groups(to_config_group(item)); 881 794 if (ret) { 882 795 configfs_detach_item(item); 883 796 dentry->d_inode->i_flags |= S_DEAD; 884 797 } 798 + configfs_adjust_dir_dirent_depth_after_populate(sd); 885 799 mutex_unlock(&dentry->d_inode->i_mutex); 886 800 if (ret) 887 801 d_delete(dentry); ··· 1006 916 * Note, btw, that this can be called at *any* time, even when a configfs 1007 917 * subsystem isn't registered, or when configfs is loading or unloading. 1008 918 * 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 919 + * precautions. We pin the filesystem. We lock configfs_dirent_lock. 920 + * If we can find the target item in the 1011 921 * 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. 922 + * do not need the subsystem semaphore. Holding configfs_dirent_lock helps 923 + * locking out mkdir() and rmdir(), who might be racing us. 1014 924 */ 1015 925 1016 926 /* ··· 1023 933 * do that so we can unlock it if we find nothing. 1024 934 * 1025 935 * 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. 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. 1029 945 * 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(). 946 + * If the target is not found, -ENOENT is bubbled up. 1033 947 * 1034 948 * This adds a requirement that all config_items be unique! 1035 949 * 1036 - * This is recursive because the locking traversal is tricky. There isn't 950 + * This is recursive. There isn't 1037 951 * much on the stack, though, so folks that need this function - be careful 1038 952 * about your stack! Patches will be accepted to make it iterative. 1039 953 */ ··· 1049 955 1050 956 BUG_ON(!origin || !sd); 1051 957 1052 - /* Lock this guy on the way down */ 1053 - mutex_lock(&sd->s_dentry->d_inode->i_mutex); 1054 958 if (sd->s_element == target) /* Boo-yah */ 1055 959 goto out; 1056 960 1057 961 list_for_each_entry(child_sd, &sd->s_children, s_sibling) { 1058 - if (child_sd->s_type & CONFIGFS_DIR) { 962 + if ((child_sd->s_type & CONFIGFS_DIR) && 963 + !(child_sd->s_type & CONFIGFS_USET_DROPPING) && 964 + !(child_sd->s_type & CONFIGFS_USET_CREATING)) { 1059 965 ret = configfs_depend_prep(child_sd->s_dentry, 1060 966 target); 1061 967 if (!ret) ··· 1064 970 } 1065 971 1066 972 /* We looped all our children and didn't find target */ 1067 - mutex_unlock(&sd->s_dentry->d_inode->i_mutex); 1068 973 ret = -ENOENT; 1069 974 1070 975 out: 1071 976 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 977 } 1093 978 1094 979 int configfs_depend_item(struct configfs_subsystem *subsys, ··· 1110 1037 1111 1038 /* Ok, now we can trust subsys/s_item */ 1112 1039 1113 - /* Scan the tree, locking i_mutex recursively, return 0 if found */ 1040 + spin_lock(&configfs_dirent_lock); 1041 + /* Scan the tree, return 0 if found */ 1114 1042 ret = configfs_depend_prep(subsys_sd->s_dentry, target); 1115 1043 if (ret) 1116 - goto out_unlock_fs; 1044 + goto out_unlock_dirent_lock; 1117 1045 1118 - /* We hold all i_mutexes from the subsystem down to the target */ 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 + */ 1119 1050 p = target->ci_dentry->d_fsdata; 1120 1051 p->s_dependent_count += 1; 1121 1052 1122 - configfs_depend_rollback(subsys_sd->s_dentry, target); 1123 - 1053 + out_unlock_dirent_lock: 1054 + spin_unlock(&configfs_dirent_lock); 1124 1055 out_unlock_fs: 1125 1056 mutex_unlock(&configfs_sb->s_root->d_inode->i_mutex); 1126 1057 ··· 1149 1072 struct configfs_dirent *sd; 1150 1073 1151 1074 /* 1152 - * Since we can trust everything is pinned, we just need i_mutex 1153 - * on the item. 1075 + * Since we can trust everything is pinned, we just need 1076 + * configfs_dirent_lock. 1154 1077 */ 1155 - mutex_lock(&target->ci_dentry->d_inode->i_mutex); 1078 + spin_lock(&configfs_dirent_lock); 1156 1079 1157 1080 sd = target->ci_dentry->d_fsdata; 1158 1081 BUG_ON(sd->s_dependent_count < 1); ··· 1163 1086 * After this unlock, we cannot trust the item to stay alive! 1164 1087 * DO NOT REFERENCE item after this unlock. 1165 1088 */ 1166 - mutex_unlock(&target->ci_dentry->d_inode->i_mutex); 1089 + spin_unlock(&configfs_dirent_lock); 1167 1090 } 1168 1091 EXPORT_SYMBOL(configfs_undepend_item); 1169 1092 ··· 1363 1286 if (sd->s_type & CONFIGFS_USET_DEFAULT) 1364 1287 return -EPERM; 1365 1288 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 1289 /* Get a working ref until we have the child */ 1374 1290 parent_item = configfs_get_config_item(dentry->d_parent); 1375 1291 subsys = to_config_group(parent_item)->cg_subsys; ··· 1386 1316 1387 1317 mutex_lock(&configfs_symlink_mutex); 1388 1318 spin_lock(&configfs_dirent_lock); 1389 - ret = configfs_detach_prep(dentry, &wait_mutex); 1390 - if (ret) 1391 - configfs_detach_rollback(dentry); 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 + } 1392 1330 spin_unlock(&configfs_dirent_lock); 1393 1331 mutex_unlock(&configfs_symlink_mutex); 1394 1332 ··· 1507 1429 */ 1508 1430 err = -ENOENT; 1509 1431 if (configfs_dirent_is_ready(parent_sd)) { 1510 - file->private_data = configfs_new_dirent(parent_sd, NULL); 1432 + file->private_data = configfs_new_dirent(parent_sd, NULL, 0); 1511 1433 if (IS_ERR(file->private_data)) 1512 1434 err = PTR_ERR(file->private_data); 1513 1435 else
+38
fs/configfs/inode.c
··· 33 33 #include <linux/backing-dev.h> 34 34 #include <linux/capability.h> 35 35 #include <linux/sched.h> 36 + #include <linux/lockdep.h> 36 37 37 38 #include <linux/configfs.h> 38 39 #include "configfs_internal.h" 40 + 41 + #ifdef CONFIG_LOCKDEP 42 + static struct lock_class_key default_group_class[MAX_LOCK_DEPTH]; 43 + #endif 39 44 40 45 extern struct super_block * configfs_sb; 41 46 ··· 155 150 return inode; 156 151 } 157 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 + 158 185 int configfs_create(struct dentry * dentry, int mode, int (*init)(struct inode *)) 159 186 { 160 187 int error = 0; ··· 199 162 struct inode *p_inode = dentry->d_parent->d_inode; 200 163 p_inode->i_mtime = p_inode->i_ctime = CURRENT_TIME; 201 164 } 165 + configfs_set_inode_lock_class(sd, inode); 202 166 goto Proceed; 203 167 } 204 168 else