From nobody Fri Dec 19 15:00:25 2025 Received: from mail-wm1-f53.google.com (mail-wm1-f53.google.com [209.85.128.53]) (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 0A06F2FBE05 for ; Sat, 6 Dec 2025 13:20:11 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.53 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1765027214; cv=none; b=VCWfZ4Mq3kudcBZd247/U5wQwX0PFs+FsDO23mXIOqWN37zFt+vkrCzqh9OZx8yfSdKOrSmrYjf1r0R5yoEtHE7gpU3JOg29IdOLpdqLd74FKOyL/yk9N5GzemdWYFgLgE0bMUfRh+CHfpmhb5ZpKMB/Nfi22r5qhz/ipSQs79g= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1765027214; c=relaxed/simple; bh=f8MD+J6pw0T5jI4Ykh120KiB3sqKKm6Ab43gT1+WgQ4=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=GXKI5PwUmlRiDwAFeBade5h/8mMBiNc0hAe/gKhNZ4xsbz7Ovd0kmPpSsi09k2gZd0rjHI8S6s2NggABfL/txARPXaQnJLxC2KfLkmlUUWAuhkganKhoktiE7E2DPrYLCh8gXV/EjneP0npODa7788H1h8+QgO410siHxT2XtdI= 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=WA4o4Nwh; arc=none smtp.client-ip=209.85.128.53 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="WA4o4Nwh" Received: by mail-wm1-f53.google.com with SMTP id 5b1f17b1804b1-4779cc419b2so34163785e9.3 for ; Sat, 06 Dec 2025 05:20:11 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1765027210; x=1765632010; 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=1eBN/3RVqqipQQ2DO5GdrljyMWQyFdnr51hngBzFh80=; b=WA4o4NwhUu0oDqr7Ta4PZS2+BMLGwnxhwkN3tjmxZB8UMk9WszujnlXlwaKGMoVKfl Bd8GvIZPEwwYNTvqZr4teqFISQ51fbtbeknNSLOQw986sZxO+iJihSxI1trZJ6l6XKJs Ev3dULqZ2jEv6RCUu9qvTasb3a7SI10yMS3a2zJqlqH3kAOwObQtzPDwa+RyvlgXY00x 1nO/xw1A5s28Ycfta5Z0MkZ02XeFSAkSKfhxnH/RN7satWkILjZbsa07XVWnbGLKoLPx m62aQrJeV838+/Kaf5YKy78z/vJ/auJ/simgpy04K7gGqHIyEnDEpR0YrtLLocksDcqo iewg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1765027210; x=1765632010; 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=1eBN/3RVqqipQQ2DO5GdrljyMWQyFdnr51hngBzFh80=; b=mkKRmJVgh89vu+gUbSVtCC8TA0Z6CY2BKvmQ3Ni7K6fnMe2R/g0CibHzIE41GoUSqV XKBqbyeOFpDNtaj4mniSGx6m5ixJ/1rFByTxLCDonDveteENtOBz1X4Fdg8f4zJwht/2 AVMmpQVX8M5dINYnuRJ/GvFSRX7tZXNi4tBhWy1z1jxiaoClkOtfxG/A9tId7i8N9Q8f cBb6s4BGqwhS6hJVEKnzTMx5hOxRe6AXqHkacYGqYsKQm0CZkav6tZPFYi3BbO/LxfwA JA7hAprfen/iII+PNzsqLF630bOzIWphJwSt+I5B2dbrFhvpEQDRCXy6i45FbzIPWvhR 8WWA== X-Forwarded-Encrypted: i=1; AJvYcCX8u/OWkcAb3y+hcGHQ3WhPVmkIBjTAK4p9OOQSQ6QsT5zbf7sf2m17Srvth+YKVGbhT4pSWXSVEQothLE=@vger.kernel.org X-Gm-Message-State: AOJu0YzNYMDnJBUZ4/a09t8RKKxE0RVrAl4LbLU+EUzDdpsIppkADGfy 5SaOMlceUkBB1mAIitle+I397cJb+H9kni9pubqc/Kmaq+HFX+8nF1M/ X-Gm-Gg: ASbGncvPUHdirasg4PN/fXkB7WcZTUuyP6IUYZLKMZN0ej1Cy6FMG5qfIjheQPfgWVG 5xRpdXs4Q7g5uj0/1ixofHAaXttX7Hok5eLsf1WunzpPpkUi6MYgVqvVOOfwZp/Zyj49kaAB79y 82PeSGQjygEhNCe/oKwjw/wLQunZq9fXF8hGXE0hSnqcb9CQeEGreeyaVdLwbRafwo7mOSPLhoM b4hC82e2ZQ6g7OQHw8CReeQFTJBGPF1PbNoRRB4BFxBW5E17rduMQjPAgFttvZG/OsU7zvwi7aa BoN5DQaMt49QMtnK+jIBlQ1Ie6k2emjY3xO54ST+zbfkzPffeuhV2axeQJb//LeN1yb3j4tK2RF ZYbR8huTNzegxC+zZNUuqRRE9bg3bZLzZb6XW8d1Tw/hbJwbhGdilBoOoRlN+m2+Zqg5PL5tSHb xzfAmRPIuP4md5iMdt2ByitrwrwUUu4fKrKO0zTiEPTnjjO2jintKqZI2YHrA= X-Google-Smtp-Source: AGHT+IFZb5Z0/0DighBaa19FjmR9ROw1yxaOpWzjCzR6imaY0dN30sNYVUMBs3npCCcBE6q66Cd0oA== X-Received: by 2002:a7b:cd04:0:b0:479:3a88:de5f with SMTP id 5b1f17b1804b1-4793a88de98mr12441875e9.36.1765027210020; Sat, 06 Dec 2025 05:20:10 -0800 (PST) Received: from f.. (cst-prg-14-82.cust.vodafone.cz. [46.135.14.82]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-4792afd3b31sm110871215e9.0.2025.12.06.05.20.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 06 Dec 2025 05:20:09 -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 v3 2/2] pid: only take pidmap_lock once on alloc Date: Sat, 6 Dec 2025 14:19:55 +0100 Message-ID: <20251206131955.780557-3-mjguzik@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20251206131955.780557-1-mjguzik@gmail.com> References: <20251206131955.780557-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 changes. Reviewed-by: Oleg Nesterov Signed-off-by: Mateusz Guzik Tested-by: Tycho Andersen (AMD) --- kernel/pid.c | 134 +++++++++++++++++++++++++++++++++------------------ 1 file changed, 86 insertions(+), 48 deletions(-) diff --git a/kernel/pid.c b/kernel/pid.c index a31771bc89c1..ad4400a9f15f 100644 --- a/kernel/pid.c +++ b/kernel/pid.c @@ -159,65 +159,94 @@ 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 (unlikely(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) + if (unlikely(!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;) { + 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; + i--; + } + + /* + * 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, tid + 1, GFP_ATOMIC); /* * If ENOSPC is returned it means that the PID is - * alreay in use. Return EEXIST in that case. + * already in use. Return EEXIST in that case. */ if (nr =3D=3D -ENOSPC) nr =3D -EEXIST; @@ -235,19 +264,41 @@ 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 with GFP_ATOMIC. It may be the situation is salvage= able with + * GFP_KERNEL. But make sure to not loop indefinitely if preload did no= t help + * (the routine unfortunately returns void, so we have no idea if it go= t anywhere). + * + * The pidmap lock can be safely dropped and picked up as historically = pid allocation + * 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 +308,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 +327,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 +338,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