diff options
author | Tejun Heo <tj@kernel.org> | 2013-10-30 10:28:36 -0400 |
---|---|---|
committer | Greg Kroah-Hartman <gregkh@linuxfoundation.org> | 2013-11-01 15:13:37 -0400 |
commit | 0cae60f91494e34a0c5391f1455f825d5849b05f (patch) | |
tree | 56746a582c79df11a88fe0016ea4919c018d5553 /fs/sysfs/dir.c | |
parent | 044e3bc33391b1f2769d5ab2c04f246c3d8e04c3 (diff) |
sysfs: rename sysfs_assoc_lock and explain what it's about
sysfs_assoc_lock is an odd piece of locking. In general, whoever owns
a kobject is responsible for synchronizing sysfs operations and sysfs
proper assumes that, for example, removal won't race with any other
operation; however, this doesn't work for symlinking because an entity
performing symlink doesn't usually own the target kobject and thus has
no control over its removal.
sysfs_assoc_lock synchronizes symlink operations against kobj->sd
disassociation so that symlink code doesn't end up dereferencing
already freed sysfs_dirent by racing with removal of the target
kobject.
This is quite obscure and the generic name of the lock and lack of
comments make it difficult to understand its role. Let's rename it to
sysfs_symlink_target_lock and add comments explaining what's going on.
Signed-off-by: Tejun Heo <tj@kernel.org>
Reported-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Diffstat (limited to 'fs/sysfs/dir.c')
-rw-r--r-- | fs/sysfs/dir.c | 18 |
1 files changed, 15 insertions, 3 deletions
diff --git a/fs/sysfs/dir.c b/fs/sysfs/dir.c index de47ed32d5c7..08c66969d52a 100644 --- a/fs/sysfs/dir.c +++ b/fs/sysfs/dir.c | |||
@@ -26,7 +26,7 @@ | |||
26 | #include "sysfs.h" | 26 | #include "sysfs.h" |
27 | 27 | ||
28 | DEFINE_MUTEX(sysfs_mutex); | 28 | DEFINE_MUTEX(sysfs_mutex); |
29 | DEFINE_SPINLOCK(sysfs_assoc_lock); | 29 | DEFINE_SPINLOCK(sysfs_symlink_target_lock); |
30 | 30 | ||
31 | #define to_sysfs_dirent(X) rb_entry((X), struct sysfs_dirent, s_rb) | 31 | #define to_sysfs_dirent(X) rb_entry((X), struct sysfs_dirent, s_rb) |
32 | 32 | ||
@@ -902,9 +902,21 @@ void sysfs_remove_dir(struct kobject *kobj) | |||
902 | { | 902 | { |
903 | struct sysfs_dirent *sd = kobj->sd; | 903 | struct sysfs_dirent *sd = kobj->sd; |
904 | 904 | ||
905 | spin_lock(&sysfs_assoc_lock); | 905 | /* |
906 | * In general, kboject owner is responsible for ensuring removal | ||
907 | * doesn't race with other operations and sysfs doesn't provide any | ||
908 | * protection; however, when @kobj is used as a symlink target, the | ||
909 | * symlinking entity usually doesn't own @kobj and thus has no | ||
910 | * control over removal. @kobj->sd may be removed anytime and | ||
911 | * symlink code may end up dereferencing an already freed sd. | ||
912 | * | ||
913 | * sysfs_symlink_target_lock synchronizes @kobj->sd disassociation | ||
914 | * against symlink operations so that symlink code can safely | ||
915 | * dereference @kobj->sd. | ||
916 | */ | ||
917 | spin_lock(&sysfs_symlink_target_lock); | ||
906 | kobj->sd = NULL; | 918 | kobj->sd = NULL; |
907 | spin_unlock(&sysfs_assoc_lock); | 919 | spin_unlock(&sysfs_symlink_target_lock); |
908 | 920 | ||
909 | if (sd) { | 921 | if (sd) { |
910 | WARN_ON_ONCE(sysfs_type(sd) != SYSFS_DIR); | 922 | WARN_ON_ONCE(sysfs_type(sd) != SYSFS_DIR); |