All of lore.kernel.org
 help / color / mirror / Atom feed
From: Al Viro <viro@ZenIV.linux.org.uk>
To: Linus Torvalds <torvalds@linux-foundation.org>
Cc: linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org,
	Marc Zyngier <maz@kernel.org>
Subject: [RFC PATCH 18/27] lift locking/unlocking ep->mtx out of ep_{start,done}_scan()
Date: Sun,  4 Oct 2020 03:39:20 +0100	[thread overview]
Message-ID: <20201004023929.2740074-18-viro@ZenIV.linux.org.uk> (raw)
In-Reply-To: <20201004023929.2740074-1-viro@ZenIV.linux.org.uk>

From: Al Viro <viro@zeniv.linux.org.uk>

get rid of depth/ep_locked arguments there and document
the kludge in ep_item_poll() that has lead to ep_locked existence in
the first place

Signed-off-by: Al Viro <viro@zeniv.linux.org.uk>
---
 fs/eventpoll.c | 57 ++++++++++++++++++++++++++-------------------------------
 1 file changed, 26 insertions(+), 31 deletions(-)

diff --git a/fs/eventpoll.c b/fs/eventpoll.c
index ac996b959e94..f9c567af1f5f 100644
--- a/fs/eventpoll.c
+++ b/fs/eventpoll.c
@@ -554,20 +554,13 @@ static inline void ep_pm_stay_awake_rcu(struct epitem *epi)
 	rcu_read_unlock();
 }
 
-static void ep_start_scan(struct eventpoll *ep,
-			  int depth, bool ep_locked,
-			  struct list_head *txlist)
-{
-	lockdep_assert_irqs_enabled();
-
-	/*
-	 * We need to lock this because we could be hit by
-	 * eventpoll_release_file() and epoll_ctl().
-	 */
-
-	if (!ep_locked)
-		mutex_lock_nested(&ep->mtx, depth);
 
+/*
+ * ep->mutex needs to be held because we could be hit by
+ * eventpoll_release_file() and epoll_ctl().
+ */
+static void ep_start_scan(struct eventpoll *ep, struct list_head *txlist)
+{
 	/*
 	 * Steal the ready list, and re-init the original one to the
 	 * empty list. Also, set ep->ovflist to NULL so that events
@@ -576,6 +569,7 @@ static void ep_start_scan(struct eventpoll *ep,
 	 * because we want the "sproc" callback to be able to do it
 	 * in a lockless way.
 	 */
+	lockdep_assert_irqs_enabled();
 	write_lock_irq(&ep->lock);
 	list_splice_init(&ep->rdllist, txlist);
 	WRITE_ONCE(ep->ovflist, NULL);
@@ -583,7 +577,6 @@ static void ep_start_scan(struct eventpoll *ep,
 }
 
 static void ep_done_scan(struct eventpoll *ep,
-			 int depth, bool ep_locked,
 			 struct list_head *txlist)
 {
 	struct epitem *epi, *nepi;
@@ -624,9 +617,6 @@ static void ep_done_scan(struct eventpoll *ep,
 	list_splice(txlist, &ep->rdllist);
 	__pm_relax(ep->ws);
 	write_unlock_irq(&ep->lock);
-
-	if (!ep_locked)
-		mutex_unlock(&ep->mtx);
 }
 
 static void epi_rcu_free(struct rcu_head *head)
@@ -763,11 +753,16 @@ static __poll_t ep_item_poll(const struct epitem *epi, poll_table *pt,
 
 	ep = epi->ffd.file->private_data;
 	poll_wait(epi->ffd.file, &ep->poll_wait, pt);
-	locked = pt && (pt->_qproc == ep_ptable_queue_proc);
 
-	ep_start_scan(ep, depth, locked, &txlist);
+	// kludge: ep_insert() calls us with ep->mtx already locked
+	locked = pt && (pt->_qproc == ep_ptable_queue_proc);
+	if (!locked)
+		mutex_lock_nested(&ep->mtx, depth);
+	ep_start_scan(ep, &txlist);
 	res = ep_read_events_proc(ep, &txlist, depth + 1);
-	ep_done_scan(ep, depth, locked, &txlist);
+	ep_done_scan(ep, &txlist);
+	if (!locked)
+		mutex_unlock(&ep->mtx);
 	return res & epi->event.events;
 }
 
@@ -809,9 +804,11 @@ static __poll_t ep_eventpoll_poll(struct file *file, poll_table *wait)
 	 * Proceed to find out if wanted events are really available inside
 	 * the ready list.
 	 */
-	ep_start_scan(ep, 0, false, &txlist);
+	mutex_lock(&ep->mtx);
+	ep_start_scan(ep, &txlist);
 	res = ep_read_events_proc(ep, &txlist, 1);
-	ep_done_scan(ep, 0, false, &txlist);
+	ep_done_scan(ep, &txlist);
+	mutex_unlock(&ep->mtx);
 	return res;
 }
 
@@ -1573,15 +1570,13 @@ static int ep_send_events(struct eventpoll *ep,
 
 	init_poll_funcptr(&pt, NULL);
 
-	ep_start_scan(ep, 0, false, &txlist);
+	mutex_lock(&ep->mtx);
+	ep_start_scan(ep, &txlist);
 
 	/*
 	 * We can loop without lock because we are passed a task private list.
-	 * Items cannot vanish during the loop because ep_scan_ready_list() is
-	 * holding "mtx" during this call.
+	 * Items cannot vanish during the loop we are holding ep->mtx.
 	 */
-	lockdep_assert_held(&ep->mtx);
-
 	list_for_each_entry_safe(epi, tmp, &txlist, rdllink) {
 		struct wakeup_source *ws;
 		__poll_t revents;
@@ -1609,9 +1604,8 @@ static int ep_send_events(struct eventpoll *ep,
 
 		/*
 		 * If the event mask intersect the caller-requested one,
-		 * deliver the event to userspace. Again, ep_scan_ready_list()
-		 * is holding ep->mtx, so no operations coming from userspace
-		 * can change the item.
+		 * deliver the event to userspace. Again, we are holding ep->mtx,
+		 * so no operations coming from userspace can change the item.
 		 */
 		revents = ep_item_poll(epi, &pt, 1);
 		if (!revents)
@@ -1645,7 +1639,8 @@ static int ep_send_events(struct eventpoll *ep,
 			ep_pm_stay_awake(epi);
 		}
 	}
-	ep_done_scan(ep, 0, false, &txlist);
+	ep_done_scan(ep, &txlist);
+	mutex_unlock(&ep->mtx);
 
 	return res;
 }
-- 
2.11.0


  parent reply	other threads:[~2020-10-04  2:39 UTC|newest]

Thread overview: 38+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-10-04  2:36 [RFC][PATCHSET] epoll cleanups Al Viro
2020-10-04  2:39 ` [RFC PATCH 01/27] epoll: switch epitem->pwqlist to single-linked list Al Viro
2020-10-04  2:39   ` [RFC PATCH 02/27] epoll: get rid of epitem->nwait Al Viro
2020-10-04  2:39   ` [RFC PATCH 03/27] untangling ep_call_nested(): get rid of useless arguments Al Viro
2020-10-04  2:39   ` [RFC PATCH 04/27] untangling ep_call_nested(): it's all serialized on epmutex Al Viro
2020-10-04  2:39   ` [RFC PATCH 05/27] untangling ep_call_nested(): take pushing cookie into a helper Al Viro
2020-10-04  2:39   ` [RFC PATCH 06/27] untangling ep_call_nested(): move push/pop of cookie into the callbacks Al Viro
2020-10-04  2:39   ` [RFC PATCH 07/27] untangling ep_call_nested(): and there was much rejoicing Al Viro
2020-10-04  2:39   ` [RFC PATCH 08/27] reverse_path_check_proc(): sane arguments Al Viro
2020-10-04  2:39   ` [RFC PATCH 09/27] reverse_path_check_proc(): don't bother with cookies Al Viro
2020-10-04  2:39   ` [RFC PATCH 10/27] clean reverse_path_check_proc() a bit Al Viro
2020-10-04  2:39   ` [RFC PATCH 11/27] ep_loop_check_proc(): lift pushing the cookie into callers Al Viro
2020-10-04  2:39   ` [RFC PATCH 12/27] get rid of ep_push_nested() Al Viro
2020-10-04  2:39   ` [RFC PATCH 13/27] ep_loop_check_proc(): saner calling conventions Al Viro
2020-10-04  2:39   ` [RFC PATCH 14/27] ep_scan_ready_list(): prepare to splitup Al Viro
2020-10-04  2:39   ` [RFC PATCH 15/27] lift the calls of ep_read_events_proc() into the callers Al Viro
2020-10-04  2:39   ` [RFC PATCH 16/27] lift the calls of ep_send_events_proc() " Al Viro
2020-10-04  2:39   ` [RFC PATCH 17/27] ep_send_events_proc(): fold into the caller Al Viro
2020-10-04  2:39   ` Al Viro [this message]
2020-10-04  2:39   ` [RFC PATCH 19/27] ep_insert(): don't open-code ep_remove() on failure exits Al Viro
2020-10-04  2:39   ` [RFC PATCH 20/27] ep_insert(): we only need tep->mtx around the insertion itself Al Viro
2020-10-04 12:56     ` [ep_insert()] 9ee1cc5666: WARNING:possible_recursive_locking_detected kernel test robot
2020-10-04 14:17       ` Al Viro
2020-10-04 14:27         ` Al Viro
2020-10-04  2:39   ` [RFC PATCH 21/27] take the common part of ep_eventpoll_poll() and ep_item_poll() into helper Al Viro
2020-10-04  2:39   ` [RFC PATCH 22/27] fold ep_read_events_proc() into the only caller Al Viro
2020-10-04  2:39   ` [RFC PATCH 23/27] ep_insert(): move creation of wakeup source past the fl_ep_links insertion Al Viro
2020-10-04  2:39   ` [RFC PATCH 24/27] convert ->f_ep_links/->fllink to hlist Al Viro
2020-10-04  2:39   ` [RFC PATCH 25/27] lift rcu_read_lock() into reverse_path_check() Al Viro
2020-10-04  2:39   ` [RFC PATCH 26/27] epoll: massage the check list insertion Al Viro
2020-10-04  2:39   ` [RFC PATCH 27/27] epoll: take epitem list out of struct file Al Viro
2020-10-05 20:37     ` Qian Cai
2020-10-05 20:49       ` Al Viro
2020-10-04  2:49 ` [RFC][PATCHSET] epoll cleanups Al Viro
2020-10-04 12:13 ` Matthew Wilcox
2020-10-04 14:15   ` Al Viro
2020-10-04 18:08 ` Linus Torvalds
2020-10-04 20:05   ` Al Viro

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=20201004023929.2740074-18-viro@ZenIV.linux.org.uk \
    --to=viro@zeniv.linux.org.uk \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=maz@kernel.org \
    --cc=torvalds@linux-foundation.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.