tmpfs: don't interrupt fallocate with EINTR
Subsystems (3)
PATCH 1/1 tmpfs: don't interrupt fallocate with EINTR
Hi
I have a program that sets up a periodic timer with 10ms interval. When the program attempts to call fallocate on tmpfs, it goes into an infinite loop. fallocate takes longer than 10ms, so it gets interrupted by a signal and it returns EINTR. On EINTR, the fallocate call is restarted, going into the same loop again.
fallocate(19, FALLOC_FL_KEEP_SIZE, 0, 206057565) = -1 EINTR (Přerušené volání systému) --- SIGALRM {si_signo=SIGALRM, si_code=SI_TIMER, si_timerid=0, si_overrun=0, si_int=0, si_ptr=NULL} ---sigreturn({mask=[]}) = -1 EINTR (Přerušené volání systému)fallocate(19, FALLOC_FL_KEEP_SIZE, 0, 206057565) = -1 EINTR (Přerušené volání systému) --- SIGALRM {si_signo=SIGALRM, si_code=SI_TIMER, si_timerid=0, si_overrun=0, si_int=0, si_ptr=NULL} ---sigreturn({mask=[]}) = -1 EINTR (Přerušené volání systému)fallocate(19, FALLOC_FL_KEEP_SIZE, 0, 206057565) = -1 EINTR (Přerušené volání systému) --- SIGALRM {si_signo=SIGALRM, si_code=SI_TIMER, si_timerid=0, si_overrun=0, si_int=0, si_ptr=NULL} ---sigreturn({mask=[]}) = -1 EINTR (Přerušené volání systému)fallocate(19, FALLOC_FL_KEEP_SIZE, 0, 206057565) = -1 EINTR (Přerušené volání systému) --- SIGALRM {si_signo=SIGALRM, si_code=SI_TIMER, si_timerid=0, si_overrun=0, si_int=0, si_ptr=NULL} ---sigreturn({mask=[]}) = -1 EINTR (Přerušené volání systému)
Should there be fatal_signal_pending instead of signal_pending in the shmem_fallocate loop?
--- mm/shmem.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
mm/shmem.c +1 -1
@@ -3143,7 +3143,7 @@ static long shmem_fallocate(struct file* Good, the fallocate(2) manpage permits EINTR: we may have * been interrupted because we are using up too much memory. */ -if (signal_pending (current)) +if (fatal_signal_pending (current)) error = -EINTR ;else if (shmem_falloc.nr_unswapped > shmem_falloc.nr_falloced) error = -ENOMEM ;
Christian Brauner
maintainerQuoted 35 lines · Mikulas Patocka
Hi
I have a program that sets up a periodic timer with 10ms interval. When the program attempts to call fallocate on tmpfs, it goes into an infinite loop. fallocate takes longer than 10ms, so it gets interrupted by a signal and it returns EINTR. On EINTR, the fallocate call is restarted, going into the same loop again.
fallocate(19, FALLOC_FL_KEEP_SIZE, 0, 206057565)= -1 EINTR (Přerušené volání systému) --- SIGALRM {si_signo=SIGALRM, si_code=SI_TIMER, si_timerid=0, si_overrun=0, si_int=0, si_ptr=NULL} ---sigreturn({mask=[]})= -1 EINTR (Přerušené volání systému)fallocate(19, FALLOC_FL_KEEP_SIZE, 0, 206057565)= -1 EINTR (Přerušené volání systému) --- SIGALRM {si_signo=SIGALRM, si_code=SI_TIMER, si_timerid=0, si_overrun=0, si_int=0, si_ptr=NULL} ---sigreturn({mask=[]})= -1 EINTR (Přerušené volání systému)fallocate(19, FALLOC_FL_KEEP_SIZE, 0, 206057565)= -1 EINTR (Přerušené volání systému) --- SIGALRM {si_signo=SIGALRM, si_code=SI_TIMER, si_timerid=0, si_overrun=0, si_int=0, si_ptr=NULL} ---sigreturn({mask=[]})= -1 EINTR (Přerušené volání systému)fallocate(19, FALLOC_FL_KEEP_SIZE, 0, 206057565)= -1 EINTR (Přerušené volání systému) --- SIGALRM {si_signo=SIGALRM, si_code=SI_TIMER, si_timerid=0, si_overrun=0, si_int=0, si_ptr=NULL} ---sigreturn({mask=[]})= -1 EINTR (Přerušené volání systému)Should there be
fatal_signal_pendinginstead ofsignal_pendingin theshmem_fallocateloop?Signed-off-by Mikulas Patocka <[email protected]>--- mm/shmem.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)Index:
linux-2.6/mm/shmem.c
===================================================================
mm/shmem.c static long shmem_fallocate(struct file · line 3146
Quoted 5 lines
mm/shmem.c +1
-1@@ -3143,7 +3143,7 @@ static long shmem_fallocate(struct file* Good, the fallocate(2) manpage permits EINTR: we may have * been interrupted because we are using up too much memory. */ -if (signal_pending (current)) +if (fatal_signal_pending (current))
I think that's likely wrong and probably would cause regressions as there may be users relying on this?
Quoted 15 lines · Christian Brauner
mm/shmem.c static long shmem_fallocate(struct file · line 3146
Quoted 11 lines · Mikulas Patocka
Index:
linux-2.6/mm/shmem.c
===================================================================mm/shmem.c +1
-1@@ -3143,7 +3143,7 @@ static long shmem_fallocate(struct file* Good, the fallocate(2) manpage permits EINTR: we may have * been interrupted because we are using up too much memory. */ -if (signal_pending (current)) +if (fatal_signal_pending (current))I think that's likely wrong and probably would cause regressions as there may be users relying on this?
ext4 fallocate doesn't return -EINTR. So, userspace code can't rely on it.
Mikulas
Christian Brauner
maintainerQuoted 20 lines · Mikulas Patocka
Quoted 15 lines · Christian Brauner
mm/shmem.c static long shmem_fallocate(struct file · line 3146
Quoted 11 lines · Mikulas Patocka
Index:
linux-2.6/mm/shmem.c
===================================================================mm/shmem.c +1
-1@@ -3143,7 +3143,7 @@ static long shmem_fallocate(struct file* Good, the fallocate(2) manpage permits EINTR: we may have * been interrupted because we are using up too much memory. */ -if (signal_pending (current)) +if (fatal_signal_pending (current))I think that's likely wrong and probably would cause regressions as there may be users relying on this?
Quoted 1 line
ext4 fallocate doesn't return
-EINTR. So, userspace code can't rely on it.
I'm confused what does this have to do with ext4 since this is about tmpfs. Also note, that fallocate(2) documents EINTR as a valid return value. And fwiw, the manpage also states that "EINTR A signal was caught during execution; see signal(7)." not a "fatal signal".
Aside from that. If a user sends SIGUSR1 then with the code as it is now that fallocate call will be interrupted. With your change that SIGUSR1 won't do anything anymore. Instead userspace would need to send SIGKILL. So userspace that uses SIGUSR1 will suddenly hang.
Quoted 21 lines · Christian Brauner
Quoted 20 lines · Mikulas Patocka
Quoted 15 lines · Christian Brauner
mm/shmem.c static long shmem_fallocate(struct file · line 3146
Quoted 11 lines · Mikulas Patocka
Index:
linux-2.6/mm/shmem.c
===================================================================mm/shmem.c +1
-1@@ -3143,7 +3143,7 @@ static long shmem_fallocate(struct file* Good, the fallocate(2) manpage permits EINTR: we may have * been interrupted because we are using up too much memory. */ -if (signal_pending (current)) +if (fatal_signal_pending (current))I think that's likely wrong and probably would cause regressions as there may be users relying on this?
Quoted 4 lines
Quoted 1 line
ext4 fallocate doesn't return
-EINTR. So, userspace code can't rely on it.I'm confused what does this have to do with ext4 since this is about tmpfs.
You said that applications may rely on -EINTR and I said they don't because ext4 doesn't return -EINTR.
Quoted 3 lines
Also note, that fallocate(2) documents EINTR as a valid return value. And fwiw, the manpage also states that "EINTR A signal was caught during execution; see signal(7)." not a "fatal signal".
Yes, but how should the userspace use the fallocate call reliably? Block all the signals around the call to fallocate? What to do if I use some library that calls fallocate and retries on EINTR?
Quoted 4 lines
Aside from that. If a user sends SIGUSR1 then with the code as it is now that fallocate call will be interrupted. With your change that SIGUSR1 won't do anything anymore. Instead userspace would need to send SIGKILL. So userspace that uses SIGUSR1 will suddenly hang.
It will survive one SIGUSR, but it hangs if the signal is being sent at a periodic interval.
A quick search shows that people are already adding loops when fallocate returns EINTR. All these loops will livelock when a signal is repeatedly being delivered:
https://forge.chapril.org/hardcoresushi/libgocryptfs/commit/8518d6d7bde33fdc7ef5bcb7c3c7709404392ad8?style=unified&whitespace=
https://postgrespro.com/media/maillist-attaches/pgsql-hackers/2022/07/1/[email protected]/v2-0001-retry-ftruncate.patch
https://lists.nongnu.org/archive/html/qemu-devel/2015-02/msg01116.html
Here, Postgres developers hit the same problem with retrying (they have 5ms timer):
https://www.postgresql.org/message-id/CA%2BhUKGKS2Radu-1Ewhe1-LEj19C-3XAQ7wnkQMb4e9E9q9ZXSg%40mail.gmail.com
Mikulas
Christian Brauner
maintainerQuoted 56 lines · Mikulas Patocka
Quoted 21 lines · Christian Brauner
Quoted 20 lines · Mikulas Patocka
Quoted 15 lines · Christian Brauner
mm/shmem.c static long shmem_fallocate(struct file · line 3146
Quoted 11 lines · Mikulas Patocka
Index:
linux-2.6/mm/shmem.c
===================================================================mm/shmem.c +1
-1@@ -3143,7 +3143,7 @@ static long shmem_fallocate(struct file* Good, the fallocate(2) manpage permits EINTR: we may have * been interrupted because we are using up too much memory. */ -if (signal_pending (current)) +if (fatal_signal_pending (current))I think that's likely wrong and probably would cause regressions as there may be users relying on this?
Quoted 4 lines
Quoted 1 line
ext4 fallocate doesn't return
-EINTR. So, userspace code can't rely on it.I'm confused what does this have to do with ext4 since this is about tmpfs.
You said that applications may rely on
-EINTRand I said they don't because ext4 doesn't return-EINTR.Quoted 3 lines
Also note, that fallocate(2) documents EINTR as a valid return value. And fwiw, the manpage also states that "EINTR A signal was caught during execution; see signal(7)." not a "fatal signal".
Yes, but how should the userspace use the fallocate call reliably? Block all the signals around the call to fallocate? What to do if I use some library that calls fallocate and retries on EINTR?
Quoted 4 lines
Aside from that. If a user sends SIGUSR1 then with the code as it is now that fallocate call will be interrupted. With your change that SIGUSR1 won't do anything anymore. Instead userspace would need to send SIGKILL. So userspace that uses SIGUSR1 will suddenly hang.
It will survive one SIGUSR, but it hangs if the signal is being sent at a periodic interval.
A quick search shows that people are already adding loops when fallocate returns EINTR. All these loops will livelock when a signal is repeatedly being delivered:
https://forge.chapril.org/hardcoresushi/libgocryptfs/commit/8518d6d7bde33fdc7ef5bcb7c3c7709404392ad8?style=unified&whitespace=
https://postgrespro.com/media/maillist-attaches/pgsql-hackers/2022/07/1/[email protected]/v2-0001-retry-ftruncate.patch
https://lists.nongnu.org/archive/html/qemu-devel/2015-02/msg01116.html
Quoted 3 lines
Here, Postgres developers hit the same problem with retrying (they have 5ms timer):
https://www.postgresql.org/message-id/CA%2BhUKGKS2Radu-1Ewhe1-LEj19C-3XAQ7wnkQMb4e9E9q9ZXSg%40mail.gmail.com
All fair points.
Jan Kara
reviewerHello,
Quoted 29 lines · Christian Brauner
Quoted 28 lines · Mikulas Patocka
I have a program that sets up a periodic timer with 10ms interval. When the program attempts to call fallocate on tmpfs, it goes into an infinite loop. fallocate takes longer than 10ms, so it gets interrupted by a signal and it returns EINTR. On EINTR, the fallocate call is restarted, going into the same loop again.
fallocate(19, FALLOC_FL_KEEP_SIZE, 0, 206057565)= -1 EINTR (Přerušené volání systému) --- SIGALRM {si_signo=SIGALRM, si_code=SI_TIMER, si_timerid=0, si_overrun=0, si_int=0, si_ptr=NULL} ---sigreturn({mask=[]})= -1 EINTR (Přerušené volání systému)fallocate(19, FALLOC_FL_KEEP_SIZE, 0, 206057565)= -1 EINTR (Přerušené volání systému) --- SIGALRM {si_signo=SIGALRM, si_code=SI_TIMER, si_timerid=0, si_overrun=0, si_int=0, si_ptr=NULL} ---sigreturn({mask=[]})= -1 EINTR (Přerušené volání systému)fallocate(19, FALLOC_FL_KEEP_SIZE, 0, 206057565)= -1 EINTR (Přerušené volání systému) --- SIGALRM {si_signo=SIGALRM, si_code=SI_TIMER, si_timerid=0, si_overrun=0, si_int=0, si_ptr=NULL} ---sigreturn({mask=[]})= -1 EINTR (Přerušené volání systému)fallocate(19, FALLOC_FL_KEEP_SIZE, 0, 206057565)= -1 EINTR (Přerušené volání systému) --- SIGALRM {si_signo=SIGALRM, si_code=SI_TIMER, si_timerid=0, si_overrun=0, si_int=0, si_ptr=NULL} ---sigreturn({mask=[]})= -1 EINTR (Přerušené volání systému)Should there be
fatal_signal_pendinginstead ofsignal_pendingin theshmem_fallocateloop?Signed-off-by Mikulas Patocka <[email protected]>--- mm/shmem.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 13 lines
mm/shmem.c static long shmem_fallocate(struct file · line 3146
Quoted 10 lines
Index:
linux-2.6/mm/shmem.c
===================================================================mm/shmem.c +1
-1@@ -3143,7 +3143,7 @@ static long shmem_fallocate(struct file* Good, the fallocate(2) manpage permits EINTR: we may have * been interrupted because we are using up too much memory. */ -if (signal_pending (current)) +if (fatal_signal_pending (current))I think that's likely wrong and probably would cause regressions as there may be users relying on this?
I understand your concern about userspace regressions but is the EINTR behavior that useful? Sure, something can be relying on terminating fallocate(2) with any signal but since tmpfs is the only filesystem having this behavior, it is fair to say there are even higher chances some application will be surprised by this behavior when used on tmpfs as Mikulas was? So I wouldn't be that opposed to this change. *But* tmpfs has a comment explaining the signal_pending() check:
/*
* Good, the fallocate(2) manpage permits EINTR: we may have
* been interrupted because we are using up too much memory. */
Now I'd expect the signal to be fatal in this case but we definitely need to make sure this is the case if we want to consider changing the test. Hugh?
Honza
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR
Christian Brauner
maintainerQuoted 46 lines · Jan Kara
Hello,
Quoted 29 lines · Christian Brauner
Quoted 28 lines · Mikulas Patocka
I have a program that sets up a periodic timer with 10ms interval. When the program attempts to call fallocate on tmpfs, it goes into an infinite loop. fallocate takes longer than 10ms, so it gets interrupted by a signal and it returns EINTR. On EINTR, the fallocate call is restarted, going into the same loop again.
fallocate(19, FALLOC_FL_KEEP_SIZE, 0, 206057565)= -1 EINTR (Přerušené volání systému) --- SIGALRM {si_signo=SIGALRM, si_code=SI_TIMER, si_timerid=0, si_overrun=0, si_int=0, si_ptr=NULL} ---sigreturn({mask=[]})= -1 EINTR (Přerušené volání systému)fallocate(19, FALLOC_FL_KEEP_SIZE, 0, 206057565)= -1 EINTR (Přerušené volání systému) --- SIGALRM {si_signo=SIGALRM, si_code=SI_TIMER, si_timerid=0, si_overrun=0, si_int=0, si_ptr=NULL} ---sigreturn({mask=[]})= -1 EINTR (Přerušené volání systému)fallocate(19, FALLOC_FL_KEEP_SIZE, 0, 206057565)= -1 EINTR (Přerušené volání systému) --- SIGALRM {si_signo=SIGALRM, si_code=SI_TIMER, si_timerid=0, si_overrun=0, si_int=0, si_ptr=NULL} ---sigreturn({mask=[]})= -1 EINTR (Přerušené volání systému)fallocate(19, FALLOC_FL_KEEP_SIZE, 0, 206057565)= -1 EINTR (Přerušené volání systému) --- SIGALRM {si_signo=SIGALRM, si_code=SI_TIMER, si_timerid=0, si_overrun=0, si_int=0, si_ptr=NULL} ---sigreturn({mask=[]})= -1 EINTR (Přerušené volání systému)Should there be
fatal_signal_pendinginstead ofsignal_pendingin theshmem_fallocateloop?Signed-off-by Mikulas Patocka <[email protected]>--- mm/shmem.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)Quoted 13 lines
mm/shmem.c static long shmem_fallocate(struct file · line 3146
Quoted 10 lines
Index:
linux-2.6/mm/shmem.c
===================================================================mm/shmem.c +1
-1@@ -3143,7 +3143,7 @@ static long shmem_fallocate(struct file* Good, the fallocate(2) manpage permits EINTR: we may have * been interrupted because we are using up too much memory. */ -if (signal_pending (current)) +if (fatal_signal_pending (current))I think that's likely wrong and probably would cause regressions as there may be users relying on this?
Quoted 2 lines
I understand your concern about userspace regressions but is the EINTR behavior that useful? Sure, something can be relying on terminating
I don't know.
Quoted 1 line
fallocate(2) with any signal but since tmpfs is the only filesystem having
Hugetlbfs has the same logic.
Quoted 5 lines
this behavior, it is fair to say there are even higher chances some application will be surprised by this behavior when used on tmpfs as Mikulas was? So I wouldn't be that opposed to this change. *But* tmpfs has a comment explaining the
signal_pending()check:
Quoted 7 lines
/*
* Good, the fallocate(2) manpage permits EINTR: we may have
* been interrupted because we are using up too much memory. */Now I'd expect the signal to be fatal in this case but we definitely need to make sure this is the case if we want to consider changing the test.
Right now fallocate() is restartable. You could get EINTR and then retry. Changing this to fatal_signal_pending() would mean that this property is lost. The task will have to be wiped.
If this is only done for the sake of the OOM killer then we can probably try and change it. But then we'd need to also reflect that on the manpage.
Quoted 3 lines · Christian Brauner
Right now
fallocate()is restartable. You could get EINTR and then retry. Changing this tofatal_signal_pending()would mean that this property is lost. The task will have to be wiped.
People made much the same argument when I removed the nfs 'intr' mount option. It hasn't actually caused any issues that I've seen.
Quoted 3 lines
If this is only done for the sake of the OOM killer then we can probably try and change it. But then we'd need to also reflect that on the manpage.
It's part of POSIX, I wouldn't remove the manpage. I think it's a QoI issue; we should only check for fatal signals.