linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] fork: report pid exhaustion correctly
@ 2018-09-03 11:10 ktsanaktsidis
  2018-09-03 23:22 ` Andrew Morton
  0 siblings, 1 reply; 3+ messages in thread
From: ktsanaktsidis @ 2018-09-03 11:10 UTC (permalink / raw)
  Cc: akpm, linux-kernel, trivial, KJ Tsanaktsidis

From: KJ Tsanaktsidis <ktsanaktsidis@zendesk.com>

Make the clone and fork syscalls return EAGAIN when the limit on the
number of pids /proc/sys/kernel/pid_max is exceeded.

Currently, when the pid_max limit is exceeded, the kernel will return
ENOSPC from the fork and clone syscalls. This is contrary to the
documented behaviour, which explicitly calls out the pid_max case as one
where EAGAIN should be returned. It also leads to really confusing error
messages in userspace programs which will complain about a lack of disk
space when they fail to create processes/threads for this reason.

This error is being returned because the alloc_pid function uses the idr
api to find a new pid; when there are none available, idr_alloc_cyclic
is returns -ENOSPC, and this is being propagated back into userspace.

This behaviour has been broken before, and was explicitly fixed in
commit 35f71bc0a09a ("fork: report pid reservation failure properly"),
so I think -EAGAIN is definitely the right thing to return in this case.
The current behaviour change dates from commit 95846ecf9dac ("pid:
replace pid bitmap implementation with IDR AIP") and was I believe
unintentional.

This patch has no impact on the case where allocating a pid fails
because the child reaper for the namespace is dead; that case will still
return -ENOMEM.

Fixes: 95846ecf9dac ("pid: replace pid bitmap implementation with IDR AIP")
Signed-off-by: KJ Tsanaktsidis <ktsanaktsidis@zendesk.com>
---
 kernel/pid.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/kernel/pid.c b/kernel/pid.c
index de1cfc4f75a2..cdf63e53a014 100644
--- a/kernel/pid.c
+++ b/kernel/pid.c
@@ -195,7 +195,7 @@ struct pid *alloc_pid(struct pid_namespace *ns)
 		idr_preload_end();
 
 		if (nr < 0) {
-			retval = nr;
+			retval = (nr == -ENOSPC) ? -EAGAIN : nr;
 			goto out_free;
 		}
 
-- 
2.17.1


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH] fork: report pid exhaustion correctly
  2018-09-03 11:10 [PATCH] fork: report pid exhaustion correctly ktsanaktsidis
@ 2018-09-03 23:22 ` Andrew Morton
  2018-09-03 23:30   ` KJ Tsanaktsidis
  0 siblings, 1 reply; 3+ messages in thread
From: Andrew Morton @ 2018-09-03 23:22 UTC (permalink / raw)
  To: ktsanaktsidis; +Cc: linux-kernel, trivial, Michal Hocko, Oleg Nesterov

On Mon,  3 Sep 2018 04:10:16 -0700 ktsanaktsidis@zendesk.com wrote:

> From: KJ Tsanaktsidis <ktsanaktsidis@zendesk.com>
> 
> Make the clone and fork syscalls return EAGAIN when the limit on the
> number of pids /proc/sys/kernel/pid_max is exceeded.
> 
> Currently, when the pid_max limit is exceeded, the kernel will return
> ENOSPC from the fork and clone syscalls. This is contrary to the
> documented behaviour, which explicitly calls out the pid_max case as one
> where EAGAIN should be returned. It also leads to really confusing error
> messages in userspace programs which will complain about a lack of disk
> space when they fail to create processes/threads for this reason.
> 
> This error is being returned because the alloc_pid function uses the idr
> api to find a new pid; when there are none available, idr_alloc_cyclic
> is returns -ENOSPC, and this is being propagated back into userspace.
> 
> This behaviour has been broken before, and was explicitly fixed in
> commit 35f71bc0a09a ("fork: report pid reservation failure properly"),
> so I think -EAGAIN is definitely the right thing to return in this case.
> The current behaviour change dates from commit 95846ecf9dac ("pid:
> replace pid bitmap implementation with IDR AIP") and was I believe
> unintentional.
> 
> This patch has no impact on the case where allocating a pid fails
> because the child reaper for the namespace is dead; that case will still
> return -ENOMEM.

Thanks.  First ever kernel patch?  It was a damn good one!

> Fixes: 95846ecf9dac ("pid: replace pid bitmap implementation with IDR AIP")
> Signed-off-by: KJ Tsanaktsidis <ktsanaktsidis@zendesk.com>

I'll add cc:stable here so the fix gets backported into earlier kernels


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] fork: report pid exhaustion correctly
  2018-09-03 23:22 ` Andrew Morton
@ 2018-09-03 23:30   ` KJ Tsanaktsidis
  0 siblings, 0 replies; 3+ messages in thread
From: KJ Tsanaktsidis @ 2018-09-03 23:30 UTC (permalink / raw)
  To: akpm; +Cc: linux-kernel, trivial, mhocko, oleg

Thanks :) yeah, first kernel patch. I got pretty annoyed by how long I
spent trying to figure
out why my database was reporting disk space errors on pthread_create,
and when I
finally worked out what was going on sending a patch to fix it was
somewhat therapeutic!

On Tue, Sep 4, 2018 at 9:22 AM Andrew Morton <akpm@linux-foundation.org> wrote:
>
> On Mon,  3 Sep 2018 04:10:16 -0700 ktsanaktsidis@zendesk.com wrote:
>
> > From: KJ Tsanaktsidis <ktsanaktsidis@zendesk.com>
> >
> > Make the clone and fork syscalls return EAGAIN when the limit on the
> > number of pids /proc/sys/kernel/pid_max is exceeded.
> >
> > Currently, when the pid_max limit is exceeded, the kernel will return
> > ENOSPC from the fork and clone syscalls. This is contrary to the
> > documented behaviour, which explicitly calls out the pid_max case as one
> > where EAGAIN should be returned. It also leads to really confusing error
> > messages in userspace programs which will complain about a lack of disk
> > space when they fail to create processes/threads for this reason.
> >
> > This error is being returned because the alloc_pid function uses the idr
> > api to find a new pid; when there are none available, idr_alloc_cyclic
> > is returns -ENOSPC, and this is being propagated back into userspace.
> >
> > This behaviour has been broken before, and was explicitly fixed in
> > commit 35f71bc0a09a ("fork: report pid reservation failure properly"),
> > so I think -EAGAIN is definitely the right thing to return in this case.
> > The current behaviour change dates from commit 95846ecf9dac ("pid:
> > replace pid bitmap implementation with IDR AIP") and was I believe
> > unintentional.
> >
> > This patch has no impact on the case where allocating a pid fails
> > because the child reaper for the namespace is dead; that case will still
> > return -ENOMEM.
>
> Thanks.  First ever kernel patch?  It was a damn good one!
>
> > Fixes: 95846ecf9dac ("pid: replace pid bitmap implementation with IDR AIP")
> > Signed-off-by: KJ Tsanaktsidis <ktsanaktsidis@zendesk.com>
>
> I'll add cc:stable here so the fix gets backported into earlier kernels
>


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2018-09-03 23:31 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-09-03 11:10 [PATCH] fork: report pid exhaustion correctly ktsanaktsidis
2018-09-03 23:22 ` Andrew Morton
2018-09-03 23:30   ` KJ Tsanaktsidis

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).