From: Jes Sorensen <jes.sorensen@gmail.com>
To: linux-kernel@vger.kernel.org
Cc: john@johnmccutchan.com, rlove@rlove.org, eparis@parisplace.org,
josef@toxicpanda.com, kernel-team@fb.com,
Jes Sorensen <jsorensen@fb.com>
Subject: [PATCH 2/5] inotify: Use mutex to prevent threaded clients reading events out of order
Date: Fri, 24 Mar 2017 17:36:35 -0400 [thread overview]
Message-ID: <20170324213638.9114-3-jsorensen@fb.com> (raw)
In-Reply-To: <20170324213638.9114-1-jsorensen@fb.com>
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
next prev parent reply other threads:[~2017-03-24 21:37 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2017-03-24 21:36 [PATCH 0/5] inofify: Reduce lock contention on read() Jes Sorensen
2017-03-24 21:36 ` [PATCH 1/5] notify: Call mutex_destroy() before freeing mutex memory Jes Sorensen
2017-03-24 21:36 ` Jes Sorensen [this message]
2017-03-24 21:36 ` [PATCH 3/5] notify: Split up some fsnotify functions Jes Sorensen
2017-03-24 21:36 ` [PATCH 4/5] inotify: switch get_one_event() to use fsnotify_list_*() helpers Jes Sorensen
2017-03-24 21:36 ` [PATCH 5/5] inotify: Avoid taking spinlock for each event processed in read() Jes Sorensen
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20170324213638.9114-3-jsorensen@fb.com \
--to=jes.sorensen@gmail.com \
--cc=eparis@parisplace.org \
--cc=john@johnmccutchan.com \
--cc=josef@toxicpanda.com \
--cc=jsorensen@fb.com \
--cc=kernel-team@fb.com \
--cc=linux-kernel@vger.kernel.org \
--cc=rlove@rlove.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®