Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > linux.kernel > #1608990

[PATCH 2/5] inotify: Use mutex to prevent threaded clients reading events out of order

From Jes Sorensen <jes.sorensen@gmail.com>
Newsgroups linux.kernel
Subject [PATCH 2/5] inotify: Use mutex to prevent threaded clients reading events out of order
Date 2017-03-24 22:40 +0100
Message-ID <toF69-7By-17@gated-at.bofh.it> (permalink)
References <toF69-7By-1@gated-at.bofh.it>
Organization linux.* mail to news gateway

Show all headers | View raw


Introduces mutex in the read() path to prevent a threaded client reading
from the same fd consuming events out of order.

This will matter when avoiding taking the spinlock when consuming each
event in the read() path.

Signed-off-by: Jes Sorensen <jsorensen@fb.com>
Reviewed-by: Josef Bacik <jbacik@fb.com>
---
 fs/notify/inotify/inotify_fsnotify.c | 1 +
 fs/notify/inotify/inotify_user.c     | 4 ++++
 include/linux/fsnotify_backend.h     | 3 +++
 3 files changed, 8 insertions(+)

diff --git a/fs/notify/inotify/inotify_fsnotify.c b/fs/notify/inotify/inotify_fsnotify.c
index 1aeb837..63c071f 100644
--- a/fs/notify/inotify/inotify_fsnotify.c
+++ b/fs/notify/inotify/inotify_fsnotify.c
@@ -168,6 +168,7 @@ static void inotify_free_group_priv(struct fsnotify_group *group)
 	idr_destroy(&group->inotify_data.idr);
 	if (group->inotify_data.ucounts)
 		dec_inotify_instances(group->inotify_data.ucounts);
+	mutex_destroy(&group->inotify_data.consumer_mutex);
 }
 
 static void inotify_free_event(struct fsnotify_event *fsn_event)
diff --git a/fs/notify/inotify/inotify_user.c b/fs/notify/inotify/inotify_user.c
index 498d609..6f5b360 100644
--- a/fs/notify/inotify/inotify_user.c
+++ b/fs/notify/inotify/inotify_user.c
@@ -230,6 +230,7 @@ static ssize_t inotify_read(struct file *file, char __user *buf,
 	start = buf;
 	group = file->private_data;
 
+	mutex_lock(&group->inotify_data.consumer_mutex);
 	add_wait_queue(&group->notification_waitq, &wait);
 	while (1) {
 		spin_lock(&group->notification_lock);
@@ -264,6 +265,7 @@ static ssize_t inotify_read(struct file *file, char __user *buf,
 		wait_woken(&wait, TASK_INTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
 	}
 	remove_wait_queue(&group->notification_waitq, &wait);
+	mutex_unlock(&group->inotify_data.consumer_mutex);
 
 	if (start != buf && ret != -EFAULT)
 		ret = buf - start;
@@ -650,6 +652,8 @@ static struct fsnotify_group *inotify_new_group(unsigned int max_events)
 
 	group->max_events = max_events;
 
+	mutex_init(&group->inotify_data.consumer_mutex);
+
 	spin_lock_init(&group->inotify_data.idr_lock);
 	idr_init(&group->inotify_data.idr);
 	group->inotify_data.ucounts = inc_ucount(current_user_ns(),
diff --git a/include/linux/fsnotify_backend.h b/include/linux/fsnotify_backend.h
index e6e689b..e0686ed 100644
--- a/include/linux/fsnotify_backend.h
+++ b/include/linux/fsnotify_backend.h
@@ -172,6 +172,9 @@ struct fsnotify_group {
 			spinlock_t	idr_lock;
 			struct idr      idr;
 			struct ucounts *ucounts;
+			struct mutex	consumer_mutex; /* Prevent out of order
+							 * delivery of events
+							 * to threaded consumers */
 		} inotify_data;
 #endif
 #ifdef CONFIG_FANOTIFY
-- 
2.9.3

Back to linux.kernel | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

[PATCH 0/5] inofify: Reduce lock contention on read() Jes Sorensen <jes.sorensen@gmail.com> - 2017-03-24 22:40 +0100
  [PATCH 5/5] inotify: Avoid taking spinlock for each event processed in read() Jes Sorensen <jes.sorensen@gmail.com> - 2017-03-24 22:40 +0100
  [PATCH 1/5] notify: Call mutex_destroy() before freeing mutex memory Jes Sorensen <jes.sorensen@gmail.com> - 2017-03-24 22:40 +0100
  [PATCH 2/5] inotify: Use mutex to prevent threaded clients reading events out of order Jes Sorensen <jes.sorensen@gmail.com> - 2017-03-24 22:40 +0100
  [PATCH 4/5] inotify: switch get_one_event() to use fsnotify_list_*() helpers Jes Sorensen <jes.sorensen@gmail.com> - 2017-03-24 22:40 +0100
  [PATCH 3/5] notify: Split up some fsnotify functions Jes Sorensen <jes.sorensen@gmail.com> - 2017-03-24 22:40 +0100

csiph-web