Skip to contentSkip to conversation

fsnotify: Cleanup spinlock assertions

PATCH Applied Jan Kara 2 participants 3 messages Sep 22, 2016 – Sep 23, 2016

Subsystems (3)
lore
Share & tools
lore

Jan Kara

author maintainer
#0 lore raw

PATCH 1/1 fsnotify: Cleanup spinlock assertions

Use assert_spin_locked() macro instead of hand-made BUG_ON statements.

---
 fs/notify/fanotify/fanotify_user.c | 3 +--
 fs/notify/notification.c           | 9 +++------
 2 files changed, 4 insertions(+), 8 deletions(-)

Andrew, can you please add this cleanup to the fanotify patches you carry? Thanks!

fs/notify/fanotify/fanotify_user.c +1 -2
index 189fab3ac4e6..7ebfca6a1427 100644
@@ -54,8 +54,7 @@ struct kmem_cache *fanotify_perm_event_cachep __read_mostly;
 static struct fsnotify_event *get_one_event(struct fsnotify_group *group,
 					    size_t count)
 {
-	BUG_ON(IS_ENABLED(CONFIG_SMP) &&
-	       !spin_is_locked(&group->notification_lock));
+	assert_spin_locked(&group->notification_lock);
 
 	pr_debug("%s: group=%p count=%zd\n", __func__, group, count);
 
fs/notify/notification.c +3 -6
index 1a8010e7a2a0..66f85c651c52 100644
@@ -63,8 +63,7 @@ EXPORT_SYMBOL_GPL(fsnotify_get_cookie);
 /* return true if the notify queue is empty, false otherwise */
 bool fsnotify_notify_queue_is_empty(struct fsnotify_group *group)
 {
-	BUG_ON(IS_ENABLED(CONFIG_SMP) &&
-	       !spin_is_locked(&group->notification_lock));
+	assert_spin_locked(&group->notification_lock);
 	return list_empty(&group->notification_list) ? true : false;
 }
 
@@ -149,8 +148,7 @@ struct fsnotify_event *fsnotify_remove_first_event(struct fsnotify_group *group)
 {
 	struct fsnotify_event *event;
 
-	BUG_ON(IS_ENABLED(CONFIG_SMP) &&
-	       !spin_is_locked(&group->notification_lock));
+	assert_spin_locked(&group->notification_lock);
 
 	pr_debug("%s: group=%p\n", __func__, group);
 
@@ -172,8 +170,7 @@ struct fsnotify_event *fsnotify_remove_first_event(struct fsnotify_group *group)
  */
 struct fsnotify_event *fsnotify_peek_first_event(struct fsnotify_group *group)
 {
-	BUG_ON(IS_ENABLED(CONFIG_SMP) &&
-	       !spin_is_locked(&group->notification_lock));
+	assert_spin_locked(&group->notification_lock);
 
 	return list_first_entry(&group->notification_list,
 				struct fsnotify_event, list);
Signature · 1 line
2.6.6
Reviewed-by on v1 1/1
#1 lore raw
Quoted 51 lines · Jan Kara

Use assert_spin_locked() macro instead of hand-made BUG_ON statements.

---
 fs/notify/fanotify/fanotify_user.c | 3 +--
 fs/notify/notification.c           | 9 +++------
 2 files changed, 4 insertions(+), 8 deletions(-)

Andrew, can you please add this cleanup to the fanotify patches you carry? Thanks!

fs/notify/fanotify/fanotify_user.c +1 -2
index 189fab3ac4e6..7ebfca6a1427 100644
@@ -54,8 +54,7 @@ struct kmem_cache *fanotify_perm_event_cachep __read_mostly;
 static struct fsnotify_event *get_one_event(struct fsnotify_group *group,
 					    size_t count)
 {
-	BUG_ON(IS_ENABLED(CONFIG_SMP) &&
-	       !spin_is_locked(&group->notification_lock));
+	assert_spin_locked(&group->notification_lock);
 
 	pr_debug("%s: group=%p count=%zd\n", __func__, group, count);
 
fs/notify/notification.c +2 -4
index 1a8010e7a2a0..66f85c651c52 100644
@@ -63,8 +63,7 @@ EXPORT_SYMBOL_GPL(fsnotify_get_cookie);
 /* return true if the notify queue is empty, false otherwise */
 bool fsnotify_notify_queue_is_empty(struct fsnotify_group *group)
 {
-	BUG_ON(IS_ENABLED(CONFIG_SMP) &&
-	       !spin_is_locked(&group->notification_lock));
+	assert_spin_locked(&group->notification_lock);
 	return list_empty(&group->notification_list) ? true : false;
 }
 
@@ -149,8 +148,7 @@ struct fsnotify_event *fsnotify_remove_first_event(struct fsnotify_group *group)
 {
 	struct fsnotify_event *event;
 
-	BUG_ON(IS_ENABLED(CONFIG_SMP) &&
-	       !spin_is_locked(&group->notification_lock));
+	assert_spin_locked(&group->notification_lock);
 
 	pr_debug("%s: group=%p\n", __func__, group);
 

fs/notify/notification.c struct fsnotify_event *fsnotify_remove_first_event(struct fsnotify_group *group) · line 176

Quoted 9 lines
fs/notify/notification.c +1 -2
@@ -172,8 +170,7 @@ struct fsnotify_event *fsnotify_remove_first_event(struct fsnotify_group *group)
  */
 struct fsnotify_event *fsnotify_peek_first_event(struct fsnotify_group *group)
 {
-	BUG_ON(IS_ENABLED(CONFIG_SMP) &&
-	       !spin_is_locked(&group->notification_lock));
+	assert_spin_locked(&group->notification_lock);
 
 	return list_first_entry(&group->notification_list,
 				struct fsnotify_event, list);

Much cleaner.

That said, I have a personal preference for lockdep_assert_held() in these situations, which not only tells you whether the lock is locked, but (I believe) whether it was locked by the current task as well.

Theoretically you could have a different task take this spinlock, and then call into here without holding it and not get the assertion since it was locked at the time. Of course, that does require lockdep...

In any case, this is still an improvement:

author maintainer
#2 lore raw
Quoted 59 lines · Jeff Layton
Quoted 58 lines · Jan Kara

Use assert_spin_locked() macro instead of hand-made BUG_ON statements.

---
 fs/notify/fanotify/fanotify_user.c | 3 +--
 fs/notify/notification.c           | 9 +++------
 2 files changed, 4 insertions(+), 8 deletions(-)

Andrew, can you please add this cleanup to the fanotify patches you carry? Thanks!

fs/notify/fanotify/fanotify_user.c +1 -2
index 189fab3ac4e6..7ebfca6a1427 100644
@@ -54,8 +54,7 @@ struct kmem_cache *fanotify_perm_event_cachep __read_mostly;
 static struct fsnotify_event *get_one_event(struct fsnotify_group *group,
 					    size_t count)
 {
-	BUG_ON(IS_ENABLED(CONFIG_SMP) &&
-	       !spin_is_locked(&group->notification_lock));
+	assert_spin_locked(&group->notification_lock);
 
 	pr_debug("%s: group=%p count=%zd\n", __func__, group, count);
 
fs/notify/notification.c +3 -6
index 1a8010e7a2a0..66f85c651c52 100644
@@ -63,8 +63,7 @@ EXPORT_SYMBOL_GPL(fsnotify_get_cookie);
 /* return true if the notify queue is empty, false otherwise */
 bool fsnotify_notify_queue_is_empty(struct fsnotify_group *group)
 {
-	BUG_ON(IS_ENABLED(CONFIG_SMP) &&
-	       !spin_is_locked(&group->notification_lock));
+	assert_spin_locked(&group->notification_lock);
 	return list_empty(&group->notification_list) ? true : false;
 }
 
@@ -149,8 +148,7 @@ struct fsnotify_event *fsnotify_remove_first_event(struct fsnotify_group *group)
 {
 	struct fsnotify_event *event;
 
-	BUG_ON(IS_ENABLED(CONFIG_SMP) &&
-	       !spin_is_locked(&group->notification_lock));
+	assert_spin_locked(&group->notification_lock);
 
 	pr_debug("%s: group=%p\n", __func__, group);
 
@@ -172,8 +170,7 @@ struct fsnotify_event *fsnotify_remove_first_event(struct fsnotify_group *group)
  */
 struct fsnotify_event *fsnotify_peek_first_event(struct fsnotify_group *group)
 {
-	BUG_ON(IS_ENABLED(CONFIG_SMP) &&
-	       !spin_is_locked(&group->notification_lock));
+	assert_spin_locked(&group->notification_lock);
 
Quoted 8 lines

fs/notify/notification.c struct fsnotify_event *fsnotify_remove_first_event(struct fsnotify_group *group) · line 176

Quoted 2 lines
fs/notify/notification.c +0 -0
 	return list_first_entry(&group->notification_list,
 				struct fsnotify_event, list);

Much cleaner.

That said, I have a personal preference for lockdep_assert_held() in these situations, which not only tells you whether the lock is locked, but (I believe) whether it was locked by the current task as well.

Yes, it does.

Quoted 3 lines

Theoretically you could have a different task take this spinlock, and then call into here without holding it and not get the assertion since it was locked at the time. Of course, that does require lockdep...

Yeah, I personally don't have a strong preference. Both have advantages and disadvantages - as you said, lockdep_assert_held() is reliable when lockdep is enabled but there's much less testing happening with lockdep enabled and also lockdep changes the timing enough that some cases just need not trigger...

Quoted 3 lines

In any case, this is still an improvement:

Thanks!

Honza

Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR