Re: [PATCH] epoll: simplify ep_poll_safewake() for CONFIG_DEBUG_LOCK_ALLOC

From: Roman Penyaev
Date: Tue Sep 24 2019 - 13:52:41 EST


On 2019-09-24 19:34, Jason Baron wrote:
On 9/23/19 3:23 PM, Roman Penyaev wrote:
On 2019-09-23 17:43, Jason Baron wrote:
On 9/4/19 4:22 PM, Jason Baron wrote:
Currently, ep_poll_safewake() in the CONFIG_DEBUG_LOCK_ALLOC case uses
ep_call_nested() in order to pass the correct subclass argument to
spin_lock_irqsave_nested(). However, ep_call_nested() adds unnecessary
checks for epoll depth and loops that are already verified when doing
EPOLL_CTL_ADD. This mirrors a conversion that was done for
!CONFIG_DEBUG_LOCK_ALLOC in: commit 37b5e5212a44 ("epoll: remove
ep_call_nested() from ep_eventpoll_poll()")

Signed-off-by: Jason Baron <jbaron@xxxxxxxxxx>
Cc: Davidlohr Bueso <dave@xxxxxxxxxxxx>
Cc: Roman Penyaev <rpenyaev@xxxxxxx>
Cc: Al Viro <viro@xxxxxxxxxxxxxxxxxx>
Cc: Eric Wong <normalperson@xxxxxxxx>
Cc: Andrew Morton <akpm@xxxxxxxxxxxxxxxxxxxx>
---
Âfs/eventpoll.c | 36 +++++++++++++-----------------------
Â1 file changed, 13 insertions(+), 23 deletions(-)

diff --git a/fs/eventpoll.c b/fs/eventpoll.c
index d7f1f50..a9b2737 100644
--- a/fs/eventpoll.c
+++ b/fs/eventpoll.c
@@ -551,28 +551,23 @@ static int ep_call_nested(struct nested_calls
*ncalls,
 */
Â#ifdef CONFIG_DEBUG_LOCK_ALLOC

-static struct nested_calls poll_safewake_ncalls;
-
-static int ep_poll_wakeup_proc(void *priv, void *cookie, int
call_nests)
-{
-ÂÂÂ unsigned long flags;
-ÂÂÂ wait_queue_head_t *wqueue = (wait_queue_head_t *)cookie;
-
-ÂÂÂ spin_lock_irqsave_nested(&wqueue->lock, flags, call_nests + 1);
-ÂÂÂ wake_up_locked_poll(wqueue, EPOLLIN);
-ÂÂÂ spin_unlock_irqrestore(&wqueue->lock, flags);
-
-ÂÂÂ return 0;
-}
+static DEFINE_PER_CPU(int, wakeup_nest);

Âstatic void ep_poll_safewake(wait_queue_head_t *wq)
Â{
-ÂÂÂ int this_cpu = get_cpu();
-
-ÂÂÂ ep_call_nested(&poll_safewake_ncalls,
-ÂÂÂÂÂÂÂÂÂÂÂÂÂÂ ep_poll_wakeup_proc, NULL, wq, (void *) (long)
this_cpu);
+ÂÂÂ unsigned long flags;
+ÂÂÂ int subclass;

-ÂÂÂ put_cpu();
+ÂÂÂ local_irq_save(flags);
+ÂÂÂ preempt_disable();
+ÂÂÂ subclass = __this_cpu_read(wakeup_nest);
+ÂÂÂ spin_lock_nested(&wq->lock, subclass + 1);
+ÂÂÂ __this_cpu_inc(wakeup_nest);
+ÂÂÂ wake_up_locked_poll(wq, POLLIN);
+ÂÂÂ __this_cpu_dec(wakeup_nest);
+ÂÂÂ spin_unlock(&wq->lock);
+ÂÂÂ local_irq_restore(flags);
+ÂÂÂ preempt_enable();
Â}

What if reduce number of lines with something as the following:

ÂÂ int this_cpu = get_cpu();
ÂÂ subclass = __this_cpu_inc_return(wakeup_nest);
ÂÂ spin_lock_irqsave_nested(&wq->lock, flags, subclass);
ÂÂ wake_up_locked_poll(wq, POLLIN);
ÂÂ spin_unlock_irqrestore(&wq->lock, flags);
ÂÂ __this_cpu_dec(wakeup_nest);
ÂÂ put_cpu();

Other than that looks good to me.

Reviewed-by: Roman Penyaev <rpenyaev@xxxxxxx>

--
Roman


Hi,

I put the local_irq_save(flags), call there first so that there wouldn't
be any nesting. For example, in your sequence, there could be an irq
after the __this_cpu_inc_return(), that could end up back here.

That is correct, but seems this is the original behavior of ep_call_nested(),
where irq can happen just after spin_unlock_irqrestore():

spin_unlock_irqrestore(&ncalls->lock, flags);

>>>> irq here <<<<<

/* Call the nested function */
error = (*nproc)(priv, cookie, call_nests);

so eventually you end up with spin_lock_irqsave_nested() call where
call_nests is not monotonically increased (not sequential) but has
a gap (depends on nesting).

So if shorter, I thought that your "local_irq_save + increment" sequence
is excessive.

--
Roman