diff mbox series

net: Do not break out of sk_stream_wait_memory() with TIF_NOTIFY_SIGNAL

Message ID 20231023121346.4098160-1-s.hauer@pengutronix.de (mailing list archive)
State Changes Requested
Delegated to: Netdev Maintainers
Headers show
Series net: Do not break out of sk_stream_wait_memory() with TIF_NOTIFY_SIGNAL | expand

Checks

Context Check Description
netdev/series_format warning Single patches do not need cover letters; Target tree name not specified in the subject
netdev/tree_selection success Guessed tree name to be net-next
netdev/fixes_present success Fixes tag not required for -next series
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit success Errors and warnings before: 1365 this patch: 1365
netdev/cc_maintainers fail 2 blamed authors not CCed: oleg@redhat.com tglx@linutronix.de; 3 maintainers not CCed: tglx@linutronix.de oleg@redhat.com edumazet@google.com
netdev/build_clang success Errors and warnings before: 1389 this patch: 1389
netdev/verify_signedoff success Signed-off-by tag matches author and committer
netdev/deprecated_api success None detected
netdev/check_selftest success No net selftest shell script
netdev/verify_fixes success Fixes tag looks correct
netdev/build_allmodconfig_warn success Errors and warnings before: 1390 this patch: 1390
netdev/checkpatch warning WARNING: Please use correct Fixes: style 'Fixes: <12 chars of sha1> ("<title line>")' - ie: 'Fixes: 12db8b690010 ("entry: Add support for TIF_NOTIFY_SIGNAL")'
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/source_inline success Was 0 now: 0

Commit Message

Sascha Hauer Oct. 23, 2023, 12:13 p.m. UTC
It can happen that a socket sends the remaining data at close() time.
With io_uring and KTLS it can happen that sk_stream_wait_memory() bails
out with -512 (-ERESTARTSYS) because TIF_NOTIFY_SIGNAL is set for the
current task. This flag has been set in io_req_normal_work_add() by
calling task_work_add().

It seems signal_pending() is too broad, so this patch replaces it with
task_sigpending(), thus ignoring the TIF_NOTIFY_SIGNAL flag.

A discussion of this issue can be found at
https://lore.kernel.org/20231010141932.GD3114228@pengutronix.de

Suggested-by: Jens Axboe <axboe@kernel.dk>
Fixes: 12db8b690010c ("entry: Add support for TIF_NOTIFY_SIGNAL")
Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
---
 net/core/stream.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

Comments

Paolo Abeni Oct. 24, 2023, 1:56 p.m. UTC | #1
On Mon, 2023-10-23 at 14:13 +0200, Sascha Hauer wrote:
> It can happen that a socket sends the remaining data at close() time.
> With io_uring and KTLS it can happen that sk_stream_wait_memory() bails
> out with -512 (-ERESTARTSYS) because TIF_NOTIFY_SIGNAL is set for the
> current task. This flag has been set in io_req_normal_work_add() by
> calling task_work_add().
> 
> It seems signal_pending() is too broad, so this patch replaces it with
> task_sigpending(), thus ignoring the TIF_NOTIFY_SIGNAL flag.

This looks dangerous, at best. Other possible legit users setting
TIF_NOTIFY_SIGNAL will be broken.

Can't you instead clear TIF_NOTIFY_SIGNAL in io_run_task_work() ?


Thanks!

Paolo
Sascha Hauer Oct. 26, 2023, 7:03 a.m. UTC | #2
On Tue, Oct 24, 2023 at 03:56:17PM +0200, Paolo Abeni wrote:
> On Mon, 2023-10-23 at 14:13 +0200, Sascha Hauer wrote:
> > It can happen that a socket sends the remaining data at close() time.
> > With io_uring and KTLS it can happen that sk_stream_wait_memory() bails
> > out with -512 (-ERESTARTSYS) because TIF_NOTIFY_SIGNAL is set for the
> > current task. This flag has been set in io_req_normal_work_add() by
> > calling task_work_add().
> > 
> > It seems signal_pending() is too broad, so this patch replaces it with
> > task_sigpending(), thus ignoring the TIF_NOTIFY_SIGNAL flag.
> 
> This looks dangerous, at best. Other possible legit users setting
> TIF_NOTIFY_SIGNAL will be broken.
> 
> Can't you instead clear TIF_NOTIFY_SIGNAL in io_run_task_work() ?

I don't have an idea how io_run_task_work() comes into play here, but it
seems it already clears TIF_NOTIFY_SIGNAL:

static inline int io_run_task_work(void)
{
        /*
         * Always check-and-clear the task_work notification signal. With how
         * signaling works for task_work, we can find it set with nothing to
         * run. We need to clear it for that case, like get_signal() does.
         */
        if (test_thread_flag(TIF_NOTIFY_SIGNAL))
                clear_notify_signal();
	...
}

Sascha
Paolo Abeni Oct. 26, 2023, 8:49 a.m. UTC | #3
On Thu, 2023-10-26 at 09:03 +0200, Sascha Hauer wrote:
> On Tue, Oct 24, 2023 at 03:56:17PM +0200, Paolo Abeni wrote:
> > On Mon, 2023-10-23 at 14:13 +0200, Sascha Hauer wrote:
> > > It can happen that a socket sends the remaining data at close() time.
> > > With io_uring and KTLS it can happen that sk_stream_wait_memory() bails
> > > out with -512 (-ERESTARTSYS) because TIF_NOTIFY_SIGNAL is set for the
> > > current task. This flag has been set in io_req_normal_work_add() by
> > > calling task_work_add().
> > > 
> > > It seems signal_pending() is too broad, so this patch replaces it with
> > > task_sigpending(), thus ignoring the TIF_NOTIFY_SIGNAL flag.
> > 
> > This looks dangerous, at best. Other possible legit users setting
> > TIF_NOTIFY_SIGNAL will be broken.
> > 
> > Can't you instead clear TIF_NOTIFY_SIGNAL in io_run_task_work() ?
> 
> I don't have an idea how io_run_task_work() comes into play here, but it
> seems it already clears TIF_NOTIFY_SIGNAL:
> 
> static inline int io_run_task_work(void)
> {
>         /*
>          * Always check-and-clear the task_work notification signal. With how
>          * signaling works for task_work, we can find it set with nothing to
>          * run. We need to clear it for that case, like get_signal() does.
>          */
>         if (test_thread_flag(TIF_NOTIFY_SIGNAL))
>                 clear_notify_signal();
> 	...
> }

I see, io_run_task_work() is too late, sk_stream_wait_memory() is
already woken up.

I still think this patch is unsafe. What about explicitly handling the
restart in tls_sw_release_resources_tx() ? The main point is that such
function is called by inet_release() and the latter can't be re-
started.

Cheers,

Paolo
Sascha Hauer Oct. 27, 2023, 12:04 p.m. UTC | #4
On Thu, Oct 26, 2023 at 10:49:18AM +0200, Paolo Abeni wrote:
> On Thu, 2023-10-26 at 09:03 +0200, Sascha Hauer wrote:
> > On Tue, Oct 24, 2023 at 03:56:17PM +0200, Paolo Abeni wrote:
> > > On Mon, 2023-10-23 at 14:13 +0200, Sascha Hauer wrote:
> > > > It can happen that a socket sends the remaining data at close() time.
> > > > With io_uring and KTLS it can happen that sk_stream_wait_memory() bails
> > > > out with -512 (-ERESTARTSYS) because TIF_NOTIFY_SIGNAL is set for the
> > > > current task. This flag has been set in io_req_normal_work_add() by
> > > > calling task_work_add().
> > > > 
> > > > It seems signal_pending() is too broad, so this patch replaces it with
> > > > task_sigpending(), thus ignoring the TIF_NOTIFY_SIGNAL flag.
> > > 
> > > This looks dangerous, at best. Other possible legit users setting
> > > TIF_NOTIFY_SIGNAL will be broken.
> > > 
> > > Can't you instead clear TIF_NOTIFY_SIGNAL in io_run_task_work() ?
> > 
> > I don't have an idea how io_run_task_work() comes into play here, but it
> > seems it already clears TIF_NOTIFY_SIGNAL:
> > 
> > static inline int io_run_task_work(void)
> > {
> >         /*
> >          * Always check-and-clear the task_work notification signal. With how
> >          * signaling works for task_work, we can find it set with nothing to
> >          * run. We need to clear it for that case, like get_signal() does.
> >          */
> >         if (test_thread_flag(TIF_NOTIFY_SIGNAL))
> >                 clear_notify_signal();
> > 	...
> > }
> 
> I see, io_run_task_work() is too late, sk_stream_wait_memory() is
> already woken up.
> 
> I still think this patch is unsafe. What about explicitly handling the
> restart in tls_sw_release_resources_tx() ? The main point is that such
> function is called by inet_release() and the latter can't be re-
> started.

I don't think there's anything I can do in tls_sw_release_resources_tx().
When entering this function TIF_NOTIFY_SIGNAL is not (yet) set. It gets
set at some point while tls_sw_release_resources_tx() is running. I find
it set when tls_tx_records() returns with -ERESTARTSYS. I tried clearing
TIF_NOTIFY_SIGNAL then and called tls_tx_records() again, but that doesn't
work.

Sascha
Marc Kleine-Budde Nov. 17, 2023, 10:43 a.m. UTC | #5
On 27.10.2023 14:04:32, Sascha Hauer wrote:
> On Thu, Oct 26, 2023 at 10:49:18AM +0200, Paolo Abeni wrote:
> > On Thu, 2023-10-26 at 09:03 +0200, Sascha Hauer wrote:
> > > On Tue, Oct 24, 2023 at 03:56:17PM +0200, Paolo Abeni wrote:
> > > > On Mon, 2023-10-23 at 14:13 +0200, Sascha Hauer wrote:
> > > > > It can happen that a socket sends the remaining data at close() time.
> > > > > With io_uring and KTLS it can happen that sk_stream_wait_memory() bails
> > > > > out with -512 (-ERESTARTSYS) because TIF_NOTIFY_SIGNAL is set for the
> > > > > current task. This flag has been set in io_req_normal_work_add() by
> > > > > calling task_work_add().
> > > > > 
> > > > > It seems signal_pending() is too broad, so this patch replaces it with
> > > > > task_sigpending(), thus ignoring the TIF_NOTIFY_SIGNAL flag.
> > > > 
> > > > This looks dangerous, at best. Other possible legit users setting
> > > > TIF_NOTIFY_SIGNAL will be broken.
> > > > 
> > > > Can't you instead clear TIF_NOTIFY_SIGNAL in io_run_task_work() ?
> > > 
> > > I don't have an idea how io_run_task_work() comes into play here, but it
> > > seems it already clears TIF_NOTIFY_SIGNAL:
> > > 
> > > static inline int io_run_task_work(void)
> > > {
> > >         /*
> > >          * Always check-and-clear the task_work notification signal. With how
> > >          * signaling works for task_work, we can find it set with nothing to
> > >          * run. We need to clear it for that case, like get_signal() does.
> > >          */
> > >         if (test_thread_flag(TIF_NOTIFY_SIGNAL))
> > >                 clear_notify_signal();
> > > 	...
> > > }
> > 
> > I see, io_run_task_work() is too late, sk_stream_wait_memory() is
> > already woken up.
> > 
> > I still think this patch is unsafe. What about explicitly handling the
> > restart in tls_sw_release_resources_tx() ? The main point is that such
> > function is called by inet_release() and the latter can't be re-
> > started.
> 
> I don't think there's anything I can do in tls_sw_release_resources_tx().
> When entering this function TIF_NOTIFY_SIGNAL is not (yet) set. It gets
> set at some point while tls_sw_release_resources_tx() is running. I find
> it set when tls_tx_records() returns with -ERESTARTSYS. I tried clearing
> TIF_NOTIFY_SIGNAL then and called tls_tx_records() again, but that doesn't
> work.

Seems the discussion got stuck, what are the blocking points?

Marc
Steffen Trumtrar Dec. 19, 2023, 11 a.m. UTC | #6
On 2023-11-17 at 11:43 +01, Marc Kleine-Budde <mkl@pengutronix.de> wrote:

> [[PGP Signed Part:Undecided]]
> On 27.10.2023 14:04:32, Sascha Hauer wrote:
>> On Thu, Oct 26, 2023 at 10:49:18AM +0200, Paolo Abeni wrote:
>> > On Thu, 2023-10-26 at 09:03 +0200, Sascha Hauer wrote:
>> > > On Tue, Oct 24, 2023 at 03:56:17PM +0200, Paolo Abeni wrote:
>> > > > On Mon, 2023-10-23 at 14:13 +0200, Sascha Hauer wrote:
>> > > > > It can happen that a socket sends the remaining data at close() time.
>> > > > > With io_uring and KTLS it can happen that sk_stream_wait_memory() bails
>> > > > > out with -512 (-ERESTARTSYS) because TIF_NOTIFY_SIGNAL is set for the
>> > > > > current task. This flag has been set in io_req_normal_work_add() by
>> > > > > calling task_work_add().
>> > > > >
>> > > > > It seems signal_pending() is too broad, so this patch replaces it with
>> > > > > task_sigpending(), thus ignoring the TIF_NOTIFY_SIGNAL flag.
>> > > >
>> > > > This looks dangerous, at best. Other possible legit users setting
>> > > > TIF_NOTIFY_SIGNAL will be broken.
>> > > >
>> > > > Can't you instead clear TIF_NOTIFY_SIGNAL in io_run_task_work() ?
>> > >
>> > > I don't have an idea how io_run_task_work() comes into play here, but it
>> > > seems it already clears TIF_NOTIFY_SIGNAL:
>> > >
>> > > static inline int io_run_task_work(void)
>> > > {
>> > >         /*
>> > >          * Always check-and-clear the task_work notification signal. With how
>> > >          * signaling works for task_work, we can find it set with nothing to
>> > >          * run. We need to clear it for that case, like get_signal() does.
>> > >          */
>> > >         if (test_thread_flag(TIF_NOTIFY_SIGNAL))
>> > >                 clear_notify_signal();
>> > > 	...
>> > > }
>> >
>> > I see, io_run_task_work() is too late, sk_stream_wait_memory() is
>> > already woken up.
>> >
>> > I still think this patch is unsafe. What about explicitly handling the
>> > restart in tls_sw_release_resources_tx() ? The main point is that such
>> > function is called by inet_release() and the latter can't be re-
>> > started.
>>
>> I don't think there's anything I can do in tls_sw_release_resources_tx().
>> When entering this function TIF_NOTIFY_SIGNAL is not (yet) set. It gets
>> set at some point while tls_sw_release_resources_tx() is running. I find
>> it set when tls_tx_records() returns with -ERESTARTSYS. I tried clearing
>> TIF_NOTIFY_SIGNAL then and called tls_tx_records() again, but that doesn't
>> work.
>
> Seems the discussion got stuck, what are the blocking points?

Ping!

Any pointers on how to get this sorted out?


Best regards,
Steffen

--
Pengutronix e.K.                | Dipl.-Inform. Steffen Trumtrar |
Steuerwalder Str. 21            | https://www.pengutronix.de/    |
31137 Hildesheim, Germany       | Phone: +49-5121-206917-0       |
Amtsgericht Hildesheim, HRA 2686| Fax:   +49-5121-206917-5555    |
Paolo Abeni Dec. 19, 2023, 1:13 p.m. UTC | #7
On Tue, 2023-12-19 at 12:00 +0100, Steffen Trumtrar wrote:
> On 2023-11-17 at 11:43 +01, Marc Kleine-Budde <mkl@pengutronix.de> wrote:
> 
> > [[PGP Signed Part:Undecided]]
> > On 27.10.2023 14:04:32, Sascha Hauer wrote:
> > > On Thu, Oct 26, 2023 at 10:49:18AM +0200, Paolo Abeni wrote:
> > > > On Thu, 2023-10-26 at 09:03 +0200, Sascha Hauer wrote:
> > > > > On Tue, Oct 24, 2023 at 03:56:17PM +0200, Paolo Abeni wrote:
> > > > > > On Mon, 2023-10-23 at 14:13 +0200, Sascha Hauer wrote:
> > > > > > > It can happen that a socket sends the remaining data at close() time.
> > > > > > > With io_uring and KTLS it can happen that sk_stream_wait_memory() bails
> > > > > > > out with -512 (-ERESTARTSYS) because TIF_NOTIFY_SIGNAL is set for the
> > > > > > > current task. This flag has been set in io_req_normal_work_add() by
> > > > > > > calling task_work_add().
> > > > > > > 
> > > > > > > It seems signal_pending() is too broad, so this patch replaces it with
> > > > > > > task_sigpending(), thus ignoring the TIF_NOTIFY_SIGNAL flag.
> > > > > > 
> > > > > > This looks dangerous, at best. Other possible legit users setting
> > > > > > TIF_NOTIFY_SIGNAL will be broken.
> > > > > > 
> > > > > > Can't you instead clear TIF_NOTIFY_SIGNAL in io_run_task_work() ?
> > > > > 
> > > > > I don't have an idea how io_run_task_work() comes into play here, but it
> > > > > seems it already clears TIF_NOTIFY_SIGNAL:
> > > > > 
> > > > > static inline int io_run_task_work(void)
> > > > > {
> > > > >         /*
> > > > >          * Always check-and-clear the task_work notification signal. With how
> > > > >          * signaling works for task_work, we can find it set with nothing to
> > > > >          * run. We need to clear it for that case, like get_signal() does.
> > > > >          */
> > > > >         if (test_thread_flag(TIF_NOTIFY_SIGNAL))
> > > > >                 clear_notify_signal();
> > > > > 	...
> > > > > }
> > > > 
> > > > I see, io_run_task_work() is too late, sk_stream_wait_memory() is
> > > > already woken up.
> > > > 
> > > > I still think this patch is unsafe. What about explicitly handling the
> > > > restart in tls_sw_release_resources_tx() ? The main point is that such
> > > > function is called by inet_release() and the latter can't be re-
> > > > started.
> > > 
> > > I don't think there's anything I can do in tls_sw_release_resources_tx().
> > > When entering this function TIF_NOTIFY_SIGNAL is not (yet) set. It gets
> > > set at some point while tls_sw_release_resources_tx() is running. I find
> > > it set when tls_tx_records() returns with -ERESTARTSYS. I tried clearing
> > > TIF_NOTIFY_SIGNAL then and called tls_tx_records() again, but that doesn't
> > > work.
> > 
> > Seems the discussion got stuck, what are the blocking points?
> 
> Ping!
> 
> Any pointers on how to get this sorted out?

I raised the point if this patch could be dangerous outside of the
specific scenario and I did not get any reply to that.

To be more explicit: why this will not cause user-space driven
connect() from missing relevant events? Why this is needed only here
and not in all the many others event loops waiting for signals? Why
can't the issue be addressed in any place more closely tied to the
scenario, e.g. io_uring or ktls?

Thanks

Paolo
diff mbox series

Patch

diff --git a/net/core/stream.c b/net/core/stream.c
index 96fbcb9bbb30a..e9e17b48e0122 100644
--- a/net/core/stream.c
+++ b/net/core/stream.c
@@ -67,7 +67,7 @@  int sk_stream_wait_connect(struct sock *sk, long *timeo_p)
 			return -EPIPE;
 		if (!*timeo_p)
 			return -EAGAIN;
-		if (signal_pending(tsk))
+		if (task_sigpending(tsk))
 			return sock_intr_errno(*timeo_p);
 
 		add_wait_queue(sk_sleep(sk), &wait);
@@ -103,7 +103,7 @@  void sk_stream_wait_close(struct sock *sk, long timeout)
 		do {
 			if (sk_wait_event(sk, &timeout, !sk_stream_closing(sk), &wait))
 				break;
-		} while (!signal_pending(current) && timeout);
+		} while (!task_sigpending(current) && timeout);
 
 		remove_wait_queue(sk_sleep(sk), &wait);
 	}
@@ -134,7 +134,7 @@  int sk_stream_wait_memory(struct sock *sk, long *timeo_p)
 			goto do_error;
 		if (!*timeo_p)
 			goto do_eagain;
-		if (signal_pending(current))
+		if (task_sigpending(current))
 			goto do_interrupted;
 		sk_clear_bit(SOCKWQ_ASYNC_NOSPACE, sk);
 		if (sk_stream_memory_free(sk) && !vm_wait)