Commit c21dbe20 authored by Jan Kara's avatar Jan Kara Committed by Linus Torvalds

fsnotify: convert notification_mutex to a spinlock

notification_mutex is used to protect the list of pending events.  As such
there's no reason to use a sleeping lock for it.  Convert it to a
spinlock.

[jack@suse.cz: fixed version]
  Link: http://lkml.kernel.org/r/1474031567-1831-1-git-send-email-jack@suse.cz
Link: http://lkml.kernel.org/r/1473797711-14111-5-git-send-email-jack@suse.czSigned-off-by: default avatarJan Kara <jack@suse.cz>
Reviewed-by: default avatarLino Sanfilippo <LinoSanfilippo@gmx.de>
Tested-by: default avatarGuenter Roeck <linux@roeck-us.net>
Cc: Miklos Szeredi <mszeredi@redhat.com>
Cc: Eric Paris <eparis@redhat.com>
Cc: Al Viro <viro@ZenIV.linux.org.uk>
Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
Signed-off-by: default avatarLinus Torvalds <torvalds@linux-foundation.org>
parent 1404ff3c
...@@ -49,12 +49,13 @@ struct kmem_cache *fanotify_perm_event_cachep __read_mostly; ...@@ -49,12 +49,13 @@ struct kmem_cache *fanotify_perm_event_cachep __read_mostly;
* enough to fit in "count". Return an error pointer if the count * enough to fit in "count". Return an error pointer if the count
* is not large enough. * is not large enough.
* *
* Called with the group->notification_mutex held. * Called with the group->notification_lock held.
*/ */
static struct fsnotify_event *get_one_event(struct fsnotify_group *group, static struct fsnotify_event *get_one_event(struct fsnotify_group *group,
size_t count) size_t count)
{ {
BUG_ON(!mutex_is_locked(&group->notification_mutex)); BUG_ON(IS_ENABLED(CONFIG_SMP) &&
!spin_is_locked(&group->notification_lock));
pr_debug("%s: group=%p count=%zd\n", __func__, group, count); pr_debug("%s: group=%p count=%zd\n", __func__, group, count);
...@@ -64,7 +65,7 @@ static struct fsnotify_event *get_one_event(struct fsnotify_group *group, ...@@ -64,7 +65,7 @@ static struct fsnotify_event *get_one_event(struct fsnotify_group *group,
if (FAN_EVENT_METADATA_LEN > count) if (FAN_EVENT_METADATA_LEN > count)
return ERR_PTR(-EINVAL); return ERR_PTR(-EINVAL);
/* held the notification_mutex the whole time, so this is the /* held the notification_lock the whole time, so this is the
* same event we peeked above */ * same event we peeked above */
return fsnotify_remove_first_event(group); return fsnotify_remove_first_event(group);
} }
...@@ -244,10 +245,10 @@ static unsigned int fanotify_poll(struct file *file, poll_table *wait) ...@@ -244,10 +245,10 @@ static unsigned int fanotify_poll(struct file *file, poll_table *wait)
int ret = 0; int ret = 0;
poll_wait(file, &group->notification_waitq, wait); poll_wait(file, &group->notification_waitq, wait);
mutex_lock(&group->notification_mutex); spin_lock(&group->notification_lock);
if (!fsnotify_notify_queue_is_empty(group)) if (!fsnotify_notify_queue_is_empty(group))
ret = POLLIN | POLLRDNORM; ret = POLLIN | POLLRDNORM;
mutex_unlock(&group->notification_mutex); spin_unlock(&group->notification_lock);
return ret; return ret;
} }
...@@ -268,9 +269,9 @@ static ssize_t fanotify_read(struct file *file, char __user *buf, ...@@ -268,9 +269,9 @@ static ssize_t fanotify_read(struct file *file, char __user *buf,
add_wait_queue(&group->notification_waitq, &wait); add_wait_queue(&group->notification_waitq, &wait);
while (1) { while (1) {
mutex_lock(&group->notification_mutex); spin_lock(&group->notification_lock);
kevent = get_one_event(group, count); kevent = get_one_event(group, count);
mutex_unlock(&group->notification_mutex); spin_unlock(&group->notification_lock);
if (IS_ERR(kevent)) { if (IS_ERR(kevent)) {
ret = PTR_ERR(kevent); ret = PTR_ERR(kevent);
...@@ -387,17 +388,17 @@ static int fanotify_release(struct inode *ignored, struct file *file) ...@@ -387,17 +388,17 @@ static int fanotify_release(struct inode *ignored, struct file *file)
* dequeue them and set the response. They will be freed once the * dequeue them and set the response. They will be freed once the
* response is consumed and fanotify_get_response() returns. * response is consumed and fanotify_get_response() returns.
*/ */
mutex_lock(&group->notification_mutex); spin_lock(&group->notification_lock);
while (!fsnotify_notify_queue_is_empty(group)) { while (!fsnotify_notify_queue_is_empty(group)) {
fsn_event = fsnotify_remove_first_event(group); fsn_event = fsnotify_remove_first_event(group);
if (!(fsn_event->mask & FAN_ALL_PERM_EVENTS)) { if (!(fsn_event->mask & FAN_ALL_PERM_EVENTS)) {
mutex_unlock(&group->notification_mutex); spin_unlock(&group->notification_lock);
fsnotify_destroy_event(group, fsn_event); fsnotify_destroy_event(group, fsn_event);
mutex_lock(&group->notification_mutex); spin_lock(&group->notification_lock);
} else } else
FANOTIFY_PE(fsn_event)->response = FAN_ALLOW; FANOTIFY_PE(fsn_event)->response = FAN_ALLOW;
} }
mutex_unlock(&group->notification_mutex); spin_unlock(&group->notification_lock);
/* Response for all permission events it set, wakeup waiters */ /* Response for all permission events it set, wakeup waiters */
wake_up(&group->fanotify_data.access_waitq); wake_up(&group->fanotify_data.access_waitq);
...@@ -423,10 +424,10 @@ static long fanotify_ioctl(struct file *file, unsigned int cmd, unsigned long ar ...@@ -423,10 +424,10 @@ static long fanotify_ioctl(struct file *file, unsigned int cmd, unsigned long ar
switch (cmd) { switch (cmd) {
case FIONREAD: case FIONREAD:
mutex_lock(&group->notification_mutex); spin_lock(&group->notification_lock);
list_for_each_entry(fsn_event, &group->notification_list, list) list_for_each_entry(fsn_event, &group->notification_list, list)
send_len += FAN_EVENT_METADATA_LEN; send_len += FAN_EVENT_METADATA_LEN;
mutex_unlock(&group->notification_mutex); spin_unlock(&group->notification_lock);
ret = put_user(send_len, (int __user *) p); ret = put_user(send_len, (int __user *) p);
break; break;
} }
......
...@@ -45,9 +45,9 @@ static void fsnotify_final_destroy_group(struct fsnotify_group *group) ...@@ -45,9 +45,9 @@ static void fsnotify_final_destroy_group(struct fsnotify_group *group)
*/ */
void fsnotify_group_stop_queueing(struct fsnotify_group *group) void fsnotify_group_stop_queueing(struct fsnotify_group *group)
{ {
mutex_lock(&group->notification_mutex); spin_lock(&group->notification_lock);
group->shutdown = true; group->shutdown = true;
mutex_unlock(&group->notification_mutex); spin_unlock(&group->notification_lock);
} }
/* /*
...@@ -125,7 +125,7 @@ struct fsnotify_group *fsnotify_alloc_group(const struct fsnotify_ops *ops) ...@@ -125,7 +125,7 @@ struct fsnotify_group *fsnotify_alloc_group(const struct fsnotify_ops *ops)
atomic_set(&group->refcnt, 1); atomic_set(&group->refcnt, 1);
atomic_set(&group->num_marks, 0); atomic_set(&group->num_marks, 0);
mutex_init(&group->notification_mutex); spin_lock_init(&group->notification_lock);
INIT_LIST_HEAD(&group->notification_list); INIT_LIST_HEAD(&group->notification_list);
init_waitqueue_head(&group->notification_waitq); init_waitqueue_head(&group->notification_waitq);
group->max_events = UINT_MAX; group->max_events = UINT_MAX;
......
...@@ -115,10 +115,10 @@ static unsigned int inotify_poll(struct file *file, poll_table *wait) ...@@ -115,10 +115,10 @@ static unsigned int inotify_poll(struct file *file, poll_table *wait)
int ret = 0; int ret = 0;
poll_wait(file, &group->notification_waitq, wait); poll_wait(file, &group->notification_waitq, wait);
mutex_lock(&group->notification_mutex); spin_lock(&group->notification_lock);
if (!fsnotify_notify_queue_is_empty(group)) if (!fsnotify_notify_queue_is_empty(group))
ret = POLLIN | POLLRDNORM; ret = POLLIN | POLLRDNORM;
mutex_unlock(&group->notification_mutex); spin_unlock(&group->notification_lock);
return ret; return ret;
} }
...@@ -138,7 +138,7 @@ static int round_event_name_len(struct fsnotify_event *fsn_event) ...@@ -138,7 +138,7 @@ static int round_event_name_len(struct fsnotify_event *fsn_event)
* enough to fit in "count". Return an error pointer if * enough to fit in "count". Return an error pointer if
* not large enough. * not large enough.
* *
* Called with the group->notification_mutex held. * Called with the group->notification_lock held.
*/ */
static struct fsnotify_event *get_one_event(struct fsnotify_group *group, static struct fsnotify_event *get_one_event(struct fsnotify_group *group,
size_t count) size_t count)
...@@ -157,7 +157,7 @@ static struct fsnotify_event *get_one_event(struct fsnotify_group *group, ...@@ -157,7 +157,7 @@ static struct fsnotify_event *get_one_event(struct fsnotify_group *group,
if (event_size > count) if (event_size > count)
return ERR_PTR(-EINVAL); return ERR_PTR(-EINVAL);
/* held the notification_mutex the whole time, so this is the /* held the notification_lock the whole time, so this is the
* same event we peeked above */ * same event we peeked above */
fsnotify_remove_first_event(group); fsnotify_remove_first_event(group);
...@@ -234,9 +234,9 @@ static ssize_t inotify_read(struct file *file, char __user *buf, ...@@ -234,9 +234,9 @@ static ssize_t inotify_read(struct file *file, char __user *buf,
add_wait_queue(&group->notification_waitq, &wait); add_wait_queue(&group->notification_waitq, &wait);
while (1) { while (1) {
mutex_lock(&group->notification_mutex); spin_lock(&group->notification_lock);
kevent = get_one_event(group, count); kevent = get_one_event(group, count);
mutex_unlock(&group->notification_mutex); spin_unlock(&group->notification_lock);
pr_debug("%s: group=%p kevent=%p\n", __func__, group, kevent); pr_debug("%s: group=%p kevent=%p\n", __func__, group, kevent);
...@@ -300,13 +300,13 @@ static long inotify_ioctl(struct file *file, unsigned int cmd, ...@@ -300,13 +300,13 @@ static long inotify_ioctl(struct file *file, unsigned int cmd,
switch (cmd) { switch (cmd) {
case FIONREAD: case FIONREAD:
mutex_lock(&group->notification_mutex); spin_lock(&group->notification_lock);
list_for_each_entry(fsn_event, &group->notification_list, list_for_each_entry(fsn_event, &group->notification_list,
list) { list) {
send_len += sizeof(struct inotify_event); send_len += sizeof(struct inotify_event);
send_len += round_event_name_len(fsn_event); send_len += round_event_name_len(fsn_event);
} }
mutex_unlock(&group->notification_mutex); spin_unlock(&group->notification_lock);
ret = put_user(send_len, (int __user *) p); ret = put_user(send_len, (int __user *) p);
break; break;
} }
......
...@@ -63,7 +63,8 @@ EXPORT_SYMBOL_GPL(fsnotify_get_cookie); ...@@ -63,7 +63,8 @@ EXPORT_SYMBOL_GPL(fsnotify_get_cookie);
/* return true if the notify queue is empty, false otherwise */ /* return true if the notify queue is empty, false otherwise */
bool fsnotify_notify_queue_is_empty(struct fsnotify_group *group) bool fsnotify_notify_queue_is_empty(struct fsnotify_group *group)
{ {
BUG_ON(!mutex_is_locked(&group->notification_mutex)); BUG_ON(IS_ENABLED(CONFIG_SMP) &&
!spin_is_locked(&group->notification_lock));
return list_empty(&group->notification_list) ? true : false; return list_empty(&group->notification_list) ? true : false;
} }
...@@ -95,10 +96,10 @@ int fsnotify_add_event(struct fsnotify_group *group, ...@@ -95,10 +96,10 @@ int fsnotify_add_event(struct fsnotify_group *group,
pr_debug("%s: group=%p event=%p\n", __func__, group, event); pr_debug("%s: group=%p event=%p\n", __func__, group, event);
mutex_lock(&group->notification_mutex); spin_lock(&group->notification_lock);
if (group->shutdown) { if (group->shutdown) {
mutex_unlock(&group->notification_mutex); spin_unlock(&group->notification_lock);
return 2; return 2;
} }
...@@ -106,7 +107,7 @@ int fsnotify_add_event(struct fsnotify_group *group, ...@@ -106,7 +107,7 @@ int fsnotify_add_event(struct fsnotify_group *group,
ret = 2; ret = 2;
/* Queue overflow event only if it isn't already queued */ /* Queue overflow event only if it isn't already queued */
if (!list_empty(&group->overflow_event->list)) { if (!list_empty(&group->overflow_event->list)) {
mutex_unlock(&group->notification_mutex); spin_unlock(&group->notification_lock);
return ret; return ret;
} }
event = group->overflow_event; event = group->overflow_event;
...@@ -116,7 +117,7 @@ int fsnotify_add_event(struct fsnotify_group *group, ...@@ -116,7 +117,7 @@ int fsnotify_add_event(struct fsnotify_group *group,
if (!list_empty(list) && merge) { if (!list_empty(list) && merge) {
ret = merge(list, event); ret = merge(list, event);
if (ret) { if (ret) {
mutex_unlock(&group->notification_mutex); spin_unlock(&group->notification_lock);
return ret; return ret;
} }
} }
...@@ -124,7 +125,7 @@ int fsnotify_add_event(struct fsnotify_group *group, ...@@ -124,7 +125,7 @@ int fsnotify_add_event(struct fsnotify_group *group,
queue: queue:
group->q_len++; group->q_len++;
list_add_tail(&event->list, list); list_add_tail(&event->list, list);
mutex_unlock(&group->notification_mutex); spin_unlock(&group->notification_lock);
wake_up(&group->notification_waitq); wake_up(&group->notification_waitq);
kill_fasync(&group->fsn_fa, SIGIO, POLL_IN); kill_fasync(&group->fsn_fa, SIGIO, POLL_IN);
...@@ -139,7 +140,8 @@ struct fsnotify_event *fsnotify_remove_first_event(struct fsnotify_group *group) ...@@ -139,7 +140,8 @@ struct fsnotify_event *fsnotify_remove_first_event(struct fsnotify_group *group)
{ {
struct fsnotify_event *event; struct fsnotify_event *event;
BUG_ON(!mutex_is_locked(&group->notification_mutex)); BUG_ON(IS_ENABLED(CONFIG_SMP) &&
!spin_is_locked(&group->notification_lock));
pr_debug("%s: group=%p\n", __func__, group); pr_debug("%s: group=%p\n", __func__, group);
...@@ -161,7 +163,8 @@ struct fsnotify_event *fsnotify_remove_first_event(struct fsnotify_group *group) ...@@ -161,7 +163,8 @@ struct fsnotify_event *fsnotify_remove_first_event(struct fsnotify_group *group)
*/ */
struct fsnotify_event *fsnotify_peek_first_event(struct fsnotify_group *group) struct fsnotify_event *fsnotify_peek_first_event(struct fsnotify_group *group)
{ {
BUG_ON(!mutex_is_locked(&group->notification_mutex)); BUG_ON(IS_ENABLED(CONFIG_SMP) &&
!spin_is_locked(&group->notification_lock));
return list_first_entry(&group->notification_list, return list_first_entry(&group->notification_list,
struct fsnotify_event, list); struct fsnotify_event, list);
...@@ -175,14 +178,14 @@ void fsnotify_flush_notify(struct fsnotify_group *group) ...@@ -175,14 +178,14 @@ void fsnotify_flush_notify(struct fsnotify_group *group)
{ {
struct fsnotify_event *event; struct fsnotify_event *event;
mutex_lock(&group->notification_mutex); spin_lock(&group->notification_lock);
while (!fsnotify_notify_queue_is_empty(group)) { while (!fsnotify_notify_queue_is_empty(group)) {
event = fsnotify_remove_first_event(group); event = fsnotify_remove_first_event(group);
mutex_unlock(&group->notification_mutex); spin_unlock(&group->notification_lock);
fsnotify_destroy_event(group, event); fsnotify_destroy_event(group, event);
mutex_lock(&group->notification_mutex); spin_lock(&group->notification_lock);
} }
mutex_unlock(&group->notification_mutex); spin_unlock(&group->notification_lock);
} }
/* /*
......
...@@ -135,7 +135,7 @@ struct fsnotify_group { ...@@ -135,7 +135,7 @@ struct fsnotify_group {
const struct fsnotify_ops *ops; /* how this group handles things */ const struct fsnotify_ops *ops; /* how this group handles things */
/* needed to send notification to userspace */ /* needed to send notification to userspace */
struct mutex notification_mutex; /* protect the notification_list */ spinlock_t notification_lock; /* protect the notification_list */
struct list_head notification_list; /* list of event_holder this group needs to send to userspace */ struct list_head notification_list; /* list of event_holder this group needs to send to userspace */
wait_queue_head_t notification_waitq; /* read() on the notification file blocks on this waitq */ wait_queue_head_t notification_waitq; /* read() on the notification file blocks on this waitq */
unsigned int q_len; /* events on the queue */ unsigned int q_len; /* events on the queue */
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment