Re: [PATCH] pipe_read: don't wake up the writer if the pipe is still full
From: Hillf Danton
Date: Tue Mar 04 2025 - 18:35:38 EST
On Tue, 4 Mar 2025 13:34:57 +0100 Oleg Nesterov <oleg@xxxxxxxxxx>
> On 03/04, Hillf Danton wrote:
> > On Tue, 4 Mar 2025 11:05:57 +0530 K Prateek Nayak <kprateek.nayak@xxxxxxx>
> > >> @@ -573,11 +573,13 @@ pipe_write(struct kiocb *iocb, struct io
> > >> * after waiting we need to re-check whether the pipe
> > >> * become empty while we dropped the lock.
> > >> */
> > >> + tail = pipe->tail;
> > >> mutex_unlock(&pipe->mutex);
> > >> if (was_empty)
> > >> wake_up_interruptible_sync_poll(&pipe->rd_wait, EPOLLIN | EPOLLRDNORM);
> > >> kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
> > >> - wait_event_interruptible_exclusive(pipe->wr_wait, pipe_writable(pipe));
> > >> + wait_event_interruptible_exclusive(pipe->wr_wait,
> > >> + !READ_ONCE(pipe->readers) || tail != READ_ONCE(pipe->tail));
> > >
> > >That could work too for the case highlighted but in case the head too
> > >has moved by the time the writer wakes up, it'll lead to an extra
> > >wakeup.
> > >
> > Note wakeup can occur even if pipe is full,
>
> Perhaps I misunderstood you, but I don't think pipe_read() can ever do
> wake_up(pipe->wr_wait) if pipe is full...
>
> > * So we still need to wake up any pending writers in the
> > * _very_ unlikely case that the pipe was full, but we got
> > * no data.
> > */
>
> Only if wake_writer is true,
>
> if (unlikely(wake_writer))
> wake_up_interruptible_sync_poll(...);
>
> and in this case the pipe is no longer full. A zero-sized buffer was
> removed.
>
> Of course this pipe can be full again when the woken writer checks the
> condition, but this is another story. And in this case, with your
> proposed change, the woken writer will take pipe->mutex for no reason.
>
See the following sequence,
1) waker makes full false
2) waker makes full true
3) waiter checks full
4) waker makes full false
waiter has no real idea of full without lock held, perhaps regardless
the code cut below.
> Note also that the comment and code above was already removed by
> https://lore.kernel.org/all/20250210114039.GA3588@xxxxxxxxxx/
>
> Oleg.