From nobody Fri Dec 19 20:52:25 2025 Received: from mail-ed1-f49.google.com (mail-ed1-f49.google.com [209.85.208.49]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 1DA8C2DF13B for ; Wed, 3 Dec 2025 09:29:04 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.208.49 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1764754148; cv=none; b=T23EjWg5kFjphZzXR43+nWytFQUeuskvLL4uVPd7352K6Bqip1X3m5uGY0iaEZYRByigpPgXYF++4YjHMP8Urnjgi43nEB/wh2tDvs1hd88iS4yoqwJYJKEYhu2sUzT+5psOahKZ8CJScdfUCMa3sI+FDL1MjKbt43LpVgXBotM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1764754148; c=relaxed/simple; bh=NCDZRGh4CNEjF8wzrz0/qmUmjmi/vLJ1iChHLMKHngw=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=UK4LKeiteTnkZStK/hYt3xtvXX0XrktNBd4cUbndJjWZqDQZozLY2pkNkmu8ul0STooKEe3olsbIKt4hmQbFP8pD2GGTg+9eFFBLRANfRl2FsJk86eokgg8h5gDmGAuXp+dUCA8L90EsPeoZTjYCt3ySnBJGJxJsl9Snd2EvzzA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=b0ThrO1r; arc=none smtp.client-ip=209.85.208.49 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="b0ThrO1r" Received: by mail-ed1-f49.google.com with SMTP id 4fb4d7f45d1cf-64074f01a6eso11506694a12.2 for ; Wed, 03 Dec 2025 01:29:04 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1764754143; x=1765358943; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=ighOjyvrf0/Zx4X8xYQ5PxpLMHcY6lBYspK5VjpofZs=; b=b0ThrO1rfmV3khTHJjE6ETpcLNYjsxZjuEhD4S/cdh6otCFgxVYCqmx3aA699b7BEf 3P134dZ9UgQh2TK96lVcoC3RqGGX0hOYXeZjd8xCmc0gu67ErrWy6lH3TizjptWI/kRF Mvu0wcubJvXI0U6TvBH8Z9JtP/manvZ8vdGS4fYlFi9kqLIm0lZhNt7IzYVOShx4aA0J M3SZ+8UNj9gxPLLsYMkQjxiuA5Rfmer9LGPzZqw7FtXSwRKvKBTGpxbONSemw6BIKNSB yMAFfXMMUKK/MdDDReXRJtMekWZIrJzXnvLfTE/EGiotLWSOQdgfndXn4HLzwYMQU090 s4hA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1764754143; x=1765358943; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=ighOjyvrf0/Zx4X8xYQ5PxpLMHcY6lBYspK5VjpofZs=; b=JUEnkzYi/oHqYEh1ZwU6Iqopo0aJXvLhCSWk4cXQ9ATsN/0JnZJSAq6K46b8amoGr9 IH7ug8emojUKm+mWxV3ocjSMYdCG8xbLpZ3G09FV9AkiWe9ebwfNAz7R++3B+Tlng/6z ABzzoNDJ0qiexI5ctFalz4/R4wxUtAQG0CPCRj+Enayj4qNU629zkjxD9P/RtMZWrUpl PiEPXKT8ELB2Rm8lFDKuOujiRk/IrZx30z//2EI2Xb7rbqp2YV9QP9tSLWsDAHc1oai4 BvYl3wOCfRJsyCYKoS+zp/TUMo+nBe8fezCJLHaEaNSDdAS3ePmAujYFBhHfCrvatG8o cGLA== X-Forwarded-Encrypted: i=1; AJvYcCXMKaU9cA5+uZ59yAGT7Df7yYyFATcZHtaPQFs5p3/OTAANHVNtWNrp5tFTBDYRfHkLP9ARVVPSVoX8QDQ=@vger.kernel.org X-Gm-Message-State: AOJu0Yy5djZF3nvsfcRKMUOfw7UBfJHZF+IKO7IeiH73n2DVVNet8C07 Th5hZEaZO2joFuHz3WfjhazfxaWT4vzbaSJaRB6CFQ5IHYZPuOGimzvM X-Gm-Gg: ASbGnctRvAhnttRVtteQGFzma1Q7Wlu/sKMD0kG85bdWMC4LbTKfKLlfpfrI3KONI6o sP7vBL1H3n+WMlrc6zBQxJZPDmF+mCL4n2zFaEgi+PdZKkKojum0L40rO0tYfCVo+N5fRXQjI7F nDPeHWKwpNVIGskY99slc4nthrVTXzhbwGLXsLCA+DCvObdqUeOy95xYLarZgSEMxYG/v4M7V9I taPagMMiCiPyPbo2ZRQ4OmVJ4Cklb+Mm4y2yFTod1sTsXxMBcxiqe3WOHyRnXnvfySSzBX1c37V 8Q4Naga2B4k0OJkBQh4kSZ3Y6Do73UEHUfJ6YNSZ6kugvY5oOCsWtCo+795jH6L4L1Cp99KCZSF 8Lpm+C42g+1skyXB+44bHmNJQGszdROZyAXlDm8Pd9fRcJtEHjjZ8a0zEOoqrEuLE9XHK5kswTX v7/k8FlAZCdt4IsrJ9D5ScH59Z8dCZLAoStNhgM/LFwxSsgEiYeEJEr4/O7tNsb4hIvdXjkw== X-Google-Smtp-Source: AGHT+IG/yyPdPy6ledU410KsgIsElOobmCF5nw6rgVbZS+p0uJGh6qPsi6bQnuwzwkvntLO05gO2GQ== X-Received: by 2002:a05:6402:1d53:b0:640:9c99:bfac with SMTP id 4fb4d7f45d1cf-6479c47c53emr1439101a12.13.1764754142831; Wed, 03 Dec 2025 01:29:02 -0800 (PST) Received: from f.. (cst-prg-14-82.cust.vodafone.cz. [46.135.14.82]) by smtp.gmail.com with ESMTPSA id 4fb4d7f45d1cf-64750a90d14sm17859981a12.10.2025.12.03.01.29.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 03 Dec 2025 01:29:02 -0800 (PST) From: Mateusz Guzik To: oleg@redhat.com Cc: brauner@kernel.org, linux-kernel@vger.kernel.org, akpm@linux-foundation.org, linux-mm@kvack.org, willy@infradead.org, Mateusz Guzik Subject: [PATCH v2 1/2] ns: pad refcount Date: Wed, 3 Dec 2025 10:28:50 +0100 Message-ID: <20251203092851.287617-2-mjguzik@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20251203092851.287617-1-mjguzik@gmail.com> References: <20251203092851.287617-1-mjguzik@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Note no effort is made to make sure structs embedding the namespace are themselves aligned, so this is not guaranteed to eliminate cacheline bouncing due to refcount management. Signed-off-by: Mateusz Guzik --- include/linux/ns/ns_common_types.h | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/include/linux/ns/ns_common_types.h b/include/linux/ns/ns_commo= n_types.h index b332b019b29c..0014fbc1c626 100644 --- a/include/linux/ns/ns_common_types.h +++ b/include/linux/ns/ns_common_types.h @@ -108,11 +108,13 @@ extern const struct proc_ns_operations utsns_operatio= ns; * @ns_tree: namespace tree nodes and active reference count */ struct ns_common { + struct { + refcount_t __ns_ref; /* do not use directly */ + } ____cacheline_aligned_in_smp; u32 ns_type; struct dentry *stashed; const struct proc_ns_operations *ops; unsigned int inum; - refcount_t __ns_ref; /* do not use directly */ union { struct ns_tree; struct rcu_head ns_rcu; --=20 2.48.1 From nobody Fri Dec 19 20:52:25 2025 Received: from mail-ed1-f52.google.com (mail-ed1-f52.google.com [209.85.208.52]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 515962DE70A for ; Wed, 3 Dec 2025 09:29:06 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.208.52 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1764754150; cv=none; b=LoaOXHij+JMRJf+chSPAXF56ajgW6zYqgvBSINJClT32l/+NcO0TRXRdmK5ekDoAAYUfBXwVbBfl/9PhQi23nR5D5Kezd/HWq0OfclC1/jg08HhOhaa42ac0aYgN+PVvnxM75U0gALWVmYMzjyYv+VrFotDKpijx7aJmxyTAhpU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1764754150; c=relaxed/simple; bh=KpXKLWSKUlXNXXl9ZKVbTZMEsjVR7loFj6kuc9KjEQk=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=mku4JNWp97xQLjhjwddZUCDT431cw++Rmm/1S18yJ9zsa13cJJvF6x1c7dz+XiMRHBPNG8Fmj5hBk/v6BA0p4fZ9gCzwfu+IVBO7Wrp2pGAPI3dmT7Svb0YyWuQ/Rfo1NSgHmVnm76rwDT1ykn5/1D6HGsC6dt8dI04Jv7YN7zw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=F19x8Q5Y; arc=none smtp.client-ip=209.85.208.52 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="F19x8Q5Y" Received: by mail-ed1-f52.google.com with SMTP id 4fb4d7f45d1cf-64080ccf749so9544012a12.2 for ; Wed, 03 Dec 2025 01:29:06 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1764754145; x=1765358945; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=QdTv6wkSHwAMSOll3YwLh5RVP5GPGXPQVcJa4xCpXQ8=; b=F19x8Q5YILXrf5Vo2IAltMv0iLZmWHdTkY+Fufx9DKNCRw7pNSAoALyypGKUoBg+NZ e8KR2GdvXotThh1d+DryURyY0+0X7Cc3+cs2WmhD0oiv2YL6xRFBZtb0yU6nWjI6pQdR U0oKkQgNL9q1Lt83ocKmd2F0vMyQvWCcY8eZ1pzKQSqZDIajMTblP9J1JUvdwkXcNqkx t8ifv+FvACHCVr3qHpDd8WwFbJ/5oBR+ua5Bbj3FD2n4G1jrVfTIBCpSXvA3G363nAZM rdmAL3aWLeTSsfOdUxI73N0/3DTObVzcDQbuCLFjZE/71b0Upk3+bfmcFAJAOKhoqS1L nBBw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1764754145; x=1765358945; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=QdTv6wkSHwAMSOll3YwLh5RVP5GPGXPQVcJa4xCpXQ8=; b=cC2+wQ5tkzOwzP1Y7vE/oUs96zqHr7qMN1v2bM/yL8iJOQtivnbeU02yx4iXEvkVM5 W8uXvpxW3fhO6tnGIK5XodgrG6oIC+Qvz7BE3O91OBR6fStYkLi50mS2lNhiAQD44Dcz zf3ZVuCAPrB9S5tihK1Frt8CG0we9AWTiEiZan6e8vnr81c7LyOTgTlQwNLuOTABhI/4 MojZ1UsYpJ0oay+28b6N3ELdlJ4Q8745Knm5OEX1uK80VjDX1YO85NkaHtr+1Su3OdL4 KRYbefPOZHJyB+8bt6Hc0ID5qf3E0ZbRkXqeSbwCWlv1UM6AKw31D+VN+Ar9Pf4ZwAtX uiOQ== X-Forwarded-Encrypted: i=1; AJvYcCV7VwvfUHhzvVtqZQP0ZVyo9T1F49x8ZL76V7XomFOhKA4+LswPr+/HYNNyqRNnRjvJ3WxXzCtY0018VRw=@vger.kernel.org X-Gm-Message-State: AOJu0YztrO1kN793VZEytdLn0oGiNWI6iGaSqqlXwWUM1Zz2/npTfGJp S6vKNO5DWLN6J0DAe62fJPoC6uwVnjeX40flfOw3Hs7qZMRwGSqo3O3J X-Gm-Gg: ASbGncv0JGJPkjhyWWirOFjE0lMjoDcKr1myJYw5jM+jV//a+iFfGDYB5T77OMMApkh edEv9Q3z/Z+7a21JkoCdxFy0mnxW3A0qnYSn/Enyl+15e4OrJImhfNjYPlictRtqTLTBJS/7FLf sdOpYXExoCcbF7oS0swF6qeSPk02qSnshxElxaATljzsY7F/Swro7+wkst8L7oCkhFf00wMy6rc dNhgZnGryvdfk3K/nKM8gkjjLAY7EfMp9a+5+2s0Jtk40VjnI5ZQ6APFzoEnPn/ZdrMPdqNJM3j J1pyCz4WhqUD5Huxck8dAjrClVOZlscpkQqYf3ycnnNohacluNU4p0zAG5BDEnYi/BTeJZXvoJD b9l9TA9Vcg1WO7cgGjZeS9KuCCc4E/0nQg9jI2k0OPeOFUcshbPC2t4b1PHGsjzfrOifTzMJe3D xhSUT4OVTPM5YHyEKO6BLhp+nsK5IQ/4R/D/5Dskq1IjiELTv8imy13yeh62Q= X-Google-Smtp-Source: AGHT+IGafodK6wrQHJ3egSmEyL5fp27hY/MK2pNcmMmGZDlnu9hdJtMQDjyaHTeG4cmC0C2KG74s0Q== X-Received: by 2002:a05:6402:1468:b0:647:7b37:2bc5 with SMTP id 4fb4d7f45d1cf-6479c4b2a3bmr1126107a12.31.1764754145323; Wed, 03 Dec 2025 01:29:05 -0800 (PST) Received: from f.. (cst-prg-14-82.cust.vodafone.cz. [46.135.14.82]) by smtp.gmail.com with ESMTPSA id 4fb4d7f45d1cf-64750a90d14sm17859981a12.10.2025.12.03.01.29.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 03 Dec 2025 01:29:04 -0800 (PST) From: Mateusz Guzik To: oleg@redhat.com Cc: brauner@kernel.org, linux-kernel@vger.kernel.org, akpm@linux-foundation.org, linux-mm@kvack.org, willy@infradead.org, Mateusz Guzik Subject: [PATCH v2 2/2] pid: only take pidmap_lock once on alloc Date: Wed, 3 Dec 2025 10:28:51 +0100 Message-ID: <20251203092851.287617-3-mjguzik@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20251203092851.287617-1-mjguzik@gmail.com> References: <20251203092851.287617-1-mjguzik@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" When spawning and killing threads in separate processes in parallel the primary bottleneck on the stock kernel is pidmap_lock, largely because of a back-to-back acquire in the common case. This aspect is fixed with the patch. Performance improvement varies between reboots. When benchmarking with 20 processes creating and killing threads in a loop, the unpatched baseline hovers around 465k ops/s, while patched is anything between ~510k ops/s and ~560k depending on false-sharing (which I only minimally sanitized). So this is at least 10% if you are unlucky. The change also facilitated some cosmetic fixes. It has an unintentional side effect of no longer issuing spurious idr_preload() around idr_replace(). Signed-off-by: Mateusz Guzik Reviewed-by: Oleg Nesterov --- kernel/pid.c | 131 +++++++++++++++++++++++++++++++++------------------ 1 file changed, 85 insertions(+), 46 deletions(-) diff --git a/kernel/pid.c b/kernel/pid.c index a31771bc89c1..f45ae56db7da 100644 --- a/kernel/pid.c +++ b/kernel/pid.c @@ -159,58 +159,86 @@ void free_pids(struct pid **pids) free_pid(pids[tmp]); } =20 -struct pid *alloc_pid(struct pid_namespace *ns, pid_t *set_tid, - size_t set_tid_size) +struct pid *alloc_pid(struct pid_namespace *ns, pid_t *arg_set_tid, + size_t arg_set_tid_size) { + int set_tid[MAX_PID_NS_LEVEL + 1] =3D {}; + int pid_max[MAX_PID_NS_LEVEL + 1] =3D {}; struct pid *pid; enum pid_type type; int i, nr; struct pid_namespace *tmp; struct upid *upid; int retval =3D -ENOMEM; + bool retried_preload; =20 /* - * set_tid_size contains the size of the set_tid array. Starting at + * arg_set_tid_size contains the size of the arg_set_tid array. Starting = at * the most nested currently active PID namespace it tells alloc_pid() * which PID to set for a process in that most nested PID namespace - * up to set_tid_size PID namespaces. It does not have to set the PID - * for a process in all nested PID namespaces but set_tid_size must + * up to arg_set_tid_size PID namespaces. It does not have to set the PID + * for a process in all nested PID namespaces but arg_set_tid_size must * never be greater than the current ns->level + 1. */ - if (set_tid_size > ns->level + 1) + if (arg_set_tid_size > ns->level + 1) return ERR_PTR(-EINVAL); =20 + /* + * Prep before we take locks: + * + * 1. allocate and fill in pid struct + */ pid =3D kmem_cache_alloc(ns->pid_cachep, GFP_KERNEL); if (!pid) return ERR_PTR(retval); =20 - tmp =3D ns; + get_pid_ns(ns); pid->level =3D ns->level; + refcount_set(&pid->count, 1); + spin_lock_init(&pid->lock); + for (type =3D 0; type < PIDTYPE_MAX; ++type) + INIT_HLIST_HEAD(&pid->tasks[type]); + init_waitqueue_head(&pid->wait_pidfd); + INIT_HLIST_HEAD(&pid->inodes); =20 - for (i =3D ns->level; i >=3D 0; i--) { - int tid =3D 0; - int pid_max =3D READ_ONCE(tmp->pid_max); + /* + * 2. perm check checkpoint_restore_ns_capable() + * + * This stores found pid_max to make sure the used value is the same shou= ld + * later code need it. + */ + for (tmp =3D ns, i =3D ns->level; i >=3D 0; i--) { + pid_max[ns->level - i] =3D READ_ONCE(tmp->pid_max); =20 - if (set_tid_size) { - tid =3D set_tid[ns->level - i]; + if (arg_set_tid_size) { + int tid =3D set_tid[ns->level - i] =3D arg_set_tid[ns->level - i]; =20 retval =3D -EINVAL; - if (tid < 1 || tid >=3D pid_max) - goto out_free; + if (tid < 1 || tid >=3D pid_max[ns->level - i]) + goto out_abort; /* * Also fail if a PID !=3D 1 is requested and * no PID 1 exists. */ if (tid !=3D 1 && !tmp->child_reaper) - goto out_free; + goto out_abort; retval =3D -EPERM; if (!checkpoint_restore_ns_capable(tmp->user_ns)) - goto out_free; - set_tid_size--; + goto out_abort; + arg_set_tid_size--; } =20 - idr_preload(GFP_KERNEL); - spin_lock(&pidmap_lock); + tmp =3D tmp->parent; + } + + /* + * Prep is done, id allocation goes here: + */ + retried_preload =3D false; + idr_preload(GFP_KERNEL); + spin_lock(&pidmap_lock); + for (tmp =3D ns, i =3D ns->level; i >=3D 0;) { + int tid =3D set_tid[ns->level - i]; =20 if (tid) { nr =3D idr_alloc(&tmp->idr, NULL, tid, @@ -220,6 +248,7 @@ struct pid *alloc_pid(struct pid_namespace *ns, pid_t *= set_tid, * alreay in use. Return EEXIST in that case. */ if (nr =3D=3D -ENOSPC) + nr =3D -EEXIST; } else { int pid_min =3D 1; @@ -235,19 +264,42 @@ struct pid *alloc_pid(struct pid_namespace *ns, pid_t= *set_tid, * a partially initialized PID (see below). */ nr =3D idr_alloc_cyclic(&tmp->idr, NULL, pid_min, - pid_max, GFP_ATOMIC); + pid_max[ns->level - i], GFP_ATOMIC); + if (nr =3D=3D -ENOSPC) + nr =3D -EAGAIN; } - spin_unlock(&pidmap_lock); - idr_preload_end(); =20 - if (nr < 0) { - retval =3D (nr =3D=3D -ENOSPC) ? -EAGAIN : nr; + if (unlikely(nr < 0)) { + /* + * Preload more memory if idr_alloc{,cyclic} failed with -ENOMEM. + * + * The IDR API only allows us to preload memory for one call, while we = may end + * up doing several under pidmap_lock with GFP_ATOMIC. The situation ma= y be + * salvageable with GFP_KERNEL. But make sure to not loop indefinitely = if preload + * did not help (the routine unfortunately returns void, so we have no = idea + * if it got anywhere). + * + * The lock can be safely dropped and picked up as historically pid all= ocation + * for different namespaces was *not* atomic -- we try to hold on to it= the + * entire time only for performance reasons. + */ + if (nr =3D=3D -ENOMEM && !retried_preload) { + spin_unlock(&pidmap_lock); + idr_preload_end(); + retried_preload =3D true; + idr_preload(GFP_KERNEL); + spin_lock(&pidmap_lock); + continue; + } + retval =3D nr; goto out_free; } =20 pid->numbers[i].nr =3D nr; pid->numbers[i].ns =3D tmp; tmp =3D tmp->parent; + i--; + retried_preload =3D false; } =20 /* @@ -257,25 +309,15 @@ struct pid *alloc_pid(struct pid_namespace *ns, pid_t= *set_tid, * is what we have exposed to userspace for a long time and it is * documented behavior for pid namespaces. So we can't easily * change it even if there were an error code better suited. + * + * This can't be done earlier because we need to preserve other + * error conditions. */ retval =3D -ENOMEM; - - get_pid_ns(ns); - refcount_set(&pid->count, 1); - spin_lock_init(&pid->lock); - for (type =3D 0; type < PIDTYPE_MAX; ++type) - INIT_HLIST_HEAD(&pid->tasks[type]); - - init_waitqueue_head(&pid->wait_pidfd); - INIT_HLIST_HEAD(&pid->inodes); - - upid =3D pid->numbers + ns->level; - idr_preload(GFP_KERNEL); - spin_lock(&pidmap_lock); - if (!(ns->pid_allocated & PIDNS_ADDING)) - goto out_unlock; + if (unlikely(!(ns->pid_allocated & PIDNS_ADDING))) + goto out_free; pidfs_add_pid(pid); - for ( ; upid >=3D pid->numbers; --upid) { + for (upid =3D pid->numbers + ns->level; upid >=3D pid->numbers; --upid) { /* Make the PID visible to find_pid_ns. */ idr_replace(&upid->ns->idr, pid, upid->nr); upid->ns->pid_allocated++; @@ -286,13 +328,7 @@ struct pid *alloc_pid(struct pid_namespace *ns, pid_t = *set_tid, =20 return pid; =20 -out_unlock: - spin_unlock(&pidmap_lock); - idr_preload_end(); - put_pid_ns(ns); - out_free: - spin_lock(&pidmap_lock); while (++i <=3D ns->level) { upid =3D pid->numbers + i; idr_remove(&upid->ns->idr, upid->nr); @@ -303,7 +339,10 @@ struct pid *alloc_pid(struct pid_namespace *ns, pid_t = *set_tid, idr_set_cursor(&ns->idr, 0); =20 spin_unlock(&pidmap_lock); + idr_preload_end(); =20 +out_abort: + put_pid_ns(ns); kmem_cache_free(ns->pid_cachep, pid); return ERR_PTR(retval); } --=20 2.48.1