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 04/27] untangling ep_call_nested(): it's all serialized on epmutex.
Date: Sun,  4 Oct 2020 03:39:06 +0100	[thread overview]
Message-ID: <20201004023929.2740074-4-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>

IOW,
	* no locking is needed to protect the list
	* the list is actually a stack
	* no need to check ->ctx
	* it can bloody well be a static 5-element array - nobody is
going to be accessing it in parallel.

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

diff --git a/fs/eventpoll.c b/fs/eventpoll.c
index ef73d71a5dc8..43aecae0935c 100644
--- a/fs/eventpoll.c
+++ b/fs/eventpoll.c
@@ -109,25 +109,6 @@ struct epoll_filefd {
 	int fd;
 } __packed;
 
-/*
- * Structure used to track possible nested calls, for too deep recursions
- * and loop cycles.
- */
-struct nested_call_node {
-	struct list_head llink;
-	void *cookie;
-	void *ctx;
-};
-
-/*
- * This structure is used as collector for nested calls, to check for
- * maximum recursion dept and loop cycles.
- */
-struct nested_calls {
-	struct list_head tasks_call_list;
-	spinlock_t lock;
-};
-
 /* Wait structure used by the poll hooks */
 struct eppoll_entry {
 	/* List header used to link this structure to the "struct epitem" */
@@ -273,7 +254,8 @@ static DEFINE_MUTEX(epmutex);
 static u64 loop_check_gen = 0;
 
 /* Used to check for epoll file descriptor inclusion loops */
-static struct nested_calls poll_loop_ncalls;
+static void *cookies[EP_MAX_NESTS + 1];
+static int nesting;
 
 /* Slab cache used to allocate "struct epitem" */
 static struct kmem_cache *epi_cache __read_mostly;
@@ -348,13 +330,6 @@ static inline struct epitem *ep_item_from_wait(wait_queue_entry_t *p)
 	return container_of(p, struct eppoll_entry, wait)->base;
 }
 
-/* Initialize the poll safe wake up structure */
-static void ep_nested_calls_init(struct nested_calls *ncalls)
-{
-	INIT_LIST_HEAD(&ncalls->tasks_call_list);
-	spin_lock_init(&ncalls->lock);
-}
-
 /**
  * ep_events_available - Checks if ready events might be available.
  *
@@ -465,47 +440,20 @@ static inline void ep_set_busy_poll_napi_id(struct epitem *epi)
 static int ep_call_nested(int (*nproc)(void *, void *, int), void *priv,
 			  void *cookie)
 {
-	int error, call_nests = 0;
-	unsigned long flags;
-	struct nested_calls *ncalls = &poll_loop_ncalls;
-	struct list_head *lsthead = &ncalls->tasks_call_list;
-	struct nested_call_node *tncur;
-	struct nested_call_node tnode;
+	int error, i;
 
-	spin_lock_irqsave(&ncalls->lock, flags);
+	if (nesting > EP_MAX_NESTS) /* too deep nesting */
+		return -1;
 
-	/*
-	 * Try to see if the current task is already inside this wakeup call.
-	 * We use a list here, since the population inside this set is always
-	 * very much limited.
-	 */
-	list_for_each_entry(tncur, lsthead, llink) {
-		if (tncur->ctx == current &&
-		    (tncur->cookie == cookie || ++call_nests > EP_MAX_NESTS)) {
-			/*
-			 * Ops ... loop detected or maximum nest level reached.
-			 * We abort this wake by breaking the cycle itself.
-			 */
-			error = -1;
-			goto out_unlock;
-		}
+	for (i = 0; i < nesting; i++) {
+		if (cookies[i] == cookie) /* loop detected */
+			return -1;
 	}
-
-	/* Add the current task and cookie to the list */
-	tnode.ctx = current;
-	tnode.cookie = cookie;
-	list_add(&tnode.llink, lsthead);
-
-	spin_unlock_irqrestore(&ncalls->lock, flags);
+	cookies[nesting++] = cookie;
 
 	/* Call the nested function */
-	error = (*nproc)(priv, cookie, call_nests);
-
-	/* Remove the current task from the list */
-	spin_lock_irqsave(&ncalls->lock, flags);
-	list_del(&tnode.llink);
-out_unlock:
-	spin_unlock_irqrestore(&ncalls->lock, flags);
+	error = (*nproc)(priv, cookie, nesting - 1);
+	nesting--;
 
 	return error;
 }
@@ -2380,12 +2328,6 @@ static int __init eventpoll_init(void)
 	BUG_ON(max_user_watches < 0);
 
 	/*
-	 * Initialize the structure used to perform epoll file descriptor
-	 * inclusion loops checks.
-	 */
-	ep_nested_calls_init(&poll_loop_ncalls);
-
-	/*
 	 * We can have many thousands of epitems, so prevent this from
 	 * using an extra cache line on 64-bit (and smaller) CPUs
 	 */
-- 
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   ` Al Viro [this message]
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   ` [RFC PATCH 18/27] lift locking/unlocking ep->mtx out of ep_{start,done}_scan() Al Viro
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-4-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.