mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
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 5/5] inotify: Avoid taking spinlock for each event processed in read()
Date: Fri, 24 Mar 2017 17:36:38 -0400	[thread overview]
Message-ID: <20170324213638.9114-6-jsorensen@fb.com> (raw)
In-Reply-To: <20170324213638.9114-1-jsorensen@fb.com>

Splice off the notification list while processing read events, which allows
it to be processed without taking and releasing the spinlock for each
event.

Signed-off-by: Jes Sorensen <jsorensen@fb.com>
Reviewed-by: Josef Bacik <jbacik@fb.com>
---
 fs/notify/inotify/inotify_user.c | 28 ++++++++++++++++++++++++----
 1 file changed, 24 insertions(+), 4 deletions(-)

diff --git a/fs/notify/inotify/inotify_user.c b/fs/notify/inotify/inotify_user.c
index 4a93750..45b1f69 100644
--- a/fs/notify/inotify/inotify_user.c
+++ b/fs/notify/inotify/inotify_user.c
@@ -224,22 +224,27 @@ static ssize_t inotify_read(struct file *file, char __user *buf,
 	struct fsnotify_group *group;
 	struct fsnotify_event *kevent;
 	char __user *start;
-	int ret;
+	int ret, consumed = 0;
 	DEFINE_WAIT_FUNC(wait, woken_wake_function);
+	LIST_HEAD(tmp_list);
 
 	start = buf;
 	group = file->private_data;
 
 	mutex_lock(&group->inotify_data.consumer_mutex);
 	add_wait_queue(&group->notification_waitq, &wait);
+
+	spin_lock(&group->notification_lock);
+	list_splice_init(&group->notification_list, &tmp_list);
+	spin_unlock(&group->notification_lock);
+
 	while (1) {
-		spin_lock(&group->notification_lock);
-		kevent = get_one_event(&group->notification_list, count);
-		spin_unlock(&group->notification_lock);
+		kevent = get_one_event(&tmp_list, count);
 
 		pr_debug("%s: group=%p kevent=%p\n", __func__, group, kevent);
 
 		if (kevent) {
+			consumed++;
 			ret = PTR_ERR(kevent);
 			if (IS_ERR(kevent))
 				break;
@@ -262,8 +267,23 @@ static ssize_t inotify_read(struct file *file, char __user *buf,
 		if (start != buf)
 			break;
 
+		spin_lock(&group->notification_lock);
+		list_splice_init(&tmp_list, &group->notification_list);
+		group->q_len -= consumed;
+		consumed = 0;
+		spin_unlock(&group->notification_lock);
+
 		wait_woken(&wait, TASK_INTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
+
+		spin_lock(&group->notification_lock);
+		list_splice_init(&group->notification_list, &tmp_list);
+		spin_unlock(&group->notification_lock);
 	}
+	spin_lock(&group->notification_lock);
+	list_splice(&tmp_list, &group->notification_list);
+	group->q_len -= consumed;
+	spin_unlock(&group->notification_lock);
+
 	remove_wait_queue(&group->notification_waitq, &wait);
 	mutex_unlock(&group->inotify_data.consumer_mutex);
 
-- 
2.9.3

      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 ` [PATCH 2/5] inotify: Use mutex to prevent threaded clients reading events out of order Jes Sorensen
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 ` Jes Sorensen [this message]

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-6-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®